Skip to content
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

fix: resources not being used up correctly when there are missing items #756

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ CalculationResult calculate() {
result = CalculationResult.MISSING_RESOURCES;
}
}
if (result == CalculationResult.SUCCESS) {
craftingState.addOutputsToInternalStorage(pattern, amount);
}
craftingState.addOutputsToInternalStorage(pattern, amount);
return result;
}

Expand All @@ -81,9 +79,21 @@ private CalculationResult calculateIngredient(final IngredientState ingredientSt
remaining -= toTake;
}
if (remaining > 0) {
resourceState = tryCalculateChild(ingredientState, resourceState, remaining);
if (resourceState == null) {
final CraftingState.ResourceState newState = tryCalculateChild(
ingredientState,
resourceState,
remaining
);
if (newState == null) {
// If we end up with missing resources, we need to use up all the resulting
// resources from the internal storage so that it cannot be used later for other ingredients
// that happen to use the same resource.
// The goal was to use up the resources created by the child calculation in the next iteration,
// but since we have missing resources, we need to use them up now.
craftingState.extractFromInternalStorage(resourceState.resource(), remaining);
return CalculationResult.MISSING_RESOURCES;
} else {
resourceState = newState;
}
}
}
Expand Down Expand Up @@ -116,14 +126,14 @@ private CraftingState.ResourceState calculateChild(final IngredientState ingredi
final CraftingState.ResourceState resourceState) {
final ChildCalculationResult<T> result = calculateChild(remaining, childPatterns, resourceState);
if (result.success) {
this.craftingState = result.childCraftingState;
this.craftingState = result.childTree.craftingState;
final CraftingState.ResourceState updatedResourceState = craftingState.getResource(
resourceState.resource()
);
listener.childCalculationCompleted(
updatedResourceState.resource(),
updatedResourceState.inInternalStorage(),
result.childListener
result.childTree.listener
);
return updatedResourceState;
}
Expand Down Expand Up @@ -153,37 +163,34 @@ private ChildCalculationResult<T> calculateChild(final long remaining,
return new ChildCalculationResult<>(
true,
craftingState.getResource(resourceState.resource()).inInternalStorage(),
childTree.listener,
childTree.craftingState
childTree
);
}
return new ChildCalculationResult<>(
false,
requireNonNull(lastChildAmount).getTotal(),
requireNonNull(lastChildTree).listener,
lastChildTree.craftingState
requireNonNull(lastChildTree)
);
}

@Nullable
private CraftingState.ResourceState cycleToNextIngredientOrFail(final IngredientState ingredientState,
final CraftingState.ResourceState resourceState,
final ChildCalculationResult<T> result) {
final ChildCalculationResult<T> childResult) {
return ingredientState.cycle().map(craftingState::getResource).orElseGet(() -> {
this.craftingState = result.childCraftingState;
this.craftingState = childResult.childTree.craftingState;
listener.childCalculationCompleted(
resourceState.resource(),
result.amountCrafted,
result.childListener
childResult.amountCrafted,
childResult.childTree.listener
);
return null;
});
}

private record ChildCalculationResult<T>(boolean success,
long amountCrafted,
CraftingCalculatorListener<T> childListener,
CraftingState childCraftingState) {
CraftingTree<T> childTree) {
}

enum CalculationResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ void shouldCalculateForSingleRootPatternSingleIngredientAndAllResourcesAreAvaila
.build());
}

@ParameterizedTest
@ValueSource(longs = {1, 2})
void shouldCalculateForSingleRootPatternSingleIngredientSpreadOutOverMultipleIngredientsAndThereAreMissingResources(
final long requestedAmount
) {
// Arrange
final RootStorage storage = storage();
final PatternRepository patterns = patterns(
pattern()
.ingredient(OAK_LOG, 1)
.output(OAK_PLANKS, 4)
.build(),
pattern()
.ingredient(OAK_PLANKS, 1)
.ingredient(OAK_PLANKS, 1)
.ingredient(OAK_PLANKS, 1)
.ingredient(OAK_PLANKS, 1)
.output(CRAFTING_TABLE, 1)
.build()
);
final CraftingCalculator sut = new CraftingCalculatorImpl(patterns, storage);

// Act
final Preview preview = calculateAndGetPreview(sut, CRAFTING_TABLE, requestedAmount);

// Assert
assertThat(preview).usingRecursiveComparison(PREVIEW_CONFIG).isEqualTo(PreviewBuilder.ofType(MISSING_RESOURCES)
.addToCraft(CRAFTING_TABLE, requestedAmount)
.addToCraft(OAK_PLANKS, requestedAmount * 4)
.addMissing(OAK_LOG, requestedAmount)
.build());
}


@Test
void shouldNotCalculateForSingleRootPatternSingleIngredientAndAlmostAllResourcesAreAvailable() {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CraftingPattern implements Pattern {
this.output = output;
this.inputResources = inputs.stream().flatMap(List::stream).collect(Collectors.toSet());
this.outputResources = Set.of(output.resource());
this.ingredients = inputs.stream().map(i -> new Ingredient(1, i)).toList();
this.ingredients = inputs.stream().map(i -> new Ingredient(i.isEmpty() ? 0 : 1, i)).toList();
this.outputs = Stream.concat(Stream.of(output), byproducts.stream()).toList();
}

Expand Down
Loading