code:
:: rebellionEvents [nobr]

<<if $slaveRebellionEventFires == 1>>
	<<if $tension <= 33>>
		<<set _event = 1>>
	<<elseif $tension <= 66>>
		<<set _event = 2>>
	<<else>>
		<<set _event = 3>>
	<</if>>
<<elseif $citizenRebellionEventFires == 1>>
	<<if $tension <= 33>>
		<<set _event = 4>>
	<<elseif $tension <= 66>>
		<<set _event = 5>>
	<<else>>
		<<set _event = 6>>
	<</if>>
<</if>>

<<setNonlocalPronouns $seeDicks>>
<<switch _event>>
<<case 1>>
	/* low tension slave rebellion events */
	<<set _rand = random(0,6)>>
	<<if _rand == 0>>
		This week several slaves were found plotting the death of their master. They were quickly dealt with, but their owner's choice of punishment did little to calm tensions in the arcology.
	<<elseif _rand == 1>>
		This week a large group of slaves attempted to escape. Several were recaptured, but others were deemed too dangerous and were shot on sight. The unfortunate circumstances raised the disapproval of many citizens, either because of the waste of good slaves or the brutality with which the operation was carried. With a bit of luck, however, the incident will be soon forgotten.
	<<elseif _rand == 2>>
		This week books of unknown origin and dangerous content were found in the possession of several slaves. They were mostly sociopolitical treaties, making it clear that the intent of the ones responsible was to fan the fire of rebellion. The books were quickly collected and archived, hopefully this affair will not have lasting consequences.
	<<elseif _rand == 3>>
		This week a citizen was caught giving refuge to an escaped slave. He was not able to pay for the value of the stolen goods, so he was processed as the case required and the slave returned to their rightful master. Many questions however remain without answers.
	<<elseif _rand == 4>>
		This week a member of a well known anti-slavery group was caught trying to infiltrate the arcology. During the capture attempt shots were fired and several guards were injured, and in the end the fugitive unfortunately managed to escape. Reports indicate several slaves helped the criminal, some going as far as using themselves as shields against the bullets of the security drones.
	<<elseif _rand == 5>>
		This week a slave was caught attempting to sabotage a machine in one of the factories. _HeU explained _hisU action as "trying to defend _himselfU from a dangerous machine". Reports confirmed that the apparatus is indeed quite deadly, having killed several slaves since it was installed, but the expert way _heU handled the sabotage leaves open the possibility of a deliberate plan or even external help.
	<<else>>
		This week a slave was found dead in one of the sewer tunnels. It seems _heU was stabbed repeatedly with a sharp object. _HeU was fairly famous for _hisU capabilities as a slave trainer; _hisU old master spent not an insignificant amount of money trying to find _himU once he realized _heU was missing. The episode might have been a simple mugging gone wrong, but _hisU activities as a slave breaker might have played a role in _hisU homicide.
	<</if>>
	<<set $tension += random(1,5)>>
<<case 2>>
	/* med tension slave rebellion events */
	<<set _rand = random(0,5)>>
	<<if _rand == 0>>
		This week some strange reports came in: it seems some assemblies of slaves were observed several nights in a row. The slaves were traced and their masters notified, but many suspect there may be something deeper than a few slaves congregating in the night.
	<<elseif _rand == 1>>
		This week an underground railroad was discovered. The rebels did not go down without a fight, but in the end <<if $mercenaries >= 1>>your mercenaries<<else>>your security drones<</if>> managed to destroy the old tunnels they were using to ship out slaves out of the arcology.
	<<elseif _rand == 2>>
		This week a famous citizen was assaulted and brutally murdered by his slaves. The ones responsible were apprehended and dealt with easily enough, but the mere fact something like this could have happened is concerning. Those slaves had to be aware of their certain doom.
	<<elseif _rand == 3>>
		This week a group of slavers entering the arcology was assaulted. Many reported heavy injuries, but fortunately there were no casualties. The attackers were disguised, but the security systems already identified several slaves who were likely part of the group, based on camera feeds.
	<<elseif _rand == 4>>
		This week the waterways were found infected by a virulent pathogen. The cause was later found to be a diseased slave that died while in the maintenance tunnels. It's not clear if the slave was there because of orders given to _himU or if _heU was trying to escape.
	<<else>>
		This week a sleeper cell of a famous anti slavery organization was discovered in the low levels of the arcology. The group, however, was aware of the coming security forces and retreated before they could be dealt with.
	<</if>>
	<<set $tension += random(5,10)>>
<<case 3>>
	/* high tension slave rebellion events */
	<<set _rand = random(0,4)>>
	<<if _rand == 0>>
		This week a group of slaves took control of one of the manufacturing plants and barricaded themselves inside. It took several days of negotiations and skirmishes to finally end this little insurrection. Many of the slaves involved will be executed in the next few days.
	<<elseif _rand == 1>>
		This week a number of shops were burned to the ground by rioting slaves and sympathetic citizens. It took considerable effort for the security forces to take control of the situation. Harsh punishment is required and scheduled for the instigators.
	<<elseif _rand == 2>>
		This week a mass escape attempt was barely stopped before becoming a catastrophe. Many citizens were trampled by the desperate horde of slaves. It will take some time to restore the streets involved to working order.
	<<elseif _rand == 3>>
		This week a number of riots inflamed the arcology. Many slaves took violent actions against citizens and security personnel. The number of victims keeps getting higher as still now the last sparks of revolt are still active.
	<</if>>
	<<set $tension += random(10,15)>>
<<case 4>>
	<<set _rand = random(0,6)>>
	<<if _rand == 0>>
		This week a citizen refused to pay rent, claiming ideological opposition to the arcology's ownership policies. He was quickly dealt with, but his words might not have fallen silent yet.
	<<elseif _rand == 1>>
		This week books of unknown origin and dangerous content were found in the possession of several citizens. They were mostly sociopolitical treaties, making it clear that the intent of the ones responsible was to fan the fire of rebellion. Most of them were bought and archived, but a few are still circling amongst the citizens of the arcology.
	<<elseif _rand == 2>>
		This week a citizen was caught giving refuge to other citizens, who would be liable to be enslaved because of their debts. The situation was quickly resolved, but the misplaced generosity of that citizen might have inflamed a few souls.
	<<elseif _rand == 3>>
		This week a citizen died in one of the factories. His death sparked some outrage, even some talk of protests against the owners of the factory, but things seem to have calmed down for now.
	<<elseif _rand == 4>>
		This week a citizen refused to be evicted from his house. After some negotiations the man was forcibly removed from the property by your security forces. Unfortunately the forced entry caused some damage to the building.
	<<elseif _rand == 5>>
		This week a citizen refused to be enslaved as his contract established. With an impressive display of his rhetoric capabilities he managed to gather a small crowd agreeing with his position. The impromptu assembly was promptly disrupted by the drones.
	<<else>>
		This week a security drone was found disabled and stripped of important electronic components. It seems the act was not dictated by greed, as the most precious parts of the drone were left on the machine, but rather to cover up something that the drone saw.
	<</if>>
	<<set $tension += random(1,5)>>
<<case 5>>
	<<set _rand = random(0,5)>>
	<<if _rand == 0>>
		This week a factory was subject to a strike by a group of citizens protesting against the owner. They were promptly arrested and the factory returned to its rightful proprietor by your security department.
	<<elseif _rand == 1>>
		This week a group of citizens organized a protest against the systemic enslavement of the citizens of the arcology. Their little parade gathered a surprisingly large crowd, but it was nonetheless quickly suppressed by your forces.
	<<elseif _rand == 2>>
		This week the security department registered the formation of several assemblies of citizens, whose purpose seems to be political in nature. For now no further steps were taken, but it's a worrying sign of further political opposition within the arcology.
	<<elseif _rand == 3>>
		This week there was a protest against one of the wealthiest citizen of the arcology. Many criticize his near monopoly. Supporters of the citizen met the protesters on the streets and it was just thanks to the intervention of the security drones that violence was avoided.
	<<elseif _rand == 4>>
		This week several cameras were sabotaged and in many cases damaged beyond repair. A group of anonymous citizens claims to be responsible; their motivation is apparently the excessive surveillance in the arcology and their attack a response to the breach of their privacy.
	<<else>>
		This week several citizens barricaded themselves in a private brothel. It seems their intention is to protest against the use of ex-citizens in the sex trade, claiming that such a position is unfitting for them. The problem was quickly resolved with the intervention of the security department.
	<</if>>
	<<set $tension += random(5,10)>>
<<case 6>>
	<<set _rand = random(0,4)>>
	<<if _rand == 0>>
		This week the arcology was shaken by a number of strikes throughout the manufacturing levels. Many lament the predatory nature of Free Cities society, many other just want to cause damage to their perceived oppressors. It was a significant effort for the security department to stop all protests.
	<<elseif _rand == 1>>
		This week several factories were set aflame by their workers. The security department worked day and night to control the fire and apprehend the criminals behind the act. Many are known dissidents, but there are a fair few new faces within them. This is a worrying sign.
	<<elseif _rand == 2>>
		This week numerous riots exploded all over the arcology. Many citizens took to the streets to protest against the arcology owner and its supporters. The security forces slowly managed to stop the rioters, with no small amount of trouble and only through generous use of violence.
	<<elseif _rand == 3>>
		This week a massive protest of citizens and slaves gathered just outside the penthouse. The crowd was dispersed only after several hours. There were several victims from both sides and no shortage of injured.
	<</if>>
	<<set $tension += random(10,15)>>
<</switch>>

<<set $tension = Math.clamp($tension,0,100)>>


code:
:: rebellionGenerator [nobr]

<<set _slave = 0>>
<<set _citizen = 0>>
<<set _CSratio = $ACitizens / ($ASlaves)>>

Slaves security analysis:
<<if $SecExp.core.authority <= 3000>>
	Your very low authority allows slaves to think too freely.
	<<set _slave += 30>>
<<elseif $SecExp.core.authority <= 6000>>
	Your low authority allows slaves to think too freely.
	<<set _slave += 25>>
<<elseif $SecExp.core.authority <= 9000>>
	Your moderate authority allows slaves to think a bit too freely.
	<<set _slave += 20>>
<<elseif $SecExp.core.authority <= 12000>>
	Your good authority does not allow slaves to think too freely.
	<<set _slave += 15>>
<<elseif $SecExp.core.authority <= 15000>>
	Your high authority does not allow slaves to think too freely.
	<<set _slave += 10>>
<<elseif $SecExp.core.authority <= 18000>>
	Your very high authority does not allow slaves to think too freely.
	<<set _slave += 5>>
<<else>>
	Your absolute authority does not allow slaves to have a single free thought.
	<<set _slave += 1>>
<</if>>
<<if _CSratio <= 0.4>>
	There are a lot more slaves than citizens, making some doubt their masters are strong enough to stop them.
	<<set _slave += 30>>
<<elseif _CSratio <= 0.6>>
	There are a lot more slaves than citizens, making some doubt their masters are strong enough to stop them.
	<<set _slave += 25>>
<<elseif _CSratio <= 0.8>>
	There are more slaves than citizens, making some doubt their masters are strong enough to stop them.
	<<set _slave += 20>>
<<elseif _CSratio <= 1>>
	There are more slaves than citizens, making some doubt their masters are strong enough to stop them.
	<<set _slave += 15>>
<<elseif _CSratio <= 1.5>>
	There are fewer slaves than citizens, making some doubt they would be strong enough to defeat their masters.
	<<set _slave += 10>>
<<elseif _CSratio >= 3>>
	There are fewer slaves than citizens, making some doubt they would be strong enough to defeat their masters.
	<<set _slave -= 5>>
<<else>>
	Citizen and slave populations are sufficiently balanced not to cause problems either way.
	<<set _slave -= 1>>
<</if>>
<<if $SecExp.security.cap <= 10>>
	The very low security of the arcology leaves free space for slaves to organize and agitate.
	<<set _slave += 30>>
<<elseif $SecExp.security.cap <= 30>>
	The low security of the arcology leaves free space for slaves to organize and agitate.
	<<set _slave += 20>>
