view doc/implementation.md @ 37:a88effe4831e default tip

Added tag 1.0.0 for changeset a464bd43b76c
author ark
date 2026-10-21 22:36 +0200
parents a464bd43b76c
children
line wrap: on
line source

Implementation notes
--------------------

How the build and the game code do what game.md and architecture.md
describe, for changing or debugging the code. Tuning values are in
config/game.toml. The constants named here are set at the top of the file
or function that uses them; read the value there.


Images (tools/images.py, tools/build.py)
----------------------------------------

- Sprites are trimmed to their non-transparent pixels. Sprite sizes go into
  config.js, so the game knows them before the images load
- Tower strips are split at columns without pencil, that is, without pixels
  darker than INK_LUM. Runs of pencil narrower than 4 columns are specks.
  The cut lies halfway between drawings. A strip must give 4 drawings
- The inside of a tower outline is filled with white: the pencil lines are
  thickened by GAP_CLOSE to close gaps, the outside is flood filled from a
  corner and grown back by the same amount, and everything not reached is
  inside. Outside, light pixels fade from opaque at LUM_OPAQUE to
  transparent at LUM_CLEAR, which removes the paper halo
- HP bars: the empty part is a copy darkened by BAR_DARKEN (hpm_empty,
  hpb_empty). The blue bars have every pixel's hue set to BAR_SLOW_HUE,
  which leaves the black outline as it is (hpm_slow, hpb_slow, each with
  an _empty copy)
- Maps are written as JPEG at MAP_QUALITY. A map with transparent pixels
  stops the build
- The sprite list of the build is SPRITES in tools/build.py. A new image
  has to be added there


Masks and routes (tools/masks.py)
---------------------------------

- Pixels are classified by exact colour: transparent is free, opaque pure
  red is path, blue is entrance, green is exit. Any other pixel stops the
  build with its position
- The path and the entrance and exit areas are thinned to a one pixel
  centre line (Zhang-Suen) and turned into a graph of junctions, line ends
  and the pixel chains between them. Small loops of fewer than 8 pixels
  at one junction are dropped
- The line width is the red area divided by the centre line length
- The line end nearest to each entrance and exit blob is a terminal. Dead
  ends shorter than 0.8 line widths are dropped, and junctions closer than
  0.75 line widths count as one; thinning splits a wide crossing into
  several junctions
- Routes are all paths from an entrance terminal to an exit terminal that
  visit no junction twice. A loop that leaves a junction and comes back to
  it may be walked once. Of its two directions, the one that turns less is
  kept. This gives map2's three loops (the round one at the top left and
  two squares at the bottom) and its six routes
- Each route is resampled at 1 pixel, smoothed twice by a moving average
  with half window ROUTE_SMOOTH, and simplified to ROUTE_TOLERANCE
  (Ramer-Douglas-Peucker)
- Turning is the sum of direction changes along a route, in degrees, not
  per length. The route value is the average of length / longest and least
  turning of the map / own turning (route_values in build.py)
- The centre line runs through the entrance and exit areas, so routes
  start and end inside them. The spawn point is the first centre line
  pixel outside the entrance, walking from the route start; the exit point
  is found the same way from the route end


Placement grid (tools/masks.py)
-------------------------------

The grid has one cell per grid_cell pixels. A cell is legal when a tower
footprint centred on it:

- does not overlap the road: the road is a disc of footprint radius plus
  the map's road half width around each centre line pixel. Centre line
  pixels inside the entrance and exit areas do not count
- does not overlap the entrance and exit areas
- keeps the map's spawn_distance from each spawn point and exit_distance
  from each exit point
- comes within max_gap of the road
- has its centre at least edge_distance from the map edge

The test draws discs instead of measuring a distance for every pixel; the
result is the same. The distance between towers is checked by the game,
not the grid.


Main loop and movement (js/main.js, js/map.js, js/monster.js)
-------------------------------------------------------------

- The simulation runs on the frame clock: the time since the last frame,
  times the game speed, is used up in steps of STEP. At most MAX_STEPS
  steps per speed step run in one frame; the rest is dropped
- A route is a polyline with cumulative distances. A monster's position is
  its distance walked, turned into a point on the centre line and shifted
  sideways
- Sideways offset: a fixed lean of lean x the largest offset, chosen at
  spawn, plus a wandering part with a new random value every wander_step
  pixels, eased between values by a cosine. The sum grows from 0 over the
  first fade_in pixels. The largest offset is offset x the map's road half
  width
- On the inside of a bend the offset is limited so the shifted point keeps
  moving forward at no less than 1 - BEND_SHARE of the walking speed. The
  limit changes by at most LIMIT_SLOPE per pixel, so monsters drift
  towards the centre line before a tight bend rather than at it
- Towers rank monsters by distance left along the centre line, so the
  offset does not change the order
- Hops are drawn only; monsters move at constant speed. A hop spends AIR
  of its time in the air and the rest landing. The idle bob is a 1.3 s
  breathing on top. Each monster starts at a random point of its hop cycle
- Monsters and towers are drawn sorted by their y position


Towers and effects (js/tower.js, js/projectile.js, js/game.js, js/render.js)
----------------------------------------------------------------------------

- A monster is in range when its centre is. Projectiles hit on coming
  within the target's hit radius, hit_radius x the smaller side of its
  sprite
- Each running slow keeps its own time; the strongest applies
- The freeze splash slows every monster whose hit area it touches. It is
  drawn under the monsters, as freeze.png across its diameter at 85 %
  opacity, and grows and fades over freeze_splash seconds
