:: miscSecExpWidgets [widget nobr]

<<widget "recalcManpower">>
	<<if $wasToggledBefore == 0>>
		<<if $mercenaries == 1>>
			<<set $mercFreeManpower = random(5,20)>>
		<<elseif $mercenaries > 1>>
			<<set $mercFreeManpower = random(10,30)>>
		<</if>>
	<</if>>
	
	<<set _correctEmployedMP = 0>>
	<<for _i = 0; _i < $slaveUnits.length; _i++>>
		<<set _correctEmployedMP += $slaveUnits[_i].troops>>
	<</for>>
	
	<<if $slavesEmployedManpower != _correctEmployedMP>>
		<<set $slavesEmployedManpower = _correctEmployedMP>>
	<</if>>

	<<set _correctEmployedMP = 0>>
	<<for _i = 0; _i < $militiaUnits.length; _i++>>
		<<set _correctEmployedMP += $militiaUnits[_i].troops>>
	<</for>>
	
	<<if $militiaEmployedManpower != _correctEmployedMP>>
		<<set $militiaEmployedManpower = _correctEmployedMP>>
	<</if>>
	
	<<set _correctEmployedMP = 0>>
	<<for _i = 0; _i < $mercUnits.length; _i++>>
		<<set _correctEmployedMP += $mercUnits[_i].troops>>
	<</for>>
	
	<<if $mercEmployedManpower != _correctEmployedMP>>
		<<set $mercEmployedManpower = _correctEmployedMP>>
	<</if>>
	
	<<set $militiaTotalManpower = $militiaEmployedManpower + $militiaFreeManpower>>
	<<set $mercTotalManpower = $mercEmployedManpower + $mercFreeManpower>>
<</widget>>

<<widget "recalcSecRestPoint">>
	<<set _newRest = 0>>
	<<set _baseRest = 30>>
	<<if $secUpgrades.nanoCams == 1>>
		<<set _newRest += 15>>
	<</if>>
	<<if $secUpgrades.cyberBots == 1>>
		<<set _newRest += 15>>
	<</if>>
	<<if $secUpgrades.eyeScan == 1>>
		<<set _newRest += 20>>
	<</if>>
	<<if $secUpgrades.cryptoAnalyzer == 1>>
		<<set _newRest += 20>>
	<</if>>
	<<set _newRest += _baseRest>>
	<<set $secRestPoint = _newRest>>
<</widget>>

<<widget "recalcCrimeCap">>
	<<set _baseCap = 100>>
	<<set _newCap = _baseCap>>
	<<if $crimeUpgrades.autoTrial == 1>>
		<<set _newCap -= 10>>
	<</if>>
	<<if $crimeUpgrades.autoArchive == 1>>
		<<set _newCap -= 10>>
	<</if>>
	<<if $crimeUpgrades.worldProfiler == 1>>
		<<set _newCap -= 15>>
	<</if>>
	<<if $crimeUpgrades.advForensic == 1>>
		<<set _newCap -= 15>>
	<</if>>
	<<set $crimeCap = _newCap>>
<</widget>>

<<widget "recalcReqHelots">>
	<<set _newReq = 0>>
	<<set _baseReq = 20>>
	<<if $secUpgrades.nanoCams == 1>>
		<<set _newReq += 5>>
	<</if>>
	<<if $secUpgrades.cyberBots == 1>>
		<<set _newReq += 5>>
	<</if>>
	<<if $secUpgrades.eyeScan == 1>>
		<<set _newReq += 10>>
	<</if>>
	<<if $secUpgrades.cryptoAnalyzer == 1>>
		<<set _newReq += 10>>
	<</if>>
	<<if $crimeUpgrades.autoTrial == 1>>
		<<set _newReq += 5>>
	<</if>>
	<<if $crimeUpgrades.autoArchive == 1>>
		<<set _newReq += 5>>
	<</if>>
	<<if $crimeUpgrades.worldProfiler == 1>>
		<<set _newReq += 10>>
	<</if>>
	<<if $crimeUpgrades.advForensic == 1>>
		<<set _newReq += 10>>
	<</if>>
	<<if $intelUpgrades.sensors == 1>>
		<<set _newReq += 5>>
	<</if>>
	<<if $intelUpgrades.signalIntercept == 1>>
		<<set _newReq += 5>>
	<</if>>
	<<if $intelUpgrades.radar == 1>>
		<<set _newReq += 10>>
	<</if>>
	<<if $readinessUpgrades.pathways == 1>>
		<<set _newReq += 5>>
	<</if>>
	<<if $readinessUpgrades.rapidVehicles == 1>>
		<<set _newReq += 5>>
	<</if>>
	<<if $readinessUpgrades.rapidPlatforms == 1>>
		<<set _newReq += 10>>
	<</if>>
	<<if $readinessUpgrades.earlyWarn == 1>>
		<<set _newReq += 10>>
	<</if>>
	<<if $SFSupportLevel >= 1>>
		<<set _newReq -= 5 * $SFSupportLevel>>
	<</if>>
	<<if $secUpgrades.coldstorage >= 1>>
		<<set _newReq -= 10 * $secUpgrades.coldstorage>>
	<</if>>
	<<set _newReq += _baseReq>>
	<<set $reqHelots = _newReq>>
