Optimized steal item searcher to not search through the entire game world. (#64354)

Co-authored-by: Watermelon914 <3052169-Watermelon914@users.noreply.gitlab.com>
This commit is contained in:
Watermelon914
2022-01-28 02:05:50 -08:00
committed by GitHub
co-authored by Watermelon914
parent cea5e78210
commit d508546310
3 changed files with 138 additions and 17 deletions
@@ -21,31 +21,29 @@
GLOBAL_DATUM_INIT(steal_item_handler, /datum/objective_item_handler, new())
/datum/objective_item_handler
var/list/objectives_by_path = list()
var/list/objectives_by_path
/datum/objective_item_handler/New()
. = ..()
objectives_by_path = list()
for(var/datum/objective_item/item as anything in subtypesof(/datum/objective_item))
objectives_by_path[initial(item.targetitem)] = list()
RegisterSignal(SSatoms, COMSIG_SUBSYSTEM_POST_INITIALIZE, .proc/save_items)
// Very inefficient proc, only gets called when the map finishes loading.
/datum/objective_item_handler/proc/save_items()
for(var/datum/objective_item/steal/steal as anything in subtypesof(/datum/objective_item/steal))
if(!initial(steal.exists_on_map))
continue
objectives_by_path[initial(steal.targetitem)] = list()
for(var/atom/object as anything in world)
var/turf/place = get_turf(object)
if(!place || !is_station_level(place.z))
continue
for(var/typepath in objectives_by_path)
if(istype(object, typepath))
objectives_by_path[typepath] += object
RegisterSignal(object, COMSIG_PARENT_QDELETING, .proc/remove_item)
for(var/obj/item/typepath as anything in objectives_by_path)
for(var/obj/item/object as anything in objectives_by_path[typepath])
var/turf/place = get_turf(object)
if(!place || !is_station_level(place.z))
objectives_by_path[typepath] -= object
continue
RegisterSignal(object, COMSIG_PARENT_QDELETING, .proc/remove_item)
/datum/objective_item_handler/proc/remove_item(atom/source)
SIGNAL_HANDLER
for(var/typepath in objectives_by_path)
objectives_by_path[typepath] -= typepath
objectives_by_path[typepath] -= source
/datum/traitor_objective/steal_item
name = "Steal %ITEM% and place a bug on it. Hold it for %TIME% minutes"