mirror of
https://github.com/Anuken/Mindustry.git
synced 2026-01-27 15:02:03 -08:00
The on-going consumer disaster
This commit is contained in:
parent
8b916d03af
commit
19828410a9
68 changed files with 371 additions and 451 deletions
|
|
@ -8,52 +8,47 @@ import mindustry.game.*;
|
|||
import mindustry.type.*;
|
||||
import mindustry.world.*;
|
||||
import mindustry.world.blocks.power.*;
|
||||
import mindustry.world.blocks.power.ItemLiquidGenerator.*;
|
||||
import mindustry.world.blocks.power.ConsumeGenerator.*;
|
||||
import mindustry.world.consumers.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
|
||||
import static org.junit.jupiter.api.DynamicTest.*;
|
||||
|
||||
/**
|
||||
* This class tests generators which can process items, liquids or both.
|
||||
* All tests are run with a fixed delta of 0.5 so delta considerations can be tested as well.
|
||||
* Additionally, each PowerGraph::update() call will have its own thread frame, i.e. the method will never be called twice within the same frame.
|
||||
* Both of these constraints are handled by FakeThreadHandler within PowerTestFixture.
|
||||
* Any expected power amount (produced, consumed, buffered) should be affected by FakeThreadHandler.fakeDelta but status should not!
|
||||
* Any expected power amount (produced, consumed, buffered) should be affected by fakeDelta but status should not!
|
||||
*/
|
||||
public class ItemLiquidGeneratorTests extends PowerTestFixture{
|
||||
private ItemLiquidGenerator generator;
|
||||
public class ConsumeGeneratorTests extends PowerTestFixture{
|
||||
private ConsumeGenerator generator;
|
||||
private Tile tile;
|
||||
private ItemLiquidGeneratorBuild entity;
|
||||
private ConsumeGeneratorBuild build;
|
||||
private final float fakeItemDuration = 60f; //ticks
|
||||
private final float maximumLiquidUsage = 0.5f;
|
||||
|
||||
public void createGenerator(InputType inputType){
|
||||
Vars.state = new GameState();
|
||||
Vars.state.rules = new Rules();
|
||||
generator = new ItemLiquidGenerator(inputType != InputType.liquids, inputType != InputType.items, "fakegen" + System.nanoTime()){
|
||||
{
|
||||
powerProduction = 0.1f;
|
||||
itemDuration = fakeItemDuration;
|
||||
maxLiquidGenerate = maximumLiquidUsage;
|
||||
buildType = ItemLiquidGeneratorBuild::new;
|
||||
generator = new ConsumeGenerator("fakegen" + System.nanoTime()){{
|
||||
powerProduction = 0.1f;
|
||||
itemDuration = fakeItemDuration;
|
||||
buildType = ConsumeGeneratorBuild::new;
|
||||
|
||||
if(inputType != InputType.liquids){
|
||||
consume(new ConsumeItemFlammable());
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getItemEfficiency(Item item){
|
||||
return item.flammability;
|
||||
if(inputType != InputType.items){
|
||||
consume(new ConsumeLiquidFlammable(maximumLiquidUsage));
|
||||
}
|
||||
}};
|
||||
|
||||
@Override
|
||||
public float getLiquidEfficiency(Liquid liquid){
|
||||
return liquid.flammability;
|
||||
}
|
||||
};
|
||||
|
||||
generator.init();
|
||||
tile = createFakeTile(0, 0, generator);
|
||||
entity = (ItemLiquidGeneratorBuild)tile.build;
|
||||
build = (ConsumeGeneratorBuild)tile.build;
|
||||
}
|
||||
|
||||
/** Tests the consumption and efficiency when being supplied with liquids. */
|
||||
|
|
@ -86,16 +81,16 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{
|
|||
final float expectedRemainingLiquidAmount = Math.max(0.0f, availableLiquidAmount - expectedConsumptionPerTick * Time.delta);
|
||||
|
||||
createGenerator(inputType);
|
||||
assertTrue(entity.acceptLiquid(null, liquid), inputType + " | " + parameterDescription + ": Liquids which will be declined by the generator don't need to be tested - The code won't be called for those cases.");
|
||||
assertTrue(build.acceptLiquid(null, liquid), inputType + " | " + parameterDescription + ": Liquids which will be declined by the generator don't need to be tested - The code won't be called for those cases.");
|
||||
|
||||
entity.liquids.add(liquid, availableLiquidAmount);
|
||||
entity.updateConsumption();
|
||||
build.liquids.add(liquid, availableLiquidAmount);
|
||||
build.updateConsumption();
|
||||
|
||||
// Perform an update on the generator once - This should use up any resource up to the maximum liquid usage
|
||||
entity.updateTile();
|
||||
build.updateTile();
|
||||
|
||||
assertEquals(expectedRemainingLiquidAmount, entity.liquids.get(liquid), inputType + " | " + parameterDescription + ": Remaining liquid amount mismatch.");
|
||||
assertEquals(expectedEfficiency, entity.productionEfficiency, inputType + " | " + parameterDescription + ": Efficiency mismatch.");
|
||||
assertEquals(expectedRemainingLiquidAmount, build.liquids.get(liquid), inputType + " | " + parameterDescription + ": Remaining liquid amount mismatch.");
|
||||
assertEquals(expectedEfficiency, build.productionEfficiency, inputType + " | " + parameterDescription + ": Efficiency mismatch.");
|
||||
}
|
||||
|
||||
/** Tests the consumption and efficiency when being supplied with items. */
|
||||
|
|
@ -114,7 +109,6 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{
|
|||
tests.add(dynamicTest("02", () -> simulateItemConsumption(inputType, Items.coal, 1, "Sufficient coal provided")));
|
||||
tests.add(dynamicTest("03", () -> simulateItemConsumption(inputType, Items.coal, 10, "Excess coal provided")));
|
||||
tests.add(dynamicTest("04", () -> simulateItemConsumption(inputType, Items.blastCompound, 1, "Blast compound provided")));
|
||||
//dynamicTest("03", () -> simulateItemConsumption(inputType, Items.plastanium, 1, "Plastanium provided")), // Not accepted by generator due to low flammability
|
||||
tests.add(dynamicTest("05", () -> simulateItemConsumption(inputType, Items.sporePod, 1, "Biomatter provided")));
|
||||
tests.add(dynamicTest("06", () -> simulateItemConsumption(inputType, Items.pyratite, 1, "Pyratite provided")));
|
||||
}
|
||||
|
|
@ -124,27 +118,22 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{
|
|||
}
|
||||
|
||||
void simulateItemConsumption(InputType inputType, Item item, int amount, String parameterDescription){
|
||||
final float expectedEfficiency = amount > 0 ? item.flammability : 0f;
|
||||
final float expectedRemainingItemAmount = Math.max(0, amount - 1);
|
||||
float expectedEfficiency = amount > 0 ? item.flammability : 0f;
|
||||
int expectedRemainingItemAmount = Math.max(0, amount - 1);
|
||||
|
||||
createGenerator(inputType);
|
||||
assertTrue(entity.acceptItem(null, item), inputType + " | " + parameterDescription + ": Items which will be declined by the generator don't need to be tested - The code won't be called for those cases.");
|
||||
assertTrue(build.acceptItem(null, item), inputType + " | " + parameterDescription + ": Items which will be declined by the generator don't need to be tested - The code won't be called for those cases.");
|
||||
|
||||
if(amount > 0){
|
||||
entity.items.add(item, amount);
|
||||
build.items.add(item, amount);
|
||||
}
|
||||
entity.updateConsumption();
|
||||
|
||||
// Perform an update on the generator once - This should use up one or zero items - dependent on if the item is accepted and available or not.
|
||||
try{
|
||||
entity.updateTile();
|
||||
build.update();
|
||||
|
||||
assertEquals(expectedRemainingItemAmount, build.items.get(item), inputType + " | " + parameterDescription + ": Remaining item amount mismatch.");
|
||||
assertEquals(expectedEfficiency, build.productionEfficiency, inputType + " | " + parameterDescription + ": Efficiency mismatch.");
|
||||
|
||||
assertEquals(expectedRemainingItemAmount, entity.items.get(item), inputType + " | " + parameterDescription + ": Remaining item amount mismatch.");
|
||||
assertEquals(expectedEfficiency, entity.productionEfficiency, inputType + " | " + parameterDescription + ": Efficiency mismatch.");
|
||||
}catch(NullPointerException e){
|
||||
e.printStackTrace();
|
||||
//hacky, but sometimes tests fail here and I'm not going to bother testing it
|
||||
}
|
||||
}
|
||||
|
||||
/** Makes sure the efficiency stays equal during the item duration. */
|
||||
|
|
@ -163,19 +152,18 @@ public class ItemLiquidGeneratorTests extends PowerTestFixture{
|
|||
createGenerator(inputType);
|
||||
|
||||
// Burn a single coal and test for the duration
|
||||
entity.items.add(Items.coal, 1);
|
||||
entity.updateConsumption();
|
||||
entity.updateTile();
|
||||
build.items.add(Items.coal, 1);
|
||||
build.update();
|
||||
|
||||
float expectedEfficiency = entity.productionEfficiency;
|
||||
float expectedEfficiency = build.productionEfficiency;
|
||||
|
||||
float currentDuration = 0.0f;
|
||||
while((currentDuration += Time.delta) <= fakeItemDuration){
|
||||
entity.updateTile();
|
||||
assertEquals(expectedEfficiency, entity.productionEfficiency, "Duration: " + currentDuration);
|
||||
build.update();
|
||||
assertEquals(expectedEfficiency, build.productionEfficiency, "Duration: " + currentDuration);
|
||||
}
|
||||
entity.updateTile();
|
||||
assertEquals(0.0f, entity.productionEfficiency, "Duration: " + currentDuration);
|
||||
build.update();
|
||||
assertEquals(0.0f, build.productionEfficiency, "Duration: " + currentDuration);
|
||||
}
|
||||
|
||||
enum InputType{
|
||||
Loading…
Add table
Add a link
Reference in a new issue