code:
:: unitsRebellionReport [nobr]

<<if $losses == 0>>
	<<if $irregulars > 0>>
		The volunteering citizens were quickly organized into an irregular militia unit and deployed in the arcology. No casualties suffered.
	<</if>>
	<<if $deployingBots == 1>>

		Security drones: no casualties suffered.
	<</if>>
	<<if $SF.Toggle && $SF.Active >= 1>>

$SF.Lower, <<print commaNum($SFUnit.Troops)>> strong, was called to join the battle: no casualties suffered.
	<</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 && $battleResult != -1>>

		/* 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 negatively 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>>
	<<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 rebellion, 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 rebellion, 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 rebellion, then vanished in the wastes.
		<</if>>
		<<set $cash -= 1000 * _mercRebelledID.length>>
		<<removeUnits _mercRebelledID>>
		<<recalcManpower>>
	<</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 $SF.Toggle && $SF.Active >= 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()>>

	/* 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 _rand = random(_lossesList.length - 1)>>
		<<set _lossesList[_rand] += $losses - _count>>
	<<elseif _count > $losses>>
		<<set _diff = _count - $losses>>
		<<set _rand = random(_lossesList.length - 1)>>
		<<set _lossesList[_rand] = Math.trunc(_lossesList[_rand]-_diff,0,100)>>
	<</if>>

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

		<<set _loss = _lossesList.pluck()>>
		<<if _loss > $ACitizens * 0.95>>
			<<set _loss = Math.trunc($ACitizens * 0.95)>> /* this is unlikely to happen, but might as well be safe*/
		<</if>>
		The volunteering citizens were quickly organized into an irregular militia unit and deployed in the arcology:
		<<if _loss <= 0>>
			no casualties
		<<elseif _loss <= 10>>
			light casualties
		<<elseif _loss <= 30>>
			moderate casualties
		<<elseif _loss <= 60>>
			heavy casualties
		<<else>>
			catastrophic casualties
		<</if>>
		suffered.
		<<if _loss > $lowerClass * 0.95>> /*I suspect only lower class ever get to fight/die, but being safe*/
			<<set $lowerClass = Math.trunc($lowerClass * 0.05),
			_loss -= Math.trunc($lowerClass * 0.95)>>
			<<if _loss > $middleClass * 0.95>>
				<<set $middleClass = Math.trunc($middleClass * 0.05),
				_loss -= Math.trunc($middleClass *0.95)>>
				<<if _loss > $upperClass * 0.95>>
					<<set $upperClass = Math.trunc($upperClass * 0.05),
					_loss -= Math.trunc($upperClass * 0.95)>>
					<<if _loss > $topClass * 0.95>>
						<<set $topClass = Math.trunc($topClass * 0.05)>>
					<<else>>
						<<set $topClass -= _loss>>
					<</if>>
				<<else>>
					<<set $upperClass -= _loss>>
				<</if>>
			<<else>>
				<<set $middleClass -= _loss>>
			<</if>>
		<<else>>
			<<set $lowerClass -= _loss>>
		<</if>>
	<</if>>
	<<if $secBots.active == 1>>

		<<set _loss = _lossesList.pluck()>>
		<<set _loss = Math.clamp(_loss,0,$secBots.troops)>>
		<<set $secBots.troops -= _loss>>
		Security drones:
		<<if _loss <= 0>>
			no casualties
		<<elseif _loss <= 10>>
			light casualties
		<<elseif _loss <= 30>>
			moderate casualties
		<<elseif _loss <= 60>>
			heavy casualties
		<<else>>
			catastrophic casualties
		<</if>>
		suffered.
		<<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 $SF.Toggle && $SF.Active >= 1>>

		<<set _loss = _lossesList.pluck()>>
		<<set _loss = Math.clamp(_loss,0,$SFUnit.Troops)>>
		$SF.Lower, $SFUnit.Troops strong, is called to join the battle:
		<<set $SFUnit.Troops -= _loss>>
		<<if _loss <= 0>>
			no casualties
		<<elseif _loss <= 10>>
			light casualties
		<<elseif _loss <= 30>>
			moderate casualties
		<<elseif _loss <= 60>>
			heavy casualties
		<<else>>
			catastrophic casualties
		<</if>>
		suffered.
	<</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.
				<<if _loss <= 0>>
					No casualties
				<<elseif _loss <= $militiaUnits[_j].troops * 0.2>>
					Light casualties
				<<elseif _loss <= $militiaUnits[_j].troops * 0.4>>
					Moderate casualties
				<<elseif _loss <= $militiaUnits[_j].troops * 0.6>>
					Heavy casualties
				<<else>>
					Catastrophic casualties
				<</if>>
				suffered.
				<<set _med = Math.round(Math.clamp(_loss * $militiaUnits[_j].medics * 0.25,1,_loss))>>
				<<if $militiaUnits[_j].medics == 1 && _loss > 0>>
					Some men were saved by their medics.
				<</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)>>
				<<set $militiaTotalManpower -= Math.trunc(_loss - _med)>>
				<<if $militiaUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Experience has increased.
						<<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.
				<<if _loss <= 0>>
					No casualties
				<<elseif _loss <= $slaveUnits[_j].troops * 0.2>>
					Light casualties
				<<elseif _loss <= $slaveUnits[_j].troops * 0.4>>
					Moderate casualties
				<<elseif _loss <= $slaveUnits[_j].troops * 0.6>>
					Heavy casualties
				<<else>>
					Catastrophic casualties
				<</if>>
				suffered.
				<<set _med = Math.round(Math.clamp(_loss * $slaveUnits[_j].medics * 0.25,1,_loss))>>
				<<if $slaveUnits[_j].medics == 1 && _loss > 0>>
					Some men were saved by their medics.
				<</if>>
				<<set $slaveUnits[_j].troops -= Math.trunc(Math.clamp(_loss - _med,0,$slaveUnits[_j].maxTroops))>>
				<<set $slavesEmployedManpower -= _loss - _med>>
				<<set $slavesTotalCasualties += _loss - _med>>
				<<set $helots -= Math.trunc(_loss - _med)>>
				<<if $slaveUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Experience gained.
						<<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 survivors 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. They remained loyal to you.
				<<if _loss <= 0>>
					No casualties
				<<elseif _loss <= $mercUnits[_j].troops * 0.2>>
					Light casualties
				<<elseif _loss <= $mercUnits[_j].troops * 0.4>>
					Moderate casualties
				<<elseif _loss <= $mercUnits[_j].troops * 0.6>>
					Heavy casualties
				<<else>>
					Catastrophic casualties
				<</if>>
				suffered.
				<<set _med = Math.round(Math.clamp(_loss * $mercUnits[_j].medics * 0.25,1,_loss))>>
				<<if $mercUnits[_j].medics == 1 && _loss > 0>>
					Some men were saved by their medics.
				<</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)>>
				<<set $mercTotalManpower -= Math.trunc(_loss - _med)>>
				<<if $mercUnits[_j].training < 100>>
					<<if random(1,100) > 60>>
						Experience gained.
						<<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 negatively 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 $HackingSkillMultiplier = HSM()>>
<<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 <<print commaNum($helots)>> free menial slaves. This manufacturing complex can house 500 at most, with <<print 500 - $weapHelots>> free slots.

