From 793c88f81ae273b531dcdea267e1720c6878cc95 Mon Sep 17 00:00:00 2001 From: fred-boy Date: Fri, 27 Sep 2019 15:10:59 +0700 Subject: [PATCH] Rewrite fluid updater (might be buggy) --- .../cavedroid/game/GameFluidsThread.java | 125 +++++++++ .../cavedroid/game/GameItems.java | 6 + .../deadsoftware/cavedroid/game/GameProc.java | 253 +----------------- 3 files changed, 133 insertions(+), 251 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java b/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java new file mode 100644 index 0000000..272cd5d --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java @@ -0,0 +1,125 @@ +package ru.deadsoftware.cavedroid.game; + +import java.util.Arrays; + +import static ru.deadsoftware.cavedroid.GameScreen.GP; +import static ru.deadsoftware.cavedroid.game.GameItems.*; + +public class GameFluidsThread extends Thread { + + private static final int FLUID_UPDATE_INTERVAL_MS = 150; + private static final int FLUID_STATES = 5; + + private static final int[] WATER_IDS = {8, 60, 61, 62, 63}; + private static final int[] LAVA_IDS = {9, 64, 65, 65, 67}; + + private long fluidLastUpdateTimestamp = 0; + + private int getBlockState(int id) { + return isWater(id) ? Arrays.binarySearch(WATER_IDS, id) : Arrays.binarySearch(LAVA_IDS, id); + } + + private int getNextBlockState(int id) { + if (!isFluid(id)) { + return -1; + } + int state = getBlockState(id); + if (state < FLUID_STATES - 1) { + return state + 1; + } + return -1; + } + + private int getNextBlockStateId(int id) { + int nextState = getNextBlockState(id); + if (nextState == -1) return 0; + if (isWater(id)) { + return WATER_IDS[nextState]; + } + return LAVA_IDS[nextState]; + } + + private int id(int x, int y) { + return GP.world.getForeMap(x, y); + } + + private boolean sameFluid(int thisId, int thatId) { + return isFluid(thatId) && isWater(thatId) == isWater(thisId); + } + + private boolean noFluidNearby(int x, int y) { + return !isFluid(id(x, y - 1)) && + (!isFluid(id(x - 1, y)) || id(x - 1, y) >= id(x, y)) && + (!isFluid(id(x + 1, y)) || id(x + 1, y) >= id(x, y)); + } + + private boolean drainFluid(int x, int y) { + if (getBlockState(id(x, y)) > 0) { + if (noFluidNearby(x, y)) { + GP.world.setForeMap(x, y, getNextBlockStateId(id(x, y))); + } + if (!isFluid(id(x, y))) { + GP.world.setForeMap(x, y, 0); + return true; + } + } + return false; + } + + private void flowFluidTo(int thisId, int x, int y, int nextStateId) { + int thatId = id(x, y); + if (fluidCanFlowThere(thisId, thatId)) { + GP.world.setForeMap(x, y, nextStateId); + } else if (isWater(thisId) && isLava(thatId)) { + if (getBlockState(thatId) > 0) { + GP.world.setForeMap(x, y, 4); //cobblestone + } else { + GP.world.setForeMap(x, y, 68); //obsidian + } + } else if (isLava(thisId) && isWater(thatId)) { + GP.world.setForeMap(x, y, 1); //stone + } + } + + private void flowFluid(int x, int y) { + int id = id(x, y); + if (getBlockState(id) < FLUID_STATES - 1 && getBlock(id(x, y + 1)).hasCollision()) { + int nextState = getNextBlockState(id); + int nextStateId = getNextBlockStateId(id); + if (nextState == 1) { + nextStateId++; + } + flowFluidTo(id, x - 1, y, nextStateId); + flowFluidTo(id, x + 1, y, nextStateId); + } else { + flowFluidTo(id, x, y + 1, isWater(id) ? WATER_IDS[1] : LAVA_IDS[1]); + } + + } + + private void updateFluids(int x, int y) { + if (!isFluid(id(x, y))) return; + if (drainFluid(x, y)) return; + flowFluid(x, y); + } + + private void fluidUpdater() { + int midScreen = (int) (GP.renderer.getCamX() + GP.renderer.getWidth() / 2) / 16; + for (int y = 0; y < GP.world.getHeight(); y++) { + for (int x = 0; x < (int) (GP.renderer.getWidth() / 2) / 16 + 1; x++) { + updateFluids(midScreen + x, y); + updateFluids(midScreen - x, y); + } + } + } + + @Override + public void run() { + while (!this.isInterrupted()) { + if (System.currentTimeMillis() - fluidLastUpdateTimestamp > FLUID_UPDATE_INTERVAL_MS) { + fluidUpdater(); + fluidLastUpdateTimestamp = System.currentTimeMillis(); + } + } + } +} diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java index bd0e5bb..c3e0207 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java @@ -35,6 +35,12 @@ public class GameItems { return getBlock(id).getMeta().equals("slab"); } + public static boolean fluidCanFlowThere(int thisId, int thatId) { + return thatId == 0 || (!getBlock(thatId).hasCollision() && !isFluid(thatId)) || + (isWater(thisId) && isWater(thatId) && thatId >= thisId) || + (isLava(thisId) && isLava(thatId) && thatId >= thisId); + } + public static Block getBlock(int id) { return blocks.getValueAt(id); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java index 53b2b90..d018b3e 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java @@ -19,13 +19,10 @@ import java.io.Serializable; import java.util.ArrayList; public class GameProc implements Serializable, Disposable { - - private static final int FLUID_UPDATE_INTERVAL_MS = 100; - static boolean DO_UPD = false; static int UPD_X = -1, UPD_Y = -1; - private transient Thread fluidThread; + private transient GameFluidsThread fluidThread; public transient GameWorld world; public transient GameRenderer renderer; @@ -35,8 +32,6 @@ public class GameProc implements Serializable, Disposable { public ArrayList mobs; public ArrayList drops; - private long fluidLastUpdateTimestamp = 0; - public boolean isTouchDown, isKeyDown; public int ctrlMode, touchDownX, touchDownY, touchDownBtn, keyDownCode; public long touchDownTime; @@ -66,21 +61,13 @@ public class GameProc implements Serializable, Disposable { maxCreativeScroll = GameItems.getItemsSize() / 8; createFluidThread(); - fluidThread.start(); GameSaver.save(this); } private void createFluidThread() { - fluidThread = new Thread(() -> { - while (!fluidThread.isInterrupted()) { - if (System.currentTimeMillis() - fluidLastUpdateTimestamp > FLUID_UPDATE_INTERVAL_MS) { - fluidUpdater(); - fluidLastUpdateTimestamp = System.currentTimeMillis(); - } - } - }); + fluidThread = new GameFluidsThread(); } public void resetRenderer() { @@ -138,232 +125,6 @@ public class GameProc implements Serializable, Disposable { } } - private void updateFluids(int x, int y) { - if (GameItems.isWater(world.getForeMap(x, y)) && world.getForeMap(x, y) != 8) { - if (world.getForeMap(x, y) == 60) { - if (!GameItems.isWater(world.getForeMap(x, y - 1))) - world.setForeMap(x, y, world.getForeMap(x, y) + 1); - } else if ((!GameItems.isWater(world.getForeMap(x - 1, y)) || - (GameItems.isWater(world.getForeMap(x, y)) && world.getForeMap(x - 1, y) >= world.getForeMap(x, y))) && - (!GameItems.isWater(world.getForeMap(x + 1, y)) || - (GameItems.isWater(world.getForeMap(x, y)) && world.getForeMap(x + 1, y) >= world.getForeMap(x, y)))) { - world.setForeMap(x, y, world.getForeMap(x, y) + 1); - } - if (world.getForeMap(x, y) > 63) world.setForeMap(x, y, 0); - } - - if (world.getForeMap(x, y) == 8 || world.getForeMap(x, y) == 60) { - if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 61 && world.getForeMap(x, y + 1) <= 63) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { - world.setForeMap(x, y + 1, 60); - updateBlock(x, y + 2); - } else if (GameItems.isLava(world.getForeMap(x, y + 1))) { - if (world.getForeMap(x, y + 1) > 9) world.setForeMap(x, y + 1, 4); - else world.setForeMap(x, y + 1, 68); - } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { - if (world.getForeMap(x + 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x + 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x + 1, y))) || - (GameItems.isWater(world.getForeMap(x + 1, y)) && world.getForeMap(x + 1, y) > 61)) { - world.setForeMap(x + 1, y, 61); - updateBlock(x + 1, y + 1); - } else if (GameItems.isLava(world.getForeMap(x + 1, y))) { - if (world.getForeMap(x + 1, y) > 9) world.setForeMap(x + 1, y, 4); - else world.setForeMap(x + 1, y, 68); - } else if (world.getForeMap(x + 1, y) == 61 && (world.getForeMap(x + 2, y) == 8 || world.getForeMap(x + 2, y) == 60)) - world.setForeMap(x + 1, y, 8); - - if (world.getForeMap(x - 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x - 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x - 1, y))) || - (GameItems.isWater(world.getForeMap(x - 1, y)) && world.getForeMap(x - 1, y) > 61)) { - world.setForeMap(x - 1, y, 61); - updateBlock(x - 1, y + 1); - } else if (GameItems.isLava(world.getForeMap(x - 1, y))) { - if (world.getForeMap(x - 1, y) > 9) world.setForeMap(x - 1, y, 4); - else world.setForeMap(x - 1, y, 68); - } else if (world.getForeMap(x - 1, y) == 61 && (world.getForeMap(x - 2, y) == 8 || world.getForeMap(x - 2, y) == 60)) - world.setForeMap(x - 1, y, 8); - } - return; - } - if (world.getForeMap(x, y) == 61) { - if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 61 && world.getForeMap(x, y + 1) <= 63) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { - world.setForeMap(x, y + 1, 60); - updateBlock(x, y + 2); - } else if (GameItems.isLava(world.getForeMap(x, y + 1))) { - if (world.getForeMap(x, y + 1) > 9) world.setForeMap(x, y + 1, 4); - else world.setForeMap(x, y + 1, 68); - } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { - if (world.getForeMap(x + 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x + 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x + 1, y))) || - (GameItems.isWater(world.getForeMap(x + 1, y)) && world.getForeMap(x + 1, y) > 62)) { - world.setForeMap(x + 1, y, 62); - updateBlock(x + 1, y + 1); - } else if (GameItems.isLava(world.getForeMap(x + 1, y))) { - if (world.getForeMap(x + 1, y) > 9) world.setForeMap(x + 1, y, 4); - else world.setForeMap(x + 1, y, 68); - } - - if (world.getForeMap(x - 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x - 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x - 1, y))) || - (GameItems.isWater(world.getForeMap(x - 1, y)) && world.getForeMap(x - 1, y) > 62)) { - world.setForeMap(x - 1, y, 62); - updateBlock(x - 1, y + 1); - } else if (GameItems.isLava(world.getForeMap(x - 1, y))) { - if (world.getForeMap(x - 1, y) > 9) world.setForeMap(x - 1, y, 4); - else world.setForeMap(x - 1, y, 68); - } - } - return; - } - if (world.getForeMap(x, y) == 62) { - if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 61 && world.getForeMap(x, y + 1) <= 63) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { - world.setForeMap(x, y + 1, 60); - updateBlock(x, y + 2); - } else if (GameItems.isLava(world.getForeMap(x, y + 1))) { - if (world.getForeMap(x, y + 1) > 9) world.setForeMap(x, y + 1, 4); - else world.setForeMap(x, y + 1, 68); - } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { - if (world.getForeMap(x + 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x + 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x + 1, y)))) { - world.setForeMap(x + 1, y, 63); - updateBlock(x + 1, y + 1); - } else if (GameItems.isLava(world.getForeMap(x + 1, y))) { - if (world.getForeMap(x + 1, y) > 9) world.setForeMap(x + 1, y, 4); - else world.setForeMap(x + 1, y, 68); - } - - if (world.getForeMap(x - 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x - 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x - 1, y)))) { - world.setForeMap(x - 1, y, 63); - updateBlock(x - 1, y + 1); - } else if (GameItems.isLava(world.getForeMap(x - 1, y))) { - if (world.getForeMap(x - 1, y) > 9) world.setForeMap(x - 1, y, 4); - else world.setForeMap(x - 1, y, 68); - } - } - return; - } - if (world.getForeMap(x, y) == 63) { - if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 61 && world.getForeMap(x, y + 1) <= 63) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { - world.setForeMap(x, y + 1, 60); - updateBlock(x, y + 2); - } else if (GameItems.isLava(world.getForeMap(x, y + 1))) { - if (world.getForeMap(x, y + 1) > 9) world.setForeMap(x, y + 1, 4); - else world.setForeMap(x, y + 1, 68); - } - return; - } - - if (GameItems.isLava(world.getForeMap(x, y)) && world.getForeMap(x, y) != 9) { - if (world.getForeMap(x, y) == 64) { - if (!GameItems.isLava(world.getForeMap(x, y - 1))) - world.setForeMap(x, y, world.getForeMap(x, y) + 1); - } else if ((!GameItems.isLava(world.getForeMap(x, y - 1))) && - (!GameItems.isLava(world.getForeMap(x - 1, y)) || - (GameItems.isLava(world.getForeMap(x, y)) && world.getForeMap(x - 1, y) >= world.getForeMap(x, y))) && - (!GameItems.isLava(world.getForeMap(x + 1, y)) || - (GameItems.isLava(world.getForeMap(x, y)) && world.getForeMap(x + 1, y) >= world.getForeMap(x, y)))) { - world.setForeMap(x, y, world.getForeMap(x, y) + 1); - } - if (world.getForeMap(x, y) > 67) world.setForeMap(x, y, 0); - } - - if (world.getForeMap(x, y) == 9 || world.getForeMap(x, y) == 64) { - if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 65 && world.getForeMap(x, y + 1) <= 67) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { - world.setForeMap(x, y + 1, 64); - updateBlock(x, y + 2); - } else if (GameItems.isWater(world.getForeMap(x, y + 1))) { - world.setForeMap(x, y + 1, 1); - } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { - if (world.getForeMap(x + 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x + 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x + 1, y))) || - (GameItems.isLava(world.getForeMap(x + 1, y)) && world.getForeMap(x + 1, y) > 65)) { - world.setForeMap(x + 1, y, 65); - updateBlock(x + 1, y + 1); - } else if (GameItems.isWater(world.getForeMap(x + 1, y))) { - world.setForeMap(x + 1, y, 1); - } - - if (world.getForeMap(x - 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x - 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x - 1, y))) || - (GameItems.isLava(world.getForeMap(x - 1, y)) && world.getForeMap(x - 1, y) > 65)) { - world.setForeMap(x - 1, y, 65); - updateBlock(x - 1, y + 1); - } else if (GameItems.isWater(world.getForeMap(x - 1, y))) { - world.setForeMap(x - 1, y, 1); - } - } - return; - } - if (world.getForeMap(x, y) == 65) { - if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 65 && world.getForeMap(x, y + 1) <= 67) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { - world.setForeMap(x, y + 1, 64); - updateBlock(x, y + 2); - } else if (GameItems.isWater(world.getForeMap(x, y + 1))) { - world.setForeMap(x, y + 1, 1); - } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { - if (world.getForeMap(x + 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x + 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x + 1, y))) || - (GameItems.isLava(world.getForeMap(x + 1, y)) && world.getForeMap(x + 1, y) > 66)) { - world.setForeMap(x + 1, y, 66); - updateBlock(x + 1, y + 1); - } else if (GameItems.isWater(world.getForeMap(x + 1, y))) { - world.setForeMap(x + 1, y, 1); - } - - if (world.getForeMap(x - 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x - 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x - 1, y))) || - (GameItems.isLava(world.getForeMap(x - 1, y)) && world.getForeMap(x - 1, y) > 66)) { - world.setForeMap(x - 1, y, 66); - updateBlock(x - 1, y + 1); - } else if (GameItems.isWater(world.getForeMap(x - 1, y))) { - world.setForeMap(x - 1, y, 1); - } - } - return; - } - if (world.getForeMap(x, y) == 66) { - if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 65 && world.getForeMap(x, y + 1) <= 67) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { - world.setForeMap(x, y + 1, 64); - updateBlock(x, y + 2); - } else if (GameItems.isWater(world.getForeMap(x, y + 1))) { - world.setForeMap(x, y + 1, 1); - } else if (GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { - if (world.getForeMap(x + 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x + 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x + 1, y)))) { - world.setForeMap(x + 1, y, 67); - updateBlock(x + 1, y + 1); - } else if (GameItems.isWater(world.getForeMap(x + 1, y))) { - world.setForeMap(x + 1, y, 1); - } - - if (world.getForeMap(x - 1, y) == 0 || - (!GameItems.getBlock(world.getForeMap(x - 1, y)).hasCollision() && !GameItems.isFluid(world.getForeMap(x - 1, y)))) { - world.setForeMap(x - 1, y, 67); - updateBlock(x - 1, y + 1); - } else if (GameItems.isWater(world.getForeMap(x - 1, y))) { - world.setForeMap(x - 1, y, 1); - } - } - return; - } - if (world.getForeMap(x, y) == 67) { - if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 65 && world.getForeMap(x, y + 1) <= 67) || - (!GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision() && !GameItems.isFluid(world.getForeMap(x, y + 1)))) { - world.setForeMap(x, y + 1, 64); - updateBlock(x, y + 2); - } else if (GameItems.isWater(world.getForeMap(x, y + 1))) { - world.setForeMap(x, y + 1, 1); - } - } - } - private void updateBlock(int x, int y) { if (world.getForeMap(x, y) == 10) { if (world.getForeMap(x, y + 1) == 0 || !GameItems.getBlock(world.getForeMap(x, y + 1)).hasCollision()) { @@ -396,16 +157,6 @@ public class GameProc implements Serializable, Disposable { } } - private void fluidUpdater() { - int midScreen = (int) (renderer.getCamX() + renderer.getWidth() / 2) / 16; - for (int y = 0; y < world.getHeight(); y++) { - for (int x = 0; x < (int) (renderer.getWidth() / 2) / 16 + 1; x++) { - updateFluids(midScreen + x, y); - updateFluids(midScreen - x, y); - } - } - } - void useItem(int x, int y, int id, boolean bg) { if (id > 0) { if (GameItems.getItem(id).isBlock()) { -- 2.29.2