From d833cfa83bb153ddc589d7674c52a272397e7eb7 Mon Sep 17 00:00:00 2001 From: SkyratBot <59378654+SkyratBot@users.noreply.github.com> Date: Mon, 7 Aug 2023 15:41:46 +0200 Subject: [PATCH] [MIRROR] Optimize mirage_border by converting it to an element and a movable [MDB IGNORE] (#22913) * Optimize mirage_border by converting it to an element and a movable (#77137) ## About The Pull Request Converts /datum/component/mirage_border to an element, saving init time spent attaching components to thousands of objects. It also repaths it from /obj/effect/abstract to /atom/movable, since it doesn't need to be an object, as it's not a physical object within the game. ~~Also adds a case handling when world.view is not an integer.~~ This never happens Here it is working: https://github.com/tgstation/tgstation/assets/10366817/c8cfe2df-275a-4c97-b063-4fd83f7f09c3 Port of https://github.com/BeeStation/BeeStation-Hornet/pull/9490/ ## Why It's Good For The Game Saves init time, approx 0.32sec on Meta on my machine. ![image](https://github.com/tgstation/tgstation/assets/10366817/f423813e-e627-44a4-b6c6-b1d6fe49e8c3) ![image](https://github.com/tgstation/tgstation/assets/10366817/ad6796c6-ff78-4140-9a5a-3572ac34f42c) ## Changelog :cl: code: Optimized z-level transition mirages, saving ~0.32s init. /:cl: --------- Co-authored-by: Ghom <42542238+Ghommie@ users.noreply.github.com> * Optimize mirage_border by converting it to an element and a movable --------- Co-authored-by: itsmeow Co-authored-by: Ghom <42542238+Ghommie@ users.noreply.github.com> --- code/datums/components/mirage_border.dm | 44 ------------------- code/datums/elements/mirage_border.dm | 41 +++++++++++++++++ .../space_management/space_transition.dm | 5 ++- tgstation.dme | 2 +- 4 files changed, 46 insertions(+), 46 deletions(-) delete mode 100644 code/datums/components/mirage_border.dm create mode 100644 code/datums/elements/mirage_border.dm diff --git a/code/datums/components/mirage_border.dm b/code/datums/components/mirage_border.dm deleted file mode 100644 index eb167a98f9f..00000000000 --- a/code/datums/components/mirage_border.dm +++ /dev/null @@ -1,44 +0,0 @@ -/datum/component/mirage_border - can_transfer = TRUE - var/obj/effect/abstract/mirage_holder/holder - -/datum/component/mirage_border/Initialize(turf/target, direction, range=world.view) - if(!isturf(parent)) - return COMPONENT_INCOMPATIBLE - if(!target || !istype(target) || !direction) - . = COMPONENT_INCOMPATIBLE - CRASH("[type] improperly instanced with the following args: target=\[[target]\], direction=\[[direction]\], range=\[[range]\]") - - holder = new(parent) - - var/x = target.x - var/y = target.y - var/z = target.z - var/turf/southwest = locate(clamp(x - (direction & WEST ? range : 0), 1, world.maxx), clamp(y - (direction & SOUTH ? range : 0), 1, world.maxy), clamp(z, 1, world.maxz)) - var/turf/northeast = locate(clamp(x + (direction & EAST ? range : 0), 1, world.maxx), clamp(y + (direction & NORTH ? range : 0), 1, world.maxy), clamp(z, 1, world.maxz)) - //holder.vis_contents += block(southwest, northeast) // This doesnt work because of beta bug memes - for(var/i in block(southwest, northeast)) - holder.vis_contents += i - if(direction & SOUTH) - holder.pixel_y -= world.icon_size * range - if(direction & WEST) - holder.pixel_x -= world.icon_size * range - -/datum/component/mirage_border/Destroy() - QDEL_NULL(holder) - return ..() - -/datum/component/mirage_border/PreTransfer() - holder.moveToNullspace() - -/datum/component/mirage_border/PostTransfer() - if(!isturf(parent)) - return COMPONENT_INCOMPATIBLE - holder.forceMove(parent) - -INITIALIZE_IMMEDIATE(/obj/effect/abstract/mirage_holder) -/obj/effect/abstract/mirage_holder - name = "Mirage holder" - anchored = TRUE - mouse_opacity = MOUSE_OPACITY_TRANSPARENT - diff --git a/code/datums/elements/mirage_border.dm b/code/datums/elements/mirage_border.dm new file mode 100644 index 00000000000..999455a0b83 --- /dev/null +++ b/code/datums/elements/mirage_border.dm @@ -0,0 +1,41 @@ +/** + * Creates a mirage effect allowing you to see around the world border, by adding the opposite side to its vis_contents. + */ +/datum/element/mirage_border + +/datum/element/mirage_border/Attach(datum/target, turf/target_turf, direction, range=world.view) + . = ..() + if(!isturf(target)) + return ELEMENT_INCOMPATIBLE + #ifdef TESTING + // This is a highly used proc, and these error states never occur, so limit it to testing. + // If something goes wrong it will runtime anyway. + if(!target_turf || !istype(target_turf) || !direction) + stack_trace("[type] improperly attached with the following args: target=\[[target_turf]\], direction=\[[direction]\], range=\[[range]\]") + return ELEMENT_INCOMPATIBLE + #endif + + var/atom/movable/mirage_holder/holder = new(target) + + var/x = target_turf.x + var/y = target_turf.y + var/z = clamp(target_turf.z, 1, world.maxz) + var/turf/southwest = locate(clamp(x - (direction & WEST ? range : 0), 1, world.maxx), clamp(y - (direction & SOUTH ? range : 0), 1, world.maxy), z) + var/turf/northeast = locate(clamp(x + (direction & EAST ? range : 0), 1, world.maxx), clamp(y + (direction & NORTH ? range : 0), 1, world.maxy), z) + holder.vis_contents += block(southwest, northeast) + if(direction & SOUTH) + holder.pixel_y -= world.icon_size * range + if(direction & WEST) + holder.pixel_x -= world.icon_size * range + +/datum/element/mirage_border/Detach(atom/movable/target) + . = ..() + var/atom/movable/mirage_holder/held = locate() in target.contents + if(held) + qdel(held) + +INITIALIZE_IMMEDIATE(/atom/movable/mirage_holder) +// Using /atom/movable because this is a heavily used path +/atom/movable/mirage_holder + name = "Mirage holder" + mouse_opacity = MOUSE_OPACITY_TRANSPARENT diff --git a/code/modules/mapping/space_management/space_transition.dm b/code/modules/mapping/space_management/space_transition.dm index dfb2d30f225..b61897dd7b6 100644 --- a/code/modules/mapping/space_management/space_transition.dm +++ b/code/modules/mapping/space_management/space_transition.dm @@ -123,6 +123,9 @@ var/list/x_pos_transition = list(1, 1, TRANSITIONEDGE + 2, inner_max_x - 1) //values of x for the transition from respective blocks on the side of zlevel, 1 is being translated into turfs respective x value later in the code var/list/y_pos_transition = list(TRANSITIONEDGE + 2, inner_max_y - 1, 1, 1) //values of y for the transition from respective blocks on the side of zlevel, 1 is being translated into turfs respective y value later in the code + // Cache the range passed to the mirage border element, to reduce world var access in the thousands + var/range_cached = world.view + for(var/datum/space_level/level as anything in cached_z_list) if(!level.neigbours.len) continue @@ -156,6 +159,6 @@ continue var/turf/place = locate(S.destination_x, S.destination_y, zdestination) - S.AddComponent(/datum/component/mirage_border, place, mirage_dir) + S.AddElement(/datum/element/mirage_border, place, mirage_dir, range_cached) #undef CHORDS_TO_1D diff --git a/tgstation.dme b/tgstation.dme index 449bcab7bfc..67d342d6600 100644 --- a/tgstation.dme +++ b/tgstation.dme @@ -1078,7 +1078,6 @@ #include "code\datums\components\manual_blinking.dm" #include "code\datums\components\manual_breathing.dm" #include "code\datums\components\mind_linker.dm" -#include "code\datums\components\mirage_border.dm" #include "code\datums\components\mirv.dm" #include "code\datums\components\mob_harvest.dm" #include "code\datums\components\multiple_lives.dm" @@ -1365,6 +1364,7 @@ #include "code\datums\elements\light_eaten.dm" #include "code\datums\elements\light_eater.dm" #include "code\datums\elements\loomable.dm" +#include "code\datums\elements\mirage_border.dm" #include "code\datums\elements\mob_killed_tally.dm" #include "code\datums\elements\movement_turf_changer.dm" #include "code\datums\elements\movetype_handler.dm"