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 num($SF.Squad.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 $menials += _slaveManpower>>
					<<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 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 $menials += _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 < $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 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>>
		<<run cashX(forceNeg(1000 * _militiaRebelledID.length), "war")>>

		<<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>>
		<<run cashX(forceNeg(1000 * _slaveRebelledID.length), "war")>>
		<<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>>
		<<run cashX(forceNeg(1000 * _mercRebelledID.length), "war")>>
		<<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 !Number.isInteger(_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,$SF.Squad.Troops)>>
		$SF.Lower, $SF.Squad.Troops strong, is called to join the battle:
		<<set $SF.Squad.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 $menials -= 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 $menials += _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 $menials += _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>>
		<<run cashX(forceNeg(1000 * _militiaRebelledID.length), "war")>>

		<<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>>
		<<run cashX(forceNeg(1000 * _slaveRebelledID.length), "war")>>
		<<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>>
		<<run cashX(forceNeg(1000 * _mercRebelledID.length), "war")>>
		<<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 $weapMenials> 0>>Assigned here are $weapMenials 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 num($menials)>> free menial slaves. This manufacturing complex can house 500 at most, with <<print 500 - $weapMenials>> free slots.

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

	[[Buy|weaponsManufacturing][$menials+=1,$menialSupplyFactor-=1,cashX(forceNeg(_menialPrice), "menialTransfer")]]
	<<if $cash > (menialSlaveCost(10))*10>>
		[[(x10)|weaponsManufacturing][$menials+=10,$menialSupplyFactor-=10,cashX(forceNeg((menialSlaveCost(10))*10), "menialTransfer")]]
	<</if>>
	<<if $cash > (menialSlaveCost(100))*100>>
		[[(x100)|weaponsManufacturing][$menials+=100,$menialSupplyFactor-=100,cashX(forceNeg((menialSlaveCost(100))*100), "menialTransfer")]]
	<</if>>
	<<if $cash > (_menialPrice+1)*2>>
		<<set _menialBulkPremium = Math.trunc(1 + Math.clamp($cash/_menialPrice,0,_bulkMax)/400)>>
		[[(max)|weaponsManufacturing][$menials+=Math.trunc(Math.clamp($cash/(_menialPrice+_menialBulkPremium),0,_bulkMax)),$menialSupplyFactor-=Math.trunc(Math.clamp($cash/(_menialPrice+_menialBulkPremium),0,_bulkMax)),cashX(forceNeg(Math.trunc(Math.clamp($cash/(_menialPrice+_menialBulkPremium),0,_bulkMax)*(_menialPrice+_menialBulkPremium))), "menialTransfer")]]
	<</if>>
	//Bulk transactions may require offering a premium.//
<</if>>
<</if>>

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

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

	<<link "Transfer out all menial slaves">>
		<<set $menials += $weapMenials>>
		<<set $weapMenials = 0>>
		<<goto "weaponsManufacturing">>
	<</link>>
<</if>>

<<if $weapProductivity < 5>>
	<<link "Invest in automating the complex">>
		<<run cashX(forceNeg(10000 * $weapProductivity), "capEx")>>
		<<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">>
		<<run cashX(forceNeg(10000 * $weapLab), "capEx")>>
		<<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}>>
			<<run cashX(forceNeg(10000*$HackingSkillMultiplier), "capEx")>>
			<<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}>>
			<<run cashX(forceNeg(10000), "capEx")>>
			<<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}>>
			<<run cashX(forceNeg(30000), "capEx")>>
			<<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}>>
			<<run cashX(forceNeg(30000), "capEx")>>
			<<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}>>
			<<run cashX(forceNeg(30000), "capEx")>>
			<<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}>>
			<<run cashX(forceNeg(60000), "capEx")>>
			<<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}>>
			<<run cashX(forceNeg(60000), "capEx")>>
			<<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}>>
			<<run cashX(forceNeg(120000*$HackingSkillMultiplier), "capEx")>>
			<<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}>>
			<<run cashX(forceNeg(120000*$HackingSkillMultiplier), "capEx")>>
			<<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 && $SF.Squad.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 && $SF.Squad.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}>>
				<<run cashX(forceNeg(300000), "capEx")>>
				<<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}>>
				<<run cashX(forceNeg(1000000*$HackingSkillMultiplier), "capEx")>>
				<<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 && $SF.Squad.Firebase >= 7) || ($SFSupportLevel >= 4 && $SF.Squad.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][cashX(forceNeg(Math.trunc(10000*$upgradeMultiplierArcology)), "capEx"), $sectors[$AS].type = "Manufacturing", $weapManu = 0]] //Costs <<print cashFormat(Math.trunc(10000*$upgradeMultiplierArcology))>>//

