goSub :checkCommandPrompt

##  STARDOCK does not always return the "last" seen stardock sector
##  You have to clear the sector if it moved.

killTrigger dockSector
setTextLineTrigger dockSector :dockSector "The StarDock is located in sector "
send "v"
pause
:dockSector
setVar $dockLine CURRENTLINE
getWord $dockLine $dockSector 7
stripText $dockSector "."

send "cj?"
waitFor "Which planet type are you interested in"
setTextLineTrigger planetClassListFinished :planetClassListFinished "<Q> To Leave"

setVar $i 0
:readPlanetLoop
waitFor "Class"
add $i 1
setVar $thisline CURRENTLINE
stripText $thisline "<"
stripText $thisline ">"
getWord $thisline $planetLetter[$i] 1
getWord $thisline $planetType[$i] 4
setVar $planetName[$i] "Planet"
setVar $planetType $i
setVar $planetLetter $i
goTo :readPlanetLoop
:planetClassListFinished
send "qq"
waitFor "Computer command [TL="
waitFor "Command [TL="

# feel free to put a chooser  here

goSub :planetChooser
goSub :planetNamer


# feel free to put a fuel-ore source chooser here.
echo "Fuel ore source:  in-sector port **"
setVar $ore_source "p"

goSub :chooseWarpMode
 
echo "*"
echo "* WARNING:  This script is hardcoded to the following values:"
echo "*  Warp mode:             " $warp_mode 
echo "*  Planet with beam:      " $planet_beam
#echo "*  Desired Planet Class:  " $planetClass  "*"
echo "*  If this is ok, press y.  If not, hit any other key and edit the script**"

getConsoleInput $proceed SINGLEKEY
if ($proceed <> "y")
  echo "**Aborted**"
  halt
end

goSub :popPlanets

halt



##################################################
:chooseWarpMode
##################################################
:chooseWarpMode
  setVar $warp_mode 0
  while ($warp_mode <> "b") AND ($warp_mode <> "y")
    echo "**Warp mode (b) or (t):  "
    getConsoleInput $warp_mode SINGLEKEY
    lowercase $warp_mode
  end
  if ($warp_mode = "b")
    echo "**Planet number of beam planet?" 
    getConsoleInput $planet_beam
  end
return

##################################################
:popPlanets
##################################################
:popPlanets
  
  while (1=1)
    killAllTriggers

    # check turns, credits, and alignment
    goSub :quikstats
    setVar $planet_sector $quikstats[sect]

    goSub :refill_ship

    goSub :quikStats

    if ($quikstats[GTorp] < 1) OR ($quikstats[AtmDt] < 1)
      echo "**Aborting:  Could not refill torps or detonators**"
      return
    end

    while ($quikstats[GTorp] > 0)  AND ($quikstats[AtmDt] > 0)

      getTime $time "hhnnss" 
      setVar $e 65537
      setVar $planetName ($time * 65537)

      killTrigger overload
      killTrigger what_line
      setTextLineTrigger what_line :what_line "What do you want to name this planet?"
      setTextLineTrigger overload :overload "It is potentially hazardous to place more than"
      send "u y " 
      pause

      :overload
      send "n"
      pause

      :what_line
      getWord CURRENTLINE $this_class 11 
      stripText $this_class ")"

      #echo "**" $this_class "**" $planetClass "**"
     

      setVar $planetIndex 0
      setVar $thisPlanetIndex 0
      while ($planetIndex < $planetType)
        add $planetIndex 1
        if ($planetTypeSelected[$planetIndex] = 1) and ($this_class = $planetType[$planetIndex])
          setVar $thisPlanetIndex $planetIndex
        end
      end

      if ($thisPlanetIndex <> 0)
        send $planetName[$thisPlanetIndex] "* zc* "
        halt 
      else 
        send $planetName "* zc* d"
      end

      waitFor "Command [TL="

      # Blow up the crappy planet
      killTrigger planet_to_kill
      setTextTrigger land_list :land_list "Registry# and Planet Name"
      send "l" 
      pause
      :land_list

      setTextLineTrigger planet_to_kill :planet_to_kill $planetName
      pause
      :planet_to_kill

      setVar $planet_line CURRENTLINE
      #echo "*[" $planet_line "]*"
      stripText $planet_line "<"
      stripText $planet_line ">"
      getWord $planet_line $target_planet 1
 
      killTrigger land_prompt
      setTextTrigger land_prompt :land_prompt "Land on which planet"
      pause
      :land_prompt
      #echo "*[" $planet_line "]*"
      send $target_planet "* z d y"
      #send $target_planet "* z d * "
      #halt
      goSub :quikstats
    end
  end

halt


