:: rebellionGenerator [nobr]

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

<strong>Slaves security analysis:</strong>
<<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>>
<br>
<br>
<strong>Citizens security analysis:</strong>
<<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>>
	<br><br>
	<<include "rebellionEvents">>
<<elseif $tension > 0>>
	/* otherwise tension decays */
	<br><br>
	<strong>Tension</strong>:
	<<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>>
<br>
<<if $tension < _oldTension>>
	<br>This week @@.green;tensions relaxed.@@
<<elseif $tension == _oldTension && $tension != 0>>
	<br>This week @@.yellow;tensions did not change.@@
<<elseif $tension > _oldTension>>
	<br>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)>>
	<br>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>>
src/Mods/SecExp/rebellionGenerator.tw