From 7ae5f0ab206f1b37d543e169ad79f3a17d14ecb7 Mon Sep 17 00:00:00 2001
From: DCoded <dsoloha@live.com>
Date: Sat, 3 Aug 2019 23:56:56 -0400
Subject: [PATCH] Fixed major bug with activeChild values not updating upon
 leaving Child Interact menu, added cribsToIndices and childIndexForID

---
 src/facilities/nursery/childInteract.tw     |   6 +-
 src/facilities/nursery/nurseryWidgets.js    | 123 +++++++++++---------
 src/init/storyInit.tw                       |   1 +
 src/uncategorized/BackwardsCompatibility.tw |   1 +
 4 files changed, 77 insertions(+), 54 deletions(-)

diff --git a/src/facilities/nursery/childInteract.tw b/src/facilities/nursery/childInteract.tw
index f5504e69d0d..7f50ad272c9 100644
--- a/src/facilities/nursery/childInteract.tw
+++ b/src/facilities/nursery/childInteract.tw
@@ -4,9 +4,13 @@
 	<center><i>[[Cheat Edit Child Alternative|MOD_Edit Child Cheat New][$cheater = 1]]</i></center>
 <</if>>
 
-<<set $nextButton = "Confirm changes", $nextLink = "AC Dump", $returnTo = "Nursery", _SL = $slaves.length, _CL = $cribs.length>>
+<<set _i = App.Facilities.Nursery.childIndexForID($activeChild.ID)>>
+
+<<set $nextButton = "Confirm changes", $nextLink = "AC Dump", $returnTo = "Nursery", _SL = $slaves.length, _CL = $cribs.length, $cribs[_i] = $activeChild>>
+
 <<setLocalPronouns $activeChild>>
 <<run Enunciate($activeChild)>>
+
 /* TODO: the encyclopedia will most likely need to be updated for children as well */
 <<set $encyclopedia = either("Costs Summary", "Disease in the Free Cities", "Drugs and Their Effects", "From Rebellious to Devoted", "Gender", "Independent Slaves", "Modern Anal", "Nymphomania", "Slave Couture")>>
 <<if $activeChild.dick > 0>>
