-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.overlay.js
339 lines (298 loc) · 8.88 KB
/
debug.overlay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//#region Panels
const defaultText = {
color: "#FFFFFF",
align: "left",
font: "0.7 monospace",
};
const panelStyle = {
fill: "#000000",
opacity: 0.35,
stroke: "#000000",
strokeWidth: 0.35,
};
const columnSize = 20;
const defaultSpacing = 0.9;
const headerSpacing = 1.3;
const panels = {};
const drawnPanels = {};
class Panel {
constructor(style, roomName, anchor, parent) {
this.style = style;
this.roomName = roomName;
this.anchor = anchor;
this.parent = parent;
this.elements = [];
this.mx = 0.5;
this.my = 0.5;
}
add(...elements) {
this.elements.push(...elements);
}
draw(key) {
if (!this.elements.length) {
return;
}
const height =
this.elements.reduce((total, curr) => total + curr.spacing, 0) +
this.my * 2 -
// Last element doesn't need a spacing below it
this.elements.slice(-1)[0].spacing;
const elementSizes = this.elements.map((element) => {
if (!element.style || !element.style.font) {
return 0;
}
const scale = parseFloat(
element.style.font.match(/^\D*(\d+(?:\.\d+)?)/)
);
// For some unknown reason, this game has this magic scaling constant
// of about 1.9 from text to game units
// No idea why
return element.content.length * (scale / 1.9);
});
const largestElement = elementSizes.reduce((largest, curr) =>
curr > largest ? curr : largest
);
const width = largestElement + this.style.strokeWidth + this.mx * 2;
// If we have a parent panel, let's draw this one relative to it instead
let parentOffset = 0;
if (this.parent && drawnPanels[this.parent]) {
const parent = drawnPanels[this.parent];
parentOffset = this.anchor.includes("r")
? parent.x
: parent.x + parent.width;
}
const x = this.anchor.includes("r")
? (parentOffset || 49.5) - width - this.style.strokeWidth / 2
: (parentOffset || -0.5) + this.style.strokeWidth / 2;
const y = this.anchor.includes("b")
? 49.5 - height - this.style.strokeWidth / 2
: -0.5 + this.style.strokeWidth / 2;
const visual = new RoomVisual(this.roomName).rect(
x,
y,
width,
height,
this.style
);
// Add text to the panel for each element
let offset = this.my + this.style.strokeWidth / 2;
for (const element of this.elements) {
if (element !== this.elements[0]) {
offset += element.spacing;
}
visual.text(element.content, x + this.mx, offset, element.style);
}
// Let's mark this panel as drawn so any future children can draw relative to it
drawnPanels[key] = {
x,
y,
width,
height,
};
}
}
/**
* Creates a panel which can be drawn to.
* @param {string} name The name of the panel to reference when drawing later.
* @param {string} roomName The name of the room to draw this panel in when drawing panels.
* @param {"tr" | "tl" | "br" | "bl"} anchor The side of the screen to anchor the panel.
* @param {string?} parent The name of the parent panel. When given, this panel will
* be drawn relative to its parent.
* @returns {{ addChild: (childName: string) => ...}}
* An object with a method to add children to the created panel.
*/
const createPanel = (name, roomName, anchor, parent) => {
panels[name] = new Panel(panelStyle, roomName, anchor, parent);
return {
addChild: (childName) => createPanel(childName, roomName, anchor, name),
};
};
/**
* Adds a heading to the panel with the specified name.
* @param {string} panelName The panel to add the heading.
* @param {string} heading The heading text.
*/
const addHeading = (panelName, heading) => {
if (!panels[panelName]) {
return;
}
panels[panelName].add({
content: `- ${heading} -`,
style: defaultText,
spacing: headerSpacing,
});
};
/**
* Adds text to the panel with the specified name.
* @param {string} panelName The panel to add the text.
* @param {{}[]} figures An object array where the in each object,
* the key represents the prefix for the text, and the value represents to value.
*/
const addText = (panelName, figures) => {
if (!panels[panelName]) {
return;
}
panels[panelName].add(
...Object.keys(figures).map((fig) => {
return {
content: fig + ": " + figures[fig],
style: defaultText,
spacing: defaultSpacing,
};
})
);
};
/**
* Adds a preformatted 2 column row to the specified panel.
* @param {string} panelName The panel to add the text.
* @param {string} leftElement The left text.
* @param {string} rightElement The right text.
*/
const addColumns = (panelName, leftElement, rightElement) => {
if (!panels[panelName]) {
return;
}
panels[panelName].add({
content:
leftElement +
" ".repeat(
Math.max(
columnSize -
leftElement.toString().length -
rightElement.toString().length,
0
)
) +
rightElement,
style: defaultText,
spacing: defaultSpacing,
});
};
/**
* Draws all panels.
*/
const finalizePanels = () => {
if (!DEBUG.drawOverlay) {
return;
}
for (const key in panels) {
panels[key].draw(key);
}
};
//#endregion
//#region Shapes
const defaultStyle = {
fill: "#FFFFFF",
};
const rects = (positions, width = 0.5, height = 0.5, style = defaultStyle) => {
if (!DEBUG.drawOverlay) {
return;
}
const visuals = {};
positions.forEach((pos) => {
if (!visuals[pos.roomName]) {
visuals[pos.roomName] = new RoomVisual(pos.roomName);
}
visuals[pos.roomName].rect(
pos.x - width / 2,
pos.y - height / 2,
width,
height,
style
);
});
};
const circles = (positions, style = defaultStyle) => {
if (!DEBUG.drawOverlay) {
return;
}
const visuals = {};
positions.forEach((pos) => {
if (!visuals[pos.roomName]) {
visuals[pos.roomName] = new RoomVisual(pos.roomName);
}
visuals[pos.roomName].circle(pos.x, pos.y, style);
});
};
//#endregion
//#region Matrices
const matrixDisplayColor = "#fcba03";
const visualizeCostMatrix = (
roomName,
matrix,
excludedValues = [0, 255],
color = matrixDisplayColor
) => {
if (!DEBUG.drawOverlay) {
return;
}
let highestValue = 0;
for (let x = 0; x < ROOM_SIZE; x++) {
for (let y = 0; y < ROOM_SIZE; y++) {
highestValue = Math.max(matrix.get(x, y), highestValue);
}
}
const visual = new RoomVisual(roomName);
for (let x = 0; x < ROOM_SIZE; x++) {
for (let y = 0; y < ROOM_SIZE; y++) {
const value = matrix.get(x, y);
if (excludedValues.includes(value)) {
continue;
}
visual.rect(x - 0.5, y - 0.5, 1, 1, {
fill: color,
opacity: value / highestValue,
});
visual.text(value, x, y, {
font: "0.5 monospace",
opacity: 0.8,
});
}
}
};
//#endregion
//#region Structures
const visualizeBasePlan = (roomName, planMatrix, rampartMatrix, mapping) => {
if (!DEBUG.drawOverlay) {
return;
}
const visual = new RoomVisual(roomName);
for (let x = 0; x < ROOM_SIZE; x++) {
for (let y = 0; y < ROOM_SIZE; y++) {
const value = planMatrix.get(x, y);
if (rampartMatrix.get(x, y)) {
visual.structure(x, y, STRUCTURE_RAMPART);
}
if (mapping[value]) {
visual.structure(x, y, mapping[value]);
}
}
}
visual.connectRoads();
};
//#endregion
//#region Arrows
const { directionDelta, ROOM_SIZE } = require("./constants");
const drawTrafficArrow = (pos, direction, style) => {
if (!DEBUG.drawOverlay) {
return;
}
const targetX = pos.x + directionDelta[direction].x;
const targetY = pos.y + directionDelta[direction].y;
const x = targetX - (targetX - pos.x) * 0.5;
const y = targetY - (targetY - pos.y) * 0.5;
Game.rooms[pos.roomName].visual.line(pos.x, pos.y, x, y, style);
};
//#endregion
module.exports = {
createPanel,
addText,
addHeading,
addColumns,
finalizePanels,
rects,
circles,
visualizeCostMatrix,
visualizeBasePlan,
drawTrafficArrow,
};