From 3d3dfdc80a47aacf56f36b5cd72182d467ebd7f5 Mon Sep 17 00:00:00 2001 From: SkyratBot <59378654+SkyratBot@users.noreply.github.com> Date: Mon, 23 Nov 2020 13:34:33 +0100 Subject: [PATCH] [MIRROR] Cell chargers now pull from the grid's surplus rather than their room's APC cell (#1782) * Cell chargers now pull from the grid's surplus rather than their room's APC cell (#54989) About The Pull Request As the title says. Creates a new proc, use_power_from_net() that attempts to pull power from the grid (by adding the amount needed to the APC's powernet load), and returns the amount gathered if there was enough surplus to do so. This bypasses the APC's internal cell for power drawn this way. Changes cell chargers to use this new proc for charging a cell. The charger machine still uses some power from the APC, set to 1% of it's max cell charging rate, for machine-related power costs. I'm not deadset on that number, I just needed to start with something. To be clear, this 1% is to simulate running the cell charger's circuitry; all of the power for the cell it's charging is 1-to-1 coming from the powernet. This does not subvert the original PR's purpose; cells are still drawing as much power from the grid as they charge with. Why It's Good For The Game Fixes cell chargers breaking rooms after the prior cell charger fix. The load is now on the grid, and it being surplus means that it (probably) won't actually powersink anything. Fixes #54919 by taking a different approach that entirely avoids the issue. * Cell chargers now pull from the grid's surplus rather than their room's APC cell Co-authored-by: zxaber <37497534+zxaber@users.noreply.github.com> --- code/game/machinery/cell_charger.dm | 11 +++++++--- code/modules/power/power.dm | 34 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/code/game/machinery/cell_charger.dm b/code/game/machinery/cell_charger.dm index c5c00fc7668..2b82ad34a03 100644 --- a/code/game/machinery/cell_charger.dm +++ b/code/game/machinery/cell_charger.dm @@ -128,6 +128,11 @@ if(charging.percent() >= 100) return - if(directly_use_power(charge_rate * delta_time)) - charging.give(charge_rate * delta_time) - update_icon() + + var/main_draw = use_power_from_net(charge_rate * delta_time, take_any = TRUE) //Pulls directly from the Powernet to dump into the cell + if(!main_draw) + return + charging.give(main_draw) + use_power(charge_rate / 100) //use a small bit for the charger itself, but power usage scales up with the part tier + + update_icon() diff --git a/code/modules/power/power.dm b/code/modules/power/power.dm index b7f742ffb87..bf018620b10 100644 --- a/code/modules/power/power.dm +++ b/code/modules/power/power.dm @@ -118,6 +118,40 @@ local_apc.cell.use(amount) return TRUE +/** + * Attempts to draw power directly from the APC's Powernet rather than the APC's battery. For high-draw machines, like the cell charger + * + * Checks the surplus power on the APC's powernet, and compares to the requested amount. If the requested amount is available, this proc + * will add the amount to the APC's usage and return that amount. Otherwise, this proc will return FALSE. + * If the take_any var arg is set to true, this proc will use and return any surplus that is under the requested amount, assuming that + * the surplus is above zero. + * Args: + * - amount, the amount of power requested from the Powernet. In standard loosely-defined SS13 power units. + * - take_any, a bool of whether any amount of power is acceptable, instead of all or nothing. Defaults to FALSE + */ +/obj/machinery/proc/use_power_from_net(amount, take_any = FALSE) + if(amount <= 0) //just in case + return FALSE + var/area/home = get_area(src) + + if(!home) + return FALSE //apparently space isn't an area + if(!home.requires_power) + return amount //Shuttles get free power, don't ask why + + var/obj/machinery/power/apc/local_apc = home?.get_apc() + if(!local_apc) + return FALSE + var/surplus = local_apc.surplus() + if(surplus <= 0) //I don't know if powernet surplus can ever end up negative, but I'm just gonna failsafe it + return FALSE + if(surplus < amount) + if(!take_any) + return FALSE + amount = surplus + local_apc.add_load(amount) + return amount + /obj/machinery/proc/addStaticPower(value, powerchannel) var/area/A = get_area(src) if(!A)