#
# 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

goSub :collectPlanetStats

##echo "**"
##goSub :displaySectorSummary

goSub :sortPlanetArrays

goSub :checkNeighbors

echo "**"
goSub :displaySectorSummary

halt



##################################################
#  :sortPlanetArrays
##################################################
:sortPlanetArrays
  setArray $ps $planetCount
  setArray $pc $planetCount
  setArray $pl $planetCount


##  echo "count=" $planetCount "*"
  setVar $i 0
  setVar $j 1
  while ($i < ($planetCount -1))
    add $i 1
    setVar $smallestIndex $i
    setVar $j $i
    while ($j < $planetCount)
      add $j 1
##      echo $planetSector[$j] " "
      if ($planetSector[$j] < $planetSector[$smallestIndex]) 
##        echo "<"
        setVar $DEBUGVAR $smallestIndex
        setVar $smallestIndex $j
      end
##      echo " " $planetSector[$DEBUGVAR] "*"
    end
##if ($i >3)
##  halt
##end
    # smallest Index should be in $smallestIndex.  swap with $i
##    echo $planetSector[$smallestIndex] "*"
    if ($planetSector[$smallestIndex] <> $planetSector[$i])
##      echo "swapping " $smallestIndex ":" $planetSector[$smallestIndex] " with " $i ":" $planetSector[$i] "*"
      setVar $tempSector $planetSector[$i]
      setVar $tempClass  $planetClass[$i]
      setVar $tempLevel  $citadelLevel[$i]
      setVar $planetSector[$i] $planetSector[$smallestIndex]
      setVar $planetClass[$i]  $planetClass[$smallestIndex]
      setVar $citadelLevel[$i] $citadelLevel[$smallestIndex]
      setVar $planetSector[$smallestIndex] $tempSector
      setVar $planetClass[$smallestIndex] $tempClass
      setVar $citadelLevel[$smallestIndex] $tempLevel
##      echo "swapped " $smallestIndex "=" $planetSector[$smallestIndex] ", " $i "=" $planetSector[$i] "*"
    end
  end
return

##################################################
#  :checkNeighbors
#   put the sectors in array sector[].
##################################################
:checkNeighbors
  setVar $i 1
  setVar $inSectorCount 0
  setVar $lastSector 0
  setVar $sectorIndex 1
  while ($i <= $planetCount)
    if ($inSectorCount = 1)
      setVar $sector[$sectorIndex] $planetSector[$i]
    end
    if ($planetSector[$i]=$lastSector)
      add $inSectorCount 1
    else
      setVar $inSectorCount 1
    end
    add $i 1
  end
return

##################################################
#  :displaySectorSummary
##################################################
:displaySectorSummary
  setvar $colCount 0
  setVar $columns 3
  setVar $i 1
  setVar $lastSector 0
  setVar $inSectorCount 99 
  setVar $lineFeed 0
  echo "   "
  while ($i <= $planetCount) 
    # case to display new sector
    if ($planetSector[$i] <> $lastSector)

      # buffer with spaces
      setVar $j 5
      while ($inSectorCount < $j)
        echo "   "
        add $inSectorCount 1
      end

      # linefeed
      if ($lineFeed = 3)
        echo "*    "
        setVar $lineFeed 0
      elseif ($inSectorCount >= 6)
        echo " "
      else
        echo "      "
      end
    
      add $lineFeed 1
      
      setVar $lastSector $planetSector[$i]
      echo ANSI_4  $planetSector[$i] " " 
      setVar $inSectorCount 1
    # not a new sector.  add one.
    else
      add $inSectorCount 1 
    end

    # case for planets 1-5
    if ($inSectorCount <= 5)
      echo  $planetClass[$i] $citadelLevel[$i]  " "
    # case for planet 6
    elseif ($inSectorCount = 6)
      echo "...  "
    end

    add $i 1
  end
return

##################################################
#  :collectPlanetStats
##################################################
:collectPlanetStats
  setVar $planetCount 0
  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 :addPlanetToArray
  end 
  :endOfPlanetList
  waitFor "Corporate command [TL="
  send "q"
  waitFor "Command [TL="
return


##################################################
#  :addPlanetToArray
##################################################
:addPlanetToArray
  add $planetCount 1
  setVar $planetSector[$planetCount] $sector
  setVar $citadelLevel[$planetCount] $citadelLevel
  setVar $planetClass[$planetCount]  $thisPlanetClass
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"

