From 15dbe061f8a3449ded8af75aa22768a9a9ed3bc2 Mon Sep 17 00:00:00 2001
From: Svornost <11434-svornost@users.noreply.gitgud.io>
Date: Sun, 8 Dec 2019 22:02:41 -0800
Subject: [PATCH] Don't directly iterate the slavelist from slaveInteract in
 twinescript; do it in Javascript instead.

---
 src/facilities/nursery/childInteract.tw | 70 ++++++--------------
 src/js/extendedFamilyModeJS.js          | 35 ++++++++++
 src/uncategorized/slaveInteract.tw      | 87 +++++++++----------------
 3 files changed, 85 insertions(+), 107 deletions(-)

diff --git a/src/facilities/nursery/childInteract.tw b/src/facilities/nursery/childInteract.tw
index 5e516713c9a..b860288de25 100644
--- a/src/facilities/nursery/childInteract.tw
+++ b/src/facilities/nursery/childInteract.tw
@@ -347,41 +347,28 @@ FIXME:
 	<</link>>
 	<<if $seeIncest == 1>>
 		<<if $familyTesting == 1>>
-			<<for $i = 0; $i < _SL; $i++>>
-				<<if $activeChild.mother == $slaves[$i].ID>>
-					<<if isSlaveAvailable($slaves[$i])>>
-					|	<<link "Fuck $him with $his mother">>
-							<<replace "#miniscene">>
-								<<set $partner = "mother">>
-								<<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>>
-						<</link>>
-					<<else>>
-						//$His mother, $slaves[$i].slaveName, is unavailable//
-					<</if>>
-				<</if>>
-				/*
-				<<if $activeChild.father == $slaves[$i].ID>>
-					<<if isSlaveAvailable($slaves[$i])>>
-					|	<<link "Fuck $him with $his father">>
-						<<replace "#miniscene">>
-						<<set $partner = "father">>
-						<<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>>
-						<</link>>
-					<<else>>
-						//$His father, $slaves[$i].slaveName, is unavailable//
-					<</if>>
-				<</if>>
-				*/
-			<</for>>
+			<<set _availRelatives = availableRelatives($activeChild)>>
+			<<if _availRelatives.mother>>
+			|	<<link "Fuck $him with $his mother">>
+				<<replace "#miniscene">>
+				<<set $partner = "mother">>
+				<<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>>
+				<</link>>
+			<<elseif _availRelatives.motherName !== null>>
+				//$His mother, _availRelatives.motherName, is unavailable//
+			<</if>>
+			/*
+			<<if _availRelatives.father>>
+			|	<<link "Fuck $him with $his father">>
+				<<replace "#miniscene">><<set $partner = "father">>
+				<<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>>
+				<</link>>
+			<<elseif _availRelatives.fatherName !== null>>
+				//$His father, _availRelatives.fatherName, is unavailable//
+			<</if>>
+			*/
 			<<if $activeChild.sisters > 0>>
-				<<set $relation = 0>>
-				<<for $i = 0; $i < _SL; $i++>>
-					<<setLocalPronouns $slaves[$i] 2>>
-					<<if areSisters($activeChild, $slaves[$i]) > 0 && !isSlaveAvailable($slaves[$i])>>
-						<<set $relation++>>
-					<</if>>
-				<</for>>
-				<<if $relation == $activeChild.sisters>>
+				<<if _availRelatives.sisters == 0>>
 					<<if $activeChild.sisters == 1>>
 						//$His _sister2 is unavailable//
 					<<else>>
@@ -1538,21 +1525,6 @@ Hormones: <b><span id="hormones">$activeChild.hormones</span>.</b>
 <</link>>
 
 <br><br>__Behavior__:<br>
-<<set $dormitoryPopulation = 0, $roomsPopulation = 0>>
-<<for $i = 0; $i < _SL; $i++>>
-	<<if $slaves[$i].assignmentVisible && ($slaves[$i].assignment != "be your Head Girl" || $HGSuite != 1) && ($slaves[$i].assignment != "guard you" || $dojo <= 1)>>
-		<<if $slaves[$i].rules.living == "luxurious">>
-			<<if $slaves[$i].relationship >= 4>>
-				<<set $roomsPopulation += 0.5>>
-			<<else>>
-				<<set $roomsPopulation++>>
-			<</if>>
-		<<else>>
-			<<set $dormitoryPopulation++>>
-		<</if>>
-	<</if>>
-<</for>>
-
 Living standard: <b><span id="livingRules">$activeChild.rules.living</span>.</b>
 //$His living conditions are managed by $nurseryName's décor.//
 
