view doc/balance.md @ 32:85d23328a215 0.10.0

added intro music, logic for browser audio triggering on splash screen
author ark
date 2026-10-20 18:39 +0200
parents 3f2811aebb3d
children
line wrap: on
line source

balance
-------

all game numbers in one place. the values used by the game are in the config
file (see architecture.md); this document explains them and records where each
one comes from.

source markers:

- notes: from the original design notes or the original drawing sheet,
  decided
- decided: decided after the notes were written
- guess: a starting value for playtesting, expected to change
- derived: calculated from other values

notes and decided values are not fixed either. they come from the original
drawing sheet and the first design rounds, made without experience in game
design, and are all open to change in balancing.

units: distances are in map pixels, that is pixels of the map image as
scanned. times are in seconds, speeds in map pixels per second.


scale
-----

| item                         | value | source |
|------------------------------|-------|--------|
| map images                   | 1.0   | notes  |
| towers, monsters, projectiles | 0.25 | notes ("maps scaled approx 400%") |
| coins                        | 0.4   | guess, 0.25 makes them about 10 pixels |
| hp bars                      | 0.25  | guess  |

measured from the earlier masks, which covered the whole road (approximate,
from red area divided by path width):

| map  | size    | path width | path length, all branches |
|------|---------|------------|---------------------------|
| map0 | 705x697 | 56         | 1930 |
| map1 | 663x590 | 48         | 1350 |
| map2 | 618x537 | 42         | 2075 |
| map3 | 642x459 | 52         | 905  |

a single route on map1 or map2 is shorter than the total. exact route lengths
come from the build (see architecture.md).


monsters
--------

| monster | hp | coins | coins per hp | speed | source |
|---------|----|-------|--------------|-------|--------|
| m0      | 5  | 1     | 0.20         | 35    | hp, coins: notes; speed: guess |
| m1      | 10 | 1     | 0.10         | 40    | hp: notes; coins: decided (was 2); speed: guess |
| m2      | 15 | 2     | 0.13         | 45    | hp: notes; coins: decided (was 3); speed: guess |
| m3      | 20 | 2     | 0.10         | 40    | hp: notes; coins: decided (was 4); speed: guess |
| m4      | 30 | 3     | 0.10         | 35    | hp: notes; coins: decided (was 6); speed: guess |
| b0      | 150 | 30   | 0.20         | 25    | hp: decided, 5 x the notes; coins: notes; speed: guess |
| b1      | 250 | 50   | 0.20         | 25    | as above |
| b2      | 350 | 70   | 0.20         | 25    | as above |
| b3      | 500 | 100  | 0.20         | 25    | as above |

monster hp is the same on every map (decided). a monster that took two hits
on one map must not take four on a later one. a map gets harder through the
size of its waves and the mix of monsters.

the notes had every monster drop 0.2 coins per hp. regular monster coins are
now set apart from hp (decided): m0 keeps 0.2, the others drop 0.10 to 0.13.
the mix of monsters then sets the coins per hp of a map. the notes gave
bosses hp equal to their coins; balancing raised it to 5 times that
(decided).

time to walk the whole path at 35-45 speed (derived): map0 about 50 s, map3
about 23 s.

animation (all guesses):

- one hop takes 0.35 s
- hop height 5
- squash on landing 6 % (wider and flatter), stretch in the air 4 %
- idle bob 2 %
- each monster starts at a random point in its hop cycle, so a group does not
  hop in step
- the death puff shows for 0.4 s

variation between monsters, so a group does not walk in single file (all
guesses):

- each monster's speed differs from its table speed by up to 5 % either way.
  faster ones overtake slower ones
- a monster walks beside the centre line of the road, at most 0.45 of the
  road half width away from it: 8.6, 7.7, 7.2 and 9.9 pixels by map
- half of that is a fixed lean to one side, chosen at spawn. the other half
  wanders, with a new random value every 40 pixels along the route and a
  smooth change between them
- the offset grows from 0 over the first 20 pixels of the route
- on the inside of tight bends the offset is reduced, so a monster never moves
  backwards. monsters may bunch up there


route preference
----------------

on maps with several routes, a monster picks one at spawn. faster monsters
prefer longer or straighter routes, slower ones prefer shortcuts (decided).

