Files
Bubberstation/code/controllers/subsystem/machines.dm
Pickle-Coding c1f11f26ce Converts arbitrary energy units to the joule. Fixes conservation of energy issues relating to charging cells. (#81579)
## About The Pull Request
Removes all arbitrary energy and power units in the codebase. Everything
is replaced with the joule and watt, with 1 = 1 joule, or 1 watt if you
are going to multiply by time. This is a visible change, where all
arbitrary energy units you see in the game will get proper prefixed
units of energy.

With power cells being converted to the joule, charging one joule of a
power cell will require one joule of energy.

The grid will now store energy, instead of power. When an energy usage
is described as using the watt, a power to energy conversion based on
the relevant subsystem's timing (usually multiplying by seconds_per_tick
or applying power_to_energy()) is needed before adding or removing from
the grid. Power usages that are described as the watt is really anything
you would scale by time before applying the load. If it's described as a
joule, no time conversion is needed. Players will still read the grid as
power, having no visible change.

Machines that dynamically use power with the use_power() proc will
directly drain from the grid (and apc cell if there isn't enough)
instead of just tallying it up on the dynamic power usages for the area.
This should be more robust at conserving energy as the surplus is
updated on the go, preventing charging cells from nothing.

APCs no longer consume power for the dynamic power usage channels. APCs
will consume power for static power usages. Because static power usages
are added up without checking surplus, static power consumption will be
applied before any machine processes. This will give a more truthful
surplus for dynamic power consumers.

APCs will display how much power it is using for charging the cell. APC
cell charging applies power in its own channel, which gets added up to
the total. This will prevent invisible power usage you see when looking
at the power monitoring console.

After testing in MetaStation, I found roundstart power consumption to be
around 406kW after all APCs get fully charged. During the roundstart APC
charge rush, the power consumption can get as high as over 2MW (up to
25kW per roundstart APC charging) as long as there's that much
available.

Because of the absurd potential power consumption of charging APCs near
roundstart, I have changed how APCs decide to charge. APCs will now
charge only after all other machines have processed in the machines
processing subsystem. This will make sure APC charging won't disrupt
machines taking from the grid, and should stop APCs getting their power
drained due to others demanding too much power while charging. I have
removed the delays for APC charging too, so they start charging
immediately whenever there's excess power. It also stops them turning
red when a small amount of cell gets drained (airlocks opening and shit
during APC charge rush), as they immediately become fully charged
(unless too much energy got drained somehow) before changing icon.

Engineering SMES now start at 100% charge instead of 75%. I noticed
cells were draining earlier than usual after these changes, so I am
making them start maxed to try and combat that.

These changes will fix all conservation of energy issues relating to
charging powercells.
## Why It's Good For The Game
Closes #73438
Closes #75789
Closes #80634
Closes #82031

Makes it much easier to interface with the power system in the codebase.
It's more intuitive. Removes a bunch of conservation of energy issues,
making energy and power much more meaningful. It will help the
simulation remain immersive as players won't encounter energy
duplication so easily. Arbitrary energy units getting replaced with the
joule will also tell people more meaningful information when reading it.
APC charging will feel more snappy.
## Changelog
🆑
fix: Fixes conservation of energy issues relating to charging
powercells.
qol: APCs will display how much power they are using to charge their
cell. This is accounted for in the power monitoring console.
qol: All arbitrary power cell energy units you see are replaced with
prefixed joules.
balance: As a consequence of the conservation of energy issues getting
fixed, the power consumption for charging cells is now very significant.
balance: APCs only use surplus power from the grid after every machine
processes when charging, preventing APCs from causing others to
discharge while charging.
balance: Engineering SMES start at max charge to combat the increased
energy loss due to conservation of energy fixes.
/🆑

---------

Co-authored-by: SyncIt21 <110812394+SyncIt21@users.noreply.github.com>
Co-authored-by: Ghom <42542238+Ghommie@users.noreply.github.com>
2024-03-23 16:58:56 +01:00

150 lines
5.6 KiB
Plaintext

SUBSYSTEM_DEF(machines)
name = "Machines"
init_order = INIT_ORDER_MACHINES
flags = SS_KEEP_TIMING
wait = 2 SECONDS
/// Assosciative list of all machines that exist.
VAR_PRIVATE/list/machines_by_type = list()
/// All machines, not just those that are processing.
VAR_PRIVATE/list/all_machines = list()
var/list/processing = list()
var/list/currentrun = list()
var/list/apc_early_processing = list()
var/list/apc_late_processing = list()
var/current_part = SSMACHINES_APCS_EARLY
///List of all powernets on the server.
var/list/datum/powernet/powernets = list()
/datum/controller/subsystem/machines/Initialize()
makepowernets()
fire()
return SS_INIT_SUCCESS
/// Registers a machine with the machine subsystem; should only be called by the machine itself during its creation.
/datum/controller/subsystem/machines/proc/register_machine(obj/machinery/machine)
LAZYADD(machines_by_type[machine.type], machine)
all_machines |= machine
/// Removes a machine from the machine subsystem; should only be called by the machine itself inside Destroy.
/datum/controller/subsystem/machines/proc/unregister_machine(obj/machinery/machine)
var/list/existing = machines_by_type[machine.type]
existing -= machine
if(!length(existing))
machines_by_type -= machine.type
all_machines -= machine
/// Gets a list of all machines that are either the passed type or a subtype.
/datum/controller/subsystem/machines/proc/get_machines_by_type_and_subtypes(obj/machinery/machine_type)
if(!ispath(machine_type))
machine_type = machine_type.type
if(!ispath(machine_type, /obj/machinery))
CRASH("called get_machines_by_type_and_subtypes with a non-machine type [machine_type]")
var/list/machines = list()
for(var/next_type in typesof(machine_type))
var/list/found_machines = machines_by_type[next_type]
if(found_machines)
machines += found_machines
return machines
/// Gets a list of all machines that are the exact passed type.
/datum/controller/subsystem/machines/proc/get_machines_by_type(obj/machinery/machine_type)
if(!ispath(machine_type))
machine_type = machine_type.type
if(!ispath(machine_type, /obj/machinery))
CRASH("called get_machines_by_type with a non-machine type [machine_type]")
var/list/machines = machines_by_type[machine_type]
return machines?.Copy() || list()
/datum/controller/subsystem/machines/proc/get_all_machines()
return all_machines.Copy()
/datum/controller/subsystem/machines/proc/makepowernets()
for(var/datum/powernet/power_network as anything in powernets)
qdel(power_network)
powernets.Cut()
for(var/obj/structure/cable/power_cable as anything in GLOB.cable_list)
if(!power_cable.powernet)
var/datum/powernet/new_powernet = new()
new_powernet.add_cable(power_cable)
propagate_network(power_cable, power_cable.powernet)
/datum/controller/subsystem/machines/stat_entry(msg)
msg = "M:[length(all_machines)]|MT:[length(machines_by_type)]|PM:[length(processing)]|PN:[length(powernets)]"
return ..()
/datum/controller/subsystem/machines/fire(resumed = FALSE)
if (!resumed)
for(var/datum/powernet/powernet as anything in powernets)
powernet.reset() //reset the power state.
current_part = SSMACHINES_APCS_EARLY
src.currentrun = apc_early_processing.Copy()
//APC early processing. Draws static power usages from their grids.
if(current_part == SSMACHINES_APCS_EARLY)
//cache for sanic speed (lists are references anyways)
var/list/currentrun = src.currentrun
while(currentrun.len)
var/obj/machinery/power/apc/apc = currentrun[currentrun.len]
currentrun.len--
if(QDELETED(apc) || apc.early_process(wait * 0.1) == PROCESS_KILL)
apc_early_processing -= apc
apc.datum_flags &= ~DF_ISPROCESSING
if(MC_TICK_CHECK)
return
current_part = SSMACHINES_MACHINES
src.currentrun = processing.Copy()
//General machine processing. Their power usage can be dynamic and based on surplus power, so they come after static power usage have been applied.
if(current_part == SSMACHINES_MACHINES)
//cache for sanic speed (lists are references anyways)
var/list/currentrun = src.currentrun
while(currentrun.len)
var/obj/machinery/thing = currentrun[currentrun.len]
currentrun.len--
if(QDELETED(thing) || thing.process(wait * 0.1) == PROCESS_KILL)
processing -= thing
thing.datum_flags &= ~DF_ISPROCESSING
if (MC_TICK_CHECK)
return
current_part = SSMACHINES_APCS_LATE
src.currentrun = apc_late_processing.Copy()
//APC late processing. APCs will use the remaining power on the grid to charge their cells if needed.
//This is applied at the end so charging APCs don't cause others to discharge by taking all the power from the grid before machines use power.
if(current_part == SSMACHINES_APCS_LATE)
//cache for sanic speed (lists are references anyways)
var/list/currentrun = src.currentrun
while(currentrun.len)
var/obj/machinery/power/apc/apc = currentrun[currentrun.len]
currentrun.len--
if(QDELETED(apc) || apc.late_process(wait * 0.1) == PROCESS_KILL)
apc_late_processing -= apc
apc.datum_flags &= ~DF_ISPROCESSING
if(MC_TICK_CHECK)
return
/datum/controller/subsystem/machines/proc/setup_template_powernets(list/cables)
var/obj/structure/cable/PC
for(var/A in 1 to cables.len)
PC = cables[A]
if(!PC.powernet)
var/datum/powernet/NewPN = new()
NewPN.add_cable(PC)
propagate_network(PC,PC.powernet)
/datum/controller/subsystem/machines/Recover()
if(islist(SSmachines.processing))
processing = SSmachines.processing
if(islist(SSmachines.powernets))
powernets = SSmachines.powernets
if(islist(SSmachines.all_machines))
all_machines = SSmachines.all_machines
if(islist(SSmachines.machines_by_type))
machines_by_type = SSmachines.machines_by_type