From 599e566f94df24bd6b07354d77da54fb1eb7af7c Mon Sep 17 00:00:00 2001
From: Svornost <11434-svornost@users.noreply.gitgud.io>
Date: Mon, 6 Apr 2020 22:15:00 -0700
Subject: [PATCH] Get rid of $slavesVisible, clean up main.tw a bit, and
 improve penthouse bed calculation.

---
 js/003-data/gameVariableData.js             |  1 -
 src/js/utilsFC.js                           | 22 +++++++++++++++++++++
 src/uncategorized/main.tw                   | 17 ++--------------
 src/uncategorized/slaveAssignmentsReport.tw |  8 +++++---
 src/uncategorized/slaveInteract.tw          |  4 +---
 5 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/js/003-data/gameVariableData.js b/js/003-data/gameVariableData.js
index fcbca4509a8..d7430655cc6 100644
--- a/js/003-data/gameVariableData.js
+++ b/js/003-data/gameVariableData.js
@@ -590,7 +590,6 @@ App.Data.resetOnNGPlus = {
 	startingGirlRelation: 0,
 	createRelatedSlave: 0,
 
-	slavesVisible: 0,
 	dormitory: 20,
 	dormitoryPopulation: 0,
 	rooms: 5,
diff --git a/src/js/utilsFC.js b/src/js/utilsFC.js
index 0e613fd1bb0..a4cf51d691f 100644
--- a/src/js/utilsFC.js
+++ b/src/js/utilsFC.js
@@ -2916,3 +2916,25 @@ window.getRevivalistNationality = function() {
 	}
 	return 0;
 };
+
+window.penthouseCensus = function() {
+	function occupiesRoom(slave) {
+		if (slave.rules.living !== "luxurious") {
+			return false; // assigned to dormitory
+		} else if (slave.assignment === "be your Head Girl" && V.HGSuite > 0) {
+			return false; // lives in HG suite
+		} else if (slave.assignment === "guard you" && V.dojo > 0) {
+			return false; // lives in dojo
+		} else if (slave.relationship >= 4) {
+			const partner = getSlave(slave.relationshipTarget);
+			if (assignmentVisible(partner) && partner.ID < slave.ID && partner.rules.living === "luxurious") {
+				return false; // living with partner, who is already assigned a room (always allocate a room to the partner with the lower ID)
+			}
+		}
+		return true; // takes her own room
+	}
+
+	const penthouseSlaves = V.slaves.filter(s => assignmentVisible(s));
+	V.roomsPopulation = penthouseSlaves.filter(occupiesRoom).length;
+	V.dormitoryPopulation = penthouseSlaves.filter(s => s.rules.living !== "luxurious").length;
+};
diff --git a/src/uncategorized/main.tw b/src/uncategorized/main.tw
index 245873ca9e1..d8b335a78de 100644
--- a/src/uncategorized/main.tw
+++ b/src/uncategorized/main.tw
@@ -10,21 +10,10 @@
 <</script>>
 
 <<if $tabChoice.SlaveInteract != 'Description'>>
- <<set $tabChoice.SlaveInteract = 'Description'>>
+	<<set $tabChoice.SlaveInteract = 'Description'>>
 <</if>>
 
-<<set _visibleSlaves = $slaves.filter(s => assignmentVisible(s) && (s.assignment != "be your Head Girl" || $HGSuite != 1) && (s.assignment != "guard you" || $dojo <= 1)),
-	$slavesVisible = _visibleSlaves.length,
-	$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,
-	_PA = (Array.isArray($personalAttention) ? $personalAttention.map(function(x) { return getSlave(x.ID); }) : []),
-	_HG = $slaveIndices[$HeadGirl.ID],
-	_RC = $slaveIndices[$Recruiter.ID],
-	_BG = $slaveIndices[$Bodyguard.ID]>>
-
-<<if (def _HG && $HGSuite > 0)>><<set $slavesVisible++>><</if>>
-<<if ($HGSuiteiIDs.length > 0)>><<set $slavesVisible++>><</if>>
-<<if (def _BG && $dojo > 1)>><<set $slavesVisible++>><</if>>
+<<run penthouseCensus()>>
 
 <<set $nextButton = "END WEEK", $nextLink = "End Week", $showEncyclopedia = 1, $encyclopedia = "How to Play">>
 <<set $costs = Math.trunc(calculateCosts.predict())>>
