From eb942371cdcdab401ccc7a203d12ba17faf07219 Mon Sep 17 00:00:00 2001 From: Alexander Nutz Date: Sat, 27 Sep 2025 18:11:12 +0200 Subject: [PATCH] proper fluid flow --- include/globals.h | 4 ++-- src/procedures.c | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/include/globals.h b/include/globals.h index 32910a7..085b2f9 100644 --- a/include/globals.h +++ b/include/globals.h @@ -34,8 +34,8 @@ // Max render distance, determines how many chunks to send #define VIEW_DISTANCE 2 -// Time between server ticks in microseconds (default = 1s) -#define TIME_BETWEEN_TICKS 1000000 +// Time between server ticks in microseconds (default = 0.05s) +#define TIME_BETWEEN_TICKS 50000 // Calculated from TIME_BETWEEN_TICKS #define TICKS_PER_SECOND ((float)1000000 / TIME_BETWEEN_TICKS) diff --git a/src/procedures.c b/src/procedures.c index 75f7c07..a0ac832 100644 --- a/src/procedures.c +++ b/src/procedures.c @@ -1037,6 +1037,20 @@ void handleFluidMovement (short x, uint8_t y, short z, uint8_t fluid, uint8_t bl // a higher fluid "level" means the fluid has traveled farther uint8_t level = block - fluid; + uint8_t flow_speed; + uint8_t max_level; + switch (fluid) { + case B_lava: + flow_speed = 30; + max_level = 3; + break; + + case B_water: + flow_speed = 5; + max_level = 7; + break; + } + // Query blocks adjacent to this fluid stream uint8_t adjacent[4] = { getBlockAt(x + 1, y, z), @@ -1058,10 +1072,10 @@ void handleFluidMovement (short x, uint8_t y, short z, uint8_t fluid, uint8_t bl // If not connected, clear this block and recalculate surrounding flow if (!connected) { makeBlockChange(x, y, z, B_air); - checkFluidUpdate(x + 1, y, z, adjacent[0]); - checkFluidUpdate(x - 1, y, z, adjacent[1]); - checkFluidUpdate(x, y, z + 1, adjacent[2]); - checkFluidUpdate(x, y, z - 1, adjacent[3]); + deferBlockUpdate(x + 1, y, z, flow_speed); + deferBlockUpdate(x - 1, y, z, flow_speed); + deferBlockUpdate(x, y, z + 1, flow_speed); + deferBlockUpdate(x, y, z - 1, flow_speed); return; } } @@ -1070,29 +1084,29 @@ void handleFluidMovement (short x, uint8_t y, short z, uint8_t fluid, uint8_t bl uint8_t block_below = getBlockAt(x, y - 1, z); if (isReplaceableBlock(block_below)) { makeBlockChange(x, y - 1, z, fluid); - return handleFluidMovement(x, y - 1, z, fluid, fluid); + deferBlockUpdate(x, y - 1, z, flow_speed); + return; } // Stop flowing laterally at the maximum level - if (level == 3 && fluid == B_lava) return; - if (level == 7) return; + if (level == max_level) return; // Handle lateral water flow, increasing level by 1 if (isReplaceableFluid(adjacent[0], level, fluid)) { makeBlockChange(x + 1, y, z, block + 1); - handleFluidMovement(x + 1, y, z, fluid, block + 1); + deferBlockUpdate(x + 1, y, z, flow_speed); } if (isReplaceableFluid(adjacent[1], level, fluid)) { makeBlockChange(x - 1, y, z, block + 1); - handleFluidMovement(x - 1, y, z, fluid, block + 1); + deferBlockUpdate(x - 1, y, z, flow_speed); } if (isReplaceableFluid(adjacent[2], level, fluid)) { makeBlockChange(x, y, z + 1, block + 1); - handleFluidMovement(x, y, z + 1, fluid, block + 1); + deferBlockUpdate(x, y, z + 1, flow_speed); } if (isReplaceableFluid(adjacent[3], level, fluid)) { makeBlockChange(x, y, z - 1, block + 1); - handleFluidMovement(x, y, z - 1, fluid, block + 1); + deferBlockUpdate(x, y, z - 1, flow_speed); } } @@ -1156,8 +1170,9 @@ void processBlockUpdate (short x, uint8_t y, short z, uint8_t block) { makeBlockChange(x, y, z, 0); makeBlockChange(x, y - 1, z, 0); makeBlockChange(x, y - 1, z, block); - // update this (now moved) block and the block above next tick - deferBlockUpdate(x, y - 1, z, 0); + // update this now moved block at the sand fall speed + deferBlockUpdate(x, y - 1, z, 15); + // update the block above next tick deferBlockUpdate(x, y + 1, z, 0); } }