Allow mechanisms to be linked, and allow storing links in schematics.

This commit is contained in:
Signal 2025-11-12 00:55:14 -05:00
parent 8f98a7fa2d
commit 82817a1cb4
10 changed files with 438 additions and 104 deletions

View file

@ -25,14 +25,17 @@ minetest.register_entity(":artifact:door", {
return (e._open and vector.new(-0.5, 0.5, 0) or vector.new(0, 0.5, 0.5)):rotate(e.object:get_rotation())
end,
on_activate = function(e, data)
if not minetest.get_node(e.object:get_pos()).name:find "door" then
local node = minetest.get_node(e.object:get_pos())
if not node.name:find "door" then
e.object:remove()
return
end
e.object:set_armor_groups{immortal = 1}
extend(e, minetest.deserialize(data) or {})
if e.type == "iron" and e._locked == nil then
e._locked = true
if e.type == "iron" then
if e._locked == nil then
e._locked = true
end
e.object:set_properties {
textures = {"artifact_door_iron.png"},
}
@ -45,50 +48,69 @@ minetest.register_entity(":artifact:door", {
e._no_interact = true
end
e._name = ""..math.random()
local nm = minetest.get_meta(e.object:get_pos())
if (node.name:find "_open") and not e._open then
e:open(true)
elseif not (node.name:find "_open") and e._open then
e:close(true)
end
doors[e.object:get_pos():round():to_string()] = e
end,
on_deactivate = function(e)
doors[e.object:get_pos():round():to_string()] = nil
end,
on_interact = function(e)
if e._locked then return end
if e._open then
e._open = nil
e._no_interact = true
e._name = ""..math.random()
e.object:set_animation({x=0.5,y=1}, 1.5, 0.1, false)
minetest.after(0.1, function()
local pos = e.object:get_pos():round()
local node = minetest.get_node(pos)
node.name = "door_"..e.type
minetest.swap_node(pos, node)
e.object:set_properties {
selectionbox = artifact.rotate_selectionbox({
-0.5, -0.5, -0.5,
0.5, 1.5, -6/16
}, e.rotation)
}
end)
minetest.after(0.25, function()
e:close()
else
e:open()
end
end,
open = function(e, snap)
if e._open then return end
e._open = true
e._no_interact = true
e._name = ""..math.random()
e.object:set_animation({x=snap and 0.5 or 0,y=0.5}, 1.5, 0.1, false)
minetest.after(snap and 0 or 0.1, function()
local pos = e.object:get_pos():round()
local node = minetest.get_node(pos)
node.name = "door_"..e.type.."_open"
minetest.swap_node(pos, node)
e.object:set_properties {
selectionbox = artifact.rotate_selectionbox({
0.5, -0.5, -0.5,
6/16, 1.5, 0.5
}, e.rotation)
}
end)
if not e._locked then
minetest.after(snap and 0 or 0.25, function()
e._no_interact = nil
end)
else
e._open = true
e._no_interact = true
e._name = ""..math.random()
e.object:set_animation({x=0,y=0.5}, 1.5, 0.1, false)
minetest.after(0.1, function()
local pos = e.object:get_pos():round()
local node = minetest.get_node(pos)
node.name = "door_"..e.type.."_open"
minetest.swap_node(pos, node)
e.object:set_properties {
selectionbox = artifact.rotate_selectionbox({
0.5, -0.5, -0.5,
6/16, 1.5, 0.5
}, e.rotation)
}
end)
minetest.after(0.25, function()
end
end,
close = function(e, snap)
if not e._open then return end
e._open = nil
e._no_interact = true
e._name = ""..math.random()
e.object:set_animation({x=snap and 1 or 0.5,y=1}, 1.5, 0.1, false)
minetest.after(snap and 0 or 0.1, function()
local pos = e.object:get_pos():round()
local node = minetest.get_node(pos)
node.name = "door_"..e.type
minetest.swap_node(pos, node)
e.object:set_properties {
selectionbox = artifact.rotate_selectionbox({
-0.5, -0.5, -0.5,
0.5, 1.5, -6/16
}, e.rotation)
}
end)
if not e._locked then
minetest.after(snap and 0 or 0.25, function()
e._no_interact = nil
end)
end
@ -113,6 +135,43 @@ minetest.register_entity(":artifact:door", {
})
local function register_basic_door(type)
local function onload(pos)
local m = minetest.get_meta(pos)
-- Dynamically initialize doors that were mapgen'd in.
if not m:contains("initialized") then
m:set_string("initialized", "true")
local rot = minetest.facedir_to_dir(minetest.get_node(pos).param2):dir_to_rotation()
rot.y = rot.y -math.pi
minetest.add_entity(pos, "artifact:door", minetest.serialize{type = type}):get_luaentity():rotate(rot)
end
end
local function ondestruct(pos, reason)
if reason == "whack" then
-- TODO: Particles
end
doors[pos:to_string()].object:remove()
doors[pos:to_string()] = nil
end
local function onsignal(pos, event)
local door = doors[vector.to_string(pos)]
if event.type == "on" then
if door then
door:open()
else
local node = minetest.get_node(pos)
node.name = "door_"..e.type.."_open"
minetest.swap_node(pos, node)
end
elseif event.type == "off" then
if door then
door:close()
else
local node = minetest.get_node(pos)
node.name = "door_"..e.type.."_open"
minetest.swap_node(pos, node)
end
end
end
artifact.register_node("door_"..type, {
drawtype = "nodebox",
node_box = {
@ -128,30 +187,10 @@ local function register_basic_door(type)
paramtype = "light",
pointable = false,
groups = {call_on_load = 1, whackable = type == "wood" and 1 or nil},
on_construct = function(pos)
local m = minetest.get_meta(pos)
m:set_string("initialized", "true")
local rot = minetest.facedir_to_dir(minetest.get_node(pos).param2):dir_to_rotation()
rot.y = rot.y -math.pi
minetest.add_entity(pos, "artifact:door", minetest.serialize{type = type}):get_luaentity():rotate(rot)
end,
on_destruct = function(pos, reason)
if reason == "whack" then
-- TODO: Particles
end
doors[pos:to_string()].object:remove()
doors[pos:to_string()] = nil
end,
on_load = function(pos)
local m = minetest.get_meta(pos)
-- Dynamically initialize doors that were mapgen'd in.
if not m:contains("initialized") then
m:set_string("initialized", "true")
local rot = minetest.facedir_to_dir(minetest.get_node(pos).param2):dir_to_rotation()
rot.y = rot.y -math.pi
minetest.add_entity(pos, "artifact:door", minetest.serialize{type = type}):get_luaentity():rotate(rot)
end
end
on_construct = onload,
on_destruct = ondestruct,
on_load = onload,
on_signal = onsignal
})
artifact.register_node("door_"..type.."_open", {
drawtype = "nodebox",
@ -168,27 +207,10 @@ local function register_basic_door(type)
paramtype = "light",
pointable = false,
groups = {call_on_load = 1, whackable = type == "wood" and 1 or nil},
on_construct = function(pos)
local m = minetest.get_meta(pos)
m:set_string("initialized", "true")
local rot = minetest.facedir_to_dir(minetest.get_node(pos).param2):dir_to_rotation()
rot.y = rot.y -math.pi
minetest.add_entity(pos, "artifact:door", minetest.serialize{type = type}):get_luaentity():rotate(rot)
end,
on_destruct = function(pos)
doors[pos:to_string()].object:remove()
doors[pos:to_string()] = nil
end,
on_load = function(pos)
local m = minetest.get_meta(pos)
-- Dynamically initialize doors that were mapgen'd in.
if not m:contains("initialized") then
m:set_string("initialized", "true")
local rot = minetest.facedir_to_dir(minetest.get_node(pos).param2):dir_to_rotation()
rot.y = rot.y -math.pi
minetest.add_entity(pos, "artifact:door", minetest.serialize{type = type}):get_luaentity():rotate(rot)
end
end
on_construct = onload,
on_destruct = ondestruct,
on_load = onload,
on_signal = onsignal
})
end