Files
Bubberstation/code/modules/power/monitor.dm
SkyratBot a2aaacdead [MIRROR] large refactor of machine/power code to cut down on processing time and wasted lists (#7920)
* large refactor of machine/power code to cut down on processing time and wasted lists (#60317)

original pr here: #59789 (Closed because he didn't think it was good enough)
came back to this because i realized that

    all machines were area sensitive, meaning they had a list with at least a reference to themselves (assuming they arent in the contents of another movable which most arent) for the purposes of handling power differences when their area changes
    pipes are machines
    there are ~14k machines and ~6k pipes
    i made this problem worse with a recent pr by making it a nested list

so i needed to track what machines needed power, and this pr had work already done that could be used for that purpose. now machines that have use_power == NO_POWER_USE do not have this extra memory overhead for no reason

currently every machine that uses power draws that amount from its area from a dynamic channel via auto_use_power() which is called every SSmachines fire(), then in apc/process() the area's dynamic power draw is reset and the power is used. with static power its not calculated then reset every loop, its just taken from the grid. so now machines handle updating their static power usage from their current area (this doesnt touch power machines that require a wire connection). in order to allow this, use_power, idle_power_usage, and active_power_usage have setters to track state correctly and update the static power usage on the machines current area and handle area sensitivity.

also goes through a lot of heavy abusers of SSmachine processing time and tries to make it faster. makes airalarm/process() into a signal handler for COMSIG_TURF_EXPOSE since air alarms only need to process for changes.
Why It's Good For The Game

SSmachines isnt the heaviest hitter in terms of total cpu and certainly not in terms of overtime, but its not a lightweight. it frequently takes > 50ms to complete a run and seems to be in the top 5 or so of subsystem costs looking at some round profilers

also gets rid of a few thousand lists since every pipe no longer has two useless lists each (and any other machines that dont use power)

Love ya kyler

Co-authored-by: Rohesie <rohesie@ gmail.com>

* large refactor of machine/power code to cut down on processing time and wasted lists

Co-authored-by: Kylerace <kylerlumpkin1@gmail.com>
Co-authored-by: Rohesie <rohesie@ gmail.com>
2021-09-02 03:11:59 +01:00

122 lines
4.0 KiB
Plaintext

//modular computer program version is located in code\modules\modular_computers\file_system\programs\powermonitor.dm, /datum/computer_file/program/power_monitor
/obj/machinery/computer/monitor
name = "power monitoring console"
desc = "It monitors power levels across the station."
icon_screen = "power"
icon_keyboard = "power_key"
light_color = LIGHT_COLOR_YELLOW
use_power = ACTIVE_POWER_USE
idle_power_usage = 20
active_power_usage = 100
circuit = /obj/item/circuitboard/computer/powermonitor
tgui_id = "PowerMonitor"
var/obj/structure/cable/attached_wire
var/obj/machinery/power/apc/local_apc
var/list/history = list()
var/record_size = 60
var/record_interval = 50
var/next_record = 0
var/is_secret_monitor = FALSE
/obj/machinery/computer/monitor/secret //Hides the power monitor (such as ones on ruins & CentCom) from PDA's to prevent metagaming.
name = "outdated power monitoring console"
desc = "It monitors power levels across the local powernet."
circuit = /obj/item/circuitboard/computer/powermonitor/secret
is_secret_monitor = TRUE
/obj/machinery/computer/monitor/secret/examine(mob/user)
. = ..()
. += span_notice("It's operating system seems quite outdated... It doesn't seem like it'd be compatible with the latest remote NTOS monitoring systems.")
/obj/machinery/computer/monitor/Initialize()
. = ..()
search()
history["supply"] = list()
history["demand"] = list()
/obj/machinery/computer/monitor/process()
if(!get_powernet())
update_use_power(IDLE_POWER_USE)
search()
else
update_use_power(ACTIVE_POWER_USE)
record()
/obj/machinery/computer/monitor/proc/search() //keep in sync with /datum/computer_file/program/power_monitor's version
var/turf/T = get_turf(src)
attached_wire = locate(/obj/structure/cable) in T
if(attached_wire)
return
var/area/A = get_area(src) //if the computer isn't directly connected to a wire, attempt to find the APC powering it to pull it's powernet instead
if(!A)
return
local_apc = A.get_apc()
if(!local_apc)
return
if(!local_apc.terminal) //this really shouldn't happen without badminnery.
local_apc = null
/obj/machinery/computer/monitor/proc/get_powernet() //keep in sync with /datum/computer_file/program/power_monitor's version
if(attached_wire || (local_apc?.terminal))
return attached_wire ? attached_wire.powernet : local_apc.terminal.powernet
return FALSE
/obj/machinery/computer/monitor/proc/record() //keep in sync with /datum/computer_file/program/power_monitor's version
if(world.time >= next_record)
next_record = world.time + record_interval
var/datum/powernet/connected_powernet = get_powernet()
var/list/supply = history["supply"]
if(connected_powernet)
supply += connected_powernet.viewavail
if(supply.len > record_size)
supply.Cut(1, 2)
var/list/demand = history["demand"]
if(connected_powernet)
demand += connected_powernet.viewload
if(demand.len > record_size)
demand.Cut(1, 2)
/obj/machinery/computer/monitor/ui_interact(mob/user, datum/tgui/ui)
ui = SStgui.try_update_ui(user, src, ui)
if(!ui)
ui = new(user, src, "PowerMonitor", name)
ui.open()
/obj/machinery/computer/monitor/ui_data()
var/datum/powernet/connected_powernet = get_powernet()
var/list/data = list()
data["stored"] = record_size
data["interval"] = record_interval / 10
data["attached"] = connected_powernet ? TRUE : FALSE
data["history"] = history
data["areas"] = list()
if(connected_powernet)
data["supply"] = DisplayPower(connected_powernet.viewavail)
data["demand"] = DisplayPower(connected_powernet.viewload)
for(var/obj/machinery/power/terminal/term in connected_powernet.nodes)
var/obj/machinery/power/apc/A = term.master
if(istype(A))
var/cell_charge
if(!A.cell)
cell_charge = 0
else
cell_charge = A.cell.percent()
data["areas"] += list(list(
"name" = A.area.name,
"charge" = cell_charge,
"load" = DisplayPower(A.lastused_total),
"charging" = A.charging,
"eqp" = A.equipment,
"lgt" = A.lighting,
"env" = A.environ
))
return data