From 56e96b2593817eb77fcb51e4a81c80f4df6bb0c7 Mon Sep 17 00:00:00 2001
From: Tim <t4im@users.noreply.github.com>
Date: Fri, 30 Jan 2015 05:16:41 +0100
Subject: [PATCH] add sawdust and tree/wood grindings with arcacia tree for dye and provide a better alternative rubbertree->latex path that yields the same as the tree tap, but requires more machine work

---
 technic/machines/switching_station.lua |   43 ++++++++++++++++++++++++++++++++-----------
 1 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/technic/machines/switching_station.lua b/technic/machines/switching_station.lua
index 6b59277..737970d 100644
--- a/technic/machines/switching_station.lua
+++ b/technic/machines/switching_station.lua
@@ -1,7 +1,10 @@
 -- SWITCHING STATION
 -- The switching station is the center of all power distribution on an electric network.
--- The station will collect all produced power from producers (PR) and batteries (BA)
--- and distribute it to receivers (RE) and depleted batteries (BA).
+--
+-- The station collects power from sources (PR), distributes it to sinks (RE),
+-- and uses the excess/shortfall to charge and discharge batteries (BA).
+--
+-- For now, all supply and demand values are expressed in kW.
 --
 -- It works like this:
 --  All PR,BA,RE nodes are indexed and tagged with the switching station.
@@ -79,15 +82,9 @@
 	return true
 end
 
-local load_position = function(pos)
-	if minetest.get_node_or_nil(pos) then return end
-	local vm = VoxelManip()
-	local MinEdge, MaxEdge = vm:read_from_map(pos, pos)
-end
-
 -- Generic function to add found connected nodes to the right classification array
 local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos)
-	load_position(pos)
+	technic.get_or_load_node(pos)
 	local meta = minetest.get_meta(pos)
 	local name = minetest.get_node(pos).name
 
@@ -216,7 +213,7 @@
 		-- Run all the nodes
 		local function run_nodes(list)
 			for _, pos2 in ipairs(list) do
-				load_position(pos2)
+				technic.get_or_load_node(pos2)
 				local node2 = minetest.get_node(pos2)
 				local nodedef
 				if node2 and node2.name then
@@ -236,6 +233,30 @@
 		local eu_demand_str    = tier.."_EU_demand"
 		local eu_input_str     = tier.."_EU_input"
 		local eu_supply_str    = tier.."_EU_supply"
+
+		-- Distribute charge equally across multiple batteries.
+		local charge_total = 0
+		local battery_count = 0
+
+		for n, pos1 in pairs(BA_nodes) do
+			meta1 = minetest.get_meta(pos1)
+			local charge = meta1:get_int("internal_EU_charge")
+
+			if (meta1:get_int(eu_demand_str) ~= 0) then
+				charge_total = charge_total + charge
+				battery_count = battery_count + 1
+			end
+		end
+
+		local charge_distributed = math.floor(charge_total / battery_count)
+
+		for n, pos1 in pairs(BA_nodes) do
+			meta1 = minetest.get_meta(pos1)
+
+			if (meta1:get_int(eu_demand_str) ~= 0) then
+				meta1:set_int("internal_EU_charge", charge_distributed)
+			end
+		end
 
 		-- Get all the power from the PR nodes
 		local PR_eu_supply = 0 -- Total power
@@ -344,7 +365,7 @@
 	local meta = minetest.get_meta(pos)
 	local timeout = meta:get_int(tier.."_EU_timeout")
 	if timeout <= 0 then
-		--meta:set_int(tier.."_EU_input", 0) -- Not needed anymore
+		meta:set_int(tier.."_EU_input", 0) -- Not needed anymore <-- actually, it is for supply converter
 		return true
 	else
 		meta:set_int(tier.."_EU_timeout", timeout - 1)

--
Gitblit v1.8.0