From e14f0a76d86e14a586781e420d1eccc5a9f15e82 Mon Sep 17 00:00:00 2001
From: Blank_Alt <12406-Blank_Alt@users.noreply.gitgud.io>
Date: Thu, 30 Apr 2020 21:06:55 -0700
Subject: [PATCH] Sec exp battle fix

---
 src/Mods/SecExp/attackReport.tw          |  15 +-
 src/Mods/SecExp/buildings/secBarracks.tw |  18 +--
 src/Mods/SecExp/js/secExp.js             | 170 +++++++++++------------
 src/Mods/SecExp/unitsBattleReport.tw     |   2 +-
 4 files changed, 91 insertions(+), 114 deletions(-)

diff --git a/src/Mods/SecExp/attackReport.tw b/src/Mods/SecExp/attackReport.tw
index 040e1c700a6..a2c22eec0ab 100644
--- a/src/Mods/SecExp/attackReport.tw
+++ b/src/Mods/SecExp/attackReport.tw
@@ -1549,20 +1549,11 @@
 <<set $SFIntervention = 0>>
 <<set $secBots.isDeployed = 0>>
 <<for _i = 0; _i < $militiaUnits.length; _i++>>
-	<<if $militiaUnits[_i].isDeployed > 0>>
-		<<set $militiaUnits[_i].battlesFought++>>
-		<<set $militiaUnits[_i].isDeployed = 0>>
-	<</if>>
+	<<set $militiaUnits[_i].isDeployed = 0>>
 <</for>>
 <<for _i = 0; _i < $slaveUnits.length; _i++>>
-	<<if $slaveUnits[_i].isDeployed > 0>>
-		<<set $slaveUnits[_i].battlesFought++>>
-		<<set $slaveUnits[_i].isDeployed = 0>>
-	<</if>>
+	<<set $slaveUnits[_i].isDeployed = 0>>
 <</for>>
 <<for _i = 0; _i < $mercUnits.length; _i++>>
-	<<if $mercUnits[_i].isDeployed > 0>>
-		<<set $mercUnits[_i].battlesFought++>>
-		<<set $mercUnits[_i].isDeployed = 0>>
-	<</if>>
+	<<set $mercUnits[_i].isDeployed = 0>>
 <</for>>
\ No newline at end of file
diff --git a/src/Mods/SecExp/buildings/secBarracks.tw b/src/Mods/SecExp/buildings/secBarracks.tw
index bc32450072c..2e922fded69 100644
--- a/src/Mods/SecExp/buildings/secBarracks.tw
+++ b/src/Mods/SecExp/buildings/secBarracks.tw
@@ -219,9 +219,8 @@ You are free to organize your menial slaves into fighting units. Currently you h
 <</if>>
 <<for _i = 0; _i < _sL; _i++>>
 	<<capture _i>>
+	<<= App.SecExp.unit.dec($slaveUnits[_i], "Slaves")>>
 	<<if $slaveUnits[_i].active == 1>>
-		<br>
-		<<= App.SecExp.unit.dec($slaveUnits[_i], "Slaves")>>
 		<br>
 		<<link "Disband the unit" "secBarracks">>
 			<<set $menials += $slaveUnits[_i].troops>>
@@ -254,9 +253,6 @@ You are free to organize your menial slaves into fighting units. Currently you h
 		<</if>>
 
 	<<else>>
-		<br>
-		<br>
-		$slaveUnits[_i].platoonName lost too many operatives to be considered active.
 		<br>
 		<<link "Disband the unit" "secBarracks">>
 			<<set _elimUnit = $slaveUnits[_i]>>
@@ -332,9 +328,8 @@ __Militia__
 	<</if>>
 	<<for _i = 0; _i < _mL; _i++>>
 		<<capture _i>>
+		<<= App.SecExp.unit.dec($militiaUnits[_i], "Militia")>>
 		<<if $militiaUnits[_i].active == 1>>
-			<br>
-			<<= App.SecExp.unit.dec($militiaUnits[_i], "Militia")>>
 			<br>
 			<<link "Disband the unit" "secBarracks">>
 				<<set $militiaFreeManpower += $militiaUnits[_i].troops>>
@@ -366,9 +361,6 @@ __Militia__
 				<</link>>
 			<</if>>
 		<<else>>
