# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# 
# For source notes please refer to Notes.txt
# For license terms please refer to GPL.txt.
# 
# These files should be stored in the root of the compression you 
# received this source in.

# Trans-Warp Colonize with Wait
# Karl Leifeste, 2007
# Code ganked from Xide's scriptpack 1.
# Description      : Move colonists from earth to another planet.  Requires
#                    Transporter on planet which can reach sector 1.  Requires
#                    fuel ore on a planet in the destination sector.  Requires
#                    multiple planets in target sector or planet scanner.  
#                    Requires transwarp drive.  
#                    Planet transporter must be on destination planet.
# Trigger Point    : Destination planet surface

# check if we can run it from here
cutText CURRENTLINE $location 1 14

if ($location <> "Planet command")
  clientMessage "This script must be run from the surface of a planet"
  halt
end

logging off

echo "*TransWarp Beam Colonizer 0.20**"

getinput $fuelPlanet "Enter planet ID for fuel ore source" 0
getinput $citadelPlanet "Enter planet ID where citadel transporter is located." 0

#getinput $terraSector "Enter sector of planet to move to (S = same sector)" 0
setVar $terraSector 1
#getinput $quantity "Enter quantity to move" 0
getInput $trips "Enter trips to make" 0
getInput $timeLimitMinutes "Enter time limit in minutes" 0
setVar $timeLimitSeconds ($timeLimitMinutes * 60)
setVar $timeLimit ($timeLimitSeconds * 1000)

#setDelayTrigger timeout :timeout $timeLimit

# Set trigger to stop script if disconnected
setEventTrigger 0 :End "Connection lost"

# get the planet number for the planet we're on
send "d"
setTextLineTrigger 1 :getPlanet "Planet #"
pause
:getPlanet
getWord CURRENTLINE $destPlanet 2
stripText $destPlanet "#"

setVar $waitString "Planet command"
waitFor $waitString

# get the sector we're in
send "qd"

setTextLineTrigger 1 :getSector "Sector  : "
pause
:getSector
getWord CURRENTLINE $destSector 3

setVar $waitString "Command ["
waitFor $waitString

# find turns remaining
send "i"
setTextLineTrigger 1 :getTurnsLeft "Turns left     : "
pause
:getTurnsLeft
getWord CURRENTLINE $turnsLeft 4


# get the distance between sectors  ACHTUNG:  Assumes same distance
if ($destSector = $terraSector) or ($terraSector = "S") or ($terraSector = "s")
  setVar $distance 0
else
  send "cf" $terraSector "*" $destSector "*"
  setTextLineTrigger 1 :getDistance1 "The shortest path"
  pause
  :getDistance1
  getWord CURRENTLINE $distance 4
  stripText $distance "("
  
  send "f" "*" $terraSector "*"
  setTextLineTrigger 2 :getDistance2 "The shortest path"
  pause
  :getDistance2
  getWord CURRENTLINE $beamDistance 4
  stripText $beamDistance "("
end

setVar $waitString "Computer command"
waitFor $waitString

send "q"

# get the planet transport power
send "l" 
setVar $waitString "Land on which planet <Q to abort> ?"
waitFor $waitString

send $citadelPlanet "*"
setTextLineTrigger 1 :getTransportPower "      -=-=-=-=-=- TransPort power = "
pause
:getTransportPower
getWord CURRENTLINE $transportPower 5


# check planet transport power vs. required hops.  
#clientMessage ($distance)
#clientMessage ($transportPower)
if ($beamDistance > $transportPower)
  clientMessage ("TransportPower not great enough to reach Terra")
  halt
end

# get the total free holds in this ship
setTextLineTrigger 1 :getHolds "You have "
pause
:getHolds
getWord CURRENTLINE $holds 3

# calculate required fuel ore to return from terra
setVar $fuelOreRequired ($distance * 3)

# don't run script without free cargo holds
#clientMessage ($holds)
#clientMessage ($fuelOreRequired)
if ($holds < $fuelOreRequired)
  clientMessage "Not enough free cargo holds to make return transwarp!"
  halt
end


# Since this is mass colonization, the question is how many trips to make
# based on turns.  Do we have enough turns?

setVar $turnsRequired ($trips * 7)
if ($turnsLeft < $turnsRequired)
  clientMessage ("Not enough turns to make that many trips.")
  halt
end


setTextTrigger 1 :sub_Done "There aren't that many on the planet"
setVar $twarp 0

# at this point in initialization, we are on the citadelPlanet.
# at the end of a move, we are on the destPlanet.  Let's get the
# ship on the destination planet for consistency.

if ($citadelPlanet <> $destPlanet)
  send "q"
  setVar $waitString "Command"
  waitFor $waitString
  send "l"
  setVar $waitString "Land on which planet"
  waitFor $waitString
  send $destPlanet "*"
  setVar $waitString "Planet command"
  waitFor $waitString
  # Ship is now located on destination planet
end