################################
:refill_ship
################################
:refill_ship
  goSub :quikStats
  if ($quikstats[GTorp] < 1) OR ($quikstats[AtmDt] < 1)

    if ($quikstats[Turns]<100) OR ($quikstats[Creds]<250000) OR ($quikstats[Aln]<=1000)
      echo "Aborting:*check turns, credits, or alignment**"
    end

    #  Case:  fuel from port
    if ($ore_source = "p")  
      send "p * * * "
      waitFor "Enter your choice"
      waitFor "Command [TL"
      goSub :quikstats
      echo "*" $quikstats[Hlds] "*" $quikstats[Ore] "*"
      if ($quikstats[Hlds] > $quikstats[Ore])
        echo "Aborting:  Low fuel.**"
        return
      end
    else 
      echo "Aborting:  Fuel ore source unavailable.**"
      return
    end

    # todo: other possible case:  from planet
    
    # buy torps and dets
    # beam mode
    if ($warp_mode = "b")
      :start_beam_check
      killTrigger beam_prompt
      killTrigger no_cby 
      setTextTrigger beam_prompt :beam_prompt "Beam to what sector? (U=Upgrade Q=Quit)"
      setTextTrigger no_cby :no_cby "ARE YOU SURE CAPTAIN"
      send "l " $planet_beam " * c b "
      pause

      :no_cby
      send "nqqq"
      waitFor "Command [TL"
      goTo :start_beam_check
      
      :beam_prompt
      echo "*" $dockSector "*"
      send $dockSector "* y p s h"

    elseif ($warp_mode = "t")
      send "n s y y p s h"
    end
    waitFor "So what are you looking for (?) ?"
    send "a"
    waitFor "[0] ?"
    getWord CURRENTLINE $atmdt_limit 9
    stripText $atmdt_limit ")"
    send $atmdt_limit "* t"
    waitFor "[0] ?"
    getWord CURRENTLINE $gtorp_limit 9
    stripText $gtorp_limit ")"
    # buy the torps and head to the shipyards
    killTrigger fig_line
    setTextLineTrigger fig_line :fig_line " credits per fighter"
    send $gtorp_limit "*qsp" 
    pause
    :fig_line
    setVar $fig_line CURRENTLINE
    killTrigger shield_line
    setTextLineTrigger shield_line :shield_line " credits per point"
    pause
    :shield_line
    setVar $shield_line CURRENTLINE
    getWord $fig_line $max_figs 8
    getWord $shield_line $max_shields 9
    killTrigger purchase_prompt
    setTextTrigger purchase_prompt :purchase_prompt "Which item do you wish to buy? (A,B,C,Q,?)"
    pause
    :purchase_prompt
    send "b" $max_figs "*"
    send "c" $max_shields "*"
    send "qq"
    killTrigger warp_lock
    killTrigger no_warp_lock
    setTextLineTrigger warp_lock :warp_lock "TransWarp Locked."
    setTextLineTrigger no_warp_lock :no_warp_lock "No locating beam found"
    send "q m " $planet_sector "* y"
    pause
    :no_warp_lock
      send "p s"
      echo "**Aborting:  Cannot find warp lock.**"
      halt
    :warp_lock
      send "y "
      waitFor "[" & $planet_sector & "]"
  end
return

#quickstats -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
:quikstats
	setVar $h[1] "Sect"
	setVar $h[2] "Turns"
	setVar $h[3] "Creds"
	setVar $h[4] "Figs"
	setVar $h[5] "Shlds"
	setVar $h[6] "Hlds"
	setVar $h[7] "Ore"
	setVar $h[8] "Org"
	setVar $h[9] "Equ"
	setVar $h[10] "Col" 
	setVar $h[11] "Phot"
	setVar $h[12] "Armd"
	setVar $h[13] "Lmpt"
	setVar $h[14] "GTorp"
	setVar $h[15] "TWarp"
	setVar $h[16] "Clks"
	setVar $h[17] "Beacns"
	setVar $h[18] "AtmDt"
	setVar $h[19] "Crbo"
	setVar $h[20] "EPrb"
	setVar $h[21] "MDis"
	setVar $h[22] "PsPrb"
	setVar $h[23] "PlScn"
	setVar $h[24] "LRS"
	setVar $h[25] "Aln"
	setVar $h[26] "Exp"
	setVar $h[27] "Ship"
	setVar $cnt 0
	send "/"
:chk
	setTextLineTrigger getLine :getLine
	pause

:getLine
	killtrigger done
	add $cnt 1
	setVar $culine CURRENTLINE
	replaceText $culine #179 " " & #179 & " "
	setVar $line[$cnt] $culine
	getWordPos $culine $pos " Ship "
	if ($pos > 0)
	     goto :done_read_qss
	end
	goto :chk