<<elseif $SecExp.security.cap <= 60>>
	The moderate security of the arcology does not allow free space for slaves to organize and agitate.
	<<set _slave += 10>>
<<elseif $SecExp.security.cap >= 90>>
	The high security of the arcology does not allow free space for slaves to organize and agitate.
	<<set _slave -= 5>>
<<else>>
	The high security of the arcology does not allow free space for slaves to organize and agitate.
	<<set _slave -= 1>>
<</if>>
<<if $arcologies[0].FSDegradationist != "unset">>
	Many slaves are so disgusted by your degradationist society, that they are willing to rise up against their masters to escape.
	<<set _slave += 30>>
<<elseif $arcologies[0].FSPaternalist != "unset">>
	Many slaves are content to live in your paternalist society.
	<<set _slave -= 5>>
<<else>>
	<<set _slave += 5>>
<</if>>
<<if $arcologies[0].FSRestart != "unset">>
	Many slaves are worried by your eugenics projects and some are pushed towards radicalization.
	<<set _slave += 30>>
<<elseif $arcologies[0].FSRepopulationFocus != "unset">>
	Many slaves are pleasantly happy of your repopulation effort, affording them the freedom to reproduce.
	<<set _slave -= 5>>
<<else>>
	<<set _slave += 5>>
<</if>>

Citizens security analysis:
<<if $SecExp.core.authority <= 3000>>
	Your very low authority allows your citizens to think too freely.
	<<set _citizen += 30>>
<<elseif $SecExp.core.authority <= 6000>>
	Your very low authority allows your citizens to think too freely.
	<<set _citizen += 25>>
<<elseif $SecExp.core.authority <= 9000>>
	Your moderate authority allows your citizens to think a bit too freely.
	<<set _citizen += 20>>
<<elseif $SecExp.core.authority <= 12000>>
	Your good authority does not allow your citizens to think too freely.
	<<set _citizen += 15>>
<<elseif $SecExp.core.authority <= 15000>>
	Your high authority does not allow your citizens to think too freely.
	<<set _citizen += 10>>
<<elseif $SecExp.core.authority <= 18000>>
	Your very high authority does not allow your citizens to think too freely.
	<<set _citizen += 5>>
<<else>>
	Your absolute authority does not allow your citizens to have a single free thought.
	<<set _citizen += 1>>
<</if>>
<<if $SecExp.core.crimeLow >= 90>>
	The very high crime level of the arcology breeds extreme discontent between your citizens.
	<<set _citizen += 30>>
<<elseif $SecExp.core.crimeLow >= 60>>
	The high crime level of the arcology breeds high discontent between your citizens.
	<<set _citizen += 15>>
<<elseif $SecExp.core.crimeLow >= 30>>
	The low crime level of the arcology leaves your citizens happy and satisfied.
	<<set _citizen += 5>>
<<else>>
	The very low crime level of the arcology leaves your citizens happy and satisfied.
	<<set _citizen -= 5>>
<</if>>
<<if $militiaFounded == 1>>
	<<if ($arcologies[0].FSRomanRevivalist == "unset" && $arcologies[0].FSAztecRevivalist == "unset" && $arcologies[0].FSEgyptianRevivalist == "unset" && $arcologies[0].FSEdoRevivalist == "unset" && $arcologies[0].FSArabianRevivalist == "unset" && $arcologies[0].FSChineseRevivalist == "unset")>>
		<<if $militarizedSociety == 1>>
			Many of your citizens are offended by your extreme militarization of the arcology's society.
			<<set _citizen += 20>>
		<<elseif $militaryService == 1>>
			Many of your citizens are offended by your militarization of the arcology's society.
			<<set _citizen += 15>>
		<<else>>
			<<set _citizen += 10>>
		<</if>>
	<<else>>
		<<if $militarizedSociety == 1>>
			Some of your citizens are offended by your extreme militarization of the arcology's society.
			<<set _citizen += 10>>
		<<elseif $militaryService == 1>>
			Some of your citizens are offended by your militarization of the arcology's society.
			<<set _citizen += 5>>
		<<else>>
			<<set _citizen -= 5>>
		<</if>>
	<</if>>
<</if>>
<<if $arcologies[0].FSNull != "unset">>
	Many of your more conservative citizens do not enjoy the cultural freedom you afford the residents of the arcology.
	<<set _citizen += either(20,30)>>
<</if>>
<<if $arcologies[0].FSRestart != "unset">>
	<<if _CSratio > 2>>
		Your citizens are not happy with the noticeable lack of slaves compared to their numbers.
		<<set _citizen += 20>>
	<<elseif _CSratio > 1>>
		Your citizens are not happy with the lack of slaves compared to their numbers.
		<<set _citizen += 15>>
	<<elseif _CSratio < 0.5>>
		<<set _citizen -= 5>>
	<</if>>
<<elseif $arcologies[0].FSRepopulationFocus != "unset">>
	<<if _CSratio < 0.5>>
		Your citizens are not happy about being outbred by the slaves of the arcology.
		<<set _citizen += 20>>
	<<elseif _CSratio < 1>>
		Your citizens are not happy about being outbred by the slaves of the arcology.
		<<set _citizen += 15>>
	<<elseif _CSratio > 1.4>>
		<<set _citizen += 5>>
	<</if>>
<</if>>

/* rolls to see if event happens */
<<if _slave < 0>>
	<<set _slave = 0>>
<<elseif _slave >= 95>>
	<<set _slave = 95>>																	/* there's always a min 5% chance nothing happens */
<</if>>
<<if _citizen < 0>>
	<<set _citizen = 0>>
<<elseif _citizen >= 95>>
	<<set _citizen = 95>>
<</if>>
<<set _roll = random(1,_slave + _citizen)>>
<<if $brainImplant == 106>>
	<<set _slave = Math.trunc(_slave * 0.5 * $SecExp.settings.rebellion.speed), _citizen = Math.trunc(_citizen * 0.5 * $SecExp.settings.rebellion.speed)>>
<<else>>
	<<set _slave = Math.trunc(_slave * $SecExp.settings.rebellion.speed), _citizen = Math.trunc(_citizen * $SecExp.settings.rebellion.speed)>>
<</if>>
<<if _roll <= _slave>>
	<<if random(1,100) < _slave>>
		<<set $slaveRebellionEventFires = 1>>
		<<set $citizenRebellionEventFires = 0>>
		<<if $tension != 0>>
			<<set $slaveProgress += Math.trunc(random(1,5) * ($tension / 100) * 10)>>		/* progress scales with tension */
		<<else>>
			<<set $slaveProgress += random(1,5)>>
		<</if>>
	<</if>>
<<else>>
	<<if random(1,100) < _citizen>>
		<<set $slaveRebellionEventFires = 0>>
		<<set $citizenRebellionEventFires = 1>>
		<<if $tension != 0>>
			<<set $citizenProgress += Math.trunc(random(1,5) * ($tension / 100) * 10)>>
		<<else>>
			<<set $citizenProgress += random(1,5)>>
		<</if>>
	<</if>>
<</if>>

/* if there is an advancement selects a random mini event */
<<set _oldTension = $tension>>
<<if $slaveRebellionEventFires == 1 || $citizenRebellionEventFires == 1>>

	<<include "rebellionEvents">>
<<elseif $tension > 0>>
	/* otherwise tension decays */

	Tension:
	<<if $riotUpgrades.freeMedia >= 1>>
		The guaranteed free media access you offer does wonders to lower tensions in the arcology.
		<<set $tension = Math.trunc(Math.clamp($tension - $riotUpgrades.freeMedia / 2,0,100))>>
	<</if>>
	In the absence of noteworthy events, tensions in the arcology are able to relax.
	<<set $tension = Math.trunc(Math.clamp($tension * 0.97,0,100))>>
<</if>>

<<if $tension < _oldTension>>

This week @@.green;tensions relaxed.@@
<<elseif $tension == _oldTension && $tension != 0>>

This week @@.yellow;tensions did not change.@@
<<elseif $tension > _oldTension>>

This week @@.red;tension rose@@ and
	<<if $slaveRebellionEventFires == 1>>
		@@.red;slave malcontent increased.@@
	<<elseif $citizenRebellionEventFires == 1>>
		@@.red;citizen malcontent increased.@@
	<</if>>
<<elseif !Number.isInteger($tension)>>

Error: tension is outside accepted range.
<</if>>

/* resets flags */
<<set $slaveRebellionEventFires = 0>>
<<set $citizenRebellionEventFires = 0>>

/* rolls for rebellions */
<<if $slaveProgress >= 100>>
	<<if random(1,100) <= 80>>												/* 80% of firing a rebellion once progress is at 100 */
		<<set $slaveRebellion = 1>>
		<<set $slaveProgress = 0>>
		<<set $citizenProgress *= 0.2>>
	<<else>>
		<<set $slaveProgress = 100>>
	<</if>>
<<elseif $citizenProgress >= 100>>
	<<if random(1,100) <= 80>>
		<<set $citizenRebellion = 1>>
		<<set $citizenProgress = 0>>
		<<set $slaveProgress *= 0.2>>
	<<else>>
		<<set $citizenProgress = 100>>
	<</if>>
<</if>>

<<if $SecExp.settings.rebellion.force == 1 && $foughtThisWeek == 0>>
	<<if random(1,100) <= 50>>
		<<set $slaveRebellion = 1>>
		<<set $citizenRebellion = 0>>
	<<else>>
		<<set $slaveRebellion = 0>>
		<<set $citizenRebellion = 1>>
	<</if>>
<</if>>

<<set _weekMod = 0.50>>
<<if $week <= 30>>
	<<set _weekMod = 0.75 + (0.01+($week/200))>>
<<elseif $week <= 60>>
	<<set _weekMod = 1 + (0.01+($week/300))>>
<<elseif $week <= 90>>
	<<set _weekMod = 1.25 + (0.01+($week/400))>>
<<elseif $week <= 120>>
	<<set _weekMod = 1.50 + (0.01+($week/500))>>
<<else>>
	<<set _weekMod = 1.75>>
<</if>>

/* resetting ID list */
<<unset $rebellingID>>
<<set $rebellingID = []>>
<<unset $loyalID>>
<<set $loyalID = []>>

/* if a rebellion fires determine amount of rebels and rebelling units */
<<if $slaveRebellion == 1>>
	<<set $engageRule = 0>>
	<<set $lastRebellionWeeks = 0>>
	<<set $leadingTroops = "assistant">>
	<<if $hasRebelledOnce == 0>>
		<<set $hasRebelledOnce = 1>>
	<</if>>
	/* calc how many slaves and citizens participate */
	<<set _authFactor = Math.clamp(1 - ($SecExp.core.authority / 20000),0.4,0.6)>>
	<<set _repFactor = Math.clamp($rep / 20000,0.4,0.6)>>
	<<set _rebelPercent = 0.3 * _authFactor>>
	<<set _irregularPercent = 0.2 * _repFactor>>
	<<set $attackTroops = Math.clamp(Math.trunc($ASlaves * _rebelPercent + random(-100,100)) * _weekMod,50,$ASlaves)>>
	<<set $irregulars = Math.clamp(Math.trunc($ACitizens * _irregularPercent + random(-100,100)) * _weekMod,50,$ACitizens)>>
	/* calc if units rebel */
	<<for _i = 0; _i < $slaveUnits.length; _i++>>
		<<if $slaveUnits[_i].loyalty < 10>>
			<<if random(1,100) <= 70>>
				<<set $rebellingID.push($slaveUnits[_i].ID)>>
			<</if>>
		<<elseif $slaveUnits[_i].loyalty < 33>>
			<<if random(1,100) <= 30>>
				<<set $rebellingID.push($slaveUnits[_i].ID)>>
			<</if>>
		<<elseif $slaveUnits[_i].loyalty < 66>>
			<<if random(1,100) <= 10>>
				<<set $rebellingID.push($slaveUnits[_i].ID)>>
			<</if>>
		<</if>>
		<<if !($rebellingID.includes($slaveUnits[_i].ID))>>
			<<set $loyalID.push($slaveUnits[_i].ID)>>
		<</if>>
	<</for>>
	<<for _i = 0; _i < $militiaUnits.length; _i++>>
		<<if $militiaUnits[_i].loyalty < 10>>
			<<if random(1,100) <= 70>>
				<<set $rebellingID.push($militiaUnits[_i].ID)>>
			<</if>>
		<<elseif $militiaUnits[_i].loyalty < 33>>
			<<if random(1,100) <= 30>>
				<<set $rebellingID.push($militiaUnits[_i].ID)>>
			<</if>>
		<<elseif $militiaUnits[_i].loyalty < 66>>
			<<if random(1,100) <= 10>>
				<<set $rebellingID.push($militiaUnits[_i].ID)>>
			<</if>>
		<</if>>
		<<if !($rebellingID.includes($militiaUnits[_i].ID))>>
			<<set $loyalID.push($militiaUnits[_i].ID)>>
		<</if>>
	<</for>>
	<<for _i = 0; _i < $mercUnits.length; _i++>>
		<<if $mercUnits[_i].loyalty < 10>>
			<<if random(1,100) <= 70>>
				<<set $rebellingID.push($mercUnits[_i].ID)>>
			<</if>>
		<<elseif $mercUnits[_i].loyalty < 33>>
			<<if random(1,100) <= 30>>
				<<set $rebellingID.push($mercUnits[_i].ID)>>
			<</if>>
		<<elseif $mercUnits[_i].loyalty < 66>>
			<<if random(1,100) <= 10>>
				<<set $rebellingID.push($mercUnits[_i].ID)>>
			<</if>>
		<</if>>
		<<if !($rebellingID.includes($mercUnits[_i].ID))>>
			<<set $loyalID.push($mercUnits[_i].ID)>>
		<</if>>
	<</for>>
	<<set $attackEquip = Math.clamp($weaponsLaw + random(-2,1),0,4)>>
