summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 2e5cf19)
raw | patch | inline | side by side (parent: 2e5cf19)
author | fred-boy <fredboy@protonmail.com> | |
Fri, 27 Sep 2019 08:10:59 +0000 (15:10 +0700) | ||
committer | fred-boy <fredboy@protonmail.com> | |
Sun, 29 Sep 2019 04:39:17 +0000 (11:39 +0700) |
(might be buggy)
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java b/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java
--- /dev/null
@@ -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 bd0e5bb1dcd90f2788e7b2bd15f47ac9da3fed3e..c3e020791570a456bf906f41165d8a76cb654171 100644 (file)
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 53b2b9060ed6eca4fd65e0aa73d4c4380837d47e..d018b3e35b6817955f6f0e529c65e349643be323 100644 (file)
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;
public ArrayList<Mob> mobs;
public ArrayList<Drop> drops;
- private long fluidLastUpdateTimestamp = 0;
-
public boolean isTouchDown, isKeyDown;
public int ctrlMode, touchDownX, touchDownY, touchDownBtn, keyDownCode;
public long touchDownTime;
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() {
}
}
- 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()) {
}
}
- 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()) {