code:
:: seeUnit [nobr]

<<if $attackThisWeek == 1>>
	<<set $nextButton = "Back", $nextLink = "attackOption", $returnTo = "secBarracks">>
<<else>>
	<<set $nextButton = "Back", $nextLink = "secBarracks", $returnTo = "secBarracks">>
<</if>>

<<if $targetUnit == "secBots">>
	<<secBotsDescription>>
	<<if $secBots.maxTroops > $secBots.troops>>

		<<link "Replenish the unit">>
			<<set $cash -= ($secBots.maxTroops - $secBots.troops) * $secBotsCost>>
			<<set $secBots.troops = $secBots.maxTroops>>
			<<goto "seeUnit">>
		<</link>>
	<</if>>

	<<if $secBots.maxTroops < 80>>

		<<link "Improve the digital control matrix">>
			<<set $secBots.maxTroops += 10>>
			<<set $cash -= 5000>>
			<<goto "seeUnit">>
		<</link>>
		Invest in the development of more refined controls for your drones to increase the maximum number of drones in the unit.

//Costs 5000 per upgrade and each will increase the max by 10//
	<<elseif $secBots.maxTroops < 100 && $SFSupportLevel >= 1>>

		<<link "Refine the drone network with $securityForceName assistance">>
		<<set $secBots.maxTroops += 10>>
			<<set $cash -= 5000>>
			<<goto "seeUnit">>
		<</link>>
		Utilize the technological developments made by $securityForceName to further improve the control matrix of the security drones.

//Costs 5000 per upgrade and each will increase the max by 10//
	<<elseif $SFSupportLevel < 1 && $securityForceCreate == 1>>
		There's little left to improve in the matrix. However support from $securityForceName might give some more room from improvement.
	<<else>>
		There's little left to improve in the matrix. Your control systems are at top capacity and won't be able to handle bigger a drone unit.
	<</if>>
	<<if $secBots.equip < 3>>

		<<link "Improve drone weaponry and armor">>
			<<set $secBots.equip += 1>>
			<<set $cash -= (($secBotsUpgradeCost * $secBots.maxTroops) + 1000)>>
			<<goto "seeUnit">>
		<</link>>
		Invest in better equipment for your drones to increase their battle effectiveness.

//Costs <<print (($secBotsUpgradeCost * $secBots.maxTroops) + 1000)>> and will increase attack and defense value of the unit by 15% for every upgrade.//
	<<else>>

		Your drones are equipped with top tier weaponry and armor.
	<</if>>
	<<if $showBattleStatistics == 1>>

Security drones base attack: $secBotsBaseAttack <<if $droneUpgrades.attack > 0>> + 1 <</if>>(<<print Math.round($secBotsBaseAttack + $secBotsBaseAttack * $secBots.equip * 0.15)>>)

Security drones base defense: $secBotsBaseDefense <<if $droneUpgrades.defense > 0>> + 1 <</if>>(<<print Math.round($secBotsBaseDefense + $secBotsBaseDefense * $secBots.equip * 0.15)>>)

Equipment bonus: + <<print $secBots.equip * 15>>%

Security drones base hp: $secBotsBaseHp <<if $droneUpgrades.hp > 0>> + 1 <</if>>

Security drones base morale: $secBotsMorale
	<</if>>
<<elseif $targetUnit == "militiaUnits">>
	<<militiaUnitsDescription $militiaUnits[$targetIndex]>>

	Rename unit <<textbox "$militiaUnits[$targetIndex].platoonName" $militiaUnits[$targetIndex].platoonName "seeUnit">>
	<<if $militiaUnits[$targetIndex].maxTroops > $militiaUnits[$targetIndex].troops && $militiaFreeManpower > 0>>

		<<link "Replenish unit">>
			<<if $militiaFreeManpower >= $militiaUnits[$targetIndex].maxTroops - $militiaUnits[$targetIndex].troops>>
				<<set $militiaFreeManpower -= $militiaUnits[$targetIndex].maxTroops - $militiaUnits[$targetIndex].troops>>
				<<set $militiaEmployedManpower += $militiaUnits[$targetIndex].maxTroops - $militiaUnits[$targetIndex].troops>>
				<<set _expLoss = ($militiaUnits[$targetIndex].maxTroops - $militiaUnits[$targetIndex].troops) / $militiaUnits[$targetIndex].troops>>
				<<set $militiaUnits[$targetIndex].training -= $militiaUnits[$targetIndex].training * _expLoss>>
				<<set $militiaUnits[$targetIndex].troops = $militiaUnits[$targetIndex].maxTroops>>
			<<else>>
				<<set $militiaEmployedManpower += $militiaFreeManpower>>
				<<set _expLoss = $militiaFreeManpower / $militiaUnits[$targetIndex].troops>>
				<<set $militiaUnits[$targetIndex].training -= $militiaUnits[$targetIndex].training * _expLoss>>
				<<set $militiaUnits[$targetIndex].troops += $militiaFreeManpower>>
				<<set $militiaFreeManpower = 0>>
			<</if>>
			<<goto "seeUnit">>
		<</link>>
	<</if>>

	<<if $militiaUnits[$targetIndex].maxTroops < 50>>

		<<link "Intensive officers training">>
			<<set $militiaUnits[$targetIndex].maxTroops += 10>>
			<<set $cash -= 5000>>
			<<goto "seeUnit">>
		<</link>>
		Invest in the training of your officers to increase the maximum number of soldiers in the unit.

//Costs 5000 per upgrade and each will increase the max by 10//
	<<else>>

		Your officers rached their peak. Further training will have little impact on the number of troops they can effectively lead.
	<</if>>
	<<if $militiaUnits[$targetIndex].equip < 3>>

		<<link "Improve weaponry and equipment">>
			<<set $militiaUnits[$targetIndex].equip += 1>>
			<<set $cash -= ($equipUpgradeCost * $militiaUnits[$targetIndex].maxTroops) + 1000>>
			<<goto "seeUnit">>
		<</link>>
		Invest in better equipment for your soldiers to increase their battle effectiveness.

//Costs <<print ($equipUpgradeCost * $militiaUnits[$targetIndex].maxTroops) + 1000>> and will increase attack and defense value of the unit by 15% for every upgrade.//
	<<else>>

		The unit is equipped with state of the art weaponry and equipment.
	<</if>>
	<<if $militiaUnits[$targetIndex].commissars == 0>>

		<<link "Attach commissars to the unit">>
			<<set $militiaUnits[$targetIndex].commissars = 1>>
			<<set $cash -= $equipUpgradeCost * $militiaUnits[$targetIndex].maxTroops + 1000>>
			<<goto "seeUnit">>
		<</link>>
		Attach a small squad of commissars to the unit.

//Costs <<print ($equipUpgradeCost * $militiaUnits[$targetIndex].maxTroops) + 1000>> and will slowly increase the loyalty of the unit.//
	<<elseif $militiaUnits[$targetIndex].commissars < 2>>

		<<link "Intensive loyalty training">>
			<<set $militiaUnits[$targetIndex].commissars += 1>>
			<<set $cash -= $equipUpgradeCost * $militiaUnits[$targetIndex].maxTroops + 1000>>
			<<goto "seeUnit">>
		<</link>>
		Provide special training for the officers and the commissars of the unit.

//Costs <<print ($equipUpgradeCost * $militiaUnits[$targetIndex].maxTroops) + 1000>> and will increase the loyalty of the unit faster.//
	<<elseif $militiaUnits[$targetIndex].commissars == 1>>

The unit has a commissar detachment, keeping under control the ambitions of the unit's officers.
	<<else>>

The unit has a perfectly trained and loyal commissar detachment, keeping under control the ambitions of the unit's officers.
	<</if>>
	<<if $militiaUnits[$targetIndex].medics == 0>>

		<<link "Attach trained medics to the unit">>
			<<set $militiaUnits[$targetIndex].medics = 1>>
			<<set $cash -= $equipUpgradeCost * $militiaUnits[$targetIndex].maxTroops + 1000>>
			<<goto "seeUnit">>
		<</link>>
		Attach a small squad of trained medics to the unit.

//Costs <<print ($equipUpgradeCost * $militiaUnits[$targetIndex].maxTroops) + 1000>> and will decrease the number of casualties suffered during battle.//
	<<else>>

The unit has a medic detachment following it into battle, decreasing the number of casualties the unit suffers.
	<</if>>
	<<if $securityForceActive == 1>>
		<<if $militiaUnits[$targetIndex].SF == 0>>

			<<link "Attach Special Force advisors">>
				<<set $militiaUnits[$targetIndex].SF = 1>>
				<<set $cash -= ($equipUpgradeCost * $militiaUnits[$targetIndex].maxTroops) + 5000>>
				<<goto "seeUnit">>
			<</link>>
			Attach $securityForceName advisors to the unit.

//Costs <<print ($equipUpgradeCost * $militiaUnits[$targetIndex].maxTroops) + 5000>> and will slightly increase the base stats of the unit.//
		<<else>>

The unit has attached advisors from $securityForceName that will help the squad remain tactically aware and active.
		<</if>>
	<</if>>

	<<if $showBattleStatistics == 1>>
		<<if $militiaUnits[$targetIndex].training <= 10>>
			<<set _expBonus = 0>>
		<<elseif $militiaUnits[$targetIndex].training <= 33>>
			<<set _expBonus = 10>>
		<<elseif $militiaUnits[$targetIndex].training <= 66>>
			<<set _expBonus = 25>>
		<<else>>
			<<set _expBonus = 50>>
		<</if>>
		<<if $militiaUnits[$targetIndex].loyalty <= 10>>
			<<set _loyaltyBonus = 0>>
		<<elseif $militiaUnits[$targetIndex].loyalty <= 33>>
			<<set _loyaltyBonus = 10>>
		<<elseif $militiaUnits[$targetIndex].loyalty <= 66>>
			<<set _loyaltyBonus = 20>>
		<<else>>
			<<set _loyaltyBonus = 30>>
		<</if>>

Militia base attack: $militiaBaseAttack <<if $humanUpgrade.attack > 0>> + $humanUpgrade.attack <</if>>(<<print Math.round($militiaBaseAttack + $militiaBaseAttack * $militiaUnits[$targetIndex].equip * $equipMod + $militiaBaseAttack * _expBonus * 0.01 + $militiaBaseAttack * _loyaltyBonus * 0.01 + $militiaBaseAttack * $militiaUnits[$targetIndex].SF * 0.20)>>)

Militia base defense: $militiaBaseDefense <<if $humanUpgrade.defense > 0>> + $humanUpgrade.defense <</if>>(<<print Math.round($militiaBaseDefense + $militiaBaseDefense * $militiaUnits[$targetIndex].equip * $equipMod + $militiaBaseDefense * _expBonus * 0.01 + $militiaBaseDefense * _loyaltyBonus * 0.01 + $militiaBaseDefense * $militiaUnits[$targetIndex].SF * 0.20)>>)

Equipment bonus: + <<print $militiaUnits[$targetIndex].equip * 15>>%
		<<if _expBonus > 0>>

Experience bonus: +<<print _expBonus>>%
		<</if>>
		<<if _loyaltyBonus > 0>>

Loyalty bonus: +<<print _loyaltyBonus>>%
		<</if>>
		<<if $militiaUnits[$targetIndex].SF > 0>>

Special Force advisors bonus: +20%
		<</if>>

Militia base morale: $militiaBaseMorale <<if $humanUpgrade.morale > 0>> + $humanUpgrade.morale <</if>>(<<print Math.round($militiaBaseMorale + $militiaBaseMorale * $secBarracksUpgrades.luxury * 0.05)>>)
		<<if $secBarracksUpgrades.luxury > 0>>

Barracks bonus: + <<print $secBarracksUpgrades.luxury * 5>>%
		<</if>>

Militia base hp: $militiaBaseHp <<if $humanUpgrade.hp > 0>> + $humanUpgrade.hp <</if>>(<<print Math.round($militiaBaseHp + $militiaBaseHp * $militiaUnits[$targetIndex].medics * 0.25)>>)
		<<if $militiaUnits[$targetIndex].medics > 0>>

Medics detachment bonus: +25%
		<</if>>
	<</if>>
<<elseif $targetUnit == "slaveUnits">>
	<<slaveUnitsDescription $slaveUnits[$targetIndex]>>

	Rename unit <<textbox "$slaveUnits[$targetIndex].platoonName" $slaveUnits[$targetIndex].platoonName "seeUnit">>
	<<if $slaveUnits[$targetIndex].maxTroops > $slaveUnits[$targetIndex].troops && $helots > 0>>

		<<link "Replenish unit">>
			<<if $helots >= $slaveUnits[$targetIndex].maxTroops - $slaveUnits[$targetIndex].troops>>
				<<set $helots -= $slaveUnits[$targetIndex].maxTroops - $slaveUnits[$targetIndex].troops>>
				<<set $slavesEmployedManpower += $slaveUnits[$targetIndex].maxTroops - $slaveUnits[$targetIndex].troops>>
				<<set _expLoss = ($slaveUnits[$targetIndex].maxTroops - $slaveUnits[$targetIndex].troops) / $slaveUnits[$targetIndex].troops>>
				<<set $slaveUnits[$targetIndex].training -= $slaveUnits[$targetIndex].training * _expLoss>>
				<<set $slaveUnits[$targetIndex].troops = $slaveUnits[$targetIndex].maxTroops>>
			<<else>>
				<<set $slavesEmployedManpower += $helots>>
				<<set _expLoss = $helots / $slaveUnits[$targetIndex].troops>>
				<<set $slaveUnits[$targetIndex].training -= $slaveUnits[$targetIndex].training * _expLoss>>
				<<set $slaveUnits[$targetIndex].troops += $helots>>
				<<set $helots = 0>>
			<</if>>
			<<goto "seeUnit">>
		<</link>>
	<</if>>

	<<if $slaveUnits[$targetIndex].maxTroops < 50>>

		<<link "Intensive officers training">>
			<<set $slaveUnits[$targetIndex].maxTroops += 10>>
			<<set $cash -= 5000>>
			<<goto "seeUnit">>
		<</link>>
		Invest in the training of your officers to increase the maximum number of soldiers in the unit.

