summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAuke Kok <sofar@foo-projects.org>2016-01-24 15:34:14 -0800
committerJeija <norrepli@gmail.com>2016-02-14 11:30:52 +0100
commitfb4c440265a6e26740cb6a97844b98361596c634 (patch)
treebd46f5a3b451cb3ebdbedc740b24b30f70a09f52
parentf56c4ce35cc4fd4f6eace5d4345400283e1536a3 (diff)
downloadmesecons-fb4c440265a6e26740cb6a97844b98361596c634.tar
mesecons-fb4c440265a6e26740cb6a97844b98361596c634.tar.gz
mesecons-fb4c440265a6e26740cb6a97844b98361596c634.tar.bz2
mesecons-fb4c440265a6e26740cb6a97844b98361596c634.tar.xz
mesecons-fb4c440265a6e26740cb6a97844b98361596c634.zip
Doors: Use new mesh door API if available
I've implemented a new door API in minetest_game that performs all the needed things to assure doors are properly openend and closed, without mods needing to know the inner details of what needs to be done. Mesecons can just fetch a reference to the door object and call the appropriate open or close method, which simplifies this code a lot. For compatibility, this code retains the old code path and tests whether the new API is available, so this code remains functional if the new API is not available. Since the metal trapdoor was only recently added, I added it to the new API codepath only, it's unlikely to be present on older versions of minetest_game anyway. As a benefit from the new door API, there is now absolutely no more accidental switching closed trapdoors to open on power off, which could happen with the old method - since that was just a simple toggle.
-rw-r--r--mesecons_doors/init.lua111
1 files changed, 78 insertions, 33 deletions
diff --git a/mesecons_doors/init.lua b/mesecons_doors/init.lua
index 7346128..52d6c17 100644
--- a/mesecons_doors/init.lua
+++ b/mesecons_doors/init.lua
@@ -20,31 +20,53 @@ local function on_rightclick(pos, dir, check_name, replace, replace_dir, params)
end
local function meseconify_door(name)
- if not minetest.registered_items[name] then return end
+ if minetest.registered_items[name .. "_b_1"] then
+ -- old style double-node doors
+ local function toggle_state1 (pos, node)
+ on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
+ end
- local function toggle_state1 (pos, node)
- on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
- end
-
- local function toggle_state2 (pos, node)
- on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
- end
+ local function toggle_state2 (pos, node)
+ on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
+ end
- minetest.override_item(name.."_b_1", {
- mesecons = {effector = {
- action_on = toggle_state1,
- action_off = toggle_state1,
- rules = mesecon.rules.pplate
- }},
- })
+ minetest.override_item(name.."_b_1", {
+ mesecons = {effector = {
+ action_on = toggle_state1,
+ action_off = toggle_state1,
+ rules = mesecon.rules.pplate
+ }}
+ })
- minetest.override_item(name.."_b_2", {
- mesecons = {effector = {
- action_on = toggle_state2,
- action_off = toggle_state2,
- rules = mesecon.rules.pplate
- }},
- })
+ minetest.override_item(name.."_b_2", {
+ mesecons = {effector = {
+ action_on = toggle_state2,
+ action_off = toggle_state2,
+ rules = mesecon.rules.pplate
+ }}
+ })
+ elseif minetest.registered_items[name .. "_a"] then
+ -- new style mesh node based doors
+ local override = {
+ mesecons = {effector = {
+ action_on = function(pos, node)
+ local door = doors.get(pos)
+ if door then
+ door:open()
+ end
+ end,
+ action_off = function(pos, node)
+ local door = doors.get(pos)
+ if door then
+ door:close()
+ end
+ end,
+ rules = mesecon.rules.pplate
+ }}
+ }
+ minetest.override_item(name .. "_a", override)
+ minetest.override_item(name .. "_b", override)
+ end
end
meseconify_door("doors:door_wood")
@@ -67,18 +89,41 @@ local function trapdoor_switch(pos, node)
minetest.get_meta(pos):set_int("state", state == 1 and 0 or 1)
end
-if minetest.registered_nodes["doors:trapdoor"] then
- minetest.override_item("doors:trapdoor", {
+if doors and doors.get then
+ local override = {
mesecons = {effector = {
- action_on = trapdoor_switch,
- action_off = trapdoor_switch
+ action_on = function(pos, node)
+ local door = doors.get(pos)
+ if door then
+ door:open()
+ end
+ end,
+ action_off = function(pos, node)
+ local door = doors.get(pos)
+ if door then
+ door:close()
+ end
+ end,
}},
- })
+ }
+ minetest.override_item("doors:trapdoor", override)
+ minetest.override_item("doors:trapdoor_open", override)
+ minetest.override_item("doors:trapdoor_steel", override)
+ minetest.override_item("doors:trapdoor_steel_open", override)
+else
+ if minetest.registered_nodes["doors:trapdoor"] then
+ minetest.override_item("doors:trapdoor", {
+ mesecons = {effector = {
+ action_on = trapdoor_switch,
+ action_off = trapdoor_switch
+ }},
+ })
- minetest.override_item("doors:trapdoor_open", {
- mesecons = {effector = {
- action_on = trapdoor_switch,
- action_off = trapdoor_switch
- }},
- })
+ minetest.override_item("doors:trapdoor_open", {
+ mesecons = {effector = {
+ action_on = trapdoor_switch,
+ action_off = trapdoor_switch
+ }},
+ })
+ end
end