  ON BREAK GOSUB quit:BREAK ON
  WINDOW CLOSE 1
  WINDOW 2,"STAR TREK AMIGA",,20,-1

  DEFINT a-z
  FOR i=1 TO 4:MENU i,0,0,"":NEXT
  OPTION BASE 1
  IF NOT debug THEN GOSUB intro1
  DEF FNd(d)=SQR((klingon(i,1)-shipx)^2+(klingon(i,2)-shipy)^2)
  DIM galaxy(8,8),record(8,8),quadrant(8,8)
  DIM dx(9),dy(9),moves(10,2),klingon(3,3)
  DIM damage!(8),damage$(8)
  DIM saw(256),chop(256),boom(256)
  DIM eprs(35),klon(35),home(35),torp(35),star(35),bng1(35),bng2(35),bng3(35)
  DIM rgb(4,3)
  temp&=PEEKL(PEEKL(PEEKL(WINDOW(7)+46)+48)+4)
  FOR i=1 TO 4
    msg$=RIGHT$("00"+HEX$(PEEKW(temp&+2*i)),3)
    FOR j=1 TO 3:rgb(i,j)=VAL("&h"+MID$(msg$,j,1)):NEXT
  NEXT
  PALETTE 0, 2/16, 4/16, 9/16
  PALETTE 1,15/16,15/16,15/16
  PALETTE 2, 0/16, 0/16, 2/16
  PALETTE 3,15/16, 8/16, 0/16
  enterprise=30000
  fullenergy=3000
  fullphoton=10
  dx(1)= 1:dy(1)= 0
  dx(2)= 1:dy(2)=-1
  dx(3)= 0:dy(3)=-1
  dx(4)=-1:dy(4)=-1
  dx(5)=-1:dy(5)= 0
  dx(6)=-1:dy(6)= 1
  dx(7)= 0:dy(7)= 1
  dx(8)= 1:dy(8)= 1
  dx(9)= 1:dy(9)= 0
  FOR i=1 TO 256
    saw(i)=i-129
    IF i<129 THEN chop(i)=127 ELSE chop(i)=-128
    boom(i)=255*RND-128
  NEXT
  WAVE 2,saw
  WAVE 3,chop
  RESTORE quit
  OPEN "grfx" FOR INPUT AS #1
    FOR i=1 TO 35:INPUT #1,eprs(i):NEXT
    FOR i=1 TO 35:INPUT #1,klon(i):NEXT
    FOR i=1 TO 35:INPUT #1,home(i):NEXT
    FOR i=1 TO 35:INPUT #1,torp(i):NEXT
    FOR i=1 TO 35:INPUT #1,star(i):NEXT
    FOR i=1 TO 35:INPUT #1,bng1(i):NEXT
    FOR i=1 TO 35:INPUT #1,bng2(i):NEXT
    FOR i=1 TO 35:INPUT #1,bng3(i):NEXT
  CLOSE #1
  FOR i=1 TO 8:READ device$(i):NEXT
  command$="NAVSRSLRSPHATORSHIDAMCOMRESFIX"
  command2$="GALREGHISBASKLIDIR"
  quad1$="Antares    Rigel      Procyon    Vega       "
  quad1$=quad1$+"Canopus    Altair     SagittariusPollux     "
  quad2$="Sirius    Deneb     Capella   Betelgeuse"
  quad2$=quad2$+"Aldebaran Regulus   Arcturus  Spica     "
begin:
  IF NOT debug THEN GOSUB intro2
  RANDOMIZE TIMER
  thisdate!=100*INT(20*RND+20)
  startdate=thisdate!
  enddate=INT(10*RND)+25
  energy=fullenergy
  photon=fullphoton
  quadx=INT(8*RND)+1
  quady=INT(8*RND)+1
  shipx=INT(8*RND)+1
  shipy=INT(8*RND)+1
  docked=0
  shields=0
  FOR i=1 TO 8:damage!(i)=0:NEXT
  totalbases=0
  totalklingons=0
  FOR i=1 TO 8
    FOR j=1 TO 8
      klingons=-(RND>.8)-(RND>.8)-(RND>.8)
      totalklingons=totalklingons+klingons
      IF RND>.96 THEN starbases=1:totalbases=totalbases+1 ELSE starbases=0
      galaxy(i,j)=100*klingons+10*starbases+INT(8*RND)+1
      record(i,j)=0
    NEXT
  NEXT
  IF totalklingons>enddate THEN enddate=totalklingons+1
  IF totalbases=0 THEN
    IF galaxy(quadx,quady)<200 THEN
      galaxy(quadx,quady)=galaxy(quadx,quady)+100
      totalklingons=totalklingons+1
    END IF
    totalbases=1
    galaxy(quadx,quady)=galaxy(quadx,quady)+10
    quadx=INT(8*RND)+1
    quady=INT(8*RND)+1
  END IF
  numshot=totalklingons
  IF NOT debug THEN GOSUB intro3
  GOSUB drawscreen
  GOSUB newquadrant

dock:
  docked=0
  FOR i=shipx-1 TO shipx+1
    FOR j=shipy-1 TO shipy+1
      IF i>0 AND i<9 AND j>0 AND j<9 THEN
        IF quadrant(i,j)=1 THEN
          i=shipx+1
          j=shipy+1
          docked=-1
          energy=fullenergy
          photon=fullphoton
          shields=0
          CALL prtmsg ("Shields dropped for docking, please remember to raise them, sir!")
        END IF
      END IF
    NEXT
  NEXT

doinfo:
  IF shields+energy<=10 OR (energy<=10 AND damage!(7)<>0) THEN
    CALL prtmsg("*** FATAL ERROR ***"):delay 3
    GOSUB redalert
    CALL prtmsg("You've just stranded your ship in space."):delay 4
    CALL prtmsg("You have insufficient maneuvering energy..."):delay 3
    CALL prtmsg("...and shield control is incapable of cross-circuiting to engine room!!"):delay 3
    CALL prtmsg("It is stardate"+STR$(10*INT(thisdate!/10))+".  The Enterprise is gone."):delay 3
    GOTO newgame
  END IF
  COLOR 1,0
  LOCATE 3,17:PRINT USING "####.##";thisdate!
  LOCATE 4,17:PRINT USING "####.##";enddate+startdate-thisdate!
  LOCATE 5,17:PRINT USING "####";totalbases
  LOCATE 6,17
  IF klingons>0 THEN
    COLOR 2,3:PRINT "  RED  "
  ELSEIF energy<fullenergy/10 THEN
    COLOR 3:PRINT "YELLOW "
  ELSE
    PRINT " GREEN "
  END IF
  COLOR 1,0
  LOCATE 7,17:PRINT quadx","quady
  LOCATE 8,17:PRINT shipx","shipy
  LOCATE 9,19:PRINT USING "##";photon
  LOCATE 10,17:PRINT USING "####";energy+shields
  LOCATE 11,2
  IF shields<200 AND klingons>0 THEN
    PRINT "  Shields LOW: ";
  ELSE
    PRINT "      Shields: ";
  END IF
  PRINT USING "####";shields
  LOCATE 12,18:PRINT USING "###";totalklingons
  IF klingons>0 THEN GOSUB redalert
  CALL zoom(194,103,400,111,2)
  COLOR 3,2:LOCATE 14,28:PRINT "Command Please:  ___"
  msg$="___"
