changeset 10:22b604680c5a

adjusted monster movement to be less uniform
author ark
date 2026-10-19 16:56 +0200
parents 53f015966118
children a77857594150
files config/game.toml doc/architecture.md doc/balance.md doc/game_design.md game/data/config.js game/js/map.js game/js/monster.js tools/build.py
diffstat 8 files changed, 151 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/config/game.toml	Mon Oct 19 15:55:37 2026 +0200
+++ b/config/game.toml	Mon Oct 19 16:56:38 2026 +0200
@@ -82,6 +82,15 @@
 death_puff = 0.4    # guess, seconds the death puff shows
 
 
+# variation between monsters, so they do not walk in single file
+[movement]
+speed_variation = 0.05  # guess, each monster's speed differs by up to this share either way
+offset = 0.45           # guess, largest sideways offset from the centre line, share of the road half width
+lean = 0.5              # guess, fixed part of a monster's offset, share of the largest offset
+wander_step = 40        # guess, distance along the route between the wandering part's values
+fade_in = 20            # guess, distance from the route start over which the offset grows from 0
+
+
 [route_preference]
 reference_speed = 40    # guess
 bias = 3                # guess
--- a/doc/architecture.md	Mon Oct 19 15:55:37 2026 +0200
+++ b/doc/architecture.md	Mon Oct 19 16:56:38 2026 +0200
@@ -157,6 +157,13 @@
 replace a route with hand-placed points, for cases where the automatic result
 is wrong.
 
+the game walks a monster along the route's centre line, by distance, and
+draws it beside the line. the offset is set per monster at spawn
+(balance.md, monsters) and needs no route data from the build. when a map
+loads, each route works out the sideways direction at its points and how far
+an offset may go on the inside of each bend. towers rank monsters by their
+distance along the centre line, so the offset does not change the order.
+
 
 config
 ------
--- a/doc/balance.md	Mon Oct 19 15:55:37 2026 +0200
+++ b/doc/balance.md	Mon Oct 19 16:56:38 2026 +0200
@@ -71,6 +71,20 @@
   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
 ----------------
--- a/doc/game_design.md	Mon Oct 19 15:55:37 2026 +0200
+++ b/doc/game_design.md	Mon Oct 19 16:56:38 2026 +0200
@@ -95,6 +95,8 @@
 - monsters emulate animation with slight bobbing, squish/stretch on x and y by a
   few percent; walking animation is by small hops; hops and animation may
   overlap out of red path
+- monsters do not walk in single file: each walks a little to the side of the
+  path, drifting from side to side, and at a slightly different speed
 - bosses are map-specific, b0 belongs to map0 etc; boss hp 30/50/70/100 coin
   drop equal to hp
 - hp bars are always shown above every monster. the boss uses its own bar, also
--- a/game/data/config.js	Mon Oct 19 15:55:37 2026 +0200
+++ b/game/data/config.js	Mon Oct 19 16:56:38 2026 +0200
@@ -67,6 +67,13 @@
   "hit_radius": 0.4,
   "death_puff": 0.4
  },
