Skip to content

Commit

Permalink
Optimize get_hearers_in_view() (#421)
Browse files Browse the repository at this point in the history
* swap out view() for hearers()

* docs
  • Loading branch information
Kapu1178 authored Jul 11, 2023
1 parent 68e5ab3 commit 917f48a
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions code/__HELPERS/spatial_info.dm
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,17 @@

var/list/assigned_oranges_ears = SSspatial_grid.assign_oranges_ears(hearables_from_grid)

var/old_luminosity = center_turf.luminosity
center_turf.luminosity = 6 //man if only we had an inbuilt dview()

//this is the ENTIRE reason all this shit is worth it due to how view() works and can be optimized
//view() constructs lists of viewed atoms by default and specifying a specific type of atom to look for limits the lists it constructs to those of that
//primitive type and then when the view operation is completed the output is then typechecked to only iterate through objects in view with the same
//typepath. by assigning one /mob/oranges_ear to every turf with hearable atoms on it and giving them references to each one means that:
//1. view() only constructs lists of atoms with the mob primitive type and
//2. the mobs returned by view are fast typechecked to only iterate through /mob/oranges_ear mobs, which guarantees at most one per turf
//on a whole this can outperform iterating through all movables in view() by ~2x especially when hearables are a tiny percentage of movables in view
for(var/mob/oranges_ear/ear in view(view_radius, center_turf))
//typepath. by assigning one /mob/oranges_ear to every turf with hearable atoms on it and giving them references to each one means that
//hearers() can be used over view(), which is a huge speed increase.
for(var/mob/oranges_ear/ear in hearers(view_radius, center_turf))
. += ear.references

for(var/mob/oranges_ear/remaining_ear as anything in assigned_oranges_ears)//we need to clean up our mess
remaining_ear.unassign()

center_turf.luminosity = old_luminosity
return .

/**
Expand Down

0 comments on commit 917f48a

Please sign in to comment.