techage_modpack/signs_bot/logic_and.lua

257 lines
5.7 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
Signal AND.
Signal is sent, if all input signals are received.
]]--
-- 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
local S2P = minetest.string_to_pos
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
local function inputs(tbl)
local out = {}
for _,v in ipairs(tbl) do
2022-09-24 12:01:30 +03:00
if v then
2020-05-31 23:31:18 +03:00
out[#out + 1] = "1"
else
out[#out + 1] = "0"
end
end
return table.concat(out, " ")
end
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
local function update_infotext(pos, dest_pos, cmnd)
local mem = tubelib2.get_mem(pos)
local text = table.concat({
2021-05-14 19:50:16 +03:00
S("Signal AND with"),
2020-05-31 23:31:18 +03:00
#mem.inputs or 0,
2021-05-14 19:50:16 +03:00
S("inputs"),
2020-05-31 23:31:18 +03:00
":",
2021-05-14 19:50:16 +03:00
S("Connected with"),
P2S(dest_pos),
2020-05-31 23:31:18 +03:00
"/",
cmnd,
":",
inputs(mem.inputs or {})
}, " ")
M(pos):set_string("infotext", text)
2022-09-24 12:01:30 +03:00
end
2020-05-31 23:31:18 +03:00
local function all_inputs(mem)
mem.inputs = mem.inputs or {}
for _,v in ipairs(mem.inputs) do
if not v then return false end
end
return true
end
local function zero_inputs(mem)
mem.inputs = mem.inputs or {}
for _,v in ipairs(mem.inputs) do
if v then return true end
end
return false
end
local function clear_inputs(mem)
mem.inputs = mem.inputs or {}
for i,_ in ipairs(mem.inputs) do
mem.inputs[i] = false
end
end
local function infotext(pos)
local meta = M(pos)
local dest_pos = meta:get_string("signal_pos")
local signal = meta:get_string("signal_data")
if dest_pos ~= "" and signal ~= "" then
2021-05-14 19:50:16 +03:00
update_infotext(pos, S2P(dest_pos), signal)
2020-05-31 23:31:18 +03:00
end
end
-- Used by the pairing tool
local function signs_bot_get_signal(pos, node)
local mem = tubelib2.get_mem(pos)
mem.inputs = mem.inputs or {}
mem.inputs[#mem.inputs + 1] = false
clear_inputs(mem)
infotext(pos)
return #mem.inputs
end
-- switch to normal texture
2022-09-24 12:01:30 +03:00
local function turn_off(pos)
2020-05-31 23:31:18 +03:00
local node = minetest.get_node(pos)
if node.name == "signs_bot:and2" or node.name == "signs_bot:and3" then
node.name = "signs_bot:and1"
minetest.swap_node(pos, node)
end
end
-- switch to not zero texture
2022-09-24 12:01:30 +03:00
local function not_zero(pos)
2020-05-31 23:31:18 +03:00
local node = minetest.get_node(pos)
if node.name == "signs_bot:and1" or node.name == "signs_bot:and3" then
node.name = "signs_bot:and2"
minetest.swap_node(pos, node)
end
infotext(pos)
end
local function send_signal(pos)
local meta = M(pos)
local mem = tubelib2.get_mem(pos)
local node = minetest.get_node(pos)
if node.name == "signs_bot:and1" or node.name == "signs_bot:and2" then
node.name = "signs_bot:and3"
minetest.swap_node(pos, node)
minetest.after(2, turn_off, pos)
end
signs_bot.send_signal(pos)
signs_bot.lib.activate_extender_nodes(pos, true)
clear_inputs(mem)
infotext(pos)
end
-- To be called from sensors
local function signs_bot_on_signal(pos, node, signal)
local mem = tubelib2.get_mem(pos)
signal = tonumber(signal) or 1
mem.inputs = mem.inputs or {}
mem.inputs[signal] = true
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
if all_inputs(mem) then
send_signal(pos)
else
not_zero(pos)
end
end
minetest.register_node("signs_bot:and1", {
2021-05-14 19:50:16 +03:00
description = S("Signal AND"),
2020-05-31 23:31:18 +03:00
inventory_image = "signs_bot_and_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_sensor2.png^signs_bot_and1.png",
"signs_bot_sensor2.png",
},
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)
mem.inputs = {}
infotext(pos)
end,
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
signs_bot_get_signal = signs_bot_get_signal,
signs_bot_on_signal = signs_bot_on_signal,
update_infotext = update_infotext,
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:and2", {
2021-05-14 19:50:16 +03:00
description = S("Signal AND"),
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_sensor2.png^signs_bot_and2.png",
"signs_bot_sensor2.png",
},
signs_bot_get_signal = signs_bot_get_signal,
signs_bot_on_signal = signs_bot_on_signal,
update_infotext = update_infotext,
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,
drop = "signs_bot:and1",
groups = {sign_bot_sensor = 1, cracky = 1, not_in_creative_inventory = 1},
sounds = default.node_sound_metal_defaults(),
})
minetest.register_node("signs_bot:and3", {
2021-05-14 19:50:16 +03:00
description = S("Signal AND"),
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_sensor2.png^signs_bot_and3.png",
"signs_bot_sensor2.png",
},
update_infotext = update_infotext,
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,
diggable = false,
groups = {sign_bot_sensor = 1, not_in_creative_inventory = 1},
sounds = default.node_sound_metal_defaults(),
})
minetest.register_craft({
output = "signs_bot:and1",
recipe = {
{"group:wood", "dye:yellow", ""},
{"", "", ""},
{"default:steel_ingot", "", "default:mese_crystal_fragment"},
}
})
if minetest.get_modpath("doc") then
doc.add_entry("signs_bot", "and", {
2021-05-14 19:50:16 +03:00
name = S("Signal AND"),
2020-05-31 23:31:18 +03:00
data = {
item = "signs_bot:and1",
text = table.concat({
2022-09-24 12:01:30 +03:00
S("Signal is sent, if all input signals are received."),
}, "\n")
2020-05-31 23:31:18 +03:00
},
})
end