Bugfixes & liquid unit tests

This commit is contained in:
Anuken
2020-03-16 10:41:32 -04:00
parent 1e3326fb8a
commit 928812142a
4 changed files with 68 additions and 9 deletions

View File

@@ -161,10 +161,14 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc{
public byte absoluteRelativeTo(int cx, int cy){ public byte absoluteRelativeTo(int cx, int cy){
int x = tile.x, y = tile.y; int x = tile.x, y = tile.y;
if(x == cx && y <= cy - 1) return 1; if(Math.abs(x - cx) > Math.abs(y - cy)){
if(x == cx && y >= cy + 1) return 3; if(x <= cx - 1 && y == cy) return 0;
if(x <= cx - 1 && y == cy) return 0; if(x >= cx + 1 && y == cy) return 2;
if(x >= cx + 1 && y == cy) return 2; }else{
if(x == cx && y <= cy - 1) return 1;
if(x == cx && y >= cy + 1) return 3;
}
return -1; return -1;
} }
@@ -360,11 +364,11 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc{
public float moveLiquid(Tilec next, float leakResistance, Liquid liquid){ public float moveLiquid(Tilec next, float leakResistance, Liquid liquid){
if(next == null) return 0; if(next == null) return 0;
next = next.getLiquidDestination(next, liquid); next = next.getLiquidDestination(this, liquid);
if(next.team() == team() && next.block().hasLiquids && liquids().get(liquid) > 0f){ if(next.team() == team() && next.block().hasLiquids && liquids().get(liquid) > 0f){
if(next.acceptLiquid(next, liquid, 0f)){ if(next.acceptLiquid(this, liquid, 0f)){
float ofract = next.liquids().get(liquid) / next.block().liquidCapacity; float ofract = next.liquids().get(liquid) / next.block().liquidCapacity;
float fract = liquids().get(liquid) / block.liquidCapacity * block.liquidPressure; float fract = liquids().get(liquid) / block.liquidCapacity * block.liquidPressure;
float flow = Math.min(Mathf.clamp((fract - ofract) * (1f)) * (block.liquidCapacity), liquids().get(liquid)); float flow = Math.min(Mathf.clamp((fract - ofract) * (1f)) * (block.liquidCapacity), liquids().get(liquid));

View File

@@ -121,7 +121,7 @@ public class Conduit extends LiquidBlock implements Autotiler{
smoothLiquid = Mathf.lerpDelta(smoothLiquid, liquids.currentAmount() / liquidCapacity, 0.05f); smoothLiquid = Mathf.lerpDelta(smoothLiquid, liquids.currentAmount() / liquidCapacity, 0.05f);
if(liquids.total() > 0.001f && timer(timerFlow, 1)){ if(liquids.total() > 0.001f && timer(timerFlow, 1)){
moveLiquid(tile.getNearbyEntity(tile.rotation()), leakResistance, liquids.current()); moveLiquid(tile.getNearbyEntity(rotation()), leakResistance, liquids.current());
noSleep(); noSleep();
}else{ }else{
sleep(); sleep();

View File

@@ -37,10 +37,10 @@ public class LiquidJunction extends LiquidBlock{
@Override @Override
public Tilec getLiquidDestination(Tilec source, Liquid liquid){ public Tilec getLiquidDestination(Tilec source, Liquid liquid){
int dir = source.relativeTo(tile.x, tile.y); int dir = source.absoluteRelativeTo(tile.x, tile.y);
dir = (dir + 4) % 4; dir = (dir + 4) % 4;
Tilec next = nearby(dir); Tilec next = nearby(dir);
if(next == null || !next.acceptLiquid(this, liquid, 0f) && !(next.block() instanceof LiquidJunction)){ if(next == null || (!next.acceptLiquid(this, liquid, 0f) && !(next.block() instanceof LiquidJunction))){
return this; return this;
} }
return next.getLiquidDestination(this, liquid); return next.getLiquidDestination(this, liquid);

View File

@@ -212,6 +212,61 @@ public class ApplicationTests{
assertTrue(state.teams.playerCores().size > 0); assertTrue(state.teams.playerCores().size > 0);
} }
void updateBlocks(int times){
for(Tile tile : world.tiles){
if(tile.entity != null && tile.isCenter()){
tile.entity.updateProximity();
}
}
for(int i = 0; i < times; i++){
Time.update();
for(Tile tile : world.tiles){
if(tile.entity != null && tile.isCenter()){
tile.entity.update();
}
}
}
}
@Test
void liquidOutput(){
world.loadMap(testMap);
state.set(State.playing);
world.tile(0, 0).setBlock(Blocks.liquidSource);
world.tile(0, 0).configureAny(Liquids.water);
world.tile(2, 1).setBlock(Blocks.liquidTank);
updateBlocks(10);
assertTrue(world.tile(2, 1).entity.liquids().currentAmount() >= 1);
assertTrue(world.tile(2, 1).entity.liquids().current() == Liquids.water);
}
@Test
void liquidJunctionOutput(){
world.loadMap(testMap);
state.set(State.playing);
Tile source = world.rawTile(0, 0), tank = world.rawTile(1, 4), junction = world.rawTile(0, 1), conduit = world.rawTile(0, 2);
source.setBlock(Blocks.liquidSource);
source.configureAny(Liquids.water);
junction.setBlock(Blocks.liquidJunction);
conduit.setBlock(Blocks.conduit, Team.derelict, 1);
tank.setBlock(Blocks.liquidTank);
updateBlocks(10);
assertTrue(tank.entity.liquids().currentAmount() >= 1, "Liquid not moved through junction");
assertTrue(tank.entity.liquids().current() == Liquids.water, "Tank has no water");
}
@Test @Test
void conveyorCrash(){ void conveyorCrash(){
world.loadMap(testMap); world.loadMap(testMap);