- The attack of t0 stretches each spike from under the tower until its tip
  reaches the range, and back, over BURST_TIME. At rest a quarter of each
  spike shows beyond the tower's edge
- Spike sides of t0: each side covers a quarter of the directions around
  the tower. Route centre lines are sampled every SIDE_STEP pixels. A side
  gets a spike when it holds at least SIDE_SHARE of the samples within
  t0's level 5 range. With no sample in that range, only the side towards
  the nearest one does. The sides are fixed when the preview is placed
- Death puffs and the freeze splash grow from 60 % to full size over the
  first half of their time and fade out over the second half. Both are
  dropped when the map ends
- Flying coins: one sprite per coin, at most max_sprites, started 0.05 s
  apart, flying in game time on an arc that eases in towards the counter.
  Early start bonus coins start at the exits in turn, within each exit's
  radius. Coins in flight are dropped when the map ends; they have already
  been counted


Layout and UI (js/ui.js)
------------------------

- The sizes of top bar rows, buttons, shop items, the tower menu bubble
  and digits are the constants at the top of ui.js, in CSS pixels
- Each top bar counter keeps the width of a usual largest value (USUAL),
  so the next one does not move. When the counters at those widths and the
  buttons do not fit side by side, the buttons get a second row under the
  counters. Narrower screens are not handled further
- Screens: one scale for all four drawings, so that the widest and the
  tallest fit 92 % of the window width and 90 % of its height, at most 1.
  Each screen's separate button image is drawn over the button in the
  drawing, at the position in [art.screen]; its tap area is the button
  image. The splash screen's logo and question mark are placed by top left
  corner and height. Buttons pulse by 4 % over PULSE seconds; under the
  mouse they are 8 % larger with a shadow
- The help popup has a scale of its own, to fit 96 % of the window. Its
  tap areas are the email address ([art.screen] help_email) and the whole
  window, which closes it
- The splash screen has a pale veil over the map, the other screens a dark
  one. A paused game has a light veil over the map
- Numbers: the digit variant of each place depends on the digit and its
  place counted from the right, so variants differ along a number but a
  number keeps its look. Prices have coin_icon in front of the amount;
  placed behind it, the icon would read as an extra zero
- The chosen shop item switches between shop_bubble_dark and
  shop_bubble_dark2 every BUBBLE_FLIP ms. The preview grows while one
  shows and shrinks while the other does, between 45 % and 80 % opacity and
  100 % and 108 % size. The range circle does not pulse
- The tower menu bubble sits above the tower, or below it when there is no
  room above, kept inside the map area. Upgrade and sell each take half of
  it as tap area. An armed sell button pulses by 15 % every WARN_PULSE
  seconds with a red glow; there is no timeout
- The early start button draws the countdown on next_wave, without the
  bonus


Input (js/input.js)
-------------------

- Only the first pointer is followed while it is down; it is captured, so
  a drag may leave the canvas
- A press within the footprint radius or PREVIEW_HIT CSS pixels of the
  preview grabs it. Released after a move of at most DRAG_SLOP CSS pixels,
  it builds the tower; a longer move drags it, keeping its offset to the
  pointer
- A press or drag that never finds a legal position shows the red cross
  for REFUSED_TIME where the pointer is let go
- Any button other than sell cancels an armed sell; any button outside
  the tower menu closes it
- A cancelled pointer, for example taken by a system gesture, builds
  nothing


Audio (js/audio.js)
-------------------

- The audio context is created at start and stays suspended until
  audio.start() runs in a tap: the splash screen's sound button, or the
  play button. Older Safari also needs a short silent sound started in the
  handler. Later pointer and touch events resume a context the browser has
  suspended. For the mouse, pointerdown counts as a user interaction; for
  touch, pointerup and touchend do
- A sound that cannot play at once, while audio is still locked, is
  dropped, not queued
- Switching sound off mutes the output at once and stops new sounds. The
  button tap sound plays after the switch, so it is heard when sound goes
  on
- The min_interval setting drops plays of an event that come too soon
  after the last
- Beeps are oscillator notes with the wave form beep_wave, each with a
  falling envelope
- The splash music loops the event's first recording. If it is not
  decoded yet, it starts once it is; on a locked context, with the unlock


Cutting scripts (tools/cut_*.py)
--------------------------------

- cut_sheet.py: one hand-set box per drawing on
  art_sources/source_sheet_2.jpg, at the sheet's scale. In each box the
  largest connected outline, ink darker than INK_LUM, is the drawing; other
  outlines are kept only when they reach KEEP_SHARE of it and do not touch
  the box border. Paper lighter than PAPER_LUM becomes white, the outside
  transparent. The four levels of a t*p strip are written side by side
  with STRIP_GAP empty columns, as the build expects. The files
  game_won.png and game_won_button.png were cut once from
  art_sources/game_won.jpg with its cut function; the scan has no boxes in
  the script
- cut_digits.py: every connected stroke on digits.png darker than INK_LUM
  is one character; strokes under MIN_PIXELS are specks. Rows are told
  apart by the stroke centre, and the characters of each row are listed in
  ROWS. All characters are scaled to the median height of the drawings
- cut_sounds.py: high-pass filter at HIGHPASS, trim to the voice (frames
  within THRESHOLD_DB of the loudest), split recordings with variants at
  their longest pauses (VARIANTS), fade the ends, bring every sound to the
  same peak level, write mono MP3. SKIP drops the start of a recording,
  LOOP_GAP adds silence after a looping sound