Files
Bubberstation/code/datums/elements/turf_transparency.dm
LemonInTheDark 23bfdec8f4 Multiz Rework: Human Suffering Edition (Contains PLANE CUBE) (#69115)
About The Pull Request

I've reworked multiz. This was done because our current implementation of multiz flattens planes down into just the openspace plane. This breaks any effects we attach to plane masters (including lighting), but it also totally kills the SIDE_MAP map format, which we NEED for wallening (A major 3/4ths resprite of all wall and wall adjacent things, making them more then one tile high. Without sidemap we would be unable to display things both in from of and behind objects on map. Stupid.)

This required MASSIVE changes. Both to all uses of the plane var for reasons I'll discuss later, and to a ton of different systems that interact with rendering.

I'll do my best to keep this compact, but there's only so much I can do. Sorry brother.
Core idea

OK: first thing.
vis_contents as it works now squishes the planes of everything inside it down into the plane of the vis_loc.
This is bad. But how to do better?

It's trivially easy to make copies of our existing plane masters but offset, and relay them to the bottom of the plane above. Not a problem. The issue is how to get the actual atoms on the map to "land" on them properly.

We could use FLOAT_PLANE to offset planes based off how they're being seen, in theory this would allow us to create lens for how objects are viewed.
But that's not a stable thing to do, because properly "landing" a plane on a desired plane master would require taking into account every bit of how it's being seen, would inherently break this effect.

Ok so we need to manually edit planes based off "z layer" (IE: what layer of a z stack are you on).

That's the key conceit of this pr. Implementing the plane cube, and ensuring planes are always offset properly.
Everything else is just gravy.
About the Plane Cube

Each plane master (except ones that opt out) is copied down by some constant value equal to the max absolute change between the first and the last plane.
We do this based off the max z stack size detected by SSmapping. This is also where updates come from, and where all our updating logic will live.

As mentioned, plane masters can choose to opt out of being mirrored down. In this case, anything that interacts with them assuming that they'll be offset will instead just get back the valid plane value. This works for render targets too, since I had to work them into the system as well.

Plane masters can also be temporarily hidden from the client's screen. This is done as an attempt at optimization, and applies to anything used in niche cases, or planes only used if there's a z layer below you.
About Plane Master Groups

BYOND supports having different "maps" on screen at once (IE: groups of items/turfs/etc)
Plane masters cannot cover 2 maps at once, since their location is determined by their screen_loc.
So we need to maintain a mirror of each plane for every map we have open.

This was quite messy, so I've refactored it (and maps too) to be a bit more modular.

Rather then storing a list of plane masters, we store a list of plane master group datums.
Each datum is in charge of the plane masters for its particular map, both creating them, and managing them.

Like I mentioned, I also refactored map views. Adding a new mapview is now as simple as newing a /atom/movable/screen/map_view, calling generate_view with the appropriate map id, setting things you want to display in its vis_contents, and then calling display_to on it, passing in the mob to show ourselves to.

Much better then the hardcoded pattern we used to use. So much duplicated code man.

Oh and plane master controllers, that system we have that allows for applying filters to sets of plane masters? I've made it use lookups on plane master groups now, rather then hanging references to all impacted planes. This makes logic easier, and prevents the need to manage references and update the controllers.

image

In addition, I've added a debug ui for plane masters.
It allows you to view all of your own plane masters and short descriptions of what they do, alongside tools for editing them and their relays.

It ALSO supports editing someone elses plane masters, AND it supports (in a very fragile and incomplete manner) viewing literally through someone else's eyes, including their plane masters. This is very useful, because it means you can debug "hey my X is yorked" issues yourself, on live.

In order to accomplish this I have needed to add setters for an ungodly amount of visual impacting vars. Sight flags, eye, see_invis, see_in_dark, etc.

It also comes with an info dump about the ui, and plane masters/relays in general.

Sort of on that note. I've documented everything I know that's niche/useful about our visual effects and rendering system. My hope is this will serve to bring people up to speed on what can be done more quickly, alongside making my sin here less horrible.
See https://github.com/LemonInTheDark/tgstation/blob/multiz-hell/.github/guides/VISUALS.md.
"Landing" planes

Ok so I've explained the backend, but how do we actually land planes properly?
Most of the time this is really simple. When a plane var is set, we need to provide some spokesperson for the appearance's z level. We can use this to derive their z layer, and thus what offset to use.

This is just a lot of gruntwork, but it's occasionally more complex.
Sometimes we need to cache a list of z layer -> effect, and then use that.
Also a LOT of updating on z move. So much z move shit.

Oh. and in order to make byond darkness work properly, I needed to add SEE_BLACKNESS to all sight flags.
This draws darkness to plane 0, which means I'm able to relay it around and draw it on different z layers as is possible. fun darkness ripple effects incoming someday

I also need to update mob overlays on move.
I do this by realiizing their appearances, mutating their plane, and then readding the overlay in the correct order.

The cost of this is currently 3N. I'm convinced this could be improved, but I've not got to it yet.
It can also occasionally cause overlays to corrupt. This is fixed by laying a protective ward of overlays.Copy in the sand, but that spell makes the compiler confused, so I'll have to bully lummy about fixing it at some point.
Behavior changes

We've had to give up on the already broken gateway "see through" effect. Won't work without managing gateway plane masters or something stupid. Not worth it.
So instead we display the other side as a ui element. It's worse, but not that bad.

Because vis_contents no longer flattens planes (most of the time), some uses of it now have interesting behavior.
The main thing that comes to mind is alert popups that display mobs. They can impact the lighting plane.
I don't really care, but it should be fixable, I think, given elbow grease.

Ah and I've cleaned up layers and plane defines to make them a bit easier to read/reason about, at least I think.
Why It's Good For The Game
<visual candy>

Fixes #65800
Fixes #68461
Changelog

cl
refactor: Refactored... well a lot really. Map views, anything to do with planes, multiz, a shit ton of rendering stuff. Basically if you see anything off visually report it
admin: VV a mob, and hit View/Edit Planes in the dropdown to steal their view, and modify it as you like. You can do the same to yourself using the Edit/Debug Planes verb
/cl
2022-09-27 20:11:04 +13:00

277 lines
10 KiB
Plaintext

/// List of z pillars (datums placed in the bottom left of XbyX squares that control transparency in that space)
/// The pillars are stored in triple depth lists indexed by (world_size % pillar_size) + 1
/// They are created at transparent turf request, and deleted when no turfs remain
GLOBAL_LIST_EMPTY(pillars_by_z)
#define Z_PILLAR_RADIUS 20
// Takes a position, transforms it into a z pillar key
#define Z_PILLAR_TRANSFORM(pos) (ROUND_UP(pos / Z_PILLAR_RADIUS))
// Takes a z pillar key, hands back the actual posiiton it represents
// A key of 1 becomes 1, a key of 2 becomes Z_PILLAR_RADIUS + 1, etc.
#define Z_KEY_TO_POSITION(key) (((key - 1) * Z_PILLAR_RADIUS) + 1)
/// Returns a z pillar to insert turfs into
/proc/request_z_pillar(x, y, z)
var/list/pillars_by_z = GLOB.pillars_by_z
if(length(pillars_by_z) < z)
pillars_by_z.len = z
var/list/our_z = pillars_by_z[z]
if(!our_z)
our_z = list()
pillars_by_z[z] = our_z
//Now that we've got the z layer sorted, we're gonna check the X line
var/x_key = Z_PILLAR_TRANSFORM(x)
if(length(our_z) < x_key)
our_z.len = x_key
var/list/our_x = our_z[x_key]
if(!our_x)
our_x = list()
our_z[x_key] = our_x
//And now the y layer
var/y_key = Z_PILLAR_TRANSFORM(y)
if(length(our_x) < y_key)
our_x.len = y_key
var/datum/z_pillar/our_lad = our_x[y_key]
if(!our_lad)
our_lad = new(x_key, y_key, z)
our_x[y_key] = our_lad
return our_lad
/// Exists to be placed on the turf of walls and such to hold the vis_contents of the tile below
/// Otherwise the lower turf might get shifted around, which is dumb. do this instead.
/obj/effect/abstract/z_holder
var/datum/z_pillar/pillar
var/turf/show_for
appearance_flags = PIXEL_SCALE
plane = HUD_PLANE
anchored = TRUE
move_resist = INFINITY
mouse_opacity = MOUSE_OPACITY_TRANSPARENT
/obj/effect/abstract/z_holder/Destroy()
if(pillar)
pillar.drawing_object -= show_for
pillar = null
show_for = null
return ..()
/obj/effect/abstract/z_holder/proc/display(turf/display, datum/z_pillar/behalf_of)
if(pillar)
CRASH("We attempted to use a z holder to display when it was already in use, what'd you do")
pillar = behalf_of
show_for = display
vis_contents += display
behalf_of.drawing_object[display] = src
/// Grouping datum that manages transparency for a block of space
/// Setup to ease debugging, and to make add/remove operations cheaper
/datum/z_pillar
var/x_pos
var/y_pos
var/z_pos
/// Assoc list in the form displayed turf -> list of sources
var/list/turf_sources = list()
/// Assoc list of turfs using z holders in the form displayed turf -> z holder
var/list/drawing_object = list()
/datum/z_pillar/New(x_pos, y_pos, z_pos)
. = ..()
src.x_pos = x_pos
src.y_pos = y_pos
src.z_pos = z_pos
/datum/z_pillar/Destroy()
GLOB.pillars_by_z[z_pos][x_pos][y_pos] = null
// Just to be totally clear, this is code that exists to
// A: make sure cleanup is actually possible for this datum, just in case someone goes insane
// B: allow for easier debugging and making sure everything behaves as expected when fully removed
// It is not meant to be relied on, please don't actually it's not very fast
for(var/turf/displaying in turf_sources)
for(var/turf/displaying_for in turf_sources[displaying])
hide_turf(displaying, displaying_for)
return ..()
/// Displays a turf from the z level below us on our level
/datum/z_pillar/proc/display_turf(turf/to_display, turf/source)
var/list/sources = turf_sources[to_display]
if(!sources)
sources = list()
turf_sources[to_display] = sources
sources |= source
if(length(sources) != 1) // If we aren't the first to request this turf, return
var/obj/effect/abstract/z_holder/holding = drawing_object[to_display]
if(!holding)
return
var/turf/visual_target = to_display.above()
/// Basically, if we used to be under a non transparent turf, but are no longer in that position
/// Then we add to the transparent turf we're now under, and nuke the old object
if(!istransparentturf(visual_target))
return
holding.vis_contents -= to_display
qdel(holding)
drawing_object -= to_display
visual_target.vis_contents += to_display
return
var/turf/visual_target = to_display.above()
if(istransparentturf(visual_target) || isopenspaceturf(visual_target))
visual_target.vis_contents += to_display
else
var/obj/effect/abstract/z_holder/hold_this = new(visual_target)
hold_this.display(to_display, src)
/// Hides an existing turf from our vis_contents, or the vis_contents of the source if applicable
/datum/z_pillar/proc/hide_turf(turf/to_hide, turf/source)
var/list/sources = turf_sources[to_hide]
if(!sources)
return
sources -= source
// More sources remain
if(length(sources))
return
turf_sources -= to_hide
var/obj/effect/abstract/z_holder/holding = drawing_object[to_hide]
if(holding)
qdel(holding)
else
var/turf/visual_target = to_hide.above()
visual_target.vis_contents -= to_hide
if(!length(turf_sources) && !QDELETED(src))
qdel(src)
/// Called when a transparent turf is cleared. We wait a tick, then check to see what
/// Kind of turf replaced our former holder, and resetup our visuals as desired
/// We do not need to do this for non transparent holders, because they will have their abstract object cleared
/// When a transparent holder comes back.
/datum/z_pillar/proc/parent_cleared(turf/visual, turf/current_holder)
addtimer(CALLBACK(src, .proc/refresh_orphan, visual, current_holder))
/// Runs the actual refresh of some formerly orphaned via vis_loc deletiong turf
/// We'll only reup if we either have no souece, or if the source is a transparent turf
/datum/z_pillar/proc/refresh_orphan(turf/orphan, turf/parent)
var/list/sources = turf_sources[orphan]
if(!length(sources))
return
var/obj/effect/abstract/z_holder/holding = drawing_object[orphan]
if(holding)
return
if(istransparentturf(parent) || isopenspaceturf(parent))
parent.vis_contents += orphan
else
var/obj/effect/abstract/z_holder/hold_this = new(parent)
hold_this.display(orphan, src)
/datum/element/turf_z_transparency
element_flags = ELEMENT_DETACH
///This proc sets up the signals to handle updating viscontents when turfs above/below update. Handle plane and layer here too so that they don't cover other obs/turfs in Dream Maker
/datum/element/turf_z_transparency/Attach(datum/target, mapload)
. = ..()
if(!isturf(target))
return ELEMENT_INCOMPATIBLE
var/turf/our_turf = target
our_turf.layer = OPENSPACE_LAYER
RegisterSignal(target, COMSIG_TURF_MULTIZ_DEL, .proc/on_multiz_turf_del)
RegisterSignal(target, COMSIG_TURF_MULTIZ_NEW, .proc/on_multiz_turf_new)
ADD_TRAIT(our_turf, TURF_Z_TRANSPARENT_TRAIT, ELEMENT_TRAIT(type))
if(!mapload)
update_multi_z(our_turf)
/datum/element/turf_z_transparency/Detach(datum/source)
. = ..()
var/turf/our_turf = source
clear_multiz(our_turf)
UnregisterSignal(our_turf, list(COMSIG_TURF_MULTIZ_NEW, COMSIG_TURF_MULTIZ_DEL))
REMOVE_TRAIT(our_turf, TURF_Z_TRANSPARENT_TRAIT, ELEMENT_TRAIT(type))
///Updates the viscontents or underlays below this tile.
/datum/element/turf_z_transparency/proc/update_multi_z(turf/our_turf)
var/turf/below_turf = our_turf.below()
if(below_turf) // If we actually have something below us, display it.
for(var/turf/partner in range(1, below_turf))
// We use our z here to ensure the pillar is actually on our level
var/datum/z_pillar/z_boss = request_z_pillar(partner.x, partner.y, our_turf.z)
z_boss.display_turf(partner, our_turf)
else
our_turf.underlays += get_baseturf_underlay(our_turf)
// This shit is stupid
// z transparency is for making something SHOW WHAT'S BENEATH it, or if nothing is, show
// the appropriate underlay
// IT IS NOT FOR MAKING YOUR CLOSED TURF SEETHROUGH
// these are different concerns, and should not be HANDLED TOGETHER
// similarly, if you rip this out, rework diagonal closed turfs to work with this system
// it will make them look significantly nicer, and should let you tie into their logic more easily
// Just please don't break behavior yeah? thanks, I love you <3
if(isclosedturf(our_turf)) //Show girders below closed turfs
var/mutable_appearance/girder_underlay = mutable_appearance('icons/obj/structures.dmi', "girder", layer = TURF_LAYER-0.01)
girder_underlay.appearance_flags = RESET_ALPHA | RESET_COLOR
our_turf.underlays += girder_underlay
var/mutable_appearance/plating_underlay = mutable_appearance('icons/turf/floors.dmi', "plating", layer = TURF_LAYER-0.02)
plating_underlay.appearance_flags = RESET_ALPHA | RESET_COLOR
our_turf.underlays += plating_underlay
return TRUE
/datum/element/turf_z_transparency/proc/clear_multiz(turf/our_turf)
var/turf/below_turf = our_turf.below()
if(below_turf) // If we actually have something below us, we need to clear ourselves from it
for(var/turf/partner in range(1, below_turf))
// We use our z here to ensure the pillar is actually on our level
var/datum/z_pillar/z_boss = request_z_pillar(partner.x, partner.y, our_turf.z)
z_boss.hide_turf(partner, our_turf)
if(partner == below_turf)
z_boss.parent_cleared(below_turf, our_turf)
else
our_turf.underlays -= get_baseturf_underlay(our_turf)
if(isclosedturf(our_turf)) //Show girders below closed turfs
var/mutable_appearance/girder_underlay = mutable_appearance('icons/obj/structures.dmi', "girder", layer = TURF_LAYER-0.01)
girder_underlay.appearance_flags = RESET_ALPHA | RESET_COLOR
our_turf.underlays -= girder_underlay
var/mutable_appearance/plating_underlay = mutable_appearance('icons/turf/floors.dmi', "plating", layer = TURF_LAYER-0.02)
plating_underlay.appearance_flags = RESET_ALPHA | RESET_COLOR
our_turf.underlays -= plating_underlay
/datum/element/turf_z_transparency/proc/on_multiz_turf_del(turf/our_turf, turf/below_turf, dir)
SIGNAL_HANDLER
if(dir != DOWN)
return
update_multi_z(our_turf)
/datum/element/turf_z_transparency/proc/on_multiz_turf_new(turf/our_turf, turf/below_turf, dir)
SIGNAL_HANDLER
if(dir != DOWN)
return
update_multi_z(our_turf)
///Called when there is no real turf below this turf
/datum/element/turf_z_transparency/proc/get_baseturf_underlay(turf/our_turf)
var/turf/path = SSmapping.level_trait(our_turf.z, ZTRAIT_BASETURF) || /turf/open/space
if(!ispath(path))
path = text2path(path)
if(!ispath(path))
warning("Z-level [our_turf.z] has invalid baseturf '[SSmapping.level_trait(our_turf.z, ZTRAIT_BASETURF)]'")
path = /turf/open/space
var/mutable_appearance/underlay_appearance = mutable_appearance(initial(path.icon), initial(path.icon_state), layer = TURF_LAYER-0.02, offset_spokesman = our_turf, plane = PLANE_SPACE)
underlay_appearance.appearance_flags = RESET_ALPHA | RESET_COLOR
return underlay_appearance