Files
Bubberstation/code/controllers/subsystem/machines.dm
Zephyr f71ea26f72 Machine list is now stored in SSmachines | Remove excessive use of global lists for specific machine types (#76822)
## About The Pull Request

Removes all of the duplicate global lists for specific machine types
where the only thing they do is store all machines of that type.
Adds machine tracking to SSmachines in the form of a list for all
machines, and then an associative list for machines by their type.
Previously we have machines in multiple global lists, such as airlocks
being in GLOB.doors, GLOB.airlocks, GLOB.machines.
This makes that not a thing, and also means that iterating through
GLOB.machines looking for a specific type is no longer as expensive.
2023-07-15 16:17:46 -04:00

114 lines
4.0 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()
///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.
src.currentrun = processing.Copy()
//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
/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