mainloop:
  i=15
  key$=""
  WHILE key$=""
    key$=UCASE$(INKEY$)
    IF i>600 THEN
      LINE (i,10)-STEP(9,0),0
      i=15
      thisdate!=thisdate!+.1
      COLOR 1,0
      LOCATE 3,17:PRINT USING "####.##";thisdate!
      LOCATE 4,17:PRINT USING "####.##";enddate+startdate-thisdate!
    ELSE
      i=i+5
      LINE (i-5,10)-STEP(4,0),0
      LINE (i,10)-STEP(4,0),1
      IF enddate+startdate=INT(thisdate!) THEN
        CALL prtmsg("You have run out of time for completing you mission."):delay 3
        CALL prtmsg("The Klingons have just overrun Federation Headquarters!!!"):delay 3
        CALL prtmsg("All is lost."):delay 3
        GOTO newgame
      END IF
    END IF
  WEND
  LINE (i,10)-STEP(9,0),0
  IF key$=CHR$(27) THEN
    GOTO quit
  ELSEIF key$=CHR$(8) THEN
    msg$=RIGHT$("___"+LEFT$(msg$,2),3)
  ELSEIF key$=CHR$(13) OR key$=CHR$(139) THEN
    WINDOW 3,"Command Summary-Any Key to Close",(185,50)-(185+256,50+80),0,-1
    COLOR 3,2:CLS
    PRINT " NAV = set course"
    PRINT " SRS = short range sensor scan"
    PRINT " LRS = long range sensor scan"
    PRINT " PHA = fire phasers"
    PRINT " TOR = fire photon torpedoes"
    PRINT " SHI = raise (or lower) shields"
    PRINT " DAM = damage control reports"
    PRINT " COM = call on library-computer"
    PRINT " RES = resign your command"
    PRINT " FIX = fix and redraw screen";
    COLOR 1,0
    CALL whoa
    msg$="___"
    WINDOW CLOSE 3
  ELSEIF INSTR(command$,key$)>0 THEN
    msg$=RIGHT$("___"+msg$+key$,3)
  END IF
  COLOR 3,2:LOCATE 14,45:PRINT msg$
  temp=0
  FOR i=0 TO 9
    IF msg$=MID$(command$,3*i+1,3) THEN temp=i+1:i=9
  NEXT
  IF temp=0 THEN mainloop
  ON z GOSUB znav,zlrs,zpha,ztor,zshi,zdam:z=0
  CALL zoom(194,103,400,111,0)
  ON temp GOTO nav,srs,lrs,pha,tor,shi,dam,com,res,cle

znav:
  CALL zoom(408,119,627,135,0)
  RETURN
nav:
  CALL zoom(408,119,627,135,2):z=1
  COLOR 1,2:LOCATE 16,53:PRINT "Enter course (1-9) ->":ding
  course!=0
  WHILE course!<1 OR course!=>9
    CALL navnum(75,16,course!)
    IF course!<1 OR course!=>9 THEN CALL prtmsg ("Lt. Sulu reports: 'Incorrect course data, sir!'")
  WEND
  IF damage!(1)<0 THEN a$=".2)" ELSE a$="8) "
  COLOR 1,2:LOCATE 17,53:PRINT "Warp factor  (0-"+a$+"->":ding
  warp!=9
  WHILE warp!=>9
    CALL navnum(75,17,warp!)
  WEND
  IF warp!=0 THEN doinfo
  IF damage!(1)<0 AND warp!>.2 THEN
    CALL prtmsg("Warp engines are damaged. Maximum speed = warp 0.2")
    GOTO doinfo
  END IF
  IF warp!=>8 THEN
    CALL prtmsg("Chief Engineer Scott reports: The engines won't take warp"+STR$(warp!)+"!"):delay 3
    GOTO doinfo
  END IF
  navnrgy=CINT(8*warp!)             'navnrgy=energy used by navigation
  IF energy-navnrgy<0 THEN
    CALL prtmsg("Engineering reports: Insufficient energy for maneuvering at warp"+STR$(warp!)+"!")
    IF shields=>navnrgy-energy AND damage!(7)=>0 THEN
      CALL prtmsg("Control room acknowledges:"+STR$(shields)+" UNITS OF ENERGY TO SHIELDS.")
    END IF
    GOTO doinfo
  END IF
  fix1=0
  FOR i=1 TO 8
    IF damage!(i)>=0 THEN
      damage!(i)=0
    ELSE
      damage!(i)=damage!(i)-(warp!=>1)-warp!*(warp!<1)
      IF damage!(i)<0 THEN
        IF damage!(i)>-.1 THEN damage!(i)=-.1
      ELSEIF NOT fix1 THEN
        fix1=-1
        CALL prtmsg ("DAMAGE CONTROL REPORT: "+device$(i)+" Repair completed."):delay 3
      END IF
    END IF
  NEXT
  IF RND<.2 THEN
    rnd1=INT(8*RND)+1
    IF RND<.6 THEN
      damage!(rnd1)=damage!(rnd1)-5*RND+1
      CALL prtmsg ("DAMAGE CONTROL REPORT: "+device$(rnd1)+" damaged"):delay 3
      IF rnd1=2 THEN GOSUB zsrs:srsflag=0
    ELSE
      damage!(rnd1)=damage!(rnd1)+3*RND+1
      CALL prtmsg ("DAMAGE CONTROL REPORT: "+device$(rnd1)+" State of repair improved."):delay 3
    END IF
  END IF
  quadrant(shipx,shipy)=0
  newx=shipx
  newy=shipy
  x!=newx
  y!=newy
  temp=INT(course!)
  crsx!=dx(temp)+(dx(temp+1)-dx(temp))*(course!-temp)
  crsy!=dy(temp)+(dy(temp+1)-dy(temp))*(course!-temp)
  FOR temp=1 TO navnrgy                'begin moving starship
    oldx=newx
    oldy=newy
    x!=x!+crsx!
    y!=y!+crsy!
    newx=CINT(x!)
    newy=CINT(y!)
    IF newx>0 AND newx<9 AND newy>0 AND newy<9 THEN
      IF quadrant(newx,newy)=0 THEN
        IF srsflag THEN
          LINE (24*oldx+408,8*oldy+24)-STEP(23,7),2,bf
          PUT (24*newx+408,8*newy+24),eprs,PSET
        END IF
      ELSE
        CALL prtmsg ("Warp engines shut down at sect"+STR$(shipx)+","+STR$(shipy)+" due to bad navigation."):delay 3
        temp=navnrgy
        newx=oldx
        newy=oldy
      END IF
    ELSE
      IF (newx<1 AND quadx=1) OR (newx>8 AND quadx=8) OR (newy<1 AND quady=1) OR (newy>8 AND quady=8) THEN
        CALL prtmsg ("Lt. Uhura reports a message from Starfleet Command:"):delay 4
        CALL prtmsg ("`Permission to attempt crossing of galactic perimeter is hereby *DENIED*!'"):delay 2
        CALL prtmsg ("`You must shut down your engines!'"):delay 2
        CALL prtmsg ("Chief Engineer Scott reports:"):delay 2
        CALL prtmsg ("`Warp engines shut down at Sector"+STR$(oldx)+","+STR$(oldy)+" - Quadrant"+STR$(quadx)+","+STR$(quady)+"'"):delay 2
        temp=navnrgy
        newx=oldx
        newy=oldy
      ELSE
        x!=x!-INT(x!)
        y!=y!-INT(y!)
        shipx=newx
        shipy=newy
        IF newx<1 THEN quadx=quadx-1:shipx=8
        IF newx>8 THEN quadx=quadx+1:shipx=1
        IF newy<1 THEN quady=quady-1:shipy=8
        IF newy>8 THEN quady=quady+1:shipy=1
        GOSUB newquadrant
        quadrant(shipx,shipy)=0
        newflag=-1
        newx=shipx
        newy=shipy
        x!=newx+x!
        y!=newy+y!
      END IF
    END IF
  NEXT
  shipx=newx
  shipy=newy
  quadrant(shipx,shipy)=enterprise
  IF NOT newflag THEN GOSUB klingonfire
  newflag=0
  GOSUB moveklingons
  energy=energy-navnrgy-10
  IF energy<0 THEN
    CALL prtmsg ("Shield control supplies energy to complete the maneuver.")
    shields=shields+energy
    energy=0
    IF shields<0 THEN shields=0
  END IF
  thisdate!=thisdate!+warp!
  IF thisdate!>startdate+enddate THEN
    CALL prtmsg("Time has run out for your mission!"):delay 3
    GOTO newgame
  END IF
  GOTO dock