proposed rule (guess):

- for each route the build calculates its length and how much it turns (sum of
  direction changes along the route)
- each route gets a value between 0 and 1: the average of its length relative
  to the longest route and its straightness relative to the straightest route
- each monster gets a speed factor: its speed minus the reference speed,
  divided by the reference speed, limited to -1..1
- route weight = 1 + bias * speed factor * (2 * route value - 1), at least 0.1
- a monster at the reference speed sees all routes as equally likely
- a boss always takes the shortest route (decided)

| parameter       | value | source |
|-----------------|-------|--------|
| reference speed | 40    | guess  |
| bias            | 3     | guess  |

with the speeds above, m2 is the only regular monster faster than the
reference, and the effect on it is small. the speeds may need to spread out
more for the preference to be visible.


towers
------

towers have 5 levels (decided). the config sets damage, range, slow and cost
for level 1 and level 5 only. the build fills in the levels between in equal
steps, rounded to whole numbers where both ends are whole.

the drawing sheet gives five damage values for t0 and t1, and four for t2 and
t3. level 5 of t3 continues the trend (guess). t2 does no damage (decided);
it only slows. the equal steps change
three values from the sheet: t1 level 2 from 7 to 8, t1 level 4 from 17 to 18,
and t3 level 2 from 5 to 4 (decided).

| tower | damage by level  | cost | range by level      | attack every | projectile speed | source |
|-------|------------------|------|---------------------|--------------|------------------|--------|
| t0    | 5,10,15,20,25    | 5    | 60,63,66,69,72      | 1.5 s        | none (melee)     | damage, cost: notes; range level 1: decided (was 45); rest: guess |
| t1    | 3,8,13,18,23     | 10   | 100,104,108,112,116 | 0.7 s        | 300              | damage levels 1 and 5, cost: notes; range: guess |
| t2    | none             | 20   | 90,94,98,102,106    | 1.2 s        | 220              | damage: decided; cost: notes; rest: guess |
| t3    | 1,4,7,10,13      | 25   | 220,230,240,250,260 | 1.3 s        | 800              | damage level 1: notes; cost: decided (was 40), may go lower; rest: guess |

the cost column is the build cost, level 1.

rules:

- attack speed stays the same at every level (decided). t0 is slower than the
  others (decided), t1 faster than t2, t2 faster than t3 (notes)
- range grows about 4 % per level (decided "slightly", amount guessed)
- t0 damages every monster in range at once, each attack
- t2 slow: p2 bursts where it lands into a freeze splash of radius 28,
  about two monsters across (decided, radius guessed). every monster the
  splash touches moves at 0.60, 0.55, 0.50, 0.45, 0.40 of its speed by
  level, for 2 s (guess). a new hit restarts the time. slows do not add up;
  the strongest one applies
- p1, p2, p3 home in on their target. if the target dies first, the
  projectile flies on to the last position and disappears (guess)
- towers aim at the monster furthest along its route (guess)

costs: the price of building, or of the upgrade to a level. they started as
level n costs base cost x n (decided); the config sets levels 1 and 5:

| tower | level 1 (build) | level 2 | level 3 | level 4 | level 5 | total |
|-------|-----------------|---------|---------|---------|---------|-------|
| t0    | 5               | 10      | 15      | 20      | 25      | 75    |
| t1    | 10              | 20      | 30      | 40      | 50      | 150   |
| t2    | 20              | 40      | 60      | 80      | 100     | 300   |
| t3    | 25              | 50      | 75      | 100     | 125     | 375   |

selling refunds half of everything paid for the tower, build and upgrades,
rounded down (decided). a tower is sold whole.

| tower | level 1 | level 2 | level 3 | level 4 | level 5 |
|-------|---------|---------|---------|---------|---------|
| t0    | 2       | 7       | 15      | 25      | 37      |
| t1    | 5       | 15      | 30      | 50      | 75      |
| t2    | 10      | 30      | 60      | 100     | 150     |
| t3    | 12      | 37      | 75      | 125     | 187     |

damage per second against a single target, at the guessed attack times
(derived):

