From 72baebfe5b0d5ec7f284e9fbda4f4984312bb8e2 Mon Sep 17 00:00:00 2001
From: Anony <solosenpai@protonmail.com>
Date: Sun, 19 Jan 2020 09:26:15 -0800
Subject: [PATCH] Refactor high checks and cleanup intelligence check

---
 devNotes/Useful JS Function Documentation.txt |  2 ++
 src/facilities/nursery/childInteract.tw       |  2 +-
 src/js/DefaultRules.js                        |  5 ++---
 src/js/assayJS.js                             | 20 +++++++++++++++++--
 src/uncategorized/saDrugs.tw                  |  6 +-----
 src/uncategorized/saLiveWithHG.tw             |  2 +-
 src/uncategorized/slaveInteract.tw            |  2 +-
 src/utility/miscWidgets.tw                    |  2 +-
 8 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/devNotes/Useful JS Function Documentation.txt b/devNotes/Useful JS Function Documentation.txt
index 65831888e72..0bf3273d183 100644
--- a/devNotes/Useful JS Function Documentation.txt	
+++ b/devNotes/Useful JS Function Documentation.txt	
@@ -24,6 +24,8 @@ TatScore(slave) - Returns int representing degree of tattooing. Higher means mor
 
 canImproveIntelligence(slave) - Returns if slave intelligence can be improved with Psychostimulants
 
+canImproveHeight(slave) - Returns if slave height can be improved with growth stimulants
+
 sameAssignmentP(slave, slave) - Returns if slaves are on the same assignment.
 
 haveRelationP(slave1, slave2) - Returns if slave1 is related to second slave2. (NOT extended family mode compliant).
diff --git a/src/facilities/nursery/childInteract.tw b/src/facilities/nursery/childInteract.tw
index 2f0b3b13de8..cd5b2e7cfbf 100644
--- a/src/facilities/nursery/childInteract.tw
+++ b/src/facilities/nursery/childInteract.tw
@@ -1161,7 +1161,7 @@ FIXME:
 			<</if>>
 		<</if>>
 		<<if $growthStim>>
-			<<if $activeChild.height < 274 && $activeChild.height < Math.clamp((Height.mean($activeChild) * 1.25),0,274)>>
+			<<if canImproveHeight($activeChild)>>
 			|	<<link "Growth Stimulants">>
 					<<set $activeChild.drugs = "growth stimulants">>
 					<<SlaveInteractDrugs>>
