view game/js/map.js @ 24:3f2811aebb3d 0.9.0

splash screen buttons, help button, info screen
author ark
date 2026-10-20 15:58 +0200
parents 2f555a54c332
children a464bd43b76c
line wrap: on
line source

// placement lookup, snapping, route sampling

window.MM = window.MM || {};

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. maxOffset is the largest
    // sideways offset that shifted() will be asked for.
    function Route(data, maxOffset) {
        var p = data.points;
        this.xs = [];
        this.ys = [];
        this.at = [];
        var d = 0;
        for (var i = 0; i < p.length; i += 2) {
            if (i > 0) d += Math.hypot(p[i] - p[i - 2], p[i + 1] - p[i - 1]);
            this.xs.push(p[i]);
            this.ys.push(p[i + 1]);
            this.at.push(d);
        }
        this.length = d;
        this.value = data.value;
        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
    Route.prototype.point = function (s, out) {
        var at = this.at, n = at.length;
        if (s <= 0) { out.x = this.xs[0]; out.y = this.ys[0]; return out; }
        if (s >= this.length) { out.x = this.xs[n - 1]; out.y = this.ys[n - 1]; return out; }
        var i = out.seg || 1;
        if (i >= n || at[i - 1] > s) i = 1;
        while (at[i] < s) i++;
        out.seg = i;
        var t = (s - at[i - 1]) / (at[i] - at[i - 1]);
        out.x = this.xs[i - 1] + (this.xs[i] - this.xs[i - 1]) * t;
        out.y = this.ys[i - 1] + (this.ys[i] - this.ys[i - 1]) * t;
        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;
})();

MM.GameMap = (function () {
    "use strict";

    var cfg = MM.config;
    // spike sides: distance between route samples, and the share of the
    // samples in reach a side needs to count as facing the path
    var SIDE_STEP = 4;
    var SIDE_SHARE = 0.2;

    function GameMap(index) {
        var d = MM.maps[index];
        this.index = index;
        this.data = d;
        this.name = d.name;
        this.width = d.width;
        this.height = d.height;
        // 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) {
        var d = this.data;
        if (cx < 0 || cy < 0 || cx >= d.cols || cy >= d.rows) return false;
        return d.grid.charCodeAt(cy * d.cols + cx) === 49;
    };

    // nearest legal tower position to x, y within the snap radius that keeps
    // the minimum distance to the towers in the list. returns {x, y} or null.
    GameMap.prototype.snap = function (x, y, towers) {
        var p = cfg.placement, cell = this.data.cell;
        var reach = Math.ceil(p.snap_radius / cell);
        var cx0 = Math.floor(x / cell), cy0 = Math.floor(y / cell);
        var best = null, bestD = p.snap_radius;
        for (var cy = cy0 - reach; cy <= cy0 + reach; cy++) {
            for (var cx = cx0 - reach; cx <= cx0 + reach; cx++) {
                if (!this.legalCell(cx, cy)) continue;
                var px = cx * cell + cell / 2, py = cy * cell + cell / 2;
                var d = Math.hypot(px - x, py - y);
                if (d > bestD) continue;
                var free = true;
                for (var i = 0; i < towers.length; i++) {
                    if (Math.hypot(towers[i].x - px, towers[i].y - py) < p.min_tower_distance) {
                        free = false;
                        break;
                    }
                }
                if (free) {
                    best = { x: px, y: py };
                    bestD = d;
                }
            }
        }
        return best;
    };

    // which sides of a tower at x, y face the path, as four booleans: up,
    // right, down, left. each side covers a quarter of the directions. a side
    // faces the path when it holds at least SIDE_SHARE of the route points
    // within reach. with no route point in reach, only the side towards the
    // nearest one does.
    GameMap.prototype.facingSides = function (x, y, reach) {
        var count = [0, 0, 0, 0], total = 0, nearest = Infinity, nearSide = 0;
        var pt = {};
        this.routes.forEach(function (r) {
            pt.seg = 1;
            for (var s = 0; s <= r.length; s += SIDE_STEP) {
                r.point(s, pt);
                var dx = pt.x - x, dy = pt.y - y, d = Math.hypot(dx, dy);
                var side = Math.abs(dx) > Math.abs(dy) ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0);
                if (d < nearest) {
                    nearest = d;
                    nearSide = side;
                }
                if (d <= reach) {
                    count[side]++;
                    total++;
                }
            }
        });
        return count.map(function (c, i) {
            return total ? c >= SIDE_SHARE * total : i === nearSide;
        });
    };

    // route weights for a monster of the given speed (balance.md, route
    // preference)
    GameMap.prototype.routeWeights = function (speed) {
        var rp = cfg.route_preference;
        var f = MM.util.clamp((speed - rp.reference_speed) / rp.reference_speed, -1, 1);
        return this.routes.map(function (r) {
            return Math.max(rp.min_weight, 1 + rp.bias * f * (2 * r.value - 1));
        });
    };

    // index of the shortest route
    GameMap.prototype.shortestRoute = function () {
        var best = -1;
        this.routes.forEach(function (r, i) {
            if (best < 0 || r.length < this.routes[best].length) best = i;
        }, this);
        return best;
    };

    return GameMap;
})();