From 9e8b261b556ac2b1cb7dfb0706f16982c82d95d9 Mon Sep 17 00:00:00 2001
From: hexall <hexall90@gmail.com>
Date: Thu, 1 Mar 2018 14:48:55 +0100
Subject: [PATCH] adding replenish all units and corrected rebellion error

---
 src/SecExp/attackOptions.tw             |   2 +
 src/SecExp/rebellionOptions.tw          |   2 +
 src/SecExp/rebellionReport.tw           |   4 +-
 src/SecExp/secBarracks.tw               |   2 +
 src/SecExp/seeUnit.tw                   |   2 +-
 src/SecExp/widgets/miscSecExpWidgets.tw | 106 ++++++++++++++++++++++++
 6 files changed, 115 insertions(+), 3 deletions(-)

diff --git a/src/SecExp/attackOptions.tw b/src/SecExp/attackOptions.tw
index e0ddd9e1b7e..71db9263e7e 100644
--- a/src/SecExp/attackOptions.tw
+++ b/src/SecExp/attackOptions.tw
@@ -502,6 +502,8 @@ Units about to be deployed:
 <<else>>
 	Clear saved roster
 <</if>>
+<br><br>
+<<replenishAllUnits>>
 		
 <br><br>
 
diff --git a/src/SecExp/rebellionOptions.tw b/src/SecExp/rebellionOptions.tw
index e56c105cee8..40e6fd7d213 100644
--- a/src/SecExp/rebellionOptions.tw
+++ b/src/SecExp/rebellionOptions.tw
@@ -290,6 +290,8 @@ We can dedicate some of our forces to the protection of the vital parts of the a
 	<</link>>
 <</if>>
 
+<br><br>
+<<replenishAllUnits>>
 
 <br>
 <br>
diff --git a/src/SecExp/rebellionReport.tw b/src/SecExp/rebellionReport.tw
index 6cf4a24dcef..62a752196c5 100644
--- a/src/SecExp/rebellionReport.tw
+++ b/src/SecExp/rebellionReport.tw
@@ -818,8 +818,6 @@
 <<set $enemyLosses = 0>>
 <<set $losses = 0>>
 <<set $battleTurns = 0>>
-<<set $slaveRebellion = 0>>
-<<set $citizenRebellion = 0>>
 <<set $irregulars = 0>>
 <<set $deployingMilitia = 0>>
 <<set $deployingSlaves = 0>>
@@ -831,4 +829,6 @@
 	<<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)>>
diff --git a/src/SecExp/secBarracks.tw b/src/SecExp/secBarracks.tw
index ae14bb0da42..d935c9b35c9 100644
--- a/src/SecExp/secBarracks.tw
+++ b/src/SecExp/secBarracks.tw
@@ -143,6 +143,8 @@ Your maximum number of units is <<print $maxUnits>>, currently you have <<print
 <<if $arcologies[0].FSRomanRevivalist != "unset" || $arcologies[0].FSChineseRevivalist != "unset" || $arcologies[0].FSArabianRevivalist != "unset" || $arcologies[0].FSEdoRevivalist != "unset" || $arcologies[0].FSEgyptianRevivalist != "unset" || $arcologies[0].FSAztecRevivalist != "unset">>
 <</if>>
 <br>
+<<replenishAllUnits>>
+<br>
 <br>
 __Security Drones__
 /* drones */
diff --git a/src/SecExp/seeUnit.tw b/src/SecExp/seeUnit.tw
index a99221484f5..3e26a8e9f3f 100644
--- a/src/SecExp/seeUnit.tw
+++ b/src/SecExp/seeUnit.tw
@@ -1,7 +1,7 @@
 :: seeUnit [nobr]
 
 <<if $attackThisWeek == 1>>
-	<<set $nextButton = "Back", $nextLink = "attackOption", $returnTo = "secBarracks">>
+	<<set $nextButton = "Back", $nextLink = "attackOptions", $returnTo = "secBarracks">>
 <<else>>
 	<<set $nextButton = "Back", $nextLink = "secBarracks", $returnTo = "secBarracks">>
 <</if>>
diff --git a/src/SecExp/widgets/miscSecExpWidgets.tw b/src/SecExp/widgets/miscSecExpWidgets.tw
index fb898a13f74..89af1a1ee9a 100644
--- a/src/SecExp/widgets/miscSecExpWidgets.tw
+++ b/src/SecExp/widgets/miscSecExpWidgets.tw
@@ -532,4 +532,110 @@
 	<<if $SFBaseHp != 4 + $humanUpgrade.hp>>
 		<<set $SFBaseHp = 4 + $humanUpgrade.hp>>
 	<</if>>
