From fed44a633e889f651e3c0bb25194eec1bed50960 Mon Sep 17 00:00:00 2001
From: hexall90 <hexall90@gmail.com>
Date: Sun, 29 Oct 2017 11:06:45 +0100
Subject: [PATCH] fixes, recalc widgets, rebellions

---
 TODO.txt                                  |  18 ++--
 src/SecExp/SecExpBackwardCompatibility.tw |  44 ++++++---
 src/SecExp/authorityReport.tw             |   5 ++
 src/SecExp/edicts.tw                      |  10 +--
 src/SecExp/rebellionEvents.tw             |  15 ++--
 src/SecExp/rebellionGenerator.tw          |  42 ++++++---
 src/SecExp/rebellionReport.tw             |  56 +++++++++---
 src/SecExp/secInit.tw                     |  13 ++-
 src/SecExp/securityHQ.tw                  |  96 +++++++++++---------
 src/SecExp/securityReport.tw              |   3 +-
 src/SecExp/widgets/miscSecExpWidgets.tw   | 103 ++++++++++++++++++++++
 11 files changed, 309 insertions(+), 96 deletions(-)

diff --git a/TODO.txt b/TODO.txt
index 490a988449f..83846dacecf 100644
--- a/TODO.txt
+++ b/TODO.txt
@@ -1,15 +1,19 @@
-Base:
--encyclopedia entries
+Main:
+-finish recalc widgets
 
 Rebellions:
--add modifiers to rebellion mini events
--mini events for ciizens
+-mini events for citizens
 -create/modify handler and report for rebellion
 -riot control center
 
-later development:
--events to flesh out system
+further development:
+-specialized slave schools
 -fortifications
 -weap manufactoring
 -transport hub
--big battles
\ No newline at end of file
+
+Events:
+-famous criminal escapes to the arcology, followed by another arcology police force
+
+Edicts:
+-cyber enhancements for slave units
\ No newline at end of file
diff --git a/src/SecExp/SecExpBackwardCompatibility.tw b/src/SecExp/SecExpBackwardCompatibility.tw
index 7a9232ae032..b6cc0d71358 100644
--- a/src/SecExp/SecExpBackwardCompatibility.tw
+++ b/src/SecExp/SecExpBackwardCompatibility.tw
@@ -188,7 +188,7 @@
 <<set $secHelots = 0>>
 <</if>>
 <<if ndef $reqHelots>>
