Fix edge weights not getting assigned properly, still some edge exceptions to handle in the graph construction

This commit is contained in:
pahimar 2013-06-01 21:49:21 -04:00
parent 0c35d8cf24
commit d8e80366e1
6 changed files with 87 additions and 105 deletions

View file

@ -9,7 +9,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.oredict.OreDictionary;
import com.pahimar.ee3.item.CustomStackWrapper;
import com.pahimar.ee3.item.CustomWrappedStack;
import com.pahimar.ee3.item.ModItems;
import com.pahimar.ee3.lib.Colours;
import com.pahimar.ee3.lib.Strings;
@ -91,10 +91,9 @@ public class ItemUtil {
return false;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static ArrayList<CustomStackWrapper> collateStacks(List<CustomStackWrapper> unCollatedStacks) {
public static ArrayList<CustomWrappedStack> collateStacks(List<CustomWrappedStack> unCollatedStacks) {
ArrayList collatedStacks = new ArrayList();
ArrayList<CustomWrappedStack> collatedStacks = new ArrayList<CustomWrappedStack>();
for (int i = 0; i < unCollatedStacks.size(); i++) {
@ -105,21 +104,15 @@ public class ItemUtil {
boolean found = false;
for (int j = 0; j < collatedStacks.size(); j++) {
if ((unCollatedStacks.get(i) instanceof ItemStack) && (collatedStacks.get(j) instanceof ItemStack)) {
ItemStack unCollatedStack = (ItemStack) unCollatedStacks.get(i);
ItemStack collatedStack = (ItemStack) collatedStacks.get(j);
if (compare(unCollatedStack, collatedStack)) {
((ItemStack) collatedStacks.get(j)).stackSize += 1;
if (unCollatedStacks.get(i).getItemStack() != null && collatedStacks.get(j).getItemStack() != null) {
if (compare(unCollatedStacks.get(i).getItemStack(), collatedStacks.get(j).getItemStack())) {
collatedStacks.get(j).setStackSize(collatedStacks.get(j).getStackSize() + 1);
found = true;
}
}
else if ((unCollatedStacks.get(i) instanceof OreStack) && (collatedStacks.get(j) instanceof OreStack)) {
OreStack unCollatedStack = (OreStack) unCollatedStacks.get(i);
OreStack collatedStack = (OreStack) collatedStacks.get(j);
if (OreStack.compareStacks(unCollatedStack, collatedStack)) {
((OreStack) collatedStacks.get(j)).stackSize += 1;
else if (unCollatedStacks.get(i).getOreStack() != null && collatedStacks.get(j).getOreStack() != null) {
if (OreStack.compareStacks(unCollatedStacks.get(i).getOreStack(), collatedStacks.get(j).getOreStack())) {
collatedStacks.get(j).setStackSize(collatedStacks.get(j).getStackSize() + 1);
found = true;
}
}

View file

@ -13,7 +13,7 @@ import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
import com.pahimar.ee3.item.CustomStackWrapper;
import com.pahimar.ee3.item.CustomWrappedStack;
import cpw.mods.fml.common.registry.GameRegistry;
@ -37,9 +37,9 @@ public class RecipeHelper {
* Could be an ItemStack or an Arraylist
*/
@SuppressWarnings({ "rawtypes" })
public static ArrayList<CustomStackWrapper> getRecipeInputs(IRecipe recipe) {
public static ArrayList<CustomWrappedStack> getRecipeInputs(IRecipe recipe) {
ArrayList<CustomStackWrapper> recipeInputs = new ArrayList<CustomStackWrapper>();
ArrayList<CustomWrappedStack> recipeInputs = new ArrayList<CustomWrappedStack>();
if (recipe instanceof ShapedRecipes) {
@ -51,7 +51,7 @@ public class RecipeHelper {
ItemStack itemStack = shapedRecipe.recipeItems[i];
itemStack.stackSize = 1;
recipeInputs.add(new CustomStackWrapper(itemStack));
recipeInputs.add(new CustomWrappedStack(itemStack));
}
}
}
@ -65,7 +65,7 @@ public class RecipeHelper {
ItemStack itemStack = (ItemStack) object;
itemStack.stackSize = 1;
recipeInputs.add(new CustomStackWrapper(itemStack));
recipeInputs.add(new CustomWrappedStack(itemStack));
}
}
}
@ -85,7 +85,7 @@ public class RecipeHelper {
OreStack oreStack = new OreStack((ItemStack) shapedOreRecipeList.get(0));
oreStack.stackSize = 1;
recipeInputs.add(new CustomStackWrapper(oreStack));
recipeInputs.add(new CustomWrappedStack(oreStack));
}
}
/*
@ -96,7 +96,7 @@ public class RecipeHelper {
ItemStack itemStack = (ItemStack) shapedOreRecipe.getInput()[i];
itemStack.stackSize = 1;
recipeInputs.add(new CustomStackWrapper(itemStack));
recipeInputs.add(new CustomWrappedStack(itemStack));
}
}
}
@ -113,14 +113,14 @@ public class RecipeHelper {
OreStack oreStack = new OreStack((ItemStack) shapelessOreRecipeList.get(0));
oreStack.stackSize = 1;
recipeInputs.add(new CustomStackWrapper(oreStack));
recipeInputs.add(new CustomWrappedStack(oreStack));
}
}
else if (object instanceof ItemStack) {
ItemStack itemStack = (ItemStack) object;
itemStack.stackSize = 1;
recipeInputs.add(new CustomStackWrapper(itemStack));
recipeInputs.add(new CustomWrappedStack(itemStack));
}
}
}
@ -128,13 +128,12 @@ public class RecipeHelper {
return recipeInputs;
}
@SuppressWarnings("unchecked")
public static ArrayList<CustomStackWrapper> getCollatedRecipeInputs(IRecipe recipe) {
public static ArrayList<CustomWrappedStack> getCollatedRecipeInputs(IRecipe recipe) {
return ItemUtil.collateStacks(getRecipeInputs(recipe));
}
public static ArrayList<IRecipe> getReverseRecipes(CustomStackWrapper customStackWrapper) {
public static ArrayList<IRecipe> getReverseRecipes(CustomWrappedStack customStackWrapper) {
return getReverseRecipes(customStackWrapper.getItemStack());
}

View file

@ -18,19 +18,19 @@ import com.pahimar.ee3.core.util.OreStack;
import com.pahimar.ee3.core.util.RecipeHelper;
import com.pahimar.ee3.emc.graph.WeightedDirectedGraph;
import com.pahimar.ee3.emc.graph.WeightedEdge;
import com.pahimar.ee3.item.CustomStackWrapper;
import com.pahimar.ee3.item.CustomWrappedStack;
public class DynEMC {
private static DynEMC dynEMC = null;
private ArrayList<CustomStackWrapper> discoveredItems;
private WeightedDirectedGraph<CustomStackWrapper> graph;
private ArrayList<CustomWrappedStack> discoveredItems;
private WeightedDirectedGraph<CustomWrappedStack> graph;
private DynEMC() {
discoveredItems = new ArrayList<CustomStackWrapper>();
graph = new WeightedDirectedGraph<CustomStackWrapper>();
discoveredItems = new ArrayList<CustomWrappedStack>();
graph = new WeightedDirectedGraph<CustomWrappedStack>();
init();
}
@ -44,7 +44,7 @@ public class DynEMC {
return dynEMC;
}
public List<CustomStackWrapper> getDiscoveredItems() {
public List<CustomWrappedStack> getDiscoveredItems() {
return discoveredItems;
}
@ -62,7 +62,7 @@ public class DynEMC {
*/
for (String oreName : OreDictionary.getOreNames()) {
CustomStackWrapper customWrappedStack = new CustomStackWrapper(new OreStack(oreName));
CustomWrappedStack customWrappedStack = new CustomWrappedStack(new OreStack(oreName));
if (!discoveredItems.contains(customWrappedStack)) {
discoveredItems.add(customWrappedStack);
@ -75,17 +75,17 @@ public class DynEMC {
if (craftingResult != null) {
if (craftingResult.getItemDamage() == OreDictionary.WILDCARD_VALUE) {
CustomStackWrapper wrappedCraftingResult = new CustomStackWrapper(craftingResult);
CustomWrappedStack wrappedCraftingResult = new CustomWrappedStack(craftingResult);
if (!discoveredItems.contains(wrappedCraftingResult)) {
discoveredItems.add(wrappedCraftingResult);
}
}
for (CustomStackWrapper wrappedRecipeInput : RecipeHelper.getCollatedRecipeInputs((IRecipe) recipe)) {
for (CustomWrappedStack wrappedRecipeInput : RecipeHelper.getCollatedRecipeInputs((IRecipe) recipe)) {
if ((wrappedRecipeInput.getItemStack() != null) && (wrappedRecipeInput.getItemStack().getItemDamage() == OreDictionary.WILDCARD_VALUE)) {
wrappedRecipeInput.setWrappedStackSize(1);
wrappedRecipeInput.setStackSize(1);
if (!discoveredItems.contains(wrappedRecipeInput)) {
discoveredItems.add(wrappedRecipeInput);
}
@ -109,7 +109,7 @@ public class DynEMC {
for (ItemStack itemStack : subItems) {
if (itemStack != null) {
CustomStackWrapper customStackWrapper = new CustomStackWrapper(itemStack);
CustomWrappedStack customStackWrapper = new CustomWrappedStack(itemStack);
if (!discoveredItems.contains(customStackWrapper)) {
discoveredItems.add(customStackWrapper);
@ -121,7 +121,7 @@ public class DynEMC {
ItemStack itemStack = new ItemStack(Item.itemsList[i]);
CustomStackWrapper customStackWrapper = new CustomStackWrapper(itemStack);
CustomWrappedStack customStackWrapper = new CustomWrappedStack(itemStack);
if (!discoveredItems.contains(customStackWrapper)) {
discoveredItems.add(customStackWrapper);
}
@ -133,42 +133,41 @@ public class DynEMC {
* Now that we have discovered as many items as possible, trim out the
* items that are black listed
*/
for (CustomStackWrapper customStackWrapper : EmcBlackList.getInstance().getBlackListStacks()) {
for (CustomWrappedStack customStackWrapper : EmcBlackList.getInstance().getBlackListStacks()) {
while (discoveredItems.contains(customStackWrapper)) {
discoveredItems.remove(customStackWrapper);
}
}
for (CustomStackWrapper customWrappedStack : discoveredItems) {
for (CustomWrappedStack customWrappedStack : discoveredItems) {
if (!graph.nodeExists(customWrappedStack)) {
graph.addNode(customWrappedStack);
}
}
for (CustomStackWrapper customWrappedStack : discoveredItems) {
for (CustomWrappedStack customWrappedStack : discoveredItems) {
ArrayList<IRecipe> recipes = RecipeHelper.getReverseRecipes(customWrappedStack);
for (IRecipe recipe : recipes) {
ArrayList<CustomStackWrapper> recipeInputs = RecipeHelper.getCollatedRecipeInputs(recipe);
System.out.println(recipeInputs);
ArrayList<CustomWrappedStack> recipeInputs = RecipeHelper.getCollatedRecipeInputs(recipe);
for (CustomStackWrapper wrappedRecipeInput : recipeInputs) {
for (CustomWrappedStack wrappedRecipeInput : recipeInputs) {
if (wrappedRecipeInput.getItemStack() != null) {
ItemStack itemStack = wrappedRecipeInput.getItemStack();
itemStack.stackSize = wrappedRecipeInput.getStackSize();
if (OreDictionary.getOreID(itemStack) != -1) {
wrappedRecipeInput = new CustomStackWrapper(new OreStack(itemStack));
wrappedRecipeInput = new CustomWrappedStack(new OreStack(itemStack));
}
}
float weight = wrappedRecipeInput.getWrappedStackSize();
wrappedRecipeInput.setWrappedStackSize(1);
float weight = wrappedRecipeInput.getStackSize();
wrappedRecipeInput.setStackSize(1);
try {
graph.addEdge(customWrappedStack, wrappedRecipeInput, weight);
@ -187,9 +186,9 @@ public class DynEMC {
StringBuilder stringBuilder = new StringBuilder();
LogHelper.log(Level.INFO, "***** START NODES *****");
Iterator<CustomStackWrapper> nodeIter = graph.iterator();
Iterator<CustomWrappedStack> nodeIter = graph.iterator();
while (nodeIter.hasNext()) {
CustomStackWrapper node = nodeIter.next();
CustomWrappedStack node = nodeIter.next();
LogHelper.log(Level.INFO, "Node: " + node);
}
LogHelper.log(Level.INFO, "***** END NODES *****");
@ -197,9 +196,9 @@ public class DynEMC {
LogHelper.log(Level.INFO, "***** START EDGES FROM *****");
nodeIter = graph.iterator();
while (nodeIter.hasNext()) {
CustomStackWrapper node = nodeIter.next();
Set<WeightedEdge<CustomStackWrapper>> edgesFrom = graph.edgesFrom(node);
for (WeightedEdge<CustomStackWrapper> edge : edgesFrom) {
CustomWrappedStack node = nodeIter.next();
Set<WeightedEdge<CustomWrappedStack>> edgesFrom = graph.edgesFrom(node);
for (WeightedEdge<CustomWrappedStack> edge : edgesFrom) {
LogHelper.log(Level.INFO, "Crafting Output: " + node);
LogHelper.log(Level.INFO, "Crafting Input: " + edge.getTarget());
LogHelper.log(Level.INFO, "Weight: " + edge.getWeight());

View file

@ -8,13 +8,13 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
import com.pahimar.ee3.item.CustomStackWrapper;
import com.pahimar.ee3.item.CustomWrappedStack;
public class EmcBlackList {
private static EmcBlackList emcBlackList = null;
private ArrayList<CustomStackWrapper> stackBlackList = new ArrayList<CustomStackWrapper>();
private ArrayList<CustomWrappedStack> stackBlackList = new ArrayList<CustomWrappedStack>();
private EmcBlackList() {
@ -31,14 +31,14 @@ public class EmcBlackList {
return emcBlackList;
}
public List<CustomStackWrapper> getBlackListStacks() {
public List<CustomWrappedStack> getBlackListStacks() {
return stackBlackList;
}
public void add(ItemStack itemStack) {
CustomStackWrapper customStackWrapper = new CustomStackWrapper(itemStack);
CustomWrappedStack customStackWrapper = new CustomWrappedStack(itemStack);
if (!stackBlackList.contains(customStackWrapper)) {
stackBlackList.add(customStackWrapper);
@ -72,7 +72,7 @@ public class EmcBlackList {
itemStack.stackSize = 1;
}
return stackBlackList.contains(new CustomStackWrapper(itemStack));
return stackBlackList.contains(new CustomWrappedStack(itemStack));
}
public boolean contains(Item item) {
@ -97,7 +97,7 @@ public class EmcBlackList {
public void remove(ItemStack itemStack) {
CustomStackWrapper customStackWrapper = new CustomStackWrapper(itemStack);
CustomWrappedStack customStackWrapper = new CustomWrappedStack(itemStack);
while (stackBlackList.contains(customStackWrapper)) {
stackBlackList.remove(customStackWrapper);

View file

@ -5,64 +5,64 @@ import java.util.List;
import net.minecraft.item.ItemStack;
import com.pahimar.ee3.item.CustomStackWrapper;
import com.pahimar.ee3.item.CustomWrappedStack;
public class EquivalencyGroup {
private List<CustomStackWrapper> equivalentItems;
private List<CustomWrappedStack> equivalentItems;
public EquivalencyGroup() {
equivalentItems = new ArrayList<CustomStackWrapper>();
equivalentItems = new ArrayList<CustomWrappedStack>();
}
public EquivalencyGroup(List<ItemStack> equivalentItems) {
this.equivalentItems = new ArrayList<CustomStackWrapper>();
this.equivalentItems = new ArrayList<CustomWrappedStack>();
for (ItemStack itemStack : equivalentItems) {
this.equivalentItems.add(new CustomStackWrapper(itemStack));
this.equivalentItems.add(new CustomWrappedStack(itemStack));
}
}
public List<CustomStackWrapper> getMembers() {
public List<CustomWrappedStack> getMembers() {
return equivalentItems;
}
public boolean containsMember(ItemStack itemStack) {
return containsMember(new CustomStackWrapper(itemStack));
return containsMember(new CustomWrappedStack(itemStack));
}
public boolean containsMember(CustomStackWrapper customStackWrapper) {
public boolean containsMember(CustomWrappedStack customStackWrapper) {
return equivalentItems.contains(customStackWrapper);
}
public void addMember(ItemStack itemStack) {
this.addMember(new CustomStackWrapper(itemStack));
this.addMember(new CustomWrappedStack(itemStack));
}
public void addMember(CustomStackWrapper customStackWrapper) {
public void addMember(CustomWrappedStack customStackWrapper) {
if (!containsMember(customStackWrapper)) {
equivalentItems.add(customStackWrapper);
}
}
public void setEquivalentItems(List<CustomStackWrapper> equivalentItems) {
public void setEquivalentItems(List<CustomWrappedStack> equivalentItems) {
this.equivalentItems = equivalentItems;
}
public void removeMember(ItemStack itemStack) {
removeMember(new CustomStackWrapper(itemStack));
removeMember(new CustomWrappedStack(itemStack));
}
public void removeMember(CustomStackWrapper customStackWrapper) {
public void removeMember(CustomWrappedStack customStackWrapper) {
while (containsMember(customStackWrapper)) {
equivalentItems.remove(customStackWrapper);
@ -71,7 +71,7 @@ public class EquivalencyGroup {
public void clearMembers() {
equivalentItems = new ArrayList<CustomStackWrapper>();
equivalentItems = new ArrayList<CustomWrappedStack>();
}
@Override
@ -92,7 +92,7 @@ public class EquivalencyGroup {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Equivalent Group Members: ");
for (CustomStackWrapper customStackWrapper : equivalentItems) {
for (CustomWrappedStack customStackWrapper : equivalentItems) {
stringBuilder.append(String.format("%s ", customStackWrapper));
}
@ -104,7 +104,7 @@ public class EquivalencyGroup {
int hashCode = 1;
for (CustomStackWrapper customStackWrapper : equivalentItems) {
for (CustomWrappedStack customStackWrapper : equivalentItems) {
hashCode = 37 * hashCode + customStackWrapper.hashCode();
}

View file

@ -6,51 +6,42 @@ import net.minecraftforge.oredict.OreDictionary;
import com.pahimar.ee3.core.util.ItemUtil;
import com.pahimar.ee3.core.util.OreStack;
public class CustomStackWrapper {
public class CustomWrappedStack {
private int stackSize;
private ItemStack itemStack;
private OreStack oreStack;
public CustomStackWrapper(ItemStack itemStack) {
public CustomWrappedStack(ItemStack itemStack) {
this.itemStack = itemStack;
this.oreStack = null;
stackSize = itemStack.stackSize;
if (this.itemStack != null) {
this.itemStack.stackSize = 1;
}
}
public CustomStackWrapper(OreStack oreStack) {
public CustomWrappedStack(OreStack oreStack) {
this.itemStack = null;
this.oreStack = oreStack;
stackSize = oreStack.stackSize;
if (this.oreStack != null) {
this.oreStack.stackSize = 1;
}
}
public int getWrappedStackSize() {
public int getStackSize() {
if (itemStack != null) {
return itemStack.stackSize;
}
else if (oreStack != null) {
return oreStack.stackSize;
}
return -1;
return stackSize;
}
public void setWrappedStackSize(int stackSize) {
public void setStackSize(int stackSize) {
if (itemStack != null) {
itemStack.stackSize = stackSize;
}
else if (oreStack != null) {
oreStack.stackSize = stackSize;
}
this.stackSize = stackSize;
}
public ItemStack getItemStack() {
@ -66,19 +57,19 @@ public class CustomStackWrapper {
@Override
public boolean equals(Object object) {
if (!(object instanceof CustomStackWrapper)) {
if (!(object instanceof CustomWrappedStack)) {
return false;
}
CustomStackWrapper customWrappedStack = (CustomStackWrapper) object;
CustomWrappedStack customWrappedStack = (CustomWrappedStack) object;
if (itemStack != null) {
if (customWrappedStack.itemStack != null) {
return ItemUtil.compare(this.itemStack, customWrappedStack.itemStack);
return (ItemUtil.compare(this.itemStack, customWrappedStack.itemStack) && stackSize == customWrappedStack.itemStack.stackSize);
}
else if (customWrappedStack.oreStack != null) {
for (ItemStack oreDictItemStack : OreDictionary.getOres(customWrappedStack.oreStack.oreName)) {
if (ItemUtil.compare(itemStack, oreDictItemStack)) {
if (ItemUtil.compare(itemStack, oreDictItemStack) && stackSize == oreDictItemStack.stackSize) {
return true;
}
}
@ -87,13 +78,13 @@ public class CustomStackWrapper {
else if (oreStack != null) {
if (customWrappedStack.itemStack != null) {
for (ItemStack oreDictItemStack : OreDictionary.getOres(oreStack.oreName)) {
if (ItemUtil.compare(customWrappedStack.itemStack, oreDictItemStack)) {
if (ItemUtil.compare(customWrappedStack.itemStack, oreDictItemStack) && stackSize == oreDictItemStack.stackSize) {
return true;
}
}
}
else if (customWrappedStack.oreStack != null) {
return oreStack.equals(customWrappedStack.oreStack);
return (oreStack.equals(customWrappedStack.oreStack) && stackSize == oreStack.stackSize);
}
}
@ -117,6 +108,8 @@ public class CustomStackWrapper {
public int hashCode() {
int hashCode = 1;
hashCode = 37 * hashCode + stackSize;
if (itemStack != null) {
hashCode = 37 * hashCode + itemStack.itemID;
@ -127,12 +120,10 @@ public class CustomStackWrapper {
else {
hashCode = 37 * hashCode + itemStack.getItemDamage();
}
hashCode = 37 * hashCode + itemStack.stackSize;
hashCode = 37 * hashCode + itemStack.getItemName().hashCode();
}
else if (oreStack != null) {
hashCode = 37 * hashCode + oreStack.stackSize;
hashCode = 37 * hashCode + oreStack.oreName.hashCode();
}