From 5ca83a5914b6c933aeaea6fef78cb41f4f1abaf2 Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Wed, 2 Oct 2019 19:33:39 +0200 Subject: [PATCH] power markers for info tool added error message for inverter and further improvements --- basis/mark2.lua | 67 ++++++++++++++++++++++++++++++++++++ basis/node_states.lua | 10 +++--- energy_storage/generator.lua | 2 +- init.lua | 1 + power/power.lua | 39 +++++++++++++++++++++ solar/inverter.lua | 55 +++++++++++++++++------------ tools/repairkit.lua | 1 + 7 files changed, 148 insertions(+), 27 deletions(-) create mode 100644 basis/mark2.lua diff --git a/basis/mark2.lua b/basis/mark2.lua new file mode 100644 index 0000000..b691403 --- /dev/null +++ b/basis/mark2.lua @@ -0,0 +1,67 @@ +--[[ + + TechAge + ======= + + Copyright (C) 2019 Joachim Stolberg + + GPL v3 + See LICENSE.txt for more information + + mark.lua: + +]]-- + +local marker_region = {} + +function techage.unmark_position(name) + if marker_region[name] ~= nil then --marker already exists + --wip: make the area stay loaded somehow + for _, entity in ipairs(marker_region[name]) do + entity:remove() + end + marker_region[name] = nil + end +end + +function techage.mark_position(name, pos, nametag, color) + local marker = minetest.add_entity(pos, "techage:position_cube") + if marker ~= nil then + marker:set_nametag_attributes({color = color, text = nametag}) + marker:get_luaentity().player_name = name + if not marker_region[name] then + marker_region[name] = {} + end + marker_region[name][#marker_region[name] + 1] = marker + end + minetest.after(30, techage.unmark_position, name) +end + +minetest.register_entity(":techage:position_cube", { + initial_properties = { + visual = "cube", + textures = { + "techage_cube_mark.png", + "techage_cube_mark.png", + "techage_cube_mark.png", + "techage_cube_mark.png", + "techage_cube_mark.png", + "techage_cube_mark.png", + }, + --use_texture_alpha = true, + physical = false, + visual_size = {x = 2, y = 2}, + collisionbox = {-1,-1,-1, 1,1,1}, + glow = 8, + }, + on_step = function(self, dtime) + if marker_region[self.player_name] == nil then + self.object:remove() + return + end + end, + on_punch = function(self, hitter) + techage.unmark_position(self.player_name) + end, +}) + diff --git a/basis/node_states.lua b/basis/node_states.lua index 91c6b50..9831806 100644 --- a/basis/node_states.lua +++ b/basis/node_states.lua @@ -219,8 +219,9 @@ end function NodeStates:start(pos, mem) local state = mem.techage_state or STOPPED if state ~= RUNNING and state ~= FAULT then - if not self.can_start(pos, mem, state) then - self:fault(pos, mem) + local res = self.can_start(pos, mem, state) + if res ~= true then + self:fault(pos, mem, res) return false end if not self.has_power(pos, mem, state) then @@ -338,8 +339,9 @@ function NodeStates:nopower(pos, mem) return false end -function NodeStates:fault(pos, mem) +function NodeStates:fault(pos, mem, err_string) local state = mem.techage_state or STOPPED + err_string = err_string or "fault" if state == RUNNING or state == STOPPED then mem.techage_state = FAULT if self.node_name_passive then @@ -347,7 +349,7 @@ function NodeStates:fault(pos, mem) end if self.infotext_name then local number = M(pos):get_string("node_number") - M(pos):set_string("infotext", self.infotext_name.." "..number..": fault") + M(pos):set_string("infotext", self.infotext_name.." "..number..": "..err_string) end if self.formspec_func then M(pos):set_string("formspec", self.formspec_func(self, pos, mem)) diff --git a/energy_storage/generator.lua b/energy_storage/generator.lua index 5dceb86..33e550c 100644 --- a/energy_storage/generator.lua +++ b/energy_storage/generator.lua @@ -132,7 +132,7 @@ techage.register_node({"techage:ta4_generator", "techage:ta4_generator_on"}, { local mem = tubelib2.get_mem(pos) if topic == "power" then mem.remote_trigger = 2 - return techage.power.power_available(pos, mem, 0) + return techage.power.power_network_available(pos) elseif topic == "start" then mem.remote_trigger = 2 swap_node(pos, "techage:ta4_generator_on") diff --git a/init.lua b/init.lua index 695775d..c4ca2aa 100644 --- a/init.lua +++ b/init.lua @@ -46,6 +46,7 @@ else dofile(MP.."/basis/command.lua") -- tubelib replacement dofile(MP.."/basis/firebox_lib.lua") -- common firebox functions dofile(MP.."/basis/mark.lua") + dofile(MP.."/basis/mark2.lua") dofile(MP.."/basis/assemble.lua") -- Main doc diff --git a/power/power.lua b/power/power.lua index e1e85ad..5a9fd00 100644 --- a/power/power.lua +++ b/power/power.lua @@ -237,6 +237,25 @@ local function connection_walk(pos, clbk) end end +-- walk limited by number of nodes and hops +local function connection_walk2(pos, max_hops, max_nodes, clbk) + local mem = tubelib2.get_mem(pos) + mem.interrupted_dirs = mem.interrupted_dirs or {} + if clbk then + clbk(pos, mem, max_hops) + end + max_hops = max_hops - 1 + if max_hops < 0 then return end + for out_dir,item in pairs(mem.connections or {}) do + if item.pos and not pos_already_reached(item.pos) and + not mem.interrupted_dirs[out_dir] then + max_nodes = max_nodes - 1 + if max_nodes < 0 then return end + connection_walk2(item.pos, max_hops, max_nodes, clbk) + end + end +end + -- if no power available local function consumer_turn_off(pos, mem) local pwr = PWR(pos) @@ -584,3 +603,23 @@ function techage.power.get_power(start_pos) return sum end +function techage.power.power_network_available(start_pos) + Route = {} + NumNodes = 0 + pos_already_reached(start_pos) + local sum = 0 + connection_walk2(start_pos, 2, 3, function(pos, mem) + sum = sum + 1 + end) + return sum > 1 +end + +function techage.power.mark_nodes(name, start_pos) + Route = {} + NumNodes = 0 + pos_already_reached(start_pos) + techage.unmark_position(name) + connection_walk2(start_pos, 3, 100, function(pos, mem, max_hops) + techage.mark_position(name, pos, S(pos).." : "..(4 - max_hops), "#60FF60") + end) +end diff --git a/solar/inverter.lua b/solar/inverter.lua index 8782b77..7065ca0 100644 --- a/solar/inverter.lua +++ b/solar/inverter.lua @@ -25,7 +25,38 @@ local power = techage.power local CYCLE_TIME = 2 local PWR_PERF = 120 +local function determine_power(pos, mem) + local time = minetest.get_timeofday() or 0 + if time < 6.00/24.00 or time > 18.00/24.00 then + mem.ticks = 0 + mem.max_power = 0 + return + end + mem.ticks = mem.ticks or 0 + if (mem.ticks % 20) == 0 then -- calculate max_power not to often + local dir = M(pos):get_int("left_dir") + -- We have to use get_connected_node_pos, because the inverter has already + -- a AC power connection. An additional DC power connection is not possibe, + -- so we have to start the connection_walk on the next node. + local pos1 = Solar:get_connected_node_pos(pos, dir) + mem.max_power = math.min(PWR_PERF, power.get_power(pos1)) + else + mem.max_power = mem.max_power or 0 + end + mem.ticks = mem.ticks + 1 +end + +local function determine_power_now(pos, mem) + local dir = M(pos):get_int("left_dir") + -- We have to use get_connected_node_pos, because the inverter has already + -- a AC power connection. An additional DC power connection is not possibe, + -- so we have to start the connection_walk on the next node. + local pos1 = Solar:get_connected_node_pos(pos, dir) + mem.max_power = math.min(PWR_PERF, power.get_power(pos1)) +end + local function formspec(self, pos, mem) + determine_power_now(pos, mem) local max_power = mem.max_power or 0 local delivered = mem.delivered or 0 local bar_in = techage.power.formspec_power_bar(max_power, max_power) @@ -44,29 +75,9 @@ local function formspec(self, pos, mem) "label[4,2.5;"..delivered.." ku]" end -local function determine_power(pos, mem) - local time = minetest.get_timeofday() or 0 - if time < 6.00/24.00 or time > 18.00/24.00 then - mem.ticks = 0 - mem.max_power = 0 - return - end - if (mem.ticks % 20) == 0 then -- calculate max_power not to often - local dir = M(pos):get_int("left_dir") - -- We have to use get_connected_node_pos, because the inverter has already - -- a AC power connection. An additional DC power connection is not possibe, - -- so we have to start the connection_walk on the next node. - local pos1 = Solar:get_connected_node_pos(pos, dir) - mem.max_power = math.min(PWR_PERF, power.get_power(pos1)) - else - mem.max_power = mem.max_power or 0 - end - mem.ticks = mem.ticks + 1 -end - local function can_start(pos, mem, state) determine_power(pos, mem) - return mem.max_power > 0 + return mem.max_power > 0 or "no solar power" end local function start_node(pos, mem, state) @@ -127,7 +138,7 @@ minetest.register_node("techage:ta4_solar_inverter", { description = S("TA4 Solar Inverter"), tiles = { -- up, down, right, left, back, front - "techage_filling_ta4.png^techage_frame_ta4_top.png", + "techage_filling_ta4.png^techage_frame_ta4_top.png^techage_appl_arrow.png", "techage_filling_ta4.png^techage_frame_ta4.png", "techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_hole_electric.png", "techage_filling_ta4.png^techage_frame_ta4.png^techage_appl_ta4_cable.png", diff --git a/tools/repairkit.lua b/tools/repairkit.lua index 9d0f003..03284d3 100644 --- a/tools/repairkit.lua +++ b/tools/repairkit.lua @@ -92,6 +92,7 @@ local function read_state(itemstack, user, pointed_thing) end elseif ndef and ndef.description then if ndef.is_power_available then + techage.power.mark_nodes(user:get_player_name(), pos) local power = ndef.is_power_available(pos) if power and power.prim_available then local text = "\nGenerators: "..power.prim_available.." ku\nAkkus: "..power.sec_available.." ku\nMachines: "..power.prim_needed.." ku\nNum Nodes: "..power.num_nodes.."\n"