|
1 |
| -# This will need to be updated. Currently for 1.30.2 |
2 |
| -from typing import Dict, List, Optional, Tuple, Union |
| 1 | +from typing import Dict, Optional, Tuple, Union |
| 2 | +import yaml |
3 | 3 |
|
4 | 4 | from PIL import Image, ImageDraw
|
| 5 | +from os import listdir, path |
5 | 6 |
|
6 | 7 |
|
7 | 8 | def country(text: str, mod: str = "vanilla") -> Optional[str]:
|
@@ -72,69 +73,40 @@ def tagToName(tag: str, mod: str = "vanilla") -> Optional[str]:
|
72 | 73 | return None
|
73 | 74 |
|
74 | 75 |
|
75 |
| -def province(id: Union[str, int], mod: str = "vanilla") -> Optional[Tuple[float, float]]: |
| 76 | +# LOAD PROVINCE LOCATION DATA |
| 77 | +# Even if not __main__ |
| 78 | +province_locations: Dict[str, Dict[int, Tuple[float, float]]] = {} |
| 79 | +for p in listdir("resources"): |
| 80 | + filepath = path.join("resources", p, "positions.yml") |
| 81 | + if path.isfile(filepath): |
| 82 | + province_locations_file = open(filepath, "r") |
| 83 | + province_locations[p] = yaml.load( |
| 84 | + province_locations_file, yaml.CLoader) |
| 85 | + province_locations_file.close() |
| 86 | + |
| 87 | + |
| 88 | +def province(id: Union[str, int], mod: str = "vanilla") -> Tuple[float, float]: |
76 | 89 | """
|
77 | 90 | Gets the location of a province on a screenshot map.
|
78 | 91 |
|
79 | 92 | Returns a tuple of floats, (x, y).
|
80 | 93 | """
|
81 |
| - # Read file |
82 |
| - srcFile = open(f"resources/{mod}/positions.txt", "r", encoding="cp1252") |
83 |
| - """ |
84 |
| - Format of the file: |
85 |
| - 1={ |
86 |
| - position={ |
87 |
| - 3085.000 1723.000 3086.000 1730.000 3084.500 1729.000 3095.000 1724.500 3082.000 1730.000 3080.000 1736.000 0.000 0.000 |
88 |
| - } |
89 |
| - rotation={ |
90 |
| - 0.000 0.000 0.087 -0.698 0.000 0.000 0.000 |
91 |
| - } |
92 |
| - height={ |
93 |
| - 0.000 0.000 1.000 0.000 0.000 0.000 0.000 |
94 |
| - } |
95 |
| - } |
96 |
| - |
97 |
| - So we want the 3085.000 1723.000 from the 1={ because the first two are the location of the city in the province |
98 |
| - """ |
99 |
| - beyond = 0 |
100 |
| - for line in srcFile: |
101 |
| - if beyond == 2: # Two after the province, this is the line with the position. |
102 |
| - vals = line.split() |
103 |
| - # Need to subtract the y value because the position starts from the bottom rather than the top like images |
104 |
| - return (float(vals[0]), 2048-float(vals[1])) |
105 |
| - elif beyond == 1: # One after the province, wait one more line for the position |
106 |
| - beyond = 2 |
107 |
| - elif line.strip() == str(id)+"={": |
108 |
| - # So we have the province... Wait two lines for the position |
109 |
| - beyond = 1 |
110 |
| - return None |
| 94 | + return province_locations[mod][int(id)] |
111 | 95 |
|
112 | 96 |
|
113 |
| -def provinces(ids: List[Union[str, int]], mod: str = "vanilla") -> Dict[int, Tuple[float, float]]: |
114 |
| - """ |
115 |
| - Gets the location of multiple provinces on a screenshot map. |
116 |
| - Similar to province() except that it will only go through the file once to find all provinces. |
117 |
| - """ |
118 |
| - out: Dict[int, Optional[Tuple[float, float]]] = {} |
119 |
| - ids: List[int] = [int(x) for x in ids] |
120 |
| - srcFile = open(f"resources/{mod}/positions.txt", "r", encoding="cp1252") |
121 |
| - currentID: int = None |
122 |
| - beyond = 0 |
123 |
| - for line in srcFile: |
124 |
| - if beyond == 2: # Two after the province, this is the line with the position. |
125 |
| - vals = line.split() |
126 |
| - # Need to subtract the y value because the position starts from the bottom rather than the top like images |
127 |
| - out[currentID] = (float(vals[0]), 2048-float(vals[1])) |
128 |
| - currentID = None |
129 |
| - beyond = 0 |
130 |
| - elif beyond == 1: # One after the province, wait one more line for the position |
131 |
| - beyond = 2 |
132 |
| - elif line.strip("\t ={\n").isdigit(): # Province top-level bracket |
133 |
| - id = int(line[:line.index("={")]) |
134 |
| - if id in ids: |
135 |
| - currentID = id |
136 |
| - beyond = 1 |
137 |
| - return out |
| 97 | +# LOAD INITIAL TAG CAPITAL DATA |
| 98 | +# Even if not __main__ |
| 99 | +tag_capitals: Dict[str, Dict[str, int]] = {} |
| 100 | +for p in listdir("resources"): |
| 101 | + filepath = path.join("resources", p, "tagCapitals.yml") |
| 102 | + if path.isfile(filepath): |
| 103 | + tag_capitals_file = open(filepath, "r") |
| 104 | + tag_capitals[p] = yaml.load(tag_capitals_file, yaml.CLoader) |
| 105 | + tag_capitals_file.close() |
| 106 | + |
| 107 | + |
| 108 | +def tagCapital(tag: str, mod: str = "vanilla") -> int: |
| 109 | + return tag_capitals[mod][tag] |
138 | 110 |
|
139 | 111 |
|
140 | 112 | def flag(tag: str, mod: str = "vanilla") -> Image.Image:
|
@@ -328,88 +300,3 @@ def colonialFlag(overlordTag: str, colReg: str, mod: str = "vanilla") -> Image.I
|
328 | 300 | flagDraw = ImageDraw.Draw(flagimg)
|
329 | 301 | flagDraw.rectangle([64, 0, 127, 127], color)
|
330 | 302 | return flagimg
|
331 |
| - |
332 |
| - |
333 |
| -class dataReq: |
334 |
| - DATATYPE_PROVINCEDAT = 0 |
335 |
| - REQUEST_PROVINCE_NAME = 0 |
336 |
| - REQUEST_PROVINCE_TRADE = 1 |
337 |
| - REQUEST_PROVINCE_CULTURE_ORIGINAL = 2 |
338 |
| - REQUEST_PROVINCE_RELIGION_ORIGINAL = 3 |
339 |
| - |
340 |
| - def __init__(self, datatype: int, key: str, request: int): |
341 |
| - self.datatype = datatype |
342 |
| - self.key = key |
343 |
| - self.request = request |
344 |
| - self.response = None |
345 |
| - |
346 |
| - def respond(self, r): |
347 |
| - if self.datatype == self.DATATYPE_PROVINCEDAT: |
348 |
| - if self.request == self.REQUEST_PROVINCE_NAME: |
349 |
| - if isinstance(r, str): |
350 |
| - self.response = r |
351 |
| - else: |
352 |
| - raise ValueError( |
353 |
| - f"PROVINCE NAME request for {self.key} was the wrong type.") |
354 |
| - elif self.request == self.REQUEST_PROVINCE_TRADE: |
355 |
| - if isinstance(r, str): |
356 |
| - self.response = r |
357 |
| - else: |
358 |
| - raise ValueError( |
359 |
| - f"PROVINCE TRADE request for {self.key} was the wrong type.") |
360 |
| - elif self.request == self.REQUEST_PROVINCE_CULTURE_ORIGINAL: |
361 |
| - if isinstance(r, str): |
362 |
| - self.response = r |
363 |
| - else: |
364 |
| - raise ValueError( |
365 |
| - f"PROVINCE CULTURE ORIGINAL request for {self.key} was the wrong type.") |
366 |
| - elif self.request == self.REQUEST_PROVINCE_RELIGION_ORIGINAL: |
367 |
| - if isinstance(r, str): |
368 |
| - self.response = r |
369 |
| - else: |
370 |
| - raise ValueError( |
371 |
| - f"PROVINCE RELIGION ORIGINAL request for {self.key} was the wrong type.") |
372 |
| - # More things |
373 |
| - # More datatypes |
374 |
| - |
375 |
| - |
376 |
| -def provinceData(*requests: dataReq, mod: str = "vanilla") -> List[dataReq]: |
377 |
| - data = requests |
378 |
| - srcFile = open(f"resources/{mod}/save_1444.eu4", encoding="cp1252") |
379 |
| - brackets: List[str] = [] |
380 |
| - |
381 |
| - # Reading save file... |
382 |
| - linenum = 0 |
383 |
| - for line in srcFile: |
384 |
| - linenum += 1 |
385 |
| - if "{" in line: |
386 |
| - if line.count("{") == line.count("}"): |
387 |
| - continue |
388 |
| - elif line.count("}") == 0 and line.count("{") == 1: |
389 |
| - brackets.append(line.rstrip("\n ")) |
390 |
| - elif line.count("}") == 0 and line.count("{") > 1: |
391 |
| - # TODO: fix this so it has more stuff |
392 |
| - brackets.append("{" * line.count("{")) |
393 |
| - else: |
394 |
| - pass |
395 |
| - elif "}" in line: |
396 |
| - try: |
397 |
| - brackets.pop() |
398 |
| - except IndexError: |
399 |
| - pass |
400 |
| - elif len(brackets) == 2 and "provinces={" == brackets[0]: |
401 |
| - for request in data: |
402 |
| - if request.response is None and request.datatype == dataReq.DATATYPE_PROVINCEDAT and ("-" + str(request.key) + "={") == brackets[1]: |
403 |
| - if request.request == dataReq.REQUEST_PROVINCE_NAME and line.startswith("\t\tname="): |
404 |
| - request.respond(line.split("\"", 2)[1].strip("\n\t ")) |
405 |
| - elif request.request == dataReq.REQUEST_PROVINCE_TRADE and line.startswith("\t\ttrade="): |
406 |
| - request.respond(line.split("\"", 2)[1].strip("\n\t ")) |
407 |
| - elif request.request == dataReq.REQUEST_PROVINCE_CULTURE_ORIGINAL and line.startswith("\t\toriginal_culture="): |
408 |
| - request.respond(line.split("=", 1)[1].strip("\n\t ")) |
409 |
| - elif request.request == dataReq.REQUEST_PROVINCE_RELIGION_ORIGINAL and line.startswith("\t\toriginal_religion="): |
410 |
| - request.respond(line.split("=", 1)[1].strip("\n\t ")) |
411 |
| - # elif len(brackets) < 0 and ("trade={" == brackets[1] or "rebel_faction={" == brackets[0] or (len(brackets) < 1 and "\tledger_data={" == brackets[1]) or "_area={" in brackets[0] or "change_price={" == brackets[0]): |
412 |
| - # continue |
413 |
| - else: |
414 |
| - pass |
415 |
| - return data |
0 commit comments