//Costs 5000 per upgrade and each will increase the max by 10//
	<<else>>

Your officers rached their peak. Further training will have little impact on the number of troops they can effectively lead.
	<</if>>
	<<if $slaveUnits[$targetIndex].equip < 3>>

		<<link "Improve weaponry and equipment">>
			<<set $slaveUnits[$targetIndex].equip += 1>>
			<<set $cash -= ($equipUpgradeCost * $slaveUnits[$targetIndex].maxTroops) + 1000>>
			<<goto "seeUnit">>
		<</link>>
		Invest in better equipment for your soldiers to increase their battle effectiveness.

//Costs <<print ($equipUpgradeCost * $slaveUnits[$targetIndex].maxTroops) + 1000>> and will increase attack and defense value of the unit by 15% for every upgrade.//
	<<else>>

The unit is equipped with state of the art weaponry and equipment.
	<</if>>
	<<if $slaveUnits[$targetIndex].commissars == 0>>

		<<link "Attach commissars to the unit">>
			<<set $slaveUnits[$targetIndex].commissars = 1>>
			<<set $cash -= $equipUpgradeCost * $slaveUnits[$targetIndex].maxTroops + 1000>>
			<<goto "seeUnit">>
		<</link>>
		Attach a small squad of commissars to the unit.

//Costs <<print ($equipUpgradeCost * $slaveUnits[$targetIndex].maxTroops) + 1000>> and will slowly increase the loyalty of the unit.//
	<<elseif $slaveUnits[$targetIndex].commissars < 2>>

		<<link "Intensive loyalty training">>
			<<set $slaveUnits[$targetIndex].commissars += 1>>
			<<set $cash -= $equipUpgradeCost * $slaveUnits[$targetIndex].maxTroops + 1000>>
			<<goto "seeUnit">>
		<</link>>
		Provide special training for the officers and the commissars of the unit.

//Costs <<print ($equipUpgradeCost * $slaveUnits[$targetIndex].maxTroops) + 1000>> and will	increase the loyalty of the unit faster.//
	<<elseif $slaveUnits[$targetIndex].commissars == 1>>

The unit has a commissar detachment, keeping under control the ambitions of the unit's officers.
	<<else>>

The unit has a perfectly trained and loyal commissar detachment, keeping under control the ambitions of the unit's officers.
	<</if>>
	<<if $slaveUnits[$targetIndex].medics == 0>>

		<<link "Attach trained medics to the unit">>
			<<set $slaveUnits[$targetIndex].medics = 1>>
			<<set $cash -= $equipUpgradeCost * $slaveUnits[$targetIndex].maxTroops + 1000>>
			<<goto "seeUnit">>
		<</link>>
		Attach a small squad of trained medics to the unit.

//Costs <<print ($equipUpgradeCost * $slaveUnits[$targetIndex].maxTroops) + 1000>> and will decrease the number of casualties suffered during battle.//
	<<else>>

The unit has a medic detachment following it into battle, decreasing the number of casualties the unit suffers.
	<</if>>
	<<if $securityForceActive == 1>>
		<<if $slaveUnits[$targetIndex].SF == 0>>

			<<link "Attach Special Force advisors">>
				<<set $slaveUnits[$targetIndex].SF = 1>>
				<<set $cash -= ($equipUpgradeCost * $slaveUnits[$targetIndex].maxTroops) + 5000>>
				<<goto "seeUnit">>
			<</link>>
			Attach $securityForceName advisors to the unit.

//Costs <<print ($equipUpgradeCost * $slaveUnits[$targetIndex].maxTroops) + 5000>> and will slightly increase the base stats of the unit.//
		<<else>>

The unit has attached advisors from $securityForceName that will help the squad remain tactically aware and active.
		<</if>>
	<</if>>
	<<if $showBattleStatistics == 1>>
		<<if $slaveUnits[$targetIndex].training <= 10>>
			<<set _expBonus = 0>>
		<<elseif $slaveUnits[$targetIndex].training <= 33>>
			<<set _expBonus = 10>>
		<<elseif $slaveUnits[$targetIndex].training <= 66>>
			<<set _expBonus = 25>>
		<<else>>
			<<set _expBonus = 50>>
		<</if>>
		<<if $slaveUnits[$targetIndex].loyalty <= 10>>
			<<set _loyaltyBonus = 0>>
		<<elseif $slaveUnits[$targetIndex].loyalty <= 33>>
			<<set _loyaltyBonus = 10>>
		<<elseif $slaveUnits[$targetIndex].loyalty <= 66>>
			<<set _loyaltyBonus = 20>>
		<<else>>
			<<set _loyaltyBonus = 30>>
		<</if>>

Slaves base attack: $slaveBaseAttack <<if $humanUpgrade.attack > 0>> + $humanUpgrade.attack <</if>>(<<print Math.round($slaveBaseAttack + $slaveBaseAttack * $slaveUnits[$targetIndex].equip * $equipMod + $slaveBaseAttack * _expBonus * 0.01 + $slaveBaseAttack * _loyaltyBonus * 0.01 + $slaveBaseAttack * $slaveUnits[$targetIndex].SF * 0.20)>>)

Slaves base defense: $slaveBaseDefense <<if $humanUpgrade.defense > 0>> + $humanUpgrade.defense <</if>>(<<print Math.round($slaveBaseDefense + $slaveBaseDefense * $slaveUnits[$targetIndex].equip * $equipMod + $slaveBaseDefense * _expBonus * 0.01 + $slaveBaseDefense * _loyaltyBonus * 0.01 + $slaveBaseDefense * $slaveUnits[$targetIndex].SF * 0.20)>>)

Equipment bonus: + <<print $slaveUnits[$targetIndex].equip * 15>>%
		<<if _expBonus > 0>>

Experience bonus: +<<print _expBonus>>%
		<</if>>
		<<if _loyaltyBonus > 0>>

Loyalty bonus: +<<print _loyaltyBonus>>%
		<</if>>
		<<if $slaveUnits[$targetIndex].SF > 0>>

Special Force advisors bonus: +20%
		<</if>>

Slaves base morale: $slaveBaseMorale <<if $humanUpgrade.morale > 0>> + $humanUpgrade.morale <</if>>(<<print Math.round($slaveBaseMorale + $slaveBaseMorale * $secBarracksUpgrades.luxury * 0.05)>>)
		<<if $secBarracksUpgrades.luxury > 0>>

Barracks bonus: + <<print $secBarracksUpgrades.luxury * 5>>%
		<</if>>

Slaves base hp: $slaveBaseHp <<if $humanUpgrade.hp > 0>> + $humanUpgrade.hp <</if>>(<<print Math.round($slaveBaseHp + $slaveBaseHp * $slaveUnits[$targetIndex].medics * 0.25)>>)
		<<if $slaveUnits[$targetIndex].medics > 0>>

Medics detachment bonus: +25%
		<</if>>
	<</if>>
<<elseif $targetUnit == "mercUnits">>
	<<mercUnitsDescription $mercUnits[$targetIndex]>>

	Rename unit <<textbox "$mercUnits[$targetIndex].platoonName" $mercUnits[$targetIndex].platoonName "seeUnit">>
	<<if $mercUnits[$targetIndex].troops < $mercUnits[$targetIndex].maxTroops && $mercFreeManpower > 0>>

		<<link "Replenish unit">>
			<<if $mercFreeManpower >= $mercUnits[$targetIndex].maxTroops - $mercUnits[$targetIndex].troops>>
				<<set $mercFreeManpower -= $mercUnits[$targetIndex].maxTroops - $mercUnits[$targetIndex].troops>>
				<<set $mercEmployedManpower += $mercUnits[$targetIndex].maxTroops - $mercUnits[$targetIndex].troops>>
				<<set _expLoss = ($mercUnits[$targetIndex].maxTroops - $mercUnits[$targetIndex].troops) / $mercUnits[$targetIndex].troops>>
				<<set $mercUnits[$targetIndex].training -= $mercUnits[$targetIndex].training * _expLoss>>
				<<set $mercUnits[$targetIndex].troops = $mercUnits[$targetIndex].maxTroops>>
			<<else>>
				<<set $mercEmployedManpower += $mercFreeManpower>>
				<<set _expLoss = $mercFreeManpower / $mercUnits[$targetIndex].troops>>
				<<set $mercUnits[$targetIndex].training -= $mercUnits[$targetIndex].training * _expLoss>>
				<<set $mercUnits[$targetIndex].troops += $mercFreeManpower>>
				<<set $mercFreeManpower = 0>>
			<</if>>
			<<goto "seeUnit">>
		<</link>>
	<</if>>

	<<if $mercUnits[$targetIndex].maxTroops < 50>>

		<<link "Intensive officers training">>
			<<set $mercUnits[$targetIndex].maxTroops += 10>>
			<<set $cash -= 5000>>
			<<goto "seeUnit">>
		<</link>>
		Invest in the training of your officers to increase the maximum number of soldiers in the unit.

//Costs 5000 per upgrade and each will increase the max by 10//
	<<else>>

Your officers rached their peak. Further training will have little impact on the number of troops they can effectively lead.
	<</if>>
	<<if $mercUnits[$targetIndex].equip < 3>>

		<<link "Improve weaponry and equipment">>
			<<set $mercUnits[$targetIndex].equip += 1>>
			<<set $cash -= ($equipUpgradeCost * $mercUnits[$targetIndex].maxTroops) + 1000>>
			<<goto "seeUnit">>
		<</link>>
		Invest in better equipment for your soldiers to increase their battle effectiveness.

//Costs <<print ($equipUpgradeCost * $mercUnits[$targetIndex].maxTroops) + 1000>> and will increase attack and defense value of the unit by 15% for every upgrade.//
	<<else>>

The unit is equipped with state of the art weaponry and equipment.
	<</if>>
	<<if $mercUnits[$targetIndex].commissars == 0>>

		<<link "Attach commissars to the unit">>
			<<set $mercUnits[$targetIndex].commissars = 1>>
			<<set $cash -= $equipUpgradeCost * $mercUnits[$targetIndex].maxTroops + 1000>>
			<<goto "seeUnit">>
		<</link>>
		Attach a small squad of commissars to the unit.

//Costs <<print ($equipUpgradeCost * $mercUnits[$targetIndex].maxTroops) + 1000>> and will slowly increase the loyalty of the unit.//
	<<elseif $mercUnits[$targetIndex].commissars < 2>>

		<<link "Intensive loyalty training">>
			<<set $mercUnits[$targetIndex].commissars += 1>>
			<<set $cash -= $equipUpgradeCost * $mercUnits[$targetIndex].maxTroops + 1000>>
			<<goto "seeUnit">>
		<</link>>
		Provide special training for the officers and the commissars of the unit.

//Costs <<print ($equipUpgradeCost * $mercUnits[$targetIndex].maxTroops) + 1000>> and will increase the loyalty of the unit faster.//
	<<elseif $mercUnits[$targetIndex].commissars == 1>>

The unit has a commissar detachment, keeping under control the ambitions of the unit's officers.
	<<else>>

The unit has a perfectly trained and loyal commissar detachment, keeping under control the ambitions of the unit's officers.
	<</if>>
	<<if $mercUnits[$targetIndex].medics == 0>>

		<<link "Attach trained medics to the unit">>
			<<set $mercUnits[$targetIndex].medics = 1>>
			<<set $cash -= ($equipUpgradeCost * $mercUnits[$targetIndex].maxTroops) + 5000>>
			<<goto "seeUnit">>
		<</link>>
		Attach a small squad of trained medics to the unit.

//Costs <<print ($equipUpgradeCost * $mercUnits[$targetIndex].maxTroops) + 5000>> and will decrease the number of casualties suffered during battle.//
	<<else>>

The unit has a medic detachment following it into battle, decreasing the number of casualties the unit suffers.
	<</if>>
	<<if $securityForceActive == 1>>
		<<if $mercUnits[$targetIndex].SF == 0>>

			<<link "Attach Special Force advisors">>
				<<set $mercUnits[$targetIndex].SF = 1>>
				<<set $cash -= ($equipUpgradeCost * $mercUnits[$targetIndex].maxTroops) + 5000>>
				<<goto "seeUnit">>
			<</link>>
			Attach $securityForceName advisors to the unit.

//Costs <<print ($equipUpgradeCost * $mercUnits[$targetIndex].maxTroops) + 5000>> and will slightly increase the base stats of the unit.//
		<<else>>

The unit has attached advisors from $securityForceName that will help the squad remain tactically aware and active.
		<</if>>
	<</if>>
	<<if $showBattleStatistics == 1>>
		<<if $mercUnits[$targetIndex].training <= 10>>
			<<set _expBonus = 0>>
		<<elseif $mercUnits[$targetIndex].training <= 33>>
			<<set _expBonus = 10>>
		<<elseif $mercUnits[$targetIndex].training <= 66>>
			<<set _expBonus = 25>>
		<<else>>
			<<set _expBonus = 50>>
		<</if>>
		<<if $mercUnits[$targetIndex].loyalty <= 10>>
			<<set _loyaltyBonus = 0>>
		<<elseif $mercUnits[$targetIndex].loyalty <= 33>>
			<<set _loyaltyBonus = 10>>
		<<elseif $mercUnits[$targetIndex].loyalty <= 66>>
			<<set _loyaltyBonus = 20>>
		<<else>>
			<<set _loyaltyBonus = 30>>
		<</if>>