<<elseif $citizenRebellion == 1>>
	<<set $engageRule = 0>>
	<<set $lastRebellionWeeks = 0>>
	<<if $hasRebelledOnce == 0>>
		<<set $hasRebelledOnce = 1>>
	<</if>>
	<<set $leadingTroops = "assistant">>
	/* calc how many citizens participate */
	<<set _authFactor = Math.clamp(1 - ($SecExp.core.authority / 20000),0.4,0.6)>>
	<<set _repFactor = Math.clamp($rep / 20000,0.4,0.6)>>
	<<set _rebelPercent = 0.3 * _authFactor>>
	<<set _irregularPercent = 0.2 * _repFactor>>
	<<set $attackTroops = Math.clamp(Math.trunc($ACitizens * _rebelPercent + random(-100,100)) * _weekMod,50,$ACitizens)>>
	<<set $irregulars = Math.clamp(Math.trunc($ACitizens * _irregularPercent + random(-100,100)) * _weekMod,50,$ACitizens)>>
	/* calc if units rebel */
	<<for _i = 0; _i < $militiaUnits.length; _i++>>
		<<if $militiaUnits[_i].loyalty < 10>>
			<<if random(1,100) <= 70>>
				<<set $rebellingID.push($militiaUnits[_i].ID)>>
			<</if>>
		<<elseif $militiaUnits[_i].loyalty < 33>>
			<<if random(1,100) <= 30>>
				<<set $rebellingID.push($militiaUnits[_i].ID)>>
			<</if>>
		<<elseif $militiaUnits[_i].loyalty < 66>>
			<<if random(1,100) <= 10>>
				<<set $rebellingID.push($militiaUnits[_i].ID)>>
			<</if>>
		<</if>>
		<<if !($rebellingID.includes($militiaUnits[_i].ID))>>
			<<set $loyalID.push($militiaUnits[_i].ID)>>
		<</if>>
	<</for>>
	<<for _i = 0; _i < $slaveUnits.length; _i++>>
		<<if $slaveUnits[_i].loyalty < 10>>
			<<if random(1,100) <= 70>>
				<<set $rebellingID.push($slaveUnits[_i].ID)>>
			<</if>>
		<<elseif $slaveUnits[_i].loyalty < 33>>
			<<if random(1,100) <= 30>>
				<<set $rebellingID.push($slaveUnits[_i].ID)>>
			<</if>>
		<<elseif $slaveUnits[_i].loyalty < 66>>
			<<if random(1,100) <= 10>>
				<<set $rebellingID.push($slaveUnits[_i].ID)>>
			<</if>>
		<</if>>
		<<if !($rebellingID.includes($slaveUnits[_i].ID))>>
			<<set $loyalID.push($slaveUnits[_i].ID)>>
		<</if>>
	<</for>>
	<<for _i = 0; _i < $mercUnits.length; _i++>>
		<<if $mercUnits[_i].loyalty < 10>>
			<<if random(1,100) <= 70>>
				<<set $rebellingID.push($mercUnits[_i].ID)>>
			<</if>>
		<<elseif $mercUnits[_i].loyalty < 33>>
			<<if random(1,100) <= 30>>
				<<set $rebellingID.push($mercUnits[_i].ID)>>
			<</if>>
		<<elseif $mercUnits[_i].loyalty < 66>>
			<<if random(1,100) <= 10>>
				<<set $rebellingID.push($mercUnits[_i].ID)>>
			<</if>>
		<</if>>
		<<if !($rebellingID.includes($mercUnits[_i].ID))>>
			<<set $loyalID.push($mercUnits[_i].ID)>>
		<</if>>
	<</for>>
	<<set $attackEquip = Math.clamp($weaponsLaw + random(-1,1),0,4)>>
<<else>>
	<<set $lastRebellionWeeks++>>
<</if>>

license: mit


code:
:: rebellionHandler [nobr]

<<set $nextButton = " ", $nextLink = "attackReport", $showEncyclopedia = 0>>

<<set _turn = 0>>
<<set _turns = $maxTurns * 2>>
<<set _attack = 0>>
<<set _defense = 0>>
<<set _morale = 0>>
<<set _hp = 0>>
<<set _baseHp = 0>>
<<set _enemyAttack = 0>>
<<set _enemyDefense = 0>>
<<set _enemyMorale = 0>>
<<set _enemyHp = 0>>
<<set _enemyBaseHp = 0>>
<<set _woundChance = 5>>								/* leader has a base chance of 5% to get wounded */
<<set _tacChance = 0.5>>								/* by default tactics have a 50% chance of succeeding */
<<set _expBonus = 0>>
<<set _loyaltyBonus = 0>>
<<set _irregularMod = 0>>
<<set _armyMod = 0>>

/* calculates PC army stats */
<<if $engageRule == 0>>
	<<set _engageMod = 0.5>>
<<elseif $engageRule == 1>>
	<<set _engageMod = 0.75>>
<<elseif $engageRule == 2>>
	<<set _engageMod = 1>>
<<else>>
	<<set _engageMod = 1.4>>
<</if>>

<<if $week <= 30>>
	<<set _irregularMod = $irregulars / 60>>
<<elseif $week <= 60>>
	<<set _irregularMod = $irregulars / 50>>
<<elseif $week <= 90>>
	<<set _irregularMod = $irregulars / 40>>
<<elseif $week <= 120>>
	<<set _irregularMod = $irregulars / 30>>
<<else>>
	<<set _irregularMod = $irregulars / 20>>
<</if>>
<<if $irregulars > 0>>
	<<set _irregularMod = Math.trunc(_irregularMod)>>
	<<set _attack += (($militiaBaseAttack - $humanUpgrade.attack) + ($militiaBaseAttack - $humanUpgrade.attack) * $attackEquip * $equipMod) * _irregularMod * 0.80>>
	<<set _defense += (($militiaBaseDefense - $humanUpgrade.defense) + ($militiaBaseDefense - $humanUpgrade.defense) * $attackEquip * $equipMod) * _irregularMod * 0.80>>
	<<set _hp += $militiaBaseHp * $irregulars>>
<</if>>

<<if $secBots.active == 1>>
	<<set _attack += ($secBotsBaseAttack + $secBotsBaseAttack * $secBots.equip * $equipMod)>>
	<<set _defense += ($secBotsBaseDefense + $secBotsBaseDefense * $secBots.equip * $equipMod)>>
	<<set _hp += $secBotsBaseHp * $secBots.troops>>
<</if>>

<<for _i = 0; _i < $militiaUnits.length; _i++>>
	<<if $militiaUnits[_i].active == 1 && $loyalID.includes($militiaUnits[_i].ID)>>
		<<if $militiaUnits[_i].training <= 10>>
			<<set _expBonus = 0>>
		<<elseif $militiaUnits[_i].training <= 33>>
			<<set _expBonus = 0.10>>
		<<elseif $militiaUnits[_i].training <= 66>>
			<<set _expBonus = 0.25>>
		<<else>>
			<<set _expBonus = 0.50>>
		<</if>>
		<<if $militiaUnits[_i].loyalty <= 10>>
			<<set _loyaltyBonus = 0>>
		<<elseif $militiaUnits[_i].loyalty <= 33>>
			<<set _loyaltyBonus = 0.10>>
		<<elseif $militiaUnits[_i].loyalty <= 66>>
			<<set _loyaltyBonus = 0.20>>
		<<else>>
			<<set _loyaltyBonus = 0.30>>
		<</if>>
		<<set _attack += ($militiaBaseAttack + $militiaBaseAttack * $militiaUnits[_i].equip * $equipMod + $militiaBaseAttack * _expBonus + $militiaBaseAttack * _loyaltyBonus + $militiaBaseAttack * $militiaUnits[_i].SF * 0.20 + $militiaUnits[_i].cyber)>>
		<<set _defense += ($militiaBaseDefense + $militiaBaseDefense * $militiaUnits[_i].equip * $equipMod + $militiaBaseDefense * _expBonus + $militiaBaseDefense * _loyaltyBonus + $militiaBaseDefense * $militiaUnits[_i].SF * 0.20 + $militiaUnits[_i].cyber)>>
		<<set _hp += ($militiaBaseHp + $militiaUnits[_i].cyber + $militiaBaseHp * $militiaUnits[_i].medics * 0.25) * $militiaUnits[_i].troops>>
	<</if>>
<</for>>
<<for _i = 0; _i < $slaveUnits.length; _i++>>
	<<if $slaveUnits[_i].active == 1 && $loyalID.includes($slaveUnits[_i].ID)>>
		<<if $slaveUnits[_i].training <= 33>>
			<<set _expBonus = 0>>
		<<elseif $slaveUnits[_i].training <= 66>>
			<<set _expBonus = 0.25>>
		<<else>>
			<<set _expBonus = 0.50>>
		<</if>>
		<<if $slaveUnits[_i].loyalty <= 10>>
			<<set _loyaltyBonus = 0>>
		<<elseif $slaveUnits[_i].loyalty <= 33>>
			<<set _loyaltyBonus = 0.10>>
		<<elseif $slaveUnits[_i].loyalty <= 66>>
			<<set _loyaltyBonus = 0.20>>
		<<else>>
			<<set _loyaltyBonus = 0.30>>
		<</if>>
		<<set _attack += ($slaveBaseAttack + $slaveBaseAttack * $slaveUnits[_i].equip * $equipMod + $slaveBaseAttack * _expBonus + $slaveBaseAttack * _loyaltyBonus + $slaveBaseAttack * $slaveUnits[_i].SF * 0.20 + $slaveUnits[_i].cyber)>>
		<<set _defense += ($slaveBaseDefense + $slaveBaseDefense * $slaveUnits[_i].equip * $equipMod + $slaveBaseDefense * _expBonus + $slaveBaseDefense * _loyaltyBonus + $slaveBaseDefense * $slaveUnits[_i].SF * 0.20 + $slaveUnits[_i].cyber)>>
		<<set _hp += ($slaveBaseHp + $slaveUnits[_i].cyber + $slaveBaseHp * $slaveUnits[_i].medics * 0.25) * $slaveUnits[_i].troops>>
	<</if>>