<<silently>><<= MenialPopCap()>><</silently>>
<<set _menialPrice = menialSlaveCost()>>
<<set _bulkMax = $PopCap-$helots-$fuckdolls-$menialBioreactors>>
<<if $cash > _menialPrice>>
<<if _bulkMax > 0 || $helots+$fuckdolls+$menialBioreactors == 0>>

	[[Buy|weaponsManufacturing][$helots+=1,$menialSupplyFactor-=1,$cash-=_menialPrice]]
	<<if $cash > (menialSlaveCost(10))*10>>
		[[(x10)|weaponsManufacturing][$helots+=10,$menialSupplyFactor-=10,$cash-=(menialSlaveCost(10))*10]]
	<</if>>
	<<if $cash > (menialSlaveCost(100))*100>>
		[[(x100)|weaponsManufacturing][$helots+=100,$menialSupplyFactor-=100,$cash-=(menialSlaveCost(100))*100]]
	<</if>>
	<<if $cash > (_menialPrice+1)*2>>
		<<set _menialBulkPremium = Math.trunc(1 + Math.clamp($cash/_menialPrice,0,_bulkMax)/400)>>
		[[(max)|weaponsManufacturing][$helots+=Math.trunc(Math.clamp($cash/(_menialPrice+_menialBulkPremium),0,_bulkMax)),$menialSupplyFactor-=Math.trunc(Math.clamp($cash/(_menialPrice+_menialBulkPremium),0,_bulkMax)),$cash-=Math.trunc(Math.clamp($cash/(_menialPrice+_menialBulkPremium),0,_bulkMax)*(_menialPrice+_menialBulkPremium))]]
	<</if>>
	//Bulk transactions may require offering a premium.//
