Files
Bubberstation/code/modules/power/gravitygenerator.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

474 lines
15 KiB
Plaintext

//
// Gravity Generator
//
/// We will keep track of this by adding new gravity generators to the list, and keying it with the z level.
GLOBAL_LIST_EMPTY(gravity_generators)
#define POWER_IDLE 0
#define POWER_UP 1
#define POWER_DOWN 2
#define GRAV_NEEDS_SCREWDRIVER 0
#define GRAV_NEEDS_WELDING 1
#define GRAV_NEEDS_PLASTEEL 2
#define GRAV_NEEDS_WRENCH 3
//
// Abstract Generator
//
/obj/machinery/gravity_generator
name = "gravitational generator"
desc = "A device which produces a graviton field when set up."
icon = 'icons/obj/machines/gravity_generator.dmi'
density = TRUE
move_resist = INFINITY
use_power = NO_POWER_USE
resistance_flags = INDESTRUCTIBLE | LAVA_PROOF | FIRE_PROOF | UNACIDABLE | ACID_PROOF
var/sprite_number = 0
/obj/machinery/gravity_generator/safe_throw_at(atom/target, range, speed, mob/thrower, spin = TRUE, diagonals_first = FALSE, datum/callback/callback, force = MOVE_FORCE_STRONG, gentle = FALSE)
return FALSE
/obj/machinery/gravity_generator/ex_act(severity, target)
if(severity >= EXPLODE_DEVASTATE) // Very sturdy.
set_broken()
/obj/machinery/gravity_generator/blob_act(obj/structure/blob/B)
if(prob(20))
set_broken()
/obj/machinery/gravity_generator/zap_act(power, zap_flags)
. = ..()
if(zap_flags & ZAP_MACHINE_EXPLOSIVE)
qdel(src)//like the singulo, tesla deletes it. stops it from exploding over and over
/obj/machinery/gravity_generator/update_icon_state()
icon_state = "[get_status()]_[sprite_number]"
return ..()
/obj/machinery/gravity_generator/proc/get_status()
return "off"
// You aren't allowed to move.
/obj/machinery/gravity_generator/Move()
. = ..()
qdel(src)
/obj/machinery/gravity_generator/proc/set_broken()
atom_break()
/obj/machinery/gravity_generator/proc/set_fix()
set_machine_stat(machine_stat & ~BROKEN)
/**
* Generator part
*
* Parts of the gravity generator used to have a proper sprite.
*/
/obj/machinery/gravity_generator/part
var/obj/machinery/gravity_generator/main/main_part
/obj/machinery/gravity_generator/part/Destroy()
atom_break()
if(main_part)
UnregisterSignal(main_part, COMSIG_ATOM_UPDATED_ICON)
main_part = null
return ..()
/obj/machinery/gravity_generator/part/attackby(obj/item/weapon, mob/user, params)
if(!main_part)
return
return main_part.attackby(weapon, user)
/obj/machinery/gravity_generator/part/get_status()
if(!main_part)
return
return main_part.get_status()
/obj/machinery/gravity_generator/part/attack_hand(mob/user, list/modifiers)
if(!main_part)
return
return main_part.attack_hand(user, modifiers)
/obj/machinery/gravity_generator/part/set_broken()
..()
if(!main_part || (main_part.machine_stat & BROKEN))
return
main_part.set_broken()
/// Used to eat args
/obj/machinery/gravity_generator/part/proc/on_update_icon(obj/machinery/gravity_generator/source, updates, updated)
SIGNAL_HANDLER
return update_appearance(updates)
/**
* Main gravity generator
*
* The actual gravity generator, that actually holds the UI, contains the grav gen parts, ect.
*/
/obj/machinery/gravity_generator/main
icon_state = "on_8"
idle_power_usage = 0
active_power_usage = BASE_MACHINE_ACTIVE_CONSUMPTION * 3
power_channel = AREA_USAGE_ENVIRON
sprite_number = 8
use_power = IDLE_POWER_USE
interaction_flags_machine = INTERACT_MACHINE_ALLOW_SILICON | INTERACT_MACHINE_OFFLINE
/// List of all gravity generator parts
var/list/generator_parts = list()
/// The gravity generator part in the very center, the fifth one, where we place the overlays.
var/obj/machinery/gravity_generator/part/center_part
/// Whether the gravity generator is currently active.
var/on = TRUE
/// If the main breaker is on/off, to enable/disable gravity.
var/breaker = TRUE
/// If the generatir os idle, charging, or down.
var/charging_state = POWER_IDLE
/// How much charge the gravity generator has, goes down when breaker is shut, and shuts down at 0.
var/charge_count = 100
/// The gravity overlay currently used.
var/current_overlay = null
/// When broken, what stage it is at (GRAV_NEEDS_SCREWDRIVER:0) (GRAV_NEEDS_WELDING:1) (GRAV_NEEDS_PLASTEEL:2) (GRAV_NEEDS_WRENCH:3)
var/broken_state = GRAV_NEEDS_SCREWDRIVER
/// Gravity value when on, honestly I don't know why it does it like this, but it does.
var/setting = 1
/// The gravity field created by the generator.
var/datum/proximity_monitor/advanced/gravity/gravity_field
/// Audio for when the gravgen is on
var/datum/looping_sound/gravgen/soundloop
///Station generator that spawns with gravity turned off.
/obj/machinery/gravity_generator/main/off
on = FALSE
breaker = FALSE
charge_count = 0
/obj/machinery/gravity_generator/main/Initialize(mapload)
. = ..()
soundloop = new(src, start_immediately = FALSE)
setup_parts()
if(on)
enable()
center_part.add_overlay("activated")
/obj/machinery/gravity_generator/main/Destroy() // If we somehow get deleted, remove all of our other parts.
investigate_log("was destroyed!", INVESTIGATE_GRAVITY)
disable()
QDEL_NULL(soundloop)
QDEL_NULL(center_part)
QDEL_LIST(generator_parts)
return ..()
/obj/machinery/gravity_generator/main/proc/setup_parts()
var/turf/our_turf = get_turf(src)
// 9x9 block obtained from the bottom middle of the block
var/list/spawn_turfs = block(locate(our_turf.x - 1, our_turf.y + 2, our_turf.z), locate(our_turf.x + 1, our_turf.y, our_turf.z))
var/count = 10
for(var/turf/T in spawn_turfs)
count--
if(T == our_turf) // Skip our turf.
continue
var/obj/machinery/gravity_generator/part/part = new(T)
if(count == 5) // Middle
center_part = part
if(count <= 3) // Their sprite is the top part of the generator
part.set_density(FALSE)
part.layer = WALL_OBJ_LAYER
SET_PLANE(part, GAME_PLANE_UPPER, our_turf)
part.sprite_number = count
part.main_part = src
generator_parts += part
part.update_appearance()
part.RegisterSignal(src, COMSIG_ATOM_UPDATED_ICON, /obj/machinery/gravity_generator/part/proc/on_update_icon)
/obj/machinery/gravity_generator/main/set_broken()
. = ..()
for(var/obj/machinery/gravity_generator/internal_parts as anything in generator_parts)
if(!(internal_parts.machine_stat & BROKEN))
internal_parts.set_broken()
center_part.cut_overlays()
charge_count = 0
breaker = FALSE
set_power()
disable()
investigate_log("has broken down.", INVESTIGATE_GRAVITY)
/obj/machinery/gravity_generator/main/set_fix()
. = ..()
for(var/obj/machinery/gravity_generator/internal_parts as anything in generator_parts)
if(internal_parts.machine_stat & BROKEN)
internal_parts.set_fix()
broken_state = FALSE
update_appearance()
set_power()
// Interaction
// Fixing the gravity generator.
/obj/machinery/gravity_generator/main/attackby(obj/item/weapon, mob/user, params)
if(machine_stat & BROKEN)
switch(broken_state)
if(GRAV_NEEDS_SCREWDRIVER)
if(weapon.tool_behaviour == TOOL_SCREWDRIVER)
to_chat(user, span_notice("You secure the screws of the framework."))
weapon.play_tool_sound(src)
broken_state++
update_appearance()
return
if(GRAV_NEEDS_WELDING)
if(weapon.tool_behaviour == TOOL_WELDER)
if(weapon.use_tool(src, user, 0, volume=50, amount=1))
to_chat(user, span_notice("You mend the damaged framework."))
broken_state++
update_appearance()
return
if(GRAV_NEEDS_PLASTEEL)
if(istype(weapon, /obj/item/stack/sheet/plasteel))
var/obj/item/stack/sheet/plasteel/PS = weapon
if(PS.get_amount() >= 10)
PS.use(10)
to_chat(user, span_notice("You add the plating to the framework."))
playsound(src.loc, 'sound/machines/click.ogg', 75, TRUE)
broken_state++
update_appearance()
else
to_chat(user, span_warning("You need 10 sheets of plasteel!"))
return
if(GRAV_NEEDS_WRENCH)
if(weapon.tool_behaviour == TOOL_WRENCH)
to_chat(user, span_notice("You secure the plating to the framework."))
weapon.play_tool_sound(src)
set_fix()
return
return ..()
/obj/machinery/gravity_generator/main/ui_interact(mob/user, datum/tgui/ui)
ui = SStgui.try_update_ui(user, src, ui)
if(!ui)
ui = new(user, src, "GravityGenerator", name)
ui.open()
/obj/machinery/gravity_generator/main/ui_data(mob/user)
var/list/data = list()
data["breaker"] = breaker
data["charge_count"] = charge_count
data["charging_state"] = charging_state
data["on"] = on
data["operational"] = (machine_stat & BROKEN) ? FALSE : TRUE
return data
/obj/machinery/gravity_generator/main/ui_act(action, params)
. = ..()
if(.)
return
switch(action)
if("gentoggle")
breaker = !breaker
investigate_log("was toggled [breaker ? "<font color='green'>ON</font>" : "<font color='red'>OFF</font>"] by [key_name(usr)].", INVESTIGATE_GRAVITY)
set_power()
. = TRUE
// Power and Icon States
/obj/machinery/gravity_generator/main/power_change()
. = ..()
if(SSticker.current_state == GAME_STATE_PLAYING)
investigate_log("has [machine_stat & NOPOWER ? "lost" : "regained"] power.", INVESTIGATE_GRAVITY)
set_power()
/obj/machinery/gravity_generator/main/get_status()
if(machine_stat & BROKEN)
return "fix[min(broken_state, 3)]"
return on || charging_state != POWER_IDLE ? "on" : "off"
// Set the charging state based on power/breaker.
/obj/machinery/gravity_generator/main/proc/set_power()
var/new_state = FALSE
if(machine_stat & (NOPOWER|BROKEN) || !breaker)
new_state = FALSE
else if(breaker)
new_state = TRUE
charging_state = new_state ? POWER_UP : POWER_DOWN // Startup sequence animation.
if(SSticker.current_state == GAME_STATE_PLAYING)
investigate_log("is now [charging_state == POWER_UP ? "charging" : "discharging"].", INVESTIGATE_GRAVITY)
update_appearance()
/obj/machinery/gravity_generator/main/proc/enable()
charging_state = POWER_IDLE
on = TRUE
update_use_power(ACTIVE_POWER_USE)
soundloop.start()
if (!gravity_in_level())
if(SSticker.current_state == GAME_STATE_PLAYING)
investigate_log("was brought online and is now producing gravity for this level.", INVESTIGATE_GRAVITY)
message_admins("The gravity generator was brought online [ADMIN_VERBOSEJMP(src)]")
shake_everyone()
gravity_field = new(src, 2, TRUE, 6)
complete_state_update()
/obj/machinery/gravity_generator/main/proc/disable()
charging_state = POWER_IDLE
on = FALSE
update_use_power(IDLE_POWER_USE)
soundloop.stop()
QDEL_NULL(gravity_field)
if (gravity_in_level())
if(SSticker.current_state == GAME_STATE_PLAYING)
investigate_log("was brought offline and there is now no gravity for this level.", INVESTIGATE_GRAVITY)
message_admins("The gravity generator was brought offline with no backup generator. [ADMIN_VERBOSEJMP(src)]")
shake_everyone()
complete_state_update()
/obj/machinery/gravity_generator/main/proc/complete_state_update()
update_appearance()
update_list()
// Charge/Discharge and turn on/off gravity when you reach 0/100 percent.
/obj/machinery/gravity_generator/main/process()
if(machine_stat & BROKEN)
return
if(charging_state == POWER_IDLE)
return
if(charging_state == POWER_UP && charge_count >= 100)
enable()
else if(charging_state == POWER_DOWN && charge_count <= 0)
disable()
else
if(charging_state == POWER_UP)
charge_count += 2
else if(charging_state == POWER_DOWN)
charge_count -= 2
if(charge_count % 4 == 0 && prob(75)) // Let them know it is charging/discharging.
playsound(src.loc, 'sound/effects/empulse.ogg', 100, TRUE)
var/overlay_state = null
switch(charge_count)
if(0 to 20)
overlay_state = null
if(21 to 40)
overlay_state = "startup"
if(41 to 60)
overlay_state = "idle"
if(61 to 80)
overlay_state = "activating"
if(81 to 100)
overlay_state = "activated"
if(overlay_state != current_overlay)
if(center_part)
center_part.cut_overlays()
if(overlay_state)
center_part.add_overlay(overlay_state)
current_overlay = overlay_state
/// Shake everyone on the z level to let them know that gravity was enagaged/disengaged.
/obj/machinery/gravity_generator/main/proc/shake_everyone()
var/turf/T = get_turf(src)
var/sound/alert_sound = sound('sound/effects/alert.ogg')
for(var/mob/mobs as anything in GLOB.mob_list)
var/turf/mob_turf = get_turf(mobs)
if(!istype(mob_turf))
continue
if(!is_valid_z_level(T, mob_turf))
continue
mobs.update_gravity(mobs.has_gravity())
if(mobs.client)
shake_camera(mobs, 15, 1)
mobs.playsound_local(T, null, 100, 1, 0.5, sound_to_use = alert_sound)
/obj/machinery/gravity_generator/main/proc/gravity_in_level()
var/turf/T = get_turf(src)
if(!T)
return FALSE
if(GLOB.gravity_generators["[T.z]"])
return length(GLOB.gravity_generators["[T.z]"])
return FALSE
/obj/machinery/gravity_generator/main/proc/update_list()
var/turf/T = get_turf(src)
if(!T)
return
var/list/z_list = list()
// Multi-Z, station gravity generator generates gravity on all ZTRAIT_STATION z-levels.
if(SSmapping.level_trait(T.z, ZTRAIT_STATION))
for(var/z in SSmapping.levels_by_trait(ZTRAIT_STATION))
z_list += z
else
z_list += T.z
for(var/z in z_list)
if(!GLOB.gravity_generators["[z]"])
GLOB.gravity_generators["[z]"] = list()
if(on)
GLOB.gravity_generators["[z]"] |= src
else
GLOB.gravity_generators["[z]"] -= src
SSmapping.calculate_z_level_gravity(z)
/obj/machinery/gravity_generator/main/proc/change_setting(value)
if(value != setting)
setting = value
shake_everyone()
/obj/machinery/gravity_generator/main/proc/blackout()
charge_count = 0
breaker = FALSE
set_power()
disable()
investigate_log("was turned off by blackout event or a gravity anomaly detonation.", INVESTIGATE_GRAVITY)
/obj/machinery/gravity_generator/main/beforeShuttleMove(turf/newT, rotation, move_mode, obj/docking_port/mobile/moving_dock)
. = ..()
disable()
/obj/machinery/gravity_generator/main/afterShuttleMove(turf/oldT, list/movement_force, shuttle_dir, shuttle_preferred_direction, move_dir, rotation)
. = ..()
if(charge_count != 0 && charging_state != POWER_UP)
enable()
/obj/machinery/gravity_generator/main/on_changed_z_level(turf/old_turf, turf/new_turf, same_z_layer, notify_contents)
. = ..()
if(same_z_layer)
return
for(var/obj/machinery/gravity_generator/part as anything in generator_parts)
SET_PLANE(part, PLANE_TO_TRUE(part.plane), new_turf)
//prevents shuttles attempting to rotate this since it messes up sprites
/obj/machinery/gravity_generator/main/shuttleRotate(rotation, params)
params = NONE
return ..()
// Misc
/// Gravity generator instruction guide
/obj/item/paper/guides/jobs/engi/gravity_gen
name = "paper- 'Generate your own gravity!'"
default_raw_text = {"<h1>Gravity Generator Instructions For Dummies</h1>
<p>Surprisingly, gravity isn't that hard to make! All you have to do is inject deadly radioactive minerals into a ball of
energy and you have yourself gravity! You can turn the machine on or off when required.
The generator produces a very harmful amount of gravity when enabled, so don't stay close for too long.</p>
<br>
<h3>It blew up!</h3>
<p>Don't panic! The gravity generator was designed to be easily repaired. If, somehow, the sturdy framework did not survive then
please proceed to panic; otherwise follow these steps.</p><ol>
<li>Secure the screws of the framework with a screwdriver.</li>
<li>Mend the damaged framework with a welding tool.</li>
<li>Add additional plasteel plating.</li>
<li>Secure the additional plating with a wrench.</li></ol>"}