view doc/balancing.md @ 36:a464bd43b76c 1.0.0

clean documentation rewrite
author ark
date 2026-10-21 22:34 +0200
parents
children
line wrap: on
line source

Balancing
---------

How the game's numbers are tuned, and the two tools that help with it.
The numbers themselves are in config/game.toml. This document does not
record results; run the tools for current figures.


Aim
---

The player should have 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 to win it. Coins left over then come from better
choices.

Two scripted players mark the range:

- Naive: builds t1 only, at level 1, on placement cells of median
  coverage, whenever it can afford one. Never starts a wave early. It
  should win with about half its hearts left (HEARTS_TARGET in both
  tools) and next to no coins at the start of the last wave
- Greedy: builds t1 on the best cells and upgrades when that adds more
  damage per second per coin than a new tower. Starts every wave early for
  the bonus. It should win with coins to spare

A boss that gets through loses the map, so a player who wins has held the
boss. The hearts target dates from when a boss cost 5 hearts; see
roadmap.md.

Coverage of a cell is the length of route within a tower's range from it,
the mean over the map's routes.

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, and the last wave's coins are left out.


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: hit points, damage, coin
  drops, speeds, attack times
- Shop prices are set first. Hit points, damage and the wave mix are then
  fitted to them
- The main control is coins per hit point, set by the mix of monsters. The
  monster m0 pays more coins per hit point than the other regular
  monsters, so a mix with fewer m0 pays less for the same hit points
- Monster hit points stay the same on every map. Wave size and the mix
  make a map harder
- Each wave is at least as hard as the one before, and the last wave is
  the largest. Early waves are mostly m0 and m1; later waves mix levels

Workflow: change config/game.toml, build, run tools/simulate.js and read
temp/preview/simulate.txt. The script tools/balance.py gives a faster
estimate and drafts wave lists.


Drafting waves
--------------

`python3 tools/balance.py --draft [MAP]` prints wave lists as TOML, to
paste over a map's [[map.N.wave]] lists. They are made from the draft
numbers in the map's section, which the game does not read:

- wave_count: the number of waves
- wave_size: regular monsters in the first and in the last wave. The waves
  between grow in equal steps
- monster_share: the share of each regular monster, m0 to m4, by count
- first_wave: the wave in which each regular monster first appears

Each monster gets a weight per wave that grows by one per wave from its
first wave. The weights are scaled, in turn per wave and per monster,
until the wave sizes and the shares are met, then rounded to whole
monsters. Later waves therefore hold more of the monsters that came in
late. Every monster keeps at least one, and the boss goes into the last
wave. Below each map the tool prints what the calculation makes of the
draft, and warns about a wave with fewer hit points than the one before.
Nothing is written in this mode. The lists may be edited by hand after
pasting.


tools/simulate.js
-----------------

Plays the real game logic with scripted players. Run by hand with Node
after a build:

    node tools/simulate.js
    node tools/simulate.js --maps 2 --players naive,naive_t0 --seeds 10

It loads www/data/config.js, www/data/maps.js and the logic files util.js,
assets.js, map.js, monster.js, tower.js, projectile.js, wave.js and
game.js, as the browser would. The files render.js, ui.js, input.js,
debug.js and main.js are left out, so the debug switches have no effect.
Audio is a stub.

- The game advances in steps of 1/60 s; game speed plays no part. Each
  seed is passed to the game, so a run can be repeated
- Players buy through the game's own functions (shop choice, preview,
  build, tower selection, upgrade, early start), so placement rules and
  prices are the game's. They buy the moment they can afford the next
  purchase, also during a wave
- Players: naive and greedy as above, naive_t0 (t0 only) and naive_mix (t1
  and t0 in turn). The list and their rules are at the top of the file
- The coins a wave needs are measured by playing that wave alone from an
  empty map, with the first purchases of the player's list, and searching
  for the fewest that lose no heart. By default only the last wave is
  measured; `--need all` measures every wave
- `--waves FILE` takes wave lists from a JSON file in place of the
  config's, for trying drafts without a build. `--config FILE` uses
  another config.js. `--json` prints the results as JSON and writes
  nothing

It writes temp/preview/simulate.txt (per map and player: maps won, hearts
left, coins on hand and the surplus at the start of the last wave, hearts
lost per wave, and monsters hit per t0 attack) and
temp/preview/simulate.json, which tools/balance.py reads.


tools/balance.py
----------------

Estimates, map by map and wave by wave, whether the coins a map pays buy
enough firepower. It only calculates, against t1, and is much faster than
the simulation. Run it by hand after a build, as it reads the routes and
placement grid from temp/build/maps.json:

    python3 tools/balance.py

It needs NumPy and Matplotlib, which the build does not. It writes to
temp/preview/:

- balance.txt: value per coin for every tower and level; per map the
  coverage of each tower's range, and per wave the coins on hand, the
  damage rate the wave demands, and the coins the naive and greedy players
  need to hold it; t0's value per coin with area damage
- balance_value.png: value per coin by tower level
- balance_map0.png to balance_map3.png: coins per wave, and the surplus at
  the last wave and the hearts left against a wave size factor. The factor
  scales the number of regular monsters in every wave; their coins follow

Method:

- At the start of each wave the player has the starting coins, the coins
  dropped in earlier waves and, for greedy, the early start bonus. It buys
  what it can in its order. Coins dropped during a wave count from the
  next wave
- The wave sets two demands. Throughput: the towers' damage has to keep up
  with the hit points arriving. 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: a tower that needs 2 shots for a
  monster spends 2 attacks on it
- A tower attacks from the first monster of a wave reaching it until the
  last has passed, and 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
- Leaked monsters cost hearts and drop no coins, so a leaky wave leaves
  less for the next one
- Naive uses the median coverage of all legal cells. Greedy picks cells
  one after the other, each counting only route that no earlier pick
  covers. Maps with several routes are measured per route and averaged
- Value per coin is damage per second divided by all coins paid for the
  tower, with whole shots against each regular monster
- The tower t0 hits several monsters per attack. Its value per coin is
  multiplied by the monsters hit per attack, as measured by the simulation
  when simulate.json exists, otherwise estimated from coverage and spacing

Simplifications and limits:

- Only t1 is considered for the wave demands. The tower t2 works as a time
  multiplier (a slow to a share k of the speed gives 1/k the time in range
  for every tower covering the same stretch), which helps only against the
  single target demand. It is not modelled
- Routes are equally likely; the route preference is left out. The boss
  takes the shortest route
- Towers keep their level 1 range when upgraded
- Towers that only slow are left out of the value per coin table


Where the tools differ
----------------------

The two tools agree on the coins on hand at each wave. They differ in the
coins a wave needs, for reasons the calculation leaves out:

- The boss needs more in the simulation: faster monsters overtake it and
  draw fire away from it
- On maps with several routes, towers on median cells cover the routes
  unevenly, and monsters on a route with little cover get through. The
  mean over routes hides this. It shows most on map2, with six routes
- Some shots are lost because the target died before the projectile
  arrived
- The simulated players also build during a wave, with coins from the
  monsters killed in it

The waves in the config were fitted with the simulation.