Files
claudeandClaude Opus 5.5 3f5c0c6848
test / test (push) Failing after 11m56s
2D liquids: water and lava that flow only on the plane
- strata_core/liquids.lua: strata.register_liquid; water and lava as
  leveled nodeboxes (level 1..64 in param2) moved by a cellular sim on
  z = 0 only: react (lava + water = ashglass), fall up to 3 rows, share
  level with lower side neighbours. Active-set driven; cells wake on
  place/dig next to them, neighbour changes and block load (LBM).
- Buckets: empty, water and lava (3 iron bars at an anvil).
- Mapgen: cave pools resting on a solid floor, water in the upper three
  layers, lava in the underworld (mg.pool_liquid).
- strata_player/swim.lua: liquid physics for the avatar (slower walk,
  reduced gravity, sink cap, hold jump to swim, surface hop), own breath
  with a HUD bubble bar, drowning and lava damage; liquids break falls.
- Placeholder textures for water, lava, ashglass, buckets and bubbles.
- 8 new selftest cases; layer tests treat liquid as open cave.

Closes #22

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 01:22:25 -05:00

467 lines
13 KiB
Lua

-- Strata liquids: water and lava that flow only on the gameplay plane (#22).
--
-- Engine liquids flow in 3D and would leak towards the camera, so Strata
-- runs its own cellular simulation on z = PLANE_Z. Each liquid cell holds a
-- level in param2, 1..LEVEL_MAX (a leveled nodebox: the surface sits at
-- param2 / 64 of the cell). Every SIM_STEP seconds the active cells:
-- 1. react with a touching liquid of another kind (water + lava = ashglass),
-- 2. fall into the cell below if it is open, or top it up,
-- 3. share their level evenly with lower neighbours to the left and right.
-- Liquid is conserved except for spreads too thin to split (see MIN_SPREAD)
-- and blocks placed into it, which displace it.
--
-- Only cells that might move are simulated. A cell becomes active when it is
-- placed, when a neighbour changes, when a block next to it is dug or
-- placed, or when its mapblock loads and it has somewhere to go. Unloaded
-- neighbours ("ignore") count as solid.
--
-- Swimming, breath and lava damage for avatars live in strata_player
-- (swim.lua), which reads the per-liquid physics from strata.liquids.
local S = strata.S
local PLANE_Z = strata.PLANE_Z
local LEVEL_MAX = 64 -- param2 of a full cell
local SIM_STEP = 0.15 -- seconds between simulation ticks
local MAX_UPDATES = 600 -- cell updates per tick; the rest wait a tick
local FALL_CELLS = 3 -- rows a falling cell drops per update
local MIN_SPREAD = 2 -- a sideways share below this doesn't split off
local BUCKET_MIN = 32 -- an empty bucket needs at least half a cell
local REACT_NODE = "strata_core:ashglass"
strata.LIQUID_LEVEL_MAX = LEVEL_MAX
strata.LIQUID_SIM_STEP = SIM_STEP
strata.liquids = {}
local SIDES = {vector.new(-1, 0, 0), vector.new(1, 0, 0)}
local NEIGHBOURS = {
vector.new(-1, 0, 0), vector.new(1, 0, 0),
vector.new(0, -1, 0), vector.new(0, 1, 0),
}
--------------------------------------------------------------------------
-- Active set
--------------------------------------------------------------------------
local active = {} -- hash -> pos, cells to update next tick
local n_active = 0
local tick = 0
local function activate(pos)
if pos.z ~= PLANE_Z then
return
end
local h = core.hash_node_position(pos)
if not active[h] then
active[h] = vector.new(pos.x, pos.y, pos.z)
n_active = n_active + 1
end
end
--- Wake the liquid at pos and around it (call after changing a plane cell).
function strata.liquid_wake(pos)
activate(pos)
for _, d in ipairs(NEIGHBOURS) do
activate(vector.add(pos, d))
end
end
function strata.liquid_active_count()
return n_active
end
--------------------------------------------------------------------------
-- Queries
--------------------------------------------------------------------------
--- The liquid def and level (1..LEVEL_MAX) in a node, or nil.
function strata.liquid_of(node)
local def = strata.liquids[node.name]
if not def then
return nil
end
local level = node.param2
if level < 1 or level > LEVEL_MAX then
level = LEVEL_MAX
end
return def, level
end
--- The liquid def and level at pos, or nil. With surface = true, a partly
-- filled cell only counts below its surface.
function strata.liquid_at(pos, surface)
local cell = vector.round(pos)
local def, level = strata.liquid_of(core.get_node(cell))
if def and surface and pos.y - (cell.y - 0.5) > level / LEVEL_MAX then
return nil
end
return def, level
end
local function is_open(name)
return name == "air"
end
local function set_level(pos, name, level)
if level <= 0 then
core.swap_node(pos, {name = "air"})
else
core.swap_node(pos, {name = name, param2 = level})
end
end
--------------------------------------------------------------------------
-- Simulation
--------------------------------------------------------------------------
-- A liquid touching a different liquid that it reacts with: the cell that
-- holds the reacting liquid (`reacts_with` is the other's name) turns solid.
local function react(pos, def)
if not def.reacts_with then
return false
end
for _, d in ipairs(NEIGHBOURS) do
local p = vector.add(pos, d)
if core.get_node(p).name == def.reacts_with then
core.set_node(pos, {name = def.react_into or REACT_NODE})
strata.liquid_wake(pos)
return true
end
end
return false
end
-- One update of the liquid at pos. Returns true if anything changed.
local function update(pos)
local node = core.get_node(pos)
local def, level = strata.liquid_of(node)
if not def then
return false
end
if react(pos, def) then
return true
end
local name = node.name
local changed = false
-- fall, up to FALL_CELLS rows at once
local below = vector.new(pos.x, pos.y - 1, pos.z)
local bnode = core.get_node(below)
if is_open(bnode.name) then
local target = below
for _ = 2, FALL_CELLS do
local p = vector.new(target.x, target.y - 1, target.z)
if not is_open(core.get_node(p).name) then
break
end
target = p
end
set_level(target, name, level)
set_level(pos, name, 0)
strata.liquid_wake(pos)
strata.liquid_wake(target)
return true
elseif bnode.name == name then
local _, blevel = strata.liquid_of(bnode)
local t = math.min(level, LEVEL_MAX - blevel)
if t > 0 then
set_level(below, name, blevel + t)
level = level - t
changed = true
activate(below)
if level == 0 then
set_level(pos, name, 0)
strata.liquid_wake(pos)
return true
end
end
end
-- spread sideways towards lower neighbours
local cells, total = {}, level
for _, d in ipairs(SIDES) do
local p = vector.add(pos, d)
local n = core.get_node(p)
local other
if is_open(n.name) then
other = 0
elseif n.name == name then
local _, l = strata.liquid_of(n)
other = l
end
if other and other < level - 1 then
cells[#cells + 1] = {pos = p, level = other}
total = total + other
end
end
if #cells > 0 then
local share = math.floor(total / (#cells + 1))
if share >= MIN_SPREAD then
local rest = total - share * (#cells + 1)
for _, c in ipairs(cells) do
if c.level ~= share then
set_level(c.pos, name, share)
strata.liquid_wake(c.pos)
end
end
level = share + rest
changed = true
end
end
if changed then
set_level(pos, name, level)
strata.liquid_wake(pos)
end
return changed
end
strata.liquid_update = update
--- Run one simulation tick now. Returns how many cells were updated.
function strata.liquid_tick()
tick = tick + 1
local batch = active
active, n_active = {}, 0
local done = 0
for h, pos in pairs(batch) do
if done >= MAX_UPDATES then
if not active[h] then
active[h] = pos
n_active = n_active + 1
end
else
local def = strata.liquids[core.get_node(pos).name]
if def and def.slowness > 1 and tick % def.slowness ~= 0 then
activate(pos) -- viscous: try again next tick
else
update(pos)
done = done + 1
end
end
end
return done
end
local timer = 0
core.register_globalstep(function(dtime)
timer = timer + dtime
if timer < SIM_STEP then
return
end
timer = 0
if n_active > 0 then
strata.liquid_tick()
end
end)
-- Digging or placing anything on the plane wakes liquid next to it.
core.register_on_dignode(function(pos)
strata.liquid_wake(pos)
end)
core.register_on_placenode(function(pos)
strata.liquid_wake(pos)
end)
--------------------------------------------------------------------------
-- Registration
--------------------------------------------------------------------------
--- Register a 2D liquid. def fields:
-- description, texture, alpha (0..255, texture opacity), light_source
-- slowness simulate only every Nth tick (1 = every tick)
-- reacts_with name of a liquid that turns this one into react_into
-- react_into node the cell becomes (default strata_core:ashglass)
-- physics for strata_player/swim.lua:
-- gravity fraction of normal gravity on a submerged avatar
-- speed fraction of normal walk speed
-- sink fastest fall speed while submerged (nodes/s)
-- swim upward speed while jump is held
-- drowning true if it uses up breath
-- damage HP per second to a submerged avatar
function strata.register_liquid(name, def)
def.name = name
def.slowness = def.slowness or 1
strata.liquids[name] = def
core.register_node(name, {
description = def.description,
drawtype = "nodebox",
tiles = {{name = def.texture, backface_culling = true}},
use_texture_alpha = def.alpha and "blend" or "opaque",
paramtype = "light",
paramtype2 = "leveled",
place_param2 = LEVEL_MAX,
node_box = {type = "leveled", fixed = {-0.5, -0.5, -0.5, 0.5, -0.5, 0.5}},
sunlight_propagates = true,
light_source = def.light_source,
walkable = false,
pointable = false,
diggable = false,
buildable_to = true,
is_ground_content = false,
drop = "",
post_effect_color = def.post_effect_color,
groups = {liquid = 1, not_in_creative_inventory = 1},
on_construct = function(pos)
strata.liquid_wake(pos)
end,
})
end
strata.register_liquid("strata_core:water", {
description = S("Water"),
texture = "strata_water.png", alpha = true,
slowness = 1,
reacts_with = nil,
gravity = 0.3, speed = 0.55, sink = 3, swim = 4.5,
drowning = true, damage = 0,
})
strata.register_liquid("strata_core:lava", {
description = S("Lava"),
texture = "strata_lava.png",
light_source = 12,
slowness = 4,
reacts_with = "strata_core:water",
gravity = 0.45, speed = 0.35, sink = 2, swim = 2.5,
drowning = false, damage = 60,
})
-- What lava turns into where it meets water.
core.register_node(REACT_NODE, {
description = S("Ashglass Block"),
tiles = {"strata_ashglass.png"},
groups = {cracky = 1},
})
-- After a restart the active set is empty, so liquid that was still moving
-- (or that a newly generated block left hanging) wakes up when it loads.
core.register_lbm({
label = "Wake unsettled liquid",
name = "strata_core:liquid_wake",
nodenames = {"group:liquid"},
run_at_every_load = true,
action = function(pos, node)
if pos.z ~= PLANE_Z then
return
end
local def, level = strata.liquid_of(node)
local below = core.get_node(vector.new(pos.x, pos.y - 1, pos.z))
local _, blevel = strata.liquid_of(below)
if is_open(below.name) or (below.name == node.name and blevel < LEVEL_MAX) then
activate(pos)
return
end
for _, d in ipairs(SIDES) do
local n = core.get_node(vector.add(pos, d))
local _, l = strata.liquid_of(n)
if (is_open(n.name) and level >= 2 * MIN_SPREAD)
or (n.name == node.name and l < level - 1)
or (def.reacts_with and n.name == def.reacts_with) then
activate(pos)
return
end
end
end,
})
--------------------------------------------------------------------------
-- Buckets
--------------------------------------------------------------------------
-- The plane cell a bucket acts on: the cell in front of the pointed wall
-- or backdrop. Pointing at a block on the plane gives nothing.
local function bucket_cell(pointed)
if not pointed or pointed.type ~= "node" then
return nil
end
if pointed.under.z == strata.WALL_Z then
return vector.new(pointed.under.x, pointed.under.y, PLANE_Z)
end
end
local function can_use(pos, user)
local name = user and user:get_player_name() or ""
if core.is_protected(pos, name) then
core.record_protection_violation(pos, name)
return false
end
return true
end
--- Fill an empty bucket from the cell at pos. Returns the full bucket's
-- item name, or nil.
function strata.bucket_take(pos)
local node = core.get_node(pos)
local def, level = strata.liquid_of(node)
if not def or not def.bucket or level < BUCKET_MIN then
return nil
end
core.swap_node(pos, {name = "air"})
strata.liquid_wake(pos)
return def.bucket
end
--- Pour a full cell of liquid into pos. Returns true if it was poured.
function strata.bucket_pour(pos, liquid)
local node = core.get_node(pos)
if is_open(node.name) then
core.set_node(pos, {name = liquid, param2 = LEVEL_MAX})
elseif node.name == liquid then
core.swap_node(pos, {name = liquid, param2 = LEVEL_MAX})
strata.liquid_wake(pos)
else
return false
end
return true
end
local function give_back(itemstack, user, item)
if itemstack:get_count() == 1 then
return ItemStack(item)
end
itemstack:take_item()
local inv = user and user:get_inventory()
local left = inv and inv:add_item("main", item) or ItemStack(item)
if not left:is_empty() and user then
core.add_item(strata.get_avatar_pos(user:get_player_name()) or user:get_pos(), left)
end
return itemstack
end
core.register_craftitem("strata_core:bucket", {
description = S("Empty Bucket"),
inventory_image = "strata_bucket.png",
stack_max = 99,
liquids_pointable = false,
on_place = function(itemstack, user, pointed)
local pos = bucket_cell(pointed)
if not pos or not can_use(pos, user) then
return itemstack
end
local full = strata.bucket_take(pos)
if not full then
return itemstack
end
strata.play_place_sound(pos, "strata_core:water")
return give_back(itemstack, user, full)
end,
})
for liquid, item in pairs({["strata_core:water"] = "strata_core:bucket_water",
["strata_core:lava"] = "strata_core:bucket_lava"}) do
local ldef = strata.liquids[liquid]
ldef.bucket = item
core.register_craftitem(item, {
description = S("@1 Bucket", ldef.description),
inventory_image = "strata_" .. item:sub(13) .. ".png",
stack_max = 1,
on_place = function(itemstack, user, pointed)
local pos = bucket_cell(pointed)
if not pos or not can_use(pos, user) or not strata.bucket_pour(pos, liquid) then
return itemstack
end
return ItemStack("strata_core:bucket")
end,
})
end