From 2e63dff2df75fdebcabb513af435a0287f5dfbe3 Mon Sep 17 00:00:00 2001
From: lowercase-donkey <lowercasedonkey@gmail.com>
Date: Thu, 17 Jan 2019 22:31:52 -0500
Subject: [PATCH] Fix recording of a slaves cost

---
 src/js/datatypeCleanupJS.tw             |  2 +-
 src/js/economyJS.tw                     | 36 ++++++++++++-------------
 src/js/slaveGenerationJS.tw             |  9 ++++++-
 src/uncategorized/corporateMarket.tw    |  2 +-
 src/uncategorized/costsWidgets.tw       |  2 +-
 src/uncategorized/genericPlotEvents.tw  |  2 +-
 src/uncategorized/pCoupAttempt.tw       |  2 +-
 src/uncategorized/pInvasion.tw          |  2 +-
 src/uncategorized/pMercenaryRomeo.tw    |  2 +-
 src/uncategorized/pRaidResult.tw        |  2 +-
 src/uncategorized/reBoomerang.tw        |  2 +-
 src/uncategorized/reFSAcquisition.tw    |  2 +-
 src/uncategorized/reMalefactor.tw       |  2 +-
 src/uncategorized/reRecruit.tw          |  2 +-
 src/uncategorized/resFailure.tw         | 16 +++++------
 src/uncategorized/seRecruiterSuccess.tw |  2 +-
 16 files changed, 47 insertions(+), 40 deletions(-)

diff --git a/src/js/datatypeCleanupJS.tw b/src/js/datatypeCleanupJS.tw
index 75f7f434a08..63307a7559b 100644
--- a/src/js/datatypeCleanupJS.tw
+++ b/src/js/datatypeCleanupJS.tw
@@ -598,7 +598,7 @@ window.slaveMiscellaneousDatatypeCleanup = function slaveMiscellaneousDatatypeCl
 		slave.death = "";
 	}
 	if (slave.slaveCost !== 0) {
-		slave.slaveCost = Math.max(+slave.slaveCost, -1) || -1;
+		slave.slaveCost = Math.min(+slave.slaveCost, 1) || 1;
 	}
 	slave.lifetimeCashExpenses = Math.max(+slave.lifetimeCashExpenses, 0) || 0;
 	slave.lifetimeCashIncome = Math.max(+slave.lifetimeCashIncome, 0) || 0;
