|
1
|
1 // placement lookup, snapping, route sampling
|
|
|
2
|
|
|
3 window.MM = window.MM || {};
|
|
|
4
|
|
|
5 MM.Route = (function () {
|
|
|
6 "use strict";
|
|
|
7
|
|
|
8 // a route from the build: a polyline with cumulative distances, so a
|
|
|
9 // point can be found by the distance walked
|
|
|
10 function Route(data) {
|
|
|
11 var p = data.points;
|
|
|
12 this.xs = [];
|
|
|
13 this.ys = [];
|
|
|
14 this.at = [];
|
|
|
15 var d = 0;
|
|
|
16 for (var i = 0; i < p.length; i += 2) {
|
|
|
17 if (i > 0) d += Math.hypot(p[i] - p[i - 2], p[i + 1] - p[i - 1]);
|
|
|
18 this.xs.push(p[i]);
|
|
|
19 this.ys.push(p[i + 1]);
|
|
|
20 this.at.push(d);
|
|
|
21 }
|
|
|
22 this.length = d;
|
|
|
23 this.value = data.value;
|
|
|
24 this.weight = data.weight;
|
|
|
25 }
|
|
|
26
|
|
|
27 // write the point at distance s along the route into out.x, out.y.
|
|
|
28 // out.seg remembers the segment, so the next search for a point
|
|
|
29 // further along starts there
|
|
|
30 Route.prototype.point = function (s, out) {
|
|
|
31 var at = this.at, n = at.length;
|
|
|
32 if (s <= 0) { out.x = this.xs[0]; out.y = this.ys[0]; return out; }
|
|
|
33 if (s >= this.length) { out.x = this.xs[n - 1]; out.y = this.ys[n - 1]; return out; }
|
|
|
34 var i = out.seg || 1;
|
|
|
35 if (i >= n || at[i - 1] > s) i = 1;
|
|
|
36 while (at[i] < s) i++;
|
|
|
37 out.seg = i;
|
|
|
38 var t = (s - at[i - 1]) / (at[i] - at[i - 1]);
|
|
|
39 out.x = this.xs[i - 1] + (this.xs[i] - this.xs[i - 1]) * t;
|
|
|
40 out.y = this.ys[i - 1] + (this.ys[i] - this.ys[i - 1]) * t;
|
|
|
41 return out;
|
|
|
42 };
|
|
|
43
|
|
|
44 return Route;
|
|
|
45 })();
|
|
|
46
|
|
|
47 MM.GameMap = (function () {
|
|
|
48 "use strict";
|
|
|
49
|
|
|
50 var cfg = MM.config;
|
|
6
|
51 // spike sides: distance between route samples, and the share of the
|
|
|
52 // samples in reach a side needs to count as facing the path
|
|
|
53 var SIDE_STEP = 4;
|
|
|
54 var SIDE_SHARE = 0.2;
|
|
1
|
55
|
|
|
56 function GameMap(index) {
|
|
|
57 var d = MM.maps[index];
|
|
|
58 this.index = index;
|
|
|
59 this.data = d;
|
|
|
60 this.name = d.name;
|
|
|
61 this.width = d.width;
|
|
|
62 this.height = d.height;
|
|
|
63 this.routes = d.routes.map(function (r) { return new MM.Route(r); });
|
|
|
64 }
|
|
|
65
|
|
|
66 GameMap.prototype.legalCell = function (cx, cy) {
|
|
|
67 var d = this.data;
|
|
|
68 if (cx < 0 || cy < 0 || cx >= d.cols || cy >= d.rows) return false;
|
|
|
69 return d.grid.charCodeAt(cy * d.cols + cx) === 49;
|
|
|
70 };
|
|
|
71
|
|
|
72 // nearest legal tower position to x, y within the snap radius that keeps
|
|
|
73 // the minimum distance to the towers in the list. returns {x, y} or null.
|
|
|
74 GameMap.prototype.snap = function (x, y, towers) {
|
|
|
75 var p = cfg.placement, cell = this.data.cell;
|
|
|
76 var reach = Math.ceil(p.snap_radius / cell);
|
|
|
77 var cx0 = Math.floor(x / cell), cy0 = Math.floor(y / cell);
|
|
|
78 var best = null, bestD = p.snap_radius;
|
|
|
79 for (var cy = cy0 - reach; cy <= cy0 + reach; cy++) {
|
|
|
80 for (var cx = cx0 - reach; cx <= cx0 + reach; cx++) {
|
|
|
81 if (!this.legalCell(cx, cy)) continue;
|
|
|
82 var px = cx * cell + cell / 2, py = cy * cell + cell / 2;
|
|
|
83 var d = Math.hypot(px - x, py - y);
|
|
|
84 if (d > bestD) continue;
|
|
|
85 var free = true;
|
|
|
86 for (var i = 0; i < towers.length; i++) {
|
|
|
87 if (Math.hypot(towers[i].x - px, towers[i].y - py) < p.min_tower_distance) {
|
|
|
88 free = false;
|
|
|
89 break;
|
|
|
90 }
|
|
|
91 }
|
|
|
92 if (free) {
|
|
|
93 best = { x: px, y: py };
|
|
|
94 bestD = d;
|
|
|
95 }
|
|
|
96 }
|
|
|
97 }
|
|
|
98 return best;
|
|
|
99 };
|
|
|
100
|
|
6
|
101 // which sides of a tower at x, y face the path, as four booleans: up,
|
|
|
102 // right, down, left. each side covers a quarter of the directions. a side
|
|
|
103 // faces the path when it holds at least SIDE_SHARE of the route points
|
|
|
104 // within reach. with no route point in reach, only the side towards the
|
|
|
105 // nearest one does.
|
|
|
106 GameMap.prototype.facingSides = function (x, y, reach) {
|
|
|
107 var count = [0, 0, 0, 0], total = 0, nearest = Infinity, nearSide = 0;
|
|
|
108 var pt = {};
|
|
|
109 this.routes.forEach(function (r) {
|
|
|
110 pt.seg = 1;
|
|
|
111 for (var s = 0; s <= r.length; s += SIDE_STEP) {
|
|
|
112 r.point(s, pt);
|
|
|
113 var dx = pt.x - x, dy = pt.y - y, d = Math.hypot(dx, dy);
|
|
|
114 var side = Math.abs(dx) > Math.abs(dy) ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0);
|
|
|
115 if (d < nearest) {
|
|
|
116 nearest = d;
|
|
|
117 nearSide = side;
|
|
|
118 }
|
|
|
119 if (d <= reach) {
|
|
|
120 count[side]++;
|
|
|
121 total++;
|
|
|
122 }
|
|
|
123 }
|
|
|
124 });
|
|
|
125 return count.map(function (c, i) {
|
|
|
126 return total ? c >= SIDE_SHARE * total : i === nearSide;
|
|
|
127 });
|
|
|
128 };
|
|
|
129
|
|
1
|
130 // route weights for a monster of the given speed (balance.md, route
|
|
|
131 // preference). fixed weights from routes.toml take precedence.
|
|
|
132 GameMap.prototype.routeWeights = function (speed) {
|
|
|
133 var rp = cfg.route_preference;
|
|
|
134 var f = MM.util.clamp((speed - rp.reference_speed) / rp.reference_speed, -1, 1);
|
|
|
135 return this.routes.map(function (r) {
|
|
|
136 if (r.weight !== null) return r.weight;
|
|
|
137 return Math.max(rp.min_weight, 1 + rp.bias * f * (2 * r.value - 1));
|
|
|
138 });
|
|
|
139 };
|
|
|
140
|
|
|
141 return GameMap;
|
|
|
142 })();
|