-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrowser.js
2826 lines (2802 loc) · 99.2 KB
/
browser.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
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
const s = () => {// various settings and magic numbers, these will get inlined by uglify
const tileSize = 16;
const fontSize = 8;
const computerIconSize = 7;
const viewTiles = 9;
const canvasTiles = viewTiles + 1;
const fontTiles = canvasTiles * 2;
const centerTile = ~~(viewTiles / 2);
const mapSize = tileSize * canvasTiles;
const animTime = 500;
const waterBorder = ~~(mapSize / 20);
const landBorder = ~~(mapSize / 8);
const gridTiles = 4;
const gridSize = ~~(mapSize / gridTiles);
const initialMonsterCount = ~~(mapSize / 20);
const sunrise = 6;
const sunset = 18;
// async image loader
const loadImage = (path) => new Promise(resolve => {
const img = new Image();
img.onload = () => resolve(img);
img.src = path;
});
// load a series of images
const loadImages = (...paths) => Promise.all(paths.map(loadImage));
// randomly pick something from an array
const pick = (arr) => arr[randInt(arr.length)];
// generate a random integer between min and exclusive max
const randInt = (exclMax, min = 0) => ~~(Math.random() * exclMax) + min;
// not very sound way to shuffle an array randomly - but very cheap in bytes
const shuffle = (arr) => arr.slice().sort(() => randInt(3) - 1);
// sprite indices
const T_SEA = 0;
const T_WATER = 1;
const T_LAND = 2;
const T_GRASS = 3;
const T_GRASS_L = 8;
const T_TREE = 11;
const T_TREE_L = 4;
const T_FOOD = 15;
const T_HEALTH = 16;
const T_SAND = 17;
const T_SAND_L = 3;
const T_HUT = 20;
const T_COMPUTER = 21;
const T_SYNTH = 22;
const T_BED = 23;
const T_HUT_L = 24;
const T_HUT_M = 25;
const T_HUT_R = 26;
const T_BLACK = 27;
const T_RUINS = 28;
const T_RUINS_L = 3;
const T_MOUNTAINS = 31;
const T_MOUNTAINS_L = 3;
const T_SATELLITE = 34;
const T_PORTAL = 36;
const T_RANGER = 38;
const T_KEY = 39;
const T_DISK = 40;
const T_CHIP = 41;
const T_FOG = 42;
const T_PORTAL_OFFLINE = 43;
const T_PORTAL_DAY = 44;
const S_SKELETON = 4;
const S_BOAT_LEFT = 5;
const S_BOAT_RIGHT = 6;
const S_MONSTER = 7;
const C_HUT_LOCKED = 0;
const C_RUINS_ACTIVE = 1;
const C_SATELLITE_OFFLINE = 2;
const C_PLAYER = 3;
const C_PORTAL_ACTIVE = 4;
const C_HUT_UNLOCKED = 5;
const C_RUINS_EMPTY = 6;
const C_SATELLITE_ACTIVE = 7;
const C_PORTAL_OFFLINE = 8;
// state indices
const ST_PLAYER_FACING = 0;
const ST_PLAYER_FOOD = 1;
const ST_PLAYER_HEALTH = 2;
const ST_PLAYER_MAX_HEALTH = 3;
const ST_HOURS = 4;
const ST_MINUTES = 5;
const ST_COLOR = 6;
const ST_DISPLAY_ITEM = 7;
const ST_MONSTERS = 8;
const ST_PLAYER_KEYS = 9;
const ST_PLAYER_CHIPS = 10;
const ST_PLAYER_DISKS = 11;
const ST_SEEN = 12;
const ST_HUTCACHE = 13;
const ST_RUINCACHE = 14;
const ST_PORTALCACHE = 15;
const ST_SATELLITE_FIXED = 16;
const ST_MOD_CHIPS = 17;
const ST_SATELLITE_CHIPS = 18;
// api indices
const API_STATE = 0;
const API_RESET = 1;
const API_TIMESTR = 2;
const API_INCTIME = 3;
const API_MOVE = 4;
const API_CLOSE = 5;
const API_SELECT = 6;
const API_CONFIRM_SELECT = 7;
// display item types
const DTYPE_IMAGE = 0;
const DTYPE_MESSAGE = 1;
const DTYPE_SCREEN = 2;
const DTYPE_MAP = 3;
const DTYPE_ACTION = 4;
const DTYPE_COMPUTER_MAP = 5;
// game data indices
const DATA_SPLASH = 0;
const DATA_INTRO = 1;
const DATA_SUNRISE = 2;
const DATA_SUNSET = 3;
const DATA_C_MAIN = 4;
const DATA_C_DIAGNOSTICS = 5;
const DATA_C_SYNTH = 6;
const DATA_ISLAND = 7;
const DATA_INVESTIGATE = 8;
const DATA_BED = 9;
const DATA_NOT_TIRED = 10;
const DATA_SLEEP = 11;
const DATA_HUNGRY = 12;
const DATA_DEAD = 13;
const DATA_RANGER = 14;
const DATA_LOCKED_NOKEYS = 15;
const DATA_LOCKED_UNLOCK = 16;
const DATA_UNLOCK = 17;
const DATA_RUINS = 18;
const DATA_SEARCH_RUINS = 19;
const DATA_COMPUTER = 20;
const DATA_USE_COMPUTER = 21;
const DATA_FIXABLE_COMPUTER = 22;
const DATA_FIX_COMPUTER = 23;
const DATA_C_FIXED = 24;
const DATA_C_SYNTH_CHARGING = 25;
const DATA_CREATE_FOOD = 26;
const DATA_SYNTH = 27;
const DATA_DB = 28;
const DATA_COMMS = 29;
const DATA_SECURITY = 30;
const DATA_MAP = 31;
const DATA_C_DIAGNOSTICS_FIXED = 32;
const DATA_C_DB_INTRO = 33;
const DATA_C_DB_PORTALS = 34;
const DATA_C_DB_GHOSTS = 35;
const DATA_C_DB_ERRORS = 36;
const DATA_C_DB_SHUTDOWN_PORTALS = 37;
const DATA_C_DB_SECURITY = 38;
const DATA_C_DB_FIX_SATELLITE = 39;
const DATA_C_DB_RESCUE_TEAM = 40;
const DATA_C_DB_L = 8;
const DATA_RESTORE_BACKUPS = 41;
const DATA_DIAGNOSTICS = 42;
const DATA_MODCHIPS = 43;
const DATA_SATELLITE_CHIP = 44;
const DATA_DISTRESS_SIGNAL = 45;
// map data indices
const MAP_PLAYERX = 1;
const MAP_PLAYERY = 2;
const MAP_TILES = 3;
const MAP_TYPE = 4;
const MAP_STARTX = 5;
const MAP_STARTY = 6;
const COMPUTER_MAP_MAPDB = 4;
// map type indices
const MT_ISLAND = 0;
const MT_HUT = 1;
// display item indices
const DISPLAY_TYPE = 0;
const DISPLAY_NAME = 1;
const DISPLAY_MESSAGE = 1;
// actions
const ACTION_INDEX = 1;
// screen indices
const SCREEN_MESSAGE = 1;
const SCREEN_OPTIONS = 2;
const SCREEN_SELECTION = 3;
const SCREEN_COLOR = 4;
const OPTION_MESSAGE = 0;
const OPTION_DATA_INDEX = 1;
// point
const X = 0;
const Y = 1;
// edges
const TOP = 0;
const RIGHT = 1;
const BOTTOM = 2;
const LEFT = 3;
//monster
const MON_X = 0;
const MON_Y = 1;
const MON_FACING = 2;
const MON_HEALTH = 3;
// actions
const ACTION_SLEEP = 0;
const ACTION_UNLOCK = 1;
const ACTION_SEARCH = 2;
const ACTION_USE_COMPUTER = 3;
const ACTION_FIX_COMPUTER = 4;
const ACTION_CREATE_FOOD = 5;
const ACTION_SHOW_SYNTH = 6;
const ACTION_SHOW_DB = 7;
const ACTION_SHOW_COMMS = 8;
const ACTION_SHOW_SECURITY = 9;
const ACTION_SHOW_MAP = 10;
const ACTION_RESTORE_BACKUPS = 11;
const ACTION_DIAGNOSTICS = 12;
const ACTION_CREATE_MODCHIP = 13;
const ACTION_CREATE_SATELLITE_CHIP = 14;
const ACTION_DISTRESS_SIGNAL = 15;
// hut state
const HUT_UNLOCKED = 0;
const HUT_COMPUTER_FIXED = 1;
const HUT_SYNTH_CHARGING = 2;
// ruin item
const ITEM_KEY = 0;
const ITEM_CHIP = 1;
const ITEM_DISK = 2;
const ITEM_FOOD = 3;
// quest location
const QUEST_RANGER = 0;
const QUEST_HUT = 1;
const QUEST_RUINS = 2;
const QUEST_PORTAL = 3;
const QUEST_SATELLITE = 4;
const QUEST_BLANK = 5;
// normalized different between two numbers
const delta = (i, j) => Math.max(i, j) - Math.min(i, j);
// returns points that are to the left, right, top and bottom of passed in point
const immediateNeighbours = ([x, y]) => [
[x - 1, y],
[x + 1, y],
[x, y - 1],
[x, y + 1]
];
// returns every neighbouring tile, including diagonals
const allNeighbours = ([x, y]) => [
[x - 1, y],
[x + 1, y],
[x, y - 1],
[x, y + 1],
[x - 1, y - 1],
[x + 1, y - 1],
[x - 1, y + 1],
[x + 1, y + 1]
];
// gets all the immediate neighbours that match the passed in tileIndex
const getImmediateNeighbours = (tiles, p, tileIndex) => immediateNeighbours(p).filter(p => tiles[p[Y]][p[X]] === tileIndex);
/*
gets a random tile from the passed in collection that is between min and max
distance from the passed in point
*/
const withinDist = (tiles, [x, y], min, max) => {
const candidates = tiles.filter(([tx, ty]) => {
return delta(tx, x) >= min &&
delta(ty, y) >= min &&
delta(tx, x) <= max &&
delta(ty, y) <= max;
});
return pick(candidates);
};
// use trig to get distance between points
const dist = ([x1, y1], [x2, y2]) => Math.hypot(delta(x1, x2), delta(y1, y2));
// find the nearest point in a list of points
const nearest = (p1, points) => {
// set d to a big number
let d = mapSize * mapSize;
let p;
for (let i = 0; i < points.length; i++) {
const currentDist = dist(p1, points[i]);
// this point is the closest so far
if (currentDist < d) {
d = currentDist;
p = points[i];
}
}
return p;
};
// sort a list of points by their distance from p
const sortByDistance = (p, points) => points.slice().sort((p1, p2) => dist(p, p1) - dist(p, p2));
// filters a list of points to be unique
const unique = (points) => {
const result = [];
const cache = [];
for (let i = 0; i < points.length; i++) {
const [x, y] = points[i];
if (!cache[y * mapSize + x]) {
result.push(points[i]);
cache[y * mapSize + x] = 1;
}
}
return result;
};
// floodFill algorithm
const floodFill = ([x, y], canFlood) => {
const flooded = [];
const queue = [[x, y, 0]];
const cache = [];
const floodPoint = ([x, y, d]) => {
if (!inBounds([x, y]))
return;
if (!canFlood([x, y]))
return;
if (cache[y * mapSize + x])
return;
flooded.push([x, y, d]);
cache[y * mapSize + x] = 1;
queue.push(...immediateNeighbours([x, y]).map(([x, y]) => [x, y, d + 1]));
};
while (queue.length) {
floodPoint(queue.shift());
}
return flooded;
};
// find a tile in a list of tiles
const findTile = (tiles, [x, y]) => {
for (let i = 0; i < tiles.length; i++) {
if (tiles[i][X] === x && tiles[i][Y] === y)
return tiles[i];
}
};
// changed map code so it didn't need path finding - removed by uglify
const findPath = (flood, [x2, y2]) => {
const path = [];
const [x1, y1] = flood[0];
const end = findTile(flood, [x2, y2]);
if (!end)
return path;
const queue = [end];
const connectNext = ([x, y, min]) => {
path.unshift([x, y]);
if (x === x1 && y === y1)
return;
const neighbours = immediateNeighbours([x, y]);
let n;
neighbours.forEach(([x, y]) => {
const t = flood.find(([fx, fy]) => fx === x && fy === y);
if (t) {
const [, , d] = t;
if (d < min) {
min = d;
n = t;
}
}
});
if (n)
queue.push(n);
};
while (queue.length) {
connectNext(queue.shift());
}
return path;
};
// find the best neighbouring tile to move to if you want to go towards a point
const towards = ([x1, y1], [x2, y2]) => {
let dx = delta(x1, x2);
let dy = delta(y1, y2);
let x = x1;
let y = y1;
if (dx > dy) {
if (x2 > x1) {
x = x1 + 1;
}
if (x1 > x2) {
x = x1 - 1;
}
}
if (dy > dx) {
if (y2 > y1) {
y = y1 + 1;
}
if (y1 > y2) {
y = y1 - 1;
}
}
return [x, y];
};
// allows for carving out paths between points that aren't too perfect looking
const drunkenWalk = ([x1, y1], [x2, y2], allowed = inBounds, drunkenness = 0.66) => {
const steps = [];
const cache = [];
const step = ([x, y]) => {
if (!cache[y * mapSize + x]) {
steps.push([x, y]);
cache[y * mapSize + x] = 1;
}
if (x === x2 && y === y2)
return;
step(Math.random() < drunkenness ?
pick(immediateNeighbours([x, y]).filter(allowed)) || [x, y] :
towards([x, y], [x2, y2]));
};
step([x1, y1]);
return steps;
};
// randomly adds points to edges of existing land until land is a certain size
// gives a nice coastliney appearance while also ensuring there is a given
// amount of land tiles
const expandLand = (mapTiles, landTiles, tileCount = ~~((mapSize * mapSize) * 0.2)) => {
while (landTiles.length < tileCount) {
const [cx, cy] = pick(landTiles);
const neighbours = getImmediateNeighbours(mapTiles, [cx, cy], T_WATER).filter(inWaterBorder);
if (neighbours.length) {
const [nx, ny] = pick(neighbours);
if (!hasPoint(landTiles, [nx, ny])) {
landTiles.push([nx, ny]);
mapTiles[ny][nx] = T_LAND;
}
}
}
};
// same as above but instead of mutating maps returns list of points that
// should be expanded to, allows for further processing
const expanded = (points, tileCount = ~~((mapSize * mapSize) * 0.33)) => {
const expandedPoints = points.slice();
const cache = [];
for (let i = 0; i < expandedPoints.length; i++) {
const [x, y] = expandedPoints[i];
cache[y * mapSize + x] = 1;
}
while (expandedPoints.length < tileCount) {
const [cx, cy] = pick(expandedPoints);
const neighbours = immediateNeighbours([cx, cy]);
if (neighbours.length) {
const [nx, ny] = pick(neighbours);
if (!cache[ny * mapSize + nx]) {
expandedPoints.push([nx, ny]);
cache[ny * mapSize + nx] = 1;
}
}
}
return expandedPoints;
};
// some point on the map, randomly chosen
const randomPoint = () => [randInt(mapSize), randInt(mapSize)];
// some point along the nominated landBorder edge, randomly chosen
const randomLandEdge = (edge) => [
edge === LEFT ?
landBorder :
edge === RIGHT ?
mapSize - landBorder :
randInt(mapSize - landBorder * 2, landBorder),
edge === TOP ?
landBorder :
edge === BOTTOM ?
mapSize - landBorder :
randInt(mapSize - landBorder * 2, landBorder),
];
// some point within the land border, randomly chosen
const randomPointInLandBorder = () => [
randInt(mapSize - landBorder * 2, landBorder),
randInt(mapSize - landBorder * 2, landBorder)
];
// get the leftmost point amongst passed in tiles
const leftMost = (tiles) => {
let left = mapSize;
let p = [0, 0];
for (let i = 0; i < tiles.length; i++) {
const [x, y] = tiles[i];
if (x < left) {
left = x;
p = [x, y];
}
}
return p;
};
// does this list of points contain the given point?
const hasPoint = (tiles, [x, y]) => {
for (let i = 0; i < tiles.length; i++) {
if (tiles[i][X] === x && tiles[i][Y] === y)
return true;
}
return false;
};
// find the points of all tiles in a map that match tileIndex
const findTilePoints = (mapTiles, tileIndex) => {
const tiles = [];
for (let y = 0; y < mapSize; y++) {
for (let x = 0; x < mapSize; x++) {
if (mapTiles[y][x] === tileIndex)
tiles.push([x, y]);
}
}
return tiles;
};
// is within map boundaries
const inBounds = ([x, y]) => x >= 0 &&
x <= mapSize - 1 &&
y >= 0 &&
y <= mapSize - 1;
// is within the water border we want to leave on all sides
const inWaterBorder = ([x, y]) => x >= waterBorder &&
x <= mapSize - waterBorder &&
y >= waterBorder &&
y <= mapSize - waterBorder;
// is within the area we allow land to be created in
const inLandBorder = ([x, y]) => x >= landBorder &&
x <= mapSize - landBorder &&
y >= landBorder &&
y <= mapSize - landBorder;
// create a new map filled with water
const createMap = () => {
const rows = [];
for (let y = 0; y < mapSize; y++) {
const row = [];
for (let x = 0; x < mapSize; x++) {
row.push(T_WATER);
}
rows.push(row);
}
return rows;
};
// clone an existing map
const cloneMap = (tiles) => {
const rows = [];
for (let y = 0; y < mapSize; y++) {
const row = [];
for (let x = 0; x < mapSize; x++) {
row.push(tiles[y][x]);
}
rows.push(row);
}
return rows;
};
// draw some points to map according to return value of passed in function
const drawTilesToMap = (tiles, points, getTileIndex) => {
for (let i = 0; i < points.length; i++) {
const [px, py] = points[i];
tiles[py][px] = getTileIndex([px, py]);
}
};
// any internal water tiles on the map become a randomly selected biome
const addBiomes = (tiles) => {
let i = 0;
// make sure there's at least one of each biome for variety
const oneOfEachBiome = shuffle([0, 3, 6, 9]);
for (let y = 0; y < mapSize; y++) {
for (let x = 0; x < mapSize; x++) {
if (tiles[y][x] === T_WATER) {
const flood = floodFill([x, y], ([tx, ty]) => tiles[ty][tx] === T_WATER);
let biome = 0;
// don't use up the interesting biomes on small areas
if (flood.length > 5) {
// the first four times, add one of each for variety
if (i < 4) {
biome = oneOfEachBiome[i];
}
// if there is an area left to make biomes, choose biome randomly
else {
biome = randInt(10);
}
i++;
}
// 0 1 2
if (biome < 3) {
// meadow, no trees
drawTilesToMap(tiles, flood, () => randInt(T_GRASS_L + 1) + T_LAND);
}
// 3 4 5
else if (biome < 6) {
// 75% trees, 25% meadow
drawTilesToMap(tiles, flood, () => randInt(3) ?
randInt(T_TREE_L) + T_TREE :
randInt(T_GRASS_L + 1) + T_LAND);
}
// 6 7 8
else if (biome < 9) {
// 75% mountains, 25% meadow
drawTilesToMap(tiles, flood, () => randInt(4) ?
randInt(T_MOUNTAINS_L) + T_MOUNTAINS :
randInt(T_GRASS_L + 1) + T_LAND);
}
// 9
else {
// lake/inland sea
drawTilesToMap(tiles, flood, () => T_SEA);
}
}
}
}
};
/*
decorate coastlines with sand and any other land tiles with grass or trees
*/
const decorate = (tiles) => {
for (let y = 0; y < mapSize; y++) {
for (let x = 0; x < mapSize; x++) {
if (tiles[y][x] === T_LAND) {
const neighbours = getImmediateNeighbours(tiles, [x, y], T_SEA);
if (neighbours.length) {
tiles[y][x] = randInt(T_SAND_L) + T_SAND;
}
else {
// The 1 is for the bare land tile: T_GRASS_L + T_TREE_L + 1
tiles[y][x] = randInt(T_GRASS_L + T_TREE_L + 1) + T_LAND;
}
}
}
}
};
// make a hut map
const createHut = () => {
const tiles = createMap();
const black = floodFill([0, 0], ([tx, ty]) => tiles[ty][tx] === T_WATER);
drawTilesToMap(tiles, black, () => T_BLACK);
/*
we need an arbitrary point to draw the hut at, anything reasonably far from
the map edge will do so we use landBorder
*/
tiles[landBorder - 1][landBorder - 2] = T_COMPUTER;
tiles[landBorder - 1][landBorder - 1] = T_SYNTH;
tiles[landBorder - 1][landBorder] = T_BED;
tiles[landBorder][landBorder - 2] = T_LAND;
tiles[landBorder][landBorder - 1] = T_LAND;
tiles[landBorder][landBorder] = T_LAND;
tiles[landBorder + 1][landBorder - 2] = T_HUT_L;
tiles[landBorder + 1][landBorder - 1] = T_HUT_M;
tiles[landBorder + 1][landBorder] = T_HUT_R;
return [DTYPE_MAP, landBorder, landBorder, tiles, MT_HUT, landBorder, landBorder];
};
// draw a nice island with believable coastlines, different biomes to help the
// player build a mental map, quest locations that are guaranteed to be
// connected etc
const createIsland = (hutCache, ruinCache, portalCache) => {
const tiles = createMap();
// choose clearways (they will become quest locations, define path ends etc)
// start with one on each side so that we generally end up with a rough
// polygon shaped island
const clearwayCount = randInt(10, 40);
const clearways = [
randomLandEdge(TOP),
randomLandEdge(RIGHT),
randomLandEdge(BOTTOM),
randomLandEdge(LEFT)
];
/*
only select points for clearways that are at least 10 tiles apart -
does two things, prevents from picking already picked points, and makes sure
that the icons on the computer map never overlap
*/
while (clearways.length < clearwayCount) {
const clearway = randomPointInLandBorder();
const near = nearest(clearway, clearways);
if (dist(clearway, near) > 10) {
clearways.push(clearway);
}
}
// make paths between waypoints
const paths = [];
const pathSegs = clearways.slice();
let current = pathSegs.pop();
const start = current;
while (pathSegs.length) {
// pick the nearest and draw a rough line to it
const near = nearest(current, pathSegs);
const steps = drunkenWalk(current, near, inWaterBorder, 0.33);
paths.push(...steps);
current = pathSegs.pop();
}
// draw a rough line from the last waypoint to the first
// so we generally end up with a rough polygon
const steps = drunkenWalk(current, start, inWaterBorder, 0.33);
paths.push(...steps);
// now randomly join 10 of the waypoints, helps make a higher number of
// different biomes
for (let i = 0; i < 10; i++) {
const steps = drunkenWalk(pick(clearways), pick(clearways), inWaterBorder, 0.33);
paths.push(...steps);
}
// take all the quest points and mark all of their neighbours so that later
// we know not to put anything blocking next to a quest point
const clearings = [];
for (let i = 0; i < clearways.length; i++) {
clearings.push(...allNeighbours(clearways[i]));
}
// make a collection of all the areas we've marked so far
const land = unique([...clearways, ...clearings, ...paths]);
// now expand it out until we have the desired amount of land area
const expandedLand = expanded(land);
// start the player at the leftmost point
const [playerX, playerY] = leftMost(expandedLand);
// fill in the map with the land we have so far
drawTilesToMap(tiles, expandedLand, () => T_LAND);
// flood the outside of the map with sea (as opposed to water) - the areas
// that remain water will be used for placing biomes
const sea = floodFill([0, 0], ([tx, ty]) => tiles[ty][tx] === T_WATER);
drawTilesToMap(tiles, sea, () => T_SEA);
// sort all the quest points by distance so we can place useful things close
// and the satellite far away
const waypoints = sortByDistance([playerX, playerY], clearways);
// make sure there's a clear path from the player to the first waypoint
const playerSteps = drunkenWalk([playerX, playerY], waypoints[0], inWaterBorder, 0.33);
paths.push(...playerSteps);
// change all the internal water to different biomes
addBiomes(tiles);
// add sand along coastlines, decorate the remaining land with random grass, trees etc
decorate(tiles);
// now draw all the paths to the map to make sure you can always walk between
// quest points - make them as blank tiles to the player can visually pick
// out the paths, unless it's coastline in which case leave it as sand
drawTilesToMap(tiles, paths, ([wx, wy]) => {
if (tiles[wy][wx] >= T_SAND && tiles[wy][wx] < T_SAND + T_SAND_L) {
return tiles[wy][wx];
}
return T_LAND;
});
// decorate all the clearing around quest locations with various non-blocking
// grass tiles etc
drawTilesToMap(tiles, clearings, ([wx, wy]) => {
if (tiles[wy][wx] >= T_SAND && tiles[wy][wx] < T_SAND + T_SAND_L) {
return tiles[wy][wx];
}
return randInt(T_GRASS_L + 1) + T_LAND;
});
// now let's allocate quest locations to all the waypoints
// 50% ruins, 25% huts, 15% portals
const questSlots = waypoints.length - 4;
const numHuts = ~~(questSlots * 0.25);
const numPortals = ~~(questSlots * 0.15);
const numRuins = ~~(questSlots * 0.5);
const numBlank = questSlots - numHuts - numPortals - numRuins;
const randQuests = [];
for (let i = 0; i < numHuts; i++) {
randQuests.push(QUEST_HUT);
}
for (let i = 0; i < numPortals; i++) {
randQuests.push(QUEST_PORTAL);
}
for (let i = 0; i < numRuins; i++) {
randQuests.push(QUEST_RUINS);
}
for (let i = 0; i < numBlank; i++) {
randQuests.push(QUEST_BLANK);
}
// make sure that the closest ones are useful, the furthest is satellite,
// the rest are random
const quests = [QUEST_RANGER, QUEST_HUT, QUEST_RUINS, ...shuffle(randQuests), QUEST_SATELLITE];
// add them to the map
for (let i = 0; i < waypoints.length; i++) {
const [wx, wy] = waypoints[i];
const type = quests[i];
// dead ranger
if (type === QUEST_RANGER) {
tiles[wy][wx] = T_RANGER;
}
// hut
else if (type === QUEST_HUT) {
tiles[wy][wx] = T_HUT;
hutCache[wy * mapSize + wx] = [0, 0, 0];
hutCache[0].push([wx, wy]);
}
// ruins
else if (type === QUEST_RUINS) {
tiles[wy][wx] = randInt(T_RUINS_L) + T_RUINS;
ruinCache[wy * mapSize + wx] = [];
ruinCache[0].push([wx, wy]);
}
// portal
else if (type === QUEST_PORTAL) {
tiles[wy][wx] = T_PORTAL;
portalCache[0].push([wx, wy]);
}
// satellite
else if (type === QUEST_SATELLITE) {
tiles[wy][wx] = T_SATELLITE;
}
}
// done! return the island
return [DTYPE_MAP, playerX, playerY, tiles, MT_ISLAND, playerX, playerY];
};
// is this tileindex blocking?
const blocks = i => i < 2 || (i >= T_TREE && i < T_TREE + T_TREE_L) || i === T_HUT ||
i === T_BLACK || i === T_HUT_L || i === T_HUT_M || i === T_HUT_R ||
i === T_COMPUTER || i === T_SYNTH || i === T_BED ||
(i >= T_RUINS && i < T_RUINS + T_RUINS_L) ||
(i >= T_MOUNTAINS && i < T_MOUNTAINS + T_MOUNTAINS_L) ||
i === T_PORTAL || i === T_PORTAL_DAY || i === T_PORTAL_OFFLINE ||
i === T_SATELLITE;
// temporary for documenting map generation process:
const observableCreateIsland = (callback) => {
const tiles = createMap();
// choose clearways (they will become quest locations, define path ends etc)
// start with one on each side so that we generally end up with a rough
// polygon shaped island
const clearwayCount = randInt(10, 40);
const clearways = [
randomLandEdge(TOP),
randomLandEdge(RIGHT),
randomLandEdge(BOTTOM),
randomLandEdge(LEFT)
];
const clearwayEdgeTiles = createMap();
drawTilesToMap(clearwayEdgeTiles, clearways, () => T_LAND);
const clearwayEdgeSea = floodFill([0, 0], ([tx, ty]) => clearwayEdgeTiles[ty][tx] === T_WATER);
drawTilesToMap(clearwayEdgeTiles, clearwayEdgeSea, () => T_SEA);
callback(clearwayEdgeTiles, '01-clearway-edges');
/*
only select points for clearways that are at least 10 tiles apart -
does two things, prevents from picking already picked points, and makes sure
that the icons on the computer map never overlap
*/
while (clearways.length < clearwayCount) {
const clearway = randomPointInLandBorder();
const near = nearest(clearway, clearways);
if (dist(clearway, near) > 10) {
clearways.push(clearway);
}
}
const clearwaysTiles = createMap();
drawTilesToMap(clearwaysTiles, clearways, () => T_LAND);
const clearwaysSea = floodFill([0, 0], ([tx, ty]) => clearwaysTiles[ty][tx] === T_WATER);
drawTilesToMap(clearwaysTiles, clearwaysSea, () => T_SEA);
callback(clearwaysTiles, '02-clearways');
// make paths between waypoints
const paths = [];
const pathSegs = clearways.slice();
let current = pathSegs.pop();
const start = current;
let n = 3;
while (pathSegs.length) {
// pick the nearest and draw a rough line to it
const near = nearest(current, pathSegs);
const steps = drunkenWalk(current, near, inWaterBorder, 0.33);
paths.push(...steps);
current = pathSegs.pop();
const pathTiles = createMap();
drawTilesToMap(pathTiles, clearways, () => T_LAND);
drawTilesToMap(pathTiles, paths, () => T_LAND);
const pathSea = floodFill([0, 0], ([tx, ty]) => pathTiles[ty][tx] === T_WATER);
drawTilesToMap(pathTiles, pathSea, () => T_SEA);
callback(pathTiles, `${n < 10 ? '0' : ''}${n}-paths`);
n++;
}
// draw a rough line from the last waypoint to the first
// so we generally end up with a rough polygon
const steps = drunkenWalk(current, start, inWaterBorder, 0.33);
paths.push(...steps);
const pathTiles = createMap();
drawTilesToMap(pathTiles, clearways, () => T_LAND);
drawTilesToMap(pathTiles, paths, () => T_LAND);
const pathSea = floodFill([0, 0], ([tx, ty]) => pathTiles[ty][tx] === T_WATER);
drawTilesToMap(pathTiles, pathSea, () => T_SEA);
callback(pathTiles, `${n < 10 ? '0' : ''}${n}-paths`);
n++;
// now randomly join 10 of the waypoints, helps make a higher number of
// different biomes
for (let i = 0; i < 10; i++) {
const steps = drunkenWalk(pick(clearways), pick(clearways), inWaterBorder, 0.33);
paths.push(...steps);
const pathTiles = createMap();
drawTilesToMap(pathTiles, clearways, () => T_LAND);
drawTilesToMap(pathTiles, paths, () => T_LAND);
const pathSea = floodFill([0, 0], ([tx, ty]) => pathTiles[ty][tx] === T_WATER);
drawTilesToMap(pathTiles, pathSea, () => T_SEA);
callback(pathTiles, `${n < 10 ? '0' : ''}${n}-paths`);
n++;
}
// take all the quest points and mark all of their neighbours so that later
// we know not to put anything blocking next to a quest point
const clearings = [];
for (let i = 0; i < clearways.length; i++) {
clearings.push(...allNeighbours(clearways[i]));
}
// make a collection of all the areas we've marked so far
const land = unique([...clearways, ...clearings, ...paths]);
const landTiles = createMap();
drawTilesToMap(landTiles, land, () => T_LAND);
const landSea = floodFill([0, 0], ([tx, ty]) => landTiles[ty][tx] === T_WATER);
drawTilesToMap(landTiles, landSea, () => T_SEA);
callback(landTiles, `${n}-land`);
n++;
// now expand it out until we have the desired amount of land area
const expandedLand1 = expanded(land, ~~((mapSize * mapSize) * 0.11));
const expandedLandTiles1 = createMap();
drawTilesToMap(expandedLandTiles1, expandedLand1, () => T_LAND);
const expandedLandSea1 = floodFill([0, 0], ([tx, ty]) => expandedLandTiles1[ty][tx] === T_WATER);
drawTilesToMap(expandedLandTiles1, expandedLandSea1, () => T_SEA);
callback(expandedLandTiles1, `${n}-expanded-land`);
n++;
// now expand it out until we have the desired amount of land area
const expandedLand2 = expanded(land, ~~((mapSize * mapSize) * 0.22));
const expandedLandTiles2 = createMap();
drawTilesToMap(expandedLandTiles2, expandedLand2, () => T_LAND);
const expandedLandSea2 = floodFill([0, 0], ([tx, ty]) => expandedLandTiles2[ty][tx] === T_WATER);
drawTilesToMap(expandedLandTiles2, expandedLandSea2, () => T_SEA);
callback(expandedLandTiles2, `${n}-expanded-land`);
n++;
// now expand it out until we have the desired amount of land area
const expandedLand = expanded(land);
const expandedLandTiles = createMap();
drawTilesToMap(expandedLandTiles, expandedLand, () => T_LAND);
const expandedLandSea = floodFill([0, 0], ([tx, ty]) => expandedLandTiles[ty][tx] === T_WATER);
drawTilesToMap(expandedLandTiles, expandedLandSea, () => T_SEA);
callback(expandedLandTiles, `${n}-expanded-land`);
n++;
// start the player at the leftmost point
const [playerX, playerY] = leftMost(expandedLand);
// fill in the map with the land we have so far
drawTilesToMap(tiles, expandedLand, () => T_LAND);
// flood the outside of the map with sea (as opposed to water) - the areas
// that remain water will be used for placing biomes
const sea = floodFill([0, 0], ([tx, ty]) => tiles[ty][tx] === T_WATER);
drawTilesToMap(tiles, sea, () => T_SEA);
// sort all the quest points by distance so we can place useful things close
// and the satellite far away
const waypoints = sortByDistance([playerX, playerY], clearways);
// make sure there's a clear path from the player to the first waypoint
const playerSteps = drunkenWalk([playerX, playerY], waypoints[0], inWaterBorder, 0.33);
paths.push(...playerSteps);
// change all the internal water to different biomes
addBiomes(tiles);
const biomes = cloneMap(tiles);
callback(biomes, `${n}-biomes`);
n++;
// add sand along coastlines, decorate the remaining land with random grass, trees etc
decorate(tiles);
const decorated = cloneMap(tiles);
callback(decorated, `${n}-decorated`);
n++;
// now draw all the paths to the map to make sure you can always walk between
// quest points - make them as blank tiles to the player can visually pick
// out the paths, unless it's coastline in which case leave it as sand
drawTilesToMap(tiles, paths, ([wx, wy]) => {
if (tiles[wy][wx] >= T_SAND && tiles[wy][wx] < T_SAND + T_SAND_L) {
return tiles[wy][wx];
}
return T_LAND;
});
const footpaths = cloneMap(tiles);
callback(footpaths, `${n}-footpaths`);
n++;
// decorate all the clearing around quest locations with various non-blocking
// grass tiles etc
drawTilesToMap(tiles, clearings, ([wx, wy]) => {
if (tiles[wy][wx] >= T_SAND && tiles[wy][wx] < T_SAND + T_SAND_L) {
return tiles[wy][wx];
}
return randInt(T_GRASS_L + 1) + T_LAND;
});
// now let's allocate quest locations to all the waypoints
// 50% ruins, 25% huts, 15% portals
const questSlots = waypoints.length - 4;
const numHuts = ~~(questSlots * 0.25);
const numPortals = ~~(questSlots * 0.15);
const numRuins = ~~(questSlots * 0.5);
const numBlank = questSlots - numHuts - numPortals - numRuins;
const randQuests = [];
for (let i = 0; i < numHuts; i++) {
randQuests.push(QUEST_HUT);
}
for (let i = 0; i < numPortals; i++) {
randQuests.push(QUEST_PORTAL);
}
for (let i = 0; i < numRuins; i++) {
randQuests.push(QUEST_RUINS);
}
for (let i = 0; i < numBlank; i++) {
randQuests.push(QUEST_BLANK);
}
// make sure that the closest ones are useful, the furthest is satellite,
// the rest are random
const quests = [QUEST_RANGER, QUEST_HUT, QUEST_RUINS, ...shuffle(randQuests), QUEST_SATELLITE];
// add them to the map
for (let i = 0; i < waypoints.length; i++) {
const [wx, wy] = waypoints[i];
const type = quests[i];
// dead ranger
if (type === QUEST_RANGER) {
tiles[wy][wx] = T_RANGER;
}
// hut
else if (type === QUEST_HUT) {
tiles[wy][wx] = T_HUT;
}
// ruins
else if (type === QUEST_RUINS) {
tiles[wy][wx] = randInt(T_RUINS_L) + T_RUINS;
}
// portal
else if (type === QUEST_PORTAL) {
tiles[wy][wx] = T_PORTAL;
}
// satellite
else if (type === QUEST_SATELLITE) {