From 5c0675d0e0e2b4838513ac4374d147ca37c446ea Mon Sep 17 00:00:00 2001
From: Empress Sela <empresssela@cock.li>
Date: Wed, 8 Jul 2020 00:06:36 -0400
Subject: [PATCH] First attempt at integration

- Still missing effect on intelligence/face and acquisition passage
---
 .../backwardsCompatibility/backwardsCompatibility.js  |  6 ++++++
 src/data/backwardsCompatibility/updateSlaveObject.js  | 10 ++++++++++
 src/facilities/nursery/widgets/children/ChildState.js |  2 ++
 src/facilities/nursery/widgets/infants/InfantState.js |  2 ++
 src/js/SlaveState.js                                  |  2 ++
 src/js/ibcJS.js                                       | 11 ++++++++++-
 src/npc/generate/generateGenetics.js                  |  3 +++
 src/player/js/PlayerState.js                          |  2 ++
 src/pregmod/widgets/bodyswapWidgets.tw                |  1 +
 9 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/src/data/backwardsCompatibility/backwardsCompatibility.js b/src/data/backwardsCompatibility/backwardsCompatibility.js
index d49802024c0..05b17490418 100644
--- a/src/data/backwardsCompatibility/backwardsCompatibility.js
+++ b/src/data/backwardsCompatibility/backwardsCompatibility.js
@@ -1258,6 +1258,7 @@ App.Update.slaveRecords = function(node) {
 					child.spermY = normalRandInt(50, 5);
 				}
 				App.Facilities.Nursery.InfantDatatypeCleanup(child);
+				child.inbreedingCoeff = ibc.coeff(child);
 			} else {
 				App.Update.Slave(child);
 				App.Entity.Utils.SlaveDataSchemeCleanup(child, true);
@@ -1280,6 +1281,11 @@ App.Update.slaveRecords = function(node) {
 };
 
 App.Update.genePoolRecords = function(node) {
+	if (V.releaseID < 1075) {
+		let ib_coeff = ibc.coeff_slaves(V.genePool);
+		V.genePool.forEach(g => {g.inbreedingCoefficient = ib_coeff[g.ID]});
+	}
+
 	for (let bci = 0; bci < V.genePool.length; bci++) {
 		App.Update.Slave(V.genePool[bci], true);
 		let slave = V.genePool[bci];
diff --git a/src/data/backwardsCompatibility/updateSlaveObject.js b/src/data/backwardsCompatibility/updateSlaveObject.js
index 0e63c24a3c0..1747ddfe272 100644
--- a/src/data/backwardsCompatibility/updateSlaveObject.js
+++ b/src/data/backwardsCompatibility/updateSlaveObject.js
@@ -1026,4 +1026,14 @@ App.Update.Slave = function(slave, genepool = false) {
 			}
 		}
 	}
+	
+	if (V.releaseID < 1075) {
+		slave.inbreedingCoeff = ibc.coeff(slave);
+		slave.womb.forEach(f => {
+			// Use null as the ID, since fetuses are missing it
+			f.genetics.inbreedingCoeff = ibc.coeff(
+				{ID: null, mother: f.genetics.mother, father: f.genetics.father}
+			);
+		});
+	}
 };
diff --git a/src/facilities/nursery/widgets/children/ChildState.js b/src/facilities/nursery/widgets/children/ChildState.js
index 8de31f2d3e8..4adb08d4e2c 100644
--- a/src/facilities/nursery/widgets/children/ChildState.js
+++ b/src/facilities/nursery/widgets/children/ChildState.js
@@ -1931,5 +1931,7 @@ App.Facilities.Nursery.ChildState = class ChildState {
 		this.lastWeeksRepIncome = 0;
 		/** Not currently used, will work similarly to the cash variables above */
 		this.lastWeeksRepExpenses = 0;
+		/** Slave's inbreeding coefficient */
+		this.inbreedingCoeff = 0;
 	}
 };
diff --git a/src/facilities/nursery/widgets/infants/InfantState.js b/src/facilities/nursery/widgets/infants/InfantState.js
index f50c3506b54..bad32d73198 100644
--- a/src/facilities/nursery/widgets/infants/InfantState.js
+++ b/src/facilities/nursery/widgets/infants/InfantState.js
@@ -187,5 +187,7 @@ App.Facilities.Nursery.InfantState = class InfantState {
 		this.spermY = 50;
 		/** how many weeks until the child is ready for release */
 		this.growTime = 156;
+		/** Slave's inbreeding coefficient */
+		this.inbreedingCoeff = 0;
 	}
 };
