diff --git a/src/main/java/codechicken/nei/recipe/CraftRampThrottle.java b/src/main/java/codechicken/nei/recipe/CraftRampThrottle.java index 45436ad2..5abd7e03 100644 --- a/src/main/java/codechicken/nei/recipe/CraftRampThrottle.java +++ b/src/main/java/codechicken/nei/recipe/CraftRampThrottle.java @@ -1,22 +1,23 @@ package codechicken.nei.recipe; -import java.util.HashMap; -import java.util.Map; +import java.util.Objects; import codechicken.nei.recipe.Recipe.RecipeId; /** - * Per-craft delay ramp for auto-crafting. A recipe starts at the start delay and - * decays geometrically toward a floor. Each recipe keeps its own ramp for the - * run, so switching away and back resumes rather than restarts. At floor (max) - * speed crafts are batched. Pure logic; does not sleep. + * Per-craft delay ramp for auto-crafting. Crafting a recipe repeatedly speeds up + * (delay decays geometrically toward a floor). Momentum is global: switching to + * a different recipe reduces speed by a penalty but does not reset it, so going + * back to a recipe resumes with most of its momentum. At floor (max) speed + * crafts are batched. Pure logic; does not sleep. */ public class CraftRampThrottle { - public static final long START_DELAY_MS = 500L; // delay before the 1st craft - public static final long FLOOR_DELAY_MS = 50L; // fastest allowed (cap) - public static final double DECAY = 0.85D; - public static final int BULK_CRAFTS = 8; // crafts per tick once maxed out + public static final long START_DELAY_MS = 300L; // delay before the 1st craft + public static final long FLOOR_DELAY_MS = 100L; // fastest allowed (cap) + public static final double DECAY = 0.88D; // per-craft speedup + public static final double SWITCH_PENALTY = 2.0D; // momentum lost on recipe change + public static final int BULK_CRAFTS = 4; // crafts per tick once maxed out /** Delay to wait before a craft, and how many crafts that tick covers. */ public static final class Tick { @@ -30,12 +31,20 @@ public class CraftRampThrottle { } } - private final Map delays = new HashMap<>(); + private RecipeId current; + private long delayMs = START_DELAY_MS; - /** Next tick for {@code id}: the start delay the first time, then a decaying delay. */ + /** Next tick for {@code id}: decays on repeat, slows (but keeps momentum) on a recipe change. */ public Tick next(RecipeId id) { - final long delay = this.delays.getOrDefault(id, START_DELAY_MS); - this.delays.put(id, Math.max(FLOOR_DELAY_MS, Math.round(delay * DECAY))); - return new Tick(delay, delay == FLOOR_DELAY_MS ? BULK_CRAFTS : 1); + if (!Objects.equals(id, this.current)) { + if (this.current != null) { + this.delayMs = Math.min(START_DELAY_MS, Math.round(this.delayMs * SWITCH_PENALTY)); + } + this.current = id; + } + + final long delay = this.delayMs; + this.delayMs = Math.max(FLOOR_DELAY_MS, Math.round(delay * DECAY)); + return new Tick(delay, delay <= FLOOR_DELAY_MS ? BULK_CRAFTS : 1); } } diff --git a/src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java b/src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java index b699315f..6d7cc2e6 100644 --- a/src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java +++ b/src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java @@ -1,6 +1,7 @@ package codechicken.nei.recipe; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import org.junit.jupiter.api.DisplayName; @@ -11,6 +12,14 @@ import codechicken.nei.recipe.Recipe.RecipeId; class CraftRampThrottleTest { + private static Tick rampToFloor(CraftRampThrottle throttle, RecipeId id) { + Tick tick = null; + for (int i = 0; i < 100; i++) { + tick = throttle.next(id); + } + return tick; + } + @Test @DisplayName("first craft of a recipe uses the start delay and is single") void firstCraftUsesStartDelay() { @@ -23,20 +32,20 @@ class CraftRampThrottleTest { } @Test - @DisplayName("same recipe decays geometrically and clamps at floor") + @DisplayName("same recipe decays monotonically and clamps at floor") void rampDecaysToFloor() { CraftRampThrottle throttle = new CraftRampThrottle(); RecipeId id = mock(RecipeId.class); - assertEquals(500L, throttle.next(id).delayMs); // craft 1: START - assertEquals(425L, throttle.next(id).delayMs); // 500 * 0.85 - assertEquals(361L, throttle.next(id).delayMs); // round(425 * 0.85) - - Tick last = null; - for (int i = 0; i < 50; i++) { - last = throttle.next(id); + long prev = Long.MAX_VALUE; + for (int i = 0; i < 5; i++) { + long delay = throttle.next(id).delayMs; + assertTrue(delay <= prev, "delay should not increase while ramping"); + assertTrue(delay >= CraftRampThrottle.FLOOR_DELAY_MS, "delay should not drop below floor"); + prev = delay; } - assertEquals(CraftRampThrottle.FLOOR_DELAY_MS, last.delayMs); // clamped at floor + + assertEquals(CraftRampThrottle.FLOOR_DELAY_MS, rampToFloor(throttle, id).delayMs); } @Test @@ -45,13 +54,9 @@ class CraftRampThrottleTest { CraftRampThrottle throttle = new CraftRampThrottle(); RecipeId id = mock(RecipeId.class); - Tick tick = throttle.next(id); - for (int i = 0; i < 50; i++) { - tick = throttle.next(id); - } - + Tick tick = rampToFloor(throttle, id); assertEquals(CraftRampThrottle.FLOOR_DELAY_MS, tick.delayMs); - assertEquals(CraftRampThrottle.BULK_CRAFTS, tick.crafts); // batched once maxed out + assertEquals(CraftRampThrottle.BULK_CRAFTS, tick.crafts); } @Test @@ -60,22 +65,23 @@ class CraftRampThrottleTest { CraftRampThrottle throttle = new CraftRampThrottle(); RecipeId id = mock(RecipeId.class); - assertEquals(1, throttle.next(id).crafts); // 500 - assertEquals(1, throttle.next(id).crafts); // 425 - assertEquals(1, throttle.next(id).crafts); // 361 + assertEquals(1, throttle.next(id).crafts); + assertEquals(1, throttle.next(id).crafts); } @Test - @DisplayName("each recipe keeps its own ramp; returning resumes, not resets") - void perRecipeMemoryResumes() { + @DisplayName("recipe change reduces momentum but does not fully reset") + void recipeChangeReducesMomentum() { CraftRampThrottle throttle = new CraftRampThrottle(); RecipeId a = mock(RecipeId.class); RecipeId b = mock(RecipeId.class); - assertEquals(500L, throttle.next(a).delayMs); // a craft 1 - assertEquals(425L, throttle.next(a).delayMs); // a craft 2 - assertEquals(500L, throttle.next(b).delayMs); // b starts fresh - assertEquals(361L, throttle.next(a).delayMs); // a resumes where it left off - assertEquals(425L, throttle.next(b).delayMs); // b resumes its own ramp + // Ramp A to full speed (floor). + assertEquals(CraftRampThrottle.FLOOR_DELAY_MS, rampToFloor(throttle, a).delayMs); + + // Switching to B slows down, but keeps momentum: slower than floor, faster than a cold start. + long afterSwitch = throttle.next(b).delayMs; + assertTrue(afterSwitch > CraftRampThrottle.FLOOR_DELAY_MS, "switch should reduce momentum"); + assertTrue(afterSwitch < CraftRampThrottle.START_DELAY_MS, "switch should not fully reset"); } }