Mercenaries base attack: $mercBaseAttack <<if $humanUpgrade.attack > 0>> + $humanUpgrade.attack <</if>>(<<print Math.round($mercBaseAttack + $mercBaseAttack * $mercUnits[$targetIndex].equip * $equipMod + $mercBaseAttack * _expBonus * 0.01 + $mercBaseAttack * _loyaltyBonus * 0.01 + $mercBaseAttack * $mercUnits[$targetIndex].SF * 0.20)>>)

Mercenaries base defense: $mercBaseDefense <<if $humanUpgrade.defense > 0>> + $humanUpgrade.defense <</if>>(<<print Math.round($mercBaseDefense + $mercBaseDefense * $mercUnits[$targetIndex].equip * $equipMod + $mercBaseDefense * _expBonus * 0.01 + $mercBaseDefense * _loyaltyBonus * 0.01 + $mercBaseDefense * $mercUnits[$targetIndex].SF * 0.20)>>)

Equipment bonus: + <<print $mercUnits[$targetIndex].equip * 15>>%
		<<if _expBonus > 0>>

Experience bonus: +<<print _expBonus>>%
		<</if>>
		<<if _loyaltyBonus > 0>>

Loyalty bonus: +<<print _loyaltyBonus>>%
		<</if>>
		<<if $mercUnits[$targetIndex].SF > 0>>

Special Force advisors bonus: +20%
		<</if>>

Mercenaries base morale: $mercBaseMorale <<if $humanUpgrade.morale > 0>> + $humanUpgrade.morale <</if>>(<<print Math.round($mercBaseMorale + $mercBaseMorale * $secBarracksUpgrades.luxury * 0.05)>>)
		<<if $secBarracksUpgrades.luxury > 0>>

Barracks bonus: + <<print $secBarracksUpgrades.luxury * 5>>%
		<</if>>

Mercenaries base hp: $mercBaseHp <<if $humanUpgrade.hp > 0>> + $humanUpgrade.hp <</if>>(<<print Math.round($mercBaseHp + $mercBaseHp * $mercUnits[$targetIndex].medics * 0.25)>>)
		<<if $mercUnits[$targetIndex].medics > 0>>

Medics detachment bonus: +25%
		<</if>>
	<</if>>
<</if>>


code:
:: tradeReport [nobr]

<<if $week < 30>>
	The world economy is in good enough shape to sustain economic growth. Trade flows liberally in all the globe.<<set _tradeChange += 1>>
<<elseif $week < 60>>
	The world economy is deteriorating, but still in good enough shape to sustain economic growth.<<set _tradeChange += 0.5>>
<<elseif $week < 90>>
	The world economy is deteriorating, but still in decent enough shape to sustain economic growth.
<<elseif $week < 120>>
	The world economy is deteriorating and the slowing down of global growth is starting to have some effect on trade flow.<<set _tradeChange -= 1>>
<<else>>
	The world economy is heavily deteriorated. The slowing down of global growth has a great engative effect on trade flow.<<set _tradeChange -= 2>>
<</if>>

<<set _tradeChange = 0>>
<<if $lastAttackWeeks < 2 && $hasFoughtOnce == 1>>
	The recent attack has a negative effect on the trade of the arcology.<<set _tradeChange -= 1>>
<<elseif $lastAttackWeeks < 4 && $hasFoughtOnce == 1>>
	While some time has passed, the last attack still has a negative effect on the commercial activity of the arcology.<<set _tradeChange -= 0.5>>
<</if>>
<<if $lastRebellionWeeks < 2 && $hasRebelledOnce == 1>>
	The recent rebellion has a negative effect on the trade of the arcology.<<set _tradeChange -= 1>>
<<elseif $lastRebellionWeeks < 4 && $hasRebelledOnce == 1>>
	While some time has passed, the last rebellion still has a negative effect on the commercial activity of the arcology.<<set _tradeChange -= 0.5>>
<</if>>

<<if $terrain == "urban">>
	Since your arcology is located in the heart of an urban area, its commerce is naturally vibrant.
	<<set _tradeChange++>>
<</if>>
<<if $terrain == "ravine">>
	Since your arcology is located in the heart of a ravine, its commerce is hindered by a lack of accessability.
	<<set _tradeChange -= 0.5>>
<</if>>

<<if $PC.career == "wealth" || $PC.career == "capitalist" || $PC.career == "celebrity">>
	<<set _tradeChange += 1>>
<<elseif $PC.career == "escort" || $PC.career == "servant" || $PC.career == "gang">>
	<<set _tradeChange -= 0.5>>
<</if>>

<<if $rep > 18000>>
	Your extremely high reputation attracts trade from all over the world.
<<elseif $rep > 12000>>
	Your high reputation attracts trade from all over the world.
<</if>>

<<if $assistantPower == 1>>
	Thanks to the computing power available to her, $assistantName is able to guide the commercial development of the arcology to greater levels. <<set _tradeChange++>>
<<elseif $assistantPower == 2>>
	Thanks to the incredible computing power available to her, $assistantName is able to guide the commercial development of the arcology to greater levels. <<set _tradeChange += 2>>
<</if>>

<<if $tradeLegalAid == 1>>
	Your support in legal matters for new businesses helps improve the economic dynamicity of your arcology, boosting commercial activities.<<set _tradeChange += 1>>
<</if>>

<<if $taxTrade == 1>>
	The fees imposed on transitioning goods do little to earn you the favor of the companies making use of your arcology.<<set _tradeChange -= 1>>
<</if>>

<<if $weapManu == 1>>
	The weapons manufacturing facility of the arcology attracts a significant amount of trade.
	<<set _tradeChange += 0.5 * ($weapProductivity + $weapLab)>>
<</if>>
<<if $transportHub == 1>>
	<<if $airport == 1>>
		The arcology's airport, while small, helps facilitate the commercial development of the arcology.<<set _tradeChange += 1>>
	<<elseif $airport == 2>>
		The arcology's airport, while fairly small, helps facilitate the commercial development of the arcology.<<set _tradeChange += 1.5>>
	<<elseif $airport == 3>>
		The arcology's airport helps facilitate the commercial development of the arcology.<<set _tradeChange += 2>>
	<<elseif $airport == 4>>
		The arcology's airport is a great boon to the commercial development of the arcology.<<set _tradeChange += 2.5>>
	<<else>>
		The arcology's airport is an incredible boon to the commercial development of the arcology.<<set _tradeChange += 3>>
	<</if>>

	<<if $terrain != "oceanic" || $terrain != "marine">>
		<<if $railway == 1>>
			The railway network's age and limited extension limit commercial activity.
		<<elseif $railway == 2>>
			The railway network is a great help to the commercial development of the arcology, but its limited extension hampers its potential.<<set _tradeChange += 1>>
		<<elseif $railway == 3>>
			The railway network is a great help to the commercial development of the arcology.<<set _tradeChange += 1.5>>
		<<else>>
			The railway network is a huge help to the commercial development of the arcology. Few in the world can boast such a modern and efficient transport system.<<set _tradeChange += 2>>
		<</if>>
	<<else>>
		<<if $docks == 1>>
			The docks' age and limited size limit commercial activity.
		<<elseif $docks == 2>>
			The docks are a great help to the commercial development of the arcology, but their limited size hampers its potential.<<set _tradeChange += 1>>
		<<elseif $docks == 3>>
			The docks are a great help to the commercial development of the arcology.<<set _tradeChange += 1.5>>
		<<else>>
			The docks are a huge help to the commercial development of the arcology. Few in the world can boast such a modern and efficient transport system.<<set _tradeChange += 2>>
		<</if>>
	<</if>>
<</if>>

<<if _tradeChange > 0>>
	This week @@.green;trade improved.@@
<<elseif _tradeChange == 0>>
	This week @@.yellow;trade did not change.@@
<<else>>
	This week @@.red;trade diminished.@@
<</if>>

<<if $trade <= 20>>
	The almost non-existent trade crossing the arcology @@.yellow;does little to promote growth@@.
<<elseif $trade <= 40>>
	The low level of trade crossing the arcology promotes a @@.green;slow yet steady growth@@ of its economy.
	<<set _AWeekGrowth += 1.5>>
<<elseif $trade <= 60>>
	With trade at positive levels, the @@.green;prosperity of the arcology grows more powerful@@.
	<<set _AWeekGrowth += 2.5>>
<<elseif $trade <= 80>>
	With trade at high levels, the @@.green;prosperity of the arcology grows quickly and violently@@.
	<<set _AWeekGrowth += 3.5>>
<<else>>
	With trade at extremely high levels, the @@.green;prosperity of the arcology grows with unprecedented speed@@.
	<<set _AWeekGrowth += 4.5>>
<</if>>

<<set $trade = Math.clamp($trade + _tradeChange,0,100)>>

repo_name: darkcy/fc

license: mit


code:
:: transportHub [nobr]

<<set $nextButton = "Back", $nextLink = "Main">>

The Transport Hub

You quickly reach the transport hub, where a constant stream of vehicles, people and goods greets you. Part of the structure is dedicated to air travel and the other is mainly occupied by <<if $terrain != "oceanic" || $terrain != "marine">>the rail station.<<else>> the docks.<</if>>

<<if $limitImmigration == 1 || $AntiImmigrationRep == 1>>
	Due to your strict policies concerning immigration, very few new citizens arrive in the transport hub.
<<elseif $openBorders == 1 || $ProImmigrationCash == 1>>
	Due to your liberal policies concerning immigration, the transport hub is filled with a flow of new citizens.
<</if>>

<<if $airport == 1>>
	The arcology's airport is relatively small and poorly equipped. It can handle some traffic, but nothing noteworthy.
<<elseif $airport == 2>>
	The arcology's airport is relatively small, but well equipped. It can handle some traffic, but nothing too serious.
<<elseif $airport == 3>>
	The arcology's airport is good sized and well equipped. It can handle a good amount of traffic.
<<elseif $airport == 4>>
	The arcology's airport is good sized and very well equipped. It can handle a lot of traffic.
<<else>>
	The arcology's airport is huge and very well equipped. It can handle an impressive amount of traffic.
<</if>>

<<if $terrain != "oceanic" || $terrain != "marine">>
	<<if $railway == 1>>
		The railway network is old and limited. It can handle some traffic, but not sustain commercial activity.
	<<elseif $railway == 2>>
		The railway network is modern and efficient, but limited in reach. It can handle some traffic, but not sustain commercial activity of any significant size.
	<<elseif $railway == 3>>
		The railway network is modern, efficient and expansive. It can handle a significant amount of traffic.
	<<else>>
		The railway network is high tech and very far reaching. It can handle an enormous amount of traffic.
	<</if>>
<<else>>
	<<if $docks == 1>>
		The docks are old and small. They can handle some traffic, but not sustain commercial activity.
	<<elseif $docks == 2>>
		The docks are modern and efficient, but limited in size. They can handle some traffic, but not sustain commercial activity of significant size.
	<<elseif $docks == 3>>
		The docks are modern, efficient and expansive. They can handle a significant amount of traffic.
	<<else>>
		The docks are huge in size and high tech. They can handle an enormous amount of traffic.
	<</if>>
<</if>>

<<if $hubSecurity == 1>>
	The security of the hub is limited to a few cameras and the occasional guard.
<<elseif $hubSecurity == 2>>
	The security of the hub is guaranteed by a powerful camera surveillance system.
<<elseif $hubSecurity == 3>>
	The security of the hub is guaranteed by a powerful camera surveillance system and a rapid response team constantly patrolling the structure.
<<else>>
	The security of the hub is guaranteed by a powerful camera surveillance system, a rapid response team constantly patrolling the building and additional security drones making the rounds around the exterior.
<</if>>

<<if $trade <= 20>>
	Trade is almost non-existent. Outside the supplies for the arcology's domestic consumption little else crosses the territory of the free city.
<<elseif $trade <= 40>>
	Trade is low. There's some but limited commercial activity crossing the territory of the free city.
<<elseif $trade <= 60>>
	Trade is at positive levels. There's a good amount commercial activity outside the supplies for the arcology's domestic consumption.
<<elseif $trade <= 80>>
	Trade is at high levels. There's a lot of commercial activity outside the supplies for the arcology's domestic consumption.
<<else>>
	Trade is at extremely high levels. There's a constant stream of commercial activity crossing the arcology.
<</if>>

/* airport */
<<if $airport == 1>>

	<<link "Modernize the airport">>
		<<set $cash -= 5000>>
		<<set $airport++>>
		<<goto "transportHub">>
	<</link>> //Will cost ¤5000 and will increase trade, but will affect security//
<<elseif $airport == 2>>

	<<link "Enlarge the airport">>
		<<set $cash -= 15000>>
		<<set $airport++>>
		<<goto "transportHub">>
	<</link>> //Will cost ¤15000 and will increase trade, but will affect security//
<<elseif $airport == 3>>

	<<link "Further modernize the airport">>
		<<set $cash -= 45000>>
		<<set $airport++>>
		<<goto "transportHub">>
	<</link>> //Will cost ¤45000 and will increase trade, but will affect security//
