xdecor-libre/src/mechanisms.lua

161 lines
4.6 KiB
Lua
Raw Normal View History

-- Thanks to sofar for helping with that code.
2016-02-27 22:54:38 +03:00
local plate = {}
screwdriver = screwdriver or {}
2016-02-27 22:54:38 +03:00
local S = minetest.get_translator("xdecor")
2022-02-26 14:03:20 +03:00
local ALPHA_OPAQUE = minetest.features.use_texture_alpha_string_modes and "opaque" or false
2016-02-28 13:38:57 +03:00
local function door_toggle(pos_actuator, pos_door, player)
local player_name = player:get_player_name()
2016-02-28 13:38:57 +03:00
local actuator = minetest.get_node(pos_actuator)
2016-02-27 22:54:38 +03:00
local door = doors.get(pos_door)
if not door then return end
if actuator.name:sub(-4) == "_off" then
minetest.set_node(pos_actuator,
2019-07-23 15:03:20 +03:00
{name = actuator.name:gsub("_off", "_on"), param2 = actuator.param2})
end
2018-09-17 20:57:22 +03:00
door:open(player)
2016-02-27 22:54:38 +03:00
minetest.after(2, function()
if minetest.get_node(pos_actuator).name:sub(-3) == "_on" then
minetest.set_node(pos_actuator,
2019-07-23 15:03:20 +03:00
{name = actuator.name, param2 = actuator.param2})
end
2018-09-17 20:57:22 +03:00
-- 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))
2016-02-27 22:54:38 +03:00
end)
end
2016-02-28 13:38:57 +03:00
function plate.construct(pos)
local timer = minetest.get_node_timer(pos)
timer:start(0.1)
2016-02-28 13:38:57 +03:00
end
2016-02-27 22:54:38 +03:00
function plate.timer(pos)
local objs = minetest.get_objects_inside_radius(pos, 0.8)
2016-12-30 17:11:37 +03:00
if not next(objs) or not doors.get then return true end
2019-07-23 15:03:20 +03: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}
2016-02-27 22:54:38 +03:00
local doors = minetest.find_nodes_in_area(minp, maxp, "group:door")
for _, player in pairs(objs) do
if player:is_player() then
2019-07-23 15:03:20 +03:00
for i = 1, #doors do
2016-02-28 13:38:57 +03:00
door_toggle(pos, doors[i], player)
2016-02-27 22:54:38 +03:00
end
break
2016-02-27 22:54:38 +03:00
end
end
2019-07-23 15:03:20 +03:00
2016-02-27 22:54:38 +03:00
return true
end
2016-03-09 00:10:57 +03:00
function plate.register(material, desc, def)
2019-07-23 15:03:20 +03:00
xdecor.register("pressure_" .. material .. "_off", {
description = def.description or (desc .. " Pressure Plate"),
2023-07-10 20:52:38 +03:00
_tt_help = S("Opens doors when stepped on"),
2019-07-23 15:03:20 +03:00
tiles = {"xdecor_pressure_" .. material .. ".png"},
2022-02-26 14:03:20 +03:00
use_texture_alpha = ALPHA_OPAQUE,
2016-02-27 22:54:38 +03:00
drawtype = "nodebox",
node_box = xdecor.pixelbox(16, {{1, 0, 1, 14, 1, 14}}),
2016-03-09 14:36:32 +03:00
groups = def.groups,
is_ground_content = false,
2016-03-09 14:36:32 +03:00
sounds = def.sounds,
2016-02-27 22:54:38 +03:00
sunlight_propagates = true,
on_rotate = screwdriver.rotate_simple,
2016-02-27 22:54:38 +03:00
on_construct = plate.construct,
on_timer = plate.timer
})
2019-07-23 15:03:20 +03:00
xdecor.register("pressure_" .. material .. "_on", {
tiles = {"xdecor_pressure_" .. material .. ".png"},
2022-02-26 14:03:20 +03:00
use_texture_alpha = ALPHA_OPAQUE,
2016-02-27 22:54:38 +03:00
drawtype = "nodebox",
node_box = xdecor.pixelbox(16, {{1, 0, 1, 14, 0.4, 14}}),
2016-03-09 14:36:32 +03:00
groups = def.groups,
is_ground_content = false,
2016-03-09 14:36:32 +03:00
sounds = def.sounds,
2019-07-23 15:03:20 +03:00
drop = "xdecor:pressure_" .. material .. "_off",
sunlight_propagates = true,
on_rotate = screwdriver.rotate_simple
2016-02-27 22:54:38 +03:00
})
end
2016-02-28 13:38:57 +03:00
2016-03-09 00:10:57 +03:00
plate.register("wood", "Wooden", {
sounds = default.node_sound_wood_defaults(),
groups = {choppy = 3, oddly_breakable_by_hand = 2, flammable = 2},
description = S("Wooden Pressure Plate"),
2016-03-09 00:10:57 +03:00
})
plate.register("stone", "Stone", {
sounds = default.node_sound_stone_defaults(),
groups = {cracky = 3, oddly_breakable_by_hand = 2},
description = S("Stone Pressure Plate"),
2016-03-09 00:10:57 +03:00
})
2016-02-28 18:32:12 +03:00
xdecor.register("lever_off", {
description = S("Lever"),
2023-07-10 20:52:38 +03:00
_tt_help = S("Opens doors when pulled"),
2016-02-28 18:32:12 +03:00
tiles = {"xdecor_lever_off.png"},
2022-02-26 14:03:20 +03:00
use_texture_alpha = ALPHA_OPAQUE,
2016-02-28 13:38:57 +03:00
drawtype = "nodebox",
node_box = xdecor.pixelbox(16, {{2, 1, 15, 12, 14, 1}}),
2019-07-23 15:03:20 +03:00
groups = {cracky = 3, oddly_breakable_by_hand = 2},
is_ground_content = false,
2016-02-28 13:38:57 +03:00
sounds = default.node_sound_stone_defaults(),
sunlight_propagates = true,
on_rotate = screwdriver.rotate_simple,
2019-07-23 15:03:20 +03:00
2016-12-30 17:11:37 +03:00
on_rightclick = function(pos, node, clicker, itemstack)
if not doors.get then return itemstack end
2019-07-23 15:03:20 +03: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}
2016-02-28 13:38:57 +03:00
local doors = minetest.find_nodes_in_area(minp, maxp, "group:door")
2019-07-23 15:03:20 +03:00
for i = 1, #doors do
2016-02-28 13:38:57 +03:00
door_toggle(pos, doors[i], clicker)
end
2019-07-23 15:03:20 +03:00
return itemstack
2023-06-30 15:48:20 +03:00
end,
_xdecor_itemframe_offset = -3.5,
2016-02-28 13:38:57 +03:00
})
2016-02-28 18:32:12 +03:00
xdecor.register("lever_on", {
tiles = {"xdecor_lever_on.png"},
2022-02-26 14:03:20 +03:00
use_texture_alpha = ALPHA_OPAQUE,
2016-02-28 13:38:57 +03:00
drawtype = "nodebox",
node_box = xdecor.pixelbox(16, {{2, 1, 15, 12, 14, 1}}),
2019-07-23 15:03:20 +03:00
groups = {cracky = 3, oddly_breakable_by_hand = 2, not_in_creative_inventory = 1},
is_ground_content = false,
2016-02-28 13:38:57 +03:00
sounds = default.node_sound_stone_defaults(),
sunlight_propagates = true,
on_rotate = screwdriver.rotate_simple,
2016-02-28 18:32:12 +03:00
drop = "xdecor:lever_off"
2016-02-28 13:38:57 +03:00
})
-- 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"}
}
2019-07-23 15:03:20 +03:00
})