From 724607f6d205c26471f254eb77c8d6802963cf43 Mon Sep 17 00:00:00 2001
From: lowercasedonkey <lowercasedonkey@gmail.com>
Date: Sun, 16 Aug 2020 17:31:58 -0400
Subject: [PATCH] move loop into function

---
 src/facilities/incubator/incubatorUtils.js | 30 ++++++++++------------
 src/js/birth.js                            | 25 ++++++++++++++++++
 src/npc/interaction/passage/birthStorm.tw  | 15 +----------
 src/npc/interaction/passage/csec.tw        | 16 +-----------
 src/pregmod/seBurst.tw                     | 14 +---------
 src/pregmod/sePlayerBirth.tw               |  8 ++++--
 src/pregmod/widgets/seBirthWidgets.tw      | 14 +---------
 7 files changed, 49 insertions(+), 73 deletions(-)
 create mode 100644 src/js/birth.js

diff --git a/src/facilities/incubator/incubatorUtils.js b/src/facilities/incubator/incubatorUtils.js
index 9c04341f013..2765e9e43e2 100644
--- a/src/facilities/incubator/incubatorUtils.js
+++ b/src/facilities/incubator/incubatorUtils.js
@@ -3,21 +3,19 @@
  * @param {App.Entity.SlaveState|App.Entity.InfantState} child
  */
 App.Facilities.Incubator.newChild = function(child) {
-	if (V.tanks.length < V.incubator) {
-		let fullAdapt;
-		child.growTime = Math.trunc(V.targetAge * 52);
-		child.incubatorPregAdaptationPower = V.incubatorPregAdaptationPower;
-		if (V.incubatorPregAdaptationPower === 1) {
-			fullAdapt = 45000 / 2000;
-		} else if (V.incubatorPregAdaptationPower === 2) {
-			fullAdapt = 100000 / 2000;
-		} else if (V.incubatorPregAdaptationPower === 3) {
-			fullAdapt = 150000 / 2000;
-		} else {
-			fullAdapt = 15000 / 2000;
-		}
-		child.incubatorPregAdaptationInWeek = (fullAdapt - child.pregAdaptation) / child.growTime;
-		V.tanks.push(child);
-		V.incubatorSlaves++;
+	let fullAdapt;
+	child.growTime = Math.trunc(V.targetAge * 52);
+	child.incubatorPregAdaptationPower = V.incubatorPregAdaptationPower;
+	if (V.incubatorPregAdaptationPower === 1) {
+		fullAdapt = 45000 / 2000;
+	} else if (V.incubatorPregAdaptationPower === 2) {
+		fullAdapt = 100000 / 2000;
+	} else if (V.incubatorPregAdaptationPower === 3) {
+		fullAdapt = 150000 / 2000;
+	} else {
+		fullAdapt = 15000 / 2000;
 	}
+	child.incubatorPregAdaptationInWeek = (fullAdapt - child.pregAdaptation) / child.growTime;
+	V.tanks.push(child);
+	V.incubatorSlaves++;
 };
diff --git a/src/js/birth.js b/src/js/birth.js
new file mode 100644
index 00000000000..2d7666b525b
--- /dev/null
+++ b/src/js/birth.js
@@ -0,0 +1,25 @@
+/**
+ * Sends newborns to incubator or nursery
+ * @param {App.Entity.SlaveState} mom
+ */
+globalThis.sendNewbornsToFacility = function(mom) {
+	let curBabies = mom.curBabies.length;
+	for (let cb = 0; cb < curBabies; cb++) {
+		// if there is no reserved children, code in loop will not trigger
+		if (mom.curBabies[cb].reserve === "incubator") {
+			if (V.tanks.length < V.incubator) {
+				App.Facilities.Incubator.newChild(generateChild(mom, mom.curBabies[cb], true));
+			}
+			mom.curBabies.splice(mom.curBabies[cb], 1);
+			cb--;
+			curBabies--;
+		} else if (mom.curBabies[cb].reserve === "nursery") {
+			if (V.cribs.length < V.nursery) {
+				App.Facilities.Nursery.newChild(generateChild(mom, mom.curBabies[cb]));
+			}
+			mom.curBabies.splice(mom.curBabies[cb], 1);
+			cb--;
+			curBabies--;
+		}
+	}
+};
diff --git a/src/npc/interaction/passage/birthStorm.tw b/src/npc/interaction/passage/birthStorm.tw
index 7585fc43abf..8b796a8500b 100644
--- a/src/npc/interaction/passage/birthStorm.tw
+++ b/src/npc/interaction/passage/birthStorm.tw
@@ -208,21 +208,8 @@ The remote surgery allows the removal of the pregnancy generator through convent
 		<<set _nursed = 1>>
 	<</if>>
 
-	<<set $mom = getSlave($AS)>>
 	<<if _cToIncub > 0 || _cToNursery > 0>>
-		<<for _cb = 0; _cb < _curBabies; _cb++>> /* if there is no reserved children, code in loop will not trigger */
-			<<if $mom.curBabies[_cb].reserve == "incubator">>
-				<<run App.Facilities.Incubator.newChild(generateChild($mom, $mom.curBabies[_cb], 1))>>
-				<<run $mom.curBabies.splice($mom.curBabies[_cb], 1)>>
-				<<set _cb--, _curBabies-->>
-			<<elseif $mom.curBabies[_cb].reserve == "nursery">>
-				<<if $cribs.length < $nursery>>
-					<<run App.Facilities.Nursery.newChild(generateChild($mom, $mom.curBabies[_cb]))>>
-				<</if>>
-				<<run $mom.curBabies.splice($mom.curBabies[_cb], 1)>>
-				<<set _cb--, _curBabies-->>
-			<</if>>
-		<</for>>
+		<<run sendNewbornsToFacility(getSlave($AS))>>
 	<</if>>
 
 <<else>>
diff --git a/src/npc/interaction/passage/csec.tw b/src/npc/interaction/passage/csec.tw
index b63b50ea618..eda9862a2c6 100644
--- a/src/npc/interaction/passage/csec.tw
+++ b/src/npc/interaction/passage/csec.tw
@@ -179,23 +179,9 @@ Performing a cesarean section is trivial for the remote surgery to carry out. <<
 		<<set _nursed = 1>>
 	<</if>>
 
-	<<set $mom = getSlave($AS)>>
 	<<if _cToIncub > 0 || _cToNursery > 0>>
-		<<for _cb = 0; _cb < _curBabies; _cb++>> /* if there is no reserved children, code in loop will not trigger */
-			<<if $mom.curBabies[_cb].reserve == "incubator">>
-				<<run App.Facilities.Incubator.newChild(generateChild($mom, $mom.curBabies[_cb], 1))>>
-				<<run $mom.curBabies.splice($mom.curBabies[_cb], 1)>>
-				<<set _cb--, _curBabies-->>
-			<<elseif $mom.curBabies[_cb].reserve == "nursery">>
-				<<if $cribs.length < $nursery>>
-					<<run App.Facilities.Nursery.newChild(generateChild($mom, $mom.curBabies[_cb]))>>
-				<</if>>
-				<<run $mom.curBabies.splice($mom.curBabies[_cb], 1)>>
-				<<set _cb--, _curBabies-->>
-			<</if>>
-		<</for>>
+		<<run sendNewbornsToFacility(getSlave($AS))>>
 	<</if>>
-	<<set $AS = $mom.ID>>
 
 <<else>>
 	/*No live babies. Placeholder */
diff --git a/src/pregmod/seBurst.tw b/src/pregmod/seBurst.tw
index f5abd391987..801b01f2878 100644
--- a/src/pregmod/seBurst.tw
+++ b/src/pregmod/seBurst.tw
@@ -173,19 +173,7 @@
 			<</if>>
 
 			<<if _cToIncub > 0 || _cToNursery > 0>>
-				<<for _cb = 0; _cb < _curBabies; _cb++>> /* if there is no reserved children, code in loop will not trigger */
-					<<if $slaves[_b].curBabies[_cb].reserve == "incubator">>
-						<<run App.Facilities.Incubator.newChild(generateChild($slaves[_b], $slaves[_b].curBabies[_cb], 1))>>
-						<<run $slaves[_b].curBabies.splice($slaves[_b].curBabies[_cb], 1)>>
-						<<set _cb--, _curBabies-->>
-					<<elseif $slaves[_b].curBabies[_cb].reserve == "nursery">>
-						<<if $cribs.length < $nursery>>
-							<<run App.Facilities.Nursery.newChild(generateChild($slaves[_b], $slaves[_b].curBabies[_cb]))>>
-						<</if>>
-						<<run $slaves[_b].curBabies.splice($slaves[_b].curBabies[_cb], 1)>>
-						<<set _cb--, _curBabies-->>
-					<</if>>
-				<</for>>
+				<<run sendNewbornsToFacility($slaves[_b])>>
 			<</if>>
 		<<else>>
 			life and <<if _curBabies <= 1>>that of $his child<<else>>those of $his children<</if>>.
diff --git a/src/pregmod/sePlayerBirth.tw b/src/pregmod/sePlayerBirth.tw
index 7d2df1d3f47..c9a587cbbe9 100644
--- a/src/pregmod/sePlayerBirth.tw
+++ b/src/pregmod/sePlayerBirth.tw
@@ -378,7 +378,9 @@ You arrange yourself to give birth, relaxing until your body urges you to begin
 
 		<<if $PC.curBabies[0].reserve === "incubator">>
 			@@.pink;You set <<if _gender == "XX">>her<<else>>him<</if>> aside for incubation.@@
-			<<run App.Facilities.Incubator.newChild(generateChild($PC, $PC.curBabies[0], 1))>>
+			<<if $tanks.length < $incubator>>
+				<<run App.Facilities.Incubator.newChild(generateChild($PC, $PC.curBabies[0], true))>>
+			<</if>>
 		<<elseif $PC.curBabies[0].reserve === "nursery">>
 			@@.pink;You set <<if _gender == "XX">>her<<else>>him<</if>> aside for incubation.@@
 			<<if $cribs.length < $nursery>>
@@ -428,7 +430,9 @@ You arrange yourself to give birth, relaxing until your body urges you to begin
 			<</if>>
 			<<if $PC.curBabies[_p].reserve == "incubator">>
 				@@.pink;You set <<if $PC.curBabies[_p].genetics.gender == "XX">>her<<else>>him<</if>> aside for incubation.@@
-				<<run App.Facilities.Incubator.newChild(generateChild($PC, $PC.curBabies[_p], 1))>>
+				<<if $tanks.length < $incubator>>
+					<<run App.Facilities.Incubator.newChild(generateChild($PC, $PC.curBabies[_p], true))>>
+				<</if>>
 				<<run $PC.curBabies.splice($PC.curBabies[_p], 1)>>
 				<<set _p--, _curBabies-->>
 			<<elseif $PC.curBabies[_p].reserve == "nursery">>
diff --git a/src/pregmod/widgets/seBirthWidgets.tw b/src/pregmod/widgets/seBirthWidgets.tw
index 74a35b9535e..1fd091c6899 100644
--- a/src/pregmod/widgets/seBirthWidgets.tw
+++ b/src/pregmod/widgets/seBirthWidgets.tw
@@ -894,19 +894,7 @@ All in all,
 	<br><br>
 	<<if _curBabies > 1>>Of $his _curBabies child<<if _curBabies > 1>>ren<</if>>,<<else>>$His child<</if>> <<if _cToIncub > 0>><<if _curBabies > 1>>_cToIncub <</if>><<if _cToIncub === 1>>was<<else>>were<</if>> taken to $incubatorName<<if _cToNursery > 0>> and <</if>><</if>><<if _cToNursery > 0>><<if _curBabies > 1>>_cToNursery <</if>><<if _cToNursery === 1>>was<<else>>were<</if>> taken to $nurseryName<</if>>.
 	<<if _cToIncub + _cToNursery > 0>>
-		<<for _cb = 0; _cb < _curBabies; _cb++>> /* if there are no reserved children, code in loop will not trigger */
-			<<if $slaves[$i].curBabies[_cb].reserve == "incubator">>
-				<<run App.Facilities.Incubator.newChild(generateChild($slaves[$i], $slaves[$i].curBabies[_cb], 1))>>
-				<<run $slaves[$i].curBabies.splice($slaves[$i].curBabies[_cb], 1)>>
-				<<set _cb--, _curBabies-->>
-			<<elseif $slaves[$i].curBabies[_cb].reserve == "nursery">>
-				<<if $cribs.length < $nursery>>
-					<<run App.Facilities.Nursery.newChild(generateChild($slaves[$i], $slaves[$i].curBabies[_cb]))>>
-				<</if>>
-				<<run $slaves[$i].curBabies.splice($slaves[$i].curBabies[_cb], 1)>>
-				<<set _cb--, _curBabies-->>
-			<</if>>
-		<</for>>
+		<<run sendNewbornsToFacility($slaves[$i])>>
 	<</if>>
 
 	<<set _curBabies = $slaves[$i].curBabies.length>>
-- 
GitLab