# START MOVEMENT LOOP
:sub_Move
  # get fuel ore off the fuel ore planet.  Ship is on destPlanet
  if ($fuelPlanet <> $destPlanet)
    send "ql" 
    setVar $waitString "Land on which planet <Q to abort> ?"
    waitFor $waitString
    send $fuelPlanet "*"
    setVar $waitString "Planet command (?=help) [D]"
    waitFor $waitString  
  end
  # Load up fuel for the return trip.
  send "tnt1" $fuelOreRequired "*"
  # calculate free holds
  setVar $freeholds $holds
  subtract $freeHolds $fuelOreRequired
  
  # Move ship to citadelPlanet.  Ship is currently on fuelPlanet
  if ($fuelPlanet <> $citadelPlanet)
    send "ql"
    setVar $waitString "Land on which planet <Q to abort> ?"
    waitFor $waitString
    send $citadelPlanet "*"
    setVar $waitString "Planet command (?=help) [D]"
    waitFor $waitString
  end
  
  # Enter citadel, beam to terraSector
  send "c "
  setVar $waitString "Citadel command"
  waitFor $waitString  
  send "b " 
  setVar $waitString "Beam to what sector?"
  waitFor $waitString
  send $terraSector "*"
  
  # Verify lock
  setTextTrigger 50 :beamLocked "Locating beam pinpointed, TransWarp Locked."
  setTextTrigger 51 :beamUnlocked "*** WARNING ***"
  pause
  :beamLocked
  killTrigger 51
  goto :beamlockAcquired
  :beamUnlocked
  killTrigger 50
  clientMessage ("Transport beam lock not acquired")
  send "n"
  halt
  :beamlockAcquired
  send "y"

  setVar $waitString "Command ["
  waitFor $waitString

  # Land on Terra, grab colonists  
  setVar $timer 0
  
  while ($freeHolds > 0)
    :grabColonists
    killTrigger grabColonists
   
    if ($timer = 1)
      send "l1*"
      echo "*Debug:  Timer = 1*"
      setDelayTrigger wait :wait 90000
      pause
      :wait
      killTrigger wait
      setVar $waitString "colonists ready to leave Terra."
      setTextTrigger colonistsReady2 :colonistsReady $waitString
      send "ql 1*"
      pause
    end
    if ($timer = 0)     
      send "l1*"
      setVar $waitString "colonists ready to leave Terra."
      setTextTrigger colonistsReady :colonistsReady $waitString
    end
    send "1* "
    pause
    :colonistsReady
    setVar $colonistLine1 CURRENTLINE
    getWord $colonistLine1 $colonistsAvailable 4
  
    setVar $waitString "Do you wish to (L)eave or (T)ake Colonists?"
    waitFor $waitString
    send "t"
    setVar $waitString "] empty holds) ?"
    waitFor $waitString
    setVar $colonistLine2 CURRENTLINE
    getWord $colonistLine2 $freeHolds 11 
    stripText $freeHolds "("
    stripText $freeHolds "[" 
    stripText $freeHolds "]"
    echo "*Debug:  Freeholds = " $freeHolds "*"
    echo "*Debug:  Taking " $colonistsAvailable " colonists.*"
    setTextLineTrigger grabColonists :grabColonists "There aren't that many on Terra!"
    if ($freeHolds > $colonistsAvailable)
      send $colonistsAvailable "*"
      setVar $timer 1
      echo "*Debug:  Freeholds:  " $freeHolds "  Colonists Available:  " $colonistsAvailable "*"
      echo "*Debug:  Setting timer to 1"
    end
    if ($freeHolds <= $colonistsAvailable)
      send "*"
      setVar $timer 0
      echo "*Debug:  Setting timer to 0"
      # There's a race condition here.  If the colonists are snagged first,
      # Our ship will probably warp back without the colonists.  Oh well.
    end
    subtract $freeHolds $colonistsAvailable

  end
    

  # return
  if ($distance > 0)
    send $destSector
    if (SECTORS > 5000) or ($destSector < 600)
      send "*"
    end
  end
 
  if ($distance > 1)
    if ($twarp = 0)
      # Ensure the ship can transwarp
      # so we find out now
      setTextTrigger 2 :setTwarp "Do you want to engage"
      setTextTrigger 3 :setNoTwarp "The shortest path"
      pause
      :setTwarp
      killTrigger 3
      setVar $twarp "Y"
      goto :gotTwarp
      :setNoTwarp
      killTrigger 2
      setVar $twarp "N"
      :gotTwarp
    end

    if ($twarp = "N")
      ClientMessage ("This script requires the use of a transwarp drive.  Go buy one.")
      halt
    end

    # Assume we've got the drive.  Verify Lock
    send "y"
    # Verify lock
    setTextTrigger 60 :transWarpLocked "Locating beam pinpointed, TransWarp Locked."
    setTextTrigger 61 :transWarpUnlocked "*** WARNING ***"
    pause
    :transWarpLocked
    killTrigger 61
    goto :warpLockAcquired
    :transWarpUnlocked
    killTrigger 60
    clientMessage ("Transwarp lock not acquired")
    send "n"
    halt
    :warpLockAcquired
    send "y "   
  end
  
  
  setVar $waitString "Command [TL"
  waitFor $waitString
  
  send "l " 
  setVar $waitString "Land on which planet <Q to abort>"
  waitFor $waitString
  send $destPlanet "*"
  setVar $waitString "Planet command (?=help) [D]"
  waitFor $waitString
  send "snl1*"
  #send "ql" $fuelPlanet "*tnt1" $fuelOreRequired "*q"
  #send "l" $destPlanet "*" 

  #mergeText "Planet #" $fuelPlanet $waitText
  #waitFor $waitText
  setVar $waitString "Planet command (?=help) [D]"
  waitFor $waitString
  subtract $trips 1

  

  if ($trips > 0)
    goto :sub_Move
  end
 
:sub_Done
  # transferred it all
  halt

:End
