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