wobblyhead hg > minor_monsters
annotate doc/architecture.md @ 26:28e1fc486be5
get_version.sh
| author | ark |
|---|---|
| date | 2026-10-20 16:06 +0200 |
| parents | 3f2811aebb3d |
| children | 75deda08db90 |
| rev | line source |
|---|---|
| 0 | 1 architecture |
| 2 ------------ | |
| 3 | |
| 4 the game is a static html page with plain javascript, drawn on a 2d canvas. it | |
| 17 | 5 uses no javascript libraries. a python build script prepares everything that |
| 6 can be calculated in advance: processed images, map data, route data and the | |
| 7 config. it writes them to www/, together with a copy of the hand-written game | |
| 8 files from game/. www/ is the runnable game. it is not kept in the repository, | |
| 9 so the game has to be built before it can be run. | |
| 0 | 10 |
| 11 this is a plan; nothing is implemented yet. | |
| 12 | |
| 13 | |
| 14 directory layout | |
| 15 ---------------- | |
| 16 | |
| 17 proposed: | |
| 18 | |
| 19 art/ drawings as supplied: scans, masks, sprites | |
| 4 | 20 sound_sources/ voice recordings as supplied |
| 21 sounds/ sounds cut from the recordings (tools/cut_sounds.py) | |
| 0 | 22 config/ hand-edited game settings (toml) |
| 23 game.toml numbers from balance.md, waves, debug switches | |
| 24 tools/ python build scripts | |
| 17 | 25 game/ hand-written game files |
| 0 | 26 index.html |
| 27 style.css | |
| 17 | 28 js/ game code |
| 0 | 29 doc/ design and technical notes |
| 17 | 30 www/ the built game, opened as www/index.html (generated, ignored) |
| 31 index.html copied from game/, as are style.css and js/ | |
| 32 assets/ processed images | |
| 33 data/ maps.js, sounds.js, config.js | |
| 0 | 34 dist/ zip archives for distribution (ignored) |
| 35 temp/ scratch files, build previews (ignored) | |
| 36 | |
| 37 generated files carry a header line saying so and naming the script that made | |
| 38 them. | |
| 39 | |
| 40 | |
| 41 running the game | |
| 42 ---------------- | |
| 43 | |
| 17 | 44 - desktop: open www/index.html directly from disk, or serve the directory |
| 0 | 45 with any static web server |
| 46 - tablet: tablets cannot easily open local html files, so the game is served | |
| 17 | 47 over http, from a computer on the home network or from a web host. the zip |
| 48 in dist/ is meant for that | |
| 0 | 49 - an optional later step makes it installable to the home screen (web app |
| 50 manifest, offline cache). that requires https | |
| 51 | |
| 52 opening the page from disk (file://) limits what the browser allows. chrome | |
| 53 blocks javascript modules, fetch() and reading pixels back from a canvas | |
| 54 there. so: | |
| 55 | |
| 56 - scripts are loaded with plain script tags in a fixed order, and share one | |
| 57 global namespace object | |
| 58 - data files are javascript files that assign to that object, not json files | |
| 59 - the game never reads mask pixels; the build turns the masks into data | |
| 60 - images are loaded with image elements, which works from disk | |
| 4 | 61 - recorded sounds are embedded by the build as base64 data in a javascript |
| 62 file, so they can be decoded without fetch() | |
| 0 | 63 |
| 64 | |
| 6 | 65 screenshots |
| 66 ----------- | |
| 67 | |
| 68 firefox's own --screenshot captures at the load event. the game loads its | |
| 69 images and draws its first frame after that, so the capture shows an empty | |
| 70 page. the ff_screenshot script (in $PATH, ~/bin/ff_screenshot) drives | |
| 71 headless firefox over marionette and waits before capturing: | |
| 72 | |
| 17 | 73 ff_screenshot -w 1024,768 -s 2 file://$PWD/www/index.html temp/splash.png |
| 6 | 74 |
| 75 -w sets the viewport, -s the wait after loading. the game state is not | |
| 76 reachable from outside, so other screens are reached by tapping: -J runs | |
| 77 javascript after the wait, and -S waits again before capturing. a pointer | |
| 78 event sent to the canvas counts as a tap at that position in css pixels. | |
| 79 this taps the play button on the splash screen at 1024 x 768: | |
| 80 | |
| 81 ff_screenshot -w 1024,768 -s 2 -S 3 -J 'document.getElementById("game") | |
| 82 .dispatchEvent(new PointerEvent("pointerdown", | |
| 11 | 83 {clientX: 497, clientY: 560, bubbles: true}))' \ |
| 17 | 84 file://$PWD/www/index.html temp/play.png |
| 6 | 85 |
| 86 `ff_screenshot -h` lists the other options. | |
| 87 | |
| 88 | |
| 0 | 89 build |
| 90 ----- | |
| 91 | |
| 92 one command, `python3 tools/build.py`, runs all steps. each step skips its work | |
| 93 when its inputs have not changed. requires python 3.11 or later (for the | |
| 94 built-in toml reader) and pillow. numpy is optional, if the pure python image | |
| 95 steps turn out too slow. | |
| 96 | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
97 tools/cut_sheet.py, tools/cut_digits.py and tools/cut_sounds.py are run by |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
98 hand and write to art/. |
| 4 | 99 cut_sounds.py also needs ffmpeg with mp3 support, so the build itself does not |
| 100 depend on it. see sound.md. | |
| 101 | |
| 0 | 102 steps: |
| 103 | |
| 17 | 104 1. sprites: trim transparent edges, write to www/assets/. this covers the |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
105 icons, buttons and screens, and the digits from art/digits/ as |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
106 digit_NAME_N. make the hp bar variants: the empty part, and the blue bars |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
107 for slowed monsters (art.md) |
| 0 | 108 2. tower levels: t0-t3 are level 1. split each t*p strip into four images, |
| 109 levels 2-5. fill the inside of the outline with white (flood fill from | |
| 110 outside, everything not reached is inside), trim, write as t0_2..t0_5 and | |
| 111 so on. see art.md | |
| 112 3. maps: copy the map images, possibly converted to a smaller format | |
| 113 4. masks: for each map | |
| 9 | 114 - classify pixels as path, entrance, exit or free by their exact colour. |
| 115 masks must have hard edges: a pixel that is neither fully transparent | |
| 116 nor opaque pure red, blue or green stops the build (art.md) | |
| 117 - extract routes, see below | |
| 0 | 118 - write a placement grid: cells where a tower footprint fits next to the |
| 9 | 119 road, within the limits in balance.md. the red path in the mask is the |
| 120 centre line of the road; the road's half width is set per map | |
| 17 | 121 5. sounds: embed art/sounds/*.mp3 as base64 in www/data/sounds.js, |
| 4 | 122 grouped by event |
| 17 | 123 6. config: read config/game.toml, check it, write www/data/config.js |
| 4 | 124 7. checks: print a summary per map (routes with length and turning, monster |
| 0 | 125 counts, coins and hp per map against the target) and warn on problems |
| 4 | 126 8. previews: draw each map with its routes, entrances, exits and placement |
| 21 | 127 grid into temp/preview/, for checking without starting the game. towers.png |
| 128 shows the tower levels, each turned by its drawing direction to face up, | |
| 129 with its pivot and footprint | |
| 17 | 130 9. game files: copy the files in game/ to www/. a file is copied when its |
| 131 time or size differs from the copy. files no longer in game/ are removed | |
| 132 from www/, apart from www/assets/ and www/data/ | |
| 0 | 133 |
| 134 a watch mode (`tools/build.py --watch`) reruns the build when a file in | |
| 17 | 135 config/, art/, art/sounds/ or game/ changes. tuning then means: edit the toml, |
| 136 save, reload the browser. the same goes for changes to the game code: they | |
| 137 show in www/ only after the build has copied them. | |
| 0 | 138 |
| 17 | 139 `tools/publish.py` writes a zip of www/ into dist/, with debug switches off. |
| 0 | 140 |
| 141 | |
| 22 | 142 balance tools |
| 143 ------------- | |
| 144 | |
| 145 tools/balance.py estimates, map by map and wave by wave, whether the coins | |
| 146 a map pays buy enough firepower (balance.md, balancing). it is run by hand | |
| 147 after a build: it reads config/game.toml, and the routes and placement grid | |
| 148 from temp/build/maps.json. it only calculates and leaves the images alone. | |
| 149 it writes tables and charts (numpy, matplotlib) to temp/preview/: | |
| 150 | |
| 151 - balance.txt: value per coin for every tower and level; per map the | |
| 152 coverage of each tower's range, and per wave the coins on hand, the | |
| 153 damage rate both demands need and the coins the naive and greedy players | |
| 154 need to hold the wave | |
| 155 - balance_value.png: value per coin by tower level | |
| 156 - balance_map0.png to balance_map3.png: coins per wave, and the surplus at | |
| 23 | 157 the last wave and the hearts left against a wave size factor. the factor |
| 158 scales the number of regular monsters in every wave; the coins they drop | |
| 159 follow | |
| 160 | |
| 161 `tools/balance.py --draft` prints wave lists made from the draft numbers in | |
| 162 each map's section of the config (balance.md, waves), as toml to paste over | |
| 163 the map's [[map.N.wave]] lists. `--draft 2` does map2 only. below each map | |
| 164 it prints what the calculation makes of the drafted lists. nothing is | |
| 165 written in this mode. | |
| 22 | 166 |
| 167 tools/build.py does not count balance.py as one of its inputs, so changing | |
| 168 it does not rebuild the game. | |
| 169 | |
| 23 | 170 tools/simulate.js checks the calculation by playing the real game logic |
| 171 with scripted players. it is run by hand with node after a build: | |
| 172 | |
| 173 node tools/simulate.js | |
| 174 node tools/simulate.js --maps 2 --players naive,naive_t0 --seeds 10 | |
| 175 | |
| 176 it loads www/data/config.js, www/data/maps.js and the logic files util.js, | |
| 177 assets.js, map.js, monster.js, tower.js, projectile.js, wave.js and | |
| 178 game.js, as the browser would, with window set to the global object. | |
| 179 render.js, ui.js, input.js and main.js are left out. MM.audio is the only | |
| 180 stub; assets.js loads unchanged, since sprite sizes come from the config | |
| 181 and images are only loaded by main.js. the debug switches are forced off. | |
| 182 | |
| 183 the game advances in steps of 1/60 s, as in the browser; game speed plays | |
| 184 no part. each run has its own seed, passed to the game. the players build, | |
| 185 upgrade and start waves through the game's own functions (shop choice, | |
| 186 preview, build, tower selection, upgrade, early start), so placement rules | |
| 187 and prices are the game's. the players and their rules are listed at the | |
| 188 top of the file; naive and greedy follow balance.md, balancing, and pick | |
| 189 cells as tools/balance.py does, from the placement grid in maps.js. | |
| 190 | |
| 191 the coins a wave needs are measured by playing that wave alone from an | |
| 192 empty map with the first purchases of the player's list, and searching | |
| 193 for the fewest that lose no heart. every wave starts on an empty map in | |
| 194 the game, so this matches the wave in a full run, apart from towers | |
| 195 bought during the wave. | |
| 196 | |
| 197 it writes temp/preview/simulate.txt (per map and player: maps won, hearts | |
| 198 left, coins on hand and the surplus at the start of the last wave, hearts | |
| 199 lost per wave) and temp/preview/simulate.json, which tools/balance.py reads | |
| 200 for the hits per t0 attack. `--need all` measures the coins needed for | |
| 201 every wave, `--waves` takes wave lists from a json file in place of the | |
| 202 config's, for trying drafts without a build. | |
| 22 | 203 |
| 204 | |
| 0 | 205 routes |
| 206 ------ | |
| 207 | |
| 208 a route is a list of points from an entrance to an exit. a map has one or | |
| 209 more routes. a monster picks a route at spawn (weights in balance.md) and | |
| 210 follows it to the end. this handles the branches on map1 and map2 without | |
| 211 junction logic at runtime. | |
| 212 | |
| 213 extraction in the build: | |
| 214 | |
| 215 1. thin the path pixels to a one-pixel centre line (skeleton) | |
| 216 2. turn the skeleton into a graph of junctions and segments, and drop short | |
| 217 dead ends left by uneven strokes | |
| 218 3. find the graph nodes nearest to each entrance and exit blob | |
| 219 4. list every path through the graph from an entrance to an exit that visits | |
| 220 no junction twice | |
| 221 5. smooth each route and reduce it to a few points per bend | |
| 222 6. record length and turning for each route | |
| 223 | |
| 224 map0 and map3 give one route each. map1 and map2 give several; map2 may give | |
| 23 | 225 more than wanted. where the automatic result is wrong, the mask is redrawn. |
| 0 | 226 |
| 10 | 227 the game walks a monster along the route's centre line, by distance, and |
| 228 draws it beside the line. the offset is set per monster at spawn | |
| 229 (balance.md, monsters) and needs no route data from the build. when a map | |
| 230 loads, each route works out the sideways direction at its points and how far | |
| 231 an offset may go on the inside of each bend. towers rank monsters by their | |
| 232 distance along the centre line, so the offset does not change the order. | |
| 233 | |
| 0 | 234 |
| 235 config | |
| 236 ------ | |
| 237 | |
| 238 config/game.toml holds every number in balance.md, the wave lists, and debug | |
| 17 | 239 switches. the build converts it into www/data/config.js. toml allows comments, |
| 0 | 240 so each value can carry its source note (guess, decided). |
| 241 | |
| 14 | 242 wave format: |
| 0 | 243 |
| 244 [[map.0.wave]] | |
| 14 | 245 monsters = { m0 = 5, m1 = 1, m2 = 1, b0 = 1 } |
| 0 | 246 |
| 14 | 247 a wave lists how many of each monster it holds. when the wave starts, the |
| 248 boss spawns first: it is the slowest monster and the others overtake it. the | |
| 249 rest follow one per spawn interval, each drawn at random from the monsters | |
| 250 left in the wave. the spawn interval is set per map. the boss is in the last | |
| 251 wave. | |
| 0 | 252 |
| 253 debug switches, all off by default: | |
| 254 | |
| 255 - start coins override, unlimited coins | |
| 6 | 256 - start at a given map and wave. that map counts as the first map: the play |
| 257 button starts it, and a lost map returns to it | |
| 0 | 258 - no heart loss |
| 259 - show routes, placement grid, tower ranges, hit areas | |
| 260 - higher game speeds | |
| 261 - on-screen frame time and entity counts | |
| 262 | |
| 263 there is no in-game config editor: values are changed in the toml file. | |
| 264 | |
| 265 | |
| 266 runtime | |
| 267 ------- | |
| 268 | |
| 269 script files, in load order: | |
| 270 | |
| 271 data/config.js generated settings | |
| 272 data/maps.js generated map data | |
| 4 | 273 data/sounds.js generated sound data |
| 0 | 274 js/util.js vector maths, random numbers, easing |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
275 js/storage.js settings kept in the browser |
| 0 | 276 js/assets.js image loading, sprite table |
| 4 | 277 js/audio.js recorded sounds, placeholder beeps for the other events |
| 0 | 278 js/map.js placement lookup, snapping, route sampling |
| 279 js/monster.js movement along a route, hops, slow, hp | |
| 280 js/tower.js targeting, attacks, upgrade, sell | |
| 281 js/projectile.js | |
| 282 js/wave.js wave timing, countdown, early start bonus | |
| 283 js/game.js game state, rules, win and loss | |
| 284 js/render.js drawing | |
| 285 js/ui.js top bar, shop, tower menu, screens | |
| 286 js/input.js pointer events | |
| 287 js/main.js start-up and main loop | |
| 288 | |
| 289 game loop: | |
| 290 | |
| 291 - the simulation advances in fixed steps of 1/60 s. drawing happens once per | |
| 292 browser frame and is independent of it | |
| 293 - game speed 2x runs two simulation steps per frame | |
|
13
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
11
diff
changeset
|
294 - pause stops the simulation; drawing and the top bar buttons continue |
| 0 | 295 - a frame that takes too long runs at most a few steps and drops the rest, so |
| 296 the game slows down rather than freezing after the tab was in the background | |
| 297 | |
| 298 state: | |
| 299 | |
| 300 - game state is plain objects: monsters, towers, projectiles, coins in flight, | |
| 301 wave state, coins, hearts. rendering reads it and does not change it | |
| 302 - random numbers come from one seeded generator, so a run can be repeated with | |
| 303 the same seed when chasing a bug | |
| 304 | |
| 305 display: | |
| 306 | |
| 307 - the canvas fills the window. the map is scaled to fit and centred, with | |
| 308 borders where the shapes differ | |
| 309 - the canvas resolution follows the device pixel ratio, so drawings stay sharp | |
| 310 on high-resolution screens | |
| 24 | 311 - layout: top bar with coins, hearts, wave, countdown, sound, speed, pause |
| 312 and help. on a narrow screen the buttons move to a second row under the | |
| 23 | 313 counters. the tower shop sits on the right in landscape and at the bottom in portrait |
| 0 | 314 - buttons are at least 48 css pixels for touch |
| 315 | |
| 316 input: | |
| 317 | |
| 318 - pointer events cover mouse and touch alike | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
319 - nothing relies on hover; selection and confirmation are by tap. with a |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
320 mouse, hover enlarges the screen buttons and shows the pointer cursor over |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
321 any button |
| 11 | 322 - buttons and tower selection act when the pointer goes down. placing |
| 323 follows the pointer until it is let go, and building happens then | |
| 0 | 324 - the page blocks browser zoom and scroll gestures on the canvas |
| 325 | |
| 326 placement: | |
| 327 | |
| 328 - the build's placement grid marks every cell where a tower may stand | |
| 11 | 329 - while the pointer is down, the game searches outward from it for the |
| 330 nearest legal cell that keeps the minimum distance to other towers, within | |
| 331 the snap radius. where there is none, the preview stays where it was | |
| 0 | 332 - t0 checks once, at placement, which of its four sides face the path, and |
| 333 only draws spikes on those | |
| 334 | |
| 335 persistence: | |
| 336 | |
| 6 | 337 - nothing is stored for now. maps are played in order and a lost map goes |
| 338 back to the first map, so there is no progress to keep. a reload starts at | |
| 339 the splash screen with a fresh game | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
340 - settings, so far only sound on or off, are stored by js/storage.js. it |
|
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
341 wraps localStorage, and one entry holds them |
| 0 | 342 - all access is wrapped so the game still runs when storage is unavailable, |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
343 for example in private browsing; settings are then not kept |
| 0 | 344 - the entry carries a version number for later format changes |
| 345 | |
| 346 | |
| 347 not planned | |
| 348 ----------- | |
| 349 | |
| 350 - in-browser editors or tuning panels |