<</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 cashFormat(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 cashFormat(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 = Math.ceil($baseUpgradeTime / $weapProductivity)>>
__Upgrades__:

With our current industrial and research capabilities upgrades will be finished in _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*$HackingSkillMultiplier>>
			<<goto "weaponsManufacturing">>
		<</link>>

//Will take _time weeks, cost <<print cashFormat(10000*$HackingSkillMultiplier)>> 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 <<print cashFormat(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 <<print cashFormat(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 have 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 <<print cashFormat(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 <<print cashFormat(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 <<print cashFormat(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 <<print cashFormat(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*$HackingSkillMultiplier>>
			<<goto "weaponsManufacturing">>
		<</link>>

//Will take _time weeks, cost <<print cashFormat(120000*$HackingSkillMultiplier)>> 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*$HackingSkillMultiplier>>
			<<goto "weaponsManufacturing">>
		<</link>>

//Will take _time weeks, cost <<print cashFormat(120000*$HackingSkillMultiplier)>> and will increase the base hp and morale values of human troops.//
	<</if>>

	<<if $SF.Toggle && $SF.Active >= 1>>
		<<if !$completedUpgrades.includes(6) && $weapLab >= 2 && $SFSupportLevel >= 2 && $SFUnit.Firebase >= 7>>

			<<link "Develop combined training regimens with $SF.Lower">>
				<<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 && $SFUnit.Drugs >= 8>>

			<<link "Develop a variant of the stimulant cocktail that $SF.Lower 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 <<print cashFormat(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 $SF.Lower">>
				<<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*$HackingSkillMultiplier>>
				<<goto "weaponsManufacturing">>
			<</link>>

//Will take _time weeks, cost <<print cashFormat(1000000*$HackingSkillMultiplier)>> and will increase all base stats of human troops.//
		<</if>>
	<</if>>
	<<if $SF.Toggle && $SF.Active >= 1 && ($humanUpgrade.attack >= 4 || $humanUpgrade.hp >= 4 || $humanUpgrade.morale >= 40 || $humanUpgrade.defense >= 4)>>
		You have fully upgraded your human troops.
	<<elseif $humanUpgrade.attack >= 2 || $humanUpgrade.hp >= 2 || $humanUpgrade.morale >= 20 || $humanUpgrade.defense >= 2>>
		You have fully upgraded your human troops.
		<<if $SF.Toggle && $SF.Active >= 1 && ($humanUpgrade.attack < 4 || $humanUpgrade.hp < 4 || $humanUpgrade.morale < 40 || $humanUpgrade.defense < 4) && (($SFSupportLevel >= 2 && $SFUnit.Firebase >= 7) || ($SFSupportLevel >= 4 && $SFUnit.Drugs >= 8) || ($SFSupportLevel >= 5))>>
			With support from $SF.Lower, 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>>

[[Return this sector to standard manufacturing|Main][$cash -= Math.trunc(10000*$upgradeMultiplierArcology), $sectors[$AS].type = "Manufacturing", $weapManu = 0]] //Costs <<print cashFormat(Math.trunc(10000*$upgradeMultiplierArcology))>>//


code:
:: battleWidgets [widget nobr]

<<widget "calcSFStatistics">>
	<<if $slaveRebellion != 1 || $citizenRebellion != 1>>
		/* atk, def */
		<<set _upgradesSum = $SFUnit.Armoury + $SFUnit.Drugs + ($SFUnit.AA+$SFUnit.TA < 1) + ($SFUnit.AV+$SFUnit.TV)>>
		<<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 = $SFUnit.Troops / 10>>
		<</if>>
		<<if $SFUnit.Troops > $carriableSoldiers>>
			<<set $SFhp = $carriableSoldiers * $SFBaseHp>>
		<<else>>
			<<set $carriableSoldiers = $SFUnit.Troops>>
			<<set $SFhp = $carriableSoldiers * $SFBaseHp>>
		<</if>>
	<<else>>
		/* atk, def */
		<<set _upgradesSum = $SFUnit.Armoury + $SFUnit.Drugs + ($SFUnit.AA+$SFUnit.TA < 1) + ($SFUnit.AV+$SFUnit.TV)>>
		<<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 = $SFUnit.Troops * $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 $SF.Toggle && $SF.Active >= 1 && $SFIntervention>>
			<<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>>

<<widget "calcBribe">>
	<<if $week <= 30>>
		<<set $bribeCost = 5000 + $baseBribePerAttacker * $attackTroops>>
	<<elseif $week <= 30>>
		<<set $bribeCost = 10000 + $baseBribePerAttacker * $attackTroops>>
	<<elseif $week <= 30>>
		<<set $bribeCost = 15000 + $baseBribePerAttacker * $attackTroops>>
	<<elseif $week <= 30>>
		<<set $bribeCost = 20000 + $baseBribePerAttacker * $attackTroops>>
	<<elseif $week <= 30>>
		<<set $bribeCost = 25000 + $baseBribePerAttacker * $attackTroops>>
	<</if>>
	<<if $majorBattle == 1>>
		<<set $bribeCost *= 3>>
	<</if>>
	<<set $bribeCost = Math.trunc(Math.clamp($bribeCost,0,1000000))>>
<</widget>>


code:
:: miscSecExpWidgets [widget nobr]

<<widget "calcInitialTrade">>
	<<set _initialTrade = random(20,30)>>
	<<if $terrain == "urban">>
		<<set _initialTrade += random(10)>>
	<</if>>
	<<if $terrain == "ravine">>
		<<set _initialTrade -= random(5)>>
	<</if>>
	<<if $PC.career == "wealth" || $PC.career == "capitalist" || $PC.career == "celebrity" || $PC.career == "BlackHat">>
		<<set _initialTrade += random(5)>>
	<<elseif $PC.career == "escort" || $PC.career == "servant" || $PC.career == "gang">>
		<<set _initialTrade -= random(5)>>
	<</if>>
	<<set $trade = _initialTrade>>
<</widget>>

<<widget "recalcManpower">>
	<<if $wasToggledBefore == 0>>
		<<if $mercenaries == 1>>
			<<set $mercFreeManpower = random(5,20)>>
		<<elseif $mercenaries > 1>>
			<<set $mercFreeManpower = random(10,30)>>
		<</if>>
	<</if>>

	<<set _correctEmployedMP = 0>>
	<<for _i = 0; _i < $slaveUnits.length; _i++>>
		<<set _correctEmployedMP += $slaveUnits[_i].troops>>
	<</for>>

	<<if $slavesEmployedManpower != _correctEmployedMP>>
		<<set $slavesEmployedManpower = _correctEmployedMP>>
	<</if>>

	<<set _correctEmployedMP = 0>>
	<<for _i = 0; _i < $militiaUnits.length; _i++>>
		<<set _correctEmployedMP += $militiaUnits[_i].troops>>
	<</for>>

	<<if $militiaEmployedManpower != _correctEmployedMP>>
		<<set $militiaEmployedManpower = _correctEmployedMP>>
	<</if>>

	<<set _correctEmployedMP = 0>>
	<<for _i = 0; _i < $mercUnits.length; _i++>>
		<<set _correctEmployedMP += $mercUnits[_i].troops>>
	<</for>>

	<<if $mercEmployedManpower != _correctEmployedMP>>
		<<set $mercEmployedManpower = _correctEmployedMP>>
	<</if>>

	<<set $militiaTotalManpower = $militiaEmployedManpower + $militiaFreeManpower>>
	<<set $mercTotalManpower = $mercEmployedManpower + $mercFreeManpower>>
<</widget>>

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

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

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

<<widget "recalcEdictsUpkeep">>
	/* authority cost */
	<<set _newAuthUpkeep = 0>>

	<<if $sellData == 1>>
		<<set _newAuthUpkeep += 10>>
	<</if>>

	<<if $slaveSoldierPrivilege == 1>>
		<<set _newAuthUpkeep += 10>>
	<</if>>

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

	<<if $slavesOfficers == 1>>
		<<set _newAuthUpkeep += 10>>
	<</if>>

	/* cash cost */
	<<set _newUpkeep = 0>>

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

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

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

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

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

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

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

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

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

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

	<<set $edictsUpkeep = _newUpkeep>>
	<<set $edictsAuthUpkeep = _newAuthUpkeep>>
<</widget>>

<<widget "recalcUnits">>
	<<set $maxUnits = 6 + $secBarracksUpgrades.size * 2>>
	<<set $activeUnits = $militiaUnits.length + $slaveUnits.length + $mercUnits.length>>
<</widget>>

<<widget "fixBrokenUnits">>
	<<if ndef $secBots.ID>>
		<<set $secBots.ID = -1>>
	<</if>>
	<<if $secBots.maxTroops < 30>>

Fixed security bots wrong max troop count.
		<<set $secBots.maxTroops = 30>>
	<</if>>
	<<if !isInt($secBots.troops)>>

Fixed security bots wrong max troop count.
		<<set $secBots.troops = $secBots.maxTroops>>
	<</if>>
	<<if $secBots.troops > 0 && $secBots.active != 1>>

Fixed security bots wrong "active" flag.
		<<set $secBots.active = 1>>
	<</if>>

	<<for _i = 0; _i < $militiaUnits.length; _i++>>
		<<if ndef $militiaUnits[_i].SF>>

Set militia missing flag
			<<set $militiaUnits[_i].SF = 0>>
		<</if>>
		<<if ndef $militiaUnits[_i].ID>>

Set militia missing ID
			<<generateUnitID $militiaUnits[_i]>>
		<</if>>
		<<if ndef $militiaUnits[_i].cyber>>

Set militia missing flag
			<<set $militiaUnits[_i].cyber = 0>>
		<</if>>
		<<if ndef $militiaUnits[_i].commissars>>

Set militia missing flag
			<<set $militiaUnits[_i].commissars = 0>>
		<</if>>
		<<if $militiaUnits[_i].maxTroops < 30>>

Fixed militia unit wrong max troop count.
			<<set $militiaUnits[_i].maxTroops = 30>>
		<</if>>
		<<if !isInt($militiaUnits[_i].troops)>>
			<<set $militiaUnits[_i].troops = $militiaUnits[_i].maxTroops>>

Fixed militia unit wrong troop count.
		<</if>>
		<<if $militiaUnits[_i].troops > 0 && $militiaUnits[_i].active != 1>>

Fixed militia unit wrong "active" flag.
			<<set $militiaUnits[_i].active = 1>>
		<</if>>
	<</for>>

	<<for _i = 0; _i < $slaveUnits.length; _i++>>
		<<if ndef $slaveUnits[_i].SF>>

Set slave missing flag
			<<set $slaveUnits[_i].SF = 0>>
		<</if>>
		<<if ndef $slaveUnits[_i].ID>>

Set slave missing ID
			<<generateUnitID $slaveUnits[_i]>>
		<</if>>
		<<if ndef $slaveUnits[_i].cyber>>

Set slave missing flag
			<<set $slaveUnits[_i].cyber = 0>>
		<</if>>
		<<if ndef $slaveUnits[_i].commissars>>

Set slave missing flag
			<<set $slaveUnits[_i].commissars = 0>>
		<</if>>
		<<if $slaveUnits[_i].maxTroops < 30>>

Fixed slave unit wrong max troop count.
			<<set $slaveUnits[_i].maxTroops = 30>>
		<</if>>
		<<if !isInt($slaveUnits[_i].troops)>>
			<<set $slaveUnits[_i].troops = $slaveUnits[_i].maxTroops>>

Fixed slave unit wrong troop count.
		<</if>>
		<<if $slaveUnits[_i].troops > 0 && $slaveUnits[_i].active != 1>>

Fixed slave unit wrong "active" flag.
			<<set $slaveUnits[_i].active = 1>>
		<</if>>
	<</for>>

	<<for _i = 0; _i < $mercUnits.length; _i++>>
		<<if ndef $mercUnits[_i].SF>>

Set merc missing flag
			<<set $mercUnits[_i].SF = 0>>
		<</if>>
		<<if ndef $mercUnits[_i].ID>>

Set merc missing ID
			<<generateUnitID $mercUnits[_i]>>
		<</if>>
		<<if ndef $mercUnits[_i].cyber>>

Set merc missing flag
			<<set $mercUnits[_i].cyber = 0>>
		<</if>>
		<<if ndef $mercUnits[_i].commissars>>

Set merc missing flag
			<<set $mercUnits[_i].commissars = 0>>
		<</if>>
		<<if $mercUnits[_i].maxTroops < 30>>

Fixed merc unit wrong max troop count.
			<<set $mercUnits[_i].maxTroops = 30>>
		<</if>>
		<<if !isInt($mercUnits[_i].troops)>>
			<<set $mercUnits[_i].troops = $mercUnits[_i].maxTroops>>

Fixed merc unit wrong troop count.
		<</if>>
		<<if $mercUnits[_i].troops > 0 && $mercUnits[_i].active != 1>>

Fixed merc unit wrong "active" flag.
			<<set $mercUnits[_i].active = 1>>
		<</if>>
	<</for>>
<</widget>>

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

<<widget "fixBrokenFS">>
	<<if $arcologies[0].FSSubjugationist !== "unset" && !Number.isFinite($arcologies[0].FSSubjugationist)>>
		<<set $arcologies[0].FSSubjugationist = 10>>

Fixed NaN FS value for FSSubjugationist
	<</if>>
	<<if $arcologies[0].FSGenderRadicalist !== "unset" && !Number.isFinite($arcologies[0].FSGenderRadicalist)>>
		<<set $arcologies[0].FSGenderRadicalist = 10>>

Fixed NaN FS value for FSGenderRadicalist
	<</if>>
	<<if $arcologies[0].FSRestart !== "unset" && !Number.isFinite($arcologies[0].FSRestart)>>
		<<set $arcologies[0].FSRestart = 10>>

Fixed NaN FS value for FSRestart
	<</if>>
	<<if $arcologies[0].FSRepopulationFocus !== "unset" && !Number.isFinite($arcologies[0].FSRepopulationFocus)>>
		<<set $arcologies[0].FSRepopulationFocus = 10>>

Fixed NaN FS value for FSRepopulationFocus
	<</if>>
	<<if $arcologies[0].FSSupremacist !== "unset" && !Number.isFinite($arcologies[0].FSSupremacist)>>
		<<set $arcologies[0].FSSupremacist = 10>>

Fixed NaN FS value for FSSupremacist
	<</if>>
	<<if $arcologies[0].FSBodyPurist !== "unset" && !Number.isFinite($arcologies[0].FSBodyPurist)>>
		<<set $arcologies[0].FSBodyPurist = 10>>

Fixed NaN FS value for FSBodyPurist
	<</if>>
	<<if $arcologies[0].FSPaternalist !== "unset" && !Number.isFinite($arcologies[0].FSPaternalist)>>
		<<set $arcologies[0].FSPaternalist = 10>>

Fixed NaN FS value for FSPaternalist
	<</if>>
	<<if $arcologies[0].FSSlimnessEnthusiast !== "unset" && !Number.isFinite($arcologies[0].FSSlimnessEnthusiast)>>
		<<set $arcologies[0].FSSlimnessEnthusiast = 10>>

Fixed NaN FS value for FSSlimnessEnthusiast
	<</if>>
	<<if $arcologies[0].FSGenderFundamentalist !== "unset" && !Number.isFinite($arcologies[0].FSGenderFundamentalist)>>
		<<set $arcologies[0].FSGenderFundamentalist = 10>>

Fixed NaN FS value for FSGenderFundamentalist
	<</if>>
	<<if $arcologies[0].FSMaturityPreferentialist !== "unset" && !Number.isFinite($arcologies[0].FSMaturityPreferentialist)>>
		<<set $arcologies[0].FSMaturityPreferentialist = 10>>

Fixed NaN FS value for FSMaturityPreferentialist
	<</if>>
	<<if $arcologies[0].FSYouthPreferentialist !== "unset" && !Number.isFinite($arcologies[0].FSYouthPreferentialist)>>
		<<set $arcologies[0].FSYouthPreferentialist = 10>>

Fixed NaN FS value for FSYouthPreferentialist
	<</if>>
	<<if $arcologies[0].FSTransformationFetishist !== "unset" && !Number.isFinite($arcologies[0].FSTransformationFetishist)>>
		<<set $arcologies[0].FSTransformationFetishist = 10>>

Fixed NaN FS value for FSTransformationFetishist
	<</if>>
	<<if $arcologies[0].FSHedonisticDecadence !== "unset" && !Number.isFinite($arcologies[0].FSHedonisticDecadence)>>
		<<set $arcologies[0].FSHedonisticDecadence = 10>>

Fixed NaN FS value for FSHedonisticDecadence
	<</if>>
	<<if $arcologies[0].FSPhysicalIdealist !== "unset" && !Number.isFinite($arcologies[0].FSPhysicalIdealist)>>
		<<set $arcologies[0].FSPhysicalIdealist = 10>>

Fixed NaN FS value for FSPhysicalIdealist
	<</if>>
	<<if $arcologies[0].FSPastoralist !== "unset" && !Number.isFinite($arcologies[0].FSPastoralist)>>
		<<set $arcologies[0].FSPastoralist = 10>>

Fixed NaN FS value for FSPastoralist
	<</if>>
	<<if $arcologies[0].FSAssetExpansionist !== "unset" && !Number.isFinite($arcologies[0].FSAssetExpansionist)>>
		<<set $arcologies[0].FSAssetExpansionist = 10>>

Fixed NaN FS value for FSAssetExpansionist
	<</if>>
	<<if $arcologies[0].FSDegradationist !== "unset" && !Number.isFinite($arcologies[0].FSDegradationist)>>
		<<set $arcologies[0].FSDegradationist = 10>>

Fixed NaN FS value for FSDegradationist
	<</if>>
	<<if $arcologies[0].FSRomanRevivalist !== "unset" && !Number.isFinite($arcologies[0].FSRomanRevivalist)>>
		<<set $arcologies[0].FSRomanRevivalist = 10>>

Fixed NaN FS value for FSRomanRevivalist
	<</if>>
	<<if $arcologies[0].FSChattelReligionist !== "unset" && !Number.isFinite($arcologies[0].FSChattelReligionist)>>
		<<set $arcologies[0].FSChattelReligionist = 10>>

Fixed NaN FS value for FSChattelReligionist
	<</if>>
	<<if $arcologies[0].FSChineseRevivalist !== "unset" && !Number.isFinite($arcologies[0].FSChineseRevivalist)>>
		<<set $arcologies[0].FSChineseRevivalist = 10>>

Fixed NaN FS value for FSChineseRevivalist
	<</if>>
	<<if $arcologies[0].FSArabianRevivalist !== "unset" && !Number.isFinite($arcologies[0].FSArabianRevivalist)>>
		<<set $arcologies[0].FSArabianRevivalist = 10>>

Fixed NaN FS value for FSArabianRevivalist
	<</if>>
	<<if $arcologies[0].FSEdoRevivalist !== "unset" && !Number.isFinite($arcologies[0].FSEdoRevivalist)>>
		<<set $arcologies[0].FSEdoRevivalist = 10>>

Fixed NaN FS value for FSEdoRevivalist
	<</if>>
	<<if $arcologies[0].FSEgyptianRevivalist !== "unset" && !Number.isFinite($arcologies[0].FSEgyptianRevivalist)>>
		<<set $arcologies[0].FSEgyptianRevivalist = 10>>

Fixed NaN FS value for FSEgyptianRevivalist
	<</if>>
	<<if $arcologies[0].FSAztecRevivalist !== "unset" && !Number.isFinite($arcologies[0].FSAztecRevivalist)>>
		<<set $arcologies[0].FSAztecRevivalist = 10>>

Fixed NaN FS value for FSAztecRevivalist
	<</if>>
<</widget>>

<<widget "recalcBaseStats">>
	<<if $secBotsBaseAttack != 7 + $droneUpgrades.attack>>
		<<set $secBotsBaseAttack = 7 + $droneUpgrades.attack>>
	<</if>>
	<<if $secBotsBaseDefense != 3 + $droneUpgrades.defense>>
		<<set $secBotsBaseDefense = 3 + $droneUpgrades.defense>>
	<</if>>
	<<if $secBotsMorale != 200>>
		<<set $secBotsMorale = 200>>
	<</if>>
	<<if $secBotsBaseHp != 3 + $droneUpgrades.hp>>
		<<set $secBotsBaseHp = 3 + $droneUpgrades.hp>>
	<</if>>
	<<if $militiaBaseAttack != 7 + $humanUpgrade.attack>>
		<<set $militiaBaseAttack = 7 + $humanUpgrade.attack>>
	<</if>>
	<<if $militiaBaseDefense != 5 + $humanUpgrade.defense>>
		<<set $militiaBaseDefense = 5 + $humanUpgrade.defense>>
	<</if>>
	<<if $militiaBaseMorale != 140 + $humanUpgrade.morale>>
		<<set $militiaBaseMorale = 140 + $humanUpgrade.morale>>
	<</if>>
	<<if $militiaBaseHp != 3 + $humanUpgrade.hp>>
		<<set $militiaBaseHp = 3 + $humanUpgrade.hp>>
	<</if>>
	<<if $slaveBaseAttack != 8 + $humanUpgrade.attack>>
		<<set $slaveBaseAttack = 8 + $humanUpgrade.attack>>
	<</if>>
	<<if $slaveBaseDefense != 3 + $humanUpgrade.defense>>
		<<set $slaveBaseDefense = 3 + $humanUpgrade.defense>>
	<</if>>
	<<if $slaveBaseMorale != 110 + $humanUpgrade.morale>>
		<<set $slaveBaseMorale = 110 + $humanUpgrade.morale>>
	<</if>>
	<<if $slaveBaseHp != 3 + $humanUpgrade.hp>>
		<<set $slaveBaseHp = 3 + $humanUpgrade.hp>>
	<</if>>
	<<if $mercBaseAttack != 8 + $humanUpgrade.attack>>
		<<set $mercBaseAttack = 8 + $humanUpgrade.attack>>
	<</if>>
	<<if $mercBaseDefense != 4 + $humanUpgrade.defense>>
		<<set $mercBaseDefense = 4 + $humanUpgrade.defense>>
	<</if>>
	<<if $mercBaseMorale != 125 + $humanUpgrade.morale>>
		<<set $mercBaseMorale = 125 + $humanUpgrade.morale>>
	<</if>>
	<<if $mercBaseHp != 4 + $humanUpgrade.hp>>
		<<set $mercBaseHp = 4 + $humanUpgrade.hp>>
	<</if>>
	<<if $SFBaseAttack != 8 + $humanUpgrade.attack>>
		<<set $SFBaseAttack = 8 + $humanUpgrade.attack>>
	<</if>>
	<<if $SFBaseDefense != 4 + $humanUpgrade.defense>>
		<<set $SFBaseDefense = 4 + $humanUpgrade.defense>>
	<</if>>
	<<if $SFBaseMorale != 140 + $humanUpgrade.morale>>
		<<set $SFBaseMorale = 140 + $humanUpgrade.morale>>
	<</if>>
	<<if $SFBaseHp != 4 + $humanUpgrade.hp>>
		<<set $SFBaseHp = 4 + $humanUpgrade.hp>>
	<</if>>
<</widget>>

<<widget "replenishAllUnits">>
	<<set _hasLossesM = 0>>
	<<set _hasLossesS = 0>>
	<<set _hasLossesMe = 0>>

	<<for _i = 0; _i < $militiaUnits.length; _i++>>
		<<if $militiaUnits[_i].troops < $militiaUnits[_i].maxTroops && $militiaFreeManpower > 0>>
			<<set _hasLossesM = 1>>
			<<break>>
		<</if>>
	<</for>>

	<<for _i = 0; _i < $slaveUnits.length; _i++>>
		<<if $slaveUnits[_i].troops < $slaveUnits[_i].maxTroops && $helots > 0>>
			<<set _hasLossesS = 1>>
			<<break>>
		<</if>>
	<</for>>

	<<for _i = 0; _i < $mercUnits.length; _i++>>
		<<if $mercUnits[_i].troops < $mercUnits[_i].maxTroops && $mercFreeManpower > 0>>
			<<set _hasLossesMe = 1>>
			<<break>>
		<</if>>
	<</for>>

	<<if _hasLossesM == 1 && $militiaFreeManpower > 0 || _hasLossesS == 1 && $helots > 0 || _hasLossesMe == 1 && $mercFreeManpower > 0>>

		<<link "Replenish all units">>

			<<if _hasLossesM == 1>>
				<<for _i = 0; _i < $militiaUnits.length; _i++>>
					<<if $militiaUnits[_i].troops < $militiaUnits[_i].maxTroops && $militiaFreeManpower > 0>>
						<<if $militiaFreeManpower >= $militiaUnits[_i].maxTroops - $militiaUnits[_i].troops>>
							<<set $militiaFreeManpower -= $militiaUnits[_i].maxTroops - $militiaUnits[_i].troops>>
							<<set $militiaEmployedManpower += $militiaUnits[_i].maxTroops - $militiaUnits[_i].troops>>
							<<set _expLoss = ($militiaUnits[_i].maxTroops - $militiaUnits[_i].troops) / $militiaUnits[_i].troops>>
							<<set $militiaUnits[_i].training -= $militiaUnits[_i].training * _expLoss>>
							<<set $militiaUnits[_i].troops = $militiaUnits[_i].maxTroops>>
						<<else>>
							<<set $militiaEmployedManpower += $militiaFreeManpower>>
							<<set _expLoss = $militiaFreeManpower / $militiaUnits[_i].troops>>
							<<set $militiaUnits[_i].training -= $militiaUnits[_i].training * _expLoss>>
							<<set $militiaUnits[_i].troops += $militiaFreeManpower>>
							<<set $militiaFreeManpower = 0>>
						<</if>>
					<</if>>
				<</for>>
			<</if>>

			<<if _hasLossesS == 1>>
				<<for _i = 0; _i < $slaveUnits.length; _i++>>
					<<if $slaveUnits[_i].troops < $slaveUnits[_i].maxTroops && $helots > 0>>
						<<if $helots >= $slaveUnits[_i].maxTroops - $slaveUnits[_i].troops>>
							<<set $helots -= $slaveUnits[_i].maxTroops - $slaveUnits[_i].troops>>
							<<set $slavesEmployedManpower += $slaveUnits[_i].maxTroops - $slaveUnits[_i].troops>>
							<<set _expLoss = ($slaveUnits[_i].maxTroops - $slaveUnits[_i].troops) / $slaveUnits[_i].troops>>
							<<set $slaveUnits[_i].training -= $slaveUnits[_i].training * _expLoss>>
							<<set $slaveUnits[_i].troops = $slaveUnits[_i].maxTroops>>
						<<else>>
							<<set $slavesEmployedManpower += $helots>>
							<<set _expLoss = $helots / $slaveUnits[_i].troops>>
							<<set $slaveUnits[_i].training -= $slaveUnits[_i].training * _expLoss>>
							<<set $slaveUnits[_i].troops += $helots>>
							<<set $helots = 0>>
						<</if>>
					<</if>>
				<</for>>
			<</if>>

			<<if _hasLossesMe == 1>>
				<<for _i = 0; _i < $mercUnits.length; _i++>>
					<<if $mercUnits[_i].troops < $mercUnits[_i].maxTroops && $mercFreeManpower > 0>>
						<<if $mercFreeManpower >= $mercUnits[_i].maxTroops - $mercUnits[_i].troops>>
							<<set $mercFreeManpower -= $mercUnits[_i].maxTroops - $mercUnits[_i].troops>>
							<<set $mercEmployedManpower += $mercUnits[_i].maxTroops - $mercUnits[_i].troops>>
							<<set _expLoss = ($mercUnits[_i].maxTroops - $mercUnits[_i].troops) / $mercUnits[_i].troops>>
							<<set $mercUnits[_i].training -= $mercUnits[_i].training * _expLoss>>
							<<set $mercUnits[_i].troops = $mercUnits[_i].maxTroops>>
						<<else>>
							<<set $mercEmployedManpower += $mercFreeManpower>>
							<<set _expLoss = $mercFreeManpower / $mercUnits[_i].troops>>
							<<set $mercUnits[_i].training -= $mercUnits[_i].training * _expLoss>>
							<<set $mercUnits[_i].troops += $mercFreeManpower>>
							<<set $mercFreeManpower = 0>>
						<</if>>
					<</if>>
				<</for>>
			<</if>>

			<<if $slaveRebellion == 1 || $citizenRebellion == 1>>
				<<goto "rebellionOptions">>
			<<elseif $attackThisWeek == 1>>
				<<goto "attackOptions">>
			<<else>>
				<<goto "secBarracks">>
			<</if>>

		<</link>>

//Will replenish units as long as manpower is available
	<</if>>

<</widget>>


code:
:: unitsWidgets [widget nobr]

<<widget "militiaUnitsDescription">>
	<<if $args[0].active == 1>>
		$args[0].platoonName <<if $args[0].battlesFought > 1>> participated in $args[0].battlesFought battles and is ready to face the enemy once more at your command.<<elseif $args[0].battlesFought == 1>>is ready to face the enemy once more at your command.<<else>>is ready to face the enemy in battle.<</if>>
		Its $args[0].troops men and women are all proud citizens of your arcology, willing to put their lives on the line to protect their home.
		<<if $args[0].troops < $args[0].maxTroops>> The unit is not at its full strength of $args[0].maxTroops operatives.<</if>>
		<<if $args[0].equip == 0>>
			They are issued with simple, yet effective equipment: firearms, a few explosives and standard uniforms, nothing more.
		<<elseif $args[0].equip == 1>>
			They are issued with good, modern equipment: firearms, explosives and a few specialized weapons like sniper rifles and machine guns. They also carry simple body armor.
		<<elseif $args[0].equip == 2>>
			They are issued with excellent, high tech equipment: modern firearms, explosives, specialized weaponry and modern body armor. They are also issued with modern instruments like night vision and portable radars.
		<<else>>
			They are equipped with the best the modern world has to offer: modern firearms, explosives, specialized weaponry, experimental railguns, adaptive body armor and high tech recon equipment.
		<</if>>
		<<if $args[0].training <= 33>>
			They lack the experience to be considered consummate professionals, but their eagerness to defend the arcology makes up for it.
		<<elseif $args[0].training <= 66>>
			They have trained <<if $args[0].battlesFought > 0>>and fought<</if>> enough to be considered disciplined, professional soldiers, ready to face the battlefield.
		<<else>>
			They are consummate veterans, with a wealth of experience and perfectly trained. On the battlefield they are a well oiled war machine capable of facing pretty much anything.
		<</if>>
		<<if $args[0].loyalty < 10>>
			The unit is extremely disloyal. Careful monitoring of their activities and relationships should be implemented.
		<<elseif $args[0].loyalty < 33>>
			Their loyalty is low. Careful monitoring of their activities and relationships is advised.
		<<elseif $args[0].loyalty < 66>>
			Their loyalty is not as high as it can be, but they are not actively working against their arcology owner.
		<<elseif $args[0].loyalty < 90>>
			Their loyalty is high and strong. The likelihood of this unit betraying the arcology is low to non-existent.
		<<else>>
			The unit is fanatically loyal. They would prefer death over betrayal.
		<</if>>
		<<if $args[0].cyber == 1>>
			The soldiers of the unit have been enhanced with numerous cyberaugmentations which greatly increase their raw power.
		<</if>>
		<<if $args[0].medics == 1>>
			The unit has a dedicated squad of medics that will follow them in battle.
		<</if>>
		<<if $SF.Toggle && $SF.Active >= 1 && $args[0].SF>>
			The unit has attached advisors from $SF.Lower that will help the squad remain tactically aware and active.
		<</if>>
	<<else>>
		$args[0].platoonName lost too many operatives in the $args[0].battlesFought it fought and can no longer be considered a unit at all.
	<</if>>
<</widget>>

<<widget "slaveUnitsDescription">>
	<<if $args[0].active == 1>>
		$args[0].platoonName <<if $args[0].battlesFought > 1>> participated in $args[0].battlesFought battles and is ready to face the enemy once more at your command.<<elseif $args[0].battlesFought == 1>>is ready to face the enemy once more at your command.<<else>>is ready to face the enemy in battle.<</if>>
		Its $args[0].troops men and women are slaves in your possession, tasked with the protection of their owner and their arcology.
		<<if $args[0].troops < $args[0].maxTroops>> The unit is not at its full strength of $args[0].maxTroops operatives.<</if>>
		<<if $args[0].equip == 0>>
			They are issued with simple, yet effective equipment: firearms, a few explosives and standard uniforms, nothing more.
		<<elseif $args[0].equip == 1>>
			They are issued with good, modern equipment: firearms, explosives and a few specialized weapons like sniper rifles and machine guns. They also carry simple body armor.
		<<elseif $args[0].equip == 2>>
			They are issued with excellent, high tech equipment: modern firearms, explosives, specialized weaponry and modern body armor. They are also issued with modern instruments like night vision and portable radars.
		<<else>>
			They are equipped with the best the modern world has to offer: modern firearms, explosives, specialized weaponry, experimental railguns, adaptive body armor and high tech recon equipment.
		<</if>>
		<<if $args[0].training <= 33>>
			They lack the experience to be considered professionals, but their eagerness to prove themselves makes up for it.
		<<elseif $args[0].training <= 66>>
			They have trained <<if $args[0].battlesFought > 0>>and fought<</if>> enough to be considered disciplined, professional soldiers, ready to face the battlefield.
		<<else>>
			They are consummate veterans, with a wealth of experience and perfectly trained. On the battlefield they are a well oiled war machine capable of facing pretty much anything.
		<</if>>
		<<if $args[0].loyalty < 10>>
			The unit is extremely disloyal. Careful monitoring of their activities and relationships should be implemented.
		<<elseif $args[0].loyalty < 33>>
			Their loyalty is low. Careful monitoring of their activities and relationships is advised.
		<<elseif $args[0].loyalty < 66>>
			Their loyalty is not as high as it can be, but they are not actively working against their arcology owner.
		<<elseif $args[0].loyalty < 90>>
			Their loyalty is high and strong. The likelihood of this unit betraying the arcology is low to non-existent.
		<<else>>
			The unit is fanatically loyal. They would prefer death over betrayal.
		<</if>>
		<<if $args[0].cyber == 1>>
			The soldiers of the unit have been enhanced with numerous cyberaugmentations which greatly increase their raw power.
		<</if>>
		<<if $args[0].medics == 1>>
			The unit has a dedicated squad of medics that will follow them in battle.
		<</if>>
		<<if $SF.Toggle && $SF.Active >= 1 && $args[0].SF>>
			The unit has attached advisors from $SF.Lower that will help the squad remain tactically aware and active.
		<</if>>
	<<else>>
		$args[0].platoonName lost too many operatives in the $args[0].battlesFought it fought and can no longer be considered a unit at all.
	<</if>>
<</widget>>

<<widget "mercUnitsDescription">>
	<<if $args[0].active == 1>>
		$args[0].platoonName <<if $args[0].battlesFought > 1>> participated in $args[0].battlesFought battles and is ready to face the enemy once more at your command.<<elseif $args[0].battlesFought == 1>>is ready to face the enemy once more at your command.<<else>>is ready to face the enemy in battle.<</if>>
		Its $args[0].troops men and women are mercenaries contracted to defend the arcology against external threats.
		<<if $args[0].troops < $args[0].maxTroops>> The unit is not at its full strength of $args[0].maxTroops operatives.<</if>>
		<<if $args[0].equip == 0>>
			They are issued with simple, yet effective equipment: firearms, a few explosives and standard uniforms, nothing more.
		<<elseif $args[0].equip == 1>>
			They are issued with good, modern equipment: firearms, explosives and a few specialized weapons like sniper rifles and machine guns. They also carry simple body armor.
		<<elseif $args[0].equip == 2>>
			They are issued with excellent, high tech equipment: modern firearms, explosives, specialized weaponry and modern body armor. They are also issued with modern instruments like night vision and portable radars.
		<<else>>
			They are equipped with the best the modern world has to offer: modern firearms, explosives, specialized weaponry, experimental railguns, adaptive body armor and high tech recon equipment.
		<</if>>
		<<if $args[0].training <= 33>>
			They lack the experience to be considered professionals, but they're trained more than enough to still be an effective unit.
		<<elseif $args[0].training <= 66>>
			They have trained <<if $args[0].battlesFought > 0>>and fought<</if>> enough to be considered disciplined, professional soldiers, ready to face the battlefield.
		<<else>>
			They are consummate veterans, with a wealth of experience and perfectly trained. On the battlefield they are a well oiled war machine capable of facing pretty much anything.
		<</if>>
		<<if $args[0].loyalty < 10>>
			The unit is extremely disloyal. Careful monitoring of their activities and relationships should be implemented.
		<<elseif $args[0].loyalty < 33>>
			Their loyalty is low. Careful monitoring of their activities and relationships is advised.
		<<elseif $args[0].loyalty < 66>>
			Their loyalty is not as high as it can be, but they are not actively working against their arcology owner.
		<<elseif $args[0].loyalty < 90>>
			Their loyalty is high and strong. The likelihood of this unit betraying the arcology is low to non-existent.
		<<else>>
			The unit is fanatically loyal. They would prefer death over betrayal.
		<</if>>
		<<if $args[0].cyber == 1>>
			The soldiers of the unit have been enhanced with numerous cyberaugmentations which greatly increase their raw power.
		<</if>>
		<<if $args[0].medics == 1>>
			The unit has a dedicated squad of medics that will follow them in battle.
		<</if>>
		<<if $SF.Toggle && $SF.Active >= 1 && $args[0].SF>>
			The unit has attached advisors from $SF.Lower that will help the squad remain tactically aware and active.
		<</if>>
	<<else>>
		$args[0].platoonName lost too many operatives in the $args[0].battlesFought it fought and can no longer be considered a unit at all.
	<</if>>
<</widget>>

<<widget "secBotsDescription">>
	<<if $secBots.active == 1>>
		The <<print $secBots.troops>> security drones are assembled in an ordered line in front of you, absolutely silent and ready to receive their orders.
		<<if $secBots.troops < $secBots.maxTroops>> The unit is not at its full strength of $secBots.maxTroops drones.<</if>>
		<<if $secBots.equip == 0>>
			They are equipped with light weaponry, mainly anti-riot non lethal weapons. Not particularly effective in battle.
		<<elseif $secBots.equip == 1>>
			They are equipped with light firearms, not an overwhelming amount of firepower, but with their mobility good enough to be effective.
		<<elseif $secBots.equip == 2>>
			They are equipped with powerful, modern firearms and simple armor mounted around their frames. They do not make for a pretty sight, but on the battlefield they are a dangerous weapon.
		<<else>>
			They are equipped with high energy railguns and adaptive armor. They are a formidable force on the battlefield, even for experienced soldiers.
		<</if>>
	<<else>>
		There are too few security drones left to be able to deploy them on the battlefield.
	<</if>>
<</widget>>

<<widget "generateUnitID">>
	<<set _newID = 0>>

	<<for _i = 0; _i < $militiaUnits.length; _i++>>
		<<if $militiaUnits[_i].ID >= _newID>>
			<<set _newID = $militiaUnits[_i].ID + 1>>
		<</if>>
	<</for>>

	<<for _i = 0; _i < $slaveUnits.length; _i++>>
		<<if $slaveUnits[_i].ID >= _newID>>
			<<set _newID = $slaveUnits[_i].ID + 1>>
		<</if>>
	<</for>>

	<<for _i = 0; _i < $mercUnits.length; _i++>>
		<<if $mercUnits[_i].ID >= _newID>>
			<<set _newID = $mercUnits[_i].ID + 1>>
		<</if>>
	<</for>>

	<<set $args[0].ID = _newID>>
<</widget>>


code:
:: CheatEdit [nobr]
<<set $nextButton = "Back to $SF.Lower's Firebase", $nextLink = "Firebase", $returnTo = "Firebase">>
<<= Count()>>__Upgrades__: $SF.Units/_max
<<if $SF.Units >= 30>><<set _T1 = 1>><<else>><<set _T1 = 0>><</if>>

''Firebase:'' <<textbox "$SFUnit.Firebase" $SFUnit.Firebase "CheatEdit">>/_FU

''Armory:'' <<textbox "$SFUnit.Armoury" $SFUnit.Armoury "CheatEdit">>/_AU

''Drug Lab:'' <<textbox "$SFUnit.Drugs" $SFUnit.Drugs "CheatEdit">>/_DrugsU
<<if $SFUnit.Firebase >= 2>>

''Drone Bay:'' <<textbox "$SFUnit.Drones" $SFUnit.Drones "CheatEdit">>/_DU<</if>>

<<if $SFUnit.Firebase >= 1 && $terrain !== "oceanic">>

''Garage:''

 ''Vehicles:''

  ''Attack:'' <<textbox "$SFUnit.AV" $SFUnit.AV "CheatEdit">>/_AVU

  ''Transport:'' <<textbox "$SFUnit.TV" $SFUnit.TV "CheatEdit">>/_TVU
	<<if _T1>>

 ''Prototype Goliath Tank:'' <<textbox "$SFUnit.PGT" $SFUnit.PGT "CheatEdit">>/_PGTU<</if>>
<</if>>

<<if $SFUnit.Firebase >= 4>>

''Hangar:''

 ''Aircraft:''

  ''Attack:'' <<textbox "$SFUnit.AA" $SFUnit.AA "CheatEdit">>/_AAU

  ''Transport:'' <<textbox "$SFUnit.TA" $SFUnit.TA "CheatEdit">>/_TAU
	<<if _T1>>

 ''Spaceplane'': <<textbox "$SFUnit.SpacePlane" $SFUnit.SpacePlane "CheatEdit">>/_SPU

 ''Gunship:'' <<textbox "$SFUnit.GunS" $SFUnit.GunS "CheatEdit">>/_GunSU

''Launch Bay:''

 ''Satellite:'' <<textbox "$SFUnit.Satellite" $SFUnit.Satellite "CheatEdit">>/_SatU
		<<if $terrain !== "oceanic">>

 ''Giant Robot:'' <<textbox "$SFUnit.GiantRobot" $SFUnit.GiantRobot "CheatEdit">>/_GRU<</if>>

 ''Cruise Missile:'' <<textbox "$SFUnit.MissileSilo" $SFUnit.MissileSilo "CheatEdit">>/_MSU
<<if $terrain === "oceanic" || $terrain === "marine">>

''Naval Yard:''

 ''Aircraft Carrier:'' <<textbox "$SFUnit.AircraftCarrier" $SFUnit.AircraftCarrier "CheatEdit">>/_ACU

 ''Submarine:'' <<textbox "$SFUnit.Sub" $SFUnit.Sub "CheatEdit">>/_SubU

 ''Amphibious Transport:'' <<textbox "$SFUnit.HAT" $SFUnit.HAT "CheatEdit">>/_HATU
<</if>><</if>><</if>>


code:
:: SFColonelSexDec
<<switch $SFColonel.Core>>
<<case "shell shocked">>

	The entire time it is obvious that The Colonel is reliving a horrible event.

<<link "Try to bring her back">>
	<<replace "#result7">>
	"You made an attempt to try to bring her back to the present."
	<</replace>>
	<</link>>

<<link "Leave her be">>
	<<replace "#result7">>
	"It is probably better that she tries to deal with her demons alone"
	<</replace>>
	<</link>>

<<default>>
	Inset sex noises here.
<</switch>>


code:
:: Firebase [nobr]

	<<if ndef $Tour>> <<set $Tour = 0>> <</if>>

	<<if $Tour === 0>>

		<<= Count()>> <<if $SF.Lower != "the special force">>

			<<set $SF.Caps = $SF.Lower.replace("the ", "The ")>> <</if>>

		<<switch _Env>> <<case 4>>

				<<set _EnvCash2 = 450,_EnvCash3 = 200,_EnvCash4 = 100,_EnvProsp = 3>>

			<<case 3>>

				<<set _EnvCash2 = 500,_EnvCash3 = 250,_EnvCash4 = 150,_EnvProsp = 5>>

			<<case 2>>

				<<set _EnvCash2 = 550,_EnvCash3 = 300,_EnvCash4 = 200,_EnvProsp = 7>>

		<</switch>> <<set $nextButton = "Back to Main",$nextLink = "Main",$returnTo = "Firebase">>

		<<if $cheatMode > 0>> <<link "Cheat edit""CheatEdit">> <</link>>
 <</if>>

		The firebase of $arcologies[0].name's <<textbox "$SF.Lower" $SF.Lower "Firebase">> is located in the lower levels, occupying unneeded warehouse space. It is not accessible to the general citizenry, but your personal elevator has express service to it. As you step off, two soldiers in combat armor manning the entry checkpoint tense before recognizing their Marshal and stepping aside with a sharp salute.

<<if $SFTradeShow.CanAttend === 1 || ($SFColonel.Fun + $SFColonel.Talk >= 1)>>

			You make your way to the operations center, <<print SFC()>> is handling a minor issue. As you enter, she salutes. <<if $SFTradeShow.CanAttend == 1>> The Colonel is away at her merc meetup, so <<print SFC()>> will assist you.<</if>>

		<<elseif random(1,100) > 5>>

			You make your way to the operations center. The Colonel is

				<<if random(1,100) > 50>>glancing between her tablet and the large wallscreen, occasionally taking notes or barking orders.

				<<else>>examining a table with a map of the surrounding area, planning maneuvers in the event of an attack.<</if>>

			 She notices your entrance and turns her attention to you.

			<<if $SFColonel.Core == "brazen">>

				She gives a textbook salute. "<<= properTitle()>>, how can I help you?"

			<<else>>

			"Hey boss, what do you need?"<</if>>

		<<else>>

			You make your way to the operations center, finding it empty. A short walk takes you through the barracks to the Colonel's quarters. As you approach, the Colonel <<print ColonelQuarters()>>

		<</if>>

		<<if ndef $SFTradeShow.View && ($SFColonel.Fun + $SFColonel.Talk < 1)>>

Her expression changes as something jogs her memory. "Before we begin <<if $SFColonel.Core == "brazen">><<= properTitle()>><<else>>boss<</if>>, back when I was a merc me and a couple of my old friends would have a meetup every several months. Drinking, fucking, drugs... a little poker. It eventually grew into a whole thing, and now we bring our latest and greatest toys to show off, maybe make some money off selling the schematics. I'd like to continue going, for old times' sake."

[[Grant leave|Firebase][$SFTradeShow.CanAttend = 1,$SFTradeShow.View = 1]]

[[Request she remain on base|Firebase][$SFTradeShow.CanAttend = -2,$SFTradeShow.View = 0]]

		<</if>>

			<<if $SFTradeShow.History >= 1 && (Math.trunc($week/24) === ($week/24)) && $SFTradeShow.CanAttend === -1>>

Her expression changes as something jogs her memory. "Before we begin <<if $SFColonel.Core == "brazen">><<= properTitle()>><<else>>boss<</if>>, that biannual merc meetup has come around again. You've already gave me leave to attend, but I just wanted to be sure I'm still clear to go."

[[Grant leave.|Firebase][$SFTradeShow.CanAttend = 1]]

[[Request she remain on site.|Firebase][$SFTradeShow.CanAttend = -1]]

		<</if>>

		<<if $SFTradeShow.History >= 1 && ((Math.trunc(($week-1)/24) === ($week-1)/24) || (Math.trunc(($week-2)/24) === ($week-2)/24) || (Math.trunc(($week-3)/24) === ($week-3)/24))>>

			While at the recent merc meetup, the Colonel made @@.yellowgreen;<<print cashFormat(Math.ceil($SFTradeShow.Income))>>@@ selling generic schematics to her friends, <<print commaNum($SFTradeShow.Helots)>> menial slaves were won in a poker game, and <<print commaNum($SFTradeShow.TotalMercs)>> mercenaries were persuaded to join $SF.Lower.

Total earnings thus far: @@.yellowgreen;<<print cashFormat(Math.ceil($SFTradeShow.Revenue))>>@@ in income, <<print commaNum($SFTradeShow.TotalHelots)>> menial slaves and <<print commaNum($SFTradeShow.Mercs)>> mercenaries joined across $SFTradeShow.History meetups.

		<</if>>

		<<print Interactions()>>

		<<include "WC">>

		<<if $SF.SpecOpsLock < 1>>

<<switch $SF.SpecOps>>

			<<case 0>>

No soldiers are working undercover. [[Full time assignement|Firebase][$SF.SpecOpsLock = 1]]

[[Reassign soldiers|Firebase][$SF.SpecOps = -1]]

			<<case 1>>

A small section of soldiers are working undercover. [[Full time assignement|Firebase][$SF.SpecOpsLock = 1]]

[[Reassign soldiers|Firebase][$SF.SpecOps = -1]]

			<<case 2>>

A large section of soldiers are working undercover. [[Full time assignement|Firebase][$SF.SpecOpsLock = 1]]

[[Reassign soldiers|Firebase][$SF.SpecOps = -1]]

			<<default>>

Would you like to assign soldiers to undercover duty?

[[Do not assign soldiers to work undercover|Firebase][$SF.SpecOps = 0]]

[[Assign a small section of soldiers to work undercover|Firebase][$SF.SpecOps = 1]]

[[Assign a large section of soldiers to work undercover|Firebase][$SF.SpecOps = 2]]

			<</switch>>
		<<else>>

 <<if $SF.SpecOps < 1>>''Zero''<<elseif $SF.SpecOps < 2>>A ''small'' section<<else>>A ''large'' section<</if>> of the special force is assigned to undercover work. [[Re-alloclate the units|Firebase][$SF.SpecOpsLock = 0]]
		<</if>>

		<<if $SFUnit.Firebase > 5 && $secExp > 0 && $SFSupportLevel >= 4 && $maxUnits === 16 && $readiness <= 10>>

[[Provide the security force with their own section.|Firebase][$maxUnits += 4,$readiness = 10,$cash -= Math.ceil((750000*(1.15+($SF.Units/1000))*(1.15+($SFUnit.Firebase/10)))*_Env)]]

			@@.yellowgreen;<<print cashFormat(Math.ceil((750000*(1.15+($SF.Units/1000))*(1.15+($SFUnit.Firebase/10)))*_Env))>>@@

		<</if>>

 <<if $SF.Bonus < 1>>
			<<link "Provide @@.yellowgreen;<<print cashFormat(Math.ceil(_SFCashBonus))>>@@ in bonuses for basic FS acceptance amongst $SF.Lower.""Firebase">> <<set $SF.Bonus = 1>> <</link>>
		<<else>>
			You are providing @@.yellowgreen;<<print cashFormat(Math.ceil(_SFCashBonus))>>@@ in bonuses for basic FS acceptance amongst $SF.Lower. [[Withdraw the bonuses.|Firebase][$SF.Bonus = 0]]
		<</if>>

		<<include "Upgrades">>

<<link "Tour the firebase" "Firebase">><<set $Tour = 1>><</link>>

	<</if>>

	<<if $Tour === 1>> <<set $nextButton = " ">>

		<<=Count()>><<include "FlavourText">>

		[[Return to Operations|Firebase][$Tour = 0]]

	<</if>>