2021-05-14 19:50:16 +03:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Signs Bot
|
|
|
|
=========
|
|
|
|
|
|
|
|
Copyright (C) 2019-2021 Joachim Stolberg
|
|
|
|
|
|
|
|
GPLv3
|
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
2021-08-01 12:00:22 +03:00
|
|
|
Signs Bot: interface for techage
|
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
|
|
|
|
2021-08-01 12:00:22 +03:00
|
|
|
local MAX_CAPA = signs_bot.MAX_CAPA
|
|
|
|
local PWR_NEEDED = 8
|
2020-05-31 23:31:18 +03:00
|
|
|
|
|
|
|
if minetest.get_modpath("techage") then
|
|
|
|
|
2021-08-01 12:00:22 +03:00
|
|
|
local function on_power(pos)
|
|
|
|
local mem = tubelib2.get_mem(pos)
|
|
|
|
mem.power_available = true
|
|
|
|
mem.charging = true
|
|
|
|
signs_bot.infotext(pos, S("charging"))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_nopower(pos)
|
|
|
|
local mem = tubelib2.get_mem(pos)
|
|
|
|
mem.power_available = false
|
|
|
|
signs_bot.infotext(pos, S("no power"))
|
|
|
|
end
|
|
|
|
|
2020-05-31 23:31:18 +03:00
|
|
|
local Cable = techage.ElectricCable
|
2021-08-01 12:00:22 +03:00
|
|
|
local power = networks.power
|
2020-05-31 23:31:18 +03:00
|
|
|
|
2020-09-13 14:24:06 +03:00
|
|
|
signs_bot.register_inventory({"techage:chest_ta2", "techage:chest_ta3", "techage:chest_ta4",
|
2021-05-14 19:50:16 +03:00
|
|
|
"techage:ta3_silo", "techage:ta4_silo", "techage:ta4_sensor_chest"}, {
|
2020-05-31 23:31:18 +03:00
|
|
|
allow_inventory_put = function(pos, stack, player_name)
|
|
|
|
return not minetest.is_protected(pos, player_name)
|
|
|
|
end,
|
|
|
|
allow_inventory_take = function(pos, stack, player_name)
|
|
|
|
return not minetest.is_protected(pos, player_name)
|
|
|
|
end,
|
|
|
|
put = {
|
|
|
|
listname = "main",
|
|
|
|
},
|
|
|
|
take = {
|
|
|
|
listname = "main",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
signs_bot.register_inventory({"techage:meltingpot", "techage:meltingpot_active"}, {
|
|
|
|
allow_inventory_put = function(pos, stack, player_name)
|
|
|
|
return not minetest.is_protected(pos, player_name)
|
|
|
|
end,
|
|
|
|
allow_inventory_take = function(pos, stack, player_name)
|
|
|
|
return not minetest.is_protected(pos, player_name)
|
|
|
|
end,
|
|
|
|
put = {
|
|
|
|
listname = "src",
|
|
|
|
},
|
|
|
|
take = {
|
|
|
|
listname = "dst",
|
|
|
|
},
|
|
|
|
})
|
2021-05-14 19:50:16 +03:00
|
|
|
signs_bot.register_inventory({
|
|
|
|
"techage:ta2_autocrafter_pas", "techage:ta2_autocrafter_act",
|
|
|
|
"techage:ta3_autocrafter_pas", "techage:ta3_autocrafter_act"}, {
|
|
|
|
allow_inventory_put = function(pos, stack, player_name)
|
|
|
|
return not minetest.is_protected(pos, player_name)
|
|
|
|
end,
|
|
|
|
allow_inventory_take = function(pos, stack, player_name)
|
|
|
|
return not minetest.is_protected(pos, player_name)
|
|
|
|
end,
|
|
|
|
put = {
|
|
|
|
listname = "src",
|
|
|
|
},
|
|
|
|
take = {
|
|
|
|
listname = "dst",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
signs_bot.register_inventory({
|
|
|
|
"techage:ta2_distributor_pas", "techage:ta2_distributor_act",
|
|
|
|
"techage:ta3_distributor_pas", "techage:ta3_distributor_act",
|
|
|
|
"techage:ta4_distributor_pas", "techage:ta4_distributor_act",
|
|
|
|
"techage:ta4_high_performance_distributor_pas", "techage:ta4_high_performance_distributor_act"}, {
|
|
|
|
allow_inventory_put = function(pos, stack, player_name)
|
|
|
|
return not minetest.is_protected(pos, player_name)
|
|
|
|
end,
|
|
|
|
allow_inventory_take = function(pos, stack, player_name)
|
|
|
|
return not minetest.is_protected(pos, player_name)
|
|
|
|
end,
|
|
|
|
put = {
|
|
|
|
listname = "src",
|
|
|
|
},
|
|
|
|
take = {
|
|
|
|
listname = "src",
|
|
|
|
},
|
|
|
|
})
|
2020-05-31 23:31:18 +03:00
|
|
|
|
|
|
|
local function percent_value(max_val, curr_val)
|
|
|
|
return math.min(math.ceil(((curr_val or 0) * 100.0) / (max_val or 1.0)), 100)
|
|
|
|
end
|
|
|
|
signs_bot.percent_value = percent_value
|
|
|
|
|
|
|
|
function signs_bot.formspec_battery_capa(max_capa, current_capa)
|
|
|
|
local percent = percent_value(max_capa, current_capa)
|
|
|
|
return "image[0.1,0;0.5,1;signs_bot_form_level_bg.png^[lowpart:"..
|
|
|
|
percent..":signs_bot_form_level_fg.png]"
|
|
|
|
end
|
|
|
|
|
|
|
|
signs_bot.register_botcommand("ignite", {
|
|
|
|
mod = "techage",
|
|
|
|
params = "",
|
|
|
|
num_param = 0,
|
|
|
|
description = S("Ignite the techage charcoal lighter"),
|
|
|
|
cmnd = function(base_pos, mem)
|
|
|
|
local pos = signs_bot.lib.dest_pos(mem.robot_pos, mem.robot_param2, {0})
|
2021-02-07 16:37:07 +03:00
|
|
|
local node = tubelib2.get_node_lvm(pos)
|
2020-05-31 23:31:18 +03:00
|
|
|
if minetest.registered_nodes[node.name]
|
|
|
|
and minetest.registered_nodes[node.name].on_ignite then
|
|
|
|
minetest.registered_nodes[node.name].on_ignite(pos)
|
|
|
|
end
|
2020-09-13 14:24:06 +03:00
|
|
|
return signs_bot.DONE
|
2020-05-31 23:31:18 +03:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
signs_bot.register_botcommand("low_batt", {
|
|
|
|
mod = "techage",
|
|
|
|
params = "<percent>",
|
|
|
|
num_param = 1,
|
2020-08-08 13:52:38 +03:00
|
|
|
description = S("Turns the bot off if the\nbattery power is below the\ngiven value in percent (1..99)"),
|
2020-05-31 23:31:18 +03:00
|
|
|
check = function(val)
|
|
|
|
val = tonumber(val) or 5
|
|
|
|
return val and val > 0 and val < 100
|
|
|
|
end,
|
|
|
|
cmnd = function(base_pos, mem, val)
|
|
|
|
val = tonumber(val) or 5
|
|
|
|
local pwr = percent_value(signs_bot.MAX_CAPA, mem.capa)
|
|
|
|
if pwr < val then
|
|
|
|
signs_bot.stop_robot(base_pos, mem)
|
|
|
|
return signs_bot.TURN_OFF
|
|
|
|
end
|
2020-08-08 13:52:38 +03:00
|
|
|
return signs_bot.DONE
|
2020-05-31 23:31:18 +03:00
|
|
|
end,
|
|
|
|
})
|
2020-10-25 23:32:47 +03:00
|
|
|
|
|
|
|
signs_bot.register_botcommand("send_cmnd", {
|
|
|
|
mod = "techage",
|
|
|
|
params = "<receiver> <command>",
|
|
|
|
num_param = 2,
|
2021-05-14 19:50:16 +03:00
|
|
|
description = S([[Sends a techage command
|
|
|
|
to a given node.
|
|
|
|
Receiver is addressed by
|
|
|
|
the techage node number.
|
|
|
|
For commands with two or more
|
|
|
|
words, use the '*' character
|
|
|
|
instead of spaces, e.g.:
|
|
|
|
send_cmnd 3465 pull*default:dirt*2]]),
|
2020-10-25 23:32:47 +03:00
|
|
|
check = function(address, command)
|
|
|
|
address = tonumber(address)
|
|
|
|
return address ~= nil and command ~= nil and command ~= ""
|
|
|
|
end,
|
|
|
|
cmnd = function(base_pos, mem, address, command)
|
2021-05-14 19:50:16 +03:00
|
|
|
command = command:gsub("*", " ")
|
2020-10-25 23:32:47 +03:00
|
|
|
address = tostring(tonumber(address))
|
|
|
|
local meta = minetest.get_meta(base_pos)
|
2021-09-03 20:32:06 +03:00
|
|
|
local number = tostring(meta:get_int("number") or 0)
|
2021-05-14 19:50:16 +03:00
|
|
|
local topic, payload = unpack(string.split(command, " ", false, 1))
|
2021-09-03 20:32:06 +03:00
|
|
|
techage.send_single(number, address, topic, payload)
|
2020-10-25 23:32:47 +03:00
|
|
|
return signs_bot.DONE
|
|
|
|
end,
|
|
|
|
})
|
2020-05-31 23:31:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Bot in the box
|
|
|
|
function signs_bot.while_charging(pos, mem)
|
|
|
|
mem.capa = mem.capa or 0
|
2021-08-01 12:00:22 +03:00
|
|
|
|
|
|
|
if mem.capa < signs_bot.MAX_CAPA then
|
|
|
|
local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED)
|
|
|
|
mem.capa = mem.capa + consumed
|
2020-05-31 23:31:18 +03:00
|
|
|
else
|
2021-08-01 12:00:22 +03:00
|
|
|
minetest.get_node_timer(pos):stop()
|
|
|
|
mem.charging = false
|
|
|
|
if not mem.running then
|
|
|
|
signs_bot.infotext(pos, S("fully charged"))
|
|
|
|
end
|
|
|
|
return false
|
2020-05-31 23:31:18 +03:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-08-01 12:00:22 +03:00
|
|
|
power.register_nodes({"signs_bot:box"}, Cable, "con")
|
2020-05-31 23:31:18 +03:00
|
|
|
|
|
|
|
techage.register_node({"signs_bot:box"}, {
|
2021-05-14 19:50:16 +03:00
|
|
|
on_inv_request = function(pos, in_dir, access_type)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
return meta:get_inventory(), "main"
|
|
|
|
end,
|
2020-05-31 23:31:18 +03:00
|
|
|
on_pull_item = function(pos, in_dir, num)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
return techage.get_items(pos, inv, "main", num)
|
|
|
|
end,
|
|
|
|
on_push_item = function(pos, in_dir, stack)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
return techage.put_items(inv, "main", stack)
|
|
|
|
end,
|
|
|
|
on_unpull_item = function(pos, in_dir, stack)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
return techage.put_items(inv, "main", stack)
|
|
|
|
end,
|
|
|
|
|
|
|
|
on_recv_message = function(pos, src, topic, payload)
|
|
|
|
local mem = tubelib2.get_mem(pos)
|
|
|
|
if topic == "state" then
|
|
|
|
if mem.error then
|
|
|
|
return "fault"
|
|
|
|
elseif mem.running then
|
|
|
|
if mem.curr_cmnd == "stop" then
|
|
|
|
return "standby"
|
|
|
|
elseif mem.blocked then
|
|
|
|
return "blocked"
|
|
|
|
else
|
|
|
|
return "running"
|
|
|
|
end
|
|
|
|
elseif mem.capa then
|
|
|
|
if mem.capa <= 0 then
|
|
|
|
return "nopower"
|
|
|
|
elseif mem.capa >= signs_bot.MAX_CAPA then
|
|
|
|
return "stopped"
|
|
|
|
else
|
2021-05-14 19:50:16 +03:00
|
|
|
return "charging"
|
2020-05-31 23:31:18 +03:00
|
|
|
end
|
|
|
|
else
|
|
|
|
return "stopped"
|
|
|
|
end
|
|
|
|
elseif topic == "load" then
|
|
|
|
return signs_bot.percent_value(signs_bot.MAX_CAPA, mem.capa)
|
2020-10-25 23:32:47 +03:00
|
|
|
elseif topic == "on" then
|
|
|
|
if not mem.running then
|
|
|
|
signs_bot.start_robot(pos)
|
|
|
|
end
|
|
|
|
elseif topic == "off" then
|
|
|
|
if mem.running then
|
|
|
|
signs_bot.stop_robot(pos, mem)
|
|
|
|
end
|
2020-05-31 23:31:18 +03:00
|
|
|
else
|
|
|
|
return "unsupported"
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
2020-07-21 18:45:15 +03:00
|
|
|
techage.register_node({"signs_bot:chest"}, {
|
2021-05-14 19:50:16 +03:00
|
|
|
on_inv_request = function(pos, in_dir, access_type)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
return meta:get_inventory(), "main"
|
|
|
|
end,
|
2020-07-21 18:45:15 +03:00
|
|
|
on_pull_item = function(pos, in_dir, num)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
return techage.get_items(pos, inv, "main", num)
|
|
|
|
end,
|
|
|
|
on_push_item = function(pos, in_dir, stack)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
return techage.put_items(inv, "main", stack)
|
|
|
|
end,
|
|
|
|
on_unpull_item = function(pos, in_dir, stack)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
return techage.put_items(inv, "main", stack)
|
|
|
|
end,
|
|
|
|
})
|
2021-08-01 12:00:22 +03:00
|
|
|
|
|
|
|
techage.register_node_for_v1_transition({"signs_bot:box"}, function(pos, node)
|
|
|
|
power.update_network(pos, nil, Cable)
|
|
|
|
end)
|
2020-05-31 23:31:18 +03:00
|
|
|
else
|
|
|
|
function signs_bot.formspec_battery_capa(max_capa, current_capa)
|
|
|
|
return ""
|
|
|
|
end
|
2021-08-01 12:00:22 +03:00
|
|
|
end
|
|
|
|
|