Skip to content

Commit

Permalink
Add asteroid summary and cache/improve recipe lookup for space mining (
Browse files Browse the repository at this point in the history
…#76)

Co-authored-by: Martin Robertz <[email protected]>
Co-authored-by: Maya <[email protected]>
Co-authored-by: chochem <[email protected]>
Co-authored-by: lordIcocain <[email protected]>
Co-authored-by: Alexdoru <[email protected]>
  • Loading branch information
6 people authored Dec 22, 2024
1 parent a57bc82 commit a60a6b8
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public static boolean addSpaceAssemblerRecipe(ItemStack[] aItemInputs, FluidStac
/**
* Add a Space Mining recipe
*
* @param asteroidName Unlocalized name of the asteroid
* @param aItemInputs Equipment used for the mining operation
* @param aFluidInputs Additional input fluids
* @param aChances Chances to get each ore type
Expand All @@ -128,9 +129,10 @@ public static boolean addSpaceAssemblerRecipe(ItemStack[] aItemInputs, FluidStac
* @param recipeWeight Weight of this recipe (Used in determining which recipe to execute)
* @return True if recipes could be added, else false
*/
public static boolean addSpaceMiningRecipe(ItemStack[] aItemInputs, FluidStack[] aFluidInputs, int[] aChances,
Materials[] ores, OrePrefixes orePrefixes, int minSize, int maxSize, int minDistance, int maxDistance,
int computationRequiredPerSec, int minModuleTier, int duration, int EUt, int recipeWeight) {
public static boolean addSpaceMiningRecipe(String asteroidName, ItemStack[] aItemInputs, FluidStack[] aFluidInputs,
int[] aChances, Materials[] ores, OrePrefixes orePrefixes, int minSize, int maxSize, int minDistance,
int maxDistance, int computationRequiredPerSec, int minModuleTier, int duration, int EUt,
int recipeWeight) {
if ((aItemInputs == null && aFluidInputs == null) || ores == null) {
return false;
}
Expand Down Expand Up @@ -176,6 +178,7 @@ public static boolean addSpaceMiningRecipe(ItemStack[] aItemInputs, FluidStack[]
IGRecipeMaps.spaceMiningRecipes.add(
new IG_SpaceMiningRecipe(
false,
asteroidName,
aItemInputs,
outputs,
aFluidInputs,
Expand All @@ -192,8 +195,8 @@ public static boolean addSpaceMiningRecipe(ItemStack[] aItemInputs, FluidStack[]
return true;
}

public static boolean addSpaceMiningRecipe(ItemStack[] aItemInputs, FluidStack[] aFluidInputs, int[] aChances,
ItemStack[] aItemOutputs, int minSize, int maxSize, int minDistance, int maxDistance,
public static boolean addSpaceMiningRecipe(String asteroidName, ItemStack[] aItemInputs, FluidStack[] aFluidInputs,
int[] aChances, ItemStack[] aItemOutputs, int minSize, int maxSize, int minDistance, int maxDistance,
int computationRequiredPerSec, int minModuleTier, int duration, int EUt, int recipeWeight) {
if ((aItemInputs == null && aFluidInputs == null) || aItemOutputs == null) {
return false;
Expand Down Expand Up @@ -232,6 +235,7 @@ public static boolean addSpaceMiningRecipe(ItemStack[] aItemInputs, FluidStack[]
IGRecipeMaps.spaceMiningRecipes.add(
new IG_SpaceMiningRecipe(
false,
asteroidName,
aItemInputs,
aItemOutputs,
aFluidInputs,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.gtnewhorizons.gtnhintergalactic.recipe;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

import gregtech.api.util.GTRecipe;
import gregtech.api.util.GTUtility;

/**
* Recipe for Space Mining in the Space Elevator
Expand All @@ -12,17 +19,20 @@
*/
public class IG_SpaceMiningRecipe extends GTRecipe {

public String asteroidName;
public int minDistance;
public int maxDistance;
public int minSize;
public int maxSize;
public int computation;
public int recipeWeight;

public IG_SpaceMiningRecipe(boolean aOptimize, ItemStack[] aItemInputs, ItemStack[] aItemOutputs,
FluidStack[] aFluidInputs, int[] aChances, int aDuration, int aEUt, int computation, int minModuleTier,
int minDistance, int maxDistance, int minSize, int maxSize, int recipeWeight) {
public IG_SpaceMiningRecipe(boolean aOptimize, String asteroidName, ItemStack[] aItemInputs,
ItemStack[] aItemOutputs, FluidStack[] aFluidInputs, int[] aChances, int aDuration, int aEUt,
int computation, int minModuleTier, int minDistance, int maxDistance, int minSize, int maxSize,
int recipeWeight) {
super(aOptimize, aItemInputs, aItemOutputs, null, aChances, aFluidInputs, null, aDuration, aEUt, minModuleTier);
this.asteroidName = asteroidName;
this.minDistance = minDistance;
this.maxDistance = maxDistance;
this.minSize = minSize;
Expand All @@ -31,6 +41,15 @@ public IG_SpaceMiningRecipe(boolean aOptimize, ItemStack[] aItemInputs, ItemStac
this.recipeWeight = recipeWeight;
}

/**
* Get the non localized name of the asteroid, can be used in nei and gui
*
* @return Asteroid Name
*/
public String getAsteroidName() {
return asteroidName;
}

/**
* Get the weight of the recipe
*
Expand All @@ -39,4 +58,63 @@ public IG_SpaceMiningRecipe(boolean aOptimize, ItemStack[] aItemInputs, ItemStac
public int getRecipeWeight() {
return recipeWeight;
}

public int hashCode() {
int res = 0;
res = 31 * res + minDistance;
res = 31 * res + maxDistance;
res = 31 * res + minSize;
res = 31 * res + maxSize;
res = 31 * res + computation;
res = 31 * res + recipeWeight;
res = 31 * res + mSpecialValue;
res = 31 * res + mDuration;
res = 31 * res + mEUt;
res = 31 * res + GTUtility.ItemId.createWithoutNBT(mInputs[0]).hashCode();
// We don't care about the order of the output items, so we compute the first five sums
// of powers of the hashes of the items. This is obviously order invariant, but highly sensitive
// to changes of item hashes, which is what we want. Five is more than we need but whatever
int[] moments = Arrays.stream(mOutputs).reduce(new int[5], (a, item) -> {
int ph = item == null ? 7 : GTUtility.ItemId.createNoCopy(item).hashCode();
int x = ph;
for (int i = 0; i < a.length; ++i) {
a[i] += x;
x *= ph;
}
return a;
}, (a, b) -> {
for (int i = 0; i < a.length; ++i) {
a[i] += b[i];
}
return a;
});
for (int ph : moments) {
res = 31 * res + ph;
}
return res;
}

public boolean equals(Object _other) {
if (!(_other instanceof IG_SpaceMiningRecipe)) {
return false;
}
IG_SpaceMiningRecipe other = (IG_SpaceMiningRecipe) _other;
if (minDistance != other.minDistance || maxDistance != other.maxDistance
|| minSize != other.minSize
|| maxSize != other.maxSize
|| computation != other.computation
|| recipeWeight != other.recipeWeight
|| mSpecialValue != other.mSpecialValue
|| mEUt != other.mEUt) {
return false;
}
Collector<ItemStack, ?, Map<GTUtility.ItemId, Long>> collector = Collectors
.toMap(GTUtility.ItemId::createNoCopy, input -> (long) input.stackSize, (a, b) -> a + b);
if (!Arrays.stream(mInputs).filter(Objects::nonNull).collect(collector)
.equals(Arrays.stream(other.mInputs).filter(Objects::nonNull).collect(collector))) {
return false;
}
return Arrays.stream(mOutputs).filter(Objects::nonNull).collect(collector)
.equals(Arrays.stream(other.mOutputs).filter(Objects::nonNull).collect(collector));
}
}
Loading

0 comments on commit a60a6b8

Please sign in to comment.