From 9ce2aa40bb7db5dfbe0d65527995346000434464 Mon Sep 17 00:00:00 2001 From: Caedis Date: Fri, 17 Jul 2026 13:58:14 -0500 Subject: [PATCH] feat(autocraft): add CraftRampThrottle ramp math Pure per-craft delay ramp for throttling auto-craft: first craft instant, then geometric decay from 500ms toward a 50ms floor, resetting when the RecipeId changes. Wired into AutoCrafting in a later change. --- .../nei/recipe/CraftRampThrottle.java | 33 ++++++++++++ .../nei/recipe/CraftRampThrottleTest.java | 52 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/main/java/codechicken/nei/recipe/CraftRampThrottle.java create mode 100644 src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java diff --git a/src/main/java/codechicken/nei/recipe/CraftRampThrottle.java b/src/main/java/codechicken/nei/recipe/CraftRampThrottle.java new file mode 100644 index 00000000..378240c2 --- /dev/null +++ b/src/main/java/codechicken/nei/recipe/CraftRampThrottle.java @@ -0,0 +1,33 @@ +package codechicken.nei.recipe; + +import java.util.Objects; + +import codechicken.nei.recipe.Recipe.RecipeId; + +/** + * Per-craft delay ramp for auto-crafting. First craft of a recipe is instant, + * then the delay decays geometrically toward a floor. Changing the recipe + * resets the ramp. Pure logic; does not sleep. + */ +public class CraftRampThrottle { + + public static final long START_DELAY_MS = 500L; // delay before the 2nd craft + public static final long FLOOR_DELAY_MS = 50L; // fastest allowed (cap) + public static final double DECAY = 0.85D; + + private RecipeId current; + private long delayMs; + + /** Delay to wait before the next craft of {@code id}; 0 for the first / after a change. */ + public long nextDelayMs(RecipeId id) { + if (!Objects.equals(id, this.current)) { + this.current = id; + this.delayMs = START_DELAY_MS; + return 0L; + } + + final long delay = this.delayMs; + this.delayMs = Math.max(FLOOR_DELAY_MS, Math.round(this.delayMs * DECAY)); + return delay; + } +} diff --git a/src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java b/src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java new file mode 100644 index 00000000..e4b048a9 --- /dev/null +++ b/src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java @@ -0,0 +1,52 @@ +package codechicken.nei.recipe; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import codechicken.nei.recipe.Recipe.RecipeId; + +class CraftRampThrottleTest { + + @Test + @DisplayName("first craft of a recipe is instant") + void firstCraftInstant() { + CraftRampThrottle throttle = new CraftRampThrottle(); + RecipeId id = mock(RecipeId.class); + assertEquals(0L, throttle.nextDelayMs(id)); + } + + @Test + @DisplayName("same recipe decays geometrically and clamps at floor") + void rampDecaysToFloor() { + CraftRampThrottle throttle = new CraftRampThrottle(); + RecipeId id = mock(RecipeId.class); + + assertEquals(0L, throttle.nextDelayMs(id)); // craft 1: instant + assertEquals(500L, throttle.nextDelayMs(id)); // craft 2: START + assertEquals(425L, throttle.nextDelayMs(id)); // 500 * 0.85 + assertEquals(361L, throttle.nextDelayMs(id)); // round(425 * 0.85) + + long last = 361L; + for (int i = 0; i < 50; i++) { + last = throttle.nextDelayMs(id); + } + assertEquals(CraftRampThrottle.FLOOR_DELAY_MS, last); // clamped at floor + } + + @Test + @DisplayName("changing recipe resets the ramp to instant") + void recipeChangeResets() { + CraftRampThrottle throttle = new CraftRampThrottle(); + RecipeId a = mock(RecipeId.class); + RecipeId b = mock(RecipeId.class); + + assertEquals(0L, throttle.nextDelayMs(a)); // a craft 1 + assertEquals(500L, throttle.nextDelayMs(a)); // a craft 2 + assertEquals(0L, throttle.nextDelayMs(b)); // switch -> instant + assertEquals(500L, throttle.nextDelayMs(b)); // b craft 2 + assertEquals(0L, throttle.nextDelayMs(a)); // switch back -> instant again + } +}