117 lines
5.6 KiB
Lua
117 lines
5.6 KiB
Lua
-- 3. PROGRAMMER
|
|
minetest.register_tool("smart_light:programmer", {
|
|
description = "Smart Light Programmer",
|
|
inventory_image = "smart_light_programmer.png",
|
|
|
|
on_use = function(itemstack, user, pointed_thing)
|
|
if pointed_thing.type ~= "node" then return end
|
|
local pos = pointed_thing.under
|
|
local name = user:get_player_name()
|
|
local node = minetest.get_node(pos)
|
|
|
|
local t_meta = itemstack:get_meta()
|
|
local area = t_meta:get_string("last_area")
|
|
local chan = t_meta:get_string("last_channel")
|
|
|
|
if area == "" or chan == "" then
|
|
minetest.chat_send_player(name, "Programmer leer! Nutze Rechtsklick zum Einstellen.")
|
|
return
|
|
end
|
|
|
|
local pos_str = minetest.pos_to_string(pos)
|
|
|
|
-- LAMPE PASTEN
|
|
local original = smart_light.shadow_to_original[node.name] or node.name
|
|
if smart_light.original_to_shadows[original] then
|
|
smart_light.registry.areas[area].channels[chan] = smart_light.registry.areas[area].channels[chan] or { nodes = {}, last_level = "max" }
|
|
smart_light.registry.areas[area].channels[chan].nodes[pos_str] = true
|
|
smart_light.save()
|
|
local n_meta = minetest.get_meta(pos)
|
|
n_meta:set_string("sl_area", area)
|
|
n_meta:set_string("sl_chan", chan)
|
|
minetest.chat_send_player(name, "PASTE (Lampe): " .. area .. ":" .. chan)
|
|
return itemstack
|
|
end
|
|
|
|
-- SCHALTER PASTEN
|
|
if node.name:sub(1, 18) == "smart_light:switch" then
|
|
if not smart_light.registry.areas[area] or not smart_light.registry.areas[area].channels[chan] then
|
|
minetest.chat_send_player(name, "FEHLER: Ziel-Kanal existiert nicht!")
|
|
return
|
|
end
|
|
smart_light.registry.areas[area].channels[chan].switches = smart_light.registry.areas[area].channels[chan].switches or {}
|
|
smart_light.registry.areas[area].channels[chan].switches[pos_str] = true
|
|
smart_light.save()
|
|
local n_meta = minetest.get_meta(pos)
|
|
n_meta:set_string("sl_area", area)
|
|
n_meta:set_string("sl_chan", chan)
|
|
n_meta:set_string("infotext", "Schalter: " .. area .. ":" .. chan)
|
|
smart_light.update_switch_visuals(area, chan, "on")
|
|
minetest.chat_send_player(name, "PASTE (Schalter): " .. area .. ":" .. chan)
|
|
return itemstack
|
|
end
|
|
end,
|
|
|
|
on_place = function(itemstack, user, pointed_thing)
|
|
if pointed_thing.type ~= "node" then return end
|
|
local pos = pointed_thing.under
|
|
local name = user:get_player_name()
|
|
local node = minetest.get_node(pos)
|
|
|
|
-- ADMIN PANEL
|
|
if node.name == "smart_light:controller" then
|
|
local area = minetest.get_meta(pos):get_string("area_id")
|
|
minetest.show_formspec(name, "smart_light:admin_" .. minetest.pos_to_string(pos), smart_light.get_admin_fs(area))
|
|
return itemstack
|
|
end
|
|
|
|
-- SCHALTER SETUP (Beide Dropdowns mit Platzhalter)
|
|
if node.name:sub(1, 18) == "smart_light:switch" then
|
|
local n_meta = minetest.get_meta(pos)
|
|
local curr_area = n_meta:get_string("sl_area")
|
|
|
|
local areas_table = {"-- Bitte wählen --"}
|
|
for a_id, _ in pairs(smart_light.registry.areas) do table.insert(areas_table, a_id) end
|
|
|
|
local area_idx = 1
|
|
for i, v in ipairs(areas_table) do if v == curr_area then area_idx = i end end
|
|
|
|
local channels_table = {"-- Bitte wählen --"}
|
|
if curr_area ~= "" and curr_area ~= "-- Bitte wählen --" and smart_light.registry.areas[curr_area] then
|
|
local tmp = {}
|
|
for c_id, _ in pairs(smart_light.registry.areas[curr_area].channels) do table.insert(tmp, c_id) end
|
|
table.sort(tmp)
|
|
for _, c in ipairs(tmp) do table.insert(channels_table, c) end
|
|
end
|
|
|
|
local fs = "size[5,4.5]label[0.5,0.2;Schalter Programmierung]" ..
|
|
"dropdown[0.5,1.2;4,1;area_sel;" .. table.concat(areas_table, ",") .. ";" .. area_idx .. "]" ..
|
|
"label[0.5,2.2;Kanal auswählen:]" ..
|
|
"dropdown[0.5,2.7;4,1;chan_sel;" .. table.concat(channels_table, ",") .. ";1]" ..
|
|
"button[0.5,3.8;1.5,1;cancel;Abbruch]" ..
|
|
"button_exit[2.5,3.8;2,1;save_switch;Speichern]"
|
|
|
|
minetest.show_formspec(name, "smart_light:prog_sw_" .. minetest.pos_to_string(pos), fs)
|
|
return itemstack
|
|
end
|
|
|
|
-- LAMPEN SETUP
|
|
local original = smart_light.shadow_to_original[node.name] or node.name
|
|
if not smart_light.original_to_shadows[original] then return end
|
|
local n_meta = minetest.get_meta(pos)
|
|
local curr_area, curr_chan = n_meta:get_string("sl_area"), n_meta:get_string("sl_chan")
|
|
|
|
local areas_table = {"-- Bitte wählen --"}
|
|
for a_id, _ in pairs(smart_light.registry.areas) do table.insert(areas_table, a_id) end
|
|
local area_idx = 1
|
|
for i, v in ipairs(areas_table) do if v == curr_area then area_idx = i end end
|
|
|
|
local fs = "size[5,4.5]label[0.5,0.2;Licht Programmierung]" ..
|
|
"dropdown[0.5,1.2;4,1;area_sel;" .. table.concat(areas_table, ",") .. ";" .. area_idx .. "]" ..
|
|
"field[0.8,2.5;4,1;chan_in;Channel ID;" .. curr_chan .. "]" ..
|
|
"button[0.5,3.5;1.5,1;cancel;Abbruch]" ..
|
|
"button_exit[2.5,3.5;2,1;save;Übernehmen]"
|
|
minetest.show_formspec(name, "smart_light:prog_" .. minetest.pos_to_string(pos), fs)
|
|
return itemstack
|
|
end,
|
|
})
|