From 209c18da215c52214278dd4fc27aace29063bc97 Mon Sep 17 00:00:00 2001 From: Pahimar Date: Tue, 17 May 2016 22:09:41 -0400 Subject: [PATCH] Getting close to the new EnergyValueRegistry implementation --- .../com/pahimar/ee3/EquivalentExchange3.java | 1 - .../DynamicEnergyValueInitThread.java | 7 +- .../ee3/exchange/NewEnergyValueRegistry.java | 65 +++++++------------ .../pahimar/ee3/reference/Comparators.java | 9 +-- 4 files changed, 29 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/pahimar/ee3/EquivalentExchange3.java b/src/main/java/com/pahimar/ee3/EquivalentExchange3.java index 5713a625..bd2acaed 100644 --- a/src/main/java/com/pahimar/ee3/EquivalentExchange3.java +++ b/src/main/java/com/pahimar/ee3/EquivalentExchange3.java @@ -118,7 +118,6 @@ public class EquivalentExchange3 { CachedOreDictionary.getInstance(); Abilities.initNotLearnables(); - NewEnergyValueRegistry.INSTANCE.compute(); } @EventHandler diff --git a/src/main/java/com/pahimar/ee3/exchange/DynamicEnergyValueInitThread.java b/src/main/java/com/pahimar/ee3/exchange/DynamicEnergyValueInitThread.java index 2fe9e837..82bf8bae 100644 --- a/src/main/java/com/pahimar/ee3/exchange/DynamicEnergyValueInitThread.java +++ b/src/main/java/com/pahimar/ee3/exchange/DynamicEnergyValueInitThread.java @@ -20,8 +20,9 @@ public class DynamicEnergyValueInitThread implements Runnable RecipeRegistry.getInstance().registerVanillaRecipes(); AludelRecipeManager.registerRecipes(); - long startTime = System.currentTimeMillis(); - EnergyValueRegistry.getInstance().init(); - LogHelper.info(EnergyValueRegistry.ENERGY_VALUE_MARKER, "DynamicEMC system initialized after {} ms", System.currentTimeMillis() - startTime); + long startTime = System.nanoTime(); +// EnergyValueRegistry.getInstance().init(); + NewEnergyValueRegistry.INSTANCE.compute(); + LogHelper.info(EnergyValueRegistry.ENERGY_VALUE_MARKER, "DynamicEMC system initialized after {} ms", System.nanoTime() - startTime); } } diff --git a/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java b/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java index 5ac462bf..4032053a 100644 --- a/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java +++ b/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java @@ -538,14 +538,13 @@ public class NewEnergyValueRegistry { private Map calculateStackValueMap(Map stackValueMap) { - Map computedMap = new TreeMap<>(stackValueMap); - - Map tempComputedMap = new TreeMap<>(); - int passNumber = 0; - LogHelper.info(ENERGY_VALUE_MARKER, "Beginning energy value calculation"); long startingTime = System.nanoTime(); + Map computedMap = new TreeMap<>(stackValueMap); + Map tempComputedMap = new TreeMap<>(); + int passNumber = 0; + while ((passNumber == 0 || tempComputedMap.size() != computedMap.size()) && passNumber < 16) { long passStartTime = System.nanoTime(); @@ -560,7 +559,24 @@ public class NewEnergyValueRegistry { // We won't attempt to recalculate values that already have a pre-calculation value assignment if (!stackValueMap.containsKey(WrappedStack.wrap(recipeOutput, 1))) { for (List recipeInputs : RecipeRegistry.getInstance().getRecipeMappings().get(recipeOutput)) { - // FIXME PRIORITY NUMBER 1 + + // FIXME PRIORITY NUMBER 1 - getting of values should use the same algo in getEnergyValue + EnergyValue currentOutputValue = tempComputedMap.get(WrappedStack.wrap(recipeOutput, 1)); + EnergyValue computedOutputValue = computeFromInputs(tempComputedMap, recipeOutput, recipeInputs); + + if (computedOutputValue != null && computedOutputValue.compareTo(currentOutputValue) < 0) { + + uncomputedStacks.removeIf(wrappedStack -> uncomputedStacks.contains(WrappedStack.wrap(recipeOutput, 1))); + + if (ConfigurationHandler.Settings.energyValueDebugLoggingEnabled) { + LogHelper.info(ENERGY_VALUE_MARKER, "Pass {}: Calculated value {} for object {}", passNumber, computedOutputValue, recipeOutput); + } + + tempComputedMap.put(WrappedStack.wrap(recipeOutput), computedOutputValue); + } + else if (computedOutputValue != null) { + uncomputedStacks.add(WrappedStack.wrap(recipeOutput, 1)); + } } } } @@ -578,43 +594,6 @@ public class NewEnergyValueRegistry { return computedMap; } - private Map computeValuesFromRecipes(Map stackValueMap) { - - Map tempStackValueMap = new TreeMap<>(); - - /** - * Algorithm - * - * For everything we know how to make in the RecipeRegistry - * Check to see if we have a value for it already - */ - for (WrappedStack recipeOutput : RecipeRegistry.getInstance().getRecipeMappings().keySet()) { - // TODO Review: possible fault in the logic here that is preventing some values from being assigned? - if (!hasEnergyValue(recipeOutput.getWrappedObject()) && !tempStackValueMap.containsKey(recipeOutput)) { - EnergyValue lowestValue = null; - - for (List recipeInputs : RecipeRegistry.getInstance().getRecipeMappings().get(recipeOutput)) { - EnergyValue computedValue = computeFromInputs(stackValueMap, recipeOutput, recipeInputs); - - if (computedValue != null) { - if (computedValue.compareTo(lowestValue) < 0) { - lowestValue = computedValue; - } - } - else { - uncomputedStacks.add(recipeOutput); - } - } - - if ((lowestValue != null) && (lowestValue.getValue() > 0f)) { - tempStackValueMap.put(WrappedStack.wrap(recipeOutput.getWrappedObject()), lowestValue); - } - } - } - - return tempStackValueMap; - } - private void calculateValueStackMap() { SortedMap> tempValueMap = new TreeMap<>(); diff --git a/src/main/java/com/pahimar/ee3/reference/Comparators.java b/src/main/java/com/pahimar/ee3/reference/Comparators.java index 9f9ed43f..dd9bbd8b 100644 --- a/src/main/java/com/pahimar/ee3/reference/Comparators.java +++ b/src/main/java/com/pahimar/ee3/reference/Comparators.java @@ -33,11 +33,8 @@ public class Comparators public int compare(ItemStack itemStack1, ItemStack itemStack2) { if (itemStack1 != null && itemStack2 != null) { - - String name1 = Item.itemRegistry.getNameForObject(itemStack1.getItem()); - String name2 = Item.itemRegistry.getNameForObject(itemStack2.getItem()); - - if (name1.equalsIgnoreCase(name2)) { + // Sort on id + if (Item.getIdFromItem(itemStack1.getItem()) - Item.getIdFromItem(itemStack2.getItem()) == 0) { // Sort on item if (itemStack1.getItem() == itemStack2.getItem()) { // Then sort on meta @@ -71,7 +68,7 @@ public class Comparators } } else { - return name1.compareToIgnoreCase(name2); + return Item.getIdFromItem(itemStack1.getItem()) - Item.getIdFromItem(itemStack2.getItem()); } } else if (itemStack1 != null) {