-			<br>
-			<br>
-			$militiaUnits[_i].platoonName lost too many operatives to be considered active.
 			<br>
 			<<link "Disband the unit" "secBarracks">>
 				<<set $militiaFreeManpower += $militiaUnits[_i].troops>>
@@ -442,9 +434,8 @@ __Mercenaries__
 	<</if>>
 	<<for _i = 0; _i < _meL; _i++>>
 		<<capture _i>>
+		<<= App.SecExp.unit.dec($mercUnits[_i], "Mercs")>>
 		<<if $mercUnits[_i].active == 1>>
-			<br>
-			<<= App.SecExp.unit.dec($mercUnits[_i], "Mercs")>>
 			<br>
 			<<link "Disband the unit" "secBarracks">>
 				<<set $mercFreeManpower += $mercUnits[_i].troops>>
@@ -477,9 +468,6 @@ __Mercenaries__
 			<</if>>
 
 		<<else>>
-			<br>
-			<br>
-			$mercUnits[_i].platoonName lost too many operatives to be considered active.
 			<br>
 			<<link "Disband the unit" "secBarracks">>
 				<<set _elimUnit = $mercUnits[_i]>>
diff --git a/src/Mods/SecExp/js/secExp.js b/src/Mods/SecExp/js/secExp.js
index 425d5388b4a..cb791e3e7eb 100644
--- a/src/Mods/SecExp/js/secExp.js
+++ b/src/Mods/SecExp/js/secExp.js
@@ -735,105 +735,95 @@ App.SecExp.unit = (function() {
 			} else {
 				r += `\nThe drone unit is made up of ${input.troops} drones. `;
 			}
-			if (jsDef(input.active) || input.active > 0) {
-				if (unitType !== "Bots") {
-						if(input.battlesFought > 1) {
-							r += `has participated in ${input.battlesFought} battles and is ready to face the enemy once more at your command. `;
-						} else if (input.battlesFought === 1) {
-							r += `is ready to face the enemy once more at your command. `;
-						} else {
-							r += `is ready to face the enemy in battle. `;
-						}
-					r += `\nIt's ${input.troops} `;
-				}
 
-				if(unitType !== "Bots") {
-					r += `men and women are `;
-					if(unitType === "Militia") {
-						r += `all proud citizens of your arcology, willing to put their lives on the line to protect their home. `;
-					} else if (unitType === "Slaves") {
-						r += `slaves in your possession, tasked with the protection of their owner and their arcology. `;
-					} else if (unitType === "Mercs") {
-					r += `mercenaries contracted to defend the arcology against external threats. `;
+			if (unitType !== "Bots") {
+					if(input.battlesFought > 1) {
+						r += `has participated in ${input.battlesFought} battles and is ready to face the enemy once more at your command. `;
+					} else if (input.battlesFought === 1) {
+						r += `is ready to face the enemy once more at your command. `;
+					} else {
+						r += `is ready to face the enemy in battle. `;
 					}
-				} else {
-					r += `All of which are assembled in an ordered line in front of you, absolutely silent and ready to receive their orders. `;
-				}
+				r += `\nIt's ${input.troops} men and women are `;
 
-				if(input.troops < input.maxTroops) {
-					r += `The unit is not at its full strength of ${input.maxTroops} operatives. `;
+				if(unitType === "Militia") {
+					r += `all proud citizens of your arcology, willing to put their lives on the line to protect their home. `;
+				} else if (unitType === "Slaves") {
+					r += `slaves in your possession, tasked with the protection of their owner and their arcology. `;
+				} else if (unitType === "Mercs") {
+				r += `mercenaries contracted to defend the arcology against external threats. `;
 				}
+			} else {
+				r += `All of which are assembled in an ordered formation in front of you, absolutely silent and ready to receive their orders. `;
+			}
 
-				if(unitType !== "Bots") {
-					if(input.equip === 0) {
-						r += `They are issued with simple, yet effective equipment: firearms, a few explosives and standard uniforms, nothing more. `;
-					} else if (input.equip === 1) {
-						r += `They are issued with good, modern equipment: firearms, explosives and a few specialized weapons like sniper rifles and machine guns. They also carry simple body armor. `;
-					} else if (input.equip === 2) {
-						r += `They are issued with excellent, high tech equipment: modern firearms, explosives, specialized weaponry and modern body armor. They are also issued with modern instruments like night vision and portable radars. `;
-					} else {
-						r += `They are equipped with the best the modern world has to offer: modern firearms, explosives, specialized weaponry, experimental railguns, adaptive body armor and high tech recon equipment. `;
-					}
+			if(input.troops < input.maxTroops) {
+				r += `The unit is not at its full strength of ${input.maxTroops} operatives. `;
+			}
+
+			if(unitType !== "Bots") {
+				if(input.equip === 0) {
+					r += `They are issued with simple, yet effective equipment: firearms, a few explosives and standard uniforms, nothing more. `;
+				} else if (input.equip === 1) {
+					r += `They are issued with good, modern equipment: firearms, explosives and a few specialized weapons like sniper rifles and machine guns. They also carry simple body armor. `;
+				} else if (input.equip === 2) {
+					r += `They are issued with excellent, high tech equipment: modern firearms, explosives, specialized weaponry and modern body armor. They are also issued with modern instruments like night vision and portable radars. `;
 				} else {
-					if(input.equip === 0) {
-						r += `They are equipped with light weaponry, mainly anti-riot nonlethal weapons. Not particularly effective in battle. `;
-					} else if (input.equip === 1) {
-						r += `They are equipped with light firearms, not an overwhelming amount of firepower, but with their mobility good enough to be effective. `;
-					} else if (input.equip === 2) {
-						r += `They are equipped with powerful, modern firearms and simple armor mounted around their frames. They do not make for a pretty sight, but on the battlefield they are a dangerous weapon. `;
-					} else {
-						r += `They are equipped with high energy railguns and adaptive armor. They are a formidable force on the battlefield, even for experienced soldiers. `;
-					}
+					r += `They are equipped with the best the modern world has to offer: modern firearms, explosives, specialized weaponry, experimental railguns, adaptive body armor and high tech recon equipment. `;
 				}
+			} else {
+				if(input.equip === 0) {
+					r += `They are equipped with light weaponry, mainly anti-riot nonlethal weapons. Not particularly effective in battle. `;
+				} else if (input.equip === 1) {
+					r += `They are equipped with light firearms, not an overwhelming amount of firepower, but with their mobility good enough to be effective. `;
+				} else if (input.equip === 2) {
+					r += `They are equipped with powerful, modern firearms and simple armor mounted around their frames. They do not make for a pretty sight, but on the battlefield they are a dangerous weapon. `;
+				} else {
+					r += `They are equipped with high energy railguns and adaptive armor. They are a formidable force on the battlefield, even for experienced soldiers. `;
+				}
+			}
 
-				if(unitType !== "Bots") {
-					if(input.training <= 33) {
-						r += `They lack the experience to be considered professionals, but `;
-					if (input === "Militia") {
-						r += `their eagerness to defend the arcology makes up for it. `;
-						} else if (unitType === "Slaves") {
-							r += `their eagerness to prove themselves makes up for it. `;
-						} else if (unitType === "Mercs") {
-							r += `they're trained more than enough to still be an effective unit. `;
-						}
-					} else if (input.training <= 66) {
-						r += `They have trained `;
-						if (input.battlesFought > 0) {
-							r += `and fought `;
-						}
-						r += `enough to be considered disciplined, professional soldiers, ready to face the battlefield. `;
-					} else {
-						r += `They are consummate veterans, with a wealth of experience and perfectly trained. On the battlefield they are a well oiled war machine capable of facing pretty much anything. `;
+			if(unitType !== "Bots") {
+				if(input.training <= 33) {
+					r += `They lack the experience to be considered professionals, but `;
+				if (input === "Militia") {
+					r += `their eagerness to defend the arcology makes up for it. `;
+					} else if (unitType === "Slaves") {
+						r += `their eagerness to prove themselves makes up for it. `;
+					} else if (unitType === "Mercs") {
+						r += `they're trained more than enough to still be an effective unit. `;
 					}
-
-					if(input.loyalty < 10) {
-						r += `The unit is extremely disloyal. Careful monitoring of their activities and relationships should be implemented. `;
-					} else if (input.loyalty < 33) {
-						r += `Their loyalty is low. Careful monitoring of their activities and relationships is advised. `;
-					} else if (input.loyalty < 66) {
-						r += `Their loyalty is not as high as it can be, but they are not actively working against their arcology owner. `;
-					} else if (input.loyalty < 90) {
-						r += `Their loyalty is high and strong. The likelihood of this unit betraying the arcology is low to non-existent. `;
-					} else {
-						r += `The unit is fanatically loyal. They would prefer death over betrayal. `;
+				} else if (input.training <= 66) {
+					r += `They have trained `;
+					if (input.battlesFought > 0) {
+						r += `and fought `;
 					}
+					r += `enough to be considered disciplined, professional soldiers, ready to face the battlefield. `;
+				} else {
+					r += `They are consummate veterans, with a wealth of experience and perfectly trained. On the battlefield they are a well oiled war machine capable of facing pretty much anything. `;
+				}
 
-					if (input.cyber > 0) {
-						r += `The soldiers of the unit have been enhanced with numerous cyberaugmentations which greatly increase their raw power. `;
-					}
-					if (input.medics > 0) {
-						r += `The unit has a dedicated squad of medics that will follow them in battle. `;
-					}
-					if(V.SF.Toggle && V.SF.Active >= 1 && input.SF > 0) {
-						r += `The unit has attached "advisors" from ${V.SF.Lower} that will help the squad remain tactically aware and active. `;
-					}
+				if(input.loyalty < 10) {
+					r += `The unit is extremely disloyal. Careful monitoring of their activities and relationships should be implemented. `;
+				} else if (input.loyalty < 33) {
+					r += `Their loyalty is low. Careful monitoring of their activities and relationships is advised. `;
+				} else if (input.loyalty < 66) {
+					r += `Their loyalty is not as high as it can be, but they are not actively working against their arcology owner. `;
+				} else if (input.loyalty < 90) {
+					r += `Their loyalty is high and strong. The likelihood of this unit betraying the arcology is low to non-existent. `;
+				} else {
+					r += `The unit is fanatically loyal. They would prefer death over betrayal. `;
 				}
-			} else {
-				r += `This unit has lost too many operatives`;
-				if (jsDef(input.battlesFought)) {
-					r += `in the ${input.battlesFought} it fought`;
+
+				if (input.cyber > 0) {
+					r += `The soldiers of the unit have been enhanced with numerous cyberaugmentations which greatly increase their raw power. `;
+				}
+				if (input.medics > 0) {
+					r += `The unit has a dedicated squad of medics that will follow them in battle. `;
+				}
+				if(V.SF.Toggle && V.SF.Active >= 1 && input.SF > 0) {
+					r += `The unit has attached "advisors" from ${V.SF.Lower} that will help the squad remain tactically aware and active. `;
 				}
-				r += `and can no longer be considered a unit at all. `;
 			}
 		} else if (V.SecExp.settings.unitDescriptions > 0) {
 			if (unitType !== "Bots") {
@@ -889,6 +879,14 @@ App.SecExp.unit = (function() {
 				r += `${App.SF.Caps()} "advisors" are attached. `;
 			}
 		}
+
+		if (!input.active) {
+			r += `<br>This unit has lost too many operatives `;
+			if (jsDef(input.battlesFought)) {
+				r += `in the ${input.battlesFought} it fought `;
+			}
+			r += `and can no longer be considered a unit at all.`;
+		}
 		return r;
 	}
 })();
diff --git a/src/Mods/SecExp/unitsBattleReport.tw b/src/Mods/SecExp/unitsBattleReport.tw
index 1ed2d3523ad..8130374dc3b 100644
--- a/src/Mods/SecExp/unitsBattleReport.tw
+++ b/src/Mods/SecExp/unitsBattleReport.tw
@@ -209,7 +209,7 @@
 				<<set $slaveUnits[_j].troops -= Math.trunc(Math.clamp(_loss - _med,0,$slaveUnits[_j].maxTroops))>>
 				<<set $slavesEmployedManpower -= Math.trunc(_loss - _med)>>
 				<<set $slavesTotalCasualties += Math.trunc(_loss - _med)>>
-				<<set $menials -= Math.trunc(_loss - _med)>>
+				<<set $menials -= Math.clamp($menials, 0, Math.trunc(_loss - _med))>>
 				<<if $slaveUnits[_j].training < 100>>
 					<<if random(1,100) > 60>>
 						Experience has increased.
-- 
GitLab