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)