Skip to content

Commit

Permalink
Heavily optimize decomposition code (#3210)
Browse files Browse the repository at this point in the history
  • Loading branch information
Absolucy authored Sep 7, 2024
1 parent d9721bd commit 156728e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions code/__DEFINES/~monkestation/elevation.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#define GIVE_TURF_ELEVATED_TRAIT(X) ##X/Initialize(mapload){\
. = ..();\
var/static/list/give_turf_traits = list(TRAIT_TURF_HAS_ELEVATED_STRUCTURE);\
AddElement(/datum/element/give_turf_traits, give_turf_traits);\
}
3 changes: 3 additions & 0 deletions code/__DEFINES/~monkestation/traits.dm
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
#define TRAIT_COVERED_BY_METEOR_SHIELD "covered_by_meteor_shield"
/// Liquids cannot spread over this turf.
#define TRAIT_BLOCK_LIQUID_SPREAD "block_liquid_spread"
/// Replacement for GLOB.typecache_elevated_structures
#define TRAIT_TURF_HAS_ELEVATED_STRUCTURE "turf_has_elevated_structure"

///added to structures we want the mobs to be able to target.
#define TRAIT_MOB_DESTROYABLE "mob_destroyable"

2 changes: 2 additions & 0 deletions code/_globalvars/lists/typecache.dm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ GLOBAL_LIST_INIT(typecache_machine_or_structure, typecacheof(list(
/obj/structure,
)))

/* monkestation removal: use TRAIT_TURF_HAS_ELEVATED_STRUCTURE instead
/// A typecache listing structures that are considered to have surfaces that you can place items on that are higher than the floor. This, of course, should be restricted to /atom/movables. This is primarily used for food decomposition code.
GLOBAL_LIST_INIT(typecache_elevated_structures, typecacheof(list(
/obj/machinery/conveyor,
Expand All @@ -24,6 +25,7 @@ GLOBAL_LIST_INIT(typecache_elevated_structures, typecacheof(list(
/obj/machinery/smartfridge,
/obj/machinery/smartfridge/drying_rack, // Redundant, given above, but this is for the sake of explicitness.
)))
monkestation end */

/// A typecache of objects that player controlled, easily accessible, hostile mobs should not be able to attack
GLOBAL_LIST_INIT(typecache_general_bad_hostile_attack_targets, typecacheof(list(
Expand Down
17 changes: 15 additions & 2 deletions code/datums/components/food/decomposition.dm
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@
var/decomp_result
/// Does our food attract ants?
var/produce_ants = FALSE
/// Typecache of turfs that support decomposition
var/static/list/decomp_turf_typecache // monkestation edit: attempt at micro-optimizing

/datum/component/decomposition/Initialize(mapload, decomp_req_handle, decomp_flags = NONE, decomp_result, ant_attracting = FALSE, custom_time = 0)
if(!isobj(parent))
return COMPONENT_INCOMPATIBLE

// monkestation start: attempt at micro-optimizing
if(!decomp_turf_typecache)
decomp_turf_typecache = typecacheof(/turf/open) - (typecacheof(/turf/open/lava) + typecacheof(/turf/open/misc/asteroid))
// monkestation end

src.decomp_flags = decomp_flags
src.decomp_result = decomp_result
if(mapload || decomp_req_handle)
Expand Down Expand Up @@ -77,14 +84,20 @@

var/turf/open/open_turf = food.loc

if(!istype(open_turf) || islava(open_turf) || isasteroidturf(open_turf)) //Are we actually in a valid open turf?
// monkestation start: heavily optimize this stupid proc
if(!is_type_in_typecache(open_turf, decomp_turf_typecache)) //Are we actually in a valid open turf?
remove_timer()
return

for(var/atom/movable/content as anything in open_turf.contents)
if(HAS_TRAIT(open_turf, TRAIT_ELEVATED_TURF) || HAS_TRAIT(open_turf, TRAIT_TURF_HAS_ELEVATED_STRUCTURE))
remove_timer()
return
/* for(var/atom/movable/content as anything in open_turf.contents)
if(GLOB.typecache_elevated_structures[content.type])
remove_timer()
return
CHECK_TICK */
// monkestation end

// If all other checks fail, then begin decomposition.
timerid = addtimer(CALLBACK(src, PROC_REF(decompose)), time_remaining, TIMER_STOPPABLE | TIMER_UNIQUE)
Expand Down
6 changes: 6 additions & 0 deletions monkestation/code/game/objects/structures/elevation.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GIVE_TURF_ELEVATED_TRAIT(/obj/machinery/conveyor)
GIVE_TURF_ELEVATED_TRAIT(/obj/machinery/stasis)
GIVE_TURF_ELEVATED_TRAIT(/obj/machinery/closet)
GIVE_TURF_ELEVATED_TRAIT(/obj/machinery/rack)
GIVE_TURF_ELEVATED_TRAIT(/obj/machinery/table)
GIVE_TURF_ELEVATED_TRAIT(/obj/machinery/smartfridge)
2 changes: 2 additions & 0 deletions tgstation.dme
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@
#include "code\__DEFINES\~monkestation\cooldowns.dm"
#include "code\__DEFINES\~monkestation\cybernetics.dm"
#include "code\__DEFINES\~monkestation\DNA.dm"
#include "code\__DEFINES\~monkestation\elevation.dm"
#include "code\__DEFINES\~monkestation\factions.dm"
#include "code\__DEFINES\~monkestation\guns.dm"
#include "code\__DEFINES\~monkestation\hacking.dm"
Expand Down Expand Up @@ -5927,6 +5928,7 @@
#include "monkestation\code\game\objects\items\storage\garment.dm"
#include "monkestation\code\game\objects\items\storage\uplink_kits.dm"
#include "monkestation\code\game\objects\items\storage\boxes\security_boxes.dm"
#include "monkestation\code\game\objects\structures\elevation.dm"
#include "monkestation\code\game\objects\structures\tables_racks.dm"
#include "monkestation\code\game\objects\structures\window.dm"
#include "monkestation\code\game\objects\structures\beds_chairs\chair.dm"
Expand Down

0 comments on commit 156728e

Please sign in to comment.