zsrs:
  CALL zoom(432,32,626,95,0)
  RETURN
srs:
  srsflag=-1
  GOSUB drawsrs
  GOTO dock
drawsrs:
  IF damage!(2)<0 THEN
    CALL prtmsg ("Short Range Sensors are disabled")
    srsflag=0
    GOSUB zsrs
    GOTO doinfo
  END IF
  CALL zoom(432,32,626,95,2)
  FOR i=1 TO 8
    FOR j=1 TO 8
      IF quadrant(i,j)<>0 THEN
        IF quadrant(i,j)=2 THEN
          PUT (24*i+408,8*j+24),star,PSET
        ELSEIF quadrant(i,j)=enterprise THEN
          PUT (24*i+408,8*j+24),eprs,PSET
        ELSEIF quadrant(i,j)<0 THEN
          PUT (24*i+408,8*j+24),klon,PSET
        ELSE
          PUT (24*i+408,8*j+24),home,PSET
        END IF
      END IF
    NEXT
  NEXT:ding
  RETURN

zlrs:
  CALL zoom(4,119,186,183,0)
  RETURN
lrs:
  IF damage!(3)<0 THEN
    CALL prtmsg("Long Range Sensors are inoperable")
    GOTO doinfo
  END IF
  CALL zoom(4,119,186,183,1):z=2
  COLOR 2,1:LOCATE 16,5:PRINT "Quadrant ";quadx;",";quady
  COLOR 3,1
  temp=1
  FOR i=quady-1 TO quady+1
    LINE (17,16*temp+114)-STEP(149,2),2,bf
    LOCATE 16+temp*2,5
    FOR j=quadx-1 TO quadx+1
      LINE (48*(j-quadx+2)-31,16*temp+117)-STEP(5,13),2,bf
      IF i>0 AND i<9 AND j>0 AND j<9 THEN
        record(j,i)=galaxy(j,i)
        PRINT RIGHT$(STR$(galaxy(j,i)+1000),3);
      ELSE
        PRINT "***";
      END IF
      PRINT SPC(3);
    NEXT
    LINE (48*(j-quadx+2)-31,16*temp+117)-STEP(5,13),2,bf
    temp=temp+1
  NEXT
  LINE (17,178)-STEP(149,2),2,bf
  CALL prtmsg("Storing Data in 'GALactic' memory, found in our computer banks.")
  GOTO doinfo

zpha:
  CALL zoom(194,152,400,159,0)
  RETURN
pha:
  CALL zoom(194,152,400,159,2):z=3
  IF damage!(4)<0 THEN
    CALL prtmsg ("Phasers Inoperative")
    GOTO doinfo
  ELSEIF klingons<=0 THEN
    GOTO noships
  ELSEIF damage!(8)<0 THEN
    CALL prtmsg ("Computer failure hampers accuracy"):delay 3
  END IF
  IF klingons>1 THEN plural$="s" ELSE plural$=""
  COLOR 3,2
  LOCATE 20,26:PRINT "Phasers locked on target"+plural$:ding:delay 2
  LOCATE 20,26:PRINT USING "Energy available:    ####";energy:ding:delay 2
  LOCATE 20,26:PRINT "Number units to fire:":ding