-<<set $reqHelots = 40>>
+<<set $reqHelots = 30>>
 <</if>>
 <<if ndef $secUpgrades >>
 <<set $secUpgrades = {
@@ -287,6 +287,29 @@
 <<set $smilingManFate = 4>>
 <</if>>
 
+/* rebellions */
+<<if ndef $tension>>
+<<set $tension = 0>>
+<</if>>
+<<if ndef $slaveProgress>>
+<<set $slaveProgress = 0>>
+<</if>>
+<<if ndef $citizenProgress>>
+<<set $citizenProgress = 0>>
+<</if>>
+<<if ndef $slaveRebellionEventFires>>
+<<set $slaveRebellionEventFires = 0>>
+<</if>>
+<<if ndef $citizenRebellionEventFires>>
+<<set $citizenRebellionEventFires = 0>>
+<</if>>
+<<if ndef $slaveRebellion>>
+<<set $slaveRebellion = 0>>
+<</if>>
+<<if ndef $citizenRebellion>>
+<<set $citizenRebellion = 0>>
+<</if>>
+
 /* armed forces stats */
 <<if ndef $targetUnit>>
 <<set $targetUnit = 0>>
@@ -340,15 +363,7 @@
 <<set $mercTotalManpower = 0>>
 <</if>>
 <<if ndef $mercFreeManpower>>
-	<<if $wasToggledBefore == 0>>
-		<<if $mercenaries == 1>>
-			<<set $mercFreeManpower = random(5,20)>>
-		<<elseif $mercenaries > 1>>
-			<<set $mercFreeManpower = random(10,30)>>
-		<</if>>
-	<<else>>
-		<<set $mercFreeManpower = 0>>
-	<</if>>
+<<set $mercFreeManpower = 0>>
 <</if>>
 <<if ndef mercEmployedManpower>>
 <<set $mercEmployedManpower = 0>>
@@ -633,7 +648,7 @@
 	<<set $mercUnits = []>>
 <</if>>
 
-<<recalcManpower>>
+
 
 /* SFanon additions */
 
@@ -647,4 +662,11 @@
 	<<set $secUpgrades.coldstorage = 0>>
 <</if>>
 
+
+/* recalculation widgets */
+<<recalcSecRestPoint>>
+<<recalcCrimeCap>>
+<<recalcReqHelots>>
+<<recalcManpower>>
+
 Missing Security Expansion variables set. All done!
\ No newline at end of file
diff --git a/src/SecExp/authorityReport.tw b/src/SecExp/authorityReport.tw
index f07a34ccf60..b99be88453c 100644
--- a/src/SecExp/authorityReport.tw
+++ b/src/SecExp/authorityReport.tw
@@ -160,3 +160,8 @@ This week
 
 <<set $authority += _authGrowth>>
 <<set $authority = Math.trunc(Math.clamp($authority, 0, 20000))>>
+
+<br>
+<br>
+
+<<include "rebellionReport">>
diff --git a/src/SecExp/edicts.tw b/src/SecExp/edicts.tw
index cfe04b06a03..641b437edf4 100644
--- a/src/SecExp/edicts.tw
+++ b/src/SecExp/edicts.tw
@@ -240,23 +240,23 @@
 		<<else>>
 			<br>//Not enough Authority.//
 		<</if>>
-		<br>&nbsp;&nbsp;&nbsp;&nbsp;//Will lower the amount of personnel necessary to man the security HQ, but will incur in upkeep costs.//
+		<br>&nbsp;&nbsp;&nbsp;&nbsp;//Will lower the amount of personnel necessary to man the security HQ by 5, but will incur in upkeep costs.//
 	<<elseif $SFSupportLevel == 1 && $securityForceArcologyUpgrades != 4 && $reqHelots > 5>>
 		<br>''Personnel training:'' $securityForceName will provide the security HQ personnel with advanced training, boosting its efficacy.
 		<<if $authority >= 1000>>
-			[[Implement|edicts][$SFSupportLevel++, $cash -=5000, $authority -= 1000, $SFSupportUpkeep += 2000, $reqHelots -= 5, $secRestPoint += 5]]
+			[[Implement|edicts][$SFSupportLevel++, $cash -=5000, $authority -= 1000, $SFSupportUpkeep += 2000, $reqHelots -= 5]]
 		<<else>>
 			<br>//Not enough Authority.//
 		<</if>>
-		<br>&nbsp;&nbsp;&nbsp;&nbsp;//Will lower the amount of personnel necessary to man the security HQ and increase the security resting point by 5, but will incur in upkeep costs.//
+		<br>&nbsp;&nbsp;&nbsp;&nbsp;//Will lower the amount of personnel necessary to man the security HQ by a further 5, but will incur in upkeep costs.//
 	<<elseif $SFSupportLevel == 2 && $securityForceArcologyUpgrades != 6 && $reqHelots > 5>>
 		<br>''Troops detachment:'' $securityForceName will provide troops to the security department to boost their effectiveness in the field.
 		<<if $authority >= 1000>>
-			[[Implement|edicts][$SFSupportLevel++, $cash -=5000, $authority -= 1000, $SFSupportUpkeep += 3000, $reqHelots -= 5, $secRestPoint += 5]]
+			[[Implement|edicts][$SFSupportLevel++, $cash -=5000, $authority -= 1000, $SFSupportUpkeep += 3000, $reqHelots -= 5]]
 		<<else>>
 			<br>//Not enough Authority.//
 		<</if>>
-		<br>&nbsp;&nbsp;&nbsp;&nbsp;//Will lower the amount of personnel necessary to man the security HQ and increase the security resting point by a further 5, but will incur in upkeep costs.//
+		<br>&nbsp;&nbsp;&nbsp;&nbsp;//Will lower the amount of personnel necessary to man the security HQ by a further 5, but will incur in upkeep costs.//
 	<<elseif $SFSupportLevel > 0>>
 		//You need to upgrade the security force base further to unlock the next edict//
 	<</if>>
diff --git a/src/SecExp/rebellionEvents.tw b/src/SecExp/rebellionEvents.tw
index 6e71d8b7f5c..17c45ba722b 100644
--- a/src/SecExp/rebellionEvents.tw
+++ b/src/SecExp/rebellionEvents.tw
@@ -29,7 +29,7 @@
 	<<elseif _rand == 2>>
 		This week books of unknwon 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 responsibles 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 what is in front of the law stolen goods, so he was processed as the case required and the slave returned to their rightful master. Many questions however remain without answers.
+		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 knwon anti-slavery group was caught trying to infiltrate the arcology. During the capture attempt shots were fired and several guards were injured, unfortunately the fugitive managed in th end 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>>
@@ -37,7 +37,7 @@
 	<<else>>
 		This week a slave was found dead in one of the sewer tunnels. It seems she was stabbed repeatedly with a sharp object. She was fairly famous for her capabilities as a slave trainer, her old master spent not an insignificant amount of money trying to find her once he realized she was missing. The episode might have been a simple mugging gone wrong, but her activities as a slave breaker might have played a role in her homicide.
 	<</if>>
-	<<set $tension += random(0,3)>>
+	<<set $tension += random(1,5)>>
 <<case 2>>
 	/* med tension slave rebellion events */
 	<<set _rand = random(0,5)>>
@@ -54,7 +54,7 @@
 	<<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 repreated before they could be dealt with.
 	<</if>>
-	<<set $tension += random(3,6)>>
+	<<set $tension += random(5,10)>>
 <<case 3>>
 	/* high tension slave rebellion events */
 	<<set _rand = random(0,4)>>
@@ -67,13 +67,18 @@
 	<<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(9,12)>>
+	<<set $tension += random(10,15)>>
 <<case 4>>
 	random citizen event, low tension
+	<<set $tension += random(1,5)>>
 <<case 5>>
 	random citizen event, med tension
+	<<set $tension += random(5,10)>>
 <<case 6>>
 	random citizen event, high tension
+	<<set $tension += random(10,15)>>
 <</switch>>
 
-<<set $tension = Math.clamp($tension,0,100)>>
\ No newline at end of file
+<<set $tension = Math.clamp($tension,0,100)>>
+
+<br>Debug: tension: $tension
diff --git a/src/SecExp/rebellionGenerator.tw b/src/SecExp/rebellionGenerator.tw
index cf8cb76fb34..fc7aa35bec4 100644
--- a/src/SecExp/rebellionGenerator.tw
+++ b/src/SecExp/rebellionGenerator.tw
@@ -119,22 +119,44 @@
 <</if>>
 
 /* rolls to see if event happens */
-<<if _slave > _citizen>>
-	<<if _slave >= 100>>
-		<<set _slave = random(90,95)>> 				/* there's always a 5% chance nothing happens */
-	<</if>>
+<<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 _roll <= _slave>>
 	<<if random(1,100) < _slave>>
 		<<set $slaveRebellionEventFires = 1>>
 		<<set $citizenRebellionEventFires = 0>>
-		<<set $slaveProgress += random(1,5) * ($tension / 100)>>
+		<<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 _citizen >= 100>>
-		<<set _citizen = random(90,95)>>
-	<</if>>
 	<<if random(1,100) < _citizen>>
 		<<set $slaveRebellionEventFires = 0>>
 		<<set $citizenRebellionEventFires = 1>>
-		<<set $citizenProgress += random(1,5) * ($tension / 100)>>
+		<<if $tension != 0>>
+			<<set $citizenProgress += Math.round(random(1,5) * ($tension / 100) * 10)>>
+		<<else>>
+			<<set $citizenProgress += random(1,5)>>
+		<</if>>
 	<</if>>
-<</if>>
\ No newline at end of file
+<</if>>
+
+<br>Debug: slave chance: _slave
+<br>Debug: citizen chance: _citizen
+<br>Debug: Event fires for slaves: $slaveRebellionEventFires
+<br>Debug: Event fires for citizens: $citizenRebellionEventFires
+<br>Debug: slave progress: $slaveProgress
+<br>Debug: citizen progress: $citizenProgress
+<br>
+<br>
\ No newline at end of file
diff --git a/src/SecExp/rebellionReport.tw b/src/SecExp/rebellionReport.tw
index 57b4a9d1fc1..c60d5146c12 100644
--- a/src/SecExp/rebellionReport.tw
+++ b/src/SecExp/rebellionReport.tw
@@ -1,23 +1,57 @@
 :: rebellionReport [nobr]
 
+<<include "rebellionGenerator">>
+
 <<if $tension <= 33>>
-	Tension in the arcology is low. There's no large scale organized movement against you or sign of impending rebellion.
+	Tensions are the arcology is low. Political and ideological opposition against the arcology owner is almost unheard of.
 <<elseif $tension <= 66>>
-	Tension in the arcology is rising, but there's still no significant rise in political opposition or ideologically motivated violence.
+	Tensions in the arcology are rising, political and ideological opposition against the arcology owner are becoming a part of the daily life of the arcology.
 <<else>>
-	Tensions are high. Opposition to the arcology owner is a sentiment shared by many and ideologically motivated violence is not unheard of.
+	Tensions are high. Opposition to the arcology owner is a sentiment shared by many and armed resistance is on the rise.
 <</if>>
-<br>
-<<if $slaveRebellionEventFires == 1>>
-	
-
-
-
 
+<<if $slaveProgress <= 25>>
+	There is very low unrest between slaves in the arcology. The chances of a rebellion igniting are extremely low.
+<<elseif $slaveProgress <= 50>>
+	There is some unrest between the slaves. No major movement is forming yet, but it might be time to consider preventive measures.
+<<elseif $slaveProgress <= 75>>
+	Unrest is getting high between the slaves of the arcology. Preventive measures are necessary if we want to prevent a violent rebellion.
+<<else>>
+	Unrest is extremely high between slaves. The chances of a rebellion happening in the near future are extremely high.
+<</if>>
 
+<<if $citizenProgress <= 25>>
+	There is very low unrest between the citizens of the arcology. The chances of a rebellion igniting are extremely low.
+<<elseif $citizenProgress <= 50>>
+	There is some unrest between the citizens. No major movement is forming yet, but it might be time to consider preventive measures.
+<<elseif $citizenProgress <= 75>>
+	Unrest is getting high between the citizens of the arcology. Preventive measures are necessary if we want to prevent a violent rebellion.
+<<else>>
+	Unrest is extremely high between citizens. The chances of a rebellion happening in the near future are extremely high.
+<</if>>
 
+<br>
+<br>
 
+<<if $slaveRebellionEventFires == 1 || $citizenRebellionEventFires == 1>>
+	<<include "rebellionEvents">>
+<</if>>
 
-/* clean up */
 <<set $slaveRebellionEventFires = 0>>
-<<set $citizenRebellionEventFires = 0>>
\ No newline at end of file
+<<set $citizenRebellionEventFires = 0>>
+<<if $slaveProgress >= 100>>
+	<<if random(1,100) <= 80>>			/* 80% of firing a rebellion once progress is at 100 */
+		<<set $slaveRebellion = 1>>
+	<<else>>
+		<<set $slaveProgress = 100>>
+	<</if>>
+<<elseif $citizenProgress >= 100>>
+	<<if random(1,100) <= 80>>		
+		<<set $citizenRebellion = 1>>
+	<<else>>
+		<<set $citizenProgress = 100>>
+	<</if>>
+<</if>>
+<br>
+<br>Debug: slave rebellion fires: $slaveRebellion
+<br>Debug: citizen rebellion fires: $citizenRebellion
\ No newline at end of file
diff --git a/src/SecExp/secInit.tw b/src/SecExp/secInit.tw
index a8ae10ea6b5..10233916032 100644
--- a/src/SecExp/secInit.tw
+++ b/src/SecExp/secInit.tw
@@ -2,7 +2,7 @@
 
 /* base vars */
 <<set $authority = 0>>
-<<set $security = 35>>
+<<set $security = 30>>
 <<set $secRestPoint = 30>>
 <<set $crime = 30>>
 <<set $crimeCap = 100>>
@@ -72,7 +72,7 @@
 	luxury: 0,
 	training: 0}>>
 <<set $secHelots = 0>>