| tower | level 1 | level 2 | level 3 | level 4 | level 5 |
|-------|---------|---------|---------|---------|---------|
| t0    | 3.3     | 6.7     | 10.0    | 13.3    | 16.7    |
| t1    | 4.3     | 11.4    | 18.6    | 25.7    | 32.9    |
| t3    | 0.8     | 3.1     | 5.4     | 7.7     | 10.0    |

t3 is meant to be weaker than the other towers; its range lets it reach more
targets (decided). to keep the gap moderate, its cost went from 40 to 25 and
its attack time from 2.0 s to 1.3 s, just slower than t2 as the notes require
(decided, amount guessed). at level 1 it deals about a fifth of t1's damage per
second at two and a half times the cost. its range covers more path, roughly
three times as much on a winding map (estimate).


placement
---------

| parameter                                  | value | source |
|--------------------------------------------|-------|--------|
| tower footprint radius                     | 16    | guess  |
| road half width, centre line to road edge  | per map: 19, 17, 16, 22 | measured from the earlier masks |
| largest gap between footprint and road edge | 14   | guess  |
| space between footprint and spawn point    | per map: 50 | decided |
| space between footprint and exit point     | per map: 40 | decided |
| smallest distance between tower centres    | 40    | decided (was 32, two footprints; towers stood too close together) |
| distance from the map edge                 | 16    | guess  |
| snap radius, how far a tap may be from a legal position | 48 | guess |
| placement grid cell                        | 4     | guess  |

towers may not be placed on the entrance or exit areas.

the spawn point is where the path leaves the blue entrance area. the space
between it and a tower footprint is one to one and a half times the average
tower size (decided). level 1 towers average about 36 pixels across at sprite
scale 0.25, so the space is 36 to 53 pixels.

the exit point is where the path enters the green exit area. towers keep a
somewhat smaller space from it (decided), so they do not cover the building
drawn at the exit.


economy
-------

| map  | starting coins |
|------|----------------|
| map0 | 10             |
| map1 | 20             |
| map2 | 40             |
| map3 | 60             |

starting coins are from the notes. the source sheet gives 50 for map3. the
notes raise it to 60 to make the step from map1 to map3 more even (10, 20,
40, 60 instead of 10, 20, 40, 50). likely to change after playtesting.

the notes set the coins dropped per map to 10 times the starting coins. this
is no longer a target (decided): the coins a map pays follow from its waves.
the build prints, per map, the coins dropped by regular monsters and the
boss, the total with the starting coins, and the coins per hp of the regular
monsters.

coins per map from the current wave lists (derived):

| map  | regular monsters | boss | dropped in all | with starting coins | regular hp | coins per hp |
|------|------------------|------|----------------|---------------------|------------|--------------|
| map0 | 48               | 30   | 78             | 88                  | 395        | 0.12         |
| map1 | 139              | 50   | 189            | 209                 | 1145       | 0.12         |
| map2 | 326              | 70   | 396            | 436                 | 2615       | 0.12         |
| map3 | 157              | 100  | 257            | 317                 | 1160       | 0.14         |

early start bonuses come on top.

all monster levels appear on all maps (decided).


waves
-----

| parameter                          | value               | source |
|------------------------------------|---------------------|--------|
| waves per map                      | 6, 8, 10, 12        | guess  |
| break before the first wave        | 40 s                | guess  |
| break between waves, from the last spawn of a wave | 25 s | guess ("longer", decided) |
| time between spawns               | 1.0, 0.65, 0.65, 0.5 s by map | guess, fitted in the simulation |
| early start bonus, coins per second left | 0.05, 0.1, 0.2, 0.3 by map | guess (starting coins / 200) |

the bonus is rounded down. if every break is skipped at once, the bonus adds
about 6, 17, 45 and 82 coins per map (derived).

shape of a map's waves (decided): each wave is at least as hard as the one
before, and the last wave is the largest. the boss comes in the last wave and
spawns first. early waves are mostly m0 and m1; later waves mix monster
levels.

the wave lists in the config are drafted by tools/balance.py
(architecture.md) from a few numbers per map, and may be edited by hand
afterwards:

- the number of waves
- the number of regular monsters in the first and in the last wave. the waves
  between grow in equal steps
- the share of each monster level in the map's regular monsters, by count
- the wave in which each monster level first appears