phaloop:
  tmp!=0:CALL navnum(47,20,tmp!)
  IF tmp!<=0 THEN doinfo
  IF tmp!>energy THEN phaloop
  energy=energy-tmp!
  GOSUB klingonfire
  GOSUB phasersnd
  IF damage!(7)<0 THEN tmp!=tmp!*RND
  temp=INT(tmp!/klingons)
  FOR i=1 TO 3
    IF klingon(i,3)>0 THEN
      hit!=INT((temp/FNd(0))*(RND+2))
      IF hit!>.15*klingon(i,3) THEN
        klingon(i,3)=klingon(i,3)-hit!
        IF klingon(i,3)<0 THEN
          CALL prtmsg (STR$(INT(hit!))+" unit hit on Klingon at sector"+STR$(klingon(i,1))+","+STR$(klingon(i,2))):delay 4
          CALL prtmsg ("(Sensors show"+STR$(klingon(i,3))+" units remaining)"):delay 3
        ELSE
          IF srsflag THEN
            CALL blast(24*klingon(i,1)+408,8*klingon(i,2)+24)
          ELSE
            CALL prtmsg("KLINGON DESTROYED!!!")
          END IF
          klingons=klingons-1
          totalklingons=totalklingons-1
          klingon(i,3)=0
          quadrant(klingon(i,1),klingon(i,2))=0
          galaxy(quadx,quady)=galaxy(quadx,quady)-100
          record(quadx,quady)=galaxy(quadx,quady)
        END IF
      ELSE
        CALL prtmsg("Sensors show no damage to enemy at "+STR$(klingon(i,1))+","+STR$(klingon(i,2))):delay 3
      END IF
    END IF
  NEXT
  IF totalklingons=0 THEN winner
  GOSUB moveklingons
  GOTO doinfo

ztor:
  CALL zoom(194,176,400,183,0)
  RETURN
tor:
  IF photon<=0 THEN
    CALL prtmsg ("All photon torpedoes expended")
    GOTO doinfo
  ELSEIF damage!(5)<0 THEN
    CALL prtmsg ("Photon tubes are not operational")
    GOTO doinfo
  END IF
  CALL zoom(194,176,400,183,2):z=4
  COLOR 1,2:LOCATE 23,26:PRINT "Enter course (1-9) :";:ding
  course!=0
  WHILE course!<1 OR course!=>9
    CALL navnum(46,23,course!)
    IF course!<1 OR course!=>9 THEN CALL prtmsg ("Ensign Chekov reports: `Incorrect course data, sir!'")
  WEND
  GOSUB klingonfire
  energy=energy-2
  photon=photon-1
  x!=shipx
  y!=shipy
  temp=INT(course!)
  crsx!=dx(temp)+(dx(temp+1)-dx(temp))*(course!-temp)
  crsy!=dy(temp)+(dy(temp+1)-dy(temp))*(course!-temp)
torpedoloop:
  x!=x!+crsx!
  y!=y!+crsy!
  newx=CINT(x!)
  newy=CINT(y!)
  IF newx>0 AND newx<9 AND newy>0 AND newy<9 THEN
    IF quadrant(newx,newy)=0 THEN
      IF srsflag THEN PUT (24*newx+408,8*newy+24),torp,PSET:stall:stall
    ELSEIF quadrant(newx,newy)=1 THEN
      IF srsflag THEN
        CALL blast(24*oldx+408,8*oldy+24)
      ELSE
        CALL prtmsg ("*** STARBASE DESTROYED ***"):delay 3
      END IF
      docked=0
      quadrant(newx,newy)=0
      starbases=starbases-1
      totalbases=totalbases-1
      IF totalbases=0 OR totalklingons<=thisdate!-startdate-enddate THEN
        CALL prtmsg("THAT DOES IT, CAPTAIN!!  You are hereby relieved of command"):delay 3
        CALL prtmsg("and sentenced to 99 stardates of forced hard labor on CYGNUS 12!!"):delay 3
        GOTO newgame
      ELSE
        CALL prtmsg("Starfleet reviewing your record to consider court martial!"):delay 6
        GOTO missed
      END IF
    ELSEIF quadrant(newx,newy)=2 THEN
      IF srsflag THEN
        CALL blast(24*newx+408,8*newy+24)
        PUT (24*newx+408,8*newy+24),star,PSET
      ELSE
        CALL prtmsg ("Star at"+STR$(newx)+","+STR$(newy)+" absorbed Torpedo energy.")
      END IF
      GOTO missed
    ELSEIF quadrant(newx,newy)<0 THEN
      IF srsflag THEN
        CALL blast(24*newx+408,8*newy+24)
      ELSE
        CALL prtmsg("KLINGON DESTROYED!!!")
      END IF
      quadrant(newx,newy)=0
      i=1:WHILE klingon(i,1)<>newx OR klingon(i,2)<>newy:i=i+1:WEND
      klingon(i,3)=0
      klingons=klingons-1
      totalklingons=totalklingons-1
      IF totalklingons=0 THEN winner
      galaxy(quadx,quady)=100*klingons+10*starbases+stars
      record(quadx,quady)=galaxy(quadx,quady)
      GOTO missed
    END IF
    IF srsflag AND quadrant(newx,newy)<>2 THEN LINE (24*newx+408,8*newy+24)-STEP(23,7),2,bf
    GOTO torpedoloop
  ELSE
    CALL prtmsg ("Torpedo missed")
  END IF
missed:
  GOSUB moveklingons
  GOTO doinfo

zshi:
  CALL zoom(194,127,400,135,0)
  RETURN
shi:
  IF damage!(7)<0 THEN CALL prtmsg ("Shield control is inoperable"):GOTO doinfo
  CALL zoom(194,127,400,135,2):z=5
  COLOR 3,2
  LOCATE 17,26:PRINT USING "Energy available  = ####";energy+shields:ding:delay 2
  LOCATE 17,26:PRINT "Units to shields  =     ":ding
  CALL navnum(46,17,tmp!)
  temp=CINT(tmp!)
  IF shields=temp THEN LOCATE 17,26:PRINT "Shields Unchanged       ":GOTO doinfo
  IF temp=>energy+shields THEN
    CALL prtmsg ("Shield Control reports 'Your request is invalid!'")
    LOCATE 17,26:PRINT "Shields Unchanged       "
    GOTO doinfo
  END IF
  energy=energy+shields-temp
  shields=temp
  CALL prtmsg ("Deflector Control Room: Shields now at"+STR$(shields)+" Units")
  GOTO doinfo

zdam:
  CALL zoom(408,152,625,183,0)
  RETURN
dam:
  IF damage!(6)<0 THEN
    CALL prtmsg ("Damage report not available!"):delay 2
    IF docked THEN repairs ELSE doinfo
  ELSE
    GOTO daminfo
  END IF
repairs:
  tmp!=0
  FOR i=1 TO 8
    IF damage!(i)<0 THEN tmp!=tmp!+1
  NEXT
  IF tmp!=0 THEN doinfo
  tmp!=tmp!+RND/2
  IF tmp!>=1 THEN tmp!=.9
  CALL zoom(408,152,625,183,2)
  COLOR 3,2
  LOCATE 20,54:PRINT "Standing by for repairs"
  LOCATE 21,54:PRINT "to your ship. Etimated"
  LOCATE 22,54:PRINT USING "time to repair is: #.##";tmp!
  LOCATE 23,56:PRINT "OK to proceed? (y/n)";
  key$="":WHILE key$<>"Y" AND key$<>"N":key$=UCASE$(INKEY$):SLEEP:WEND
  IF key$="N" THEN doinfo
  FOR i=1 TO 8
    IF damage!(i)<0 THEN damage!(i)=0
  NEXT
  thisdate!=thisdate!+tmp!+.1
  CALL ding:delay 1
