From ef9d90491765079b171f794fcef76b06694b7d68 Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Wed, 9 Jun 2021 22:09:59 +0200 Subject: [PATCH] Prepare for lib 'networks' --- .test/sink.lua | 2 +- basic_machines/concentrator.lua | 13 +- basic_machines/source.lua | 47 +++-- basis/firebox_lib.lua | 2 +- basis/fuel_lib.lua | 61 +++++- basis/node_states.lua | 8 +- chemistry/ta4_doser.lua | 9 +- chemistry/ta4_liquid_filter.lua | 3 +- chemistry/ta4_reactor.lua | 2 +- chemistry/ta4_stand.lua | 3 +- coal_power_station/generator.lua | 26 ++- coal_power_station/oilfirebox.lua | 44 +--- furnace/booster.lua | 73 +++---- furnace/firebox.lua | 45 +--- furnace/heater.lua | 79 +++----- init.lua | 92 ++++----- items/filling.lua | 1 + liquids/liquid_pipe.lua | 4 +- liquids/node_api.lua | 245 ---------------------- liquids/pump.lua | 71 ++----- liquids/silo.lua | 23 +-- liquids/tank.lua | 119 ++++++----- liquids/valve.lua | 103 ++-------- liquids/waterpump.lua | 3 +- oil/distiller.lua | 86 +++----- oil/drillbox.lua | 3 +- oil/explore.lua | 2 +- oil/pumpjack.lua | 14 +- oil/reboiler.lua | 259 +++++++++++------------- power/electric_cable.lua | 4 +- power/formspecs.lua | 122 ++++++----- power/gearbox.lua | 6 + power/laser.lua | 7 +- power/power_line.lua | 38 +--- power/ta4_cable.lua | 23 +-- solar/inverter.lua | 103 ++++++---- solar/solarcell.lua | 58 +++--- steam_engine/flywheel.lua | 14 +- ta3_power/axle2power.lua | 139 ++++--------- ta3_power/power2axle.lua | 140 ++++--------- ta3_power/tiny_generator.lua | 116 ++++++----- textures/techage_gaspipe_valve_open.png | Bin 163 -> 134 bytes tools/repairkit.lua | 256 +++++++---------------- tools/submenu.lua | 190 +++++++++++++++++ 44 files changed, 1067 insertions(+), 1591 deletions(-) delete mode 100644 liquids/node_api.lua create mode 100644 tools/submenu.lua diff --git a/.test/sink.lua b/.test/sink.lua index 9a7d659..ec32e83 100644 --- a/.test/sink.lua +++ b/.test/sink.lua @@ -3,7 +3,7 @@ TechAge ======= - Copyright (C) 2019-2020 Joachim Stolberg + Copyright (C) 2019-2021 Joachim Stolberg GPL v3 See LICENSE.txt for more information diff --git a/basic_machines/concentrator.lua b/basic_machines/concentrator.lua index 2843c57..b7d5466 100644 --- a/basic_machines/concentrator.lua +++ b/basic_machines/concentrator.lua @@ -16,7 +16,6 @@ local M = minetest.get_meta local S = techage.S -local networks = techage.networks local Tube = techage.Tube local size = 2/8 @@ -29,7 +28,7 @@ local Boxes = { {{-size, -size, -size, size, 0.5, size}}, -- y+ } -local names = techage.register_junction("techage:concentrator", 2/8, Boxes, Tube, { +local names = networks.register_junction("techage:concentrator", 2/8, Boxes, Tube, { description = S("Tube Concentrator"), tiles = { "techage_tube_junction.png^techage_appl_arrow2.png^[transformR270", @@ -45,13 +44,13 @@ local names = techage.register_junction("techage:concentrator", 2/8, Boxes, Tube after_place_node = function(pos, placer, itemstack, pointed_thing) local node = minetest.get_node(pos) - local name = "techage:concentrator"..techage.junction_type(pos, Tube, "R", node.param2) + local name = "techage:concentrator"..networks.junction_type(pos, Tube, "R", node.param2) minetest.swap_node(pos, {name = name, param2 = node.param2}) M(pos):set_int("push_dir", techage.side_to_outdir("R", node.param2)) Tube:after_place_node(pos) end, tubelib2_on_update2 = function(pos, dir1, tlib2, node) - local name = "techage:concentrator"..techage.junction_type(pos, Tube, "R", node.param2) + local name = "techage:concentrator"..networks.junction_type(pos, Tube, "R", node.param2) minetest.swap_node(pos, {name = name, param2 = node.param2}) end, after_dig_node = function(pos, oldnode, oldmetadata, digger) @@ -67,7 +66,7 @@ techage.register_node(names, { is_pusher = true, -- is a pulling/pushing node }) -names = techage.register_junction("techage:ta4_concentrator", 2/8, Boxes, Tube, { +names = networks.register_junction("techage:ta4_concentrator", 2/8, Boxes, Tube, { description = S("TA4 Tube Concentrator"), tiles = { "techage_tubeta4_junction.png^techage_appl_arrow2.png^[transformR270", @@ -83,13 +82,13 @@ names = techage.register_junction("techage:ta4_concentrator", 2/8, Boxes, Tube, after_place_node = function(pos, placer, itemstack, pointed_thing) local node = minetest.get_node(pos) - local name = "techage:ta4_concentrator"..techage.junction_type(pos, Tube, "R", node.param2) + local name = "techage:ta4_concentrator"..networks.junction_type(pos, Tube, "R", node.param2) minetest.swap_node(pos, {name = name, param2 = node.param2}) M(pos):set_int("push_dir", techage.side_to_outdir("R", node.param2)) Tube:after_place_node(pos) end, tubelib2_on_update2 = function(pos, dir1, tlib2, node) - local name = "techage:ta4_concentrator"..techage.junction_type(pos, Tube, "R", node.param2) + local name = "techage:ta4_concentrator"..networks.junction_type(pos, Tube, "R", node.param2) minetest.swap_node(pos, {name = name, param2 = node.param2}) end, after_dig_node = function(pos, oldnode, oldmetadata, digger) diff --git a/basic_machines/source.lua b/basic_machines/source.lua index 3710f77..2a67c49 100644 --- a/basic_machines/source.lua +++ b/basic_machines/source.lua @@ -28,8 +28,7 @@ local CYCLE_TIME = 2 local PWR_PERF = 100 local function formspec(self, pos, nvm) - return techage.generator_formspec(self, pos, nvm, S("Power Source"), - nvm.provided, PWR_PERF, nvm.generating) + return techage.generator_formspec(self, pos, nvm, S("Power Source"), nvm.provided, PWR_PERF) end -- Axles texture animation @@ -39,7 +38,7 @@ local function switch_axles(pos, on) end local function start_node2(pos, nvm, state) - nvm.generating = true + nvm.running = true nvm.provided = 0 local outdir = M(pos):get_int("outdir") switch_axles(pos, true) @@ -47,7 +46,7 @@ local function start_node2(pos, nvm, state) end local function stop_node2(pos, nvm, state) - nvm.generating = false + nvm.running = false nvm.provided = 0 nvm.load = 0 local outdir = M(pos):get_int("outdir") @@ -56,14 +55,16 @@ local function stop_node2(pos, nvm, state) end local function start_node3(pos, nvm, state) - nvm.generating = true + local meta = M(pos) + nvm.running = true nvm.provided = 0 - local outdir = M(pos):get_int("outdir") + techage.evaluate_charge_termination(nvm, meta) + local outdir = meta:get_int("outdir") power.start_storage_calc(pos, Cable, outdir) end local function stop_node3(pos, nvm, state) - nvm.generating = false + nvm.running = false nvm.provided = 0 local outdir = M(pos):get_int("outdir") power.start_storage_calc(pos, Cable, outdir) @@ -89,24 +90,30 @@ local State3 = techage.NodeStates:new({ local function node_timer2(pos, elapsed) --print("node_timer2") + local meta = M(pos) local nvm = techage.get_nvm(pos) - local outdir = M(pos):get_int("outdir") - nvm.provided = power.provide_power(pos, Axle, outdir, PWR_PERF, nvm.termpoint1, nvm.termpoint2) + local outdir = meta:get_int("outdir") + local tp1 = tonumber(meta:get_string("termpoint1")) + local tp2 = tonumber(meta:get_string("termpoint2")) + nvm.provided = power.provide_power(pos, Axle, outdir, PWR_PERF, tp1, tp2) nvm.load = power.get_storage_load(pos, Axle, outdir, PWR_PERF) if techage.is_activeformspec(pos) then - M(pos):set_string("formspec", formspec(State2, pos, nvm)) + meta:set_string("formspec", formspec(State2, pos, nvm)) end return true end local function node_timer3(pos, elapsed) --print("node_timer4") + local meta = M(pos) local nvm = techage.get_nvm(pos) local outdir = M(pos):get_int("outdir") - nvm.provided = power.provide_power(pos, Cable, outdir, PWR_PERF, nvm.termpoint1, nvm.termpoint2) + local tp1 = tonumber(meta:get_string("termpoint1")) + local tp2 = tonumber(meta:get_string("termpoint2")) + nvm.provided = power.provide_power(pos, Cable, outdir, PWR_PERF, tp1, tp2) nvm.load = power.get_storage_load(pos, Cable, outdir, PWR_PERF) if techage.is_activeformspec(pos) then - M(pos):set_string("formspec", formspec(State3, pos, nvm)) + meta:set_string("formspec", formspec(State3, pos, nvm)) end return true end @@ -117,7 +124,6 @@ local function on_receive_fields2(pos, formname, fields, player) end local nvm = techage.get_nvm(pos) State2:state_button_event(pos, nvm, fields) - techage.evaluate_charge_termination(nvm, fields) M(pos):set_string("formspec", formspec(State2, pos, nvm)) end @@ -127,7 +133,6 @@ local function on_receive_fields3(pos, formname, fields, player) end local nvm = techage.get_nvm(pos) State3:state_button_event(pos, nvm, fields) - techage.evaluate_charge_termination(nvm, fields) M(pos):set_string("formspec", formspec(State3, pos, nvm)) end @@ -172,7 +177,7 @@ end local function get_generator_data(pos, tlib2) local nvm = techage.get_nvm(pos) - if nvm.generating then + if nvm.running then return {level = (nvm.load or 0) / PWR_PERF, perf = PWR_PERF, capa = PWR_PERF * 2} end end @@ -221,6 +226,7 @@ minetest.register_node("techage:t4_source", { after_place_node = after_place_node3, after_dig_node = after_dig_node3, get_generator_data = get_generator_data, + ta3_formspec = techage.generator_settings("ta3", PWR_PERF), }) power.register_nodes({"techage:t2_source"}, Axle, "gen", {"R"}) @@ -231,10 +237,8 @@ techage.register_node({"techage:t4_source"}, { 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) + return State3:on_receive_message(pos, topic, payload) end end, }) @@ -245,13 +249,14 @@ control.register_nodes({"techage:t4_source"}, { on_request = function(pos, tlib2, topic) if topic == "info" then local nvm = techage.get_nvm(pos) + local meta = M(pos) return { type = S("Ele Power Source"), - number = M(pos):get_string("node_number") or "", - running = nvm.generating or false, + number = meta:get_string("node_number") or "", + running = nvm.running or false, available = PWR_PERF, provided = nvm.provided or 0, - termpoint = nvm.termpoint or "unknown", + termpoint = meta:get_string("termpoint"), } end return false diff --git a/basis/firebox_lib.lua b/basis/firebox_lib.lua index b88ce89..28e6807 100644 --- a/basis/firebox_lib.lua +++ b/basis/firebox_lib.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 diff --git a/basis/fuel_lib.lua b/basis/fuel_lib.lua index d86bcb3..4654a32 100644 --- a/basis/fuel_lib.lua +++ b/basis/fuel_lib.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 @@ -17,7 +17,7 @@ local P2S = minetest.pos_to_string local M = minetest.get_meta local S = techage.S local Pipe = techage.LiquidPipe -local liquid = techage.liquid +local liquid = networks.liquid local ValidOilFuels = techage.firebox.ValidOilFuels local Burntime = techage.firebox.Burntime @@ -28,11 +28,12 @@ local BLOCKING_TIME = 0.3 -- 300ms techage.fuel.CAPACITY = CAPACITY --- fuel burning categories (better than...) -techage.fuel.BT_BITUMEN = 4 -techage.fuel.BT_OIL = 3 -techage.fuel.BT_FUELOIL = 2 -techage.fuel.BT_NAPHTHA = 1 +-- fuel burning categories (equal or better than...) +techage.fuel.BT_BITUMEN = 5 +techage.fuel.BT_OIL = 4 +techage.fuel.BT_FUELOIL = 3 +techage.fuel.BT_NAPHTHA = 2 +techage.fuel.BT_GASOLINE = 1 function techage.fuel.fuel_container(x, y, nvm) @@ -95,6 +96,7 @@ function techage.fuel.burntime(name) return 0.01 -- not zero ! end +-- equal or better than the given category (see 'techage.fuel.BT_BITUMEN,...') function techage.fuel.valid_fuel(name, category) return ValidOilFuels[name] and ValidOilFuels[name] <= category end @@ -109,7 +111,7 @@ function techage.fuel.on_punch(pos, node, puncher, pointed_thing) local wielded_item = puncher:get_wielded_item():get_name() local item_count = puncher:get_wielded_item():get_count() - local new_item = liquid.fill_on_punch(nvm, wielded_item, item_count, puncher) + local new_item = techage.liquid.fill_on_punch(nvm, wielded_item, item_count, puncher) if new_item then puncher:set_wielded_item(new_item) M(pos):set_string("formspec", techage.fuel.formspec(pos, nvm)) @@ -117,11 +119,11 @@ function techage.fuel.on_punch(pos, node, puncher, pointed_thing) return end - local ldef = liquid.get_liquid_def(wielded_item) + local ldef = techage.liquid.get_liquid_def(wielded_item) if ldef and ValidOilFuels[ldef.inv_item] then local lqd = (minetest.registered_nodes[node.name] or {}).liquid if not lqd.fuel_cat or ValidOilFuels[ldef.inv_item] <= lqd.fuel_cat then - local new_item = liquid.empty_on_punch(pos, nvm, wielded_item, item_count) + local new_item = techage.liquid.empty_on_punch(pos, nvm, wielded_item, item_count) if new_item then puncher:set_wielded_item(new_item) M(pos):set_string("formspec", techage.fuel.formspec(pos, nvm)) @@ -153,4 +155,43 @@ function techage.fuel.get_fuel_amount(nvm) return nvm.liquid.amount end return 0 +end + +function techage.fuel.get_liquid_table(valid_fuel, capacity, start_firebox) + return { + capa = capacity, + fuel_cat = valid_fuel, + peek = function(pos) + local nvm = techage.get_nvm(pos) + return liquid.srv_peek(nvm) + end, + put = function(pos, indir, name, amount) + if techage.fuel.valid_fuel(name, valid_fuel) then + local nvm = techage.get_nvm(pos) + local res = liquid.srv_put(nvm, name, amount, capacity) + start_firebox(pos, nvm) + if techage.is_activeformspec(pos) then + M(pos):set_string("formspec", techage.fuel.formspec(nvm)) + end + return res + end + return amount + end, + take = function(pos, indir, name, amount) + local nvm = techage.get_nvm(pos) + amount, name = liquid.srv_take(nvm, name, amount) + if techage.is_activeformspec(pos) then + M(pos):set_string("formspec", techage.fuel.formspec(nvm)) + end + return amount, name + end, + untake = function(pos, indir, name, amount) + local nvm = techage.get_nvm(pos) + local leftover = liquid.srv_put(nvm, name, amount, capacity) + if techage.is_activeformspec(pos) then + M(pos):set_string("formspec", techage.fuel.formspec(nvm)) + end + return leftover + end + } end \ No newline at end of file diff --git a/basis/node_states.lua b/basis/node_states.lua index ab6ce52..0071893 100644 --- a/basis/node_states.lua +++ b/basis/node_states.lua @@ -67,7 +67,7 @@ local N = techage.get_node_lvm -- techage.RUNNING = 1 -- in normal operation/turned on -techage.BLOCKED = 2 -- a pushing node is blocked due to a full destination inventory +techage.BLOCKED = 2 -- a pushing node is blocked due to a full destination inventory techage.STANDBY = 3 -- nothing to do (e.g. no input items), or node (world) not loaded techage.NOPOWER = 4 -- only for power consuming nodes, no operation techage.FAULT = 5 -- any fault state (e.g. wrong source items), which can be fixed by the player @@ -158,6 +158,12 @@ function techage.needs_power(nvm) return state == RUNNING or state == NOPOWER end +-- consumes power +function techage.needs_power2(state) + state = state or STOPPED + return state == RUNNING or state == NOPOWER +end + function techage.get_state_string(nvm) return techage.StateStrings[nvm.techage_state or STOPPED] end diff --git a/chemistry/ta4_doser.lua b/chemistry/ta4_doser.lua index e9df68d..dbd3c29 100644 --- a/chemistry/ta4_doser.lua +++ b/chemistry/ta4_doser.lua @@ -67,7 +67,7 @@ local function get_liquids(pos) -- determine the available input liquids local tbl = {} for outdir = 1,4 do - local name, num = liquid.peek(pos, outdir) + local name, num = liquid.peek(pos, Pipe, outdir) if name then tbl[name] = outdir end @@ -87,7 +87,7 @@ local function reload_liquids(pos) -- determine the available input liquids local tbl = {} for outdir = 1,4 do - local name, num = liquid.peek(pos, outdir) + local name, num = liquid.peek(pos, Pipe, outdir) if name then tbl[name] = outdir end @@ -155,7 +155,7 @@ local State = techage.NodeStates:new({ local function untake(pos, taken) for _,item in pairs(taken) do - liquid.untake(pos, item.outdir, item.name, item.num) + liquid.untake(pos, Pipe, item.outdir, item.name, item.num) end end @@ -210,7 +210,7 @@ local function dosing(pos, nvm, elapsed) untake(pos, taken) return end - local num = liquid.take(pos, outdir, item.name, item.num, starter) + local num = liquid.take(pos, Pipe, outdir, item.name, item.num, starter) if num < item.num then taken[#taken + 1] = {outdir = outdir, name = item.name, num = num} State:standby(pos, nvm) @@ -307,7 +307,6 @@ minetest.register_node("techage:ta4_doser", { after_dig_node = function(pos, oldnode, oldmetadata, digger) techage.remove_node(pos, oldnode, oldmetadata) Pipe:after_dig_node(pos) - liquid.after_dig_pump(pos) techage.del_mem(pos) end, on_receive_fields = on_receive_fields, diff --git a/chemistry/ta4_liquid_filter.lua b/chemistry/ta4_liquid_filter.lua index 47db659..6c2f956 100644 --- a/chemistry/ta4_liquid_filter.lua +++ b/chemistry/ta4_liquid_filter.lua @@ -105,7 +105,6 @@ minetest.register_node("techage:ta4_liquid_filter_filler", { end, after_dig_node = function(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) - liquid.after_dig_pump(pos) techage.del_mem(pos) end, @@ -134,7 +133,7 @@ minetest.register_node("techage:ta4_liquid_filter_filler", { end if math.random() < 0.5 then local out_pos = {x=pos.x,y=pos.y-8,z=pos.z} - local leftover = liquid.put(out_pos, networks.side_to_outdir(out_pos, "R"), "techage:lye", 1) + local leftover = liquid.put(out_pos, Pipe, networks.side_to_outdir(out_pos, "R"), "techage:lye", 1) if leftover > 0 then return amount end diff --git a/chemistry/ta4_reactor.lua b/chemistry/ta4_reactor.lua index 6443a4c..93ba23e 100644 --- a/chemistry/ta4_reactor.lua +++ b/chemistry/ta4_reactor.lua @@ -85,7 +85,7 @@ end local function base_waste(pos, payload) local pos2 = {x = pos.x, y = pos.y-3, z = pos.z} local outdir = M(pos2):get_int("outdir") - return liquid.put(pos2, outdir, payload.name, payload.amount, payload.player_name) + return liquid.put(pos2, Pipe, outdir, payload.name, payload.amount, payload.player_name) end -- controlled by the doser diff --git a/chemistry/ta4_stand.lua b/chemistry/ta4_stand.lua index bbe5330..9a3fba6 100644 --- a/chemistry/ta4_stand.lua +++ b/chemistry/ta4_stand.lua @@ -117,7 +117,6 @@ minetest.register_node("techage:ta4_reactor_stand", { after_dig_node = function(pos, oldnode) Pipe:after_dig_node(pos) Cable:after_dig_node(pos) - liquid.after_dig_pump(pos) techage.del_mem(pos) end, @@ -153,7 +152,7 @@ techage.register_node({"techage:ta4_reactor_stand"}, { return nvm.has_power or power.power_available(pos, Cable) elseif topic == "output" then local outdir = M(pos):get_int("outdir") - return liquid.put(pos, outdir, payload.name, payload.amount, payload.player_name) + return liquid.put(pos, Pipe, outdir, payload.name, payload.amount, payload.player_name) elseif topic == "can_start" then return power.power_available(pos, Cable) elseif topic == "start" then diff --git a/coal_power_station/generator.lua b/coal_power_station/generator.lua index 6216363..03b9d59 100644 --- a/coal_power_station/generator.lua +++ b/coal_power_station/generator.lua @@ -26,8 +26,7 @@ local power = networks.power local control = networks.control local function formspec(self, pos, nvm) - return techage.generator_formspec(self, pos, nvm, S("Generator"), - nvm.provided, PWR_CAPA, nvm.running) + return techage.generator_formspec(self, pos, nvm, S("Generator"), nvm.provided, PWR_CAPA) end local function transfer_turbine(pos, topic, payload) @@ -41,17 +40,18 @@ end local function start_node(pos, nvm, state) local outdir = M(pos):get_int("outdir") - power.start_storage_calc(pos, Cable, outdir) + techage.evaluate_charge_termination(nvm, M(pos)) transfer_turbine(pos, "start") nvm.running = true + power.start_storage_calc(pos, Cable, outdir) end local function stop_node(pos, nvm, state) local outdir = M(pos):get_int("outdir") - power.start_storage_calc(pos, Cable, outdir) nvm.provided = 0 transfer_turbine(pos, "stop") nvm.running = false + power.start_storage_calc(pos, Cable, outdir) end local State = techage.NodeStates:new({ @@ -67,6 +67,7 @@ local State = techage.NodeStates:new({ }) local function node_timer(pos, elapsed) + local meta = M(pos) local nvm = techage.get_nvm(pos) nvm.firebox_trigger = (nvm.firebox_trigger or 0) - 1 if nvm.firebox_trigger <= 0 then @@ -74,13 +75,15 @@ local function node_timer(pos, elapsed) stop_node(pos, nvm, State) transfer_turbine(pos, "stop") else - local outdir = M(pos):get_int("outdir") - nvm.provided = power.provide_power(pos, Cable, outdir, PWR_CAPA, nvm.termpoint1, nvm.termpoint2) + local outdir = meta:get_int("outdir") + local tp1 = tonumber(meta:get_string("termpoint1")) + local tp2 = tonumber(meta:get_string("termpoint2")) + nvm.provided = power.provide_power(pos, Cable, outdir, PWR_CAPA, tp1, tp2) 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 - M(pos):set_string("formspec", formspec(State, pos, nvm)) + meta:set_string("formspec", formspec(State, pos, nvm)) end return State:is_active(nvm) end @@ -91,7 +94,6 @@ 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) @@ -106,6 +108,7 @@ local function after_place_node(pos) State:node_init(pos, nvm, number) M(pos):set_int("outdir", networks.side_to_outdir(pos, "R")) M(pos):set_string("formspec", formspec(State, pos, nvm)) + techage.evaluate_charge_termination(nvm, M(pos)) Cable:after_place_node(pos) end @@ -139,6 +142,7 @@ minetest.register_node("techage:generator", { after_place_node = after_place_node, after_dig_node = after_dig_node, get_generator_data = get_generator_data, + ta3_formspec = techage.generator_settings("ta3", PWR_CAPA), paramtype2 = "facedir", groups = {cracky=2, crumbly=2, choppy=2}, @@ -183,6 +187,7 @@ minetest.register_node("techage:generator_on", { after_place_node = after_place_node, after_dig_node = after_dig_node, get_generator_data = get_generator_data, + ta3_formspec = techage.generator_settings("ta3", PWR_CAPA), drop = "", paramtype2 = "facedir", @@ -225,13 +230,14 @@ control.register_nodes({"techage:generator", "techage:generator_on"}, { on_request = function(pos, tlib2, topic) if topic == "info" then local nvm = techage.get_nvm(pos) + local meta = M(pos) return { type = S("TA3 Generator"), - number = M(pos):get_string("node_number") or "", + number = meta:get_string("node_number") or "", running = nvm.running or false, available = PWR_CAPA, provided = nvm.provided or 0, - termpoint = nvm.termpoint or "unknown", + termpoint = meta:get_string("termpoint"), } end return false diff --git a/coal_power_station/oilfirebox.lua b/coal_power_station/oilfirebox.lua index 3ccb5d3..eff5593 100644 --- a/coal_power_station/oilfirebox.lua +++ b/coal_power_station/oilfirebox.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 @@ -20,7 +20,7 @@ local S = techage.S local firebox = techage.firebox local fuel = techage.fuel local Pipe = techage.LiquidPipe -local liquid = techage.liquid +local liquid = networks.liquid local CYCLE_TIME = 2 local BURN_CYCLE_FACTOR = 0.5 @@ -115,44 +115,10 @@ minetest.register_node("techage:oilfirebox", { minetest.after(1, start_firebox, pos, nvm) end end, - --- 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"}) +liquid.register_nodes({"techage:oilfirebox"}, + Pipe, "tank", nil, fuel.get_liquid_table(fuel.BT_OIL, fuel.CAPACITY, start_firebox)) techage.register_node({"techage:oilfirebox"}, { @@ -161,7 +127,7 @@ techage.register_node({"techage:oilfirebox"}, { if topic == "state" then return nvm.running and "running" or "stopped" elseif topic == "fuel" then - return techage.fuel.get_fuel_amount(nvm) + return fuel.get_fuel_amount(nvm) else return "unsupported" end diff --git a/furnace/booster.lua b/furnace/booster.lua index 8783f0d..8e8be01 100644 --- a/furnace/booster.lua +++ b/furnace/booster.lua @@ -20,8 +20,7 @@ local PWR_NEEDED = 3 local CYCLE_TIME = 2 local Cable = techage.ElectricCable -local power = techage.power ---local networks = techage.networks +local power = networks.power local function infotext(pos, state) M(pos):set_string("infotext", S("TA3 Booster")..": "..state) @@ -58,23 +57,6 @@ local function stop_sound(pos) end end -local function on_power(pos) - swap_node(pos, "techage:ta3_booster_on") - infotext(pos, "running") - play_sound(pos) -end - -local function on_nopower(pos) - swap_node(pos, "techage:ta3_booster") - infotext(pos, "no power") - stop_sound(pos) -end - -local function node_timer(pos, elapsed) - power.consumer_alive(pos, Cable, CYCLE_TIME) - return true -end - local function after_place_node(pos) local nvm = techage.get_nvm(pos) Cable:after_place_node(pos) @@ -89,10 +71,6 @@ 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) -end - minetest.register_node("techage:ta3_booster", { description = S("TA3 Booster"), tiles = { @@ -105,19 +83,17 @@ minetest.register_node("techage:ta3_booster", { "techage_filling_ta3.png^techage_appl_compressor.png^[transformFX^techage_frame_ta3.png", }, - on_timer = node_timer, + on_timer = function(pos, elapsed) + local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED) + if consumed == PWR_NEEDED then + swap_node(pos, "techage:ta3_booster_on") + infotext(pos, "running") + play_sound(pos) + end + return true + end, after_place_node = after_place_node, after_dig_node = after_dig_node, - tubelib2_on_update2 = tubelib2_on_update2, - networks = { - ele1 = { - sides = {B = true, F = true, L = true, D = true, U = true}, - ntype = "con1", - on_power = on_power, - on_nopower = on_nopower, - nominal = PWR_NEEDED, - }, - }, paramtype2 = "facedir", groups = {cracky=2, crumbly=2, choppy=2}, @@ -126,7 +102,6 @@ minetest.register_node("techage:ta3_booster", { sounds = default.node_sound_wood_defaults(), }) - minetest.register_node("techage:ta3_booster_on", { tiles = { -- up, down, right, left, back, front @@ -156,20 +131,17 @@ minetest.register_node("techage:ta3_booster_on", { }, }, - on_timer = node_timer, + on_timer = function(pos, elapsed) + local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED) + if consumed < PWR_NEEDED then + swap_node(pos, "techage:ta3_booster") + infotext(pos, "no power") + stop_sound(pos) + end + return true + end, after_place_node = after_place_node, after_dig_node = after_dig_node, - tubelib2_on_update2 = tubelib2_on_update2, - networks = { - ele1 = { - sides = {B = true, F = true, L = true, D = true, U = true}, - ntype = "con1", - on_power = on_power, - on_nopower = on_nopower, - nominal = PWR_NEEDED, - is_running = function() return true end, - }, - }, paramtype2 = "facedir", groups = {not_in_creative_inventory = 1}, @@ -179,7 +151,7 @@ minetest.register_node("techage:ta3_booster_on", { sounds = default.node_sound_wood_defaults(), }) -Cable:add_secondary_node_names({"techage:ta3_booster", "techage:ta3_booster_on"}) +power.register_nodes({"techage:ta3_booster", "techage:ta3_booster_on"}, Cable, "con", {"B", "F", "L", "D", "U"}) -- for intra machine communication techage.register_node({"techage:ta3_booster", "techage:ta3_booster_on"}, { @@ -194,15 +166,16 @@ techage.register_node({"techage:ta3_booster", "techage:ta3_booster_on"}, { elseif topic == "start" and not nvm.running then if power.power_available(pos, Cable) then nvm.running = true - power.consumer_start(pos, Cable, CYCLE_TIME) minetest.get_node_timer(pos):start(CYCLE_TIME) + swap_node(pos, "techage:ta3_booster_on") + infotext(pos, "running") + play_sound(pos) else infotext(pos, "no power") end elseif topic == "stop" then nvm.running = false swap_node(pos, "techage:ta3_booster") - power.consumer_stop(pos, Cable) minetest.get_node_timer(pos):stop() infotext(pos, "stopped") stop_sound(pos) diff --git a/furnace/firebox.lua b/furnace/firebox.lua index a6f2ca5..813a78b 100644 --- a/furnace/firebox.lua +++ b/furnace/firebox.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 @@ -19,7 +19,7 @@ local S = techage.S local firebox = techage.firebox local fuel = techage.fuel local Pipe = techage.LiquidPipe -local liquid = techage.liquid +local liquid = networks.liquid local CYCLE_TIME = 2 local EFFICIENCY = 2 -- burn cycles @@ -80,38 +80,6 @@ local function booster_cmnd(pos, cmnd) {"techage:ta3_booster", "techage:ta3_booster_on"}) end -local _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 res = liquid.srv_put(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 res - 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 -} - -local _networks = { - pipe2 = { - sides = techage.networks.AllSides, -- Pipe connection sides - ntype = "tank", - }, -} - minetest.register_node("techage:furnace_firebox", { description = S("TA3 Furnace Oil Burner"), tiles = { @@ -134,8 +102,6 @@ minetest.register_node("techage:furnace_firebox", { on_punch = fuel.on_punch, on_receive_fields = fuel.on_receive_fields, on_rightclick = fuel.on_rightclick, - liquid = _liquid, - networks = _networks, on_construct = function(pos) local nvm = techage.get_nvm(pos) @@ -185,8 +151,6 @@ minetest.register_node("techage:furnace_firebox_on", { on_receive_fields = fuel.on_receive_fields, on_punch = fuel.on_punch, on_rightclick = fuel.on_rightclick, - liquid = _liquid, - networks = _networks, }) minetest.register_craft({ @@ -204,7 +168,7 @@ techage.register_node({"techage:furnace_firebox", "techage:furnace_firebox_on"}, if topic == "state" then return nvm.running and "running" or "stopped" elseif topic == "fuel" then - return techage.fuel.get_fuel_amount(nvm) + return fuel.get_fuel_amount(nvm) else return "unsupported" end @@ -237,4 +201,5 @@ techage.register_node({"techage:furnace_firebox", "techage:furnace_firebox_on"}, end, }) -Pipe:add_secondary_node_names({"techage:furnace_firebox", "techage:furnace_firebox_on"}) +liquid.register_nodes({"techage:furnace_firebox", "techage:furnace_firebox_on"}, + Pipe, "tank", nil, fuel.get_liquid_table(fuel.BT_OIL, fuel.CAPACITY, start_firebox)) diff --git a/furnace/heater.lua b/furnace/heater.lua index 98cde08..e7a4be6 100644 --- a/furnace/heater.lua +++ b/furnace/heater.lua @@ -16,11 +16,11 @@ local M = minetest.get_meta local S = techage.S -local CYCLE_TIME = 2 local PWR_NEEDED = 14 +local CYCLE_TIME = 2 local Cable = techage.ElectricCable -local power = techage.power +local power = networks.power local function swap_node(pos, name) local node = techage.get_node_lvm(pos) @@ -31,18 +31,6 @@ local function swap_node(pos, name) minetest.swap_node(pos, node) end -local function on_power(pos) - swap_node(pos, "techage:furnace_heater_on") -end - -local function on_nopower(pos) - swap_node(pos, "techage:furnace_heater") -end - -local function node_timer(pos, elapsed) - power.consumer_alive(pos, Cable, CYCLE_TIME) - return true -end local function after_place_node(pos) Cable:after_place_node(pos) @@ -53,10 +41,6 @@ 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) -end - minetest.register_node("techage:furnace_heater", { description = S("TA4 Furnace Heater"), tiles = { @@ -69,19 +53,15 @@ minetest.register_node("techage:furnace_heater", { "techage_concrete.png^techage_appl_heater.png^techage_frame_ta3.png", }, - on_timer = node_timer, + on_timer = function(pos, elapsed) + local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED) + if consumed == PWR_NEEDED then + swap_node(pos, "techage:furnace_heater_on") + end + return true + end, after_place_node = after_place_node, after_dig_node = after_dig_node, - tubelib2_on_update2 = tubelib2_on_update2, - networks = { - ele1 = { - sides = {B = true, F = true, L = true, D = true, U = true}, - ntype = "con1", - on_power = on_power, - on_nopower = on_nopower, - nominal = PWR_NEEDED, - }, - }, paramtype2 = "facedir", groups = {cracky=2, crumbly=2, choppy=2}, @@ -102,20 +82,15 @@ minetest.register_node("techage:furnace_heater_on", { "techage_concrete.png^techage_appl_heater_on.png^techage_frame_ta3.png", }, - on_timer = node_timer, + on_timer = function(pos, elapsed) + local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED) + if consumed < PWR_NEEDED then + swap_node(pos, "techage:furnace_heater") + end + return true + end, after_place_node = after_place_node, after_dig_node = after_dig_node, - tubelib2_on_update2 = tubelib2_on_update2, - networks = { - ele1 = { - sides = {B = true, F = true, L = true, D = true, U = true}, - ntype = "con1", - on_power = on_power, - on_nopower = on_nopower, - nominal = PWR_NEEDED, - is_running = function() return true end, - }, - }, light_source = 8, paramtype2 = "facedir", @@ -126,16 +101,7 @@ minetest.register_node("techage:furnace_heater_on", { sounds = default.node_sound_wood_defaults(), }) -Cable:add_secondary_node_names({"techage:furnace_heater", "techage:furnace_heater_on"}) - -minetest.register_craft({ - output = "techage:furnace_heater", - recipe = { - {'techage:aluminum', 'default:steel_ingot', 'techage:aluminum'}, - {'techage:basalt_stone', 'basic_materials:heating_element', 'techage:basalt_stone'}, - {'techage:aluminum', 'techage:ta4_furnace_ceramic', 'techage:aluminum'}, - }, -}) +power.register_nodes({"techage:furnace_heater", "techage:furnace_heater_on"}, Cable, "con", {"B", "F", "L", "D", "U"}) techage.register_node({"techage:furnace_heater", "techage:furnace_heater_on"}, { -- called from furnace_top @@ -148,16 +114,23 @@ techage.register_node({"techage:furnace_heater", "techage:furnace_heater_on"}, { elseif topic == "start" and not nvm.running then if power.power_available(pos, Cable) then nvm.running = true - power.consumer_start(pos, Cable, CYCLE_TIME) minetest.get_node_timer(pos):start(CYCLE_TIME) return true end elseif topic == "stop" and nvm.running then nvm.running = false swap_node(pos, "techage:furnace_heater") - power.consumer_stop(pos, Cable) minetest.get_node_timer(pos):stop() return true end end }) + +minetest.register_craft({ + output = "techage:furnace_heater", + recipe = { + {'techage:aluminum', 'default:steel_ingot', 'techage:aluminum'}, + {'techage:basalt_stone', 'basic_materials:heating_element', 'techage:basalt_stone'}, + {'techage:aluminum', 'techage:ta4_furnace_ceramic', 'techage:aluminum'}, + }, +}) diff --git a/init.lua b/init.lua index e87e46c..df08e80 100644 --- a/init.lua +++ b/init.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 @@ -13,7 +13,7 @@ techage = {} -- Version for compatibility checks, see readme.md/history -techage.version = 0.26 +techage.version = 1.00 if minetest.global_exists("tubelib") then minetest.log("error", "[techage] Techage can't be used together with the mod tubelib!") @@ -113,10 +113,10 @@ dofile(MP.."/power/powerswitchbox_legacy.lua") dofile(MP.."/power/powerswitchbox.lua") dofile(MP.."/power/powerswitch.lua") dofile(MP.."/power/protection.lua") ---dofile(MP.."/power/power_line.lua") ---dofile(MP.."/power/ta4_cable.lua") ---dofile(MP.."/power/ta4_cable_wall_entry.lua") ---dofile(MP.."/power/laser.lua") +dofile(MP.."/power/power_line.lua") +dofile(MP.."/power/ta4_cable.lua") +dofile(MP.."/power/ta4_cable_wall_entry.lua") +dofile(MP.."/power/laser.lua") -- Iron Age dofile(MP.."/iron_age/main.lua") @@ -143,8 +143,8 @@ dofile(MP.."/steam_engine/flywheel.lua") -- Liquids I dofile(MP.."/liquids/liquid_pipe.lua") ---dofile(MP.."/liquids/valve.lua") ---dofile(MP.."/liquids/pipe_wall_entry.lua") +dofile(MP.."/liquids/valve.lua") +dofile(MP.."/liquids/pipe_wall_entry.lua") -- Basic Machines dofile(MP.."/basic_machines/consumer.lua") -- consumer base model @@ -165,15 +165,15 @@ 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") +dofile(MP.."/basic_machines/concentrator.lua") -- Liquids II ---dofile(MP.."/liquids/tank.lua") ---dofile(MP.."/liquids/filler.lua") ---dofile(MP.."/liquids/silo.lua") ---dofile(MP.."/liquids/pump.lua") ---dofile(MP.."/liquids/waterpump.lua") ---dofile(MP.."/liquids/waterinlet.lua") +dofile(MP.."/liquids/tank.lua") +dofile(MP.."/liquids/filler.lua") +dofile(MP.."/liquids/silo.lua") +dofile(MP.."/liquids/pump.lua") +dofile(MP.."/liquids/waterpump.lua") +dofile(MP.."/liquids/waterinlet.lua") -- Coal power station dofile(MP.."/coal_power_station/firebox.lua") @@ -185,19 +185,19 @@ dofile(MP.."/coal_power_station/cooler.lua") dofile(MP.."/coal_power_station/oilfirebox.lua") -- Industrial Furnace ---dofile(MP.."/furnace/firebox.lua") ---dofile(MP.."/furnace/cooking.lua") ---dofile(MP.."/furnace/furnace_top.lua") ---dofile(MP.."/furnace/booster.lua") ---dofile(MP.."/furnace/heater.lua") ---dofile(MP.."/furnace/recipes.lua") +dofile(MP.."/furnace/firebox.lua") +dofile(MP.."/furnace/cooking.lua") +dofile(MP.."/furnace/furnace_top.lua") +dofile(MP.."/furnace/booster.lua") +dofile(MP.."/furnace/heater.lua") +dofile(MP.."/furnace/recipes.lua") -- Tools dofile(MP.."/tools/trowel.lua") ---dofile(MP.."/tools/repairkit.lua") ---dofile(MP.."/tools/pipe_wrench.lua") ---dofile(MP.."/basic_machines/blackhole.lua") ---dofile(MP.."/basic_machines/forceload.lua") +dofile(MP.."/tools/repairkit.lua") +dofile(MP.."/tools/pipe_wrench.lua") +dofile(MP.."/basic_machines/blackhole.lua") +dofile(MP.."/basic_machines/forceload.lua") -- Lamps dofile(MP.."/lamps/lib.lua") @@ -214,19 +214,19 @@ dofile(MP.."/lamps/growlight.lua") dofile(MP.."/lamps/lampholder.lua") -- Oil ---dofile(MP.."/oil/explore.lua") ---dofile(MP.."/oil/tower.lua") ---dofile(MP.."/oil/drillbox.lua") ---dofile(MP.."/oil/pumpjack.lua") ---dofile(MP.."/oil/distiller.lua") ---dofile(MP.."/oil/reboiler.lua") --- -- dofile(MP.."/oil/gasflare.lua") +dofile(MP.."/oil/explore.lua") +dofile(MP.."/oil/tower.lua") +dofile(MP.."/oil/drillbox.lua") +dofile(MP.."/oil/pumpjack.lua") +dofile(MP.."/oil/distiller.lua") +dofile(MP.."/oil/reboiler.lua") +-- dofile(MP.."/oil/gasflare.lua") -- TA3 power based ---dofile(MP.."/ta3_power/tiny_generator.lua") +dofile(MP.."/ta3_power/tiny_generator.lua") dofile(MP.."/ta3_power/akkubox.lua") ---dofile(MP.."/ta3_power/axle2power.lua") ---dofile(MP.."/ta3_power/power2axle.lua") +dofile(MP.."/ta3_power/axle2power.lua") +dofile(MP.."/ta3_power/power2axle.lua") -- Digtron --if minetest.global_exists("digtron") then @@ -262,9 +262,9 @@ dofile(MP.."/recipe_checker.lua") dofile(MP.."/.test/sink.lua") -- Solar ---dofile(MP.."/solar/minicell.lua") ---dofile(MP.."/solar/solarcell.lua") ---dofile(MP.."/solar/inverter.lua") +dofile(MP.."/solar/minicell.lua") +dofile(MP.."/solar/solarcell.lua") +dofile(MP.."/solar/inverter.lua") -- Wind --dofile(MP.."/wind_turbine/rotor.lua") @@ -311,14 +311,14 @@ dofile(MP.."/lua_controller/controller.lua") --dofile(MP.."/lua_controller/terminal.lua") -- Items ---dofile(MP.."/items/registered_nodes.lua") ---dofile(MP.."/items/barrel.lua") ---dofile(MP.."/items/baborium.lua") ---dofile(MP.."/items/usmium.lua") ---dofile(MP.."/items/lye.lua") ---dofile(MP.."/items/oil.lua") ---dofile(MP.."/items/petroleum.lua") ---dofile(MP.."/items/bauxit.lua") +dofile(MP.."/items/registered_nodes.lua") +dofile(MP.."/items/barrel.lua") +dofile(MP.."/items/baborium.lua") +dofile(MP.."/items/usmium.lua") +dofile(MP.."/items/lye.lua") +dofile(MP.."/items/oil.lua") +dofile(MP.."/items/petroleum.lua") +dofile(MP.."/items/bauxit.lua") --dofile(MP.."/items/silicon.lua") --dofile(MP.."/items/steelmat.lua") --dofile(MP.."/items/powder.lua") diff --git a/items/filling.lua b/items/filling.lua index d6024e0..5ecd988 100644 --- a/items/filling.lua +++ b/items/filling.lua @@ -26,4 +26,5 @@ techage.FILLING_ITEMS = { "default:goldblock", "default:mese", "default:diamondblock", + "techage:power_pole3", } \ No newline at end of file diff --git a/liquids/liquid_pipe.lua b/liquids/liquid_pipe.lua index a7b1455..daf667c 100644 --- a/liquids/liquid_pipe.lua +++ b/liquids/liquid_pipe.lua @@ -165,12 +165,12 @@ local names = networks.register_junction("techage:ta3_junctionpipe", 1/8, Boxes, sounds = default.node_sound_metal_defaults(), after_place_node = function(pos, placer, itemstack, pointed_thing) - local name = "techage:ta3_junctionpipe"..techage.junction_type(pos, Pipe) + local name = "techage:ta3_junctionpipe" .. networks.junction_type(pos, Pipe) minetest.swap_node(pos, {name = name, param2 = 0}) Pipe:after_place_node(pos) end, tubelib2_on_update2 = function(pos, dir1, tlib2, node) - local name = "techage:ta3_junctionpipe"..techage.junction_type(pos, Pipe) + local name = "techage:ta3_junctionpipe" .. networks.junction_type(pos, Pipe) minetest.swap_node(pos, {name = name, param2 = 0}) power.update_network(pos, 0, tlib2, node) end, diff --git a/liquids/node_api.lua b/liquids/node_api.lua deleted file mode 100644 index 87e9874..0000000 --- a/liquids/node_api.lua +++ /dev/null @@ -1,245 +0,0 @@ ---[[ - - TechAge - ======= - - Copyright (C) 2019-2020 Joachim Stolberg - - AGPL v3 - See LICENSE.txt for more information - - Liquid transportation API via Pipe(s) (peer, put, take) - -]]-- - -local P2S = minetest.pos_to_string -local M = minetest.get_meta -local N = function(pos) return minetest.get_node(pos).name end -local LQD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).liquid end -local Pipe = techage.LiquidPipe -local S = techage.S - -local net_def = techage.networks.net_def -local networks = techage.networks - -local liquid = techage.liquid - --- --- Networks --- - --- determine network ID (largest hash number of all pumps) -local function determine_netID(pos, outdir) - local netID = 0 - networks.connection_walk(pos, outdir, Pipe, function(pos, indir, node) - local ntype = net_def(pos, "pipe2").ntype - if ntype and ntype == "pump" then - local new = minetest.hash_node_position(pos) * 8 + outdir - if netID <= new then - netID = new - end - end - end) - return netID -end - --- store network ID on each pump like node -local function store_netID(pos, outdir, netID) - networks.connection_walk(pos, outdir, Pipe, function(pos, indir, node) - local ntype = net_def(pos, "pipe2").ntype - if ntype and ntype == "pump" then - local nvm = techage.get_nvm(pos) - local outdir = networks.Flip[indir] - nvm.pipe2 = nvm.pipe2 or {} - nvm.pipe2.netIDs = nvm.pipe2.netIDs or {} - nvm.pipe2.netIDs[outdir] = netID - end - end) -end - --- delete network and ID on each pump like node -local function delete_netID(pos, outdir) - local netID = 0 - networks.connection_walk(pos, outdir, Pipe, function(pos, indir, node) - local ntype = net_def(pos, "pipe2").ntype - if ntype and ntype == "pump" then - local nvm = techage.get_nvm(pos) - local outdir = networks.Flip[indir] - if nvm.pipe2 and nvm.pipe2.netIDs and nvm.pipe2.netIDs[outdir] then - netID = nvm.pipe2.netIDs[outdir] - nvm.pipe2.netIDs[outdir] = nil - end - end - end) - networks.delete_network("pipe2", netID) -end - -local function get_netID(pos, outdir) - local nvm = techage.get_nvm(pos) - if not nvm.pipe2 or not nvm.pipe2.netIDs or not nvm.pipe2.netIDs[outdir] then - local netID = determine_netID(pos, outdir) - store_netID(pos, outdir, netID) - end - return nvm.pipe2 and nvm.pipe2.netIDs and nvm.pipe2.netIDs[outdir] -end - --- return list of nodes {pos = ..., indir = ...} of given type -local function get_network_table(pos, outdir, ntype) - local netID = get_netID(pos, outdir) - if netID then - local netw = networks.get_network("pipe2", netID) - if not netw then - netw = networks.collect_network_nodes(pos, outdir, Pipe) - networks.set_network("pipe2", netID, netw) - end - if not netw[ntype] then -- connection lost (e.g. tank cart)? - -- reactivate network - networks.node_connections(pos, Pipe) - delete_netID(pos, outdir) - end - --print("netw", string.format("%012X", netID), dump(netw[ntype])) - return netw[ntype] or {} - end - return {} -end - - --- --- Client remote functions --- - --- Determine and return liquid 'name' from the --- remote inventory. -function liquid.peek(pos, outdir) - for _,item in ipairs(get_network_table(pos, outdir, "tank")) do - local liq = LQD(item.pos) - if liq and liq.peek then - return liq.peek(item.pos, item.indir) - end - end -end - --- Add given amount of liquid to the remote inventory. --- return leftover amount -function liquid.put(pos, outdir, name, amount, player_name) - for _,item in ipairs(get_network_table(pos, outdir, "tank")) do - local liq = LQD(item.pos) - if liq and liq.put and liq.peek then - -- wrong items? - local peek = liq.peek(item.pos, item.indir) - if peek and peek ~= name then return amount or 0 end - if player_name then - local num = techage.get_node_number(pos) or "000" - techage.mark_position(player_name, item.pos, "("..num..") put", "", 1) - end - amount = liq.put(item.pos, item.indir, name, amount) - if not amount or amount == 0 then break end - end - end - return amount or 0 -end - --- Take given amount of liquid from the remote inventory. --- return taken amount and item name -function liquid.take(pos, outdir, name, amount, player_name) - local taken = 0 - for _,item in ipairs(get_network_table(pos, outdir, "tank")) do - local liq = LQD(item.pos) - if liq and liq.take then - if player_name then - local num = techage.get_node_number(pos) - techage.mark_position(player_name, item.pos, "("..num..") take", "", 1) - end - taken, name = liq.take(item.pos, item.indir, name, amount) - if taken and name and taken > 0 then - break - end - end - end - return taken, name -end - -function liquid.untake(pos, outdir, name, amount) - for _,item in ipairs(get_network_table(pos, outdir, "tank")) do - local liq = LQD(item.pos) - if liq and liq.untake then - amount = liq.untake(item.pos, item.indir, name, amount) - if not amount or amount == 0 then break end - end - end - return amount or 0 -end - --- --- Server local functions --- - -function liquid.srv_peek(pos, indir) - local nvm = techage.get_nvm(pos) - nvm.liquid = nvm.liquid or {} - return nvm.liquid.name -end - -function liquid.srv_put(pos, indir, name, amount) - local nvm = techage.get_nvm(pos) - nvm.liquid = nvm.liquid or {} - amount = amount or 0 - if not nvm.liquid.name then - nvm.liquid.name = name - nvm.liquid.amount = amount - return 0 - elseif nvm.liquid.name == name then - nvm.liquid.amount = nvm.liquid.amount or 0 - local capa = LQD(pos).capa - if nvm.liquid.amount + amount <= capa then - nvm.liquid.amount = nvm.liquid.amount + amount - return 0 - else - local rest = nvm.liquid.amount + amount - capa - nvm.liquid.amount = capa - return rest - end - end - return amount -end - -function liquid.srv_take(pos, indir, name, amount) - local nvm = techage.get_nvm(pos) - nvm.liquid = nvm.liquid or {} - amount = amount or 0 - if not name or nvm.liquid.name == name then - name = nvm.liquid.name - nvm.liquid.amount = nvm.liquid.amount or 0 - if nvm.liquid.amount > amount then - nvm.liquid.amount = nvm.liquid.amount - amount - return amount, name - else - local rest = nvm.liquid.amount - local name = nvm.liquid.name - nvm.liquid.amount = 0 - nvm.liquid.name = nil - return rest, name - end - end - return 0 -end - - --- To be called from each node via 'tubelib2_on_update2' --- 'output' is optional and only needed for nodes with dedicated --- pipe sides (e.g. pumps). -function liquid.update_network(pos, outdir) - networks.node_connections(pos, Pipe) - delete_netID(pos, outdir) -end - --- To be called from each pump in 'after_dig_node' --- before calling 'techage.del_mem(pos)' -function liquid.after_dig_pump(pos) - local nvm = techage.get_nvm(pos) - if nvm.pipe2 and nvm.pipe2.netIDs then - for outdir, netID in pairs(nvm.pipe2.netIDs) do - networks.delete_network("pipe2", netID) - end - end -end \ No newline at end of file diff --git a/liquids/pump.lua b/liquids/pump.lua index 66cdd53..2762e78 100644 --- a/liquids/pump.lua +++ b/liquids/pump.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 @@ -17,35 +17,14 @@ local P2S = minetest.pos_to_string local M = minetest.get_meta local S = techage.S local Pipe = techage.LiquidPipe -local networks = techage.networks -local liquid = techage.liquid -local Flip = techage.networks.Flip +local liquid = networks.liquid +local Flip = networks.Flip local STANDBY_TICKS = 3 local COUNTDOWN_TICKS = 4 local CYCLE_TIME = 2 local CAPA = 4 --- to mark the pump source and destinstion node -local DebugCache = {} - -local function set_starter_name(pos, clicker) - local key = minetest.hash_node_position(pos) - DebugCache[key] = {starter = clicker:get_player_name(), count = 10} -end - -local function get_starter_name(pos) - local key = minetest.hash_node_position(pos) - local def = DebugCache[key] - if def then - def.count = (def.count or 0) - 1 - if def.count > 0 then - return def.starter - end - DebugCache[key] = nil - end -end - local State3 = techage.NodeStates:new({ node_name_passive = "techage:t3_pump", node_name_active = "techage:t3_pump_on", @@ -63,14 +42,15 @@ local State4 = techage.NodeStates:new({ }) local function pumping(pos, nvm, state, capa) + local mem = techage.get_mem(pos) + mem.dbg_cycles = (mem.dbg_cycles or 0) - 1 local outdir = M(pos):get_int("outdir") - local starter = get_starter_name(pos) --print("pumping", outdir, Flip[outdir]) - local taken, name = liquid.take(pos, Flip[outdir], nil, capa, starter) + local taken, name = liquid.take(pos, Pipe, Flip[outdir], nil, capa, mem.dbg_cycles > 0) if taken > 0 then - local leftover = liquid.put(pos, outdir, name, taken, starter) - if leftover then - liquid.untake(pos, Flip[outdir], name, leftover) + local leftover = liquid.put(pos, Pipe, outdir, name, taken, mem.dbg_cycles > 0) + if leftover and leftover > 0 then + liquid.untake(pos, Pipe, Flip[outdir], name, leftover) if leftover == taken then state:blocked(pos, nvm) return @@ -117,25 +97,22 @@ local function on_rightclick(pos, node, clicker) local nvm = techage.get_nvm(pos) if node.name == "techage:t3_pump" then - set_starter_name(pos, clicker) + local mem = techage.get_mem(pos) + mem.dbg_cycles = 5 State3:start(pos, nvm) elseif node.name == "techage:t3_pump_on" then State3:stop(pos, nvm) elseif node.name == "techage:t4_pump" then - set_starter_name(pos, clicker) + local mem = techage.get_mem(pos) + mem.dbg_cycles = 5 State4:start(pos, nvm) elseif node.name == "techage:t4_pump_on" then State4:stop(pos, nvm) end end -local function tubelib2_on_update2(pos, outdir, tlib2, node) - liquid.update_network(pos, outdir) -end - local function after_dig_node(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) - liquid.after_dig_pump(pos) techage.del_mem(pos) end @@ -215,23 +192,14 @@ local ta4_tiles_act = { }, } -local nworks = { - pipe2 = { - sides = {L = 1, R = 1}, -- Pipe connection side - ntype = "pump", - }, -} - minetest.register_node("techage:t3_pump", { description = S("TA3 Pump"), tiles = ta3_tiles_pas, after_place_node = after_place_node3, on_rightclick = on_rightclick, - tubelib2_on_update2 = tubelib2_on_update2, on_timer = node_timer3, after_dig_node = after_dig_node, on_rotate = screwdriver.disallow, - networks = nworks, paramtype2 = "facedir", on_rotate = screwdriver.disallow, groups = {cracky=2}, @@ -244,11 +212,9 @@ minetest.register_node("techage:t3_pump_on", { tiles = ta3_tiles_act, --after_place_node = after_place_node3, on_rightclick = on_rightclick, - tubelib2_on_update2 = tubelib2_on_update2, on_timer = node_timer3, after_dig_node = after_dig_node, on_rotate = screwdriver.disallow, - networks = nworks, paramtype2 = "facedir", on_rotate = screwdriver.disallow, diggable = false, @@ -262,11 +228,9 @@ minetest.register_node("techage:t4_pump", { tiles = ta4_tiles_pas, after_place_node = after_place_node4, on_rightclick = on_rightclick, - tubelib2_on_update2 = tubelib2_on_update2, on_timer = node_timer4, after_dig_node = after_dig_node, on_rotate = screwdriver.disallow, - networks = nworks, paramtype2 = "facedir", on_rotate = screwdriver.disallow, groups = {cracky=2}, @@ -279,11 +243,9 @@ minetest.register_node("techage:t4_pump_on", { tiles = ta4_tiles_act, --after_place_node = after_place_node4, on_rightclick = on_rightclick, - tubelib2_on_update2 = tubelib2_on_update2, on_timer = node_timer4, after_dig_node = after_dig_node, on_rotate = screwdriver.disallow, - networks = nworks, paramtype2 = "facedir", on_rotate = screwdriver.disallow, diggable = false, @@ -304,11 +266,12 @@ techage.register_node({"techage:t4_pump", "techage:t4_pump_on"}, { end, }) -Pipe:add_secondary_node_names({ +-- Pumps have to provide one output and one input side +liquid.register_nodes({ "techage:t3_pump", "techage:t3_pump_on", "techage:t4_pump", "techage:t4_pump_on", -}) - +}, Pipe, "pump", {"L", "R"}, {}) + minetest.register_craft({ output = "techage:t3_pump 2", recipe = { diff --git a/liquids/silo.lua b/liquids/silo.lua index 7b521f3..f570f78 100644 --- a/liquids/silo.lua +++ b/liquids/silo.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 @@ -18,7 +18,7 @@ local M = minetest.get_meta local S = techage.S local LQD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).liquid end local Pipe = techage.LiquidPipe -local liquid = techage.liquid +local liquid = networks.liquid local INV_SIZE = 8 local STACKMAX = 99 @@ -170,13 +170,6 @@ local tLiquid = { end, } -local tNetworks = { - pipe2 = { - sides = techage.networks.AllSides, -- Pipe connection sides - ntype = "tank", - }, -} - minetest.register_node("techage:ta3_silo", { description = S("TA3 Silo"), tiles = { @@ -203,16 +196,11 @@ minetest.register_node("techage:ta3_silo", { meta:set_string("infotext", S("TA3 Silo").." "..number) Pipe:after_place_node(pos) end, - tubelib2_on_update2 = function(pos, outdir, tlib2, node) - liquid.update_network(pos, outdir) - end, after_dig_node = function(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) techage.remove_node(pos, oldnode, oldmetadata) techage.del_mem(pos) end, - liquid = tLiquid, - networks = tNetworks, can_dig = can_dig, allow_metadata_inventory_put = allow_metadata_inventory_put, allow_metadata_inventory_take = allow_metadata_inventory_take, @@ -249,16 +237,11 @@ minetest.register_node("techage:ta4_silo", { meta:set_string("infotext", S("TA4 Silo").." "..number) Pipe:after_place_node(pos) end, - tubelib2_on_update2 = function(pos, outdir, tlib2, node) - liquid.update_network(pos, outdir) - end, after_dig_node = function(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) techage.remove_node(pos, oldnode, oldmetadata) techage.del_mem(pos) end, - liquid = tLiquid, - networks = tNetworks, can_dig = can_dig, allow_metadata_inventory_put = allow_metadata_inventory_put, allow_metadata_inventory_take = allow_metadata_inventory_take, @@ -335,7 +318,7 @@ techage.register_node({"techage:ta3_silo", "techage:ta4_silo"}, { end, }) -Pipe:add_secondary_node_names({"techage:ta3_silo", "techage:ta4_silo"}) +liquid.register_nodes({"techage:ta3_silo", "techage:ta4_silo"}, Pipe, "tank", nil, tLiquid) minetest.register_craft({ output = "techage:ta3_silo", diff --git a/liquids/tank.lua b/liquids/tank.lua index 0e53d05..72dad47 100644 --- a/liquids/tank.lua +++ b/liquids/tank.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 @@ -15,23 +15,25 @@ local S2P = minetest.string_to_pos local P2S = minetest.pos_to_string local M = minetest.get_meta +local N = tubelib2.get_node_lvm +local LQD = function(pos) return (minetest.registered_nodes[N(pos).name] or {}).liquid end local S = techage.S local Pipe = techage.LiquidPipe -local liquid = techage.liquid +local liquid = networks.liquid local CAPACITY = 1000 local function on_rightclick(pos, node, clicker) local nvm = techage.get_nvm(pos) techage.set_activeformspec(pos, clicker) - M(pos):set_string("formspec", liquid.formspec(pos, nvm)) + M(pos):set_string("formspec", techage.liquid.formspec(pos, nvm)) minetest.get_node_timer(pos):start(2) end local function node_timer(pos, elapsed) if techage.is_activeformspec(pos) then local nvm = techage.get_nvm(pos) - M(pos):set_string("formspec", liquid.formspec(pos, nvm)) + M(pos):set_string("formspec", techage.liquid.formspec(pos, nvm)) return true end return false @@ -41,14 +43,19 @@ local function can_dig(pos, player) if minetest.is_protected(pos, player:get_player_name()) then return false end - return liquid.is_empty(pos) + return techage.liquid.is_empty(pos) +end + +local function peek_liquid(pos) + local nvm = techage.get_nvm(pos) + return liquid.srv_peek(nvm) end local function take_liquid(pos, indir, name, amount) - amount, name = liquid.srv_take(pos, indir, name, amount) + local nvm = techage.get_nvm(pos) + amount, name = liquid.srv_take(nvm, name, amount) if techage.is_activeformspec(pos) then - local nvm = techage.get_nvm(pos) - M(pos):set_string("formspec", liquid.formspec(pos, nvm)) + M(pos):set_string("formspec", techage.liquid.formspec(pos, nvm)) end return amount, name end @@ -57,10 +64,10 @@ local function put_liquid(pos, indir, name, amount) -- check if it is not powder local ndef = minetest.registered_craftitems[name] or {} if not ndef.groups or ndef.groups.powder ~= 1 then - local leftover = liquid.srv_put(pos, indir, name, amount) + local nvm = techage.get_nvm(pos) + local leftover = liquid.srv_put(nvm, name, amount, LQD(pos).capa) if techage.is_activeformspec(pos) then - local nvm = techage.get_nvm(pos) - M(pos):set_string("formspec", liquid.formspec(pos, nvm)) + M(pos):set_string("formspec", techage.liquid.formspec(pos, nvm)) end return leftover end @@ -68,21 +75,14 @@ local function put_liquid(pos, indir, name, amount) end local function untake_liquid(pos, indir, name, amount) - local leftover = liquid.srv_put(pos, indir, name, amount) + local nvm = techage.get_nvm(pos) + local leftover = liquid.srv_put(nvm, name, amount, LQD(pos).capa) if techage.is_activeformspec(pos) then - local nvm = techage.get_nvm(pos) - M(pos):set_string("formspec", liquid.formspec(pos, nvm)) + M(pos):set_string("formspec", techage.liquid.formspec(pos, nvm)) end return leftover end -local networks_def = { - pipe2 = { - sides = techage.networks.AllSides, -- Pipe connection sides - ntype = "tank", - }, -} - minetest.register_node("techage:ta3_tank", { description = S("TA3 Tank"), tiles = { @@ -101,27 +101,16 @@ minetest.register_node("techage:ta3_tank", { local number = techage.add_node(pos, "techage:ta3_tank") meta:set_string("node_number", number) meta:set_string("owner", placer:get_player_name()) - meta:set_string("formspec", liquid.formspec(pos, nvm)) + meta:set_string("formspec", techage.liquid.formspec(pos, nvm)) meta:set_string("infotext", S("TA3 Tank").." "..number) Pipe:after_place_node(pos) end, - tubelib2_on_update2 = function(pos, outdir, tlib2, node) - liquid.update_network(pos, outdir) - end, on_timer = node_timer, - on_punch = liquid.on_punch, + on_punch = techage.liquid.on_punch, after_dig_node = function(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) techage.remove_node(pos, oldnode, oldmetadata) end, - liquid = { - capa = CAPACITY, - peek = liquid.srv_peek, - put = put_liquid, - take = take_liquid, - untake = untake_liquid, - }, - networks = networks_def, on_rightclick = on_rightclick, can_dig = can_dig, paramtype2 = "facedir", @@ -131,6 +120,16 @@ minetest.register_node("techage:ta3_tank", { sounds = default.node_sound_metal_defaults(), }) +liquid.register_nodes({"techage:ta3_tank"}, + Pipe, "tank", nil, { + capa = CAPACITY, + peek = peek_liquid, + put = put_liquid, + take = take_liquid, + untake = untake_liquid, + } +) + minetest.register_node("techage:oiltank", { description = S("Oil Tank"), tiles = { @@ -161,27 +160,16 @@ minetest.register_node("techage:oiltank", { local number = techage.add_node(pos, "techage:oiltank") meta:set_string("node_number", number) meta:set_string("owner", placer:get_player_name()) - meta:set_string("formspec", liquid.formspec(pos, nvm)) + meta:set_string("formspec", techage.liquid.formspec(pos, nvm)) meta:set_string("infotext", S("Oil Tank").." "..number) Pipe:after_place_node(pos) end, - tubelib2_on_update2 = function(pos, outdir, tlib2, node) - liquid.update_network(pos, outdir) - end, on_timer = node_timer, - on_punch = liquid.on_punch, + on_punch = techage.liquid.on_punch, after_dig_node = function(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) techage.remove_node(pos, oldnode, oldmetadata) end, - liquid = { - capa = CAPACITY * 4, - peek = liquid.srv_peek, - put = put_liquid, - take = take_liquid, - untake = untake_liquid, - }, - networks = networks_def, on_rightclick = on_rightclick, can_dig = can_dig, paramtype2 = "facedir", @@ -191,6 +179,16 @@ minetest.register_node("techage:oiltank", { sounds = default.node_sound_metal_defaults(), }) +liquid.register_nodes({"techage:oiltank"}, + Pipe, "tank", nil, { + capa = CAPACITY * 4, + peek = peek_liquid, + put = put_liquid, + take = take_liquid, + untake = untake_liquid, + } +) + minetest.register_node("techage:ta4_tank", { description = S("TA4 Tank"), tiles = { @@ -210,13 +208,10 @@ minetest.register_node("techage:ta4_tank", { local number = techage.add_node(pos, "techage:ta4_tank") meta:set_string("node_number", number) meta:set_string("owner", placer:get_player_name()) - meta:set_string("formspec", liquid.formspec(pos, nvm)) + meta:set_string("formspec", techage.liquid.formspec(pos, nvm)) meta:set_string("infotext", S("TA4 Tank").." "..number) Pipe:after_place_node(pos) end, - tubelib2_on_update2 = function(pos, outdir, tlib2, node) - liquid.update_network(pos, outdir) - end, on_receive_fields = function(pos, formname, fields, player) if minetest.is_protected(pos, player:get_player_name()) then return @@ -226,19 +221,11 @@ minetest.register_node("techage:ta4_tank", { end end, on_timer = node_timer, - on_punch = liquid.on_punch, + on_punch = techage.liquid.on_punch, after_dig_node = function(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) techage.remove_node(pos, oldnode, oldmetadata) end, - liquid = { - capa = CAPACITY * 2, - peek = liquid.srv_peek, - put = put_liquid, - take = take_liquid, - untake = untake_liquid, - }, - networks = networks_def, on_rightclick = on_rightclick, can_dig = can_dig, paramtype2 = "facedir", @@ -248,9 +235,17 @@ minetest.register_node("techage:ta4_tank", { sounds = default.node_sound_metal_defaults(), }) -techage.register_node({"techage:ta3_tank", "techage:ta4_tank", "techage:oiltank"}, liquid.recv_message) +liquid.register_nodes({"techage:ta4_tank"}, + Pipe, "tank", nil, { + capa = CAPACITY * 2, + peek = peek_liquid, + put = put_liquid, + take = take_liquid, + untake = untake_liquid, + } +) -Pipe:add_secondary_node_names({"techage:ta3_tank", "techage:ta4_tank", "techage:oiltank"}) +techage.register_node({"techage:ta3_tank", "techage:ta4_tank", "techage:oiltank"}, techage.liquid.recv_message) minetest.register_craft({ output = "techage:ta3_tank 2", diff --git a/liquids/valve.lua b/liquids/valve.lua index a3ccc68..afea000 100644 --- a/liquids/valve.lua +++ b/liquids/valve.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 @@ -17,73 +17,7 @@ local P2S = minetest.pos_to_string local M = minetest.get_meta local S = techage.S local Pipe = techage.LiquidPipe -local liquid = techage.liquid - -local function switch_node(pos, node) - if node.name == "techage:ta3_valve_open" then - node.name = "techage:ta3_valve_closed" - --node.name = "default:dirt" - minetest.swap_node(pos, node) - local number = M(pos):get_string("node_number") - M(pos):set_string("infotext", S("TA3 Valve closed")..": "..number) - Pipe:after_dig_tube(pos, {name = "techage:ta3_valve_open", param2 = node.param2}) - elseif node.name == "techage:ta3_valve_closed" then - node.name = "techage:ta3_valve_open" - minetest.swap_node(pos, node) - local number = M(pos):get_string("node_number") - M(pos):set_string("infotext", S("TA3 Valve open")..": "..number) - Pipe:after_place_tube(pos) - end - minetest.sound_play("techage_valve", { - pos = pos, - gain = 1, - max_hear_distance = 10}) -end - -local function on_rightclick(pos, node, clicker) - if not minetest.is_protected(pos, clicker:get_player_name()) then - switch_node(pos, node) - end -end - ---local function node_timer(pos, elapsed) --- if techage.is_activeformspec(pos) then --- local nvm = techage.get_nvm(pos) --- M(pos):set_string("formspec", liquid.formspec(pos, nvm)) --- return true --- end --- return false ---end - ---local function can_dig(pos, player) --- if minetest.is_protected(pos, player:get_player_name()) then --- return false --- end --- return liquid.is_empty(pos) ---end - ---local function take_liquid(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", liquid.formspec(pos, nvm)) --- end --- return amount, name ---end - ---local function put_liquid(pos, indir, name, amount) --- -- check if it is not powder --- local ndef = minetest.registered_craftitems[name] or {} --- if not ndef.groups or ndef.groups.powder ~= 1 then --- local leftover = liquid.srv_put(pos, indir, name, amount) --- if techage.is_activeformspec(pos) then --- local nvm = techage.get_nvm(pos) --- M(pos):set_string("formspec", liquid.formspec(pos, nvm)) --- end --- return leftover --- end --- return amount ---end +local liquid = networks.liquid minetest.register_node("techage:ta3_valve_open", { description = S("TA Valve"), @@ -102,21 +36,24 @@ minetest.register_node("techage:ta3_valve_open", { return true end local meta = M(pos) - local nvm = techage.get_nvm(pos) - nvm.liquid = {} local number = techage.add_node(pos, "techage:ta3_valve_closed") meta:set_string("node_number", number) meta:set_string("owner", placer:get_player_name()) - meta:set_string("infotext", S("TA3 Valve open")..": "..number) + meta:set_string("infotext", S("TA3 Valve")..": "..number) return false end, - + on_rightclick = function(pos, node, clicker) + if liquid.turn_valve_off(pos, Pipe, "techage:ta3_valve_closed", "techage:ta3_valve_open") then + minetest.sound_play("techage_valve", { + pos = pos, + gain = 1, + max_hear_distance = 10}) + end + end, after_dig_node = function(pos, oldnode, oldmetadata, digger) Pipe:after_dig_tube(pos, oldnode, oldmetadata) end, - on_rightclick = on_rightclick, - paramtype2 = "facedir", -- important! drawtype = "nodebox", node_box = { @@ -146,17 +83,19 @@ minetest.register_node("techage:ta3_valve_closed", { "techage_gaspipe_valve_hole.png", }, - tubelib2_on_update2 = function(pos, outdir, tlib2, node) - liquid.update_network(pos, outdir) + on_rightclick = function(pos, node, clicker) + if liquid.turn_valve_on(pos, Pipe, "techage:ta3_valve_closed", "techage:ta3_valve_open") then + minetest.sound_play("techage_valve", { + pos = pos, + gain = 1, + max_hear_distance = 10}) + end end, - after_dig_node = function(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) techage.remove_node(pos, oldnode, oldmetadata) end, - on_rightclick = on_rightclick, - paramtype2 = "facedir", -- important! drawtype = "nodebox", node_box = { @@ -180,10 +119,10 @@ techage.register_node({"techage:ta3_valve_closed", "techage:ta3_valve_open"}, { on_recv_message = function(pos, src, topic, payload) local node = techage.get_node_lvm(pos) if topic == "on" and node.name == "techage:ta3_valve_closed" then - switch_node(pos, node) + liquid.turn_valve_on(pos, Pipe, "techage:ta3_valve_closed", "techage:ta3_valve_open") return true elseif topic == "off" and node.name == "techage:ta3_valve_open" then - switch_node(pos, node) + liquid.turn_valve_off(pos, Pipe, "techage:ta3_valve_closed", "techage:ta3_valve_open") return true elseif topic == "state" then if node.name == "techage:ta3_valve_open" then @@ -196,6 +135,8 @@ techage.register_node({"techage:ta3_valve_closed", "techage:ta3_valve_open"}, { end, }) +liquid.register_nodes({"techage:ta3_valve_closed"}, Pipe, "special", {}, {}) + minetest.register_craft({ output = "techage:ta3_valve_open", recipe = { diff --git a/liquids/waterpump.lua b/liquids/waterpump.lua index 4d69f73..6fd716f 100644 --- a/liquids/waterpump.lua +++ b/liquids/waterpump.lua @@ -87,7 +87,7 @@ local function pumping(pos, nvm) power.consumer_alive(pos, Cable, CYCLE_TIME) end if nvm.running then - local leftover = liquid.put(pos, 6, "techage:water", 1) + local leftover = liquid.put(pos, Pipe, 6, "techage:water", 1) if leftover and leftover > 0 then State:blocked(pos, nvm) return @@ -126,7 +126,6 @@ end local function after_dig_node(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) Cable:after_dig_node(pos) - liquid.after_dig_pump(pos) techage.del_mem(pos) end diff --git a/oil/distiller.lua b/oil/distiller.lua index e8fcc6e..ac57644 100644 --- a/oil/distiller.lua +++ b/oil/distiller.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 @@ -17,8 +17,7 @@ local P2S = minetest.pos_to_string local M = minetest.get_meta local S = techage.S local Pipe = techage.LiquidPipe -local networks = techage.networks -local liquid = techage.liquid +local liquid = networks.liquid local function orientation(pos, names) @@ -40,10 +39,6 @@ local function after_place_node(pos, placer) Pipe:after_place_node(pos) end -local function tubelib2_on_update2(pos, dir, tlib2, node) - liquid.update_network(pos, dir, tlib2) -end - local function after_dig_node(pos, oldnode, oldmetadata, digger) Pipe:after_dig_node(pos) end @@ -73,9 +68,7 @@ minetest.register_node("techage:ta3_distiller_base", { type = "fixed", fixed = {-1/2, -1/2, -1/2, 1/2, 1/2, 1/2}, }, - after_place_node = after_place_node, - tubelib2_on_update2 = tubelib2_on_update2, after_dig_node = after_dig_node, paramtype2 = "facedir", @@ -83,15 +76,10 @@ minetest.register_node("techage:ta3_distiller_base", { groups = {cracky=2}, is_ground_content = false, sounds = default.node_sound_stone_defaults(), - - networks = { - pipe2 = { - sides = {B=1}, -- Pipe connection sides - ntype = "pump", - }, - }, }) +liquid.register_nodes({"techage:ta3_distiller_base"}, Pipe, "pump", {"B"}, {}) + minetest.register_node("techage:ta3_distiller1", { description = S("TA3 Distillation Tower 1"), tiles = {"techage_distiller1.png"}, @@ -112,8 +100,6 @@ minetest.register_node("techage:ta3_distiller1", { after_place_node(pos, placer) return res end, - - tubelib2_on_update2 = tubelib2_on_update2, after_dig_node = after_dig_node, paramtype = "light", @@ -122,15 +108,10 @@ minetest.register_node("techage:ta3_distiller1", { groups = {cracky=2}, is_ground_content = false, sounds = default.node_sound_metal_defaults(), - - networks = { - pipe2 = { - sides = {F=1}, -- Pipe connection sides - ntype = "tank", - }, - }, }) +liquid.register_nodes({"techage:ta3_distiller1"}, Pipe, "pump", {"F"}, {}) + minetest.register_node("techage:ta3_distiller2", { description = S("TA3 Distillation Tower 2"), tiles = {"techage_distiller2.png"}, @@ -175,8 +156,6 @@ minetest.register_node("techage:ta3_distiller3", { local res = orientation(pos, {"techage:ta3_distiller2"}) return res end, - - tubelib2_on_update2 = tubelib2_on_update2, after_dig_node = after_dig_node, paramtype = "light", @@ -185,15 +164,10 @@ minetest.register_node("techage:ta3_distiller3", { groups = {cracky=2}, is_ground_content = false, sounds = default.node_sound_metal_defaults(), - - networks = { - pipe2 = { - sides = {B=1}, -- Pipe connection sides - ntype = "pump", - }, - }, }) +liquid.register_nodes({"techage:ta3_distiller3"}, Pipe, "pump", {"B"}, {}) + minetest.register_node("techage:ta3_distiller4", { description = S("TA3 Distillation Tower 4"), tiles = {"techage_distiller4.png"}, @@ -213,8 +187,6 @@ minetest.register_node("techage:ta3_distiller4", { after_place_node(pos, placer) return res end, - - tubelib2_on_update2 = tubelib2_on_update2, after_dig_node = after_dig_node, paramtype = "light", @@ -223,40 +195,28 @@ minetest.register_node("techage:ta3_distiller4", { groups = {cracky=2}, is_ground_content = false, sounds = default.node_sound_metal_defaults(), - - networks = { - pipe2 = { - sides = {U=1}, -- Pipe connection sides - ntype = "pump", - }, - }, }) +liquid.register_nodes({"techage:ta3_distiller4"}, Pipe, "pump", {"U"}, {}) -Pipe:add_secondary_node_names({ - "techage:ta3_distiller_base", "techage:ta3_distiller1", - "techage:ta3_distiller3", "techage:ta3_distiller4", -}) - -local Liquids = { - [-1] = "techage:bitumen", - [2] = "techage:fueloil", - [4] = "techage:naphtha", - [6] = "techage:gasoline", - [7] = "techage:gas", -} +local Liquids = {"techage:bitumen", "techage:fueloil", "techage:naphtha", "techage:gasoline", "techage:gas"} +local YPos = {-1, 2, 4, 6, 7} techage.register_node({"techage:ta3_distiller1"}, { on_transfer = function(pos, in_dir, topic, payload) if topic == "put" then - local leftover = 0 - local outdir = M(pos):get_int("outdir") - for _,y in ipairs({-1, 2, 4, 6, 7}) do - local pos2 = {x = pos.x, y = pos.y + y, z = pos.z} - if y == 7 then - outdir = 6 - end - leftover = leftover + liquid.put(pos2, outdir, Liquids[y], 1) + local nvm = techage.get_nvm(pos) + nvm.idx = nvm.idx or 1 + local outdir + if nvm.idx == 5 then + outdir = 6 -- up + else + outdir = M(pos):get_int("outdir") + end + local pos2 = {x = pos.x, y = pos.y + YPos[nvm.idx], z = pos.z} + local leftover = liquid.put(pos2, Pipe, outdir, Liquids[nvm.idx], 1) + if leftover == 0 then + nvm.idx = (nvm.idx % 5) + 1 end return leftover end diff --git a/oil/drillbox.lua b/oil/drillbox.lua index 042afa2..bcc4466 100644 --- a/oil/drillbox.lua +++ b/oil/drillbox.lua @@ -238,7 +238,7 @@ local tubing = { local nvm = techage.get_nvm(pos) if not nvm.assemble_locked then local meta = minetest.get_meta(pos) - if meta:get_int("push_dir") == in_dir or in_dir == 5 then + if meta:get_int("push_dir") == in_dir then local inv = M(pos):get_inventory() CRD(pos).State:start_if_standby(pos) return techage.put_items(inv, "src", stack) @@ -299,6 +299,7 @@ local _, node_name_ta3, _ = sounds = default.node_sound_wood_defaults(), num_items = {0,1,1,1}, power_consumption = {0,10,16,24}, + tube_sides = {L=1, R=1}, }, {false, false, true, false}) -- TA3 only diff --git a/oil/explore.lua b/oil/explore.lua index c4d4180..c7f6e2c 100644 --- a/oil/explore.lua +++ b/oil/explore.lua @@ -23,7 +23,7 @@ local OIL_MAX = 20000 local DEPTH_MIN = 16 local DEPTH_MAX = 25*16 local DEPTH_STEP = 16 -local YPOS_MAX = -6*16 -- oil can found below this level +local YPOS_MAX = -6*16 -- oil can't found below this level local OIL_BUBBLE_SIZE = 4096 local seed = tonumber(minetest.settings:get("techage_oil_exploration_seed")) or 1234 -- confidental! diff --git a/oil/pumpjack.lua b/oil/pumpjack.lua index eb70012..4fbc016 100644 --- a/oil/pumpjack.lua +++ b/oil/pumpjack.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 @@ -18,7 +18,7 @@ local M = minetest.get_meta local P = minetest.string_to_pos local S = techage.S local Pipe = techage.LiquidPipe -local liquid = techage.liquid +local liquid = networks.liquid -- Consumer Related Data local CRD = function(pos) return (minetest.registered_nodes[techage.get_node_lvm(pos).name] or {}).consumer end @@ -103,7 +103,7 @@ end local function pumping(pos, crd, meta, nvm) if has_oil(pos, meta) then - local leftover = liquid.put(pos, 6, "techage:oil_source", 1) + local leftover = liquid.put(pos, Pipe, 6, "techage:oil_source", 1) if leftover and leftover > 0 then crd.State:blocked(pos, nvm) stop_sound(pos) @@ -211,12 +211,6 @@ local _, node_name_ta3, _ = end Pipe:after_place_node(pos) end, - networks = { - pipe2 = { - sides = {U = 1}, -- Pipe connection side - ntype = "pump", - }, - }, power_sides = {F=1, B=1, L=1, R=1, D=1}, on_rightclick = on_rightclick, on_receive_fields = on_receive_fields, @@ -244,5 +238,5 @@ minetest.register_craft({ }, }) -Pipe:add_secondary_node_names({"techage:ta3_pumpjack_pas", "techage:ta3_pumpjack_act"}) +liquid.register_nodes({"techage:ta3_pumpjack_pas", "techage:ta3_pumpjack_act"}, Pipe, "pump", {"U"}, {}) diff --git a/oil/reboiler.lua b/oil/reboiler.lua index d395f37..94044c5 100644 --- a/oil/reboiler.lua +++ b/oil/reboiler.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 @@ -16,15 +16,14 @@ local S2P = minetest.string_to_pos local P2S = minetest.pos_to_string local M = minetest.get_meta local S = techage.S +local Flip = networks.Flip local Pipe = techage.LiquidPipe -local networks = techage.networks -local liquid = techage.liquid -local Flip = techage.networks.Flip local Cable = techage.ElectricCable -local power = techage.power +local liquid = networks.liquid +local power = networks.power -local CYCLE_TIME = 16 -local CAPA = 12 +local CYCLE_TIME = 2 +local WAITING_CYCLES = 5 -- in case BLOCKED local PWR_NEEDED = 14 local function play_sound(pos) @@ -51,90 +50,98 @@ end local function swap_node(pos, on) local nvm = techage.get_nvm(pos) - if on then - local node = techage.get_node_lvm(pos) + local node = techage.get_node_lvm(pos) + if on and node.name == "techage:ta3_reboiler" then node.name = "techage:ta3_reboiler_on" minetest.swap_node(pos, node) - minetest.get_node_timer(pos):start(CYCLE_TIME) play_sound(pos) - elseif not on and nvm.running then - local node = techage.get_node_lvm(pos) + elseif not on and node.name == "techage:ta3_reboiler_on" then node.name = "techage:ta3_reboiler" minetest.swap_node(pos, node) - minetest.get_node_timer(pos):stop() - nvm.running = false - power.consumer_stop(pos, Cable) stop_sound(pos) end end -local function on_power(pos) - swap_node(pos, true) -end - -local function on_nopower(pos) - swap_node(pos, false) -end - -local function is_running(pos, nvm) - return nvm.running -end - -local function pump_cmnd(pos, cmnd, payload) - return techage.transfer( +local function pump_cmnd(pos) + local leftover = techage.transfer( pos, "R", -- outdir - cmnd, -- topic - payload, -- payload + "put", -- topic + nil, -- payload Pipe, -- Pipe {"techage:ta3_distiller1"}) + + -- number of processed oil items + return 1 - (tonumber(leftover) or 1) end -local function node_timer(pos, elapsed) - local nvm = techage.get_nvm(pos) - nvm.liquid = nvm.liquid or {} - nvm.liquid.amount = nvm.liquid.amount or 0 - - if not nvm.error or nvm.error == 0 then - power.consumer_alive(pos, Cable, CYCLE_TIME) +local function new_state(pos, nvm, state) + if nvm.state ~= state then + nvm.state = state + M(pos):set_string("infotext", S("TA3 Oil Reboiler") .. ": " .. techage.StateStrings[state]) + swap_node(pos, state == techage.RUNNING) end - - if nvm.liquid.amount >= 5 and nvm.liquid.name == "techage:oil_source" then - nvm.liquid.amount = nvm.liquid.amount - 5 - local leftover = pump_cmnd(pos, "put") - if (tonumber(leftover) or 1) > 0 then - nvm.liquid.amount = nvm.liquid.amount + 5 - nvm.error = 2 -- = 2 pump cycles - M(pos):set_string("infotext", S("TA3 Oil Reboiler: blocked")) - swap_node(pos, false) - return false - end - return true - end - swap_node(pos, false) - return false -end +end -local function start_node(pos) +local function on_timer(pos) local nvm = techage.get_nvm(pos) - if nvm.running then return end + nvm.oil_amount = nvm.oil_amount or 0 - nvm.liquid = nvm.liquid or {} - nvm.liquid.amount = nvm.liquid.amount or 0 - if nvm.liquid.amount >= 5 and nvm.liquid.name == "techage:oil_source" then - if power.power_available(pos, Cable) then - if node_timer(pos, CYCLE_TIME) then - nvm.running = true - power.consumer_start(pos, Cable, CYCLE_TIME) - minetest.get_node_timer(pos):start(CYCLE_TIME) + -- Power handling + if nvm.state == techage.STOPPED then + local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED) + if consumed == PWR_NEEDED then + new_state(pos, nvm, techage.RUNNING) + return true + end + elseif nvm.state == techage.RUNNING then + local consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED) + if consumed < PWR_NEEDED then + local nvm = techage.get_nvm(pos) + new_state(pos, nvm, techage.STOPPED) + return true + end + elseif nvm.state == techage.BLOCKED or nvm.state == techage.STANDBY then + if not power.power_available(pos, Cable) then + local nvm = techage.get_nvm(pos) + new_state(pos, nvm, techage.STOPPED) + return true + end + end + + -- Oil handling + if nvm.state == techage.RUNNING then + if nvm.oil_amount >= 1 then + local processed = pump_cmnd(pos) + nvm.oil_amount = nvm.oil_amount - processed + nvm.waiting_cycles = WAITING_CYCLES + if processed == 0 then + new_state(pos, nvm, techage.BLOCKED) + else + new_state(pos, nvm, techage.RUNNING) + end + else + nvm.waiting_cycles = nvm.waiting_cycles - 1 + if nvm.waiting_cycles <= 0 then + new_state(pos, nvm, techage.STANDBY) end end + elseif nvm.state == techage.BLOCKED then + nvm.waiting_cycles = nvm.waiting_cycles - 1 + if nvm.waiting_cycles <= 0 then + new_state(pos, nvm, techage.RUNNING) + end + else + -- STANDBY: 'put' will trigger the state change end + return true end local function after_place_node(pos) + local nvm = techage.get_nvm(pos) + new_state(pos, nvm, techage.STOPPED) Pipe:after_place_node(pos) - Cable:after_place_node(pos) + Cable.after_place_node(pos) end local function after_dig_node(pos, oldnode) @@ -143,51 +150,13 @@ local function after_dig_node(pos, oldnode) techage.del_mem(pos) end -local function tubelib2_on_update2(pos, outdir, tlib2, node) - if tlib2.tube_type == "pipe2" then - liquid.update_network(pos, outdir, tlib2) - else - power.update_network(pos, outdir, tlib2) - end +local function on_rightclick(pos, node, clicker) + local nvm = techage.get_nvm(pos) + nvm.oil_amount = 0 + new_state(pos, nvm, techage.STOPPED) + minetest.get_node_timer(pos):start(CYCLE_TIME) end -local liquid_def = { - capa = CAPA, - peek = liquid.srv_peek, - put = function(pos, indir, name, amount) - local nvm = techage.get_nvm(pos) - if nvm.error and nvm.error > 0 then - nvm.error = nvm.error - 1 - if nvm.error <= 0 then - M(pos):set_string("infotext", S("TA3 Oil Reboiler")) - start_node(pos) - return liquid.srv_put(pos, indir, name, amount) - else - return amount - end - else - start_node(pos) - return liquid.srv_put(pos, indir, name, amount) - end - end, - take = liquid.srv_take, -} - -local net_def = { - pipe2 = { - sides = {L = true, R = true}, -- Pipe connection sides - ntype = "tank", - }, - ele1 = { - sides = techage.networks.AllSides, -- Cable connection sides - ntype = "con1", - on_power = on_power, - on_nopower = on_nopower, - nominal = PWR_NEEDED, - is_running = is_running, - }, -} - minetest.register_node("techage:ta3_reboiler", { description = S("TA3 Oil Reboiler"), tiles = { @@ -200,26 +169,10 @@ minetest.register_node("techage:ta3_reboiler", { "techage_filling_ta3.png^techage_appl_reboiler.png^techage_frame_ta3.png", }, - after_place_node = function(pos, placer) - local nvm = techage.get_nvm(pos) - nvm.liquid = {} - local meta = M(pos) - meta:set_string("infotext", S("TA3 Oil Reboiler")) - meta:set_int("outdir", networks.side_to_outdir(pos, "R")) - local number = techage.add_node(pos, "techage:ta3_reboiler") - meta:set_string("node_number", number) - meta:set_string("owner", placer:get_player_name()) - Pipe:after_place_node(pos) - power.after_place_node(pos) - end, - - tubelib2_on_update2 = tubelib2_on_update2, - on_timer = node_timer, + on_timer = on_timer, after_place_node = after_place_node, after_dig_node = after_dig_node, - after_dig_node = after_dig_node, - liquid = liquid_def, - networks = net_def, + on_rightclick = on_rightclick, paramtype2 = "facedir", on_rotate = screwdriver.disallow, @@ -258,10 +211,8 @@ minetest.register_node("techage:ta3_reboiler_on", { }, }, - tubelib2_on_update2 = tubelib2_on_update2, - on_timer = node_timer, - liquid = liquid_def, - networks = net_def, + on_timer = on_timer, + on_rightclick = on_rightclick, paramtype2 = "facedir", on_rotate = screwdriver.disallow, @@ -271,25 +222,43 @@ minetest.register_node("techage:ta3_reboiler_on", { sounds = default.node_sound_metal_defaults(), }) -Pipe:add_secondary_node_names({"techage:ta3_reboiler", "techage:ta3_reboiler_on"}) -Cable:add_secondary_node_names({"techage:ta3_reboiler", "techage:ta3_reboiler_on"}) +local liquid_def = { + peek = function(pos) + local nvm = techage.get_nvm(pos) + return liquid.srv_peek(nvm) + end, + put = function(pos, indir, name, amount) + local nvm = techage.get_nvm(pos) + nvm.oil_amount = nvm.oil_amount or 0 + + if nvm.state == techage.STANDBY or nvm.state == techage.RUNNING then + if name == "techage:oil_source" and amount > 0 then + if nvm.state == techage.STANDBY then + new_state(pos, nvm, techage.RUNNING) + end + -- Take one oil item every 2 cycles + -- Hint: We have to take two items, because the pump will pause for 4 cycles, + -- if nothing is taken. + nvm.take = nvm.take ~= true + if nvm.take and nvm.oil_amount < 5 then + nvm.oil_amount = nvm.oil_amount + 2 + return amount - 2 + end + end + end + return amount + end +} + +liquid.register_nodes({"techage:ta3_reboiler", "techage:ta3_reboiler_on"}, Pipe, "tank", {"L", "R"}, liquid_def) +power.register_nodes({"techage:ta3_reboiler", "techage:ta3_reboiler_on"}, Cable, "con") techage.register_node({"techage:ta3_reboiler", "techage:ta3_reboiler_on"}, { on_recv_message = function(pos, src, topic, payload) local nvm = techage.get_nvm(pos) - if topic == "on" then - start_node(pos) - return true - elseif topic == "off" then - swap_node(pos, false) - return true - elseif topic == "state" then - if nvm.error and nvm.error > 0 then - return "blocked" - elseif nvm.running then - return "running" - end - return "stopped" + if topic == "state" then + nvm.state = nvm.state or techage.STOPPED + return techage.StateStrings[nvm.state] else return "unsupported" end diff --git a/power/electric_cable.lua b/power/electric_cable.lua index 12f78b7..5cfaca3 100644 --- a/power/electric_cable.lua +++ b/power/electric_cable.lua @@ -37,6 +37,8 @@ local Cable = tubelib2.Tube:new({ minetest.swap_node(pos, {name = name, param2 = param2 % 32}) elseif name == "techage:power_line" or name == "techage:power_lineS" or name == "techage:power_lineA" then minetest.swap_node(pos, {name = "techage:power_line"..tube_type, param2 = param2 % 32}) + elseif name == "techage:power_pole2" then + -- nothing elseif not networks.hidden_name(pos) then minetest.swap_node(pos, {name = "techage:electric_cable"..tube_type, param2 = param2 % 32}) end @@ -47,7 +49,7 @@ local Cable = tubelib2.Tube:new({ -- Enable hidden cables networks.use_metadata(Cable) -networks.register_hidden_message("Use the tool to remove the node.") +networks.register_hidden_message("Use the trowel tool to remove the node.") networks.register_filling_items(techage.FILLING_ITEMS) -- Use global callback instead of node related functions diff --git a/power/formspecs.lua b/power/formspecs.lua index 5abddeb..b0e6038 100644 --- a/power/formspecs.lua +++ b/power/formspecs.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 @@ -24,10 +24,6 @@ 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 ------------------------------------------------------------------------------- @@ -131,8 +127,8 @@ function techage.formspec_charging_bar(pos, x, y, label, data) if data then charging = data.provided - data.consumed - consumed = data.consumed - available = data.available + consumed = round(data.consumed) + available = round(data.available) if charging > 0 then percent = 50 + (charging / data.available * 50) elseif charging < 0 then @@ -159,26 +155,6 @@ function techage.formspec_storage_bar(pos, x, y, label, 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 ------------------------------------------------------------------------------- @@ -195,36 +171,86 @@ function techage.storage_formspec(self, pos, nvm, label, netw_data, curr_load, m "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]" .. +function techage.generator_formspec(self, pos, nvm, label, provided, max_available) + return "size[5,4]" .. default.gui_bg .. default.gui_bg_img .. default.gui_slots .. - "box[0,-0.1;5.8,0.5;#c6e8ff]" .. + "box[0,-0.1;4.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) + "image_button[3.2,2.0;1,1;" .. self:get_state_button_image(nvm) .. ";state_button;]" .. + "tooltip[3.2,2.0;1,1;" .. self:get_state_tooltip(nvm) .. "]" 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 +function techage.generator_settings(tier, available) + if tier == "ta3" then + return { + { + type = "const", + name = "available", + label = S("Maximum output [ku]"), + tooltip = S("The maximum power the generator can provide"), + value = available, + }, + { + type = "output", + name = "provided", + label = S("Current output [ku]"), + tooltip = S("The current power the generator provides"), + }, + { + type = "dropdown", + choices = "40% - 60%,60% - 80%,80% - 100%", + name = "termpoint", + label = S("Charge termination"), + tooltip = S("Range in which the generator reduces its power"), + }, + } + else + return { + { + type = "const", + name = "available", + label = S("Maximum output [ku]"), + tooltip = S("The maximum power the generator can provide"), + value = available, + }, + { + type = "output", + name = "provided", + label = S("Current output [ku]"), + tooltip = S("The current power the generator provides"), + }, + { + type = "dropdown", + choices = "40% - 60%,60% - 80%,80% - 100%", + name = "termpoint", + label = S("Charge termination"), + tooltip = S("Range in which the generator reduces its power"), + }, + } end end - + +function techage.evaluate_charge_termination(nvm, meta) + local termpoint = meta:get_string("termpoint") + if termpoint == "40% - 60%" then + meta:set_string("termpoint1", 0.4) + meta:set_string("termpoint2", 0.6) + elseif termpoint == "60% - 80%" then + meta:set_string("termpoint1", 0.6) + meta:set_string("termpoint2", 0.8) + elseif termpoint == "80% - 100%" then + meta:set_string("termpoint1", 0.8) + meta:set_string("termpoint2", 1.0) + else + meta:set_string("termpoint", "80% - 100%") + meta:set_string("termpoint1", 0.8) + meta:set_string("termpoint2", 1.0) + 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/gearbox.lua b/power/gearbox.lua index 3444674..d444f24 100644 --- a/power/gearbox.lua +++ b/power/gearbox.lua @@ -130,6 +130,12 @@ minetest.register_node("techage:gearbox_on", { power.register_nodes({"techage:gearbox", "techage:gearbox_on"}, Axle, "junc") +techage.register_node({"techage:gearbox", "techage:gearbox_on"}, { + on_node_load = function(pos, node) + minetest.get_node_timer(pos):start(CYCLE_TIME) + end, +}) + minetest.register_craft({ output = "techage:gearbox 2", recipe = { diff --git a/power/laser.lua b/power/laser.lua index 7a0b359..58c89b4 100644 --- a/power/laser.lua +++ b/power/laser.lua @@ -19,8 +19,7 @@ local M = minetest.get_meta local S = techage.S local Cable = techage.ElectricCable -local power = techage.power -local networks = techage.networks +local power = networks.power minetest.register_node("techage:ta4_laser_emitter", { description = S("TA4 Laser Beam Emitter"), @@ -122,9 +121,7 @@ minetest.register_node("techage:ta4_laser_receiver", { sounds = default.node_sound_wood_defaults(), }) -Cable:add_secondary_node_names({"techage:ta4_laser_emitter", "techage:ta4_laser_receiver"}) -Cable:set_valid_sides("techage:ta4_laser_emitter", {"F"}) -Cable:set_valid_sides("techage:ta4_laser_receiver", {"F"}) +power.register_nodes({"techage:ta4_laser_emitter", "techage:ta4_laser_receiver"}, Cable, "special", {"F"}) minetest.register_craft({ output = "techage:ta4_laser_emitter", diff --git a/power/power_line.lua b/power/power_line.lua index a9f2db9..4d3e9c4 100644 --- a/power/power_line.lua +++ b/power/power_line.lua @@ -43,10 +43,7 @@ minetest.register_node("techage:power_line", { end, after_dig_node = function(pos, oldnode, oldmetadata, digger) - if oldmetadata and oldmetadata.fields and oldmetadata.fields.tl2_param2 then - oldnode.param2 = oldmetadata.fields.tl2_param2 - Cable:after_dig_tube(pos, oldnode) - end + Cable:after_dig_tube(pos, oldnode) end, paramtype2 = "facedir", -- important! @@ -85,10 +82,7 @@ minetest.register_node("techage:power_lineS", { end, after_dig_node = function(pos, oldnode, oldmetadata, digger) - if oldmetadata and oldmetadata.fields and oldmetadata.fields.tl2_param2 then - oldnode.param2 = oldmetadata.fields.tl2_param2 - Cable:after_dig_tube(pos, oldnode) - end + Cable:after_dig_tube(pos, oldnode) end, paramtype2 = "facedir", -- important! @@ -133,10 +127,7 @@ minetest.register_node("techage:power_lineA", { end, after_dig_node = function(pos, oldnode, oldmetadata, digger) - if oldmetadata and oldmetadata.fields and oldmetadata.fields.tl2_param2 then - oldnode.param2 = oldmetadata.fields.tl2_param2 - Cable:after_dig_tube(pos, oldnode) - end + Cable:after_dig_tube(pos, oldnode) end, paramtype2 = "facedir", -- important! @@ -213,13 +204,7 @@ minetest.register_node("techage:power_pole2", { end, can_dig = can_dig, after_dig_node = function(pos, oldnode, oldmetadata, digger) - if oldmetadata and oldmetadata.fields and oldmetadata.fields.tl2_param2 then - oldnode.param2 = oldmetadata.fields.tl2_param2 - Cable:after_dig_tube(pos, oldnode) - end - end, - tubelib2_on_update2 = function(pos, dir1, tlib2, node) - power.update_network(pos, nil, tlib2) + Cable:after_dig_tube(pos, oldnode) end, on_rotate = screwdriver.disallow, -- important! @@ -303,20 +288,10 @@ minetest.register_node("techage:power_pole_conn", { }, connects_to = {"techage:power_line", "techage:power_lineS", "techage:power_lineA"}, - -- after_place_node -- see techage:power_pole - tubelib2_on_update2 = function(pos, dir1, tlib2, node) - power.update_network(pos, nil, tlib2) - end, + can_dig = can_dig, after_dig_node = function(pos, oldnode, oldmetadata, digger) Cable:after_dig_node(pos) end, - can_dig = can_dig, - networks = { - ele1 = { - sides = networks.AllSides, -- connection sides for cables - ntype = "junc", - }, - }, drop = "techage:power_pole", on_rotate = screwdriver.disallow, -- important! @@ -328,8 +303,7 @@ minetest.register_node("techage:power_pole_conn", { sounds = default.node_sound_defaults(), }) -Cable:add_secondary_node_names({"techage:power_pole_conn"}) - +power.register_nodes({"techage:power_pole_conn"}, Cable, "junc") minetest.register_node("techage:power_pole3", { description = S("TA Power Pole"), diff --git a/power/ta4_cable.lua b/power/ta4_cable.lua index cbd2760..6650e05 100644 --- a/power/ta4_cable.lua +++ b/power/ta4_cable.lua @@ -18,7 +18,7 @@ local P2S = minetest.pos_to_string local M = minetest.get_meta local S = techage.S -local power = techage.power +local power = networks.power local ELE2_MAX_CABLE_LENGHT = 200 @@ -40,13 +40,10 @@ local Cable = tubelib2.Tube:new({ end, }) ---Cable:register_on_tube_update(function(node, pos, out_dir, peer_pos, peer_in_dir) --- local ndef = minetest.registered_nodes[node.name] --- if ndef and ndef.after_tube_update then --- minetest.registered_nodes[node.name].after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) --- end ---end) - +-- Use global callback instead of node related functions +Cable:register_on_tube_update2(function(pos, outdir, tlib2, node) + power.update_network(pos, outdir, tlib2, node) +end) minetest.register_node("techage:ta4_power_cableS", { description = S("TA4 Low Power Cable"), @@ -155,17 +152,11 @@ minetest.register_node("techage:ta4_power_box", { Cable:after_place_node(pos) end, tubelib2_on_update2 = function(pos, dir1, tlib2, node) - power.update_network(pos, nil, tlib2) + power.update_network(pos, 0, tlib2, node) end, after_dig_node = function(pos, oldnode, oldmetadata, digger) Cable:after_dig_node(pos) end, - networks = { - ele2 = { - sides = {L=1, R=1, F=1, B=1}, - ntype = "junc", - }, - }, on_rotate = screwdriver.disallow, -- important! paramtype = "light", @@ -176,7 +167,7 @@ minetest.register_node("techage:ta4_power_box", { sounds = default.node_sound_defaults(), }) -Cable:add_secondary_node_names({"techage:ta4_power_box"}) +power.register_nodes({"techage:ta4_power_box"}, Cable, "junc", {"L", "R", "F", "B"}) minetest.register_craft({ output = "techage:ta4_power_cableS 8", diff --git a/solar/inverter.lua b/solar/inverter.lua index 9930e31..f74fc13 100644 --- a/solar/inverter.lua +++ b/solar/inverter.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 @@ -20,8 +20,8 @@ local S2P = minetest.string_to_pos local Cable = techage.ElectricCable local Solar = techage.TA4_Cable -local power = techage.power -local networks = techage.networks +local power = networks.power +local control = networks.control local CYCLE_TIME = 2 local PWR_PERF = 100 @@ -29,13 +29,19 @@ local PWR_PERF = 100 local function determine_power(pos, nvm) -- determine DC node position local outdir = M(pos):get_int("leftdir") - local max_power, num_inverter = power.get_power(pos, outdir, Solar, "techage:ta4_solar_inverter") - if num_inverter == 1 then + local netw = networks.get_network_table(pos, Solar, outdir) or {} + local num_inv = #(netw.con or {}) + local max_power = 0 + for _, power in ipairs(control.request(pos, Solar, outdir, "junc", "power")) do + max_power = max_power + power + end + + if num_inv == 1 then -- only one inverter is allowed nvm.max_power = math.min(PWR_PERF, max_power) else nvm.max_power = 0 end - return max_power, num_inverter + return max_power, num_inv end local function determine_power_from_time_to_time(pos, nvm) @@ -46,7 +52,7 @@ local function determine_power_from_time_to_time(pos, nvm) return end nvm.ticks = nvm.ticks or 0 - if (nvm.ticks % 10) == 0 then -- calculate max_power not to often + if (nvm.ticks % 30) == 0 then -- calculate max_power not to often determine_power(pos, nvm) else nvm.max_power = nvm.max_power or 0 @@ -56,7 +62,7 @@ end local function formspec(self, pos, nvm) local max_power = nvm.max_power or 0 - local delivered = nvm.delivered or 0 + local provided = nvm.provided or 0 local arrow = "image[2.5,1.5;1,1;techage_form_arrow_bg.png^[transformR270]" if nvm.running then arrow = "image[2.5,1.5;1,1;techage_form_arrow_fg.png^[transformR270]" @@ -67,8 +73,8 @@ local function formspec(self, pos, nvm) default.gui_slots.. "box[0,-0.1;5.8,0.5;#c6e8ff]".. "label[2.5,-0.1;"..minetest.colorize( "#000000", S("Inverter")).."]".. - power.formspec_label_bar(pos, 0, 0.8, S("Power DC"), PWR_PERF, max_power).. - power.formspec_label_bar(pos, 3.5, 0.8, S("Power AC"), max_power, delivered).. + techage.formspec_power_bar(pos, 0, 0.8, S("Power DC"), max_power, PWR_PERF).. + techage.formspec_power_bar(pos, 3.5, 0.8, S("Power AC"), provided, max_power).. arrow.. "image_button[2.5,3;1,1;".. self:get_state_button_image(nvm) ..";state_button;]".. "tooltip[2.5,3;1,1;"..self:get_state_tooltip(nvm).."]" @@ -82,18 +88,20 @@ local function can_start(pos, nvm, state) end local function start_node(pos, nvm, state) + local meta = M(pos) nvm.running = true - nvm.delivered = 0 + nvm.provided = 0 nvm.ticks = 0 - local outdir = M(pos):get_int("outdir") - power.generator_start(pos, Cable, CYCLE_TIME, outdir, nvm.max_power) + local outdir = meta:get_int("outdir") + techage.evaluate_charge_termination(nvm, meta) + power.start_storage_calc(pos, Cable, outdir) end local function stop_node(pos, nvm, state) nvm.running = false - nvm.delivered = 0 + nvm.provided = 0 local outdir = M(pos):get_int("outdir") - power.generator_stop(pos, Cable, outdir) + power.start_storage_calc(pos, Cable, outdir) end local State = techage.NodeStates:new({ @@ -108,16 +116,18 @@ local State = techage.NodeStates:new({ }) local function node_timer(pos, elapsed) + local meta = M(pos) local nvm = techage.get_nvm(pos) determine_power_from_time_to_time(pos, nvm) - --if nvm.max_power > 0 then - local outdir = M(pos):get_int("outdir") - nvm.delivered = power.generator_alive(pos, Cable, CYCLE_TIME, outdir, nvm.max_power) - --else - --nvm.delivered = 0 - --end + local outdir = M(pos):get_int("outdir") + local tp1 = tonumber(meta:get_string("termpoint1")) + local tp2 = tonumber(meta:get_string("termpoint2")) + if nvm.max_power and nvm.max_power > 0 then + nvm.provided = power.provide_power(pos, Cable, outdir, nvm.max_power, tp1, tp2) + nvm.load = power.get_storage_load(pos, Cable, outdir, nvm.max_power) + end if techage.is_activeformspec(pos) then - M(pos):set_string("formspec", formspec(State, pos, nvm)) + meta:set_string("formspec", formspec(State, pos, nvm)) end State:trigger_state(pos, nvm) return true @@ -142,8 +152,11 @@ local function on_rightclick(pos, node, clicker) M(pos):set_string("formspec", formspec(State, pos, nvm)) 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) / nvm.max_power, perf = nvm.max_power, capa = nvm.max_power * 2} + end end minetest.register_node("techage:ta4_solar_inverter", { @@ -179,38 +192,48 @@ minetest.register_node("techage:ta4_solar_inverter", { techage.del_mem(pos) end, - tubelib2_on_update2 = tubelib2_on_update2, on_receive_fields = on_receive_fields, on_rightclick = on_rightclick, on_timer = node_timer, - networks = { - ele1 = { - sides = {R = 1}, - ntype = "gen1", - nominal = PWR_PERF, - regenerative = true, - }, - ele2 = { - sides = {L = 1}, - ntype = "con1", - }, - } + get_generator_data = get_generator_data, + ta3_formspec = techage.generator_settings("ta4", PWR_PERF) }) -Cable:add_secondary_node_names({"techage:ta4_solar_inverter"}) -Solar:add_secondary_node_names({"techage:ta4_solar_inverter"}) +power.register_nodes({"techage:ta4_solar_inverter"}, Cable, "gen", {"R"}) +power.register_nodes({"techage:ta4_solar_inverter"}, Solar, "con", {"L"}) techage.register_node({"techage:ta4_solar_inverter"}, { on_recv_message = function(pos, src, topic, payload) local nvm = techage.get_nvm(pos) if topic == "delivered" then - return math.floor((nvm.delivered or 0) + 0.5) + return math.floor((nvm.provided or 0) + 0.5) else return State:on_receive_message(pos, topic, payload) end end, }) +control.register_nodes({"techage:ta4_solar_inverter"}, { + on_receive = function(pos, tlib2, topic, payload) + end, + on_request = function(pos, tlib2, topic) + if topic == "info" then + local nvm = techage.get_nvm(pos) + local meta = M(pos) + return { + type = S("TA4 Solar Inverter"), + number = meta:get_string("node_number") or "", + running = nvm.running or false, + available = nvm.max_power or 0, + provided = nvm.provided or 0, + termpoint = meta:get_string("termpoint"), + } + end + return false + end, + } +) + minetest.register_craft({ output = "techage:ta4_solar_inverter", recipe = { diff --git a/solar/solarcell.lua b/solar/solarcell.lua index e303f90..96a94ce 100644 --- a/solar/solarcell.lua +++ b/solar/solarcell.lua @@ -20,7 +20,8 @@ local S = techage.S local PWR_PERF = 3 local Cable = techage.TA4_Cable -local power = techage.power +local power = networks.power +local control = networks.control local function temperature(pos) local data = minetest.get_biome_data(pos) @@ -32,7 +33,7 @@ end -- return the required param2 for solar modules local function get_param2(pos, side) - local dir = techage.networks.side_to_outdir(pos, side) + local dir = networks.side_to_outdir(pos, side) return (dir + 1) % 4 end @@ -89,25 +90,9 @@ local function after_dig_node(pos, oldnode) end local function tubelib2_on_update2(pos, outdir, tlib2, node) - power.update_network(pos, nil, tlib2) + power.update_network(pos, 0, tlib2, node) end -local net_def1 = { - ele2 = { - sides = {F=1, B=1}, - ntype = "junc", - on_getpower = on_getpower1, - }, -} - -local net_def2 = { - ele2 = { - sides = {F=1, B=1}, - ntype = "junc", - on_getpower = on_getpower2, - }, -} - minetest.register_node("techage:ta4_solar_module", { description = S("TA4 Solar Module"), inventory_image = "techage_solar_module_top.png", @@ -170,7 +155,6 @@ minetest.register_node("techage:ta4_solar_carrier", { after_place_node = after_place_node, after_dig_node = after_dig_node, tubelib2_on_update2 = tubelib2_on_update2, - networks = net_def1, paramtype = "light", use_texture_alpha = techage.CLIP, @@ -210,7 +194,6 @@ minetest.register_node("techage:ta4_solar_carrierB", { after_place_node = after_place_node, after_dig_node = after_dig_node, tubelib2_on_update2 = tubelib2_on_update2, - networks = net_def2, paramtype = "light", use_texture_alpha = techage.CLIP, @@ -249,8 +232,29 @@ minetest.register_node("techage:ta4_solar_carrierT", { sounds = default.node_sound_stone_defaults(), }) -Cable:add_secondary_node_names({"techage:ta4_solar_carrier", "techage:ta4_solar_carrierB"}) +power.register_nodes({"techage:ta4_solar_carrier", "techage:ta4_solar_carrierB"}, Cable, "junc", {"F", "B"}) +control.register_nodes({"techage:ta4_solar_carrier"}, { + on_receive = function(pos, tlib2, topic, payload) + end, + on_request = function(pos, tlib2, topic) + if topic == "power" then + return on_getpower1(pos) + end + end, + } +) + +control.register_nodes({"techage:ta4_solar_carrierB"}, { + on_receive = function(pos, tlib2, topic, payload) + end, + on_request = function(pos, tlib2, topic) + if topic == "power" then + return on_getpower2(pos) + end + end, + } +) minetest.register_craft({ output = "techage:ta4_solar_module", @@ -288,13 +292,3 @@ minetest.register_craft({ }, }) ---minetest.register_lbm({ --- label = "Repair Carrier Module", --- name = "techage:ta4_solar_carrier", --- nodenames = {"techage:ta4_solar_carrier", "techage:ta4_solar_carrierB"}, --- run_at_every_load = false, --- action = function(pos, node) --- local ndef = minetest.registered_nodes[node.name] --- ndef.after_place_node(pos) --- end, ---}) diff --git a/steam_engine/flywheel.lua b/steam_engine/flywheel.lua index 74fadec..eed0152 100644 --- a/steam_engine/flywheel.lua +++ b/steam_engine/flywheel.lua @@ -31,15 +31,7 @@ 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.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) .. "]" + return techage.generator_formspec(self, pos, nvm, S("Flywheel"), nvm.provided, PWR_PERF) end local function transfer_cylinder(pos, topic, payload) @@ -54,18 +46,18 @@ end local function start_node(pos, nvm, state) switch_axles(pos, true) local outdir = M(pos):get_int("outdir") - power.start_storage_calc(pos, Axle, outdir) transfer_cylinder(pos, "start") nvm.running = true + power.start_storage_calc(pos, Axle, outdir) end local function stop_node(pos, nvm, state) switch_axles(pos, false) local outdir = M(pos):get_int("outdir") - power.start_storage_calc(pos, Axle, outdir) nvm.provided = 0 transfer_cylinder(pos, "stop") nvm.running = false + power.start_storage_calc(pos, Axle, outdir) end local State = techage.NodeStates:new({ diff --git a/ta3_power/axle2power.lua b/ta3_power/axle2power.lua index cc06dfd..1663666 100644 --- a/ta3_power/axle2power.lua +++ b/ta3_power/axle2power.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 @@ -20,8 +20,7 @@ local S2P = minetest.string_to_pos local Cable = techage.ElectricCable local Axle = techage.Axle -local power = techage.power -local networks = techage.networks +local power = networks.power local CYCLE_TIME = 2 local PWR_PERF = 24 @@ -35,64 +34,54 @@ local function swap_node(pos, name) minetest.swap_node(pos, node) end -local function on_power(pos) +local function node_timer_on(pos, elapsed) + local meta = M(pos) local nvm = techage.get_nvm(pos) - nvm.axle = nvm.axle or {} - nvm.consumer_powered = true - M(pos):set_string("infotext", S("TA2 Power Generator")) - swap_node(pos, "techage:ta2_generator_on") - nvm.ticks = 0 - local outdir = M(pos):get_int("outdir") - nvm.axle.curr_power = techage.power.needed_power(pos, Cable, outdir) -end - -local function on_nopower(pos) - local nvm = techage.get_nvm(pos) - nvm.consumer_powered = false - if (nvm.ticks or 0) < 4 then - M(pos):set_string("infotext", S("TA2 Power Generator: Overload fault?\n(restart with right-click)")) - end - nvm.ticks = 0 -end - -local function node_timer(pos, elapsed) - local nvm = techage.get_nvm(pos) - nvm.axle = nvm.axle or {} + local outdir = meta:get_int("outdir") + nvm.buffer = nvm.buffer or 0 - -- trigger network on consumer side - nvm.ticks = (nvm.ticks or 0) + 1 - if nvm.ticks % 2 then - power.consumer_alive(pos, Axle, CYCLE_TIME) - end + local amount = math.min(PWR_PERF * 2 - nvm.buffer, PWR_PERF) + local taken = power.consume_power(pos, Axle, networks.Flip[outdir], amount) + print("node_timer_on", amount, taken, nvm.buffer) + nvm.buffer = nvm.buffer + taken - 1 -- some loss - -- handle generator side delayed - if nvm.ticks > 3 then + if nvm.buffer >= PWR_PERF then + local tp1 = tonumber(meta:get_string("termpoint1")) + local tp2 = tonumber(meta:get_string("termpoint2")) + nvm.provided = power.provide_power(pos, Cable, outdir, PWR_PERF, tp1, tp2) + nvm.load = power.get_storage_load(pos, Cable, outdir, PWR_PERF) + nvm.buffer = nvm.buffer - nvm.provided + print("provided", nvm.provided, nvm.load, nvm.buffer) + end + if amount > 0 and taken == 0 then + swap_node(pos, "techage:ta2_generator_off") local outdir = M(pos):get_int("outdir") + nvm.running = false + power.start_storage_calc(pos, Cable, outdir) + end + return true +end - if nvm.consumer_powered and not nvm.running_as_generator then - nvm.running_as_generator = true - power.generator_start(pos, Cable, CYCLE_TIME, outdir, nvm.max_power) - elseif not nvm.consumer_powered and nvm.running_as_generator then - nvm.running_as_generator = false - power.generator_stop(pos, Cable, outdir) - end - - if nvm.running_as_generator then - nvm.axle.curr_power = power.generator_alive(pos, Cable, CYCLE_TIME, outdir, PWR_PERF) + 1 - else - swap_node(pos, "techage:ta2_generator_off") - end +local function node_timer_off(pos, elapsed) + local nvm = techage.get_nvm(pos) + local outdir = M(pos):get_int("outdir") + + if power.power_available(pos, Axle, networks.Flip[outdir]) then + swap_node(pos, "techage:ta2_generator_on") + nvm.running = true + power.start_storage_calc(pos, Cable, outdir) end return true end -local function tubelib2_on_update2(pos, outdir, tlib2, node) +local function get_generator_data(pos, tlib2) local nvm = techage.get_nvm(pos) - nvm.axle = nvm.axle or {} - nvm.axle.curr_power = 1 - power.update_network(pos, outdir, tlib2) + if nvm.running then + return {level = (nvm.load or 0) / PWR_PERF, perf = PWR_PERF, capa = PWR_PERF * 2} + end end + minetest.register_node("techage:ta2_generator_off", { description = S("TA2 Power Generator"), tiles = { @@ -110,24 +99,10 @@ minetest.register_node("techage:ta2_generator_off", { is_ground_content = false, after_place_node = function(pos) - local nvm = techage.get_nvm(pos) - nvm.axle = nvm.axle or {} - nvm.axle.curr_power = 1 - nvm.consumer_powered = false - nvm.running_as_generator = false M(pos):set_int("outdir", networks.side_to_outdir(pos, "R")) - M(pos):set_int("leftdir", networks.side_to_outdir(pos, "L")) Cable:after_place_node(pos) Axle:after_place_node(pos) minetest.get_node_timer(pos):start(CYCLE_TIME) - power.consumer_start(pos, Axle, CYCLE_TIME*2) - M(pos):set_string("infotext", S("TA2 Power Generator")) - end, - - on_rightclick = function(pos, node, clicker) - local nvm = techage.get_nvm(pos) - nvm.axle = nvm.axle or {} - nvm.axle.curr_power = 1 M(pos):set_string("infotext", S("TA2 Power Generator")) end, @@ -137,21 +112,8 @@ minetest.register_node("techage:ta2_generator_off", { techage.del_mem(pos) end, - tubelib2_on_update2 = tubelib2_on_update2, - on_timer = node_timer, - networks = { - ele1 = { - sides = {R = 1}, - ntype = "gen1", - nominal = PWR_PERF, - }, - axle = { - sides = {L = 1}, - ntype = "con1", - on_power = on_power, - on_nopower = on_nopower, - }, - } + on_timer = node_timer_off, + get_generator_data = get_generator_data, }) minetest.register_node("techage:ta2_generator_on", { @@ -199,21 +161,8 @@ minetest.register_node("techage:ta2_generator_on", { groups = {not_in_creative_inventory=1}, diggable = false, - tubelib2_on_update2 = tubelib2_on_update2, - on_timer = node_timer, - networks = { - ele1 = { - sides = {R = 1}, - ntype = "gen1", - nominal = PWR_PERF, - }, - axle = { - sides = {L = 1}, - ntype = "con1", - on_power = on_power, - on_nopower = on_nopower, - }, - } + on_timer = node_timer_on, + get_generator_data = get_generator_data, }) techage.register_node({"techage:ta2_generator_off", "techage:ta2_generator_on"}, { @@ -222,8 +171,8 @@ techage.register_node({"techage:ta2_generator_off", "techage:ta2_generator_on"}, end, }) -Cable:add_secondary_node_names({"techage:ta2_generator_off", "techage:ta2_generator_on"}) -Axle:add_secondary_node_names({"techage:ta2_generator_off", "techage:ta2_generator_on"}) +power.register_nodes({"techage:ta2_generator_off", "techage:ta2_generator_on"}, Axle, "con", {"L"}) +power.register_nodes({"techage:ta2_generator_off", "techage:ta2_generator_on"}, Cable, "gen", {"R"}) minetest.register_craft({ output = "techage:ta2_generator_off", diff --git a/ta3_power/power2axle.lua b/ta3_power/power2axle.lua index 3e37294..bab4f6a 100644 --- a/ta3_power/power2axle.lua +++ b/ta3_power/power2axle.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 @@ -20,17 +20,15 @@ local S2P = minetest.string_to_pos local Cable = techage.ElectricCable local Axle = techage.Axle -local power = techage.power -local networks = techage.networks +local power = networks.power local CYCLE_TIME = 2 local PWR_PERF = 40 -- Axles texture animation local function switch_axles(pos, on) - for _,outdir in ipairs(networks.get_node_connections(pos, "axle")) do - Axle:switch_tube_line(pos, outdir, on and "on" or "off") - end + local outdir = M(pos):get_int("outdir") + Axle:switch_tube_line(pos, outdir, on and "on" or "off") end local function swap_node(pos, name) @@ -42,64 +40,48 @@ local function swap_node(pos, name) minetest.swap_node(pos, node) end -local function on_power(pos) +local function node_timer_on(pos, elapsed) local nvm = techage.get_nvm(pos) - nvm.ele1 = nvm.ele1 or {} - nvm.consumer_powered = true - M(pos):set_string("infotext", S("TA3 Electric Motor")) - swap_node(pos, "techage:ta3_motor_on") - nvm.ticks = 0 local outdir = M(pos):get_int("outdir") - nvm.ele1.curr_power = techage.power.needed_power(pos, Axle, outdir) -end - -local function on_nopower(pos) - local nvm = techage.get_nvm(pos) - nvm.consumer_powered = false - if (nvm.ticks or 0) < 4 then - M(pos):set_string("infotext", S("TA3 Electric Motor: Overload fault?\n(restart with right-click)")) - end - nvm.ticks = 0 -end - -local function node_timer(pos, elapsed) - local nvm = techage.get_nvm(pos) - nvm.ele1 = nvm.ele1 or {} + nvm.buffer = nvm.buffer or 0 - -- trigger network on consumer side - nvm.ticks = (nvm.ticks or 0) + 1 - if nvm.ticks % 2 then - power.consumer_alive(pos, Cable, CYCLE_TIME) - end + local amount = math.min(PWR_PERF * 2 - nvm.buffer, PWR_PERF) + local taken = power.consume_power(pos, Cable, networks.Flip[outdir], amount) + nvm.buffer = nvm.buffer + taken - 1 -- some loss - -- handle generator side delayed - if nvm.ticks > 3 then + if nvm.buffer >= PWR_PERF then + nvm.provided = power.provide_power(pos, Axle, outdir, PWR_PERF) + nvm.load = power.get_storage_load(pos, Axle, outdir, PWR_PERF) + nvm.buffer = nvm.buffer - nvm.provided + end + if amount > 0 and taken == 0 then + swap_node(pos, "techage:ta3_motor_off") + switch_axles(pos, false) local outdir = M(pos):get_int("outdir") + nvm.running = false + power.start_storage_calc(pos, Cable, outdir) + end + return true +end - if nvm.consumer_powered and not nvm.running_as_generator then - nvm.running_as_generator = true - power.generator_start(pos, Axle, CYCLE_TIME, outdir, nvm.max_power) - switch_axles(pos, true) - elseif not nvm.consumer_powered and nvm.running_as_generator then - nvm.running_as_generator = false - power.generator_stop(pos, Axle, outdir) - switch_axles(pos, false) - end - - if nvm.running_as_generator then - nvm.ele1.curr_power = power.generator_alive(pos, Axle, CYCLE_TIME, outdir, PWR_PERF) + 1 - else - swap_node(pos, "techage:ta3_motor_off") - end +local function node_timer_off(pos, elapsed) + local nvm = techage.get_nvm(pos) + local outdir = M(pos):get_int("outdir") + + if power.power_available(pos, Cable) then + swap_node(pos, "techage:ta3_motor_on") + switch_axles(pos, true) + nvm.running = true + power.start_storage_calc(pos, Cable, outdir) end return true end -local function tubelib2_on_update2(pos, outdir, tlib2, node) +local function get_generator_data(pos, tlib2) local nvm = techage.get_nvm(pos) - nvm.ele1 = nvm.ele1 or {} - nvm.ele1.curr_power = 1 - power.update_network(pos, outdir, tlib2) + if nvm.running then + return {level = (nvm.load or 0) / PWR_PERF, perf = PWR_PERF, capa = PWR_PERF * 2} + end end minetest.register_node("techage:ta3_motor_off", { @@ -119,24 +101,10 @@ minetest.register_node("techage:ta3_motor_off", { is_ground_content = false, after_place_node = function(pos) - local nvm = techage.get_nvm(pos) - nvm.ele1 = nvm.ele1 or {} - nvm.ele1.curr_power = 1 - nvm.consumer_powered = false - nvm.running_as_generator = false M(pos):set_int("outdir", networks.side_to_outdir(pos, "R")) - M(pos):set_int("leftdir", networks.side_to_outdir(pos, "L")) Cable:after_place_node(pos) Axle:after_place_node(pos) minetest.get_node_timer(pos):start(CYCLE_TIME) - power.consumer_start(pos, Cable, CYCLE_TIME*2) - M(pos):set_string("infotext", S("TA3 Electric Motor")) - end, - - on_rightclick = function(pos, node, clicker) - local nvm = techage.get_nvm(pos) - nvm.ele1 = nvm.ele1 or {} - nvm.ele1.curr_power = 1 M(pos):set_string("infotext", S("TA3 Electric Motor")) end, @@ -146,21 +114,8 @@ minetest.register_node("techage:ta3_motor_off", { techage.del_mem(pos) end, - tubelib2_on_update2 = tubelib2_on_update2, - on_timer = node_timer, - networks = { - axle = { - sides = {R = 1}, - ntype = "gen1", - nominal = PWR_PERF, - }, - ele1 = { - sides = {L = 1}, - ntype = "con1", - on_power = on_power, - on_nopower = on_nopower, - }, - } + on_timer = node_timer_off, + get_generator_data = get_generator_data, }) minetest.register_node("techage:ta3_motor_on", { @@ -208,21 +163,8 @@ minetest.register_node("techage:ta3_motor_on", { groups = {not_in_creative_inventory=1}, diggable = false, - tubelib2_on_update2 = tubelib2_on_update2, - on_timer = node_timer, - networks = { - axle = { - sides = {R = 1}, - ntype = "gen1", - nominal = PWR_PERF, - }, - ele1 = { - sides = {L = 1}, - ntype = "con1", - on_power = on_power, - on_nopower = on_nopower, - }, - } + on_timer = node_timer_on, + get_generator_data = get_generator_data, }) techage.register_node({"techage:ta3_motor_off", "techage:ta3_motor_on"}, { @@ -231,8 +173,8 @@ techage.register_node({"techage:ta3_motor_off", "techage:ta3_motor_on"}, { end, }) -Cable:add_secondary_node_names({"techage:ta3_motor_off", "techage:ta3_motor_on"}) -Axle:add_secondary_node_names({"techage:ta3_motor_off", "techage:ta3_motor_on"}) +power.register_nodes({"techage:ta3_motor_off", "techage:ta3_motor_on"}, Axle, "gen", {"R"}) +power.register_nodes({"techage:ta3_motor_off", "techage:ta3_motor_on"}, Cable, "con", {"L"}) minetest.register_craft({ output = "techage:ta3_motor_off", diff --git a/ta3_power/tiny_generator.lua b/ta3_power/tiny_generator.lua index 3d27862..fd5b984 100644 --- a/ta3_power/tiny_generator.lua +++ b/ta3_power/tiny_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 @@ -18,11 +18,10 @@ local S = techage.S local Cable = techage.ElectricCable local firebox = techage.firebox -local power = techage.power local fuel = techage.fuel local Pipe = techage.LiquidPipe -local liquid = techage.liquid -local networks = techage.networks +local power = networks.power +local liquid = networks.liquid local CYCLE_TIME = 2 local PWR_CAPA = 12 @@ -34,12 +33,12 @@ local function formspec(self, pos, nvm) 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("Tiny Generator")).."]".. + "label[0.2,-0.1;"..minetest.colorize( "#000000", S("Tiny Generator")).."]".. fuel.fuel_container(0, 0.9, nvm).. "image[1.4,1.6;1,1;techage_form_arrow_bg.png^[transformR270]".. "image_button[1.4,3.2;1,1;".. self:get_state_button_image(nvm) ..";state_button;]".. "tooltip[1.5,3;1,1;"..self:get_state_tooltip(nvm).."]".. - power.formspec_label_bar(pos, 2.5, 0.8, S("Electricity"), PWR_CAPA, nvm.provided) + techage.formspec_power_bar(pos, 2.5, 0.8, S("Electricity"), nvm.provided, PWR_CAPA) end local function play_sound(pos) @@ -74,7 +73,7 @@ end local function start_node(pos, nvm, state) nvm.running = true -- needed by fuel_lib local outdir = M(pos):get_int("outdir") - power.generator_start(pos, Cable, CYCLE_TIME, outdir) + power.start_storage_calc(pos, Cable, outdir) play_sound(pos) end @@ -82,7 +81,7 @@ local function stop_node(pos, nvm, state) nvm.running = false nvm.provided = 0 local outdir = M(pos):get_int("outdir") - power.generator_stop(pos, Cable, outdir) + power.start_storage_calc(pos, Cable, outdir) stop_sound(pos) end @@ -122,15 +121,19 @@ local function burning(pos, nvm) end local function node_timer(pos, elapsed) + local meta = M(pos) local nvm = techage.get_nvm(pos) - local outdir = M(pos):get_int("outdir") + local outdir = meta:get_int("outdir") + local tp1 = tonumber(meta:get_string("termpoint1")) + local tp2 = tonumber(meta:get_string("termpoint2")) if nvm.running and burning(pos, nvm) then - nvm.provided = power.generator_alive(pos, Cable, CYCLE_TIME, outdir) + nvm.provided = power.provide_power(pos, Cable, outdir, PWR_CAPA, tp1, tp2) + nvm.load = power.get_storage_load(pos, Cable, outdir, PWR_CAPA) else nvm.provided = 0 end if techage.is_activeformspec(pos) then - M(pos):set_string("formspec", formspec(State, pos, nvm)) + meta:set_string("formspec", formspec(State, pos, nvm)) end return State:is_active(nvm) end @@ -150,42 +153,12 @@ local function on_rightclick(pos, node, clicker) M(pos):set_string("formspec", formspec(State, pos, nvm)) end -local liquid_def = { - fuel_cat = fuel.BT_NAPHTHA, - capa = fuel.CAPACITY, - peek = liquid.srv_peek, - put = function(pos, indir, name, amount) - if fuel.valid_fuel(name, fuel.BT_OIL) then - local res = liquid.srv_put(pos, indir, name, amount) - if techage.is_activeformspec(pos) then - local nvm = techage.get_nvm(pos) - M(pos):set_string("formspec", formspec(State, pos, nvm)) - end - return res - 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", formspec(State, pos, nvm)) - end - return amount, name +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 -} - -local net_def = { - pipe2 = { - sides = techage.networks.AllSides, -- Pipe connection sides - ntype = "tank", - }, - ele1 = { - sides = {R = 1}, - ntype = "gen1", - nominal = PWR_CAPA, - }, -} +end minetest.register_node("techage:tiny_generator", { description = S("TA3 Tiny Power Generator"), @@ -221,13 +194,13 @@ minetest.register_node("techage:tiny_generator", { techage.del_mem(pos) end, + get_generator_data = get_generator_data, + ta3_formspec = techage.generator_settings("ta3", PWR_CAPA), on_receive_fields = on_receive_fields, on_rightclick = on_rightclick, on_punch = fuel.on_punch, on_timer = node_timer, can_dig = fuel.can_dig, - liquid = liquid_def, - networks = net_def, }) minetest.register_node("techage:tiny_generator_on", { @@ -268,17 +241,56 @@ minetest.register_node("techage:tiny_generator_on", { on_rotate = screwdriver.disallow, is_ground_content = false, + get_generator_data = get_generator_data, + ta3_formspec = techage.generator_settings("ta3", PWR_CAPA), on_receive_fields = on_receive_fields, on_rightclick = on_rightclick, on_punch = fuel.on_punch, on_timer = node_timer, can_dig = fuel.can_dig, - liquid = liquid_def, - networks = net_def, }) -Pipe:add_secondary_node_names({"techage:tiny_generator", "techage:tiny_generator_on"}) -Cable:add_secondary_node_names({"techage:tiny_generator", "techage:tiny_generator_on"}) +local liquid_def = { + fuel_cat = fuel.BT_GASOLINE, + capa = fuel.CAPACITY, + peek = function(pos) + local nvm = techage.get_nvm(pos) + return liquid.srv_peek(nvm) + end, + put = function(pos, indir, name, amount) + if techage.fuel.valid_fuel(name, fuel.BT_GASOLINE) then + local nvm = techage.get_nvm(pos) + local leftover = liquid.srv_put(nvm, name, amount, fuel.CAPACITY) + if techage.is_activeformspec(pos) then + local nvm = techage.get_nvm(pos) + M(pos):set_string("formspec", formspec(State, pos, nvm)) + end + return leftover + end + return amount + end, + take = function(pos, indir, name, amount) + local nvm = techage.get_nvm(pos) + local taken = liquid.srv_take(nvm, name, amount) + if techage.is_activeformspec(pos) then + local nvm = techage.get_nvm(pos) + M(pos):set_string("formspec", formspec(State, pos, nvm)) + end + return taken + end, + untake = function(pos, indir, name, amount) + local nvm = techage.get_nvm(pos) + local leftover = liquid.srv_put(nvm, name, amount, fuel.CAPACITY) + if techage.is_activeformspec(pos) then + local nvm = techage.get_nvm(pos) + M(pos):set_string("formspec", formspec(State, pos, nvm)) + end + return leftover + end, +} + +power.register_nodes({"techage:tiny_generator", "techage:tiny_generator_on"}, Cable, "gen", {"R"}) +liquid.register_nodes({"techage:tiny_generator", "techage:tiny_generator_on"}, Pipe, "tank", nil, liquid_def) techage.register_node({"techage:tiny_generator", "techage:tiny_generator_on"}, { on_recv_message = function(pos, src, topic, payload) diff --git a/textures/techage_gaspipe_valve_open.png b/textures/techage_gaspipe_valve_open.png index 45b929de9dc090dae574495e3daa0b297b617c19..ef27b6f0d05a2fdc91097c3c588405ee3c4b01ad 100644 GIT binary patch delta 34 pcmZ3?*v2?PMS?%TC&bmgz=wf>L04B-^`vT0P>@#eH|2?uUI3e337G%@ delta 63 zcmZo;T+BE@#grx8(btiIVPik{pF~y$1_s9BAa^H*b?0PW0y+EvJ|V8|1wKHguCDG` R28N)Zpv7x 0 then - local nvm = techage.get_nvm(pos) - nvm.liquid = nvm.liquid or {} - nvm.liquid.amount = count - nvm.liquid.name = inv_get_name(inv, listname, size) - inv:set_stack(listname, 1, nil) - inv_clear(inv, listname, size) - return true - end - return false -end - -local function init_data(pos, netw) - local sides = netw.ele1 and netw.ele1.sides - if sides and sides["R"] then - M(pos):set_int("outdir", networks.side_to_outdir(pos, "R")) - M(pos):set_string("infotext", "repaired") - end - - sides = netw.pipe2 and netw.pipe2.sides - if sides and sides["R"] then - M(pos):set_int("outdir", networks.side_to_outdir(pos, "R")) - M(pos):set_string("infotext", "repaired") - end - - sides = netw.ele2 and netw.ele2.sides - if sides and sides["L"] then - M(pos):set_int("leftdir", networks.side_to_outdir(pos, "L")) - end - - sides = netw.axle and netw.axle.sides - if sides and sides["R"] then - M(pos):set_int("outdir", networks.side_to_outdir(pos, "R")) - end - - Cable1:after_place_node(pos) - Cable2:after_place_node(pos) - Pipe2:after_place_node(pos) -end - - -local function repair(itemstack, user, pointed_thing) - local pos = pointed_thing.under - if pos and user then - if minetest.is_protected(pos, user:get_player_name()) then - return itemstack - end - - local number = techage.get_node_number(pos) - local node = minetest.get_node(pos) - local ndef = minetest.registered_nodes[node.name] - if ndef then - local netw = ndef.networks - if netw and ListOfNodes[node.name] then - if node.name == "techage:tiny_generator" or node.name == "techage:tiny_generator_on" then - restore_inv_content(pos, "fuel", 1) - elseif node.name == "techage:oilfirebox" then - restore_inv_content(pos, "fuel", 1) - elseif node.name == "techage:ta4_fuelcell" or node.name == "techage:ta4_fuelcell_on" then - restore_inv_content(pos, "src", 4) - elseif node.name == "techage:ta4_electrolyzer" or node.name == "techage:ta4_electrolyzer_on" then - restore_inv_content(pos, "dst", 1) - end - delete_data(pos) - init_data(pos, netw) - minetest.chat_send_player(user:get_player_name(), ndef.description.." "..S("repaired")) - itemstack:add_wear(65636/200) - return itemstack - end - - if netw and netw.ele1 and netw.ele1.ntype == "junc" then - if ndef.after_place_node and ndef.tubelib2_on_update2 then - ndef.after_place_node(pos) - ndef.tubelib2_on_update2(pos, 0, Cable1) - minetest.chat_send_player(user:get_player_name(), ndef.description.." "..S("repaired")) - itemstack:add_wear(65636/200) - return itemstack - end - end - - if netw and netw.ele2 and netw.ele2.ntype == "junc" then - if ndef.after_place_node and ndef.tubelib2_on_update2 then - ndef.after_place_node(pos) - ndef.tubelib2_on_update2(pos, 0, Cable2) - minetest.chat_send_player(user:get_player_name(), ndef.description.." "..S("repaired")) - itemstack:add_wear(65636/200) - return itemstack - end - end - end - end - return itemstack -end - local function network_check(start_pos, Cable, player_name) - local ndef = techage.networks.net_def(start_pos, Cable.tube_type) - local outdir = nil - local num = 0 - if ndef and ndef.ntype ~= "junc" then - outdir = M(start_pos):get_int("outdir") - end - networks.connection_walk(start_pos, outdir, Cable, function(pos, indir, node) - local distance = vector.distance(start_pos, pos) - num = num + 1 - if distance < 50 and num < 100 then - local state = techage.power.power_available(pos, Cable) and "power" or "no power" - techage.mark_position(player_name, pos, state, "#ff0000", 6) - end - end) +-- local ndef = techage.networks.net_def(start_pos, Cable.tube_type) +-- local outdir = nil +-- local num = 0 +-- if ndef and ndef.ntype ~= "junc" then +-- outdir = M(start_pos):get_int("outdir") +-- end +-- networks.connection_walk(start_pos, outdir, Cable, function(pos, indir, node) +-- local distance = vector.distance(start_pos, pos) +-- num = num + 1 +-- if distance < 50 and num < 100 then +-- local state = techage.power.power_available(pos, Cable) and "power" or "no power" +-- techage.mark_position(player_name, pos, state, "#ff0000", 6) +-- end +-- end) end local function read_state(itemstack, user, pointed_thing) @@ -233,7 +77,7 @@ local function read_state(itemstack, user, pointed_thing) local info = techage.send_single("0", number, "info", nil) if info and info ~= "" and info ~= "unsupported" then info = dump(info) - minetest.chat_send_player(user:get_player_name(), ndef.description.." "..number..": Supported Commands:\n"..info.." ") + minetest.chat_send_player(user:get_player_name(), ndef.description.." "..number..":\n"..info.." ") end local state = techage.send_single("0", number, "state", nil) if state and state ~= "" and state ~= "unsupported" then @@ -284,13 +128,64 @@ local function read_state(itemstack, user, pointed_thing) end end +local context = {} + +local function settings_menu(pos, playername) + local number = techage.get_node_number(pos) + local node = minetest.get_node(pos) + local ndef = minetest.registered_nodes[node.name] + local form_def = ndef and (ndef.ta3_formspec or ndef.ta4_formspec) + + context[playername] = pos + if form_def then + minetest.show_formspec(playername, "techage:ta_formspec", menu.generate_formspec(pos, ndef, form_def)) + end +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname ~= "techage:ta_formspec" then + return false + end + + local playername = player:get_player_name() + local pos = context[playername] + if pos then + --context[playername] = nil + local number = techage.get_node_number(pos) + local node = minetest.get_node(pos) + local ndef = minetest.registered_nodes[node.name] + local form_def = ndef and (ndef.ta3_formspec or ndef.ta4_formspec) + + if form_def then + if menu.eval_input(pos, ndef, form_def, fields) then + --context[playername] = pos + minetest.after(0.2, function() + minetest.show_formspec(playername, "techage:ta_formspec", menu.generate_formspec(pos, ndef, form_def)) + end) + end + end + end + return true +end) + + +local function on_place(itemstack, placer, pointed_thing) + if pointed_thing.type == "node" then + local pos = pointed_thing.under + local playername = placer:get_player_name() + if placer:get_player_control().sneak then + settings_menu(pos, playername) + end + end +end + minetest.register_tool("techage:repairkit", { description = S("TechAge Repair Kit"), inventory_image = "techage_repairkit.png", wield_image = "techage_repairkit.png^[transformR270", groups = {cracky=1, book=1}, - on_use = repair, - on_place = repair, + --on_use = repair, + --on_place = repair, node_placement_prediction = "", stack_max = 1, }) @@ -302,21 +197,12 @@ minetest.register_tool("techage:end_wrench", { wield_image = "techage_end_wrench.png", groups = {cracky=1, book=1}, on_use = read_state, - on_place = read_state, + on_place = on_place, node_placement_prediction = "", liquids_pointable = true, stack_max = 1, }) ---minetest.register_craft({ --- output = "techage:repairkit", --- recipe = { --- {"", "basic_materials:gear_steel", ""}, --- {"", "techage:end_wrench", ""}, --- {"", "basic_materials:oil_extract", ""}, --- }, ---}) - minetest.register_craft({ output = "techage:end_wrench", recipe = { diff --git a/tools/submenu.lua b/tools/submenu.lua new file mode 100644 index 0000000..7ca7671 --- /dev/null +++ b/tools/submenu.lua @@ -0,0 +1,190 @@ +--[[ + + TechAge + ======= + + Copyright (C) 2019-2021 Joachim Stolberg + + AGPL v3 + See LICENSE.txt for more information + + A formspec control to generate formspec strings for machine settings and monitoring +]]-- + +local S = techage.S + +local menu = {} + +local function index(list, x) + for idx, v in ipairs(list) do + if v == x then return idx end + end + return nil +end + + +-- generate the formspec string to be placed into a container frame +local function generate_formspec_substring(pos, meta, form_def) + local tbl = {} + if meta and form_def then + local nvm = techage.get_nvm(pos) + + for i,elem in ipairs(form_def) do + local offs = (i - 1) * 0.9 - 0.2 + tbl[#tbl+1] = "label[0," .. offs .. ";" .. minetest.formspec_escape(elem.label) .. ":]" + tbl[#tbl+1] = "tooltip[0," .. offs .. ";4,1;" .. elem.tooltip .. "]" + if elem.type == "number" then + local val = meta:get_int(elem.name) + if nvm.running then + tbl[#tbl+1] = "label[4.75," .. offs .. ";" .. val .. "]" + else + tbl[#tbl+1] = "field[5," .. (offs+0.2) .. ";5.3,1;" .. elem.name .. ";;" .. val .. "]" + end + elseif elem.type == "numbers" then + local val = meta:get_string(elem.name) + if nvm.running then + tbl[#tbl+1] = "label[4.75," .. offs .. ";" .. val .. "]" + else + tbl[#tbl+1] = "field[5," .. (offs+0.2) .. ";5.3,1;" .. elem.name .. ";;" .. val .. "]" + end + elseif elem.type == "float" then + local val = tonumber(meta:get_string(elem.name)) or 0 + if nvm.running then + tbl[#tbl+1] = "label[4.75," .. offs .. ";" .. val .. "]" + else + tbl[#tbl+1] = "field[5," .. (offs+0.2) .. ";5.3,1;" .. elem.name .. ";;" .. val .. "]" + end + elseif elem.type == "ascii" then + local val = meta:get_string(elem.name) + if nvm.running then + tbl[#tbl+1] = "label[4.75," .. offs .. ";" .. minetest.formspec_escape(val) .. "]" + else + tbl[#tbl+1] = "field[5," .. (offs+0.2) .. ";5.3,1;" .. elem.name .. ";;" .. minetest.formspec_escape(val) .. "]" + end + elseif elem.type == "const" then + tbl[#tbl+1] = "label[4.75," .. offs .. ";" .. elem.value .. "]" + elseif elem.type == "output" then + local val = nvm[elem.name] or "" + if tonumber(val) then + val = techage.round(val) + end + tbl[#tbl+1] = "label[4.75," .. offs .. ";" .. val .. "]" + elseif elem.type == "dropdown" then + local l = elem.choices:split(",") + if nvm.running then + local val = meta:get_string(elem.name) or "" + tbl[#tbl+1] = "label[4.75," .. offs .. ";" .. val .. "]" + else + local val = meta:get_string(elem.name) or "" + local idx = index(l, val) or 1 + tbl[#tbl+1] = "dropdown[4.72," .. (offs) .. ";5.5,1.4;" .. elem.name .. ";" .. elem.choices .. ";" .. idx .. "]" + end + end + end + end + return table.concat(tbl, "") +end + +local function value_check(elem, value) + if elem.check then + return elem.check(value) + end + return true +end + +local function evaluate_data(pos, meta, form_def, fields) + local res = true + + if meta and form_def then + local nvm = techage.get_nvm(pos) + if not nvm.running then + + for idx,elem in ipairs(form_def) do + if elem.type == "number" then + if fields[elem.name] then + if fields[elem.name]:find("^[%d ]+$") then + local val = tonumber(fields[elem.name]) + if value_check(elem, val) then + meta:set_int(elem.name, val) + print("set_int", elem.name, val) + else + res = false + end + else + res = false + end + end + elseif elem.type == "numbers" then + if fields[elem.name] then + if fields[elem.name]:find("^[%d ]+$") and value_check(elem, fields[elem.name]) then + meta:set_string(elem.name, fields[elem.name]) + else + res = false + end + end + elseif elem.type == "float" then + if fields[elem.name] then + local val = tonumber(fields[elem.name]) + if val and value_check(elem, val) then + meta:set_string(elem.name, val) + else + res = false + end + end + elseif elem.type == "ascii" then + if fields[elem.name] then + if value_check(elem, fields[elem.name]) then + meta:set_string(elem.name, fields[elem.name]) + else + res = false + end + end + elseif elem.type == "dropdown" then + if fields[elem.name] ~= nil then + meta:set_string(elem.name, fields[elem.name]) + end + end + end + end + end + return res +end + +function menu.generate_formspec(pos, ndef, form_def) + local meta = minetest.get_meta(pos) + local number = techage.get_node_number(pos) + local mem = techage.get_mem(pos) + mem.star = ((mem.star or 0) + 1) % 2 + local star = mem.star == 1 and "*" or "" + + if meta and number and ndef and form_def then + local title = ndef.description .. " (" .. number .. ")" + + return "size[10,9]" .. + default.gui_bg .. + default.gui_bg_img .. + default.gui_slots .. + "box[0,-0.1;9.8,0.5;#c6e8ff]" .. + "label[0.2,-0.1;" .. minetest.colorize( "#000000", title) .. "]" .. + "label[9.5,-0.1;" .. minetest.colorize( "#000000", star) .. "]" .. + "container[0,1]" .. + generate_formspec_substring(pos, meta, form_def) .. + "container_end[]" .. + "button[0.5,8.4;3,1;refresh;" .. S("Refresh") .. "]" .. + "button_exit[3.5,8.4;3,1;cancel;" .. S("Cancel") .. "]" .. + "button[6.5,8.4;3,1;save;" .. S("Save") .. "]" + end + return "" +end + +function menu.eval_input(pos, ndef, form_def, fields) + --print(dump(fields)) + if fields.save then + local meta = minetest.get_meta(pos) + evaluate_data(pos, meta, form_def, fields) + end + return fields.refresh or fields.save +end + + +return menu \ No newline at end of file