-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
854 lines (704 loc) · 24.3 KB
/
index.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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
const Anteroom = require('./lib/anteroom')
const Door = require('./lib/door')
const QuadTree = require('./lib/qtree')
const Room = require('./lib/room')
const Roomie = require('./lib/roomie')
const Tunneler = require('./lib/tunneler')
const buildGraph = require('./lib/build-graph')
const constants = require('./lib/constants')
const designDefault = require('./lib/design-default')
const expandAABB = require('./lib/aabb-expand')
const getPortal = require('./lib/aabb-portal')
const intersect1D = require('./lib/1d-intersect')
const intersectAABB = require('./lib/aabb-intersect')
const levelFurnish = require('./lib/level-furnish')
const moveAABB = require('./lib/aabb-move')
const overlaps = require('./lib/aabb-overlaps')
const random = require('./lib/random')
const removeItems = require('remove-array-items')
const seed = require('seedrandom')
const turn90 = require('./lib/turn-90')
//const zeros = require('zeros')
// global constants
const ENTITIES = [ 'tunnels', 'rooms', 'anterooms', 'doors' ]
class LevelGenerator {
constructor(design, seedValue, theme) {
let t
if (design == null) { design = designDefault }
if (seedValue == null) { seedValue = null }
this.theme = theme
this.design = JSON.parse(JSON.stringify(design))
this.objects = []
this.workers = []
this.generation = 0
// stores references to items, which speeds up AABB overlap checks
this.qtree = new QuadTree(0, 0, this.design.dimensions.width, this.design.dimensions.height)
// if present, use the design file's seed for reproducible random numbers
random.seed(seedValue || this.design.randSeed)
// build a tunneler for each one specified in the design
for (var init of Array.from(this.design.tunnelers)) {
t = new Tunneler(this, this.design, init)
this.workers.push(t)
}
// add predefined rooms to the level from the design
for (let roomInit of Array.from(this.design.rooms)) {
const r = new Room(roomInit)
r.prefab = true // flag room as prefab to prevent tunneling into it later
this.addEntity(r)
// put a tunneler at every prefab room door facing the exit
for (let door of Array.from(r.doors)) {
const d = new Door(door)
this.addEntity(d)
init = JSON.parse(JSON.stringify(this.design.roomExitTunneler))
init.x = door.x
init.y = door.y
init.direction = r.findExitDirection(door)
d.direction = init.direction
t = new Tunneler(this, this.design, init)
this.workers.push(t)
}
// rooms don't contain doors, so remove them
delete r.doors
}
this.roomCount = {small: 0, medium: 0, large: 0}
}
addEntity(entity) {
entity.id = this.objects.length
this.objects.push(entity)
return this.qtree.put(entity)
}
build() {
let built = false
while (!built) {
let entities = this.workers.filter(o => o.generation === this.generation)
for (let e of Array.from(entities)) {
if (!e.step()) {
this._removeWorker(e.id)
}
}
// when there are no roomies/tunnelers in the current generation, advance
entities = this.workers.filter(o => o.generation === this.generation)
if (entities.length === 0) {
this.generation++
}
if (this.workers.length === 0) {
built = true
}
setTimeout(
function() {},
0
)
}
return this._postBuildStep()
}
// determine if an AABB is fully inside the level dimensions
contains(aabb) {
const minPadding = 1 // ensure there is a 1 unit border around the level edge
if ((aabb.x < minPadding) || (aabb.y < minPadding)) {
return false
}
if (((aabb.x + aabb.width) > (this.design.dimensions.width - minPadding)) ||
((aabb.y + aabb.height) > (this.design.dimensions.height - minPadding))) {
return false
}
return true
}
// determine the largest room that fits at the given level position
findLargestRoom(doorPosition, direction, maxSize) {
const leftDirection = turn90(direction, 'LEFT')
const rightDirection = turn90(direction, 'RIGHT')
const directionVectors = {
forward: direction,
left: leftDirection,
right: rightDirection
}
const expand = {forward: true, left: true, right: true}
// move to the first position in the room (right in front of the door)
let result = { x: doorPosition.x, y: doorPosition.y, width: 1, height: 1 }
moveAABB(result, directionVectors.forward)
// Grow in all directions from the door. When overlapping or adjacent to an
// object, stop growing in that direction. When growing stops in all
// directions, we've found the largest room that fits in this area.
const minTunnelSpacing = 1
const minAnteroomSpacing = 1
while (expand.left || expand.right || expand.forward) {
for (let dir in expand) {
const exp = expand[dir]
if (exp) {
const cardinalDirection = directionVectors[dir]
const test = expandAABB(result, cardinalDirection)
if ((test.width * test.height) > maxSize) {
return result
}
// ensure room aspect ratio are within acceptable limits
if ((test.width / test.height) < this.design.roomAspectRatio) {
return result
}
if ((test.height / test.width) < this.design.roomAspectRatio) {
return result
}
if (!this.contains(test)) {
// expanded outside of level dimensions, reject this expansion
expand[dir] = false
} else {
if (this.overlaps(test, this.design.minRoomSpacing, minAnteroomSpacing, minTunnelSpacing)) {
expand[dir] = false
} else {
result = test
}
}
}
}
}
return result
}
// determine if an entity's AABB overlaps any existing level items
overlaps(aabb, minRoomSpace, minAnteroomSpace, minTunnelSpace, ignoreEntity) {
if (ignoreEntity == null) { ignoreEntity = null }
const spacing = {
room: minRoomSpace,
anteroom: minAnteroomSpace,
tunnel: minTunnelSpace,
door: 0
}
const toCheck = this.qtree.get(aabb)
for (let item of Array.from(toCheck)) {
const minSpacing = spacing[item.type]
if ((ignoreEntity != null ? ignoreEntity.id : undefined) !== item.id) {
if (overlaps(aabb, item, minSpacing)) {
return item
}
}
}
return null
}
// given 2 level rooms, determine a random space between them to place a valid door.
// returns null if door can't be placed
_getAdjacentRoomDoorPosition(room, next) {
// check if rooms are lined up on x axis ( side by side with 1 space between)
let intersection, length, x, y
if (((room.x+room.width) === (next.x - 1)) || ((room.x) === (next.x + next.width + 1))) {
intersection = intersect1D(room.y, room.y+room.height, next.y, next.y+next.height)
if (!intersection) { return null }
length = (intersection[1] - intersection[0])
y = intersection[0] + Math.round(random.next() * length)
if ((room.x+room.width) === (next.x - 1)) {
x = room.x+room.width
} else {
x = next.x + next.width
}
// TODO: ensure the space isnt occupied with anything
return { x, y, direction: 'EAST' }
}
// check if rooms are lined up on y axis ( on top of each other with 1 space between)
if (((room.y+room.height) === (next.y - 1)) || ((room.y) === (next.y + next.height + 1))) {
intersection = intersect1D(room.x, room.x+room.width, next.x, next.x+next.width)
if (!intersection) { return null }
length = (intersection[1] - intersection[0])
x = intersection[0] + Math.round(random.next() * length)
if ((room.y+room.height) === (next.y - 1)) {
y = room.y+room.height
} else {
y = next.y + next.height
}
// TODO: ensure the space isnt occupied with anything
return { x, y, direction: 'NORTH' }
}
return null
}
// get list of all adjacent but unconnected rooms
_getAdjacentRooms(room) {
const results = []
if (room.prefab) {
// don't connect from prefab rooms
return results
}
const rooms = this.objects.filter(o => o.type === 'room')
for (let next of Array.from(rooms)) {
// don't connect to prefab rooms
if (!next.prefab) {
if (next.id !== room.id) {
if (!this._roomsAreConnected(room, next)) {
const doorPosition = this._getAdjacentRoomDoorPosition(room, next)
if (doorPosition) {
results.push({room: next, doorPosition })
}
}
}
}
}
return results
}
_roomsAreConnected(room, next) {
for (let id in room.to) {
const item = room.to[id]
if ((item.entity.type === 'door') && item.entity.to[next.id]) {
return true
}
}
return false
}
_connectRoomsRandomly() {
let adjacentRooms
const rooms = this.objects.filter(o => o.type === 'room')
return Array.from(rooms).map((room) =>
// get all adjacent but unconnected rooms
((adjacentRooms = this._getAdjacentRooms(room)),
(() => {
const result = []
for (let next of Array.from(adjacentRooms)) {
// link some adjacent rooms
let item
const diceRoll = random.next()
if (diceRoll <= this.design.roomConnectionProbability) {
const d = new Door({ x: next.doorPosition.x, y: next.doorPosition.y, width: 1, height: 1, direction: next.doorPosition.direction })
d.KEEP = true
this.addEntity(d)
// link the door to the 2 rooms
let portal = getPortal(d, room)
d.to[room.id] = {entity: room, portal}
room.to[d.id] = {entity: d, portal}
portal = getPortal(d, next.room)
d.to[next.room.id] = {entity: next.room, portal}
item = next.room.to[d.id] = {entity: d, portal}
}
result.push(item)
}
return result
})()))
}
// determine if there's a path between 2 level entities
_isConnected(src, dest) {
const visited = {}
const toVisit = [ src ]
while (toVisit.length) {
const next = toVisit.pop()
if (next.id === dest.id) {
return true
}
if (!visited[next.id]) {
visited[next.id] = true
for (let id in next.to) { const child = next.to[id]; toVisit.push(child.entity) }
}
}
return false
}
// determine if all prefabs are reachable from each other
_isFullyConnected() {
const prefabs = []
const rooms = this.objects.filter(o => o.type === 'room')
for (let room of Array.from(rooms)) {
if (room.prefab) {
prefabs.push(room)
}
}
if (!(prefabs.length > 1)) { return true }
for (let prefabA of Array.from(prefabs)) {
for (let prefabB of Array.from(prefabs)) {
if (prefabA.id > prefabB.id) {
if (!this._isConnected(prefabA, prefabB)) {
return false
}
}
}
}
return true
}
_getEntranceAndExitRooms() {
const rooms = this.objects.filter(o => o.type === 'room')
if (rooms.length === 0) {
return null
}
if (rooms.length === 1) {
return [rooms[0], rooms[0]]
}
const entranceIndex = Math.floor(random.next() * rooms.length)
while (true) {
const exitIndex = Math.floor(random.next() * rooms.length)
if (exitIndex !== entranceIndex) {
return [ rooms[entranceIndex], rooms[exitIndex] ]
}
}
return null
}
_getLevelMetrics(aabbs, levelWidth, levelHeight) {
/*
* cave related metric. not useful at the moment.
* number of items directly connected to, and how many it's neighbors are connected to
for obj in @objects
obj.caveConnectedness = Object.keys(obj.to).length
for id, child of obj.to
obj.caveConnectedness += Object.keys(child.to).length
*/
// tunneling generated levels are highly connected. rate all rooms by
// seclusion: the distance from the fastest path the player can take between
// an entrance and the exit(s)
// TODO: use BFS to find the fastest path between any 2 entrance/exit pairs.
const totalArea = levelWidth * levelHeight
let maxSeclusion = 0
let totalSeclusion = 0
const rooms = this.objects.filter(o => o.type === 'room')
for (let room of Array.from(rooms)) {
room.seclusionFactor = 0 // TODO: pathfind from the room entrance to the closest point on any of the egress paths
totalSeclusion += room.seclusionFactor
maxSeclusion = Math.max(maxSeclusion, room.seclusionFactor)
}
const metrics = {
seclusion: {
average: totalSeclusion / (rooms.length || 1),
max: maxSeclusion
},
playableSpace: 0, // ratio. 0 means fully closed, 1 is fully open
goalDistance: 0, // distance from entrance to exit (e.g., 7 entities between entrance and exit)
composition: { // histogram of cell values
CLOSED: totalArea,
DOOR: 0,
ROOM: 0,
ROOM_PREFAB: 0,
TUNNEL: 0,
ANTEROOM: 0
}
}
for (let obj of Array.from(aabbs)) {
let area = obj.width * obj.height
if (obj.type === 'door') {
metrics.composition.DOOR += area
} else if (obj.type === 'anteroom') {
metrics.composition.ANTEROOM += area
} else if (obj.prefab) {
metrics.composition.ROOM_PREFAB += area
} else if (obj.type === 'room') {
metrics.composition.ROOM += area
} else if (obj.type === 'tunnel') {
metrics.composition.TUNNEL += area
} else {
area = 0
}
metrics.composition.CLOSED -= area
}
metrics.playableSpace = (totalArea - metrics.composition.CLOSED) / totalArea
return metrics
}
_postBuildStep() {
// generate a graph from all of the level objects
buildGraph(this.objects)
if (!this._isFullyConnected()) {
console.log('rejected level, not fully connected')
return null
}
this._removeRedundantTunnelLoops()
const metrics = this._getLevelMetrics(this.objects, this.design.dimensions.width, this.design.dimensions.height)
console.log('playable space:', metrics.playableSpace, ' m', metrics)
const r = (metrics.composition.ROOM + metrics.composition.ROOM_PREFAB) / (metrics.composition.TUNNEL + metrics.composition.ANTEROOM)
console.log('room:tunnel ratio', r)
if (r < 1.1) {
console.log('rejected level, room:tunnel ratio too low')
return null
}
this._connectRoomsRandomly()
//r = @_getEntranceAndExitRooms()
//if r
// console.log 'start room:', r[0]
// console.log 'end room:', r[1]
// TODO: combine compatible tunnels (same direction, tunnelWidth, and position)
//@_mergeTunnels()
this._compactEntities()
const TILE_SIZE = 32
return this._makeLevel(TILE_SIZE)
}
_makeLevel(TILE_SIZE) {
let next
const level = {
cells: this._buildGrid(),
objects: [],
cols: this.design.dimensions.width,
rows: this.design.dimensions.height
}
// convert portal coordinates from tiles to pixels
for (let obj of Array.from(this.objects)) {
for (let id in obj.to) {
next = obj.to[id]
if (!next.portal.converted) {
next.portal.converted = true
next.portal[0].x *= TILE_SIZE
next.portal[0].y *= TILE_SIZE
next.portal[1].x *= TILE_SIZE
next.portal[1].y *= TILE_SIZE
}
}
}
// convert doors to level objects
const doors = this.objects.filter(o => o.type === 'door')
for (let door of Array.from(doors)) {
next = {
type: 'door',
properties: {
x: door.x * TILE_SIZE,
y: door.y * TILE_SIZE,
doorSpeed: 0.3
}
}
if ((door.direction === 'NORTH') || (door.direction === 'SOUTH')) {
next.properties.direction = 'EAST-WEST'
next.properties.width = TILE_SIZE
next.properties.height = TILE_SIZE / 4
} else {
next.properties.direction = 'NORTH-SOUTH'
next.properties.width = TILE_SIZE / 4
next.properties.height = TILE_SIZE
}
level.objects.push(next)
}
const machines = levelFurnish(this.objects, this.theme)
// convert machines from tiles to pixels and add to the level objects
for (let machine of Array.from(machines)) {
machine.type = 'machine'
machine.x *= TILE_SIZE
machine.y *= TILE_SIZE
machine.width *= TILE_SIZE
machine.height *= TILE_SIZE
level.objects.push(machine)
}
const startRoom = this.objects.find(o => o.type === 'room')
const playerSpawn = {
type: 'player-spawn',
x: startRoom.x + 1,
y: startRoom.y + 1
}
playerSpawn.x *= TILE_SIZE
playerSpawn.y *= TILE_SIZE
level.objects.push(playerSpawn)
return level
}
_removeWorker(id) {
let idx = 0
for (let worker of Array.from(this.workers)) {
if (worker.id === id) {
removeItems(this.workers, idx, 1)
return
}
idx++
}
}
// find path from start to end
// returns the length of the path
findPath(start, end, path) {
let pathLength = 0
const frontier = [ start ]
// TODO: store portals in came_from
const came_from = {}
came_from[start.id] = start.id
while (frontier.length) {
let current = frontier.shift()
if (current.id === end.id) {
// record the path
current = end
while (current !== start.id) {
//path[pathLength] = x
//path[pathLength+] = y
current = came_from[current.id]
pathLength += 2
}
return
}
for (let id in current.to) {
const next = current.to[id]
if (came_from[id] === undefined) {
frontier.push(next)
came_from[id] = current
}
}
}
return pathLength
}
_markRoomPath(start, end, came_from) {
let current = end
return (() => {
const result = []
while (current !== start.id) {
current.KEEP = true
result.push(current = came_from[current.id])
}
return result
})()
}
_markAllRoomPaths(start) {
const frontier = [ start ]
const came_from = {}
came_from[start.id] = start.id
return (() => {
const result = []
while (frontier.length) {
var current = frontier.shift()
if ((current.id !== start.id) && (current.type === 'room')) {
this._markRoomPath(start, current, came_from)
}
result.push((() => {
const result1 = []
for (let id in current.to) {
const next = current.to[id]
let item
if (came_from[id] === undefined) {
frontier.push(next.entity)
item = came_from[id] = current
}
result1.push(item)
}
return result1
})())
}
return result
})()
}
_removeEntity(entity) {
let idx = this.objects.length - 1
while (idx >= 0) {
let { id } = this.objects[idx]
if (entity.id === id) {
// remove all references to this entity
for (id in this.objects[idx].to) {
const neighbor = this.objects[idx].to[id]
delete neighbor.entity.to[entity.id]
}
// remove the entity itself
removeItems(this.objects, idx, 1)
return
}
idx--
}
}
_removeRedundantTunnelLoops() {
const rooms = this.objects.filter(o => o.type === 'room')
for (let room of Array.from(rooms)) { this._markAllRoomPaths(room) }
// consider applying some heuristic, like allowing only X optional
// tunnels in one area, etc.
let idx = this.objects.length - 1
return (() => {
const result = []
while (idx >= 0) {
const obj = this.objects[idx]
if (!obj.KEEP) {
this._removeEntity(obj)
}
result.push(idx--)
}
return result
})()
}
_compactEntities() {
// since the level is represented with a 2d grid, minify level dimensions to
// reduce the number of cells/memory needed
// scan all entities, findining minimum (x,y) encountered
let dx, dy
let minX = null
let minY = null
for (var obj of Array.from(this.objects)) {
if ((minX === null) || (obj.x < minX)) {
minX = obj.x
}
if ((minY === null) || (obj.y < minY)) {
minY = obj.y
}
}
if (minX > 1) {
dx = -(minX - 1)
} else {
dx = 0
}
if (minY > 1) {
dy = -(minY - 1)
} else {
dy = 0
}
if (!dx && !dy) { return }
// shift all entities by dx, dy
return (() => {
const result = []
for (obj of Array.from(this.objects)) {
obj.x += dx
result.push(obj.y += dy)
}
return result
})()
}
// TODO: rewrite this as a breadth-first graph traversal. I suspect this will
// be shorter and faster
// combine compatible tunnels (same direction, tunnelWidth, and position)
_mergeTunnels() {
const deleted = {}
for (let obj of Array.from(this.objects)) {
for (let obj2 of Array.from(this.objects)) {
if (!(deleted[obj2.id] || deleted[obj.id])) {
if (obj.id > obj2.id) {
if ((obj.type === 'tunnel') && (obj2.type === 'tunnel')) {
var max, min, width
if (((obj.direction === 'EAST') || (obj.direction === 'WEST')) && ((obj2.direction === 'EAST') || (obj2.direction === 'WEST'))) {
if ((obj.y === obj2.y) && (obj.height === obj2.height)) {
if (intersect1D(obj.x, obj.x + obj.width, obj2.x, obj2.x + obj2.width)) {
// they overlap, take the min/max extents
min = Math.min(obj.x, obj2.x)
max = Math.max(obj.x + obj.width, obj2.x + obj2.width)
width = (max - min) + 1
obj.x = min
obj.width = width
// TODO: connect all of obj2's neighbors to obj
deleted[obj2.id] = true // mark object 2 as removed
}
}
}
if (((obj.direction === 'NORTH') || (obj.direction === 'SOUTH')) && ((obj2.direction === 'NORTH') || (obj2.direction === 'SOUTH'))) {
if ((obj.x === obj2.x) && (obj.width === obj2.width)) {
if (intersect1D(obj.y, obj.y + obj.height, obj2.y, obj2.y + obj2.height)) {
// they overlap, take the min/max extents
min = Math.min(obj.y, obj2.y)
max = Math.max(obj.y + obj.height, obj2.y + obj2.height)
const height = (max - min) + 1
obj.y = min
obj.height = height
// TODO: connect all of obj2's neighbors to obj
deleted[obj2.id] = true // mark object 2 as removed
}
}
}
}
}
}
}
}
// remove all the tunnels marked for deletion
let idx = this.objects.length - 1
return (() => {
const result = []
while (idx >= 0) {
const { id } = this.objects[idx]
if (deleted[id]) {
this._removeEntity(this.objects[idx])
}
result.push(idx--)
}
return result
})()
}
// convert AABBs to grid, setting id of each item in cell.
_buildGrid() {
//grid = zeros [ @design.dimensions.width, @design.dimensions.height ]
const grid = []
// start with assuming all spaces are filled
for (let i = 0, end = (this.design.dimensions.width*this.design.dimensions.height)-1, asc = 0 <= end; asc ? i <= end : i >= end; asc ? i++ : i--) {
grid.push(1)
}
// hollow out all entities (rooms, anteroom, tunnels, etc.)
for (let obj of Array.from(this.objects)) {
//if obj.type isnt 'door'
for (let row = obj.y, end1 = (obj.y+obj.height)-1, asc1 = obj.y <= end1; asc1 ? row <= end1 : row >= end1; asc1 ? row++ : row--) {
for (let col = obj.x, end2 = (obj.x+obj.width)-1, asc2 = obj.x <= end2; asc2 ? col <= end2 : col >= end2; asc2 ? col++ : col--) {
const idx = (row * this.design.dimensions.width) + col
//grid.set row, col, 0
grid[idx] = 0
}
}
}
return grid
}
}
module.exports = LevelGenerator