<<elseif $airport == 4>>

	<<link "Further enlarge the airport">>
		<<set $cash -= 85000>>
		<<set $airport++>>
		<<goto "transportHub">>
	<</link>> //Will cost ¤85000 and will increase trade, but will affect security//
<<else>>

	The airport is fully upgraded.
<</if>>

/* trainyard/dockyard */
<<if $terrain != "oceanic" || $terrain != "marine">>
	<<if $railway == 1>>

		<<link "Modernize the railway">>
			<<set $cash -= 10000>>
			<<set $railway++>>
			<<goto "transportHub">>
		<</link>> //Will cost ¤10000, will increase trade and slightly lower arcology's upkeep, but will affect security//
	<<elseif $railway == 2>>

		<<link "Enlarge the railway">>
			<<set $cash -= 25000>>
			<<set $railway++>>
			<<goto "transportHub">>
		<</link>> //Will cost ¤25000, will increase trade and slightly lower arcology's upkeep, but will affect security//
	<<elseif $railway == 3>>

		<<link "Further modernize and enlarge the railway">>
			<<set $cash -= 65000>>
			<<set $railway++>>
			<<goto "transportHub">>
		<</link>> //Will cost ¤65000, will increase trade and slightly lower arcology's upkeep, but will affect security//
	<<else>>

		The railway is fully upgraded.
	<</if>>
<<else>>
	<<if $docks == 1>>

		<<link "Modernize the docks">>
			<<set $cash -= 10000>>
			<<set $docks++>>
			<<goto "transportHub">>
		<</link>> //Will cost ¤10000, will increase trade and slightly lower arcology's upkeep, but will affect security//
	<<elseif $docks == 2>>

		<<link "Enlarge the docks">>
			<<set $cash -= 25000>>
			<<set $docks++>>
			<<goto "transportHub">>
		<</link>> //Will cost ¤25000, will increase trade and slightly lower arcology's upkeep, but will affect security//
	<<elseif $docks == 3>>

		<<link "Further modernize and enlarge the docks">>
			<<set $cash -= 65000>>
			<<set $docks++>>
			<<goto "transportHub">>
		<</link>> //Will cost ¤65000, will increase trade and slightly lower arcology's upkeep, but will affect security//
	<<else>>

		The docks are fully upgraded.
	<</if>>
<</if>>

/* security */
<<if $hubSecurity == 1>>

	<<link "Expand and modernize the surveillance system">>
		<<set $cash -= 15000>>
		<<set $hubSecurity++>>
		<<goto "transportHub">>
	<</link>> //Will cost ¤15000 and lower the transport hub security modifiers//
<<elseif $hubSecurity == 2>>

	<<link "Establish a rapid response team">>
		<<set $cash -= 35000>>
		<<set $hubSecurity++>>
		<<goto "transportHub">>
	<</link>> //Will cost ¤35000 and further lower the transport hub security modifiers//
<<elseif $hubSecurity == 3>>

	<<link "Add additional security drones to the structure">>
		<<set $cash -= 55000>>
		<<set $hubSecurity++>>
		<<goto "transportHub">>
	<</link>> //Will cost ¤55000 and further lower the transport hub security modifiers//
<<else>>

	The hub security is fully upgraded
<</if>>


code:
:: unitsBattleReport [nobr]

<<if $losses == 0>>
	<<if $deployingBots == 1>>
		Your squadron of security drones participated in the battle, dominating the airspace and providing vital intelligence to the rest of the army.
		During the battle they suffered no casualties.
	<</if>>
	<<if $SFIntervention == 1>>
		<<print $securityForcePersonnel>> soldiers from $securityForceName joined the battle aboard their <<if $securityForceVehiclePower >= 4>> armored <</if>> vehicles <<if $securityForceAC130 > 0>> and their AC130<</if>>.
		Free cities armies are a rare sight, but their power is undeniable and your $securityForceName is living testament of this.
		During the battle they suffered no casualties.
	<</if>>
	<<if $deployingMilitia == 1>>
		<<for _j = 0; _j < $militiaUnits.length; _j++>>
			<<if $militiaUnits[_j].isDeployed == 1>>
				$militiaUnits[_j].platoonName participated in the battle, your proud citizens defending with their lives their arcology.
				During the battle they suffered no casualties.
			<</if>>
		<</for>>
	<</if>>
	<<if $deployingSlaves == 1>>
		<<for _j = 0; _j < $slaveUnits.length; _j++>>
			<<if $slaveUnits[_j].isDeployed == 1>>
				$slaveUnits[_j].platoonName participated in the battle. While a rare sight, your slave soldiers show no less willingness to fight for their arcology than their free counterparts, be it because of fear or devotion to their owner.
				During the battle they suffered no casualties.
			<</if>>
		<</for>>
	<</if>>
	<<if $deployingMercs == 1>>
		<<for _j = 0; _j < $mercUnits.length; _j++>>
			<<if $mercUnits[_j].isDeployed == 1>>
				$mercUnits[_j].platoonName participated in the battle. Mercenaries are a common sight on the modern battlefield and yours are proud to serve their generous contractor.
				During the battle they suffered no casualties.
			<</if>>
		<</for>>
	<</if>>

<<elseif $losses > 0>>
	/* if the losses are more than zero */
	/* generates a list of randomized losses, from which each unit picks one at random */
	<<set _losses = $losses>>
	<<if $SFIntervention == 1>>
		<<set $deployedUnits++>>
	<</if>>
	<<set _averageLosses = Math.trunc(_losses / $deployedUnits)>>
	<<set _lossesList = []>>
	<<set _validityCount = 0>>
	<<for _i = 0; _i < $deployedUnits; _i++>>
		<<set _assignedLosses = Math.trunc(Math.clamp(_averageLosses + random(-5,5), 0, 100))>>
		<<if _assignedLosses > _losses>>
			<<set _assignedLosses = _losses>>
			<<set _losses = 0>>
		<<else>>
			<<set _losses -= _assignedLosses>>
		<</if>>
		<<set _lossesList.push(_assignedLosses)>>
		<<set _validityCount += _assignedLosses>>
	<</for>>
	<<if _losses > 0>>
		<<set _lossesList[random(_lossesList.length - 1)] += _losses>>
	<</if>>
	<<set _lossesList.shuffle()>>

	/* sanity check for losses */
	<<set _count = 0>>
	<<for _i = 0; _i < _lossesList.length; _i++>>
		<<if !isInt(_lossesList[_i])>>
			<<set _lossesList[_i] = 0>>
		<</if>>
		<<set _count += _lossesList[_i]>>
	<</for>>
	<<if _count < $losses>>
		<<set _lossesList[random(_lossesList.length - 1)] += $losses - _count>>
	<<elseif _count > $losses>>

@@.red;Error: assigned losses higher than actual losses@@
	<</if>>

	/* assigns the losses and notify the player */
	<<if $deployingBots == 1>>

		<<set _loss = _lossesList.pluck()>>
		<<set _loss = Math.clamp(_loss,0,$secBots.troops)>>
		<<set $secBots.troops -= _loss>>
		Your squadron of security drones participated in the battle, dominating the airspace and providing vital intelligence to the rest of the army.
		During the battle they suffered
		<<if _loss <= 0>>
			no casualties.
		<<elseif _loss <= 10>>
			light casualties.
		<<elseif _loss <= 30>>
			moderate casualties.
		<<elseif _loss <= 60>>
			heavy casualties.
		<<else>>
			catastrofic casualties.
		<</if>>
		<<if $secBots.troops <= 0>>
			<<set $secBots.active = 0>>
			Unfortunately the losses they took were simply too great, their effective combatants are in so small number you can no longer call them a deployable unit. It will take quite the investment to rebuild them.
		<<elseif $secBots.troops <= 10>>
			The unit has very few operatives left, it risks complete annihilation if deployed again.
		<</if>>
	<</if>>
	<<if $SFIntervention == 1>>

		<<set _loss = _lossesList.pluck()>>
		<<set _loss = Math.clamp(_loss,0,$securityForcePersonnel)>>
		<<set $securityForcePersonnel -= _loss>>
		<<print $securityForcePersonnel>> soldiers from the $securityForceName joined the battle aboard their <<if $securityForceVehiclePower >= 4>> armored <</if>> vehicles <<if $securityForceAC130 > 0>> and their AC130<</if>>.
		Free cities armies are a rare sight, but their power is undeniable and your $securityForceName is living testament of this.
		During the battle they suffered
		<<if _loss <= 0>>
			no casualties.
		<<elseif _loss <= 10>>
			light casualties.
		<<elseif _loss <= 30>>
			moderate casualties.
		<<elseif _loss <= 60>>
			heavy casualties.
		<<else>>
			catastrofic casualties.
		<</if>>
	<</if>>
	<<if $deployingMilitia == 1>>
		<<for _j = 0; _j < $militiaUnits.length; _j++>>
			<<if $militiaUnits[_j].isDeployed == 1>>

				<<set $militiaUnits[_j].battlesFought++>>
				<<set _loss = _lossesList.pluck()>>
				<<set _loss = Math.clamp(_loss,0,$militiaUnits[_j].troops)>>
				$militiaUnits[_j].platoonName participated in the battle, your proud citizens defending with their lives their arcology.
				During the battle they suffered
				<<if _loss <= 0>>
					no casualties
				<<elseif _loss <= 10>>
					light casualties
				<<elseif _loss <= 30>>
					moderate casualties
				<<elseif _loss <= 60>>
					heavy casualties
				<<else>>
					catastrofic casualties
				<</if>>
				<<set _med = Math.round(Math.clamp(_loss * $militiaUnits[_j].medics * 0.25,1,_loss))>>
				<<if $militiaUnits[_j].medics == 1 && _loss > 0>>
					, however some men were saved by the medics attached to this unit.
				<<else>>
					.
				<</if>>
				<<set $militiaUnits[_j].troops -= Math.trunc(Math.clamp(_loss - _med,0,$militiaUnits[_j].maxTroops))>>
				<<set $militiaEmployedManpower -= Math.trunc(_loss - _med)>>
				<<set $militiaTotalCasualties += Math.trunc(_loss - _med)>>
				<<if $militiaUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Fighting in the field increased the experience of the unit, making it more effective in future fights.
						<<set $militiaUnits[_j].training += random(5,15) + $majorBattle * random(5,15)>>
					<</if>>
				<</if>>
				<<if $militiaUnits[_j].troops <= 0>>
					<<set $militiaUnits[_j].active = 0>>

Unfortunately the losses they took were simply too great, their effective combatants are in so small number you can no longer call them a deployable unit. The remnants will be sent home honored as veterans or reorganized in a new unit.
				<<elseif $militiaUnits[_j].troops <= 10>>

The unit has very few operatives left, it risks complete annihilation if deployed again.
				<</if>>
			<</if>>
		<</for>>
	<</if>>
	<<if $deployingSlaves == 1>>
		<<for _j = 0; _j < $slaveUnits.length; _j++>>
			<<if $slaveUnits[_j].isDeployed == 1>>

				<<set $slaveUnits[_j].battlesFought++>>
				<<set _loss = _lossesList.pluck()>>
				<<set _loss = Math.clamp(_loss,0,$slaveUnits[_j].troops)>>
				$slaveUnits[_j].platoonName participated in the battle. While a rare sight, your slave soldiers show no less willingness to fight for their arcology than their free counterparts, be it because of fear or devotion to their owner.
				During the battle they suffered
				<<if _loss <= 0>>
					no casualties
				<<elseif _loss <= 10>>
					light casualties
				<<elseif _loss <= 30>>
					moderate casualties
				<<elseif _loss <= 60>>
					heavy casualties
				<<else>>
					catastrofic casualties
				<</if>>
				<<set _med = Math.round(Math.clamp(_loss * $slaveUnits[_j].medics * 0.25,1,_loss))>>
				<<if $slaveUnits[_j].medics == 1 && _loss > 0>>
					, however some men were saved by the medics attached to this unit.
				<<else>>
					.
				<</if>>
				<<set $slaveUnits[_j].troops -= Math.trunc(Math.clamp(_loss - _med,0,$slaveUnits[_j].maxTroops))>>
				<<set $slavesEmployedManpower -= Math.trunc(_loss - _med)>>
				<<set $slavesTotalCasualties += Math.trunc(_loss - _med)>>
				<<if $slaveUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Fighting in the field increased the experience of the unit, making it more effective in future fights.
						<<set $slaveUnits[_j].training += random(5,15) + $majorBattle * random(5,15)>>
					<</if>>
				<</if>>
				<<if $slaveUnits[_j].troops <= 0>>
					<<set $slaveUnits[_j].active = 0>>

Unfortunately the losses they took were simply too great, their effective combatants are in so small number you can no longer call them a deployable unit. The sorvivors will be sent home honored as veterans or reorganized in a new unit.
				<<elseif $slaveUnits[_j].troops <= 10>>