diff --git a/src/js/economyJS.tw b/src/js/economyJS.tw
index fd813506d0b..bcd0c23e57b 100644
--- a/src/js/economyJS.tw
+++ b/src/js/economyJS.tw
@@ -981,7 +981,7 @@ window.cashX = function(cost, what, who) {
 					V.lastWeeksCashIncome[what] += cost;
 
 					//record the slave, if available
-					if (who !== undefined){
+					if (typeof who !== 'undefined'){
 							who.lastWeeksCashIncome += cost;
 							who.lifetimeCashIncome += cost;
 					}
@@ -995,25 +995,25 @@ window.cashX = function(cost, what, who) {
 		
 		//EXPENSES
 		else if(cost < 0) {
-				if (typeof V.lastWeeksCashExpenses[what] !== 'undefined') {
-					
-					//record the action
-					V.lastWeeksCashExpenses[what] += cost;
-					
-					//record the slave, if available
-					if (who !== undefined){
-						if (what == "slaveTransfer"){
-							who.slaveCost += cost;
-						} else {
-							who.lifetimeCashExpenses += cost;
-						}
+			if (typeof V.lastWeeksCashExpenses[what] !== 'undefined') {
+				
+				//record the action
+				V.lastWeeksCashExpenses[what] += cost;
+				
+				//record the slave, if available
+				if (typeof who !== 'undefined'){
+					if (what === "slaveTransfer"){
+						who.slaveCost = cost;
+					} else {
+						who.lifetimeCashExpenses += cost;
 					}
-
-					//Spend the money
-					V.cash += cost;
-				} else {
-					V.lastWeeksErrors += `Unknown place "${what}",`;
 				}
+
+				//Spend the money
+				V.cash += cost;
+			} else {
+				V.lastWeeksErrors += `Unknown place "${what}",`;
+			}
 		}
 		else {
 			//somebody probably tried to pass a 0, ignore it.
diff --git a/src/js/slaveGenerationJS.tw b/src/js/slaveGenerationJS.tw
index 62987b67111..e9aa0247ffb 100644
--- a/src/js/slaveGenerationJS.tw
+++ b/src/js/slaveGenerationJS.tw
@@ -1747,7 +1747,14 @@ window.BaseSlave = function BaseSlave() {
 		override_Brow_H_Color: 0,
 		missingEyes: 0,
 		missingArms: 0,
-		missingLegs: 0
+		missingLegs: 0,
+		slaveCost: 0,
+		lifetimeCashExpenses: 0,
+		lifetimeCashIncome: 0,
+		lastWeeksCashIncome: 0,
+		lifetimeRepExpenses: 0,
+		lifetimeRepIncome: 0,
+		lastWeeksRepIncome: 0
 	};
 };
 
diff --git a/src/uncategorized/corporateMarket.tw b/src/uncategorized/corporateMarket.tw
index 99152ad2974..019fcb19cdb 100644
--- a/src/uncategorized/corporateMarket.tw
+++ b/src/uncategorized/corporateMarket.tw
@@ -87,7 +87,7 @@ while you browse.
 The offered price is <<print cashFormat($slaveCost)>>.<<if $slavesSeen > $slaveMarketLimit>> You have cast such a wide net for slaves this week that it is becoming more expensive to find more for sale. Your reputation helps determine your reach within the slave market.<</if>>
 <br>
 <<if $cash >= $slaveCost>>
-	[["Buy " + $his + " slave contract"|New Slave Intro][cashX($slaveCost, "slaveTransfer", $activeSlave),$nextButton = "Continue",$nextLink = "AS Dump",$returnTo = "Main"]]
+	[["Buy " + $his + " slave contract"|New Slave Intro][cashX(forceNeg($slaveCost), "slaveTransfer", $activeSlave),$nextButton = "Continue",$nextLink = "AS Dump",$returnTo = "Main"]]
 <<else>>
 	//You lack the necessary funds to buy this slave.//
 <</if>>
diff --git a/src/uncategorized/costsWidgets.tw b/src/uncategorized/costsWidgets.tw
index d406383f29b..b259ecb3e44 100644
--- a/src/uncategorized/costsWidgets.tw
+++ b/src/uncategorized/costsWidgets.tw
@@ -409,7 +409,7 @@
 	<<if $args[0].origin != 0>>
 		$args[0].origin<br>
 	<</if>>
-	<<if $args[0].slaveCost > 0>>
+	<<if $args[0].slaveCost < 0>>
 		You bought her for: ¤$args[0].slaveCost.
 		<<set _Cost = $args[0].slaveCost>>
 	<<elseif $args[0].slaveCost == 0>>
diff --git a/src/uncategorized/genericPlotEvents.tw b/src/uncategorized/genericPlotEvents.tw
index 8cfecb1e08b..c4e124a1eeb 100644
--- a/src/uncategorized/genericPlotEvents.tw
+++ b/src/uncategorized/genericPlotEvents.tw
@@ -1333,7 +1333,7 @@ A screen opposite your desk springs to life, <<if $assistant == 0>>showing your
 	<<replace "#result">>
 	<<for _genPlot = 0; _genPlot < _newSlaves.length; _genPlot++>>
 		<<slaveCost _newSlaves[_genPlot]>>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 	<</for>>
 	Prisoners @@.green;sold.@@
 	<</replace>>
diff --git a/src/uncategorized/pCoupAttempt.tw b/src/uncategorized/pCoupAttempt.tw
index 24788e6e0fe..1891bd4f872 100644
--- a/src/uncategorized/pCoupAttempt.tw
+++ b/src/uncategorized/pCoupAttempt.tw
@@ -262,7 +262,7 @@ You are awakened in the middle of the night by a jolt that shakes the entire arc
 	<<replace "#result">>
 	<<for _pca = 0; _pca < _newSlaves.length; _pca++>>
 		<<slaveCost _newSlaves[_pca]>>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 	<</for>>
 	Prisoners sold.
 	<</replace>>
diff --git a/src/uncategorized/pInvasion.tw b/src/uncategorized/pInvasion.tw
index 0a46fb9ff04..ab202522e99 100644
--- a/src/uncategorized/pInvasion.tw
+++ b/src/uncategorized/pInvasion.tw
@@ -116,7 +116,7 @@ The loose militia lately organized by the arcology owners has been called out to
 	<<replace "#result">>
 	<<for _i = 0; _i < _newSlaves.length; _i++>>
 		<<slaveCost _newSlaves[_i]>>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 	<</for>>
 	Prisoners sold.
 	<</replace>>
diff --git a/src/uncategorized/pMercenaryRomeo.tw b/src/uncategorized/pMercenaryRomeo.tw
index 67ce4b964c2..713bddbd343 100644
--- a/src/uncategorized/pMercenaryRomeo.tw
+++ b/src/uncategorized/pMercenaryRomeo.tw
@@ -103,7 +103,7 @@ proffered by an attentive slave girl, he seems almost bashful.
 					As he picks up her limbless form to give her a hug,
 				<</if>>
 				she gives the nearest camera a little nod and silently mouths the words "Thank you, <<= WrittenMaster($activeSlave)>>." Soon the romantic story of The Mercenary and the Slave Girl is being told in bars and brothels across the Free City, with you naturally playing @@.green;a supporting role.@@
-				<<run cashX($slaveCost, "slaveTransfer")>>
+				<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 				<<include "Remove activeSlave">>
 			<</if>>
 			<<unset $romeoID>>
diff --git a/src/uncategorized/pRaidResult.tw b/src/uncategorized/pRaidResult.tw
index 4e1460f643a..efcd84e8e22 100644
--- a/src/uncategorized/pRaidResult.tw
+++ b/src/uncategorized/pRaidResult.tw
@@ -50,7 +50,7 @@ Out ahead of the main body of refugees there is a small knot moving quickly and
 	<<replace "#result">>
 	<<for _prr = 0; _prr < _newSlaves.length; _prr++>>
 		<<slaveCost _newSlaves[_prr]>>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 	<</for>>
 	Prisoners sold.
 	<</replace>>
diff --git a/src/uncategorized/reBoomerang.tw b/src/uncategorized/reBoomerang.tw
index a4e695dc1be..89405f94f27 100644
--- a/src/uncategorized/reBoomerang.tw
+++ b/src/uncategorized/reBoomerang.tw
@@ -468,7 +468,7 @@ It isn't obvious how $he managed to escape, though no doubt you could review the
 	<</replace>>
 <</link>> //This will cost in <<print cashFormat($contractCost)>>.//
 <br><<link "Sell $him immediately">>
-	<<run cashX($slaveCost, "slaveTransfer")>>
+	<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 	<<replace "#result">>
 		The penthouse opens, and $activeSlave.slaveName stumbles inside, sobbing $his thanks. $He's immediately conducted to the autosurgery for some quick cosmetic surgery, while you and $assistantName do the necessary work to falsify the arcology records and conceal $his origins. Before $he's even out from under anesthesia, $he's bundled off into one of the hundreds of slave shipments that move between arcologies every day. This time $he'll be sold far enough away that no matter how much of an escape artist $he is, you won't be seeing any more of $him. $He can whine elsewhere.
 		<<set $boomerangStats = 0>>
diff --git a/src/uncategorized/reFSAcquisition.tw b/src/uncategorized/reFSAcquisition.tw
index 70301817bf4..e5ff5c67415 100644
--- a/src/uncategorized/reFSAcquisition.tw
+++ b/src/uncategorized/reFSAcquisition.tw
@@ -1421,7 +1421,7 @@ The call comes in from an office, and you suppress the urge to check whether $as
 	<</replace>>
 <</link>> //This will cost <<print cashFormat($contractCost)>>//
 <br><<link "Sell $him immediately">>
-	<<run cashX($slaveCost, "slaveTransfer")>>
+	<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 	<<replace "#result">>
 	<<switch $FSAcquisitionEvents>>
 	<<case "Pastoralist">>
diff --git a/src/uncategorized/reMalefactor.tw b/src/uncategorized/reMalefactor.tw
index bf2ffeab58d..a2a54aac5e6 100644
--- a/src/uncategorized/reMalefactor.tw
+++ b/src/uncategorized/reMalefactor.tw
@@ -590,7 +590,7 @@ $He is easily taken into custody and $his contraband confiscated once the jeerin
 <</link>>
 <</if>>
 <br><<link "Sell $him immediately">>
-	<<run cashX($slaveCost, "slaveTransfer")>>
+	<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 	<<if $cash4Babies == 1 && $malefactor == "anchorBaby">>
 		<<run cashX(random(20,30), "slaveTransfer")>>
 	<</if>>
diff --git a/src/uncategorized/reRecruit.tw b/src/uncategorized/reRecruit.tw
index 0b304fe8db9..e6e1ce7f228 100644
--- a/src/uncategorized/reRecruit.tw
+++ b/src/uncategorized/reRecruit.tw
@@ -2674,7 +2674,7 @@ He explains that one of his servant <<= $girl>>s was impregnated by his fool of
 	<<case "forbidden love">>
 	<<default>>
 	<br><<link "Sell $him immediately">>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 		<<replace "#result">>
 		<<switch $recruit>>
 		<<case "held POW">>
diff --git a/src/uncategorized/resFailure.tw b/src/uncategorized/resFailure.tw
index b5fd4e4e3ca..590353b3113 100644
--- a/src/uncategorized/resFailure.tw
+++ b/src/uncategorized/resFailure.tw
@@ -804,56 +804,56 @@ The failure of a prominent organization within your arcology has @@.red;affected
 	<<if $RESFailure == "TSS">>
 		<<if ($slaves[$i].origin == "She was given to you by a failed branch campus of The Slavegirl School right after her majority.") || ($slaves[$i].origin == "She was given to you by a failed branch campus of The Slavegirl School after she was retrained as a slave girl.")>>
 		<<slaveCost $slaves[$i]>>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 		<<include "Remove activeSlave">>
 		<<set $i -= 1>>
 		<</if>>
 	<<elseif $RESFailure == "GRI">>
 		<<if $slaves[$i].origin == "She was given to you by a failed subsidiary lab of the Growth Research Institute right after her use as a test subject ended.">>
 		<<slaveCost $slaves[$i]>>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 		<<include "Remove activeSlave">>
 		<<set $i -= 1>>
 		<</if>>
 	<<elseif $RESFailure == "SCP">>
 		<<if $slaves[$i].origin == "She was given to you by a failed branch campus of St. Claver Preparatory after she served as a plastic surgeon's passing final exam.">>
 		<<slaveCost $slaves[$i]>>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 		<<include "Remove activeSlave">>
 		<<set $i -= 1>>
 		<</if>>
 	<<elseif $RESFailure == "LDE">>
 		<<if $slaves[$i].origin == "She was given to you by a failed branch campus of the innovative École des Enculées right after her graduation.">>
 		<<slaveCost $slaves[$i]>>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 		<<include "Remove activeSlave">>
 		<<set $i -= 1>>
 		<</if>>
 	<<elseif $RESFailure == "TGA">>
 		<<if $slaves[$i].origin == "She was given to you by a failed branch campus of the intense Gymnasium-Academy right after her majority.">>
 		<<slaveCost $slaves[$i]>>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 		<<include "Remove activeSlave">>
 		<<set $i -= 1>>
 		<</if>>
 	<<elseif $RESFailure == "HA">>
 		<<if $slaves[$i].origin == "She was given to you by a failed branch campus of the Hippolyta Academy right after her majority.">>
 		<<slaveCost $slaves[$i]>>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 		<<include "Remove activeSlave">>
 		<<set $i -= 1>>
 		<</if>>
 	<<elseif $RESFailure == "TCR">>
 		<<if $slaves[$i].origin == "She is a prized dairy cow given to you by a failed local pasture of The Cattle Ranch.">>
 		<<slaveCost $slaves[$i]>>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 		<<include "Remove activeSlave">>
 		<<set $i -= 1>>
 		<</if>>
 	<<else>>
 		<<if ($slaves[$i].origin == "She was the leader of your arcology's Futanari Sisters until you engineered her community's failure and enslavement.") || ($slaves[$i].origin == "She was a Futanari Sister until you engineered her early enslavement.")>>
 		<<slaveCost $slaves[$i]>>
-		<<run cashX($slaveCost, "slaveTransfer")>>
+		<<run cashX(forceNeg($slaveCost), "slaveTransfer")>>
 		<<include "Remove activeSlave">>
 		<<set $i -= 1>>
 		<</if>>
diff --git a/src/uncategorized/seRecruiterSuccess.tw b/src/uncategorized/seRecruiterSuccess.tw
index 7411a622b22..5dd8d366a6b 100644
--- a/src/uncategorized/seRecruiterSuccess.tw
+++ b/src/uncategorized/seRecruiterSuccess.tw
@@ -256,7 +256,7 @@ Your recruiter $Recruiter.slaveName has succeeded; $he's convinced a desperate o
 	<</replace>>
 <</link>> //This will cost <<print cashFormat($contractCost)>>//
 <br><<link "Sell _him2 immediately">>
-	<<run cashX($slaveCost, "slaveTransfer", $Recruiter)>>
+	<<run cashX(forceNeg($slaveCost), "slaveTransfer", $Recruiter)>>
 	<<replace "#result">>
 	$activeSlave.slaveName accepts being resold without much fuss. _He2's merely exchanged one unknown owner for another. For all _he2 knows _his2 new buyer will be less abusive than you would have been. _He2 would be less complacent if _he2 knew who _his2 buyers are; _he2'll be immured in an arcade within the hour.
 	<</replace>>
-- 
GitLab