From 553c9e598bf5bcb7f33eb7dd275ea048ed7ffc34 Mon Sep 17 00:00:00 2001
From: Garet Halliday <me@garet.holiday>
Date: Thu, 27 Jul 2023 10:52:43 -0600
Subject: [PATCH] balance scaling between available recipes

---
 lib/gat/pool.go | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/lib/gat/pool.go b/lib/gat/pool.go
index 243d0f01..4b4d662b 100644
--- a/lib/gat/pool.go
+++ b/lib/gat/pool.go
@@ -188,11 +188,28 @@ func (T *Pool) Scale(amount int) {
 	}
 	T.mu.Unlock()
 
-	for _, recipe := range recipes {
-		amount = T.recipes[recipe].Scale(T, amount)
-		if amount == 0 {
-			break
+outer:
+	for len(recipes) > 0 {
+		j := 0
+		for i := 0; i < len(recipes); i++ {
+			recipe := recipes[i]
+			if amount > 0 {
+				if T.recipes[recipe].Scale(T, 1) == 0 {
+					amount--
+					recipes[j] = recipes[i]
+					j++
+				}
+			} else if amount < 0 {
+				if T.recipes[recipe].Scale(T, -1) == 0 {
+					amount++
+					recipes[j] = recipes[i]
+					j++
+				}
+			} else {
+				break outer
+			}
 		}
+		recipes = recipes[:j]
 	}
 }
 
-- 
GitLab