mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-19 20:00:00 +00:00
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.
This commit is contained in:
2 files changed
+85
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user