:: rebellionGenerator [nobr]

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

/* slaves */
<<if $authority <= 3000>>
	Your very low authority allows slaves to think too freely.<<set _slave += 30>>
<<elseif $authority <= 6000>>
	Your low authority allows slaves to think too freely.<<set _slave += 25>>
<<elseif $authority <= 9000>>
	Your moderate authority allows slaves to think a bit too freely.<<set _slave += 20>>
<<elseif $authority <= 12000>>
	Your good authority does not allow slaves to think too freely.<<set _slave += 15>>
<<elseif $authority <= 15000>>
	Your high authority does not allow slaves to think too freely.<<set _slave += 10>>
<<elseif $authority <= 18000>>
	Your very high authority does not allow slaves to think too freely.<<set _slave += 5>>
<</if>>
<<if _CSratio <= 0.2>>
	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.4>>
	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.6>>
	There are more slaves than citizens, making some doubt their masters are strong enough to stop them.<<set _slave += 20>>
<<elseif _CSratio <= 0.8>>
	There are more slaves than citizens, making some doubt their masters are strong enough to stop them.<<set _slave += 15>>
<<elseif _CSratio <= 1>>
	There are less slaves than citizens, making some doubt they would be strong enough to defeat their masters.<<set _slave += 10>>
<<elseif _CSratio >= 1.2>>
	There are less slaves than citizens, making some doubt they would be strong enough to defeat their masters.<<set _slave -= 5>>
<</if>>
<<if $security <= 10>>
	The very low security of the arcology leaves free space for slaves to organize and agitate.<<set _slave += 30>>
<<elseif $security <= 30>>
	The low security of the arcology leaves free space for slaves to organize and agitate.<<set _slave += 20>>
<<elseif $security <= 60>>
	The moderate security of the arcology does not allow free space for slaves to organize and agitate.<<set _slave += 10>>
<<elseif $security >= 90>>
	The high security of the arcology does not allow free space for slaves to organize and agitate.<<set _slave -= 5>>
<</if>>
<<if $arcologies[0].FSDegradationist != "unset">>
	Many slaves are so disgusted by your degradationist society, that 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>
/* citizens */
<<if $authority <= 3000>>
	Your very low authority allows your citizens to think too freely.<<set _citizen += 30>>
<<elseif $authority <= 6000>>
	Your very low authority allows your citizens to think too freely.<<set _citizen += 25>>
<<elseif $authority <= 9000>>
	Your moderate authority allows your citizens to think a bit too freely.<<set _citizen += 20>>
<<elseif $authority <= 12000>>
	Your good authority does not allow your citizens to think too freely.<<set _citizen += 15>>
<<elseif $authority <= 15000>>
	Your high authority does not allow your citizens to think too freely.<<set _citizen += 10>>
<<elseif $authority <= 18000>>
	Your very high authority does not allow your citizens to think too freely.<<set _citizen += 5>>
<</if>>
<<if $crime >= 90>>
	The very high crime level of the arcology breeds extreme discontent between your citizens.<<set _citizen += 30>>
<<elseif $crime >= 60>>
	The high crime level of the arcology breeds high discontent between your citizens.<<set _citizen += 15>>
<<elseif $crime >= 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 > 1>>
		Your citizens are not happy with the noticeable lack of slaves compared to their numbers.<<set _citizen +=20>>
	<<elseif _CSratio > 0.5>>
		Your citizens are not happy with the lack of slaves compared to their numbers.<<set _citizen += 15>>
	<<elseif _CSratio < 0.2>>
		<<set _citizen -= 5>>
	<</if>>
<<elseif $arcologies[0].FSRepopulationFocus != "unset">>
	<<if _CSratio < 0.5>>
		Your citizens are not happy of being outbred by the slaves of the arcology<<set _citizen +=20>>
	<<elseif _CSratio < 1>>
		Your citizens are not happy of 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.3), _citizen = Math.trunc(_citizen * 0.3)>>