<</widget>>

<<widget "recalcEdictsUpkeep">>
	/* authority cost */
	<<set _newAuthUpkeep = 0>>
	
	<<if $sellData == 1>>
		<<set _newAuthUpkeep += 10>>
	<</if>>

	<<if $weaponsLaw == 0>>
		<<set _newAuthUpkeep += 30>>
	<<elseif $weaponsLaw == 2>>
		<<set _newAuthUpkeep += 10>>
	<<elseif $weaponsLaw == 1>>
		<<set _newAuthUpkeep += 20>>
	<</if>>

	<<if $slavesOfficers == 1>>
		<<set _newAuthUpkeep += 10>>
	<</if>>
	
	/* cash cost */
	<<set _newUpkeep = 0>>
	
	<<if $slaveWatch == 1>>
		<<set _newUpkeep += 1000>>
	<</if>>

	<<if $subsidyChurch == 1>>
		<<set _newUpkeep += 1000>>
	<</if>>

	<<if $martialSchool == 1>>
		<<set _newUpkeep += 1000>>
	<</if>>
	
	<<if $legionTradition == 1>>
		<<set _newUpkeep += 1000>>
	<</if>>
	
	<<if $pharaonTradition == 1>>
		<<set _newUpkeep += 1000>>
	<</if>>
	
	<<if $eagleWarriors == 1>>
		<<set _newUpkeep += 1000>>
	<</if>>
	
	<<if $ronin == 1>>
		<<set _newUpkeep += 1000>>
	<</if>>

	<<if $mamluks == 1>>
		<<set _newUpkeep += 1000>>
	<</if>>
	
	<<if $sunTzu == 1>>
		<<set _newUpkeep += 1000>>
	<</if>>
	
	<<set $edictsUpkeep = _newUpkeep>>
	<<set $edictsAuthUpkeep = _newAuthUpkeep>>
<</widget>>

<<widget "fixBrokenUnits">>
	<<for _i = 0; _i < $militiaUnits.length; _i++>>
		<<if ndef $militiaUnits[_i].SF>>
			<br>Set militia missing flag
			<<set $militiaUnits[_i].SF = 0>>
		<</if>>
		<<if !Number.isInteger($militiaUnits[_i].troops)>>
			<<set $militiaUnits[_i].troops = $militiaUnits[_i].maxTroops>>
			<br>Fixed militia unit wrong troop count.
		<</if>>
	<</for>>

	<<for _i = 0; _i < $slaveUnits.length; _i++>>
		<<if ndef $slaveUnits[_i].SF>>
			<br>Set slave missing flag
			<<set $slaveUnits[_i].SF = 0>>
		<</if>>
		<<if !Number.isInteger($slaveUnits[_i].troops)>>
			<<set $slaveUnits[_i].troops = $slaveUnits[_i].maxTroops>>
			<br>Fixed slave unit wrong troop count.
		<</if>>
	<</for>>
	
	<<for _i = 0; _i < $mercUnits.length; _i++>>
		<<if ndef $mercUnits[_i].SF>>
			<br>Set merc missing flag
			<<set $mercUnits[_i].SF = 0>>
		<</if>>
		<<if !Number.isInteger($mercUnits[_i].troops)>>
			<<set $mercUnits[_i].troops = $mercUnits[_i].maxTroops>>
			<br>Fixed merc unit wrong troop count.
		<</if>>
	<</for>>
<</widget>>

<<widget "fixBrokenStats">>
	<<if !Number.isInteger($totalKills)>>
		<<set $totalKills = 0>>
	<</if>>
	<<if !Number.isInteger($mercTotalCasualties)>>
		<<set $mercTotalCasualties = 0>>
	<</if>>
	<<if !Number.isInteger($mercEmployedManpower)>>
		<<set $mercEmployedManpower = 0>>
	<</if>>
	<<if !Number.isInteger($mercTotalManpower)>>
		<<set $mercTotalManpower = 0>>
	<</if>>
	<<if !Number.isInteger($slavesTotalCasualties)>>
		<<set $slavesTotalCasualties = 0>>
	<</if>>
	<<if !Number.isInteger($slavesEmployedManpower)>>
		<<set $slavesEmployedManpower = 0>>
	<</if>>
	<<if !Number.isInteger($militiaTotalCasualties)>>
		<<set $militiaTotalCasualties = 0>>
	<</if>>
	<<if !Number.isInteger($militiaEmployedManpower)>>
		<<set $militiaEmployedManpower = 0>>
	<</if>>
	<<if !Number.isInteger($militiaFreeManpower)>>
		<<set $militiaFreeManpower = 0>>
	<</if>>
	<<if !Number.isInteger($militiaTotalManpower)>>
		<<set $militiaTotalManpower = 0>>
	<</if>>
	<<if !Number.isInteger($battlesCount)>>
		<<set $battlesCount = 0>>
	<</if>>
<</widget>>