-<<set $reqHelots = 40>>
+<<set $reqHelots = 30>>
 <<set $secUpgrades = {
 	nanoCams: 0,
 	cyberBots: 0,
@@ -115,6 +115,15 @@
 <<set $globalCrisisWeeks = 0>>
 <<set $smilingManFate = 4>>
 
+/* rebellions */
+<<set $tension = 0>>
+<<set $slaveProgress = 0>>
+<<set $citizenProgress = 0>>
+<<set $slaveRebellionEventFires = 0>>
+<<set $citizenRebellionEventFires = 0>>
+<<set $slaveRebellion = 0>>
+<<set $citizenRebellion = 0>>
+
 /* armed forces stats */
 <<set $targetUnit = 0>>
 <<set $targetIndex = 0>>
diff --git a/src/SecExp/securityHQ.tw b/src/SecExp/securityHQ.tw
index 1d73ad98018..097e24f600d 100644
--- a/src/SecExp/securityHQ.tw
+++ b/src/SecExp/securityHQ.tw
@@ -101,14 +101,14 @@ Considering the current upgrades the resting level for security is <<print $secR
 <br>
 
 <<if $secUpgrades.nanoCams == 0>>
-	[[Install a nano-camera system |securityHQ][$cash -= Math.trunc(5000*$upgradeMultiplierArcology), $secUpgrades.cctvCams = 1, $secRestPoint += 10, $reqHelots += 5, $secHQUpkeep += $upgradeUpkeep]]
+	[[Install a nano-camera system |securityHQ][$cash -= Math.trunc(5000*$upgradeMultiplierArcology), $secUpgrades.nanoCams = 1, $secRestPoint += 15, $reqHelots += 5, $secHQUpkeep += $upgradeUpkeep]]
 	<br>//Costs ¤<<print Math.trunc(5000*$upgradeMultiplierArcology)>>. Will raise rest point of security by 10 points, but will require 5 extra slaves in the headquarters and increases upkeep.//
 <<else>>
 	You have installed all across the arcology closed circuit nano-cameras to keep the arcology under your watchful eye.
 <</if>>
 <br>
 <<if $secUpgrades.cyberBots == 0>>
-	[[Buy cybersecurity algorithms|securityHQ][$cash -= Math.trunc(7500*$upgradeMultiplierArcology), $secUpgrades.cyberBots = 1, $secRestPoint += 10, $reqHelots += 5, $secHQUpkeep += $upgradeUpkeep]]
+	[[Buy cybersecurity algorithms|securityHQ][$cash -= Math.trunc(7500*$upgradeMultiplierArcology), $secUpgrades.cyberBots = 1, $secRestPoint += 15, $reqHelots += 5, $secHQUpkeep += $upgradeUpkeep]]
 	<br>//Costs ¤<<print Math.trunc(7500*$upgradeMultiplierArcology)>>. Will raise rest point of security by 10 points, but will require 5 extra slaves in the headquarters and increases upkeep.//
 <<else>>
 	You have bought advanced cybersecurity algorithms that will defend your arcology against hack attempts or cyber frauds.
@@ -116,14 +116,14 @@ Considering the current upgrades the resting level for security is <<print $secR
 <br>
 <<if $rep > 10000>>
 	<<if $secUpgrades.eyeScan == 0>>
-		[[Install invisible eye scanners|securityHQ][$cash -= Math.trunc(10000*$upgradeMultiplierArcology), $secUpgrades.eyeScan = 1, $secRestPoint += 15, $reqHelots += 10, $secHQUpkeep += $upgradeUpkeep]]
+		[[Install invisible eye scanners|securityHQ][$cash -= Math.trunc(10000*$upgradeMultiplierArcology), $secUpgrades.eyeScan = 1, $secRestPoint += 20, $reqHelots += 10, $secHQUpkeep += $upgradeUpkeep]]
 		<br>//Costs ¤<<print Math.trunc(10000*$upgradeMultiplierArcology)>>. Will raise rest point of security by 15 points, but will require 10 extra slaves in the headquarters and increases upkeep.//
 	<<else>>	
 		You have installed numerous hidden eye scanners that accurately register the movements of everyone inside the arcology.
 	<</if>>
 	<br>
 	<<if $secUpgrades.cryptoAnalyzer == 0>>
-		[[Buy and install crypto analyzers|securityHQ][$cash -= Math.trunc(15000*$upgradeMultiplierArcology), $secUpgrades.cryptoAnalyzer = 1, $secRestPoint += 15, $reqHelots += 10, $secHQUpkeep += $upgradeUpkeep]]
+		[[Buy and install crypto analyzers|securityHQ][$cash -= Math.trunc(15000*$upgradeMultiplierArcology), $secUpgrades.cryptoAnalyzer = 1, $secRestPoint += 20, $reqHelots += 10, $secHQUpkeep += $upgradeUpkeep]]
 		<br>//Costs ¤<<print Math.trunc(15000*$upgradeMultiplierArcology)>>. Will raise rest point of security by 15 points, but will require 10 extra slaves in the headquarters and increases upkeep.//
 	<<else>>
 		You have bought and employed sophisticated crypto analyzing software to accurately track and archive every financial movement or transaction made inside the walls of your arcology.
@@ -132,45 +132,6 @@ Considering the current upgrades the resting level for security is <<print $secR
 	<br>You lack the reputation to access more advanced upgrades.
 <</if>>
 
-<<if $secUpgrades.coldstorage == 6 && $rep >= 19500>>
-	<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of two years.
-	<br>
-	[[Expand the cold storage facility to increase data retention to three years|securityHQ][$cash -= Math.trunc(2400000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $secRestPoint += 5, $reqHelots += 5, $secHQUpkeep += $upgradeUpkeep]]
-	<br>//Costs ¤<<print Math.trunc(2400000*$upgradeMultiplierArcology)>>. Will raise rest point of security by 5 points, but will require an extra 5 slaves and will increases upkeep.//
-<<elseif $secUpgrades.coldstorage == 5 && $rep >= 19500>>
-<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of one year.
-	<br>
-	[[Expand the cold storage facility to increase data retention to two years|securityHQ][$cash -= Math.trunc(1200000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $secRestPoint += 5, $reqHelots += 5, $secHQUpkeep += $upgradeUpkeep]]
-	<br>//Costs ¤<<print Math.trunc(1200000*$upgradeMultiplierArcology)>>. Will raise rest point of security by 5 points, but will require an extra 5 slaves and will increases upkeep.//
-<<elseif $secUpgrades.coldstorage == 4 && $rep >= 19500>>
-	<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of nine months.
-	<br>
-	[[Expand the cold storage facility to increase data retention to one year|securityHQ][$cash -= Math.trunc(900000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $secRestPoint += 5, $reqHelots += 5, $secHQUpkeep += $upgradeUpkeep]]
-	<br>//Costs ¤<<print Math.trunc(900000*$upgradeMultiplierArcology)>>. Will raise rest point of security by 5 points, but will require an extra 5 slaves and will increases upkeep.//
-<<elseif $secUpgrades.coldstorage == 3 && $rep > 18000>>
-	<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of six months.
-	<br>
-	[[Expand the cold storage facility to increase data retention to nine months|securityHQ][$cash -= Math.trunc(600000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $secRestPoint += 5, $reqHelots += 5, $secHQUpkeep += $upgradeUpkeep]]
-	<br>//Costs ¤<<print Math.trunc(600000*$upgradeMultiplierArcology)>>. Will raise rest point of security by 5 points, but will require an extra 5 slaves and will increases upkeep.//
-<<elseif $secUpgrades.coldstorage == 2 && $rep > 16000>>
-	<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of three months.
-	<br>
-	[[Expand the cold storage facility to increase data retention to six months|securityHQ][$cash -= Math.trunc(300000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $secRestPoint += 5, $reqHelots += 5, $secHQUpkeep += $upgradeUpkeep]]
-	<br>//Costs ¤<<print Math.trunc(300000*$upgradeMultiplierArcology)>>. Will raise rest point of security by 5 points, but will require an extra 5 slaves and will increases upkeep.//
-<<elseif $secUpgrades.coldstorage == 1 && $rep > 14000>>
-	<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of one month.
-	<br>
-	[[Expand the cold storage facility to increase data retention to three months|securityHQ][$cash -= Math.trunc(100000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $secRestPoint += 5, $reqHelots += 5, $secHQUpkeep += $upgradeUpkeep]]
-	<br>//Costs ¤<<print Math.trunc(1000000*$upgradeMultiplierArcology)>>. Will raise rest point of security by 5 points, but will require an extra 5 slaves and will increases upkeep.//
-<<elseif $secUpgrades.coldstorage == 0 && $rep > 12000>>
-	[[Install a cold storage facility|securityHQ][$cash -= Math.trunc(50000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $secRestPoint += 5, $reqHelots += 5, $secHQUpkeep += $upgradeUpkeep]]
-	<br>//Costs ¤<<print Math.trunc(500000*$upgradeMultiplierArcology)>>. Will raise rest point of security by 5 points, but will require 5 extra slaves in the headquarters and increases upkeep.//
-<<elseif $secUpgrades.coldstorage > 6>>
-	<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of three years.
-<<else>>
-	<br>You lack the reputation to access more advanced upgrades.
-<</if>>
-
 <br>
 <br>
 
@@ -186,7 +147,7 @@ Considering the current upgrades the resting level for security is <<print $secR
 <<else>>
 	Your crime level (@@.orangered;<<print $crime>>@@) is extremely high.
 <</if>>
-Considering the current upgrades the maximum level of crime is <<print $crimeCap>>, while the effective maximum level is <<print Math.trunc($crimeCap * ($secHelots / $reqHelots))>>.
+Considering the current upgrades the maximum level of crime is <<print $crimeCap>>, while the effective maximum level is <<print Math.trunc(Math.clamp($crimeCap + $crimeCap * ($secHelots / $reqHelots),0,100))>>.
 
 <br>
 <br>
@@ -314,4 +275,51 @@ Considering the current upgrades the maximum level of crime is <<print $crimeCap
 	<</if>>
 <<else>>
 	<br>You lack the reputation to access more advanced upgrades.
+<</if>>
+
+<br>
+<br>
+
+<<if $rep > 12000>>
+	<br>__Cold Data Storage Facility__:
+	<<if $secUpgrades.coldstorage == 6 && $rep >= 19500 && $reqHelots > 10>>
+		<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of two years.
+		<br>
+		[[Expand the cold storage facility to increase data retention to three years|securityHQ][$cash -= Math.trunc(2400000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $reqHelots -= 10, $secHQUpkeep += $upgradeUpkeep]]
+		<br>//Costs ¤<<print Math.trunc(2400000*$upgradeMultiplierArcology)>>. Will lower the amount of required slaves by a further 10, but will increases upkeep.//
+	<<elseif $secUpgrades.coldstorage == 5 && $rep >= 19500 && $reqHelots > 10>>
+	<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of one year.
+		<br>
+		[[Expand the cold storage facility to increase data retention to two years|securityHQ][$cash -= Math.trunc(1200000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $reqHelots -= 10, $secHQUpkeep += $upgradeUpkeep]]
+		<br>//Costs ¤<<print Math.trunc(1200000*$upgradeMultiplierArcology)>>. Will lower the amount of required slaves by a further 10, but will increases upkeep.//
+	<<elseif $secUpgrades.coldstorage == 4 && $rep >= 19500 && $reqHelots > 10>>
+		<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of nine months.
+		<br>
+		[[Expand the cold storage facility to increase data retention to one year|securityHQ][$cash -= Math.trunc(900000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $reqHelots -= 10, $secHQUpkeep += $upgradeUpkeep]]
+		<br>//Costs ¤<<print Math.trunc(900000*$upgradeMultiplierArcology)>>. Will lower the amount of required slaves by a further 10, but will increases upkeep.//
+	<<elseif $secUpgrades.coldstorage == 3 && $rep > 18000 && $reqHelots > 10>>
+		<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of six months.
+		<br>
+		[[Expand the cold storage facility to increase data retention to nine months|securityHQ][$cash -= Math.trunc(600000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $reqHelots -= 10, $secHQUpkeep += $upgradeUpkeep]]
+		<br>//Costs ¤<<print Math.trunc(600000*$upgradeMultiplierArcology)>>. Will lower the amount of required slaves by a further 10, but will increases upkeep.//
+	<<elseif $secUpgrades.coldstorage == 2 && $rep > 16000 && $reqHelots > 10>>
+		<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of three months.
+		<br>
+		[[Expand the cold storage facility to increase data retention to six months|securityHQ][$cash -= Math.trunc(300000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $reqHelots -= 10, $secHQUpkeep += $upgradeUpkeep]]
+		<br>//Costs ¤<<print Math.trunc(300000*$upgradeMultiplierArcology)>>. Will lower the amount of required slaves by a further 10, but will increases upkeep.//
+	<<elseif $secUpgrades.coldstorage == 1 && $rep > 14000 && $reqHelots > 10>>
+		<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of one month.
+		<br>
+		[[Expand the cold storage facility to increase data retention to three months|securityHQ][$cash -= Math.trunc(100000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $reqHelots -= 10, $secHQUpkeep += $upgradeUpkeep]]
+		<br>//Costs ¤<<print Math.trunc(1000000*$upgradeMultiplierArcology)>>. Will lower the amount of required slaves by a further 10, but will increases upkeep.//
+	<<elseif $secUpgrades.coldstorage == 0 && $rep > 12000 && $reqHelots > 10>>
+		[[Install a cold storage facility|securityHQ][$cash -= Math.trunc(50000*$upgradeMultiplierArcology), $secUpgrades.coldstorage++, $reqHelots -= 10, $secHQUpkeep += $upgradeUpkeep]]
+		<br>//Costs ¤<<print Math.trunc(500000*$upgradeMultiplierArcology)>>. Will lower the amount of required slaves by 10, but will increases upkeep.//
+	<<elseif $secUpgrades.coldstorage > 6>>
+		<br>You have installed a cold storage facility for the Security HQ's archives with a data retention capability of three years.
+	<<elseif $reqHelots <= 10>>
+		<br>Personnel cannot be further reduced.
+	<<else>>
+		<br>You lack the reputation to access more advanced upgrades.
+	<</if>>
 <</if>>
\ No newline at end of file
diff --git a/src/SecExp/securityReport.tw b/src/SecExp/securityReport.tw
index 7b8277f6301..4f4a5c73e91 100644
--- a/src/SecExp/securityReport.tw
+++ b/src/SecExp/securityReport.tw
@@ -105,6 +105,7 @@
 	
 <<if $SFSupportLevel >= 3>>
 	The two squads of $securityForceName assigned to the Security HQ provide an essential help to the security department.
+<</if>>
 <<if $SFSupportLevel >= 2>>
 	The training officers of $securityForceName assigned to the Security HQ improve its effectiveness.
 <</if>>
@@ -212,7 +213,7 @@
 <</if>>
 
 /* crime cap */
-<<set _crimeCap = Math.trunc(Math.clamp($crimeCap * ($secHelots / $reqHelots),0,100))>>
+<<set _crimeCap = Math.trunc(Math.clamp($crimeCap + $crimeCap * ($secHelots / $reqHelots),0,100))>>
 <<if _crimeCap < $crimeCap && $secHQ == 1>>
 	The limited staff assigned to the HQ allows more space for criminals to act.
 <</if>>
diff --git a/src/SecExp/widgets/miscSecExpWidgets.tw b/src/SecExp/widgets/miscSecExpWidgets.tw
index 04f4f35ed62..dbcfbbbdb86 100644
--- a/src/SecExp/widgets/miscSecExpWidgets.tw
+++ b/src/SecExp/widgets/miscSecExpWidgets.tw
@@ -1,6 +1,14 @@
 :: miscSecExpWidgets [widget nobr]
 
 <<widget "recalcManpower">>
+	<<if $wasToggledBefore == 0>>
+		<<if $mercenaries == 1>>
+			<<set $mercFreeManpower = random(5,20)>>
+		<<elseif $mercenaries > 1>>
+			<<set $mercFreeManpower = random(10,30)>>
+		<</if>>
+	<</if>>
+	
 	<<set _correctEmployedMP = 0>>
 	<<for _i = 0; _i < $slaveUnits.length; _i++>>
 		<<set _correctEmployedMP += $slaveUnits[_i].troops>>
@@ -27,4 +35,99 @@
 	<<if $mercEmployedManpower != _correctEmployedMP>>
 		<<set $mercEmployedManpower = _correctEmployedMP>>
 	<</if>>
+<</widget>>
+
+<<widget "recalcSecRestPoint">>
+	<<set _newRest = 0>>
+	<<set _baseRest = 30>>
+	<<if $secUpgrades.nanoCams == 1>>
+		<<set _newRest += 15>>
+	<</if>>
+	<<if $secUpgrades.cyberBots == 1>>
+		<<set _newRest += 15>>
+	<</if>>
+	<<if $secUpgrades.eyeScan == 1>>
+		<<set _newRest += 20>>
+	<</if>>
+	<<if $secUpgrades.cryptoAnalyzer == 1>>
+		<<set _newRest += 20>>
+	<</if>>
+	<<set _newRest += _baseRest>>
+	<<set $secRestPoint = _newRest>>
+<</widget>>
+
+<<widget "recalcCrimeCap">>
+	<<set _baseCap = 100>>
+	<<set _newCap = _baseCap>>
+	<<if $crimeUpgrades.autoTrial == 1>>
+		<<set _newCap -= 10>>
+	<</if>>
+	<<if $crimeUpgrades.autoArchive == 1>>
+		<<set _newCap -= 10>>
+	<</if>>
+	<<if $crimeUpgrades.worldProfiler == 1>>
+		<<set _newCap -= 15>>
+	<</if>>
+	<<if $crimeUpgrades.advForensic == 1>>
+		<<set _newCap -= 15>>
+	<</if>>
+	<<set $crimeCap = _newCap>>
+<</widget>>
+
+<<widget "recalcReqHelots">>
+	<<set _newReq = 0>>
+	<<set _baseReq = 30>>
+	<<if $secUpgrades.nanoCams == 1>>
+		<<set _newReq += 5>>
+	<</if>>
+	<<if $secUpgrades.cyberBots == 1>>
+		<<set _newReq += 5>>
+	<</if>>
+	<<if $secUpgrades.eyeScan == 1>>
+		<<set _newReq += 10>>
+	<</if>>
+	<<if $secUpgrades.cryptoAnalyzer == 1>>
+		<<set _newReq += 10>>
+	<</if>>
+	<<if $crimeUpgrades.autoTrial == 1>>
+		<<set _newReq += 5>>
+	<</if>>
+	<<if $crimeUpgrades.autoArchive == 1>>
+		<<set _newReq += 5>>
+	<</if>>
+	<<if $crimeUpgrades.worldProfiler == 1>>
+		<<set _newReq += 10>>
+	<</if>>
+	<<if $crimeUpgrades.advForensic == 1>>
+		<<set _newReq += 10>>
+	<</if>>
+	<<if $intelUpgrades.sensors == 1>>
+		<<set _newReq += 5>>
+	<</if>>
+	<<if $intelUpgrades.signalIntercept == 1>>
+		<<set _newReq += 5>>
+	<</if>>
+	<<if $intelUpgrades.radar == 1>>
+		<<set _newReq += 10>>
+	<</if>>
+	<<if $readinessUpgrades.pathways == 1>>
+		<<set _newReq += 5>>
+	<</if>>
+	<<if $readinessUpgrades.rapidVehicles == 1>>
+		<<set _newReq += 5>>
+	<</if>>
+	<<if $readinessUpgrades.rapidPlatforms == 1>>
+		<<set _newReq += 10>>
+	<</if>>
+	<<if $readinessUpgrades.earlyWarn == 1>>
+		<<set _newReq += 10>>
+	<</if>>
+	<<if $SFSupportLevel >= 1>>
+		<<set _newReq -= 5 * $SFSupportLevel>>
+	<</if>>
+	<<if $secUpgrades.coldstorage >= 1>>
+		<<set _newReq -= 10 * $secUpgrades.coldstorage>>
+	<</if>>
+	<<set _newReq += _baseReq>>
+	<<set $reqHelots = _newReq>>
 <</widget>>
\ No newline at end of file
-- 
GitLab