diff --git a/src/js/DefaultRules.js b/src/js/DefaultRules.js
index aa83b7541b6..84bd8cab1fe 100644
--- a/src/js/DefaultRules.js
+++ b/src/js/DefaultRules.js
@@ -1277,7 +1277,7 @@ window.DefaultRules = (function() {
 					break;
 
 				case "growth stimulants":
-					if (!(slave.height < 274 && slave.height < Math.clamp((Height.mean(slave) * 1.25), 0, 274))) {
+					if (!canImproveHeight(slave)) {
 						flag = false;
 					}
 					break;
@@ -1307,8 +1307,7 @@ window.DefaultRules = (function() {
 					break;
 
 				case "psychostimulants": {
-					let origIntel = V.genePool.find(s => s.ID === slave.ID).intelligence;
-					if (!((slave.intelligence < 100) && (slave.intelligence < origIntel + 15))) {
+					if (!canImproveIntelligence(slave)) {
 						flag = false;
 					}
 				}	break;
diff --git a/src/js/assayJS.js b/src/js/assayJS.js
index c4835e1eba9..156cb431186 100644
--- a/src/js/assayJS.js
+++ b/src/js/assayJS.js
@@ -12,10 +12,26 @@ window.sameAssignmentP = function sameAssignmentP(A, B) {
  * @returns {boolean}
  */
 window.canImproveIntelligence = function canImproveIntelligence(slave) {
-	let origIntel = V.genePool.find(function(s) { return s.ID == slave.ID; }).intelligence
-	return (slave.intelligence < origIntel+15) && (slave.intelligence < 100)
+	let origIntel = V.genePool.find(function(s) { return s.ID === slave.ID; }).intelligence;
+	return (slave.intelligence < origIntel+15) && (slave.intelligence < 100);
 };
 
+/**
+ * @param {App.Entity.SlaveState} slave
+ * @returns {boolean}
+ */
+window.canImproveHeight = function canImproveHeight(slave) {
+	if (slave.height >= 274) {
+		return false;
+	}
+	let maxHeight = Math.trunc(Math.clamp((Height.mean(slave) * 1.25), 0, 274)); /* max achievable height is expected height plus 25% */
+
+	if (slave.geneticQuirks.dwarfism === 2 && slave.geneticQuirks.gigantism !== 2) {
+		maxHeight = Math.min(maxHeight, 160);
+	}
+
+	return slave.height < maxHeight;
+};
 
 /**
  * @param {App.Entity.SlaveState} slave
diff --git a/src/uncategorized/saDrugs.tw b/src/uncategorized/saDrugs.tw
index cc4ee86fb15..a2f61e1a4bb 100644
--- a/src/uncategorized/saDrugs.tw
+++ b/src/uncategorized/saDrugs.tw
@@ -1465,11 +1465,7 @@
 /* DRUG EXPIRY */
 <<switch $slaves[$i].drugs>>
 <<case "growth stimulants">>
-	<<set _maxHeight = Math.trunc(Math.clamp((Height.mean($slaves[$i]) * 1.25),0,274))>> /*max achievable height is expected height plus 25%*/
-	<<if $slaves[$i].geneticQuirks.dwarfism == 2 && $slaves[$i].geneticQuirks.gigantism != 2>>
-		<<set _maxHeight = Math.min(_maxHeight, 160)>>
-	<</if>>
-	<<if $slaves[$i].height >= _maxHeight>>
+	<<if !canImproveHeight($slaves[$i])>>
 		$His body has already grown far past $his natural limits; further injections of stimulants will have no effect. @@.yellow;$His drug regimen has been ended.@@
 		<<set $slaves[$i].height = Math.clamp($slaves[$i].height,0,_maxHeight), $slaves[$i].drugs = "no drugs">>
 	<</if>>
diff --git a/src/uncategorized/saLiveWithHG.tw b/src/uncategorized/saLiveWithHG.tw
index 2d27fbaf14d..28847752d9d 100644
--- a/src/uncategorized/saLiveWithHG.tw
+++ b/src/uncategorized/saLiveWithHG.tw
@@ -883,7 +883,7 @@
 	<<elseif ($HeadGirl.fetishKnown == 1) && ($HeadGirl.fetish == "cumslut") && $slaves[$i].balls < 10 && $slaves[$i].balls > 0 && $slaves[$i].dick > 0>>
 		$HeadGirl.slaveName gives $slaves[$i].slaveName testicle injections, since _he2 wants $slaves[$i].slaveName shooting bigger loads.
 		<<set $slaves[$i].drugs = "testicle enhancement">>
-	<<elseif ($HeadGirl.fetishKnown == 1) && ($HeadGirl.fetish == "masochist" || $HeadGirl.fetish == "submissive") && $slaves[$i].geneticQuirks.dwarfism == 2 && $slaves[$i].geneticQuirks.gigantism != 2 && $slaves[$i].height < Math.clamp((Height.mean($slaves[$i]) * 1.25),0,274)>>
+	<<elseif ($HeadGirl.fetishKnown == 1) && ($HeadGirl.fetish == "masochist" || $HeadGirl.fetish == "submissive") && canImproveHeight($slaves[$i])>>
 		$HeadGirl.slaveName has a subconscious need to be hurt by the biggest, strongest $girl possible, so $he gives $slaves[$i].slaveName injections of growth stimulants to make $him grow taller.
 		<<set $slaves[$i].drugs = "growth stimulants">>
 	<<elseif ($HeadGirl.fetishKnown == 1) && ($HeadGirl.fetish == "boobs") && ($slaves[$i].boobs < 8000)>>
diff --git a/src/uncategorized/slaveInteract.tw b/src/uncategorized/slaveInteract.tw
index f5276ac54f7..076fc739f09 100644
--- a/src/uncategorized/slaveInteract.tw
+++ b/src/uncategorized/slaveInteract.tw
@@ -1106,7 +1106,7 @@
 			<</if>>
 		<</if>>
 		<<if $growthStim == 1>>
-			<<if ($activeSlave.height < 274 || ($activeSlave.height < 160 && $activeSlave.geneticQuirks.dwarfism == 2 && $activeSlave.geneticQuirks.gigantism != 2)) && $activeSlave.height < Math.clamp((Height.mean($activeSlave) * 1.25),0,274)>>
+			<<if canImproveHeight($activeSlave)>>
 				| <<link "Growth stimulants">><<set $activeSlave.drugs = "growth stimulants">><<SlaveInteractDrugs>><</link>>
 			<<else>>
 				| Growth stimulants
diff --git a/src/utility/miscWidgets.tw b/src/utility/miscWidgets.tw
index bc92127207f..f3da165af47 100644
--- a/src/utility/miscWidgets.tw
+++ b/src/utility/miscWidgets.tw
@@ -207,7 +207,7 @@ Call as <<SlaveInteractDrugs>>
 			<</if>>
 		<</if>>
 		<<if $growthStim == 1>>
-			<<if ($activeSlave.height < 274 || ($activeSlave.height < 160 && $activeSlave.geneticQuirks.dwarfism == 2 && $activeSlave.geneticQuirks.gigantism != 2)) && $activeSlave.height < Math.clamp((Height.mean($activeSlave) * 1.25),0,274)>>
+			<<if canImproveHeight($activeSlave)>>
 				| <<link "Growth Stimulants">><<set $activeSlave.drugs = "growth stimulants">><<SlaveInteractDrugs>><</link>>
 			<<else>>
 				| Growth Stimulants
-- 
GitLab