repo_name: Fekenol/fc


code:
:: battleWidgets [widget nobr]

<<widget "calcSFStatistics">>
	<<if $slaveRebellion != 1 || $citizenRebellion != 1>>
		/* atk, def */
		<<set _upgradesSum = $SF.Squad.Armoury + $SF.Squad.Drugs + ($SF.Squad.AA+$SF.Squad.TA < 1) + ($SF.Squad.AV+$SF.Squad.TV)>>
		<<if !Number.isInteger(_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 * ($SF.Squad.GunS + (($SF.Squad.AV + $SF.Squad.TV)/2))>>
		<<if !Number.isInteger($carriableSoldiers)>>
			<<set $carriableSoldiers = $SF.Squad.Troops / 10>>
		<</if>>
		<<if $SF.Squad.Troops > $carriableSoldiers>>
			<<set $SFhp = $carriableSoldiers * $SFBaseHp>>
		<<else>>
			<<set $carriableSoldiers = $SF.Squad.Troops>>
			<<set $SFhp = $carriableSoldiers * $SFBaseHp>>
		<</if>>
	<<else>>
		/* atk, def */
		<<set _upgradesSum = $SF.Squad.Armoury + $SF.Squad.Drugs + ($SF.Squad.AA+$SF.Squad.TA < 1) + ($SF.Squad.AV+$SF.Squad.TV)>>
		<<if !Number.isInteger(_upgradesSum)>>
			<<set _upgradesSum = random(10,15)>>
		<</if>>
		<<set $SFatk = Math.trunc(0.75 * _upgradesSum)>>
		<<set $SFdef = Math.trunc(0.50 * _upgradesSum)>>
		/* hp */
		<<set $SFhp = $SF.Squad.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 $SF.Toggle && $SF.Active >= 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 "recalcReqMenials">>
	<<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 $reqMenials = _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>>
	<<if $readiness === 10>> <<set $maxUnits += 4>> <</if>>
<</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 !Number.isInteger($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 !Number.isInteger($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 !Number.isInteger($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 !Number.isInteger($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 !Number.isInteger($totalKills)>>
		<<set $totalKills = 0>>
	<</if>>
	<<if !Number.isInteger($mercTotalCasualties)>>
		<<set $mercTotalCasualties = 0>>
	<</if>>
	<<if !Number.isInteger($mercEmployedManpower)>>
		<<set $mercEmployedManpower = 0>>
	<</if>>
	<<if !Number.isInteger($mercTotalManpower)>>
		<<set $mercTotalManpower = 0>>
	<</if>>
	<<if !Number.isInteger($slavesTotalCasualties)>>
		<<set $slavesTotalCasualties = 0>>
	<</if>>
	<<if !Number.isInteger($slavesEmployedManpower)>>
		<<set $slavesEmployedManpower = 0>>
	<</if>>
	<<if !Number.isInteger($militiaTotalCasualties)>>
		<<set $militiaTotalCasualties = 0>>
	<</if>>
	<<if !Number.isInteger($militiaEmployedManpower)>>
		<<set $militiaEmployedManpower = 0>>
	<</if>>
	<<if !Number.isInteger($militiaFreeManpower)>>
		<<set $militiaFreeManpower = 0>>
	<</if>>
	<<if !Number.isInteger($militiaTotalManpower)>>
		<<set $militiaTotalManpower = 0>>
	<</if>>
	<<if !Number.isInteger($battlesCount)>>
		<<set $battlesCount = 0>>
	<</if>>
<</widget>>

<<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 && $menials > 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 && $menials > 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 && $menials > 0>>
						<<if $menials >= $slaveUnits[_i].maxTroops - $slaveUnits[_i].troops>>
							<<set $menials -= $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 += $menials>>
							<<set _expLoss = $menials / $slaveUnits[_i].troops>>
							<<set $slaveUnits[_i].training -= $slaveUnits[_i].training * _expLoss>>
							<<set $slaveUnits[_i].troops += $menials>>
							<<set $menials = 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 nonlethal 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.Size/_max(<<print ($SF.Size/_max).toFixed(2)>>%)

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

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

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

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

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

''Garage:''

 ''Vehicles:''

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

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

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

<<if $SF.Squad.Firebase >= 4>>

''Hangar:''

 ''Aircraft:''

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

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

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

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

''Launch Bay:''

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

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

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

''Naval Yard:''

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

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

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


code:
:: SFColonelSexDec

<<switch $SF.Colonel.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:
:: Security Force Naming-Colonel [nobr]

<<set $nextButton = " ">>
<<if $SF.IntroProgress < 0>>
	<<setNonlocalPronouns $seeDicks>>
	You instruct $assistantName to announce to the arcology's citizenry that you will be making an important announcement in the near future regarding the security situation. Given the damage still present from the Daughters' attack, everyone will be tuning in. You also instruct your assistant to begin quietly investigating potential leadership figures for the force itself.
	It's been a short while since you told your citizens that you were going to talk to them about their security, and by all accounts, they've turned out in force to watch your address over the arcology's internal communications system. You wake up early, relieve your frustrations on a few slaves woken out of deep sleep, and take position behind your desk. You also call over a slave and push _himU under your desk. The unspoken instruction is clear, and _heU begins enthusiastically
	<<if $PC.dick > 0>>
			sucking your cock, taking it as deep as _heU can without gagging.
	<<else>>
			eating you out, pressing _hisU face into your pussy and forcing _hisU tongue deep inside you.
	<</if>>

A blinking light tells you that the channel is open. You take a deep breath, and begin. You greet your citizens and explain that while you believe deeply in the underlying principles of the Free Cities, recent events have forced you to modify some of your views. The old world attack from the outside and the more recent assault by the Daughters of Liberty from within has proven that some form of permanent, organized standing force is needed to ensure the personal safety of the citizen body.

You tell them that the old world continues to deteriorate. You tell them that it is only a matter of time before the poor, diseased, starving, and unwashed masses try their hand at invading the arcology again. You tell them that such a force would be good for business, securing trade routes and conducting slaving raids far greater in scale than those performed by private slaving corporations. And finally, to quell their greatest fear, you tell them that you would personally support the force financially.

As you speak, you carefully monitor the citizens' opinions as indicated on their communication devices. It is uniformly positive — they know whom they have to thank for their continued survival and dominance. You also monitor your arousal given the ministrations of your slave. A few small movements on your part communicate to your citizens what is happening without being too obvious. Free Cities business etiquette respects business conducted while being subtly serviced (and your doing so during such a public and important broadcast signals how seriously you are taking it), but a climax would be seen as a serious lack of discipline.

You finally wrap up your speech, declaring yourself Marshal of the newly-formed <<textbox "$SF.Lower" $SF.Lower "Security Force Naming-Colonel">>

You close the link to the communication system and read a message from your assistant that appeared during the last moments of your address. In consultation with major figures in the mercenary community, a suitable candidate for day-to-day command of the new unit has been found. Your instructions were to keep you in the dark about them so as to avoid prejudgment. They are waiting outside your office.

[[Invite them inside|Security Force Naming-Colonel][$SF.IntroProgress = 1]]
<<elseif $SF.IntroProgress < 2>> <<= SFNameCapsCheck()>>
	The figure that enters is not what you were expecting, given your previous experiences with the mercenary groups that work with the arcology owners of the Free Cities. Most mercenaries you've worked with have been grizzled stout men, veterans of the old world militaries that finally had too much and went private. Instead, a woman walks in.
	<<if $SF.Colonel.Core === "">>
		She strikes you as someone who is likely to be:

[[Kind|Security Force Naming-Colonel][$SF.Colonel.Core = "kind"]]

[[Cruel and psychopathic|Security Force Naming-Colonel][$SF.Colonel.Core = "cruel"]]

[[A brazen warmonger|Security Force Naming-Colonel][$SF.Colonel.Core = "brazen"]]

[[Jaded|Security Force Naming-Colonel][$SF.Colonel.Core = "jaded"]]

[[Shell-shocked|Security Force Naming-Colonel][$SF.Colonel.Core = "shell shocked"]]
	<<else>>
		She is likely to be ''$SF.Colonel.Core''.

She strides in, stopping in front of your desk,
		<<switch $SF.Colonel.Core>>
			<<case "kind">> pulling off a laid-back salute with an easy grin.
			<<case "cruel">>
				her eyes flashing a hard glare in an instant before quickly softening into those of someone who wants something you have.
			<<case "brazen">>
				snapping off a textbook salute that decades of hard service grills into a veteran.
			<<default>>
				not bothering to put on even the semi-military air (complete with salute) that most mercenaries tend to adopt when meeting new clients.
		<</switch>>
		She is very tall and wearing the pants, boots, gloves, and the tank top undershirt of a standard female combat uniform. Her bare arms and upper body are corded with muscle, and through the tank top's thin fabric you can see both the shape of her muscled abdomen and the curves of her small but perky breasts, complete with what your experience tells you are barbell nipple piercings. Her eyes are alive with intelligence, and you can see her scanning your office, clearly impressed by its opulence. Her hair is shaved close to the scalp, and her ears and nose are heavily pierced. You can make out three long, ugly scars running over top of the mottled tissue of a previous, severe burn along one side of her face, as well as numerous smaller scars and burns on her bare arms. She's been disarmed prior to meeting you; the pistol holster on her hip lies empty, as do at least three knife holsters about her person.

Returning your gaze to her face, she crosses her arms underneath her chest, pressing her breasts up and forward. You have her measure. Given the generally patriarchal nature of both the mercenary community, and the same nature combined with the heavily sexualized lifestyle of the Free Cities, she's decided to embrace her position rather than fight it.

"So," she begins, "you're the boss." You invite her to sit down. "No thanks, boss. Besides," She
		<<switch $SF.Colonel.Core>>
			<<case "kind">> playfully
			<<case "shell shocked">> uncomfortably
		<</switch>>
		indicates the slave under your desk, "you look a little occupied." She nods at the camera across from you. "Saw the speech. Very nice. I'd heard you crazy bastards do business while getting
		<<if $PC.dick > 0>> sucked off, <<else>> eaten out, <</if>>
		but I've never seen anyone actually do it. Hell, most of you people don't want to have to have too much to do with a merc like me. I usually get my instructions remotely."
		<<switch $SF.Colonel.Core>>
			<<case "jaded" "brazen">>
				A short, harsh laugh escapes her. "But I guess it keeps you focused. Can't have the entire arcology seeing you cum."
			<<case "kind">>
				She grins. "That kind of thing doesn't really bother me though."
			<<case "cruel">>
				She frowns. "The client always seems to be happier that way."
		<</switch>>

She moves a step closer. "Your computer-helper-thing told me you wanted me to be a surprise, so I guess I'll tell you why you want me to run the $SF.Lower for you. I'm a killer, pure and simple,<<if $SF.Colonel.Core === "cruel">>" she smiles, "<</if>>and you need that. I looked into those attacks you've suffered. Nasty business. I'll make sure that an attack like that never happens again. I was a soldier out there, in charge of about a thousand men when the Free Cities first started going up, and I knew they were the future. Eventually I deserted, found the first refugee convoy I could, killed the moron protecting it, sold the girls off to slavers, and bought enough gear to start killing for people like you. Ran my own merc crew, did well till we tried to take on a bigger one and everyone died."
		<<switch $SF.Colonel.Core>>
			<<case "shell shocked">>
				She looks away, caught in her own memories. It takes a solid minute before she starts again.
			<<case "cruel">>
				Her smile grows.
			<<default>>
				She pauses for a moment.
		<</switch>>
		"Joined with another big outfit, became the number two, then shit went bad and I had to run. Been a solo fighter and slaver ever since. I know my work, and I know I can make this work."

You feel your climax approaching and hold up a finger. The merc pauses while you grab the slave's head
		<<if $PC.dick > 0>>
			then force your cock roughly down her throat while you cum. She swallows as much as she can before pulling
		<<else>>
			 tightly with your thighs, pressing her face tightly against your pussy as you cum. When you release her, she pulls
		<</if>> away, coughing.

		<<if $SF.Colonel.Core === "shell shocked">>
			The merc looks away again, letting the girl settle down before continuing.
		<<else>>
			The merc laughs again. "I could get used to a place like this."
		<</if>>
		She waves her hand around the office. "I bet you want to know why I'd be trustworthy for something like this." You don't correct her. "Thought so." Her demeanor softens, and you can detect a hit of nervousness. "I would say that I've never turned on a client and leave it at that, but this is different. It's getting worse out there. I'm sure you know that." You give her a slight nod. "Four times now I've woken up in the middle of the night and had to kill someone.<<if $SF.Colonel.Core === "shell shocked">>"<<else>> Two of them were the people I'd taken to bed. You can't even trust your drunken fucks anymore.
		<<switch $SF.Colonel.Core>>
			<<case "kind">>
				It's a shame, but that's the world we live in."
			<<case "cruel">>
				Then again, who doesn't like a good hard fuck and stab?"
			<<default>>
				But what else is new?"
		<</switch>><</if>>

		<<if $SF.Colonel.Core === "jaded" || $SF.Colonel.Core === "shell shocked">>
			"All I know how to do at this point is fight, and that's kept me alive this far.
		<<else>>
			"I like fighting, but I want to live somewhere where I can relax from life out there.
		<</if>>
		You give me the job and a place to live, let me hang up the uncertainty of being a merc, and I'll die for you if it comes to that. I promise the people I recruit will feel the same. Besides," she grins, "I could get used to
		<<switch $SF.Colonel.Core>>
			<<case "brazen">>
				crushing any enemy that looks our way."
			<<case "cruel">>
				having my own stable to abuse as I see fit. A terrified little slavegirl locked between my legs, struggling to breathe?"
			<<default>>
				spending my R&R time with a cold beer in one hand, a few lines of coke or a stack of pills in front of me."
		<</switch>>
		A glint runs through her eyes. "Sounds like a good fucking time."

You quickly decide she'll do. You tap a few commands on your desk's console, assigning her personal quarters on the arcology's higher levels and transferring her first stipend to her new account. You also ask her what title she wants.

"Title?" Another short laugh. "I guess I do need one, given that I'm all official and shit now." She thinks for a moment. "I was a major before I went freelance, and I think I'd like a promotion. Colonel sounds good." You make a note of this in her file. "You people don't seal contracts with a fuck do you?" Reassuring her you don't, she laughs again. "Good. I make it a point never to fuck the boss. It's bad for business." She turns around. "Well, I guess I'd better get to it. Your helper-thing assigned me space on the lower levels for the firebase. I brought a few squads of guys I know from the old days to start, but we'll grow fast once I put the word out, I guarantee it."

[[Let her leave|Security Force Naming-Colonel][$SF.IntroProgress = 2]]
	<</if>>
<<elseif $SF.IntroProgress < 3>> <<set $nextLink = "RIE Eligibility Check",$nextButton = "Continue">>
	She turns and leaves, and you chase the slave out after her. A few minutes later, a soft chime announces the arrival of a message. It's from the Colonel.

//Hey boss, just wanted to mention something else. In your speech you said that you were going to be paying for $SF.Lower. In my mind that means it's yours, no matter what anyone else here might think. I do what you tell me to do. I make sure the troops behave as you want them to behave. I've worked for some 'nice guys' in the past, and I can do that job if you want. It's boring, but sustainable, and I'll have the $SF.Lower turning a profit and supporting the arcology in good order. But if you let me <<if $SF.Colonel.Core === "cruel">>off the leash<<else>>do what I do<</if>> and throw any old world complaints in the trash where they belong, I promise you'll have money pouring into your coffers, even accounting for the good amounts me and my boys will pocket along the way. You'll have an empire in short order.
	<<if $mercenaries > 1>>
		Either way, I'll keep my hands off those mercs you've already installed. I figure that you've reasons for having two different death squads under contract.
	<</if>>

Oh, one last thing. I know you've got some kind of grand social experiment going on up there like all the other arco owners, and that's your own deal, but I'd appreciate it if you could keep that stuff out of the new barracks. I'll have a hard time approaching potential recruits and telling them they should come live in a Roman apartment, an Egyptian temple, a goddamn Japanese teahouse, or some of the other crazy shit I've seen in the past. They're hard, nasty people, and trust me, I can tell you from experience that changing that is just not going to happen. Like I said, though, I can hold them back a bit if you like.

Talk to you later, boss.//
<</if>>