The unit has very few operatives left, it risks complete annihilation if deployed again.
				<</if>>
			<</if>>
		<</for>>
	<</if>>
	<<if $deployingMercs == 1>>
		<<for _j = 0; _j < $mercUnits.length; _j++>>
			<<if $mercUnits[_j].isDeployed == 1>>

				<<set $mercUnits[_j].battlesFought++>>
				<<set _loss = _lossesList.pluck()>>
				<<set _loss = Math.clamp(_loss,0,$mercUnits[_j].troops)>>
				$mercUnits[_j].platoonName participated in the battle. Mercenaries are a common sight on the modern battlefield and yours are proud to serve their generous contractor.
				During the battle they suffered
				<<if _loss <= 0>>
					no casualties
				<<elseif _loss <= 10>>
					light casualties
				<<elseif _loss <= 30>>
					moderate casualties
				<<elseif _loss <= 60>>
					heavy casualties
				<<else>>
					catastrofic casualties
				<</if>>
				<<set _med = Math.round(Math.clamp(_loss * $mercUnits[_j].medics * 0.25,1,_loss))>>
				<<if $mercUnits[_j].medics == 1 && _loss > 0>>
					, however some men were saved by the medics attached to this unit.
				<<else>>
					.
				<</if>>
				<<set $mercUnits[_j].troops -= Math.trunc(Math.clamp(_loss - _med,0,$mercUnits[_j].maxTroops))>>
				<<set $mercEmployedManpower -= Math.trunc(_loss - _med)>>
				<<set $mercTotalCasualties += Math.trunc(_loss - _med)>>
				<<if $mercUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Fighting in the field increased the experience of the unit, making it more effective in future fights.
						<<set $mercUnits[_j].training += random(5,15) + $majorBattle * random(5,15)>>
					<</if>>
				<</if>>
				<<if $mercUnits[_j].troops <= 0>>
					<<set $mercUnits[_j].active = 0>>

Unfortunately the losses they took were simply too great, their effective combatants are in so small number you can no longer call them a deployable unit. The remnants will be sent home honored as veterans or reorganized in a new unit.
				<<elseif $mercUnits[_j].troops <= 10>>

The unit has very few operatives left, it risks complete annihilation if deployed again.
				<</if>>
			<</if>>
		<</for>>
	<</if>>
<<else>>

@@.red;Error: losses are a negative number or NaN@@
<</if>> /* closes check for more than zero casualties */


code:
:: unitsRebellionReport [nobr]

<<if $losses == 0>>
	<<if $irregulars > 0>>
		The volounteeing citizens were quickly organized into an irregular militia unit and deployed in the arcology. While without the discipline of your other instruments, they nonetheless provided precious extra firepower and standing strength.
		During the battle they suffered no casualties.
	<</if>>
	<<if $deployingBots == 1>>

		Your squadron of security drones participated in the battle, dominating the airspace and providing vital intelligence to the rest of the army.
		During the battle they suffered no casualties.
	<</if>>
	<<if $securityForceCreate == 1>>

		$securityForceName, $securityForcePersonnel strong, was called to join the battle aboard their<<if $securityForceVehiclePower >= 4>> armored<</if>> vehicles <<if $securityForceAC130 > 0>>and their AC130<</if>>.
		They brought their unmatched strength to crush those that would want you deposed.
		During the battle they suffered no casualties.
	<</if>>
	<<set _count = 0>>
	<<if $loyalID.length > 0>>

		<<for _i = 0; _i < $militiaUnits.length; _i++>>
			<<if $militiaUnits[_i].active == 1 && ($loyalID.includes($militiaUnits[_i].ID))>>
				<<set _count++>>
				<<if _count < $loyalID.length>>
					$militiaUnits[_i].platoonName,
				<<else>>
					$militiaUnits[_i].platoonName
				<</if>>
			<</if>>
		<</for>>
		<<for _i = 0; _i < $slaveUnits.length; _i++>>
			<<if $slaveUnits[_i].active == 1 && ($loyalID.includes($slaveUnits[_i].ID))>>
				<<set _count++>>
				<<if _count < $loyalID.length>>
					$slaveUnits[_i].platoonName,
				<<else>>
					$slaveUnits[_i].platoonName
				<</if>>
			<</if>>
		<</for>>
		<<for _i = 0; _i < $mercUnits.length; _i++>>
			<<if $mercUnits[_i].active == 1 && ($loyalID.includes($mercUnits[_i].ID))>>
				<<set _count++>>
				<<if _count < $loyalID.length>>
					$mercUnits[_i].platoonName,
				<<else>>
					$mercUnits[_i].platoonName
				<</if>>
			<</if>>
		<</for>>
		participated in the battle without taking any casualties. They remained loyal until the end.
	<</if>>
	<<if $rebellingID.length > 0>>

		/* militia */
		<<set _militiaRebelledID = []>>
		<<set _militiaManpower = 0>>
		<<for _j = 0; _j < $militiaUnits.length; _j++>>
			<<if $militiaUnits[_j].active == 1 && $rebellingID.includes($militiaUnits[_j].ID)>>
				$militiaUnits[_j].platoonName,
				<<set _militiaRebelledID.push($militiaUnits[_j].ID)>>
				<<set _militiaManpower += Math.clamp($militiaUnits[_j].troops - random(_averageLosses),0,$militiaUnits[_j].troops)>>
			<</if>>
		<</for>>
		<<if _militiaRebelledID.length > 0>>
			had the gall to betray you and join your enemies.

<<link "Dissolve the units">>
					<<removeUnits _militiaRebelledID>>
					<<set $militiaFreeManpower += _militiaManpower>>
					<<for _i = 0; _i < $militiaUnits.length; _i++>>
						<<if $militiaUnits[_i].active == 1>>
							<<set $militiaUnits[_i].loyalty = Math.clamp($militiaUnits[_i].loyalty - random(10,40),0,100)>>
						<</if>>
					<</for>>
					<<recalcManpower>>
					<<replace "#militiaResult">>

Units dissolved.
					<</replace>>
				<</link>>

//Manpower will be refunded, but will negatvely influence the loyalty of the other units//

<<link "Purge the dissidents and dissolve the units">>
					<<removeUnits _militiaRebelledID>>
					<<set $militiaFreeManpower += _militiaManpower * 0.5>>
					<<recalcManpower>>
					<<replace "#militiaResult">>

Dissidents purged and units dissolved.
					<</replace>>
				<</link>>

//Will not influence the loyalty of the other units, but only half the manpower will be refunded.//

<<link "Execute them all">>
					<<removeUnits _militiaRebelledID>>
					<<for _i = 0; _i < $militiaUnits.length; _i++>>
						<<if $militiaUnits[_i].active == 1>>
							<<set $militiaUnits[_i].loyalty = Math.clamp($militiaUnits[_i].loyalty + random(10,40),0,100)>>
						<</if>>
					<</for>>
					<<recalcManpower>>
					<<replace "#militiaResult">>

Units executed. Dissent will not be tolerated.
					<</replace>>
				<</link>>

//Will positively influence the loyalty of the other units, but no manpower will be refunded.//

		<</if>>

		/* slaves */
		<<set _slaveRebelledID = []>>
		<<set _slaveManpower = 0>>

		<<for _j = 0; _j < $slaveUnits.length; _j++>>
			<<if $slaveUnits[_j].active == 1 && $rebellingID.includes($slaveUnits[_j].ID)>>
				$slaveUnits[_j].platoonName,
				<<set _slaveRebelledID.push($slaveUnits[_j].ID)>>
				<<set _slaveManpower += Math.clamp($slaveUnits[_j].troops - random(_averageLosses),0,$slaveUnits[_j].troops)>>
			<</if>>
		<</for>>
		<<if _slaveRebelledID.length > 0>>
			decided in their blind arrogance to betray you.

<<link "Dissolve the units">>
					<<removeUnits _slaveRebelledID>>
					<<set $helots += _slaveManpower>>
					<<for _i = 0; _i < $militiaUnits.length; _i++>>
						<<if $slaveUnits[_i].active == 1>>
							<<set $slaveUnits[_i].loyalty = Math.clamp($slaveUnits[_i].loyalty - random(10,40),0,100)>>
						<</if>>
					<</for>>
					<<recalcManpower>>
					<<replace "#slaveResult">>

Units dissolved.
					<</replace>>
				<</link>>

//Manpower will be refunded, but will negatively influence the loyalty of the other units//

<<link "Purge the dissidents and dissolve the units">>
					<<removeUnits _slaveRebelledID>>
					<<set $helots += _slaveManpower * 0.5>>
					<<recalcManpower>>
					<<replace "#slaveResult">>

Dissidents purged and units dissolved.
					<</replace>>
				<</link>>

//Will not influence the loyalty of the other units, but only half the manpower will be refunded.//

<<link "Execute them all">>
					<<removeUnits _slaveRebelledID>>
					<<for _i = 0; _i < $slaveUnits.length; _i++>>
						<<if $slaveUnits[_i].active == 1>>
							<<set $slaveUnits[_i].loyalty = Math.clamp($slaveUnits[_i].loyalty + random(10,40),0,100)>>
						<</if>>
					<</for>>
					<<recalcManpower>>
					<<replace "#slaveResult">>

Units executed. Dissent will not be tolerated.
					<</replace>>
				<</link>>

//Will positively influence the loyalty of the other units, but no manpower will be refunded.//

		<</if>>

		/* mercs */
		<<set _mercRebelledID = []>>
		<<set _mercManpower = 0>>

		<<for _j = 0; _j < $mercUnits.length; _j++>>
			<<if $mercUnits[_j].active == 1 && $rebellingID.includes($mercUnits[_j].ID)>>
				$mercUnits[_j].platoonName,
				<<set _mercRebelledID.push($mercUnits[_j].ID)>>
				<<set _mercManpower += Math.clamp($mercUnits[_j].troops - random(_averageLosses),0,$mercUnits[_j].troops)>>
			<</if>>
		<</for>>
		<<if _mercRebelledID.length > 0>>
			made the grave mistake of betraying you.

<<link "Dissolve the units">>
					<<removeUnits _mercRebelledID>>
					<<set $mercFreeManpower += _mercManpower>>
					<<for _i = 0; _i < $militiaUnits.length; _i++>>
						<<if $mercUnits[_i].active == 1>>
							<<set $mercUnits[_i].loyalty = Math.clamp($mercUnits[_i].loyalty - random(10,40),0,100)>>
						<</if>>
					<</for>>
					<<recalcManpower>>
					<<replace "#mercResult">>

Units dissolved.
					<</replace>>
				<</link>>

//Manpower will be refunded, but will negatively influence the loyalty of the other units//

<<link "Purge the dissidents and dissolve the units">>
					<<removeUnits _mercRebelledID>>
					<<set $mercFreeManpower += _mercManpower * 0.5>>
					<<recalcManpower>>
					<<replace "#mercResult">>

Dissidents purged and units dissolved.
					<</replace>>
				<</link>>

//Will not influence the loyalty of the other units, but only half the manpower will be refunded.//

<<link "Execute them all">>
					<<removeUnits _mercRebelledID>>
					<<for _i = 0; _i < $mercUnits.length; _i++>>
						<<if $mercUnits[_i].active == 1>>
							<<set $mercUnits[_i].loyalty = Math.clamp($mercUnits[_i].loyalty + random(10,40),0,100)>>
						<</if>>
					<</for>>
					<<recalcManpower>>
					<<replace "#mercResult">>

Units executed. Dissent will not be tolerated.
					<</replace>>
				<</link>>

//Will positively influence the loyalty of the other units, but no manpower will be refunded.//

		<</if>>
	<</if>>