diff --git a/src/facilities/nursery/nurseryWidgets.js b/src/facilities/nursery/nurseryWidgets.js
index 25a4300ddef..56352baeb21 100644
--- a/src/facilities/nursery/nurseryWidgets.js
+++ b/src/facilities/nursery/nurseryWidgets.js
@@ -1855,7 +1855,7 @@ App.Facilities.Nursery.LongInfantDescription = function(child) {
 
 	r += `<span id="childName"><strong><span class="pink">${SlaveFullName(child)}</span></strong></span> `;
 
-	if (def(child.custom)) {
+	if (jsDef(child.custom)) {
 		if (child.custom.label) {
 			r += ` (<strong><span class="yellow">${child.custom.label}</span></strong>) `;
 		}
@@ -1885,7 +1885,7 @@ App.Facilities.Nursery.LongInfantDescription = function(child) {
 
 	r += ` is a ${age} <strong><span class="coral">${title}.</span></strong> ${He} was born in ${arcology.name} ${weeksOwned > 4 ? weeksOwned < 9 ? `about a month` : `${num(weeksOwned)} months` : weeksOwned <= 1 ? `last week` : `${num(weeksOwned)} weeks`}${weeksOwned > 1 ? ` ago` : ``}`;
 
-	if (def(child.counter)) {
+	if (jsDef(child.counter)) {
 		const
 			oral = child.counter.oral,
 			vaginal = child.counter.vaginal,
@@ -2056,7 +2056,7 @@ App.Facilities.Nursery.LongInfantDescription = function(child) {
 		}
 	}
 
-	function def(x) {
+	function jsDef(x) {
 		return typeof x !== "undefined";
 	}
 
@@ -2071,7 +2071,7 @@ App.Facilities.Nursery.LongInfantDescription = function(child) {
 			mfi,
 			ffi;
 
-		if (def(mi)) {
+		if (jsDef(mi)) {
 			mmi = mother.mother;
 			fmi = mother.father;
 		} else if (mother === PC) {
@@ -2079,7 +2079,7 @@ App.Facilities.Nursery.LongInfantDescription = function(child) {
 			fmi = PC.father;
 		}
 
-		if (def(fi)) {
+		if (jsDef(fi)) {
 			mfi = father.mother;
 			ffi = father.father;
 		} else if (father === PC) {
@@ -2088,9 +2088,9 @@ App.Facilities.Nursery.LongInfantDescription = function(child) {
 		}
 
 		// grandparents
-		if (def(mi) || def(fi) && !def(mmi) && !def(fmi) && !def(mfi) && !def(ffi)) {
-			if (def(mi)) {
-				if (def(fi) && mi === fi) {
+		if (jsDef(mi) || jsDef(fi) && !jsDef(mmi) && !jsDef(fmi) && !jsDef(mfi) && !jsDef(ffi)) {
+			if (jsDef(mi)) {
+				if (jsDef(fi) && mi === fi) {
 					if (PC === mother.mother && PC === father.father) {
 						r += `${He} is <span class="lightgreen">your grandchild.</span> You impregnated yourself with ${his} sole biological parent. `;
 					} else if (PC === mother.mother) {
@@ -2098,16 +2098,16 @@ App.Facilities.Nursery.LongInfantDescription = function(child) {
 					} else if (PC === father.father) {
 						r += `${He} is <span class="lightgreen">your grandchild.</span> You fathered ${his} sole biological parent. `;
 					}
-				} else if (def(fi) && PC === mother.mother && PC === father.mother) {
+				} else if (jsDef(fi) && PC === mother.mother && PC === father.mother) {
 					r += `${He} is <span class="lightgreen">your grandchild.</span> You gave birth to both of ${his} parents. `;
-				} else if (def(fi) && PC === mother.father && PC === father.father) {
+				} else if (jsDef(fi) && PC === mother.father && PC === father.father) {
 					r += `${He} is <span class="lightgreen">your grandchild.</span> You fathered both of ${his} parents. `;
 				} else if (PC === mother.mother) {
 					r += `${He} is <span class="lightgreen">your grandchild.</span> You gave birth to ${his} mother. `;
 				} else if (PC === mother.father) {
 					r += `${He} is <span class="lightgreen">your grandchild.</span> You fathered ${his} mother. `;
 				}
-			} else if (def(fi)) {
+			} else if (jsDef(fi)) {
 				if (PC === father.mother) {
 					r += `${He} is <span class="lightgreen">your grandchild.</span> You gave birth to ${his} father. `;
 				} else if (PC === father.father) {
@@ -2115,28 +2115,28 @@ App.Facilities.Nursery.LongInfantDescription = function(child) {
 				}
 			}
 		} else {
-			if (def(mmi) && def(ffi) && mmi === ffi) {
+			if (jsDef(mmi) && jsDef(ffi) && mmi === ffi) {
 				r += `${His} sole granparent is ${mmi.slaveName}.</span> `;
 			} else {
-				if (def(mmi) && def(mfi) && mmi === mfi) {
-					if (def(mmi)) {
+				if (jsDef(mmi) && jsDef(mfi) && mmi === mfi) {
+					if (jsDef(mmi)) {
 						r += `${His} sole <span class="lightgreen">grandparent is ${mmi}.</span> `;
 					}
 				} else {
-					if (def(mmi)) {
+					if (jsDef(mmi)) {
 						r += `${His} maternal <span class="lightgreen">grandmother is ${mmi.slaveName}.</span> `;
 					}
-					if (def(mfi)) {
+					if (jsDef(mfi)) {
 						r += `${His} paternal <span class="lightgreen">grandmother is ${mfi.slaveName}.</span> `;
 					}
 				}
-				if (def(fmi) && def(ffi) && fmi === ffi) {
+				if (jsDef(fmi) && jsDef(ffi) && fmi === ffi) {
 					r += `${His} sole <span class="lightgreen">grandparent is ${ffi}.</span> `;
 				} else {
-					if (def(fmi)) {
+					if (jsDef(fmi)) {
 						r += `${His} maternal <span class="lightgreen">grandfather is ${fmi.slaveName}.</span> `;
 					}
-					if (def(ffi)) {
+					if (jsDef(ffi)) {
 						r += `${His} paternal <span class="lightgreen">grandfather is ${ffi.slaveName}.</span> `;
 					}
 				}
@@ -10816,9 +10816,9 @@ App.Facilities.Nursery.LongChildDescription = function(child) {
 		let
 			r = ``;
 
-		if (def(child)) {
+		if (jsDef(child)) {
 			Enunciate(child);
-		} else if (!def(V.titleEnunciate)) {
+		} else if (!jsDef(V.titleEnunciate)) {
 			Enunciate(child);
 		}
 
@@ -14237,7 +14237,7 @@ App.Facilities.Nursery.LongChildDescription = function(child) {
 				mfi,
 				ffi;
 
-			if (def(mi)) {
+			if (jsDef(mi)) {
 				mmi = mother.mother;
 				fmi = mother.father;
 			} else if (mother === PC) {
@@ -14245,7 +14245,7 @@ App.Facilities.Nursery.LongChildDescription = function(child) {
 				fmi = PC.father;
 			}
 
-			if (def(fi)) {
+			if (jsDef(fi)) {
 				mfi = father.mother;
 				ffi = father.father;
 			} else if (father === PC) {
@@ -14254,9 +14254,9 @@ App.Facilities.Nursery.LongChildDescription = function(child) {
 			}
 
 			// grandparents
-			if (def(mi) || def(fi) && !def(mmi) && !def(fmi) && !def(mfi) && !def(ffi)) {
-				if (def(mi)) {
-					if (def(fi) && mi === fi) {
+			if (jsDef(mi) || jsDef(fi) && !jsDef(mmi) && !jsDef(fmi) && !jsDef(mfi) && !jsDef(ffi)) {
+				if (jsDef(mi)) {
+					if (jsDef(fi) && mi === fi) {
 						if (PC === mother.mother && PC === father.father) {
 							r += `${He} is <span class="lightgreen">your grandchild.</span> You impregnated yourself with ${his} sole biological parent. `;
 						} else if (PC === mother.mother) {
@@ -14264,16 +14264,16 @@ App.Facilities.Nursery.LongChildDescription = function(child) {
 						} else if (PC === father.father) {
 							r += `${He} is <span class="lightgreen">your grandchild.</span> You fathered ${his} sole biological parent. `;
 						}
-					} else if (def(fi) && PC === mother.mother && PC === father.mother) {
+					} else if (jsDef(fi) && PC === mother.mother && PC === father.mother) {
 						r += `${He} is <span class="lightgreen">your grandchild.</span> You gave birth to both of ${his} parents. `;
-					} else if (def(fi) && PC === mother.father && PC === father.father) {
+					} else if (jsDef(fi) && PC === mother.father && PC === father.father) {
 						r += `${He} is <span class="lightgreen">your grandchild.</span> You fathered both of ${his} parents. `;
 					} else if (PC === mother.mother) {
 						r += `${He} is <span class="lightgreen">your grandchild.</span> You gave birth to ${his} mother. `;
 					} else if (PC === mother.father) {
 						r += `${He} is <span class="lightgreen">your grandchild.</span> You fathered ${his} mother. `;
 					}
-				} else if (def(fi)) {
+				} else if (jsDef(fi)) {
 					if (PC === father.mother) {
 						r += `${He} is <span class="lightgreen">your grandchild.</span> You gave birth to ${his} father. `;
 					} else if (PC === father.father) {
@@ -14281,28 +14281,28 @@ App.Facilities.Nursery.LongChildDescription = function(child) {
 					}
 				}
 			} else {
-				if (def(mmi) && def(ffi) && mmi === ffi) {
+				if (jsDef(mmi) && jsDef(ffi) && mmi === ffi) {
 					r += `${His} sole granparent is ${mmi.slaveName}.</span> `;
 				} else {
-					if (def(mmi) && def(mfi) && mmi === mfi) {
-						if (def(mmi)) {
+					if (jsDef(mmi) && jsDef(mfi) && mmi === mfi) {
+						if (jsDef(mmi)) {
 							r += `${His} sole <span class="lightgreen">grandparent is ${mmi}.</span> `;
 						}
 					} else {
-						if (def(mmi)) {
+						if (jsDef(mmi)) {
 							r += `${His} maternal <span class="lightgreen">grandmother is ${mmi.slaveName}.</span> `;
 						}
-						if (def(mfi)) {
+						if (jsDef(mfi)) {
 							r += `${His} paternal <span class="lightgreen">grandmother is ${mfi.slaveName}.</span> `;
 						}
 					}
-					if (def(fmi) && def(ffi) && fmi === ffi) {
+					if (jsDef(fmi) && jsDef(ffi) && fmi === ffi) {
 						r += `${His} sole <span class="lightgreen">grandparent is ${ffi}.</span> `;
 					} else {
-						if (def(fmi)) {
+						if (jsDef(fmi)) {
 							r += `${His} maternal <span class="lightgreen">grandfather is ${fmi.slaveName}.</span> `;
 						}
-						if (def(ffi)) {
+						if (jsDef(ffi)) {
 							r += `${His} paternal <span class="lightgreen">grandfather is ${ffi.slaveName}.</span> `;
 						}
 					}
@@ -14550,7 +14550,7 @@ App.Facilities.Nursery.LongChildDescription = function(child) {
 
 		if (child.relationship >= 3 && totalRelatives(child) > 0) {
 			const lover = getSlave(child.relationshipTarget);
-			if (def(lover)) {
+			if (jsDef(lover)) {
 				if (child.mother === lover.ID) {
 					r += `${He} is in an <span class="lightgreen">incestuous relationship with ${his} mother, ${SlaveFullName(lover)}.</span> `;
 				} else if (child.father === lover.ID) {
@@ -14603,7 +14603,7 @@ App.Facilities.Nursery.LongChildDescription = function(child) {
 	} else {
 		if (child.relation !== 0) {
 			let lcd = V.slaveIndices[child.relationTarget];
-			if (def(lcd)) {
+			if (jsDef(lcd)) {
 				if (slaves[lcd].ID === child.relationshipTarget && child.relationship >= 3) {
 					r += `${He} is <span class="lightgreen">${SlaveFullName(slaves[lcd])}'s ${child.relation}, making their relationship incestuous.</span> `;
 				} else {
@@ -14617,7 +14617,7 @@ App.Facilities.Nursery.LongChildDescription = function(child) {
 
 	if (child.rivalry) {
 		let lcd = V.slaveIndices[child.rivalryTarget];
-		if (def(lcd)) {
+		if (jsDef(lcd)) {
 			r += `${He} `;
 			if (child.rivalry <= 1) {
 				r += `<span class="lightsalmon">dislikes</span> ${SlaveFullName(slaves[Lcd])}. `;
@@ -14730,7 +14730,7 @@ App.Facilities.Nursery.LongChildDescription = function(child) {
 		r += `a <span class=aquamarine>masterful whore.</span> `;
 	}
 
-	if (child.custom.desc !== "" && def(child.custom.desc)) {
+	if (child.custom.desc !== "" && jsDef(child.custom.desc)) {
 		r += `${child.custom.desc} `;
 	}
 
@@ -15248,7 +15248,7 @@ App.Facilities.Nursery.LongChildDescription = function(child) {
 		r += piercings.ears(child);
 		r += piercings.nose(child);
 		r += piercings.eyebrows(child);
-		if (child.custom.tattoo !== "" && def(child.custom.tattoo)) {
+		if (child.custom.tattoo !== "" && jsDef(child.custom.tattoo)) {
 			r += `${child.custom.tattoo}`;
 		}
 	}
@@ -16065,7 +16065,7 @@ App.Facilities.Nursery.nameChild = function nameChild(child) {
 	}
 	if (mother !== PC && mother !== 0) {
 		App.Utils.setLocalPronouns(mother, 2);
-		if (def(V.Concubine) && mother.ID === V.Concubine.ID) {
+		if (jsDef(V.Concubine) && mother.ID === V.Concubine.ID) {
 			r += `<br>
 			<<link "Permit your Concubine to name _his2 daughter">>
 				<<replace "#naming">>`;
@@ -16083,7 +16083,7 @@ App.Facilities.Nursery.nameChild = function nameChild(child) {
 					`After some careful consideration, ${mother.slaveName} picks a name suitable for your daughter; from now on ${he2} will be known as "${child.slaveName}".
 				<</replace>>
 			<</link>>`;
-		} else if (def(V.Bodyguard) && mother.ID === V.Bodyguard.ID) {
+		} else if (jsDef(V.Bodyguard) && mother.ID === V.Bodyguard.ID) {
 			r += `<br>
 			<<link "Permit your bodyguard to name ${his2} daughter">>
 				<<replace "#naming">>`;
@@ -16092,7 +16092,7 @@ App.Facilities.Nursery.nameChild = function nameChild(child) {
 					`After some careful consideration, ${mother.slaveName} decides on "${child.slaveName}" for ${his2} daughter. ${He2} hopes you'll find it fitting ${his} station.
 				<</replace>>
 			<</link>>`;
-		} else if (def(V.Headgirl) && mother.ID === V.Headgirl.ID) {
+		} else if (jsDef(V.Headgirl) && mother.ID === V.Headgirl.ID) {
 			r += `<br>
 			<<link "Permit your Head Girl to name ${his2} daughter">>
 				<<replace "#naming">>`;
@@ -16114,7 +16114,7 @@ App.Facilities.Nursery.nameChild = function nameChild(child) {
 	}
 	if (father !== PC && father !== 0 && father !== mother) {
 		App.Utils.setLocalPronouns(father, 2);
-		if (def(V.Concubine) && father.ID === V.Concubine.ID) {
+		if (jsDef(V.Concubine) && father.ID === V.Concubine.ID) {
 			r += `<br>
 			<<link "Permit your Concubine to name ${his2} daughter">>
 				<<replace "#naming">>`;
@@ -16132,7 +16132,7 @@ App.Facilities.Nursery.nameChild = function nameChild(child) {
 					`After some careful consideration, ${father.slaveName} picks a name suitable for your daughter; from now on ${he} will be known as "${child.slaveName}".
 				<</replace>>
 			<</link>>`;
-		} else if (def(V.Bodyguard) && father.ID === V.Bodyguard.ID) {
+		} else if (jsDef(V.Bodyguard) && father.ID === V.Bodyguard.ID) {
 			r += `<br>
 			<<link "Permit your bodyguard to name ${his2} daughter">>
 				<<replace "#naming">>`;
@@ -16141,7 +16141,7 @@ App.Facilities.Nursery.nameChild = function nameChild(child) {
 					`After some careful consideration, ${father.slaveName} decides on "${child.slaveName}" for ${his2} daughter. ${He2} hopes you'll find it fitting ${his} station.
 				<</replace>>
 			<</link>>`;
-		} else if (def(V.Headgirl) && father.ID === V.Headgirl.ID) {
+		} else if (jsDef(V.Headgirl) && father.ID === V.Headgirl.ID) {
 			r += `<br>
 			<<link "Permit your Head Girl to name ${his2} daughter">>
 				<<replace "#naming">>`;
@@ -16167,7 +16167,7 @@ App.Facilities.Nursery.nameChild = function nameChild(child) {
 
 /**
  * Creates a new child object
- * @param {App.Entity.SlaveState} child
+ * @param {App.Entity.ChildState} child
  */
 App.Facilities.Nursery.newChild = function newChild(child) {
 	const V = State.variables;
@@ -16250,6 +16250,23 @@ App.Facilities.Nursery.newChild = function newChild(child) {
 	V.nurseryBabies++;
 };
 
+/**
+ * @param {App.Entity.ChildState[]} [cribs]
+ * @returns {Object.<number, number>}
+ */
+App.Facilities.Nursery.cribsToIndices = function cribsToIndices(cribs = State.variables.cribs) {
+	return cribs.reduce((acc, child, i) => { acc[child.ID] = i; return acc; }, {});
+};
+
+/**
+ * Returns index in the children array for the given ID
+ * @param {number} ID child's ID
+ * @returns {number}
+ */
+App.Facilities.Nursery.childIndexForID = function childIndexForID(id) {
+	return State.variables.cribsIndices[id];
+};
+
 /**
  * Removes the child using the child's ID
  * @param {number} index
@@ -16340,7 +16357,7 @@ App.Facilities.Nursery.nurserySort = function nurserySort() {
 								r += `someone's, though it is too early to tell whose,`;
 							} else {
 								let t = V.slaveIndices[slave.pregSource];
-								if (def(t)) {
+								if (jsDef(t)) {
 									r += `${V.slaves[t].slaveName}'s`;
 								}
 							}
@@ -18732,7 +18749,7 @@ App.Facilities.Nursery.childrenReport = function childrenReport() {
 				let
 					chance = jsRandom(1, 100);
 
-				if (def(Matron)) {
+				if (jsDef(Matron)) {
 					if (Matron.fetish !== "none") {
 						if (chance > 90) {
 							if (child.fetish === "none") {
@@ -18891,7 +18908,7 @@ App.Facilities.Nursery.childrenReport = function childrenReport() {
 				}
 
 				// TODO: add education system
-				if (def(Matron) && Matron) {
+				if (jsDef(Matron) && Matron) {
 					const {
 						// eslint-disable-next-line no-unused-vars
 						he2, him2, his2, hers2, himself2, boy2, He2, His2
@@ -18941,7 +18958,7 @@ App.Facilities.Nursery.childrenReport = function childrenReport() {
 				}
 
 				// TODO: add fitness system
-				if (def(Matron) && Matron) {
+				if (jsDef(Matron) && Matron) {
 					// TODO:
 				}
 
diff --git a/src/init/storyInit.tw b/src/init/storyInit.tw
index 9d843d7f981..570d805902f 100644
--- a/src/init/storyInit.tw
+++ b/src/init/storyInit.tw
@@ -84,6 +84,7 @@ You should have received a copy of the GNU General Public License along with thi
 		<</if>>
 	<</for>>
 	<<set $slaveIndices = slaves2indices()>>
+	<<set $cribsIndices = App.Facilities.Nursery.cribsToIndices()>>
 	<<for _k = 0; _k < _SL; _k++>>
 		<<for _i = 0; _i < _SL; _i++>>
 			<<if $slaves[_k].mother == $slaves[_i].ID || $slaves[_k].father == $slaves[_i].ID>>
diff --git a/src/uncategorized/BackwardsCompatibility.tw b/src/uncategorized/BackwardsCompatibility.tw
index 1f6cac50fa6..fcd9e6a9bdf 100644
--- a/src/uncategorized/BackwardsCompatibility.tw
+++ b/src/uncategorized/BackwardsCompatibility.tw
@@ -3519,6 +3519,7 @@ Done<br>
 			<<run App.Facilities.Nursery.ChildDatatypeCleanup(_Child)>>
 		<</if>>
 	<</for>>
+	<<set $cribsIndices = App.Facilities.Nursery.cribsToIndices()>>
 	Done<br>
 <</if>>
 
-- 
GitLab