Files
Bubberstation/code/modules/lighting/lighting_area.dm
T
ab5a4d0f99 [MIRROR] split area.contained_turfs up by zlevel, make init 10 seconds faster (#26161)
* split area.contained_turfs up by zlevel, make init 10 seconds faster (#80941)

## About The Pull Request

Situation: areas have a list of all turfs in their area.

Problem: `/area/space` is an area and has a 6 to 7 digit count of turfs
that has to be traversed for every turf we need to remove from it. This
can take multiple byond ticks just to preform this action for a single
space rune

Solution: split the list by zlevel, and only search the right zlevel
list when removing turfs from areas.

replaces `area.get_contained_turfs()` with a few new procs:

* `get_highest_zlevel()` - returns the highest zlevel the area contains
turfs in. useful for use with `get_turfs_by_zlevel`
* `get_turfs_by_zlevel(zlevel)` - returns a list of turfs in the area in
a given zlevel. Useful for code that only cares about a specific zlevel
or changes behavior based on zlevel like lighting init.
* `get_turfs_from_all_zlevels()` - the replacement for
`get_contained_turfs()`, renamed as such so anybody copying/cargo
culting code gets a hint that a zlevel specific version might exist.
Still used in for loops that type checked so byond would do that all at
once
* `get_zlevel_turf_lists()` - returns the area's zlevel lists of lists
but only for non-empty zlevels. very useful for for loops.

The area contents unit test has been rewritten to ensure any improper
data triggers failures or runtimes by not having it use the helpers
above (some of which ensure a list is always returned) and access the
lists directly.

* split area.contained_turfs up by zlevel, make init 10 seconds faster

* eeyes

* Update area_spawn_subsystem.dm

* Unshits turf contain code slightly (#81023)

Literally just implements my reviews from #80941 
I am frankly a smidge pissed that the pr was merged without them being
handled. No code is worth merging past known issues, and if the author
is just gonna dip then that's life.
I don't like privileging mso on stuff like this, especially because
frankly I'm kinda mad at him rn but also because when a pr is made the
onus on finishing it falls to the person who made it.

Should not need to clean up after someone as a maintainer, and shouldn't
normalize doing it. I'm not like mad at zypher directly mind he offered
to do this too, just the idea he was espousing here.

---------

Co-authored-by: Kyle Spier-Swenson <kyleshome@gmail.com>
Co-authored-by: Gandalf <9026500+Gandalf2k15@users.noreply.github.com>
Co-authored-by: LemonInTheDark <58055496+LemonInTheDark@users.noreply.github.com>
2024-01-21 03:34:23 +00:00

128 lines
5.1 KiB
Plaintext

/area
luminosity = 1
///List of mutable appearances we underlay to show light
///In the form plane offset + 1 -> appearance to use
var/list/mutable_appearance/lighting_effects = null
///Whether this area has a currently active base lighting, bool
var/area_has_base_lighting = FALSE
///alpha 0-255 of lighting_effect and thus baselighting intensity
var/base_lighting_alpha = 0
///The colour of the light acting on this area
var/base_lighting_color = COLOR_WHITE
/area/proc/set_base_lighting(new_base_lighting_color = -1, new_alpha = -1)
if(base_lighting_alpha == new_alpha && base_lighting_color == new_base_lighting_color)
return FALSE
if(new_alpha != -1)
base_lighting_alpha = new_alpha
if(new_base_lighting_color != -1)
base_lighting_color = new_base_lighting_color
update_base_lighting()
return TRUE
/area/vv_edit_var(var_name, var_value)
switch(var_name)
if(NAMEOF(src, base_lighting_color))
set_base_lighting(new_base_lighting_color = var_value)
return TRUE
if(NAMEOF(src, base_lighting_alpha))
set_base_lighting(new_alpha = var_value)
return TRUE
if(NAMEOF(src, static_lighting))
if(!static_lighting)
create_area_lighting_objects()
else
remove_area_lighting_objects()
return ..()
/area/proc/update_base_lighting()
if(!area_has_base_lighting && (!base_lighting_alpha || !base_lighting_color))
return
if(!area_has_base_lighting)
add_base_lighting()
return
remove_base_lighting()
if(base_lighting_alpha && base_lighting_color)
add_base_lighting()
/area/proc/remove_base_lighting()
UnregisterSignal(SSdcs, COMSIG_STARLIGHT_COLOR_CHANGED)
var/list/z_offsets = SSmapping.z_level_to_plane_offset
if(length(lighting_effects) > 1)
for(var/area_zlevel as anything in 1 to get_highest_zlevel())
if(z_offsets[area_zlevel])
for(var/turf/T as anything in get_turfs_by_zlevel(area_zlevel))
T.cut_overlay(lighting_effects[z_offsets[T.z] + 1])
cut_overlay(lighting_effects[1])
lighting_effects = null
area_has_base_lighting = FALSE
/area/proc/add_base_lighting()
lighting_effects = list()
for(var/offset in 0 to SSmapping.max_plane_offset)
var/mutable_appearance/light
if(base_lighting_color == COLOR_STARLIGHT)
light = new(GLOB.starlight_overlays[offset + 1])
else
light = mutable_appearance('icons/effects/alphacolors.dmi', "white")
light.color = base_lighting_color
light.layer = LIGHTING_PRIMARY_LAYER
light.blend_mode = BLEND_ADD
light.appearance_flags = RESET_TRANSFORM | RESET_ALPHA | RESET_COLOR
light.alpha = base_lighting_alpha
SET_PLANE_W_SCALAR(light, LIGHTING_PLANE, offset)
lighting_effects += light
if(base_lighting_color == COLOR_STARLIGHT)
// Ok this is gonna be dumb
// We rely on render_source working, and it DOES NOT APPEAR TO in area rendering
// So we're gonna have to update the area's overlay manually. everything else can be automatic tho
// Fortunately the first overlay is only ever used by the area, soooo
var/mutable_appearance/light = mutable_appearance('icons/effects/alphacolors.dmi', "white")
light.layer = LIGHTING_PRIMARY_LAYER
light.blend_mode = BLEND_ADD
light.appearance_flags = RESET_TRANSFORM | RESET_ALPHA | RESET_COLOR
light.color = GLOB.starlight_color
light.alpha = base_lighting_alpha
SET_PLANE_W_SCALAR(light, LIGHTING_PLANE, 0)
lighting_effects[1] = light
RegisterSignal(SSdcs, COMSIG_STARLIGHT_COLOR_CHANGED, PROC_REF(starlight_changed))
add_overlay(lighting_effects[1])
var/list/z_offsets = SSmapping.z_level_to_plane_offset
for (var/area_zlevel in 1 to get_highest_zlevel())
// We will only add overlays to turfs not on the first z layer, because that's a significantly lesser portion
// And we need to do them separate, or lighting will go fuckey
// This inside loop is EXTREMELY hot because it's run by space tiles, so we do the if check once on the outside
if(length(lighting_effects) > 1 && z_offsets[area_zlevel])
var/lighting_effect_to_add = lighting_effects[z_offsets[area_zlevel] + 1]
for(var/turf/area_turf as anything in get_turfs_by_zlevel(area_zlevel))
area_turf.luminosity = 1
area_turf.add_overlay(lighting_effect_to_add)
else
for(var/turf/area_turf as anything in get_turfs_by_zlevel(area_zlevel))
area_turf.luminosity = 1
area_has_base_lighting = TRUE
/area/proc/starlight_changed(datum/source, old_star, new_star)
var/mutable_appearance/old_star_effect = mutable_appearance('icons/effects/alphacolors.dmi', "white")
old_star_effect.layer = LIGHTING_PRIMARY_LAYER
old_star_effect.blend_mode = BLEND_ADD
old_star_effect.appearance_flags = RESET_TRANSFORM | RESET_ALPHA | RESET_COLOR
old_star_effect.color = old_star
old_star_effect.alpha = base_lighting_alpha
SET_PLANE_W_SCALAR(old_star_effect, LIGHTING_PLANE, 0)
cut_overlay(old_star_effect)
var/mutable_appearance/new_star_effect = mutable_appearance('icons/effects/alphacolors.dmi', "white")
new_star_effect.layer = LIGHTING_PRIMARY_LAYER
new_star_effect.blend_mode = BLEND_ADD
new_star_effect.appearance_flags = RESET_TRANSFORM | RESET_ALPHA | RESET_COLOR
new_star_effect.color = new_star
new_star_effect.alpha = base_lighting_alpha
SET_PLANE_W_SCALAR(new_star_effect, LIGHTING_PLANE, 0)
add_overlay(new_star_effect)
lighting_effects[1] = new_star_effect