<<elseif $losses > 0>>
	/* if the losses are more than zero */
	/* generates a list of randomized losses, from which each unit picks one at random */
	<<if $securityForceCreate == 1>>
		<<set $deployedUnits++>>
	<</if>>
	<<if $irregulars > 0>>
		<<set $deployedUnits++>>
	<</if>>
	<<set _averageLosses = Math.trunc($losses / $deployedUnits)>>
	<<set _lossesList = []>>
	<<for _i = 0; _i < $deployedUnits; _i++>>
		<<set _assignedLosses = Math.trunc(Math.clamp(_averageLosses + random(-5,5), 0, 100))>>
		<<if _assignedLosses > $losses>>
			<<set _assignedLosses = $losses>>
			<<set $losses = 0>>
		<<else>>
			<<set $losses -= _assignedLosses>>
		<</if>>
		<<set _lossesList.push(_assignedLosses)>>
	<</for>>
	<<if $losses > 0>>
		<<set _lossesList[random(_lossesList.length - 1)] += $losses>>
	<</if>>
	<<set _lossesList.shuffle()>>

	/* assigns the losses and notify the player */
	<<if $irregulars > 0>>

		<<set _loss = _lossesList.pluck()>>
		<<if _loss < $ACitizens + 100>>
			<<set $ACitizens -= _loss>>
		<<else>>
			<<set $ACitizens = 100>>
		<</if>>
		The volounteeing citizens were quickly organized into an irregular militia unit and deployed in the arcology. While without the discipline of your other instruments, they nonetheless provided precious extra firepower and standing strength.
		During the battle they suffered
		<<if _loss <= 0>>
			no casualties.
		<<elseif _loss <= 10>>
			light casualties.
		<<elseif _loss <= 30>>
			moderate casualties.
		<<elseif _loss <= 60>>
			heavy casualties.
		<<else>>
			catastrofic casualties.
		<</if>>
	<</if>>
	<<if $secBots.active == 1>>

		<<set _loss = _lossesList.pluck()>>
		<<set _loss = Math.clamp(_loss,0,$secBots.troops)>>
		<<set $secBots.troops -= _loss>>
		Your squadron of security drones participated in the battle, dominating the airspace and providing vital intelligence to the rest of the army.
		During the battle they suffered
		<<if _loss <= 0>>
			no casualties.
		<<elseif _loss <= 10>>
			light casualties.
		<<elseif _loss <= 30>>
			moderate casualties.
		<<elseif _loss <= 60>>
			heavy casualties.
		<<else>>
			catastrofic casualties.
		<</if>>
		<<if $secBots.troops <= 0>>
			<<set $secBots.active = 0>>
			Unfortunately the losses they took were simply too great, their effective combatants are in so small number you can no longer call them a deployable unit. It will take quite the investment to rebuild them.
		<<elseif $secBots.troops <= 10>>
			The unit has very few operatives left, it risks compelte annihilation if deployed again.
		<</if>>
	<</if>>
	<<if $securityForceCreate == 1>>

		<<set _loss = _lossesList.pluck()>>
		<<set _loss = Math.clamp(_loss,0,$securityForcePersonnel)>>
		<<set $securityForcePersonnel -= _loss>>
		$securityForceName, $securityForcePersonnel strong, is called to join the battle aboard their<<if $securityForceVehiclePower >= 4>>armored<</if>>vehicles<<if $securityForceAC130 > 0>>and their AC130<</if>>.
		They brought their unmatched strength to crush those that would want you deposed.
		During the battle they suffered
		<<if _loss <= 0>>
			no casualties.
		<<elseif _loss <= 10>>
			light casualties.
		<<elseif _loss <= 30>>
			moderate casualties.
		<<elseif _loss <= 60>>
			heavy casualties.
		<<else>>
			catastrofic casualties.
		<</if>>
	<</if>>
	<<if $deployingMilitia == 1>>
		<<for _j = 0; _j < $militiaUnits.length; _j++>>
			<<if $militiaUnits[_j].active == 1 && $loyalID.includes($militiaUnits[_j].ID)>>

				<<set $militiaUnits[_j].battlesFought++>>
				<<set _loss = _lossesList.pluck()>>
				<<set _loss = Math.clamp(_loss,0,$militiaUnits[_j].troops)>>
				$militiaUnits[_j].platoonName participated in the battle. They remained loyal to you to the end.
				During the battle they suffered
				<<if _loss <= 0>>
					no casualties
				<<elseif _loss <= 10>>
					light casualties
				<<elseif _loss <= 30>>
					moderate casualties
				<<elseif _loss <= 60>>
					heavy casualties
				<<else>>
					catastrofic casualties
				<</if>>
				<<set _med = Math.round(Math.clamp(_loss * $militiaUnits[_j].medics * 0.25,1,_loss))>>
				<<if $militiaUnits[_j].medics == 1>>
					, however some men were saved by the medics attached to this unit.
				<<else>>
					.
				<</if>>
				<<set $militiaUnits[_j].troops -= Math.trunc(Math.clamp(_loss - _med,0,$militiaUnits[_j].maxTroops))>>
				<<set $militiaEmployedManpower -= Math.trunc(_loss - _med)>>
				<<set $militiaTotalCasualties += Math.trunc(_loss - _med)>>
				<<if $militiaUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Fighting in the field increased the experience of the unit, making it more effective in future fights.
						<<set $militiaUnits[_j].training += random(5,15) + $majorBattle * random(5,15)>>
					<</if>>
				<</if>>
				<<if $militiaUnits[_j].troops <= 0>>
					<<set $militiaUnits[_j].active = 0>>

Unfortunately the losses they took were simply too great, their effective combatants are in so small number you can no longer call them a deployable unit. The remnants will be sent home honored as veterans or reorganized in a new unit.
				<<elseif $militiaUnits[_j].troops <= 10>>

The unit has very few operatives left, it risks complete annihilation if deployed again.
				<</if>>
			<</if>>
		<</for>>
	<</if>>
	<<if $deployingSlaves == 1>>
		<<set _med = 0>>
		<<for _j = 0; _j < $slaveUnits.length; _j++>>
			<<if $slaveUnits[_j].active == 1 && $loyalID.includes($slaveUnits[_j].ID)>>

				<<set $slaveUnits[_j].battlesFought++>>
				<<set _loss = _lossesList.pluck()>>
				<<if !(Number.isInteger(_loss))>>

@@.red;Error: failed to assign losses, input was negative or NaN@@
					<<break>>
				<</if>>
				<<set _loss = Math.clamp(_loss,0,$slaveUnits[_j].troops)>>
				$slaveUnits[_j].platoonName participated in the battle. They remained loyal to you to the end.
				During the battle they suffered
				<<if _loss <= 0>>
					no casualties
				<<elseif _loss <= 10>>
					light casualties
				<<elseif _loss <= 30>>
					moderate casualties
				<<elseif _loss <= 60>>
					heavy casualties
				<<else>>
					catastrofic casualties
				<</if>>
				<<set _med = Math.round(Math.clamp(_loss * $slaveUnits[_j].medics * 0.25,1,_loss))>>
				<<if $slaveUnits[_j].medics == 1>>
					, however some men were saved by the medics attached to this unit.
				<<else>>
					.
				<</if>>
				<<set $slaveUnits[_j].troops -= Math.trunc(Math.clamp(_loss - _med,0,$slaveUnits[_j].maxTroops))>>
				<<set $slavesEmployedManpower -= _loss - _med>>
				<<set $slavesTotalCasualties += _loss - _med>>
				<<if $slaveUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Fighting in the field increased the experience of the unit, making it more effective in future fights.
						<<set $slaveUnits[_j].training += random(5,15) + $majorBattle * random(5,15)>>
					<</if>>
				<</if>>
				<<if $slaveUnits[_j].troops <= 0>>
					<<set $slaveUnits[_j].active = 0>>

Unfortunately the losses they took were simply too great, their effective combatants are in so small number you can no longer call them a deployable unit. The sorvivors will be sent home honored as veterans or reorganized in a new unit.
				<<elseif $slaveUnits[_j].troops <= 10>>

The unit has very few operatives left, it risks complete annihilation if deployed again.
				<</if>>
			<</if>>
		<</for>>
	<</if>>
	<<if $deployingMercs == 1>>
		<<for _j = 0; _j < $mercUnits.length; _j++>>
			<<if $mercUnits[_j].active == 1 && $loyalID.includes($mercUnits[_j].ID)>>

				<<set $mercUnits[_j].battlesFought++>>
				<<set _loss = _lossesList.pluck()>>
				<<set _loss = Math.clamp(_loss,0,$mercUnits[_j].troops)>>
				$mercUnits[_j].platoonName participated in the battle. Mercenaries are a common sight on the modern battlefield and yours are proud to serve their generous contractor.
				During the battle they suffered
				<<if _loss <= 0>>
					no casualties
				<<elseif _loss <= 10>>
					light casualties
				<<elseif _loss <= 30>>
					moderate casualties
				<<elseif _loss <= 60>>
					heavy casualties
				<<else>>
					catastrofic casualties
				<</if>>
				<<set _med = Math.round(Math.clamp(_loss * $mercUnits[_j].medics * 0.25,1,_loss))>>
				<<if $mercUnits[_j].medics == 1>>
					, however some men were saved by the medics attached to this unit.
				<<else>>
					.
				<</if>>
				<<set $mercUnits[_j].troops -= Math.trunc(Math.clamp(_loss - _med,0,$mercUnits[_j].maxTroops))>>
				<<set $mercEmployedManpower -= Math.trunc(_loss - _med)>>
				<<set $mercTotalCasualties += Math.trunc(_loss - _med)>>
				<<if $mercUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Fighting in the field increased the experience of the unit, making it more effective in future fights.
						<<set $mercUnits[_j].training += random(5,15) + $majorBattle * random(5,15)>>
					<</if>>
				<</if>>
				<<if $mercUnits[_j].troops <= 0>>
					<<set $mercUnits[_j].active = 0>>

Unfortunately the losses they took were simply too great, their effective combatants are in so small number you can no longer call them a deployable unit. The remnants will be sent home honored as veterans or reorganized in a new unit.
				<<elseif $mercUnits[_j].troops <= 10>>

The unit has very few operatives left, it risks complete annihilation if deployed again.
				<</if>>
			<</if>>
		<</for>>
	<</if>>

	<<if $rebellingID.length > 0 && $battleResult >= 2>>
		/* win */
		/* militia */
		<<set _militiaRebelledID = []>>
		<<set _militiaManpower = 0>>
		<<for _j = 0; _j < $militiaUnits.length; _j++>>
			<<if $militiaUnits[_j].active == 1 && $rebellingID.includes($militiaUnits[_j].ID)>>
				$militiaUnits[_j].platoonName,
				<<set _militiaRebelledID.push($militiaUnits[_j].ID)>>
				<<set _militiaManpower += Math.clamp($militiaUnits[_j].troops - random(_averageLosses),0,$militiaUnits[_j].troops)>>
			<</if>>
		<</for>>
		<<if _militiaRebelledID.length > 0>>
			had the gall to betray you and join your enemies.

<<link "Dissolve the units">>
					<<removeUnits _militiaRebelledID>>
					<<set $militiaFreeManpower += _militiaManpower>>
					<<for _i = 0; _i < $militiaUnits.length; _i++>>
						<<if $militiaUnits[_i].active == 1>>
							<<set $militiaUnits[_i].loyalty = Math.clamp($militiaUnits[_i].loyalty - random(10,40),0,100)>>
						<</if>>
					<</for>>
					<<recalcManpower>>
					<<replace "#militiaResult">>

Units dissolved.
					<</replace>>
				<</link>>

//Manpower will be refunded, but will negatvely influence the loyalty of the other units//

<<link "Purge the dissidents and dissolve the units">>
					<<removeUnits _militiaRebelledID>>
					<<set $militiaFreeManpower += _militiaManpower * 0.5>>
					<<recalcManpower>>
					<<replace "#militiaResult">>

Dissidents purged and units dissolved.
					<</replace>>
				<</link>>

//Will not influence the loyalty of the other units, but only half the manpower will be refunded.//

<<link "Execute them all">>
					<<removeUnits _militiaRebelledID>>
					<<for _i = 0; _i < $militiaUnits.length; _i++>>
						<<if $militiaUnits[_i].active == 1>>
							<<set $militiaUnits[_i].loyalty = Math.clamp($militiaUnits[_i].loyalty + random(10,40),0,100)>>
						<</if>>
					<</for>>
					<<recalcManpower>>
					<<replace "#militiaResult">>

Units executed. Dissent will not be tolerated.
					<</replace>>
				<</link>>

//Will positively influence the loyalty of the other units, but no manpower will be refunded.//

		<</if>>

		/* slaves */
		<<set _slaveRebelledID = []>>
		<<set _slaveManpower = 0>>

		<<for _j = 0; _j < $slaveUnits.length; _j++>>
			<<if $slaveUnits[_j].active == 1 && $rebellingID.includes($slaveUnits[_j].ID)>>
				$slaveUnits[_j].platoonName,
				<<set _slaveRebelledID.push($slaveUnits[_j].ID)>>
				<<set _slaveManpower += Math.clamp($slaveUnits[_j].troops - random(_averageLosses),0,$slaveUnits[_j].troops)>>
			<</if>>
		<</for>>
		<<if _slaveRebelledID.length > 0>>
			decided in their blind arrogance to betray you.

<<link "Dissolve the units">>
					<<removeUnits _slaveRebelledID>>
					<<set $helots += _slaveManpower>>
					<<for _i = 0; _i < $militiaUnits.length; _i++>>
						<<if $slaveUnits[_i].active == 1>>
							<<set $slaveUnits[_i].loyalty = Math.clamp($slaveUnits[_i].loyalty - random(10,40),0,100)>>
						<</if>>
					<</for>>
					<<recalcManpower>>
					<<replace "#slaveResult">>

Units dissolved.
					<</replace>>
				<</link>>

//Manpower will be refunded, but will negatively influence the loyalty of the other units//

<<link "Purge the dissidents and dissolve the units">>
					<<removeUnits _slaveRebelledID>>
					<<set $helots += _slaveManpower * 0.5>>
					<<recalcManpower>>
					<<replace "#slaveResult">>

Dissidents purged and units dissolved.
					<</replace>>
				<</link>>

//Will not influence the loyalty of the other units, but only half the manpower will be refunded.//

<<link "Execute them all">>
					<<removeUnits _slaveRebelledID>>
					<<for _i = 0; _i < $slaveUnits.length; _i++>>
						<<if $slaveUnits[_i].active == 1>>
							<<set $slaveUnits[_i].loyalty = Math.clamp($slaveUnits[_i].loyalty + random(10,40),0,100)>>
						<</if>>
					<</for>>
					<<recalcManpower>>
					<<replace "#slaveResult">>

Units executed. Dissent will not be tolerated.
					<</replace>>
				<</link>>

//Will positively influence the loyalty of the other units, but no manpower will be refunded.//

		<</if>>

		/* mercs */
		<<set _mercRebelledID = []>>
		<<set _mercManpower = 0>>

		<<for _j = 0; _j < $mercUnits.length; _j++>>
			<<if $mercUnits[_j].active == 1 && $rebellingID.includes($mercUnits[_j].ID)>>
				$mercUnits[_j].platoonName,
				<<set _mercRebelledID.push($mercUnits[_j].ID)>>
				<<set _mercManpower += Math.clamp($mercUnits[_j].troops - random(_averageLosses),0,$mercUnits[_j].troops)>>
			<</if>>
		<</for>>
		<<if _mercRebelledID.length > 0>>
			made the grave mistake of betraying you.

<<link "Dissolve the units">>
					<<removeUnits _mercRebelledID>>
					<<set $mercFreeManpower += _mercManpower>>
					<<for _i = 0; _i < $militiaUnits.length; _i++>>
						<<if $mercUnits[_i].active == 1>>
							<<set $mercUnits[_i].loyalty = Math.clamp($mercUnits[_i].loyalty - random(10,40),0,100)>>
						<</if>>
					<</for>>
					<<recalcManpower>>
					<<replace "#mercResult">>

Units dissolved.
					<</replace>>
				<</link>>

