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 |   65 +++++++++++++++++++++++++-------
 1 files changed, 51 insertions(+), 14 deletions(-)

diff --git a/technic/machines/supply_converter.lua b/technic/machines/supply_converter.lua
index 8ae4dd4..2ecd610 100644
--- a/technic/machines/supply_converter.lua
+++ b/technic/machines/supply_converter.lua
@@ -11,6 +11,8 @@
 
 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").."]"
@@ -70,6 +72,9 @@
 	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
@@ -88,7 +93,7 @@
 				meta:set_int("enabled", 1)
 			elseif msg == "toggle" then
 				local onn = meta:get_int("enabled")
-				onn = -(onn-1) -- Mirror onn with pivot 0.5, so switch between 1 and 0.
+				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))
@@ -101,6 +106,8 @@
 				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
@@ -113,7 +120,7 @@
 		return
 	end
 
-	local remain = 0.9
+	local efficiency = 0.9
 	-- Machine information
 	local machine_name  = S("Supply Converter")
 	local meta          = minetest.get_meta(pos)
@@ -126,7 +133,6 @@
 		enabled = enabled == "1"
 	end
 	enabled = enabled and (meta:get_int("mesecon_mode") == 0 or meta:get_int("mesecon_effect") ~= 0)
-	local demand = enabled and meta:get_int("power") or 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}
@@ -137,12 +143,37 @@
 	local to   = technic.get_cable_tier(name_down)
 
 	if from and to then
-		local input = meta:get_int(from.."_EU_input")
-		meta:set_int(from.."_EU_demand", demand)
-		meta:set_int(from.."_EU_supply", 0)
-		meta:set_int(to.."_EU_demand", 0)
-		meta:set_int(to.."_EU_supply", input * remain)
-		meta:set_string("infotext", S("@1 (@2 @3 -> @4 @5)", machine_name, technic.pretty_num(input), from, technic.pretty_num(input * remain), to))
+		-- 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
@@ -158,9 +189,14 @@
 
 minetest.register_node("technic:supply_converter", {
 	description = S("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"},
+	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"},
@@ -187,10 +223,11 @@
 minetest.register_craft({
 	output = 'technic:supply_converter 1',
 	recipe = {
-		{'technic:fine_gold_wire', 'technic:rubber',         'technic:doped_silicon_wafer'},
+		{'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"}, },
 })
 
 for tier, machines in pairs(technic.machines) do

--
Gitblit v1.8.0