Files
claudeandClaude Opus 5.5 9e8284b93a
test / test (push) Successful in 55s
Doors open when the avatar walks into them
strata.open_door_ahead (furniture.lua) checks the cells just ahead of a
walking entity, at feet to head height, and opens a closed door there.
The avatar calls it while a walk key is held; creatures don't, so
slimes can't get in. Doors don't close by themselves, as before.

Selftest: an avatar walking right opens a placed door and passes through;
standing still, facing away or being too far doesn't open it.

Closes #67

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

251 lines
8.4 KiB
Lua

-- Strata furniture: doors and storage chests.
local S = strata.S
local PLANE_Z = strata.PLANE_Z
local DOOR_HEIGHT = 3
local DOOR_THICKNESS = 0.15 -- half-depth (z) of a door panel
local DOOR_OPEN_WIDTH = 0.1875 -- width (x) of a swung-open door panel
local DOOR_REACH_AHEAD = 0.2 -- an avatar this close to a closed door opens it
local CHEST_SIZE = 40
local CHEST_COLS = 10
-- Within reach of the player (strata_player's is_protected override).
local function can_reach(pos, player)
return player and player:is_player() and not core.is_protected(pos, player:get_player_name())
end
--------------------------------------------------------------------------
-- Doors: 1 wide, DOOR_HEIGHT tall. Each cell is its own node, named
-- <door>_<state>_<segment>, so any cell can be right-clicked or dug.
--------------------------------------------------------------------------
strata.doors = {} -- node name -> {door = item name, open = bool, seg = 0-based}
local function door_node(door, open, seg)
return ("%s_%s_%d"):format(door, open and "open" or "closed", seg)
end
--- Bottom cell of the door that the node at pos belongs to, plus its info.
local function door_base(pos)
local info = strata.doors[core.get_node(pos).name]
if not info then
return nil
end
return vector.new(pos.x, pos.y - info.seg, pos.z), info
end
--- Open or close the door at pos (any of its cells). Returns true if it
-- changed. A door won't close on an avatar or creature.
function strata.set_door(pos, open)
local base, info = door_base(pos)
if not base or info.open == open then
return false
end
if not open then
for seg = 0, DOOR_HEIGHT - 1 do
if #strata.objects_in_cell(vector.offset(base, 0, seg, 0)) > 0 then
return false
end
end
end
for seg = 0, DOOR_HEIGHT - 1 do
local p = vector.offset(base, 0, seg, 0)
local i = strata.doors[core.get_node(p).name]
if i and i.door == info.door then
core.swap_node(p, {name = door_node(info.door, open, seg)})
end
end
return true
end
--- Open a closed door that an entity at pos, walking in direction dir
-- (-1 or 1), is about to bump into. half_width and half_height describe its
-- collision box. Returns true if a door opened. Only avatars call this;
-- creatures must not open doors.
function strata.open_door_ahead(pos, dir, half_width, half_height)
if dir == 0 then
return false
end
local x = math.floor(pos.x + dir * (half_width + DOOR_REACH_AHEAD) + 0.5)
local y0 = math.floor(pos.y - half_height + 0.5 + 0.01)
local y1 = math.floor(pos.y + half_height - 0.01 + 0.5)
for y = y0, y1 do
local p = vector.new(x, y, PLANE_Z)
local info = strata.doors[core.get_node(p).name]
if info and not info.open then
return strata.set_door(p, true)
end
end
return false
end
function strata.toggle_door(pos)
local _, info = door_base(pos)
return info and strata.set_door(pos, not info.open) or false
end
function strata.is_door_open(pos)
local _, info = door_base(pos)
return info and info.open
end
local function buildable(pos)
local def = core.registered_nodes[core.get_node(pos).name]
return def and def.buildable_to
end
local function door_on_place(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" or not placer or not placer:is_player() then
return itemstack
end
local name = placer:get_player_name()
local base = pointed_thing.above
if base.z ~= PLANE_Z then
return itemstack
end
local below = core.registered_nodes[core.get_node(vector.offset(base, 0, -1, 0)).name]
if not (below and below.walkable) then
return itemstack -- doors stand on solid ground
end
for seg = 0, DOOR_HEIGHT - 1 do
local p = vector.offset(base, 0, seg, 0)
if not buildable(p) or core.is_protected(p, name) or #strata.objects_in_cell(p) > 0 then
return itemstack
end
end
local door = itemstack:get_name()
for seg = 0, DOOR_HEIGHT - 1 do
core.set_node(vector.offset(base, 0, seg, 0), {name = door_node(door, false, seg)})
end
strata.play_place_sound(base, door_node(door, false, 0))
if not core.is_creative_enabled(name) then
itemstack:take_item()
end
return itemstack
end
--- Register a door item and its cell nodes.
-- def.description, def.inventory_image, def.textures = {bottom, middle, top},
-- def.groups (dig groups for the cells).
function strata.register_door(name, def)
assert(#def.textures == DOOR_HEIGHT, "door needs one texture per cell")
core.register_craftitem(name, {
description = def.description,
inventory_image = def.inventory_image,
wield_image = def.inventory_image,
stack_max = 99,
node_placement_prediction = "",
on_place = door_on_place,
})
for _, open in ipairs({false, true}) do
for seg = 0, DOOR_HEIGHT - 1 do
local node = door_node(name, open, seg)
strata.doors[node] = {door = name, open = open, seg = seg}
local groups = table.copy(def.groups)
groups.door = 1
groups.not_in_creative_inventory = 1
local box = open
and {-0.5, -0.5, -DOOR_THICKNESS, -0.5 + DOOR_OPEN_WIDTH, 0.5, DOOR_THICKNESS}
or {-0.5, -0.5, -DOOR_THICKNESS, 0.5, 0.5, DOOR_THICKNESS}
core.register_node(node, {
description = def.description,
tiles = {def.textures[seg + 1]},
use_texture_alpha = "clip",
drawtype = "nodebox",
paramtype = "light",
sunlight_propagates = open,
walkable = not open,
node_box = {type = "fixed", fixed = box},
selection_box = {type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5}},
groups = groups,
drop = name,
on_rightclick = function(pos, _, clicker, itemstack)
if can_reach(pos, clicker) then
strata.toggle_door(pos)
end
return itemstack
end,
after_dig_node = function(pos, oldnode)
local info = strata.doors[oldnode.name]
local base = vector.new(pos.x, pos.y - info.seg, pos.z)
for s = 0, DOOR_HEIGHT - 1 do
local p = vector.offset(base, 0, s, 0)
local i = strata.doors[core.get_node(p).name]
if s ~= info.seg and i and i.door == info.door then
core.remove_node(p)
end
end
end,
})
end
end
end
strata.register_door("strata_core:door_wood", {
description = S("Wooden Door"),
inventory_image = "strata_door_wood_item.png",
textures = {"strata_door_wood_bottom.png", "strata_door_wood_middle.png", "strata_door_wood_top.png"},
groups = {choppy = 3},
})
--------------------------------------------------------------------------
-- Storage chests
--------------------------------------------------------------------------
local function chest_formspec(pos, title)
local spos = ("%d,%d,%d"):format(pos.x, pos.y, pos.z)
local rows = math.ceil(CHEST_SIZE / CHEST_COLS)
local inv_y = 0.9 + rows * 1.25 + 0.6
return table.concat({
"formspec_version[6]",
("size[13.25,%.2f]"):format(inv_y + 5.4),
"label[0.5,0.5;" .. core.formspec_escape(title) .. "]",
("list[nodemeta:%s;main;0.5,0.9;%d,%d;]"):format(spos, CHEST_COLS, rows),
("label[0.5,%.2f;%s]"):format(inv_y - 0.3, core.formspec_escape(S("Inventory"))),
("list[current_player;main;0.5,%.2f;10,4;]"):format(inv_y),
("listring[nodemeta:%s;main]"):format(spos),
"listring[current_player;main]",
})
end
strata.chest_formspec = chest_formspec
--- Register a storage chest node.
function strata.register_chest(name, def)
core.register_node(name, {
description = def.description,
tiles = def.tiles, -- the 6th (front, -z) faces the camera
groups = def.groups,
is_ground_content = false,
on_construct = function(pos)
core.get_meta(pos):get_inventory():set_size("main", CHEST_SIZE)
end,
can_dig = function(pos)
return core.get_meta(pos):get_inventory():is_empty("main")
end,
on_rightclick = function(pos, _, clicker, itemstack)
if can_reach(pos, clicker) then
core.show_formspec(clicker:get_player_name(), "strata_core:chest",
chest_formspec(pos, def.description))
end
return itemstack
end,
allow_metadata_inventory_put = function(pos, _, _, stack, player)
return can_reach(pos, player) and stack:get_count() or 0
end,
allow_metadata_inventory_take = function(pos, _, _, stack, player)
return can_reach(pos, player) and stack:get_count() or 0
end,
allow_metadata_inventory_move = function(pos, _, _, _, _, count, player)
return can_reach(pos, player) and count or 0
end,
})
end
strata.register_chest("strata_core:chest_wood", {
description = S("Wooden Chest"),
tiles = {"strata_chest_top.png", "strata_chest_top.png", "strata_chest_side.png",
"strata_chest_side.png", "strata_chest_side.png", "strata_chest_front.png"},
groups = {choppy = 3, chest = 1},
})