From 66e20a9231b9fea823f9b683e495c57ef9a4ad41 Mon Sep 17 00:00:00 2001 From: sfence <sfence.software@gmail.com> Date: Fri, 04 Nov 2022 19:23:44 +0100 Subject: [PATCH] Add register_cable_tier API function (#612) --- technic/machines/supply_converter.lua | 442 +++++++++++++++++++++++++++--------------------------- 1 files changed, 223 insertions(+), 219 deletions(-) diff --git a/technic/machines/supply_converter.lua b/technic/machines/supply_converter.lua index bee99d2..2ecd610 100644 --- a/technic/machines/supply_converter.lua +++ b/technic/machines/supply_converter.lua @@ -1,232 +1,236 @@ -- The supply converter is a generic device which can convert from -- LV to MV and back, and HV to MV and back. --- The machine will not convert from HV directly to LV. -- The machine is configured by the wiring below and above it. --- It is prepared for an upgrade slot if this is to be implemented later. --- --- The conversion factor is a constant and the conversion is a lossy operation. -- -- It works like this: --- The top side is setup as the "RE" side, the bottom as the "PR" side. --- Once the RE side is powered it will deliver power to the other side. +-- The top side is setup as the receiver side, the bottom as the producer side. +-- Once the receiver side is powered it will deliver power to the other side. -- Unused power is wasted just like any other producer! --- -minetest.register_node( - "technic:supply_converter", { - description = "Supply Converter", - tiles = {"technic_supply_converter_top.png", "technic_supply_converter_bottom.png", "technic_supply_converter_side.png", - "technic_supply_converter_side.png", "technic_supply_converter_side.png", "technic_supply_converter_side.png"}, - groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, - sounds = default.node_sound_wood_defaults(), - drawtype = "nodebox", - paramtype = "light", - is_ground_content = true, - node_box = { - type = "fixed", - fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5}, - }, - selection_box = { - type = "fixed", - fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5}, - }, - on_construct = function(pos) - local meta = minetest.env:get_meta(pos) - meta:set_float("technic_hv_power_machine", 1) - meta:set_float("technic_mv_power_machine", 1) - meta:set_float("technic_power_machine", 1) - meta:set_string("infotext", "Supply Converter") - meta:set_float("active", false) - end, - }) + +local digilines_path = minetest.get_modpath("digilines") + +local S = technic.getter + +local cable_entry = "^technic_cable_connection_overlay.png" + +local function set_supply_converter_formspec(meta) + local formspec = "size[5,2.25]".. + "field[0.3,0.5;2,1;power;"..S("Input Power")..";"..meta:get_int("power").."]" + if digilines_path then + formspec = formspec.. + "field[2.3,0.5;3,1;channel;Digiline Channel;"..meta:get_string("channel").."]" + end + -- The names for these toggle buttons are explicit about which + -- state they'll switch to, so that multiple presses (arising + -- from the ambiguity between lag and a missed press) only make + -- the single change that the user expects. + if meta:get_int("mesecon_mode") == 0 then + formspec = formspec.."button[0,1;5,1;mesecon_mode_1;"..S("Ignoring Mesecon Signal").."]" + else + formspec = formspec.."button[0,1;5,1;mesecon_mode_0;"..S("Controlled by Mesecon Signal").."]" + end + if meta:get_int("enabled") == 0 then + formspec = formspec.."button[0,1.75;5,1;enable;"..S("%s Disabled"):format(S("Supply Converter")).."]" + else + formspec = formspec.."button[0,1.75;5,1;disable;"..S("%s Enabled"):format(S("Supply Converter")).."]" + end + meta:set_string("formspec", formspec) +end + +local supply_converter_receive_fields = function(pos, formname, fields, sender) + local meta = minetest.get_meta(pos) + local power = nil + if fields.power then + power = tonumber(fields.power) or 0 + power = math.max(power, 0) + power = math.min(power, 10000) + power = 100 * math.floor(power / 100) + if power == meta:get_int("power") then power = nil end + end + if power then meta:set_int("power", power) end + if fields.channel then meta:set_string("channel", fields.channel) end + if fields.enable then meta:set_int("enabled", 1) end + if fields.disable then meta:set_int("enabled", 0) end + if fields.mesecon_mode_0 then meta:set_int("mesecon_mode", 0) end + if fields.mesecon_mode_1 then meta:set_int("mesecon_mode", 1) end + set_supply_converter_formspec(meta) +end + +local mesecons = { + effector = { + action_on = function(pos, node) + minetest.get_meta(pos):set_int("mesecon_effect", 1) + end, + action_off = function(pos, node) + minetest.get_meta(pos):set_int("mesecon_effect", 0) + end + } +} + + +local digiline_def = { + receptor = {action = function() end}, + effector = { + action = function(pos, node, channel, msg) + if type(msg) ~= "string" then + return + end + local meta = minetest.get_meta(pos) + if channel ~= meta:get_string("channel") then + return + end + msg = msg:lower() + if msg == "get" then + digilines.receptor_send(pos, digilines.rules.default, channel, { + enabled = meta:get_int("enabled"), + power = meta:get_int("power"), + mesecon_mode = meta:get_int("mesecon_mode") + }) + return + elseif msg == "off" then + meta:set_int("enabled", 0) + elseif msg == "on" then + meta:set_int("enabled", 1) + elseif msg == "toggle" then + local onn = meta:get_int("enabled") + onn = 1-onn -- Mirror onn with pivot 0.5, so switch between 1 and 0. + meta:set_int("enabled", onn) + elseif msg:sub(1, 5) == "power" then + local power = tonumber(msg:sub(7)) + if not power then + return + end + power = math.max(power, 0) + power = math.min(power, 10000) + power = 100 * math.floor(power / 100) + meta:set_int("power", power) + elseif msg:sub(1, 12) == "mesecon_mode" then + meta:set_int("mesecon_mode", tonumber(msg:sub(14))) + else + return + end + set_supply_converter_formspec(meta) + end + }, +} + +local run = function(pos, node, run_stage) + -- run only in producer stage. + if run_stage == technic.receiver then + return + end + + local efficiency = 0.9 + -- Machine information + local machine_name = S("Supply Converter") + local meta = minetest.get_meta(pos) + local enabled = meta:get_string("enabled") + if enabled == "" then + -- Backwards compatibility + minetest.registered_nodes["technic:supply_converter"].on_construct(pos) + enabled = true + else + enabled = enabled == "1" + end + enabled = enabled and (meta:get_int("mesecon_mode") == 0 or meta:get_int("mesecon_effect") ~= 0) + + local pos_up = {x=pos.x, y=pos.y+1, z=pos.z} + local pos_down = {x=pos.x, y=pos.y-1, z=pos.z} + local name_up = minetest.get_node(pos_up).name + local name_down = minetest.get_node(pos_down).name + + local from = technic.get_cable_tier(name_up) + local to = technic.get_cable_tier(name_down) + + if from and to then + -- Get the "to" network switching station for EU demand calculation + local network_hash = technic.cables[minetest.hash_node_position(pos_down)] + local network = network_hash and minetest.get_position_from_hash(network_hash) + local sw_pos = network and {x=network.x,y=network.y+1,z=network.z} + local timeout = 0 + for tier in pairs(technic.machines) do + -- Supply converter must be connected to a network + timeout = math.max(meta:get_int(tier.."_EU_timeout"), timeout) + end + if timeout > 0 and sw_pos and minetest.get_node(sw_pos).name == "technic:switching_station" then + local sw_meta = minetest.get_meta(sw_pos) + local demand = 0 + if enabled then + -- Reverse evaluate the required machine and round to a nice number + demand = sw_meta:get_int("ba_demand") + sw_meta:get_int("demand") + demand = 100 * math.ceil(demand / efficiency / 100) + -- Do not draw more than the limit + demand = math.min(demand, meta:get_int("power")) + end + + local input = meta:get_int(from.."_EU_input") -- actual input + meta:set_int(from.."_EU_demand", demand) -- desired input + meta:set_int(from.."_EU_supply", 0) + meta:set_int(to.."_EU_demand", 0) + meta:set_int(to.."_EU_supply", input * efficiency) + meta:set_string("infotext", S("@1 (@2 @3 -> @4 @5)", machine_name, + technic.EU_string(input), from, + technic.EU_string(input * efficiency), to)) + else + meta:set_string("infotext",S("%s Has No Network"):format(machine_name)) + end + else + meta:set_string("infotext", S("%s Has Bad Cabling"):format(machine_name)) + if to then + meta:set_int(to.."_EU_supply", 0) + end + if from then + meta:set_int(from.."_EU_demand", 0) + end + return + end + +end + +minetest.register_node("technic:supply_converter", { + description = S("Supply Converter"), + tiles = { + "technic_supply_converter_tb.png"..cable_entry, + "technic_supply_converter_tb.png"..cable_entry, + "technic_supply_converter_side.png", + "technic_supply_converter_side.png", + "technic_supply_converter_side.png", + "technic_supply_converter_side.png" + }, + groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, + technic_machine=1, technic_all_tiers=1}, + connect_sides = {"top", "bottom"}, + sounds = default.node_sound_wood_defaults(), + on_receive_fields = supply_converter_receive_fields, + on_construct = function(pos) + local meta = minetest.get_meta(pos) + meta:set_string("infotext", S("Supply Converter")) + if digilines_path then + meta:set_string("channel", "supply_converter"..minetest.pos_to_string(pos)) + end + meta:set_int("power", 10000) + meta:set_int("enabled", 1) + meta:set_int("mesecon_mode", 0) + meta:set_int("mesecon_effect", 0) + set_supply_converter_formspec(meta) + end, + mesecons = mesecons, + digiline = digiline_def, + technic_run = run, + technic_on_disable = run, +}) minetest.register_craft({ output = 'technic:supply_converter 1', recipe = { - {'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot','technic:stainless_steel_ingot'}, - {'technic:mv_transformer', 'technic:mv_cable', 'technic:lv_transformer'}, - {'technic:mv_cable', 'technic:rubber', 'technic:lv_cable'}, - } + {'basic_materials:gold_wire', 'technic:rubber', 'technic:doped_silicon_wafer'}, + {'technic:mv_transformer', 'technic:machine_casing', 'technic:lv_transformer'}, + {'technic:mv_cable', 'technic:rubber', 'technic:lv_cable'}, + }, + replacements = { {"basic_materials:gold_wire", "basic_materials:empty_spool"}, }, }) -minetest.register_abm( - { nodenames = {"technic:supply_converter"}, - interval = 1, - chance = 1, - action = function(pos, node, active_object_count, active_object_count_wider) - -- Conversion factors (a picture of V*A - loss) Asymmetric. - local lv_mv_factor = 5 -- division (higher is less efficient) - local mv_lv_factor = 4 -- multiplication (higher is more efficient) - local mv_hv_factor = 5 -- division - local hv_mv_factor = 4 -- multiplication - local max_lv_demand = 2000 -- The increment size power supply tier. Determines how many are needed - local max_mv_demand = 2000 -- -""- - local max_hv_demand = 2000 -- -""- +for tier, machines in pairs(technic.machines) do + technic.register_machine(tier, "technic:supply_converter", technic.producer_receiver) +end - -- Machine information - local machine_name = "Supply Converter" - local meta = minetest.env:get_meta(pos) - local upgrade = "" -- Replace with expansion slot later?? - - -- High voltage on top, low at bottom regardless of converter direction - local pos_up = {x=pos.x, y=pos.y+1, z=pos.z} - local pos_down = {x=pos.x, y=pos.y-1, z=pos.z} - local meta_up = minetest.env:get_meta(pos_up) - local meta_down = minetest.env:get_meta(pos_down) - local convert_MV_LV = 0 - local convert_LV_MV = 0 - local convert_MV_HV = 0 - local convert_HV_MV = 0 - -- check cabling - if meta_up:get_float("mv_cablelike") == 1 and meta_down:get_float("cablelike") == 1 then - convert_MV_LV = 1 - upgrade = "MV-LV step down" - end - if meta_up:get_float("cablelike") == 1 and meta_down:get_float("mv_cablelike") == 1 then - convert_LV_MV = 1 - upgrade = "LV-MV step up" - end - if meta_up:get_float("mv_cablelike") == 1 and meta_down:get_float("hv_cablelike") == 1 then - convert_MV_HV = 1 - upgrade = "MV-HV step up" - end - if meta_up:get_float("hv_cablelike") == 1 and meta_down:get_float("mv_cablelike") == 1 then - convert_HV_MV = 1 - upgrade = "HV-MV step down" - end - --print("Cabling:"..convert_MV_LV.."|"..convert_LV_MV.."|"..convert_HV_MV.."|"..convert_MV_HV) - - if convert_MV_LV == 0 and convert_LV_MV == 0 and convert_HV_MV == 0 and convert_MV_HV == 0 then - meta:set_string("infotext", machine_name.." has bad cabling") - meta:set_int("LV_EU_demand", 0) - meta:set_int("LV_EU_supply", 0) - meta:set_int("LV_EU_input", 0) - meta:set_int("MV_EU_demand", 0) - meta:set_int("MV_EU_supply", 0) - meta:set_int("MV_EU_input", 0) - meta:set_int("HV_EU_demand", 0) - meta:set_int("HV_EU_supply", 0) - meta:set_int("HV_EU_input", 0) - return - end - - -- The node is programmed with an upgrade slot - -- containing a MV-LV step down, LV-MV step up, HV-MV step down or MV-HV step up unit - - if upgrade == "" then - meta:set_string("infotext", machine_name.." has an empty converter slot"); - technic.unregister_LV_machine("technic:supply_converter") - technic.unregister_MV_machine("technic:supply_converter") - technic.unregister_HV_machine("technic:supply_converter") - meta:set_int("LV_EU_demand", 0) - meta:set_int("LV_EU_supply", 0) - meta:set_int("LV_EU_input", 0) - meta:set_int("MV_EU_demand", 0) - meta:set_int("MV_EU_supply", 0) - meta:set_int("MV_EU_input", 0) - meta:set_int("HV_EU_demand", 0) - meta:set_int("HV_EU_supply", 0) - meta:set_int("HV_EU_input", 0) - return - end - - -- State machine - if upgrade == "MV-LV step down" and convert_MV_LV then - -- Register machine type - technic.register_LV_machine("technic:supply_converter","PR") - technic.register_MV_machine("technic:supply_converter","RE") - - -- Power off automatically if no longer connected to a switching station - technic.switching_station_timeout_count(pos, "MV") - - local eu_input = meta:get_int("MV_EU_input") - if eu_input == 0 then - -- Unpowered - go idle - --hacky_swap_node(pos, machine_node) - meta:set_string("infotext", machine_name.." Unpowered") - meta:set_int("LV_EU_supply", 0) - meta:set_int("MV_EU_supply", 0) - - meta:set_int("LV_EU_demand", 0) - meta:set_int("MV_EU_demand", max_mv_demand) - else - -- MV side has got power to spare - meta:set_string("infotext", machine_name.." is active (MV:"..max_mv_demand.."->LV:"..eu_input*mv_lv_factor..")"); - meta:set_int("LV_EU_supply", eu_input*mv_lv_factor) - end - --------------------------------------------------- - elseif upgrade == "LV-MV step up" and convert_LV_MV then - -- Register machine type - technic.register_LV_machine("technic:supply_converter","RE") - technic.register_MV_machine("technic:supply_converter","PR") - - -- Power off automatically if no longer connected to a switching station - technic.switching_station_timeout_count(pos, "LV") - - local eu_input = meta:get_int("LV_EU_input") - if eu_input == 0 then - -- Unpowered - go idle - --hacky_swap_node(pos, machine_node) - meta:set_string("infotext", machine_name.." Unpowered") - meta:set_int("LV_EU_supply", 0) - meta:set_int("MV_EU_supply", 0) - - meta:set_int("LV_EU_demand", max_lv_demand) - meta:set_int("MV_EU_demand", 0) - else - -- LV side has got power to spare - meta:set_string("infotext", machine_name.." is active (LV:"..max_lv_demand.."->MV:"..eu_input/lv_mv_factor..")"); - meta:set_int("MV_EU_supply", eu_input/lv_mv_factor) - end - --------------------------------------------------- - - elseif upgrade == "HV-MV step down" and convert_HV_MV then - -- Register machine type - technic.register_MV_machine("technic:supply_converter","PR") - technic.register_HV_machine("technic:supply_converter","RE") - - -- Power off automatically if no longer connected to a switching station - technic.switching_station_timeout_count(pos, "HV") - - local eu_input = meta:get_int("HV_EU_input") - if eu_input == 0 then - -- Unpowered - go idle - --hacky_swap_node(pos, machine_node) - meta:set_string("infotext", machine_name.." Unpowered") - meta:set_int("MV_EU_supply", 0) - meta:set_int("HV_EU_supply", 0) - - meta:set_int("MV_EU_demand", 0) - meta:set_int("HV_EU_demand", max_hv_demand) - else - -- HV side has got power to spare - meta:set_string("infotext", machine_name.." is active (HV:"..max_hv_demand.."->MV:"..eu_input*hv_mv_factor..")"); - meta:set_int("MV_EU_supply", eu_input*hv_mv_factor) - end - --------------------------------------------------- - elseif upgrade == "MV-HV step up" and convert_MV_HV then - -- Register machine type - technic.register_MV_machine("technic:supply_converter","RE") - technic.register_HV_machine("technic:supply_converter","PR") - - -- Power off automatically if no longer connected to a switching station - technic.switching_station_timeout_count(pos, "MV") - - local eu_input = meta:get_int("MV_EU_input") - if eu_input == 0 then - -- Unpowered - go idle - --hacky_swap_node(pos, machine_node) - meta:set_string("infotext", machine_name.." Unpowered") - meta:set_int("MV_EU_supply", 0) - meta:set_int("HV_EU_supply", 0) - - meta:set_int("MV_EU_demand", max_mv_demand) - meta:set_int("HV_EU_demand", 0) - else - -- MV side has got power to spare - meta:set_string("infotext", machine_name.." is active (MV:"..max_mv_demand.."->HV:"..eu_input/mv_hv_factor..")"); - meta:set_int("HV_EU_supply", eu_input/mv_hv_factor) - end - --------------------------------------------------- - end - end, -}) -- Gitblit v1.8.0