<</if>>
<<if _roll <= _slave>>
	<<if random(1,100) < _slave>>
		<<set $slaveRebellionEventFires = 1>>
		<<set $citizenRebellionEventFires = 0>>
		<<if $tension != 0>>
			<<set $slaveProgress += Math.round(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.round(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 */
<<if $slaveRebellionEventFires == 1 || $citizenRebellionEventFires == 1>>
	<br>
	<br>
	<<include "rebellionEvents">>
	/* and resets flags */
	<<set $slaveRebellionEventFires = 0>>
	<<set $citizenRebellionEventFires = 0>>
<</if>>

<<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 $forceRebellion == 1 && $foughtThisWeek == 0>>
	<<if random(1,100) <= 50>>
		<<set $slaveRebellion = 1>>
		<<set $citizenRebellion = 0>>
	<<else>>
		<<set $slaveRebellion = 0>>
		<<set $citizenRebellion = 1>>
	<</if>>
<</if>>

<<if $slaveRebellion == 1>>
	<<set $engageRule = 0>>
	<<set $lastRebellionWeeks = 0>>
	<<set $leadingTroops = "assistant">>
	/* calc how many slaves participate */
	<<set _authFactor = Math.clamp($authority / 20000,0.4,0.6)>>
	<<set _repFactor  = Math.clamp($rep / 20000,0.4,0.6)>>
	<<set $notInvolved = Math.round(($ASlaves * (1 - _repFactor)))>> 								/* a percentage of slave will not be involved in the rebellion, depending on reputation, the higher the more slaves stand aside */
	<<set $notInvolved = Math.clamp($notInvolved - random(1,100),0,$ASlaves)>>
	<<set $attackTroops = Math.round((($ASlaves - $notInvolved) * (1 - _authFactor)))>>				/* a percentage of slave will actively participate in the rebellion, depending on authority, the higher the fewer slaves join */
	<<set $attackTroops = Math.clamp($attackTroops - random(1,100),0,$ASlaves)>>
	<<set _citNonInvolved = $ACitizens * (1 - Math.clamp(_repFactor,0.5,0.7))>>
	<<set $irregulars = Math.round((($ACitizens  - _citNonInvolved) * (1 - Math.clamp(_repFactor,0.4,0.6))) * 0.1)>>		/* citizens participating on the side of the player.
	<<set $irregulars = Math.clamp($irregulars - random(1,100),0,$ACitizens)>>
	/* calc if units participate */
	<<for _i = 0; _i < $slaveUnits.length; _i++>>
		<<if $slaveUnits[_i].loyalty < 3>>
			<<if random(1,100) <= 80>>
				<<set $rebellingUnits.push($slaveUnits[_i])>>
			<</if>>
		<<elseif $slaveUnits[_i].loyalty < 6>>
			<<if random(1,100) <= 20>>
				<<set $rebellingUnits.push($slaveUnits[_i])>>
			<</if>>
		<</if>>
	<</for>>
	<<for _i = 0; _i < $mercUnits.length; _i++>>
		<<if $mercUnits[_i].loyalty < 3>>
			<<if random(1,100) <= 60>>
				<<set $rebellingUnits.push($mercUnits[_i])>>
			<</if>>
		<<elseif $mercUnits[_i].loyalty < 6>>
			<<if random(1,100) <= 10>>
				<<set $rebellingUnits.push($mercUnits[_i])>>
			<</if>>
		<</if>>
	<</for>>
	<<set $attackEquip = Math.clamp($weaponsLaw + random(-1,1),0,4)>>
<<elseif $citizenRebellion == 1>>
	<<set $engageRule = 0>>
	<<set $lastRebellionWeeks = 0>>
	<<set $leadingTroops = "assistant">>
	/* calc how many citizens participate */
	<<set _authFactor = $authority / 20000>>
	<<set _repFactor  = $rep / 20000>>
	<<set $notInvolved = Math.round($ACitizens * (1 - Math.clamp(_repFactor,0.5,0.7)))>> 							/* a percentage of citizens will not be involved in the rebellion, depending on reputation, the higher the more slaves stand aside */
	<<set $notInvolved = Math.clamp($notInvolved - random(1,100),0,$ACitizens)>>
	<<set $attackTroops = Math.round(($ACitizens - $notInvolved) * (1 - Math.clamp(_authFactor,0.4,0.6)))>>			/* a percentage of citizens will actively participate in the rebellion, depending on authority, the higher the fewer slaves join */
	<<set $attackTroops = Math.clamp($attackTroops - random(1,100),0,$ACitizens)>>
	<<set $irregulars = Math.round((($ACitizens - $notInvolved) * (1 - Math.clamp(_repFactor,0.4,0.6))) * 0.5)>>	/* citizens joining the player */
	<<set $irregulars = Math.clamp($irregulars - random(1,100),0,$ACitizens)>>
	/* calc if units participate */
	<<for _i = 0; _i < $militiaUnits.length; _i++>>
		<<if $militiaUnits[_i].loyalty < 3>>
			<<if random(1,100) <= 80>>
				<<set $rebellingUnits.push($militiaUnits[_i])>>
			<</if>>
		<<elseif $militiaUnits[_i].loyalty < 6>>
			<<if random(1,100) <= 20>>
				<<set $rebellingUnits.push($militiaUnits[_i])>>
			<</if>>
		<</if>>
	<</for>>
	<<for _i = 0; _i < $mercUnits.length; _i++>>
		<<if $mercUnits[_i].loyalty < 3>>
			<<if random(1,100) <= 60>>
				<<set $rebellingUnits.push($mercUnits[_i])>>
			<</if>>
		<<elseif $mercUnits[_i].loyalty < 6>>
			<<if random(1,100) <= 10>>
				<<set $rebellingUnits.push($mercUnits[_i])>>
			<</if>>
		<</if>>
	<</for>>
	<<set $attackEquip = Math.clamp($weaponsLaw + random(-1,1),0,4)>>
<<else>>
	<<set $lastRebellionWeeks++>>
<</if>>