-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
1011 lines (915 loc) · 33.3 KB
/
map.js
File metadata and controls
1011 lines (915 loc) · 33.3 KB
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
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Game.Map = function(tiles, player, items, stairs, gasMap) {
this._tiles = tiles;
// cache the width and height based
// on the length of the dimensions of
// the tiles array
this._width = tiles.length;
this._height = tiles[0].length;
this._stairs = stairs;
// create a hash table which will hold the entities
this._entities = {};
// Create a table which will hold the items
this._items = {};
// create the engine and scheduler
this._scheduler = new ROT.Scheduler.Speed();
this._engine = new ROT.Engine(this._scheduler);
// add the player
this._player = player;
this._gasMap = gasMap;
//runes and hellfire
this.setDynamicTile('protectTile');
this.setDynamicTile('protectTile');
this.setDynamicTile('vulnerabilityTile');
this.setDynamicTile('vulnerabilityTile');
if (Game.getLevel() > 6){
this.setDynamicTile('vulnerabilityTile');
this.setDynamicTile('vulnerabilityTile');
}
if (Game.getLevel() > 9){
this.setDynamicTile('vulnerabilityTile');
this.setDynamicTile('vulnerabilityTile');
}
if (Game.getLevel() > 12){
let position = this.getRandomFloorPosition();
let list = [];
list.push(position);
this.cellGrow(list, 'hellFireTile', 10, true);
position = this.getRandomFloorPosition();
list = [];
list.push(position);
this.cellGrow(list, 'hellFireTile', 10, true);
position = this.getRandomFloorPosition();
list = [];
list.push(position);
this.cellGrow(list, 'hellFireTile', 10, true);
position = this.getRandomFloorPosition();
list = [];
list.push(position);
this.cellGrow(list, 'hellFireTile', 10, true);
}
//App Player
this.addEntityAtRandomPosition(player, 0);
//set up the field of vision
this._fov = {};
this.setupFov();
// 15 entities per floor
let entitiesPerArea = 10
if (Game.getLevel() > 6){
entitiesPerArea = 15;
}
if (Game.getLevel() > 9){
entitiesPerArea = 21;
}
for (let i = 0; i < entitiesPerArea; i++) {
// Add a random entity
let randomEntity = Game.EntityRepository.createRandomByFrequency('L' + Game.getLevel())
this.addEntityAtRandomPosition( randomEntity , 1);
if (Game.getLevel() <= 3){
this.addEntityAtRandomPosition(Game.EntityRepository.create("barrel"), 0);
}
}
// if on L3, create one rat king
if (Game.getLevel() === 3){
this.addEntityAtRandomPosition(Game.EntityRepository.create('rat king'), 1);
}
// if on L6, create one toad queen
if (Game.getLevel() === 6){
this.addEntityAtRandomPosition(Game.EntityRepository.create('toad queen'), 1);
}
// if on L9, create one hydra
if (Game.getLevel() === 9){
this.addEntityAtRandomPosition(Game.EntityRepository.create('hydra'), 1);
}
// if on L11, create one vampire
if (Game.getLevel() === 11){
this.addEntityAtRandomPosition(Game.EntityRepository.create('vampire'), 1);
}
// if on L12, create one cerberus
if (Game.getLevel() === 12){
this.addEntityAtRandomPosition(Game.EntityRepository.create('cerberus'), 1);
}
// if on L12, create one underwyrm
if (Game.getLevel() === 13){
this.addEntityAtRandomPosition(Game.EntityRepository.create('underwyrm'), 1);
}
// if on L12, create two underwyrms
if (Game.getLevel() === 14){
this.addEntityAtRandomPosition(Game.EntityRepository.create('underwyrm'), 1);
this.addEntityAtRandomPosition(Game.EntityRepository.create('underwyrm'), 1);
}
// if on L15, create one lich
if (Game.getLevel() === 15){
this.addEntityAtRandomPosition(Game.EntityRepository.create('lich'), 1);
}
/*let emptyItemList = [];
let emptyItemCount = {};
for (let i =0; i < 1000; i++){
emptyItemList.push(Game.ItemRepository.createRandomConstrained(Game.getLevel()));
emptyItemList.sort();
}
for (let item of emptyItemList) {
if( Object.keys(emptyItemCount).includes(item) ){
emptyItemCount[item] = emptyItemCount[item] + 1;
} else {
emptyItemCount[item] = 1;
}
}
console.log(emptyItemCount);*/
// Items per floor
let itemsPerArea = 6;
if (Game.getLevel() > 3){
itemsPerArea = 5;
}
if (Game.getLevel() > 6){
itemsPerArea = 4;
}
for (let i = 0; i < itemsPerArea; i++) {
// Add a random entity
this.addItemAtRandomPosition(Game.ItemRepository.createRandomConstrained(Game.getLevel()));
}
this.addGatedItem();
//set up the explored array
this._explored = new Array(this._width);
this._setupExploredArray();
};
// Standard getters
Game.Map.prototype.getWidth = function() {
return this._width;
};
Game.Map.prototype.getHeight = function() {
return this._height;
};
Game.Map.prototype.getWalkableByStairs = function() {
walkableByStairs = []
let x = this._stairs.x
let y = this._stairs.y
if (this._tiles[x - 1][y].isWalkable()){
walkableByStairs.push({
x: x - 1,
y: y
});
}
if (this._tiles[x + 1][y].isWalkable()){
walkableByStairs.push({
x: x + 1,
y: y
});
}
if (this._tiles[x][y - 1].isWalkable()){
walkableByStairs.push({
x: x,
y: y - 1
});
}
if (this._tiles[x][y + 1].isWalkable()) {
walkableByStairs.push({
x: x,
y: y + 1
});
}
return walkableByStairs;
};
Game.Map.prototype.getEngine = function() {
return this._engine;
}
Game.Map.prototype.getPlayer = function() {
return this._player;
};
Game.Map.prototype.getEntities = function() {
return this._entities;
}
Game.Map.prototype.getEntityAt = function(x, y){
// Iterate through all entities searching for one with matching position
return this._entities[x + ',' + y];
}
Game.Map.prototype.isInBounds = function(x, y){
if (x < 0 || x >= this._width || y < 0 || y >= this._height) return 0;
else return 1;
}
Game.Map.prototype.isFireTile = function(x, y){
if (!this.isInBounds(x, y)) {
return Game.Tile.nullTile;
} else {
return this._tiles[x][y].getDescription() === "an eternal shimmering flame" || this._tiles[x][y].getDescription() === "a dancing flame";
}
}
Game.Map.prototype.addGatedItem = function() {
const randNum = Math.random()*100;
let chosenItem = ''
let alreadyGotList = Game.Screen.playScreen.alreadyGotList;
if (alreadyGotList.length === 3 || alreadyGotList.length === 0){
if ( randNum > 66){
chosenItem = 'strength potion';
} else if (randNum > 33) {
chosenItem = 'altar';
} else {
chosenItem = 'life potion';
}
Game.Screen.playScreen.alreadyGotList = [];
Game.Screen.playScreen.alreadyGotList.push(chosenItem)
} else if (alreadyGotList.length === 2){
//find the missing item
if (!alreadyGotList.includes("strength potion")) chosenItem = 'strength potion';
else if (!alreadyGotList.includes("altar")) chosenItem = 'altar';
else if (!alreadyGotList.includes("life potion")) chosenItem = 'life potion';
Game.Screen.playScreen.alreadyGotList.push(chosenItem);
} else if (alreadyGotList.length === 1){
//find the missing two, use those on a 50% chance
if (alreadyGotList.includes("altar")){
firstOption = 'strength potion';
secondOption = 'life potion';
} else if (alreadyGotList.includes("strength potion")){
firstOption = 'altar';
secondOption = 'life potion';
} else if (alreadyGotList.includes("life potion")){
firstOption = 'strength potion';
secondOption = 'altar';
}
chosenItem = (randNum > 50) ? firstOption : secondOption;
Game.Screen.playScreen.alreadyGotList.push(chosenItem);
}
this.spawnGatedItem(chosenItem);
};
Game.Map.prototype.spawnGatedItem = function(in_string) {
if (in_string === 'altar'){
let floorPosition = this.getRandomFloorPosition();
this._tiles[floorPosition.x][floorPosition.y] = Game.Tile.altarTile;
} else {
this.addItemAtRandomPosition(Game.GatedItemRepository.create(in_string));
}
};
Game.Map.prototype.cleanUpDoors = function(){
let doorList = this.getAllTlesOfType(Game.Tile.doorTile);
for (let door of doorList){
if (this.checkAdjacentNumber(door.x, door.y, Game.Tile.wallTile) !== 2){
this._tiles[door.x][door.y] = Game.Tile.floorTile;
//console.log('changing door');
}
}
}
Game.Map.prototype.deleteHalfOfDoors = function(){
let doorList = this.getAllTlesOfType(Game.Tile.doorTile);
let doorsToDelete = Math.floor(doorList.length/2);
for (let i = 0; i < doorsToDelete; i++){
let door = Game.pickRandomElement(doorList)
this._tiles[door.x][door.y] = Game.Tile.floorTile;
}
}
Game.Map.prototype.getAllTlesOfType = function(tileType){
let tileTypeList = [];
let rowCount = 0;
let columnCount = 0;
for (let row of this._tiles) {
columnCount = 0;
for (let tile of row){
if (tile === tileType){
tileTypeList.push(
{
x: rowCount,
y: columnCount
}
)
}
columnCount++;
}
rowCount++;
}
return tileTypeList;
}
Game.Map.prototype.checkAdjacentNumber = function(x,y,tileType) {
let adjacentCount = 0;
if (x > 0 && this._tiles[x - 1][y] === tileType){
adjacentCount++;
}
if (x < Game.getScreenWidth() - 1 && this._tiles[x + 1][y] === tileType){
adjacentCount++;
}
if (y > 0 && this._tiles[x][y - 1] === tileType){
adjacentCount++;
}
if (y < Game.getScreenHeight() - 1 && this._tiles[x][y + 1] === tileType) {
adjacentCount++;
}
return adjacentCount;
}
Game.Map.prototype.getRidOfBoringRooms = function (rooms) {
let boringRooms = this.findBoringRooms(rooms);
for (room of boringRooms){
let left = room.getLeft();
let right = room.getRight();
let top = room.getTop();
let bottom = room.getBottom();
for (let x = left; x <= right; x++){
for (let y = top; y <= bottom; y ++){
this._tiles[x][y] = Game.Tile.wallTile;
}
}
for (let key in room._doors){
let xy = key.split(",");
let x = xy[0];
let y = xy[1];
this._tiles[Number(x)][Number(y)] = Game.Tile.wallTile;
}
}
}
Game.Map.prototype.putGoldInBoringRooms = function (rooms) {
let boringRooms = this.findBoringRooms(rooms);
for (room of boringRooms){
let left = room.getLeft();
let right = room.getRight();
let top = room.getTop();
let bottom = room.getBottom();
let x, y;
let count = 0;
do {
x = Math.ceil(Math.random() * Math.abs(left - right)- 1);
y = Math.ceil(Math.random() * Math.abs(top - bottom)- 1);
count++;
} while(!this.isEmptyTileOfType(left + x, top + y, Game.Tile.floorTile) && !this.isEmptyTileOfType(left + x, top + y, Game.Tile.grassTile) && count < 100);
let item = Game.ItemRepository.create('gold')
this.addItem(left + x, top + y, item);
}
}
Game.Map.prototype.findBoringRooms = function (rooms) {
let boringRooms = [];
for (room of rooms){
if (Object.keys(room._doors).length === 1){
let left = room.getLeft();
let right = room.getRight();
let top = room.getTop();
let bottom = room.getBottom();
let roomMatters = false;
for (let x = left; x <= right; x++){
for (let y = top; y <= bottom; y ++){
//check for stairs
if (this._tiles[x][y] === Game.Tile.stairsDownTile || this._tiles[x][y] === Game.Tile.stairsDownTileLocked){
roomMatters = true;
}
//check for entities
const key = x + ',' + y;
if (this._entities[key] !== undefined && this._entities[key] !== null && !this._entities[key].isNotMonster()) {
roomMatters = true;
}
//check for items
if (this.getItemsAt(x,y) !== undefined){
roomMatters = true;
}
}
}
if (roomMatters === false) {
boringRooms.push(room);
}
}
}
return boringRooms;
}
// Gets the tile for a given coordinate set
Game.Map.prototype.getTile = function(x, y) {
// Make sure we are inside the bounds. If we aren't, return
// null tile.
if (!this.isInBounds(x, y)) {
return Game.Tile.nullTile;
} else {
return this._tiles[x][y] || Game.Tile.nullTile;
}
};
// Set the tile for a given coordinate set
Game.Map.prototype.setTile = function(x, y, tileType) {
// Make sure we are inside the bounds. If we aren't, return
// null tile.
if (!this.isInBounds(x, y)) {
console.log('out of bounds!');
} else {
this._tiles[x][y] = tileType;
}
};
// Gets the tile for a given coordinate set
Game.Map.prototype.getGas = function(x, y) {
// Make sure we are inside the bounds. If we aren't, return
// null tile.
if (!this.isInBounds(x, y)) {
return null;
} else {
return this._gasMap[x][y] || null;
}
};
// Gets the full tile array
Game.Map.prototype.getTiles = function() {
return this._tiles;
};
Game.Map.prototype.shatter = function(x, y) {
for (let i = x-2; i <= x + 2; i++){
for (let j = y-2; j <= y+2; j++){
if (!(i < 1 || i >= this._width - 1 || j < 1 || j >= this._height - 1)) {
this.dig(i,j);
}
}
}
if (!(x - 3 < 1)) {
this.dig(x-3,y);
}
if (!(x + 3 >= this._width - 1)) {
this.dig(x+3,y);
}
if (!(y - 3 < 1)) {
this.dig(x,y-3);
}
if (!(y + 3 >= this._height - 1)) {
this.dig(x,y+3);
}
};
Game.Map.prototype.dig = function(x, y) {
// If the tile is diggable, update it to a floor
if (this.getTile(x, y).isDiggable()) {
this._tiles[x][y] = Game.Tile.rubbleTile;
}
const entity = this.getEntityAt(x, y);
if (entity){
if (entity.isNotMonster() || entity.getName() === 'golem'){
entity.takeDamage(entity, 50, false);
}
}
};
Game.Map.prototype.changeTile = function(x, y, template) {
if (this._tiles[x][y] !== Game.Tile.floorTile) {return;}
this._tiles[x][y] = template;
};
Game.Map.prototype.getRandomFloorPosition = function() {
// Randomly choose a tile which is a floor
let x, y;
do {
x = Math.floor(Math.random() * this._width);
y = Math.floor(Math.random() * this._height);
} while(!this.isEmptyTileOfType(x, y, Game.Tile.floorTile));
return {x: x, y: y};
}
Game.Map.prototype.getRandomWaterPosition = function() {
// Randomly choose a tile which is water
let x, y;
do {
x = Math.floor(Math.random() * this._width);
y = Math.floor(Math.random() * this._height);
} while(!this.isEmptyTileOfType(x, y, Game.Tile.shallowWaterTile));
return {x: x, y: y};
}
Game.Map.prototype.addEntity = function(entity) {
// Update the entity's map
entity.setMap(this);
// Update the map with the entity's position
this.updateEntityPosition(entity);
// Check if this entity is an actor, and if so add them to the scheduler
if (entity.hasMixin('Actor')) {
this._scheduler.add(entity, true);
}
}
//adds and entity on an empty floor tile
Game.Map.prototype.addEntityAtRandomPosition = function(entity, outOfSightline) {
let isSwimmer = entity.hasMixin('Swimmer');
let position = isSwimmer ? this.getRandomWaterPosition() : this.getRandomFloorPosition();
if (outOfSightline){
// Cache the FOV
let visibleCells = {};
this.getFov().compute(
this._player.getX(), this._player.getY(),
this._player.getSightRadius(),
function(x, y, radius, visibility) {
visibleCells[x + "," + y] = true;
}
);
while (visibleCells[String(position.x) + ',' + String(position.y)] === true){
position = isSwimmer ? this.getRandomWaterPosition() : this.getRandomFloorPosition();
}
}
entity.setX(position.x);
entity.setY(position.y);
this.addEntity(entity);
}
//adds and entity on an file, far away
Game.Map.prototype.addEntityAtDistantPosition = function(entity, x, y, outOfSightline) {
let position = this.getRandomFloorPosition();
let tileCandidates = [];
if (outOfSightline){
// Cache the FOV
let visibleCells = {};
this.getFov().compute(
this._player.getX(), this._player.getY(),
this._player.getSightRadius(),
function(x, y, radius, visibility) {
visibleCells[x + "," + y] = true;
}
);
//find 20 candidates
while (tileCandidates.length < 100){
position = this.getRandomFloorPosition();
if (visibleCells[String(position.x) + ',' + String(position.y)] === undefined) {
tileCandidates.push(position);
}
}
}
//sort tileCandidates by distance
let totalDist = 0;
let winningX = 0;
let winningY = 0;
for (tileCandidate of tileCandidates){
let newTotalDist = Math.abs(tileCandidate.x - x) + Math.abs(position.y - y);
if (newTotalDist > totalDist) {
totalDist = newTotalDist;
winningX = tileCandidate.x;
winningY = tileCandidate.y;
}
}
entity.setX(winningX);
entity.setY(winningY);
this.addEntity(entity);
}
Game.Map.prototype.removeEntity = function(entity) {
// Remove the entity from the map
const key = entity.getX() + ',' + entity.getY();
if (this._entities[key] == entity) {
delete this._entities[key];
}
// If the entity is an actor, remove them from the scheduler
if (entity.hasMixin('Actor')) {
this._scheduler.remove(entity);
}
}
Game.Map.prototype.isEmptyTileOfType = function(x, y, tileType) {
// Check if the tile is of a certain type and also has no entity
return this.getTile(x, y) == tileType &&
!this.getEntityAt(x, y);
}
Game.Map.prototype.getEntitiesWithinRadius = function(centerX, centerY, radius) {
results = [];
// Determine our bounds
const leftX = centerX - radius;
const rightX = centerX + radius;
const topY = centerY - radius;
const bottomY = centerY + radius;
// Iterate through our entities, adding any which are within the bounds
for (let key in this._entities) {
let entity = this._entities[key];
if (entity.getX() >= leftX &&
entity.getX() <= rightX &&
entity.getY() >= topY &&
entity.getY() <= bottomY) {
results.push(entity);
}
}
return results;
}
Game.Map.prototype.setDynamicTilePosition = function(runeName, x, y){
let tileObject = Game.DynamicTileRepository.create(runeName);
this.addDynamicTile(tileObject, x, y);
}
Game.Map.prototype.setDynamicTile = function(runeName) {
let matches = [];
// Iterate through all tiles, checking if they are floor tiles.
for (let x = 0; x < this._width; x++) {
for (let y = 0; y < this._height; y++) {
if (this._tiles[x][y] == Game.Tile.floorTile) {
matches.push({x: x, y: y});
}
}
}
// We shuffle the list of matches to prevent bias
let match = Game.pickRandomElement(matches);
this.setDynamicTilePosition(runeName, match.x, match.y);
}
Game.Map.prototype.sortByDistance = function(x,y,locationArray) {
let locationDistance = [];
for (let location of locationArray){
let distance = Math.abs(x - location.x) + Math.abs(y - location.y)
locationDistance.push({
x: location.x,
y: location.y,
distance: distance
})
}
locationDistance.sort(function(a, b){return a.distance - b.distance;});
return locationDistance;
}
Game.Map.prototype.setupFov = function() {
// Keep this in 'map' variable so that we don't lose it.
let map = this;
// We need to create a callback which figures out if light can pass through a given tile.
map._fov = new ROT.FOV.DiscreteShadowcasting(function(x, y) {
var gas = map.getGas(x,y);
return !map.getTile(x,y).isBlockingLight() && (gas === null || !gas.isBlockingLight());
},
{topology: 4}
);
}
Game.Map.prototype.getFov = function() {
return this._fov;
}
Game.Map.prototype._setupExploredArray = function() {
for (let x = 0; x < this._width; x++) {
this._explored[x] = new Array(this._height);
for (let y = 0; y < this._height; y++) {
this._explored[x][y] = false;
}
}
};
Game.Map.prototype.setExplored = function(x, y, state) {
// Only update if the tile is within bounds
if (this.getTile(x, y) !== Game.Tile.nullTile) {
this._explored[x][y] = state;
}
};
Game.Map.prototype.isExplored = function(x, y) {
// Only return the value if within bounds
if (this.getTile(x, y) !== Game.Tile.nullTile) {
return this._explored[x][y];
} else {
return false;
}
};
Game.Map.prototype.updateEntityPosition = function(entity, oldX, oldY) {
// Delete the old key if it is the same entity and we have old positions.
if (typeof(oldX) !=="undefined") {
let oldKey = oldX + ',' + oldY;
if (this._entities[oldKey] == entity) {
delete this._entities[oldKey];
}
}
// Make sure the entity's position is within bounds
if (!this.isInBounds(entity.getX(), entity.getY())) {
throw new Error("Entity's position is out of bounds.");
}
// Sanity check to make sure there is no entity at the new position.
let key = entity.getX() + ',' + entity.getY();
if (this._entities[key]) {
throw new Error('Tried to add an entity at an occupied position.');
}
// Add the entity to the table of entities
this._entities[key] = entity;
};
//find a tile with not item on it
Game.Map.prototype.findFreeTile = function(x, y){
let radius = 0;
let maxRadius = 4;
while (radius <= maxRadius){
// Iterate through our tiles, escaping for the first one that is item-free
// Do the cross first
for (let i = 0 - radius; i <= radius; i++) {
let items = this._items[(x + i) + ',' + y];
if ( (items === undefined || items.length === 0) && this.isInBounds(x + i, y) && this._tiles[x+i][y].isWalkable()){
// catch the edge case of the starting tile not having a path to itself
if (radius === 0 ||this.checkPathLength(x, y, x + i, y, maxRadius*2)){
return {
x: x + i,
y: y
};
}
}
}
for (let j = 0 - radius; j <= radius; j++) {
let items = this._items[x + ',' + (y + j)];
if ( (items === undefined || items.length === 0) && this.isInBounds(x, y + j) && this._tiles[x][y+j].isWalkable()){
if (this.checkPathLength(x, y, x, y + j, maxRadius*2)){
return {
x: x,
y: y + j
};
}
}
}
//Then the rest of the cells in the current radius
for (let i = 0 - radius; i <= radius; i++) {
for (let j = 0 - radius; j <= radius; j++){
let items = this._items[(x + i) + ',' + (y + j)];
if ( (items === undefined || items.length === 0) && this.isInBounds(x + i, y + j) && this._tiles[x+i][y+j].isWalkable()){
if (this.checkPathLength(x, y, x + i, y + j, maxRadius*2)){
return {
x: x + i,
y: y + j
};
}
}
}
}
radius ++;
}
//last resort is a stack
return {x:x, y:y};
}
Game.Map.prototype.checkPathLength = function(sourceX, sourceY, targetX, targetY, pathLength) {
var thisRef = this;
var path = new ROT.Path.AStar(targetX, targetY, function(x, y) {
return thisRef.getTile(x, y).isWalkable();
}, {topology: 4});
// Once we've gotten the path, we want to move to the second cell that is passed in the callback (the first is the entity's starting point)
path.compute(sourceX, sourceY, function(x, y) {});
//If we can get there, go to it
if(path._todo.length > 0 && path._todo.length <= pathLength){
return true;
}
return false;
};
Game.Map.prototype.getItemsAt = function(x, y) {
return this._items[x + ',' + y];
};
Game.Map.prototype.setItemsAt = function(x, y, items) {
// If our items array is empty, then delete the key from the table.
let key = x + ',' + y;
if (items.length === 0) {
if (this._items[key]) {
delete this._items[key];
}
} else {
// Simply update the items at that key
this._items[key] = items;
}
};
Game.Map.prototype.addItem = function(x, y, item) {
// If we already have items at that position, simply append the item to the list of items.
let key = x + ',' + y;
if (this._items[key]) {
this._items[key].push(item);
} else {
this._items[key] = [item];
}
};
Game.Map.prototype.addItemAtRandomPosition = function(item) {
let position = this.getRandomFloorPosition();
while (this.getItemsAt(position.x, position.y)){
position = this.getRandomFloorPosition();
}
this.addItem(position.x, position.y, item);
};
Game.Map.prototype.addDynamicTile = function(tile, x, y) {
this.addCell(tile, x, y, true);
// Check if this tile is an actor, and if so add them to the scheduler
if (tile.hasMixin('Actor')) {
this._scheduler.add(tile, true);
}
};
Game.Map.prototype.addGas = function(gas, x, y) {
this.addCell(gas, x, y, false);
// Add gas to the scheduler
this._scheduler.add(gas, true);
};
Game.Map.prototype.addCell = function (cell, x, y, isTile){
cell.setMap(this);
// Make sure the cell's position is within bounds
if (!this.isInBounds(x,y)) {
throw new Error("Cell's position is out of bounds.");
}
// Add the cell to the table of cells (tiles or gas)
if (isTile) {
this._tiles[x][y] = cell;
} else {
this._gasMap[x][y] = cell;
}
cell.setX(x);
cell.setY(y);
}
Game.Map.prototype.removeDynamicTile = function(tile) {
// Remove the dynamic tile from the map
let x = tile.getX();
let y = tile.getY();
if (this._tiles[x][y] === tile) {
this._tiles[x][y] = Game.Tile.floorTile;
}
// If the entity is an actor, remove them from the scheduler
if (tile.hasMixin('Actor')) {
this._scheduler.remove(tile);
}
}
Game.Map.prototype.removeGas = function(gas) {
// Remove the gas from the map
let x = gas.getX();
let y = gas.getY();
if (this._gasMap[x][y] === gas) {
this._gasMap[x][y] = null;
}
// Remove them from the scheduler
this._scheduler.remove(gas);
}
Game.Map.prototype.areHunters = function() {
let nearbyEntities = [];
nearbyEntities = this.getEntitiesWithinRadius(this._player.getX(),this._player.getY(),16)
// Remove the entity from the map
if (nearbyEntities.length === 0) {
return false;
}
for (let entity of nearbyEntities){
if (entity.hasMixin('TaskActor')){
if (entity._hunting){
return true;
}
}
}
return false;
}
Game.Map.prototype.cellGrow = function(list, tileType, numberOfTiles, isDynamic) {
let growthCount = 0;
let i = 0;
while (growthCount < numberOfTiles){
let currentTile = list[i]
if (currentTile === undefined){
break;
}
let x = currentTile.x;
let y = currentTile.y;
if (this._tiles[x][y] === Game.Tile.floorTile || this._tiles[x][y] === Game.Tile.bloodTile || this._tiles[x][y] === Game.Tile.grassTile || this._tiles[x][y] === Game.Tile.rubbleTile || this._tiles[x][y] === Game.Tile.openDoorTile) {
this._tiles[x][y] = tileType;
if (isDynamic){
let tileObject = Game.DynamicTileRepository.create(tileType);
this.addDynamicTile(tileObject, x, y);
}
list.push({x: x, y: y});
growthCount ++;
if (growthCount === numberOfTiles) break;
}
if (this._tiles[x - 1][y] === Game.Tile.floorTile || this._tiles[x - 1][y] === Game.Tile.bloodTile || this._tiles[x - 1][y] === Game.Tile.grassTile || this._tiles[x - 1][y] === Game.Tile.rubbleTile || this._tiles[x - 1][y] === Game.Tile.openDoorTile) {
this._tiles[x - 1][y] = tileType;
if (isDynamic){
let tileObject = Game.DynamicTileRepository.create(tileType);
this.addDynamicTile(tileObject, x - 1, y);
}
list.push({x: x - 1, y: y});
growthCount ++;
if (growthCount === numberOfTiles) break;
}
if (this._tiles[x + 1][y] === Game.Tile.floorTile || this._tiles[x + 1][y] === Game.Tile.bloodTile || this._tiles[x + 1][y] === Game.Tile.grassTile || this._tiles[x + 1][y] === Game.Tile.rubbleTile || this._tiles[x + 1][y] === Game.Tile.openDoorTile) {
this._tiles[x + 1][y] = tileType;
if (isDynamic){
let tileObject = Game.DynamicTileRepository.create(tileType);
this.addDynamicTile(tileObject, x + 1, y);
}
list.push({x: x + 1, y: y});
growthCount ++;
if (growthCount === numberOfTiles) break;
}
if (this._tiles[x][y - 1] === Game.Tile.floorTile || this._tiles[x][y - 1] === Game.Tile.bloodTile || this._tiles[x][y - 1] === Game.Tile.grassTile || this._tiles[x][y - 1] === Game.Tile.rubbleTile || this._tiles[x][y - 1] === Game.Tile.openDoorTile) {
this._tiles[x][y -1] = tileType;
if (isDynamic){
let tileObject = Game.DynamicTileRepository.create(tileType);
this.addDynamicTile(tileObject, x, y - 1);
}
list.push({x: x, y: y - 1});
growthCount ++;
if (growthCount === numberOfTiles) break;
}
if (this._tiles[x][y + 1] === Game.Tile.floorTile || this._tiles[x][y + 1] === Game.Tile.bloodTile || this._tiles[x][y + 1] === Game.Tile.grassTile || this._tiles[x][y + 1] === Game.Tile.rubbleTile || this._tiles[x][y + 1] === Game.Tile.openDoorTile) {
this._tiles[x][y + 1] = tileType;
if (isDynamic){
let tileObject = Game.DynamicTileRepository.create(tileType);
this.addDynamicTile(tileObject, x, y + 1);
}
list.push({x: x, y: y + 1});
growthCount ++;
if (growthCount === numberOfTiles) break;
}
i++
}
};
Game.Map.prototype.gasGrow = function(list, gasType, numberOfCells) {
let growthCount = 0;
let i = 0;
while (growthCount < numberOfCells){
let currentTile = list[i]
if (currentTile === undefined){
break;
}
let x = currentTile.x;
let y = currentTile.y;
if (this._tiles[x][y] !== Game.Tile.wallTile && this._tiles[x][y] !== Game.Tile.doorTile) {
this._gasMap[x][y] = gasType;
let gasObject = Game.GasRepository.create(gasType);
this.addGas(gasObject, x, y);
list.push({x: x, y: y});
growthCount++;
}
if (this._tiles[x - 1][y] !== Game.Tile.wallTile && this._tiles[x - 1][y] !== Game.Tile.doorTile) {
this._gasMap[x - 1][y] = gasType;
let gasObject = Game.GasRepository.create(gasType);
this.addGas(gasObject, x -1, y);
list.push({x: x - 1, y: y});
growthCount++;
}
if (this._tiles[x + 1][y] !== Game.Tile.wallTile && this._tiles[x + 1][y] !== Game.Tile.doorTile) {
this._gasMap[x + 1][y] = gasType;
let gasObject = Game.GasRepository.create(gasType);
this.addGas(gasObject, x + 1, y);
list.push({x: x + 1, y: y});
growthCount++;
}
if (this._tiles[x][y - 1] !== Game.Tile.wallTile && this._tiles[x][y - 1] !== Game.Tile.doorTile) {
this._gasMap[x][y -1] = gasType;
let gasObject = Game.GasRepository.create(gasType);
this.addGas(gasObject, x, y - 1);
list.push({x: x, y: y - 1});
growthCount++;