
goSub :checkCommandPrompt~main

echo ANSI_4 "**Refresh port list?  "
getConsoleInput $getPortList SINGLEKEY
lowerCase $getPortList

echo ANSI_4 "**Refresh fighter list?  "
getConsoleInput $getFigList SINGLEKEY
lowerCase $getFigList

send "*"
waitFor "Command [TL="
if ($getFigList = "y")
  goSub :writeFigs~main 
end
if ($getPortList = "y")
  send "^?"
  waitFor "<Q> Quit"
  send "r?"
  waitFor "<Q> Quit"
  send "q"
end


goSub :readFigs

goSub :findSectorsWithPlanetsAndPorts

mergeText GAMENAME "-destinationList" $destinationFile 
delete $destinationFile
setVar $i 0
echo "*"
while ($i < SECTORS)
  add $i 1
  ##echo ANSI_4 "*[i=" $i ": " $planetEquipSector[$i] " " $planetOrgSector[$i] "]*"
  if ((PORT.EQUIP[$i] > 31000) AND ($planetEquipSector[$i] > 31000)) OR ((PORT.ORG[$i] > 31000) AND ($planetOrgSector[$i] > 31000))
    if (PORT.BUYFUEL[$i] = 1)
      setVar $c1 "b"
    else 
      setVar $c1 "s"
    end
    if (PORT.BUYORG[$i] = 1)
      setVar $c2 "b"
    else 
      setVar $c2 "s"
    end
    if (PORT.BUYEQUIP[$i] = 1)
      setVar $c3 "b"
    else 
      setVar $c3 "s"
    end
    if (figgedSector[$i] <> 0)
      echo ANSI_4 $i " " $c1 $c2 $c3 " " PORT.FUEL[$i] " " PORT.PERCENTFUEL[$i] "% " PORT.ORG[$i] " " PORT.PERCENTORG[$i] "% "  PORT.EQUIP[$i] " " PORT.PERCENTEQUIP[$i] "% " PORT.UPDATED[$i] "*"
      write $destinationFile $i
    end
    if ($figgedSector[$i] = 0)
      echo ANSI_5 $i " " $c1 $c2 $c3 " " PORT.FUEL[$i] " " PORT.PERCENTFUEL[$i] "% " PORT.ORG[$i] " " PORT.PERCENTORG[$i] "% "  PORT.EQUIP[$i] " " PORT.PERCENTEQUIP[$i] "% " PORT.UPDATED[$i] " [nofig]*"
    end
  end
end

goSub :waitCommandPrompt~main
goSub :plotRoute~main

halt

:readFigs
  setVar $figFile GAMENAME & "-figreport"
  fileExists $fileExists $figFile
  if ($fileExists = 1)
    setVar $lineNumber 0
    setVar $figLine 0
    while ($figLine <> EOF)
      add $lineNumber 1
      read $figFile $figLine $lineNumber
    end
    setVar $lineCount $lineNumber
    subtract $lineCount 1
  end

  setArray $figgedSector SECTORS
  setVar $i 1
  while ($i <= SECTORS)
    setVar $figgedSector[$i] 0
    add $i 1
  end

  setVar $lineNumber 0
  setVar $figLine " "
  while ($figLine <> EOF)
    add $lineNumber 1
    read $figFile $figLine $lineNumber
    if ($figLine <> EOF)
      getWord $figLine $sector 1
      getWord $figLine $figCount 2
      setVar $figgedSector[$sector] $figcount
      #echo $sector " " $figgedSector[$sector] " *"
    end
  end

  ## Debug
  #setVar $i 1
  #while ($i <= SECTORS)
  #  echo $figgedSector[$i] " "
  #  add $i 1
  #end
return


#  Get a list of planets.
#  Find their sector # and how much org/equ on each.
#  If geq $minRequired, put results in two SECTORS sized arrays
#  Gotcha:  I do not want a summary of goods per sector, I want 
#  to know the largest quantity of goodies on any planet in the
#  sector.  We cannot get a planet number from this list; however,
#  we can at least save the largest single quantity.
#  
:findSectorsWithPlanetsAndPorts
  setArray $planetEquipSector SECTORS
  setArray $planetOrgSector SECTORS

  setVar $i 1
  while ($i <= SECTORS)
    setVar $planetEquipSector[$i] 0
    setVar $planetOrgSector[$i] 0
    add $i 1
  end

  send "tl"
  waitFor "Corporate command [TL="
  waitFor "Corporate Planet Scan"
  waitFor "=============================================================================="
  killTrigger endOfPlanetList
  setTextTrigger endOfPlanetList :endOfPlanetList "Corporate command [TL"
  while (1)
    killTrigger parens
    setTextLineTrigger orgPlanet :possiblePlanet "Oceanic"
    setTextLineTrigger equPlanet :possiblePlanet "Mineral/Iron"
    pause
    :possiblePlanet
    killTrigger orgPlanet
    killTrigger equPlanet
    setVar $planetSummaryLine1 CURRENTLINE
    getWord $planetSummaryLine1 $sector 1
    setTextLineTrigger parens :parens "("
    pause
    :parens
    setVar $planetSummaryLine2 CURRENTLINE
    getWord $planetSummaryLine2 $org 7
    getWord $planetSummaryLine2 $equ 8
    #echo ANSI_4 "*[" $planetSummaryLine2 "]*"

    setVar $val $org  
    goSub :parseVal
    setVar $org $val

    setVar $val $equ
    goSub :parseVal
    setVar $equ $val

    if ($planetEquipSector[$sector] < $equ)
      setVar $planetEquipSector[$sector] $equ
    end

    if ($planetOrgSector[$sector] < $org)
      setVar $planetOrgSector[$sector] $org
    end

  end
  :endOfPlanetList
  killalltriggers
  send "q"
return

# parseVal operates on local variable $var
# anyone know what the maximum integer size is?
:parseVal
  #echo ANSI_5 "*val [" $val "]*"
  setVar $base $val
  #echo ANSI_5 "*base [" $base "*]"
  stripText  $base "T"
  stripText  $base "M" 
  stripText  $base "B"
  #echo ANSI_5 "*base [" $base "*]"
  stripText  $val "0"
  stripText  $val "1"
  stripText  $val "2"
  stripText  $val "3"
  stripText  $val "4"
  stripText  $val "5"
  stripText  $val "6"
  stripText  $val "7"
  stripText  $val "8"
  stripText  $val "9"
  #echo ANSI_5 "*val [" $val "]*"
  if ($val = "T") 
    setVar $val 1000
  elseif ($val = "M")
    setVar $val 1000000
  elseif ($val = "B")
    setVar $val 1000000000
  else
    setVar $val 1
  end
  multiply $val $base
return

halt
include "lib\plotRoute.ts"
include "lib\waitCommandPrompt.ts"
include "lib\checkCommandPrompt.ts"
include "lib\writeFigs.ts"
