From 71a7bb9223d9a4dbd33fa30051b326e32bbf3677 Mon Sep 17 00:00:00 2001 From: Fluffy <65877598+FluffyGhoster@users.noreply.github.com> Date: Thu, 7 Sep 2023 17:41:41 +0200 Subject: [PATCH] Shave process spatial gridmap (#16693) * Atomization * atomization + possible fix + changelog * Update html/changelogs/FluffyGhost-shave_process_spatial_gridmap.yml Co-authored-by: Cody Brittain --------- Co-authored-by: FluffyGhost Co-authored-by: Cody Brittain --- code/__defines/spatial_gridmap.dm | 35 ++++++++++++++++ code/_helpers/spatial_info.dm | 26 ++---------- .../living/simple_animal/hostile/hostile.dm | 4 +- ...ffyGhost-shave_process_spatial_gridmap.yml | 41 +++++++++++++++++++ 4 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 html/changelogs/FluffyGhost-shave_process_spatial_gridmap.yml diff --git a/code/__defines/spatial_gridmap.dm b/code/__defines/spatial_gridmap.dm index 8e0ee8b0495..04d2c4c5d76 100644 --- a/code/__defines/spatial_gridmap.dm +++ b/code/__defines/spatial_gridmap.dm @@ -17,3 +17,38 @@ // Whether movable is itself or containing something which should be in one of the spatial grid channels #define HAS_SPATIAL_GRID_CONTENTS(movable) (movable.important_recursive_contents && \ (movable.important_recursive_contents[RECURSIVE_CONTENTS_HEARING_SENSITIVE] || movable.important_recursive_contents[RECURSIVE_CONTENTS_CLIENT_MOBS] || movable.important_recursive_contents[RECURSIVE_CONTENTS_AI_TARGETS])) + + +/** + * Checks if two atoms are in line of sight (can see each other) + * + * * RETURN_VALUE - Where to store the result of this check (TRUE if LoS, FALSE otherwise) + * * source - Source atom + * * target - Target atom + * * view_radius - How far the atom can see, usually you want to use `world.view` for the default value + */ +#define SPATIAL_CHECK_LOS(RETURN_VALUE, source, target, view_radius)\ + do{\ + var/turf/source_turf = get_turf(source);\ + var/turf/target_turf = get_turf(target);\ + var/distance = get_dist(source_turf, target_turf);\ + if(distance > view_radius){\ + RETURN_VALUE = FALSE;\ + break;\ + }\ + if(distance < 2){\ + RETURN_VALUE = TRUE;\ + break;\ + }\ + for(var/step_counter in 1 to distance){\ + source_turf = get_step_towards(source_turf, target_turf);\ + if(source_turf == target_turf){\ + RETURN_VALUE = TRUE;\ + break;\ + }\ + if(IS_OPAQUE_TURF(source_turf)){\ + RETURN_VALUE = FALSE;\ + break;\ + }\ + }\ + } while(FALSE) diff --git a/code/_helpers/spatial_info.dm b/code/_helpers/spatial_info.dm index 46f1583af85..484cb0fd717 100644 --- a/code/_helpers/spatial_info.dm +++ b/code/_helpers/spatial_info.dm @@ -191,32 +191,12 @@ . = SSspatial_grid.orthogonal_range_search(source, SPATIAL_GRID_CONTENTS_TYPE_TARGETS, view_radius) for(var/mob/target as anything in .) - if(!check_los(source, target, view_radius)) + var/los = null + SPATIAL_CHECK_LOS(los, source, target, view_radius) + if(!los) . -= target continue -/proc/check_los(atom/source, atom/target, view_radius = world.view) - var/turf/source_turf = get_turf(source) - var/turf/target_turf = get_turf(target) - - var/distance = get_dist(source_turf, target_turf) - if(distance > view_radius) - return FALSE - - if(distance < 2) - return TRUE - - var/turf/mid_turf = source_turf - - for(var/step_counter in 1 to distance) - mid_turf = get_step_towards(mid_turf, target_turf) - if(mid_turf == target_turf) - break - if(IS_OPAQUE_TURF(mid_turf)) - return FALSE - - return TRUE - ///Calculate if two atoms are in sight, returns TRUE or FALSE /proc/inLineOfSight(X1,Y1,X2,Y2,Z=1,PX1=16.5,PY1=16.5,PX2=16.5,PY2=16.5) var/turf/T diff --git a/code/modules/mob/living/simple_animal/hostile/hostile.dm b/code/modules/mob/living/simple_animal/hostile/hostile.dm index e4eb542a7e5..342ac7d4e5c 100644 --- a/code/modules/mob/living/simple_animal/hostile/hostile.dm +++ b/code/modules/mob/living/simple_animal/hostile/hostile.dm @@ -151,7 +151,9 @@ return /mob/living/simple_animal/hostile/proc/see_target() - return check_los(src, target_mob) + var/los = null + SPATIAL_CHECK_LOS(los, src, target_mob, world.view) + return los /mob/living/simple_animal/hostile/proc/MoveToTarget() stop_automated_movement = 1 diff --git a/html/changelogs/FluffyGhost-shave_process_spatial_gridmap.yml b/html/changelogs/FluffyGhost-shave_process_spatial_gridmap.yml new file mode 100644 index 00000000000..e356238545e --- /dev/null +++ b/html/changelogs/FluffyGhost-shave_process_spatial_gridmap.yml @@ -0,0 +1,41 @@ +################################ +# Example Changelog File +# +# Note: This file, and files beginning with ".", and files that don't end in ".yml" will not be read. If you change this file, you will look really dumb. +# +# Your changelog will be merged with a master changelog. (New stuff added only, and only on the date entry for the day it was merged.) +# When it is, any changes listed below will disappear. +# +# Valid Prefixes: +# bugfix +# wip (For works in progress) +# tweak +# soundadd +# sounddel +# rscadd (general adding of nice things) +# rscdel (general deleting of nice things) +# imageadd +# imagedel +# maptweak +# spellcheck (typo fixes) +# experiment +# balance +# admin +# backend +# security +# refactor +################################# + +# Your name. +author: FluffyGhost + +# Optional: Remove this file after generating master changelog. Useful for PR changelogs that won't get used again. +delete-after: True + +# Any changes you've made. See valid prefix list above. +# INDENT WITH TWO SPACES. NOT TABS. SPACES. +# SCREW THIS UP AND IT WON'T WORK. +# Also, all entries are changed into a single [] after a master changelog generation. Just remove the brackets when you add new entries. +# Please surround your changes in double quotes ("), as certain characters otherwise screws up compiling. The quotes will not show up in the changelog. +changes: + - refactor: "Added a spatial gridmap check LoS define, to be used inside spatial_info for often called procs (get_target_in_LOS for now, which is called very often, 455k times in 10 minutes)."