#
# Karl's overcolonized planet checker
#
#  Checks for planets with too many colonists
#  Checks for planets with maxed goods and colonists
#  Checks for planets that could use seme colonists
#

goSub :checkCommandPrompt

goSub :quikStats

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
cutText $thisline $planetClass[$i] 9 1
#echo "**" $i " " $planetClass[$i] " from " $thisline "**"
getWord $thisline $planetType[$i] 4
setVar $planetName[$i] "Planet"
setVar $planetType $i
setVar $planetClass $i
setVar $planetLetter $i
goTo :readPlanetLoop
:planetClassListFinished
send "qq"
waitFor "Computer command [TL="
waitFor "Command [TL="

goSub :planetLimits

setVar $overloadedPlanets 0
setVar $goodsPlanets 0
setVar $underloadedFuel 0
setVar $underloadedOrg 0
setVar $underloadedEquip 0
goSub :collectPlanetStats

setDelayTrigger delay :delay 50
pause
:delay 

echo ANSI_8 "**Too many colonists:*"
goSub :displayOverloadedPlanets
echo ANSI_8 "**Goods maxed:*"
goSub :displayMaxGoodsPlanets
	echo ANSI_8 "**[Planets in these sectors have room]*"
goSub :displayUnderloadedPlanets

halt


##################################################
#  :displayUnderloadedPlanets
##################################################
:displayUnderloadedPlanets


setVar $GameName GAMENAME
mergeText $GameName "-destinationList" $thisRoute
delete $thisRoute
setVar $i 1
while ($i <= $underloadedFuel)
  write $thisRoute $underloadedFuel[$i]
  add $i 1
end
echo ANSI_8 "Fuel Ore colonists needed in:  "
echo ANSI_4 "*"
setVar $plotRoute~startSector $quikstats[SECT]
goSub :plotRoute~quiet
echo ANSI_4 "*"


setVar $GameName GAMENAME
mergeText $GameName "-destinationList" $thisRoute
delete $thisRoute
setVar $i 1
while ($i <= $underloadedOrg)
  write $thisRoute $underloadedOrg[$i] 
  add $i 1
end
echo ANSI_8 "Organics colonists needed in:  "
echo ANSI_4 "*"
setVar $plotRoute~startSector $quikstats[SECT]
goSub :plotRoute~quiet
echo ANSI_4 "*"


setVar $GameName GAMENAME
mergeText $GameName "-destinationList" $thisRoute
delete $thisRoute
setVar $i 1
while ($i <= $underloadedEquip)
  write $thisRoute $underloadedEquip[$i]
  add $i 1
end
echo ANSI_8 "Equipment colonists needed in:  "
echo ANSI_4 "*"
setVar $plotRoute~startSector $quikstats[SECT]
goSub :plotRoute~quiet
echo ANSI_4 "*"

return


##################################################
#  :displayMaxGoodsPlanets
##################################################
:displayMaxGoodsPlanets
  setVar $i 1
  while ($i <= $goodsPlanets)
    echo ANSI_4 $goodsSector[$i] ", " $goodsName[$i] " Class " $goodsClass[$i] " L-" $goodsLevel[$i] " Maxed out in " $goodsTypeMaxed[$i] ", Colonists: " $goodsColonists[$i] "*"
    add $i 1
  end
return

##################################################
#  :displayOverloadedPlanets
##################################################
:displayOverloadedPlanets
  setVar $i 1
  while ($i <= $overloadedPlanets)
    echo ANSI_4 $overloadedSector[$i] ", " $overloadedName[$i] ", " $overloadedClass[$i] "-" $overloadedLevel[$i] ", by " $overloadedColonists[$i] " more than " $overloadedLimit[$i] "*"
    add $i 1
  end
return

##################################################
#  :collectPlanetStats
##################################################
:collectPlanetStats
  send "tl"
  waitFor "Corporate Planet Scan"
  waitFor "============================================================================"
  killTrigger endOfPlanetList
  setTextLineTrigger endOfPlanetList :endOfPlanetList "======   ============  ==== ==== ==== ===== ===== ===== ========== =========="
  while (1)
    goSub :setPlanetTriggers1
    pause
    :planetLineTrigger1
    goSub :killPlanetTriggers1
    setVar $planetSummaryLine1 CURRENTLINE
    cutText $planetSummaryLine1 $sector  2 5
    cutText $planetSummaryLine1 $planetName 10 30
    cutText $planetSummaryLine1 $thisPlanetClass 47 1
    cutText $planetSummaryLine1 $planetClassFullName 50 18
    cutText $planetSummaryLine1 $citadelLevel 77 1
    if ($citadelLevel = "l")
      setVar $citadelLevel 0
    end
    setTextLineTrigger parens :parens "("
    pause
    :parens
    setVar $planetSummaryLine2 CURRENTLINE
    getWord $planetSummaryLine2 $colonists 2
    getWord $planetSummaryLine2 $fuelProd 3
    getWord $planetSummaryLine2 $orgProd 4
    getWord $planetSummaryLine2 $equipProd 5
    getWord $planetSummaryLine2 $fuel 6
    getWord $planetSummaryLine2 $org 7
    getWord $planetSummaryLine2 $equip 8
    stripText $colonists "("
    stripText $colonists ")"
    setVar $val $colonists
    goSub :parseVal
    setVar $colonists $val
    divide $colonists 1000
    goSub :evaluateColonists
    goSub :evaluateGoods
  end 
  :endOfPlanetList
  waitFor "Corporate command [TL="
  send "q"
  waitFor "Command [TL="