<</for>>
<<for _i = 0; _i < $mercUnits.length; _i++>>
	<<if $mercUnits[_i].active == 1 && $loyalID.includes($mercUnits[_i].ID)>>
		<<if $mercUnits[_i].training <= 33>>
			<<set _expBonus = 0>>
		<<elseif $mercUnits[_i].training <= 66>>
			<<set _expBonus = 0.25>>
		<<else>>
			<<set _expBonus = 0.50>>
		<</if>>
		<<if $mercUnits[_i].loyalty <= 10>>
			<<set _loyaltyBonus = 0>>
		<<elseif $mercUnits[_i].loyalty <= 33>>
			<<set _loyaltyBonus = 0.10>>
		<<elseif $mercUnits[_i].loyalty <= 66>>
			<<set _loyaltyBonus = 0.20>>
		<<else>>
			<<set _loyaltyBonus = 0.30>>
		<</if>>
		<<set _attack += ($mercBaseAttack + $mercBaseAttack * $mercUnits[_i].equip * $equipMod + $mercBaseAttack * _expBonus + $mercBaseAttack * _loyaltyBonus + $mercBaseAttack * $mercUnits[_i].SF * 0.20 + $mercUnits[_i].cyber)>>
		<<set _defense += ($mercBaseDefense + $mercBaseDefense * $mercUnits[_i].equip * $equipMod + $mercBaseDefense * _expBonus + $mercBaseDefense * _loyaltyBonus + $mercBaseDefense * $mercUnits[_i].SF * 0.20 + $mercUnits[_i].cyber)>>
		<<set _hp += ($mercBaseHp + $mercUnits[_i].cyber + $mercBaseHp * $mercUnits[_i].medics * 0.25) * $mercUnits[_i].troops>>
	<</if>>
<</for>>

<<if $SF.Toggle && $SF.Active >= 1>>
	<<set $SFatk = 0>>
	<<set $SFdef = 0>>
	<<set $SFhp = 0>>
	<<run calcSFStatistics()>>
	<<set _attack += $SFatk>>
	<<set _defense += $SFdef>>
	<<set _hp += $SFhp>>
<</if>>

<<set _attack *= _engageMod>>
<<set _defense *= _engageMod>>
<<set _hp *= _engageMod>>

<<if $garrison.assistant == 1>>
	<<set _attack *= 0.95>>
	<<set _defense *= 0.95>>
	<<set _hp *= 0.95>>
<</if>>
<<if $garrison.reactor == 1>>
	<<set _attack *= 0.95>>
	<<set _defense *= 0.95>>
	<<set _hp *= 0.95>>
<</if>>
<<if $garrison.penthouse == 1>>
	<<set _attack *= 0.95>>
	<<set _defense *= 0.95>>
	<<set _hp *= 0.95>>
<</if>>
<<if $garrison.waterway == 1>>
	<<set _attack *= 0.95>>
	<<set _defense *= 0.95>>
	<<set _hp *= 0.95>>
<</if>>

<<set _moraleTroopMod = Math.clamp(App.SecExp.conflict.troopCount() / 100,1,10)>>

/* morale and baseHp calculation */
<<set _morale = ($secBotsMorale * $secBots.active + $militiaBaseMorale * App.SecExp.conflict.deployedUnits('militia') + $slaveBaseMorale * App.SecExp.conflict.deployedUnits('slaves') + $mercBaseMorale * App.SecExp.conflict.deployedUnits('mercs') + $SFBaseMorale * $SF.Active) / ($secBots.active + App.SecExp.conflict.deployedUnits('militia') +App.SecExp.conflict.deployedUnits('slaves') + App.SecExp.conflict.deployedUnits('mercs') + $SF.Active)>>
<<set _morale = _morale + _morale * $SecExp.buildings.barracks.upgrades.luxury * 0.05>>	/* barracks bonus */
<<set _morale *= _moraleTroopMod>>
<<set _baseHp = ($secBotsBaseHp * $secBots.active + $militiaBaseHp * App.SecExp.conflict.deployedUnits('militia') + $slaveBaseHp * App.SecExp.conflict.deployedUnits('slaves') + $mercBaseHp * App.SecExp.conflict.deployedUnits('mercs') + $SFBaseHp * $SF.Active) / ($secBots.active + App.SecExp.conflict.deployedUnits('militia') +App.SecExp.conflict.deployedUnits('slaves') + App.SecExp.conflict.deployedUnits('mercs') + $SF.Active)>>

/* calculates rebelling army stats */
<<if $week <= 30>>
	<<set _armyMod = $attackTroops / 100>>
<<elseif $week <= 60>>
	<<set _armyMod = $attackTroops / 90>>
<<elseif $week <= 90>>
	<<set _armyMod = $attackTroops / 80>>
<<elseif $week <= 120>>
	<<set _armyMod = $attackTroops / 70>>
<<else>>
	<<set _armyMod = $attackTroops / 60>>
<</if>>
<<set _armyMod = Math.trunc(_armyMod)>>

<<if $slaveRebellion == 1>>
	<<set $rebellingSlaves = 1>>
	<<set _enemyAttack += (($slaveBaseAttack - $humanUpgrade.attack) + ($slaveBaseAttack - $humanUpgrade.attack) * $attackEquip * $equipMod) * _armyMod>>
	<<set _enemyDefense += (($slaveBaseDefense - $humanUpgrade.defense) + ($slaveBaseDefense - $humanUpgrade.defense) * $attackEquip * $equipMod) * _armyMod>>
	<<set _enemyHp += $slaveBaseHp * $attackTroops>>
<<else>>
	<<set $rebellingMilitia = 1>>
	<<set _enemyAttack += (($militiaBaseAttack - $humanUpgrade.attack) + ($militiaBaseAttack - $humanUpgrade.attack) * $attackEquip * $equipMod) * _armyMod>>
	<<set _enemyDefense += (($militiaBaseDefense - $humanUpgrade.defense) + ($militiaBaseDefense - $humanUpgrade.defense) * $attackEquip * $equipMod) * _armyMod>>
	<<set _enemyHp += $militiaBaseHp * $attackTroops>>
<</if>>

<<for _i = 0; _i < $militiaUnits.length; _i++>>
	<<if $militiaUnits[_i].active == 1 && $rebellingID.includes($militiaUnits[_i].ID)>>
		<<set $rebellingMilitia = 1>>
		<<set $attackTroops += $militiaUnits[_i].troops>>
		<<if $militiaUnits[_i].training <= 10>>
			<<set _expBonus = 0>>
		<<elseif $militiaUnits[_i].training <= 33>>
			<<set _expBonus = 0.10>>
		<<elseif $militiaUnits[_i].training <= 66>>
			<<set _expBonus = 0.25>>
		<<else>>
			<<set _expBonus = 0.50>>
		<</if>>
		<<set _enemyAttack += ($militiaBaseAttack + $militiaBaseAttack * $militiaUnits[_i].equip * $equipMod + $militiaBaseAttack * _expBonus + $militiaBaseAttack * $militiaUnits[_i].SF * 0.20 + $militiaUnits[_i].cyber)>>
		<<set _enemyDefense += ($militiaBaseDefense + $militiaBaseDefense * $militiaUnits[_i].equip * $equipMod + $militiaBaseDefense * _expBonus + $militiaBaseDefense * $militiaUnits[_i].SF * 0.20 + $militiaUnits[_i].cyber)>>
		<<set _enemyHp += ($militiaBaseHp + $militiaUnits[_i].cyber + $militiaBaseHp * $militiaUnits[_i].medics * $equipMod) * $militiaUnits[_i].troops>>
	<</if>>
<</for>>
<<for _i = 0; _i < $slaveUnits.length; _i++>>
	<<if $slaveUnits[_i].active == 1 && $rebellingID.includes($slaveUnits[_i].ID)>>
		<<set $rebellingSlaves = 1>>
		<<set $attackTroops += $slaveUnits[_i].troops>>
		<<if $slaveUnits[_i].training <= 33>>
			<<set _expBonus = 0>>
		<<elseif $slaveUnits[_i].training <= 66>>
			<<set _expBonus = 0.25>>
		<<else>>
			<<set _expBonus = 0.50>>
		<</if>>
		<<set _enemyAttack += ($slaveBaseAttack + $slaveBaseAttack * $slaveUnits[_i].equip * $equipMod + $slaveBaseAttack * _expBonus + $slaveBaseAttack * $slaveUnits[_i].SF * 0.20 + $slaveUnits[_i].cyber)>>
		<<set _enemyDefense += ($slaveBaseDefense + $slaveBaseDefense * $slaveUnits[_i].equip * $equipMod + $slaveBaseDefense * _expBonus + $slaveBaseDefense * $slaveUnits[_i].SF * 0.20 + $slaveUnits[_i].cyber)>>
		<<set _enemyHp += ($slaveBaseHp + $slaveUnits[_i].cyber + $slaveBaseHp * $slaveUnits[_i].medics * 0.25) * $slaveUnits[_i].troops>>
	<</if>>
<</for>>
<<for _i = 0; _i < $mercUnits.length; _i++>>
	<<if $mercUnits[_i].active == 1 && $rebellingID.includes($mercUnits[_i].ID)>>
		<<set $rebellingMercs = 1>>
		<<set $attackTroops += $mercUnits[_i].troops>>
		<<if $mercUnits[_i].training <= 33>>
			<<set _expBonus = 0>>
		<<elseif $mercUnits[_i].training <= 66>>
			<<set _expBonus = 0.25>>
		<<else>>
			<<set _expBonus = 0.50>>
		<</if>>
		<<set _enemyAttack += ($mercBaseAttack + $mercBaseAttack * $mercUnits[_i].equip * $equipMod + $mercBaseAttack * _expBonus + $mercBaseAttack * $mercUnits[_i].SF * 0.20 + $mercUnits[_i].cyber)>>
		<<set _enemyDefense += ($mercBaseDefense + $mercBaseDefense * $mercUnits[_i].equip * $equipMod + $mercBaseDefense * _expBonus + $mercBaseDefense * $mercUnits[_i].SF * 0.20 + $mercUnits[_i].cyber)>>
		<<set _enemyHp += ($mercBaseHp + $mercUnits[_i].cyber + $mercBaseHp * $mercUnits[_i].medics * 0.25) * $mercUnits[_i].troops>>
	<</if>>
<</for>>

<<set _enemyMoraleTroopMod = Math.clamp($attackTroops / 100,1,10)>>

<<set _enemyMorale = 1.5 * ($militiaBaseMorale * $rebellingMilitia + $slaveBaseMorale * $rebellingSlaves + $mercBaseMorale * $rebellingMercs) / ($rebellingMilitia + $rebellingSlaves + $rebellingMercs)>>
<<set _enemyMorale *= _enemyMoraleTroopMod>>
<<set _enemyBaseHp = ($militiaBaseHp * $rebellingMilitia + $slaveBaseHp * $rebellingSlaves + $mercBaseHp * $rebellingMercs) / ($rebellingMilitia +$rebellingSlaves + $rebellingMercs)>>

<<if isNaN(_attack)>>

@@.red;Error: attack value reported NaN@@
<</if>>
<<if isNaN(_defense)>>

@@.red;Error: defense value reported NaN@@
<</if>>
<<if isNaN(_hp)>>

@@.red;Error: hp value reported NaN@@
<</if>>
<<if isNaN(_morale)>>

@@.red;Error: morale value reported NaN@@
<</if>>
<<if isNaN(_enemyAttack)>>

@@.red;Error: enemy attack value reported NaN@@
<</if>>
<<if isNaN(_enemyDefense)>>

@@.red;Error: enemy defense value reported NaN@@
<</if>>
<<if isNaN(_enemyHp)>>

@@.red;Error: enemy hp value reported NaN@@
<</if>>
<<if isNaN(_enemyMorale)>>

@@.red;Error: enemy morale value reported NaN@@
<</if>>

/* difficulty */
<<set _enemyAttack *= $SecExp.settings.difficulty>>
<<set _enemyDefense *= $SecExp.settings.difficulty>>
<<set _enemyMorale *= $SecExp.settings.difficulty>>
<<set _enemyHp *= $SecExp.settings.difficulty>>
<<set _enemyBaseHp *= $SecExp.settings.difficulty>>

<<if $SecExp.settings.showStats == 1>>
<<set _engageMod -= 1>>
<<set _engageMod = Math.round(_engageMod * 100)>>
<<set _difficulty = ($SecExp.settings.difficulty -1) * 100>>

