Skip to content

Commit

Permalink
Downstream refactors (#2104)
Browse files Browse the repository at this point in the history
  • Loading branch information
UEDCommander authored Apr 11, 2024
1 parent d046c97 commit 5761ecd
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 158 deletions.
1 change: 1 addition & 0 deletions baystation12.dme
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
#include "code\__defines\zmimic.dm"
#include "code\__defines\dcs\flags.dm"
#include "code\__defines\dcs\helpers.dm"
#include "code\__defines\dcs\signals\signals_area.dm"
#include "code\__defines\dcs\signals\signals_atom.dm"
#include "code\__defines\dcs\signals\signals_atom_movable.dm"
#include "code\__defines\dcs\signals\signals_datum.dm"
Expand Down
10 changes: 10 additions & 0 deletions code/__defines/dcs/signals/signals_area.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Main area signals. Format:
// When the signal is called: (signal arguments)
// All signals send the source datum of the signal as the first argument

///from base of /area/proc/power_change(): (area/apc_area)
#define COMSIG_AREA_POWER_CHANGE "area_apc_power_change"
///from base of /area/proc/set_apc(): (area/apc_area)
#define COMSIG_AREA_APC_ADDED "area_apc_added"
///from base of /area/proc/remove_apc(): (area/apc_area)
#define COMSIG_AREA_APC_REMOVED "area_apc_removed"
45 changes: 22 additions & 23 deletions code/controllers/subsystems/atoms.dm
Original file line number Diff line number Diff line change
Expand Up @@ -44,47 +44,46 @@ SUBSYSTEM_DEF(atoms)
return
atom_init_stage = INITIALIZATION_INNEW_MAPLOAD
var/list/mapload_arg = list(TRUE)
var/atom/atom
var/list/params
var/count = 0
var/time = Uptime()
if (!initialized)
for (atom in world)
if (!atom || atom.atom_flags & ATOM_FLAG_INITIALIZED)
continue
InitAtom(atom, mapload_arg)
if (++count % 1000)
if(!initialized)
for(var/atom/atom_to_initialize as anything in world)
if(!atom_to_initialize || atom_to_initialize.atom_flags & ATOM_FLAG_INITIALIZED)
continue
InitAtom(atom_to_initialize, mapload_arg)
count++
CHECK_TICK

var/init_queue_length = length(init_queue)
if (init_queue_length)
for (var/i = 1 to init_queue_length)
atom = init_queue[i]
if (!atom || atom.atom_flags & ATOM_FLAG_INITIALIZED)
if(init_queue_length)
for(var/i = 1 to init_queue_length)
var/atom/atom_to_initialize = init_queue[i]
if (!atom_to_initialize || atom_to_initialize.atom_flags & ATOM_FLAG_INITIALIZED)
continue
params = init_queue[atom]

var/list/params = init_queue[atom_to_initialize]
if (params)
InitAtom(atom, mapload_arg + params)
InitAtom(atom_to_initialize, mapload_arg + params)
else
InitAtom(atom, mapload_arg)
if (++count % 500)
continue
InitAtom(atom_to_initialize, mapload_arg)
count++
CHECK_TICK
init_queue.Cut(1, init_queue_length + 1)

time = max((Uptime() - time) * 0.1, 0.1)
report_progress("Initialized [count] atom\s in [time]s ([floor(count/time)]/s)")
atom_init_stage = INITIALIZATION_INNEW_REGULAR
var/late_queue_length = length(late_init_queue)
if (late_queue_length)
count = 0
time = Uptime()
for (var/i = 1 to late_queue_length)
atom = late_init_queue[i]
if (!atom)
continue
atom.LateInitialize(arglist(late_init_queue[atom]))
if (++count % 250)
for(var/i = 1 to late_queue_length)
var/atom/atom_to_late_init = late_init_queue[i]
if (!atom_to_late_init)
continue

atom_to_late_init.LateInitialize(arglist(late_init_queue[atom_to_late_init]))
count++
CHECK_TICK
late_init_queue.Cut(1, late_queue_length + 1)
time = max((Uptime() - time) * 0.1, 0.1)
Expand Down
4 changes: 4 additions & 0 deletions code/controllers/subsystems/garbage.dm
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,13 @@ SUBSYSTEM_DEF(garbage)
++details.qdels
switch (datum.gc_destroyed)
if (null)
if(SEND_SIGNAL(datum, COMSIG_PREQDELETED)) // Gives any signal listener a chance to prevent atom qdel
return

datum.gc_destroyed = GC_CURRENTLY_BEING_QDELETED
var/start_time = world.time
var/start_tick = world.tick_usage
SEND_SIGNAL(datum, COMSIG_QDELETING) // Leting signal listeners know, that datum is being qdeleted
var/hint = datum.Destroy()
if (world.time != start_time)
++details.slept_destroy
Expand Down
163 changes: 93 additions & 70 deletions code/controllers/subsystems/overlays.dm
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,33 @@ var/global/const/ATOM_ICON_CACHE_ALL = (ATOM_ICON_CACHE_NORMAL | ATOM_ICON_CACHE
SUBSYSTEM_DEF(overlays)
name = "Overlays"
flags = SS_TICKER
wait = 1 // ticks
wait = 1
priority = FIRE_PRIORITY_OVERLAYS
init_order = SS_INIT_OVERLAYS

/// The queue of atoms that need under/overlay updates.
var/static/list/atom/queue = list()

VAR_PRIVATE/static/list/atom/queue = list()
/// A list([icon] = list([state] = [appearance], ...), ...) cache of appearances.
var/static/list/state_cache = list()

VAR_PRIVATE/static/list/state_cache = list()
/// A list([icon] = [appearance], ...) cache of appearances.
var/static/list/icon_cache = list()

VAR_PRIVATE/static/list/icon_cache = list()
/// The number of appearances currently cached.
var/static/cache_size = 0
VAR_PRIVATE/static/cache_size = 0


/datum/controller/subsystem/overlays/Recover()
LIST_RESIZE(queue, 0)
LIST_RESIZE(state_cache, 0)
LIST_RESIZE(icon_cache, 0)
cache_size = 0
var/count = 0
for (var/atom/atom)
atom.atom_flags &= ~ATOM_AWAITING_OVERLAY_UPDATE
if (++count % 500)
continue
for(var/atom/atom as anything in world)
if(atom.atom_flags & ATOM_AWAITING_OVERLAY_UPDATE)
SSoverlays.queue += atom

CHECK_TICK


/datum/controller/subsystem/overlays/Initialize(start_uptime)
fire(FALSE, TRUE)
flush_queue()


/datum/controller/subsystem/overlays/UpdateStat(time)
Expand All @@ -51,73 +46,100 @@ SUBSYSTEM_DEF(overlays)
..({"Queued Atoms: [length(queue)], Cache Size: [cache_size]"})


/datum/controller/subsystem/overlays/fire(resumed, no_mc_tick)
var/queue_length = length(queue)
if (queue_length)
var/atom/atom
for (var/i = 1 to queue_length)
atom = queue[i]
if (QDELETED(atom))
continue
if (atom.atom_flags & ATOM_AWAITING_OVERLAY_UPDATE)
atom.UpdateOverlays()
if (no_mc_tick)
if (i % 1000)
continue
CHECK_TICK
else if (MC_TICK_CHECK)
queue.Cut(1, i + 1)
return
queue.Cut(1, queue_length + 1)
/datum/controller/subsystem/overlays/fire(resumed)
var/queue_position = 1
while(length(queue) >= queue_position)
var/atom/atom_to_update = queue[queue_position]
if(!QDELETED(atom_to_update) && atom_to_update.atom_flags & ATOM_AWAITING_OVERLAY_UPDATE)
atom_to_update.UpdateOverlays()

queue_position++
if(MC_TICK_CHECK)
break

queue.Cut(1, queue_position)

/datum/controller/subsystem/overlays/proc/flush_queue()
var/queue_position = 1
while(length(queue) >= queue_position)
process_atom_overlays_update(queue[queue_position])
queue_position++
CHECK_TICK

LIST_RESIZE(queue, 0)

/datum/controller/subsystem/overlays/proc/process_atom_overlays_update(atom/atom_to_update)
if(!QDELETED(atom_to_update) && atom_to_update.atom_flags & ATOM_AWAITING_OVERLAY_UPDATE)
atom_to_update.UpdateOverlays()


/datum/controller/subsystem/overlays/proc/GetStateAppearance(icon, state)
var/list/subcache = state_cache[icon]
if (!subcache)
subcache = list()
state_cache[icon] = subcache
if (!subcache[state])
var/image/image = image(icon, null, state)
subcache[state] = image.appearance
++cache_size
return subcache[state]
var/list/state_to_appearance = state_cache[icon]
if(!state_to_appearance)
state_to_appearance = list()
state_cache[icon] = state_to_appearance

var/state_appearance = state_to_appearance[state]
if(!state_appearance)
var/image/state_image = image(icon, null, state)
state_appearance = state_image.appearance
state_to_appearance[state] = state_appearance
cache_size++

return state_appearance


/datum/controller/subsystem/overlays/proc/GetIconAppearance(icon)
if (!icon_cache[icon])
var/image/image = image(icon)
icon_cache[icon] = image.appearance
++cache_size
return icon_cache[icon]
var/icon_appearance = icon_cache[icon]
if (!icon_appearance)
var/image/icon_image = image(icon)
icon_appearance = icon_image.appearance
icon_cache[icon] = icon_appearance
cache_size++

return icon_appearance


/datum/controller/subsystem/overlays/proc/GetAppearanceList(atom/subject, list/sources)
/datum/controller/subsystem/overlays/proc/getAppearanceList(atom/subject, list/sources)
if (!sources)
return list()

if (!islist(sources))
sources = list(sources)

var/list/result = list()
var/icon/icon = subject.icon
var/atom/entry
for (var/i = 1 to length(sources))
entry = sources[i]
if (!entry)
for (var/atom/source as anything in sources)
if(!source)
continue
else if (istext(entry))
result += GetStateAppearance(icon, entry)
else if (isicon(entry))
result += GetIconAppearance(entry)

if(istext(source))
result += GetStateAppearance(subject.icon, source)

else if(isicon(source))
result += GetIconAppearance(source)

else
if (isloc(entry))
if (entry.atom_flags & ATOM_AWAITING_OVERLAY_UPDATE)
entry.UpdateOverlays()
if (!ispath(entry))
result += entry.appearance
if(isatom(source) && source.atom_flags & ATOM_AWAITING_OVERLAY_UPDATE)
source.UpdateOverlays()

if(!ispath(source))
result += source.appearance
else
var/image/image = entry
var/image/image = source
result += image.appearance

return result

/datum/controller/subsystem/overlays/proc/enque_atom_overlay_update(atom/atom_to_update)
if(!atom_to_update)
return

if(atom_to_update.atom_flags & ATOM_AWAITING_OVERLAY_UPDATE)
return

atom_to_update.atom_flags |= ATOM_AWAITING_OVERLAY_UPDATE
SSoverlays.queue += atom_to_update


/// Immediately runs an overlay update.
/atom/proc/ImmediateOverlayUpdate()
Expand Down Expand Up @@ -147,11 +169,7 @@ SUBSYSTEM_DEF(overlays)
/// Enqueues the atom for an overlay update if not already queued
/atom/proc/QueueOverlayUpdate()
SHOULD_NOT_OVERRIDE(TRUE)
if (atom_flags & ATOM_AWAITING_OVERLAY_UPDATE)
return
atom_flags |= ATOM_AWAITING_OVERLAY_UPDATE
SSoverlays.queue += src

SSoverlays.enque_atom_overlay_update(src)

/// Builds the atom's overlay state from caches
/atom/proc/UpdateOverlays()
Expand All @@ -160,6 +178,7 @@ SUBSYSTEM_DEF(overlays)
if (QDELING(src))
LIST_RESIZE(overlays, 0)
return

if (length(atom_protected_overlay_cache))
if (length(atom_overlay_cache))
overlays = atom_protected_overlay_cache + atom_overlay_cache
Expand Down Expand Up @@ -203,7 +222,7 @@ SUBSYSTEM_DEF(overlays)
SHOULD_NOT_OVERRIDE(TRUE)
if (!sources)
return
sources = SSoverlays.GetAppearanceList(src, sources)
sources = SSoverlays.getAppearanceList(src, sources)
if (!length(sources))
return
if (cache_target & ATOM_ICON_CACHE_PROTECTED)
Expand All @@ -228,22 +247,26 @@ SUBSYSTEM_DEF(overlays)
SHOULD_NOT_OVERRIDE(TRUE)
if (!sources)
return
sources = SSoverlays.GetAppearanceList(src, sources)

sources = SSoverlays.getAppearanceList(src, sources)
if (!length(sources))
return

var/update
if (cache_target & ATOM_ICON_CACHE_PROTECTED)
var/outcome = CutCacheBehavior(sources, atom_protected_overlay_cache)
if (!isnull(outcome))
update = TRUE
if (outcome == TRUE)
atom_protected_overlay_cache = null

if (cache_target & ATOM_ICON_CACHE_NORMAL)
var/outcome = CutCacheBehavior(sources, atom_overlay_cache)
if (!isnull(outcome))
update = TRUE
if (outcome == TRUE)
atom_overlay_cache = null

if (update)
QueueOverlayUpdate()

Expand Down
Loading

0 comments on commit 5761ecd

Please sign in to comment.