-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathlevels_split_gen.py
152 lines (147 loc) · 5.44 KB
/
levels_split_gen.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
import json
from typing import Union, List, Dict
from pathlib import Path
def read_json(path:Union[str, Path])->Union[Dict, List]:
with open(Path(__file__).parent/path, "r", encoding="UTF-8") as fp:
return json.loads(fp.read())
maps = read_json("map.json")
levels :List[Dict[str, str]] = []
stage_table = read_json("gamedata/excel/stage_table.json")
roguelike_topic_table = read_json("gamedata/excel/roguelike_topic_table.json")
sandbox_perm_table = read_json("gamedata/excel/sandbox_perm_table.json")
handbook_info_table = read_json("gamedata/excel/handbook_info_table.json")
for stageId, data in stage_table["stages"].items():
if data["levelId"] is None:
continue
level = {
"name":data["name"],
"code":data["code"],
"stageId":data["stageId"],
"levelId":data["levelId"].lower()
}
levels.append(level)
for rogue, rogue_data in roguelike_topic_table["details"].items():
for stageId, data in rogue_data["stages"].items():
level = {
"name":data["name"],
"code":data["code"],
"stageId":data["id"],
"levelId":data["levelId"].lower()
}
levels.append(level)
for stageId, data in sandbox_perm_table["detail"]["SANDBOX_V2"]["sandbox_1"]["stageData"].items():
if data["levelId"] is None:
continue
level = {
"name":data["name"],
"code":data["code"],
"stageId":data["stageId"],
"levelId":data["levelId"].lower()
}
levels.append(level)
for char_code, data in handbook_info_table["handbookStageData"].items():
level = {
"name":data["name"],
"code":data["code"],
"stageId":data["stageId"],
"levelId":data["levelId"].lower()
}
levels.append(level)
for i in Path("gamedata/levels/obt/rune").glob("*.json"):
levelId = i.as_posix().removeprefix("gamedata/levels/").removesuffix(".json")
level = {
"name":i.stem,
"code":i.stem,
"stageId":i.stem,
"levelId":levelId
}
levels.append(level)
for i in Path("gamedata/levels/activities").glob("*rune/*.json"):
levelId = i.as_posix().removeprefix("gamedata/levels/").removesuffix(".json")
level = {
"name":i.stem,
"code":i.stem,
"stageId":i.stem,
"levelId":levelId
}
levels.append(level)
for i in Path("gamedata/levels/obt/crisis/v2").glob("*.json"):
levelId = i.as_posix().removeprefix("gamedata/levels/").removesuffix(".json")
level = {
"name":i.stem,
"code":i.stem,
"stageId":i.stem,
"levelId":levelId
}
levels.append(level)
if not Path("generated_level_data").exists():
Path("generated_level_data").mkdir()
for level in levels:
name = level["name"]
code = level["code"]
stageId = level["stageId"]
levelId = level["levelId"]
level_path = levelId.replace("main/level_easy_sub", "main/level_sub")
level_path = level_path.replace("main/level_easy", "main/level_main")
level_path = level_path.replace("\\", "/")
#level_path = level_path.replace("main/level_tough", "main/level_main")
path = Path("gamedata/levels", f"{level_path}.json")
try:
level_data = read_json(path)
mapData = level_data["mapData"]
if "width" in mapData and "height" in mapData:
width = mapData["width"]
height = mapData["height"]
else:
width = len(mapData["map"][0])
height = len(mapData["map"])
tiles = []
routes = level_data["routes"]
StartTiles = set()
EndTiles = set()
for route in routes:
if route is not None:
startPosition = (route["startPosition"]["row"], route["startPosition"]["col"])
StartTiles.add((height-startPosition[0]-1, startPosition[1]))
endPosition = (route["endPosition"]["row"], route["endPosition"]["col"])
EndTiles.add((height-endPosition[0]-1, endPosition[1]))
for row_index, row in enumerate(mapData["map"]):
tmp = []
for i, index in enumerate(row):
tile_data = mapData["tiles"][index]
isStart = isEnd = False
if (row_index, i) in StartTiles:
isStart = True
if (row_index, i) in EndTiles:
isEnd = True
tmp.append(
{
"buildableType": tile_data["buildableType"],
"heightType": tile_data["heightType"],
"isEnd":not (isEnd and isStart) and isEnd,
"isStart": not (isEnd and isStart) and isStart,
"tileKey":tile_data["tileKey"]
}
)
tiles.append(tmp)
if level_path in maps:
view = maps[level_path]
elif (level_path:=level_path.replace("main/level_tough", "main/level_main")) in maps:
view = maps[level_path]
else:
continue
result = {
"code": code,
"height": height,
"levelId": levelId,
"name":name,
"stageId":stageId,
"tiles": tiles,
"view": view,
"width": width
}
with open("generated_level_data/" + stageId + "-" + levelId.replace("/", "-") + ".json", "w+", encoding="UTF-8") as fp:
json.dump(result, fp, indent=4, ensure_ascii=False)
except Exception as e:
print(e)
pass