From 59d8fc82e3eda141d9ae08d97c7514a4c0638013 Mon Sep 17 00:00:00 2001
From: Frankly George <franklygeorgy@gmail.com>
Date: Tue, 25 Mar 2025 08:50:26 -0400
Subject: [PATCH] Fix #5636, #5665, and #5667

---
 devTools/dictionaries/misc.txt              |  1 +
 src/005-passages/interactPassages.js        |  8 ++--
 src/events/scheduled/seHuskSlaveDelivery.js |  4 +-
 src/js/wombJS.js                            |  6 ++-
 src/npc/surgery/bodySwap/bodySwap.js        |  2 +-
 src/npc/surgery/bodySwap/huskSlaveSwap.js   | 43 ++++++++++++++-------
 6 files changed, 42 insertions(+), 22 deletions(-)

diff --git a/devTools/dictionaries/misc.txt b/devTools/dictionaries/misc.txt
index 5cec5cbe748..8d376ed6eba 100644
--- a/devTools/dictionaries/misc.txt
+++ b/devTools/dictionaries/misc.txt
@@ -4,6 +4,7 @@ mcmonkeyprojects
 orthodontal
 rgthree
 SDXL
+Svornost
 swinir
 unapprovingly
 uncircumcise
diff --git a/src/005-passages/interactPassages.js b/src/005-passages/interactPassages.js
index 3c51051720c..2ffd7cb450d 100644
--- a/src/005-passages/interactPassages.js
+++ b/src/005-passages/interactPassages.js
@@ -33,17 +33,17 @@ new App.DomPassage(
 	"Husk Slave Swap Workaround",
 	() => {
 		V.nextButton = "Abort Operation";
-		if (!V.temp.activeSlave) {
-			throw new Error(`V.temp.activeSlave is not set`);
+		if (!V.temp.husk) {
+			throw new Error(`V.temp.husk is not set`);
 		}
-		if (V.temp.activeSlave.tankBaby !== 3) {
+		if (V.temp.husk.tankBaby !== 3) {
 			V.nextLink = "Scheduled Event";
 			V.returnTo = "Scheduled Event";
 		} else {
 			V.nextLink = "Main";
 			V.returnTo = "Incubator";
 		}
-		return huskSwapSelection(V.temp.activeSlave);
+		return huskSwapSelection(V.temp.husk);
 	}
 );
 
diff --git a/src/events/scheduled/seHuskSlaveDelivery.js b/src/events/scheduled/seHuskSlaveDelivery.js
index 42ef18f1361..bee1f108784 100644
--- a/src/events/scheduled/seHuskSlaveDelivery.js
+++ b/src/events/scheduled/seHuskSlaveDelivery.js
@@ -15,6 +15,8 @@ App.Events.SEHuskSlaveDelivery = class SEHuskSlaveDelivery extends App.Events.Ba
 		if (V.huskSlave.race !== "not important") {
 			pram.race = V.huskSlave.race;
 		}
+
+		/** @type {FC.SlaveState} */
 		let husk;
 		if (V.huskSlave.sex === 2) {
 			husk = GenerateNewSlave("XY", pram);
@@ -73,7 +75,7 @@ App.Events.SEHuskSlaveDelivery = class SEHuskSlaveDelivery extends App.Events.Ba
 		App.Events.addResponses(node, choices);
 
 		function accept() {
-			V.temp.activeSlave = husk;
+			V.temp.husk = husk;
 			V.returnTo = V.nextLink;
 			Engine.play("Husk Slave Swap Workaround");
 			return ``;
diff --git a/src/js/wombJS.js b/src/js/wombJS.js
index a04a4b9e292..4f2225c6172 100644
--- a/src/js/wombJS.js
+++ b/src/js/wombJS.js
@@ -866,10 +866,14 @@ globalThis.WombCleanGenericReserve = function(actor, type, count) {
 
 /**
  * @param {FC.HumanState} actor
- * @param {"incubator"|"nursery"} type
+ * @param {"incubator"|"nursery"|"both"} type
  * @returns {number}
  */
 globalThis.WombReserveCount = function(actor, type) {
+	if (type === "both") {
+		return WombReserveCount(actor, "incubator") + WombReserveCount(actor, "nursery");
+	}
+
 	let cnt = 0;
 
 	actor.womb.forEach(function(ft) {
diff --git a/src/npc/surgery/bodySwap/bodySwap.js b/src/npc/surgery/bodySwap/bodySwap.js
index 2f3970c7a0a..6b5448d8c06 100644
--- a/src/npc/surgery/bodySwap/bodySwap.js
+++ b/src/npc/surgery/bodySwap/bodySwap.js
@@ -339,7 +339,7 @@ globalThis.huskSwapSelection = function(body) {
 				if (!hasAnyProstheticLimbs(soul)) {
 					if (soul.indenture === -1) {
 						if (soul.breedingMark === 0 || V.propOutcome === 0 || V.eugenicsFullControl === 1 || !FutureSocieties.isActive('FSRestart')) {
-							if (WombReserveCount(soul) === 0) {
+							if (WombReserveCount(soul, "both") === 0) {
 								if (soul.ID !== body.ID) {
 									App.UI.DOM.appendNewElement("div", el,
 										App.UI.DOM.link(
diff --git a/src/npc/surgery/bodySwap/huskSlaveSwap.js b/src/npc/surgery/bodySwap/huskSlaveSwap.js
index b55cba76b87..d35d3fc5994 100644
--- a/src/npc/surgery/bodySwap/huskSlaveSwap.js
+++ b/src/npc/surgery/bodySwap/huskSlaveSwap.js
@@ -1,27 +1,40 @@
+
+/**
+ * Places the mind of the slave at `V.temp.swappingSlave` in a body husk (at `V.temp.husk`) and returns a description of the procedure
+ * @returns {DocumentFragment}
+ */
 App.UI.SlaveInteract.huskSlaveSwap = function() {
 	const node = new DocumentFragment();
 
 	const target = getSlave(V.temp.swappingSlave);
 	const oldSlave = clone(target);
+	/** @type {FC.SlaveState} */
+	const husk = V.temp.husk;
+	if (!husk) {
+		throw new Error(`V.temp.husk was not set`);
+	}
 	const {
 		he
 	} = getPronouns(target);
 
 	App.UI.DOM.appendNewElement("p", node, `You strap ${target.slaveName}, and the body to which ${he} will be transferred, into the remote surgery and stand back as it goes to work.`);
-	bodySwap(target, asSlave(V.temp.activeSlave), false);
-	const gps = isInGenePool(V.temp.activeSlave) ? getGenePoolRecordWriteMode(V.temp.activeSlave) : undefined;
-	// special exception to swap genePool since the temporary body lacks an entry. Otherwise we could just call bodySwap using the genePool entries
-	gps.race = target.race;
-	gps.origRace = target.origRace;
-	gps.skin = target.skin;
-	gps.markings = target.markings;
-	gps.eye.origColor = target.eye.origColor;
-	gps.origHColor = target.origHColor;
-	gps.origSkin = target.origSkin;
-	gps.face = target.face;
-	gps.pubicHStyle = target.pubicHStyle;
-	gps.underArmHStyle = target.underArmHStyle;
-	gps.eyebrowHStyle = target.eyebrowHStyle;
+	bodySwap(target, asSlave(husk), false);
+	const gps = isInGenePool(husk) ? getGenePoolRecordWriteMode(husk) : undefined;
+	if (gps) {
+		// not sure what Svornost was trying to accomplish with this, but gps will always be undefined due to how this function is called. So this code should never run, but I left it here just in case - FranklyGeorge
+		// special exception to swap genePool since the temporary body lacks an entry. Otherwise we could just call bodySwap using the genePool entries - Svornost
+		gps.race = target.race;
+		gps.origRace = target.origRace;
+		gps.skin = target.skin;
+		gps.markings = target.markings;
+		gps.eye.origColor = target.eye.origColor;
+		gps.origHColor = target.origHColor;
+		gps.origSkin = target.origSkin;
+		gps.face = target.face;
+		gps.pubicHStyle = target.pubicHStyle;
+		gps.underArmHStyle = target.underArmHStyle;
+		gps.eyebrowHStyle = target.eyebrowHStyle;
+	}
 
 	App.Events.addParagraph(node, [
 		`After an honestly impressive procedure, ${target.slaveName} is recovering nicely.`,
@@ -57,7 +70,7 @@ App.UI.SlaveInteract.huskSlaveSwap = function() {
 	App.Events.addParagraph(node, r);
 	target.bodySwap++;
 	cashX(payout, "slaveTransfer");
-	V.temp.activeSlave = 0;
+	V.temp.husk = undefined;
 	V.temp.swappingSlave = 0;
 	return node;
 };
-- 
GitLab