minetest-mods/xdecor/src/mechanisms.lua

151 lines
4.2 KiB
Lua
Raw Normal View History

2019-12-14 17:47:31 +01:00
-- Thanks to sofar for helping with that code.
minetest.setting_set("nodetimer_interval", 0.1)
local plate = {}
screwdriver = screwdriver or {}
2021-05-15 22:34:12 +02:00
local S = minetest.get_translator("xdecor")
2019-12-14 17:47:31 +01:00
local function door_toggle(pos_actuator, pos_door, player)
local player_name = player:get_player_name()
local actuator = minetest.get_node(pos_actuator)
local door = doors.get(pos_door)
2021-05-15 22:34:12 +02:00
if not door then return end
2019-12-14 17:47:31 +01:00
if actuator.name:sub(-4) == "_off" then
minetest.set_node(pos_actuator,
2021-05-15 22:34:12 +02:00
{name = actuator.name:gsub("_off", "_on"), param2 = actuator.param2})
2019-12-14 17:47:31 +01:00
end
door:open(player)
minetest.after(2, function()
if minetest.get_node(pos_actuator).name:sub(-3) == "_on" then
minetest.set_node(pos_actuator,
2021-05-15 22:34:12 +02:00
{name = actuator.name, param2 = actuator.param2})
2019-12-14 17:47:31 +01:00
end
-- Re-get player object (or nil) because 'player' could
-- be an invalid object at this time (player left)
door:close(minetest.get_player_by_name(player_name))
end)
end
function plate.construct(pos)
local timer = minetest.get_node_timer(pos)
timer:start(0.1)
end
function plate.timer(pos)
local objs = minetest.get_objects_inside_radius(pos, 0.8)
if not next(objs) or not doors.get then return true end
2021-05-15 22:34:12 +02:00
local minp = {x = pos.x - 2, y = pos.y, z = pos.z - 2}
local maxp = {x = pos.x + 2, y = pos.y, z = pos.z + 2}
2019-12-14 17:47:31 +01:00
local doors = minetest.find_nodes_in_area(minp, maxp, "group:door")
for _, player in pairs(objs) do
if player:is_player() then
2021-05-15 22:34:12 +02:00
for i = 1, #doors do
2019-12-14 17:47:31 +01:00
door_toggle(pos, doors[i], player)
end
break
end
end
2021-05-15 22:34:12 +02:00
2019-12-14 17:47:31 +01:00
return true
end
function plate.register(material, desc, def)
2021-05-15 22:34:12 +02:00
xdecor.register("pressure_" .. material .. "_off", {
description = def.description or (desc .. " Pressure Plate"),
tiles = {"xdecor_pressure_" .. material .. ".png"},
2019-12-14 17:47:31 +01:00
drawtype = "nodebox",
node_box = xdecor.pixelbox(16, {{1, 0, 1, 14, 1, 14}}),
groups = def.groups,
sounds = def.sounds,
sunlight_propagates = true,
on_rotate = screwdriver.rotate_simple,
on_construct = plate.construct,
on_timer = plate.timer
})
2021-05-15 22:34:12 +02:00
xdecor.register("pressure_" .. material .. "_on", {
tiles = {"xdecor_pressure_" .. material .. ".png"},
2019-12-14 17:47:31 +01:00
drawtype = "nodebox",
node_box = xdecor.pixelbox(16, {{1, 0, 1, 14, 0.4, 14}}),
groups = def.groups,
sounds = def.sounds,
2021-05-15 22:34:12 +02:00
drop = "xdecor:pressure_" .. material .. "_off",
2019-12-14 17:47:31 +01:00
sunlight_propagates = true,
on_rotate = screwdriver.rotate_simple
})
end
plate.register("wood", "Wooden", {
sounds = default.node_sound_wood_defaults(),
2021-05-15 22:34:12 +02:00
groups = {choppy = 3, oddly_breakable_by_hand = 2, flammable = 2},
description = S("Wooden Pressure Plate"),
2019-12-14 17:47:31 +01:00
})
plate.register("stone", "Stone", {
sounds = default.node_sound_stone_defaults(),
2021-05-15 22:34:12 +02:00
groups = {cracky = 3, oddly_breakable_by_hand = 2},
description = S("Stone Pressure Plate"),
2019-12-14 17:47:31 +01:00
})
xdecor.register("lever_off", {
2021-05-15 22:34:12 +02:00
description = S("Lever"),
2019-12-14 17:47:31 +01:00
tiles = {"xdecor_lever_off.png"},
drawtype = "nodebox",
node_box = xdecor.pixelbox(16, {{2, 1, 15, 12, 14, 1}}),
2021-05-15 22:34:12 +02:00
groups = {cracky = 3, oddly_breakable_by_hand = 2},
2019-12-14 17:47:31 +01:00
sounds = default.node_sound_stone_defaults(),
sunlight_propagates = true,
on_rotate = screwdriver.rotate_simple,
2021-05-15 22:34:12 +02:00
2019-12-14 17:47:31 +01:00
on_rightclick = function(pos, node, clicker, itemstack)
if not doors.get then return itemstack end
2021-05-15 22:34:12 +02:00
local minp = {x = pos.x - 2, y = pos.y - 1, z = pos.z - 2}
local maxp = {x = pos.x + 2, y = pos.y + 1, z = pos.z + 2}
2019-12-14 17:47:31 +01:00
local doors = minetest.find_nodes_in_area(minp, maxp, "group:door")
2021-05-15 22:34:12 +02:00
for i = 1, #doors do
2019-12-14 17:47:31 +01:00
door_toggle(pos, doors[i], clicker)
end
2021-05-15 22:34:12 +02:00
2019-12-14 17:47:31 +01:00
return itemstack
end
})
xdecor.register("lever_on", {
tiles = {"xdecor_lever_on.png"},
drawtype = "nodebox",
node_box = xdecor.pixelbox(16, {{2, 1, 15, 12, 14, 1}}),
2021-05-15 22:34:12 +02:00
groups = {cracky = 3, oddly_breakable_by_hand = 2, not_in_creative_inventory = 1},
2019-12-14 17:47:31 +01:00
sounds = default.node_sound_stone_defaults(),
sunlight_propagates = true,
on_rotate = screwdriver.rotate_simple,
drop = "xdecor:lever_off"
})
-- Recipes
minetest.register_craft({
output = "xdecor:pressure_stone_off",
type = "shapeless",
recipe = {"group:stone", "group:stone"}
})
minetest.register_craft({
output = "xdecor:pressure_wood_off",
type = "shapeless",
recipe = {"group:wood", "group:wood"}
})
minetest.register_craft({
output = "xdecor:lever_off",
recipe = {
{"group:stick"},
{"group:stone"}
}
2021-05-15 22:34:12 +02:00
})