Merge pull request #6104 from mwerezak/power-net

Fixes for APCs (from dev), fix for solars area, and simple APC load balancing
This commit is contained in:
Ccomp5950
2014-08-24 12:26:29 -05:00
4 changed files with 329 additions and 305 deletions

View File

@@ -1142,6 +1142,16 @@
else
return 0
/obj/machinery/power/apc/proc/last_surplus()
if(terminal && terminal.powernet)
return terminal.powernet.last_surplus()
else
return 0
//Returns 1 if the APC should attempt to charge
/obj/machinery/power/apc/proc/attempt_charging()
return (chargemode && charging == 1 && operating)
/obj/machinery/power/apc/add_load(var/amount)
if(terminal && terminal.powernet)
return terminal.powernet.draw_power(amount)
@@ -1189,38 +1199,44 @@
if(cell && !shorted)
//var/cell_charge = cell.charge
var/cell_maxcharge = cell.maxcharge
// Calculate how much power the APC will try to get from the grid.
var/target_draw = lastused_total
if (src.attempt_charging())
target_draw += min((cell_maxcharge - cell.charge), (cell_maxcharge*CHARGELEVEL))/CELLRATE
target_draw = min(target_draw, perapc) //limit power draw by perapc
// try to draw power from the grid
if (!src.avail())
main_status = 0
else
var/target_draw = perapc
if (charging == 2)
target_draw = min(target_draw, lastused_total) //if we're fully charged, only take what we need to meet demand
var/power_drawn = 0
if (src.avail())
power_drawn = add_load(target_draw) //get some power from the powernet
//figure out how much power is left over after meeting demand
power_excess = power_drawn - lastused_total
if (power_excess < 0) //couldn't get enough power from the grid, we will need to take from the power cell.
var/power_drawn = add_load(target_draw) //get some power from the powernet
//figure out how much power is left over after meeting demand
power_excess = power_drawn - lastused_total
if (power_excess < 0) //couldn't get enough power from the grid, we will need to take from the power cell.
main_status = 1
charging = 0
var/required_power = -power_excess
if( (cell.charge/CELLRATE) >= required_power) // can we draw enough from cell to cover what's left over?
cell.use(required_power*CELLRATE)
charging = 0
var/required_power = -power_excess
if(cell.charge >= required_power*CELLRATE) // can we draw enough from cell to cover what's left over?
cell.use(required_power*CELLRATE)
else if (autoflag != 0) // not enough power available to run the last tick!
chargecount = 0
// This turns everything off in the case that there is still a charge left on the battery, just not enough to run the room.
equipment = autoset(equipment, 0)
lighting = autoset(lighting, 0)
environ = autoset(environ, 0)
autoflag = 0
else
main_status = 2
else if (autoflag != 0) // not enough power available to run the last tick!
chargecount = 0
// This turns everything off in the case that there is still a charge left on the battery, just not enough to run the room.
equipment = autoset(equipment, 0)
lighting = autoset(lighting, 0)
environ = autoset(environ, 0)
autoflag = 0
//Set external power status
if (!power_drawn)
main_status = 0
else if (power_excess < 0)
main_status = 1
else
main_status = 2
// Set channels depending on how much charge we have left
@@ -1263,13 +1279,9 @@
autoflag = 0
// now trickle-charge the cell
if(chargemode && charging == 1 && operating)
if(power_excess > 0) // check to make sure we have enough to charge
// Max charge is available excess power, capped to cell capacity, or % per second constant (Whichever is smallest)
var/ch = min(power_excess*CELLRATE, (cell_maxcharge - cell.charge), (cell_maxcharge*CHARGELEVEL))
cell.give(ch) // actually recharge the cell
if(src.attempt_charging())
if (power_excess > 0) // check to make sure we have enough to charge
cell.give(power_excess*CELLRATE) // actually recharge the cell
else
charging = 0 // stop charging
chargecount = 0
@@ -1281,14 +1293,14 @@
//if we have excess power for long enough, think about re-enable charging.
if(chargemode)
if(!charging)
if(power_excess*CELLRATE >= cell_maxcharge*CHARGELEVEL)
//last_surplus() overestimates the amount of power available for charging, but it's equivalent to what APCs were doing before.
if(src.last_surplus()*CELLRATE >= cell_maxcharge*CHARGELEVEL)
chargecount++
else
chargecount = 0
charging = 0
if(chargecount == 10)
if(chargecount >= 10)
chargecount = 0
charging = 1

View File

@@ -81,9 +81,7 @@
if(has_power)
stat &= ~NOPOWER
else
stat |= NOPOWER
return
// the powernet datum
// each contiguous network of cables & nodes
@@ -387,10 +385,13 @@
if( istype( term.master, /obj/machinery/power/apc ) )
numapc++
if(numapc)
perapc = avail/numapc
netexcess = avail - load
if(numapc)
//very simple load balancing. If there was a net excess this tick then it must have been that some APCs used less than perapc, since perapc*numapc = avail
//Therefore we can raise the amount of power rationed out to APCs on the assumption that those APCs that used less than perapc will continue to do so.
//If that assumption fails, then some APCs will miss out on power next tick, however it will be rebalanced for the tick after.
perapc = (avail + netexcess)/numapc
if( netexcess > 100) // if there was excess power last cycle
if(nodes && nodes.len)
@@ -406,6 +407,11 @@
/datum/powernet/proc/surplus()
return max(avail - newload, 0)
//Returns the amount of excess power (before refunding to SMESs) from last tick.
//This is for machines that might adjust their power consumption using this data.
/datum/powernet/proc/last_surplus()
return max(avail - load, 0)
//Attempts to draw power from a powernet. Returns the actual amount of power drawn
/datum/powernet/proc/draw_power(var/requested_amount)
var/surplus = max(avail - newload, 0)