equivalent-exchange-3/common/com/pahimar/ee3/emc/DynEMC.java

110 lines
3.8 KiB
Java
Raw Normal View History

2013-09-23 04:03:28 +02:00
package com.pahimar.ee3.emc;
2013-09-28 21:00:28 +02:00
import java.util.ArrayList;
import java.util.Collections;
2013-09-23 04:03:28 +02:00
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.google.common.collect.Multimap;
import com.pahimar.ee3.core.util.LogHelper;
import com.pahimar.ee3.graph.Node;
import com.pahimar.ee3.graph.WeightedDirectedGraph;
2013-09-25 06:30:00 +02:00
import com.pahimar.ee3.graph.WeightedDirectedEdge;
2013-09-23 04:03:28 +02:00
import com.pahimar.ee3.item.CustomWrappedStack;
import com.pahimar.ee3.item.crafting.RecipeRegistry;
public class DynEMC {
private static DynEMC dynEMC = null;
private RecipeRegistry recipeRegistry;
public final WeightedDirectedGraph<CustomWrappedStack> graph;
private DynEMC() {
recipeRegistry = RecipeRegistry.getInstance();
graph = new WeightedDirectedGraph<CustomWrappedStack>();
init();
}
public static DynEMC getInstance() {
if (dynEMC == null) {
dynEMC = new DynEMC();
}
return dynEMC;
}
private void init() {
populateGraph();
}
private void populateGraph() {
for (CustomWrappedStack discoveredStack : recipeRegistry.getDiscoveredStacks()) {
graph.addNode(discoveredStack);
}
Multimap<CustomWrappedStack, List<CustomWrappedStack>> recipeMappings = recipeRegistry.getRecipeMappings();
Set<CustomWrappedStack> recipeKeySet = recipeMappings.keySet();
Iterator<CustomWrappedStack> recipeKeySetIterator = recipeKeySet.iterator();
while (recipeKeySetIterator.hasNext()) {
2013-09-28 21:00:28 +02:00
CustomWrappedStack recipeOutput = recipeKeySetIterator.next();
2013-09-23 04:03:28 +02:00
for (List<CustomWrappedStack> recipeInputs : recipeMappings.get(recipeOutput)) {
CustomWrappedStack unWrappedRecipeOutput = new CustomWrappedStack(recipeOutput.getWrappedStack());
if (graph.containsNode(unWrappedRecipeOutput)) {
for (CustomWrappedStack recipeInput : recipeInputs) {
// Unwrapped the wrapped stacks so that we actually find them in the graph
CustomWrappedStack unWrappedRecipeInput = new CustomWrappedStack(recipeInput.getWrappedStack());
if (graph.containsNode(unWrappedRecipeInput)) {
if (recipeOutput.getStackSize() != 0) {
2013-09-24 06:26:42 +02:00
graph.addEdge(unWrappedRecipeOutput, unWrappedRecipeInput, (recipeInput.getStackSize() * 1.0f) / recipeOutput.getStackSize());
2013-09-23 04:03:28 +02:00
}
}
}
}
}
}
}
public void printDebugDump() {
2013-09-24 06:26:42 +02:00
LogHelper.debug("Total node count: " + graph.getAllNodes().size());
LogHelper.debug("Leaf node count: " + graph.getLeafNodes().size());
LogHelper.debug("Orphan node count: " + graph.getOrphanNodes().size());
LogHelper.debug("Compound node count: " + graph.getCompoundNodes().size());
LogHelper.debug("'Critical' node count: " + (graph.getLeafNodes().size() - graph.getOrphanNodes().size()));
LogHelper.debug("");
2013-09-28 21:00:28 +02:00
List<Node<CustomWrappedStack>> nodes = new ArrayList<Node<CustomWrappedStack>>();
nodes.addAll(graph.getAllNodes());
Collections.sort(nodes);
for (Node<CustomWrappedStack> node : nodes) {
LogHelper.debug("Node: " + node);
2013-09-28 21:00:28 +02:00
LogHelper.debug("Edges FROM this Node:");
2013-09-25 06:30:00 +02:00
for (WeightedDirectedEdge<CustomWrappedStack> fromEdge : graph.edgesFrom(node)) {
LogHelper.debug(" * " + fromEdge);
}
2013-09-28 21:00:28 +02:00
LogHelper.debug("Edges TO this Node:");
2013-09-25 06:30:00 +02:00
for (WeightedDirectedEdge<CustomWrappedStack> toEdge : graph.edgesTo(node)) {
LogHelper.debug(" * " + toEdge);
2013-09-24 06:26:42 +02:00
}
LogHelper.debug("");
2013-09-24 06:26:42 +02:00
}
2013-09-23 04:03:28 +02:00
}
}