From 623fcae4a4ad3ec12cc242b29b0d781357cff3f7 Mon Sep 17 00:00:00 2001
From: Zefram <zefram@fysh.org>
Date: Sat, 24 May 2014 00:24:30 +0200
Subject: [PATCH] Fix supply converter

---
 technic/machines/switching_station.lua |   72 +++++++++++++++++++++++++-----------
 1 files changed, 50 insertions(+), 22 deletions(-)

diff --git a/technic/machines/switching_station.lua b/technic/machines/switching_station.lua
index de9b4aa..88d1890 100644
--- a/technic/machines/switching_station.lua
+++ b/technic/machines/switching_station.lua
@@ -27,20 +27,22 @@
 --
 --  The reason the LV|MV|HV type is prepended toe meta data is because some machine could require several supplies to work.
 --  This way the supplies are separated per network.
-technic.DBG = 1
-local dprint = technic.dprint
+
+technic.networks = {}
+
+local S = technic.getter
 
 minetest.register_craft({
 	output = "technic:switching_station",
 	recipe = {
-		{"default:steel_ingot",  "technic:lv_transformer", "default:steel_ingot"},
-		{"default:copper_ingot", "technic:lv_cable0",      "default:copper_ingot"},
-		{"default:steel_ingot",  "technic:lv_cable0",      "default:steel_ingot"}
+		{"technic:cast_iron_ingot", "technic:lv_transformer", "technic:cast_iron_ingot"},
+		{"default:copper_ingot",    "technic:lv_cable0",      "default:copper_ingot"},
+		{"technic:cast_iron_ingot", "technic:lv_cable0",      "technic:cast_iron_ingot"}
 	}
 })
 
 minetest.register_node("technic:switching_station",{
-	description = "Switching Station",
+	description = S("Switching Station"),
 	tiles  = {"technic_water_mill_top_active.png", "technic_water_mill_top_active.png",
                   "technic_water_mill_top_active.png", "technic_water_mill_top_active.png",
 	          "technic_water_mill_top_active.png", "technic_water_mill_top_active.png"},
@@ -54,7 +56,7 @@
 	},
 	on_construct = function(pos)
 		local meta = minetest.get_meta(pos)
-		meta:set_string("infotext", "Switching Station")
+		meta:set_string("infotext", S("Switching Station"))
 	end,
 })
 
@@ -65,7 +67,7 @@
 -- A node must be touched by the station continuously in order to function
 function technic.switching_station_timeout_count(pos, tier)
 	local meta = minetest.get_meta(pos)
-	timeout = meta:get_int(tier.."_EU_timeout")
+	local timeout = meta:get_int(tier.."_EU_timeout")
 	if timeout == 0 then
 		meta:set_int(tier.."_EU_input", 0)
 	else
@@ -104,6 +106,9 @@
 			add_new_cable_node(PR_nodes, pos)
 		elseif machines[name] == technic.receiver then
 			add_new_cable_node(RE_nodes, pos)
+		elseif machines[name] == technic.producer_receiver then
+			add_new_cable_node(PR_nodes, pos)
+			add_new_cable_node(RE_nodes, pos)
 		elseif machines[name] == technic.battery then
 			add_new_cable_node(BA_nodes, pos)
 		end
@@ -128,6 +133,35 @@
 	end
 end
 
+local touch_nodes = function(list, tier)
+	for _, pos in ipairs(list) do
+		local meta = minetest.get_meta(pos)
+		meta:set_int(tier.."_EU_timeout", 2) -- Touch node
+	end
+end
+
+local get_network = function(pos1, tier)
+	local cached = technic.networks[minetest.hash_node_position(pos1)]
+	if cached and cached.tier == tier then
+		touch_nodes(cached.PR_nodes, tier)
+		touch_nodes(cached.BA_nodes, tier)
+		touch_nodes(cached.RE_nodes, tier)
+		return cached.PR_nodes, cached.BA_nodes, cached.RE_nodes
+	end
+	local i = 1
+	local PR_nodes = {}
+	local BA_nodes = {}
+	local RE_nodes = {}
+	local all_nodes = {pos1}
+	repeat
+		traverse_network(PR_nodes, RE_nodes, BA_nodes, all_nodes,
+				i, technic.machines[tier], tier)
+		i = i + 1
+	until all_nodes[i] == nil
+	technic.networks[minetest.hash_node_position(pos1)] = {tier = tier, PR_nodes = PR_nodes, RE_nodes = RE_nodes, BA_nodes = BA_nodes}
+	return PR_nodes, BA_nodes, RE_nodes
+end
+
 -----------------------------------------------
 -- The action code for the switching station --
 -----------------------------------------------
@@ -145,27 +179,21 @@
 		local RE_EU            = 0 -- EUs to RE nodes
 
 		local tier      = ""
-		local all_nodes = {}
-		local PR_nodes  = {}
-		local BA_nodes  = {} 
-		local RE_nodes  = {}
+		local PR_nodes
+		local BA_nodes
+		local RE_nodes
+		local machine_name = S("Switching Station")
 
 		-- Which kind of network are we on:
 		pos1 = {x=pos.x, y=pos.y-1, z=pos.z}
-		all_nodes[1] = pos1
 
 		local name = minetest.get_node(pos1).name
 		local tier = technic.get_cable_tier(name)
 		if tier then
-			local i = 1
-			repeat
-				traverse_network(PR_nodes, RE_nodes, BA_nodes, all_nodes,
-						i, technic.machines[tier], tier)
-				i = i + 1
-			until all_nodes[i] == nil
+			PR_nodes, BA_nodes, RE_nodes = get_network(pos1, tier)
 		else
 			--dprint("Not connected to a network")
-			meta:set_string("infotext", "Switching Station - no network")
+			meta:set_string("infotext", S("%s Has No Network"):format(machine_name))
 			return
 		end
 		--dprint("nodes="..table.getn(all_nodes)
@@ -211,8 +239,8 @@
 		--dprint("Total BA demand:"..BA_eu_demand)
 
 		meta:set_string("infotext",
-				"Switching Station. Supply: "..PR_eu_supply
-				.." Demand: "..RE_eu_demand)
+				S("%s. Supply: %d Demand: %d"):format(
+				machine_name, PR_eu_supply, RE_eu_demand))
 
 		-- If the PR supply is enough for the RE demand supply them all
 		if PR_eu_supply >= RE_eu_demand then

--
Gitblit v1.8.0