each monster level gets a weight per wave that grows by one per wave from its
first wave. the weights are scaled until the wave sizes and the shares are
met, then rounded to whole monsters per wave. later waves therefore hold more
of the levels that came in late. every level keeps at least one monster.

the draft numbers are kept in the map's section of the config, next to the
other map settings. the game does not read them.

draft numbers, fitted in the simulation to the targets in balancing
(guess):

| map  | waves | regular monsters, first and last wave | share by count, m0-m4 | first wave of m0-m4 |
|------|-------|-----------------|---------------------|---------------|
| map0 | 6     | 4, 7            | 36, 25, 18, 12, 9 % | 1, 2, 2, 4, 5 |
| map1 | 8     | 6, 17           | 36, 25, 18, 12, 9 % | 1, 2, 3, 5, 6 |
| map2 | 10    | 10, 36          | 39, 26, 17, 11, 7 % | 1, 2, 4, 6, 8 |
| map3 | 12    | 5, 16           | 52, 26, 13, 6, 3 %  | 1, 2, 4, 6, 9 |

resulting monster counts (derived):

| map  | m0 | m1 | m2 | m3 | m4 | regular monsters |
|------|----|----|----|----|----|------------------|
| map0 | 12 | 9  | 5  | 4  | 3  | 33               |
| map1 | 32 | 22 | 17 | 12 | 9  | 92               |
| map2 | 90 | 59 | 39 | 27 | 15 | 230              |
| map3 | 65 | 33 | 17 | 8  | 3  | 126              |

map3 has fewer and weaker monsters than map2. its 60 starting coins already
pay for much of what the naive player needs against b3, so the waves before
may pay only about 130 coins (see balancing).


hearts
------

| parameter               | value | source |
|-------------------------|-------|--------|
| hearts per map          | 10    | decided |
| heart cost of a monster | 1     | decided |
| heart cost of a boss    | 5     | decided |


coins in flight
---------------

| parameter                        | value | source |
|----------------------------------|-------|--------|
| flight time to the counter       | 0.7 s | guess  |
| most coin sprites for one death  | 8     | guess  |

coins are added to the counter at the moment the monster dies; the flying
coins only show it (guess). the same holds for an early start bonus, whose
coins fly from the exits. this keeps the counter correct for a player who
builds right after a kill.


controls
--------

| parameter  | value  | source |
|------------|--------|--------|
| game speeds | 1x, 2x, 4x | decided |


balancing
---------

the first values were set without a way to estimate their effect, and the
game was far too easy. two t1 towers won every map, t2 was never needed, and
many coins were left over when a map was won. the aim is a game where the
player has to think about where to place towers and which ones to build. a
player who only buys plain firepower should need nearly every coin a map
pays in order to win it. coins left over then come from better choices.

two scripted players mark the range (decided):

- naive: builds t1 only, on placement cells of median coverage, whenever it
  can afford one. it should win with about 5 of its 10 hearts left and with
  next to no coins at the start of the last wave
- greedy: builds on the best cells, and upgrades when no good cell is left.
  it should win with coins to spare

rules for changing values:

- shop prices and starting coins stay multiples of 5. round upgrade prices
  are preferred
- values the player does not see are free: hp, damage, coin drops, speeds,
  attack times
- shop prices are set first. hp, damage and the wave mix are then fitted to
  them
- the main control is coins per hp, set by the mix of monsters. m0 drops
  0.2 coins per hp, the other regular monsters 0.10 to 0.13. a mix with
  fewer m0 pays less for the same hp
- monster hp stays the same on every map. wave size and the mix make a map
  harder

the coins of the last wave, the boss's included, arrive when the map is as
good as won. leftover coins are therefore measured at the start of the last
wave. the last wave's coins are left out of the calculation. the boss keeps
its drop as a visible reward (decided).

the calculation goes wave by wave, not per map. at the start of each wave
the player has the starting coins, the coins dropped in earlier waves and
any early start bonus. these coins buy a certain amount of firepower. the
wave sets two demands on it:

- throughput: the towers' damage per second has to keep up with the hp
  arriving per second. the time a monster spends in range gives some buffer
- single target: the toughest monster of the wave, m4 or the boss, has to
  die within its time in range

damage is counted in whole shots. t1 at level 1 needs 2 shots for the 5 hp
of an m0, not five thirds of one.