return


##################################################
#  :evaluateGoods
#  arguments:  $colonists $fuelProd $orgProd $equipProd $fuel $org $equip 
#              $planetLimit[] $thisPlanetClass $planetClassFullName
#              $planetClass[] $planetName $citadelLevel
#  returns:    $goodsSector[] $goodsName[] $goodsLevel[] 
#              $goodsColonists[] $goodsClass[] $goodsPlanets
##################################################
:evaluateGoods
  #write "logfile2" "[" &  $planetClassFullName &  "] " & $fuelProd & " " & $fuel & "*"
  setVar $goodsMaxed 0
  setVar $underloaded 0
  #write "logfile" "thisPlanetClass=" & $thisPlanetClass & " colonists=" & $colonists
  if ($planetClassFullName = "Volcanic          ")
    setVar $val $fuelProd
    goSub :parseVal
    setVar $fuelProd $val
    setVar $val $fuel
    goSub :parseVal
    setVar $fuel $val
    if ($fuelProd > 10000) AND ($fuel >= 1000000)
      setVar $goodsMaxed 1
      setVar $itemMaxed "Fuel Ore"
    end
    if ($fuelProd < 35000) AND ($fuel < 600000) AND ($citadelLevel >= 3)
      add $underloadedFuel 1
      setVar $underloadedFuel[$underloadedFuel] $sector
    end
  end
  if ($planetClassFullName = "Oceanic           ")
    setVar $val $orgProd
    goSub :parseVal
    setVar $orgProd $val
    setVar $val $org
    goSub :parseVal
    setVar $org $val
    if ($orgProd > 0) AND ($org >= 1000000)
      setVar $goodsMaxed 1
      setVar $itemMaxed "Organics"
    end
    if ($orgProd < 35000) AND ($org < 1000000) AND ($citadelLevel >= 3)
      add $underloadedOrg 1
      setVar $underloadedOrg[$underloadedOrg] $sector
    end
  end
  if ($planetClassFullName = "Mineral/Iron      ")
    setVar $val $equipProd
    goSub :parseVal
    setVar $equipProd $val
    setVar $val $equip
    goSub :parseVal
    setVar $equip $val
    if ($equipProd > 0) AND ($equip >= 500000)
      setVar $goodsMaxed 1
      setVar $itemMaxed "Equipment"
    end
    if ($equipProd < 5000) AND ($equip < 500000) AND ($citadelLevel >= 1)
      add $underloadedEquip 1
      setVar $underloadedEquip[$underloadedEquip] $sector
    end
  end
  if ($goodsMaxed = 1)
    add $goodsPlanets 1
    setVar $goodsSector[$goodsPlanets] $sector
    setVar $goodsName[$goodsPlanets] $planetName
    setVar $goodsColonists[$goodsPlanets] $colonists
    setVar $goodsClass[$goodsPlanets] $thisPlanetClass
    setVar $goodsLevel[$goodsPlanets] $citadelLevel
    setVar $goodsTypeMaxed[$goodsPlanets] $itemMaxed
    setVar $goodsFuel[$goodsPlanets] $fuel
    setVar $goodsOrg[$goodsPlanets] $org
    setVar $goodsEquip[$goodsPlanets] $equip
    #write "logfile" $goodsSector[$goodsPlanets]  & " " & $goodsName[$goodsPlanets] & " " & $goodsLevel[$goodsPlanets] & " " & $goodsColonists[$goodsPlanets] & " f:" & $goodsFuel[$goodsPlanets]  & " f:" & $goodsFuel[$goodsPlanets]  & " f:" & $goodsFuel[$goodsPlanets] & " o:" & $goodsOrg[$goodsPlanets] & " e:" & $goodsEquip[$goodsPlanets] & "*"
  end
