Make power distribution more efficient.
authorNovatux <nathanael.courant@laposte.net>
Thu, 29 Aug 2013 07:34:40 +0000 (09:34 +0200)
committerShadowNinja <noreply@gmail.com>
Fri, 4 Oct 2013 14:48:39 +0000 (10:48 -0400)
Conflicts:
technic/machines/register/cables.lua

technic/machines/register/cables.lua
technic/machines/switching_station.lua
technic/register.lua

index 9fbd0fd5c6887412db217ac87256e936bdc18cf1..39b3f08eec80bbfd9c7918d17cde461c5b8759c6 100644 (file)
@@ -58,6 +58,8 @@ minetest.register_on_placenode(function(pos, node)
                for machine_name, _ in pairs(machine_list) do
                        if node.name == machine_name then
                                technic.update_cables(pos, tier, true)
+                               technic.networks = {}
+                               return
                        end
                end
        end
@@ -69,6 +71,8 @@ minetest.register_on_dignode(function(pos, node)
                for machine_name, _ in pairs(machine_list) do
                        if node.name == machine_name then
                                technic.update_cables(pos, tier, true)
+                               technic.networks = {}
+                               return
                        end
                end
        end
index de9b4aa12b974423035f7847ec3d5c8b15d36611..25310eb32f814f5371b5a8f757da0966dd7c779d 100644 (file)
@@ -128,6 +128,35 @@ local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, all_nodes, i, ma
        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[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[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,24 +174,17 @@ minetest.register_abm({
                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
 
                -- 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, RE_nodes, BA_nodes = get_network(pos1, tier)
                else
                        --dprint("Not connected to a network")
                        meta:set_string("infotext", "Switching Station - no network")
index 0dc321de833925e31c776e91bba7d6ad6b0b17cd..38766d906991d931160ffb343a5cc3b80a531ed4 100644 (file)
@@ -7,6 +7,8 @@ technic.battery  = "BA"
 
 technic.machines    = {}
 technic.power_tools = {}
+technic.networks = {}
+
 
 function technic.register_tier(tier, description)
        technic.machines[tier]    = {}