+<</widget>>
+
+<<widget "replenishAllUnits">>
+	<<set _hasLossesM = 0>>
+	<<set _hasLossesS = 0>>
+	<<set _hasLossesMe = 0>>
+	
+	<<for _i = 0; _i < $militiaUnits.length; _i++>>
+		<<if $militiaUnits[_i].troops < $militiaUnits[_i].maxTroops && $militiaFreeManpower > 0>>
+			<<set _hasLossesM = 1>>
+			<<break>>
+		<</if>>
+	<</for>>
+	
+	<<for _i = 0; _i < $slaveUnits.length; _i++>>
+		<<if $slaveUnits[_i].troops < $slaveUnits[_i].maxTroops && $helots > 0>>
+			<<set _hasLossesS = 1>>
+			<<break>>
+		<</if>>
+	<</for>>
+
+	<<for _i = 0; _i < $mercUnits.length; _i++>>
+		<<if $mercUnits[_i].troops < $mercUnits[_i].maxTroops && $mercFreeManpower > 0>>
+			<<set _hasLossesMe = 1>>
+			<<break>>
+		<</if>>
+	<</for>>
+	
+	<<if _hasLossesM == 1 && $militiaFreeManpower > 0 || _hasLossesS == 1 && $helots > 0 || _hasLossesMe == 1 && $mercFreeManpower > 0>>
+	
+		<<link "Replenish all units">>
+		
+			<<if _hasLossesM == 1>>
+				<<for _i = 0; _i < $militiaUnits.length; _i++>>
+					<<if $militiaUnits[_i].troops < $militiaUnits[_i].maxTroops && $militiaFreeManpower > 0>>
+						<<if $militiaFreeManpower >= $militiaUnits[_i].maxTroops - $militiaUnits[_i].troops>>
+							<<set $militiaFreeManpower -= $militiaUnits[_i].maxTroops - $militiaUnits[_i].troops>>
+							<<set $militiaEmployedManpower += $militiaUnits[_i].maxTroops - $militiaUnits[_i].troops>>
+							<<set _expLoss = ($militiaUnits[_i].maxTroops - $militiaUnits[_i].troops) / $militiaUnits[_i].troops>>
+							<<set $militiaUnits[_i].training -= $militiaUnits[_i].training * _expLoss>>
+							<<set $militiaUnits[_i].troops = $militiaUnits[_i].maxTroops>>
+						<<else>>
+							<<set $militiaEmployedManpower += $militiaFreeManpower>>
+							<<set _expLoss = $militiaFreeManpower / $militiaUnits[_i].troops>>
+							<<set $militiaUnits[_i].training -= $militiaUnits[_i].training * _expLoss>>
+							<<set $militiaUnits[_i].troops += $militiaFreeManpower>>
+							<<set $militiaFreeManpower = 0>>
+						<</if>>
+					<</if>>
+				<</for>>
+			<</if>>
+			
+			<<if _hasLossesS == 1>>
+				<<for _i = 0; _i < $slaveUnits.length; _i++>>
+					<<if $slaveUnits[_i].troops < $slaveUnits[_i].maxTroops && $helots > 0>>
+						<<if $helots >= $slaveUnits[_i].maxTroops - $slaveUnits[_i].troops>>
+							<<set $helots -= $slaveUnits[_i].maxTroops - $slaveUnits[_i].troops>>
+							<<set $slavesEmployedManpower += $slaveUnits[_i].maxTroops - $slaveUnits[_i].troops>>
+							<<set _expLoss = ($slaveUnits[_i].maxTroops - $slaveUnits[_i].troops) / $slaveUnits[_i].troops>>
+							<<set $slaveUnits[_i].training -= $slaveUnits[_i].training * _expLoss>>
+							<<set $slaveUnits[_i].troops = $slaveUnits[_i].maxTroops>>
+						<<else>>
+							<<set $slavesEmployedManpower += $helots>>
+							<<set _expLoss = $helots / $slaveUnits[_i].troops>>
+							<<set $slaveUnits[_i].training -= $slaveUnits[_i].training * _expLoss>>
+							<<set $slaveUnits[_i].troops += $helots>>
+							<<set $helots = 0>>
+						<</if>>
+					<</if>>
+				<</for>>
+			<</if>>
+			
+			<<if _hasLossesMe == 1>>
+				<<for _i = 0; _i < $mercUnits.length; _i++>>
+					<<if $mercUnits[_i].troops < $mercUnits[_i].maxTroops && $mercFreeManpower > 0>>
+						<<if $mercFreeManpower >= $mercUnits[_i].maxTroops - $mercUnits[_i].troops>>
+							<<set $mercFreeManpower -= $mercUnits[_i].maxTroops - $mercUnits[_i].troops>>
+							<<set $mercEmployedManpower += $mercUnits[_i].maxTroops - $mercUnits[_i].troops>>
+							<<set _expLoss = ($mercUnits[_i].maxTroops - $mercUnits[_i].troops) / $mercUnits[_i].troops>>
+							<<set $mercUnits[_i].training -= $mercUnits[_i].training * _expLoss>>
+							<<set $mercUnits[_i].troops = $mercUnits[_i].maxTroops>>
+						<<else>>
+							<<set $mercEmployedManpower += $mercFreeManpower>>
+							<<set _expLoss = $mercFreeManpower / $mercUnits[_i].troops>>
+							<<set $mercUnits[_i].training -= $mercUnits[_i].training * _expLoss>>
+							<<set $mercUnits[_i].troops += $mercFreeManpower>>
+							<<set $mercFreeManpower = 0>>
+						<</if>>
+					<</if>>
+				<</for>>
+			<</if>>
+			
+			<<if $slaveRebellion == 1 || $citizenRebellion == 1>>
+				<<goto "rebellionOptions">>
+			<<elseif $attackThisWeek == 1>>
+				<<goto "attackOptions">>
+			<<else>>
+				<<goto "secBarracks">>
+			<</if>>
+			
+			
+
+		<</link>>
+		<br>//Will replenish units as long as manpower is available
+	<</if>>
+
 <</widget>>
\ No newline at end of file
-- 
GitLab