return

##################################################
#  :evaluateColonists
#  arguments:  $colonists, $planetLimit[] $thisPlanetClass $planetClass[], $planetName, $citadelLevel
#  returns:    $overloadedSector[] $overloadedName[] $overloadedLevel[] $overloadedColonists[] $overLoadedClass[] $overloadedPlanets
##################################################
:evaluateColonists
  setVar $i 0
  #write "logfile" "thisPlanetClass=" & $thisPlanetClass & " colonists=" & $colonists
  while ($i <= $planetClass)
    if ($planetClass[$i] = $thisPlanetClass)
      setVar $difference $planetLimit[$i]
      subtract $difference $colonists
      #write "logfile" "  i=" & $i & " planetClass[i]=" & $planetClass[$i] & "  planetLimit[i]=" & $planetLimit[$i] & " diff=" & $difference & "*"
      #echo "*class=" $planetClass[$i] ", limit=" $planetLimit[$i] ", colonists= " $colonists "*"
      if ($difference<0) 
        add $overloadedPlanets 1
        setVar $overloadedSector[$overloadedPlanets] $sector
        setVar $overloadedName[$overloadedPlanets] $planetName
        setVar $overloadedClass[$overloadedPlanets] $thisPlanetClass
        setVar $overloadedLevel[$overloadedPlanets] $citadelLevel
        setVar $overloadedColonists[$overloadedPlanets] $difference
        multiply $overloadedColonists[$overloadedPlanets] "-1"
        setVar $overloadedLimit[$overloadedPlanets] $planetLimit[$i]
        #write "logfile" $overloadedSector[$overloadedPlanets]  & " " & $overloadedName[$overloadedPlanets] & " " & $overloadedLevel[$overloadedPlanets] & " " & $overloadedColonists[$overloadedPlanets]
      end
    end
    add $i 1
  end
return


##################################################
#  :setPlanetTriggers1
##################################################
:setPlanetTriggers1
  setTextLineTrigger noCitadel :planetLineTrigger1 " No Citadel"
  setTextLineTrigger levelOne :planetLineTrigger1 " Level 1"
  setTextLineTrigger levelTwo :planetLineTrigger1 " Level 2"
  setTextLineTrigger levelThree :planetLineTrigger1 " Level 3"
  setTextLineTrigger levelFour :planetLineTrigger1 " Level 4"
  setTextLineTrigger levelFive :planetLineTrigger1 " Level 5"
  setTextLineTrigger levelSix :planetLineTrigger1 " Level 6"
return

##################################################
#  :killPlanetTriggers1
##################################################
:killPlanetTriggers1
  killTrigger noCitadel
  killTrigger levelOne
  killTrigger levelTwo
  killTrigger levelThree
  killTrigger levelFour
  killTrigger levelFive
  killTrigger levelSix
return



##################################################
#  :planetLimits
##################################################
:planetLimits
  setVar $dataFile "data/" & GAMENAME & "-planet-population-limits"

  fileExists $dataFileExists $dataFile

  if ($dataFileExists = 0)
    setVar $planetLimit[1] 0
    setVar $planetLimit[2] 0
    setVar $planetLimit[3] 100000
    setVar $planetLimit[4] 0
    setVar $planetLimit[5] 0
    setVar $planetLimit[6] 50000
    setVar $planetLimit[7] 0
    setVar $planetLimit[8] 25000
    setVar $planetLimit 8
  else
    readToArray $dataFile $planetLimit
  end

  setVar $userInput "0"

  while ($userInput <> "Q")

    # display the planet letters and limits
    echo ANSI_3 "*=================================*"
    echo ANSI_3 "Set total population limits"
    echo ANSI_3 "=================================*"
    echo ANSI_4 "     [Type]"
    echo ANSI_10 " - [limit]*"
    setVar $pindex 0
    while ($pindex < $planetType)
      add $pindex 1
      echo ANSI_5 "<" $planetLetter[$pindex] ">  " 
      echo ANSI_4 $planetType[$pindex] 
      echo ANSI_5 " - "
      echo ANSI_10 $planetLimit[$pindex] "*"
    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")
      fileExists $dataFileExists $dataFile
      if ($dataFileExists = 1)
        delete $dataFile
      end
      setVar $i 1
      while ($i <= $planetLimit)
        write $dataFile $planetLimit[$i]
        add $i 1
      end
      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 Population Limit:  "
        getConsoleInput $testLimit
        isNumber $testNumber $testLimit
        if ($testNumber = 1)
          setVar $planetLimit[$letterIndex] $testLimit
        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


# parseVal operates on local variable $val
# 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


#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


include "lib\plotRoute.ts"