daminfo:
  CALL zoom(408,152,625,183,2):z=6
  COLOR 3,2
  LOCATE 20,53
  PRINT USING "Engines##.##";damage!(1);
  PRINT USING "  SRSnsrs##.##";damage!(2)
  LOCATE 21,53
  PRINT USING "LRSnsrs##.##";damage!(3);
  PRINT USING "  PHAsers##.##";damage!(4)
  LOCATE 22,53
  PRINT USING "PHOtons##.##";damage!(5);
  PRINT USING "  DAMctrl##.##";damage!(6)
  LOCATE 23,53
  PRINT USING "SHIctrl##.##";damage!(7);
  PRINT USING "  COMputr##.##";damage!(8);:ding
  IF docked THEN CALL delay(3):GOTO repairs
  GOTO doinfo

res:
  CALL prtmsg("There were"+STR$(totalklingons)+" Klingon battle cruisers left at the end of your mission."):delay 3
  GOTO quit

com:
  IF damage!(8)<0 THEN CALL prtmsg ("Computer Disabled"):GOTO doinfo
  WINDOW 3,"Library/Computer",(219,65)-(411,121),0,-1
  COLOR 2,1:CLS
  PRINT "GAL = Galactic Record"
  PRINT "REG = Galaxy Regions Map"
  PRINT "HIS = History"
  PRINT "BAS = Starbase Nav Data"
  PRINT "KLI = Klingon Dir/Dist"
  PRINT "DIR = Dir/Dist Calc"
  PRINT "Computer On: ___";
  msg$="___"
comploop:
  key$="":WHILE key$="":key$=UCASE$(INKEY$):SLEEP:WEND
  IF key$=CHR$(8) THEN
    msg$=RIGHT$("___"+LEFT$(msg$,2),3)
  ELSEIF INSTR(command2$,key$)>0 THEN
    msg$=RIGHT$("___"+msg$+key$,3)
  END IF
  LOCATE 7,14:PRINT msg$;
  temp=0
  FOR i=0 TO 5
    IF msg$=MID$(command2$,3*i+1,3) THEN temp=i+1:i=9
  NEXT
  IF temp=0 THEN comploop
  ON temp GOSUB gal,reg,his,bas,kli,dir
  CALL whoa
  WINDOW CLOSE 3
  GOTO doinfo

gal:
  WINDOW 3,"Computer record of galaxy:",(123,77)-(507,149),0,-1
  COLOR 3,2:CLS
  COLOR 1:PRINT "   1     2     3     4     5     6     7     8"
  FOR j=1 TO 8
    COLOR 1:PRINT USING "# ";j;
    FOR i=1 TO 8
      IF record(i,j)=0 THEN
        COLOR 0:PRINT"***   ";
      ELSE
        IF i=quadx AND j=quady THEN COLOR 1 ELSE COLOR 3
        PRINT RIGHT$(STR$(record(i,j)+1000)+"   ",6);
      END IF
    NEXT
    IF j<8 THEN PRINT
  NEXT
  RETURN

reg:
  WINDOW 3,"THE GALAXY:",(211,57)-(419,129),0,-1
  COLOR 3,2:CLS
  PRINT " 1  2  3  4    1  2  3  4"
  regionflag=0
  FOR i=1 TO 8
    PRINT USING "# ";i;
    CALL quadrantname(1,i)
    PRINT msg$;
    CALL quadrantname(5,i)
    PRINT TAB(17)msg$;
    IF i<8 THEN PRINT
  NEXT
  RETURN

his:
  WINDOW 3,"History of this game.",(115,50)-(515,138),0,-1
  COLOR 3,2:CLS
  PRINT TAB(11)"Original program by Dave Ahl"
  PRINT TAB(17)"Modifications by"
  PRINT "Bob & Sharon Fritz, Mike Stafford, and Jim Buzonik"
  PRINT TAB(9)"AMIGA VERSION CONVERTED FROM IBM"
  PRINT TAB(3)"by Phil Martinez (Phelan Gee) V1.5 08-16-88"
  PRINT TAB(4)"This is a major re-write of that version."
  PRINT TAB(3)"I couldn't run Phil's version on my 512K, so"
  PRINT TAB(7)"I was working blind in writing this."
  PRINT " Address comments, complaints, bugs, etc. to me:"
  PRINT TAB(10)"john everett (PLINK ID OHS303)"
  PRINT  TAB(10)"321 Hodges, Memphis, TN  38111";
  RETURN

bas:
  WINDOW 3,"From ENTERPRISE to Starbase:",(195,96)-(435,104),0,-1
  COLOR 3,2:CLS
  IF starbases=0 THEN PRINT "No starbases in this quadrant.";:RETURN
  FOR i=1 TO 8:FOR j=1 TO 8
    IF quadrant(j,i)=1 THEN newx=j:newy=i:j=8:i=8
  NEXT:NEXT
  CALL getcourse(shipx,shipy,newx,newy)
  PRINT USING "  Course=#.##  ";course!;
  PRINT USING "Distance=#.##";x!;
  RETURN

kli:
  WINDOW 3,"From ENTERPRISE to Klingon Cruisers:",(171,96)-(459,104),0,-1
  COLOR 3,2:CLS
  IF klingons=0 THEN PRINT "No klingons in this quadrant.";:RETURN
  FOR temp=1 TO 3
    IF klingon(temp,3)>0 THEN
      CALL getcourse(shipx,shipy,klingon(temp,1),klingon(temp,2))
      PRINT USING "Course=#.## ";course!;
    END IF
  NEXT
  RETURN

dir:
  WINDOW 3,"Direction/Distance Calculator",(205,96)-(455,112),0,-1
  COLOR 3,2:CLS
loop:
  PRINT "Start x=   y=     End x=   y= "
  COLOR 1
  LOCATE 1,9:PRINT "_";:CALL digit(oldx):LOCATE 1,9:PRINT USING "#";oldx
  LOCATE 1,14:PRINT "_";:CALL digit(oldy):LOCATE 1,14:PRINT USING "#";oldy
  LOCATE 1,25:PRINT "_";:CALL digit(newx):LOCATE 1,25:PRINT USING "#";newx
  LOCATE 1,30:PRINT "_";:CALL digit(newy):LOCATE 1,30:PRINT USING "#";newy
  CALL getcourse(oldx,oldy,newx,newy)
  PRINT USING "  Course=#.##";course!;
  PRINT USING "   Distance=#.##";x!;
  RETURN

