From 39244e5221fc81e2106c707eabca03a6cb19a551 Mon Sep 17 00:00:00 2001
From: kopareigns <kopareigns@gmail.com>
Date: Sat, 14 Apr 2018 02:05:32 -0400
Subject: [PATCH] slavesInLine Improvement

---
 src/uncategorized/BackwardsCompatibility.tw |  3 ++
 src/uncategorized/nextSlaveInLine.tw        |  8 +---
 src/uncategorized/placeInLine.tw            | 49 +++++++++++++++++----
 src/uncategorized/previousSlaveInLine.tw    |  8 +---
 4 files changed, 47 insertions(+), 21 deletions(-)

diff --git a/src/uncategorized/BackwardsCompatibility.tw b/src/uncategorized/BackwardsCompatibility.tw
index 83efb9eb52a..a5ceaf051c1 100644
--- a/src/uncategorized/BackwardsCompatibility.tw
+++ b/src/uncategorized/BackwardsCompatibility.tw
@@ -139,6 +139,9 @@
 <<if def $eventSlaves>>
 	<<unset $eventSlaves>>
 <</if>>
+<<if def $place>>
+	<<unset $place>>
+<</if>>
 
 /* pregmod stuff */
 
diff --git a/src/uncategorized/nextSlaveInLine.tw b/src/uncategorized/nextSlaveInLine.tw
index 26d49052a6e..8149ceb8742 100644
--- a/src/uncategorized/nextSlaveInLine.tw
+++ b/src/uncategorized/nextSlaveInLine.tw
@@ -2,15 +2,11 @@
 
 <<silently>>
 
-<<set $i = $slaves.findIndex(function(s) { return s.ID == $activeSlave.ID; })>>
+<<set $i = $slaveIndices[$activeSlave.ID]>>
 <<ClearSummaryCache $activeSlave>>
 <<set $slaves[$i] = $activeSlave>> /* save changes before switching */
 
-<<if $place == $slavesInLine.length-1>>
-	<<set $activeSlave = $slavesInLine[0]>>
-<<else>>
-	<<set $activeSlave = $slavesInLine[$place+1]>>
-<</if>>
+<<set $activeSlave = $slaves[$slavesInLine[1]]>>
 
 <<goto "Slave Interact">>
 
diff --git a/src/uncategorized/placeInLine.tw b/src/uncategorized/placeInLine.tw
index 4cd42eb226f..30ac0fdf046 100644
--- a/src/uncategorized/placeInLine.tw
+++ b/src/uncategorized/placeInLine.tw
@@ -3,21 +3,52 @@
 <<silently>>
 
 <<set $slavesInLine = []>>
+<<set _activeSlaveIndex = $slaveIndices[$activeSlave.ID]>>
+<<set _SL = $slaves.length>>
 
 <<if ($activeSlave.assignmentVisible == 1)>>
-	<<for $i = 0; $i < $slaves.length; $i++>>
-	<<if ($slaves[$i].assignmentVisible == 1)>>
-		<<set $slavesInLine.push($slaves[$i])>>
-	<</if>>
+	<<for _pil = _activeSlaveIndex - 1; _pil != _activeSlaveIndex; _pil-->> /* loops backwards through the $slaves array */
+		<<if _pil < 0>>
+			<<set _pil = _SL - 1>>
+		<</if>>
+		<<if $slaves[_pil].assignmentVisible == 1>>
+			<<set $slavesInLine.push(_pil)>> /* index of the previous slave in line */
+			<<break>>
+		<</if>>
+	<</for>>
+	<<for _pil = _activeSlaveIndex + 1; _pil != _activeSlaveIndex; _pil++>> /* this loops forwards through the $slaves array */
+		<<if _pil == _SL>>
+			<<set _pil = 0>>
+		<</if>>
+		<<if $slaves[_pil].assignmentVisible == 1>>
+			<<set $slavesInLine.push(_pil)>> /* index of the next slave in line */
+			<<break>>
+		<</if>>
 	<</for>>
 <<else>>
-	<<for $i = 0; $i < $slaves.length; $i++>>
-	<<if ($slaves[$i].assignment == $activeSlave.assignment)>>
-		<<set $slavesInLine.push($slaves[$i])>>
-	<</if>>
+	<<for _pil = _activeSlaveIndex - 1; _pil != _activeSlaveIndex; _pil-->> /* loops backwards through the $slaves array */
+		<<if _pil < 0>>
+			<<set _pil = _SL - 1>>
+		<</if>>
+		<<if ($slaves[_pil].assignment == $activeSlave.assignment)>>
+			<<set $slavesInLine.push(_pil)>> /* index of the previous slave in line */
+			<<break>>
+		<</if>>
+	<</for>>
+	<<for _pil = _activeSlaveIndex + 1; _pil != _activeSlaveIndex; _pil++>> /* this loops forwards through the $slaves array */
+		<<if _pil == _SL>>
+			<<set _pil = 0>>
+		<</if>>
+		<<if ($slaves[_pil].assignment == $activeSlave.assignment)>>
+			<<set $slavesInLine.push(_pil)>> /* index of the next slave in line */
+			<<break>>
+		<</if>>
 	<</for>>
 <</if>>
 
-<<set $place = $slavesInLine.findIndex(function(s) { return s.ID == $activeSlave.ID; })>>
+<<if $slavesInLine.length == 0>> /* if there are no other slaves available, set previous/next slave to self */
+	<<set $slavesInLine[0] = _activeSlaveIndex>>
+	<<set $slavesInLine[1] = _activeSlaveIndex>>
+<</if>>
 
 <</silently>>
diff --git a/src/uncategorized/previousSlaveInLine.tw b/src/uncategorized/previousSlaveInLine.tw
index d8c858a3347..008ccb819a4 100644
--- a/src/uncategorized/previousSlaveInLine.tw
+++ b/src/uncategorized/previousSlaveInLine.tw
@@ -2,15 +2,11 @@
 
 <<silently>>
 
-<<set $i = $slaves.findIndex(function(s) { return s.ID == $activeSlave.ID; })>>
+<<set $i = $slaveIndices[$activeSlave.ID]>>
 <<ClearSummaryCache $activeSlave>>
 <<set $slaves[$i] = $activeSlave>> /* save changes before switching */
 
-<<if $place == 0>>
-	<<set $activeSlave = $slavesInLine[$slavesInLine.length-1]>>
-<<else>>
-	<<set $activeSlave = $slavesInLine[$place-1]>>
-<</if>>
+<<set $activeSlave = $slaves[$slavesInLine[0]]>>
 
 <<goto "Slave Interact">>
 
-- 
GitLab