2018-11-20 20:58:46 +01:00
|
|
|
import io.anuke.mindustry.Vars;
|
|
|
|
import io.anuke.mindustry.content.blocks.Blocks;
|
|
|
|
import io.anuke.mindustry.content.blocks.PowerBlocks;
|
|
|
|
import io.anuke.mindustry.content.blocks.ProductionBlocks;
|
|
|
|
import io.anuke.mindustry.core.ContentLoader;
|
|
|
|
import io.anuke.mindustry.world.Block;
|
|
|
|
import io.anuke.mindustry.world.Tile;
|
|
|
|
import io.anuke.mindustry.world.blocks.Floor;
|
|
|
|
import io.anuke.mindustry.world.blocks.power.PowerGraph;
|
|
|
|
import io.anuke.mindustry.world.blocks.production.SolidPump;
|
|
|
|
import io.anuke.mindustry.world.modules.PowerModule;
|
|
|
|
import org.junit.jupiter.api.*;
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import static io.anuke.mindustry.Vars.threads;
|
2018-11-21 00:04:26 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
2018-11-20 20:58:46 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
|
|
|
public class PowerTests{
|
|
|
|
|
|
|
|
@BeforeAll
|
|
|
|
static void initializeDependencies(){
|
|
|
|
Vars.content = new ContentLoader();
|
|
|
|
Vars.content.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
void initTest(){
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a fake tile on the given location using the given floor and block.
|
|
|
|
* @param x The X coordinate.
|
|
|
|
* @param y The y coordinate.
|
|
|
|
* @param floor The floor.
|
|
|
|
* @param block The block on the tile.
|
|
|
|
* @return The created tile or null in case of exceptions.
|
|
|
|
*/
|
|
|
|
private static Tile createFakeTile(int x, int y, Floor floor, Block block){
|
|
|
|
try{
|
|
|
|
Tile tile = new Tile(x, y);
|
|
|
|
Field field = Tile.class.getDeclaredField("wall");
|
|
|
|
field.setAccessible(true);
|
|
|
|
field.set(tile, block);
|
|
|
|
field = Tile.class.getDeclaredField("floor");
|
|
|
|
field.setAccessible(true);
|
|
|
|
field.set(tile, floor);
|
|
|
|
tile.entity = block.newEntity();
|
|
|
|
tile.entity.power = new PowerModule();
|
|
|
|
return tile;
|
|
|
|
}catch(Exception ex){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Makes sure calculations are accurate for the case where produced power = consumed power. */
|
|
|
|
@Test
|
|
|
|
void test_balancedPower(){
|
|
|
|
PowerGraph powerGraph = new PowerGraph();
|
|
|
|
|
2018-11-21 00:04:26 +01:00
|
|
|
// Create one water extractor (5.4 power/Second = 0.09/tick)
|
2018-11-20 20:58:46 +01:00
|
|
|
Tile waterExtractorTile = createFakeTile(0, 0, (Floor)Blocks.sand, ProductionBlocks.waterExtractor);
|
|
|
|
powerGraph.add(waterExtractorTile);
|
|
|
|
|
2018-11-21 00:04:26 +01:00
|
|
|
// Create 20 small solar panels (20*0.27=5.4 power/second = 0.09/tick)
|
2018-11-20 20:58:46 +01:00
|
|
|
List<Tile> solarPanelTiles = new LinkedList<>();
|
|
|
|
for(int counter = 0; counter < 20; counter++){
|
|
|
|
Tile solarPanelTile = createFakeTile( 2 + counter / 2, counter % 2, (Floor)Blocks.sand, PowerBlocks.solarPanel);
|
|
|
|
powerGraph.add(solarPanelTile);
|
|
|
|
solarPanelTiles.add(solarPanelTile);
|
|
|
|
}
|
|
|
|
|
2018-11-21 00:04:26 +01:00
|
|
|
float powerNeeded = powerGraph.getPowerNeeded();
|
|
|
|
float powerProduced = powerGraph.getPowerProduced();
|
|
|
|
|
2018-11-20 20:58:46 +01:00
|
|
|
// If these lines fail, you probably changed power production/consumption and need to adapt this test
|
2018-11-21 00:04:26 +01:00
|
|
|
// OR their implementation is corrupt.
|
2018-11-20 20:58:46 +01:00
|
|
|
// TODO: Create fake blocks which are independent of such changes
|
2018-11-21 00:04:26 +01:00
|
|
|
float epsilon = 0.00001f;
|
|
|
|
assertEquals(powerNeeded, 0.09f, epsilon);
|
|
|
|
assertEquals(powerProduced, 0.09f, epsilon);
|
|
|
|
// Note: The assertions above induce that powerNeeded = powerProduced (with floating point inaccuracy)
|
2018-11-20 20:58:46 +01:00
|
|
|
|
2018-11-21 00:04:26 +01:00
|
|
|
// Distribute power and make sure the water extractor is powered
|
|
|
|
powerGraph.distributePower(powerNeeded, powerProduced);
|
|
|
|
assertEquals(waterExtractorTile.entity.power.satisfaction, 1.0f, epsilon);
|
2018-11-20 20:58:46 +01:00
|
|
|
}
|
|
|
|
}
|