techage/power/power_terminal2.lua

303 lines
9.0 KiB
Lua
Raw Normal View History

2020-04-11 11:14:52 +03:00
--[[
TechAge
=======
2021-06-07 23:00:52 +03:00
Copyright (C) 2019-2021 Joachim Stolberg
2020-04-11 11:14:52 +03:00
2020-10-19 20:09:17 +03:00
AGPL v3
2020-04-11 11:14:52 +03:00
See LICENSE.txt for more information
TA3 Power Terminal
]]--
-- for lazy programmers
local S2P = minetest.string_to_pos
local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local M = minetest.get_meta
local N = function(pos) return minetest.get_node(pos).name end
local S = techage.S
local CYCLE_TIME = 2
local Cable = techage.ElectricCable
2021-06-07 23:00:52 +03:00
local power = networks.power
local control = networks.control
local HELP = S([[Commands
help . . . print this text
cls . . . . . clear screen
gen . . . . output all generators
sto . . . . . output all storage systems
load . . . . output storage load values
rst . . . . . . reset storage min/max values
]])
local function row(num, label, data)
local y = 4.0 + num * 0.5
return
"box[0," .. y .. ";9.8,0.4;#395c74]"..
"label[0.2,"..y..";" .. label .. "]" ..
"label[8.5,"..y..";" .. data .. "]"
2020-04-11 11:14:52 +03:00
end
2021-06-07 23:00:52 +03:00
local function formspec1(pos, data)
local mem = techage.get_mem(pos)
local outdir = M(pos):get_int("outdir")
local netw = networks.get_network_table(pos, Cable, outdir) or {}
data = data or power.get_network_data(pos, Cable, outdir)
2020-04-11 11:14:52 +03:00
2021-06-07 23:00:52 +03:00
mem.star = ((mem.star or 0) + 1) % 2
local star = mem.star == 1 and "*" or ""
--local state = get_state(netw, gen1, gen2, con1, con2)
local state = "tbd"
local storage_provided = math.max(data.consumed - data.available, 0)
local available = math.max(data.consumed, data.available)
print(data.provided, data.consumed, data.curr_load)
2020-04-11 11:14:52 +03:00
2021-06-07 23:00:52 +03:00
return "size[10,8]"..
2021-03-13 23:27:39 +03:00
"tabheader[0,0;tab;status,console;1;;true]"..
2020-04-11 11:14:52 +03:00
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
2021-06-07 23:00:52 +03:00
"box[0,-0.1;9.8,0.5;#c6e8ff]"..
"label[0.2,-0.1;"..minetest.colorize( "#000000", S("Network Data")).."]"..
"label[9.5,-0.1;"..minetest.colorize( "#000000", star).."]"..
techage.formspec_power_bar(pos, 0.0, 0.7, S("Generator"), data.provided, data.available)..
techage.formspec_power_bar(pos, 2.5, 0.7, S("Consumer"), data.consumed, available)..
techage.formspec_charging_bar(pos, 5.0, 0.7, S("Charging"), data)..
techage.formspec_storage_bar(pos, 7.5, 0.7, S("Storage"), data.curr_load, data.max_capa)..
2020-04-11 11:14:52 +03:00
2021-06-07 23:00:52 +03:00
row(1, S("Number of network nodes:"), netw.num_nodes or 0) ..
row(2, S("Number of generators:"), #(netw.gen or {})) ..
row(3, S("Number of consumers:"), #(netw.con or {})) ..
row(4, S("Number of storage systems:"), #(netw.sto or {}))
2020-04-11 11:14:52 +03:00
end
2021-06-07 23:00:52 +03:00
local function formspec2(pos)
local mem = techage.get_mem(pos)
2021-03-13 23:27:39 +03:00
local meta = M(pos)
local output = meta:get_string("output")
2021-05-11 19:06:28 +03:00
local command = mem.cmnd or "help"
2021-03-13 23:27:39 +03:00
output = minetest.formspec_escape(output)
output = output:gsub("\n", ",")
2021-06-07 23:00:52 +03:00
return "size[10,8]"..
2021-03-13 23:27:39 +03:00
"tabheader[0,0;tab;status,console;2;;true]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
2021-06-07 23:00:52 +03:00
"box[0,-0.1;9.8,0.5;#c6e8ff]"..
"label[0.2,-0.1;"..minetest.colorize( "#000000", S("Network Data")).."]"..
--"style_type[table,field;font=mono]"..
"table[0,0.5;9.8,6.8;output;"..output..";200]"..
"field[0.4,7.7;7.6,1;cmnd;;"..command.."]" ..
2021-03-13 23:27:39 +03:00
"field_close_on_enter[cmnd;false]"..
2021-06-07 23:00:52 +03:00
"button[7.9,7.4;2,1;enter;"..S("Enter").."]"
2021-03-13 23:27:39 +03:00
end
2021-06-07 23:00:52 +03:00
local function generators(pos)
2021-03-13 23:27:39 +03:00
local tbl = {}
2021-06-07 23:00:52 +03:00
local outdir = M(pos):get_int("outdir")
local resp = control.request(pos, Cable, outdir, "gen", "info")
for _, item in ipairs(resp) do
local name = item.type .. " (" .. item.number .. ")"
if item.running then
local s = string.format("%s (%s): %s/%u ku (%s)",
item.type, item.number, techage.round(item.provided), item.available, item.termpoint)
tbl[#tbl + 1] = s
else
local s = string.format("%s (%s): off",
item.type, item.number)
tbl[#tbl + 1] = s
2021-03-13 23:27:39 +03:00
end
end
2021-06-07 23:00:52 +03:00
table.sort(tbl)
2021-03-13 23:27:39 +03:00
return table.concat(tbl, "\n")
end
2021-06-07 23:00:52 +03:00
local function storages(pos)
2021-03-13 23:27:39 +03:00
local tbl = {}
2021-06-07 23:00:52 +03:00
local outdir = M(pos):get_int("outdir")
local resp = control.request(pos, Cable, outdir, "sto", "info")
for _, item in ipairs(resp) do
local name = item.type .. " (" .. item.number .. ")"
if item.running then
local s = string.format("%s (%s): %s/%s kud",
item.type, item.number,
techage.round(item.load / techage.CYCLES_PER_DAY),
techage.round(item.capa / techage.CYCLES_PER_DAY))
tbl[#tbl + 1] = s
else
local s = string.format("%s (%s): %s/%s kud (off)",
item.type, item.number,
techage.round(item.load / techage.CYCLES_PER_DAY),
techage.round(item.capa / techage.CYCLES_PER_DAY))
tbl[#tbl + 1] = s
2021-03-13 23:27:39 +03:00
end
end
2021-06-07 23:00:52 +03:00
table.sort(tbl)
2021-03-13 23:27:39 +03:00
return table.concat(tbl, "\n")
end
2021-06-07 23:00:52 +03:00
local function determine_min_max(pos)
local nvm = techage.get_nvm(pos)
local outdir = M(pos):get_int("outdir")
local data = power.get_network_data(pos, Cable, outdir)
if data then
nvm.min_load = math.min(nvm.min_load or 0, data.curr_load)
nvm.max_load = math.max(nvm.max_load or 0, data.curr_load)
return data
end
end
2021-03-13 23:27:39 +03:00
2021-06-07 23:00:52 +03:00
local function storage_load(pos)
local outdir = M(pos):get_int("outdir")
local data = power.get_network_data(pos, Cable, outdir)
local nvm = techage.get_nvm(pos)
nvm.min_load = nvm.min_load or data.curr_load
nvm.max_load = nvm.max_load or data.curr_load
return string.format("load: %s/%s kud, min: %s kud, max: %s kud)",
techage.round(data.curr_load / techage.CYCLES_PER_DAY),
techage.round(data.max_capa / techage.CYCLES_PER_DAY),
techage.round(nvm.min_load / techage.CYCLES_PER_DAY),
techage.round(nvm.max_load / techage.CYCLES_PER_DAY))
end
local function storage_reset(pos)
local nvm = techage.get_nvm(pos)
nvm.min_load = nil
nvm.max_load = nil
return "done."
end
2021-03-13 23:27:39 +03:00
local function output(pos, command, text)
local meta = M(pos)
text = meta:get_string("output") .. "\n$ " .. command .. "\n" .. (text or "")
text = text:sub(-2000,-1)
meta:set_string("output", text)
end
local function command(pos, nvm, command)
local meta = M(pos)
if command then
command = command:sub(1,80)
command = string.trim(command)
2021-06-07 23:00:52 +03:00
local cmd, data = unpack(string.split(command, " ", false, 1))
2021-03-13 23:27:39 +03:00
2021-06-07 23:00:52 +03:00
if cmd == "cls" then
2021-03-13 23:27:39 +03:00
meta:set_string("output", "")
2021-06-07 23:00:52 +03:00
elseif cmd == "help" then
2021-03-13 23:27:39 +03:00
output(pos, command, HELP)
2021-06-07 23:00:52 +03:00
elseif cmd == "gen" then
output(pos, command, generators(pos))
elseif cmd == "sto" then
output(pos, command, storages(pos))
elseif cmd == "load" then
output(pos, command, storage_load(pos))
elseif cmd == "rst" then
output(pos, command, storage_reset(pos))
2021-03-13 23:27:39 +03:00
elseif command ~= "" then
output(pos, command, "")
end
end
end
2020-04-11 11:14:52 +03:00
minetest.register_node("techage:ta3_power_terminal", {
description = S("TA3 Power Terminal"),
inventory_image = "techage_power_terminal_front.png",
tiles = {
"techage_power_terminal_top.png",
"techage_power_terminal_top.png",
"techage_power_terminal_side.png",
"techage_power_terminal_side.png",
"techage_power_terminal_back.png",
"techage_power_terminal_front.png",
},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{ -8/16, -8/16, 0/16, 8/16, 8/16, 8/16},
},
},
after_place_node = function(pos)
M(pos):set_int("outdir", networks.side_to_outdir(pos, "B"))
Cable:after_place_node(pos)
2021-06-07 23:00:52 +03:00
M(pos):set_string("formspec", formspec1(pos))
2020-04-11 11:14:52 +03:00
end,
after_dig_node = function(pos)
Cable:after_dig_node(pos)
techage.del_mem(pos)
end,
on_rightclick = function(pos, node, clicker)
2021-03-13 23:27:39 +03:00
local mem = techage.get_mem(pos)
if mem.active_formspec == 2 then
2021-06-07 23:00:52 +03:00
M(pos):set_string("formspec", formspec2(pos))
else
2021-06-07 23:00:52 +03:00
M(pos):set_string("formspec", formspec1(pos))
minetest.get_node_timer(pos):start(CYCLE_TIME)
techage.set_activeformspec(pos, clicker)
mem.active_formspec = 1
end
2020-04-11 11:14:52 +03:00
end,
on_timer = function(pos, elapsed)
2021-06-07 23:00:52 +03:00
local data = determine_min_max(pos)
2020-04-11 11:14:52 +03:00
if techage.is_activeformspec(pos) then
2021-06-07 23:00:52 +03:00
M(pos):set_string("formspec", formspec1(pos, data))
return true
2020-04-11 11:14:52 +03:00
end
end,
on_receive_fields = function(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
local nvm = techage.get_nvm(pos)
2021-03-13 23:27:39 +03:00
local mem = techage.get_mem(pos)
2020-04-11 11:14:52 +03:00
2021-03-13 23:27:39 +03:00
if fields.key_enter_field or fields.enter then
command(pos, nvm, fields.cmnd)
mem.cmnd = ""
M(pos):set_string("formspec", formspec2(pos, mem))
mem.cmnd = fields.cmnd
elseif fields.tab == "1" then
2021-06-07 23:00:52 +03:00
M(pos):set_string("formspec", formspec1(pos))
2021-03-13 23:27:39 +03:00
techage.set_activeformspec(pos, player)
2021-06-07 23:00:52 +03:00
minetest.get_node_timer(pos):start(CYCLE_TIME)
mem.active_formspec = 1
2021-03-13 23:27:39 +03:00
elseif fields.tab == "2" then
2021-06-07 23:00:52 +03:00
M(pos):set_string("formspec", formspec2(pos))
2021-03-13 23:27:39 +03:00
techage.reset_activeformspec(pos, player)
mem.active_formspec = 2
2021-03-13 23:27:39 +03:00
elseif fields.key_up and mem.cmnd then
2021-06-07 23:00:52 +03:00
M(pos):set_string("formspec", formspec2(pos))
2020-04-11 11:14:52 +03:00
end
end,
paramtype2 = "facedir",
paramtype = "light",
use_texture_alpha = techage.CLIP,
2020-04-11 11:14:52 +03:00
on_rotate = screwdriver.disallow,
sunlight_propagates = true,
is_ground_content = false,
groups = {cracky = 2, level = 2},
sounds = default.node_sound_metal_defaults(),
})
2021-06-07 23:00:52 +03:00
power.register_nodes({"techage:ta3_power_terminal"}, Cable, "con", {"B"})
2020-04-11 11:14:52 +03:00
minetest.register_craft({
output = "techage:ta3_power_terminal",
recipe = {
{"", "techage:usmium_nuggets", "default:steel_ingot"},
{"", "techage:basalt_glass_thin", "default:copper_ingot"},
{"", "techage:vacuum_tube", "default:steel_ingot"},
},
})