moveklingons:
  FOR i=1 TO klingons
    IF klingon(i,3)<>0 THEN
      FOR temp=1 TO 3
        GOSUB findspot
        IF ABS(rnd1-klingon(i,1))<=1 AND ABS(rnd2-klingon(i,2))<=1 THEN
          IF srsflag THEN LINE (24*klingon(i,1)+408,8*klingon(i,2)+24)-STEP(23,7),2,bf
          quadrant(klingon(i,1),klingon(i,2))=0
          quadrant(rnd1,rnd2)=-1
          klingon(i,1)=rnd1
          klingon(i,2)=rnd2
          IF srsflag THEN PUT (24*klingon(i,1)+408,8*klingon(i,2)+24),klon,PSET
        END IF
      NEXT
    END IF
  NEXT
  RETURN

klingonfire:
  IF klingons=0 THEN RETURN
  IF docked THEN CALL prtmsg ("Starbase shields protect the ENTERPRISE"):delay 3:RETURN
  temp=0
  FOR i=1 TO 3
    IF klingon(i,3)>0 THEN
      temp=temp+INT((klingon(i,3)/FNd(1))*(RND+1))
      shields=shields-temp
      energy=energy-temp
    END IF
  NEXT
  IF temp=0 THEN RETURN
  ON z GOSUB znav,zlrs,zpha,ztor,zdam,zcom
  z=0:zoom 408,152,625,183,2:z=6
  LOCATE 20,57:PRINT "ENTERPRISE HIT !!!"
  GOSUB alarmsnd
  LOCATE 21,53:PRINT USING "### unit hit on ENTERPRISE";temp
  IF shields<=0 THEN
    GOSUB alarmsnd
    CALL prtmsg("The Enterprise has been destroyed by enemy laser fire."):delay 5
    GOTO newgame
  END IF
  LOCATE 22,53:PRINT USING "Shields down to #### units";shields
  IF temp=>20 AND RND<.6 AND temp/shields>.02 THEN
    rnd1=INT(8*RND)+1
    damage!(rnd1)=damage!(rnd1)-temp/shields-.5*RND
    LOCATE 23,53:PRINT device$(rnd1)+" damaged!";
    IF rnd1=2 THEN GOSUB zsrs:srsflag=0
  END IF
  RETURN

noships:
  CALL prtmsg ("Science Officer Spock reports: `Sensors show no enemy ships in this quadrant'")
  GOTO doinfo

newquadrant:
  klingons=0
  starbases=0
  stars=0
  record(quadx,quady)=galaxy(quadx,quady)
  IF quadx>0 AND quadx<9 AND quady>0 AND quady<9 THEN
    regionflag=-1:quadrantname quadx,quady
    CALL prtmsg("Now entering "+msg$+" quadrant.")
    klingons=INT(galaxy(quadx,quady)/100)
    starbases=INT(galaxy(quadx,quady)/10)-10*klingons
    stars=galaxy(quadx,quady)-100*klingons-10*starbases
    FOR i=1 TO 8
      FOR j=1 TO 8
        quadrant(i,j)=0
      NEXT
    NEXT
    FOR i=1 TO 3
      klingon(i,3)=0
    NEXT
  END IF
  quadrant(shipx,shipy)=enterprise
  IF klingons>0 THEN
    FOR i=1 TO klingons
      GOSUB findspot
      klingon(i,1)=rnd1
      klingon(i,2)=rnd2
      klingon(i,3)=INT(200*(RND+.5))
      quadrant(rnd1,rnd2)=-1
    NEXT
  END IF
  IF starbases>0 THEN
    GOSUB findspot
    basex=rnd1
    basey=rnd2
    quadrant(basex,basey)=1
  END IF
  FOR i=1 TO stars
    GOSUB findspot
    quadrant(rnd1,rnd2)=2
  NEXT
  IF srsflag THEN GOSUB drawsrs
  RETURN

findspot:
  rnd1=INT(8*RND)+1
  rnd2=INT(8*RND)+1
  IF quadrant(rnd1,rnd2)<>0 THEN findspot
  RETURN

cle:
  GOSUB drawscreen
  IF srsflag THEN GOSUB drawsrs
  GOTO doinfo
