test / test (push) Failing after 11m56s
- 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>
136 lines
4.2 KiB
Lua
136 lines
4.2 KiB
Lua
-- Strata swimming: avatar physics, breath and damage in 2D liquids (#22).
|
|
--
|
|
-- The liquids themselves are in strata_core/liquids.lua. The engine's own
|
|
-- breath and node damage act on the hidden player body at the camera, so
|
|
-- the avatar keeps its own breath and shows it in a HUD statbar.
|
|
--
|
|
-- Each liquid's def (strata.liquids) says how it moves an avatar: gravity
|
|
-- and walk speed are scaled, falls are capped at `sink`, and holding jump
|
|
-- swims up at `swim`. Landing in a liquid cancels fall damage.
|
|
|
|
local BREATH_MAX = 10 -- bubbles
|
|
local BREATH_LOSS = 0.7 -- seconds per bubble with the head under water
|
|
local BREATH_GAIN = 0.15 -- seconds per bubble regained in air
|
|
local DROWN_DAMAGE = 20 -- HP per second once breath runs out
|
|
local HURT_INTERVAL = 0.5 -- seconds between damage ticks
|
|
local HEAD_Y = 0.7 -- head height above the avatar's centre
|
|
local FEET_Y = -0.8 -- feet height below the centre
|
|
local SURFACE_HOP = 1.6 -- jump at the surface is swim speed times this
|
|
|
|
local BUBBLE = "strata_bubble.png"
|
|
local HUD_OFFSET = {x = 25, y = -(48 + 24 + 16)} -- where the engine's breath bar sits
|
|
|
|
strata.swim = {
|
|
BREATH_MAX = BREATH_MAX,
|
|
BREATH_LOSS = BREATH_LOSS,
|
|
DROWN_DAMAGE = DROWN_DAMAGE,
|
|
HURT_INTERVAL = HURT_INTERVAL,
|
|
}
|
|
|
|
--- The liquid def an avatar at pos (its centre) is swimming in, or nil.
|
|
function strata.body_liquid(pos)
|
|
return strata.liquid_at(pos, true)
|
|
or strata.liquid_at(vector.offset(pos, 0, FEET_Y, 0), true)
|
|
end
|
|
|
|
--- The liquid def over an avatar's head, or nil.
|
|
function strata.head_liquid(pos)
|
|
return strata.liquid_at(vector.offset(pos, 0, HEAD_Y, 0), true)
|
|
end
|
|
|
|
--- Apply a liquid's vertical movement rules to an avatar's velocity:
|
|
-- holding jump swims up (a bigger hop with the head above the surface, to
|
|
-- climb out onto a ledge) and falls are capped. Returns the gravity to use.
|
|
-- Walk speed is scaled by the caller (liquid.speed).
|
|
function strata.swim_velocity(vel, liquid, head, ctrl, gravity)
|
|
if not liquid then
|
|
return gravity
|
|
end
|
|
if ctrl.jump then
|
|
local up = head and liquid.swim or liquid.swim * SURFACE_HOP
|
|
vel.y = math.max(vel.y, up)
|
|
end
|
|
if vel.y < -liquid.sink then
|
|
vel.y = -liquid.sink
|
|
end
|
|
return gravity * liquid.gravity
|
|
end
|
|
|
|
local huds = {} -- player name -> hud id
|
|
|
|
local function update_hud(player, breath)
|
|
local name = player:get_player_name()
|
|
local id = huds[name]
|
|
if breath >= BREATH_MAX then
|
|
if id then
|
|
player:hud_remove(id)
|
|
huds[name] = nil
|
|
end
|
|
return
|
|
end
|
|
local number = math.ceil(breath) * 2
|
|
if id then
|
|
player:hud_change(id, "number", number)
|
|
else
|
|
huds[name] = player:hud_add({
|
|
type = "statbar",
|
|
position = {x = 0.5, y = 1},
|
|
text = BUBBLE,
|
|
number = number,
|
|
item = BREATH_MAX * 2,
|
|
direction = 0,
|
|
size = {x = 24, y = 24},
|
|
offset = HUD_OFFSET,
|
|
})
|
|
end
|
|
end
|
|
|
|
core.register_on_leaveplayer(function(player)
|
|
huds[player:get_player_name()] = nil
|
|
end)
|
|
|
|
--- Breath and liquid damage for one avatar step. Keeps state on the
|
|
-- avatar's luaentity (self). Returns the damage dealt this step (also
|
|
-- without a player, so the selftest can check it).
|
|
function strata.avatar_liquid_step(self, pos, dtime, player)
|
|
local body = strata.body_liquid(pos)
|
|
local head = strata.head_liquid(pos)
|
|
self._breath = self._breath or BREATH_MAX
|
|
self._breath_t = (self._breath_t or 0) + dtime
|
|
if head and head.drowning then
|
|
if self._breath_t >= BREATH_LOSS then
|
|
self._breath_t = 0
|
|
self._breath = math.max(0, self._breath - 1)
|
|
end
|
|
elseif self._breath < BREATH_MAX then
|
|
if self._breath_t >= BREATH_GAIN then
|
|
self._breath_t = 0
|
|
self._breath = self._breath + 1
|
|
end
|
|
else
|
|
self._breath_t = 0
|
|
end
|
|
|
|
local dps, cause = 0, nil
|
|
if body and body.damage > 0 then
|
|
dps, cause = body.damage, body.name
|
|
end
|
|
if self._breath <= 0 and head and head.drowning then
|
|
dps, cause = dps + DROWN_DAMAGE, cause or "drown"
|
|
end
|
|
self._hurt_t = math.max(0, (self._hurt_t or 0) - dtime)
|
|
local dmg = 0
|
|
if dps > 0 and self._hurt_t <= 0 then
|
|
self._hurt_t = HURT_INTERVAL
|
|
dmg = math.ceil(dps * HURT_INTERVAL)
|
|
end
|
|
|
|
if player then
|
|
if dmg > 0 and player:get_hp() > 0 then
|
|
player:set_hp(player:get_hp() - dmg, {type = "set_hp", strata_cause = cause})
|
|
end
|
|
update_hud(player, self._breath)
|
|
end
|
|
return dmg
|
|
end
|