diff --git a/src/js/SlaveState.js b/src/js/SlaveState.js
index 0313f4892c1..0024d31a739 100644
--- a/src/js/SlaveState.js
+++ b/src/js/SlaveState.js
@@ -2501,6 +2501,8 @@ App.Entity.SlaveState = class SlaveState {
 		this.whoreClass = 0;
 		/** Maximum class for whore to target */
 		this.effectiveWhoreClass = 0;
+		/** Slave's inbreeding coefficient */
+		this.inbreedingCoeff = 0;
 	}
 
 	/** Creates an object suitable for setting nested attributes as it would be a SlaveState
diff --git a/src/js/ibcJS.js b/src/js/ibcJS.js
index b34126251aa..dbbb3ea800a 100644
--- a/src/js/ibcJS.js
+++ b/src/js/ibcJS.js
@@ -290,7 +290,16 @@ globalThis.ibc = (() => {
     };
 
     // Determine the coefficient of inbreeding of a single slave
-    let coeff_slave = slave => coeff_slaves([slave])[slave.ID];
+    let coeff_slave = slave => {
+        if ("inbreedingCoeff" in slave && slave.inbreedingCoeff !== -1)
+            return slave.inbreedingCoeff;
+
+        let gp = find_gp(slave.ID);
+        if (gp !== null && "inbreedingCoeff" in gp && gp.inbreedingCoeff !== -1)
+            return gp.inbreedingCoeff;
+
+        return coeff_slaves([slave])[slave.ID];
+    };
 
     // Determine the kinship between one and many slaves. Returns an mapping from the ID of each of
     // the slaves in `others` to its kinship with slave `a`
diff --git a/src/npc/generate/generateGenetics.js b/src/npc/generate/generateGenetics.js
index 250afa87426..8e7ad2bc128 100644
--- a/src/npc/generate/generateGenetics.js
+++ b/src/npc/generate/generateGenetics.js
@@ -97,6 +97,7 @@ globalThis.generateGenetics = (function() {
 		genes.motherName = setMotherName(activeMother);
 		genes.father = setFatherID(actor2);
 		genes.fatherName = setFatherName(father, activeFather, actor2);
+		genes.inbreedingCoeff = ibc.kinship(mother, father);
 		genes.nationality = setNationality(father, mother);
 		genes.geneticQuirks = setGeneticQuirks(activeFather, activeMother, genes.gender);
 		genes.skin = setSkin(father, mother, actor2);
@@ -1350,6 +1351,8 @@ globalThis.generateChild = function (mother, ovum, incubator=false) {
 		child.navelPiercing = 0;
 	}
 
+	child.inbreedingCoeff = genes.inbreedingCoeff;
+
 	generatePronouns(child);
 
 	return child;
diff --git a/src/player/js/PlayerState.js b/src/player/js/PlayerState.js
index 4efbdaf320a..fe3b61163ee 100644
--- a/src/player/js/PlayerState.js
+++ b/src/player/js/PlayerState.js
@@ -1991,6 +1991,8 @@ App.Entity.PlayerState = class PlayerState {
 		 *
 		 * 0: no; 1: yes */
 		this.staminaPills = 0;
+		/** Player's coefficient of inbreeding */
+		this.inbreedingCoeff = 0;
 	}
 
 	/** Creates an object suitable for setting nested attributes as it would be a SlaveState
diff --git a/src/pregmod/widgets/bodyswapWidgets.tw b/src/pregmod/widgets/bodyswapWidgets.tw
index ee9c226b664..4a1ed4eeac6 100644
--- a/src/pregmod/widgets/bodyswapWidgets.tw
+++ b/src/pregmod/widgets/bodyswapWidgets.tw
@@ -164,6 +164,7 @@
 <<set $args[0].albinismOverride = $args[1].albinismOverride>>
 <<set $args[0].clone = $args[1].clone>>
 <<set $args[0].cloneID = $args[1].cloneID>>
+<<set $args[0].inbreedingCoeff = $args[1].inbreedingCoeff>>
 
 <<set $args[0].canRecruit = 0>>
 
-- 
GitLab