[MIRROR] Converts the enemies list to weakrefs (#6453)

* Converts the enemies list to weakrefs (#59694)

This has some minor overhead, but listtargets isn't exactly a hot proc, so we should be just fine

* Converts the enemies list to weakrefs

Co-authored-by: LemonInTheDark <58055496+LemonInTheDark@users.noreply.github.com>
This commit is contained in:
SkyratBot
2021-06-22 22:36:56 +01:00
committed by GitHub
co-authored by LemonInTheDark
parent aaef7e7f9d
commit 3777cff370
3 changed files with 29 additions and 26 deletions
@@ -49,7 +49,7 @@
Retaliate()
if(enemies.len && DT_PROB(5, delta_time))
clear_enemies()
enemies.Cut()
LoseTarget()
src.visible_message(span_notice("[src] calms down."))
if(stat != CONSCIOUS)
@@ -53,10 +53,19 @@
living_mobs += HM
// if no tasty mice to chase, lets chase any living mob enemies in our vision range
if(length(mice) == 0)
//Filter living mobs (in range mobs) by those we consider enemies (retaliate behaviour)
return living_mobs & enemies
return mice
if(length(mice))
return mice
var/list/actual_enemies = list()
for(var/datum/weakref/enemy as anything in enemies)
var/mob/flesh_and_blood = enemy.resolve()
if(!flesh_and_blood)
enemies -= enemy
continue
actual_enemies += flesh_and_blood
//Filter living mobs (in range mobs) by those we consider enemies (retaliate behaviour)
return living_mobs & actual_enemies
/mob/living/simple_animal/hostile/retaliate/snake/AttackingTarget()
if(istype(target, /mob/living/simple_animal/mouse))
@@ -1,4 +1,5 @@
/mob/living/simple_animal/hostile/retaliate
///A list of weakrefs pointing at things that we consider targets
var/list/enemies = list()
/mob/living/simple_animal/hostile/retaliate/Found(atom/A)
@@ -7,7 +8,7 @@
if(!L.stat)
return L
else
remove_enemy(L)
enemies -= WEAKREF(L)
else if(ismecha(A))
var/obj/vehicle/sealed/mecha/M = A
if(LAZYLEN(M.occupants))
@@ -17,7 +18,15 @@
if(!enemies.len)
return list()
var/list/see = ..()
see &= enemies // Remove all entries that aren't in enemies
var/list/actual_enemies = list()
for(var/datum/weakref/enemy as anything in enemies)
var/mob/flesh_and_blood = enemy.resolve()
if(!flesh_and_blood)
enemies -= enemy
continue
actual_enemies += flesh_and_blood
see &= actual_enemies // Remove all entries that aren't in enemies
return see
/mob/living/simple_animal/hostile/retaliate/proc/Retaliate()
@@ -29,37 +38,22 @@
if(isliving(A))
var/mob/living/M = A
if(faction_check_mob(M) && attack_same || !faction_check_mob(M))
add_enemy(M)
enemies |= WEAKREF(M)
else if(ismecha(A))
var/obj/vehicle/sealed/mecha/M = A
if(LAZYLEN(M.occupants))
add_enemy(M)
enemies |= WEAKREF(M)
add_enemies(M.occupants)
for(var/mob/living/simple_animal/hostile/retaliate/H in around)
if(faction_check_mob(H) && !attack_same && !H.attack_same)
H.add_enemies(enemies)
H.enemies |= enemies
/mob/living/simple_animal/hostile/retaliate/adjustHealth(amount, updating_health = TRUE, forced = FALSE)
. = ..()
if(. > 0 && stat == CONSCIOUS)
Retaliate()
/mob/living/simple_animal/hostile/retaliate/proc/add_enemy(new_enemy)
RegisterSignal(new_enemy, COMSIG_PARENT_QDELETING, .proc/remove_enemy, override = TRUE)
enemies |= new_enemy
/mob/living/simple_animal/hostile/retaliate/proc/add_enemies(new_enemies)
for(var/new_enemy in new_enemies)
RegisterSignal(new_enemy, COMSIG_PARENT_QDELETING, .proc/remove_enemy, override = TRUE)
enemies |= new_enemy
/mob/living/simple_animal/hostile/retaliate/proc/clear_enemies()
for(var/enemy in enemies)
UnregisterSignal(enemy, COMSIG_PARENT_QDELETING)
enemies.Cut()
/mob/living/simple_animal/hostile/retaliate/proc/remove_enemy(datum/enemy_to_remove)
SIGNAL_HANDLER
UnregisterSignal(enemy_to_remove, COMSIG_PARENT_QDELETING)
enemies -= enemy_to_remove
enemies |= WEAKREF(new_enemy)