-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsuperstartrek.pl
1657 lines (1416 loc) · 46 KB
/
superstartrek.pl
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
#!/usr/bin/perl
use strict;
use warnings;
# 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
# ***
# *************************************************************
# *** Perl Conversion By Emanuele Bolognesi - v1.0 Oct 2020
# ***
# *** The main bug fixes are the number of starbases listed
# *** at the beginning of the game, and the phaser accuracy
# *** correctly affected by computer damage, not shields
# *** I also added instructions and indication of the course.
# *** A lot of comments regarding programming choices and
# *** and possible future improvements/bug fixes
# *** The "look & feel" should be exactly the same, apart from
# *** a few additional messages that I added
# *** I will continue updating and improving this code, without
# *** changing the original game mechanics.
# ***
# *** Comments in uppercase are the original comments, lowercase
# *** are mine.
# ***
# *** My blog: http://emabolo.com/
# *** Github: https://github.com/emabolo
# ***
# *************************************************************
AskForInstructions();
my $DisableTeleprint = 0;
# the original 1978 BASIC code does not have instructions
# there is another BASIC program. I copied it here. If you don't want it, you
# can simply comment the line above
BeginningOfGame:
print "\n\n\n\n\n\n";
print " ,------*------,\n";
print " ,------------- '--- ------'\n";
print " '-------- --' / /\n";
print " ,---' '-------/ /--,\n";
print " '----------------'\n\n";
print " THE USS ENTERPRISE --- NCC-1701\n";
print "\n\n\n\n\n\n";
# here is line-260
my $SomeSpaces =" ";
# Array G is the Galaxy, contains elements of 3 numbers like 215 -> 2 klingons 1 base 5 stars
my @Galaxy = (); # DIM G(8,8) in the original code
# Array "C" contains the delta X and Y depending on the course
# I know it's ugly, but since in BASIC the array starts from 1, I'm filling with zeros the useless elements
my @C = ([0,0,0],[0,0,1],[0,-1,1],[0,-1,0],[0,-1,-1],[0,0,-1],[0,1,-1],[0,1,0],[0,1,1],[0,0,1]);
my $i;
my $j;
my @K = (); # DIM K(3,3) - Klingons
my @ExploredSpace = (); # DIM Z(8,8) # A copy of Galaxy, but only with quadrants explored/scanned
my @DamageLevel = ();
# here is line-370
my $Stardate=int(rand(1)*20+20)*100; # Stardate, numero tra 2000 e 3900 (was $T)
my $T0 = $Stardate;
my $MaxNumOfDays=25+int(rand(1)*10);
my $ShipDocked=0; # Was D0
my $MaxEnergyLevel=3000;
my $EnergyLevel = $MaxEnergyLevel;
my $PhotonTorpedoes=10; # (was $P)
my $MaxTorpedoes=$PhotonTorpedoes;
my $KlingonBaseEnergy=200; # Klingon ship energy range from 0.5x to 2x this value
my $ShieldLevel=0; # Shield level of the Enterprise (was $S)
my $TotalStarbases=0; # This was set to 2, but the code never adds the 2 additional bases so I changed to 0
my $TotalKlingonShips=0; # was K9
my $ShipCondition = '';
my $Ss="";
my $Ss0=" IS ";
my ($I,$J);
# REM INITIALIZE ENTERPRIZE'S POSITION
my $Q1=FNR(1); # There are 2 functions in the original BASIC code. I kept the original
my $Q2=FNR(1); # name for this one, that generates a random number between 1 and 8
my $S1=FNR(1); # Coordinates of Enterprise
my $S2=FNR(1);
for ($I=1;$I<=8;$I++) {
# Set Damage level to 0 for all systems
$DamageLevel[$I]=0;
}
# All possible commands are here. The algorithm will search
# the string entered by the user in this string and then there was a "ON I GOTO"
# Much shorter than a series of IF
my $AllCommands="NAVSRSLRSPHATORSHEDAMCOMXXX";
# REM SETUP WHAT EXISTS IN GALAXY . . .
my $K3; # KLINGONS in sector
my $B3; # STARBASES in sector
my $S3; # stars in sector
my ($B4,$B5); # B4, B5 = coordinates of the starbase, if any
for ($I=1;$I<=8;$I++) {
for (my $J=1;$J<=8;$J++) {
$K3=0;
$ExploredSpace[$I][$J]=0; # Galaxy explored by the player, initially all quadrants are 0
my $R1=rand(1);
if ($R1>.98) {
$K3=3;
$TotalKlingonShips=$TotalKlingonShips+3;
}
elsif($R1>.95) {
$K3=2;
$TotalKlingonShips=$TotalKlingonShips+2;
}
elsif($R1>.80) {
$K3=1;
$TotalKlingonShips=$TotalKlingonShips+1;
}
# that is line 980
$B3=0;
if(rand(1)>.96) {
$B3=1;
$TotalStarbases=$TotalStarbases+1;
}
$Galaxy[$I][$J]=$K3*100+$B3*10+FNR(1);
}
}
if ($TotalKlingonShips>$MaxNumOfDays) {$MaxNumOfDays=$TotalKlingonShips+1;}
# Q1, Q2 are random coordinates
if ($TotalStarbases == 0) {
# If sector has less than 2 klingon ship, it adds 1 ship and 2 bases, why?
# also the total number of bases is not increased - BUG ?
# Doesnt make sense so I'm commenting the next 3 lines
#if ($Galaxy[$Q1][$Q2]<200) {
# $Galaxy[$Q1][$Q2]=$Galaxy[$Q1][$Q2]+120;
# $TotalKlingonShips=$TotalKlingonShips+1;
#}
#if bases are 0, add a base in a random quadrant, this makes sense
$TotalStarbases=1;
$Galaxy[$Q1][$Q2]=$Galaxy[$Q1][$Q2]+10;
$Q1=FNR(1);
$Q2=FNR(1);
}
my $InitialKlingonShips=$TotalKlingonShips;
if ($TotalStarbases>1) {
$Ss="S";
$Ss0=" ARE ";
}
# The function telePrint is just a 'print' with a small delay to slow down text scrolling.
telePrint("YOUR ORDERS ARE AS FOLLOWS:");
telePrint(" DESTROY THE $TotalKlingonShips KLINGON WARSHIPS WHICH HAVE INVADED");
telePrint(" THE GALAXY BEFORE THEY CAN ATTACK FEDERATION HEADQUARTERS");
telePrint(" ON STARDATE ".($T0+$MaxNumOfDays).". THIS GIVES YOU $MaxNumOfDays DAYS.");
telePrint(" THERE".$Ss0." $TotalStarbases STARBASE".$Ss." IN THE GALAXY FOR RESUPPLYING YOUR SHIP.");
telePrint(" \n\n\n");
smallDelay(1);
$I=rand(1);
# IF INP(1)=13 THEN 1300 - this was already commented - function INP does not exist
#===============================================================
# This was line 1320
my ($X,$Y,$StepX1,$StepX2,$WarpFactor,$NoOfSteps); # global vars used by several functions related to movement of ship
my $QuadString;
my $QuadrantName; # Name of the quadrant (was G2)
my $GameOver = 0;
my $Q4 = 0; # this will contain the coordinate of previous quadrant
my $Q5 = 0; # useful to see if a movement produced a change of quadrant
# main loop, it goes ahead until the enterprise is destroyed or the time is over or klingons are defeated
while (!$GameOver) {
$K3=0;
$B3=0;
$S3=0;
$ExploredSpace[$Q1][$Q2]=$Galaxy[$Q1][$Q2]; # this quadrant has been discovered
# The original codes check if the Enterprise is inside the borders, not sure why
if ($Q1<1 || $Q1>8 || $Q2<1 || $Q2>8) {
die "The Enterprise went outside the border of the galaxy";
}
$QuadrantName = GetQuadrantName($Q1,$Q2);
print "\n";
if ($T0 == $Stardate) {
telePrint("YOUR MISSION BEGINS WITH YOUR STARSHIP LOCATED");
telePrint("IN THE GALACTIC QUADRANT '$QuadrantName'");
}
else {
telePrint("NOW ENTERING '$QuadrantName' QUADRANT . . .");
}
print "\n";
$K3=int($Galaxy[$Q1][$Q2]*.01);
$B3=int($Galaxy[$Q1][$Q2]*.1)-10*$K3;
$S3=$Galaxy[$Q1][$Q2]-100*$K3-10*$B3;
if ($K3>0) {
telePrint("COMBAT AREA CONDITION RED");
if($ShieldLevel<=200) {
telePrint(" SHIELDS DANGEROUSLY LOW");
}
smallDelay(1);
}
for ($I=1;$I<=3;$I++) {
$K[$I][1]=0;
$K[$I][2]=0;
$K[$I][3]=0;
}
# Clean Quadrant String
$QuadString = ' ' x 192;
# POSITION ENTERPRISE IN QUADRANT, THEN PLACE "K3" KLINGONS, &
# "B3" STARBASES, & "S3" STARS ELSEWHERE.
AddElementInQuadrantString('<*>',$S1,$S2);
# IF Klingons are present, for each klingon ship, find a place in the quadrant
if ($K3>0) {
for ($I=1;$I<=$K3;$I++) {
my ($R1,$R2) = FindEmptyPlaceinQuadrant();
AddElementInQuadrantString('+K+',$R1,$R2);
$K[$I][1]=$R1; # coordinates of Klingon ship
$K[$I][2]=$R2;
$K[$I][3]=$KlingonBaseEnergy*(0.5+rand(1)); # energy of the klingon
}
}
# If a base is present, place the base
if ($B3>0) {
my ($R1,$R2) = FindEmptyPlaceinQuadrant();
$B4=$R1;
$B5=$R2;
AddElementInQuadrantString('>!<',$R1,$R2);
}
# For each star, find a place
for ($I=1;$I<=$S3;$I++) {
my ($R1,$R2) = FindEmptyPlaceinQuadrant();
AddElementInQuadrantString(' * ',$R1,$R2);
}
# this is line 1980 in the original BASIC code
checkIfDocked();
ShortRangeSensorScan();
my $ReachedNewQuadrant = 0;
# This is line-1990 - MAIN LOOP for EXECUTING COMMANDS
while (!$ReachedNewQuadrant && !$GameOver) {
# of there is very low total energy or shield are damaged, the game is over
if ($EnergyLevel+$ShieldLevel<=10 || ($EnergyLevel<=10 && $DamageLevel[7]<0)) {
telePrint("\n** FATAL ERROR ** YOU'VE JUST STRANDED YOUR SHIP IN SPACE");
telePrint("YOU HAVE INSUFFICIENT MANEUVERING ENERGY, AND SHIELD CONTROL");
telePrint("IS PRESENTLY INCAPABLE OF CROSS-CIRCUITING TO ENGINE ROOM!!");
smallDelay(2);
$GameOver = 1;
last;
}
print "COMMAND? ";
chomp(my $Command = <STDIN>);
$Command = uc($Command);
if($Command eq "NAV"){
$ReachedNewQuadrant = CourseControl();
}
elsif($Command eq "SRS"){
ShortRangeSensorScan();
}
elsif($Command eq "LRS"){
LongRangeSensorScan();
}
elsif($Command eq "PHA"){
FirePhasers();
}
elsif($Command eq "TOR"){
FirePhotonTorpedoes();
}
elsif($Command eq "SHE"){
ShieldControl();
}
elsif($Command eq "DAM"){
DamageControl();
}
elsif($Command eq "COM"){
LibraryComputer();
}
elsif($Command eq "XXX"){
$GameOver = 1;
}
else {
print "ENTER ONE OF THE FOLLOWING:\n";
print " NAV (TO SET COURSE)\n";
print " SRS (FOR SHORT RANGE SENSOR SCAN)\n";
print " LRS (FOR LONG RANGE SENSOR SCAN)\n";
print " PHA (TO FIRE PHASERS)\n";
print " TOR (TO FIRE PHOTON TORPEDOES)\n";
print " SHE (TO RAISE OR LOWER SHIELDS)\n";
print " DAM (FOR DAMAGE CONTROL REPORTS)\n";
print " COM (TO CALL ON LIBRARY-COMPUTER)\n";
print " XXX (TO RESIGN YOUR COMMAND)\n\n";
}
} # keep asking new commands until the ship reaches a new quadrant
} # end of the main loop
#6210 REM END OF GAME
print "\n";
telePrint("IT IS STARDATE ".roundTo($Stardate,1));
# this was line 6270
if ($TotalKlingonShips>0) { # IF was not in the original code, added to be more generic
telePrint("THERE WERE $TotalKlingonShips KLINGON BATTLE CRUISERS LEFT AT");
telePrint("THE END OF YOUR MISSION.");
print "\n";
}
# This condition does not make sense. You can restart the game only if
# there are some starbases. But not in the same universe. You restart from scratch
# While if there are no starbases it doesn't ask you if you want to play again
if ($TotalStarbases > 0) {
print "THE FEDERATION IS IN NEED OF A NEW STARSHIP COMMANDER\n";
print "FOR A SIMILAR MISSION -- IF THERE IS A VOLUNTEER,\n";
print "LET HIM STEP FORWARD AND ENTER 'AYE'? ";
chomp(my $Command=<>);
if ($Command eq "AYE") {
goto BeginningOfGame;
}
}
print "\n\nThank you for playing this game!\n\nPerl conversion by Emanuele Bolognesi - http://emabolo.com\n\n";
exit;
# ============================================
sub FNR {
# the original code is int(rand()*7.98+1.01)
return int(rand(8)+1);
}
sub AddElementInQuadrantString {
# INSERT IN STRING ARRAY FOR QUADRANT line 8670
my $elem = shift;
my $y = shift;
my $x = shift;
die if (!$elem || !$y || !$x || length($elem) != 3);
$y = int($y-.5);
$x = int($x-.5);
my $position=$x*3+$y*24+1;
# Insert the element in the right position in the quadrant string
if ($position == 1) {
$QuadString = $elem . substr($QuadString,3);
}
elsif ($position == 190) {
$QuadString = substr($QuadString,0,189) . $elem;
}
else {
$QuadString = substr($QuadString,0,$position-1) . $elem . substr($QuadString,$position+2);
}
return;
}
sub SearchStringinQuadrant {
# STRING COMPARISON IN QUADRANT ARRAY - line 8830
my $elem = shift;
my $y = shift;
my $x = shift;
die if (!$elem || !$y || !$x);
$y = int($y-.5);
$x = int($x-.5);
my $position=$x*3+$y*24+1;
if (substr($QuadString,$position-1,3) eq $elem) {
return 1;
}
return 0;
}
sub FindEmptyPlaceinQuadrant {
# FIND EMPTY PLACE IN QUADRANT (FOR THINGS)
# generate 2 random coordinates, and check if quadrant cell is empty.
# If empty space is found, returns the coordinates
my ($y,$x);
my $found = 0;
while (!$found) {
$y=FNR(1);
$x=FNR(1);
$found = SearchStringinQuadrant(" ",$y,$x);
}
return ($y,$x);
}
sub GetQuadrantName {
my $Z4 = shift;
my $Z5 = shift;
my $RegionNameOnly = shift;
die if (!$Z4 || !$Z5);
# QUADRANT NAME IN G2$ FROM Z4,Z5 (=Q1,Q2)
# CALL WITH G5=1 (RegionNameOnly) TO GET REGION NAME ONLY
my @starnames = ();
if($Z5<=4) {
@starnames =('ANTARES','RIGEL','PROCYON','VEGA','CANOPUS','ALTAIR','SAGITTARIUS','POLLUX');
}
else {
@starnames =('SIRIUS','DENEB','CAPELLA','BETELGEUSE','ALDEBARAN','REGULUS','ARCTURUS','SPICA');
}
$QuadrantName = $starnames[$Z4-1];
if (!$RegionNameOnly) {
if ($Z5 ==1 || $Z5 ==5) {
$QuadrantName.=" I";
}
elsif($Z5 == 2 || $Z5 == 6) {
$QuadrantName.=" II";
}
elsif($Z5 ==3 || $Z5 == 7) {
$QuadrantName.=" III";
}
elsif($Z5 ==4 || $Z5 == 8) {
$QuadrantName.=" IV";
}
}
return $QuadrantName;
}
sub CheckShipStatus {
if ($ShipDocked) {
return 'DOCKED';
}
elsif ($K3>0) {
return "*RED*";
}
elsif ($EnergyLevel < ($MaxEnergyLevel/10) ) {
return "YELLOW";
}
else {
return "GREEN";
}
}
sub checkIfDocked {
$ShipDocked = 0;
for (my $i=$S1-1;$i<=$S1+1;$i++) {
for (my $j=$S2-1;$j<=$S2+1;$j++) {
my $ii = int($i+.5);
my $jj = int($j+.5);
if ($ii>=1 and $ii<=8 and $jj>=1 and $jj<=8) {
if (SearchStringinQuadrant(">!<",$i,$j)) { # found a starbase at coordinates I,J
$ShipDocked=1;
$ShipCondition="DOCKED";
$EnergyLevel=$MaxEnergyLevel;
$PhotonTorpedoes=$MaxTorpedoes;
telePrint("SHIELDS DROPPED FOR DOCKING PURPOSES");
$ShieldLevel=0;
last;
}
}
}
}
$ShipCondition= CheckShipStatus();
return $ShipDocked;
}
sub ShortRangeSensorScan {
# SHORT RANGE SENSOR SCAN & STARTUP SUBROUTINE
# it was line 6720
if ($DamageLevel[2]<0) {
telePrint("\n*** SHORT RANGE SENSORS ARE OUT ***\n");
return;
}
my $Header ="---------------------------------";
telePrint($Header);
for ($I=1;$I<=8;$I++) {
for (my $J=($I-1)*24+1;$J<=($I-1)*24+22;$J+=3) {
print " ".substr($QuadString,$J-1,3);
}
telePrint(" STARDATE ".int($Stardate*10)*.1) if($I == 1);
telePrint(" CONDITION ".$ShipCondition) if($I == 2);
telePrint(" QUADRANT ".$Q1." , ".$Q2) if($I == 3);
telePrint(" SECTOR ".$S1." , ".$S2) if($I == 4);
telePrint(" PHOTON TORPEDOES ".int($PhotonTorpedoes)) if($I == 5);
telePrint(" TOTAL ENERGY ".int($EnergyLevel+$ShieldLevel)) if($I == 6);
telePrint(" SHIELDS ".int($ShieldLevel)) if($I == 7);
telePrint(" KLINGONS REMAINING ".int($TotalKlingonShips)) if($I == 8);
}
telePrint($Header);
return;
}
sub LongRangeSensorScan {
# LONG RANGE SENSOR SCAN CODE
# it was line 4000
my @N = ();
if ($DamageLevel[3]<0) {
telePrint("LONG RANGE SENSORS ARE INOPERABLE.");
return 0;
}
print "LONG RANGE SCAN FOR QUADRANT $Q1,$Q2\n";
my $Header="-------------------";
telePrint($Header);
for ($I=$Q1-1;$I<=$Q1+1;$I++) {
$N[1]=-1; # if it's not a positive number later, it means the quadrant does not exist
$N[2]=-2;
$N[3]=-3;
for ($J=$Q2-1;$J<=$Q2+1;$J++) {
if ($I>0 && $I<9 && $J>0 && $J<9) {
$N[$J-$Q2+2]=$Galaxy[$I][$J];
$ExploredSpace[$I][$J]=$Galaxy[$I][$J]; # Scan a new quadrant
}
}
for(my $idx=1;$idx<=3;$idx++) {
print ": ";
if ($N[$idx]<0) {
print "*** ";
}
else {
my $strdollar = $N[$idx]+1000;
print substr($strdollar,-3)." ";
}
}
telePrint(":");
telePrint($Header);
}
return 0; # goto 1990
}
sub ShieldControl {
#5520 REM SHIELD CONTROL - 5530
if ($DamageLevel[7]<0) {
telePrint("SHIELD CONTROL INOPERABLE");
return 0;
}
telePrint("ENERGY AVAILABLE = ".($EnergyLevel+$ShieldLevel));
print "NUMBER OF UNITS TO SHIELDS? ";
my $Units;
chomp($Units = <>);
if ($Units !~ /^\d+$/) {
telePrint("INCORRECT VALUE");
}
elsif ($ShieldLevel == $Units) {
telePrint("<SHIELDS UNCHANGED>");
}
elsif ($Units > $EnergyLevel+$ShieldLevel) {
telePrint("SHIELD CONTROL REPORTS 'THIS IS NOT THE FEDERATION TREASURY.'");
telePrint("<SHIELDS UNCHANGED>");
}
else {
$EnergyLevel=$EnergyLevel+$ShieldLevel-$Units;
$ShieldLevel=$Units;
telePrint("DEFLECTOR CONTROL ROOM REPORT:");
telePrint(" 'SHIELDS NOW AT ".$ShieldLevel." UNITS PER YOUR COMMAND.'");
}
return $Units;
}
# REM PRINTS DEVICE NAME - 8790
sub deviceName {
# This was placing the device name in variable G2
my $sys = shift;
my @names = ("WARP ENGINES","SHORT RANGE SENSORS","LONG RANGE SENSORS","PHASER CONTROL","PHOTON TUBES",
"DAMAGE CONTROL","SHIELD CONTROL","LIBRARY-COMPUTER");
return $names[$sys-1];
}
sub roundTo {
my $value = shift;
my $precision = shift;
return int($value) if (!$precision);
$precision = (10 ** $precision);
$value = int($value*$precision);
return $value/$precision;
}
sub formatWithSpaces {
my $str = shift;
my $maxlength = shift;
my $centered = shift;
my $final = "";
if($centered) {
my $numsp = int(($maxlength-length($str))/2);
my $spaces = ' ' x $numsp;
$final = $spaces.$str.$spaces;
if (length($final) < $maxlength) {
$final = $final.' ';
}
}
else {
my $spaces = " " x ($maxlength-length($str));
$final = $str . $spaces;
}
return $final;
}
sub DamageControl {
#5680 REM DAMAGE CONTROL - 5690
if ($DamageLevel[6]>=0) {
telePrint("\nDEVICE STATE OF REPAIR");
for (my $index=1;$index<=8;$index++) {
my $tabs = 27;
if ($DamageLevel[$index]<0) {
$tabs=26;
}
telePrint(formatWithSpaces(deviceName($index),$tabs) . roundTo($DamageLevel[$index],2));
}
}
else {
telePrint("DAMAGE CONTROL REPORT NOT AVAILABLE");
}
print "\n";
# By checking the status, you can also ask to repair, if docked
if ($ShipDocked) {
# 5720
my $TimeToRepair=0;
for ($I=1;$I<=8;$I++) {
if ($DamageLevel[$I]<0) {
$TimeToRepair=$TimeToRepair+.1; # Time increases for each damaged system
}
}
if ($TimeToRepair==0) {
return 0; # nothing to repair
}
print "\n";
$TimeToRepair=roundTo($TimeToRepair+rand(0.5),2);
if ($TimeToRepair>0.9) {$TimeToRepair=0.9;} # never more than 1 day
telePrint("TECHNICIANS STANDING BY TO EFFECT REPAIRS TO YOUR SHIP;");
telePrint("ESTIMATED TIME TO REPAIR: $TimeToRepair STARDATES");
print "WILL YOU AUTHORIZE THE REPAIR ORDER (Y/N) ? ";
chomp(my $YesNo = <>);
if ($YesNo eq "Y") {
for ($I=1;$I<=8;$I++) {
if ($DamageLevel[$I]<0) {
$DamageLevel[$I]=0;
}
}
$Stardate=$Stardate+$TimeToRepair+0.1; # never trust engineers!
telePrint("REPAIR COMPLETED.");
}
}
return 0;
}
sub CalcAndPrintDirection {
my $m = shift; # X
my $n = shift; # A
my $StartingCourse = shift; # c1
my $Direction;
#8290
if (abs($m) > abs($n)) {
$Direction =$StartingCourse+(abs($n)/abs($m));
}
else {
$Direction =$StartingCourse+( ( abs($n)-abs($m)+abs($n) ) /abs($n));
}
telePrint(" DIRECTION = ".roundTo($Direction,2));
return 0;
}
sub PrintDistanceAndDirection {
my $W1 = shift;
my $X = shift;
my $C1 = shift;
my $A = shift;
$X=$X-$A;
$A=$C1-$W1;
if ($X<0) {
if($A>0) {
CalcAndPrintDirection($A,$X,3); # A>0 and X < 0
}
elsif($X != 0) { # else would be enough here (X<0 and A <=0)
CalcAndPrintDirection($X,$A,5);
}
}
else {
if ($A<0) { # case X>= 0 and A < 0
CalcAndPrintDirection($A,$X,7);
}
elsif($X>0) { # the only case where this is not true is X = 0
CalcAndPrintDirection($X,$A,1);
}
elsif ($A==0) { # so X = 0 and A = 0
CalcAndPrintDirection($X,$A,5);
}
elsif ($A > 0) { # so X = 0 and A > 0
CalcAndPrintDirection($X,$A,1);
}
}
my $Distance = sqrt($X**2 + $A**2);
telePrint(" DISTANCE = ".roundTo($Distance,2));
return 1;
}
sub PrintComputerRecord {
my $GalaxyMapOn = shift;
telePrint(" 1 2 3 4 5 6 7 8");
telePrint(" ----- ----- ----- ----- ----- ----- ----- -----");
for ($I=1;$I<=8;$I++) {
print $I.' ';
if ($GalaxyMapOn) {
$QuadrantName = GetQuadrantName($I,1,'RegionOnly');
print ' '.formatWithSpaces($QuadrantName,23,1);
$QuadrantName = GetQuadrantName($I,5,'RegionOnly');
print ' '.formatWithSpaces($QuadrantName,23,1);
}
else {
for ($J=1;$J<=8;$J++) {
print " ";
if ($ExploredSpace[$I][$J]==0) {
print "***";
}
else {
my $string = $ExploredSpace[$I][$J]+1000;
print substr($string,-3);
}
}
}
print "\n";
telePrint(" ----- ----- ----- ----- ----- ----- ----- -----");
}
print "\n";
return 0;
}
sub GalaxyMap {
#7390 REM SETUP TO CHANGE CUM GAL RECORD TO GALAXY MAP
telePrint(" THE GALAXY"); # GOTO7550
PrintComputerRecord(1);
return 1;
}
sub ComulativeGalacticRecord {
telePrint(" COMPUTER RECORD OF GALAXY FOR QUADRANT $Q1,$Q2\n");
PrintComputerRecord(0);
return 1;
}
sub StatusReport {
#7890 REM STATUS REPORT - 7900
telePrint(" STATUS REPORT:");
my $Ss="";
if ($TotalKlingonShips>1) {$Ss="S";}
telePrint("KLINGON".$Ss." LEFT: ".$TotalKlingonShips);
telePrint("MISSION MUST BE COMPLETED IN ".roundTo($T0+$MaxNumOfDays-$Stardate,1)." STARDATES");
$Ss="S";
if($TotalStarbases<2) {$Ss='';}
if($TotalStarbases<1) {
telePrint("YOUR STUPIDITY HAS LEFT YOU ON YOUR ON IN");
telePrint(" THE GALAXY -- YOU HAVE NO STARBASES LEFT!");
}
else {
telePrint("THE FEDERATION IS MAINTAINING ".$TotalStarbases." STARBASE".$Ss." IN THE GALAXY");
}
DamageControl();
return 1;
}
sub PhotonTorpedoData{
#8060 REM TORPEDO, BASE NAV, D/D CALCULATOR
if($K3<=0) {
telePrint("SCIENCE OFFICER SPOCK REPORTS 'SENSORS SHOW NO ENEMY SHIPS");
telePrint(" IN THIS QUADRANT'");
return 1;
}
$Ss="";
if ($K3>1) {
$Ss="S";
}
telePrint("FROM ENTERPRISE TO KLINGON BATTLE CRUISER".$Ss.":");
for ($I=1;$I<=3;$I++) {
if ($K[$I][3]>0) {
PrintDistanceAndDirection($K[$I][1],$K[$I][2],$S1,$S2);
}
}
return 1;
}
sub validCoord{
my $n = shift;
if ($n>=1 && $n<=8) {
return 1;
}
return 0;
}
sub DistanceCalculator {
telePrint("DIRECTION/DISTANCE CALCULATOR:");
print "YOU ARE AT QUADRANT $Q1,$Q2 SECTOR $S1,$S2\n";
print "PLEASE ENTER INITIAL COORDINATES (X,Y): ";
chomp(my $Coord = <>);
my ($y1,$x1) = split(/,/,$Coord,2);
print " FINAL COORDINATES (X,Y): ";
chomp($Coord = <>);
my ($y2,$x2) = split(/,/,$Coord,2);
if (validCoord($x1) && validCoord($y1) && validCoord($x2) && validCoord($y2)) {
PrintDistanceAndDirection($y2,$x2,$y1,$x1);
return 1;
}
else {
telePrint("WRONG COORDINATES");
return 0;
}
}
sub StarbaseNavData {
if ($B3 > 0) {
telePrint("FROM ENTERPRISE TO STARBASE:");
PrintDistanceAndDirection($B4,$B5,$S1,$S2); # dest, origin
}
else {
telePrint("MR. SPOCK REPORTS, 'SENSORS SHOW NO STARBASES IN THIS");
telePrint(" QUADRANT.'");
}
return 1;
}
sub LibraryComputer {
#7280 REM LIBRARY COMPUTER CODE - 7290
if($DamageLevel[8]<0) {
telePrint("COMPUTER DISABLED");
return 0;
}
my $ComputerDone = 0;
while(!$ComputerDone) {
print "COMPUTER ACTIVE AND AWAITING COMMAND? ";
chomp(my $Command=<>);
$Command = 0 if (!$Command);
$Command = 6 if ($Command !~ /^\d+$/);
print "\n";
if($Command == 0) {
$ComputerDone = ComulativeGalacticRecord(); #7540
}
elsif($Command == 1) {
$ComputerDone = StatusReport(); #7900
}
elsif($Command == 2) {
$ComputerDone = PhotonTorpedoData(); #8070
}
elsif($Command == 3) {
$ComputerDone = StarbaseNavData(); #8500
}
elsif($Command == 4) {
$ComputerDone = DistanceCalculator(); #8150
}
elsif($Command == 5) {
$ComputerDone = GalaxyMap(); #7400
}
else {
print "FUNCTIONS AVAILABLE FROM LIBRARY-COMPUTER:\n";
print " 0 = CUMULATIVE GALACTIC RECORD\n";
print " 1 = STATUS REPORT\n";
print " 2 = PHOTON TORPEDO DATA\n";
print " 3 = STARBASE NAV DATA\n";
print " 4 = DIRECTION/DISTANCE CALCULATOR\n";
print " 5 = GALAXY 'REGION NAME' MAP\n\n";
}
}
return 0;
}
sub EnterpriseDestroyed {
telePrint("\nTHE ENTERPRISE HAS BEEN DESTROYED. THEN FEDERATION WILL BE CONQUERED.");
smallDelay(2);
return;
}
sub KlingonsDefeated {
telePrint("\nCONGRATULATIONS, CAPTAIN! THEN LAST KLINGON BATTLE CRUISER");
telePrint("MENACING THE FEDERATION HAS BEEN DESTROYED.");
telePrint("\nYOUR EFFICIENCY RATING IS " . int(1000*($InitialKlingonShips/($Stardate-$T0))**2));
return;
}
sub DistanceOfShip {
# Distance between enterprise and Klingon ship
my $Index =shift;
my $DX = $K[$Index][1]-$S1;
my $DY = $K[$Index][2]-$S2;
return sqrt($DX**2 + $DY**2); # IN PERL DONT DO THIS: N^2 !!!
}
sub KlingonsAttack {
# this was line-6000
if ($K3<=0) {
return 0;
}
telePrint("KLINGON SHIPS ATTACK THE ENTERPRISE",1); # This message is not in the original game
if ($ShipDocked) {
telePrint("STARBASE SHIELDS PROTECT THE ENTERPRISE.");
return 0;
}
for ($I=1;$I<=3;$I++) {
my $KlingonEnergy = $K[$I][3];
if ($KlingonEnergy>0) {
my $Hits = int(($KlingonEnergy/DistanceOfShip($I))*(2+rand(1))+1);
$ShieldLevel = $ShieldLevel-$Hits;
# The choice below is strange. Energy of the klingon decrease when they fire
# but it does not depend on the power used by the phasers
# also it decreases a lot, because it can become 1/3 or 1/4 of the previous energy
# would be better to use an algorithm similar to the one used for the Enterprise
# Basically they are committing suicide
$K[$I][3] = $KlingonEnergy/(3+rand(1));
telePrint("$Hits UNIT HIT ON ENTERPRISE FROM SECTOR ".$K[$I][1].",".$K[$I][2]);
if($ShieldLevel<0) {
EnterpriseDestroyed();
$GameOver = 1;