diff --git a/src/js/extendedFamilyModeJS.js b/src/js/extendedFamilyModeJS.js
index b9000e4042c..4d47e1ffee4 100644
--- a/src/js/extendedFamilyModeJS.js
+++ b/src/js/extendedFamilyModeJS.js
@@ -369,6 +369,41 @@ window.randomAvailableParent = function(slave) {
 	});
 };
 
+/**
+ * @param {App.Entity.SlaveState} slave
+ * @returns {Object}
+ */
+window.availableRelatives = function(slave) {
+	let avail = {
+		mother: false, motherName: null, father: false, fatherName: null, sisters: 0, daughters: 0
+	};
+
+	V.slaves.forEach((other) => {
+		if (slave.mother === other.ID) {
+			avail.motherName = other.slaveName;
+		}
+		if (slave.father === other.ID) {
+			avail.fatherName = other.slaveName;
+		}
+		if (isSlaveAvailable(other)) {
+			if (slave.mother === other.ID) {
+				avail.mother = true;
+			}
+			if (slave.father === other.ID) {
+				avail.father = true;
+			}
+			if (slave.ID === other.mother || slave.ID === other.father) {
+				avail.daughters++;
+			}
+			if (areSisters(slave, other) > 0) {
+				avail.sisters++;
+			}
+		}
+	});
+
+	return avail;
+};
+
 window.totalPlayerRelatives = function(pc) {
 	let relatives = 0;
 	if (pc.mother > 0) {
diff --git a/src/uncategorized/slaveInteract.tw b/src/uncategorized/slaveInteract.tw
index 68e1e121b24..587b7fb41cc 100644
--- a/src/uncategorized/slaveInteract.tw
+++ b/src/uncategorized/slaveInteract.tw
@@ -243,33 +243,21 @@
 	| <<link "Abuse $him">><<replace "#miniscene">><<include "FAbuse">><</replace>><</link>>
 	<<if $seeIncest == 1>>
 		<<if $familyTesting == 1>>
-			<<for $i = 0; $i < _SL; $i++>>
-				<<if $activeSlave.mother == $slaves[$i].ID>>
-					<<if isSlaveAvailable($slaves[$i])>>
-						| <<link "Fuck $him with $his mother">><<replace "#miniscene">><<set $partner = "mother">><<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>><</link>>
-					<<else>>
-						//$His mother, $slaves[$i].slaveName, is unavailable//
-					<</if>>
-				<</if>>
-				/*
-				<<if $activeSlave.father == $slaves[$i].ID>>
-					<<if isSlaveAvailable($slaves[$i])>>
-						| <<link "Fuck $him with $his father">><<replace "#miniscene">><<set $partner = "father">><<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>><</link>>
-					<<else>>
-						//$His father, $slaves[$i].slaveName, is unavailable//
-					<</if>>
-				<</if>>
-				*/
-			<</for>>
+			<<set _availRelatives = availableRelatives($activeSlave)>>
+			<<if _availRelatives.mother>>
+				| <<link "Fuck $him with $his mother">><<replace "#miniscene">><<set $partner = "mother">><<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>><</link>>
+			<<elseif _availRelatives.motherName !== null>>
+				//$His mother, _availRelatives.motherName, is unavailable//
+			<</if>>
+			/*
+			<<if _availRelatives.father>>
+				| <<link "Fuck $him with $his father">><<replace "#miniscene">><<set $partner = "father">><<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>><</link>>
+			<<elseif _availRelatives.fatherName !== null>>
+				//$His father, _availRelatives.fatherName, is unavailable//
+			<</if>>
+			*/
 			<<if $activeSlave.daughters > 0>>
-				<<set $relation = 0>>
-				<<for $i = 0; $i < _SL; $i++>>
-					<<setLocalPronouns $slaves[$i] 2>>
-					<<if ($activeSlave.ID == $slaves[$i].father || $activeSlave.ID == $slaves[$i].mother) && !isSlaveAvailable($slaves[$i])>>
-						<<set $relation++>>
-					<</if>>
-				<</for>>
-				<<if $relation == $activeSlave.daughters>>
+				<<if _availRelatives.daughters == 0>>
 					<<if $activeSlave.daughters == 1>>
 						//$His _daughter2 is unavailable//
 					<<else>>
@@ -281,22 +269,15 @@
 					<<else>>
 						| <<link "Fuck $him with one of $his daughters">><<replace "#miniscene">><<set $partner = "daughter">><<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>><</link>>
 					<</if>>
+					/*
+					<<if _availRelatives.daughters > 1>>
+						| <<link "Fuck $him with $his daughters">><<replace "#miniscene">><<set $partner = "daughters">><<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>><</link>>
+					<</if>>
+					*/
 				<</if>>
 			<</if>>
-			/*
-			<<if $activeSlave.daughters > 1>>
-				| <<link "Fuck $him with $his daughters">><<replace "#miniscene">><<set $partner = "daughters">><<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>><</link>>
-			<</if>>
-			*/
 			<<if $activeSlave.sisters > 0>>
-				<<set $relation = 0>>
-				<<for $i = 0; $i < _SL; $i++>>
-					<<setLocalPronouns $slaves[$i] 2>>
-					<<if areSisters($activeSlave, $slaves[$i]) > 0 && !isSlaveAvailable($slaves[$i])>>
-						<<set $relation++>>
-					<</if>>
-				<</for>>
-				<<if $relation == $activeSlave.sisters>>
+				<<if _availRelatives.sisters == 0>>
 					<<if $activeSlave.sisters == 1>>
 						//$His _sister2 is unavailable//
 					<<else>>
@@ -308,13 +289,13 @@
 					<<else>>
 						| <<link "Fuck $him with one of $his sisters">><<replace "#miniscene">><<set $partner = "sister">><<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>><</link>>
 					<</if>>
+					/*
+					<<if _availRelatives.sisters > 1>>
+						| <<link "Fuck $him with $his sisters">><<replace "#miniscene">><<set $partner = "sisters">><<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>><</link>>
+					<</if>>
+					*/
 				<</if>>
 			<</if>>
-			/*
-			<<if $activeSlave.sisters > 1>>
-				| <<link "Fuck $him with $his sisters">><<replace "#miniscene">><<set $partner = "sisters">><<include "FRelation">><br>&nbsp;&nbsp;&nbsp;&nbsp;<</replace>><</link>>
-			<</if>>
-			*/
 		<<else>>
 			<<if ($activeSlave.relation != 0)>>
 				<<set _assayedSlave = getSlave($activeSlave.relationTarget)>>
@@ -1527,20 +1508,10 @@ Hormones: <strong><span id="hormones">
 <<if $activeSlave.fuckdoll > 0>>
 	//Rules have little meaning for living sex toys//
 <<else>>
-	<<set $dormitoryPopulation = 0, $roomsPopulation = 0>>
-	<<for $i = 0; $i < _SL; $i++>>
-		<<if $slaves[$i].assignmentVisible == 1 && ($slaves[$i].assignment != "be your Head Girl" || $HGSuite != 1) && ($slaves[$i].assignment != "guard you" || $dojo <= 1)>>
-			<<if $slaves[$i].rules.living == "luxurious">>
-				<<if $slaves[$i].relationship >= 4>>
-					<<set $roomsPopulation += 0.5>>
-				<<else>>
-					<<set $roomsPopulation++>>
-				<</if>>
-			<<else>>
-				<<set $dormitoryPopulation++>>
-			<</if>>
-		<</if>>
-	<</for>>
+	<<set _visibleSlaves = $slaves.filter(s => s.assignmentVisible == 1 && (s.assignment != "be your Head Girl" || $HGSuite != 1) && (s.assignment != "guard you" || $dojo <= 1)),
+		$dormitoryPopulation = _visibleSlaves.filter(s => s.rules.living != "luxurious").length,
+		$roomsPopulation = $slavesVisible - $dormitoryPopulation - _visibleSlaves.filter(s => s.rules.living == "luxurious" && s.relationship >= 4).length * 0.5>>
+
 	Living standard: ''<span id="livingRules">$activeSlave.rules.living</span>.''
 	<<if setup.facilityCareers.includes($activeSlave.assignment)>>
 		//$His living conditions are managed by $his assignment.//
-- 
GitLab