+ "movement": {
+  "speed_variation": 0.05,
+  "offset": 0.45,
+  "lean": 0.5,
+  "wander_step": 40,
+  "fade_in": 20
+ },
  "route_preference": {
   "reference_speed": 40,
   "bias": 3,
--- a/game/js/map.js	Mon Oct 19 15:55:37 2026 +0200
+++ b/game/js/map.js	Mon Oct 19 16:56:38 2026 +0200
@@ -5,9 +5,16 @@
 MM.Route = (function () {
     "use strict";
 
+    // sideways offsets on the inside of a bend: share of the distance at
+    // which the offset point would stop moving forward, and how fast the
+    // limit may change per pixel along the route
+    var BEND_SHARE = 0.7;
+    var LIMIT_SLOPE = 0.5;
+
     // a route from the build: a polyline with cumulative distances, so a
-    // point can be found by the distance walked
-    function Route(data) {
+    // point can be found by the distance walked. maxOffset is the largest
+    // sideways offset that shifted() will be asked for.
+    function Route(data, maxOffset) {
         var p = data.points;
         this.xs = [];
         this.ys = [];
@@ -22,8 +29,60 @@
         this.length = d;
         this.value = data.value;
         this.weight = data.weight;
+        this.sideways(maxOffset);
     }
 
+    // set up the sideways direction at each point (nx, ny) and how far an
+    // offset may go to the left and right there (left, right)
+    Route.prototype.sideways = function (maxOffset) {
+        var xs = this.xs, ys = this.ys, at = this.at, n = xs.length, i;
+        // direction of travel of segment i, from point i - 1 to point i
+        var tx = [0], ty = [0];
+        for (i = 1; i < n; i++) {
+            var len = at[i] - at[i - 1];
+            tx.push((xs[i] - xs[i - 1]) / len);
+            ty.push((ys[i] - ys[i - 1]) / len);
+        }
+        // at each point, the left side as drawn, averaged over the two
+        // segments that meet there
+        this.nx = [];
+        this.ny = [];
+        for (i = 0; i < n; i++) {
+            var a = Math.max(i, 1), b = Math.min(i + 1, n - 1);
+            var sx = ty[a] + ty[b], sy = -tx[a] - tx[b], sl = Math.hypot(sx, sy);
+            if (sl < 1e-6) { sx = ty[a]; sy = -tx[a]; sl = 1; }
+            this.nx.push(sx / sl);
+            this.ny.push(sy / sl);
+        }
+        // along a segment the sideways direction turns. an offset on the
+        // inside of the turn moves forward at 1 + offset * k of the walking
+        // speed, k being negative on that side. limit the offset at both
+        // ends of the segment so this stays above 1 - BEND_SHARE
+        var left = [], right = [];
+        for (i = 0; i < n; i++) {
+            left.push(maxOffset);
+            right.push(maxOffset);
+        }
+        for (i = 1; i < n; i++) {
+            var k = ((this.nx[i] - this.nx[i - 1]) * tx[i]
+                     + (this.ny[i] - this.ny[i - 1]) * ty[i]) / (at[i] - at[i - 1]);
+            var lim = BEND_SHARE / Math.abs(k);
+            var side = k < 0 ? left : right;
+            side[i - 1] = Math.min(side[i - 1], lim);
+            side[i] = Math.min(side[i], lim);
+        }
+        // let the limits change gradually, so monsters drift towards the
+        // centre line before a tight bend rather than at it
+        [left, right].forEach(function (lim) {
+            for (i = 1; i < n; i++)
+                lim[i] = Math.min(lim[i], lim[i - 1] + LIMIT_SLOPE * (at[i] - at[i - 1]));
+            for (i = n - 2; i >= 0; i--)
+                lim[i] = Math.min(lim[i], lim[i + 1] + LIMIT_SLOPE * (at[i + 1] - at[i]));
+        });
+        this.left = left;
+        this.right = right;
+    };
+
     // write the point at distance s along the route into out.x, out.y.
     // out.seg remembers the segment, so the next search for a point
     // further along starts there
@@ -41,6 +100,23 @@
         return out;
     };
 
+    // as point(), moved sideways by d: positive to the left of the direction
+    // of travel as drawn, negative to the right. on the inside of bends d is
+    // reduced, so the point keeps moving forward as s grows
+    Route.prototype.shifted = function (s, d, out) {
+        this.point(s, out);
+        var at = this.at, n = at.length, i, t;
+        if (s <= 0) { i = 1; t = 0; }
+        else if (s >= this.length) { i = n - 1; t = 1; }
+        else { i = out.seg; t = (s - at[i - 1]) / (at[i] - at[i - 1]); }
+        var lim = d > 0 ? this.left : this.right;
+        var m = lim[i - 1] + (lim[i] - lim[i - 1]) * t;
+        if (Math.abs(d) > m) d = d > 0 ? m : -m;
+        out.x += d * (this.nx[i - 1] + (this.nx[i] - this.nx[i - 1]) * t);
+        out.y += d * (this.ny[i - 1] + (this.ny[i] - this.ny[i - 1]) * t);
+        return out;
+    };
+
     return Route;
 })();
 
@@ -60,7 +136,10 @@
         this.name = d.name;
         this.width = d.width;
         this.height = d.height;
-        this.routes = d.routes.map(function (r) { return new MM.Route(r); });
+        // largest sideways offset of a monster from the centre line
+        this.maxOffset = cfg.movement.offset * cfg.maps[index].path_half_width;
+        var maxOffset = this.maxOffset;
+        this.routes = d.routes.map(function (r) { return new MM.Route(r, maxOffset); });
     }
 
     GameMap.prototype.legalCell = function (cx, cy) {
--- a/game/js/monster.js	Mon Oct 19 15:55:37 2026 +0200
+++ b/game/js/monster.js	Mon Oct 19 16:56:38 2026 +0200
@@ -13,21 +13,32 @@
     // the map's route weights
     function create(game, kind) {
         var def = cfg.monster[kind];
+        var mv = cfg.movement;
         var map = game.map;
-        var r = MM.util.pick(map.routeWeights(def.speed), game.random);
+        var speed = def.speed * (1 + mv.speed_variation * (2 * game.random() - 1));
+        var r = MM.util.pick(map.routeWeights(speed), game.random);
+        var route = map.routes[r];
+        // sideways offsets at every wander_step along the route: a fixed
+        // lean plus a wandering part, together within the map's largest offset
+        var lean = mv.lean * map.maxOffset * (2 * game.random() - 1);
+        var wander = (1 - mv.lean) * map.maxOffset;
+        var offsets = [];
+        for (var i = Math.ceil(route.length / mv.wander_step) + 2; i > 0; i--)
+            offsets.push(lean + wander * (2 * game.random() - 1));
         var size = MM.assets.size(kind, cfg.scale.sprite);
         var m = {
             kind: kind,
             boss: !!def.boss,
-            route: map.routes[r],
+            route: route,
             routeIndex: r,
             s: 0,
             x: 0,
             y: 0,
             seg: 1,
+            offsets: offsets,
             hp: def.hp,
             maxHp: def.hp,
-            speed: def.speed,
+            speed: speed,
             coins: def.coins,
             w: size[0],
             h: size[1],
@@ -41,6 +52,16 @@
         return m;
     }
 
+    // sideways offset at the distance walked: changes smoothly between the
+    // values at every wander_step, and grows from 0 over the first fade_in
+    function offset(m) {
+        var mv = cfg.movement, u = m.s / mv.wander_step;
+        var i = Math.floor(u), t = (1 - Math.cos(Math.PI * (u - i))) / 2;
+        var d = m.offsets[i] + (m.offsets[i + 1] - m.offsets[i]) * t;
+        var f = Math.min(m.s / mv.fade_in, 1);
+        return d * (1 - Math.cos(Math.PI * f)) / 2;
+    }
+
     // share of its speed a monster keeps: the strongest running slow
     // applies, slows do not add up. 1 when not slowed.
     function slowFactor(m) {
@@ -71,7 +92,7 @@
         var k = slowFactor(m);
         m.s += m.speed * k * dt;
         m.phase = (m.phase + k * dt / cfg.animation.hop_time) % 1;
-        m.route.point(m.s, m);
+        m.route.shifted(m.s, offset(m), m);
         return m.s >= m.route.length;
     }
 
--- a/tools/build.py	Mon Oct 19 15:55:37 2026 +0200
+++ b/tools/build.py	Mon Oct 19 16:56:38 2026 +0200
@@ -327,9 +327,9 @@
 
 def check_config(cfg):
     """raise ConfigError on missing or malformed settings."""
-    for sec in ("scale", "monster", "animation", "route_preference", "tower", "placement",
-                "economy", "waves", "hearts", "coins", "controls", "art", "debug", "sound",
-                "map"):
+    for sec in ("scale", "monster", "animation", "movement", "route_preference", "tower",
+                "placement", "economy", "waves", "hearts", "coins", "controls", "art", "debug",
+                "sound", "map"):
         if sec not in cfg:
             raise ConfigError(f"section [{sec}] missing")
     for mid, mon in cfg["monster"].items():
@@ -345,6 +345,8 @@
         need(tw, "projectile", str, f"tower.{tid}")
         if "slow" in tw and len(tw["slow"]) != 5:
             raise ConfigError(f"tower.{tid}: slow needs 5 levels")
+    for k in ("speed_variation", "offset", "lean", "wander_step", "fade_in"):
+        need(cfg["movement"], k, float, "movement")
     for k in ("footprint_radius", "max_gap", "min_tower_distance", "edge_distance",
               "snap_radius", "grid_cell"):
         need(cfg["placement"], k, float, "placement")