Chapter 8 PAGE 1 STARTREK THE COMPUTER PROGRAM CHAPTER 8 8.1 The TORpedo Command Now that the ship can move around, let us develop the program for one of the weapon systems so that we can destroy the enemy as we travel around the Galaxy. The Photon Torpedo function is chosen first because the software uses many similar functions to that of the Move function. It is in fact possible to combine the subroutines into one complex unwieldy routine if you wish to save memory. For such an example see Appendix . Photon Torpedoes are projectile anti-matter weapons. The Enterprise can carry ten of them at any one time. They are fired at the enemy in battle situations. Should they enter a sector containing a Klingon, they will lock onto the enemy ship and destroy it when they hit it. A Klingon however does have a small probability of dodging the missile. The Federation has superiority in Photon Torpedo technology so that if you hit a Klingon with one, you will always destroy it. On the other hand if a Klingon hits you with a torpedo, the shields will cope with most of the blast and the sub-system closest to the point of impact will suffer damage. It is also possible that the torpedoes will also enter sectors containing starbases or stars. If one hits a star, the star will absorb the torpedo. If one hits a starbase, in real life the result would depend on whether the Base has its shields up or not. If the Starbase had its shields up nothing would happen. If not, some damage would occur including most probably loss of life. In this simulation the Base will be destroyed by the torpedo, a very improbable real life occurrence. However this is a training simulation one object of which is to teach the Starfleet Academy student care in the aiming and firing of weapons. The destruction of the Base by a Photon Torpedo is thus a penalty for making a mistake. If the student happens to destroy the only base in the simulation, (ie. the player destroys the only base in the game), too bad. The Torpedo command function can be described by the flow chart shown in figure 8.1. The PHOTON.TORPEDO function first tests to see if the Torpedo sub-system is usable. If it is damaged a message is displayed to that effect. If it is in working order, the software tests to see that there is at least one available to fire. If none are left a message is displayed to that effect. Assuming that there is a torpedo available. The computer asks the player for the direction in which to fire it. Once that is established a loop begins which lasts as long as the torpedo is in the quadrant and has not hit anything. The torpedo is moved one sector in the desired direction. A test is then performed to see if the torpedo is still in the quadrant. If it has left the quadrant, a "MISSED" message is Copyright (c) Joe Kasser 1989 Chapter 8 PAGE 2 STARTREK THE COMPUTER PROGRAM displayed and the loop terminates. If it is still inside the quadrant, the contents of the new sector are checked, because if the sector is occupied, the torpedo has hit something. If the sector is not occupied, the loop continues. Once the torpedo enters a sector that is occupied by some other object, the routine tests to see which object. If it is a Klingon, and the Klingon is hit, it is destroyed. The Klingon is deleted from the quadrant, the sector and the game status. A message is displayed that the enemy has been destroyed. We also allow a small probability that the Klingon will escape the torpedo. If this does happen, the sector is treated as a blank as far as the passage of the torpedo is concerned. If the torpedo enters a sector containing a star and hits it, the star absorbs the torpedo. If the torpedo does not hit the star it is deflected due to the gravitational field around the star. In either event, a message is displayed accordingly. If the torpedo hits a starbase, it destroys it. If the torpedo returns to the sector containing the Enterprise, a message to that effect is displayed. The BASIC language implementation of the flow chart could take the format shown in figure 8.2. The subroutine begins as usual with a REMark in line 1000. Line 1010 determines if the torpedo tubes are working by testing the torpedo status in the damage control array. The statements 'IF D(I) > 0 THEN PRINT "TUBES BLOCKED : GOTO 1210' first test the I'th element in the D(I) array (Photon Torpedo status). If the contents of the element are greater than zero, then by definition, some time is estimated to pass before the sub-system will be operational. In this case it must be due to the fact that the Torpedo Control in damaged. A "TUBES BLOCKED" message is accordingly displayed on the screen and the program counter is advanced to the last line of the subroutine, namely line 1210. Line 1020 next determines if there are any torpedoes left. The Enterprise can only carry up to ten at a time. At this time, we don't care how many are aboard so line 1020 just tests to see if there are any at all. The number of torpedoes on the ship is stored in the variable 'P'. If the value of 'P' is zero, then there are none on board, and a message is displayed accordingly. The last statement of the line then advances the program counter to line 1210. The dialogue to determine the desired course in which the torpedo is to be fired takes place in line 1300. The statement 'INPUT "DIRECTION (1-9) ";C' both prompts the player to supply the direction and indicates the range expected for the reply. This is another example of "friendly' prompting. The reply is assigned to the variable 'C'. The second statement on the line 'IF C<1 OR C>9 THEN 1030' verifies that the input was within the desired range of less than Copyright (c) Joe Kasser 1989 Chapter 8 PAGE 3 STARTREK THE COMPUTER PROGRAM one (<1) or greater than nine (>9). If the value of 'C' is outside that range, the program line counter is reset back to line 1030 to phrase the question again. Line 1040 sets up the vertical and horizontal vectors of the direction of travel. The row co-ordinate is assigned to the variable 'Y1' and the column co-ordinate to 'X1' just as in the MOVe operation. This is done by the statements 'Y1=S1+.5 : X1=S2+.5' which set the co-ordinates of the sector containing the torpedo equal to that of the co-ordinates of the sector containing the Enterprise. The numerical direction is converted to an angle by the statement 'Y=(C-Z)*.785398' and the row and column vectors of that direction are determined by the statements 'X=COS(Y) : Y=-SIN(Y)'. The last item on the line displays the heading for the tracking display. Line 1050 begins by decrementing the number of torpedoes aboard the ship using the statement 'P=P-Z'. The loop that moves the torpedo through the quadrant is then begun using a FOR/NEXT construction by the statement 'FOR J=0 TO E0'. The parameter E0 or 4000 is used as the upper loop limit to give the torpedo plenty of chance to bounce around the quadrant and be deflected off a number of stars. The intermediate co-ordinates (Y1 and X1) are then computed by adding the row and column vectors to their last value, and these intermediate values are converted to their integer versions (Y2 and X2) in the same manner as the Enterprise was moved across the Quadrant, by the statements 'Y1=Y1+Y : X1=X1+X : Y2=INT(Y1) : X2=INT(X1)'. Line 1060 tests the new co-ordinates of the torpedo to see if it is outside the sector using the now familiar statement 'IF X2<0 OR X2>7 OR Y2<0 OR Y2>7 THEN '. If the torpedo is outside the quadrant, it has missed its target and a message to that effect is displayed by the statement 'PRINT "MISSED" ', and the program counter is advanced to line 1210 bypassing the rest of the subroutine. If the torpedo is still inside the quadrant, line 1070 displays the co-ordinates using the statement 'PRINT Y2+Z;",";X2+Z' before determining if something has been hit. The contents of the sector that the torpedo has just entered are examined by the 'ON' statement in line 1070. The integer values of the contents of the sector array S(I,J) represent the contents of the sector where an empty or blank sector is represented by the number one, a two represents a star, if it contains a three, that means that the Enterprise is located in the sector. A four identifies the presence of a Klingon and if there is a starbase in the sector, its presence is indicated by the number five. The 'ON' statement can thus be used to vector the program line to the required line to process the events associated with the torpedo hitting something. Copyright (c) Joe Kasser 1989 Chapter 8 PAGE 4 STARTREK THE COMPUTER PROGRAM At line 1080 the torpedo has entered a sector containing a star. The first thing it does is display that fact using the statement 'PRINT "STAR "; '. Notice the space character inside the quotation marks and the semi-colon after the quotation marks which inhibits the cursor from advancing to the next line. When a torpedo enters a sector containing a star, one of two things happen. The torpedo can hit the star and be absorbed, or it can pass close to the star and be absorbed by it. Which of the two events is determined purely at random by the statement 'IF RND(Z)<.5'. In other ways if a random number lying between 0 and 0.99 is less than 0.5 the test passes and the star is deemed to have absorbed the torpedo. A message to that effect is then displayed and the program counter advanced to the last line of the subroutine. On the other hand, if the test failed, a message stating that the star has deflected the torpedo is displayed by line 1090, which then goes on to set up a random direction and its vertical and horizontal vectors before bypassing the remainder of the loop by advancing the program counter to line 1200 using the statements 'Y=Z+RND(Z)*8*.785398 : X=COS(Y) : Y=-SIN(Y) : GOTO 1200' If the torpedo has entered a sector occupied by a Klingon, there is a probability that the Klingon can evade the torpedo. A random number is again selected using the built in RND(Z) function. There is a ten percent probability that the Klingon will evade the torpedo, or a ninety percent probability that it will hit the enemy ship. Line 1100 takes care of the evasion occurrence by using the statement 'IF RND(Z)>.9 THEN 1200'. On the other hand if the Klingon was hit, line 1110 displays that event using the statement 'PRINT " KLINGON DESTROYED" '. Having destroyed a Klingon, the particular Klingon in the quadrant has to be identified. This is done by the FOR/NEXT loop of lines 1120 and 1130. As there can only be up to eight enemy ships in the quadrant, the loop limits are 0 and 7. The loop tests the position co-ordinates of each Klingon (K1(I) AND K2(I)) to see if they are identical to those of the sector that the torpedo has entered. The statement 'IF Y2=K1(I) AND X2=K2(I) THEN 1140' does the job, advancing the program counter out of the loop to line 1140 when a match is found. The loop should only terminate normally when something goes wrong. An error message to that effect is displayed by the last part of line 1130. This should never happen when the game is played. Line 1140 deletes the Klingon from the quadrant by setting its energy level to zero. The energy level of the Klingon K3(I) is used as a flag that states if the Klingon is active or not. If the Klingon has positive energy it is alive and can hurt you. Once the energy/active flag has been cleared, the Klingon must be deleted from the simulation. The subroutine starting at line 1220 is then called to perform that operation and the program counter is advanced to line 1180. If the torpedo has entered a sector containing a starbase, Copyright (c) Joe Kasser 1989 Chapter 8 PAGE 5 STARTREK THE COMPUTER PROGRAM the program picks up at line 1150 by displaying a sarcastic message. The number of bases in the quadrant (B) and the number of bases in the galaxy are then decremented, and the values of K8 and T9 are then reset to penalize the score by reducing the size of the multiplier. The contents of the quadrant are contained in the quadrant Q(I,J) array. The starbase is represented by the tens digit. To delete the base from the quadrant, the tens digit must be adjusted. The sign of the contents of the element in the array will depend on whether the quadrant has been scanned into the computer or not. Remember, the Enterprise can be operating in a quadrant, whose contents are not entered into the galactic map because either the computer is down or the relevant sensors have been damaged and are not working. Anyhow, if the sign of the quadrant is negative (unscanned), the a value of ten has to be added to the value stored in the Q(I,J) array, and if the sign of the quadrant is positive (scanned) a ten is subtracted from the contents of the quadrant. Line 1160 performs this operation using the statement IF Q(Q1,Q2)<0 THEN Q(Q1,Q2)=Q(Q1,Q2)+10 ELSE Q(Q1,Q2)=Q(Q1,Q2)-10. If the player has destroyed the last base in the game, the value of B9 will be zero. Line 1170 detects that condition and displays a nasty message to that effect. Line 1180 sets the contents of the sector to a blank using the statement 'S(Y2,X2)=Z' and then advances the program counter to line 1210. Meanwhile if the wandering torpedo has been deflected by a star and returns to the sector occupied by the Enterprise, line 1190 displays a message to that effect. The loop counter is incremented in line 1200 and the next iteration is allowed to take place. The subroutine then ends at line 1210. In the unlikely event that the J loop ever times out and the loop exits in the normal way, the subroutine will still terminate in the normal manner. If you like you could put a message to the effect that the photon torpedo has run out of steam and the loop has terminated at the end of line 1200. The subroutine that clears the Klingon from the simulation begins at line 1220 with the usual comment. A subroutine is used to do the job, because it is done in both the torpedo and phaser functions as well as possibly any other functions to be identified later. Using a subroutine ensures that the job is done identically and correctly by the same lines of code whenever the job is to be performed. Line 1230 begins by decrementing the number of Klingons in both the quadrant (K) and the galaxy (K9). Remember that the value stored in K9 is the number of Klingons in the galaxy multiplied by a hundred. If the value of K9 has reached zero, then all the Klingons in the game have been wiped out and the game can end. The game-over flag (F9) is then set to five signifying that the player has won the game. Line 1240 then deletes the Klingon from the quadrant array by subtracting a hundred from the value stored in the quadrant array associated with the quadrant that the action is taking place. The exact mathematical operation performed depends on the sign of the Copyright (c) Joe Kasser 1989 Chapter 8 PAGE 6 STARTREK THE COMPUTER PROGRAM quadrant, namely if it has been scanned into the ship's computer or not in a similar manner to line 1160 using the statement IF Q(Q1,Q2)<0 THEN Q(Q1,Q2)=Q(Q1,Q2)+100 ELSE Q(Q1,Q2)=Q(Q1,Q2)-100. The subroutine ends with the 'RETURN' statement in line 1250. Copy lines 1000 to 1250 from figure 7.7 into your program and the newest version. Debug the program by moving around the galaxy to locate the enemy. Shoot at everything as you move, note how the score goes up as you kill Klingons and then down as time passes. Test what happens when you hit Starbases and stars. Is the torpedo deflected by stars, what happens when it comes back into the sector occupied by the Enterprise? In other words, test every possible condition in which the torpedo hits something, or every branch in the flowchart. When you have finished and all the "bugs" are out of the program save the updated debugged version and read on. 8.2 The PHASERS Phasers are offensive energy weapons. They are locked onto targets automatically, and subject the target to an intense burst of energy. They can only be used to attack enemy ships. Anything else in the quadrant will not be harmed by the use of the Phasers. The flow chart for the Phaser function is shown in figure 8.3. The first thing performed is a test for the presence of Klingons in the Quadrant. If there are no Klingons in the quadrant, then by definition there is no target in the quadrant and a message to that effect is displayed. If at least one target is present, then the state of repair of the Phaser Banks is checked. If the phasers are not operational they cannot be used and a message to that effect is displayed. If the phasers are usable, then the state of the Short Range sensors has to be checked, because if they are damaged, the phasers cannot be locked on to the target and Mr. Sulu will have to use a manual 'best guess' technique which of course is not as accurate as using the computer. In that condition a message is displayed to that effect. Energy is next allocated to the phaser banks by the player. Phaser control then equally divides that amount of energy by the number of active targets in the quadrant and fires a burst at each Klingon. The amount of energy hitting the enemy's shields is computed. The hit is deducted from the shield energy of the Klingon, and a message is displayed stating that a 'hit' took place. If the shields were beaten down to less than zero, the Klingon has been destroyed and is deleted from the quadrant and the game status parameters are updated. If the shields were just beaten down to a low value, the Klingon captain may decide that his position is hopeless and self-destruct or even surrender. If the Klingon self-destructs, it must be boarded before being deleted from the game. If it self-destructs it must also be deleted from the game. A bonus score is awarded in either case, to compensate the player for being damaged by the Klingon while Copyright (c) Joe Kasser 1989 Chapter 8 PAGE 7 STARTREK THE COMPUTER PROGRAM trying to get it to surrender or self-destruct. If the Klingon can still fight on after the hit, the loop finds the next active Klingon target in the Quadrant and tries again. The BASIC language implementation of the flowchart can take the form shown in figure 8.4. It begins with the usual REMark statement in line 800. Line 810 then checks the number of Klingons in the quadrant. If there are none present, by definition there are no targets and a message is displayed accordingly. Line 820 then tests the state repair of the Phasers. If they are damaged, represented by a positive ETR value in the Damage Control / Command array 'D(I)', a message stating that they are "OUT OF ACTION " is displayed and the remainder of the subroutine is bypassed by the 'GOTO 950' statement which advanced the program line counter to line 950. If the Short Range Sensors are not working, then the computer cannot lock the phasers onto target so the program has to determine if they are operational. Line 830 thus looks at their state of repair, represented by the '1' element in the Damage Control / Command Array in the usual way. If they are damaged, a message to that effect is displayed, and Mr. Sulu announces that he will use his best guess to locate the targets. The statement used to display the message combines two strings in the same "PRINT' statement as follows 'PRINT D$(1);"are not working, will use best guess settings" '. The D$(I) string array contains the names of all the Commands or sub-systems. The program has been laid out so that they are the same. D$(1) is thus 'SHORT RANGE SENSORS'. The semi colon then ensures that the cursor stays in place after the string is displayed, and the rest of the message contained in the string constant is then displayed. The dialogue to allow the player to tell the computer how much energy to allocate to the phasers begins in line 840. The statements 'INPUT "READY- how much energy do you want to fire "; X : IF X<=0 THEN 950' prompt the player for an input while indicating what exactly is to be input. The input is assigned to the temporary variable 'X', and then checked to ensure that is has a positive value. If it does not have a positive value, the program counter is advanced to line 950. The player can thus enter a zero in response to the prompt to bypass the phaser firing operation. Line 850 then makes sure that the ship has enough energy on board to allow the phasers to fire it. There is no point in allocating 500 units of energy to the phaser banks if the Enterprise only has 400 units at that particular time. If the player tries it on, line 850 catches it and displays a message to that effect. The last statement on the line redirects the program counter to the prompt statement in line 840 so the player can enter a correct value. It is possible at this time to display the actual amount of energy (E) remaining on the ship to tell the player, but this version does not so as to penalize the player. The test for available energy is 'IF X>(E-E1) ' because the amount of energy to be fired must be less than the total Copyright (c) Joe Kasser 1989 Chapter 8 PAGE 8 STARTREK THE COMPUTER PROGRAM amount of ship's energy excluding the amount in the Shields (represented by the variable 'E1'). Line 860 starts off by subtracting the amount of energy to be fired from the ship's power. The number of Klingons is then stored in a temporary variable (Y). This is done because when firing the phasers in line 880, the amount of energy that was allocated is divided by the number of Klingons in the quadrant. If line 880 used 'K' to represent the number of Klingons, what happens when one is destroyed in the middle of a round of fire ? It is thus mandatory to use a temporary variable to keep the number of Klingons the same at the end of the round as at the beginning, at least as far as the firing sequence is concerned. The decrementing of the number of Klingons in the quadrant is performed in the subroutine beginning in line 1220 in case you are searching for it in the listing in figure 8.4 and haven't found it. A loop is then begun to locate and shoot at each Klingon in the Quadrant. The 'FOR/NEXT' construct is used. The limit on the loop are 0 and 7 because there can only be, by definition, eight enemy ships in the quadrant, and their parameters are stored in the arrays K1(I), K2(I) and K3(I). The last statement on the line tests the amount of energy allocated to the Klingon. If it is positive, the Klingon is active and the test fails, the program flow continuing on the next sequential line. If the Klingon has negative or zero energy, it is not active and the program counter is branched forward directly to the NEXT statement of line 940. Line 870 then samples the state of repair of the Short Range Sensors represented by the 1'th element in the Damage Control / Command Array D(I). Note this is really the 1'th element, not the first element, because the first element is D(0) which represents the state of repair of the engines. If the Short Range Sensors are damaged, Mr. Sulu will have to use his best guess. The statement 'X = X*RND(Z)' performs that operation. A random number is generated by the random number generator built into BASIC using the 'RND(1)' function. The result is then multiplied by 'X' to give a random percentage of the original value of 'X'. This random percentage is then stored back in 'X' and is used as the basis for determining the amount of the hit. Mr. Sulu's best guess has thus been implemented using the random number generator. The actual amount of energy that hits each enemy is computed in line 880 by the statement 'H = X/(Y*(SQR((K1(I)-S1)^2+(K2(I)-S2)^2)^.4))' which divides the amount of energy to be fired by the number of Klingons in the quadrant (Y) multiplied by a factor representing the distance between the Enterprise and the particular Klingon. The distance between the Enterprise and the Klingon is calculated using Pythagoras's theorem. The last statement of the line subtracts the amount in the hit from the amount of energy in the Klingon. The array variables K1(I) contains the row co-ordinates of the Klingon, corresponding to the S1 co-ordinates of the Enterprise. K2(I) contains the column co-ordinates and K3(I) contains the energy quotient of the Klingon. The first part of line 890 displays a message announcing that a hit was achieved on a Klingon and adds the co-ordinates of Copyright (c) Joe Kasser 1989 Chapter 8 PAGE 9 STARTREK THE COMPUTER PROGRAM the Klingon. Since the computer stores the co-ordinates in the range 0 to 7, while the player reads them as from 1 to 8, the conversion is contained in the PRINT statement by the 'K1(I)+Z;",";K2(I)+Z;' which also formats the display. The last part of the line displays the amount of energy left in the Klingon if the Short Range Sensors are working. The format of the statement used is 'IF D(1)=0 THEN PRINT "(";K3(I);"Units left)" ELSE PRINT' because, if the sensors are damaged, the PRINT following the ELSE advances the cursor to the next line without displaying any message. Other versions of the game indicate the amount of the hit on the Klingon. This can be achieved by adding 'H;"Unit' in front of the word 'Hit' on line 890. If you do that, then the amount of the hit will be displayed whatever the state of the Short Range Sensors. That is not "realistic", thus if you want the amount of the hit displayed, you may change line 890 to take the state of those sensors into account. Line 900 first tests to see if the Klingon was destroyed. It detects that the Klingon was destroyed by checking to see if it has any energy left. If it does not have any, then by definition it has been destroyed and the program continues along the line. If the test failed, the program counter advances to the next line number in sequence. In the event that the Klingon was destroyed, a message stating that fact is displayed. The contents of the element in the sector array that contained the Klingon are then rest to reflect a blank sector by the statement 'S(K1(I),K2(I)) = Z'. The subroutine starting at line 1220 is invoked to completely clear the Klingon from the game and the program counter is advanced to line 940 to increment the loop counter. If the Klingon was not destroyed the program continues at line 910 which decides if the Klingon is going to self destruct or surrender. The line thus starts with a test to determine if the Klingon is alone in the quadrant (IF K>Z) or if the Klingon has only a small amount of energy left (K3(I)>E1/100). The test to determine of the Klingon is alone is a simple test to see if the value of 'K' is greater than 1. If it is, then obviously there are more than one Klingons in the quadrant, and the target is not alone. The energy test compares the amount of energy that the Klingon has with the amount of energy in the Shields of the Enterprise. If the Klingon has more that a hundredth fraction of the energy in the shields of the Enterprise, it is deemed to have sufficient to fight on. The last part of the test also adds a random probability of sixty percent that the Klingon will choose to fight on even if the other conditions are met. Note that the test is phrased 'RND(Z)>.4' which means that the test fails if the probability is less than forty percent and the test in this line passes if the Klingon is to fight on. The random number generator is used here to determine the probability of an event occurring. The random number generator returns a random number between 0 and 0.99. Comparing that number with a preset threshold level can thus provide a probability level saying that an event will only happen if the number is greater or less than some value. In this case, the Klingon fights on if any one of the Copyright (c) Joe Kasser 1989 Chapter 8 PAGE 10 STARTREK THE COMPUTER PROGRAM tests in line 910 pass and the program counter is advanced to line 940 bypassing the surrender/self destruct section of the subroutine. The Klingon has to be removed from the game in the event that it self destructs or fights on. Line 920 performs that operation. the first statement on the line clears the Klingon's energy. The second clears the Klingon from the quadrant by setting the co-ordinates of the sector it is in to the blank state. The subroutine starting at line 1220 is then invoked to clear the Klingon from the game. The line then continues by using the random number generator to determine if the Klingon has surrendered or destroyed itself. There is a seventy percent probability that the Klingon captain will decide to destroy his ship rather than be captured. Klingons as a rule don't surrender and rarely take prisoners. The probability of them surrendering is thus small. It happens from time to time because the captain and the officers may be killed, or the self destruct mechanism or other parts of the ship may be damaged or for any one of a number of reasons. In any event, this simulation sets the probability of surrender at about thirty percent. If the Klingon did not surrender, the last part of line 920 is performed. A message is displayed stating that the Klingon exploded. There is no need to test the state of the Short Range Sensors at this time because the explosion of a Klingon ship is so powerful that it can be detected by visual means alone. The number of Klingons that have committed suicide represented by the value stored in the variable 'K5' is incremented and the program counter is skipped forwards to line 940. The variable 'K5' is used to provide a reduced bonus on the player's score because the player usually suffers damage trying to get the Klingon to surrender and should be compensated accordingly. It is much easier to wipe out the Klingon completely than to try and make it surrender. In the event that the Klingon surrenders line 930 displays a message to that effect. The number of Klingons that have surrendered represented by the variable 'K4' is incremented. If a Klingon surrenders it provides a multiplied on the player's score. A time penalty is thus inserted at the moment of capture. It takes half a stardate to board the enemy, secure the prisoners survey the ship, transfer a prize crew and take care of any other outstanding matters. During this time however, Damage control have not been idle and have been busily working away on any sub- systems damaged during the battle. Line 930 thus first sets the temporary time variable 'T1' to the value of the time 'T'. It then subtracts half a stardate from that time and calls the subroutine starting at line 1800 to update the state of repair of the ship after the passage of half a stardate. Line 940 reiterates the loop counter and line 950 terminates the function. Carefully copy lines 800 to 950 to your program and save the latest version. Debug them by using the Phasers in all possible conditions. Try firing them with no enemy ships in the quadrant. Copyright (c) Joe Kasser 1989 Chapter 8 PAGE 11 STARTREK THE COMPUTER PROGRAM Try to get the Klingon to surrender or self-destruct. If you have a problem getting an event to occur, change the probability temporarily so that you can debug it properly. When it is completely debugged save it again and read on. 8.3 The SHIELDS Command The Shields command allows the player to set up the amount of energy in the Shields of the Enterprise. The shields protect the Enterprise against the effects of Klingon phaser blasts as well as Klingon Space Mine explosions. If the shields are damaged, they will function normally but the player will not be able to change the amount of energy allocated to the shields. The flow chart for the SHIELDS function is straight forward and is shown in figure 8.5. It begins with a test to determine if the shields are damaged. If they are, a message to that effect is displayed and the routine terminates. If they are not, some dialogue takes place to determine the amount of energy to be allocated to the shields. The player can lower the shields by allocating zero units or can raise the shields to the full amount of energy carried by the Enterprise. The usual value set in the shields at any one time is about 400 units. If the player tries to allocate more energy than the Enterprise has, an error message will be displayed. Once a valid amount of energy has been allocated, it is put into the shields, and the sequence ends. The BASIC language implementation of the Shields function could be as shown in figure 8.6 with the usual REMark statement. Line 2710 begins the action by testing the state of the 'docked' flag (C$). If the Enterprise is docked, the player is not allowed to raise the shields by definition. The shields are lowered automatically by the docking process. there is thus no further need to raise the shields while the Enterprise is docked at a starbase. Operationally this means that when un-docking, the player must move away from the base using impulse power (at a speed of less than Warp One), then raise the shields by putting some energy into them before changing to warp speed to depart for distant quadrants. The last statement on the line advances the program line counter to the end of the subroutine. Line 2720 tests the state of the shield element in the Damage Control / Command Array (D(I)). If they are damaged, a message to that effect is displayed by the PRINT D$(I)+"DAMAGED" statement before the program counter is advanced past the rest of the subroutine by the ' GOTO 2770' statement. If the Shields are fully operational, line 2730 displays that fact by generating a message notifying the player as to how much energy is allocated to the shields at that particular time. Notice that line 2720 uses a plus sign to separate the two elements to be printed, while line 2730 uses a semi-colon. Line 2730 first displays D$(I) which in this instance is "SHIELDS " and then displays the following items. There are four things being displayed in the line. The contents of the element in the Command String Array D$(I), the string constant, the contents of the variable 'E1' Copyright (c) Joe Kasser 1989 Chapter 8 PAGE 12 STARTREK THE COMPUTER PROGRAM which contains the amount of energy in the shields, and lastly, the string constant "Units". Line 2720 on the other hand contained the statement 'PRINT D$(I)+"DAMAGED" ' which added two separate strings together at the same time as they were printed. The statement could just as well have been written as 'PRINT D$(I);"DAMAGED" '. The dialogue to allocate the energy to the shields begins in line 2740. Again the 'INPUT' statement is used together with a prompt message telling the player what is expected as the reply. The number of energy units to be allocated to the shields is temporarily stored in the variable 'N' until it has been validated. The first validation test is performed at the end of line 2740. It ensures that the amount of energy to be allocated to the Shields is not a negative value. If the player input a negative value the last item on line 2470 ensures that the question is asked once again. this technique is known as 'trapping'. The negative response is trapped at this time and blocked here before it can cause trouble later. An alternative action would have been to state 'IF N<0 THEN N=0' which would also trap negative values but in a different way. The alternative ensures that all negative values are converted to zero values. The method used here inhibits negative values, and uses the zero value to signify that the shields have been lowered. The second validation test is on line 2750. It ensures that there is enough energy in the dilithium crystals aboard the Enterprise to allocate the desired amount to the shields. If the amount of energy on the starship is less than the amount the player wants to allocate to the shields, then a nasty message to that effect is displayed and the question is asked again. Once the entry has been validated, the contents of the variable 'N' are copied into the variable 'E1' which represents the contents of the shields by the statement on line 2760 and the subroutine terminates at line 2770. If the temporary variable was not used to store the desired allocation of shield power before validation the previous amount of energy in the shields would have been lost when the erroneous entry was input. In this application that would not have mattered, because the routine is not allowed to terminate until a valid amount of energy is allocated to the shields. In other cases however, that construction may not hold true and the use of a temporary variable is mandatory. Copy the lines listed in figure 8.6 into your program and debug them by trying the Shields (SHE) command. Try to allocate more energy than the Enterprise has. Try less, Damage the shields and try again. Test all possibilities. When you are satisfied that the shields are operational, save the program and read on. Copyright (c) Joe Kasser 1989