drawscreen:
  COLOR 1,2:CLS
  LINE (0, 8)-(631, 8),1                'communications (top line)
  LINE (0,10)-(631,10),0
  LINE (0,12)-(631,12),1
  COLOR 1,0
  LINE (2,14)-STEP(186,82),1,bf         'readings section
  LINE (4,15)-STEP(182,80),0,bf
  LOCATE  3,2:PRINT "     Stardate:"
  LOCATE  4,2:PRINT "    Time Left:"
  LOCATE  5,2:PRINT "   Bases Left:"
  LOCATE  6,2:PRINT "    Condition:"
  LOCATE  7,2:PRINT "     Quadrant:"
  LOCATE  8,2:PRINT "       Sector:"
  LOCATE  9,2:PRINT " Photon Torps:"
  LOCATE 10,2:PRINT " Total Energy:"
  LOCATE 11,2:PRINT "      Shields:"
  LOCATE 12,2:PRINT "Klingons left:"
  LINE (192,14)-STEP(210,82),1,bf       'control board
  LINE (194,15)-STEP(206,80),2,bf
  COLOR 0,2
  FOR i=2 TO 11
    LINE (197,8*i)-STEP(12,6),0,bf
    LOCATE i+1:PRINT PTAB(214)MID$(command$,3*(i-2)+1,3)
    LINE (241,8*i)-STEP(12,6),0,bf
    LINE (344,8*i)-STEP(11,6),0,bf
    LOCATE i+1:PRINT PTAB(359)"***"
    LINE (386,8*i)-STEP(11,6),0,bf
  NEXT
  LINE (257,16)-STEP(83,3),0,bf         'course info
  LINE (259,17)-STEP(79,1),2,bf
  LINE (257,21)-STEP(83,60),1,bf
  LINE (259,22)-STEP(79,58),0,bf
  LINE (261,23)-STEP(75,56),1,bf
  LINE (257,83)-STEP(83,11),0,bf
  LINE (259,84)-STEP(79,9),2,bf
  LINE (261,85)-STEP(75,7),0,bf
  COLOR 0,1
  LOCATE  4,34:PRINT "    3"
  LOCATE  5,34:PRINT " 4  |  2"
  LOCATE  6,34:PRINT "   \|/"
  LOCATE  7,34:PRINT "5---+---1"
  LOCATE  8,34:PRINT "   /|\"
  LOCATE  9,34:PRINT " 6  |  8"
  LOCATE 10,34:PRINT "    7"
  LINE (406,14)-STEP(223,82),1,bf       'short range scanner
  LINE (408,15)-STEP(219,80),0,bf
  COLOR 1,0
  LOCATE 3,57:PRINT "Short Range Scanner";
  LOCATE 4,56:PRINT "1  2  3  4  5  6  7  8"
  FOR i=1 TO 8:LOCATE i+4,53:PRINT CHR$(48+i):NEXT
  LINE (430,31)-STEP(196,65),1,bf
  LINE (432,32)-STEP(194,63),0,bf
  LINE (2,98)-STEP(627,2),1,bf          'top/bottom dividing line
  LINE (4,99)-STEP(623,0),0
  LINE (2,186)-STEP(627,0),1
  LINE (2,102)-STEP(186,82),1,bf        'long range scanner
  LINE (4,103)-STEP(182,80),0,bf
  LOCATE 14,4:PRINT "Long Range Scanner"
  FOR i=112 TO 118 STEP 3:LINE (4,i)-STEP(182,0),1,b:NEXT
  LINE (192,102)-STEP(210,10),1,bf      'command
  LINE (194,103)-STEP(206,8),0,bf
  LINE (192,114)-STEP(210,2),1,bf
  LINE (194,115)-STEP(206,0),0
  LINE (192,118)-STEP(210,18),1,bf      'shield control
  LINE (194,119)-STEP(206,16),0,bf
  LOCATE 16,31:PRINT "Shield Control"
  LINE (192,138)-STEP(437,2),1,bf
  LINE (194,139)-STEP(433,0),0
  LINE (192,142)-STEP(210,18),1,bf      'phasers
  LINE (194,143)-STEP(206,16),0,bf
  LOCATE 19,31:PRINT "Phaser Station"
  LINE (192,162)-STEP(210,2),1,bf
  LINE (194,163)-STEP(206,0),0
  LINE (192,166)-STEP(210,18),1,bf      'photon torpedos
  LINE (194,167)-STEP(206,16),0,bf
  LOCATE 22,27:PRINT "Photon Torpedo Station"
  LINE (406,102)-STEP(223,34),1,bf      'navigation
  LINE (408,103)-STEP(219,32),0,bf
  LOCATE 14,57:PRINT "Navigation Station"
  FOR i=112 TO 118 STEP 3:LINE (408,i)-STEP(221,0),1,b:NEXT
  LINE (406,142)-STEP(223,42),1,bf      'damage control
  LINE (408,143)-STEP(219,40),0,bf
  LOCATE 19,55:PRINT "Damage Control Station"
  srsflag=0
  RETURN

redalert:
  FOR i=1 TO 4
    FOR freq=1000 TO 2000 STEP 40
      SOUND freq,.5,255
    NEXT
  NEXT
  RETURN

torpedosnd:
  FOR freq=1500 TO 500 STEP -40
    SOUND freq,1,255
    SOUND 3600-freq,1,255
  NEXT
  RETURN

phasersnd:
  FOR i=1 TO 10
    SOUND 800,1,255
    SOUND 2500,1,255
  NEXT
  RETURN

alarmsnd:
  FOR j=1 TO 6
    FOR freq=1 TO 15
      SOUND 150-freq,1,255,2
      SOUND 200+freq,1,255,3
    NEXT
  NEXT
  RETURN

intro1:
  RESTORE intro1
  CALL zoom(0,0,631,186,1)
  CALL zoom(0,4,631,186,2)
  COLOR 1,2:LOCATE 2,15:PRINT "These are the voyages of the Starship Enterprise..."
  FOR i=1 TO 8
    READ freq,duration,volume
    SOUND freq,duration,volume,0
    SOUND freq,duration,volume,1
  NEXT
  RETURN
DATA 1568,3,255,1568,25,175,784,3,255,784,25,175
DATA 1175,3,255,1175,25,175,587,3,255,587,75,150

intro2:
  CALL zoom(0,20,631,186,3)
  CALL zoom(0,36,631,186,2)
  COLOR 2,3
  LOCATE 4,15:PRINT "Current mission, to save Federation Headquarters..."
  RETURN

intro3:
  RESTORE intro3
  CALL zoom(0,40,631,186,1)
  CALL zoom(4,44,627,186,0)
  COLOR 2,0
  IF totalbases=1 THEN plural$="" ELSE plural$="s"
  LOCATE 7,17:PRINT "Which will be attacked by";totalklingons;"Klingon warships"
  LOCATE 8,10:PRINT "that have invaded the galaxy.  You have until Stardate";startdate+enddate
  LOCATE 9,10:PRINT "before they attack.  This gives you";enddate;"days to complete your"
  LOCATE 10,15:PRINT "mission.  You will have";totalbases;"starbase";plural$;" in the galaxy"
  LOCATE 11,21:PRINT "for resupply and repair of your ship."
  OPEN "ent" FOR INPUT AS #2
    OBJECT.SHAPE 1,INPUT$(LOF(2),2)
  CLOSE #2
  OBJECT.X 1,608    '560
  OBJECT.Y 1,140
  OBJECT.VX 1,-.1
  OBJECT.AX 1,-1.5
  ON COLLISION GOSUB offscreen:COLLISION ON
  CALL zoom(0,92,631,186,1)
  CALL zoom(0,96,631,186,2)
  FOR i=1 TO 50
    PUT (24*INT((631*RND)/24),8*INT((88*RND)/8)+97),star,PSET
  NEXT
  OBJECT.ON 1
  OBJECT.START 1
  volume=255
  FOR i=1 TO 9
    READ freq,duration
    FOR j=0 TO 3
      SOUND freq,duration,volume,j
    NEXT
  NEXT
  SOUND 466,4,255,0
  SOUND 277,4,255,1
  SOUND 622,4,255,2
  SOUND 370,4,255,3
  SOUND 466,64,255,0
  SOUND 233,64,255,1
  SOUND 698,64,255,2
  SOUND 349,64,255,3
  CALL delay(10)
  RETURN
DATA 262,12,349,4,466,24,440,8,392,8,349,8,311,12,416,4,555,52

offscreen:
  COLLISION OFF
  OBJECT.OFF
  RETURN

winner:
  CALL prtmsg("Message from STARFLEET COMMAND:"):delay 3
  CALL prtmsg("Congratulations, Captain!"):delay 3
  CALL prtmsg("The last Klingon battle cruiser menacing the Federation has been destroyed!"):delay 3
  CALL prtmsg("Your efficiency rating is"+STR$(1000*(numshot/(thisdate!-startdate))^2)):delay 3