@@ -33,8 +22,6 @@
 
 <<run SlaveSort.slaves($slaves)>>
 
-<<set _SL = $slaves.length>>
-
 <<if $newModelUI == 1>>
 	<span id="building"></span>
 	<<script>>
diff --git a/src/uncategorized/slaveAssignmentsReport.tw b/src/uncategorized/slaveAssignmentsReport.tw
index e47eccc4507..98571c44315 100644
--- a/src/uncategorized/slaveAssignmentsReport.tw
+++ b/src/uncategorized/slaveAssignmentsReport.tw
@@ -971,10 +971,11 @@ $sexDemandResult.topClass = Math.trunc((($NPCSexSupply.topClass + $slaveJobValue
 
 <<if $averageDick > 0>><<set $averageDick = $averageDick/$slavesWithWorkingDicks>><</if>>
 <<set $freeSexualEnergy = $PC.sexualEnergy-$fuckSlaves>>
+<<set _penthouseSlaves = App.Entity.facilities.penthouse.employees().length>>
 <<if $freeSexualEnergy > 0>>
-	<<if $freeSexualEnergy > $slavesVisible/2>>
+	<<if $freeSexualEnergy > _penthouseSlaves/2>>
 		<<set $freeSexualEnergy = 3>>
-	<<elseif $freeSexualEnergy > $slavesVisible/4>>
+	<<elseif $freeSexualEnergy > _penthouseSlaves/4>>
 		<<set $freeSexualEnergy = 2>>
 	<<else>>
 		<<set $freeSexualEnergy = 1>>
@@ -1068,6 +1069,7 @@ $sexDemandResult.topClass = Math.trunc((($NPCSexSupply.topClass + $slaveJobValue
 * @todo Figure out why we're using variable space with capitalized facility names when we can parse it from true name
 */
 
+<<set _penthouseReportLength = App.Entity.facilities.penthouse.employees().length + $HGSuiteiIDs.length>>
 <<set _facListArr = [
 	["Arcade Report", $arcadeNameCaps, $arcade, $ArcadeiIDs.length, -1, -1],
 	["Brothel Report", $brothelNameCaps, $brothel, $BrothiIDs.length, $Madam, "Madam"],
@@ -1083,7 +1085,7 @@ $sexDemandResult.topClass = Math.trunc((($NPCSexSupply.topClass + $slaveJobValue
 	["Children Report", "Nursery Children", $nursery, $nurseryBabies, -1, -1],
 	["Incubator Report", $incubatorNameCaps, $incubator, $incubatorSlaves, -1, -1],
 	["Master Suite Report", $masterSuiteNameCaps, $masterSuite, $MastSiIDs.length, $Concubine, "Concubine"],
-	["Penthouse Report", "The Penthouse", 1, $slavesVisible, -1, -1],
+	["Penthouse Report", "The Penthouse", 1, _penthouseReportLength, -1, -1],
 	["Rules Assistant Report", "Rules Assistant", $rulesAssistantAuto, 1, -1, -1] /** should be last — may reassign slaves **/
 ]>>
 
diff --git a/src/uncategorized/slaveInteract.tw b/src/uncategorized/slaveInteract.tw
index 92a514128c5..50f9d00e247 100644
--- a/src/uncategorized/slaveInteract.tw
+++ b/src/uncategorized/slaveInteract.tw
@@ -446,9 +446,7 @@
 		<<if $slaves[_i].fuckdoll > 0>>
 			//Rules have little meaning for living sex toys//
 		<<else>>
-			<<set _visibleSlaves = $slaves.filter(s => assignmentVisible(s) && (s.assignment != "be your Head Girl" || $HGSuite != 1) && (s.assignment != "guard you" || $dojo <= 1)),
-				$dormitoryPopulation = _visibleSlaves.filter(s => s.rules.living != "luxurious").length,
-				$roomsPopulation = _visibleSlaves.length - $dormitoryPopulation - _visibleSlaves.filter(s => s.rules.living == "luxurious" && s.relationship >= 4).length * 0.5>>
+			<<run penthouseCensus()>>
 
 			Living standard: ''<span id="livingRules">$slaves[_i].rules.living</span>.''
 			<<if setup.facilityCareers.includes($slaves[_i].assignment)>>
-- 
GitLab