-
Notifications
You must be signed in to change notification settings - Fork 2
/
properties.py
197 lines (156 loc) · 6.27 KB
/
properties.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/python
import os
import sys
import math
import re
import colorsys
import naive_parser
import config
def colourDistance(hsv1, hsv2):
hueDistance = min(abs(hsv2[0] - hsv1[0]), 1 - abs(hsv2[0] - hsv1[0]))
satDistance = abs(hsv2[1] - hsv1[1])
valDistance = abs(hsv2[2] - hsv1[2])
return math.sqrt(hueDistance * hueDistance + satDistance * satDistance + valDistance * valDistance)
def getColours():
hoi4ColourData = naive_parser.ParseSaveFile(config.Config().getModdedHoi4File("common/countries/colors.txt"))
stellarisGreyData = {
"grey": [0.65, 0.05, 0.35],
"dark_grey": [0.65, 0.05, 0.22],
"black": [0.5, 0.3, 0.05]
}
stellarisColourData = {
"grey": [0.65, 0.05, 0.35],
"dark_grey": [0.65, 0.05, 0.22],
"black": [0.5, 0.3, 0.05],
"dark_brown": [0.07, 0.6, 0.23],
"brown": [0.07, 0.6, 0.4],
"beige": [0.1, 0.4, 0.6],
"yellow": [0.11, 0.8, 0.8],
"light_orange": [0.09, 1.0, 0.8],
"orange": [0.06, 0.9, 0.7],
"red_orange": [0.01, 0.75, 0.7],
"red": [0.0, 0.95, 0.5],
"burgundy": [0.95, 0.8, 0.35],
"pink": [0.88, 0.61, 0.5],
"purple": [0.74, 0.65, 0.61],
"dark_purple": [0.74, 0.71, 0.37],
"indigo": [0.71, 0.85, 0.5],
"dark_blue": [0.64, 0.85, 0.45],
"blue": [0.64, 0.7, 0.6],
"light_blue": [0.6, 0.6, 0.7],
"turquoise": [0.49, 0.6, 0.6],
"dark_teal": [0.5, 0.6, 0.3],
"teal": [0.42, 0.6, 0.5],
"light_green": [0.35, 0.5, 0.60],
"green": [0.32, 0.6, 0.40],
"dark_green": [0.33, 0.6, 0.27],
}
hsvSet = {}
nameSet = {}
for tag in hoi4ColourData:
colour = naive_parser.drill(hoi4ColourData, tag, "color", "")
rgb = ("." not in colour)
colour = [float(x) for x in colour.replace(" ", " ").split(" ")]
if rgb:
colour = list(colorsys.rgb_to_hsv(*colour))
colour[2] = colour[2] / 255
hsvSet[tag] = colour
minDist = 9999
bestColour = "red"
if colour[1] < 0.2:
colourData = stellarisGreyData
else:
colourData = stellarisColourData
for stellarisColour in colourData:
stellarisHsv = colourData[stellarisColour]
newDist = colourDistance(colour, stellarisHsv)
if newDist < minDist:
minDist = newDist
bestColour = stellarisColour
nameSet[tag] = bestColour
return nameSet
def getStates():
stateMap = {}
statepath = config.Config().getModdedHoi4File("history/states/")
for filename in os.listdir(statepath):
stateData = open(statepath + filename).read()
stateData = stateData.replace("=\n{", "={")
stateData = stateData.replace("=\n\t{", "={")
stateData = stateData.replace("=\n\t\t{", "={")
stateData = stateData.replace("=\n\t\t\t{", "={")
stateData = stateData.replace("=\n\t\t\t\t{", "={")
stateData = re.sub(r"#[^\n]*?\n", r"\n", stateData)
if not stateData:
continue
state = naive_parser.ParseSaveData(stateData)
if 0 == len(state.keys()):
print("WARNING: \"" + statepath + filename + "\" could not be parsed. Skipping.")
continue
stateId = int(naive_parser.drill(state, "state", "id"))
provinces = naive_parser.drill(state, "state", "provinces", "").split(" ")
provinceIds = []
for province in provinces:
if not province:
continue
provinceIds.append(int(province))
for province in provinceIds:
stateMap[province] = stateId
return stateMap
def getClimates():
climateMap = {}
stateMap = getStates()
strategicRegionsPath = config.Config().getModdedHoi4File("map/strategicregions/")
for filename in os.listdir(strategicRegionsPath):
climateData = naive_parser.ParseSaveFile(strategicRegionsPath + filename)
provinces = naive_parser.drill(climateData, "strategic_region", "provinces", "").split(" ")
periodses = naive_parser.drill(climateData, "strategic_region", "weather")
if "period" not in periodses:
continue
periods = periodses["period"]
yearTemperatures = []
yearRain = []
yearSnow = []
yearSand = []
if len(periods) == 0:
continue
for period in periods:
temperatureRange = naive_parser.drill(period, "temperature", "").split(" ")
maxTemp = temperatureRange[1].replace("\"", "")
minTemp = temperatureRange[0].replace("\"", "")
temperature = float(maxTemp) - float(minTemp)
yearTemperatures.append(temperature)
lightRain = naive_parser.unquote(naive_parser.drill(period, "rain_light"))
heavyRain = naive_parser.unquote(naive_parser.drill(period, "rain_heavy"))
snow = naive_parser.unquote(naive_parser.drill(period, "snow"))
blizzard = naive_parser.unquote(naive_parser.drill(period, "blizzard"))
sandstorm = naive_parser.unquote(naive_parser.drill(period, "sandstorm"))
yearRain.append(float(lightRain) + float(heavyRain) * 2)
yearSnow.append(float(snow) + float(blizzard) * 3)
yearSand.append(float(sandstorm))
averageTemperature = sum(yearTemperatures) / len(yearTemperatures)
averageRain = sum(yearRain) / len(yearRain)
averageSnow = sum(yearSnow) / len(yearSnow)
averageSand = sum(yearSand) / len(yearSand)
climate = "pc_arid"
if averageSand > 0.05:
climate = "pc_desert"
elif averageSnow > 0.3:
climate = "pc_arctic"
elif averageSnow > 0.1:
climate = "pc_tundra"
elif averageTemperature > 10.0:
climate = "pc_arid"
elif averageTemperature < 4.7:
climate = "pc_savannah"
else:
climate = "pc_alpine"
for province in provinces:
if not province:
continue
provinceId = int(province)
if provinceId in stateMap:
stateId = stateMap[provinceId]
climateMap[stateId] = climate
return climateMap
if __name__ == "__main__":
c = getStates()