//Manpower will be refunded, but will negatively influence the loyalty of the other units//

<<link "Purge the dissidents and dissolve the units">>
					<<removeUnits _mercRebelledID>>
					<<set $mercFreeManpower += _mercManpower * 0.5>>
					<<recalcManpower>>
					<<replace "#mercResult">>

Dissidents purged and units dissolved.
					<</replace>>
				<</link>>

//Will not influence the loyalty of the other units, but only half the manpower will be refunded.//

<<link "Execute them all">>
					<<removeUnits _mercRebelledID>>
					<<for _i = 0; _i < $mercUnits.length; _i++>>
						<<if $mercUnits[_i].active == 1>>
							<<set $mercUnits[_i].loyalty = Math.clamp($mercUnits[_i].loyalty + random(10,40),0,100)>>
						<</if>>
					<</for>>
					<<recalcManpower>>
					<<replace "#mercResult">>

Units executed. Dissent will not be tolerated.
					<</replace>>
				<</link>>

//Will positively influence the loyalty of the other units, but no manpower will be refunded.//

		<</if>>

	/* loss */
	<<elseif $rebellingID.length > 0>>
		<<set _militiaRebelledID = []>>
		<<for _j = 0; _j < $militiaUnits.length; _j++>>
			<<if $militiaUnits[_j].active == 1 && $rebellingID.includes($militiaUnits[_j].ID)>>
				<<set _militiaRebelledID.push($militiaUnits[_j].ID)>>
				$militiaUnits[_j].platoonName,
			<</if>>
		<</for>>
		<<if _militiaRebelledID.length > 0>>
			had the gall to betray you and join your enemies. They participated in the looting following the battle, then vanished in the wastes.
		<</if>>
		<<set $cash -= 1000 * _militiaRebelledID.length>>

		<<removeUnits _militiaRebelledID>>
		<<set _slaveRebelledID = []>>
		<<for _j = 0; _j < $slaveUnits.length; _j++>>
			<<if $slaveUnits[_j].active == 1 && $rebellingID.includes($slaveUnits[_j].ID)>>
				<<set _slaveRebelledID.push($slaveUnits[_j].ID)>>
				$slaveUnits[_j].platoonName,
			<</if>>
		<</for>>
		<<if _slaveRebelledID.length > 0>>
			decided in their blind arrogance to betray you. They participated in the looting following the battle, then vanished in the wastes.
		<</if>>
		<<set $cash -= 1000 * _slaveRebelledID.length>>
		<<removeUnits _slaveRebelledID>>

		<<set _mercRebelledID = []>>
		<<set _count = 0>>
		<<for _j = 0; _j < $mercUnits.length; _j++>>
			<<if $mercUnits[_j].active == 1 && $rebellingID.includes($mercUnits[_j].ID)>>
				<<set _mercRebelledID.push($mercUnits[_j].ID)>>
				<<set _count++>>
				$mercUnits[_j].platoonName,
			<</if>>
		<</for>>
		<<if _mercRebelledID.length > 0>>
			 made the grave mistake of betraying you. They participated in the looting following the battle, then vanished in the wastes.
		<</if>>
		<<set $cash -= 1000 * _mercRebelledID.length>>
		<<removeUnits _mercRebelledID>>
		<<recalcManpower>>
	<</if>>
<<else>>

@@.red;Error: losses are a negative number or NaN@@
<</if>>


code:
:: weaponsManufacturing [nobr]

<<set $nextButton = "Back", $nextLink = "Main">>

This sector of the arcology has been dedicated to weapons manufacturing. These factories supply
<<if $militiaUnits.length > 0>> your militia and<</if>>
<<if $slaveUnits.length > 0>> your slave soldiers and<</if>>
<<if $mercenaries >0>> your mercenaries and<</if>>
many small old world nations as the advanced technology that free cities have available is hard to come by otherwise.

<<if $weapProductivity == 1>>
	Production is completely manned by human workers. The complex has close to zero automation.
<<elseif $weapProductivity == 2>>
	Production is mostly handled by human workers. A few of the most tiresome tasks are handled by robots.
<<elseif $weapProductivity == 3>>
	A good part of production is handled by robots with humans handling the most complex tasks.
<<elseif $weapProductivity == 4>>
	Almost all production is handled by robots, with humans acting as support for the machines.
<<else>>
	All production here is handled by robots. The few humans working in the complex occupy themselves exclusively with management and quality assurance.
<</if>>

<<if $weapLab == 1>>
	There's a very Spartan lab attached to the complex that occupies itself mainly with weapons testing and small adjustments to the manufacturing process.
<<elseif $weapLab == 2>>
	There's a lab attached to the complex. It mainly test weapons effectiveness and manufacturing efficiency, but has enough equipment and personnel to develop new technology.
<<else>>
	There's a large lab attached to the complex. The complement of equipment and personnel makes it a great beacon of military science in an otherwise ignorant world.
<</if>>

<<if $weapHelots> 0>>Assigned here are $weapHelots slaves working to produce as much equipment as possible<<else>>There are no assigned menial slaves here. The spaces is manned exclusively by low rank citizens.<</if>>
You own $helots free menial slaves. This manufacturing complex can house 500 at most, with <<print 500 - $weapHelots>> free slots.

<<silently>><<MenialPopCap>><</silently>>
<<set _menialPrice = Math.trunc(($slaveCostFactor*1000)/100)*100>>
<<set _menialPrice = Math.clamp(_menialPrice, 500, 1500)>>
<<if $PopCap > $helots+$fuckdolls+$menialBioreactors || $helots+$fuckdolls+$menialBioreactors == 0>>

	[[Buy|weaponsManufacturing][$helots+=1,$menialDemandFactor+=1,$cash-=_menialPrice]]
	<<if $cash > _menialPrice*10>>
		[[(x10)|weaponsManufacturing][$helots+=10,$menialDemandFactor+=10,$cash-=_menialPrice*10]]
	<</if>>
	<<if $cash > _menialPrice*100>>
		[[(x100)|weaponsManufacturing][$helots+=100,$menialDemandFactor+=100,$cash-=_menialPrice*100]]
	<</if>>
	<<if $cash > _menialPrice*2>>
		[[(max)|weaponsManufacturing][$helots+=Math.trunc($cash/(_menialPrice)),$menialDemandFactor+=Math.trunc($cash/(_menialPrice)),$cash-=Math.trunc($cash/(_menialPrice))*(_menialPrice)]]
	<</if>>
<</if>>

<<if $weapHelots < 500>>
	<<if $helots >= 1>>
		<<link "Transfer a menial slave">>
			<<set $helots-->>
			<<set $weapHelots++>>
			<<goto "weaponsManufacturing">>
		<</link>>
	<</if>>
	<<if $helots >= 10 && $weapHelots <= 490>>
		|
		<<link "Transfer 10 menial slaves">>
			<<set $helots -= 10>>
			<<set $weapHelots += 10>>
			<<goto "weaponsManufacturing">>
		<</link>>
	<</if>>
	<<if $helots > 0>>
		|
		<<link "Transfer all free menial slaves">>
			<<if $helots > 500 - $weapHelots>>
				<<set $helots -= 500 - $weapHelots>>
				<<set $weapHelots = 500>>
			<<else>>
				<<set $weapHelots += $helots>>
				<<set $helots = 0>>
			<</if>>
			<<goto "weaponsManufacturing">>
		<</link>>
	<</if>>
	<<if $weapHelots > 0>>

		<<link "Transfer out all menial slaves">>
			<<set $helots += $weapHelots>>
			<<set $weapHelots = 0>>
			<<goto "weaponsManufacturing">>
		<</link>>
	<</if>>
<<else>>
	The complex does not require more workers.
<</if>>

<<if $weapProductivity < 5>>
	<<link "Invest in automating the complex">>
		<<set $cash -= 10000 * $weapProductivity>>
		<<set $weapProductivity++>>
		<<if $currentUpgrade.time > 0>>
			<<set _percentComplete = $currentUpgrade.time / ($baseUpgradeTime / ($weapProductivity - 1))>>
			<<set _newTime = Math.ceil(_percentComplete * $baseUpgradeTime / $weapProductivity)>>
			<<set $currentUpgrade.time  = _newTime>>
		<</if>>
		<<goto "weaponsManufacturing">>
	<</link>>
	//Will cost <<print 10000 * $weapProductivity>> and will increase the facility income in addition to speeding up upgrade production.//
<<elseif $weapLab < 3>>
	You have fully automated the complex.
<</if>>

<<if $weapLab < 3>>
	<<link "Invest in research and development">>
		<<set $cash -= 10000 * $weapLab>>
		<<set $weapLab++>>
		<<goto "weaponsManufacturing">>
	<</link>>
	//Will cost <<print 10000 * $weapLab>> and will increase the facility income as well as unlock upgrades for our troops//
<<elseif $weapProductivity < 5>>
	You have fully upgraded and funded the R&D department
<</if>>
<<if $weapLab >= 3 && $weapProductivity >= 5>>
	The facility is completely automated and its R&D department is fully upgraded and funded.
<</if>>

__Markets__:

<<if $sellTo.citizen == 1>>
	We are currently selling our weapons to the domestic market of the arcology.
	<<link "Forbid">>
		<<set $sellTo.citizen = 0>>
		<<goto "weaponsManufacturing">>
	<</link>>
<<else>>
	We are not selling our weaponry to our citizens.
	<<link "Allow">>
		<<set $sellTo.citizen = 1>>
		<<goto "weaponsManufacturing">>
	<</link>>
<</if>>

<<if $sellTo.raiders == 1>>
	We are currently selling our weapons to various groups of outlaws, also known as raiders.
	<<link "Forbid">>
		<<set $sellTo.raiders = 0>>
		<<goto "weaponsManufacturing">>
	<</link>>
<<else>>
	We are not selling our weaponry to raiders.
	<<link "Allow">>
		<<set $sellTo.raiders = 1>>
		<<goto "weaponsManufacturing">>
	<</link>>
<</if>>

<<if $sellTo.oldWorld == 1>>
	We are currently selling our weapons to many old world nations.
	<<link "Forbid">>
		<<set $sellTo.oldWorld = 0>>
		<<goto "weaponsManufacturing">>
	<</link>>
<<else>>
	We are not selling our weaponry to old world nations.
	<<link "Allow">>
		<<set $sellTo.oldWorld = 1>>
		<<goto "weaponsManufacturing">>
	<</link>>
<</if>>

<<if $sellTo.FC == 1>>
	We are currently selling our weapons to other free cities.
	<<link "Forbid">>
		<<set $sellTo.FC = 0>>
		<<goto "weaponsManufacturing">>
	<</link>>
<<else>>
	We are not selling our weaponry to other free cities.
	<<link "Allow">>
		<<set $sellTo.FC = 1>>
		<<goto "weaponsManufacturing">>
	<</link>>
<</if>>

//You are free to sell to whoever you please, but expect hostile forces of a certain kind to be stronger if we'll ever meet in battle.//

<<set _time = $baseUpgradeTime / $weapProductivity>>
__Upgrades__:

With our current industrial and research capabilities upgrades will be finished in <<print _time>> weeks.

<<if $currentUpgrade.time <= 0>>

Security Drones:

	/* Drones upgrades */
	<<if !$completedUpgrades.includes(-1)>>
		<<link "Develop dynamic battle aware AI">>
			<<set $currentUpgrade = {
				ID: -1,
				name: "dynamic battle aware AI",
				unit: 0,
				type: "attack",
				time: _time}>>
			<<set $cash -= 10000>>
			<<goto "weaponsManufacturing">>
		<</link>>

//Will take _time weeks, cost ¤10000 and will increase the base attack value of the security drones.//
	<<elseif !$completedUpgrades.includes(-2) && $weapLab >= 2>>
		<<link "Develop adaptive armored frames">>
			<<set $currentUpgrade = {
				ID: -2,
				name: "adaptive armored frames",
				unit: 0,
				type: "defense",
				time: _time}>>
			<<set $cash -= 10000>>
			<<goto "weaponsManufacturing">>
		<</link>>

//Will take _time weeks, cost ¤10000 and will increase the base defense value of the security drones.//
	<<elseif !$completedUpgrades.includes(-3) && $weapLab >= 3>>

		<<link "Develop advanced synthetic alloys">>
			<<set $currentUpgrade = {
				ID: -3,
				name: "advanced synthetic alloys",
				unit: 0,
				type: "hp",
				time: _time}>>
			<<set $cash -= 30000>>
			<<goto "weaponsManufacturing">>
		<</link>>

//Will take _time weeks, cost ¤30000 and will increase the base hp value of the security drones.//
	<<elseif $secBotsBaseDefense <= 3 || $droneUpgrades >= 3>>
		Upgrade the research facility further to unlock more upgrades for the security drones.
	<<else>>
		You fully upgraded the security drones.
	<</if>>

	/* human troops upgrades */

Troops:

	<<if !$completedUpgrades.includes(0)>>
		<<link "Develop magnetic based ballistic weaponry">>
			<<set $currentUpgrade = {
				ID: 0,
				name: "magnetic based ballistic weaponry",
				unit: 1,
				type: "attack",
				time: _time}>>
			<<set $cash -= 30000>>
			<<goto "weaponsManufacturing">>
		<</link>>

//Will take _time weeks, cost ¤30000 and will increase the base attack value of human troops.//

	<</if>>
	<<if !$completedUpgrades.includes(1)>>
		<<link "Develop ceramo-metallic alloys">>
			<<set $currentUpgrade = {
				ID: 1,
				name: "ceramo-metallic alloys",
				unit: 1,
				type: "defense",
				time: _time}>>
			<<set $cash -= 30000>>
			<<goto "weaponsManufacturing">>
		<</link>>

