forked from Chicken-Bones/NotEnoughItems
-
Notifications
You must be signed in to change notification settings - Fork 129
Add ramp up based crafting throttle to autocrafting #987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Caedis
wants to merge
9
commits into
master
Choose a base branch
from
caedis/autocraft-throttle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9ce2aa4
feat(autocraft): add CraftRampThrottle ramp math
Caedis a40289c
feat(autocraft): throttle crafting with speed ramp
Caedis cbaef9c
fix(autocraft): stop when crafted output cannot be taken
Caedis 502a688
feat(autocraft): per-recipe speed memory and bulk at max
Caedis 540caea
feat(autocraft): drop instant first craft
Caedis 6ae40a1
feat(autocraft): global momentum with switch penalty
Caedis 75e480f
Merge remote-tracking branch 'origin/master' into caedis/autocraft-th…
Caedis 30eec77
spotless
Caedis cced6fa
Merge branch 'master' into caedis/autocraft-throttle
Caedis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/codechicken/nei/recipe/CraftRampThrottle.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package codechicken.nei.recipe; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import codechicken.nei.recipe.Recipe.RecipeId; | ||
|
|
||
| /** | ||
| * 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 = 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 { | ||
|
|
||
| public final long delayMs; | ||
| public final int crafts; | ||
|
|
||
| Tick(long delayMs, int crafts) { | ||
| this.delayMs = delayMs; | ||
| this.crafts = crafts; | ||
| } | ||
| } | ||
|
|
||
| private RecipeId current; | ||
| private long delayMs = START_DELAY_MS; | ||
|
|
||
| /** Next tick for {@code id}: decays on repeat, slows (but keeps momentum) on a recipe change. */ | ||
| public Tick next(RecipeId id) { | ||
| 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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| 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; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import codechicken.nei.recipe.CraftRampThrottle.Tick; | ||
| 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() { | ||
| CraftRampThrottle throttle = new CraftRampThrottle(); | ||
| RecipeId id = mock(RecipeId.class); | ||
|
|
||
| Tick tick = throttle.next(id); | ||
| assertEquals(CraftRampThrottle.START_DELAY_MS, tick.delayMs); | ||
| assertEquals(1, tick.crafts); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("same recipe decays monotonically and clamps at floor") | ||
| void rampDecaysToFloor() { | ||
| CraftRampThrottle throttle = new CraftRampThrottle(); | ||
| RecipeId id = mock(RecipeId.class); | ||
|
|
||
| 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, rampToFloor(throttle, id).delayMs); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("at floor speed, crafts happen in bulk") | ||
| void bulkAtFloorSpeed() { | ||
| CraftRampThrottle throttle = new CraftRampThrottle(); | ||
| RecipeId id = mock(RecipeId.class); | ||
|
|
||
| Tick tick = rampToFloor(throttle, id); | ||
| assertEquals(CraftRampThrottle.FLOOR_DELAY_MS, tick.delayMs); | ||
| assertEquals(CraftRampThrottle.BULK_CRAFTS, tick.crafts); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("crafts stay single while still ramping") | ||
| void singleWhileRamping() { | ||
| CraftRampThrottle throttle = new CraftRampThrottle(); | ||
| RecipeId id = mock(RecipeId.class); | ||
|
|
||
| assertEquals(1, throttle.next(id).crafts); | ||
| assertEquals(1, throttle.next(id).crafts); | ||
| } | ||
|
|
||
| @Test | ||
| @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); | ||
|
|
||
| // 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"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This definitely needs to be config driven
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are still discussing this in meta-dev
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear not necessarily "these five values" more having more than one option here