techage_modpack/signs_bot/node_sensor.lua

229 lines
6.0 KiB
Lua
Raw Permalink Normal View History

2020-05-31 23:31:18 +03:00
--[[
Signs Bot
=========
2021-05-14 19:50:16 +03:00
Copyright (C) 2019-2021 Joachim Stolberg
2020-05-31 23:31:18 +03:00
GPL v3
See LICENSE.txt for more information
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
Node Sensor
]]--
-- for lazy programmers
2021-05-14 19:50:16 +03:00
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
2020-05-31 23:31:18 +03:00
local M = minetest.get_meta
2021-05-14 19:50:16 +03:00
-- Load support for I18n.
local S = signs_bot.S
2020-05-31 23:31:18 +03:00
local lib = signs_bot.lib
2021-02-07 16:37:07 +03:00
local CYCLE_TIME = 2
2020-05-31 23:31:18 +03:00
local function update_infotext(pos, dest_pos, cmnd)
2021-05-14 19:50:16 +03:00
M(pos):set_string("infotext", S("Node Sensor: Connected with ")..P2S(dest_pos).." / "..cmnd)
2022-09-24 12:01:30 +03:00
end
2020-05-31 23:31:18 +03:00
local function swap_node(pos, name)
local node = minetest.get_node(pos)
if node.name == name then
return false
end
2020-10-25 23:32:47 +03:00
if string.sub(node.name, 1,21) == "signs_bot:node_sensor" then
node.name = name
minetest.swap_node(pos, node)
return true
end
return false
2020-05-31 23:31:18 +03:00
end
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
local DropdownValues = {
2021-05-14 19:50:16 +03:00
[S("added")] = 1,
[S("removed")] = 2,
[S("added or removed")] = 3,
2020-05-31 23:31:18 +03:00
}
local function formspec(mem)
2021-05-14 19:50:16 +03:00
local label = S("added")..","..S("removed")..","..S("added or removed")
2020-05-31 23:31:18 +03:00
return "size[6,3]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
2021-05-14 19:50:16 +03:00
"label[0.2,0.4;"..S("Send signal if nodes have been:").."]"..
2020-05-31 23:31:18 +03:00
"dropdown[0.2,1;6,1;mode;"..label..";"..(mem.mode or 3).."]"..
2021-05-14 19:50:16 +03:00
"button_exit[1.5,2.2;3,1;accept;"..S("accept").."]"
2020-05-31 23:31:18 +03:00
end
local function any_node_changed(pos)
local mem = tubelib2.get_mem(pos)
if not mem.pos1 or not mem.pos2 or not mem.num then
local node = minetest.get_node(pos)
local param2 = (node.param2 + 2) % 4
mem.pos1 = lib.dest_pos(pos, param2, {0})
mem.pos2 = lib.dest_pos(pos, param2, {0,0,0})
mem.num = #minetest.find_nodes_in_area(mem.pos1, mem.pos2, {"air"})
return false
end
local num = #minetest.find_nodes_in_area(mem.pos1, mem.pos2, {"air"})
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
if mem.num ~= num then
2022-09-24 12:01:30 +03:00
if mem.mode == 1 and num < mem.num then
2021-02-07 16:37:07 +03:00
mem.num = num
2020-05-31 23:31:18 +03:00
return true
2022-09-24 12:01:30 +03:00
elseif mem.mode == 2 and num > mem.num then
2021-02-07 16:37:07 +03:00
mem.num = num
2020-05-31 23:31:18 +03:00
return true
elseif mem.mode == 3 then
2021-02-07 16:37:07 +03:00
mem.num = num
2020-05-31 23:31:18 +03:00
return true
end
2021-02-07 16:37:07 +03:00
mem.num = num
2020-05-31 23:31:18 +03:00
end
return false
end
local function on_receive_fields(pos, formname, fields, player)
local mem = tubelib2.get_mem(pos)
local meta = M(pos)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
if fields.accept then
mem.mode = DropdownValues[fields.mode] or 3
2020-06-21 11:21:23 +03:00
mem.num = nil
minetest.get_node_timer(pos):start(CYCLE_TIME)
swap_node(pos, "signs_bot:node_sensor")
2020-05-31 23:31:18 +03:00
end
meta:set_string("formspec", formspec(mem))
end
local function node_timer(pos)
if any_node_changed(pos)then
if swap_node(pos, "signs_bot:node_sensor_on") then
signs_bot.send_signal(pos)
signs_bot.lib.activate_extender_nodes(pos, true)
2020-06-21 11:21:23 +03:00
minetest.after(1, swap_node, pos, "signs_bot:node_sensor")
2020-05-31 23:31:18 +03:00
end
end
return true
end
minetest.register_node("signs_bot:node_sensor", {
2021-05-14 19:50:16 +03:00
description = S("Node Sensor"),
2020-05-31 23:31:18 +03:00
inventory_image = "signs_bot_sensor_node_inv.png",
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -11/32, -1/2, -11/32, 11/32, -5/16, 11/32},
},
},
tiles = {
-- up, down, right, left, back, front
"signs_bot_sensor1.png^signs_bot_sensor_node.png",
"signs_bot_sensor1.png",
"signs_bot_sensor1.png^[transformFXR90",
"signs_bot_sensor1.png^[transformFXR90",
"signs_bot_sensor1.png^[transformFXR90",
"signs_bot_sensor1.png^[transformFXR180",
},
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
after_place_node = function(pos, placer)
local meta = M(pos)
local mem = tubelib2.init_mem(pos)
2021-05-14 19:50:16 +03:00
meta:set_string("infotext", S("Node Sensor: Not connected"))
2020-05-31 23:31:18 +03:00
mem.mode = 3 -- default legacy mode
meta:set_string("formspec", formspec(mem))
minetest.get_node_timer(pos):start(CYCLE_TIME)
any_node_changed(pos)
end,
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
on_timer = node_timer,
update_infotext = update_infotext,
on_receive_fields = on_receive_fields,
on_rotate = screwdriver.disallow,
paramtype = "light",
2021-05-14 19:50:16 +03:00
use_texture_alpha = signs_bot.CLIP,
2020-05-31 23:31:18 +03:00
sunlight_propagates = true,
paramtype2 = "facedir",
is_ground_content = false,
groups = {sign_bot_sensor = 1, cracky = 1},
sounds = default.node_sound_metal_defaults(),
})
minetest.register_node("signs_bot:node_sensor_on", {
2021-05-14 19:50:16 +03:00
description = S("Node Sensor"),
2020-05-31 23:31:18 +03:00
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -11/32, -1/2, -11/32, 11/32, -5/16, 11/32},
},
},
tiles = {
-- up, down, right, left, back, front
"signs_bot_sensor1.png^signs_bot_sensor_node_on.png",
"signs_bot_sensor1.png",
"signs_bot_sensor1.png^[transformFXR90",
"signs_bot_sensor1.png^[transformFXR90",
"signs_bot_sensor1.png^[transformFXR90",
"signs_bot_sensor1.png^[transformFXR180",
},
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
on_timer = node_timer,
update_infotext = update_infotext,
2020-06-21 11:21:23 +03:00
on_receive_fields = on_receive_fields,
2020-05-31 23:31:18 +03:00
on_rotate = screwdriver.disallow,
paramtype = "light",
2021-05-14 19:50:16 +03:00
use_texture_alpha = signs_bot.CLIP,
2020-05-31 23:31:18 +03:00
sunlight_propagates = true,
paramtype2 = "facedir",
is_ground_content = false,
2020-06-21 11:21:23 +03:00
drop = "signs_bot:node_sensor",
groups = {sign_bot_sensor = 1, cracky = 1, not_in_creative_inventory = 1},
2020-05-31 23:31:18 +03:00
sounds = default.node_sound_metal_defaults(),
})
minetest.register_craft({
output = "signs_bot:node_sensor",
recipe = {
{"", "", ""},
{"dye:black", "group:stone", "dye:grey"},
{"default:steel_ingot", "default:mese_crystal_fragment", "default:steel_ingot"}
}
})
minetest.register_lbm({
label = "[signs_bot] Restart timer",
name = "signs_bot:node_sensor_restart",
nodenames = {"signs_bot:node_sensor", "signs_bot:node_sensor_on"},
run_at_every_load = true,
action = function(pos, node)
minetest.get_node_timer(pos):start(CYCLE_TIME)
if node.name == "signs_bot:node_sensor_on" then
signs_bot.send_signal(pos)
signs_bot.lib.activate_extender_nodes(pos, true)
end
end
})
if minetest.get_modpath("doc") then
doc.add_entry("signs_bot", "node_sensor", {
2021-05-14 19:50:16 +03:00
name = S("Node Sensor"),
2020-05-31 23:31:18 +03:00
data = {
item = "signs_bot:node_sensor",
text = table.concat({
2021-05-14 19:50:16 +03:00
S("The node sensor sends cyclical signals when it detects that nodes have appeared or disappeared,"),
S("but has to be configured accordingly."),
S("Valid nodes are all kind of blocks and plants."),
2022-09-24 12:01:30 +03:00
S("The sensor range is 3 nodes/meters in one direction."),
2021-05-14 19:50:16 +03:00
S("The sensor has an active side (red) that must point to the observed area."),
2022-09-24 12:01:30 +03:00
}, "\n")
2020-05-31 23:31:18 +03:00
},
})
end