Skip to content

Commit

Permalink
🥃 Basin/Vat item count disparity
Browse files Browse the repository at this point in the history
  • Loading branch information
petrolpark committed Sep 30, 2024
1 parent 1678bb3 commit fb076b5
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void tick() {
boolean shouldUpdateFluidMixture = false;
Vat vat = getVatOptional().get();
if (tankBehaviour.isEmpty()) return;
double fluidAmount = getCapacity() / Constants.MILLIBUCKETS_PER_LITER; // Converts getFluidAmount() in mB to Buckets
double fluidAmount = getCapacity() / Constants.MILLIBUCKETS_PER_LITER; // Converts getFluidAmount() in mB to liters

int cyclesPerTick = getSimulationLevel();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Optional;

import com.petrolpark.destroy.block.ArcFurnaceLidBlock;
import com.petrolpark.destroy.block.DynamoBlock;
import com.petrolpark.destroy.block.VatControllerBlock;
import com.petrolpark.destroy.block.VatSideBlock;
import com.petrolpark.destroy.block.entity.DestroyBlockEntityTypes;
Expand Down Expand Up @@ -57,7 +59,14 @@ private static BlockMovementChecks.CheckResult attachedCheckVats(BlockState stat
return BlockMovementChecks.CheckResult.PASS;
};

public static BlockMovementChecks.CheckResult attachedCheckArcFurnace(BlockState state, Level level, BlockPos pos, Direction attached) {
if (state.getBlock() instanceof DynamoBlock && state.getValue(DynamoBlock.ARC_FURNACE) && attached == Direction.DOWN) return BlockMovementChecks.CheckResult.SUCCESS;
if (state.getBlock() instanceof ArcFurnaceLidBlock && attached == Direction.UP) return BlockMovementChecks.CheckResult.SUCCESS;
return BlockMovementChecks.CheckResult.PASS;
};

public static void register() {
BlockMovementChecks.registerAttachedCheck(DestroyMovementChecks::attachedCheckVats);
BlockMovementChecks.registerAttachedCheck(DestroyMovementChecks::attachedCheckArcFurnace);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -568,28 +568,28 @@ public List<ItemStack> dissolveItems(ReactionContext context, double volume) {
@Deprecated
public int recalculateVolume(int initialVolume) {
if (contents.isEmpty()) return 0;
double initialVolumeInBuckets = (double)initialVolume / 1000d;
double newVolumeInBuckets = 0d;
double initialVolumeInLiters = (double)initialVolume / Constants.MILLIBUCKETS_PER_LITER;
double newVolumeInLiters = 0d;

// Molecules
Map<LegacySpecies, Double> molesOfMolecules = new HashMap<>();
for (Entry<LegacySpecies, Float> entry : contents.entrySet()) {
LegacySpecies molecule = entry.getKey();
double molesOfMolecule = entry.getValue() * initialVolumeInBuckets;
double molesOfMolecule = entry.getValue() * initialVolumeInLiters;
molesOfMolecules.put(molecule, molesOfMolecule);
newVolumeInBuckets += molesOfMolecule / molecule.getPureConcentration();
newVolumeInLiters += molesOfMolecule / molecule.getPureConcentration();
};
for (Entry<LegacySpecies, Double> entry : molesOfMolecules.entrySet()) {
contents.replace(entry.getKey(), (float)(entry.getValue() / newVolumeInBuckets));
contents.replace(entry.getKey(), (float)(entry.getValue() / newVolumeInLiters));
};

// Results
Map<ReactionResult, Float> resultsCopy = new HashMap<>(reactionResults);
for (Entry<ReactionResult, Float> entry : resultsCopy.entrySet()) {
reactionResults.replace(entry.getKey(), (float)(entry.getValue() * initialVolumeInBuckets / newVolumeInBuckets));
reactionResults.replace(entry.getKey(), (float)(entry.getValue() * initialVolumeInLiters / newVolumeInLiters));
};

return (int)((newVolumeInBuckets * 1000));
return (int)((newVolumeInLiters * Constants.MILLIBUCKETS_PER_LITER));
};

/**
Expand Down Expand Up @@ -718,7 +718,7 @@ protected void incrementReactionResults(LegacyReaction reaction, float molesPerB
* {@link LegacyMixture#reactForTick React} this Mixture until it reaches {@link LegacyMixture#equilibrium equilibrium}. This is mutative.
* @return A {@link com.petrolpark.destroy.recipe.ReactionInBasinRecipe.ReactionInBasinResult ReactionInBasinResult} containing
* the number of ticks it took to reach equilibrium, the {@link ReactionResult Reaction Results} and the new volume of Mixture.
* @param volume (in mB) of this Reaction
* @param volume (in liters) of this Reaction
* @param availableStacks Item Stacks available for reacting. This List and its contents will be modified.
* @param heatingPower The power being supplied to this Basin by the {@link com.petrolpark.destroy.util.vat.IVatHeaterBlock heater} below it.
* @param outsideTemperature The {@link com.petrolpark.destroy.capability.Pollution#getLocalTemperature temperature} outside the Basin.
Expand All @@ -743,7 +743,7 @@ public ReactionInBasinResult reactInBasin(int volume, List<ItemStack> availableS

int amount = recalculateVolume(volume);

return new ReactionInBasinResult(ticks, getCompletedResults(volumeInLiters), amount);
return new ReactionInBasinResult(ticks, getCompletedResults(amount), amount);
};

/**
Expand All @@ -762,12 +762,12 @@ public Map<ReactionResult, Integer> getCompletedResults(double volumeInLiters) {
continue;
};

float molesPerBucketOfReaction = reactionResults.get(result);
int numberOfResult = (int) (volumeInLiters * molesPerBucketOfReaction / result.getRequiredMoles());
float molesPerLiterOfReaction = reactionResults.get(result);
int numberOfResult = (int) (volumeInLiters * molesPerLiterOfReaction / result.getRequiredMoles());
if (numberOfResult == 0) continue;

// Decrease the amount of Reaction that has happened
reactionResults.replace(result, molesPerBucketOfReaction - numberOfResult * result.getRequiredMoles() / (float)volumeInLiters);
reactionResults.replace(result, molesPerLiterOfReaction - numberOfResult * result.getRequiredMoles() / (float)volumeInLiters);

results.put(result, numberOfResult);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean isOneOff() {
*/
public abstract void onVatReaction(Level level, VatControllerBlockEntity vatController);

public Collection<PrecipitateReactionResult> getPrecipitatesForJEI() {
public Collection<PrecipitateReactionResult> getAllPrecipitates() {
return Collections.emptySet();
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public void onVatReaction(Level level, VatControllerBlockEntity vatController) {
};

@Override
public Collection<PrecipitateReactionResult> getPrecipitatesForJEI() {
return childResults.stream().flatMap(r -> r.getPrecipitatesForJEI().stream()).toList();
public Collection<PrecipitateReactionResult> getAllPrecipitates() {
return childResults.stream().flatMap(r -> r.getAllPrecipitates().stream()).toList();
};

};
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void onVatReaction(Level level, VatControllerBlockEntity vatController) {
};

@Override
public Collection<PrecipitateReactionResult> getPrecipitatesForJEI() {
public Collection<PrecipitateReactionResult> getAllPrecipitates() {
return Collections.singleton(this);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void setRecipe(IRecipeLayoutBuilder builder, T recipe, IFocusGroup focuse
};
};

Collection<PrecipitateReactionResult> precipitates = reaction.hasResult() ? reaction.getResult().getPrecipitatesForJEI() : Collections.emptyList();
Collection<PrecipitateReactionResult> precipitates = reaction.hasResult() ? reaction.getResult().getAllPrecipitates() : Collections.emptyList();

int j = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public <T, V> List<T> getRecipes(IRecipeCategory<T> recipeCategory, IFocus<V> fo
if (reaction.getItemReactants().stream().anyMatch(ir -> ((ir.isCatalyst() && searchCatalysts) || (!ir.isCatalyst() && searchInputs)) && ir.isItemValid(stack))) return true;

// Check for precipitates
if (searchOutputs && reaction.hasResult()) reaction.getResult().getPrecipitatesForJEI().stream().anyMatch(p -> ItemStack.matches(p.getPrecipitate(), stack));
if (searchOutputs && reaction.hasResult()) reaction.getResult().getAllPrecipitates().stream().anyMatch(p -> ItemStack.matches(p.getPrecipitate(), stack));

return false;
// Unchecked conversion (but actually it is checked)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import com.petrolpark.destroy.Destroy;
import com.petrolpark.destroy.block.entity.behaviour.ExtendedBasinBehaviour;
import com.petrolpark.destroy.capability.Pollution;
import com.petrolpark.destroy.chemistry.api.util.Constants;
import com.petrolpark.destroy.chemistry.legacy.LegacyMixture;
import com.petrolpark.destroy.chemistry.legacy.ReactionResult;
import com.petrolpark.destroy.chemistry.legacy.LegacyMixture.Phases;
import com.petrolpark.destroy.chemistry.legacy.reactionresult.CombinedReactionResult;
import com.petrolpark.destroy.chemistry.legacy.reactionresult.PrecipitateReactionResult;
import com.petrolpark.destroy.fluid.DestroyFluids;
import com.petrolpark.destroy.fluid.MixtureFluid;
import com.petrolpark.destroy.util.ItemHelper;
import com.petrolpark.destroy.util.vat.IVatHeaterBlock;
import com.simibubi.create.content.processing.basin.BasinBlockEntity;
import com.simibubi.create.content.processing.basin.BasinRecipe;
Expand Down Expand Up @@ -87,7 +89,7 @@ public static ReactionInBasinRecipe create(Collection<FluidStack> availableFluid

int amount = fluidStack.getAmount();
totalAmount += amount;
mixtures.put(mixture, (double)amount);
mixtures.put(mixture, (double)amount / Constants.MILLIBUCKETS_PER_LITER);
};

if (!containsMixtures) canReact = false; // Don't react without Mixtures, even if there are fluids which could be converted into Mixtures
Expand Down Expand Up @@ -136,7 +138,7 @@ public static ReactionInBasinRecipe create(Collection<FluidStack> availableFluid

Map<ReactionResult, Integer> reactionResults = new HashMap<>();

gatherReactionResults(result.reactionresults(), reactionResults, builder); // Gather all
gatherReactionResults(result.reactionResults(), reactionResults, builder); // Gather all

ExtendedBasinBehaviour behaviour = basin.getBehaviour(ExtendedBasinBehaviour.TYPE);
behaviour.setReactionResults(reactionResults); // Schedule the Reaction Results to occur once the Mixing has finished
Expand All @@ -162,7 +164,8 @@ private static void gatherReactionResults(Map<ReactionResult, Integer> resultsOf
};
gatherReactionResults(childMap, resultsToEnact, builder);
} else if (reactionresult instanceof PrecipitateReactionResult precipitationResult) {
builder.output(precipitationResult.getPrecipitate());
ItemStack precipitate = precipitationResult.getPrecipitate();
ItemHelper.withCount(precipitate, resultsOfReaction.get(reactionresult) * precipitate.getCount()).forEach(builder::output);
} else { // Don't deal with precipitations in the normal way
resultsToEnact.put(reactionresult, resultsOfReaction.get(reactionresult));
};
Expand All @@ -177,9 +180,9 @@ protected int getMaxFluidInputCount() {
/**
* The outcome of {@link com.petrolpark.destroy.chemistry.legacy.LegacyReaction reacting} a {@link com.petrolpark.destroy.chemistry.legacy.LegacyReaction Mixture} in a Basin.
* @param ticks The number of ticks it took for the Mixture to reach equilibrium
* @param reactionresults The {@link com.petrolpark.destroy.chemistry.legacy.ReactionResult results} of Reacting this Mixture
* @param reactionResults The {@link com.petrolpark.destroy.chemistry.legacy.ReactionResult results} of Reacting this Mixture
* @param amount The amount (in mB) of resultant Mixture
*/
public static record ReactionInBasinResult(int ticks, Map<ReactionResult, Integer> reactionresults, int amount) {};
public static record ReactionInBasinResult(int ticks, Map<ReactionResult, Integer> reactionResults, int amount) {};

};
22 changes: 22 additions & 0 deletions src/main/java/com/petrolpark/destroy/util/ItemHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.petrolpark.destroy.util;

import java.util.Collection;
import java.util.List;
import java.util.ArrayList;

import net.minecraft.world.item.ItemStack;

public class ItemHelper {

/**
* Copies an Item in an ItemStack a given number of times.
* If the number exceeds the maximum stack size of the Item, it is split into multiple Item Stacks.
*/
public static Collection<ItemStack> withCount(ItemStack stack, int count) {
int maxStackSize = stack.getMaxStackSize();
List<ItemStack> stacks = new ArrayList<>(1 + (count / maxStackSize));
for (int i = 0; i < count / maxStackSize; i++) stacks.add(stack.copyWithCount(maxStackSize));
stacks.add(stack.copyWithCount(count % maxStackSize));
return stacks;
};
};

0 comments on commit fb076b5

Please sign in to comment.