view game/js/map.js @ 8:2c4fed8baed9

art update, persistent sound setting, tower selling as double action
author ark
date 2026-10-19 13:37 +0200
parents 910b26feca76
children 22b604680c5a
line wrap: on
line source

// placement lookup, snapping, route sampling

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

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

    // a route from the build: a polyline with cumulative distances, so a
    // point can be found by the distance walked
    function Route(data) {
        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.weight = data.weight;
    }

    // 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;
    };

    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;
        this.routes = d.routes.map(function (r) { return new MM.Route(r); });
    }

    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). fixed weights from routes.toml take precedence.
    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) {
            if (r.weight !== null) return r.weight;
            return Math.max(rp.min_weight, 1 + rp.bias * f * (2 * r.value - 1));
        });
    };

    return GameMap;
})();