-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.ts
1119 lines (1042 loc) · 33.1 KB
/
game.ts
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
import { blocks, createIsland, createHut } from './map'
import {
DTYPE_IMAGE, DTYPE_MESSAGE, DTYPE_SCREEN, DATA_ISLAND, DATA_INTRO,
DATA_SPLASH, DISPLAY_TYPE, DATA_SUNRISE, DATA_SUNSET, DTYPE_MAP, MAP_PLAYERX,
MAP_PLAYERY, MAP_TILES, T_HUT, T_HUT_R, MAP_STARTY, MT_ISLAND, MAP_TYPE,
MT_HUT, MAP_STARTX, DATA_INVESTIGATE, MON_X, MON_Y, MON_FACING, X, Y,
MON_HEALTH, T_COMPUTER, SCREEN_SELECTION, SCREEN_OPTIONS, OPTION_DATA_INDEX,
SCREEN_COLOR, T_BED, DATA_NOT_TIRED, DATA_BED, DTYPE_ACTION, ACTION_INDEX,
DATA_HUNGRY, DATA_DEAD, T_RANGER, DATA_RANGER, HUT_UNLOCKED,
DATA_LOCKED_NOKEYS, DATA_LOCKED_UNLOCK, T_RUINS, T_RUINS_L, DATA_RUINS,
T_PORTAL, ACTION_USE_COMPUTER, HUT_COMPUTER_FIXED, DTYPE_COMPUTER_MAP,
DATA_C_DB_INTRO, DATA_RESTORE_BACKUPS, ITEM_KEY, ITEM_CHIP, ITEM_DISK,
ITEM_FOOD, DATA_DB, DATA_MAP, ACTION_SHOW_MAP, DATA_SYNTH, DATA_COMMS,
DATA_USE_COMPUTER, DATA_FIX_COMPUTER, DATA_DIAGNOSTICS, DISPLAY_MESSAGE,
DATA_CREATE_FOOD, DATA_MODCHIPS, T_PORTAL_OFFLINE, DATA_C_DB_L,
DATA_C_DB_SHUTDOWN_PORTALS, DATA_C_DB_FIX_SATELLITE, T_SATELLITE,
DATA_SATELLITE_CHIP, DATA_DISTRESS_SIGNAL, HUT_SYNTH_CHARGING
} from './indices'
import {
DisplayItem, GameColor, GameState, DisplayMap, GameAPI, Monster,
DisplayScreen, DisplayAction, HutState, Point, DisplayComputerMap,
DisplaySelection, RuinItems, HutCache, RuinCache, RuinItem, Seen, PortalCache,
BoolAsNumber
} from './types'
import { inBounds, hasPoint, towards, allNeighbours, dist } from './geometry'
import {
initialMonsterCount, mapSize, sunrise, sunset, gridSize, gridTiles, centerTile
} from './settings'
import { randInt, shuffle, pick } from './utils'
import { gameData } from './data'
export const Game = () => {
// state, things the UI might need to know to draw
let hutCache: HutCache
let ruinCache: RuinCache
let playerFacing: 0 | 1
let playerFood: number
let playerHealth: number
let playerMaxHealth: number
let playerKeys: number
let playerChips: number
let playerDisks: number
let hours: number
let minutes: number
let color: GameColor
let displayStack: DisplayItem[]
let monsters: Monster[]
let seen: Seen
let satelliteFixed: BoolAsNumber
// internal state, no need to expose to UI
let seenRangerMessage: number
let currentHut: HutState
let currentRuins: RuinItems
let notesDb: number[]
let mapDb: number[]
let modChips: number
let satelliteChips: number
let portalCache: PortalCache
// create a clean slate, eg on first run or when restarting after died/won
const reset = () => {
// information about huts, ruins, portals etc.
hutCache = [[]]
ruinCache = [[]]
portalCache = [[]]
// player state
playerFacing = 0
playerFood = 5
playerHealth = 20
playerMaxHealth = 20
playerKeys = 0
playerChips = 0
playerDisks = 0
// start five minutes before dark to teach the player about sunrise/sunset
hours = 17
minutes = 55
// generate main map
gameData[ DATA_ISLAND ] = createIsland( hutCache, ruinCache, portalCache )
/*
setup the display stack with the splash screen, then the intro text,
then the main map
*/
displayStack = [
gameData[ DATA_ISLAND ],
gameData[ DATA_INTRO ],
gameData[ DATA_SPLASH ]
]
color = ''
monsters = []
seenRangerMessage = 0
modChips = -1
satelliteChips = -1
notesDb = [ DATA_C_DB_INTRO ]
mapDb = []
seen = []
satelliteFixed = 0
/*
once the player unlocks map, the tile they started on will already be
restored
*/
const mapItem = <DisplayMap>gameData[ DATA_ISLAND ]
const playerX = mapItem[ MAP_PLAYERX ]
const playerY = mapItem[ MAP_PLAYERY ]
const gridX = ~~( playerX / gridSize )
const gridY = ~~( playerY / gridSize )
mapDb[ gridY * gridTiles + gridX ] = 1
createMonsters()
distributeItems()
updateSeen( mapItem )
}
const currentColor = (): GameColor => {
// images and messages use the green scheme
if ( displayStack[ displayStack.length - 1 ][ DISPLAY_TYPE ] === DTYPE_IMAGE ) return 'g'
if ( displayStack[ displayStack.length - 1 ][ DISPLAY_TYPE ] === DTYPE_MESSAGE ) return 'g'
// computer stuff uses amber
if ( displayStack[ displayStack.length - 1 ][ DISPLAY_TYPE ] === DTYPE_COMPUTER_MAP ) return 'a'
// screens can be either game screens (green) or computer screens (amber)
if ( displayStack[ displayStack.length - 1 ][ DISPLAY_TYPE ] === DTYPE_SCREEN )
return (<DisplayScreen>displayStack[ displayStack.length - 1 ])[ SCREEN_COLOR ]
/*
must be in game, probably empty string - black and white in day, dark
blues at night
*/
return color
}
// current game state, used by the UI
const state = (): GameState => [
playerFacing, playerFood, playerHealth, playerMaxHealth, hours, minutes,
currentColor(),
displayStack[ displayStack.length - 1 ],
monsters,
playerKeys, playerChips, playerDisks,
seen,
hutCache, ruinCache, portalCache, satelliteFixed, modChips, satelliteChips
]
// close the current screen
const close = () => {
displayStack.pop()
// if nothing left in stack, player won or died - restart
if( !displayStack.length ) reset()
}
/*
create a new monster at x,y unless that tile is blocked or already has a
monster
*/
const createMonster = ( [ x, y ]: Point ) => {
const facing = randInt( 2 )
const health = randInt( 2 ) + 1
const mapItem = <DisplayMap>gameData[ DATA_ISLAND ]
const mapTile = mapItem[ MAP_TILES ][ y ][ x ]
const playerX = mapItem[ MAP_PLAYERX ]
const playerY = mapItem[ MAP_PLAYERY ]
if (
!blocks( mapTile ) && !hasPoint( <any>monsters, [ x, y ] ) &&
!( playerX === x && playerY === y )
) monsters.push( [ x, y, facing, health ] )
}
// create the initial monsters
const createMonsters = () => {
while ( monsters.length < initialMonsterCount ) {
const x = randInt( mapSize )
const y = randInt( mapSize )
createMonster([ x, y ])
}
}
// monster is only "here" if it's still alive
const isMonsterHere = ( [ x, y ]: Point ) => {
for ( let i = 0; i < monsters.length; i++ ) {
const monster = monsters[ i ]
const mx = monster[ MON_X ]
const my = monster[ MON_Y ]
if ( monster[ MON_HEALTH ] > 0 && x === mx && y === my ) return 1
}
}
// move all the monsters
const updateMonsters = () => {
for ( let i = 0; i < monsters.length; i++ ) {
const monster = monsters[ i ]
const x = monster[ MON_X ]
const y = monster[ MON_Y ]
const currentMapItem = <DisplayMap>displayStack[ displayStack.length - 1 ]
const mapItem = <DisplayMap>gameData[ DATA_ISLAND ]
const playerX = mapItem[ MAP_PLAYERX ]
const playerY = mapItem[ MAP_PLAYERY ]
const next = [ x, y ]
// at night, 66% chance the monster moves towards player.
if ( ( hours >= sunset || hours < sunrise ) && Math.random() < 0.66 ) {
const toPlayer = towards( [ x, y ], [ playerX, playerY ] )
next[ X ] = toPlayer[ X ]
next[ Y ] = toPlayer[ Y ]
}
// day time or 33% chance at night that monster just moves randomly
else {
// 50% chance of moving either horizontally or vertically
if ( randInt( 2 ) ) {
// either move left, stay here, or move right
next[ X ] = x + ( randInt( 3 ) - 1 )
} else {
// either move up, stay here, or move down
next[ Y ] = y + ( randInt( 3 ) - 1 )
}
}
// get the tile we're trying to move to
const mapTile = mapItem[ MAP_TILES ][ next[ Y ] ][ next[ X ] ]
// only move if not blocked by map obstacle, another monster or player
if (
!blocks( mapTile ) &&
!isMonsterHere( [ next[ X ], next[ Y ] ] ) &&
!( playerX === next[ X ] && playerY === next[ Y ] )
) {
monster[ MON_X ] = next[ X ]
monster[ MON_Y ] = next[ Y ]
// update monster facing if moved left or right
if ( next[ X ] < x ) {
monster[ MON_FACING ] = 1
}
if ( next[ X ] > x ) {
monster[ MON_FACING ] = 0
}
}
/*
if nighttime and on main map and bumped player and player not already
dead and this monster isn't dead, 50% chance of hurting player
*/
if (
currentMapItem[ DISPLAY_TYPE ] === DTYPE_MAP &&
currentMapItem[ MAP_TYPE ] === MT_ISLAND &&
( hours >= sunset || hours < sunrise ) &&
playerX === next[ X ] && playerY === next[ Y ] &&
randInt( 2 ) && playerHealth > 0 && monster[ MON_HEALTH ] > 0
) {
playerHealth--
}
}
}
// distribute items amongst ruins
const distributeItems = () => {
const numHuts = hutCache[ 0 ].length
// 1 for each hut and some extra
const numKeyCards = numHuts + 2
// 6 for each hut and some extra
const numChips = ~~( numHuts * 7 )
/*
We need:
7 for notes
15 for map
2 for synth
But make approx double that so that the game doesn't go too slowly
*/
const numBackups = 50
// this is just a guess
const numFood = ~~( numHuts * 2 )
let items: RuinItem[] = []
for( let i = 0; i < numKeyCards; i++ ){
items.push( ITEM_KEY )
}
for( let i = 0; i < numChips; i++ ){
items.push( ITEM_CHIP )
}
for( let i = 0; i < numBackups; i++ ){
items.push( ITEM_DISK )
}
for( let i = 0; i < numFood; i++ ){
items.push( ITEM_FOOD )
}
/*
ok, now shuffle items so when we distribute it's random
*/
items = shuffle( items )
while( items.length ){
/*
take an item and randomly pick a ruin to stash it in
*/
const item = items.pop()!
const [ rx, ry ] = pick( ruinCache[ 0 ] )
const ruinItems = <RuinItems>ruinCache[ ry * mapSize + rx ]
ruinItems.push( item )
}
}
/*
increase the time by 1 minute every time the player does certain actions
also keeps track of sunset/sunrise and changes color scheme accordingly
if sleeping, don't eat food
if not sleeping, try to eat food, if no food, lose health
*/
const incTime = ( sleeping: BoolAsNumber = 0 ) => {
if( playerHealth < 1 ){
displayStack = [ gameData[ DATA_DEAD ] ]
return
}
minutes++
// new hour
if ( minutes === 60 ) {
minutes = 0
hours++
if( sleeping ){
// heal one HP every hour
if ( playerHealth < playerMaxHealth ) playerHealth++
if ( hours === sunrise ) {
color = ''
}
} else {
if ( hours === sunrise ) {
color = ''
displayStack.push( gameData[ DATA_SUNRISE ] )
}
if ( hours === sunset ) {
color = 'i'
displayStack.push( gameData[ DATA_SUNSET ] )
}
if ( playerFood > 0 ) {
// eat food and heal 1 HP if needed
playerFood--
if ( playerHealth < playerMaxHealth ) playerHealth++
} else {
// starve if no food and lose 1 HP
playerHealth--
displayStack.push( gameData[ DATA_HUNGRY ] )
}
}
}
// once a day at midnight, do this
if ( hours === 24 ) {
// put all the synths back to full charge
const huts = hutCache[ 0 ]
for( let i = 0; i < huts.length; i++ ){
const [ hx, hy ] = huts[ i ]
hutCache[ hy * mapSize + hx ][ HUT_SYNTH_CHARGING ] = 0
}
// tick over the hours counter
hours = 0
// have remaining active portals randomly spawn more monsters
const mapItem = <DisplayMap>gameData[ DATA_ISLAND ]
for( let y = 0; y < mapSize; y++ ){
for( let x = 0; x < mapSize; x++ ){
const mapTile = mapItem[ MAP_TILES ][ y ][ x ]
if( mapTile === T_PORTAL ){
// every tile neighbouring portal has chance to spawn
const neighbours = allNeighbours([ x, y ])
for( let i = 0; i < neighbours.length; i++ ){
// one in three chance it spawns
if( !randInt( 3 ) ){
createMonster( neighbours[ i ] )
}
}
}
}
}
}
// move all the monsters
updateMonsters()
}
// format the time string
const timeStr = () => `${
hours < 10 ? '0' : ''
}${
hours
}:${
minutes < 10 ? '0' : ''
}${
minutes
}`
// update the fog of war - helps player keep track of where they've been
const updateSeen = ( map: DisplayMap ) => {
if( map[ MAP_TYPE ] === MT_ISLAND ){
const px = map[ MAP_PLAYERX ]
const py = map[ MAP_PLAYERY ]
for( let y = -centerTile; y < centerTile; y++ ){
for( let x = -centerTile; x < centerTile; x++ ){
const cx = px + x
const cy = py + y
// if we had a bigger viewport this would be a nice circle
if( dist( [ px, py ], [ cx, cy ] ) < centerTile ){
seen[ cy * mapSize + cx ] = 1
}
}
}
}
}
// the UI asked the game code to move the player
const move = ( x: number, y: number ) => {
const map = <DisplayMap>displayStack[ displayStack.length - 1 ]
// must be navigating around a menu or something
if( map[ 0 ] !== DTYPE_MAP ) return
// takes one minute even if blocked
incTime()
// change player facing even if blocked
if( x === -1 ){
playerFacing = 1
}
if( x === 1 ){
playerFacing = 0
}
// update current player position
x = map[ MAP_PLAYERX ] + x
y = map[ MAP_PLAYERY ] + y
// find if a monster is at the new position and if it is alive
let monsterHere
if (
( hours >= sunset || hours < sunrise ) &&
map[ MAP_TYPE ] === MT_ISLAND
){
for ( let i = 0; i < monsters.length; i++ ) {
if (
monsters[ i ][ MON_X ] === x && monsters[ i ][ MON_Y ] === y &&
monsters[ i ][ MON_HEALTH ] > 0
){
monsterHere = monsters[ i ]
}
}
}
// move the player if no map obstacle or monster
if (
playerHealth > 0 && inBounds( [ x, y ] ) &&
!blocks( map[ MAP_TILES ][ y ][ x ] ) && !monsterHere
){
map[ MAP_PLAYERX ] = x
map[ MAP_PLAYERY ] = y
}
// update the fog of war
updateSeen( map )
// check for bumping into things on island
if( map[ MAP_TYPE ] === MT_ISLAND ){
// huts
if ( map[ MAP_TILES ][ y ][ x ] === T_HUT ) {
currentHut = <HutState>hutCache[ y * mapSize + x ]
// if unlocked you just go in
if( currentHut[ HUT_UNLOCKED ] ){
displayStack.push( createHut() )
} else {
// if they have keys ask if want to unlock
if( playerKeys ){
displayStack.push( gameData[ DATA_LOCKED_UNLOCK ] )
}
// otherwise tell them it's locked
else {
displayStack.push( gameData[ DATA_LOCKED_NOKEYS ] )
}
}
}
// if they bump the boat tell them they can't leave, there's a job to do
if( y === map[ MAP_STARTY ] ){
if( x === map[ MAP_STARTX ] - 1 ){
displayStack.push( gameData[ DATA_INVESTIGATE ] )
}
}
// if bumped a monster, 50% chance of hurting it
if ( monsterHere && randInt( 2 ) ){
monsterHere[ MON_HEALTH ]--
}
// show the ranger message the first time the player bumps the skeleton
if( map[ MAP_TILES ][ y ][ x ] === T_RANGER && !seenRangerMessage ){
seenRangerMessage = 1
displayStack.push( gameData[ DATA_RANGER ] )
playerKeys++
playerFood += 5
playerChips += 3
playerDisks += 2
}
// ruins
if( map[ MAP_TILES ][ y ][ x ] >= T_RUINS && map[ MAP_TILES ][ y ][ x ] < T_RUINS + T_RUINS_L ){
currentRuins = <RuinItems>ruinCache[ y * mapSize + x ]
// offer them chance to search if this ruins has items left
if( currentRuins.length ){
displayStack.push( gameData[ DATA_RUINS ] )
}
// otherwise tell them it's empty
else {
displayStack.push( [ DTYPE_MESSAGE, [ 'Nothing here' ] ] )
}
}
// portal
if( map[ MAP_TILES ][ y ][ x ] === T_PORTAL ){
// if they have mod chips, let them disable it
if( modChips > 0 ){
displayStack.push( [ DTYPE_MESSAGE, [ 'Portal disabled!' ] ] )
map[ MAP_TILES ][ y ][ x ] = T_PORTAL_OFFLINE
portalCache[ y * mapSize + x ] = 1
modChips--
}
// they can make mod chips, but don't have any yet
else if ( modChips > -1 ) {
displayStack.push( [ DTYPE_MESSAGE, [ 'Need mod chips' ] ] )
}
// they haven't unlocked mod chips yet
else {
displayStack.push( [ DTYPE_MESSAGE, [ 'A strange portal' ] ] )
}
}
// satellite
if( map[ MAP_TILES ][ y ][ x ] === T_SATELLITE ){
// if they haven't fixed it yet and have chips, fix it
if ( satelliteChips > 0 && !satelliteFixed ) {
displayStack.push( [ DTYPE_MESSAGE, [ 'Fixed satellite!' ] ] )
satelliteFixed = 1
satelliteChips--
}
// they can make chips but haven't yet
else if ( satelliteChips > -1 && !satelliteFixed ) {
displayStack.push( [ DTYPE_MESSAGE, [ 'Need satellite chip' ] ] )
}
// they haven't unlocked satellite chips yet
else if ( !satelliteFixed ) {
displayStack.push( [ DTYPE_MESSAGE, [ 'Satellite is offline' ] ] )
}
}
}
// bumps inside a hut
if ( map[ MAP_TYPE ] === MT_HUT ) {
// leave the hut if they bump the door
if ( map[ MAP_TILES ][ y ][ x ] === T_HUT_R ) {
displayStack.pop()
}
// use the computer
if ( map[ MAP_TILES ][ y ][ x ] === T_COMPUTER ) {
const options: DisplaySelection[] = [
[ 'Use', DATA_USE_COMPUTER ]
]
const computer: DisplayScreen = [
DTYPE_SCREEN,
[
`A computer`,
'',
],
options,
0,
'g'
]
if( !currentHut[ HUT_COMPUTER_FIXED ] && playerChips > 5 ){
options.push( [ 'Fix Chips', DATA_FIX_COMPUTER ] )
}
if( currentHut[ HUT_COMPUTER_FIXED ] && playerDisks > 0 ){
options.push( [ 'Restore Backups', DATA_RESTORE_BACKUPS ] )
}
// if they can fix or restore, ask them what they want to do
if( options.length > 1 ){
displayStack.push( computer )
}
// otherwise, just use the computer
else {
actions[ ACTION_USE_COMPUTER ]()
}
}
/*
you can sleep anytime between one hour before sunset and 1 minute
before sunrise - 1 hour before, because if you hurried back to hut and
misjudged slightly and got there a bit early, it's annoying killing time
until you can sleep
*/
if( map[ MAP_TILES ][ y ][ x ] === T_BED ){
if( hours >= ( sunset - 1 ) || hours < sunrise ) {
displayStack.push( gameData[ DATA_BED ] )
} else {
displayStack.push( gameData[ DATA_NOT_TIRED ] )
}
}
}
}
// the UI is asking to the change the selection on a screen
const select = ( i: number ) => {
if (
displayStack[ displayStack.length - 1 ][ DISPLAY_TYPE ] === DTYPE_SCREEN
){
displayStack[ displayStack.length - 1 ][ SCREEN_SELECTION ] = i
}
}
// the UI is asking us to execute whatever selection was chosen
const confirmSelection = () => {
if (
displayStack[ displayStack.length - 1 ][ DISPLAY_TYPE ] === DTYPE_SCREEN
) {
const screen = <DisplayScreen>displayStack[ displayStack.length - 1 ]
// some screens have no options, just close the screen and return
if( !screen[ SCREEN_OPTIONS ].length ){
displayStack.pop()
return
}
const selected = screen[ SCREEN_SELECTION ]
const dataIndex = screen[ SCREEN_OPTIONS ][ selected ][ OPTION_DATA_INDEX ]
// magic number used by options that just want to close this screen
if( dataIndex === -1 ){
close()
}
// if it's an action, close this screen and execute it
else if( gameData[ dataIndex ][ DISPLAY_TYPE ] === DTYPE_ACTION ){
displayStack.pop()
actions[ ( <DisplayAction>gameData[ dataIndex ] )[ ACTION_INDEX ] ]()
}
// otherwise it must be another screen
else {
displayStack.push( gameData[ dataIndex ] )
}
}
}
/*
note actions don't generally check if they're valid or not, that's done by
the calling code
*/
const actions: ( () => void )[] = [
// ACTION_SLEEP
() => {
while ( hours !== sunrise ) {
incTime( 1 )
}
},
// ACTION_UNLOCK
() => {
currentHut[ HUT_UNLOCKED ] = 1
playerKeys--
},
// ACTION_SEARCH
() => {
const mapItem = <DisplayMap>gameData[ DATA_ISLAND ]
const playerX = mapItem[ MAP_PLAYERX ]
const playerY = mapItem[ MAP_PLAYERY ]
const neighbours = allNeighbours([ playerX, playerY ])
let attacked: BoolAsNumber = 0
/*
searching takes one hour - if a monster is adjacent at any point, we
want to stop searching so check every minute
*/
for( let i = 0; i < 60; i++ ){
incTime()
for( let n = 0; n < neighbours.length; n++ ){
const [ nx, ny ] = neighbours[ n ]
if( ( hours >= sunset || hours < sunrise ) && isMonsterHere([ nx, ny ]) ){
attacked = 1
i = 60
}
}
}
if( attacked ){
displayStack.push( [ DTYPE_MESSAGE, [ 'Under attack!' ] ] )
} else {
// take an item off the ruin stack and give it to the player
const item = currentRuins.pop()
if ( item === ITEM_FOOD ) {
const food = randInt( 3 ) + 2
displayStack.push( [ DTYPE_MESSAGE, [ `Found ${ food } food` ] ] )
playerFood += food
}
else if ( item === ITEM_KEY ) {
displayStack.push( [ DTYPE_MESSAGE, [ 'Found keycard' ] ] )
playerKeys++
}
else if ( item === ITEM_CHIP ) {
displayStack.push( [ DTYPE_MESSAGE, [ 'Found chip' ] ] )
playerChips++
}
else if ( item === ITEM_DISK ) {
displayStack.push( [ DTYPE_MESSAGE, [ 'Found backup' ] ] )
playerDisks++
}
}
},
// ACTION_USE_COMPUTER
() => {
// if they've used chips to fix the computer
if( currentHut[ HUT_COMPUTER_FIXED ] ){
displayStack.push([
DTYPE_SCREEN,
[
'RSOS v3.27',
'--------------------',
''
],
[
[ 'SYNTHESIZE', DATA_SYNTH ],
[ 'DIAGNOSTICS', DATA_DIAGNOSTICS ],
[ 'NOTES', DATA_DB ],
[ 'COMMS', DATA_COMMS ],
[ 'MAP', DATA_MAP ]
],
0,
'a'
])
}
// otherwise, just basic functions
else {
displayStack.push([
DTYPE_SCREEN,
[
'RSOS v3.27',
'--------------------',
'NETWORK OFFLINE',
'',
'EMERGENCY MODE',
''
],
[
[ 'SYNTHESIZE', DATA_SYNTH ],
[ 'DIAGNOSTICS', DATA_DIAGNOSTICS ],
],
0,
'a'
])
}
},
// ACTION_FIX_COMPUTER
() => {
displayStack.push( [ DTYPE_MESSAGE, [ 'Fixed 6 chips' ] ] )
playerChips -= 6
currentHut[ HUT_COMPUTER_FIXED ] = 1
},
// ACTION_CREATE_FOOD
() => {
const food = randInt( 3 ) + 6
currentHut[ HUT_SYNTH_CHARGING ] = 1
playerFood += food
displayStack.push( [ DTYPE_MESSAGE, [ `Synthesized ${ food } food` ] ] )
},
// ACTION_SHOW_SYNTH
() => {
const options: DisplaySelection[] = []
const screen: DisplayScreen = [
DTYPE_SCREEN,
[
'RSOS v3.27',
'--------------------',
'SYNTHESIZER',
],
options,
0,
'a'
]
// each hut's synth can only be used once a day
if( currentHut[ HUT_SYNTH_CHARGING ] ){
screen[ DISPLAY_MESSAGE ].push( ' CHARGING...' )
}
// if it hasn't been used
else {
// you can always make food if it has power
options.push( [ 'RATIONS', DATA_CREATE_FOOD ] )
// if they've unlocked mod chips
if( modChips > -1 ){
options.push( [ 'MOD CHIPS', DATA_MODCHIPS ] )
}
// if they've unlocked satellite chips
if( satelliteChips > -1 ){
options.push( [ 'SATELLITE CHIP', DATA_SATELLITE_CHIP ] )
}
}
displayStack.push( screen )
},
// ACTION_SHOW_DB
() => {
// options for all the notes they've unlocked so far
const dbOptions: DisplaySelection[] = notesDb.map( i => {
return <DisplaySelection>[ `ENTRY ${ i }`, i ]
})
const dbScreen: DisplayScreen = [
DTYPE_SCREEN,
[
'RSOS v3.27',
'--------------------',
'NOTES',
],
dbOptions,
0,
'a'
]
displayStack.push( dbScreen )
},
// ACTION_SHOW_COMMS
() => {
const options: DisplaySelection[] = []
const screen: DisplayScreen = [
DTYPE_SCREEN,
[
'RSOS v3.27',
'--------------------',
'COMMS',
],
options,
0,
'a'
]
// they can only send distress signal after fixing satellite
if( satelliteFixed ){
options.push( [ 'DISTRESS SIGNAL', DATA_DISTRESS_SIGNAL ] )
} else {
screen[ DISPLAY_MESSAGE ].push( ' SATELLITE OFFLINE' )
}
displayStack.push( screen )
},
// ACTION_SHOW_SECURITY
() => {
/*
this was never used - can be removed but all the action indices would
need to be updated
*/
},
// ACTION_SHOW_MAP
() => {
const mapItem = <DisplayMap>gameData[ DATA_ISLAND ]
const playerX = mapItem[ MAP_PLAYERX ]
const playerY = mapItem[ MAP_PLAYERY ]
const mapTiles = mapItem[ MAP_TILES ]
// all the information needed by the UI to display the computer map
const computerMap: DisplayComputerMap = [ DTYPE_COMPUTER_MAP, playerX, playerY, mapTiles, mapDb ]
displayStack.push( computerMap )
},
// ACTION_RESTORE_BACKUPS
() => {
playerDisks--
// which note is next (if there are any left to show)
const nextNoteDb = notesDb.length + DATA_C_DB_INTRO
const randItem = randInt( 8 )
/*
mod chip for closing down portals - give to the player on first backup
restore after they've seen the note mentioning it
*/
if ( ( notesDb.length + DATA_C_DB_INTRO ) > DATA_C_DB_SHUTDOWN_PORTALS && modChips === -1 ){
displayStack.push( [ DTYPE_MESSAGE, [
'Recovered 1 synth',
'database entry'
] ] )
// lets the game know they can make them, but don't have any yet
modChips = 0
}
/*
satellite chip for fixing satellite - give after first note as above
*/
else if ( ( notesDb.length + DATA_C_DB_INTRO ) > DATA_C_DB_FIX_SATELLITE && satelliteChips === -1 ){
displayStack.push( [ DTYPE_MESSAGE, [
'Recovered 1 synth',
'database entry'
] ] )
// as above
satelliteChips = 0
}
// note if they haven't gotten any yet and randItem was one of 0 1 2
else if( randItem < 3 && nextNoteDb < ( DATA_C_DB_INTRO + DATA_C_DB_L ) ){
notesDb.push( nextNoteDb )
displayStack.push( gameData[ notesDb.length + DATA_C_DB_INTRO - 1 ] )
displayStack.push( [ DTYPE_MESSAGE, [
'Recovered 1 note',
'database entry'
] ] )
}
// otherwise give them a map tile if randItem was 3 4 5 6 7
else {
// check which map tiles are left
let availableMaps: Point[] = []
for( let y = 0; y < gridTiles; y++ ){
for( let x = 0; x < gridTiles; x++ ){
if( !mapDb[ y * gridTiles + x ] ){
availableMaps.push( [ x, y ] )
}
}
}
// if there are any, pick one randomly and give it to them
if( availableMaps.length ){
const [ gridX, gridY ] = pick( availableMaps )
mapDb[ gridY * gridTiles + gridX ] = 1
actions[ ACTION_SHOW_MAP ]()
displayStack.push( [ DTYPE_MESSAGE, [
'Recovered 1 map',
'database entry'
] ] )
}
// none left, try to give them a note
else {
if ( nextNoteDb < ( DATA_C_DB_INTRO + DATA_C_DB_L ) ){
notesDb.push( nextNoteDb )
displayStack.push( gameData[ notesDb.length + DATA_C_DB_INTRO - 1 ] )
displayStack.push( [ DTYPE_MESSAGE, [
'Recovered 1 note',
'database entry'
] ] )
}
// no notes left either
else {
displayStack.push( [ DTYPE_MESSAGE, [
'Backup already',
'restored'
] ] )
}
}
}
},
// ACTION_DIAGNOSTICS
() => {
// if they've fixed the computer, check what else is working properly
if( currentHut[ HUT_COMPUTER_FIXED ] ){
let availableMaps: Point[] = []
for( let y = 0; y < gridTiles; y++ ){
for( let x = 0; x < gridTiles; x++ ){
if( !mapDb[ y * gridTiles + x ] ){
availableMaps.push( [ x, y ] )
}
}
}
const screen: string[] = [
'RSOS v3.27',
'--------------------',
'DIAGNOSTICS',
'',
'NETWORK ONLINE',
'SYNTHESIZE ONLINE',
]
/*
if they haven't got satellite chips yet, there are synth backups left
to restore
*/
if( satelliteChips === -1 ){
screen.push( ' RESTORE BACKUPS' )
}
screen.push( 'NOTES ONLINE' )
// still some notes to restore
if( notesDb.length < 8 ){
screen.push( ' RESTORE BACKUPS' )
}
// satellite status
if( satelliteFixed ){
screen.push( 'COMMS ONLINE' )
screen.push( ' DISTRESS MODE ONLY' )
} else {
screen.push( 'COMMS OFFLINE' )
}
screen.push( 'MAP ONLINE' )
// still some map backups left to do
if( availableMaps.length ){
screen.push( ' RESTORE BACKUPS' )
}
displayStack.push( [
DTYPE_SCREEN,
screen,
[],