__Difficulty__:

<<if $SecExp.settings.difficulty == 0.5>>
	Very easy
<<elseif $SecExp.settings.difficulty == 0.75>>
	Easy
<<elseif $SecExp.settings.difficulty == 1>>
	Normal
<<elseif $SecExp.settings.difficulty == 1.25>>
	Hard
<<elseif $SecExp.settings.difficulty == 1.5>>
	Very hard
<<else>>
	Extremly hard
<</if>>

__Army__:

troops: <<print num(Math.round(App.SecExp.conflict.troopCount()))>>

attack: <<print num(Math.round(_attack))>>

defense: <<print num(Math.round(_defense))>>

engagement rule modifier: <<if _engageMod > 0>>+<</if>><<print _engageMod>>%

Hp: <<print num(Math.round(_hp))>>

base HP: <<print num(Math.round(_baseHp))>>

morale: <<print num(Math.round(_morale))>>
<<if _enemyMoraleTroopMod > 0>>

morale increase due to troop numbers: +<<print _moraleTroopMod>>%
<</if>>

__Rebels__:

enemy troops: <<print num(Math.round($attackTroops))>>

enemy attack: <<print num(Math.round(_enemyAttack))>>

enemy defense: <<print num(Math.round(_enemyDefense))>>

enemy Hp: <<print num(Math.round(_enemyHp))>>

enemy base Hp: <<print num(Math.round(_enemyBaseHp))>>

enemy morale: <<print num(Math.round(_enemyMorale))>>
<<if _enemyMoraleTroopMod > 0>>

enemy morale increase due to troop numbers: +<<print _enemyMoraleTroopMod>>%
<</if>>

Difficulty modifier: <<if _difficulty > 0>>+<</if>><<print _difficulty>>%
<</if>>

/* simulates the combat by pitting attk against def */
<<for _i = 0; _i < _turns; _i++>>

	<<if $SecExp.settings.showStats == 1>> turn: <<print _i + 1>><</if>>
	/* player army attacks */
	<<set _damage = Math.clamp(_attack - _enemyDefense,_attack * 0.1,_attack)>>

	<<if $SecExp.settings.showStats == 1>> player damage: <<print num(Math.round(_damage))>><</if>>
	<<set _enemyHp -= _damage>>

	<<if $SecExp.settings.showStats == 1>> remaining enemy Hp: <<print num(Math.round(_enemyHp))>><</if>>
	<<set $enemyLosses += _damage / _enemyBaseHp>>
	<<set _moraleDamage = Math.clamp(_damage/ 2 + _damage / _enemyBaseHp,0,_damage*1.5)>>
	<<set _enemyMorale -= _moraleDamage>>

	<<if $SecExp.settings.showStats == 1>> remaining enemy morale: <<print num(Math.round(_enemyMorale))>><</if>>
	<<if _enemyHp <= 0 || _enemyMorale <= 0>>
		<<if $SecExp.settings.showStats == 1>>
Victory!<</if>>
		<<set $battleResult = 3>>
		<<set $battleTurns = _i>>
		<<break>>
	<</if>>

	/* attacker army attacks */
	<<set _damage = _enemyAttack - _defense>>
	<<if _damage < _enemyAttack * 0.1>>
		<<set _damage = _enemyAttack * 0.1>>
	<</if>>

	<<if $SecExp.settings.showStats == 1>> enemy damage: <<print num(Math.round(_damage))>><</if>>
	<<if $SFGear>> <<set _S = .85>> <<else>> <<set _S = 1>> <</if>>
	<<set _hp -= _damage*_S>>

	<<if $SecExp.settings.showStats == 1>> remaining hp: <<print num(Math.round(_hp))>><</if>>
	<<set $losses += _damage / _baseHp>>
	<<set _moraleDamage = Math.clamp(_damage / 2 + _damage / _baseHp,0,_damage*1.5)>>
	<<set _morale -= _moraleDamage>>

	<<if $SecExp.settings.showStats == 1>> remaining morale: <<print num(Math.round(_morale))>><</if>>
	<<if _hp <= 0 || _morale <= 0>>
		<<if $SecExp.settings.showStats == 1>>
Defeat!<</if>>
		<<set $battleResult = -3>>
		<<set $battleTurns = _i>>
		<<break>>
	<</if>>
<</for>>
<<if $battleResult != 3 && $battleResult != -3>>
	<<if _morale > _enemyMorale>>
		<<if $SecExp.settings.showStats == 1>>
Partial victory!<</if>>
		<<set $battleResult = 2>>
	<<elseif _morale < _enemyMorale>>
		<<if $SecExp.settings.showStats == 1>>
Partial defeat!<</if>>
		<<set $battleResult = -2>>
	<</if>>
<</if>>

<<if $battleResult > 3 || $battleResult < -3>>

@@.red;Error: failed to determine battle result@@
<</if>>

<<if $SecExp.settings.showStats == 1 && $SecExp.settings.rebellion.gameOver == 1 && $battleResult == -3>>

	<<link "Proceed" "Gameover">>
		<<set $gameover = "Rebellion defeat">>
	<</link>>
<<elseif $SecExp.settings.rebellion.gameOver == 1 && $battleResult == -3>>
	<<set $gameover = "Rebellion defeat">>
	<<goto "Gameover">>
<<elseif $SecExp.settings.showStats == 1>>

	<<link "Proceed" "rebellionReport">>
	<</link>>
<<else>>
	<<goto "rebellionReport">>
<</if>>


code:
:: rebellionOptions [nobr]

<<set $nextButton = " ", $nextLink = "rebellionOptions", $showEncyclopedia = 1, $encyclopedia = "Battles">>

<<if $slaveRebellion == 1>>
	Slave Rebellion!
<<else>>
	Citizen Rebellion!
<</if>>

<<if $slaveRebellion == 1>>
	In the end it happened, the slaves of your arcology dared took up arms and rose up against their betters. Your penthouse is flooded with reports from all over the arcology of small skirmishes between the rioting slaves and the security forces.
	It appears <<print num(Math.trunc($attackTroops))>> rebels are in the streets right now, building barricades and freeing their peers. They are
	<<if $attackEquip <= 0>>
		poorly armed.
	<<elseif $attackEquip == 1>>
		lightly armed.
	<<elseif $attackEquip == 2>>
		decently armed.
	<<elseif $attackEquip == 3>>
		well armed.
	<<elseif $attackEquip >= 4>>
		extremely well armed.
	<</if>>
	<<if $irregulars > 0>>
		<<print num(Math.trunc($irregulars))>> of your citizens took up arms to defend their arcology owner.
	<</if>>
	<<set _count = 0>>
	<<if $rebellingID.length > 0>>

		<<for _i = 0; _i < $militiaUnits.length; _i++>>
			<<if $militiaUnits[_i].active == 1 && ($rebellingID.includes($militiaUnits[_i].ID))>>
				<<set _count++>>
				<<if _count < $rebellingID.length>>
					$militiaUnits[_i].platoonName,
				<<else>>
					$militiaUnits[_i].platoonName
				<</if>>
			<</if>>
		<</for>>
		<<for _i = 0; _i < $slaveUnits.length; _i++>>
			<<if $slaveUnits[_i].active == 1 && ($rebellingID.includes($slaveUnits[_i].ID))>>
				<<set _count++>>
				<<if _count < $rebellingID.length>>
					$slaveUnits[_i].platoonName,
				<<else>>
					$slaveUnits[_i].platoonName
				<</if>>
			<</if>>
		<</for>>
		<<for _i = 0; _i < $mercUnits.length; _i++>>
			<<if $mercUnits[_i].active == 1 && ($rebellingID.includes($mercUnits[_i].ID))>>
				<<set _count++>>
				<<if _count < $rebellingID.length>>
					$mercUnits[_i].platoonName,
				<<else>>
					$mercUnits[_i].platoonName
				<</if>>
			<</if>>
		<</for>>
		betrayed you and joined the insurrection.
	<</if>>
	<<set _count = 0>>
	<<if $loyalID.length > 0>>

		<<if $arcologyUpgrade.drones == 1>>Your security drones,<</if>>
		<<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>>
	<<if $SF.Toggle && $SF.Active >= 1>>and $SF.Lower, <<print num($SF.Squad.Troops)>> strong<</if>>
		are called to defend the arcology from this menace.
	<<else>>
		<<if $arcologyUpgrade.drones == 1>>Your security drones<<if $SF.Toggle && $SF.Active >= 1>>and $SF.Lower, <<print num($SF.Squad.Troops)>> strong<</if>> <</if>>
		are called to defend the arcology from this menace.
	<</if>>

<<else>>
	In the end it happened, the citizens of your arcology dared took up arms and rose up against their betters. Your penthouse is flooded with reports from all over the arcology of small skirmishes between the rioting residents and the security forces.
	It appears <<print num(Math.trunc($attackTroops))>> rebels are in the streets right now, building barricades and destroying your property. They are
	<<if $attackEquip <= 0>>
		poorly armed.
	<<elseif $attackEquip == 1>>
		lightly armed.
	<<elseif $attackEquip == 2>>
		decently armed.
	<<elseif $attackEquip == 3>>
		well armed.
	<<elseif $attackEquip >= 4>>
		extremely well armed.
	<</if>>
	<<if $irregulars > 0>>
		<<print num(Math.trunc($irregulars))>> of your citizens took up arms to defend their arcology owner.
	<</if>>
	<<set _count = 0>>

	<<if $rebellingID.length > 0>>

		<<for _i = 0; _i < $militiaUnits.length; _i++>>
			<<if $militiaUnits[_i].active == 1 && ($rebellingID.includes($militiaUnits[_i].ID))>>
				<<set _count++>>
				<<if _count < $rebellingID.length>>
					$militiaUnits[_i].platoonName,
				<<else>>
					$militiaUnits[_i].platoonName
				<</if>>
			<</if>>
		<</for>>
		<<for _i = 0; _i < $slaveUnits.length; _i++>>
			<<if $slaveUnits[_i].active == 1 && ($rebellingID.includes($slaveUnits[_i].ID))>>
				<<set _count++>>
				<<if _count < $rebellingID.length>>
					$slaveUnits[_i].platoonName,
				<<else>>
					$slaveUnits[_i].platoonName
				<</if>>
			<</if>>
		<</for>>
		<<for _i = 0; _i < $mercUnits.length; _i++>>
			<<if $mercUnits[_i].active == 1 && ($rebellingID.includes($mercUnits[_i].ID))>>
				<<set _count++>>
				<<if _count < $rebellingID.length>>
					$mercUnits[_i].platoonName,
				<<else>>
					$mercUnits[_i].platoonName
				<</if>>
			<</if>>
		<</for>>
		betrayed you and joined the insurrection.
	<</if>>

	<<set _count = 0>>
	<<if $loyalID.length > 0>>

		<<if $arcologyUpgrade.drones == 1>>Your security drones,<</if>>
		<<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>>
		<<if $SF.Toggle && $SF.Active >= 1>>and $SF.Lower, <<print num($SF.Squad.Troops)>> strong<</if>>
		are called to defend the arcology from this menace.
	<<else>>
		<<if $arcologyUpgrade.drones == 1>>Your security drones<<if $SF.Toggle && $SF.Active >= 1>>and $SF.Lower, <<print num($SF.Squad.Troops)>> strong<</if>><</if>>
		are called to defend the arcology from this menace.
	<</if>>

<</if>>

The confined spaces of the arcology and the number of vital yet delicate systems within its walls do not allow a lot of tactical flexibility. This will be a long and strenuous fight, street after street, barricade after barricade.
In order to preserve the structural integrity of the building and the lives of our civilians, we will have to limit our firepower.

<<link "Only light firearms and nonlethal weapons">>
	<<set $engageRule = 0>>
	<<replace "#engage">>

Your troops will use only nonlethal weapons or light firearms to limit to the maximum the collateral damage. This will however weaken our troops considerably.
	<</replace>>
<</link>>

<<link "No heavy ordnance">>
	<<set $engageRule = 1>>
	<<replace "#engage">>

Your troops will limit the use of explosives and heavy weapons to limit considerably the collateral damage. This will however weaken our troops.
	<</replace>>
