MoreMesecons/moremesecons_wireless/init.lua

87 lines
2.5 KiB
Lua
Raw Normal View History

local wireless = {}
2015-09-01 10:51:53 +02:00
local wireless_rids = {}
local register = function(pos)
2015-09-01 10:51:53 +02:00
local RID = vector.get_data_from_pos(wireless_rids, pos.z,pos.y,pos.x)
2015-09-02 12:46:07 +02:00
if not RID then
table.insert(wireless, pos)
2015-09-01 10:51:53 +02:00
vector.set_data_to_pos(wireless_rids, pos.z,pos.y,pos.x, #wireless)
end
end
local wireless_activate = function(pos)
if not minetest.registered_nodes["moremesecons_wireless:wireless"] then return end
local channel_first_wireless = nil
for i = 1, #wireless do
meta = minetest.get_meta(pos)
channel_first_wireless = meta:get_string("channel")
meta = minetest.get_meta(wireless[i])
if wireless[i] ~= pos and meta:get_string("channel") == channel_first_wireless then
mesecon.receptor_on(wireless[i])
end
end
end
local wireless_deactivate = function(pos)
if not minetest.registered_nodes["moremesecons_wireless:wireless"] then return end
local meta = minetest.get_meta(pos)
local channel_first_wireless = nil
for i = 1, #wireless do
meta = minetest.get_meta(pos)
channel_first_wireless = meta:get_string("channel")
meta = minetest.get_meta(wireless[i])
if wireless[i] ~= pos and meta:get_string("channel") == channel_first_wireless then
mesecon.receptor_off(wireless[i])
end
end
end
minetest.register_node("moremesecons_wireless:wireless", {
2015-08-30 11:49:32 +02:00
tiles = {"moremesecons_wireless.png"},
paramtype = "light",
paramtype2 = "facedir",
description = "Wireless",
walkable = true,
2015-08-29 17:55:23 +02:00
groups = {cracky=3},
mesecons = {effector = {
action_on = wireless_activate,
action_off = wireless_deactivate
}},
sounds = default.node_sound_stone_defaults(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", "field[channel;channel;${channel}]")
register(pos)
end,
on_destruct = function(pos)
2015-09-01 10:51:53 +02:00
local RID = vector.get_data_from_pos(wireless_rids, pos.z,pos.y,pos.x)
if RID then
table.remove(wireless, RID)
2015-09-01 10:51:53 +02:00
vector.remove_data_from_pos(wireless_rids, pos.z,pos.y,pos.x)
end
end,
on_receive_fields = function(pos, formname, fields, sender)
local meta = minetest.get_meta(pos)
meta:set_string("channel", fields.channel)
end,
})
minetest.register_craft({
output = "moremesecons_wireless:wireless 2",
recipe = {
{"group:mesecon_conductor_craftable", "", "group:mesecon_conductor_craftable"},
2015-08-30 11:49:32 +02:00
{"", "mesecons_torch:mesecon_torch_on", ""},
{"group:mesecon_conductor_craftable", "", "group:mesecon_conductor_craftable"},
}
})
minetest.register_abm({
nodenames = {"moremesecons_wireless:wireless"},
interval=1,
chance=1,
action = register
})