From 01c59227b349b95a96d31f0db42275fa756e30cf Mon Sep 17 00:00:00 2001
From: Empress Sela <empresssela@cock.li>
Date: Wed, 8 Jul 2020 10:06:49 -0400
Subject: [PATCH] Implement missingtable support for COI

---
 .../backwardsCompatibility.js                 |  5 +++
 src/js/ibcJS.js                               | 12 ++++---
 src/js/removeActiveSlave.js                   | 34 +++++++++++++++++--
 3 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/src/data/backwardsCompatibility/backwardsCompatibility.js b/src/data/backwardsCompatibility/backwardsCompatibility.js
index 05b17490418..31c1e94aec1 100644
--- a/src/data/backwardsCompatibility/backwardsCompatibility.js
+++ b/src/data/backwardsCompatibility/backwardsCompatibility.js
@@ -1282,6 +1282,11 @@ App.Update.slaveRecords = function(node) {
 
 App.Update.genePoolRecords = function(node) {
 	if (V.releaseID < 1075) {
+		Object.keys(V.missingTable).map(k => V.missingTable[k]).forEach(s => {
+			s.mother = 0;
+			s.father = 0;
+			s.inbreedingCoeff = 0;
+		});
 		let ib_coeff = ibc.coeff_slaves(V.genePool);
 		V.genePool.forEach(g => {g.inbreedingCoefficient = ib_coeff[g.ID]});
 	}
diff --git a/src/js/ibcJS.js b/src/js/ibcJS.js
index dbbb3ea800a..6148bc1c58c 100644
--- a/src/js/ibcJS.js
+++ b/src/js/ibcJS.js
@@ -1,7 +1,7 @@
 globalThis.ibc = (() => {
     // These IDs are considered to be unknown parents
     let or_null = (s) => specificCharacterID(s) ? s : null;
-    let find_gp = (id) => (SugarCube.State.variables.genePool.find((s) => s.ID == id) || slaveStateById(id) || null);
+    let find_gp = (id) => (V.genePool.find((s) => s.ID == id) || slaveStateById(id) || ((id in V.missingTable) ? V.missingTable[id] : null) || null);
 
     // Create a node for the given ID
     let create_node = (id) => ({
@@ -239,10 +239,13 @@ globalThis.ibc = (() => {
                 if (p !== null && !(p in nodes)) {
                     if (p === -1) {
                         create_node_rec(SugarCube.State.variables.PC);
-                    } else if (p < 0) {
-                        nodes[p] = create_node(p);
                     } else {
-                        create_node_rec(find_gp(p));
+                        let gp = find_gp(p);
+                        if (gp !== null) {
+                            create_node_rec(gp);
+                        } else {
+                            nodes[p] = create_node(p);
+                        }
                     }
                 }
             });
@@ -254,7 +257,6 @@ globalThis.ibc = (() => {
 
             let sg = find_gp(s.ID);
             if (sg !== null && "inbreedingCoeff" in sg && sg.inbreedingCoeff !== -1) {
-                console.log("Found "+s.ID);
                 nodes[s.ID]._coeff = sg.inbreedingCoeff;
             }
         };
diff --git a/src/js/removeActiveSlave.js b/src/js/removeActiveSlave.js
index 95ea18e2bda..ba16521d22c 100644
--- a/src/js/removeActiveSlave.js
+++ b/src/js/removeActiveSlave.js
@@ -197,6 +197,11 @@ globalThis.removeActiveSlave = function() {
 				V.genePool.deleteAt(_geneIndex);
 			}
 		}
+		Object.keys(V.missingTable).forEach(k => {
+			let s = V.missingTable[k];
+			if (s.mother === V.activeSlave.ID || s.father === V.activeSlave.ID)
+				missing = true;
+		});
 		if (missing) {
 			V.missingTable[V.missingParentID] = {
 				slaveName: V.activeSlave.slaveName,
@@ -204,7 +209,10 @@ globalThis.removeActiveSlave = function() {
 				fullName: SlaveFullName(V.activeSlave),
 				dick: V.activeSlave.dick,
 				vagina: V.activeSlave.vagina,
-				ID: V.missingParentID
+				ID: V.missingParentID,
+				mother: V.activeSlave.mother,
+				father: V.activeSlave.father,
+				inbreedingCoeff: V.activeSlave.inbreedingCoeff
 			};
 			if (V.traitor.ID === V.activeSlave.ID) {
 				/* To link developing fetuses to their parent */
@@ -212,6 +220,13 @@ globalThis.removeActiveSlave = function() {
 			} else if (V.boomerangSlave.ID === V.activeSlave.ID) {
 				V.boomerangSlave.missingParentTag = V.missingParentID;
 			}
+			Object.keys(V.missingTable).forEach(k => {
+				let s = V.missingTable[k];
+				if (s.mother === V.activeSlave.ID)
+					s.mother = V.missingParentID;
+				if (s.father === V.activeSlave.ID)
+					s.father = V.missingParentID;
+			});
 			V.missingParentID--;
 		}
 
@@ -310,6 +325,11 @@ globalThis.removeNonNGPSlave = function(removedSlave) {
 				V.genePool.deleteAt(_geneIndex);
 			}
 		}
+		Object.keys(V.missingTable).forEach(k => {
+			let s = V.missingTable[k];
+			if (s.mother == removedSlave.ID || s.father == removedSlave.ID)
+				missing = true;
+		});
 		if (missing) {
 			V.missingTable[V.missingParentID] = {
 				slaveName: removedSlave.slaveName,
@@ -317,8 +337,18 @@ globalThis.removeNonNGPSlave = function(removedSlave) {
 				fullName: SlaveFullName(removedSlave),
 				dick: removedSlave.dick,
 				vagina: removedSlave.vagina,
-				ID: V.missingParentID
+				ID: V.missingParentID,
+				mother: removedSlave.mother,
+				father: removedSlave.father,
+				inbreedingCoeff: removedSlave.inbreedingCoeff
 			};
+			Object.keys(V.missingTable).forEach(k => {
+				let s = V.missingTable[k];
+				if (s.mother === removedSlave.ID)
+					s.mother = V.missingParentID;
+				if (s.father === removedSlave.ID)
+					s.father = V.missingParentID;
+			});
 			V.missingParentID--;
 		}
 
-- 
GitLab