From d339d37531755ead80d06dd3da73456c83f954db Mon Sep 17 00:00:00 2001 From: rockdtben Date: Sun, 24 Jul 2011 00:00:16 +0000 Subject: [PATCH] Created a method for powernets called /datum/powernet/proc/merge_powernets(var/datum/powernet/P) This method takes an input of a powernet and merges it with the powernet that called merge_powernets This is the first step in my updated cable logic. The code should look cleaner as a result. git-svn-id: http://tgstation13.googlecode.com/svn/trunk@1913 316c924e-a436-60f5-8080-3fe189b3f50e --- code/modules/power/cable.dm | 75 +++++++------------------------------ code/modules/power/power.dm | 18 +++++++++ 2 files changed, 31 insertions(+), 62 deletions(-) diff --git a/code/modules/power/cable.dm b/code/modules/power/cable.dm index af82c7d3363..c538c87d3cb 100644 --- a/code/modules/power/cable.dm +++ b/code/modules/power/cable.dm @@ -454,40 +454,14 @@ if(TC.netnum != netnum) var/datum/powernet/PN = powernets[netnum] var/datum/powernet/TPN = powernets[TC.netnum] - var/kingNetnum = netnum - if(PN.cables.len + PN.nodes.len < TPN.cables.len + TPN.nodes.len) - kingNetnum = TC.netnum - var/datum/powernet/temp = PN - PN = TPN - TPN = temp - for(var/obj/cable/C in TPN.cables) - TPN.cables -= C - PN.cables += C - C.netnum = kingNetnum - for(var/obj/machinery/power/M in TPN.nodes) - if(M.netnum < 0) // APCs have netnum=-1 so they don't count as network nodes directly - continue - TPN.nodes -= M - PN.nodes += M - M.netnum = kingNetnum - M.powernet = powernets[M.netnum] - TC.netnum = kingNetnum - TPN.cables -= TC - PN.cables += TC - for(var/obj/machinery/power/M in TB) - if(!netnum) - if(!M.netnum) - continue - else - netnum = M.netnum - var/datum/powernet/PN = powernets[netnum] - PN.nodes += src + + PN.merge_powernets(TPN) /obj/cable/proc/mergeConnectedNetworksOnTurf() - var/turf/TB - TB = loc - for(var/obj/cable/C in TB) + + for(var/obj/cable/C in loc) + if(C == src) continue if(netnum == 0) @@ -495,19 +469,13 @@ netnum = C.netnum PN.cables += src continue + var/datum/powernet/PN = powernets[netnum] var/datum/powernet/TPN = powernets[C.netnum] - var/kingNetnum = netnum - if(PN.cables.len + PN.nodes.len < TPN.cables.len + TPN.nodes.len) - kingNetnum = C.netnum - var/datum/powernet/temp = PN - PN = TPN - TPN = temp - TPN.cables -= C - PN.cables += C - C.netnum = kingNetnum - for(var/obj/machinery/power/M in TB) + PN.merge_powernets(TPN) + + for(var/obj/machinery/power/M in loc) if(M.netnum < 0) continue @@ -519,18 +487,10 @@ var/datum/powernet/PN = powernets[netnum] var/datum/powernet/TPN = powernets[M.netnum] - var/kingNetnum = netnum - if(PN.cables.len + PN.nodes.len < TPN.cables.len + TPN.nodes.len) - kingNetnum = M.netnum - var/datum/powernet/temp = PN - PN = TPN - TPN = temp - TPN.nodes -= M - PN.nodes += M - M.netnum = kingNetnum - M.powernet = powernets[M.netnum] - for(var/obj/machinery/power/apc/N in TB) + PN.merge_powernets(TPN) + + for(var/obj/machinery/power/apc/N in loc) var/obj/machinery/power/M M = N.terminal @@ -545,17 +505,8 @@ var/datum/powernet/PN = powernets[netnum] var/datum/powernet/TPN = powernets[M.netnum] - var/kingNetnum = netnum - if(PN.cables.len + PN.nodes.len < TPN.cables.len + TPN.nodes.len) - kingNetnum = M.netnum - var/datum/powernet/temp = PN - PN = TPN - TPN = temp - TPN.nodes -= M - PN.nodes += M - M.netnum = kingNetnum - M.powernet = powernets[M.netnum] + PN.merge_powernets(TPN) obj/cable/proc/cableColor(var/colorC) var/color_n = "red" diff --git a/code/modules/power/power.dm b/code/modules/power/power.dm index c7baec43d23..ab2fe57e37c 100644 --- a/code/modules/power/power.dm +++ b/code/modules/power/power.dm @@ -367,6 +367,24 @@ else return 0 +/datum/powernet/proc/merge_powernets(var/datum/powernet/P) +// The powernet that calls this proc will consume the other powernet - Rockdtben + if(nodes.len >= P.nodes) + nodes += P.nodes + else + P.nodes += nodes + nodes = P.nodes + for(var/obj/machinery/power/M in nodes) + M.netnum = number + + if(cables.len >= P.cables) + cables += P.cables + else + P.cables += cables + cables = P.cables + for(var/obj/cable/C in cables) + C.netnum = number + /obj/machinery/power/proc/connect_to_network() var/turf/T = src.loc var/obj/cable/C = T.get_cable_node()