From b85278fbaab17e80b0a7312991463b703086593d Mon Sep 17 00:00:00 2001 From: Vanessa Dannenberg Date: Sun, 26 May 2019 01:33:22 -0400 Subject: [PATCH] make wall switch work (mesecons) --- homedecor_electrical/depends.txt | 2 + homedecor_electrical/init.lua | 107 +++++++++++++----- .../textures/homedecor_light_switch_front.png | Bin 142 -> 0 bytes .../homedecor_light_switch_front_off.png | Bin 0 -> 284 bytes .../homedecor_light_switch_front_on.png | Bin 0 -> 360 bytes 5 files changed, 83 insertions(+), 26 deletions(-) delete mode 100644 homedecor_electrical/textures/homedecor_light_switch_front.png create mode 100644 homedecor_electrical/textures/homedecor_light_switch_front_off.png create mode 100644 homedecor_electrical/textures/homedecor_light_switch_front_on.png diff --git a/homedecor_electrical/depends.txt b/homedecor_electrical/depends.txt index fb5d601..382015e 100644 --- a/homedecor_electrical/depends.txt +++ b/homedecor_electrical/depends.txt @@ -1,3 +1,5 @@ homedecor_common default basic_materials +mesecons? +mesecons_receiver? diff --git a/homedecor_electrical/init.lua b/homedecor_electrical/init.lua index c135021..e1f4688 100644 --- a/homedecor_electrical/init.lua +++ b/homedecor_electrical/init.lua @@ -1,6 +1,40 @@ local S = homedecor.gettext +function homedecor.toggle_switch(pos, node, clicker, itemstack, pointed_thing) + if minetest.is_protected(pos, clicker:get_player_name()) then + minetest.record_protection_violation(pos, + sender:get_player_name()) + return false + end + local sep = string.find(node.name, "_o", -5) + local onoff = string.sub(node.name, sep + 1) + local newname = string.sub(node.name, 1, sep - 1)..((onoff == "off") and "_on" or "_off") + minetest.swap_node(pos, {name = newname, param2 = node.param2}) + return true +end + +local on_rc +local switch_receptor + +if minetest.get_modpath("mesecons") then + on_rc = function(pos, node, clicker, itemstack, pointed_thing) + local t = homedecor.toggle_switch(pos, node, clicker, itemstack, pointed_thing) + if not t then return end + if string.find(node.name, "_on", -5) then + mesecon.receptor_off(pos, mesecon.rules.buttonlike_get(node)) + else + mesecon.receptor_on(pos, mesecon.rules.buttonlike_get(node)) + end + end + switch_receptor = { + receptor = { + state = mesecon.state[onoff], + rules = mesecon.rules.buttonlike_get + } + } +end + homedecor.register("power_outlet", { description = S("Power Outlet"), tiles = { @@ -28,35 +62,52 @@ homedecor.register("power_outlet", { walkable = false }) -homedecor.register("light_switch", { - description = S("Light switch"), - tiles = { - "homedecor_light_switch_edges.png", - "homedecor_light_switch_edges.png", - "homedecor_light_switch_edges.png", - "homedecor_light_switch_edges.png", - "homedecor_light_switch_back.png", - "homedecor_light_switch_front.png" - }, - inventory_image = "homedecor_light_switch_inv.png", - node_box = { - type = "fixed", - fixed = { +for _, onoff in ipairs ({"on", "off"}) do + + local model = { + { -0.125, -0.5, 0.4375, 0.125, -0.1875, 0.5 }, + { -0.03125, -0.3125, 0.40625, 0.03125, -0.25, 0.5 }, + } + + if onoff == "on" then + model = { { -0.125, -0.5, 0.4375, 0.125, -0.1875, 0.5 }, - { -0.03125, -0.3125, 0.40625, 0.03125, -0.25, 0.5 }, - + { -0.03125, -0.4375, 0.40625, 0.03125, -0.375, 0.5 }, } - }, - selection_box = { - type = "fixed", - fixed = { - { -0.1875, -0.5625, 0.375, 0.1875, -0.1250, 0.5 }, - } - }, - groups = {cracky=3,dig_immediate=2}, - walkable = false -}) + end + homedecor.register("light_switch_"..onoff, { + description = S("Light switch"), + tiles = { + "homedecor_light_switch_edges.png", + "homedecor_light_switch_edges.png", + "homedecor_light_switch_edges.png", + "homedecor_light_switch_edges.png", + "homedecor_light_switch_back.png", + "homedecor_light_switch_front_"..onoff..".png" + }, + inventory_image = "homedecor_light_switch_inv.png", + node_box = { + type = "fixed", + fixed = model + }, + selection_box = { + type = "fixed", + fixed = { + { -0.1875, -0.5625, 0.375, 0.1875, -0.1250, 0.5 }, + } + }, + groups = {cracky=3,dig_immediate=2}, + walkable = false, + drop = { + items = { + {items = {"homedecor:light_switch_off"}, inherit_color = true }, + } + }, + mesecons = switch_receptor, + on_rightclick = on_rc + }) +end homedecor.register("doorbell", { tiles = { "homedecor_doorbell.png" }, @@ -106,3 +157,7 @@ minetest.register_craft( { { "homedecor:light_switch", "basic_materials:energy_crystal_simple", "homedecor:speaker_driver" } }, }) + +-- aliases + +minetest.register_alias("homedecor:light_switch", "homedecor:light_switch_on") diff --git a/homedecor_electrical/textures/homedecor_light_switch_front.png b/homedecor_electrical/textures/homedecor_light_switch_front.png deleted file mode 100644 index f91ab5e3ac2cc963f0380fa6841636ad14e81a9c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 142 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0L3?#3!&-4XSJOMr-uKV`xym9T)hj%ak|Nn2& z^5PUw+}6{@F~p)b`A0vSgd3|HYui=_4S@w5DQly)P6}yU8koU!I!#dMzy+tatrAS! lJ1($@m4dmuwst76Fer9hf0tt7^%ZCkgQu&X%Q~loCIFOtF){!E diff --git a/homedecor_electrical/textures/homedecor_light_switch_front_off.png b/homedecor_electrical/textures/homedecor_light_switch_front_off.png new file mode 100644 index 0000000000000000000000000000000000000000..d198a3be6fae532494c5d011f3e78651135a7139 GIT binary patch literal 284 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1SD0tpLGJM$DS^ZAsP4HUOULuf5Mb9;T(uhKHKtTnU& z8bbgy7TbUtV{VG{5IYu(C}aO(ISmWPt=s=Jmm`YhsN} z;KKNj6QA##&C^!8eDa7#!!@gj^*5Z(IQ*^oa{Z^4|Kctk5y43Xg&;?Iy85}Sb4q9e E0EZlI6#xJL literal 0 HcmV?d00001 diff --git a/homedecor_electrical/textures/homedecor_light_switch_front_on.png b/homedecor_electrical/textures/homedecor_light_switch_front_on.png new file mode 100644 index 0000000000000000000000000000000000000000..2caa42c6ca599c6a4b49693cb80d5ea98eb40c65 GIT binary patch literal 360 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1SD0tpLH@YFzS1{IEG~0dwb0=`>=rk>jU-Y zIhh3=&dTb+C&H}0{Qp0JPt`edq4Slcu`)^OMy~=g76xok)Dqh>yLIV#|8kF3feA&a z9(y#xds+Sg~D%> zJeGeqzQ5|-8a;-t39}bmbT>TWwIKBF?q3!qd<~+}>8p*FvP`jGeR^T>@7d}M>es`b z9@73_I^DwUvATj|*M#Ve>erc8q;KP>?71($k=^G)>9$p;j`|BPpTfXm>*cX3`OEJe z6W8(1u6SVZwf3?Mqr~Fgi!W6#m>kctTtDrz_P&~mRyW_E#qsmv-S-Bmv)_2TZI08M h#)qP%xBi6Af9Duzyy?VeBVY(Jc)I$ztaD0e0s!DWk>>ya literal 0 HcmV?d00001