comparison doc/implementation.md @ 36:a464bd43b76c 1.0.0

clean documentation rewrite
author ark
date 2026-10-21 22:34 +0200
parents
children
comparison
equal deleted inserted replaced
35:0d40f346059c 36:a464bd43b76c
1 Implementation notes
2 --------------------
3
4 How the build and the game code do what game.md and architecture.md
5 describe, for changing or debugging the code. Tuning values are in
6 config/game.toml. The constants named here are set at the top of the file
7 or function that uses them; read the value there.
8
9
10 Images (tools/images.py, tools/build.py)
11 ----------------------------------------
12
13 - Sprites are trimmed to their non-transparent pixels. Sprite sizes go into
14 config.js, so the game knows them before the images load
15 - Tower strips are split at columns without pencil, that is, without pixels
16 darker than INK_LUM. Runs of pencil narrower than 4 columns are specks.
17 The cut lies halfway between drawings. A strip must give 4 drawings
18 - The inside of a tower outline is filled with white: the pencil lines are
19 thickened by GAP_CLOSE to close gaps, the outside is flood filled from a
20 corner and grown back by the same amount, and everything not reached is
21 inside. Outside, light pixels fade from opaque at LUM_OPAQUE to
22 transparent at LUM_CLEAR, which removes the paper halo
23 - HP bars: the empty part is a copy darkened by BAR_DARKEN (hpm_empty,
24 hpb_empty). The blue bars have every pixel's hue set to BAR_SLOW_HUE,
25 which leaves the black outline as it is (hpm_slow, hpb_slow, each with
26 an _empty copy)
27 - Maps are written as JPEG at MAP_QUALITY. A map with transparent pixels
28 stops the build
29 - The sprite list of the build is SPRITES in tools/build.py. A new image
30 has to be added there
31
32
33 Masks and routes (tools/masks.py)
34 ---------------------------------
35
36 - Pixels are classified by exact colour: transparent is free, opaque pure
37 red is path, blue is entrance, green is exit. Any other pixel stops the
38 build with its position
39 - The path and the entrance and exit areas are thinned to a one pixel
40 centre line (Zhang-Suen) and turned into a graph of junctions, line ends
41 and the pixel chains between them. Small loops of fewer than 8 pixels
42 at one junction are dropped
43 - The line width is the red area divided by the centre line length
44 - The line end nearest to each entrance and exit blob is a terminal. Dead
45 ends shorter than 0.8 line widths are dropped, and junctions closer than
46 0.75 line widths count as one; thinning splits a wide crossing into
47 several junctions
48 - Routes are all paths from an entrance terminal to an exit terminal that
49 visit no junction twice. A loop that leaves a junction and comes back to
50 it may be walked once. Of its two directions, the one that turns less is
51 kept. This gives map2's three loops (the round one at the top left and
52 two squares at the bottom) and its six routes
53 - Each route is resampled at 1 pixel, smoothed twice by a moving average
54 with half window ROUTE_SMOOTH, and simplified to ROUTE_TOLERANCE
55 (Ramer-Douglas-Peucker)
56 - Turning is the sum of direction changes along a route, in degrees, not
57 per length. The route value is the average of length / longest and least
58 turning of the map / own turning (route_values in build.py)
59 - The centre line runs through the entrance and exit areas, so routes
60 start and end inside them. The spawn point is the first centre line
61 pixel outside the entrance, walking from the route start; the exit point
62 is found the same way from the route end
63
64
65 Placement grid (tools/masks.py)
66 -------------------------------
67
68 The grid has one cell per grid_cell pixels. A cell is legal when a tower
69 footprint centred on it:
70
71 - does not overlap the road: the road is a disc of footprint radius plus
72 the map's road half width around each centre line pixel. Centre line
73 pixels inside the entrance and exit areas do not count
74 - does not overlap the entrance and exit areas
75 - keeps the map's spawn_distance from each spawn point and exit_distance
76 from each exit point
77 - comes within max_gap of the road
78 - has its centre at least edge_distance from the map edge
79
80 The test draws discs instead of measuring a distance for every pixel; the
81 result is the same. The distance between towers is checked by the game,
82 not the grid.
83
84
85 Main loop and movement (js/main.js, js/map.js, js/monster.js)
86 -------------------------------------------------------------
87
88 - The simulation runs on the frame clock: the time since the last frame,
89 times the game speed, is used up in steps of STEP. At most MAX_STEPS
90 steps per speed step run in one frame; the rest is dropped
91 - A route is a polyline with cumulative distances. A monster's position is
92 its distance walked, turned into a point on the centre line and shifted
93 sideways
94 - Sideways offset: a fixed lean of lean x the largest offset, chosen at
95 spawn, plus a wandering part with a new random value every wander_step
96 pixels, eased between values by a cosine. The sum grows from 0 over the
97 first fade_in pixels. The largest offset is offset x the map's road half
98 width
99 - On the inside of a bend the offset is limited so the shifted point keeps
100 moving forward at no less than 1 - BEND_SHARE of the walking speed. The
101 limit changes by at most LIMIT_SLOPE per pixel, so monsters drift
102 towards the centre line before a tight bend rather than at it
103 - Towers rank monsters by distance left along the centre line, so the
104 offset does not change the order
105 - Hops are drawn only; monsters move at constant speed. A hop spends AIR
106 of its time in the air and the rest landing. The idle bob is a 1.3 s
107 breathing on top. Each monster starts at a random point of its hop cycle
108 - Monsters and towers are drawn sorted by their y position
109
110
111 Towers and effects (js/tower.js, js/projectile.js, js/game.js, js/render.js)
112 ----------------------------------------------------------------------------
113
114 - A monster is in range when its centre is. Projectiles hit on coming
115 within the target's hit radius, hit_radius x the smaller side of its
116 sprite
117 - Each running slow keeps its own time; the strongest applies
118 - The freeze splash slows every monster whose hit area it touches. It is
119 drawn under the monsters, as freeze.png across its diameter at 85 %
120 opacity, and grows and fades over freeze_splash seconds
121 - The attack of t0 stretches each spike from under the tower until its tip
122 reaches the range, and back, over BURST_TIME. At rest a quarter of each
123 spike shows beyond the tower's edge
124 - Spike sides of t0: each side covers a quarter of the directions around
125 the tower. Route centre lines are sampled every SIDE_STEP pixels. A side
126 gets a spike when it holds at least SIDE_SHARE of the samples within
127 t0's level 5 range. With no sample in that range, only the side towards
128 the nearest one does. The sides are fixed when the preview is placed
129 - Death puffs and the freeze splash grow from 60 % to full size over the
130 first half of their time and fade out over the second half. Both are
131 dropped when the map ends
132 - Flying coins: one sprite per coin, at most max_sprites, started 0.05 s
133 apart, flying in game time on an arc that eases in towards the counter.
134 Early start bonus coins start at the exits in turn, within each exit's
135 radius. Coins in flight are dropped when the map ends; they have already
136 been counted
137
138
139 Layout and UI (js/ui.js)
140 ------------------------
141
142 - The sizes of top bar rows, buttons, shop items, the tower menu bubble
143 and digits are the constants at the top of ui.js, in CSS pixels
144 - Each top bar counter keeps the width of a usual largest value (USUAL),
145 so the next one does not move. When the counters at those widths and the
146 buttons do not fit side by side, the buttons get a second row under the
147 counters. Narrower screens are not handled further
148 - Screens: one scale for all four drawings, so that the widest and the
149 tallest fit 92 % of the window width and 90 % of its height, at most 1.
150 Each screen's separate button image is drawn over the button in the
151 drawing, at the position in [art.screen]; its tap area is the button
152 image. The splash screen's logo and question mark are placed by top left
153 corner and height. Buttons pulse by 4 % over PULSE seconds; under the
154 mouse they are 8 % larger with a shadow
155 - The help popup has a scale of its own, to fit 96 % of the window. Its
156 tap areas are the email address ([art.screen] help_email) and the whole
157 window, which closes it
158 - The splash screen has a pale veil over the map, the other screens a dark
159 one. A paused game has a light veil over the map
160 - Numbers: the digit variant of each place depends on the digit and its
161 place counted from the right, so variants differ along a number but a
162 number keeps its look. Prices have coin_icon in front of the amount;
163 placed behind it, the icon would read as an extra zero
164 - The chosen shop item switches between shop_bubble_dark and
165 shop_bubble_dark2 every BUBBLE_FLIP ms. The preview grows while one
166 shows and shrinks while the other does, between 45 % and 80 % opacity and
167 100 % and 108 % size. The range circle does not pulse
168 - The tower menu bubble sits above the tower, or below it when there is no
169 room above, kept inside the map area. Upgrade and sell each take half of
170 it as tap area. An armed sell button pulses by 15 % every WARN_PULSE
171 seconds with a red glow; there is no timeout
172 - The early start button draws the countdown on next_wave, without the
173 bonus
174
175
176 Input (js/input.js)
177 -------------------
178
179 - Only the first pointer is followed while it is down; it is captured, so
180 a drag may leave the canvas
181 - A press within the footprint radius or PREVIEW_HIT CSS pixels of the
182 preview grabs it. Released after a move of at most DRAG_SLOP CSS pixels,
183 it builds the tower; a longer move drags it, keeping its offset to the
184 pointer
185 - A press or drag that never finds a legal position shows the red cross
186 for REFUSED_TIME where the pointer is let go
187 - Any button other than sell cancels an armed sell; any button outside
188 the tower menu closes it
189 - A cancelled pointer, for example taken by a system gesture, builds
190 nothing
191
192
193 Audio (js/audio.js)
194 -------------------
195
196 - The audio context is created at start and stays suspended until
197 audio.start() runs in a tap: the splash screen's sound button, or the
198 play button. Older Safari also needs a short silent sound started in the
199 handler. Later pointer and touch events resume a context the browser has
200 suspended. For the mouse, pointerdown counts as a user interaction; for
201 touch, pointerup and touchend do
202 - A sound that cannot play at once, while audio is still locked, is
203 dropped, not queued
204 - Switching sound off mutes the output at once and stops new sounds. The
205 button tap sound plays after the switch, so it is heard when sound goes
206 on
207 - The min_interval setting drops plays of an event that come too soon
208 after the last
209 - Beeps are oscillator notes with the wave form beep_wave, each with a
210 falling envelope
211 - The splash music loops the event's first recording. If it is not
212 decoded yet, it starts once it is; on a locked context, with the unlock
213
214
215 Cutting scripts (tools/cut_*.py)
216 --------------------------------
217
218 - cut_sheet.py: one hand-set box per drawing on
219 art_sources/source_sheet_2.jpg, at the sheet's scale. In each box the
220 largest connected outline, ink darker than INK_LUM, is the drawing; other
221 outlines are kept only when they reach KEEP_SHARE of it and do not touch
222 the box border. Paper lighter than PAPER_LUM becomes white, the outside
223 transparent. The four levels of a t*p strip are written side by side
224 with STRIP_GAP empty columns, as the build expects. The files
225 game_won.png and game_won_button.png were cut once from
226 art_sources/game_won.jpg with its cut function; the scan has no boxes in
227 the script
228 - cut_digits.py: every connected stroke on digits.png darker than INK_LUM
229 is one character; strokes under MIN_PIXELS are specks. Rows are told
230 apart by the stroke centre, and the characters of each row are listed in
231 ROWS. All characters are scaled to the median height of the drawings
232 - cut_sounds.py: high-pass filter at HIGHPASS, trim to the voice (frames
233 within THRESHOLD_DB of the loudest), split recordings with variants at
234 their longest pauses (VARIANTS), fade the ends, bring every sound to the
235 same peak level, write mono MP3. SKIP drops the start of a recording,
236 LOOP_GAP adds silence after a looping sound