Close #703 Improve the probability system for centrifuge outputs

This commit is contained in:
mezz committed 2015-06-07 01:21:30 -07:00
1 parent a2944ce4e6
commit ad1773a7f7
6 files changed
+255 -145

No files matched your search

@@ -9,6 +9,7 @@ import net.minecraftforge.fluids.FluidStack;
import forestry.api.recipes.ICarpenterManager;
import forestry.api.recipes.ICentrifugeManager;
import forestry.api.recipes.ICentrifugeRecipe;
import forestry.api.recipes.IFabricatorManager;
import forestry.api.recipes.IFermenterManager;
import forestry.api.recipes.IMoistenerManager;
@@ -42,6 +43,16 @@ public class DummyManagers {
public static class CentrifugeManager implements ICentrifugeManager {
@Override
public void addRecipe(ICentrifugeRecipe recipe) {
}
@Override
public void addRecipe(int timePerItem, ItemStack resource, Map<ItemStack, Float> products) {
}
@Override
public void addRecipe(int timePerItem, ItemStack resource, HashMap<ItemStack, Integer> products) {
@@ -10,11 +10,15 @@
******************************************************************************/
package forestry.factory.gadgets;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.Stack;
@@ -33,6 +37,7 @@ import cpw.mods.fml.common.Optional;
import forestry.api.core.ForestryAPI;
import forestry.api.recipes.ICentrifugeManager;
import forestry.api.recipes.ICentrifugeRecipe;
import forestry.core.EnumErrorCode;
import forestry.core.config.Config;
import forestry.core.config.Defaults;
@@ -55,93 +60,8 @@ public class MachineCentrifuge extends TilePowered implements ISidedInventory {
public static final int SLOT_PRODUCT_1 = 1;
public static final int SLOT_PRODUCT_COUNT = 9;
/* RECIPE MANAGMENT */
public static class Recipe {
public final int timePerItem;
public final ItemStack resource;
public final HashMap<ItemStack, Integer> products;
public Recipe(int timePerItem, ItemStack resource, HashMap<ItemStack, Integer> products) {
this.timePerItem = timePerItem;
this.resource = resource;
this.products = products;
for (ItemStack item : products.keySet()) {
if (item == null) {
throw new IllegalArgumentException("Tried to register a null product of " + resource);
}
}
}
public boolean matches(ItemStack res) {
return StackUtils.isCraftingEquivalent(resource, res);
}
}
public static class RecipeManager implements ICentrifugeManager {
public static final ArrayList<MachineCentrifuge.Recipe> recipes = new ArrayList<MachineCentrifuge.Recipe>();
@Override
public void addRecipe(int timePerItem, ItemStack resource, HashMap<ItemStack, Integer> products) {
recipes.add(new Recipe(timePerItem, resource, products));
}
@Override
public void addRecipe(int timePerItem, ItemStack resource, ItemStack[] produce, int[] chances) {
HashMap<ItemStack, Integer> products = new HashMap<ItemStack, Integer>();
int i = 0;
for (ItemStack prod : produce) {
products.put(prod, chances[i]);
i++;
}
addRecipe(timePerItem, resource, products);
}
@Override
public void addRecipe(int timePerItem, ItemStack resource, ItemStack primary, ItemStack secondary, int chance) {
HashMap<ItemStack, Integer> products = new HashMap<ItemStack, Integer>();
products.put(primary, 100);
if (secondary != null) {
products.put(secondary, chance);
}
addRecipe(timePerItem, resource, products);
}
@Override
public void addRecipe(int timePerItem, ItemStack resource, ItemStack primary) {
HashMap<ItemStack, Integer> products = new HashMap<ItemStack, Integer>();
products.put(primary, 100);
addRecipe(timePerItem, resource, products);
}
public static Recipe findMatchingRecipe(ItemStack item) {
for (Recipe recipe : recipes) {
if (recipe.matches(item)) {
return recipe;
}
}
return null;
}
@Override
public Map<Object[], Object[]> getRecipes() {
HashMap<Object[], Object[]> recipeList = new HashMap<Object[], Object[]>();
for (Recipe recipe : recipes) {
Set<ItemStack> productsKeys = recipe.products.keySet();
recipeList.put(new Object[]{recipe.resource}, productsKeys.toArray(new ItemStack[productsKeys.size()]));
}
return recipeList;
}
}
/* MEMBER */
public MachineCentrifuge.Recipe currentRecipe;
public ICentrifugeRecipe currentRecipe;
private final Stack<ItemStack> pendingProducts = new Stack<ItemStack>();
private int productionTime;
@@ -237,13 +157,8 @@ public class MachineCentrifuge extends TilePowered implements ISidedInventory {
}
// We are done, add products to queue
for (Map.Entry<ItemStack, Integer> entry : currentRecipe.products.entrySet()) {
if (entry.getValue() >= 100) {
pendingProducts.push(entry.getKey().copy());
} else if (worldObj.rand.nextInt(100) < entry.getValue()) {
pendingProducts.push(entry.getKey().copy());
}
}
Collection<ItemStack> products = currentRecipe.getProducts(worldObj.rand);
pendingProducts.addAll(products);
getInternalInventory().decrStackSize(SLOT_RESOURCE, 1);
checkRecipe();
@@ -254,7 +169,7 @@ public class MachineCentrifuge extends TilePowered implements ISidedInventory {
}
public void checkRecipe() {
Recipe machingRecipe = RecipeManager.findMatchingRecipe(getInternalInventory().getStackInSlot(SLOT_RESOURCE));
ICentrifugeRecipe machingRecipe = RecipeManager.findMatchingRecipe(getInternalInventory().getStackInSlot(SLOT_RESOURCE));
if (currentRecipe != machingRecipe) {
currentRecipe = machingRecipe;
@@ -271,8 +186,8 @@ public class MachineCentrifuge extends TilePowered implements ISidedInventory {
return;
}
productionTime = currentRecipe.timePerItem;
timePerItem = currentRecipe.timePerItem;
productionTime = currentRecipe.getProcessingTime();
timePerItem = currentRecipe.getProcessingTime();
}
private boolean tryAddPending() {
@@ -362,4 +277,135 @@ public class MachineCentrifuge extends TilePowered implements ISidedInventory {
return GuiUtil.isIndexInRange(slotIndex, SLOT_PRODUCT_1, SLOT_PRODUCT_COUNT);
}
}
/* RECIPE MANAGEMENT */
public static class CentrifugeRecipe implements ICentrifugeRecipe {
private final int processingTime;
private final ItemStack input;
private final Map<ItemStack, Float> outputs;
public CentrifugeRecipe(int processingTime, ItemStack input, Map<ItemStack, Float> outputs) {
this.processingTime = processingTime;
this.input = input;
this.outputs = outputs;
for (ItemStack item : outputs.keySet()) {
if (item == null) {
throw new IllegalArgumentException("Tried to register a null product of " + input);
}
}
}
@Override
public ItemStack getInput() {
return input;
}
@Override
public int getProcessingTime() {
return processingTime;
}
@Override
public Collection<ItemStack> getProducts(Random random) {
List<ItemStack> products = new ArrayList<ItemStack>();
for (Map.Entry<ItemStack, Float> entry : this.outputs.entrySet()) {
float probability = entry.getValue();
if (probability >= 1.0) {
products.add(entry.getKey().copy());
} else if (random.nextFloat() < probability) {
products.add(entry.getKey().copy());
}
}
return products;
}
@Override
public Map<ItemStack, Float> getAllProducts() {
return ImmutableMap.copyOf(outputs);
}
}
public static class RecipeManager implements ICentrifugeManager {
public static final List<ICentrifugeRecipe> recipes = new ArrayList<ICentrifugeRecipe>();
@Override
public void addRecipe(ICentrifugeRecipe recipe) {
recipes.add(recipe);
}
@Override
public void addRecipe(int timePerItem, ItemStack resource, Map<ItemStack, Float> products) {
ICentrifugeRecipe recipe = new CentrifugeRecipe(timePerItem, resource, products);
addRecipe(recipe);
}
@Override
public void addRecipe(int timePerItem, ItemStack resource, HashMap<ItemStack, Integer> products) {
Map<ItemStack, Float> floatProducts = new HashMap<ItemStack, Float>();
for (Map.Entry<ItemStack, Integer> entry : products.entrySet()) {
floatProducts.put(entry.getKey(), entry.getValue() / 100.0f);
}
addRecipe(timePerItem, resource, floatProducts);
}
@Override
public void addRecipe(int timePerItem, ItemStack resource, ItemStack[] produce, int[] chances) {
Map<ItemStack, Float> products = new HashMap<ItemStack, Float>();
int i = 0;
for (ItemStack prod : produce) {
products.put(prod, chances[i] / 100f);
i++;
}
addRecipe(timePerItem, resource, products);
}
@Override
public void addRecipe(int timePerItem, ItemStack resource, ItemStack primary, ItemStack secondary, int chance) {
Map<ItemStack, Float> products = new HashMap<ItemStack, Float>();
products.put(primary, 1.0f);
if (secondary != null) {
products.put(secondary, chance / 100f);
}
addRecipe(timePerItem, resource, products);
}
@Override
public void addRecipe(int timePerItem, ItemStack resource, ItemStack primary) {
Map<ItemStack, Float> products = new HashMap<ItemStack, Float>();
products.put(primary, 1.0f);
addRecipe(timePerItem, resource, products);
}
public static ICentrifugeRecipe findMatchingRecipe(ItemStack item) {
for (ICentrifugeRecipe recipe : recipes) {
ItemStack recipeInput = recipe.getInput();
if (StackUtils.isCraftingEquivalent(recipeInput, item)) {
return recipe;
}
}
return null;
}
@Override
public Map<Object[], Object[]> getRecipes() {
HashMap<Object[], Object[]> recipeList = new HashMap<Object[], Object[]>();
for (ICentrifugeRecipe recipe : recipes) {
Set<ItemStack> productsKeys = recipe.getAllProducts().keySet();
recipeList.put(new Object[]{recipe.getInput()}, productsKeys.toArray(new ItemStack[productsKeys.size()]));
}
return recipeList;
}
}
}
@@ -11,10 +11,12 @@
package forestry.factory.recipes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import net.minecraft.item.ItemStack;
import forestry.api.recipes.ICentrifugeRecipe;
import forestry.core.config.Defaults;
import forestry.core.config.ForestryBlock;
import forestry.factory.gadgets.MachineCentrifuge;
@@ -51,17 +53,17 @@ public class CraftGuideCentrifuge implements RecipeProvider {
ItemStack machine = ForestryBlock.factoryTESR.getItemStack(1, Defaults.DEFINITION_CENTRIFUGE_META);
RecipeTemplate template = generator.createRecipeTemplate(slots, machine);
for (MachineCentrifuge.Recipe recipe : MachineCentrifuge.RecipeManager.recipes) {
for (ICentrifugeRecipe recipe : MachineCentrifuge.RecipeManager.recipes) {
Object[] array = new Object[11];
ArrayList<Entry<ItemStack, Integer>> entries = new ArrayList<Entry<ItemStack, Integer>>(recipe.products.entrySet());
List<Entry<ItemStack, Float>> entries = new ArrayList<Entry<ItemStack, Float>>(recipe.getAllProducts().entrySet());
for (int i = 0; i < Math.min(entries.size(), 9); i++) {
Entry<ItemStack, Integer> entry = entries.get(i);
Entry<ItemStack, Float> entry = entries.get(i);
array[i] = entry.getValue() > 0 ? new Object[]{entry.getKey(), entry.getValue()} : null;
}
array[9] = recipe.resource;
array[9] = recipe.getInput();
array[10] = machine;
generator.addRecipe(template, array);
@@ -10,6 +10,8 @@
******************************************************************************/
package forestry.plugins;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@@ -628,50 +630,97 @@ public class PluginApiculture extends ForestryPlugin {
// / CENTRIFUGE
// Honey combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 0), ForestryItem.beeswax.getItemStack(), ForestryItem.honeyDrop.getItemStack(), 90);
// Cocoa combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 1), ForestryItem.beeswax.getItemStack(), new ItemStack(Items.dye, 1, 3), 50);
// Simmering combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 2), ForestryItem.refractoryWax.getItemStack(), ForestryItem.phosphor.getItemStack(2), 70);
// Stringy combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 3), ForestryItem.propolis.getItemStack(), ForestryItem.honeyDrop.getItemStack(), 40);
// Dripping combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 5), ForestryItem.honeydew.getItemStack(), ForestryItem.honeyDrop.getItemStack(), 40);
// Frozen combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 4), new ItemStack[]{ForestryItem.beeswax.getItemStack(),
ForestryItem.honeyDrop.getItemStack(), new ItemStack(Items.snowball), ForestryItem.pollenCluster.getItemStack(1, 1)}, new int[]{80, 70, 40, 20});
// Silky combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 6), ForestryItem.honeyDrop.getItemStack(), ForestryItem.propolis.getItemStack(1, 3), 80);
// Parched combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 7), ForestryItem.beeswax.getItemStack(), ForestryItem.honeyDrop.getItemStack(), 90);
// Mysterious combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 8), new ItemStack[]{ForestryItem.honeyDrop.getItemStack(),
ForestryItem.propolis.getItemStack(1, 2)}, new int[]{40, 100});
// Irradiated combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 9), new ItemStack[]{}, new int[]{});
// Powdery combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 10), new ItemStack[]{ForestryItem.honeyDrop.getItemStack(),
ForestryItem.beeswax.getItemStack(), new ItemStack(Items.gunpowder)}, new int[]{20, 20, 90});
// Reddened Combs
RecipeManagers.centrifugeManager.addRecipe(80, ForestryItem.beeComb.getItemStack(1, 11),
new ItemStack[]{ForestryItem.honeyDrop.getItemStack(2, 1)}, new int[]{100});
// Darkened Combs
RecipeManagers.centrifugeManager.addRecipe(80, ForestryItem.beeComb.getItemStack(1, 12),
new ItemStack[]{ForestryItem.honeyDrop.getItemStack(1, 1)}, new int[]{100});
// Omega Combs
RecipeManagers.centrifugeManager.addRecipe(400, ForestryItem.beeComb.getItemStack(1, 13), new ItemStack[]{ForestryItem.honeyDrop.getItemStack(1, 2)}, new int[]{100});
// Wheaten Combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 14), new ItemStack[]{ForestryItem.honeyDrop.getItemStack(),
ForestryItem.beeswax.getItemStack(), new ItemStack(Items.wheat)}, new int[]{20, 20, 80});
// Mossy Combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 15), ForestryItem.beeswax.getItemStack(), ForestryItem.honeyDrop.getItemStack(), 90);
// Mellow Combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 16), new ItemStack[]{ForestryItem.honeydew.getItemStack(),
ForestryItem.beeswax.getItemStack(), new ItemStack(Items.quartz)}, new int[]{60, 20, 30});
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 0), ImmutableMap.of(
ForestryItem.beeswax.getItemStack(), 1.0f,
ForestryItem.honeyDrop.getItemStack(), 0.9f
));
// Silk
RecipeManagers.centrifugeManager.addRecipe(5, ForestryItem.propolis.getItemStack(1, 3), new ItemStack[]{
ForestryItem.craftingMaterial.getItemStack(1, 2), ForestryItem.propolis.getItemStack()}, new int[]{60, 10});
// Cocoa combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 1), ImmutableMap.of(
ForestryItem.beeswax.getItemStack(), 1.0f,
new ItemStack(Items.dye, 1, 3), 0.5f
));
// Simmering combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 2), ImmutableMap.of(
ForestryItem.refractoryWax.getItemStack(), 1.0f,
ForestryItem.phosphor.getItemStack(2), 0.7f
));
// Stringy combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 3), ImmutableMap.of(
ForestryItem.propolis.getItemStack(), 1.0f,
ForestryItem.honeyDrop.getItemStack(), 0.4f
));
// Dripping combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 5), ImmutableMap.of(
ForestryItem.honeydew.getItemStack(), 1.0f,
ForestryItem.honeyDrop.getItemStack(), 0.4f
));
// Frozen combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 4), ImmutableMap.of(
ForestryItem.beeswax.getItemStack(), 0.8f,
ForestryItem.honeyDrop.getItemStack(), 0.7f,
new ItemStack(Items.snowball), 0.4f,
ForestryItem.pollenCluster.getItemStack(1, 1), 0.2f
));
// Silky combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 6), ImmutableMap.of(
ForestryItem.honeyDrop.getItemStack(), 1.0f,
ForestryItem.propolis.getItemStack(1, 3), 0.8f
));
// Parched combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 7), ImmutableMap.of(
ForestryItem.beeswax.getItemStack(), 1.0f,
ForestryItem.honeyDrop.getItemStack(), 0.9f
));
// Mysterious combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 8), ImmutableMap.of(
ForestryItem.propolis.getItemStack(1, 2), 1.0f,
ForestryItem.honeyDrop.getItemStack(), 0.4f
));
// Irradiated combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 9), ImmutableMap.<ItemStack, Float>of(
));
// Powdery combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 10), ImmutableMap.of(
ForestryItem.honeyDrop.getItemStack(), 0.2f,
ForestryItem.beeswax.getItemStack(), 0.2f,
new ItemStack(Items.gunpowder), 0.9f
));
// Wheaten Combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 14), ImmutableMap.of(
ForestryItem.honeyDrop.getItemStack(), 0.2f,
ForestryItem.beeswax.getItemStack(), 0.2f,
new ItemStack(Items.wheat), 0.8f
));
// Mossy Combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 15), ImmutableMap.of(
ForestryItem.beeswax.getItemStack(), 1.0f,
ForestryItem.honeyDrop.getItemStack(), 0.9f
));
// Mellow Combs
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.beeComb.getItemStack(1, 16), ImmutableMap.of(
ForestryItem.honeydew.getItemStack(), 0.6f,
ForestryItem.beeswax.getItemStack(), 0.2f,
new ItemStack(Items.quartz), 0.3f
));
// Silky Propolis
RecipeManagers.centrifugeManager.addRecipe(5, ForestryItem.propolis.getItemStack(1, 3), ImmutableMap.of(
ForestryItem.craftingMaterial.getItemStack(1, 2), 0.6f,
ForestryItem.propolis.getItemStack(), 0.1f
));
// / FERMENTER
RecipeManagers.fermenterManager.addRecipe(ForestryItem.honeydew.getItemStack(), 500, 1.0f, Fluids.SHORT_MEAD.getFluid(1), Fluids.HONEY.getFluid(1));
@@ -10,6 +10,8 @@
******************************************************************************/
package forestry.plugins;
import com.google.common.collect.ImmutableMap;
import java.util.EnumSet;
import net.minecraft.init.Blocks;
@@ -278,7 +280,7 @@ public class PluginIC2 extends ForestryPlugin {
}
if (resin != null) {
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.propolis.getItemStack(), resin);
RecipeManagers.centrifugeManager.addRecipe(20, ForestryItem.propolis.getItemStack(), ImmutableMap.of(resin, 1.0f));
} else {
Proxies.log.fine("Missing IC2 resin, skipping centrifuge recipe for propolis to resin.");
}