newgame:
  CALL prtmsg("This is the record of the Galaxy at the end of your mission.")
  GOSUB gal:whoa:WINDOW CLOSE 3
  IF totalbases>0 THEN
    CALL prtmsg("The Federation is in need of a new starship commander for a similar mission."):delay 3
    CALL prtmsg("If there is a volunteer, let him or her now step forward."):delay 3
    CALL prtmsg("Enter `Y' to volunteer, or `N' to retire.")
    key$="":WHILE key$<>"Y" AND key$<>"N":key$=UCASE$(INKEY$):SLEEP:WEND
    IF key$="N" THEN quit
  END IF
  IF NOT debug THEN GOSUB intro1
  GOTO begin

quit:
  OBJECT.CLOSE
  IF rgb(2,1)<>0 THEN
    FOR i=0 TO 3
      PALETTE i,rgb(i+1,1),rgb(i+1,2),rgb(i+1,3)
    NEXT
  END IF
  MENU RESET
  IF NOT debug THEN
    FOR freq=1200 TO 100 STEP -10
      SOUND freq,.3,255,0:SOUND 1.25*freq,.3,255,1
    NEXT
    WINDOW 9,,(236,89)-(236+160,89+7),0
    COLOR 3,2:CLS:PRINT "    john everett":PRINT "PeopleLINK ID OHS303";
    SOUND  200,50,255,0:SOUND  250,50,255,1
    SOUND  300,50,255,2:SOUND  400,50,255,3
  END IF
  WINDOW CLOSE 2
  CALL delay(3)
  WINDOW CLOSE 9
  WINDOW 1
  COLOR 3,2:CLS
  PRINT "You ";:COLOR 1:PRINT "MUST";
  COLOR 3:PRINT " click the ";:COLOR 1:PRINT "NO"
  COLOR 3:PRINT "gadget when the next":PRINT "requestor is presented,"
  PRINT "as most of the program":PRINT "will have been erased"
  PRINT "in order to release":PRINT "it's extra memory!";:delay 5
zx:
  DELETE begin-quit
  CLEAR,25000
  SOUND 1600,1,255,0:SOUND 2000,1,255,1
  SOUND  100,2,255,0:SOUND  125,2,255,1
  SYSTEM
  END

SUB whoa STATIC
  WHILE INKEY$="":SLEEP:WEND
END SUB

SUB delay(amount) STATIC
  late!=TIMER+CSNG(amount)
  WHILE MOUSE(0)<>0:WEND
  WHILE TIMER<late! AND MOUSE(0)=0:WEND
END SUB

SUB stall STATIC
  FOR i=1 TO 400:NEXT
END SUB

SUB ding STATIC
  SOUND 3000,2,255,3
  SOUND 1500,6,255,1
END SUB

SUB prtmsg(msg$) STATIC
  CALL ding:dissolve
  COLOR 3,2:LOCATE 1,1:PRINT TAB((80-LEN(msg$))/2)msg$
END SUB

SUB dissolve STATIC
  FOR colr=1 TO 2
    FOR j=0 TO 15 STEP 4
      FOR i=0 TO 40
        LINE (16*i+j,0)-STEP(3,7),colr,bf
      NEXT
    NEXT
  NEXT
END SUB

SUB zoom(sx,sy,ex,ey,colr) STATIC
  FOR i=(ey-sy)/2 TO 0 STEP -1
    LINE (sx+i,sy+i)-(ex-i,ey-i),colr,b
  NEXT
END SUB

SUB navnum(x,y,number!) STATIC
  COLOR 3,2
  msg$=""
digiloop:
  key$="":i=0
  WHILE i=0
    key$=INKEY$
    i=INSTR("1234567890."+CHR$(13)+CHR$(8),key$)
    SLEEP
  WEND
  IF i=13 THEN
    IF LEN(msg$)>0 THEN msg$=LEFT$(msg$,LEN(msg$)-1)
    LOCATE y,x:PRINT RIGHT$("____"+msg$,4);
    GOTO digiloop
  ELSEIF i<12 THEN
    msg$=msg$+key$
    LOCATE y,x:PRINT RIGHT$("____"+msg$,4);
    IF LEN(msg$)<4 THEN digiloop
  END IF
  number!=VAL(msg$)
END SUB

SUB blast(x,y) STATIC
  SHARED bng1(),bng2(),bng3(),boom()
  WAVE 0,boom
  PUT (x,y),bng1,PSET
  FOR freq=70 TO 120:SOUND freq,.1,255,0:NEXT
  PUT (x,y),bng2,PSET
  FOR freq=120 TO 170:SOUND 300-freq,.1,255,0:NEXT
  PUT (x,y),bng3,PSET
  SOUND 300-freq,3,255,0:stall
  LINE (x,y)-STEP(23,7),2,bf
  WAVE 0,SIN
END SUB

SUB quadrantname(x,y) STATIC
  SHARED quad1$,quad2$,msg$,regionflag
  IF x<4 THEN
    msg$=MID$(quad1$,11*(y-1)+1,11)
  ELSE
    msg$=MID$(quad2$,10*(y-1)+1,10)
  END IF
  CALL nospaces(msg$)
  IF regionflag THEN
    msg$=msg$+MID$(" I   II  III IV  I   II  III IV ",4*(x-1)+1,4)
    CALL nospaces(msg$)
  END IF
END SUB

SUB nospaces(msg$) STATIC
  WHILE RIGHT$(msg$,1)=" ":msg$=LEFT$(msg$,LEN(msg$)-1):WEND
END SUB

SUB digit(i) STATIC
  key$="":WHILE key$<"1" OR key$>"8":key$=INKEY$:SLEEP:WEND
  i=VAL(key$)
END SUB

SUB getcourse(sx,sy,ex,ey) STATIC
  SHARED course!,x!
  dy=ey-sy
  dx=ex-sx
  IF dy<0 THEN c
  IF dx<0 THEN d
  IF dy>0 THEN a
  IF dx=0 THEN course!=3:GOTO b
a:
  course!=7
b:
  dx=ABS(dx):dy=ABS(dy)
  IF dx>dy THEN
    course!=course!+(2*dx-dy)/dx
  ELSE
    course!=course!+(dx/dy)
  END IF
  GOTO out
c:
  IF dx>0 THEN course!=1:GOTO e
  IF dy<>0 THEN course!=3:GOTO b
d:
  course!=5
e:
  dx=ABS(dx):dy=ABS(dy)
  IF dx<dy THEN
    course!=course!+(2*dy-dx)/dy
  ELSE
    course!=course!+(dy/dx)
  END IF
out:
  IF course!=9 THEN course!=1
  x!=SQR(dy^2+dx^2)
END SUB

DATA "Warp Engines","ShortRange Sensor","Long Range Sensor","Phaser Control"
DATA "Photon Tubes","Damage Control","Shield Control","Library/Computer"