<</link>>

<<link "Normal engagement rules">>
	<<set $engageRule = 2>>
	<<replace "#engage">>

Your troops will not limit their arsenal. This will put the structure and your citizens at risk, but our troops will be at full capacity.
	<</replace>>
<</link>>

<<if $advancedRiotEquip == 1>>

<<link "Advanced riot protocol">>
		<<set $engageRule = 3>>
		<<replace "#engage">>

Your troops will make use of the special weaponry, equipment and infrastructure developed by the riot control center to surgically eliminate rebels and dissidents with little to no collateral damage.
		<</replace>>
	<</link>>
<</if>>

Your troops will use only nonlethal weapons or light firearms to limit to the maximum the collateral damage. This will however weaken our troops considerably.

We can dedicate some of our forces to the protection of the vital parts of the arcology, doing so will prevent the failure of said systems, but will also take away strength from our assault.
<<if $garrison.penthouse == 0>>

<<link "Garrison the penthouse" "rebellionOptions">>
		<<set $garrison.penthouse = 1>>
	<</link>>
<<else>>

Troops will be dispatched to the penthouse.
	<<link "Discard the order" "rebellionOptions">>
		<<set $garrison.penthouse = 0>>
	<</link>>
<</if>>
<<if $garrison.reactor == 0>>

<<link "Garrison the reactors" "rebellionOptions">>
		<<set $garrison.reactor = 1>>
	<</link>>
<<else>>

Troops will be dispatched to the reactors.
	<<link "Discard the order" "rebellionOptions">>
		<<set $garrison.reactor = 0>>
	<</link>>
<</if>>
<<if $garrison.assistant == 0>>

<<link "Garrison the assistant's central CPU" "rebellionOptions">>
		<<set $garrison.assistant = 1>>
	<</link>>
<<else>>

Troops will be dispatched to the assistant's central CPU.
	<<link "Discard the order" "rebellionOptions">>
		<<set $garrison.assistant = 0>>
	<</link>>
<</if>>
<<if $garrison.waterway == 0>>

<<link "Garrison the waterways" "rebellionOptions">>
		<<set $garrison.waterway = 1>>
	<</link>>
<<else>>

Troops will be dispatched to the waterways.
	<<link "Discard the order" "rebellionOptions">>
		<<set $garrison.waterway = 0>>
	<</link>>
<</if>>

<<replenishAllUnits>>

<<link "Proceed" "rebellionHandler">>
	<<set $battleResult = 4>>						/* sets $battleResult value outside accepted range to avoid evaluation problems */
	<<if $hasFoughtOnce == 0>>
		<<set $hasFoughtOnce = 1>>
	<</if>>
	<<set $foughtThisWeek = 1>>
<</link>>

<<link "Surrender" "rebellionHandler">>
	<<set $battleResult = -1>>
	<<if $hasFoughtOnce == 0>>
		<<set $hasFoughtOnce = 1>>
	<</if>>
	<<set $foughtThisWeek = 1>>
<</link>>


code:
:: rebellionReport [nobr]

<<set $nextButton = "Continue", $nextLink = "Scheduled Event", $showEncyclopedia = 1, $encyclopedia = "Battles">>
<<set _oldRep = $rep>>
<<set _oldAuth = $SecExp.core.authority>>
<<set $enemyLosses = Math.trunc($enemyLosses)>>
<<if $enemyLosses > $attackTroops>>
	<<set $enemyLosses = $attackTroops>>
<</if>>
<<set $totalKills += $enemyLosses>>
<<set $losses = Math.trunc($losses)>>
<<set $rebellionsCount++>>
<<if $battleResult == 3>>
	Victory!
	<<set $PCrebWon++>>
<<elseif $battleResult == -3>>
	Defeat!
	<<set $PCrebLoss++>>
<<elseif $battleResult == 2>>
	Partial victory!
	<<set $PCrebWon++>>
<<elseif $battleResult == -2>>
	Partial defeat!
	<<set $PCrebLoss++>>
<<elseif $battleResult == -1>>
	We surrendered
	<<set $PCrebLoss++>>
<</if>>

<<if $slaveRebellion == 1>>
	Today, <<= asDateString($week, random(0,7))>>, our arcology was inflamed by the fires of rebellion. <<print num(Math.trunc($attackTroops))>> rebels from all over the structure dared rise up against their owners and conquer their freedom through blood. Our defense force, <<print num(Math.trunc(App.SecExp.conflict.troopCount()))>> strong, fought with them street by street
	<<if $enemyLosses != $attackTroops>>
		inflicting <<print num(Math.trunc($enemyLosses))>> casualties, while sustaining <<if $losses > 1>><<print num(Math.trunc($losses))>> casualties<<elseif $losses > 0>>a casualty<<else>>zero<</if>> themselves.
	<<else>>
		completely annihilating their troops, while sustaining <<if $losses > 1>><<print num(Math.trunc($losses))>> casualties<<elseif $losses > 0>>a casualty<<else>>zero casualties<</if>>.
	<</if>>
	<<if $SFGear>>
		More units were able to survive thanks to wearing $SF.Lower's combat armor suits.
	<</if>>
	<<set $NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * $enemyLosses),
	$menials -= Math.trunc(($menials / $ASlaves) * $enemyLosses),
	$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * $enemyLosses),
	$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * $enemyLosses)>>
	<<if $battleResult == 3>>
		<<if $battleTurns <= 5>>
			The fight was quick and one sided: our men easily stopped the disorganized revolt in a few well aimed assaults.
		<<elseif $battleTurns <= 7>>
			The fight was hard, but in the end our men stopped the disorganized revolt with several well aimed assaults.
		<<else>>
			The fight was long and hard, but in the end our men stopped the revolt before it could accumulate momentum.
		<</if>>
	<<elseif $battleResult == -3>>
		<<if $battleTurns <= 5>>
			The fight was quick and one sided: our men were easily crushed by the furious charge of the rebels.
		<<elseif $battleTurns <= 7>>
			The fight was hard and in the end the rebels proved too much to handle for our men.
		<<else>>
			The fight was long and hard, but despite their bravery the rebels proved too much for our men.
		<</if>>
	<<elseif $battleResult == 2>>
		The fight was long and hard, but in the end our men managed to stop the revolt, though not without difficulty.
	<<elseif $battleResult == -2>>
		The fight was long and hard. In the end, our men had to yield to the rebelling slaves, which were fortunately unable to capitalize on their victory.
	<<elseif $battleResult == -1>>
		You gave your troops the order to surrender; they obediently stand down.
	<</if>>

	/* effects */
	<<if $battleResult == 3>>
		Thanks to your victory, your @@.green;reputation@@ and @@.darkviolet;authority@@ increased.
		<<run repX(random(800,1000), "war")>>
		<<set $SecExp.core.authority += random(800,1000)>>

Many of the rebelling slaves were recaptured and punished. The instigators were executed one after another in a public trial that lasted for almost three days.
		<<set $NPCSlaves -= random(10,30)>>
	<<elseif $battleResult == -3>>
		Due to your defeat, your @@.red;reputation@@ and @@.red;authority@@ decreased.
		<<run repX(random(-800,-1000), "war")>>
		<<set $SecExp.core.authority -= random(800,1000)>>

After the battle most of the rebelling slaves managed to escape, while others remained in the arcology for days looting and hunting their former masters. The arcology will bear the scars of this day for a long time.
		<<set $lowerClass -= random(50,100)>>
		<<set _lostSlaves = Math.trunc(($attackTroops - $enemyLosses) * 0.8),
		$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
		$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
		$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
		$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
		<<set $rebelDefeatAftermath = 5>>
	<<elseif $battleResult == -2>>
		Due to your defeat, your @@.red;reputation@@ and @@.red;authority@@ decreased.
		<<run repX(random(-600,-800), "war")>>
		<<set $SecExp.core.authority -= random(600,800)>>

After the battle most of the rebelling slaves managed to escape, while others remained in the arcology for days looting and hunting their former masters. The arcology will bear the scars of this day for a long time.
		<<set $lowerClass -= random(40,80)>>
		<<set _lostSlaves = Math.trunc(($attackTroops - $enemyLosses) * 0.8),
		$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
		$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
		$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
		$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
		<<set $rebelDefeatAftermath = 3>>
	<<elseif $battleResult == 2>>
		Thanks to your victory, your @@.green;reputation@@ and @@.darkviolet;authority@@ increased.
		<<run repX(random(600,180), "war")>>
		<<set $SecExp.core.authority += random(600,800)>>

Many of the rebelling slaves were recaptured and punished. The instigators were executed one after another in a public trial that lasted for almost three days.
		<<set $NPCSlaves -= random(10,30)>>
	<<elseif $battleResult == -1>>
		Rather than waste the lives of your men you decided to surrender, hoping the rebels will cause less damage if you indulge them, this is however a big hit to your status. Your @@.red;reputation@@ and @@.red;authority@@ are significantly impacted.
		<<run repX(random(-1000,-1200), "war")>>
		<<set $SecExp.core.authority -= random(1000,1200)>>

		The surrender allows the arcology to survive @@.yellow;mostly intact,@@ however reports of @@.red;mass looting and killing of citizens@@ flood your office for a few days.
		<<run cashX(-1000, "war")>>
		<<if $week <= 30>>
			<<set $lowerClass -= random(100)>>
			<<set _lostSlaves = random(150),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(5)>>
		<<elseif $week <= 60>>
			<<set $lowerClass -= random(120)>>
			<<set _lostSlaves = random(170),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(10)>>
		<<elseif $week <= 90>>
			<<set $lowerClass -= random(140)>>
			<<set _lostSlaves = random(190),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(15)>>
		<<elseif $week <= 120>>
			<<set $lowerClass -= random(160)>>
			<<set _lostSlaves = random(210),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(20)>>
		<<else>>
			<<set $lowerClass -= random(180)>>
			<<set _lostSlaves = random(230),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(25)>>
		<</if>>

After the battle most of the rebelling slaves managed to escape, while others remained in the arcology for days looting and hunting their former masters. The arcology will bear the scars of this day for a long time.
		<<set $lowerClass -= random(50,100)>>
		<<set _lostSlaves = Math.trunc(($attackTroops - $enemyLosses) * 0.8),
		$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
		$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
		$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
		$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
		<<set $rebelDefeatAftermath = 5>>
	<</if>>
<<else>>
	Today, <<= asDateString($week, random(0,7))>>, our arcology was inflamed by the fires of rebellion. <<print num(Math.trunc($attackTroops))>> rebels from all over the structure dared rise up to dethrone their arcology owner. Our defense force, <<print num(App.SecExp.conflict.troopCount())>> strong, fought with them street by street
	<<if $enemyLosses != $attackTroops>>
		inflicting <<print num(Math.trunc($enemyLosses))>> casualties, while sustaining <<if $losses > 1>> <<print num(Math.trunc($losses))>> casualties <<else>> a casualty<</if>> themselves.
	<<else>>
		completely annihilating their troops, while sustaining <<if $losses > 1>> <<print num(Math.trunc($losses))>> casualties <<else>> a casualty.<</if>>
	<</if>>
	<<if $SFGear>>
		More units were able to survive thanks to wearing $SF.Lower's combat armor suits.
	<</if>>
	<<set $NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * $enemyLosses),
	$menials -= Math.trunc(($menials / $ASlaves) * $enemyLosses),
	$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * $enemyLosses),
	$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * $enemyLosses)>>
	<<if $battleResult == 3>>
		<<if $battleTurns <= 5>>
			The fight was quick and one sided, our men easily stopped the disorganized revolt in a few well aimed assaults.
		<<elseif $battleTurns <= 7>>
			The fight was hard, but in the end our men stopped the disorganized revolt with several well aimed assaults.
		<<else>>
			The fight was long and hard, but in the end our men stopped the revolt before it could accumulate momentum.
		<</if>>
	<<elseif $battleResult == -3>>
		<<if $battleTurns <= 5>>
			The fight was quick and one sided, our men were easily crushed by the furious charge of the rebels.
		<<elseif $battleTurns <= 7>>
			The fight was hard and in the end the rebels proved too much to handle for our men.
		<<else>>
			The fight was long and hard, but despite their bravery the rebels proved too much for our men.
		<</if>>
	<<elseif $battleResult == 2>>
		The fight was long and hard, but in the end our men managed to stop the revolt, though not without difficulty.
	<<elseif $battleResult == -2>>
		The fight was long and hard. Our men in the end had to yield to the rebelling slaves, which were fortunately unable to fully capitalize on their victory.
	<<elseif $battleResult == -1>>
		You gave your troops the order to surrender, obediently they stand down.
	<</if>>

	/* effects */
	<<if $battleResult == 3>>
		Thanks to your victory, your @@.green;reputation@@ and @@.darkviolet;authority@@ increased.
		<<run repX(random(800,1000), "war")>>
		<<set $SecExp.core.authority += random(800,1000)>>

