- New strata_core/feedback.lua: sound sets (stone, dirt, wood, leaves) via strata.node_sounds; nodes without sounds get a set from their dig group after all mods load. Walls get dirt/wood/stone explicitly. - Server-side break dust textured from the dug node, spawned in front of the plane so every player sees it. - Walls, torches and doors play their place sound from their custom on_place (strata.play_place_sound). - Avatar footsteps on two walk frames; on damage the avatar flashes red (strata.flash) and other players hear player_damage / player_falling_damage at it. The engine plays those for the hurt player. - Slimes flash via damage_texture_modifier and squelch when hit or killed. - Sounds are synthesized by the new tools/gen_sounds.py (CC0, no samples), listed in the new CREDITS.md. The swing animation was already done in #15. - 9 selftest cases in tests/feedback.lua. Closes #16 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
224 lines
6.8 KiB
Lua
224 lines
6.8 KiB
Lua
-- Strata placement rules: blocks, walls and torches need support.
|
|
--
|
|
-- * A block placed on the plane by a player needs a supporting block next
|
|
-- to it (left, right, above or below on the plane) or a background wall
|
|
-- directly behind it. No floating blocks in open sky.
|
|
-- * A background wall needs a wall next to it on the wall layer, or a
|
|
-- supporting block on the plane in front of it or next to that cell.
|
|
-- * A torch mounts on the floor, on a block to its left or right, or on a
|
|
-- background wall behind it, in that order of preference. It drops as an
|
|
-- item when whatever holds it is removed.
|
|
--
|
|
-- Only placement by players is checked. Mapgen and mods using set_node are
|
|
-- not restricted, and blocks don't fall when their support is dug.
|
|
|
|
local S = strata.S
|
|
local PLANE_Z = strata.PLANE_Z
|
|
local WALL_Z = strata.WALL_Z
|
|
|
|
local TORCH = "strata_core:torch"
|
|
local TORCH_LEFT = "strata_core:torch_left" -- mounted on the block to its left
|
|
local TORCH_RIGHT = "strata_core:torch_right" -- mounted on the block to its right
|
|
local TORCH_LIGHT = 13
|
|
|
|
-- Neighbours on the same layer: left, right, below, above.
|
|
local SIDES = {
|
|
vector.new(-1, 0, 0), vector.new(1, 0, 0),
|
|
vector.new(0, -1, 0), vector.new(0, 1, 0),
|
|
}
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Support queries
|
|
--------------------------------------------------------------------------
|
|
|
|
--- Whether a node can hold up blocks and torches placed next to it:
|
|
-- anything solid, plus platforms and doors (which aren't walkable when
|
|
-- open or by design).
|
|
function strata.is_support(node_name)
|
|
if node_name == "air" or node_name == "ignore" then
|
|
return false
|
|
end
|
|
local def = core.registered_nodes[node_name]
|
|
if not def then
|
|
return false
|
|
end
|
|
return def.walkable ~= false
|
|
or core.get_item_group(node_name, "platform") > 0
|
|
or core.get_item_group(node_name, "door") > 0
|
|
end
|
|
|
|
local function support_at(pos)
|
|
return strata.is_support(core.get_node(pos).name)
|
|
end
|
|
|
|
local function wall_at(pos)
|
|
return core.get_item_group(core.get_node(pos).name, "wall") > 0
|
|
end
|
|
|
|
--- Whether a background wall sits behind the plane cell at pos.
|
|
function strata.has_wall_behind(pos)
|
|
return wall_at(vector.new(pos.x, pos.y, WALL_Z))
|
|
end
|
|
|
|
--- Whether a block may be placed in the plane cell at pos.
|
|
function strata.has_block_support(pos)
|
|
for _, d in ipairs(SIDES) do
|
|
if support_at(vector.add(pos, d)) then
|
|
return true
|
|
end
|
|
end
|
|
return strata.has_wall_behind(pos)
|
|
end
|
|
|
|
--- Whether a background wall may be placed in the wall-layer cell at pos.
|
|
function strata.has_wall_support(pos)
|
|
local front = vector.new(pos.x, pos.y, PLANE_Z)
|
|
if support_at(front) then
|
|
return true
|
|
end
|
|
for _, d in ipairs(SIDES) do
|
|
if wall_at(vector.add(pos, d)) or support_at(vector.add(front, d)) then
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
--- The torch node that fits the plane cell at pos, or nil if nothing
|
|
-- there can hold a torch.
|
|
function strata.torch_mount(pos)
|
|
if support_at(vector.offset(pos, 0, -1, 0)) then
|
|
return TORCH
|
|
elseif support_at(vector.offset(pos, -1, 0, 0)) then
|
|
return TORCH_LEFT
|
|
elseif support_at(vector.offset(pos, 1, 0, 0)) then
|
|
return TORCH_RIGHT
|
|
elseif strata.has_wall_behind(pos) then
|
|
return TORCH
|
|
end
|
|
end
|
|
|
|
--- Whether the torch at pos is still held by something.
|
|
local function torch_held(pos, name)
|
|
if name == TORCH_LEFT then
|
|
return support_at(vector.offset(pos, -1, 0, 0))
|
|
elseif name == TORCH_RIGHT then
|
|
return support_at(vector.offset(pos, 1, 0, 0))
|
|
end
|
|
return support_at(vector.offset(pos, 0, -1, 0)) or strata.has_wall_behind(pos)
|
|
end
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Enforcing support for blocks placed by players
|
|
--------------------------------------------------------------------------
|
|
|
|
core.register_on_placenode(function(pos, newnode, placer, oldnode)
|
|
if not (placer and placer:is_player()) or pos.z ~= PLANE_Z then
|
|
return
|
|
end
|
|
if not strata.has_block_support(pos) then
|
|
core.set_node(pos, oldnode)
|
|
return true -- don't consume the item
|
|
end
|
|
end)
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Torches
|
|
--------------------------------------------------------------------------
|
|
|
|
local function torch_on_place(itemstack, placer, pointed_thing)
|
|
if pointed_thing.type ~= "node" or not placer or not placer:is_player() then
|
|
return itemstack
|
|
end
|
|
local pos = pointed_thing.above
|
|
if pos.z ~= PLANE_Z then
|
|
return itemstack
|
|
end
|
|
local name = placer:get_player_name()
|
|
local def = core.registered_nodes[core.get_node(pos).name]
|
|
if not (def and def.buildable_to) then
|
|
return itemstack
|
|
end
|
|
if core.is_protected(pos, name) then
|
|
core.record_protection_violation(pos, name)
|
|
return itemstack
|
|
end
|
|
local node = strata.torch_mount(pos)
|
|
if not node then
|
|
return itemstack
|
|
end
|
|
core.set_node(pos, {name = node})
|
|
strata.play_place_sound(pos, node)
|
|
if not core.is_creative_enabled(name) then
|
|
itemstack:take_item()
|
|
end
|
|
return itemstack
|
|
end
|
|
|
|
local torch_common = {
|
|
paramtype = "light",
|
|
sunlight_propagates = true,
|
|
walkable = false,
|
|
light_source = TORCH_LIGHT,
|
|
drop = TORCH,
|
|
}
|
|
|
|
local function register_torch(name, def)
|
|
for k, v in pairs(torch_common) do
|
|
if def[k] == nil then
|
|
def[k] = v
|
|
end
|
|
end
|
|
core.register_node(name, def)
|
|
end
|
|
|
|
register_torch(TORCH, {
|
|
description = S("Torch"),
|
|
drawtype = "plantlike",
|
|
tiles = {"strata_torch.png"},
|
|
inventory_image = "strata_torch.png",
|
|
wield_image = "strata_torch.png",
|
|
selection_box = {type = "fixed", fixed = {-0.15, -0.5, -0.15, 0.15, 0.3, 0.15}},
|
|
node_placement_prediction = "",
|
|
on_place = torch_on_place,
|
|
groups = {dig_immediate = 3, torch = 1},
|
|
})
|
|
|
|
-- Wall-mounted torches are a thin plate on the plane facing the camera,
|
|
-- leaning away from the block that holds them.
|
|
local function register_wall_torch(name, texture, x0, x1)
|
|
register_torch(name, {
|
|
description = S("Torch"),
|
|
drawtype = "nodebox",
|
|
use_texture_alpha = "clip",
|
|
tiles = {texture},
|
|
node_box = {type = "fixed", fixed = {-0.5, -0.5, -1 / 32, 0.5, 0.5, 1 / 32}},
|
|
selection_box = {type = "fixed", fixed = {x0, -0.4, -0.15, x1, 0.4, 0.15}},
|
|
groups = {dig_immediate = 3, torch = 1, not_in_creative_inventory = 1},
|
|
})
|
|
end
|
|
|
|
register_wall_torch(TORCH_LEFT, "strata_torch_wall.png", -0.5, 0.1)
|
|
register_wall_torch(TORCH_RIGHT, "strata_torch_wall.png^[transformFX", -0.1, 0.5)
|
|
|
|
-- When a node is dug, torches it was holding drop as items. A dug block
|
|
-- may hold torches above it and to either side; a dug wall may hold the
|
|
-- torch in front of it.
|
|
core.register_on_dignode(function(pos)
|
|
local cells
|
|
if pos.z == WALL_Z then
|
|
cells = {vector.new(pos.x, pos.y, PLANE_Z)}
|
|
elseif pos.z == PLANE_Z then
|
|
cells = {vector.offset(pos, 0, 1, 0), vector.offset(pos, -1, 0, 0), vector.offset(pos, 1, 0, 0)}
|
|
else
|
|
return
|
|
end
|
|
for _, p in ipairs(cells) do
|
|
local name = core.get_node(p).name
|
|
if core.get_item_group(name, "torch") > 0 and not torch_held(p, name) then
|
|
core.remove_node(p)
|
|
core.add_item(p, TORCH)
|
|
end
|
|
end
|
|
end)
|