techage/logic/node_detector.lua

210 lines
5.6 KiB
Lua
Raw Normal View History

--[[
TechAge
=======
2020-01-31 21:55:10 +03:00
Copyright (C) 2017-2020 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
Node Detector
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
local logic = techage.logic
local CYCLE_TIME = 4
local function switch_on(pos)
if logic.swap_node(pos, "techage:ta3_nodedetector_on") then
logic.send_on(pos, M(pos))
end
end
local function switch_off(pos)
if logic.swap_node(pos, "techage:ta3_nodedetector_off") then
logic.send_off(pos, M(pos))
end
end
local DropdownValues = {
[S("added")] = 1,
[S("removed")] = 2,
[S("added or removed")] = 3,
}
2020-01-31 21:55:10 +03:00
local function formspec(meta, nvm)
local numbers = meta:get_string("numbers") or ""
local label = S("added")..","..S("removed")..","..S("added or removed")
return "size[7.5,4]"..
2019-08-29 22:43:00 +03:00
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"field[0.5,0.6;7,1;numbers;"..S("Insert destination node number(s)")..";"..numbers.."]" ..
"label[0.2,1.6;"..S("Send signal if nodes have been:").."]"..
2020-01-31 21:55:10 +03:00
"dropdown[0.2,2.1;7.3,1;mode;"..label..";"..(nvm.mode or 3).."]"..
2019-08-29 22:43:00 +03:00
"button_exit[2,3.2;3,1;accept;"..S("accept").."]"
end
local function any_node_changed(pos)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
if not nvm.pos1 or not nvm.pos2 or not nvm.num then
local node = minetest.get_node(pos)
local param2 = (node.param2 + 2) % 4
2020-01-31 21:55:10 +03:00
nvm.pos1 = logic.dest_pos(pos, param2, {0})
nvm.pos2 = logic.dest_pos(pos, param2, {0,0,0})
nvm.num = #minetest.find_nodes_in_area(nvm.pos1, nvm.pos2, {"air"})
return false
end
2020-01-31 21:55:10 +03:00
local num = #minetest.find_nodes_in_area(nvm.pos1, nvm.pos2, {"air"})
2020-01-31 21:55:10 +03:00
if nvm.num ~= num then
if nvm.mode == 1 and num < nvm.num then
nvm.num = num
return true
2020-01-31 21:55:10 +03:00
elseif nvm.mode == 2 and num > nvm.num then
nvm.num = num
return true
2020-01-31 21:55:10 +03:00
elseif nvm.mode == 3 then
nvm.num = num
return true
end
2020-01-31 21:55:10 +03:00
nvm.num = num
end
return false
end
local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
local meta = M(pos)
if fields.accept then
2020-01-31 21:55:10 +03:00
nvm.mode = DropdownValues[fields.mode] or 3
if techage.check_numbers(fields.numbers, player:get_player_name()) then
meta:set_string("numbers", fields.numbers)
logic.infotext(M(pos), S("TA3 Node Detector"))
end
end
2020-01-31 21:55:10 +03:00
meta:set_string("formspec", formspec(meta, nvm))
end
local function node_timer(pos)
if any_node_changed(pos)then
switch_on(pos)
else
switch_off(pos)
end
return true
end
minetest.register_node("techage:ta3_nodedetector_off", {
description = S("TA3 Node Detector"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta3.png^techage_frame_ta3_top.png^techage_appl_arrow.png^[transformR270",
"techage_filling_ta3.png^techage_frame_ta3_top.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_nodedetector.png",
},
after_place_node = function(pos, placer)
local meta = M(pos)
2020-01-31 21:55:10 +03:00
local nvm = techage.get_nvm(pos)
logic.after_place_node(pos, placer, "techage:ta3_repeater", S("TA3 Node Detector"))
logic.infotext(meta, S("TA3 Node Detector"))
2020-01-31 21:55:10 +03:00
nvm.mode = 3 -- default mode
meta:set_string("formspec", formspec(meta, nvm))
minetest.get_node_timer(pos):start(CYCLE_TIME)
any_node_changed(pos)
end,
on_timer = node_timer,
on_receive_fields = on_receive_fields,
techage_set_numbers = function(pos, numbers, player_name)
local meta = M(pos)
local res = logic.set_numbers(pos, numbers, player_name, S("TA3 Node Detector"))
2020-01-31 21:55:10 +03:00
meta:set_string("formspec", formspec(meta, techage.get_nvm(pos)))
return res
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
2020-06-14 00:32:00 +03:00
techage.remove_node(pos, oldnode, oldmetadata)
2020-01-31 21:55:10 +03:00
techage.del_mem(pos)
end,
on_rotate = screwdriver.disallow,
paramtype2 = "facedir",
is_ground_content = false,
groups = {choppy=2, cracky=2, crumbly=2},
sounds = default.node_sound_wood_defaults(),
})
minetest.register_node("techage:ta3_nodedetector_on", {
description = S("TA3 Node Detector"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta3.png^techage_frame_ta3_top.png^techage_appl_arrow.png^[transformR270",
"techage_filling_ta3.png^techage_frame_ta3_top.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_nodedetector_on.png",
},
on_timer = node_timer,
techage_set_numbers = function(pos, numbers, player_name)
local meta = M(pos)
local res = logic.set_numbers(pos, numbers, player_name, S("TA3 Node Detector"))
2020-01-31 21:55:10 +03:00
meta:set_string("formspec", formspec(meta, techage.get_nvm(pos)))
return res
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
2020-06-14 00:32:00 +03:00
techage.remove_node(pos, oldnode, oldmetadata)
2020-01-31 21:55:10 +03:00
techage.del_mem(pos)
end,
on_rotate = screwdriver.disallow,
paramtype2 = "facedir",
is_ground_content = false,
drop = "techage:ta3_nodedetector_off",
groups = {choppy=2, cracky=2, crumbly=2, not_in_creative_inventory = 1},
sounds = default.node_sound_wood_defaults(),
})
minetest.register_craft({
output = "techage:ta3_nodedetector_off",
recipe = {
{"", "group:wood", ""},
{"", "default:copper_ingot", "techage:vacuum_tube"},
{"", "group:wood", "default:mese_crystal"},
},
})
techage.register_node({"techage:ta3_nodedetector_off", "techage:ta3_nodedetector_on"}, {
2020-07-24 19:20:26 +03:00
on_recv_message = function(pos, src, topic, payload)
if topic == "name" then
local nvm = techage.get_nvm(pos)
return nvm.player_name or ""
elseif topic == "state" then
local node = techage.get_node_lvm(pos)
if node.name == "techage:ta3_nodedetector_off" then
return "on"
else
return "off"
end
else
return "unsupported"
end
end,
on_node_load = function(pos)
minetest.get_node_timer(pos):start(CYCLE_TIME)
end,
})