Many of the rebelling citizens were captured and punished, many others enslaved. The instigators were executed one after another in a public trial that lasted for almost three days.
		<<set $lowerClass -= random(10,30)>>
	<<elseif $battleResult == -3>>
		Due to your defeat, your @@.red;reputation@@ and @@.red;authority@@ decreased.
		<<run repX(random(-800,-1000), "war")>>
		<<set $SecExp.core.authority -= random(800,1000)>>

After the battle most of the rebelling citizens remained in the arcology for days looting and hunting their former arcology. We will bear the scars of this day for a long time.
		<<set $lowerClass -= Math.trunc(($attackTroops - $enemyLosses) * 0.8)>>
		<<set $rebelDefeatAftermath = 5>>
	<<elseif $battleResult == -2>>
		Due to your defeat, your @@.red;reputation@@ and @@.red;authority@@ decreased.
		<<run repX(random(-600,-800), "war")>>
		<<set $SecExp.core.authority -= random(600,800)>>

After the battle most of the rebelling citizens remained in the arcology for days looting and hunting their former arcology. We will bear the scars of this day for a long time.
		<<set $lowerClass -= random(40,80)>>
		<<set _lostSlaves = Math.trunc(($attackTroops - $enemyLosses) * 0.6),
		$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
		$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
		$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
		$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
		<<set $rebelDefeatAftermath = 3>>
	<<elseif $battleResult == 2>>
		Thanks to your victory, your @@.green;reputation@@ and @@.darkviolet;authority@@ increased.
		<<run repX(random(600,180), "war")>>
		<<set $SecExp.core.authority += random(600,800)>>

Many of the rebelling citizens were captured and punished, many others enslaved. The instigators were executed one after another in a public trial that lasted for almost three days.
		<<set $NPCSlaves -= random(10,30)>>
	<<elseif $battleResult == -1>>
		Rather than waste the lives of your men you decided to surrender, hoping the rebels will cause less damage if you indulge them, this is however a big hit to your status. Your @@.red;reputation@@ and @@.red;authority@@ are significantly impacted.
		<<run repX(random(-1000,-1200), "war")>>
		<<set $SecExp.core.authority -= random(1000,1200)>>

		The surrender allows the arcology to survive @@.yellow;mostly intact,@@ however reports of @@.red;mass looting and killing of citizens@@ flood your office for a few days.
		<<run cashX(-1000, "war")>>
		<<if $week <= 30>>
			<<set $lowerClass -= random(100)>>
			<<set _lostSlaves = random(150),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(5)>>
		<<elseif $week <= 60>>
			<<set $lowerClass -= random(120)>>
			<<set _lostSlaves = random(170),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(10)>>
		<<elseif $week <= 90>>
			<<set $lowerClass -= random(140)>>
			<<set _lostSlaves = random(190),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(15)>>
		<<elseif $week <= 120>>
			<<set $lowerClass -= random(160)>>
			<<set _lostSlaves = random(210),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(20)>>
		<<else>>
			<<set $lowerClass -= random(180)>>
			<<set _lostSlaves = random(230),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(25)>>
		<</if>>

After the battle most of the rebelling citizens remained in the arcology for days looting and hunting their former arcology. We will bear the scars of this day for a long time.
		<<set $lowerClass -= random(50,100)>>
		<<set _lostSlaves = Math.trunc(($attackTroops - $enemyLosses) * 0.8),
		$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
		$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
		$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
		$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
		<<set $rebelDefeatAftermath = 5>>
	<</if>>
<</if>>

/* engage rules */
<<if $battleResult != -1>>
	<<if $engageRule == 0>>
		Since you ordered your troops to limit their weaponry to low caliber or nonlethal, the arcology reported only @@.red;minor damage.@@ Most citizens and non involved slaves remained unharmed, though some casualties between the civilians were inevitable.
		A few businesses were looted and burned, but the damage was pretty limited.
		<<set $arcRepairTime += 3, IncreasePCSkills('engineering', 0.1)>>
		<<if $week <= 30>>
			<<set $lowerClass -= random(40)>>
			<<set _lostSlaves = random(65),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(2)>>
		<<elseif $week <= 60>>
			<<set $lowerClass -= random(50)>>
			<<set _lostSlaves = random(75),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(5)>>
		<<elseif $week <= 90>>
			<<set $lowerClass -= random(60)>>
			<<set _lostSlaves = random(85),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(7)>>
		<<elseif $week <= 120>>
			<<set $lowerClass -= random(70)>>
			<<set _lostSlaves = random(95),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(10)>>
		<<else>>
			<<set $lowerClass -= random(80)>>
			<<set _lostSlaves = random(105),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(12)>>
		<</if>>
	<<elseif $engageRule == 1>>
		You ordered your troops to limit their weaponry to non-heavy, non-explosive, because of this the arcology reported @@.red;moderate damage.@@ Most citizens and non involved slaves remained unharmed or only lightly wounded, but many others did not make it. Unfortunately casualties between the civilians were inevitable.
		A few businesses were looted and burned, but the damage was pretty limited.
		<<set $arcRepairTime += 5, IncreasePCSkills('engineering', 0.1)>>
		<<if $week <= 30>>
			<<set $lowerClass -= random(60)>>
			<<set _lostSlaves = random(85),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(4)>>
		<<elseif $week <= 60>>
			<<set $lowerClass -= random(70)>>
			<<set _lostSlaves = random(95),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(7)>>
		<<elseif $week <= 90>>
			<<set $lowerClass -= random(80)>>
			<<set _lostSlaves = random(105),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(9)>>
		<<elseif $week <= 120>>
			<<set $lowerClass -= random(90)>>
			<<set _lostSlaves = random(115),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(12)>>
		<<else>>
			<<set $lowerClass -= random(100)>>
			<<set _lostSlaves = random(125),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(14)>>
		<</if>>
	<<elseif $engageRule == 2>>
		Since you did not apply any restriction on the weapons your forces should use, the arcology reported @@.red;heavy damage.@@ Many citizens and uninvolved slaves are reported killed or missing. Casualties between the civilians were inevitable.
		Many businesses were damaged during the battle either by the fight itself, by fires which spread unchecked for hours or by looters.
		<<set $arcRepairTime += 7, IncreasePCSkills('engineering', 0.1)>>
		<<if $week <= 30>>
			<<set $lowerClass -= random(100)>>
			<<set _lostSlaves = random(150),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(5)>>
		<<elseif $week <= 60>>
			<<set $lowerClass -= random(120)>>
			<<set _lostSlaves = random(170),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(10)>>
		<<elseif $week <= 90>>
			<<set $lowerClass -= random(140)>>
			<<set _lostSlaves = random(190),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(15)>>
		<<elseif $week <= 120>>
			<<set $lowerClass -= random(160)>>
			<<set _lostSlaves = random(210),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(20)>>
		<<else>>
			<<set $lowerClass -= random(180)>>
			<<set _lostSlaves = random(230),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(25)>>
		<</if>>
	<<else>>
		Thanks to the advance riot control weaponry developed by your experts, the rebels were mostly subdued or killed with @@.yellow;little to no collateral damage to the arcology@@ and its inhabitants.
		A few businesses were looted, but the damage was very limited.
		<<set $arcRepairTime += 2, IncreasePCSkills('engineering', 0.1)>>
		<<run cashX(-1000, "war")>>
		<<if $week <= 30>>
			<<set $lowerClass -= random(20)>>
			<<set _lostSlaves = random(45),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(2)>>
		<<elseif $week <= 60>>
			<<set $lowerClass -= random(30)>>
			<<set _lostSlaves = random(55),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(4)>>
		<<elseif $week <= 90>>
			<<set $lowerClass -= random(40)>>
			<<set _lostSlaves = random(65),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(6)>>
		<<elseif $week <= 120>>
			<<set $lowerClass -= random(50)>>
			<<set _lostSlaves = random(75),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(8)>>
		<<else>>
			<<set $lowerClass -= random(60)>>
			<<set _lostSlaves = random(85),
			$NPCSlaves -= Math.trunc(($NPCSlaves / $ASlaves) * _lostSlaves),
			$menials -= Math.trunc(($menials / $ASlaves) * _lostSlaves),
			$fuckdolls -= Math.trunc(($fuckdolls / $ASlaves) * _lostSlaves),
			$menialBioreactors -= Math.trunc(($menialBioreactors / $ASlaves) * _lostSlaves)>>
			<<set $arcologies[0].prosperity -= random(10)>>
		<</if>>
	<</if>>
<</if>>
<<if $lowerClass < 0>>
	<<set $lowerClass = 0>>
<</if>>
<<if $NPCSlaves < 0>>
	<<set $NPCSlaves = 0>>
<</if>>

/* garrisons */
<<if $garrison.reactor == 0>>
	<<if random(1,100) <= (75 - ($fort.reactor * 25))>>
		Unfortunately during the fighting a group of slaves infiltrated the reactor complex and sabotaged it, causing massive power fluctuations and blackouts.
		It will take @@.red;time and money to repair the damage.@@
		<<set $garrison.reactorTime = $repairTime + random(1) - $fort.reactor, IncreasePCSkills('engineering', 0.1)>>
		<<run cashX(-2000, "war")>>
	<<else>>
		While the reactor was left defenseless without a garrison, there was no attempt at sabotage. Let's hope we'll always be this lucky.
	<</if>>
<<else>>
	The garrison assigned to the reactor protected it from the multiple sabotage attempts carried out by the rebels.
<</if>>
<<if $garrison.waterway == 0>>
	<<if random(1,100) <= (75 - ($fort.waterway * 25))>>
		Unfortunately during the fighting a group of slaves infiltrated the water management complex and sabotaged it, causing huge water leaks throughout the arcology and severely limiting the water supply.
		It will take @@.red;time and money to repair the damage.@@
		<<set $garrison.waterwayTime = $repairTime + random(1) - $fort.waterway, IncreasePCSkills('engineering', 0.1)>>
		<<run cashX(-2000, "war")>>
	<<else>>
		While the water management complex was left defenseless without a garrison, there was no attempt at sabotage. Let's hope we'll always be this lucky.
	<</if>>
<<else>>
	The garrison assigned to the water management complex protected it from the sabotage attempt of the rebels.
<</if>>
<<if $garrison.assistant == 0>>
	<<if random(1,100) <= (75 - ($fort.assistant * 25))>>
		Unfortunately during the fighting a group of slaves infiltrated the facility housing $assistantName's mainframe and sabotaged it. Without its AI, the arcology will be next to impossible to manage.
		It will take @@.red;time and money to repair the damage.@@
		<<set $garrison.assistantTime = $repairTime + random(1) - $fort.assistant, IncreasePCSkills('engineering', 0.1)>>
		<<run cashX(-2000, "war")>>
	<<else>>
		While the $assistantName's mainframe was left defenseless without a garrison, there was no attempt at sabotage. Let's hope we'll always be this lucky.
	<</if>>
<<else>>
	The garrison assigned to the facility housing $assistantName's mainframe prevented any sabotage attempt.
<</if>>
<<if $garrison.penthouse == 1 && $Bodyguard != 0>>
	<<setLocalPronouns $Bodyguard 2>>
	The garrison assigned to the penthouse together with your loyal Bodyguard stopped all assaults against your penthouse with ease.
