From 60ea578a8c5125c6ad9b5ac559b6e494207ecdd5 Mon Sep 17 00:00:00 2001 From: terminx Date: Fri, 19 Apr 2019 08:31:47 +0000 Subject: [PATCH] Add support for returning the closest point in the sector to getsectordist() git-svn-id: https://svn.eduke32.com/eduke32@7609 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/build/include/build.h | 4 ++-- source/build/src/engine.cpp | 31 +++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/source/build/include/build.h b/source/build/include/build.h index 9883ccb78..c1063abc4 100644 --- a/source/build/include/build.h +++ b/source/build/include/build.h @@ -1176,8 +1176,8 @@ int32_t try_facespr_intersect(uspriteptr_t const spr, const vec3_t *refpos, int findwallbetweensectors(int sect1, int sect2); static FORCE_INLINE bool sectoradjacent(int sect1, int sect2) { return findwallbetweensectors(sect1, sect2) != -1; } -int32_t getwalldist(vec2_t const &p, int const wallnum, vec2_t * const output = nullptr); -int32_t getsectordist(vec2_t const &p, int const sectnum); +int32_t getwalldist(vec2_t const &in, int const wallnum, vec2_t * const out = nullptr); +int32_t getsectordist(vec2_t const &in, int const sectnum, vec2_t * const out = nullptr); extern const int16_t *chsecptr_onextwall; int32_t checksectorpointer(int16_t i, int16_t sectnum); diff --git a/source/build/src/engine.cpp b/source/build/src/engine.cpp index cb857c869..378cbc4e5 100644 --- a/source/build/src/engine.cpp +++ b/source/build/src/engine.cpp @@ -10964,18 +10964,23 @@ static inline bool inside_z_p(int32_t const x, int32_t const y, int32_t const z, return (z >= cz && z <= fz && inside_p(x, y, sectnum)); } -int32_t getwalldist(vec2_t const &pos, int const wallnum, vec2_t * const output) +int32_t getwalldist(vec2_t const &in, int const wallnum, vec2_t * const out /*= nullptr*/) { vec2_t closest; - getclosestpointonwall_internal(pos, wallnum, &closest); - if (output) *output = closest; - return klabs(closest.x - pos.x) + klabs(closest.y - pos.y); + getclosestpointonwall_internal(in, wallnum, &closest); + if (out) + *out = closest; + return klabs(closest.x - in.x) + klabs(closest.y - in.y); } -int32_t getsectordist(vec2_t const &pos, int const sectnum) +int32_t getsectordist(vec2_t const &in, int const sectnum, vec2_t * const out /*= nullptr*/) { - if (inside_p(pos.x, pos.y, sectnum)) + if (inside_p(in.x, in.y, sectnum)) + { + if (out) + *out = in; return 0; + } int32_t distance = INT32_MAX; @@ -10983,13 +10988,23 @@ int32_t getsectordist(vec2_t const &pos, int const sectnum) int const startwall = sec->wallptr; int const endwall = sec->wallptr + sec->wallnum; auto uwal = (uwallptr_t)&wall[startwall]; + vec2_t closest = {}; for (int j = startwall; j < endwall; j++, uwal++) { - int32_t const walldist = getwalldist(pos, j); - distance = min(walldist, distance); + vec2_t p; + int32_t const walldist = getwalldist(in, j, &p); + + if (walldist < distance) + { + distance = walldist; + closest = p; + } } + if (out) + *out = closest; + return distance; }