]> git.lizzy.rs Git - xdecor.git/blob - src/mechanisms.lua
Add settings and compatibility with moreblocks and stairs
[xdecor.git] / src / mechanisms.lua
1 -- Thanks to sofar for helping with that code.
2
3 minetest.setting_set("nodetimer_interval", 0.1)
4
5 local plate = {}
6 screwdriver = screwdriver or {}
7
8 local function door_toggle(pos_actuator, pos_door, player)
9         local actuator = minetest.get_node(pos_actuator)
10         local door = doors.get(pos_door)
11
12         if actuator.name:sub(-4) == "_off" then
13                 minetest.set_node(pos_actuator,
14                         {name=actuator.name:gsub("_off", "_on"), param2=actuator.param2})
15         end
16         door:open(player)
17
18         minetest.after(2, function()
19                 if minetest.get_node(pos_actuator).name:sub(-3) == "_on" then
20                         minetest.set_node(pos_actuator,
21                                 {name=actuator.name, param2=actuator.param2})
22                 end
23                 door:close(player)
24         end)
25 end
26
27 function plate.construct(pos)
28         local timer = minetest.get_node_timer(pos)
29         timer:start(0.1)
30 end
31
32 function plate.timer(pos)
33         local objs = minetest.get_objects_inside_radius(pos, 0.8)
34         if not next(objs) or not doors.get then return true end
35         local minp = {x=pos.x-2, y=pos.y, z=pos.z-2}
36         local maxp = {x=pos.x+2, y=pos.y, z=pos.z+2}
37         local doors = minetest.find_nodes_in_area(minp, maxp, "group:door")
38
39         for _, player in pairs(objs) do
40                 if player:is_player() then
41                         for i=1, #doors do
42                                 door_toggle(pos, doors[i], player)
43                         end
44                         break
45                 end
46         end
47         return true
48 end
49
50 function plate.register(material, desc, def)
51         xdecor.register("pressure_"..material.."_off", {
52                 description = desc.." Pressure Plate",
53                 tiles = {"xdecor_pressure_"..material..".png"},
54                 drawtype = "nodebox",
55                 node_box = xdecor.pixelbox(16, {{1, 0, 1, 14, 1, 14}}),
56                 groups = def.groups,
57                 sounds = def.sounds,
58                 sunlight_propagates = true,
59                 on_rotate = screwdriver.rotate_simple,
60                 on_construct = plate.construct,
61                 on_timer = plate.timer
62         })
63         xdecor.register("pressure_"..material.."_on", {
64                 tiles = {"xdecor_pressure_"..material..".png"},
65                 drawtype = "nodebox",
66                 node_box = xdecor.pixelbox(16, {{1, 0, 1, 14, 0.4, 14}}),
67                 groups = def.groups,
68                 sounds = def.sounds,
69                 drop = "xdecor:pressure_"..material.."_off",
70                 sunlight_propagates = true,
71                 on_rotate = screwdriver.rotate_simple
72         })
73 end
74
75 plate.register("wood", "Wooden", {
76         sounds = default.node_sound_wood_defaults(),
77         groups = {choppy=3, oddly_breakable_by_hand=2, flammable=2}
78 })
79
80 plate.register("stone", "Stone", {
81         sounds = default.node_sound_stone_defaults(),
82         groups = {cracky=3, oddly_breakable_by_hand=2}
83 })
84
85 xdecor.register("lever_off", {
86         description = "Lever",
87         tiles = {"xdecor_lever_off.png"},
88         drawtype = "nodebox",
89         node_box = xdecor.pixelbox(16, {{2, 1, 15, 12, 14, 1}}),
90         groups = {cracky=3, oddly_breakable_by_hand=2},
91         sounds = default.node_sound_stone_defaults(),
92         sunlight_propagates = true,
93         on_rotate = screwdriver.rotate_simple,
94         on_rightclick = function(pos, node, clicker, itemstack)
95                 if not doors.get then return itemstack end
96                 local minp = {x=pos.x-2, y=pos.y-1, z=pos.z-2}
97                 local maxp = {x=pos.x+2, y=pos.y+1, z=pos.z+2}
98                 local doors = minetest.find_nodes_in_area(minp, maxp, "group:door")
99
100                 for i=1, #doors do
101                         door_toggle(pos, doors[i], clicker)
102                 end
103                 return itemstack
104         end
105 })
106
107 xdecor.register("lever_on", {
108         tiles = {"xdecor_lever_on.png"},
109         drawtype = "nodebox",
110         node_box = xdecor.pixelbox(16, {{2, 1, 15, 12, 14, 1}}),
111         groups = {cracky=3, oddly_breakable_by_hand=2, not_in_creative_inventory=1},
112         sounds = default.node_sound_stone_defaults(),
113         sunlight_propagates = true,
114         on_rotate = screwdriver.rotate_simple,
115         drop = "xdecor:lever_off"
116 })
117
118 -- Recipes
119
120 minetest.register_craft({
121         output = "xdecor:pressure_stone_off",
122         type = "shapeless",
123         recipe = {"group:stone", "group:stone"}
124 })
125
126 minetest.register_craft({
127         output = "xdecor:pressure_wood_off",
128         type = "shapeless",
129         recipe = {"group:wood", "group:wood"}
130 })
131
132 minetest.register_craft({
133         output = "xdecor:lever_off",
134         recipe = {
135                 {"group:stick"},
136                 {"group:stone"}
137         }
138 })