improve handling of by products by excluding them

master
agp8x 2023-04-10 11:41:55 +02:00
parent ee35fcf49b
commit 1e1fcba3cb
4 changed files with 35 additions and 27 deletions

View File

@ -40,7 +40,7 @@ public class Database {
private static Map<Item, Integer> blender() {
Map<Item, Integer> cost = new HashMap<>();
cost.put(null, null); //FIXME values
//cost.put(null, null); //FIXME values
return cost;
}
@ -110,7 +110,7 @@ public class Database {
private static Map<Item, Integer> minerMk3() {
Map<Item, Integer> cost = new HashMap<>();
cost.put(Database.PortableMiner, 2);
cost.put(null, null); //FIXME values
//cost.put(null, null); //FIXME values
return cost;
}
@ -145,7 +145,7 @@ public class Database {
private static Map<Item, Integer> resourceWellExtractor() {
Map<Item, Integer> cost = new HashMap<>();
cost.put(null, null); // FIXME values
//cost.put(null, null); // FIXME values
return cost;
}
@ -731,7 +731,7 @@ public class Database {
// TODO: duration=60/130
polymerResin.addInput(CrudeOil, 6);
polymerResin.addOutput(PolymerResin, 13);
polymerResin.addOutput(HeavyOilResidue, 2);
polymerResin.addOutput(HeavyOilResidue, 2, true);
PolymerResin.add(polymerResin);
}
{
@ -744,7 +744,7 @@ public class Database {
Recipe unpack = new Recipe(2, Buildings.PACKAGER);
recipe.addInput(PackagedLiquidBiofuel, 2);
recipe.addOutput(LiquidBiofuel, 2);
recipe.addOutput(EmptyCanister, 2);
recipe.addOutput(EmptyCanister, 2,true);
LiquidBiofuel.add(unpack);
}
{
@ -825,14 +825,14 @@ public class Database {
recipe.addInput(Bauxite, 12);
recipe.addInput(Water, 18);
recipe.addOutput(Silica, 5);
recipe.addOutput(AluminaSolution, 12);
recipe.addOutput(AluminaSolution, 12,true);
}
{
Recipe recipe = new Recipe(1, Buildings.REFINERY);
recipe.addInput(AluminaSolution, 4);
recipe.addInput(Coal, 2);
recipe.addOutput(AluminumScrap, 6);
recipe.addOutput(Water, 2);
recipe.addOutput(Water, 2,true);
}
{
Recipe recipe = new Recipe(6, Buildings.REFINERY);
@ -853,7 +853,7 @@ public class Database {
recipe.addInput(Concrete, 3);
recipe.addInput(SulfuricAcid, 8);
recipe.addOutput(EncasedUraniumCell, 5);
recipe.addOutput(SulfuricAcid, 2);
recipe.addOutput(SulfuricAcid, 2,true);
}
{
Recipe recipe = new Recipe(120, Buildings.MANUFACTURER);
@ -868,7 +868,7 @@ public class Database {
recipe.addInput(AluminaSolution, 2);
recipe.addInput(AluminumCasing, 1);
recipe.addOutput(Battery, 1);
recipe.addOutput(Water, 1.5);
recipe.addOutput(Water, 1.5,true);
}
{
Recipe recipe = new Recipe(8, Buildings.ASSEMBLER);
@ -920,7 +920,7 @@ public class Database {
Recipe packaged = new Recipe(3, Buildings.PACKAGER);
recipe.addInput(PackagedTurboFuel, 2);
recipe.addOutput(Turbofuel, 2);
recipe.addOutput(EmptyCanister, 2);
recipe.addOutput(EmptyCanister, 2, true);
Turbofuel.add(packaged);
}
{

View File

@ -14,13 +14,14 @@ import java.util.stream.Collectors;
public class Recipe {
private final Map<Item, Double> inputs;
private final Map<Item, Double> outputs;
private final Set<Item> byProducts;
private final int duration;
private final Building building;
private boolean isHandCraftable = true;
private String name;
public Recipe(int duration, Building building) {
this(duration, new HashMap<>(), new HashMap<>(), building);
this(duration, new HashMap<>(), new HashMap<>(), new HashSet<>(), building);
}
public Recipe(int duration, boolean isHandCraftable, Building building) {
@ -33,10 +34,11 @@ public class Recipe {
addInput(item, input);
}
public Recipe(int duration, Map<Item, Double> inputs, Map<Item, Double> outputs, Building building) {
public Recipe(int duration, Map<Item, Double> inputs, Map<Item, Double> outputs, Set<Item> byProducts, Building building) {
this.duration = duration;
this.inputs = inputs;
this.outputs = outputs;
this.byProducts = byProducts;
this.building = building;
}
@ -77,11 +79,22 @@ public class Recipe {
addInput(input, 1);
}
public void addOutput(Item item, int amount, boolean isByProduct) {
addOutput(item, (double) amount, isByProduct);
}
public void addOutput(Item item, int amount) {
this.outputs.put(item, (double) amount);
item.add(this, amount);
}
public void addOutput(Item item, double amount, boolean isByProduct) {
addOutput(item, amount);
if (isByProduct) {
byProducts.add(item);
}
}
public void addOutput(Item item, double amount) {
this.outputs.put(item, amount);
item.add(this, amount);
@ -123,15 +136,8 @@ public class Recipe {
return inputs;
}
public Map<Item, Double> getByProducts(Item reference) {
if (!outputs.containsKey(reference)) {
return null;
}
return outputs.entrySet().stream().filter(itemIntegerEntry -> isByProduct(reference, itemIntegerEntry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
public boolean isByProduct(Item reference, Item test) {
return !reference.equals(test) && outputs.containsKey(reference) && outputs.containsKey(test);
public boolean isByProduct(Item test) {
return byProducts.contains(test);
}
public Graph<Item, DefaultWeightedEdge> buildGraph(Item target) {
@ -201,7 +207,7 @@ public class Recipe {
for (DefaultWeightedEdge edge : buildGraph.outgoingEdgesOf(item)) {
Item product = buildGraph.getEdgeTarget(edge);
Double productWantedPerMinute = map.get(product);
if (item.getRecipe().outputs.containsKey(product)) { // TODO: method isByProduct
if (item.getRecipe().isByProduct(product)) { // TODO: method isByProduct
// product is by-product, no forward dependency
System.out.println("BY-PRODUCT " + item.getName() + " -> " + product.getName() + "... " + queue);
byProducts.add(product);
@ -247,7 +253,8 @@ public class Recipe {
public String getName() {
return name;
}
public String formatName(){
public String formatName() {
if (name == null) {
return formatOutputs();
}

View File

@ -47,8 +47,7 @@ class ItemTest {
@Test
void productionUraniumFuelRod(){
//test(Database.UraniumFuelRod, "uranium_fuel_rod");
fail();
test(Database.UraniumFuelRod, "uranium_fuel_rod");
}
}

View File

@ -27,13 +27,15 @@ public class TestHelper {
}
public static void test(Item item, String name) {
name = "test_" + name;
Map<Item, Double> ref = ValidatedValues.get(item);
SumResult calculations = calculate(item);
assertMap(ref, calculations.getMap());
//plot
name = "test_" + name;
plot2(calculations.getProduction(), name);
javaPlot(name);
list(calculations, name);
// assert
assertMap(ref, calculations.getMap());
}
private static SumResult calculate(Item item) {