From ebeee921cab019453a3c94b4ff1e5cecab43c3c5 Mon Sep 17 00:00:00 2001 From: Fox McCloud Date: Sun, 21 Jun 2020 07:08:59 -0400 Subject: [PATCH] Lighting Optimizations (#13593) * Lighting Optimizations * styling * styling * welp --- code/controllers/subsystem/lighting.dm | 30 +++++++++++++----------- code/game/atoms_movable.dm | 9 +------ code/game/turfs/turf.dm | 5 ++++ code/modules/lighting/lighting_corner.dm | 24 +++++++++---------- code/modules/lighting/lighting_object.dm | 13 ++++++---- code/modules/lighting/lighting_source.dm | 10 ++++---- code/modules/lighting/lighting_turf.dm | 4 ++-- code/modules/mob/camera/camera.dm | 5 ++++ code/modules/mob/dead/dead.dm | 2 +- code/modules/shuttle/on_move.dm | 2 ++ 10 files changed, 57 insertions(+), 47 deletions(-) diff --git a/code/controllers/subsystem/lighting.dm b/code/controllers/subsystem/lighting.dm index 3fc92a3346a..6761e9647d9 100644 --- a/code/controllers/subsystem/lighting.dm +++ b/code/controllers/subsystem/lighting.dm @@ -1,16 +1,15 @@ -GLOBAL_LIST_EMPTY(lighting_update_lights) // List of lighting sources queued for update. -GLOBAL_LIST_EMPTY(lighting_update_corners) // List of lighting corners queued for update. -GLOBAL_LIST_EMPTY(lighting_update_objects) // List of lighting objects queued for update. - SUBSYSTEM_DEF(lighting) name = "Lighting" wait = 2 init_order = INIT_ORDER_LIGHTING flags = SS_TICKER offline_implications = "Lighting will no longer update. Shuttle call recommended." + var/static/list/sources_queue = list() // List of lighting sources queued for update. + var/static/list/corners_queue = list() // List of lighting corners queued for update. + var/static/list/objects_queue = list() // List of lighting objects queued for update. /datum/controller/subsystem/lighting/stat_entry() - ..("L:[GLOB.lighting_update_lights.len]|C:[GLOB.lighting_update_corners.len]|O:[GLOB.lighting_update_objects.len]") + ..("L:[length(sources_queue)]|C:[length(corners_queue)]|O:[length(objects_queue)]") /datum/controller/subsystem/lighting/Initialize(timeofday) if(!initialized) @@ -31,9 +30,10 @@ SUBSYSTEM_DEF(lighting) MC_SPLIT_TICK_INIT(3) if(!init_tick_checks) MC_SPLIT_TICK + var/list/queue = sources_queue var/i = 0 - for(i in 1 to GLOB.lighting_update_lights.len) - var/datum/light_source/L = GLOB.lighting_update_lights[i] + for(i in 1 to length(queue)) + var/datum/light_source/L = queue[i] L.update_corners() @@ -44,14 +44,15 @@ SUBSYSTEM_DEF(lighting) else if(MC_TICK_CHECK) break if(i) - GLOB.lighting_update_lights.Cut(1, i+1) + queue.Cut(1, i + 1) i = 0 if(!init_tick_checks) MC_SPLIT_TICK - for (i in 1 to GLOB.lighting_update_corners.len) - var/datum/lighting_corner/C = GLOB.lighting_update_corners[i] + queue = corners_queue + for(i in 1 to length(queue)) + var/datum/lighting_corner/C = queue[i] C.update_objects() C.needs_update = FALSE @@ -60,15 +61,16 @@ SUBSYSTEM_DEF(lighting) else if(MC_TICK_CHECK) break if(i) - GLOB.lighting_update_corners.Cut(1, i+1) + queue.Cut(1, i + 1) i = 0 if(!init_tick_checks) MC_SPLIT_TICK - for (i in 1 to GLOB.lighting_update_objects.len) - var/atom/movable/lighting_object/O = GLOB.lighting_update_objects[i] + queue = objects_queue + for(i in 1 to length(queue)) + var/atom/movable/lighting_object/O = queue[i] if(QDELETED(O)) continue @@ -80,7 +82,7 @@ SUBSYSTEM_DEF(lighting) else if(MC_TICK_CHECK) break if(i) - GLOB.lighting_update_objects.Cut(1, i+1) + queue.Cut(1, i + 1) /datum/controller/subsystem/lighting/Recover() diff --git a/code/game/atoms_movable.dm b/code/game/atoms_movable.dm index 70926d7ba2b..c92dca64b86 100644 --- a/code/game/atoms_movable.dm +++ b/code/game/atoms_movable.dm @@ -265,15 +265,8 @@ var/dest_z = (destturf ? destturf.z : null) if(old_z != dest_z) onTransitZ(old_z, dest_z) - if(isturf(destination) && opacity) - var/turf/new_loc = destination - new_loc.reconsider_lights() - if(isturf(old_loc) && opacity) - old_loc.reconsider_lights() - - for(var/datum/light_source/L in light_sources) - L.source_atom.update_light() + Moved(old_loc, NONE) return 1 diff --git a/code/game/turfs/turf.dm b/code/game/turfs/turf.dm index aa3f7b898a1..defd6720e7d 100644 --- a/code/game/turfs/turf.dm +++ b/code/game/turfs/turf.dm @@ -163,6 +163,11 @@ loopsanity-- A.HasProximity(M, 1) + // If an opaque movable atom moves around we need to potentially update visibility. + if(M.opacity) + has_opaque_atom = TRUE // Make sure to do this before reconsider_lights(), incase we're on instant updates. Guaranteed to be on in this case. + reconsider_lights() + /turf/proc/levelupdate() for(var/obj/O in src) if(O.level == 1) diff --git a/code/modules/lighting/lighting_corner.dm b/code/modules/lighting/lighting_corner.dm index bc521114d56..57302b5b168 100644 --- a/code/modules/lighting/lighting_corner.dm +++ b/code/modules/lighting/lighting_corner.dm @@ -25,7 +25,7 @@ GLOBAL_LIST_INIT(LIGHTING_CORNER_DIAGONAL, list(NORTHEAST, SOUTHEAST, SOUTHWEST, var/cache_b = LIGHTING_SOFT_THRESHOLD var/cache_mx = 0 -/datum/lighting_corner/New(var/turf/new_turf, var/diagonal) +/datum/lighting_corner/New(turf/new_turf, diagonal) . = ..() masters = list() masters[new_turf] = turn(diagonal, 180) @@ -79,15 +79,16 @@ GLOBAL_LIST_INIT(LIGHTING_CORNER_DIAGONAL, list(NORTHEAST, SOUTHEAST, SOUTHWEST, active = FALSE var/turf/T var/thing - for (thing in masters) + for(thing in masters) T = thing if(T.lighting_object) active = TRUE + return // God that was a mess, now to do the rest of the corner code! Hooray! -/datum/lighting_corner/proc/update_lumcount(var/delta_r, var/delta_g, var/delta_b) +/datum/lighting_corner/proc/update_lumcount(delta_r, delta_g, delta_b) - if((abs(delta_r)+abs(delta_g)+abs(delta_b)) == 0) + if(!(delta_r || delta_g || delta_b)) // 0 is falsey ok return lum_r += delta_r @@ -96,10 +97,10 @@ GLOBAL_LIST_INIT(LIGHTING_CORNER_DIAGONAL, list(NORTHEAST, SOUTHEAST, SOUTHWEST, if(!needs_update) needs_update = TRUE - GLOB.lighting_update_corners += src + SSlighting.corners_queue += src /datum/lighting_corner/proc/update_objects() - // Cache these values a head of time so 4 individual lighting objects don't all calculate them individually. + // Cache these values ahead of time so 4 individual lighting objects don't all calculate them individually. var/lum_r = src.lum_r var/lum_g = src.lum_g var/lum_b = src.lum_b @@ -122,19 +123,18 @@ GLOBAL_LIST_INIT(LIGHTING_CORNER_DIAGONAL, list(NORTHEAST, SOUTHEAST, SOUTHWEST, #endif cache_mx = round(mx, LIGHTING_ROUND_VALUE) - for (var/TT in masters) + for(var/TT in masters) var/turf/T = TT - if(T.lighting_object) - if(!T.lighting_object.needs_update) - T.lighting_object.needs_update = TRUE - GLOB.lighting_update_objects += T.lighting_object + if(T.lighting_object && !T.lighting_object.needs_update) + T.lighting_object.needs_update = TRUE + SSlighting.objects_queue += T.lighting_object /datum/lighting_corner/dummy/New() return -/datum/lighting_corner/Destroy(var/force) +/datum/lighting_corner/Destroy(force) if(!force) return QDEL_HINT_LETMELIVE diff --git a/code/modules/lighting/lighting_object.dm b/code/modules/lighting/lighting_object.dm index ea3e954fa6e..168a95c07f2 100644 --- a/code/modules/lighting/lighting_object.dm +++ b/code/modules/lighting/lighting_object.dm @@ -5,7 +5,7 @@ icon = LIGHTING_ICON icon_state = "transparent" - color = LIGHTING_BASE_MATRIX + color = null //we manually set color in init instead plane = LIGHTING_PLANE mouse_opacity = MOUSE_OPACITY_TRANSPARENT layer = LIGHTING_LAYER @@ -18,6 +18,9 @@ /atom/movable/lighting_object/Initialize(mapload) . = ..() verbs.Cut() + //We avoid setting this in the base as if we do then the parent atom handling will add_atom_color it and that + //is totally unsuitable for this object, as we are always changing it's colour manually + color = LIGHTING_BASE_MATRIX myturf = loc if(myturf.lighting_object) @@ -29,11 +32,11 @@ S.update_starlight() needs_update = TRUE - GLOB.lighting_update_objects += src + SSlighting.objects_queue += src -/atom/movable/lighting_object/Destroy(var/force) +/atom/movable/lighting_object/Destroy(force) if(force) - GLOB.lighting_update_objects -= src + SSlighting.objects_queue -= src if(loc != myturf) var/turf/oldturf = get_turf(myturf) var/turf/newturf = get_turf(loc) @@ -143,6 +146,6 @@ return // Override here to prevent things accidentally moving around overlays. -/atom/movable/lighting_object/forceMove(atom/destination, var/no_tp=FALSE, var/harderforce = FALSE) +/atom/movable/lighting_object/forceMove(atom/destination, no_tp = FALSE, harderforce = FALSE) if(harderforce) . = ..() diff --git a/code/modules/lighting/lighting_source.dm b/code/modules/lighting/lighting_source.dm index fc87a855dc4..7b0b782bdca 100644 --- a/code/modules/lighting/lighting_source.dm +++ b/code/modules/lighting/lighting_source.dm @@ -29,7 +29,7 @@ var/needs_update = LIGHTING_NO_UPDATE // Whether we are queued for an update. -/datum/light_source/New(var/atom/owner, var/atom/top) +/datum/light_source/New(atom/owner, atom/top) source_atom = owner // Set our new owner. LAZYADD(source_atom.light_sources, src) top_atom = top @@ -56,7 +56,7 @@ LAZYREMOVE(top_atom.light_sources, src) if(needs_update) - GLOB.lighting_update_lights -= src + SSlighting.sources_queue -= src . = ..() @@ -65,13 +65,13 @@ // Actually that'd be great if you could! #define EFFECT_UPDATE(level) \ if(needs_update == LIGHTING_NO_UPDATE) \ - GLOB.lighting_update_lights += src; \ + SSlighting.sources_queue += src; \ if(needs_update < level) \ needs_update = level; \ // This proc will cause the light source to update the top atom, and add itself to the update queue. -/datum/light_source/proc/update(var/atom/new_top_atom) +/datum/light_source/proc/update(atom/new_top_atom) // This top atom is different. if(new_top_atom && new_top_atom != top_atom) if(top_atom != source_atom && top_atom.light_sources) // Remove ourselves from the light sources of that top atom. @@ -141,7 +141,7 @@ effect_str = null -/datum/light_source/proc/recalc_corner(var/datum/lighting_corner/C) +/datum/light_source/proc/recalc_corner(datum/lighting_corner/C) LAZYINITLIST(effect_str) if(effect_str[C]) // Already have one. REMOVE_CORNER(C) diff --git a/code/modules/lighting/lighting_turf.dm b/code/modules/lighting/lighting_turf.dm index f368ff5ac4b..5483eebf4e1 100644 --- a/code/modules/lighting/lighting_turf.dm +++ b/code/modules/lighting/lighting_turf.dm @@ -57,7 +57,7 @@ C.active = TRUE // Used to get a scaled lumcount. -/turf/proc/get_lumcount(var/minlum = 0, var/maxlum = 1) +/turf/proc/get_lumcount(minlum = 0, maxlum = 1) if(!lighting_object) return 1 @@ -102,7 +102,7 @@ recalc_atom_opacity() // Make sure to do this before reconsider_lights(), incase we're on instant updates. reconsider_lights() -/turf/proc/change_area(var/area/old_area, var/area/new_area) +/turf/proc/change_area(area/old_area, area/new_area) if(SSlighting.initialized) if(new_area.dynamic_lighting != old_area.dynamic_lighting) if(new_area.dynamic_lighting) diff --git a/code/modules/mob/camera/camera.dm b/code/modules/mob/camera/camera.dm index 39931a60fb2..090e5ddac1e 100644 --- a/code/modules/mob/camera/camera.dm +++ b/code/modules/mob/camera/camera.dm @@ -14,3 +14,8 @@ /mob/camera/experience_pressure_difference() return + +/mob/camera/forceMove(atom/destination) + var/oldloc = loc + loc = destination + Moved(oldloc, NONE) diff --git a/code/modules/mob/dead/dead.dm b/code/modules/mob/dead/dead.dm index 24f129ee387..f60777a97ff 100644 --- a/code/modules/mob/dead/dead.dm +++ b/code/modules/mob/dead/dead.dm @@ -15,7 +15,7 @@ onTransitZ(old_turf?.z, new_turf?.z) var/oldloc = loc loc = destination - Moved(oldloc, NONE, TRUE) + Moved(oldloc, NONE) /mob/dead/onTransitZ(old_z,new_z) ..() diff --git a/code/modules/shuttle/on_move.dm b/code/modules/shuttle/on_move.dm index f0394349f4d..1d9f6970135 100644 --- a/code/modules/shuttle/on_move.dm +++ b/code/modules/shuttle/on_move.dm @@ -3,6 +3,8 @@ var/turf/newT = get_turf(src) if(newT.z != oldT.z) onTransitZ(oldT.z, newT.z) + if(light) + update_light() if(rotation) shuttleRotate(rotation) forceMove(T1)