From 6ea430558a1ca0c09cb8092b8dcc48f69a22d315 Mon Sep 17 00:00:00 2001
From: kopareigns <kopareigns@gmail.com>
Date: Sun, 15 Apr 2018 13:12:35 -0400
Subject: [PATCH] Alternative fix to duped slaves

---
 devNotes/twine JS            | 6 ++++++
 src/init/storyInit.tw        | 1 -
 src/js/assayJS.tw            | 6 ++++++
 src/npc/removeActiveSlave.tw | 1 -
 4 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/devNotes/twine JS b/devNotes/twine JS
index b964dfc6c3d..fc97687892d 100644
--- a/devNotes/twine JS	
+++ b/devNotes/twine JS	
@@ -6872,6 +6872,12 @@ window.addSlave = function addSlave(slave) {
 };
 
 window.removeSlave = function removeSlave(index) {
+	delete State.variables.slaveIndices[State.variables.slaves[index].ID];
+	for (var key in State.variables.slaveIndices) {
+		if (State.variables.slaveIndices[key] > index) {
+			State.variables.slaveIndices[key] -= 1;
+		}
+	}
 	return State.variables.slaves.deleteAt(index);
 };
 
diff --git a/src/init/storyInit.tw b/src/init/storyInit.tw
index a512af24f9f..d32802dc7fc 100644
--- a/src/init/storyInit.tw
+++ b/src/init/storyInit.tw
@@ -71,7 +71,6 @@ You should have received a copy of the GNU General Public License along with thi
 		<<set $slaves[_i].rivalry = 0, $slaves[_i].rivalryTarget = 0, $slaves[_i].subTarget = 0>>
 	<<else>>
 		<<set _dump = removeSlave(_i), _i--, _SL-->>
-		<<set $slaveIndices = slaves2indices()>>
 	<</if>>
 	<</for>>
 	<<for _k = 0; _k < _SL; _k++>>
diff --git a/src/js/assayJS.tw b/src/js/assayJS.tw
index 875af5ad209..797f19d48ad 100644
--- a/src/js/assayJS.tw
+++ b/src/js/assayJS.tw
@@ -221,6 +221,12 @@ window.addSlave = function addSlave(slave) {
 };
 
 window.removeSlave = function removeSlave(index) {
+	delete State.variables.slaveIndices[State.variables.slaves[index].ID];
+	for (var key in State.variables.slaveIndices) {
+		if (State.variables.slaveIndices[key] > index) {
+			State.variables.slaveIndices[key] -= 1;
+		}
+	}
 	return State.variables.slaves.deleteAt(index);
 };
 
diff --git a/src/npc/removeActiveSlave.tw b/src/npc/removeActiveSlave.tw
index b510a25eeaa..2183e06c3a8 100644
--- a/src/npc/removeActiveSlave.tw
+++ b/src/npc/removeActiveSlave.tw
@@ -144,7 +144,6 @@
 	<</if>>
 	
 	<<set _dump = removeSlave(_x), _SL--, $activeSlave = 0>>
-	<<set $slaveIndices = slaves2indices()>>
 	
 	<<if _missingID == 1>>
 		<<set $missingParentID-->>
-- 
GitLab