From 70cf1a5e3c99c1b2dfa56c5c44e475420e9825d6 Mon Sep 17 00:00:00 2001
From: Svornost <11434-svornost@users.noreply.gitgud.io>
Date: Sun, 7 Feb 2021 17:00:06 -0800
Subject: [PATCH] Fixes and cleanup for superfetation litter data.

---
 src/js/wombJS.js                           | 72 +++++++++-------------
 src/npc/descriptions/womb/pregnancy.js     | 12 +---
 src/npc/descriptions/womb/superfetation.js |  2 +-
 3 files changed, 34 insertions(+), 52 deletions(-)

diff --git a/src/js/wombJS.js b/src/js/wombJS.js
index 21245d278d7..5374dc38709 100644
--- a/src/js/wombJS.js
+++ b/src/js/wombJS.js
@@ -890,55 +890,43 @@ globalThis.WombCleanAllReserve = function(actor) {
 	});
 };
 
-/*
-Function return object with data about litters in actor womb. This data can be used for descriptions of pregnancy with complicated structure. What it contain:
-
-data.litters.length = summary count of separate litters in the womb.
-data.litters[x] = age (.realAge) of litter "x".
-data.countLitter[x] = count of fetuses in "x" litter.
-
-data.litterData[x] = contain array with actual fetuses that belong to a litter "x". Can be used to check anything related to fetus. (This is not copy, but reference to actual fetuses, so be careful with changes of this array).
-
-Sample of usage in SugarScript:
----
-<<set _wd = WombGetLittersData(_slave)>>
-$He is _wd.litters[0] weeks pregnant with $his first set of _wd.countLitter[0] children<<if _wd.litters > 1>>, _wd.litters[1] weeks along with $his second set<</if>><<if _wd.litters > 2>>, _wd.litters[2] and _wd.litters[2] weeks along with $his third<</if>>.
-In summary $he carry _wd.litters.length separate sets of children. $His most progressed fetus of second pregnancy is already reached _wd.litterData[1][0].age biological week of gestation.
----
-*/
+/** Return object with data about litters in actor womb. This data can be used for descriptions of pregnancy with complicated structure.
+ * @param {FC.HumanState} actor
+ * @returns {{litters: Array<number>, litterData: Array<Array<App.Entity.Fetus>>}}
+ *
+ * Return contents:
+ * - data.litters.length = summary count of separate litters in the womb.
+ * - data.litters[x] = age (.realAge) of litter "x".
+ * - data.litterData[x].length = count of fetuses in "x" litter.
+ * - data.litterData[x] = array with actual fetuses that belong to a litter "x". Can be used to check anything related to fetus. (This is not a copy, but a reference to actual fetuses, so be careful with changes).
+ *
+ * Sample of usage in SugarScript:
+ * ---
+ * <<set _wd = WombGetLittersData(_slave)>>
+ * $He is _wd.litters[0] weeks pregnant with $his first set of _wd.litterData[0].length children<<if _wd.litters > 1>>, _wd.litters[1] weeks along with $his second set<</if>><<if _wd.litters > 2>>, _wd.litters[2] and _wd.litters[2] weeks along with $his third<</if>>.
+ * In summary $he carry _wd.litters.length separate sets of children. $His most progressed fetus of second pregnancy is already reached _wd.litterData[1][0].age biological week of gestation.
+ * ---
+ */
 globalThis.WombGetLittersData = function(actor) {
-	let data = {};
 	let unicLiters = []; // array with realAges of separate litters.
-	let countLitter = [];
 	let litterData = [];
-	let tmp;
 
-	// in first place we need to know how many litters here (Assuming that unique litter is have similar .realAge). Also we will know their ages.
-	actor.womb.forEach(function(ft) {
-		if (!unicLiters.includes(Math.ceil(ft.realAge))) {
-			unicLiters.push(Math.ceil(ft.realAge));
+	// we need to know how many litters here (Assuming that unique litters have similar .realAge). Also we will know their ages.
+	for (const ft of actor.womb) {
+		const age = Math.ceil(ft.realAge);
+		if (!unicLiters.includes(age)) {
+			unicLiters.push(age);
+			litterData.push(actor.womb.filter(f2 => Math.ceil(f2.realAge) === age));
 		}
-	});
-
-	// now we should find and store separate litters data (count of fetuses):
-	unicLiters.forEach(function(litter) {
-		tmp = actor.womb.filter(ft => Math.ceil(ft.realAge) === litter);
-		countLitter.push(tmp.length);
-		litterData.push(tmp);
-	});
-
-	data.litters = unicLiters;
-	data.countLitter = countLitter;
-	data.litterData = litterData;
+	}
 
-	return data;
+	return {litters: unicLiters, litterData: litterData};
 };
 
-// simple function used for splitting actual size from total size due to polyhydramnios.
+/** simple function used for splitting actual size from total size due to polyhydramnios.
+ * @param {FC.HumanState} actor
+ * @returns {number}
+ */
 globalThis.WombGetFetalSizeSum = function(actor) {
-	let sum = 0;
-
-	actor.womb.forEach((ft) => sum += ft.volume);
-
-	return sum;
+	return actor.womb.reduce((acc, cur) => acc + cur.volume, 0);
 };
diff --git a/src/npc/descriptions/womb/pregnancy.js b/src/npc/descriptions/womb/pregnancy.js
index 348d5a213c1..f0a2a774782 100644
--- a/src/npc/descriptions/womb/pregnancy.js
+++ b/src/npc/descriptions/womb/pregnancy.js
@@ -786,19 +786,13 @@ App.Desc.pregnancy = function(slave, {market, eventDescription} = {}) {
 		const slaveWD = WombGetLittersData(slave);
 		if (slave.geneticQuirks.superfetation === 2 && slaveWD.litters.length > 1) {
 			if (V.pregnancyMonitoringUpgrade !== 1) {
-				let sameFather = 1;
 				const sameFatherID = slaveWD.litterData[0][0].fatherID;
-				for (let litCount = 0; litCount < slaveWD.litters.length; litCount++) {
-					if (slaveWD.litterData[litCount][litCount] !== sameFatherID) {
-						sameFather = 0;
-						break;
-					}
-				}
+				const sameFather = slaveWD.litterData.every(l => l[0].fatherID === sameFatherID);
 				r.push(`${His} superfetation has resulted in multiple simultaneous pregnancies;`);
-				if (sameFather !== 1) {
+				if (!sameFather) {
 					r.push(`tests report multiple different sources.`);
 				} else {
-					if (slaveWD.litterData[slaveWD.litters.length-1][0].age > slave.pregData.normalBirth / 8) {
+					if (slaveWD.litterData[slaveWD.litters.length-1][0].age <= slave.pregData.normalBirth / 8) {
 						r.push(`all of them too young to tell the father of.`);
 					} else if (slave.pregSource === -7) {
 						r.push(`all of them modified children from the gene lab.`);
diff --git a/src/npc/descriptions/womb/superfetation.js b/src/npc/descriptions/womb/superfetation.js
index 18bcf150675..7dfeee48129 100644
--- a/src/npc/descriptions/womb/superfetation.js
+++ b/src/npc/descriptions/womb/superfetation.js
@@ -16,7 +16,7 @@ App.Desc.superfetation = function(slave, {market, eventDescription} = {}) {
 	if (slave.geneticQuirks.superfetation === 2 && slaveWD.litters.length > 1 && V.pregnancyMonitoringUpgrade === 1 && !market) {
 		r.push(`${His} womb contains ${num(slaveWD.litters.length)} separate pregnancies:`);
 		for (let litCount = 0; litCount < slaveWD.litters.length; litCount++) {
-			const countLitter = slaveWD.countLitter[litCount];
+			const countLitter = slaveWD.litterData[litCount].length;
 			const is = countLitter > 1 ? "are" : "is";
 			const was = countLitter > 1 ? "were" : "was";
 			if (litCount === 0) {
-- 
GitLab