(This is part three of the chronicle of my Retrochallenge 2016/01 submission, which is to port some classic Star Trek text games to the Mattel Aquarius. To jump to the beginning, go here.)

Mind Melding
In porting the 1972 version of STTR1 from HP Time-Shared BASIC to the Mattel Aquarius, I had to learn more about the code than I originally thought would be required. I imagined merely dealing with syntax differences which I could search and replace with ignorant bliss. However, the implementation of string variables differ so much between the two systems, I inevitably had to time-travel, get into the head of the high school student who authored STTR1, understand the program’s logic to a sufficient degree, and re-code some critical sections.
Is it spaghetti code? Of course it is. This is early BASIC and therefore a design requirement. Even so, some very clever algorithms exist throughout. (I’m especially impressed by the code that calculates course and torpedo trajectory.) However, the 1 or 2 character limit for variable names makes for a dizzying experience.
So before I move on and allow the weeds to germinate, I’d like to at least catalog the more interesting variables found in the original STTR1 listing along with my understanding of each one’s purpose.
Noteworthy Variables Found in Original STTR1
In case you’re inclined to follow along, here is the original BASIC listing for STTR1.
Arrays/Matrices
230 REM ***** PROGRAM STARTS HERE *****
240 Z$=" "
250 GOSUB 5460
260 DIM G[8,8],C[9,2],K[3,3],N[3],Z[8,8]
...
420 C[2,1]=C[3,1]=C[4,1]=C[4,2]=C[5,2]=C[6,2]=-1
430 C[1,1]=C[3,2]=C[5,1]=C[7,2]=C[9,1]=0
440 C[1,2]=C[2,2]=C[6,1]=C[7,1]=C[8,1]=C[8,2]=C[9,2]=1
...
1885 C2=INT(C1)
1890 X1=C[C2,1]+(C[C2+1,1]-C[C2,1])*(C1-C2)
1900 X2=C[C2,2]+(C[C2+1,2]-C[C2,2])*(C1-C2)
...
2700 H=(X/K3/FND(0))*(2*RND(1))
2710 K[I,3]=K[I,3]-H
2720 PRINT USING 2730;H,K[I,1],K[I,2],K[I,3]
2730 IMAGE 4D," UNIT HIT ON KLINGON AT SECTOR ",D,",",D," (",3D," LEFT)"
...
3400 G[Q1,Q2]=K3*100+B3*10+S3
| Variable Name | Data Type | Description |
|---|---|---|
| G | 8×8 Matrix | Galaxy; Elements contain 3-digit values to store number of Klingons, Starbases, and Stars in each of the 64 Quadrants |
| C | 2×9 Matrix | Lookup of Δrow, Δcol for a given course, where 1≤course<9 |
| K | 3×3 Matrix | Klingon attributes; row, col, and hit-points remaining for up to 3 enemies found in current Quadrant |
Strings
270 DIM C$[6],D$[72],E$[24],A$[3],Q$[72],R$[72],S$[48] 280 DIM Z$[72] ... 460 D$="WARP ENGINESS.R. SENSORSL.R. SENSORSPHASER CNTRL" 470 D$[49]="PHOTON TUBESDAMAGE CNTRL" 480 E$="SHIELD CNTRLCOMPUTER" ... 950 Q$=Z$ 960 R$=Z$ 970 S$=Z$[1,48] ... 3790 IF C$ <> "DOCKED" THEN 3820 3800 PRINT "STAR BASE SHIELDS PROTECT THE ENTERPRISE" 3810 RETURN
| Variable Name | Data Type | Description |
|---|---|---|
| C$ | 6 char String | Condition of the Enterprise – GREEN, RED, YELLOW, or DOCKED |
| D$ | 72 char String | First 6 labels for Damage Report – 72 char string treated as array of (6) 12-char strings |
| E$ | 24 char String | 2 more labels for Damage Report – 24 char string treated as array of (2) 12-char strings |
| A$ | 3 char String | Symbol definition for Stars (‘ * ‘), Klingons (‘+++’), or the Enterprise (‘<*>’) |
| Q$, R$, S$ | total 192 char String | Buffer used to display current Quadrant. 8×8 cells where each 3-char cell contains a copy of A$ or empty space |
| Z$ | 72 char String | Zeroed String – 72 spaces used to clear Q$, R$, and S$ |
Enterprise Attributes
4250 C$="DOCKED" 4260 E=3000 4270 P=10 4280 PRINT "SHIELDS DROPPED FOR DOCKING PURPOSES" 4290 S=0
| Variable Name | Description |
|---|---|
| E | Energy available to the Enterprise. Values 0-3000+ |
| S | Energy diverted to the Enterprise’s Shields |
| P | Number of Photon Torpedoes remaining. Values 0-10 |
| Q1,Q2 | Quadrant; row, col of Enterprise’s location within the Galaxy. Values 1-8 each |
| S1,S2 | Sector; row, col for Enterprise’s location within a Quadrant. Values 1-8 each |
Galaxy Attributes
3690 PRINT USING 3700;K[I,1],K[I,2] 3700 IMAGE "KLINGON AT SECTOR ",D,",",D," DESTROYED ****" 3710 K3=K3-1 3720 K9=K9-1 3730 A$=" " 3740 Z1=K[I,1] 3750 Z2=K[I,2] 3760 GOSUB 5510 3770 G[Q1,Q2]=K3*100+B3*10+S3 3780 RETURN
| Variable Name | Description |
|---|---|
| K9 | Total Number of Klingons remaining in the Galaxy. Set at start. Decremented as enemies are destroyed |
| K7 | Total Number of Klingons at start of game. Used to calculate efficiency rating for successful games |
| B9 | Total Number of Starbases remaining in the Galaxy. Set at start. Decremented if any are destroyed |
| S9 | Total Number of Stars in the Galaxy. Set at start to 200 |
Quadrant Attributes
| Variable Name | Description |
|---|---|
| K3 | Number of Klingons in current Quadrant (0-3) |
| B3 | Number of Starbases in current Quadrant (0-3) |
| S3 | Number of Stars in current Quadrant (1-8) |
Time Attributes
4080 PRINT "YOUR EFFICIENCY RATING ="((K7/(T-T0))*1000) 4090 T1=TIM(0)+TIM(1)*60 4100 PRINT "YOUR ACTUAL TIME OF MISSION ="INT((((T1-T7)*.4)-T7)*100)" MINUTES"
| Variable Name | Description |
|---|---|
| T | Current Stardate. Values like 2403 |
| T0 | Initial Stardate when game started |
| T1 | Time at End of Game; Obtained from Real-time Clock |
| T7 | Time at Start of Game; Obtained from Real-time Clock |
| T9 | Time Limit; Number of Stardates you have to complete the game. Set to 30 at start |
Set Course
1410 PRINT "COURSE (1-9):"; 1420 INPUT C1 1430 IF C1=0 THEN 1270 1440 IF C1= 9 THEN 1410 1450 PRINT "WARP FACTOR (0-8):"; 1460 INPUT W1 1470 IF W18 THEN 1410 1480 IF D[1] >= 0 OR W1 <= .2 THEN 1510 1490 PRINT "WARP ENGINES ARE DAMAGED, MAXIMUM SPEED = WARP .2" 1500 GOTO 1410
| Variable Name | Description |
|---|---|
| C1 | Course |
| W1 | Warp Factor |
Up Next
Hopefully a demonstration of STTR1 for the Aquarius