forked from coding-horror/basic-computer-games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
superstartrek.mjs
1583 lines (1411 loc) · 46.2 KB
/
superstartrek.mjs
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
/**
* SUPER STARTREK - MAY 16,1978 - REQUIRES 24K MEMORY
*
* **** STAR TREK **** ****
* SIMULATION OF A MISSION OF THE STARSHIP ENTERPRISE,
* AS SEEN ON THE STAR TREK TV SHOW.
* ORIGIONAL PROGRAM BY MIKE MAYFIELD, MODIFIED VERSION
* PUBLISHED IN DEC'S "101 BASIC GAMES", BY DAVE AHL.
* MODIFICATIONS TO THE LATTER (PLUS DEBUGGING) BY BOB
* LEEDOM - APRIL & DECEMBER 1974,
* WITH A LITTLE HELP FROM HIS FRIENDS . . .
* COMMENTS, EPITHETS, AND SUGGESTIONS SOLICITED --
* SEND TO: R. C. LEEDOM
* WESTINGHOUSE DEFENSE & ELECTRONICS SYSTEMS CNTR.
* BOX 746, M.S. 338
* BALTIMORE, MD 21203
*
* CONVERTED TO MICROSOFT 8 K BASIC 3/16/78 BY JOHN GORDERS
* LINE NUMBERS FROM VERSION STREK7 OF 1/12/75 PRESERVED AS
* MUCH AS POSSIBLE WHILE USING MULTIPLE STATEMENTS PER LINE
* SOME LINES ARE LONGER THAN 72 CHARACTERS; THIS WAS DONE
* BY USING "?" INSTEAD OF "PRINT" WHEN ENTERING LINES
*
* Translated and reworked into JavaScript in February 2021
* by Les Orchard <me@lmorchard.com>
*/
export const setGameOptions = (options = {}) =>
Object.assign(gameOptions, options);
export const getGameState = () => ({ ...gameState });
export const onPrint = (fn) => (print = fn);
export const onInput = (fn) => (input = fn);
export const onExit = (fn) => (exit = fn);
export async function gameMain() {
await gameReset();
await gameLoop();
await exit();
}
let gameState = {};
let print = () => {};
let input = () => {};
let exit = () => {};
const gameOptions = {
stardateStart: Math.floor(Math.random() * 20 + 20) * 100,
timeLimit: 25 + Math.floor(Math.random() * 10),
energyMax: 3000,
photonTorpedoesMax: 10,
starbaseSpawnChance: 0.96,
enemyMaxShield: 200,
enemySpawnChance: [0.8, 0.85, 0.98],
nameEnemy: "KLINGON",
nameEnemies: "KLINGONS",
nameScienceOfficer: "SPOCK",
nameNavigationOfficer: "LT. SULU",
nameWeaponsOfficer: "ENSIGN CHEKOV",
nameCommunicationsOfficer: "LT. UHURA",
nameChiefEngineer: "SCOTT",
systemDamageChanceOnHit: 0.6,
systemDamageHitThroughShields: 0.02,
systemChanceAffectedInWarp: 0.2,
systemChanceDamageInWarp: 0.6,
};
async function gameIntro() {
print("\n".repeat(10));
print(" ,------*------,");
print(" ,------------- '--- ------'");
print(" '-------- --' / /");
print(" ,---' '-------/ /--,");
print(" '----------------'");
print("");
print(" THE USS ENTERPRISE --- NCC-1701");
print("\n".repeat(4));
print("YOUR ORDERS ARE AS FOLLOWS:");
print();
print(
` DESTROY THE ${gameState.enemiesRemaining} ${gameOptions.nameEnemy} WARSHIPS WHICH HAVE INVADED`
);
print(" THE GALAXY BEFORE THEY CAN ATTACK FEDERATION HEADQUARTERS");
print(
` ON STARDATE ${
gameOptions.stardateStart + gameOptions.timeLimit
} THIS GIVES YOU ${gameOptions.timeLimit} DAYS. THERE${
gameState.starbasesRemaining > 1 ? " ARE " : " IS "
}`
);
print(
` ${gameState.starbasesRemaining} STARBASE${
gameState.starbasesRemaining > 1 ? "S" : " ARE"
} IN THE GALAXY FOR RESUPPLYING YOUR SHIP`
);
}
async function gameReset() {
gameState = {
gameOver: false,
gameWon: false,
gameQuit: false,
destroyed: false,
shouldRestart: false,
quadrantMap: "",
alertCondition: "",
stardateCurrent: gameOptions.stardateStart,
isDocked: false,
energyRemaining: gameOptions.energyMax,
photonTorpedoesRemaining: gameOptions.photonTorpedoesMax,
shieldsCurrent: 0,
starbasesRemaining: 0,
enemiesRemaining: 0,
quadrantPositionY: randomInt(8, 1),
quadrantPositionX: randomInt(8, 1),
sectorPositionY: randomInt(8, 1),
sectorPositionX: randomInt(8, 1),
sectorEnemiesCount: 0,
sectorStarbasesCount: 0,
sectorStarsCount: 0,
galacticMap: [],
galacticMapDiscovered: [],
};
gameState.systemsDamage = {};
for (const systemName of shipSystems) {
gameState.systemsDamage[systemName] = 0;
}
for (let mapY = 1; mapY <= 8; mapY++) {
gameState.galacticMap[mapY] = [];
gameState.galacticMapDiscovered[mapY] = [];
for (let mapX = 1; mapX <= 8; mapX++) {
gameState.galacticMapDiscovered[mapY][mapX] = 0;
gameState.sectorEnemiesCount = 0;
const enemySpawnRoll = Math.random();
if (enemySpawnRoll > gameOptions.enemySpawnChance[2]) {
gameState.sectorEnemiesCount = 3;
gameState.enemiesRemaining = gameState.enemiesRemaining + 3;
} else if (enemySpawnRoll > gameOptions.enemySpawnChance[1]) {
gameState.sectorEnemiesCount = 2;
gameState.enemiesRemaining = gameState.enemiesRemaining + 2;
} else if (enemySpawnRoll > gameOptions.enemySpawnChance[0]) {
gameState.sectorEnemiesCount = 1;
gameState.enemiesRemaining = gameState.enemiesRemaining + 1;
}
// 980
gameState.sectorStarbasesCount = 0;
if (Math.random() > gameOptions.starbaseSpawnChance) {
gameState.sectorStarbasesCount = 1;
gameState.starbasesRemaining = gameState.starbasesRemaining + 1;
}
// 1040
gameState.galacticMap[mapY][mapX] =
gameState.sectorEnemiesCount * 100 +
gameState.sectorStarbasesCount * 10 +
randomInt(8, 1);
}
}
if (gameState.enemiesRemaining > gameOptions.timeLimit) {
// Ensure the player has at least one more stardate than the number of enemies
gameOptions.timeLimit = gameState.enemiesRemaining + 1;
}
if (gameState.starbasesRemaining === 0) {
if (
gameState.galacticMap[gameState.quadrantPositionY][
gameState.quadrantPositionX
] < 200
) {
gameState.galacticMap[gameState.quadrantPositionY][
gameState.quadrantPositionX
] =
gameState.galacticMap[gameState.quadrantPositionY][
gameState.quadrantPositionX
] + 120;
}
gameState.enemiesRemaining = gameState.enemiesRemaining + 1;
gameState.starbasesRemaining = 1;
gameState.galacticMap[gameState.quadrantPositionY][
gameState.quadrantPositionX
] =
gameState.galacticMap[gameState.quadrantPositionY][
gameState.quadrantPositionX
] + 10;
gameState.quadrantPositionY = randomInt(8, 1);
gameState.quadrantPositionX = randomInt(8, 1);
}
gameState.enemiesInitialCount = gameState.enemiesRemaining;
await gameIntro();
await newQuadrantEntered();
}
async function newQuadrantEntered() {
gameState.sectorEnemiesCount = 0;
gameState.sectorStarbasesCount = 0;
gameState.sectorStarsCount = 0;
gameState.starbaseRepairDelay = 0.5 * Math.random();
// Add this sector to the known map
gameState.galacticMapDiscovered[gameState.quadrantPositionY][
gameState.quadrantPositionX
] =
gameState.galacticMap[gameState.quadrantPositionY][
gameState.quadrantPositionX
];
gameState.sectorEnemies = [
null, //TODO fix zero-index
[],
[],
[],
];
if (
gameState.quadrantPositionY >= 1 &&
gameState.quadrantPositionY <= 8 &&
gameState.quadrantPositionX >= 1 &&
gameState.quadrantPositionX <= 8
) {
let G2$ = buildQuadrantName(
gameState.quadrantPositionY,
gameState.quadrantPositionX
);
print();
if (gameOptions.stardateStart == gameState.stardateCurrent) {
print("YOUR MISSION BEGINS WITH YOUR STARSHIP LOCATED");
print("IN THE GALACTIC QUADRANT, '", G2$, "'.");
} else {
print("NOW ENTERING ", G2$, " QUADRANT . . .");
}
print();
gameState.sectorEnemiesCount = Math.floor(
gameState.galacticMap[gameState.quadrantPositionY][
gameState.quadrantPositionX
] * 0.01
);
gameState.sectorStarbasesCount =
Math.floor(
gameState.galacticMap[gameState.quadrantPositionY][
gameState.quadrantPositionX
] * 0.1
) -
10 * gameState.sectorEnemiesCount;
gameState.sectorStarsCount =
gameState.galacticMap[gameState.quadrantPositionY][
gameState.quadrantPositionX
] -
100 * gameState.sectorEnemiesCount -
10 * gameState.sectorStarbasesCount;
if (gameState.sectorEnemiesCount != 0) {
print("COMBAT AREA CONDITION RED");
if (gameState.shieldsCurrent <= 200) {
print(" SHIELDS DANGEROUSLY LOW");
}
}
for (let i = 1; i <= 3; i++) {
gameState.sectorEnemies[i][1] = 0;
gameState.sectorEnemies[i][2] = 0;
}
}
for (let i = 1; i <= 3; i++) {
gameState.sectorEnemies[i][3] = 0;
}
gameState.quadrantMap = " ".repeat(QUADRANT_MAP_CELLS.empty.length * 8 * 8);
insertInQuadrantMap(
QUADRANT_MAP_CELLS.hero,
gameState.sectorPositionY,
gameState.sectorPositionX
);
if (gameState.sectorEnemiesCount >= 1) {
// 1720
for (let i = 1; i <= gameState.sectorEnemiesCount; i++) {
const [y, x] = findSpaceInQuadrantMap();
insertInQuadrantMap(QUADRANT_MAP_CELLS.enemy, y, x);
gameState.sectorEnemies[i] = [
undefined,
y,
x,
gameOptions.enemyMaxShield * (0.5 + Math.random()),
];
}
}
if (gameState.sectorStarbasesCount >= 1) {
const [R1, R2] = findSpaceInQuadrantMap();
gameState.sectorStarbaseY = R1;
gameState.sectorStarbaseX = R2;
insertInQuadrantMap(QUADRANT_MAP_CELLS.base, R1, R2);
}
for (let i = 1; i <= gameState.sectorStarsCount; i++) {
insertInQuadrantMap(QUADRANT_MAP_CELLS.star, ...findSpaceInQuadrantMap());
}
return shortRangeSensorScanAndStartup();
}
async function gameLoop() {
while (!gameState.gameOver) {
await acceptCommand();
if (gameState.gameOver) {
await endOfGame();
}
if (gameState.shouldRestart) {
await gameReset();
}
}
}
async function acceptCommand() {
if (
gameState.shieldsCurrent + gameState.energyRemaining <= 10 ||
(gameState.energyRemaining < 10 &&
gameState.systemsDamage[SYSTEM_SHIELD_CONTROL] != 0)
) {
print();
print("** FATAL ERROR ** YOU'VE JUST STRANDED YOUR SHIP IN SPACE");
print("YOU HAVE INSUFFICIENT MANEUVERING ENERGY, AND SHIELD CONTROL");
print("IS PRESENTLY INCAPABLE OF CROSS-CIRCUITING TO ENGINE ROOM!!");
print();
gameState.gameOver = true;
return;
}
const commandInput = (await input("COMMAND")).trim().toUpperCase();
const command = COMMANDS[commandInput] || commandHelp;
await command();
}
/************************************************************************/
const COMMANDS = {
NAV: commandCourseControl,
SRS: shortRangeSensorScanAndStartup,
LRS: commandLongRangeScan,
PHA: commandPhaserControl,
TOR: commandPhotonTorpedo,
SHE: commandShieldControl,
DAM: commandDamageControl,
COM: commandLibraryComputer,
XXX: () => {
// todo more confirmation here?
gameState.gameOver = true;
gameState.gameQuit = true;
},
DUMP: () => {
console.log(JSON.stringify(gameState));
},
};
async function commandHelp() {
print("ENTER ONE OF THE FOLLOWING:");
print(" NAV (TO SET COURSE)");
print(" SRS (FOR SHORT RANGE SENSOR SCAN)");
print(" LRS (FOR LONG RANGE SENSOR SCAN)");
print(" PHA (TO FIRE PHASERS)");
print(" TOR (TO FIRE PHOTON TORPEDOES)");
print(" SHE (TO RAISE OR LOWER SHIELDS)");
print(" DAM (FOR DAMAGE CONTROL REPORTS)");
print(" COM (TO CALL ON LIBRARY-COMPUTER)");
print(" XXX (TO RESIGN YOUR COMMAND)");
print();
}
async function shortRangeSensorScanAndStartup() {
checkIfDocked();
if (gameState.isDocked) {
gameState.alertCondition = "DOCKED";
gameState.energyRemaining = gameOptions.energyMax;
gameState.photonTorpedoesRemaining = gameOptions.photonTorpedoesMax;
print("SHIELDS DROPPED FOR DOCKING PURPOSES");
gameState.shieldsCurrent = 0;
} else {
gameState.alertCondition = "GREEN";
if (gameState.energyRemaining < gameOptions.energyMax * 0.1)
gameState.alertCondition = "YELLOW";
if (gameState.sectorEnemiesCount > 0) gameState.alertCondition = "RED";
}
if (gameState.systemsDamage[SYSTEM_SHORT_RANGE_SENSORS] < 0) {
print();
print("*** SHORT RANGE SENSORS ARE OUT ***");
print();
return;
}
const statusLines = [
`STARDATE ${Math.floor(gameState.stardateCurrent * 10) * 0.1}`,
`CONDITION ${gameState.alertCondition}`,
`QUADRANT ${gameState.quadrantPositionY} , ${gameState.quadrantPositionX}`,
`SECTOR ${gameState.sectorPositionY} , ${gameState.sectorPositionX}`,
`PHOTON TORPEDOES ${gameState.photonTorpedoesRemaining}`,
`TOTAL ENERGY ${
gameState.energyRemaining + gameState.shieldsCurrent
}`,
`SHIELDS ${gameState.shieldsCurrent}`,
`${gameOptions.nameEnemies} REMAINING ${gameState.enemiesRemaining}`,
];
const lineSplit = new RegExp(
`.{${QUADRANT_MAP_CELLS.empty.length * 8}}`,
"g"
);
const cellSplit = new RegExp(`.{${QUADRANT_MAP_CELLS.empty.length}}`, "g");
print(" 1 2 3 4 5 6 7 8 ");
print(" ---------------------------------");
print(
gameState.quadrantMap
// Split the map into lines of 24 chars
.match(lineSplit)
// Split each line into cells of 3 chars
.map((line) => line.match(cellSplit))
// Format each line with Y coord, spaced out cells, and a line of status
.map(
(line, idx) =>
` ${idx + 1} ` + line.join(" ") + " ".repeat(4) + statusLines[idx]
)
// Finally, join all the lines with returns
.join("\n")
);
print(" ---------------------------------");
print();
}
function checkIfDocked() {
const { sectorPositionY: sY, sectorPositionX: sX } = gameState;
for (let posY = sY - 1; posY <= sY + 1; posY++) {
for (let posX = sX - 1; posX <= sX + 1; posX++) {
if (posY >= 1 || posY <= 8 || posX >= 1 || posX <= 8) {
if (findInQuadrantMap(QUADRANT_MAP_CELLS.base, posY, posX)) {
gameState.isDocked = true;
return;
}
}
}
}
gameState.isDocked = false;
}
async function commandCourseControl() {
let courseInput = parseFloat(await input("COURSE (0-9)"));
if (courseInput == 9) courseInput = 1;
if (isNaN(courseInput) || courseInput < 1 || courseInput > 9) {
print(
` ${gameOptions.nameNavigationOfficer} REPORTS, 'INCORRECT COURSE DATA, SIR!'`
);
return;
}
const warpFactorInput = parseFloat(
await input(
`WARP FACTOR (0-${
gameState.systemsDamage[SYSTEM_WARP_ENGINES] < 0 ? "0.2" : "8"
})`
)
);
if (warpFactorInput == 0 || isNaN(warpFactorInput)) return;
if (
gameState.systemsDamage[SYSTEM_WARP_ENGINES] < 0 &&
warpFactorInput > 0.2
) {
return print("WARP ENGINES ARE DAMAGED. MAXIMUM SPEED = WARP 0.2");
}
if (warpFactorInput < 0 && warpFactorInput > 8) {
return print(
` CHIEF ENGINEER ${gameOptions.nameChiefEngineer} REPORTS 'THE ENGINES WON'T TAKE WARP ${warpFactorInput}!'`
);
}
const sectorsToWarp = Math.floor(warpFactorInput * 8 + 0.5);
if (gameState.energyRemaining - sectorsToWarp < 0) {
print("ENGINEERING REPORTS 'INSUFFICIENT ENERGY AVAILABLE");
print(
" FOR MANEUVERING AT WARP ",
warpFactorInput,
" !'"
);
if (
gameState.shieldsCurrent > sectorsToWarp - gameState.energyRemaining &&
gameState.systemsDamage[SYSTEM_SHIELD_CONTROL] > 0
) {
print(
"DEFLECTOR CONTROL ROOM ACKNOWLEDGES ",
gameState.shieldsCurrent,
" UNITS OF ENERGY"
);
print(" PRESENTLY DEPLOYED TO SHIELDS.");
}
}
for (let enemyIdx = 1; enemyIdx <= 3; enemyIdx++) {
if (gameState.sectorEnemies[enemyIdx][3] > 0) {
insertInQuadrantMap(
QUADRANT_MAP_CELLS.empty,
gameState.sectorEnemies[enemyIdx][1],
gameState.sectorEnemies[enemyIdx][2]
);
const [R1, R2] = findSpaceInQuadrantMap();
gameState.sectorEnemies[enemyIdx][1] = R1;
gameState.sectorEnemies[enemyIdx][2] = R2;
insertInQuadrantMap(
QUADRANT_MAP_CELLS.enemy,
gameState.sectorEnemies[enemyIdx][1],
gameState.sectorEnemies[enemyIdx][2]
);
}
}
enemiesShoot();
let damageControlHeaderPrinted = false;
const printDamageReport = (msg) => {
if (!damageControlHeaderPrinted) {
damageControlHeaderPrinted = true;
print("DAMAGE CONTROL REPORT:");
}
print(msg);
};
let repairFactorDuringWarp = Math.min(1, warpFactorInput);
// Continually repair damaged systems during warp
for (const systemName of shipSystems) {
if (gameState.systemsDamage[systemName] >= 0) continue;
gameState.systemsDamage[systemName] =
gameState.systemsDamage[systemName] + repairFactorDuringWarp;
if (
gameState.systemsDamage[systemName] > -0.1 &&
gameState.systemsDamage[systemName] < 0
) {
gameState.systemsDamage[systemName] = -0.1;
continue;
}
if (gameState.systemsDamage[systemName] < 0) continue;
printDamageReport(` ${systemName} REPAIR COMPLETED.`);
}
// 20% chance of a random system being damaged, repaired, or improved in warp
if (Math.random() < gameOptions.systemChanceAffectedInWarp) {
const systemIdx = randomInt(shipSystems.length);
const systemName = shipSystems[systemIdx];
if (Math.random() < gameOptions.systemChanceDamageInWarp) {
// 60% chance of random system damage
gameState.systemsDamage[systemName] =
gameState.systemsDamage[systemName] - (Math.random() * 5 + 1);
printDamageReport(` ${systemName} DAMAGED`);
} else {
// 40% chance of random system repair or improvement
gameState.systemsDamage[systemName] =
gameState.systemsDamage[systemName] + Math.random() * 3 + 1;
printDamageReport(` ${systemName} STATE OF REPAIR IMPROVED`);
}
print();
}
// 3060 REM BEGIN MOVING STARSHIP
insertInQuadrantMap(
QUADRANT_MAP_CELLS.empty,
Math.floor(gameState.sectorPositionY),
Math.floor(gameState.sectorPositionX)
);
const [courseDeltaY, courseDeltaX] = courseToDeltaXY(courseInput);
let currentSectorPositionY = gameState.sectorPositionY;
let currentSectorPositionX = gameState.sectorPositionX;
let currentQuadrantPosY = gameState.quadrantPositionY;
let currentQuadrantPosX = gameState.quadrantPositionX;
for (let sectorsWarped = 1; sectorsWarped < sectorsToWarp; sectorsWarped++) {
gameState.sectorPositionY = gameState.sectorPositionY + courseDeltaY;
gameState.sectorPositionX = gameState.sectorPositionX + courseDeltaX;
if (
gameState.sectorPositionY < 1 ||
gameState.sectorPositionY >= 9 ||
gameState.sectorPositionX < 1 ||
gameState.sectorPositionX >= 9
) {
// 3490 REM EXCEEDED QUADRANT LIMITS
currentSectorPositionY =
8 * gameState.quadrantPositionY +
currentSectorPositionY +
sectorsToWarp * courseDeltaY;
currentSectorPositionX =
8 * gameState.quadrantPositionX +
currentSectorPositionX +
sectorsToWarp * courseDeltaX;
gameState.quadrantPositionY = Math.floor(currentSectorPositionY / 8);
gameState.quadrantPositionX = Math.floor(currentSectorPositionX / 8);
gameState.sectorPositionY = Math.floor(
currentSectorPositionY - gameState.quadrantPositionY * 8
);
gameState.sectorPositionX = Math.floor(
currentSectorPositionX - gameState.quadrantPositionX * 8
);
if (gameState.sectorPositionY == 0) {
gameState.quadrantPositionY = gameState.quadrantPositionY - 1;
gameState.sectorPositionY = 8;
}
if (gameState.sectorPositionX == 0) {
gameState.quadrantPositionX = gameState.quadrantPositionX - 1;
gameState.sectorPositionX = 8;
}
let galacticPerimeterHit = false;
if (gameState.quadrantPositionY < 1) {
galacticPerimeterHit = true;
gameState.quadrantPositionY = 1;
gameState.sectorPositionY = 1;
}
if (gameState.quadrantPositionY > 8) {
galacticPerimeterHit = true;
gameState.quadrantPositionY = 8;
gameState.sectorPositionY = 8;
}
if (gameState.quadrantPositionX < 1) {
galacticPerimeterHit = true;
gameState.quadrantPositionX = 1;
gameState.sectorPositionX = 1;
}
if (gameState.quadrantPositionX > 8) {
galacticPerimeterHit = true;
gameState.quadrantPositionX = 8;
gameState.sectorPositionX = 8;
}
if (galacticPerimeterHit) {
print(
`${gameOptions.nameCommunicationsOfficer} REPORTS MESSAGE FROM STARFLEET COMMAND:`
);
print(" 'PERMISSION TO ATTEMPT CROSSING OF GALACTIC PERIMETER");
print(" IS HEREBY *DENIED*. SHUT DOWN YOUR ENGINES.'");
print(
`CHIEF ENGINEER ${gameOptions.nameChiefEngineer} REPORTS 'WARP ENGINES SHUT DOWN`
);
print(
` AT SECTOR ${gameState.sectorPositionY} , ${gameState.sectorPositionX} OF QUADRANT ${gameState.quadrantPositionY} , ${gameState.quadrantPositionX}.'`
);
if (checkIfTimeExpired()) {
return;
}
}
if (
8 * gameState.quadrantPositionY + gameState.quadrantPositionX ==
8 * currentQuadrantPosY + currentQuadrantPosX
) {
break;
}
gameState.stardateCurrent = gameState.stardateCurrent + 1;
consumeEnergyForWarp(sectorsToWarp);
return newQuadrantEntered();
}
if (
!findInQuadrantMap(
QUADRANT_MAP_CELLS.empty,
gameState.sectorPositionY,
gameState.sectorPositionX
)
) {
// Undo this step of warp travel if the space isn't empty
gameState.sectorPositionY = Math.floor(
gameState.sectorPositionY - courseDeltaY
);
gameState.sectorPositionX = Math.floor(
gameState.sectorPositionX - courseDeltaX
);
print(
`WARP ENGINES SHUT DOWN AT SECTOR ${gameState.sectorPositionY} , ${gameState.sectorPositionX} DUE TO BAD NAVAGATION`
);
break;
}
}
gameState.sectorPositionY = Math.floor(gameState.sectorPositionY);
gameState.sectorPositionX = Math.floor(gameState.sectorPositionX);
insertInQuadrantMap(
QUADRANT_MAP_CELLS.hero,
Math.floor(gameState.sectorPositionY),
Math.floor(gameState.sectorPositionX)
);
consumeEnergyForWarp(sectorsToWarp);
let timeElapsedDuringWarp = 1;
if (warpFactorInput < 1) {
timeElapsedDuringWarp = 0.1 * Math.floor(10 * warpFactorInput);
}
gameState.stardateCurrent = gameState.stardateCurrent + timeElapsedDuringWarp;
if (checkIfTimeExpired()) {
return;
}
await shortRangeSensorScanAndStartup();
}
function checkIfTimeExpired() {
if (
gameState.stardateCurrent >
gameOptions.stardateStart + gameOptions.timeLimit
) {
gameState.gameOver = true;
}
return gameState.gameOver;
}
function consumeEnergyForWarp(sectorsToWarp) {
// 3900 REM MANEUVER ENERGY S/R **
gameState.energyRemaining = gameState.energyRemaining - sectorsToWarp - 10;
if (gameState.energyRemaining >= 0) {
return;
}
print("SHIELD CONTROL SUPPLIES ENERGY TO COMPLETE THE MANEUVER.");
gameState.shieldsCurrent =
gameState.shieldsCurrent + gameState.energyRemaining;
gameState.energyRemaining = 0;
if (gameState.shieldsCurrent <= 0) {
gameState.shieldsCurrent = 0;
}
}
async function commandLongRangeScan() {
// 3990 REM LONG RANGE SENSOR SCAN CODE
if (gameState.systemsDamage[SYSTEM_LONG_RANGE_SENSORS] < 0) {
print("LONG RANGE SENSORS ARE INOPERABLE");
return;
}
print(
"LONG RANGE SCAN FOR QUADRANT ",
gameState.quadrantPositionY,
" , ",
gameState.quadrantPositionX
);
const separatorLine = "-------------------";
print(separatorLine);
for (
let posY = gameState.quadrantPositionY - 1;
posY <= gameState.quadrantPositionY + 1;
posY++
) {
// Scan a line of sectors
const lineSectors = [null, null, null];
for (
let posX = gameState.quadrantPositionX - 1;
posX <= gameState.quadrantPositionX + 1;
posX++
) {
if (posY > 0 && posY < 9 && posX > 0 && posX < 9) {
// Add the scanned cell to the current scan output
lineSectors[posX - gameState.quadrantPositionX + 1] =
gameState.galacticMap[posY][posX];
// Add the scanned cell to the discovered map
gameState.galacticMapDiscovered[posY][posX] =
gameState.galacticMap[posY][posX];
}
}
// Print a formatted line of the scan - e.g. ": 004 : 205 : 004 :"
print(
": " +
lineSectors
.map((sector) =>
sector === null ? "***" : sector.toString().padStart(3, "0")
)
.join(" : ") +
" :"
);
print(separatorLine);
}
}
async function commandPhaserControl() {
// 4250 REM PHASER CONTROL CODE BEGINS HERE
if (gameState.systemsDamage[SYSTEM_PHASER_CONTROL] < 0) {
print("PHASERS INOPERATIVE");
return;
}
if (gameState.sectorEnemiesCount <= 0) {
print(
`SCIENCE OFFICER ${gameOptions.nameScienceOfficer} REPORTS 'SENSORS SHOW NO ENEMY SHIPS`
);
print(" IN THIS QUADRANT'");
return;
}
if (gameState.systemsDamage[SYSTEM_LIBRARY_COMPUTER] < 0) {
print("COMPUTER FAILURE HAMPERS ACCURACY");
}
print(
"PHASERS LOCKED ON TARGET; ENERGY AVAILABLE = ",
gameState.energyRemaining,
" UNITS"
);
let phaserUnitsToFire;
const continueCommandLoop = true;
while (continueCommandLoop) {
phaserUnitsToFire = parseFloat(await input("NUMBER OF UNITS TO FIRE"));
if (phaserUnitsToFire <= 0) return;
if (gameState.energyRemaining - phaserUnitsToFire >= 0) {
break;
}
print(`ENERGY AVAILABLE = ${gameState.energyRemaining} UNITS`);
}
gameState.energyRemaining = gameState.energyRemaining - phaserUnitsToFire;
// FIXED: in the original, this was shield system. Changed to phaser system.
if (gameState.systemsDamage[SYSTEM_PHASER_CONTROL] < 0) {
phaserUnitsToFire = phaserUnitsToFire * Math.random();
}
// Spread phaser fire between all enemies
let phaserUnitsPerEnemy = Math.floor(
phaserUnitsToFire / gameState.sectorEnemiesCount
);
for (let enemyIdx = 1; enemyIdx <= 3; enemyIdx++) {
if (gameState.sectorEnemies[enemyIdx][3] <= 0) {
// Skip dead enemies
continue;
}
print();
// Phaser damage falls off based on distance and a bit of chance
let phaserDamage = Math.floor(
(phaserUnitsPerEnemy / distanceFromEnemy(enemyIdx)) * (Math.random() + 2)
);
if (phaserDamage <= 0.15 * gameState.sectorEnemies[enemyIdx][3]) {
print(
"SENSORS SHOW NO DAMAGE TO ENEMY AT ",
gameState.sectorEnemies[enemyIdx][1],
" , ",
gameState.sectorEnemies[enemyIdx][2]
);
continue;
}
gameState.sectorEnemies[enemyIdx][3] =
gameState.sectorEnemies[enemyIdx][3] - phaserDamage;
print(
`${phaserDamage} UNIT HIT ON ${gameOptions.nameEnemy} AT SECTOR ${gameState.sectorEnemies[enemyIdx][1]} , ${gameState.sectorEnemies[enemyIdx][2]}`
);
if (gameState.sectorEnemies[enemyIdx][3] > 0) {
print(
` (SENSORS SHOW ${gameState.sectorEnemies[enemyIdx][3]} UNITS REMAINING)`
);
print();
} else {
print(`*** ${gameOptions.nameEnemy} DESTROYED ***`);
print();
gameState.sectorEnemiesCount = gameState.sectorEnemiesCount - 1;
gameState.enemiesRemaining = gameState.enemiesRemaining - 1;
// Remove enemy from display
insertInQuadrantMap(
QUADRANT_MAP_CELLS.empty,
gameState.sectorEnemies[enemyIdx][1],
gameState.sectorEnemies[enemyIdx][2]
);
// Set enemy health at exactly zero
gameState.sectorEnemies[enemyIdx][3] = 0;
// Update the galactic map with one fewer enemy
gameState.galacticMap[gameState.quadrantPositionY][
gameState.quadrantPositionX
] -= 100;
// Copy updated galactic map sector to discovered map.
gameState.galacticMapDiscovered[gameState.quadrantPositionY][
gameState.quadrantPositionX
] =
gameState.galacticMap[gameState.quadrantPositionY][
gameState.quadrantPositionX
];
if (gameState.enemiesRemaining <= 0) {
// If that was the last enemy, we've won!
gameState.gameOver = true;
gameState.gameWon = true;
return;
}
}
}
enemiesShoot();
}
async function commandPhotonTorpedo() {
// 4690 REM PHOTON TORPEDO CODE BEGINS HERE
// 4700
if (gameState.photonTorpedoesRemaining <= 0) {
return print("ALL PHOTON TORPEDOES EXPENDED");
}
if (gameState.systemsDamage[SYSTEM_PHOTON_TUBES] < 0) {
return print("PHOTON TUBES ARE NOT OPERATIONAL");
}
let torpedoCourse = parseFloat(await input("PHOTON TORPEDO COURSE (1-9)"));
if (torpedoCourse == 9) torpedoCourse = 1;
if (torpedoCourse < 1 || torpedoCourse > 9) {
print(
`${gameOptions.nameWeaponsOfficer} REPORTS, 'INCORRECT COURSE DATA, SIR!'`
);
}
const [courseDeltaY, courseDeltaX] = courseToDeltaXY(torpedoCourse);
gameState.energyRemaining = gameState.energyRemaining - 2;
gameState.photonTorpedoesRemaining = gameState.photonTorpedoesRemaining - 1;
let currPosY = gameState.sectorPositionY;
let currPosX = gameState.sectorPositionX;
print("TORPEDO TRACK:");
// Fly the torpedo along its course...
let quantizedPosY, quantizedPosX;
const forever = true;
while (forever) {
currPosY = currPosY + courseDeltaY;
currPosX = currPosX + courseDeltaX;
// The course will move in decimals, quantize to whole numbers
quantizedPosY = Math.floor(currPosY + 0.5);
quantizedPosX = Math.floor(currPosX + 0.5);
// Exiting the sector means the torpedo missed
if (
quantizedPosY < 1 ||
quantizedPosY > 8 ||
quantizedPosX < 1 ||
quantizedPosX > 8
) {
print("TORPEDO MISSED");
return enemiesShoot();
}
print(` ${quantizedPosY} , ${quantizedPosX}`);
if (
!findInQuadrantMap(QUADRANT_MAP_CELLS.empty, quantizedPosY, quantizedPosX)
) {
// Torpedo hit something solid, so stop flying.
break;
}
}
// Did the torpedo hit an enemy?
if (
findInQuadrantMap(QUADRANT_MAP_CELLS.enemy, quantizedPosY, quantizedPosX)
) {
print(`*** ${gameOptions.nameEnemy} DESTROYED ***`);
gameState.sectorEnemiesCount = gameState.sectorEnemiesCount - 1;
gameState.enemiesRemaining = gameState.enemiesRemaining - 1;
if (gameState.enemiesRemaining <= 0) {
// If that was the last enemy, then we've won!
gameState.gameOver = true;
gameState.gameWon = true;
return;
}