From d80304b449435bbb10c4c76e9fc2870b409d0f0a Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Mon, 7 Jun 2021 22:00:52 +0200 Subject: [PATCH] Prepare for lib 'networks' --- basic_machines/consumer.lua | 15 + basic_machines/gravelrinser.lua | 1 + basic_machines/gravelsieve.lua | 4 +- basic_machines/grinder.lua | 1 + basic_machines/pusher.lua | 1 + basic_machines/source.lua | 224 ++++-------- coal_power_station/generator.lua | 69 ++-- coal_power_station/oilfirebox.lua | 68 ++-- init.lua | 66 ++-- lamps/growlight.lua | 36 +- lamps/lib.lua | 84 +++-- power/formspecs.lua | 234 ++++++++++--- power/power_terminal.lua | 208 +---------- power/power_terminal2.lua | 555 ++++++++---------------------- steam_engine/flywheel.lua | 22 +- ta3_power/akkubox.lua | 119 +++---- 16 files changed, 647 insertions(+), 1060 deletions(-) diff --git a/basic_machines/consumer.lua b/basic_machines/consumer.lua index cd77751..e37daaa 100644 --- a/basic_machines/consumer.lua +++ b/basic_machines/consumer.lua @@ -27,6 +27,7 @@ local M = minetest.get_meta local CRD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).consumer end local CRDN = function(node) return (minetest.registered_nodes[node.name] or {}).consumer end +local Tube = techage.Tube local power = networks.power local liquid = networks.liquid local CYCLE_TIME = 2 @@ -61,6 +62,15 @@ local function node_timer_pas(pos, elapsed) crd.State:start(pos, nvm) end end + + -- call the node timer routine + if techage.is_operational(nvm) then + nvm.node_timer_call_cyle = (nvm.node_timer_call_cyle or 0) + 1 + if nvm.node_timer_call_cyle >= crd.call_cycle then + crd.node_timer(pos, crd.cycle_time) + nvm.node_timer_call_cyle = 0 + end + end return crd.State:is_active(nvm) end @@ -280,6 +290,11 @@ function techage.register_consumer(base_name, inv_name, tiles, tNode, validState power.register_nodes({name_pas, name_act}, power_network, "con", sides) end techage.register_node({name_pas, name_act}, tNode.tubing) + + if tNode.tube_sides then + Tube:set_valid_sides(name_pas, get_keys(tNode.tube_sides)) + Tube:set_valid_sides(name_act, get_keys(tNode.tube_sides)) + end end end return names[1], names[2], names[3] diff --git a/basic_machines/gravelrinser.lua b/basic_machines/gravelrinser.lua index 77b853f..a680fb0 100644 --- a/basic_machines/gravelrinser.lua +++ b/basic_machines/gravelrinser.lua @@ -293,6 +293,7 @@ local node_name_ta2, node_name_ta3, node_name_ta4 = sounds = default.node_sound_wood_defaults(), num_items = {0,1,1,1}, power_consumption = {0,3,3,3}, + tube_sides = {L=1, R=1, U=1}, }, {false, true, true, false}) -- TA2/TA3 diff --git a/basic_machines/gravelsieve.lua b/basic_machines/gravelsieve.lua index 6c2a340..0bd9aa4 100644 --- a/basic_machines/gravelsieve.lua +++ b/basic_machines/gravelsieve.lua @@ -3,7 +3,7 @@ TechAge ======= - Copyright (C) 2019 Joachim Stolberg + Copyright (C) 2019-2021 Joachim Stolberg AGPL v3 See LICENSE.txt for more information @@ -161,7 +161,6 @@ local tubing = { local meta = minetest.get_meta(pos) if meta:get_int("push_dir") == in_dir or in_dir == 5 then local inv = M(pos):get_inventory() - --CRD(pos).State:start_if_standby(pos) -- would need power! return techage.put_items(inv, "src", stack) end end, @@ -217,6 +216,7 @@ local node_name_ta2, node_name_ta3, node_name_ta4 = sounds = default.node_sound_wood_defaults(), num_items = {0,1,2,4}, power_consumption = {0,3,4,5}, + tube_sides = {L=1, R=1, U=1}, }) minetest.register_craft({ diff --git a/basic_machines/grinder.lua b/basic_machines/grinder.lua index 89ca511..c201c6f 100644 --- a/basic_machines/grinder.lua +++ b/basic_machines/grinder.lua @@ -242,6 +242,7 @@ local node_name_ta2, node_name_ta3, node_name_ta4 = sounds = default.node_sound_wood_defaults(), num_items = {0,1,2,4}, power_consumption = {0,4,6,9}, + tube_sides = {L=1, R=1, U=1}, }) minetest.register_craft({ diff --git a/basic_machines/pusher.lua b/basic_machines/pusher.lua index 4d1e1ee..12f177d 100644 --- a/basic_machines/pusher.lua +++ b/basic_machines/pusher.lua @@ -290,6 +290,7 @@ local node_name_ta2, node_name_ta3, node_name_ta4 = is_ground_content = false, sounds = default.node_sound_wood_defaults(), num_items = {0,2,6,12}, + tube_sides = {L=1, R=1}, }) minetest.register_craft({ diff --git a/basic_machines/source.lua b/basic_machines/source.lua index ec20401..3710f77 100644 --- a/basic_machines/source.lua +++ b/basic_machines/source.lua @@ -8,7 +8,7 @@ AGPL v3 See LICENSE.txt for more information - TA2/TA3/TA4 Power Test Source + TA2/TA3 Power Test Source ]]-- @@ -21,35 +21,15 @@ local Axle = techage.Axle --local Pipe = techage.SteamPipe local Cable = techage.ElectricCable local power = networks.power ---local networks = techage.networks +local control = networks.control local STANDBY_TICKS = 4 -local COUNTDOWN_TICKS = 4 local CYCLE_TIME = 2 local PWR_PERF = 100 -local function formspec2(self, pos, nvm) - return "size[4,4]".. - "box[0,-0.1;3.8,0.5;#c6e8ff]".. - "label[1,-0.1;"..minetest.colorize( "#000000", S("Power Source")).."]".. - default.gui_bg.. - default.gui_bg_img.. - default.gui_slots.. - techage.power.formspec_label_bar(pos, 0, 0.8, Axle, S("power"), PWR_PERF, nvm.provided).. - "image_button[2.8,2;1,1;".. self:get_state_button_image(nvm) ..";state_button;]".. - "tooltip[2.8,2;1,1;"..self:get_state_tooltip(nvm).."]" -end - -local function formspec4(self, pos, nvm) - return "size[4,4]".. - "box[0,-0.1;3.8,0.5;#c6e8ff]".. - "label[1,-0.1;"..minetest.colorize( "#000000", S("Power Source")).."]".. - default.gui_bg.. - default.gui_bg_img.. - default.gui_slots.. - techage.power.formspec_label_bar(pos, 0, 0.8, Cable, S("power"), PWR_PERF, nvm.provided).. - "image_button[2.8,2;1,1;".. self:get_state_button_image(nvm) ..";state_button;]".. - "tooltip[2.8,2;1,1;"..self:get_state_tooltip(nvm).."]" +local function formspec(self, pos, nvm) + return techage.generator_formspec(self, pos, nvm, S("Power Source"), + nvm.provided, PWR_PERF, nvm.generating) end -- Axles texture animation @@ -75,27 +55,14 @@ local function stop_node2(pos, nvm, state) power.start_storage_calc(pos, Axle, outdir) end ---local function start_node3(pos, nvm, state) --- nvm.generating = true --- local outdir = M(pos):get_int("outdir") --- power.generator_start(pos, Pipe, CYCLE_TIME, outdir) ---end - ---local function stop_node3(pos, nvm, state) --- nvm.generating = false --- nvm.provided = 0 --- local outdir = M(pos):get_int("outdir") --- power.generator_stop(pos, Pipe, outdir) ---end - -local function start_node4(pos, nvm, state) +local function start_node3(pos, nvm, state) nvm.generating = true nvm.provided = 0 local outdir = M(pos):get_int("outdir") power.start_storage_calc(pos, Cable, outdir) end -local function stop_node4(pos, nvm, state) +local function stop_node3(pos, nvm, state) nvm.generating = false nvm.provided = 0 local outdir = M(pos):get_int("outdir") @@ -106,66 +73,40 @@ local State2 = techage.NodeStates:new({ node_name_passive = "techage:t2_source", cycle_time = CYCLE_TIME, standby_ticks = STANDBY_TICKS, - formspec_func = formspec2, + formspec_func = formspec, start_node = start_node2, stop_node = stop_node2, }) ---local State3 = techage.NodeStates:new({ --- node_name_passive = "techage:t3_source", --- cycle_time = CYCLE_TIME, --- standby_ticks = STANDBY_TICKS, --- formspec_func = formspec3, --- start_node = start_node3, --- stop_node = stop_node3, ---}) - -local State4 = techage.NodeStates:new({ +local State3 = techage.NodeStates:new({ node_name_passive = "techage:t4_source", cycle_time = CYCLE_TIME, standby_ticks = STANDBY_TICKS, - formspec_func = formspec4, - start_node = start_node4, - stop_node = stop_node4, + formspec_func = formspec, + start_node = start_node3, + stop_node = stop_node3, }) local function node_timer2(pos, elapsed) --print("node_timer2") local nvm = techage.get_nvm(pos) local outdir = M(pos):get_int("outdir") - nvm.provided = power.provide_power(pos, Axle, outdir, PWR_PERF) - local data = power.get_storage_data(pos, Axle, outdir) - if data then - nvm.load = data.level * PWR_PERF * 4 - end + nvm.provided = power.provide_power(pos, Axle, outdir, PWR_PERF, nvm.termpoint1, nvm.termpoint2) + nvm.load = power.get_storage_load(pos, Axle, outdir, PWR_PERF) if techage.is_activeformspec(pos) then - M(pos):set_string("formspec", formspec2(State2, pos, nvm)) + M(pos):set_string("formspec", formspec(State2, pos, nvm)) end return true end ---local function node_timer3(pos, elapsed) --- --print("node_timer3") --- local nvm = techage.get_nvm(pos) --- local outdir = M(pos):get_int("outdir") --- nvm.provided = power.generator_alive(pos, Pipe, CYCLE_TIME, outdir) --- if techage.is_activeformspec(pos) then --- M(pos):set_string("formspec", formspec3(State3, pos, nvm)) --- end --- return true ---end - -local function node_timer4(pos, elapsed) +local function node_timer3(pos, elapsed) --print("node_timer4") local nvm = techage.get_nvm(pos) local outdir = M(pos):get_int("outdir") - nvm.provided = power.provide_power(pos, Cable, outdir, PWR_PERF) - local data = power.get_storage_data(pos, Cable, outdir) - if data then - nvm.load = data.level * PWR_PERF - end + nvm.provided = power.provide_power(pos, Cable, outdir, PWR_PERF, nvm.termpoint1, nvm.termpoint2) + nvm.load = power.get_storage_load(pos, Cable, outdir, PWR_PERF) if techage.is_activeformspec(pos) then - M(pos):set_string("formspec", formspec4(State4, pos, nvm)) + M(pos):set_string("formspec", formspec(State3, pos, nvm)) end return true end @@ -176,66 +117,46 @@ local function on_receive_fields2(pos, formname, fields, player) end local nvm = techage.get_nvm(pos) State2:state_button_event(pos, nvm, fields) - M(pos):set_string("formspec", formspec2(State2, pos, nvm)) + techage.evaluate_charge_termination(nvm, fields) + M(pos):set_string("formspec", formspec(State2, pos, nvm)) end ---local function on_receive_fields3(pos, formname, fields, player) --- if minetest.is_protected(pos, player:get_player_name()) then --- return --- end --- local nvm = techage.get_nvm(pos) --- State3:state_button_event(pos, nvm, fields) --- M(pos):set_string("formspec", formspec3(State3, pos, nvm)) ---end - -local function on_receive_fields4(pos, formname, fields, player) +local function on_receive_fields3(pos, formname, fields, player) if minetest.is_protected(pos, player:get_player_name()) then return end local nvm = techage.get_nvm(pos) - State4:state_button_event(pos, nvm, fields) - M(pos):set_string("formspec", formspec4(State4, pos, nvm)) + State3:state_button_event(pos, nvm, fields) + techage.evaluate_charge_termination(nvm, fields) + M(pos):set_string("formspec", formspec(State3, pos, nvm)) end local function on_rightclick2(pos, node, clicker) techage.set_activeformspec(pos, clicker) local nvm = techage.get_nvm(pos) - M(pos):set_string("formspec", formspec2(State2, pos, nvm)) + M(pos):set_string("formspec", formspec(State2, pos, nvm)) end ---local function on_rightclick3(pos, node, clicker) --- techage.set_activeformspec(pos, clicker) --- local nvm = techage.get_nvm(pos) --- M(pos):set_string("formspec", formspec3(State3, pos, nvm)) ---end - -local function on_rightclick4(pos, node, clicker) +local function on_rightclick3(pos, node, clicker) techage.set_activeformspec(pos, clicker) local nvm = techage.get_nvm(pos) - M(pos):set_string("formspec", formspec4(State4, pos, nvm)) + M(pos):set_string("formspec", formspec(State3, pos, nvm)) end local function after_place_node2(pos) local nvm = techage.get_nvm(pos) State2:node_init(pos, nvm, "") M(pos):set_int("outdir", networks.side_to_outdir(pos, "R")) - M(pos):set_string("formspec", formspec2(State2, pos, nvm)) + M(pos):set_string("formspec", formspec(State2, pos, nvm)) Axle:after_place_node(pos) end ---local function after_place_node3(pos) --- local nvm = techage.get_nvm(pos) --- State3:node_init(pos, nvm, "") --- M(pos):set_int("outdir", networks.side_to_outdir(pos, "R")) --- M(pos):set_string("formspec", formspec3(State3, pos, nvm)) --- Pipe:after_place_node(pos) ---end - -local function after_place_node4(pos) +local function after_place_node3(pos) local nvm = techage.get_nvm(pos) - State4:node_init(pos, nvm, "") + local number = techage.add_node(pos, "techage:t4_source") + State3:node_init(pos, nvm, number) M(pos):set_int("outdir", networks.side_to_outdir(pos, "R")) - M(pos):set_string("formspec", formspec4(State4, pos, nvm)) + M(pos):set_string("formspec", formspec(State3, pos, nvm)) Cable:after_place_node(pos) end @@ -244,12 +165,7 @@ local function after_dig_node2(pos, oldnode) techage.del_mem(pos) end ---local function after_dig_node3(pos, oldnode) --- Pipe:after_dig_node(pos) --- techage.del_mem(pos) ---end - -local function after_dig_node4(pos, oldnode) +local function after_dig_node3(pos, oldnode) Cable:after_dig_node(pos) techage.del_mem(pos) end @@ -257,9 +173,7 @@ end local function get_generator_data(pos, tlib2) local nvm = techage.get_nvm(pos) if nvm.generating then - return {level = (nvm.load or 0) / PWR_PERF, capa = PWR_PERF * 4} - else - return {level = 0, capa = PWR_PERF * 4} + return {level = (nvm.load or 0) / PWR_PERF, perf = PWR_PERF, capa = PWR_PERF * 2} end end @@ -286,29 +200,6 @@ minetest.register_node("techage:t2_source", { get_generator_data = get_generator_data, }) ---minetest.register_node("techage:t3_source", { --- description = S("Steam Power Source"), --- tiles = { --- -- up, down, right, left, back, front --- "techage_filling_ta3.png^techage_frame_ta3_top.png", --- "techage_filling_ta3.png^techage_frame_ta3.png", --- "techage_filling_ta3.png^techage_steam_hole.png^techage_frame_ta3.png", --- "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_source.png", --- "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_source.png", --- "techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_source.png", --- }, --- paramtype2 = "facedir", --- groups = {cracky=2, crumbly=2, choppy=2}, --- on_rotate = screwdriver.disallow, --- is_ground_content = false, --- on_receive_fields = on_receive_fields3, --- on_rightclick = on_rightclick3, --- on_timer = node_timer3, --- after_place_node = after_place_node3, --- after_dig_node = after_dig_node3, --- get_generator_data = get_generator_data, ---}) - minetest.register_node("techage:t4_source", { description = S("Ele Power Source"), tiles = { @@ -324,14 +215,47 @@ minetest.register_node("techage:t4_source", { groups = {cracky=2, crumbly=2, choppy=2}, on_rotate = screwdriver.disallow, is_ground_content = false, - on_receive_fields = on_receive_fields4, - on_rightclick = on_rightclick4, - on_timer = node_timer4, - after_place_node = after_place_node4, - after_dig_node = after_dig_node4, + on_receive_fields = on_receive_fields3, + on_rightclick = on_rightclick3, + on_timer = node_timer3, + after_place_node = after_place_node3, + after_dig_node = after_dig_node3, get_generator_data = get_generator_data, }) power.register_nodes({"techage:t2_source"}, Axle, "gen", {"R"}) ---power.register_nodes({"techage:t3_source"}, Pipe, "gen", {"R"}) power.register_nodes({"techage:t4_source"}, Cable, "gen", {"R"}) + +techage.register_node({"techage:t4_source"}, { + on_recv_message = function(pos, src, topic, payload) + local nvm = techage.get_nvm(pos) + if topic == "delivered" then + return nvm.provided or 0 + elseif topic == "info" then + + else + return State:on_receive_message(pos, topic, payload) + end + end, +}) + +control.register_nodes({"techage:t4_source"}, { + on_receive = function(pos, tlib2, topic, payload) + end, + on_request = function(pos, tlib2, topic) + if topic == "info" then + local nvm = techage.get_nvm(pos) + return { + type = S("Ele Power Source"), + number = M(pos):get_string("node_number") or "", + running = nvm.generating or false, + available = PWR_PERF, + provided = nvm.provided or 0, + termpoint = nvm.termpoint or "unknown", + } + end + return false + end, + } +) + diff --git a/coal_power_station/generator.lua b/coal_power_station/generator.lua index e8c6e8c..6216363 100644 --- a/coal_power_station/generator.lua +++ b/coal_power_station/generator.lua @@ -3,7 +3,7 @@ TechAge ======= - Copyright (C) 2019-2020 Joachim Stolberg + Copyright (C) 2019-2021 Joachim Stolberg AGPL v3 See LICENSE.txt for more information @@ -22,19 +22,12 @@ local CYCLE_TIME = 2 local PWR_CAPA = 80 local Cable = techage.ElectricCable -local power = techage.power -local networks = techage.networks +local power = networks.power +local control = networks.control local function formspec(self, pos, nvm) - return "size[4,4]".. - "box[0,-0.1;3.8,0.5;#c6e8ff]".. - "label[1,-0.1;"..minetest.colorize( "#000000", S("Generator")).."]".. - default.gui_bg.. - default.gui_bg_img.. - default.gui_slots.. - power.formspec_label_bar(pos, 0, 0.8, S("power"), PWR_CAPA, nvm.provided).. - "image_button[2.8,2;1,1;".. self:get_state_button_image(nvm) ..";state_button;]".. - "tooltip[2.8,2;1,1;"..self:get_state_tooltip(nvm).."]" + return techage.generator_formspec(self, pos, nvm, S("Generator"), + nvm.provided, PWR_CAPA, nvm.running) end local function transfer_turbine(pos, topic, payload) @@ -48,14 +41,14 @@ end local function start_node(pos, nvm, state) local outdir = M(pos):get_int("outdir") - power.generator_start(pos, Cable, CYCLE_TIME, outdir) + power.start_storage_calc(pos, Cable, outdir) transfer_turbine(pos, "start") nvm.running = true end local function stop_node(pos, nvm, state) local outdir = M(pos):get_int("outdir") - power.generator_stop(pos, Cable, outdir) + power.start_storage_calc(pos, Cable, outdir) nvm.provided = 0 transfer_turbine(pos, "stop") nvm.running = false @@ -82,7 +75,8 @@ local function node_timer(pos, elapsed) transfer_turbine(pos, "stop") else local outdir = M(pos):get_int("outdir") - nvm.provided = power.generator_alive(pos, Cable, CYCLE_TIME, outdir) + nvm.provided = power.provide_power(pos, Cable, outdir, PWR_CAPA, nvm.termpoint1, nvm.termpoint2) + nvm.load = power.get_storage_load(pos, Cable, outdir, PWR_CAPA) State:keep_running(pos, nvm, COUNTDOWN_TICKS) end if techage.is_activeformspec(pos) then @@ -97,6 +91,7 @@ local function on_receive_fields(pos, formname, fields, player) end local nvm = techage.get_nvm(pos) State:state_button_event(pos, nvm, fields) + techage.evaluate_charge_termination(nvm, fields) end local function on_rightclick(pos, node, clicker) @@ -119,18 +114,13 @@ local function after_dig_node(pos, oldnode) techage.del_mem(pos) end -local function tubelib2_on_update2(pos, outdir, tlib2, node) - power.update_network(pos, outdir, tlib2) +local function get_generator_data(pos, tlib2) + local nvm = techage.get_nvm(pos) + if nvm.running then + return {level = (nvm.load or 0) / PWR_CAPA, perf = PWR_CAPA, capa = PWR_CAPA * 2} + end end -local net_def = { - ele1 = { - sides = {R = 1}, - ntype = "gen1", - nominal = PWR_CAPA, - }, -} - minetest.register_node("techage:generator", { description = S("TA3 Generator"), tiles = { @@ -148,8 +138,7 @@ minetest.register_node("techage:generator", { on_timer = node_timer, after_place_node = after_place_node, after_dig_node = after_dig_node, - tubelib2_on_update2 = tubelib2_on_update2, - networks = net_def, + get_generator_data = get_generator_data, paramtype2 = "facedir", groups = {cracky=2, crumbly=2, choppy=2}, @@ -193,8 +182,7 @@ minetest.register_node("techage:generator_on", { on_timer = node_timer, after_place_node = after_place_node, after_dig_node = after_dig_node, - tubelib2_on_update2 = tubelib2_on_update2, - networks = net_def, + get_generator_data = get_generator_data, drop = "", paramtype2 = "facedir", @@ -205,7 +193,7 @@ minetest.register_node("techage:generator_on", { sounds = default.node_sound_wood_defaults(), }) -Cable:add_secondary_node_names({"techage:generator", "techage:generator_on"}) +power.register_nodes({"techage:generator", "techage:generator_on"}, Cable, "gen", {"R"}) -- controlled by the turbine techage.register_node({"techage:generator", "techage:generator_on"}, { @@ -230,6 +218,27 @@ techage.register_node({"techage:generator", "techage:generator_on"}, { end, }) +-- used by power terminal +control.register_nodes({"techage:generator", "techage:generator_on"}, { + on_receive = function(pos, tlib2, topic, payload) + end, + on_request = function(pos, tlib2, topic) + if topic == "info" then + local nvm = techage.get_nvm(pos) + return { + type = S("TA3 Generator"), + number = M(pos):get_string("node_number") or "", + running = nvm.running or false, + available = PWR_CAPA, + provided = nvm.provided or 0, + termpoint = nvm.termpoint or "unknown", + } + end + return false + end, + } +) + minetest.register_craft({ output = "techage:generator", recipe = { diff --git a/coal_power_station/oilfirebox.lua b/coal_power_station/oilfirebox.lua index e3341dc..3ccb5d3 100644 --- a/coal_power_station/oilfirebox.lua +++ b/coal_power_station/oilfirebox.lua @@ -116,40 +116,40 @@ minetest.register_node("techage:oilfirebox", { end end, - liquid = { - capa = fuel.CAPACITY, - fuel_cat = fuel.BT_BITUMEN, - peek = liquid.srv_peek, - put = function(pos, indir, name, amount) - if fuel.valid_fuel(name, fuel.BT_BITUMEN) then - local leftover = liquid.srv_put(pos, indir, name, amount) - local nvm = techage.get_nvm(pos) - nvm.liquid = nvm.liquid or {} - nvm.liquid.amount = nvm.liquid.amount or 0 - start_firebox(pos, nvm) - if techage.is_activeformspec(pos) then - local nvm = techage.get_nvm(pos) - M(pos):set_string("formspec", fuel.formspec(nvm)) - end - return leftover - end - return amount - end, - take = function(pos, indir, name, amount) - amount, name = liquid.srv_take(pos, indir, name, amount) - if techage.is_activeformspec(pos) then - local nvm = techage.get_nvm(pos) - M(pos):set_string("formspec", fuel.formspec(nvm)) - end - return amount, name - end - }, - networks = { - pipe2 = { - sides = techage.networks.AllSides, -- Pipe connection sides - ntype = "tank", - }, - }, +-- liquid = { TODO +-- capa = fuel.CAPACITY, +-- fuel_cat = fuel.BT_BITUMEN, +-- peek = liquid.srv_peek, +-- put = function(pos, indir, name, amount) +-- if fuel.valid_fuel(name, fuel.BT_BITUMEN) then +-- local leftover = liquid.srv_put(pos, indir, name, amount) +-- local nvm = techage.get_nvm(pos) +-- nvm.liquid = nvm.liquid or {} +-- nvm.liquid.amount = nvm.liquid.amount or 0 +-- start_firebox(pos, nvm) +-- if techage.is_activeformspec(pos) then +-- local nvm = techage.get_nvm(pos) +-- M(pos):set_string("formspec", fuel.formspec(nvm)) +-- end +-- return leftover +-- end +-- return amount +-- end, +-- take = function(pos, indir, name, amount) +-- amount, name = liquid.srv_take(pos, indir, name, amount) +-- if techage.is_activeformspec(pos) then +-- local nvm = techage.get_nvm(pos) +-- M(pos):set_string("formspec", fuel.formspec(nvm)) +-- end +-- return amount, name +-- end +-- }, +-- networks = { +-- pipe2 = { +-- sides = techage.networks.AllSides, -- Pipe connection sides +-- ntype = "tank", +-- }, +-- }, }) Pipe:add_secondary_node_names({"techage:oilfirebox"}) diff --git a/init.lua b/init.lua index b48f1d9..e87e46c 100644 --- a/init.lua +++ b/init.lua @@ -107,8 +107,8 @@ dofile(MP.."/power/gearbox.lua") dofile(MP.."/power/steam_pipe.lua") dofile(MP.."/power/electric_cable.lua") dofile(MP.."/power/junctionbox.lua") ---dofile(MP.."/power/power_terminal.lua") ---dofile(MP.."/power/power_terminal2.lua") +dofile(MP.."/power/power_terminal.lua") +dofile(MP.."/power/power_terminal2.lua") dofile(MP.."/power/powerswitchbox_legacy.lua") dofile(MP.."/power/powerswitchbox.lua") dofile(MP.."/power/powerswitch.lua") @@ -153,18 +153,18 @@ dofile(MP.."/basic_machines/pusher.lua") dofile(MP.."/basic_machines/legacy_nodes.lua") dofile(MP.."/basic_machines/mods_support.lua") dofile(MP.."/basic_machines/grinder.lua") ---dofile(MP.."/basic_machines/distributor.lua") ---dofile(MP.."/basic_machines/gravelsieve.lua") ---dofile(MP.."/basic_machines/gravelrinser.lua") +dofile(MP.."/basic_machines/distributor.lua") +dofile(MP.."/basic_machines/gravelsieve.lua") +dofile(MP.."/basic_machines/gravelrinser.lua") dofile(MP.."/basic_machines/chest.lua") ---dofile(MP.."/basic_machines/autocrafter.lua") ---dofile(MP.."/basic_machines/electronic_fab.lua") ---dofile(MP.."/basic_machines/liquidsampler.lua") ---dofile(MP.."/basic_machines/quarry.lua") ---dofile(MP.."/basic_machines/ta4_chest.lua") ---dofile(MP.."/basic_machines/ta4_injector.lua") ---dofile(MP.."/basic_machines/itemsource.lua") ---dofile(MP.."/basic_machines/recycler.lua") +dofile(MP.."/basic_machines/autocrafter.lua") +dofile(MP.."/basic_machines/electronic_fab.lua") +dofile(MP.."/basic_machines/liquidsampler.lua") +dofile(MP.."/basic_machines/quarry.lua") +dofile(MP.."/basic_machines/ta4_chest.lua") +dofile(MP.."/basic_machines/ta4_injector.lua") +dofile(MP.."/basic_machines/itemsource.lua") +dofile(MP.."/basic_machines/recycler.lua") --dofile(MP.."/basic_machines/concentrator.lua") -- Liquids II @@ -176,13 +176,13 @@ dofile(MP.."/basic_machines/chest.lua") --dofile(MP.."/liquids/waterinlet.lua") -- Coal power station ---dofile(MP.."/coal_power_station/firebox.lua") ---dofile(MP.."/coal_power_station/boiler_base.lua") ---dofile(MP.."/coal_power_station/boiler_top.lua") ---dofile(MP.."/coal_power_station/generator.lua") ---dofile(MP.."/coal_power_station/turbine.lua") ---dofile(MP.."/coal_power_station/cooler.lua") ---dofile(MP.."/coal_power_station/oilfirebox.lua") +dofile(MP.."/coal_power_station/firebox.lua") +dofile(MP.."/coal_power_station/boiler_base.lua") +dofile(MP.."/coal_power_station/boiler_top.lua") +dofile(MP.."/coal_power_station/generator.lua") +dofile(MP.."/coal_power_station/turbine.lua") +dofile(MP.."/coal_power_station/cooler.lua") +dofile(MP.."/coal_power_station/oilfirebox.lua") -- Industrial Furnace --dofile(MP.."/furnace/firebox.lua") @@ -200,18 +200,18 @@ dofile(MP.."/tools/trowel.lua") --dofile(MP.."/basic_machines/forceload.lua") -- Lamps ---dofile(MP.."/lamps/lib.lua") ---dofile(MP.."/lamps/lightblock.lua") ---dofile(MP.."/lamps/simplelamp.lua") ---dofile(MP.."/lamps/streetlamp.lua") ---dofile(MP.."/lamps/streetlamp2.lua") ---dofile(MP.."/lamps/ceilinglamp.lua") ---dofile(MP.."/lamps/industriallamp1.lua") ---dofile(MP.."/lamps/industriallamp2.lua") ---dofile(MP.."/lamps/industriallamp3.lua") ---dofile(MP.."/lamps/industriallamp4.lua") ---dofile(MP.."/lamps/growlight.lua") ---dofile(MP.."/lamps/lampholder.lua") +dofile(MP.."/lamps/lib.lua") +dofile(MP.."/lamps/lightblock.lua") +dofile(MP.."/lamps/simplelamp.lua") +dofile(MP.."/lamps/streetlamp.lua") +dofile(MP.."/lamps/streetlamp2.lua") +dofile(MP.."/lamps/ceilinglamp.lua") +dofile(MP.."/lamps/industriallamp1.lua") +dofile(MP.."/lamps/industriallamp2.lua") +dofile(MP.."/lamps/industriallamp3.lua") +dofile(MP.."/lamps/industriallamp4.lua") +dofile(MP.."/lamps/growlight.lua") +dofile(MP.."/lamps/lampholder.lua") -- Oil --dofile(MP.."/oil/explore.lua") @@ -224,7 +224,7 @@ dofile(MP.."/tools/trowel.lua") -- TA3 power based --dofile(MP.."/ta3_power/tiny_generator.lua") ---dofile(MP.."/ta3_power/akkubox.lua") +dofile(MP.."/ta3_power/akkubox.lua") --dofile(MP.."/ta3_power/axle2power.lua") --dofile(MP.."/ta3_power/power2axle.lua") diff --git a/lamps/growlight.lua b/lamps/growlight.lua index ffbb4b4..f646b36 100644 --- a/lamps/growlight.lua +++ b/lamps/growlight.lua @@ -16,8 +16,10 @@ local S = techage.S local CYCLE_TIME = 2 local RANDOM_VAL = 20 +local PWR_NEEDED = 1 + local Cable = techage.ElectricCable -local power = techage.power +local power = networks.power local Flowers = {} local Plants = {} local Ignore = { ["flowers:waterlily_waving"] = true } @@ -34,8 +36,24 @@ local Positions = { {x = 1, y =-1, z =-1}, } -local function node_timer(pos, elapsed) - power.consumer_alive(pos, Cable, CYCLE_TIME) +local function swap_node(pos, postfix) + local node = techage.get_node_lvm(pos) + local parts = string.split(node.name, "_") + if postfix == parts[2] then + return + end + node.name = parts[1].."_"..postfix + minetest.swap_node(pos, node) + techage.light_ring(pos, postfix == "on") +end + +local function on_nopower(pos) + swap_node(pos, "off") + local nvm = techage.get_nvm(pos) + nvm.turned_on = false +end + +local function grow_flowers(pos) local nvm = techage.get_nvm(pos) local mem = techage.get_mem(pos) mem.grow_pos = mem.grow_pos or {} -- keep the pos blank for some time @@ -67,6 +85,14 @@ local function node_timer(pos, elapsed) end end end +end + +local function node_timer_on(pos, elapsed) + grow_flowers(pos) + local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED) + if consumed < PWR_NEEDED then + on_nopower(pos) + end return true end @@ -95,7 +121,6 @@ techage.register_lamp("techage:growlight", { {-8/16, -8/16, -8/16, 8/16, -13/32, 8/16}, }, }, - on_switch_lamp = on_switch_lamp, high_power = true, },{ description = S("TA4 LED Grow Light"), @@ -116,8 +141,7 @@ techage.register_lamp("techage:growlight", { {-8/16, -8/16, -8/16, 8/16, -13/32, 8/16}, }, }, - on_timer = node_timer, - on_switch_lamp = on_switch_lamp, + on_timer = node_timer_on, high_power = true, }) diff --git a/lamps/lib.lua b/lamps/lib.lua index aa7147b..440c728 100644 --- a/lamps/lib.lua +++ b/lamps/lib.lua @@ -21,7 +21,7 @@ local PWR_NEEDED = 0.5 local CYCLE_TIME = 2 local Cable = techage.ElectricCable -local power = techage.power +local power = networks.power local function swap_node(pos, postfix) local node = techage.get_node_lvm(pos) @@ -53,8 +53,35 @@ local function is_running(pos, nvm) return nvm.turned_on end -local function node_timer(pos, elapsed) - power.consumer_alive(pos, Cable, CYCLE_TIME) +local function node_timer_off1(pos, elapsed) + local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED) + if consumed == PWR_NEEDED then + on_power(pos) + end + return true +end + +local function node_timer_off2(pos, elapsed) + local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED * 2) + if consumed == PWR_NEEDED * 2 then + on_power(pos) + end + return true +end + +local function node_timer_on1(pos, elapsed) + local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED) + if consumed < PWR_NEEDED then + on_nopower(pos) + end + return true +end + +local function node_timer_on2(pos, elapsed) + local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED * 2) + if consumed < PWR_NEEDED * 2 then + on_nopower(pos) + end return true end @@ -66,12 +93,10 @@ local function lamp_on_rightclick(pos, node, clicker) local nvm = techage.get_nvm(pos) if not nvm.turned_on and power.power_available(pos, Cable) then nvm.turned_on = true - power.consumer_start(pos, Cable, CYCLE_TIME) minetest.get_node_timer(pos):start(CYCLE_TIME) swap_node(pos, "on") else nvm.turned_on = false - power.consumer_stop(pos, Cable) minetest.get_node_timer(pos):stop() swap_node(pos, "off") end @@ -107,44 +132,19 @@ local function tubelib2_on_update2(pos, outdir, tlib2, node) power.update_network(pos, outdir, tlib2) end -local net_def = { - ele1 = { - sides = {U=1, D=1, L=1, R=1, F=1, B=1}, -- Cable connection sides - ntype = "con1", - on_power = on_power, - on_nopower = on_nopower, - nominal = PWR_NEEDED, - is_running = is_running, - }, -} - -local net_def2 = { - ele1 = { - sides = {U=1, D=1, L=1, R=1, F=1, B=1}, -- Cable connection sides - ntype = "con1", - on_power = on_power, - on_nopower = on_nopower, - nominal = PWR_NEEDED * 2, - is_running = is_running, - }, -} - - function techage.register_lamp(basename, ndef_off, ndef_on) + if ndef_off.high_power then + ndef_off.on_timer = ndef_off.on_timer or node_timer_off2 + else + ndef_off.on_timer = ndef_off.on_timer or node_timer_off1 + end ndef_off.after_place_node = after_place_node ndef_off.after_dig_node = after_dig_node - ndef_off.tubelib2_on_update2 = tubelib2_on_update2 - if ndef_off.high_power then - ndef_off.networks = net_def2 - else - ndef_off.networks = net_def - end ndef_off.on_rightclick = lamp_on_rightclick if not ndef_off.on_rotate then ndef_off.on_place = on_place end ndef_off.on_rotate = ndef_off.on_rotate or on_rotate - ndef_off.on_timer = node_timer ndef_off.paramtype = "light" ndef_off.use_texture_alpha = techage.CLIP ndef_off.light_source = 0 @@ -154,17 +154,15 @@ function techage.register_lamp(basename, ndef_off, ndef_on) ndef_off.is_ground_content = false ndef_off.sounds = default.node_sound_glass_defaults() + if ndef_on.high_power then + ndef_on.on_timer = ndef_on.on_timer or node_timer_on2 + else + ndef_on.on_timer = ndef_on.on_timer or node_timer_on1 + end ndef_on.after_place_node = after_place_node ndef_on.after_dig_node = after_dig_node - ndef_on.tubelib2_on_update2 = tubelib2_on_update2 - if ndef_on.high_power then - ndef_on.networks = net_def2 - else - ndef_on.networks = net_def - end ndef_on.on_rightclick = lamp_on_rightclick ndef_on.on_rotate = ndef_on.on_rotate or on_rotate - ndef_on.on_timer = ndef_on.on_timer or node_timer ndef_on.paramtype = "light" ndef_on.use_texture_alpha = techage.CLIP ndef_on.light_source = minetest.LIGHT_MAX @@ -178,6 +176,6 @@ function techage.register_lamp(basename, ndef_off, ndef_on) minetest.register_node(basename.."_off", ndef_off) minetest.register_node(basename.."_on", ndef_on) - Cable:add_secondary_node_names({basename.."_off", basename.."_on"}) + power.register_nodes({basename.."_off", basename.."_on"}, Cable, "con") end diff --git a/power/formspecs.lua b/power/formspecs.lua index ade60b9..5abddeb 100644 --- a/power/formspecs.lua +++ b/power/formspecs.lua @@ -15,86 +15,216 @@ --local P2S = minetest.pos_to_string --local M = minetest.get_meta --local N = function(pos) return minetest.get_node(pos).name end ---local S = techage.S +local S = techage.S + +local CYCLE_TIME = 2 +local CYCLES_PER_DAY = 20 * 60 / CYCLE_TIME + local in_range = techage.in_range local power = networks.power techage.power = {} +-- Charge termination areas +local Cp2Idx = {["40% - 60%"] = 1, ["60% - 80%"] = 2, ["80% - 100%"] = 3} + + +------------------------------------------------------------------------------- +-- Helper function +------------------------------------------------------------------------------- local function round(val) - return math.floor((val * 10) + 0.5) / 10 -end - -function techage.power.percent(max_val, curr_val) - return math.min(math.ceil(((curr_val or 0) * 100) / (max_val or 1.0)), 100) -end - -function techage.power.formspec_load_bar(charging, max_val) - local percent - charging = charging or 0 - max_val = max_val or 1 - if charging ~= 0 then - percent = 50 + math.ceil((charging * 50.0) / max_val) + if val > 100 then + return math.floor(val + 0.5) + elseif val > 10 then + return math.floor((val * 10) + 0.5) / 10 + else + return math.floor((val * 100) + 0.5) / 100 end +end +local function calc_percent(max_val, curr_val) + return math.min(((curr_val or 0) * 100) / (max_val or 1.0), 100) +end + +------------------------------------------------------------------------------- +-- Local bar functions +------------------------------------------------------------------------------- +-- charging > 0 ==> charging +-- charging < 0 ==> uncharging +-- charging = 0 ==> off +-- percent: 0..100 +local function charging_bar(charging, percent) if charging > 0 then - return "techage_form_level_off.png^[lowpart:"..percent..":techage_form_level_charge.png" + return "techage_form_level_off.png^[lowpart:" .. percent .. ":techage_form_level_charge.png" elseif charging < 0 then - return "techage_form_level_unload.png^[lowpart:"..percent..":techage_form_level_off.png" + return "techage_form_level_unload.png^[lowpart:" .. percent .. ":techage_form_level_off.png" else return "techage_form_level_off.png" end end -function techage.power.formspec_power_bar(max_power, current_power) - if (current_power or 0) == 0 then - return "techage_form_level_bg.png" - end - local percent = techage.power.percent(max_power, current_power) - percent = (percent + 5) / 1.22 -- texture correction - return "techage_form_level_bg.png^[lowpart:"..percent..":techage_form_level_fg.png" -end - -function techage.power.formspec_label_bar(pos, x, y, tlib2, label, max_power, current_power, unit) +local function power_bar(current_power, max_power) local percent, ypos - - max_power = max_power or 1 - current_power = round(current_power or 0) - unit = unit or "ku" - - if current_power > 0 then - -- check if power network is overloaded - if not power.power_available(pos, tlib2) then - return "container["..x..","..y.."]".. - "box[0,0;2.3,3.3;#395c74]".. - "label[0.2,0;"..label.."]".. - "label[0.7,0.4;"..max_power.." "..unit.."]".. - "image[0,0.5;1,3;techage_form_level_red_fg.png]".. - "container_end[]" - end - end + + current_power = current_power or 0 if current_power == 0 then percent = 0 ypos = 2.8 else - percent = techage.power.percent(max_power, current_power) + percent = calc_percent(max_power, current_power) -- 0.4 to 2.8 = 2.4 local offs = 2.4 - (current_power / max_power) * 2.4 ypos = 0.4 + in_range(offs, 0.4, 2.4) end - if current_power >= 100 then - current_power = math.floor(current_power) - end + current_power = round(current_power) + max_power = round(max_power) percent = (percent + 5) / 1.1 -- texture correction + + return "label[0.7,0.4;" .. max_power .. " ku]" .. + "image[0,0.5;1,3;" .. + "techage_form_level_bg.png^[lowpart:" .. percent .. + ":techage_form_level_fg.png]" .. + "label[0.7," .. ypos .. ";" .. current_power .. " ku]" +end + +local function storage_bar(current_power, max_power) + local percent, ypos + max_power = (max_power or 0) / CYCLES_PER_DAY + current_power = (current_power or 0) / CYCLES_PER_DAY + + if current_power == 0 then + percent = 0 + ypos = 2.8 + else + percent = calc_percent(max_power, current_power) + -- 0.4 to 2.8 = 2.4 + local offs = 2.4 - (current_power / max_power) * 2.4 + ypos = 0.4 + in_range(offs, 0.4, 2.4) + end + current_power = round(current_power) + max_power = round(max_power) + + local percent2 = (percent + 5) / 1.1 -- texture correction + return "label[0.7,0.4;" .. max_power .. " kud]" .. + "image[0,0.5;1,3;".. + "techage_form_level_bg.png^[lowpart:" .. percent2 .. + ":techage_form_level_fg.png]" .. + "label[0.7," .. ypos .. ";" .. round(percent) .. " %]" +end + +------------------------------------------------------------------------------- +-- API bar functions +------------------------------------------------------------------------------- +function techage.formspec_power_bar(pos, x, y, label, current_power, max_power) return "container["..x..","..y.."]".. "box[0,0;2.3,3.3;#395c74]".. "label[0.2,0;"..label.."]".. - "label[0.7,0.4;"..max_power.." "..unit.."]".. - "image[0,0.5;1,3;".. - "techage_form_level_bg.png^[lowpart:"..percent.. - ":techage_form_level_fg.png]".. - "label[0.7,"..ypos..";"..current_power.." "..unit.."]".. + power_bar(current_power, max_power).. "container_end[]" - end +function techage.formspec_charging_bar(pos, x, y, label, data) + local charging = 0 + local percent = 0 + local consumed = 0 + local available = 0 + + if data then + charging = data.provided - data.consumed + consumed = data.consumed + available = data.available + if charging > 0 then + percent = 50 + (charging / data.available * 50) + elseif charging < 0 then + percent = 50 + (charging / data.consumed * 50) + end + end + + return "container[".. x .. "," .. y .. "]" .. + "box[0,0;2.3,3.3;#395c74]" .. + "label[0.2,0;" .. label .. "]" .. + "label[0.7,0.4;" .. available .. " ku]" .. + "image[0,0.5;1,3;" .. charging_bar(charging, percent) .. "]" .. + "label[0.7,2.8;" .. consumed .. " ku]" .. + "container_end[]" +end + +function techage.formspec_storage_bar(pos, x, y, label, curr_load, max_load) + curr_load = curr_load or 0 + + return "container[" .. x .. "," .. y .. "]" .. + "box[0,0;2.3,3.3;#395c74]" .. + "label[0.2,0;" .. label .. "]" .. + storage_bar(curr_load, max_load) .. + "container_end[]" +end + +function techage.formspec_charge_termination(pos, x, y, label, value, running) + local idx = Cp2Idx[value] or 2 + value = value or 0 + + if running then + return "container[" .. x .. "," .. y .. "]" .. + "box[0,0;3.2,1.5;#395c74]" .. + "label[0.2,0;" .. label .. "]" .. + "box[0.2,0.6;2.7,0.7;#000000]" .. + "label[0.3,0.75;" .. value .. "]" .. + "container_end[]" + else + return "container[" .. x .. "," .. y .. "]" .. + "box[0,0;3.2,1.5;#395c74]" .. + "label[0.2,0;" .. label .. "]" .. + "dropdown[0.2,0.6;3.0;termpoint;40% - 60%,60% - 80%,80% - 100%;" .. idx .. "]" .. + "container_end[]" + end +end + +------------------------------------------------------------------------------- +-- API formspec functions +------------------------------------------------------------------------------- +function techage.storage_formspec(self, pos, nvm, label, netw_data, curr_load, max_load) + return "size[6.3,4]" .. + default.gui_bg .. + default.gui_bg_img .. + default.gui_slots .. + "box[0,-0.1;6.1,0.5;#c6e8ff]" .. + "label[0.2,-0.1;" .. minetest.colorize( "#000000", label) .. "]" .. + techage.formspec_charging_bar(pos, 0.0, 0.8, S("Charging"), netw_data) .. + techage.formspec_storage_bar (pos, 3.8, 0.8, S("Storage"), curr_load, max_load) .. + "image_button[2.7,2;1,1;" .. self:get_state_button_image(nvm) .. ";state_button;]" .. + "tooltip[2.7,2;1,1;" .. self:get_state_tooltip(nvm) .. "]" +end + +function techage.generator_formspec(self, pos, nvm, label, provided, max_available, running) + return "size[6,4]" .. + default.gui_bg .. + default.gui_bg_img .. + default.gui_slots .. + "box[0,-0.1;5.8,0.5;#c6e8ff]" .. + "label[0.2,-0.1;" .. minetest.colorize( "#000000", label) .. "]" .. + techage.formspec_power_bar(pos, 0, 0.8, S("power"), provided, max_available) .. + "image_button[3.8,2.9;1,1;" .. self:get_state_button_image(nvm) .. ";state_button;]" .. + "tooltip[3.8,2.9;1,1;" .. self:get_state_tooltip(nvm) .. "]" .. + techage.formspec_charge_termination(pos, 2.6, 0.8, S("Charge termination"), nvm.termpoint, running) +end + +function techage.evaluate_charge_termination(nvm, fields) + if fields.termpoint and not nvm.running then + nvm.termpoint = fields.termpoint + if fields.termpoint == "40% - 60%" then + nvm.termpoint1 = 0.4 + nvm.termpoint2 = 0.6 + elseif fields.termpoint == "60% - 80%" then + nvm.termpoint1 = 0.6 + nvm.termpoint2 = 0.8 + elseif fields.termpoint == "80% - 100%" then + nvm.termpoint1 = 0.8 + nvm.termpoint2 = 1.0 + end + return true + end +end + +techage.power.percent = calc_percent +techage.CYCLES_PER_DAY = CYCLES_PER_DAY +techage.round = round \ No newline at end of file diff --git a/power/power_terminal.lua b/power/power_terminal.lua index 50b24b6..2d882ce 100644 --- a/power/power_terminal.lua +++ b/power/power_terminal.lua @@ -12,210 +12,4 @@ ]]-- --- 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 -local power = techage.power -local networks = techage.networks -local STOPPED = techage.power.STOPPED -local NOPOWER = techage.power.NOPOWER -local RUNNING = techage.power.RUNNING - -local function generator_data(gen_tbl) - local tbl = { - pow_all=0, pow_on=0, pow_act=0, pow_used=0, - num_on=0, num_act=0, num_used=0 - } - for i,gen in ipairs(gen_tbl or {}) do - local nvm = techage.get_nvm(gen.pos) - tbl.pow_all = tbl.pow_all + (gen.nominal or 0) - if nvm.ele1 and nvm.ele1.gstate and nvm.ele1.gstate ~= STOPPED then - tbl.num_on = tbl.num_on + 1 - tbl.pow_on = tbl.pow_on + (nvm.ele1.curr_power or gen.nominal or 0) - if (nvm.ele1.galive or -1) >= 0 then - tbl.num_act = tbl.num_act + 1 - tbl.pow_act = tbl.pow_act + (nvm.ele1.curr_power or gen.nominal or 0) - if (nvm.ele1.given or 0) > 0 then - tbl.num_used = tbl.num_used + 1 - tbl.pow_used = tbl.pow_used + (nvm.ele1.given or 0) - end - end - end - end - - tbl.num_all = #(gen_tbl or {}) - return tbl -end - -local function consumer_data(con_tbl) - local tbl = { - pow_all=0, pow_on=0, pow_act=0, pow_used=0, - num_on=0, num_act=0, num_used=0 - } - for i,gen in ipairs(con_tbl or {}) do - local nvm = techage.get_nvm(gen.pos) - tbl.pow_all = tbl.pow_all + (gen.nominal or 0) - if nvm.ele1 and nvm.ele1.cstate and nvm.ele1.cstate ~= STOPPED then - tbl.num_on = tbl.num_on + 1 - tbl.pow_on = tbl.pow_on + (gen.nominal or 0) - if (nvm.ele1.calive or -1) >= 0 then - tbl.num_act = tbl.num_act + 1 - tbl.pow_act = tbl.pow_act + (gen.nominal or 0) - if (nvm.ele1.taken or 0) > 0 then - tbl.num_used = tbl.num_used + 1 - tbl.pow_used = tbl.pow_used + (nvm.ele1.taken or 0) - end - end - end - end - - tbl.num_all = #(con_tbl or {}) - return tbl -end - -local function calc_network_data(pos, nvm) - local netw = techage.networks.has_network("ele1", nvm.ele1 and nvm.ele1.netID) or {} - local gen1 = generator_data(netw.gen1) - local gen2 = generator_data(netw.gen2) - local con1 = consumer_data(netw.con1) - local con2 = consumer_data(netw.con2) - - return netw, gen1, gen2, con1, con2 -end - -local function column(x,y, data) - return - "label["..x..","..(y+0.0)..";"..data.num_all.. " ("..data.pow_all.." ku)]".. - "label["..x..","..(y+0.5)..";"..data.num_on.. " ("..data.pow_on.." ku)]".. - "label["..x..","..(y+1.0)..";"..data.num_act.. " ("..data.pow_act.." ku)]".. - "label["..x..","..(y+1.5)..";"..data.num_used.." ("..data.pow_used.." ku)]" -end - -local function get_state(netw, gen1, gen2, con1, con2) - local num_nodes = gen1.num_all + gen2.num_all + con1.num_all + - con2.num_all + (#(netw.junc or {})) + (#(netw.term or {})) - local nload = (gen1.pow_act + gen2.pow_act) / con1.pow_act - local state = S("Number of all nodes")..": ".. num_nodes - if not netw.gen1 and not netw.gen2 then - state = S("No network or active generator available!") - elseif num_nodes > (techage.ELE1_MAX_CABLE_LENGHT - 50) then - state = string.format(S("With %u of a maximum of %u blocks you are almost at the limit!"), - num_nodes, techage.ELE1_MAX_CABLE_LENGHT) - elseif nload <= 1.0 then - state = S("The network is overloaded!") - elseif nload < 1.2 then - state = S("The network load is almost at the limit!") - end - return state -end - -local function formspec(pos, nvm) - local netw, gen1, gen2, con1, con2 = calc_network_data(pos, nvm) - netw.prop = ((netw.prop or 0) + 1) % 2 - local star = netw.prop == 1 and "*" or "" - local state = get_state(netw, gen1, gen2, con1, con2) - - return "size[10,7]".. - default.gui_bg.. - default.gui_bg_img.. - default.gui_slots.. - "box[0,-0.1;9.8,0.5;#c6e8ff]".. - "label[4,-0.1;"..minetest.colorize( "#000000", S("Network Data")).."]".. - "label[9.5,-0.1;"..minetest.colorize( "#000000", star).."]".. - power.formspec_label_bar(pos, 0, 0.7, S("Genera. 1"), gen1.pow_act, gen1.pow_used).. - power.formspec_label_bar(pos, 2.5, 0.7, S("Genera. 2"), gen2.pow_act, gen2.pow_used).. - power.formspec_label_bar(pos, 5, 0.7, S("Consum. 2"), con2.pow_act, con2.pow_used).. - power.formspec_label_bar(pos, 7.5, 0.7, S("Consum. 1"), con1.pow_act, con1.pow_used).. - "box[0,4.3;9.8,0.4;#c6e8ff]".. - "box[0,4.85;9.8,0.4;#395c74]".. - "box[0,5.35;9.8,0.4;#395c74]".. - "box[0,5.85;9.8,0.4;#395c74]".. - "box[0,6.35;9.8,0.4;#395c74]".. - "label[2,4.3;"..minetest.colorize( "#000000", S("Genera. 1")).."]".. - "label[4,4.3;"..minetest.colorize( "#000000", S("Genera. 2")).."]".. - "label[6,4.3;"..minetest.colorize( "#000000", S("Consum. 2")).."]".. - "label[8,4.3;"..minetest.colorize( "#000000", S("Consum. 1")).."]".. - "label[0.1,4.8;"..S("All nodes:").."]".. - "label[0.1,5.3;"..S("Turned on:").."]".. - "label[0.1,5.8;"..S("Active:").."]".. - "label[0.1,6.3;"..S("In use:").."]".. - "box[0,6.95;9.8,0.4;#000000]".. - "label[0.1,6.9;"..state.."]".. - column(2, 4.8, gen1).. - column(4, 4.8, gen2).. - column(6, 4.8, con2).. - column(8, 4.8, con1) -end - -minetest.register_node("techage:power_terminal", { - description = S("TA3 Power Terminal Old"), - 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) - local nvm = techage.get_nvm(pos) - M(pos):set_int("outdir", networks.side_to_outdir(pos, "B")) - Cable:after_place_node(pos) - M(pos):set_string("formspec", formspec(pos, nvm)) - end, - after_dig_node = function(pos) - Cable:after_dig_node(pos) - techage.del_mem(pos) - end, - tubelib2_on_update2 = function(pos, outdir, tlib2, node) - power.update_network(pos, outdir, tlib2) - end, - on_rightclick = function(pos, node, clicker) - techage.set_activeformspec(pos, clicker) - minetest.get_node_timer(pos):start(CYCLE_TIME) - local nvm = techage.get_nvm(pos) - M(pos):set_string("formspec", formspec(pos, nvm)) - end, - on_timer = function(pos, elapsed) - local nvm = techage.get_nvm(pos) - if techage.is_activeformspec(pos) then - M(pos):set_string("formspec", formspec(pos, nvm)) - end - return true - end, - - networks = { - ele1 = { - sides = {B = 1}, -- Cable connection side - ntype = "term", - }, - }, - - paramtype2 = "facedir", - paramtype = "light", - use_texture_alpha = techage.CLIP, - on_rotate = screwdriver.disallow, - sunlight_propagates = true, - is_ground_content = false, - groups = {cracky = 2, level = 2, not_in_creative_inventory = 1}, - sounds = default.node_sound_metal_defaults(), -}) - -Cable:add_secondary_node_names({"techage:power_terminal"}) - +minetest.register_alias_force("techage:power_terminal", "techage:ta3_power_terminal") \ No newline at end of file diff --git a/power/power_terminal2.lua b/power/power_terminal2.lua index cbd142d..e7caf8e 100644 --- a/power/power_terminal2.lua +++ b/power/power_terminal2.lua @@ -3,7 +3,7 @@ TechAge ======= - Copyright (C) 2019-2020 Joachim Stolberg + Copyright (C) 2019-2021 Joachim Stolberg AGPL v3 See LICENSE.txt for more information @@ -22,416 +22,157 @@ local S = techage.S local CYCLE_TIME = 2 local Cable = techage.ElectricCable -local power = techage.power -local networks = techage.networks -local STOPPED = techage.power.STOPPED -local NOPOWER = techage.power.NOPOWER -local RUNNING = techage.power.RUNNING +local power = networks.power +local control = networks.control -local HELP = [[Commands -help print this text -cls clear screen -gen1 print all cat. 1 generators -gen2 print all cat. 2 generators -con2 print all cat. 2 consumers -num print number of network blocks -pow print provided and needed power]] +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 Generators = { - S("Power station"), - S("Tiny generator"), - S("Solar system") , - S("Wind turbine"), - S("Accu Box"), - S("Energy storage"), - S("Fuel cell cat. 1"), - S("Fuel cell cat. 2"), - S("Electrolyzer"), - S("TA2 Generator"), -} - -local Storage = { - [S("Accu Box")] = true, - [S("Energy storage")] = true, - [S("Fuel cell cat. 1")] = true, - [S("Fuel cell cat. 2")] = true, - [S("Electrolyzer")] = true, -} - -local GeneratorPerformances = { - 80, -- S("Power station") - 12, -- S("Tiny generator") - 100, -- S("Solar system") - 70, -- S("Wind turbine") - 10, -- S("Accu Box") - 60, -- S("Energy storage") - 33, -- S("Fuel cell cat. 1") - 34, -- S("Fuel cell cat. 2") - 35, -- S("Electrolyzer") - 24, -- S("TA2 Generator") -} - --- --- Generate the needed tables for the formspec --- -local Gentypes = table.concat(Generators, ",") -local Gentype2Idx = {} -local Gentype2Maxvalue = {} -local Gentype = {} - -for idx,name in ipairs(Generators) do - Gentype2Idx[name] = idx - Gentype2Maxvalue[name] = GeneratorPerformances[idx] - Gentype[GeneratorPerformances[idx]] = name +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 .. "]" end -local function short_node_name(nominal) - return Gentype[nominal or 1] or "unknown" -end -local function generator_data(gen_tbl, nominal) - local pow_max = 0 - local pow_curr = 0 - local num_nodes = 0 - - for i,gen in ipairs(gen_tbl or {}) do - if gen.nominal == nominal then - local nvm = techage.get_nvm(gen.pos) - if nvm.ele1 and nvm.ele1.gstate and nvm.ele1.galive and nvm.ele1.given then - num_nodes = num_nodes + 1 - if nvm.ele1.gstate == RUNNING then - pow_max = pow_max + (nvm.ele1.curr_power or nominal) - if nvm.ele1.galive > 0 and nvm.ele1.given > 0 then - pow_curr = pow_curr + nvm.ele1.given - end - end - end - end - end +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) - return pow_max, pow_curr, num_nodes -end - -local function get_generator_data(gen) - local nvm = techage.get_nvm(gen.pos) - local pow_max = 0 - local pow_curr = 0 - if nvm.ele1 and nvm.ele1.gstate and nvm.ele1.galive and nvm.ele1.given then - if nvm.ele1.gstate == RUNNING then - if nvm.ele1.curr_power and nvm.ele1.curr_power > 0 then - pow_max = nvm.ele1.curr_power - else - pow_max = gen.nominal - end - if nvm.ele1.galive > 0 and nvm.ele1.given > 0 then - pow_curr = nvm.ele1.given - end - end - end - return pow_curr, pow_max -end - -local function get_consumer_data(gen) - local nvm = techage.get_nvm(gen.pos) - local pow_max = 0 - local pow_curr = 0 - if nvm.ele1 and nvm.ele1.cstate and nvm.ele1.calive and nvm.ele1.taken then - if nvm.ele1.cstate == RUNNING then - pow_max = gen.nominal - if nvm.ele1.calive > 0 and nvm.ele1.taken > 0 then - pow_curr = nvm.ele1.taken - end - end - end - return pow_curr, pow_max -end - -local function consumer_data(gen_tbl, nominal) - local pow_max = 0 - local pow_curr = 0 - local num_nodes = 0 - - for i,gen in ipairs(gen_tbl or {}) do - if gen.nominal == nominal then - local nvm = techage.get_nvm(gen.pos) - if nvm.ele1 and nvm.ele1.cstate and nvm.ele1.calive and nvm.ele1.taken then - num_nodes = num_nodes + 1 - if nvm.ele1.cstate == RUNNING then - pow_max = pow_max + nominal - if nvm.ele1.calive > 0 and nvm.ele1.taken > 0 then - pow_curr = pow_curr + nvm.ele1.taken - end - end - end - end - end + 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) - return pow_max, pow_curr, num_nodes -end - -local function storage_load(gen_tbl, nominal) - local load_curr = 0 -- percentage - local num = 0 - - for i,gen in ipairs(gen_tbl or {}) do - if gen.nominal == nominal then - local ndef = techage.NodeDef[techage.get_node_lvm(gen.pos).name] - if ndef and ndef.on_recv_message then - local resp, _ = ndef.on_recv_message(gen.pos, "0", "load") - if type(resp) == "number" then - load_curr = load_curr + resp - num = num + 1 - end - end - end - end - - if num > 0 then - return math.floor(load_curr / num) - else - return 0 - end -end - -local function calc_network_data_type(pos, nvm, gentype) - local pow_max1, pow_curr1, num_nodes1, pow_stored1 - local pow_max2, pow_curr2, num_nodes2, pow_stored2 - local nominal = Gentype2Maxvalue[gentype] - local netw = techage.networks.has_network("ele1", nvm.ele1 and nvm.ele1.netID) or {} - - if gentype == S("Accu Box") or gentype == S("Energy storage") then - pow_max1, pow_curr1, num_nodes1 = generator_data(netw.gen2, nominal) - pow_max2, pow_curr2, num_nodes2 = consumer_data(netw.con2, nominal) - pow_stored1 = storage_load(netw.con2, Gentype2Maxvalue[gentype]).." %" - pow_stored2 = pow_stored1 - elseif gentype == S("Fuel cell cat. 2") then - pow_max1, pow_curr1, num_nodes1 = generator_data(netw.gen2, nominal) - pow_max2, pow_curr2, num_nodes2 = 0, 0, 0 - pow_stored1 = storage_load(netw.gen2, Gentype2Maxvalue[gentype]).." %" - pow_stored2 = "-" - elseif gentype == S("Fuel cell cat. 1") then - pow_max1, pow_curr1, num_nodes1 = generator_data(netw.gen1, nominal) - pow_max2, pow_curr2, num_nodes2 = 0, 0, 0 - pow_stored1 = storage_load(netw.gen1, Gentype2Maxvalue[gentype]).." %" - pow_stored2 = "-" - elseif gentype == S("Electrolyzer") then - pow_max1, pow_curr1, num_nodes1 = 0, 0, 0 - pow_max2, pow_curr2, num_nodes2 = consumer_data(netw.con2, nominal) - pow_stored2 = storage_load(netw.con2, Gentype2Maxvalue[gentype]).." %" - pow_stored1 = "-" - else -- gen1 generators - pow_max1, pow_curr1, num_nodes1 = generator_data(netw.gen1, nominal) - pow_max2, pow_curr2, num_nodes2 = 0, 0, 0 - pow_stored1 = "-" - pow_stored2 = "-" - end - return netw, - {pow_max = pow_max1, pow_curr = pow_curr1, num_nodes = num_nodes1, pow_stored = pow_stored1}, - {pow_max = pow_max2, pow_curr = pow_curr2, num_nodes = num_nodes2, pow_stored = pow_stored2} -end - -local function calc_network_data_total(pos, nvm) - local netw = techage.networks.has_network("ele1", nvm.ele1 and nvm.ele1.netID) or {} - - local pow_max1 = netw.available1 or 0 - local pow_max2 = netw.available2 or 0 - local pow_used1 = netw.on and math.min(netw.needed1 + netw.needed2, netw.available1) or 0 - local pow_used2 = netw.on and math.max(netw.needed1 - pow_used1, -netw.available2) or 0 - local num_nodes1 = #(netw.gen1 or {}) - local num_nodes2 = #(netw.gen2 or {}) - - return netw, - {pow_max = pow_max1, pow_curr = pow_used1, num_nodes = num_nodes1}, - {pow_max = pow_max2, pow_curr = pow_used2, num_nodes = num_nodes2} -end - -local function get_state(netw) - local state = "" - local needed = techage.power.get_con1_sum(netw, "ele1") or 0 - - if #(netw.gen1 or {}) + #(netw.gen2 or {}) == 0 then - state = S("No power grid or running generator!") - elseif needed > (netw.available1 or 0) then - state = S("Probably too many consumers (")..needed.." "..S("ku is needed").."!)" - elseif (netw.num_nodes or 0) < techage.networks.MAX_NUM_NODES then - state = S("Number of power grid blocks")..": "..(netw.num_nodes or 0)..", "..S("Max. needed power")..": "..needed.. " ku" - else - state = S("To many blocks in the power grid!") - end - return state -end - -local function column(x,y, data) - if data.pow_stored then - return - "label["..x..","..(y+0.0)..";"..data.num_nodes.. "]".. - "label["..x..","..(y+0.5)..";"..data.pow_max.. " ku]".. - "label["..x..","..(y+1.0)..";"..data.pow_curr.. " ku]".. - "label["..x..","..(y+1.5)..";"..data.pow_stored.."]" - else - return - "label["..x..","..(y+0.0)..";"..data.num_nodes.. "]".. - "label["..x..","..(y+0.5)..";"..data.pow_max.. " ku]".. - "label["..x..","..(y+1.0)..";"..data.pow_curr.. " ku]" - end -end - -local function formspec_type(pos, nvm) - return "size[5,4]".. - default.gui_bg.. - default.gui_bg_img.. - default.gui_slots.. - "box[0,-0.1;4.8,0.5;#c6e8ff]".. - "label[1.5,-0.1;"..minetest.colorize( "#000000", S("Select type")).."]".. - "dropdown[0,1;5.2;gentype;"..Gentypes..";"..(nvm.gentype_idx or 1).."]".. - "style_type[button;bgcolor=#395c74]".. - "button[0,2.4;5,1;set;"..S("Store").."]" -end - -local function formspec1(pos, nvm) - local gentype = nvm.gentype or S("Power station") - local netw, gen1, gen2 = calc_network_data_type(pos, nvm, gentype) - local _, sum1, sum2 = calc_network_data_total(pos, nvm) - netw.prop = ((netw.prop or 0) + 1) % 2 - local star = netw.prop == 1 and "*" or "" - local state = get_state(netw) - - return "size[11,9]".. + return "size[10,8]".. "tabheader[0,0;tab;status,console;1;;true]".. default.gui_bg.. default.gui_bg_img.. default.gui_slots.. - "box[0,-0.1;10.8,0.5;#c6e8ff]".. - "label[4.5,-0.1;"..minetest.colorize( "#000000", S("Network Data")).."]".. - "label[10.5,-0.1;"..minetest.colorize( "#000000", star).."]".. + "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).. - "style_type[button;bgcolor=#395c74]".. - "button[0,0.7;3,1;config;"..S("Type").."]".. - - "box[0,1.6;10.8,0.4;#c6e8ff]".. - "box[0,2.15;10.8,0.4;#395c74]".. - "box[0,2.65;10.8,0.4;#395c74]".. - "box[0,3.15;10.8,0.4;#395c74]".. - "box[0,3.65;10.8,0.4;#395c74]".. - "label[0.1,1.55;"..minetest.colorize( "#000000", gentype).."]".. - "label[5.7,1.55;"..minetest.colorize( "#000000", S("Output")).."]".. - "label[8.2,1.55;"..minetest.colorize( "#000000", S("Intake")).."]".. - "label[0.1,2.1;"..S("Number blocks:").."]".. - "label[0.1,2.6;"..S("Maximum power:").."]".. - "label[0.1,3.1;"..S("Current power:").."]".. - "label[0.1,3.6;"..S("Energy stored:").."]".. - column(5.7, 2.1, gen1).. - column(8.2, 2.1, gen2).. - - --"box[0,5.3;8.8,0.4;#c6e8ff]".. - "box[0,4.5;10.8,0.4;#c6e8ff]".. - "box[0,5.05;10.8,0.4;#395c74]".. - "box[0,5.55;10.8,0.4;#395c74]".. - "box[0,6.05;10.8,0.4;#395c74]".. - "label[0.1,4.45;"..minetest.colorize( "#000000", S("Power grid total")).."]".. - "label[5.7,4.45;"..minetest.colorize( "#000000", S("Generators")).."]".. - "label[8.2,4.45;"..minetest.colorize( "#000000", S("Storage systems")).."]".. - "label[0.1,5.0;"..S("Number blocks:").."]".. - "label[0.1,5.5;"..S("Maximum power:").."]".. - "label[0.1,6.0;"..S("Current power:").."]".. - column(5.7, 5.0, sum1).. - column(8.2, 5.0, sum2).. - "box[0,7.75;10.8,0.4;#000000]".. - "label[0.1,7.7;"..state.."]" + 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 {})) end -local function formspec2(pos, mem) +local function formspec2(pos) + local mem = techage.get_mem(pos) local meta = M(pos) local output = meta:get_string("output") local command = mem.cmnd or "help" output = minetest.formspec_escape(output) output = output:gsub("\n", ",") - return "size[11,9]".. + return "size[10,8]".. "tabheader[0,0;tab;status,console;2;;true]".. default.gui_bg.. default.gui_bg_img.. default.gui_slots.. - "box[0,-0.1;10.8,0.5;#c6e8ff]".. - "label[4.5,-0.1;"..minetest.colorize( "#000000", S("Network Data")).."]".. - "style_type[table,field;font=mono]".. - "table[0,0.5;10.8,7.8;output;"..output..";200]".. - "field[0.4,8.7;8.6,1;cmnd;;"..command.."]" .. + "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.."]" .. "field_close_on_enter[cmnd;false]".. - "button[8.9,8.4;2,1;enter;"..S("Enter").."]" + "button[7.9,7.4;2,1;enter;"..S("Enter").."]" end -local function generators(pos, gen_tbl) +local function generators(pos) local tbl = {} - for _, item in ipairs(gen_tbl) do - if item and item.pos then - local node = techage.get_node_lvm(item.pos) - local ndef = minetest.registered_nodes[node.name] - local name = short_node_name(item.nominal) - local spos = P2S(item.pos) - local pow_curr, pow_max = get_generator_data(item, ndef) - if Storage[name] then - local load_percent = 0 - local tdef = techage.NodeDef[node.name] - if tdef and tdef.on_recv_message then - load_percent = tdef.on_recv_message(item.pos, "0", "load") or 0 - end - local s = string.format("%-16s %s = %u/%u ku (%u %%)", - spos, name, pow_curr, pow_max, load_percent) - tbl[#tbl + 1] = s - else - local s = string.format("%-16s %s = %u/%u ku", spos, name, pow_curr, pow_max) - tbl[#tbl + 1] = s - end + 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 end end + table.sort(tbl) return table.concat(tbl, "\n") end -local function consumers(pos, gen_tbl) +local function storages(pos) local tbl = {} - for _, item in ipairs(gen_tbl) do - if item and item.pos then - local node = techage.get_node_lvm(item.pos) - local ndef = minetest.registered_nodes[node.name] - local name = short_node_name(item.nominal) - local spos = P2S(item.pos) - local pow_curr, pow_max = get_consumer_data(item, ndef) - if Storage[name] then - local load_percent = 0 - local tdef = techage.NodeDef[node.name] - if tdef and tdef.on_recv_message then - load_percent = tdef.on_recv_message(item.pos, "0", "load") or 0 - end - local s = string.format("%-16s %s = %u/%u ku (%u %%)", - spos, name, pow_curr, pow_max, load_percent) - tbl[#tbl + 1] = s - else - local s = string.format("%-16s %s = %u/%u ku", spos, name, pow_curr, pow_max) - tbl[#tbl + 1] = s - end + 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 end end + table.sort(tbl) return table.concat(tbl, "\n") end -local function number_nodes(pos, netw) - return - "num. generators cat. 1: " .. #(netw.gen1 or {}) .. "\n" .. - "num. generators cat. 2: " .. #(netw.gen2 or {}) .. "\n" .. - "num. consumers cat. 1: " .. #(netw.con1 or {}) .. "\n" .. - "num. consumers cat. 2: " .. #(netw.con2 or {}) -end - -local function power_network(pos, netw) - return - "pow. generators cat. 1: " .. (netw.available1 or 0) .. " ku\n" .. - "pow. generators cat. 2: " .. (netw.available2 or 0) .. " ku\n" .. - "pow. consumers cat. 1: " .. (netw.needed1 or 0) .. " ku\n" .. - "pow. consumers cat. 2: " .. (netw.needed2 or 0) .. " ku" -end +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 +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 local function output(pos, command, text) local meta = M(pos) @@ -446,26 +187,20 @@ local function command(pos, nvm, command) if command then command = command:sub(1,80) command = string.trim(command) + local cmd, data = unpack(string.split(command, " ", false, 1)) - if command == "cls" then + if cmd == "cls" then meta:set_string("output", "") - elseif command == "help" then + elseif cmd == "help" then output(pos, command, HELP) - elseif command == "gen1" then - local netw = techage.networks.has_network("ele1", nvm.ele1 and nvm.ele1.netID) or {} - output(pos, command, generators(pos, netw.gen1 or {})) - elseif command == "gen2" then - local netw = techage.networks.has_network("ele1", nvm.ele1 and nvm.ele1.netID) or {} - output(pos, command, generators(pos, netw.gen2 or {})) - elseif command == "con2" then - local netw = techage.networks.has_network("ele1", nvm.ele1 and nvm.ele1.netID) or {} - output(pos, command, consumers(pos, netw.con2 or {})) - elseif command == "num" then - local netw = techage.networks.has_network("ele1", nvm.ele1 and nvm.ele1.netID) or {} - output(pos, command, number_nodes(pos, netw)) - elseif command == "pow" then - local netw = techage.networks.has_network("ele1", nvm.ele1 and nvm.ele1.netID) or {} - output(pos, command, power_network(pos, netw)) + 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)) elseif command ~= "" then output(pos, command, "") end @@ -492,33 +227,31 @@ minetest.register_node("techage:ta3_power_terminal", { }, after_place_node = function(pos) - local nvm = techage.get_nvm(pos) M(pos):set_int("outdir", networks.side_to_outdir(pos, "B")) Cable:after_place_node(pos) - M(pos):set_string("formspec", formspec1(pos, nvm)) + M(pos):set_string("formspec", formspec1(pos)) end, after_dig_node = function(pos) Cable:after_dig_node(pos) techage.del_mem(pos) end, - tubelib2_on_update2 = function(pos, outdir, tlib2, node) - power.update_network(pos, outdir, tlib2) - end, on_rightclick = function(pos, node, clicker) local mem = techage.get_mem(pos) if mem.active_formspec == 2 then - M(pos):set_string("formspec", formspec2(pos, mem)) + M(pos):set_string("formspec", formspec2(pos)) else - local nvm = techage.get_nvm(pos) - M(pos):set_string("formspec", formspec1(pos, nvm)) + 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 end, on_timer = function(pos, elapsed) - local nvm = techage.get_nvm(pos) + local data = determine_min_max(pos) if techage.is_activeformspec(pos) then - M(pos):set_string("formspec", formspec1(pos, nvm)) + M(pos):set_string("formspec", formspec1(pos, data)) + return true end - return true end, on_receive_fields = function(pos, formname, fields, player) @@ -533,34 +266,20 @@ minetest.register_node("techage:ta3_power_terminal", { mem.cmnd = "" M(pos):set_string("formspec", formspec2(pos, mem)) mem.cmnd = fields.cmnd - elseif fields.config then - techage.reset_activeformspec(pos, player) - M(pos):set_string("formspec", formspec_type(pos, nvm)) - elseif fields.set then - nvm.gentype = fields.gentype - nvm.gentype_idx = Gentype2Idx[fields.gentype] or 1 - techage.set_activeformspec(pos, player) - M(pos):set_string("formspec", formspec1(pos, nvm)) elseif fields.tab == "1" then - M(pos):set_string("formspec", formspec1(pos, nvm)) + M(pos):set_string("formspec", formspec1(pos)) techage.set_activeformspec(pos, player) + minetest.get_node_timer(pos):start(CYCLE_TIME) mem.active_formspec = 1 elseif fields.tab == "2" then - M(pos):set_string("formspec", formspec2(pos, mem)) + M(pos):set_string("formspec", formspec2(pos)) techage.reset_activeformspec(pos, player) mem.active_formspec = 2 elseif fields.key_up and mem.cmnd then - M(pos):set_string("formspec", formspec2(pos, mem)) + M(pos):set_string("formspec", formspec2(pos)) end end, - networks = { - ele1 = { - sides = {B = 1}, -- Cable connection side - ntype = "term", - }, - }, - paramtype2 = "facedir", paramtype = "light", use_texture_alpha = techage.CLIP, @@ -571,7 +290,7 @@ minetest.register_node("techage:ta3_power_terminal", { sounds = default.node_sound_metal_defaults(), }) -Cable:add_secondary_node_names({"techage:ta3_power_terminal"}) +power.register_nodes({"techage:ta3_power_terminal"}, Cable, "con", {"B"}) minetest.register_craft({ output = "techage:ta3_power_terminal", diff --git a/steam_engine/flywheel.lua b/steam_engine/flywheel.lua index 9da6cb8..74fadec 100644 --- a/steam_engine/flywheel.lua +++ b/steam_engine/flywheel.lua @@ -31,15 +31,15 @@ local function switch_axles(pos, on) end local function formspec(self, pos, nvm) - return "size[4,4]".. - "box[0,-0.1;3.8,0.5;#c6e8ff]".. - "label[1,-0.1;"..minetest.colorize( "#000000", S("Flywheel")).."]".. - default.gui_bg.. - default.gui_bg_img.. - default.gui_slots.. - techage.power.formspec_label_bar(pos, 0, 0.8, Axle, S("power"), PWR_PERF, nvm.provided).. - "image_button[2.8,2;1,1;".. self:get_state_button_image(nvm) ..";state_button;]".. - "tooltip[2.8,2;1,1;"..self:get_state_tooltip(nvm).."]" + return "size[4,4]" .. + "box[0,-0.1;3.8,0.5;#c6e8ff]" .. + "label[1,-0.1;" .. minetest.colorize( "#000000", S("Flywheel")) .. "]" .. + default.gui_bg .. + default.gui_bg_img .. + default.gui_slots .. + techage.formspec_power_bar(pos, 0, 0.8, S("power"), nvm.provided, PWR_PERF) .. + "image_button[2.8,2;1,1;" .. self:get_state_button_image(nvm) .. ";state_button;]" .. + "tooltip[2.8,2;1,1; " .. self:get_state_tooltip(nvm) .. "]" end local function transfer_cylinder(pos, topic, payload) @@ -131,9 +131,9 @@ end local function get_generator_data(pos, tlib2) local nvm = techage.get_nvm(pos) if nvm.running then - return {level = (nvm.load or 0) / PWR_PERF, capa = PWR_PERF * 4} + return {level = (nvm.load or 0) / PWR_PERF, perf = PWR_PERF, capa = PWR_PERF * 4} else - return {level = 0, capa = PWR_PERF * 4} + return {level = 0, perf = PWR_PERF, capa = PWR_PERF * 4} end end diff --git a/ta3_power/akkubox.lua b/ta3_power/akkubox.lua index 3d7b662..8a3ac40 100644 --- a/ta3_power/akkubox.lua +++ b/ta3_power/akkubox.lua @@ -22,47 +22,31 @@ local PWR_PERF = 10 local PWR_CAPA = 2000 local Cable = techage.ElectricCable -local power = techage.power -local networks = techage.networks +local power = networks.power +local control = networks.control local in_range = techage.in_range local function formspec(self, pos, nvm) - local needed = nvm.needed or 0 - local capa = nvm.capa or 0 - return "size[5,4]".. - default.gui_bg.. - default.gui_bg_img.. - default.gui_slots.. - "box[0,-0.1;4.8,0.5;#c6e8ff]".. - "label[1,-0.1;"..minetest.colorize( "#000000", S("TA3 Akku Box")).."]".. - power.formspec_label_bar(pos, 0, 0.8, S("Load"), PWR_CAPA, capa).. - "image_button[2.6,2;1,1;".. self:get_state_button_image(nvm) ..";state_button;]".. - "tooltip[3,2;1,1;"..self:get_state_tooltip(nvm).."]".. - "label[3.7,1.2;"..S("Electricity").."]".. - "image[3.8,1.7;1,2;"..techage.power.formspec_load_bar(needed, PWR_PERF).."]" -end - -local function on_power(pos) -end - -local function on_nopower(pos) + local data + + if nvm.running then + local outdir = M(pos):get_int("outdir") + data = power.get_network_data(pos, Cable, outdir) + end + return techage.storage_formspec(self, pos, nvm, S("TA3 Akku Box"), data, nvm.capa, PWR_CAPA) end local function start_node(pos, nvm, state) nvm.running = true - nvm.needed = 0 local outdir = M(pos):get_int("outdir") - power.generator_start(pos, Cable, CYCLE_TIME, outdir) - power.consumer_start(pos, Cable, CYCLE_TIME) + power.start_storage_calc(pos, Cable, outdir) end local function stop_node(pos, nvm, state) nvm.running = false - nvm.needed = 0 local outdir = M(pos):get_int("outdir") - power.generator_stop(pos, Cable, outdir) - power.consumer_stop(pos, Cable) + power.start_storage_calc(pos, Cable, outdir) end local State = techage.NodeStates:new({ @@ -76,19 +60,8 @@ local State = techage.NodeStates:new({ local function node_timer(pos, elapsed) local nvm = techage.get_nvm(pos) - nvm.capa = nvm.capa or 0 local outdir = M(pos):get_int("outdir") - local taken = 0 - local given = 0 - if nvm.capa < PWR_CAPA then - taken = power.consumer_alive(pos, Cable, CYCLE_TIME) - end - if nvm.capa > 0 then - given = power.generator_alive(pos, Cable, CYCLE_TIME, outdir) - end - nvm.needed = taken - given - nvm.capa = in_range(nvm.capa + nvm.needed, 0, PWR_CAPA) - --print("node_timer accu "..P2S(pos), nvm.needed, nvm.capa) + nvm.capa = power.get_storage_load(pos, Cable, outdir, PWR_CAPA) if techage.is_activeformspec(pos) then M(pos):set_string("formspec", formspec(State, pos, nvm)) @@ -112,6 +85,13 @@ local function on_receive_fields(pos, formname, fields, player) M(pos):set_string("formspec", formspec(State, pos, nvm)) end +local function get_storage_data(pos, tlib2) + local nvm = techage.get_nvm(pos) + if nvm.running then + return {level = (nvm.capa or 0) / PWR_CAPA, capa = PWR_CAPA} + end +end + local function get_capa(itemstack) local meta = itemstack:get_meta() if meta then @@ -137,32 +117,20 @@ local function after_place_node(pos, placer, itemstack) local own_num = techage.add_node(pos, "techage:ta3_akku") meta:set_string("owner", placer:get_player_name()) meta:set_string("infotext", S("TA3 Accu Box").." "..own_num) - meta:set_int("outdir", networks.side_to_outdir(pos, "R")) + local outdir = networks.side_to_outdir(pos, "R") + meta:set_int("outdir", outdir) meta:set_string("formspec", formspec(State, pos, nvm)) - Cable:after_place_node(pos) + Cable:after_place_node(pos, {outdir}) State:node_init(pos, nvm, own_num) nvm.capa = get_capa(itemstack) end local function after_dig_node(pos, oldnode, oldmetadata, digger) - Cable:after_dig_node(pos) + local outdir = tonumber(oldmetadata.fields.outdir or 0) + Cable:after_dig_node(pos, {outdir}) techage.del_mem(pos) end -local function tubelib2_on_update2(pos, outdir, tlib2, node) - power.update_network(pos, outdir, tlib2) -end - -local net_def = { - ele1 = { - sides = {R = 1}, - ntype = {"gen2", "con2"}, - nominal = PWR_PERF, - on_power = on_power, - on_nopower = on_nopower, - }, -} - minetest.register_node("techage:ta3_akku", { description = S("TA3 Accu Box"), tiles = { @@ -180,8 +148,7 @@ minetest.register_node("techage:ta3_akku", { on_receive_fields = on_receive_fields, after_place_node = after_place_node, after_dig_node = after_dig_node, - tubelib2_on_update2 = tubelib2_on_update2, - networks = net_def, + get_storage_data = get_storage_data, paramtype2 = "facedir", groups = {cracky=2, crumbly=2, choppy=2}, on_rotate = screwdriver.disallow, @@ -190,7 +157,7 @@ minetest.register_node("techage:ta3_akku", { preserve_metadata = set_capa, }) -Cable:add_secondary_node_names({"techage:ta3_akku"}) +power.register_nodes({"techage:ta3_akku"}, Cable, "sto", {"R"}) -- for logical communication techage.register_node({"techage:ta3_akku"}, { @@ -198,27 +165,31 @@ techage.register_node({"techage:ta3_akku"}, { local nvm = techage.get_nvm(pos) if topic == "load" then return techage.power.percent(PWR_CAPA, nvm.capa) - elseif topic == "delivered" then - return -(nvm.needed or 0) else return State:on_receive_message(pos, topic, payload) end end, - on_node_load = function(pos) - local meta = M(pos) - meta:set_int("outdir", networks.side_to_outdir(pos, "R")) - if meta:get_string("node_number") == "" then - local own_num = techage.add_node(pos, "techage:ta3_akku") - meta:set_string("node_number", own_num) - meta:set_string("infotext", S("TA3 Accu Box").." "..own_num) - end - local mem = tubelib2.get_mem(pos) - local nvm = techage.get_nvm(pos) - nvm.capa = (nvm.capa or 0) + (mem.capa or 0) - tubelib2.del_mem(pos) - end, }) +control.register_nodes({"techage:ta3_akku"}, { + on_receive = function(pos, tlib2, topic, payload) + end, + on_request = function(pos, tlib2, topic) + if topic == "info" then + local nvm = techage.get_nvm(pos) + return { + type = S("TA3 Accu Box"), + number = M(pos):get_string("node_number") or "", + running = nvm.running or false, + capa = PWR_CAPA , + load = nvm.capa or 0, + } + end + return false + end, + } +) + minetest.register_craft({ output = "techage:ta3_akku", recipe = {