|
1
|
1 // placement lookup, snapping, route sampling
|
|
|
2
|
|
|
3 window.MM = window.MM || {};
|
|
|
4
|
|
|
5 MM.Route = (function () {
|
|
|
6 "use strict";
|
|
|
7
|
|
10
|
8 // sideways offsets on the inside of a bend: share of the distance at
|
|
|
9 // which the offset point would stop moving forward, and how fast the
|
|
|
10 // limit may change per pixel along the route
|
|
|
11 var BEND_SHARE = 0.7;
|
|
|
12 var LIMIT_SLOPE = 0.5;
|
|
|
13
|
|
1
|
14 // a route from the build: a polyline with cumulative distances, so a
|
|
10
|
15 // point can be found by the distance walked. maxOffset is the largest
|
|
|
16 // sideways offset that shifted() will be asked for.
|
|
|
17 function Route(data, maxOffset) {
|
|
1
|
18 var p = data.points;
|
|
|
19 this.xs = [];
|
|
|
20 this.ys = [];
|
|
|
21 this.at = [];
|
|
|
22 var d = 0;
|
|
|
23 for (var i = 0; i < p.length; i += 2) {
|
|
|
24 if (i > 0) d += Math.hypot(p[i] - p[i - 2], p[i + 1] - p[i - 1]);
|
|
|
25 this.xs.push(p[i]);
|
|
|
26 this.ys.push(p[i + 1]);
|
|
|
27 this.at.push(d);
|
|
|
28 }
|
|
|
29 this.length = d;
|
|
|
30 this.value = data.value;
|
|
|
31 this.weight = data.weight;
|
|
10
|
32 this.sideways(maxOffset);
|
|
1
|
33 }
|
|
|
34
|
|
10
|
35 // set up the sideways direction at each point (nx, ny) and how far an
|
|
|
36 // offset may go to the left and right there (left, right)
|
|
|
37 Route.prototype.sideways = function (maxOffset) {
|
|
|
38 var xs = this.xs, ys = this.ys, at = this.at, n = xs.length, i;
|
|
|
39 // direction of travel of segment i, from point i - 1 to point i
|
|
|
40 var tx = [0], ty = [0];
|
|
|
41 for (i = 1; i < n; i++) {
|
|
|
42 var len = at[i] - at[i - 1];
|
|
|
43 tx.push((xs[i] - xs[i - 1]) / len);
|
|
|
44 ty.push((ys[i] - ys[i - 1]) / len);
|
|
|
45 }
|
|
|
46 // at each point, the left side as drawn, averaged over the two
|
|
|
47 // segments that meet there
|
|
|
48 this.nx = [];
|
|
|
49 this.ny = [];
|
|
|
50 for (i = 0; i < n; i++) {
|
|
|
51 var a = Math.max(i, 1), b = Math.min(i + 1, n - 1);
|
|
|
52 var sx = ty[a] + ty[b], sy = -tx[a] - tx[b], sl = Math.hypot(sx, sy);
|
|
|
53 if (sl < 1e-6) { sx = ty[a]; sy = -tx[a]; sl = 1; }
|
|
|
54 this.nx.push(sx / sl);
|
|
|
55 this.ny.push(sy / sl);
|
|
|
56 }
|
|
|
57 // along a segment the sideways direction turns. an offset on the
|
|
|
58 // inside of the turn moves forward at 1 + offset * k of the walking
|
|
|
59 // speed, k being negative on that side. limit the offset at both
|
|
|
60 // ends of the segment so this stays above 1 - BEND_SHARE
|
|
|
61 var left = [], right = [];
|
|
|
62 for (i = 0; i < n; i++) {
|
|
|
63 left.push(maxOffset);
|
|
|
64 right.push(maxOffset);
|
|
|
65 }
|
|
|
66 for (i = 1; i < n; i++) {
|
|
|
67 var k = ((this.nx[i] - this.nx[i - 1]) * tx[i]
|
|
|
68 + (this.ny[i] - this.ny[i - 1]) * ty[i]) / (at[i] - at[i - 1]);
|
|
|
69 var lim = BEND_SHARE / Math.abs(k);
|
|
|
70 var side = k < 0 ? left : right;
|
|
|
71 side[i - 1] = Math.min(side[i - 1], lim);
|
|
|
72 side[i] = Math.min(side[i], lim);
|
|
|
73 }
|
|
|
74 // let the limits change gradually, so monsters drift towards the
|
|
|
75 // centre line before a tight bend rather than at it
|
|
|
76 [left, right].forEach(function (lim) {
|
|
|
77 for (i = 1; i < n; i++)
|
|
|
78 lim[i] = Math.min(lim[i], lim[i - 1] + LIMIT_SLOPE * (at[i] - at[i - 1]));
|
|
|
79 for (i = n - 2; i >= 0; i--)
|
|
|
80 lim[i] = Math.min(lim[i], lim[i + 1] + LIMIT_SLOPE * (at[i + 1] - at[i]));
|
|
|
81 });
|
|
|
82 this.left = left;
|
|
|
83 this.right = right;
|
|
|
84 };
|
|
|
85
|
|
1
|
86 // write the point at distance s along the route into out.x, out.y.
|
|
|
87 // out.seg remembers the segment, so the next search for a point
|
|
|
88 // further along starts there
|
|
|
89 Route.prototype.point = function (s, out) {
|
|
|
90 var at = this.at, n = at.length;
|
|
|
91 if (s <= 0) { out.x = this.xs[0]; out.y = this.ys[0]; return out; }
|
|
|
92 if (s >= this.length) { out.x = this.xs[n - 1]; out.y = this.ys[n - 1]; return out; }
|
|
|
93 var i = out.seg || 1;
|
|
|
94 if (i >= n || at[i - 1] > s) i = 1;
|
|
|
95 while (at[i] < s) i++;
|
|
|
96 out.seg = i;
|
|
|
97 var t = (s - at[i - 1]) / (at[i] - at[i - 1]);
|
|
|
98 out.x = this.xs[i - 1] + (this.xs[i] - this.xs[i - 1]) * t;
|
|
|
99 out.y = this.ys[i - 1] + (this.ys[i] - this.ys[i - 1]) * t;
|
|
|
100 return out;
|
|
|
101 };
|
|
|
102
|
|
10
|
103 // as point(), moved sideways by d: positive to the left of the direction
|
|
|
104 // of travel as drawn, negative to the right. on the inside of bends d is
|
|
|
105 // reduced, so the point keeps moving forward as s grows
|
|
|
106 Route.prototype.shifted = function (s, d, out) {
|
|
|
107 this.point(s, out);
|
|
|
108 var at = this.at, n = at.length, i, t;
|
|
|
109 if (s <= 0) { i = 1; t = 0; }
|
|
|
110 else if (s >= this.length) { i = n - 1; t = 1; }
|
|
|
111 else { i = out.seg; t = (s - at[i - 1]) / (at[i] - at[i - 1]); }
|
|
|
112 var lim = d > 0 ? this.left : this.right;
|
|
|
113 var m = lim[i - 1] + (lim[i] - lim[i - 1]) * t;
|
|
|
114 if (Math.abs(d) > m) d = d > 0 ? m : -m;
|
|
|
115 out.x += d * (this.nx[i - 1] + (this.nx[i] - this.nx[i - 1]) * t);
|
|
|
116 out.y += d * (this.ny[i - 1] + (this.ny[i] - this.ny[i - 1]) * t);
|
|
|
117 return out;
|
|
|
118 };
|
|
|
119
|
|
1
|
120 return Route;
|
|
|
121 })();
|
|
|
122
|
|
|
123 MM.GameMap = (function () {
|
|
|
124 "use strict";
|
|
|
125
|
|
|
126 var cfg = MM.config;
|
|
6
|
127 // spike sides: distance between route samples, and the share of the
|
|
|
128 // samples in reach a side needs to count as facing the path
|
|
|
129 var SIDE_STEP = 4;
|
|
|
130 var SIDE_SHARE = 0.2;
|
|
1
|
131
|
|
|
132 function GameMap(index) {
|
|
|
133 var d = MM.maps[index];
|
|
|
134 this.index = index;
|
|
|
135 this.data = d;
|
|
|
136 this.name = d.name;
|
|
|
137 this.width = d.width;
|
|
|
138 this.height = d.height;
|
|
10
|
139 // largest sideways offset of a monster from the centre line
|
|
|
140 this.maxOffset = cfg.movement.offset * cfg.maps[index].path_half_width;
|
|
|
141 var maxOffset = this.maxOffset;
|
|
|
142 this.routes = d.routes.map(function (r) { return new MM.Route(r, maxOffset); });
|
|
1
|
143 }
|
|
|
144
|
|
|
145 GameMap.prototype.legalCell = function (cx, cy) {
|
|
|
146 var d = this.data;
|
|
|
147 if (cx < 0 || cy < 0 || cx >= d.cols || cy >= d.rows) return false;
|
|
|
148 return d.grid.charCodeAt(cy * d.cols + cx) === 49;
|
|
|
149 };
|
|
|
150
|
|
|
151 // nearest legal tower position to x, y within the snap radius that keeps
|
|
|
152 // the minimum distance to the towers in the list. returns {x, y} or null.
|
|
|
153 GameMap.prototype.snap = function (x, y, towers) {
|
|
|
154 var p = cfg.placement, cell = this.data.cell;
|
|
|
155 var reach = Math.ceil(p.snap_radius / cell);
|
|
|
156 var cx0 = Math.floor(x / cell), cy0 = Math.floor(y / cell);
|
|
|
157 var best = null, bestD = p.snap_radius;
|
|
|
158 for (var cy = cy0 - reach; cy <= cy0 + reach; cy++) {
|
|
|
159 for (var cx = cx0 - reach; cx <= cx0 + reach; cx++) {
|
|
|
160 if (!this.legalCell(cx, cy)) continue;
|
|
|
161 var px = cx * cell + cell / 2, py = cy * cell + cell / 2;
|
|
|
162 var d = Math.hypot(px - x, py - y);
|
|
|
163 if (d > bestD) continue;
|
|
|
164 var free = true;
|
|
|
165 for (var i = 0; i < towers.length; i++) {
|
|
|
166 if (Math.hypot(towers[i].x - px, towers[i].y - py) < p.min_tower_distance) {
|
|
|
167 free = false;
|
|
|
168 break;
|
|
|
169 }
|
|
|
170 }
|
|
|
171 if (free) {
|
|
|
172 best = { x: px, y: py };
|
|
|
173 bestD = d;
|
|
|
174 }
|
|
|
175 }
|
|
|
176 }
|
|
|
177 return best;
|
|
|
178 };
|
|
|
179
|
|
6
|
180 // which sides of a tower at x, y face the path, as four booleans: up,
|
|
|
181 // right, down, left. each side covers a quarter of the directions. a side
|
|
|
182 // faces the path when it holds at least SIDE_SHARE of the route points
|
|
|
183 // within reach. with no route point in reach, only the side towards the
|
|
|
184 // nearest one does.
|
|
|
185 GameMap.prototype.facingSides = function (x, y, reach) {
|
|
|
186 var count = [0, 0, 0, 0], total = 0, nearest = Infinity, nearSide = 0;
|
|
|
187 var pt = {};
|
|
|
188 this.routes.forEach(function (r) {
|
|
|
189 pt.seg = 1;
|
|
|
190 for (var s = 0; s <= r.length; s += SIDE_STEP) {
|
|
|
191 r.point(s, pt);
|
|
|
192 var dx = pt.x - x, dy = pt.y - y, d = Math.hypot(dx, dy);
|
|
|
193 var side = Math.abs(dx) > Math.abs(dy) ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0);
|
|
|
194 if (d < nearest) {
|
|
|
195 nearest = d;
|
|
|
196 nearSide = side;
|
|
|
197 }
|
|
|
198 if (d <= reach) {
|
|
|
199 count[side]++;
|
|
|
200 total++;
|
|
|
201 }
|
|
|
202 }
|
|
|
203 });
|
|
|
204 return count.map(function (c, i) {
|
|
|
205 return total ? c >= SIDE_SHARE * total : i === nearSide;
|
|
|
206 });
|
|
|
207 };
|
|
|
208
|
|
1
|
209 // route weights for a monster of the given speed (balance.md, route
|
|
|
210 // preference). fixed weights from routes.toml take precedence.
|
|
|
211 GameMap.prototype.routeWeights = function (speed) {
|
|
|
212 var rp = cfg.route_preference;
|
|
|
213 var f = MM.util.clamp((speed - rp.reference_speed) / rp.reference_speed, -1, 1);
|
|
|
214 return this.routes.map(function (r) {
|
|
|
215 if (r.weight !== null) return r.weight;
|
|
|
216 return Math.max(rp.min_weight, 1 + rp.bias * f * (2 * r.value - 1));
|
|
|
217 });
|
|
|
218 };
|
|
|
219
|
|
|
220 return GameMap;
|
|
|
221 })();
|