-
Notifications
You must be signed in to change notification settings - Fork 176
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
[WIP] initial change for fixing round robin voiding` #2601
Open
jkieberking
wants to merge
1
commit into
GregTechCEu:master
Choose a base branch
from
jkieberking:pipe-voiding-fix
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.
Open
Changes from all commits
Commits
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 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
This file contains 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package gregtech.common.pipelike.itempipe.net; | ||
|
||
import gregtech.api.capability.GregtechTileCapabilities; | ||
import gregtech.api.capability.impl.ItemHandlerProxy; | ||
import gregtech.api.cover.Cover; | ||
import gregtech.api.cover.CoverHolder; | ||
import gregtech.api.util.FacingPos; | ||
|
@@ -30,13 +31,16 @@ | |
import java.util.Comparator; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.OptionalInt; | ||
|
||
public class ItemNetHandler implements IItemHandler { | ||
|
||
private ItemPipeNet net; | ||
private TileEntityItemPipe pipe; | ||
private final EnumFacing facing; | ||
private final Object2IntMap<FacingPos> simulatedTransfersGlobalRoundRobin = new Object2IntOpenHashMap<>(); | ||
// when doing a non-global dry run, this keeps track of the capacity of each route on the network | ||
private final Object2IntMap<FacingPos> simulatedRemainingCapacityRoundRobin = new Object2IntOpenHashMap<>(); | ||
private int simulatedTransfers = 0; | ||
private final ItemStackHandler testHandler = new ItemStackHandler(1); | ||
|
||
|
@@ -132,9 +136,14 @@ public ItemStack insertRoundRobin(ItemStack stack, boolean simulate, boolean glo | |
if (global) { | ||
stack = insertToHandlersEnhanced(routePathsCopy, stack, routePaths.size(), simulate); | ||
} else { | ||
stack = insertToHandlers(routePathsCopy, stack, simulate); | ||
if (!stack.isEmpty() && !routePathsCopy.isEmpty()) | ||
stack = insertToHandlers(routePathsCopy, stack, simulate); | ||
stack = insertEquallyToHandlers(routePathsCopy, stack, simulate); | ||
// check if we have any remaining amount that can be inserted, | ||
// and that we have routes that have free space available | ||
if (!stack.isEmpty() && !routePathsCopy.isEmpty()) { | ||
// try to insert the remaining amount | ||
stack = insertEquallyToHandlers(routePathsCopy, stack, simulate); | ||
} | ||
simulatedRemainingCapacityRoundRobin.clear(); | ||
} | ||
|
||
return stack; | ||
|
@@ -144,39 +153,65 @@ public ItemStack insertRoundRobin(ItemStack stack, boolean simulate, boolean glo | |
* Inserts items equally to all handlers | ||
* if it couldn't insert all items, the handler will be removed | ||
* | ||
* @param copy to insert to | ||
* @param stack to insert | ||
* @param simulate simulate | ||
* @param copy where to insert to | ||
* @param stack what to insert | ||
* @param simulate whether to actually insert or not | ||
* @return remainder | ||
*/ | ||
private ItemStack insertToHandlers(List<ItemRoutePath> copy, ItemStack stack, boolean simulate) { | ||
private ItemStack insertEquallyToHandlers(List<ItemRoutePath> copy, ItemStack stack, boolean simulate) { | ||
Iterator<ItemRoutePath> routePathIterator = copy.listIterator(); | ||
int inserted = 0; | ||
int count = stack.getCount(); | ||
int c = count / copy.size(); | ||
int m = c == 0 ? count % copy.size() : 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does anyone know what |
||
int amountToInsertInEach = count / copy.size(); | ||
int m = amountToInsertInEach == 0 ? count % copy.size() : 0; | ||
while (routePathIterator.hasNext()) { | ||
ItemRoutePath routePath = routePathIterator.next(); | ||
FacingPos routeFacing = routePath.toFacingPos(); | ||
|
||
int amount = c; | ||
int remainingAmount = amountToInsertInEach; | ||
if (m > 0) { | ||
amount++; | ||
remainingAmount++; | ||
m--; | ||
} | ||
amount = Math.min(amount, stack.getCount() - inserted); | ||
if (amount == 0) break; | ||
remainingAmount = Math.min(remainingAmount, stack.getCount() - inserted); | ||
|
||
// if we know the capacity from a previous dry run, subtract that from the amount we can insert in the route | ||
Integer capacityLeftAfterPreviousDryRun = simulatedRemainingCapacityRoundRobin.getOrDefault(routeFacing, null); | ||
if (capacityLeftAfterPreviousDryRun != null) { | ||
remainingAmount = Math.min(remainingAmount, capacityLeftAfterPreviousDryRun); | ||
} | ||
|
||
if (remainingAmount == 0) break; | ||
ItemStack toInsert = stack.copy(); | ||
toInsert.setCount(amount); | ||
int r = insert(routePath, toInsert, simulate).getCount(); | ||
if (r < amount) { | ||
inserted += (amount - r); | ||
toInsert.setCount(remainingAmount); | ||
int remainingAfterSubrouteInsert = insert(routePath, toInsert, simulate).getCount(); | ||
int amountInsertedIntoSubroute = remainingAmount - remainingAfterSubrouteInsert; | ||
|
||
// if we inserted anything into the subroute, add it to the inserted amount tally | ||
if (amountInsertedIntoSubroute != 0) { | ||
inserted += (remainingAmount - remainingAfterSubrouteInsert); | ||
} | ||
if (r == 1 && c == 0 && amount == 1) { | ||
if (remainingAfterSubrouteInsert == 1 && amountToInsertInEach == 0 && remainingAmount == 1) { | ||
m++; | ||
} | ||
|
||
if (r > 0) | ||
// if we can't insert anything more into the sub route, remove it from the iterator | ||
if (remainingAfterSubrouteInsert > 0) { | ||
routePathIterator.remove(); | ||
} else { | ||
int freeInventoryInSubRoute = 0; | ||
int slots = routePath.getHandler().getSlots(); | ||
|
||
for (int i = 0; i < slots; i++) { | ||
int slotLimit = routePath.getHandler().getSlotLimit(i); | ||
ItemStack testStackToInsert = stack.copy(); | ||
testStackToInsert.setCount(slotLimit); | ||
ItemStack insertedTest = routePath.getHandler().insertItem(i, testStackToInsert, true); | ||
ItemStack stackInSlot = routePath.getHandler().getStackInSlot(i); | ||
freeInventoryInSubRoute += slotLimit - insertedTest.getCount(); | ||
} | ||
simulatedRemainingCapacityRoundRobin.put(routePath.toFacingPos(), Integer.valueOf(freeInventoryInSubRoute - amountInsertedIntoSubroute)); | ||
} | ||
} | ||
|
||
ItemStack remainder = stack.copy(); | ||
|
Oops, something went wrong.
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.
not sure how this got here, will remove it