comparison tools/build.py @ 9:53f015966118

map, path, tower placement adjustments
author ark
date 2026-10-19 15:55 +0200
parents 2c4fed8baed9
children 22b604680c5a
comparison
equal deleted inserted replaced
8:2c4fed8baed9 9:53f015966118
67 # centre line smoothing: moving average half window, simplify tolerance 67 # centre line smoothing: moving average half window, simplify tolerance
68 ROUTE_SMOOTH = 8 68 ROUTE_SMOOTH = 8
69 ROUTE_TOLERANCE = 0.8 69 ROUTE_TOLERANCE = 0.8
70 # number of decimals of route points in maps.js 70 # number of decimals of route points in maps.js
71 ROUTE_DECIMALS = 1 71 ROUTE_DECIMALS = 1
72 # per map settings used by the mask step
73 MASK_SETTINGS = ("path_half_width", "spawn_distance", "exit_distance")
72 74
73 warnings = [] 75 warnings = []
74 76
75 77
76 def warn(msg): 78 def warn(msg):
193 r["value"] = (r["length"] / longest + straight) / 2 195 r["value"] = (r["length"] / longest + straight) / 2
194 196
195 197
196 def step_masks(cfg, force): 198 def step_masks(cfg, force):
197 placement = cfg["placement"] 199 placement = cfg["placement"]
200 settings = {"placement": placement,
201 "maps": [{k: cfg["map"][str(m)][k] for k in MASK_SETTINGS} for m in range(MAPS)]}
198 routes_toml = CONFIG / "routes.toml" 202 routes_toml = CONFIG / "routes.toml"
199 inputs = [ART / f"map{m}mask.png" for m in range(MAPS)] + [routes_toml] 203 inputs = [ART / f"map{m}mask.png" for m in range(MAPS)] + [routes_toml]
200 outputs = [DATA / "maps.js", MAPS_CACHE] 204 outputs = [DATA / "maps.js", MAPS_CACHE]
201 cached = None 205 cached = None
202 if MAPS_CACHE.exists(): 206 if MAPS_CACHE.exists():
203 cached = json.loads(MAPS_CACHE.read_text()) 207 cached = json.loads(MAPS_CACHE.read_text())
204 # the placement settings are part of the input, compared by value 208 # the placement settings are part of the input, compared by value
205 if not stale(inputs, outputs, force) and cached and cached["placement"] == placement: 209 if not stale(inputs, outputs, force) and cached and cached.get("settings") == settings:
206 print("masks: up to date") 210 print("masks: up to date")
207 return cached["maps"], False 211 return cached["maps"], False
208 212
209 fixes = tomllib.loads(routes_toml.read_text()).get("map", {}) 213 fixes = tomllib.loads(routes_toml.read_text()).get("map", {})
210 out = [] 214 out = []
211 for m in range(MAPS): 215 for m in range(MAPS):
212 t0 = time.time() 216 t0 = time.time()
217 mc = settings["maps"][m]
213 mask = masks.Mask(ART / f"map{m}mask.png") 218 mask = masks.Mask(ART / f"map{m}mask.png")
214 cols, rows, grid = masks.placement_grid(mask, placement)
215 found = masks.extract_routes(mask, ROUTE_SMOOTH, ROUTE_TOLERANCE) 219 found = masks.extract_routes(mask, ROUTE_SMOOTH, ROUTE_TOLERANCE)
220 spaces = [(found["spawns"], mc["spawn_distance"]),
221 (found["exit_points"], mc["exit_distance"])]
222 cols, rows, grid = masks.placement_grid(mask, placement, found["centre"],
223 mc["path_half_width"], spaces)
216 routes = [{"points": pts, "manual": False} for pts in found["routes"]] 224 routes = [{"points": pts, "manual": False} for pts in found["routes"]]
217 routes = apply_corrections(m, routes, fixes.get(str(m), {})) 225 routes = apply_corrections(m, routes, fixes.get(str(m), {}))
218 route_values(routes) 226 route_values(routes)
219 g = found["graph"] 227 g = found["graph"]
220 out.append({ 228 out.append({
223 "height": mask.h, 231 "height": mask.h,
224 "cell": placement["grid_cell"], 232 "cell": placement["grid_cell"],
225 "cols": cols, 233 "cols": cols,
226 "rows": rows, 234 "rows": rows,
227 "grid": grid, 235 "grid": grid,
228 "path_width": found["width"], 236 "line_width": found["width"],
229 "entrances": [b[:3] for b in found["entrances"]], 237 "entrances": [b[:3] for b in found["entrances"]],
230 "exits": [b[:3] for b in found["exits"]], 238 "exits": [b[:3] for b in found["exits"]],
231 "routes": routes, 239 "routes": routes,
232 "graph": { 240 "graph": {
233 "nodes": [list(p) for p in g.nodes.values()], 241 "nodes": [list(p) for p in g.nodes.values()],
236 }) 244 })
237 print(f"masks: map{m} {len(routes)} route(s), {time.time() - t0:.1f} s") 245 print(f"masks: map{m} {len(routes)} route(s), {time.time() - t0:.1f} s")
238 246
239 write_maps_js(out) 247 write_maps_js(out)
240 MAPS_CACHE.parent.mkdir(parents=True, exist_ok=True) 248 MAPS_CACHE.parent.mkdir(parents=True, exist_ok=True)
241 MAPS_CACHE.write_text(json.dumps({"placement": placement, "maps": out})) 249 MAPS_CACHE.write_text(json.dumps({"settings": settings, "maps": out}))
242 return out, True 250 return out, True
243 251
244 252
245 def write_maps_js(maps): 253 def write_maps_js(maps):
246 def rnd(v): 254 def rnd(v):
355 for m in range(MAPS): 363 for m in range(MAPS):
356 mp = cfg["map"].get(str(m)) 364 mp = cfg["map"].get(str(m))
357 if mp is None: 365 if mp is None:
358 raise ConfigError(f"[map.{m}] missing") 366 raise ConfigError(f"[map.{m}] missing")
359 where = f"map.{m}" 367 where = f"map.{m}"
360 for k in ("starting_coins", "early_bonus", "wave_count"): 368 for k in ("starting_coins", "early_bonus", "wave_count") + MASK_SETTINGS:
361 need(mp, k, float, where) 369 need(mp, k, float, where)
362 need(mp, "monster_share", list, where) 370 need(mp, "monster_share", list, where)
363 for w, wave in enumerate(mp.get("wave", []), start=1): 371 for w, wave in enumerate(mp.get("wave", []), start=1):
364 for g, grp in enumerate(need(wave, "groups", list, f"{where} wave {w}"), start=1): 372 for g, grp in enumerate(need(wave, "groups", list, f"{where} wave {w}"), start=1):
365 gw = f"{where} wave {w} group {g}" 373 gw = f"{where} wave {w} group {g}"
420 mons = cfg["monster"] 428 mons = cfg["monster"]
421 regular = sorted(k for k, v in mons.items() if not v.get("boss")) 429 regular = sorted(k for k, v in mons.items() if not v.get("boss"))
422 speeds = sorted({v["speed"] for v in mons.values()}) 430 speeds = sorted({v["speed"] for v in mons.values()})
423 for m, mp in enumerate(maps): 431 for m, mp in enumerate(maps):
424 c = cfg["map"][str(m)] 432 c = cfg["map"][str(m)]
425 print(f"\nmap{m}: {mp['width']}x{mp['height']}, path width {mp['path_width']:.0f}," 433 print(f"\nmap{m}: {mp['width']}x{mp['height']}, line width {mp['line_width']:.0f},"
426 f" {mp['grid'].count('1')} placement cells") 434 f" road half width {c['path_half_width']}, {mp['grid'].count('1')} placement cells")
427 if len(mp["entrances"]) == 0 or len(mp["exits"]) == 0: 435 if len(mp["entrances"]) == 0 or len(mp["exits"]) == 0:
428 warn(f"map{m}: no entrance or no exit found") 436 warn(f"map{m}: no entrance or no exit found")
429 if not mp["routes"]: 437 if not mp["routes"]:
430 warn(f"map{m}: no routes") 438 warn(f"map{m}: no routes")
431 for r in mp["routes"]: 439 for r in mp["routes"]: