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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
if (window.IS_DEV) {
const source = new EventSource('/esbuild');
source.addEventListener('change', () => location.reload());
}
const xmlns = 'http://www.w3.org/2000/svg';
const svg = document.querySelector('svg');
const hex = {
inradius: 8.66,
circumradius: 10,
}
const horzSpacing = hex.inradius;
const vertSpacing = hex.circumradius * 3 / 2;
function toKey(q, r, s) {
return `${[q, r, s]}`;
}
function sameSigns(a, b) {
return a > -1 && b > -1 || a < 0 && b < 0;
}
function getNeighbors(coords) {
const [q, r, s] = coords.split(',').map(n => +n);
return [
toKey(q + 1, r, s - 1),
toKey(q - 1, r, s + 1),
toKey(q + 1, r - 1, s),
toKey(q - 1, r + 1, s),
toKey(q, r + 1, s - 1),
toKey(q, r - 1, s + 1),
]
}
// function generateRadialCoords(l, { q, r, s }, radius) {
// const origin = toKey(q, r, s);
// const list = new Map(l);
// const neighbors = new Set();
// let next = new Set();
// list.set(origin);
// next.add(origin);
// for (let i = 0; i < radius; i++) {
// next.forEach(coords => {
// getNeighbors(coords).forEach(n => {
// list.set(n, null);
// neighbors.add(n);
// });
// });
// next = new Set(neighbors);
// neighbors.clear();
// }
// return list;
// }
function generateRadialCoordsRect({ rows, columns, odd = false, equal = true } = {}, { q = 0, r = 0, s = 0 } = {}) {
const list = new Map();
for (let row = 0; row < rows; row++) {
const alternating = row % 2;
const dr = row;
for (let col = 0; col < columns; col++) {
let dq = -(alternating + row) / 2 + alternating - col;
let ds = -(row + alternating) / 2 + col;
if (odd) {
dq = dq - alternating;
ds = ds + alternating;
}
list.set(toKey(q + dq, r + dr, s + ds), null);
}
if (!equal) {
if ((odd && !(row % 2)) || (!odd && row % 2)) {
let dq = -(alternating + row) / 2 + alternating - columns;
let ds = -(row + alternating) / 2 + columns;
if (odd) {
dq = dq - alternating;
ds = ds + alternating;
}
list.set(toKey(q + dq, r + dr, s + ds), null);
}
}
}
return list;
}
function addHexText(q, r, s, v) {
const qText = document.createElementNS(xmlns, 'text');
qText.textContent = q;
qText.setAttributeNS(null, 'x', -3);
qText.setAttributeNS(null, 'y', -3);
const rText = document.createElementNS(xmlns, 'text');
rText.textContent = r;
rText.setAttributeNS(null, 'x', 5);
rText.setAttributeNS(null, 'y', 1.5);
const sText = document.createElementNS(xmlns, 'text');
sText.textContent = s;
sText.setAttributeNS(null, 'x', -3);
sText.setAttributeNS(null, 'y', 5);
const vText = document.createElementNS(xmlns, 'text');
vText.textContent = v;
vText.style.fill = 'red';
vText.setAttributeNS(null, 'y', 1);
vText.setAttributeNS(null, 'x', -2);
return [qText, rText, sText, vText];
}
function drawHexes(el, list, renderText = false) {
for ([key, v] of list) {
const [q, r, s] = key.split(',').map(n => +n);
let x;
if (q === s)
x = 0;
else if (sameSigns(q, s))
x = Math.abs(q - s);
else
x = Math.abs(q) + Math.abs(s);
x = (q > s ? -1 : 1) * x * horzSpacing;
y = r * vertSpacing;
const cell = document.createElementNS(xmlns, 'g');
cell.setAttributeNS(null, 'transform', `translate(${x}, ${y})`);
const use = document.createElementNS(xmlns, 'use');
use.setAttributeNS(null, 'href', '#hex');
cell.appendChild(use);
if (renderText) addHexText(q, r, s, v).forEach(txt => cell.appendChild(txt));
el.appendChild(cell);
}
}
function translateRadialCoords({ q, r, s }, direction, distance) {
return {
q: q + direction.q * distance,
r: r + direction.r * distance,
s: s + direction.s * distance
};
}
// const building1 = generateRadialCoordsRect({ rows: 5, columns: 3, equal: false }, { q: 7, r: -4, s: -3 });
// const building2 = generateRadialCoordsRect({ rows: 4, columns: 3, odd: true }, { q: 0, r: -4, s: 4 });
// const building3 = generateRadialCoords(new Map(), { q: -1, r: 2, s: -1 }, 1);
// const buildingCoords = [
// generateRadialCoordsRect({ rows: 4, columns: 8, equal: false }, { q: 18, r: -8, s: -10 }),
// generateRadialCoordsRect({ rows: 8, columns: 2, equal: false }, { q: 9, r: -10, s: 1 }),
// generateRadialCoordsRect({ rows: 6, columns: 13, odd: true }, { q: 2, r: -8, s: 6 }),
// generateRadialCoordsRect({ rows: 1, columns: 10 }, { q: -1, r: -9, s: 10 }),
// generateRadialCoordsRect({ rows: 1, columns: 2 }, { q: -1, r: -2, s: 3 }),
// generateRadialCoordsRect({ rows: 1, columns: 6 }, { q: -8, r: -2, s: 10 }),
// // generateRadialCoordsRect({ rows: 8, columns: 4, odd: true }, { q: 4, r: 3, s: -7 }),
// generateRadialCoordsRect({ rows: 8, columns: 4, odd: true }, { q: -5, r: 3, s: 2 }),
// generateRadialCoordsRect({ rows: 6, columns: 3, equal: false }, { q: -12, r: 3, s: 9 }),
// // hex building
// // generateRadialCoordsRect({ rows: 8, columns: 4 }, { q: 10, r: 3, s: -13 }),
// // generateRadialCoordsRect({ rows: 3, columns: 1 }, { q: 9, r: 7, s: -16 }),
// // generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }),
// ]
// building2.forEach(coords => grid.delete(coords));
// building3.forEach(coords => grid.delete(coords));
// drawHexes(svg, generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, 16));
// const rectEven = generateRadialCoordsRect({ rows: 2, columns: 2 }, { q: 9, r: -5, s: -4 });
// const rectOdd = generateRadialCoordsRect({ rows: 2, columns: 2, odd: true }, { q: 8, r: -2, s: -6 });
// const rectEvenUnevenCols = generateRadialCoordsRect({ rows: 2, columns: 1, equal: false}, { q: 6, r: 1, s: -7 });
// const rectOddUnevenCols = generateRadialCoordsRect({ rows: 2, columns: 1, odd: true, equal: false}, { q: 5, r: 4, s: -9 });
// drawHexes(svg, rectEven);
// drawHexes(svg, rectOdd);
// drawHexes(svg, rectEvenUnevenCols);
// drawHexes(svg, rectOddUnevenCols);
// drawHexes(svg, generateRadialCoordsRect({ rows: 7, columns: 6, odd: true, equal: false }, { q: 5, r: -5, s: 0 }));
// const buildings = document.createElementNS(xmlns, 'g');
// buildings.classList.add('buildings');
// const bldElevationBasement = document.createElementNS(xmlns, 'g');
// bldElevationBasement.classList.add('elevation-basement');
// buildingCoords.forEach(building => drawHexes(bldElevationBasement, building));
// const bldElevation1 = document.createElementNS(xmlns, 'g');
// bldElevation1.classList.add('elevation-1');
// buildingCoords.forEach(building => drawHexes(bldElevation1, building));
// const bldElevation2 = document.createElementNS(xmlns, 'g');
// bldElevation2.classList.add('elevation-2');
// buildingCoords.forEach(building => drawHexes(bldElevation2, building));
const elevation1 = document.createElementNS(xmlns, 'g');
elevation1.classList.add('elevation-1');
// buildings.appendChild(bldElevationBasement);
// buildings.appendChild(bldElevation1);
// buildings.appendChild(bldElevation2);
const buildingBlah = svg.querySelector('#building2');
const bld2grid = document.createElementNS(xmlns, 'g');
// const bld2hexes = new Map([
// ...generateRadialCoordsRect({ rows: 9, columns: 4, odd: true, equal: false }, { q: 0, r: 0, s: 0 }),
// ...generateRadialCoordsRect({ rows: 5, columns: 1 }, { q: 10, r: 6, s: -16 }),
// ...generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }),
// ]);
let bld2hexes = generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, { left: 3, top: 5, right: 3, bottom: 5 }, 'both');
bld2hexes = generateRadialCoords(
bld2hexes,
translateRadialCoords({ q: 0, r: 0, s: 0 }, { q: 1, r: 0, s: -1 }, 3),
{ left: 1, top: 2, right: 1, bottom: 4 },
'right'
);
bld2hexes = generateRadialCoords(
bld2hexes,
translateRadialCoords({ q: 0, r: 0, s: 0 }, { q: 1, r: 0, s: -1 }, -3),
{ left: 1, top: 2, right: 1, bottom: 4 },
'left'
);
console.log(bld2hexes);
drawHexes(bld2grid, bld2hexes);
const bld2elvBasement = bld2grid.cloneNode(true);
bld2elvBasement.classList.add('elevation-basement');
buildingBlah.appendChild(bld2elvBasement);
const bld2elv1 = bld2grid.cloneNode(true);
bld2elv1.classList.add('elevation-1');
buildingBlah.appendChild(bld2elv1);
const bld2elv2 = bld2grid.cloneNode(true);
bld2elv2.classList.add('elevation-2');
buildingBlah.appendChild(bld2elv2);
const bld2elvRoof = bld2grid.cloneNode(true);
bld2elvRoof.classList.add('elevation-roof');
buildingBlah.appendChild(bld2elvRoof);
const bld1 = svg.querySelector('#building1');
const bld1grid = document.createElementNS(xmlns, 'g');
const bld1hexes = generateRadialCoordsRect({ rows: 9, columns: 4, odd: true, equal: false }, { q: 4, r: 3, s: -7 });
// drawHexes(bld1grid, bld1hexes);
const bld1elv1 = bld1grid.cloneNode(true);
bld1elv1.classList.add('elevation-1');
bld1.appendChild(bld1elv1);
function generateRadialCoords(l, { q, r, s }, { left, top, right, bottom }, offset) {
const origin = toKey(q, r, s);
const list = new Map(l);
list.set(origin, 0);
let queue = [origin];
while (queue.length > 0) {
const v = queue.shift();
getNeighbors(v).forEach(w => {
const [wq, wr, ws] = w.split(',').map(n => +n);
const rDist = Math.abs(wr - r);
const alternating = rDist % 2;
const dr = (rDist + alternating) / 2;
let dLeft = left;
let dRight = right;
if (['left', 'both'].includes(offset)) dRight -= alternating;
if (['right', 'both'].includes(offset)) dLeft -= alternating;
if ([
!list.has(w),
wr < bottom + r && wr > -top + r,
wq > -dRight + q - dr && wq < dLeft + q + dr,
ws > -dLeft + s - dr && ws < dRight + s + dr,
].every(v => v)) {
list.set(w, dr);
queue.push(w);
}
});
}
return list;
}
const elevation2 = document.createElementNS(xmlns, 'g');
elevation2.classList.add('elevation-2');
const grid = generateRadialCoordsRect({ rows: 25, columns: 33 }, { q: 22, r: -12, s: -10 });
// const grid = generateRadialCoordsRect({ rows: 52, columns: 33 }, translateRadialCoords({ q: 22, r: -12, s: -10 }, { q: 1, r: -2, s: 1 }, 7));
const mapsheet2BuildingData = [
[{ rows: 4, columns: 8, equal: false }, { q: 18, r: -8, s: -10 }],
[{ rows: 8, columns: 2, equal: false }, { q: 9, r: -10, s: 1 }],
[{ rows: 6, columns: 13, odd: true }, { q: 2, r: -8, s: 6 }],
[{ rows: 1, columns: 10 }, { q: -1, r: -9, s: 10 }],
[{ rows: 1, columns: 2 }, { q: -1, r: -2, s: 3 }],
[{ rows: 1, columns: 6 }, { q: -8, r: -2, s: 10 }],
[{ rows: 8, columns: 4, odd: true }, { q: -5, r: 3, s: 2 }],
[{ rows: 6, columns: 3, equal: false }, { q: -12, r: 3, s: 9 }],
[{ rows: 9, columns: 4, odd: true, equal: false }, { q: 11, r: 3, s: -14 }],
[{ rows: 5, columns: 1 }, { q: 10, r: 6, s: -16 }],
[{ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }],
[{ rows: 9, columns: 4, odd: true, equal: false }, { q: 4, r: 3, s: -7 }]
]//.map(([size, start]) => [size, translateRadialCoords(start, { q: 1, r: -2, s: 1 }, 7)])
;
// const bld2hexes = [
// generateRadialCoordsRect({ rows: 9, columns: 4, odd: true, equal: false }, { q: 11, r: 3, s: -14 }),
// generateRadialCoordsRect({ rows: 5, columns: 1 }, { q: 10, r: 6, s: -16 }),
// generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }),
// ]
// const bld1hexes = generateRadialCoordsRect({ rows: 9, columns: 4, odd: true, equal: false }, { q: 4, r: 3, s: -7 });
const mapsheet3BuildingData = [
[{ rows: 6, columns: 3, equal: false, odd: true }, { q: 20, r: -10, s: -10 }],
[{ rows: 4, columns: 8, equal: false }, { q: 11, r: 6, s: -17 }],
[{ rows: 8, columns: 2, equal: false, odd: true }, { q: 3, r: 3, s: -6 }],
[{ rows: 9, columns: 4, odd: true, equal: false }, { q: 13, r: -10, s: -3 }],
[{ rows: 5, columns: 1, odd: true }, { q: 8, r: -9, s: 1 }],
[{ rows: 5, columns: 1 }, { q: 13, r: -9, s: -4 }],
[{ rows: 9, columns: 4, odd: true, equal: false }, { q: 2, r: -9, s: 7 }],
[{ rows: 8, columns: 4, odd: true }, { q: -6, r: -9, s: 15 }],
[{ rows: 6, columns: 13, odd: true }, { q: -4, r: 5, s: -1 }],
[{ rows: 1, columns: 10 }, { q: -7, r: 4, s: 3 }],
[{ rows: 1, columns: 2 }, { q: -7, r: 11, s: -4 }],
[{ rows: 1, columns: 6 }, { q: -14, r: 11, s: 3 }],
];
const mapsheet1BuildingData = [
[{ rows: 6, columns: 3, equal: false }, { q: -4, r: -8, s: 12 }],
[{ rows: 8, columns: 2, equal: false, odd: true }, { q: -10, r: 4, s: 6 }],
[{ rows: 9, columns: 4, odd: true, equal: false }, { q: 11, r: 3, s: -14 }],
[{ rows: 5, columns: 1 }, { q: 10, r: 6, s: -16 }],
[{ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }],
[{ rows: 8, columns: 4 }, { q: 17, r: -10, s: -7 }],
// tree/brush copse
[{ rows: 9, columns: 6, equal: false }, { q: 5, r: -4, s: -1 }],
];
const mapsheet1BuildingCoords = mapsheet1BuildingData.map(([size, start]) => generateRadialCoordsRect(size, start));
const mapsheet2BuildingCoords = mapsheet2BuildingData.map(([size, start]) => generateRadialCoordsRect(size, start));
const mapsheet3BuildingCoords = mapsheet3BuildingData.map(([size, start]) => generateRadialCoordsRect(size, start));
const list = generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, { left: 17, top: 13, right: 17, bottom: 14 });
const mapsheet1 = new Map(list);
const mapsheet2 = new Map(list);
const mapsheet3 = new Map(list);
mapsheet1BuildingCoords.forEach(building => {
for ([coords, v] of building) mapsheet1.delete(coords);
});
mapsheet2BuildingCoords.forEach(building => {
for ([coords, v] of building) mapsheet2.delete(coords);
});
mapsheet3BuildingCoords.forEach(building => {
for ([coords, v] of building) mapsheet3.delete(coords);
});
function coordsScenario2() {
const grid = generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, { left: 17, top: 39, right: 17, bottom: 40 });
mapsheet2BuildingData
.map(([size, start]) => [size, translateRadialCoords(start, { q: 1, r: -2, s: 1 }, 13)])
.map(([size, start]) => generateRadialCoordsRect(size, start)).forEach(building => {
for ([coords, v] of building) grid.delete(coords);
}
);
mapsheet3BuildingData
.map(([size, start]) => [size, translateRadialCoords(start, { q: 1, r: -2, s: 1 }, -13)])
.map(([size, start]) => generateRadialCoordsRect(size, start)).forEach(building => {
for ([coords, v] of building) grid.delete(coords);
}
);
mapsheet1BuildingData
.map(([size, start]) => generateRadialCoordsRect(size, start)).forEach(building => {
for ([coords, v] of building) grid.delete(coords);
}
);
return grid;
}
function coordsScenario1() {
const grid = generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, { left: 17, top: 27, right: 17, bottom: 26 });
mapsheet2BuildingData
.map(([size, start]) => [size, translateRadialCoords(start, { q: 1, r: -2, s: 1 }, 7)])
.map(([size, start]) => generateRadialCoordsRect(size, start)).forEach(building => {
for ([coords, v] of building) grid.delete(coords);
}
);
mapsheet3BuildingData
.map(([size, start]) => [size, translateRadialCoords(start, { q: 1, r: -2, s: 1 }, -6)])
.map(([size, start]) => generateRadialCoordsRect(size, start)).forEach(building => {
for ([coords, v] of building) grid.delete(coords);
}
);
return grid;
}
drawHexes(elevation1, coordsScenario1(), true);
// drawHexes(document.querySelector('#mapsheet1'), mapsheet1, true);
// drawHexes(document.querySelector('#mapsheet2'), mapsheet2, true);
// drawHexes(document.querySelector('#mapsheet3'), mapsheet3, true);
const defs = svg.querySelector('defs');
// defs.after(elevation2);
defs.after(elevation1);
|