techage_modpack/signs_bot/techage.lua

372 lines
10 KiB
Lua
Raw Normal View History

2021-05-14 19:50:16 +03:00
--[[
Signs Bot
=========
Copyright (C) 2019-2021 Joachim Stolberg
GPLv3
See LICENSE.txt for more information
2022-09-24 12:01:30 +03:00
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
2023-11-05 15:18:27 +03:00
if minetest.global_exists("techage") then
2022-09-24 12:01:30 +03:00
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
2022-09-24 12:01:30 +03:00
2020-09-13 14:24:06 +03:00
signs_bot.register_inventory({"techage:chest_ta2", "techage:chest_ta3", "techage:chest_ta4",
2022-09-24 12:01:30 +03:00
"techage:ta3_silo", "techage:ta4_silo", "techage:ta4_sensor_chest",
"techage:ta4_reactor"}, {
2020-05-31 23:31:18 +03:00
allow_inventory_put = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
2022-09-24 12:01:30 +03:00
end,
2020-05-31 23:31:18 +03:00
allow_inventory_take = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
2022-09-24 12:01:30 +03:00
end,
2020-05-31 23:31:18 +03:00
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)
2022-09-24 12:01:30 +03:00
end,
2020-05-31 23:31:18 +03:00
allow_inventory_take = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
2022-09-24 12:01:30 +03:00
end,
2020-05-31 23:31:18 +03:00
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)
2022-09-24 12:01:30 +03:00
end,
2021-05-14 19:50:16 +03:00
allow_inventory_take = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
2022-09-24 12:01:30 +03:00
end,
2021-05-14 19:50:16 +03:00
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)
2022-09-24 12:01:30 +03:00
end,
2021-05-14 19:50:16 +03:00
allow_inventory_take = function(pos, stack, player_name)
return not minetest.is_protected(pos, player_name)
2022-09-24 12:01:30 +03:00
end,
2021-05-14 19:50:16 +03:00
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
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
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",
2022-09-24 12:01:30 +03:00
params = "",
2020-05-31 23:31:18 +03:00
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",
2022-09-24 12:01:30 +03:00
params = "<percent>",
2020-05-31 23:31:18 +03:00
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
2022-09-24 12:01:30 +03:00
signs_bot.register_botcommand("jump_low_batt", {
mod = "techage",
params = "<percent> <label>",
num_param = 2,
description = S("Jump to <label> if the\nbattery power is below the\ngiven value in percent (1..99)"),
check = function(val, lbl)
val = tonumber(val) or 5
return val and val > 0 and val < 100 and signs_bot.check_label(lbl)
end,
cmnd = function(base_pos, mem, val, addr)
val = tonumber(val) or 5
local pwr = percent_value(signs_bot.MAX_CAPA, mem.capa)
if pwr < val then
mem.pc = addr - 3
return signs_bot.DONE
end
return signs_bot.DONE
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
2022-09-24 12:01:30 +03:00
to a given node.
2021-05-14 19:50:16 +03:00
Receiver is addressed by
the techage node number.
2022-09-24 12:01:30 +03:00
For commands with two or more
words, use the '*' character
instead of spaces, e.g.:
2021-05-14 19:50:16 +03:00
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,
})
2022-09-24 12:01:30 +03:00
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
2022-09-24 12:01:30 +03:00
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
2022-09-24 12:01:30 +03:00
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,
2023-08-26 11:27:15 +03:00
on_pull_item = function(pos, in_dir, num, item_name)
2020-05-31 23:31:18 +03:00
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2023-08-26 11:27:15 +03:00
if item_name then
local taken = inv:remove_item("main", {name = item_name, count = num})
if taken:get_count() > 0 then
return taken
end
else -- no item given
return techage.get_items(pos, inv, "main", num)
end
2020-05-31 23:31:18 +03:00
end,
2023-08-26 11:27:15 +03:00
on_push_item = function(pos, in_dir, stack, idx)
2020-05-31 23:31:18 +03:00
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2023-08-26 11:27:15 +03:00
return techage.put_items(inv, "main", stack, idx)
2020-05-31 23:31:18 +03:00
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,
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
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,
2023-08-26 11:27:15 +03:00
on_beduino_receive_cmnd = function(pos, src, topic, payload)
local mem = tubelib2.get_mem(pos)
if topic == 1 then
if payload[1] == 1 then -- on
if not mem.running then
signs_bot.start_robot(pos)
return 0, {1}
end
else
if mem.running then
signs_bot.stop_robot(pos, mem)
return 0, {1}
end
end
else
return 2, "" -- topic is unknown or invalid
end
end,
on_beduino_request_data = function(pos, src, topic, payload)
local mem = tubelib2.get_mem(pos)
if topic == 128 then -- state
if mem.error then
return 0, {5}
elseif mem.running then
if mem.curr_cmnd == "stop" then
return 0, {3}
elseif mem.blocked then
return 0, {2}
else
return 0, {1} -- running
end
elseif mem.capa then
if mem.capa <= 0 then
return 0, {4} -- nopower
elseif mem.capa >= signs_bot.MAX_CAPA then
return 0, {6} -- stopped
else
return 0, {7} -- charging
end
else
return 0, {6} -- stopped
end
else
return 2, "" -- topic is unknown or invalid
end
end,
2022-09-24 12:01:30 +03:00
})
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,
2023-08-26 11:27:15 +03:00
on_pull_item = function(pos, in_dir, num, item_name)
2020-07-21 18:45:15 +03:00
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2023-08-26 11:27:15 +03:00
if item_name then
local taken = inv:remove_item("main", {name = item_name, count = num})
if taken:get_count() > 0 then
return taken
end
else -- no item given
return techage.get_items(pos, inv, "main", num)
end
2020-07-21 18:45:15 +03:00
end,
2023-08-26 11:27:15 +03:00
on_push_item = function(pos, in_dir, stack, idx)
2020-07-21 18:45:15 +03:00
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2023-08-26 11:27:15 +03:00
return techage.put_items(inv, "main", stack, idx)
2020-07-21 18:45:15 +03:00
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,
2022-09-24 12:01:30 +03:00
})
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)
2023-11-05 15:18:27 +03:00
techage.disable_block_for_assembly_tool("signs_bot:box")
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