:done_read_qss
	killtrigger getLine
	setVar $hcount 0
:hcount
	if ($hcount < 27)
	     add $hcount 1
	     setVar $lncount 1
:lncount
	     if ($lncount < $cnt)
        	  add $lncount 1
	          getWordPos $line[$lncount] $pos $h[$hcount]
	          if ($pos > 0)
	               setVar $work $line[$lncount]
	               cutText $work $work $pos 9999
	               upperCase $h[$hcount]
	               getWord $work $quikstats[$h[$hcount]] 2
	               stripText $quikstats[$h[$hcount]] ","
	          else
	               goto :lncount
	          end
	     end
	     goto :hcount
	end
	return

halt


##################################################
#  :planetNamer
##################################################
:planetNamer
  setVar $userInput "0"


  while ($userInput <> "Q")

    # display the planet letters and name
    echo ANSI_3 "*=================================*"
    echo ANSI_3 "Set default planet names*"
    echo ANSI_3 "=================================*"
    echo ANSI_4 "     [Type]"
    echo ANSI_10 " - [name]*"
    setVar $pindex 0
    while ($pindex < $planetType)
      add $pindex 1
      if ($planetTypeSelected[$pindex] = 1)
        echo ANSI_5 "<" $planetLetter[$pindex] ">  " 
        echo ANSI_4 $planetType[$pindex] 
        echo ANSI_5 " - "
        echo ANSI_10 $planetName[$pindex] "*"
      end
    end
    echo  ANSI_6 "*<Q>  Continue**"

    # get user input
    echo "Select Planet Type: "
    getConsoleInput $userInput SINGLEKEY
    upperCase $userInput

    # if the user wants to quit, quit
    if ($userInput = "Q")
      return
    end

    # if the user entered a letter, read the name
    setVar $letterIndex 0
    while ($letterIndex < $planetType)
      add $letterIndex 1
      if ($planetLetter[$letterIndex] = $userInput)
        echo "**Enter Planet Name:  "
        getConsoleInput $planetName[$letterIndex]
      end
    end
  end
return



##################################################
#  :planetChooser
##################################################
:planetChooser
  setVar $pindex 1

  while ($pindex <= $planetType) 
    setVar $planetTypeSelected[$pindex] 0
    add $pindex 1
  end

  setVar $userInput "0"

  
  while ($userInput <> "Q")  

    # display the planet letters and types
    echo ANSI_3 "*=================================*"
    echo ANSI_3 "Select Planet Types:*"
    echo ANSI_3 "=================================*"
    echo ANSI_3 "[Selected]       [Type]*"
    setVar $pindex 0
    while ($pindex < $planetType)
      add $pindex 1
      echo ANSI_5 "    ["
      if ($planetTypeSelected[$pindex] = 0)
        echo ANSI_4 "-"
      else
        echo ANSI_10 "+"
      end
      echo ANSI_5 "]          "
      echo ANSI_5 "<" 
      echo ANSI_4 $planetLetter[$pindex] 
      echo ANSI_5 ">  " 
      echo ANSI_4 $planetType[$pindex] "*"
    end
    echo  ANSI_6 "*<Q>  Continue**"

    # get user input
    getConsoleInput $userInput SINGLEKEY
    upperCase $userInput

    # if the user wants to quit, quit
    if ($userInput = "Q")
      setVar $planetIndex 0
      setVar $noplanets 1
      while ($planetIndex < $planetType)
        add $planetIndex 1
        if ($planetTypeSelected[$planetIndex] = 1)
          setVar $noPlanets 0
        end
      end
      if ($noplanets = 1)
        echo "** You need to select at least one planet type!*"
        halt
      else 
        return
      end
    end

    # if the user entered a letter, flip the bit
    setVar $letterIndex 0
    while ($letterIndex < $planetType)
      add $letterIndex 1
      if ($planetLetter[$letterIndex] = $userInput)
        # I'd love to just type "xor $var 1" but because
        # twx is written in freak delphi, i am not going to
        # spend $500 on a crappy compiler just so i can fix
        # the code.  
        # XOR  $planetTypeSelected[$letterIndex] 1
        if ($planetTypeSelected[$letterIndex] = 0)
          setVar $planetTypeSelected[$letterIndex]  1
        elseif ($planetTypeSelected[$letterIndex] = 1)
          setVar $planetTypeSelected[$letterIndex]  0
        end
      end
    end
  end
return


#  :checkCommandLocation
#
#  Ensure script is at Command prompt.
:checkCommandLocation
:checkCommandPrompt
  cutText CURRENTLINE $location 1 7
  if ($location <> "Command")
    clientMessage "This script must be run from the standard console."
    halt
  end
return