simplifications in tools/balance.py:

- a tower attacks from the first monster of a wave reaching it until the
  last has passed. it is idle when no monster is in range. with route
  length c in range and spacing s between monsters, the share of time with
  a monster in range is taken as 1 - e^(-c/s)
- for the single target demand the monster gets every tower's full
  attention while in range. in the game, faster monsters overtake the
  boss and draw fire away from it
- the naive player builds at the start of a wave with the coins on hand
  and never starts a wave early. the greedy player always starts early and
  takes the bonus. coins dropped during a wave count from the next wave
- monsters that leak cost one heart each and drop no coins, so a leaky
  wave leaves less for the next one
- routes are equally likely; the route preference is left out. the boss
  takes the shortest route
- towers keep their level 1 range when upgraded

the time a monster spends in range comes from coverage: the length of route
within a tower's range, measured for every legal placement cell from the
build's route and placement data. the median cell stands for the naive
player and the best cells for the greedy one. for more than one tower,
cells are picked one after the other, each counting only route that no
earlier tower covers. maps with several routes are measured per route.

t1 coverage at range 100 and footprint radius 16, in map pixels of route
within range, the mean over routes on maps with several. top 10 % is the
coverage the best tenth of the cells reach:

| map  | routes | legal cells | best | top 10 % | median |
|------|--------|-------------|------|----------|--------|
| map0 | 1      | 2893        | 415  | 360      | 228    |
| map1 | 3      | 2132        | 325  | 232      | 129    |
| map2 | 6      | 1857        | 276  | 195      | 111    |
| map3 | 1      | 1369        | 422  | 311      | 174    |

route lengths: map0 2068, map1 737 to 1048, map2 722 to 1363, map3 1035.

value per coin is damage per second divided by all coins paid for the
tower. for t1 it falls with level: 0.43, 0.38, 0.31, 0.26 and 0.22. this
is the intended trade between upgrading and building another tower. t0 at
level 1 reaches 0.67 against a single target, kills an m0 in one hit and
damages every monster in range. it may be the better buy for a naive
player than t1, so the calculation lists value per coin for every tower and
level, area damage included. the simulation measures 1.2 to 1.7 monsters
hit per t0 attack over whole runs, on median cells as on the best ones.
at that rate t0 at level 1 gives 0.8 to 1.1 against m0, two to three times
t1.

t2 works as a time multiplier. a slow to 0.6 of the speed gives 1.67 times
the time in range for every tower covering the same stretch. this only
helps against the single target demand, so t2 needs faster monsters or
tough monsters spaced apart to pay off. it is tuned after the t1 baseline.

tools/simulate.js (architecture.md) plays the same two players in the game
code. with the fitted waves, the two tools agree on the coins on hand at
each wave. they differ in the coins a wave needs:

- the boss needs 5 to 20 % more in the simulation: 49, 143, 338 and 206
  coins for the naive player against 40, 130, 320 and 170 in the
  calculation. faster monsters overtake the boss and draw fire away from it
- regular waves on map2 need up to 1.5 times as much. towers on median
  cells cover the six routes unevenly, and monsters on a route with little
  cover get through. the mean over routes hides this
- about a tenth of the shots are lost because the target died before the
  projectile arrived
- route preference changes nothing: with bias 0 the results are the same,
  since the speeds lie close to the reference speed
- the players in the simulation also build during a wave, with coins from
  the monsters killed in it

tools/balance.py was left as it is. the waves were fitted with the
simulation.

with the fitted waves the naive player loses hearts to regular monsters
only in the first two or three waves. towers stay and add up. what a wave
demands, hp arriving per second, follows from the mix and the spawn
interval; wave size sets how long the wave lasts and what it pays. the boss
is the one demand that grows faster than the coins. the naive player loses
its hearts there, 5 at once, in most runs. the target of about 5 hearts
left is met on average over the seeds: the boss gets through in most runs
and dies in a few.

order of work (decided):

1. the calculation, tools/balance.py (architecture.md): value per coin for
   every tower, the wave demands against t1 only
2. shop prices fixed, then hp, damage and waves fitted to them
3. t2 after the t1 baseline
4. a headless simulation of the real game code with the two scripted
   players confirms the result