<<elseif $Bodyguard != 0>>
	<<if random(1,100) <= 75>>
		During the fighting a group of slaves assaulted the penthouse. Your Bodyguard, $Bodyguard.slaveName, stood strong against the furious attack.
		<<set _woundChance = 0>>
		<<if $PC.career == "mercenary" || $PC.career == "gang">>
			<<set _woundChance -= 5>>
		<<elseif $PC.skill.warfare >= 75>>
			<<set _woundChance -= 3>>
		<</if>>
		<<if $personalArms >= 1>>
			<<set _woundChance -= 5>>
		<</if>>
		<<if $PC.physicalAge >= 60>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.belly > 5000>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.boobs >= 1000>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.butt >= 4>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.preg >= 30>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.balls >= 20>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.balls >= 9>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if random(1,100) <= _woundChance>>
			A lucky shot managed to find its way to you, leaving a painful, but thankfully not lethal, wound.
			<<run healthDamage($PC, 60)>>
		<<else>>
			Fortunately you managed to avoid injury.
		<</if>>
		<<if $Concubine != 0>>
			<<setLocalPronouns $Concubine>>
			<<set _woundChance = 0>>
			<<if $Concubine.skill.combat == 1>>
				<<set _woundChance -= 2>>
			<</if>>
			<<set _woundChance -= 0.25 * (getLimbCount($Concubine, 105))>>
			<<if $Concubine.health.condition >= 50>>
				<<set _woundChance -= 1>>
			<</if>>
			<<if $Concubine.weight > 130>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.muscles < -30>>
				<<set _woundChance += 1>>
			<</if>>
			<<if getBestVision($Concubine) === 0>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.heels == 1>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.boobs >= 1400>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.butt >= 6>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.belly >= 10000>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.dick >= 8>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.balls >= 8>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.intelligence+$Concubine.intelligenceImplant < -95>>
				<<set _woundChance += 1>>
			<</if>>
			<<set _woundChance *= random(2,4)>>
			<<if random(1,100) <= _woundChance>>
				Your Concubine was unfortunately caught in the crossfire and
				<<set $woundType = random(1,10)>>
				<<if $woundType == 1>>
					a splinter pierced $his throat, severing $his vocal cords.
					<<set $Concubine.voice = 0>>
				<<elseif $woundType == 2>>
					a splinter hit $his face, severely damaging $his eyes.
					<<run eyeSurgery($Concubine, "both", "blind")>>
				<<elseif $woundType == 3>>
					an explosion near $him caused the loss of all $his limbs.
					<<run removeLimbs($Concubine, "all")>>
				<<elseif $woundType >= 4>>
					a stray shot severely wounded $him.
					<<if $Concubine.health.health >= -60>>
						<<run healthDamage($Concubine, 30)>>
					<<else>>
						<<run healthDamage($Concubine, -90 - $Concubine.health.health)>>
					<</if>>
				<</if>>
			<</if>>
		<</if>>
		<<set _woundChance = 0>>
		<<if $Bodyguard.skill.combat == 1>>
			<<set _woundChance -= 2>>
		<</if>>
		<<set _woundChance -= 0.25 * (getLimbCount($Bodyguard, 105))>>
		<<if $Bodyguard.health.condition >= 50>>
			<<set _woundChance -= 1>>
		<</if>>
		<<if $Bodyguard.weight > 130>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.muscles < -30>>
			<<set _woundChance += 1>>
		<</if>>
		<<if getBestVision($Bodyguard) === 0>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.heels == 1>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.boobs >= 1400>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.butt >= 6>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.belly >= 10000>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.dick >= 8>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.balls >= 8>>
			<<set _woundChance += 1>>
		<</if>>
		<<if $Bodyguard.intelligence+$Bodyguard.intelligenceImplant < -95>>
			<<set _woundChance += 1>>
		<</if>>
		<<set _woundChance *= random(2,4)>>
		<<if random(1,100) <= _woundChance>>
			During one of the assaults your Bodyguard was hit.
			<<set $woundType = random(1,10)>>
			<<if $woundType == 1>>
				A splinter pierced _his2 throat, severing _his2 vocal cords.
				<<set $Bodyguard.voice = 0>>
			<<elseif $woundType == 2>>
				A splinter hit _his2 face, severely damaging _his2 eyes.
				<<run eyeSurgery($Bodyguard, "both", "blind")>>
			<<elseif $woundType == 3>>
				An explosion near _him2 caused the loss of all _his2 limbs.
				<<run removeLimbs($Bodyguard, "all")>>
			<<elseif $woundType >= 4>>
				A stray shot severely wounded _him2.
				<<if $Bodyguard.health.health >= -60>>
					<<run healthDamage($Bodyguard, 30)>>
				<<else>>
					<<run healthDamage($Bodyguard, $Bodyguard.health.health + 90)>>
				<</if>>
			<</if>>
		<</if>>
		The damage to the structure will be @@.red;costly to repair.@@
		<<= IncreasePCSkills('engineering', 0.1)>>
		<<run cashX(-2000, "war")>>
	<<else>>
		While the penthouse was left without a sizable garrison, there was no dangerous assault against it. Let's hope we'll always be this lucky.
	<</if>>
<<elseif $garrison.penthouse == 1>>
	<<if random(1,100) <= 75>>
		During the fighting a group of slaves assaulted the penthouse. The garrison stood strong against the furious attack.
		<<set _woundChance = 0>>
		<<if $PC.career == "mercenary" || $PC.career == "gang">>
			<<set _woundChance -= 5>>
		<<elseif $PC.skill.warfare >= 75>>
			<<set _woundChance -= 3>>
		<</if>>
		<<if $personalArms >= 1>>
			<<set _woundChance -= 5>>
		<</if>>
		<<if $PC.physicalAge >= 60>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.belly > 5000>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.boobs >= 1000>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.butt >= 4>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.preg >= 30>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.balls >= 20>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.balls >= 9>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<set _woundChance *= random(1,2)>>
		<<if random(1,100) <= _woundChance>>
			A lucky shot managed to find its way to you, leaving a painful, but thankfully nonlethal, wound.
			<<run healthDamage($PC, 60)>>
		<<else>>
			Fortunately you managed to avoid injury.
		<</if>>
		<<if $Concubine != 0>>
			<<setLocalPronouns $Concubine>>
			<<set _woundChance = 0>>
			<<if $Concubine.skill.combat == 1>>
				<<set _woundChance -= 2>>
			<</if>>
			<<set _woundChance -= 0.25 * (getLimbCount($Concubine, 105))>>
			<<if $Concubine.health.condition >= 50>>
				<<set _woundChance -= 1>>
			<</if>>
			<<if $Concubine.weight > 130>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.muscles < -30>>
				<<set _woundChance += 1>>
			<</if>>
			<<if getBestVision($Concubine) === 0>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.heels == 1>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.boobs >= 1400>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.butt >= 6>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.belly >= 10000>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.dick >= 8>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.balls >= 8>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.intelligence+$Concubine.intelligenceImplant < -95>>
				<<set _woundChance += 1>>
			<</if>>
			<<set _woundChance *= random(2,4)>>
			<<if random(1,100) <= _woundChance>>
				Your Concubine was unfortunately caught in the crossfire and
				<<set $woundType = random(1,10)>>
				<<if $woundType == 1>>
					a splinter pierced $his throat, severing $his vocal cords.
					<<set $Concubine.voice = 0>>
				<<elseif $woundType == 2>>
					a splinter hit $his face, severely damaging $his eyes.
					<<run eyeSurgery($Concubine, "both", "blind")>>
				<<elseif $woundType == 3>>
					an explosion near $him caused the loss of all $his limbs.
					<<run removeLimbs($Concubine, "all")>>
				<<elseif $woundType >= 4>>
					a stray shot severely wounded $him.
					<<if $Concubine.health.health >= -60>>
						<<run healthDamage($Concubine, 30)>>
					<<else>>
						<<run healthDamage($Concubine, $Concubine.health.health + 90)>>
					<</if>>
				<</if>>
			<</if>>
		<</if>>
		The damage to the structure will be @@.red;costly to repair.@@
		<<= IncreasePCSkills('engineering', 0.1)>>
		<<run cashX(-2000, "war")>>
	<<else>>
		There was no sizable assault against the penthouse. Let's hope we'll always be this lucky.
	<</if>>
<<else>>
	<<if random(1,100) <= 75>>
		During the fighting a group of slaves assaulted the penthouse. Isolated and alone, you stood strong against the furious attack.
		<<set _woundChance = 0>>
		<<if $PC.career == "mercenary" || $PC.career == "gang">>
			<<set _woundChance -= 5>>
		<<elseif $PC.skill.warfare >= 75>>
			<<set _woundChance -= 3>>
		<</if>>
		<<if $personalArms >= 1>>
			<<set _woundChance -= 5>>
		<</if>>
		<<if $PC.physicalAge >= 60>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.belly > 5000>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.boobs >= 1000>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.butt >= 4>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.preg >= 30>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.balls >= 20>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<if $PC.balls >= 9>>
			<<set _woundChance += random(1,5)>>
		<</if>>
		<<set _woundChance *= random(1,2)>>
		<<if random(1,100) <= _woundChance>>
			A lucky shot managed to find its way to you, leaving a painful, but thankfully nonlethal, wound.
			<<run healthDamage($PC, 60)>>
		<<else>>
			Fortunately you managed to avoid injury.
		<</if>>
		<<if $Concubine != 0>>
			<<setLocalPronouns $Concubine>>
			<<set _woundChance = 0>>
			<<if $Concubine.skill.combat == 1>>
				<<set _woundChance -= 2>>
			<</if>>
			<<set _woundChance -= 0.25 * (getLimbCount($Concubine, 105))>>
			<<if $Concubine.health.condition >= 50>>
				<<set _woundChance -= 1>>
			<</if>>
			<<if $Concubine.weight > 130>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.muscles < -30>>
				<<set _woundChance += 1>>
			<</if>>
			<<if getBestVision($Concubine) === 0>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.heels == 1>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.boobs >= 1400>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.butt >= 6>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.belly >= 10000>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.dick >= 8>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.balls >= 8>>
				<<set _woundChance += 1>>
			<</if>>
			<<if $Concubine.intelligence+$Concubine.intelligenceImplant < -95>>
				<<set _woundChance += 1>>
			<</if>>
			<<set _woundChance *= random(2,4)>>
			<<if random(1,100) <= _woundChance>>
				Your Concubine was unfortunately caught in the crossfire and
				<<set $woundType = random(1,10)>>
				<<if $woundType == 1>>
					a splinter pierced $his throat, severing $his vocal cords.
					<<set $Concubine.voice = 0>>
				<<elseif $woundType == 2>>
					a splinter hit $his face, severely damaging $his eyes.
					<<run eyeSurgery($Concubine, "both", "blind")>>
				<<elseif $woundType == 3>>
					an explosion near $him caused the loss of all $his limbs.
					<<run removeLimbs($Concubine, "all")>>
				<<elseif $woundType >= 4>>
					a stray shot severely wounded $him.
					<<if $Concubine.health.health >= -60>>
						<<run healthDamage($Concubine, 30)>>
					<<else>>
						<<run healthDamage($Concubine, -90 - $Concubine.health.health)>>
					<</if>>
				<</if>>
			<</if>>
		<</if>>
	<<else>>
		There was no sizable assault against the penthouse. Let's hope we'll always be this lucky.
	<</if>>
<</if>>

<<include "unitsRebellionReport">>

/* resets variables and flags */
<<recalcManpower>>
<<set $attackTroops = 0>>
<<set $attackEquip = 0>>
<<set $enemyLosses = 0>>
<<set $losses = 0>>
<<set $battleTurns = 0>>
<<set $irregulars = 0>>
<<if $slaveRebellion == 1>>
	<<set $slaveProgress = 0>>
	<<set $citizenProgress = Math.clamp($citizenProgress - random(50,100), 0, 100)>>
<<else>>
	<<set $citizenProgress = 0>>
	<<set $slaveProgress = Math.clamp($slaveProgress - random(50,100), 0, 100)>>
<</if>>
<<set $slaveRebellion = 0>>
<<set $citizenRebellion = 0>>
<<set $tension = Math.clamp($tension - random(50,100), 0, 100)>>