wobblyhead hg > minor_monsters
comparison tools/build.py @ 4:2d46729a022e
sound processing and in-game sounds
| author | ark |
|---|---|
| date | 2026-10-18 20:58 +0200 |
| parents | 3dd5b745fa08 |
| children | 910b26feca76 |
comparison
equal
deleted
inserted
replaced
| 3:cc0c32e71090 | 4:2d46729a022e |
|---|---|
| 2 """ | 2 """ |
| 3 build the game data from art/ and config/ into game/assets/ and game/data/. | 3 build the game data from art/ and config/ into game/assets/ and game/data/. |
| 4 | 4 |
| 5 usage: python3 tools/build.py [--force] [--watch] | 5 usage: python3 tools/build.py [--force] [--watch] |
| 6 | 6 |
| 7 steps: sprites, tower levels, maps, masks, config, checks, previews. a step | 7 steps: sprites, tower levels, maps, masks, sounds, config, checks, previews. |
| 8 a step | |
| 8 is skipped when all its outputs are newer than its inputs and the build | 9 is skipped when all its outputs are newer than its inputs and the build |
| 9 scripts; --force runs every step. --watch reruns the build whenever a file | 10 scripts; --force runs every step. --watch reruns the build whenever a file |
| 10 in art/ or config/ changes. | 11 in art/, art/sounds/ or config/ changes. |
| 11 | 12 |
| 12 requires python 3.11 or later and pillow. | 13 requires python 3.11 or later and pillow. |
| 13 """ | 14 """ |
| 14 | 15 |
| 16 import base64 | |
| 15 import json | 17 import json |
| 16 import math | 18 import math |
| 19 import re | |
| 17 import sys | 20 import sys |
| 18 import time | 21 import time |
| 19 import tomllib | 22 import tomllib |
| 20 from pathlib import Path | 23 from pathlib import Path |
| 21 | 24 |
| 28 import masks | 31 import masks |
| 29 import preview | 32 import preview |
| 30 | 33 |
| 31 ROOT = Path(__file__).resolve().parent.parent | 34 ROOT = Path(__file__).resolve().parent.parent |
| 32 ART = ROOT / "art" | 35 ART = ROOT / "art" |
| 36 SOUNDS = ART / "sounds" | |
| 33 CONFIG = ROOT / "config" | 37 CONFIG = ROOT / "config" |
| 34 ASSETS = ROOT / "game" / "assets" | 38 ASSETS = ROOT / "game" / "assets" |
| 35 DATA = ROOT / "game" / "data" | 39 DATA = ROOT / "game" / "data" |
| 36 TEMP = ROOT / "temp" | 40 TEMP = ROOT / "temp" |
| 37 PREVIEW = TEMP / "preview" | 41 PREVIEW = TEMP / "preview" |
| 243 body = ",\n".join(json.dumps(m, separators=(",", ":")) for m in js) | 247 body = ",\n".join(json.dumps(m, separators=(",", ":")) for m in js) |
| 244 write_text(DATA / "maps.js", HEADER.format(src="art/map*mask.png and config/routes.toml") | 248 write_text(DATA / "maps.js", HEADER.format(src="art/map*mask.png and config/routes.toml") |
| 245 + "window.MM = window.MM || {};\nMM.maps = [\n" + body + "\n];\n") | 249 + "window.MM = window.MM || {};\nMM.maps = [\n" + body + "\n];\n") |
| 246 | 250 |
| 247 | 251 |
| 248 # 5. config | 252 # 5. sounds |
| 253 | |
| 254 def step_sounds(cfg, force): | |
| 255 """ | |
| 256 embed art/sounds/*.mp3 as base64 in game/data/sounds.js, grouped by | |
| 257 event: NAME.mp3 or the variants NAME_1.mp3, NAME_2.mp3 and so on. | |
| 258 """ | |
| 259 inputs = sorted(SOUNDS.glob("*.mp3")) | |
| 260 dst = DATA / "sounds.js" | |
| 261 if not stale(inputs, [dst], force): | |
| 262 print("sounds: up to date") | |
| 263 return | |
| 264 events = {} | |
| 265 for p in inputs: | |
| 266 name = re.sub(r"_\d+$", "", p.stem) | |
| 267 events.setdefault(name, []).append(base64.b64encode(p.read_bytes()).decode("ascii")) | |
| 268 for name in events: | |
| 269 if name not in cfg["sound"]["event"]: | |
| 270 warn(f"sounds: {name} has no entry in [sound.event]") | |
| 271 body = ",\n".join(f" {json.dumps(k)}: {json.dumps(v)}" for k, v in events.items()) | |
| 272 write_text(dst, HEADER.format(src="art/sounds/") | |
| 273 + "window.MM = window.MM || {};\nMM.sounds = {\n" + body + "\n};\n") | |
| 274 size = dst.stat().st_size / 1024 | |
| 275 print(f"sounds: wrote {len(inputs)} sounds for {len(events)} events, {size:.0f} KB") | |
| 276 | |
| 277 | |
| 278 # 6. config | |
| 249 | 279 |
| 250 class ConfigError(Exception): | 280 class ConfigError(Exception): |
| 251 pass | 281 pass |
| 252 | 282 |
| 253 | 283 |
| 265 | 295 |
| 266 | 296 |
| 267 def check_config(cfg): | 297 def check_config(cfg): |
| 268 """raise ConfigError on missing or malformed settings.""" | 298 """raise ConfigError on missing or malformed settings.""" |
| 269 for sec in ("scale", "monster", "animation", "route_preference", "tower", "placement", | 299 for sec in ("scale", "monster", "animation", "route_preference", "tower", "placement", |
| 270 "economy", "waves", "hearts", "coins", "controls", "art", "debug", "map"): | 300 "economy", "waves", "hearts", "coins", "controls", "art", "debug", "sound", |
| 301 "map"): | |
| 271 if sec not in cfg: | 302 if sec not in cfg: |
| 272 raise ConfigError(f"section [{sec}] missing") | 303 raise ConfigError(f"section [{sec}] missing") |
| 273 for mid, mon in cfg["monster"].items(): | 304 for mid, mon in cfg["monster"].items(): |
| 274 for k in ("hp", "coins", "speed"): | 305 for k in ("hp", "coins", "speed"): |
| 275 need(mon, k, float, f"monster.{mid}") | 306 need(mon, k, float, f"monster.{mid}") |
| 284 if "slow" in tw and len(tw["slow"]) != 5: | 315 if "slow" in tw and len(tw["slow"]) != 5: |
| 285 raise ConfigError(f"tower.{tid}: slow needs 5 levels") | 316 raise ConfigError(f"tower.{tid}: slow needs 5 levels") |
| 286 for k in ("footprint_radius", "max_gap", "min_tower_distance", "edge_distance", | 317 for k in ("footprint_radius", "max_gap", "min_tower_distance", "edge_distance", |
| 287 "snap_radius", "grid_cell"): | 318 "snap_radius", "grid_cell"): |
| 288 need(cfg["placement"], k, float, "placement") | 319 need(cfg["placement"], k, float, "placement") |
| 320 snd = cfg["sound"] | |
| 321 need(snd, "enabled", bool, "sound") | |
| 322 need(snd, "volume", float, "sound") | |
| 323 need(snd, "beep_wave", str, "sound") | |
| 324 for name, ev in need(snd, "event", dict, "sound").items(): | |
| 325 where = f"sound.event.{name}" | |
| 326 need(ev, "volume", float, where) | |
| 327 if "min_interval" in ev: | |
| 328 need(ev, "min_interval", float, where) | |
| 329 for note in ev.get("beep", []): | |
| 330 if not (isinstance(note, list) and len(note) == 2): | |
| 331 raise ConfigError(f"{where}: beep notes should be [frequency, seconds]") | |
| 289 for m in range(MAPS): | 332 for m in range(MAPS): |
| 290 mp = cfg["map"].get(str(m)) | 333 mp = cfg["map"].get(str(m)) |
| 291 if mp is None: | 334 if mp is None: |
| 292 raise ConfigError(f"[map.{m}] missing") | 335 raise ConfigError(f"[map.{m}] missing") |
| 293 where = f"map.{m}" | 336 where = f"map.{m}" |
| 332 + "window.MM = window.MM || {};\nMM.config = " | 375 + "window.MM = window.MM || {};\nMM.config = " |
| 333 + json.dumps(out, indent=1) + ";\n") | 376 + json.dumps(out, indent=1) + ";\n") |
| 334 print("config: wrote game/data/config.js") | 377 print("config: wrote game/data/config.js") |
| 335 | 378 |
| 336 | 379 |
| 337 # 6. checks | 380 # 7. checks |
| 338 | 381 |
| 339 def wave_totals(cfg, waves): | 382 def wave_totals(cfg, waves): |
| 340 """return per monster counts, regular coins, boss coins, regular hp.""" | 383 """return per monster counts, regular coins, boss coins, regular hp.""" |
| 341 counts = {} | 384 counts = {} |
| 342 for wave in waves: | 385 for wave in waves: |
| 401 bonus = c["early_bonus"] * (cfg["waves"]["first_break"] | 444 bonus = c["early_bonus"] * (cfg["waves"]["first_break"] |
| 402 + cfg["waves"]["break"] * (len(waves) - 1)) | 445 + cfg["waves"]["break"] * (len(waves) - 1)) |
| 403 print(f" early start bonus if every break is skipped: {math.floor(bonus)}") | 446 print(f" early start bonus if every break is skipped: {math.floor(bonus)}") |
| 404 | 447 |
| 405 | 448 |
| 406 # 7. previews | 449 # 8. previews |
| 407 | 450 |
| 408 def step_previews(cfg, maps, force, changed): | 451 def step_previews(cfg, maps, force, changed): |
| 409 outputs = [PREVIEW / f"map{m}.png" for m in range(MAPS)] + [PREVIEW / "towers.png"] | 452 outputs = [PREVIEW / f"map{m}.png" for m in range(MAPS)] + [PREVIEW / "towers.png"] |
| 410 # the tower sheet shows the pivots from game.toml | 453 # the tower sheet shows the pivots from game.toml |
| 411 if not changed and not stale([CONFIG / "game.toml"], outputs, force): | 454 if not changed and not stale([CONFIG / "game.toml"], outputs, force): |
| 430 return False | 473 return False |
| 431 changed = step_sprites(force) | 474 changed = step_sprites(force) |
| 432 changed |= step_towers(force) | 475 changed |= step_towers(force) |
| 433 step_maps(force) | 476 step_maps(force) |
| 434 maps, masks_changed = step_masks(cfg, force) | 477 maps, masks_changed = step_masks(cfg, force) |
| 478 step_sounds(cfg, force) | |
| 435 step_config(cfg, force, changed) | 479 step_config(cfg, force, changed) |
| 436 step_checks(cfg, maps) | 480 step_checks(cfg, maps) |
| 437 step_previews(cfg, maps, force, changed or masks_changed) | 481 step_previews(cfg, maps, force, changed or masks_changed) |
| 438 print(f"\n{len(warnings)} warning(s)") | 482 print(f"\n{len(warnings)} warning(s)") |
| 439 return True | 483 return True |
| 440 | 484 |
| 441 | 485 |
| 442 def watch(): | 486 def watch(): |
| 443 def snapshot(): | 487 def snapshot(): |
| 444 files = list(ART.iterdir()) + list(CONFIG.iterdir()) + TOOLS | 488 files = list(ART.iterdir()) + list(SOUNDS.glob("*.mp3")) + list(CONFIG.iterdir()) + TOOLS |
| 445 return {p: p.stat().st_mtime for p in files if p.is_file()} | 489 return {p: p.stat().st_mtime for p in files if p.is_file()} |
| 446 | 490 |
| 447 seen = None | 491 seen = None |
| 448 while True: | 492 while True: |
| 449 now = snapshot() | 493 now = snapshot() |