//Will take _time weeks, cost ¤30000 and will increase the base defense value of human troops.//
	<</if>>
	<<if !$completedUpgrades.includes(2) && $weapLab >= 2>>

		<<link "Develop rapid action stimulants">>
			<<set $currentUpgrade = {
				ID: 2,
				name: "rapid action stimulants",
				unit: 1,
				type: "hp",
				time: _time}>>
			<<set $cash -= 60000>>
			<<goto "weaponsManufacturing">>
		<</link>>

//Will take _time weeks, cost ¤60000 and will increase the base hp value of human troops.//

	<</if>>
	<<if !$completedUpgrades.includes(3) && $weapLab >= 2>>
		<<link "Develop fast response neural stimulant">>
			<<set $currentUpgrade = {
				ID: 3,
				name: "fast response neural stimulant",
				unit: 1,
				type: "morale",
				time: _time}>>
			<<set $cash -= 60000>>
			<<goto "weaponsManufacturing">>
		<</link>>

//Will take _time weeks, cost ¤60000 and will increase the base morale value of human troops.//
	<</if>>
	<<if !$completedUpgrades.includes(4) && $weapLab >= 3>>

		<<link "Develop universal cyber enhancements">>
			<<set $currentUpgrade = {
				ID: 4,
				name: "universal cyber enhancements",
				unit: 1,
				type: "attackAndDefense",
				time: _time}>>
			<<set $cash -= 120000>>
			<<goto "weaponsManufacturing">>
		<</link>>

//Will take _time weeks, cost ¤120000 and will increase the base attack and "defense" values of human troops.//
	<</if>>
	<<if !$completedUpgrades.includes(5) && $weapLab >= 3>>

		<<link "Develop remote neural links">>
			<<set $currentUpgrade = {
				ID: 5,
				name: "remote neural links",
				unit: 1,
				type: "hpAndMorale",
				time: _time}>>
			<<set $cash -= 120000>>
			<<goto "weaponsManufacturing">>
		<</link>>

//Will take _time weeks, cost ¤120000 and will increase the base hp and morale values of human troops.//
	<</if>>

	<<if $securityForceCreate == 1>>
		<<if !$completedUpgrades.includes(6) && $weapLab >= 2 && $SFSupportLevel >= 2 && $securityForceArcologyUpgrades >= 7>>

			<<link "Develop combined training regimens with $securityForceName">>
				<<set $currentUpgrade = {
					ID: 6,
					name: "combined training regimens with the special force",
					unit: 1,
					type: "attackAndDefense",
					time: _time}>>
				<<goto "weaponsManufacturing">>
			<</link>>

//Will take _time weeks, and will increase the base attack and defense values of human troops.//
		<</if>>
		<<if !$completedUpgrades.includes(7) && $weapLab >= 2 && $SFSupportLevel >= 4 && $securityForceStimulantPower >= 8>>

			<<link "Develop a variant of the stimulant cocktail that $securityForceName created">>
				<<set $currentUpgrade = {
					ID: 7,
					name: "a variant of the stimulant cocktail that the special force created",
					unit: 1,
					type: "hpAndMorale",
					time: _time}>>
				<<set $cash -= 300000>>
				<<goto "weaponsManufacturing">>
			<</link>>

//Will take _time weeks, cost ¤300000 and will increase the base morale and hp values of human troops.//
		<</if>>
		<<if !$completedUpgrades.includes(8) && $weapLab >= 3 && $SFSupportLevel >= 5>>

			<<link "Create a mesh network based off the custom network of $securityForceName">>
				<<set $currentUpgrade = {
					ID: 8,
					name: "a mesh network based off the custom network of the special force",
					unit: 1,
					type: "all",
					time: _time}>>
				<<set $cash -= 1000000>>
				<<goto "weaponsManufacturing">>
			<</link>>

//Will take _time weeks, cost ¤1000000 and will increase all base stats of human troops.//
		<</if>>
	<</if>>
	<<if $securityForceCreate == 1 && ($humanUpgrade.attack >= 4 || $humanUpgrade.hp >= 4 || $humanUpgrade.morale >= 40 || $humanUpgrade.defense >= 4)>>
		You fully upgraded your human troops.
	<<elseif $humanUpgrade.attack >= 2 || $humanUpgrade.hp >= 2 || $humanUpgrade.morale >= 20 || $humanUpgrade.defense >= 2>>
		You fully upgraded your human troops.
		<<if $securityForceCreate == 1 && ($humanUpgrade.attack < 4 || $humanUpgrade.hp < 4 || $humanUpgrade.morale < 40 || $humanUpgrade.defense < 4)>>
			With support from $securityForceName, however, we may be able to further upgrade our troops.
		<</if>>
	<<elseif $weapLab < 3>>
		Upgrade the research facility further to unlock more upgrades for human troops.
	<</if>>
<<else>>

	You are developing <<print $currentUpgrade.name>>.
	It is for <<if $currentUpgrade.unit == 0>> the security drones<<else>> our troops<</if>>. It will enhance their
	<<if $currentUpgrade.type == "attack">>
		attack power.
	<<elseif $currentUpgrade.type == "defense">>
		defensive capabilities.
	<<elseif $currentUpgrade.type == "hp">>
		survivability.
	<<elseif $currentUpgrade.type == "morale">>
		standing power.
	<<elseif $currentUpgrade.type == "attackAndDefense">>
		offensive and defensive effectiveness.
	<<elseif $currentUpgrade.type == "hpAndMorale">>
		morale and survivability.
	<<elseif $currentUpgrade.type == "all">>
		offensive,defensive effectiveness in addition to morale and survivability.
	<</if>>
	Estimated completion time is <<print $currentUpgrade.time>> weeks.
<</if>>
<<if $completedUpgrades.length > 0>>

	you completed:
	<<for _i = 0; _i < $completedUpgrades.length; _i++>>
		<<if _i < $completedUpgrades.length - 2>>
			<<if $completedUpgrades[_i] == 0>>
				magnetic based ballistic weaponry,
			<<elseif $completedUpgrades[_i] == 1>>
				ceramo-metallic alloys,
			<<elseif $completedUpgrades[_i] == 2>>
				rapid action stimulants,
			<<elseif $completedUpgrades[_i] == 3>>
				fast response neural stimulant,
			<<elseif $completedUpgrades[_i] == 4>>
				universal cyber enhancements,
			<<elseif $completedUpgrades[_i] == 5>>
				remote neural links,
			<<elseif $completedUpgrades[_i] == 6>>
				combined training regimens with the special force,
			<<elseif $completedUpgrades[_i] == 7>>
				a variant of the stimulant cocktail that the special force created,
			<<elseif $completedUpgrades[_i] == 8>>
				a mesh network based off the custom network of the special force,
			<<elseif $completedUpgrades[_i] == -1>>
				dynamic battle aware AI,
			<<elseif $completedUpgrades[_i] == -2>>
				adaptive armored frames,
			<<elseif $completedUpgrades[_i] == -3>>
				advanced synthetic alloys,
			<</if>>
		<<else>>
			<<if $completedUpgrades[_i] == 0>>
				magnetic based ballistic weaponry.
			<<elseif $completedUpgrades[_i] == 1>>
				ceramo-metallic alloys.
			<<elseif $completedUpgrades[_i] == 2>>
				rapid action stimulants.
			<<elseif $completedUpgrades[_i] == 3>>
				fast response neural stimulant.
			<<elseif $completedUpgrades[_i] == 4>>
				universal cyber enhancements.
			<<elseif $completedUpgrades[_i] == 5>>
				remote neural links.
			<<elseif $completedUpgrades[_i] == 6>>
				combined training regimens with the special force.
			<<elseif $completedUpgrades[_i] == 7>>
				a variant of the stimulant cocktail that the special force created.
			<<elseif $completedUpgrades[_i] == 8>>
				a mesh network based off the custom network of the special force.
			<<elseif $completedUpgrades[_i] == -1>>
				dynamic battle aware AI.
			<<elseif $completedUpgrades[_i] == -2>>
				adaptive armored frames.
			<<elseif $completedUpgrades[_i] == -3>>
				advanced synthetic alloys.
			<</if>>
		<</if>>
	<</for>>
<</if>>


code:
:: battleWidgets [widget nobr]

<<widget "calcSFStatistics">>
	<<if $slaveRebellion != 1 || $citizenRebellion != 1>>
		/* atk, def */
		<<set _upgradesSum = $securityForceInfantryPower + $securityForceStimulantPower + $securityForceAircraftPower + $securityForceAircraftPower>>
		<<if !isInt(_upgradesSum)>>
			<<set _upgradesSum = random(10,15)>>
		<</if>>
		<<set $SFatk = Math.trunc(0.65 * _upgradesSum)>>
		<<set $SFdef = Math.trunc(0.40 * _upgradesSum)>>
		/* hp */
		<<set $carriableSoldiers = 125 * ($securityForceAC130 + $securityForceVehiclePower)>>
		<<if !isInt($carriableSoldiers)>>
			<<set $carriableSoldiers = $securityForcePersonnel / 10>>
		<</if>>
		<<if $securityForcePersonnel > $carriableSoldiers>>
			<<set $SFhp = $carriableSoldiers * $SFBaseHp>>
		<<else>>
			<<set $carriableSoldiers = $securityForcePersonnel>>
			<<set $SFhp = $carriableSoldiers * $SFBaseHp>>
		<</if>>
	<<else>>
		/* atk, def */
		<<set _upgradesSum = $securityForceInfantryPower + $securityForceStimulantPower + $securityForceAircraftPower + $securityForceAircraftPower>>
		<<if !isInt(_upgradesSum)>>
			<<set _upgradesSum = random(10,15)>>
		<</if>>
		<<set $SFatk = Math.trunc(0.75 * _upgradesSum)>>
		<<set $SFdef = Math.trunc(0.50 * _upgradesSum)>>
		/* hp */
		<<set $SFhp = $securityForcePersonnel * $SFBaseHp>>
	<</if>>
<</widget>>

<<widget "removeUnits">>
	/* args[0] is the array of IDs of units to be eliminated */

	<<set _newMilitia = []>>
	<<set _newSlave = []>>
	<<set _newMerc = []>>

	<<for _i = 0; _i < $militiaUnits.length; _i++>>
		<<if !($args[0].includes($militiaUnits[_i].ID))>>
			<<set _newMilitia.push($militiaUnits[_i])>>
		<</if>>
	<</for>>
	<<set $militiaUnits = _newMilitia>>

	<<for _i = 0; _i < $slaveUnits.length; _i++>>
		<<if !($args[0].includes($slaveUnits[_i].ID))>>
			<<set _newSlave.push($slaveUnits[_i])>>
		<</if>>
	<</for>>
	<<set $slaveUnits = _newSlave>>

	<<for _i = 0; _i < $mercUnits.length; _i++>>
		<<if !($args[0].includes($mercUnits[_i].ID))>>
			<<set _newMerc.push($mercUnits[_i])>>
		<</if>>
	<</for>>
	<<set $mercUnits = _newMerc>>
<</widget>>

<<widget "calcTroopCount">>
	<<set _troops = 0>>
	<<if $attackThisWeek == 1>>

		<<if $deployingBots == 1>>
			<<set _troops += $secBots.troops>>
		<</if>>
		<<for _i = 0; _i < $militiaUnits.length; _i++>>
			<<if $militiaUnits[_i].isDeployed == 1>>
				<<set _troops += $militiaUnits[_i].troops>>
			<</if>>
		<</for>>
		<<for _i = 0; _i < $slaveUnits.length; _i++>>
			<<if $slaveUnits[_i].isDeployed == 1>>
				<<set _troops += $slaveUnits[_i].troops>>
			<</if>>
		<</for>>
		<<for _i = 0; _i < $mercUnits.length; _i++>>
			<<if $mercUnits[_i].isDeployed == 1>>
				<<set _troops += $mercUnits[_i].troops>>
			<</if>>
		<</for>>
		<<if $SFIntervention == 1>>
			<<set _troops += $carriableSoldiers>>
		<</if>>
		<<set $troopCount = _troops>>

	<<elseif $slaveRebellion == 1 || $citizenRebellion == 1>>

		<<if $irregulars > 0>>
			<<set _troops += $irregulars>>
		<</if>>
		<<if $secBots.active == 1>>
			<<set _troops += $secBots.troops>>
		<</if>>
		<<for _i = 0; _i < $militiaUnits.length; _i++>>
			<<if $militiaUnits[_i].active == 1 && $loyalID.includes($militiaUnits[_i].ID)>>
				<<set _troops += $militiaUnits[_i].troops>>
			<</if>>
		<</for>>
		<<for _i = 0; _i < $slaveUnits.length; _i++>>
			<<if $slaveUnits[_i].active == 1 && $loyalID.includes($slaveUnits[_i].ID)>>
				<<set _troops += $slaveUnits[_i].troops>>
			<</if>>
		<</for>>
		<<for _i = 0; _i < $mercUnits.length; _i++>>
			<<if $mercUnits[_i].active == 1 && $loyalID.includes($mercUnits[_i].ID)>>
				<<set _troops += $mercUnits[_i].troops>>
			<</if>>
		<</for>>
		<<if $securityForceCreate == 1>>
			<<set _troops += $carriableSoldiers>>
		<</if>>
		<<set $troopCount = _troops>>

	<<else>>
		<br>@@.red;Error: widget called outside battle@@
	<</if>>
<</widget>>