From e1eb54ecdad3420c3c9508cd78e258ba87621684 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Thu, 4 Aug 2022 23:47:47 +0200 Subject: [PATCH] - replaced sectordist with a floating point variant with better precision. --- source/build/include/build.h | 1 - source/build/src/clip.cpp | 26 +++++++++++++++++--------- source/build/src/engine.cpp | 35 ++--------------------------------- 3 files changed, 19 insertions(+), 43 deletions(-) diff --git a/source/build/include/build.h b/source/build/include/build.h index 756e7f41eec..a6fe2f90d5e 100644 --- a/source/build/include/build.h +++ b/source/build/include/build.h @@ -172,7 +172,6 @@ inline void updatesectorz(int32_t const x, int32_t const y, int32_t const z, sec void updatesectorneighbor(int32_t const x, int32_t const y, int * const sectnum, int32_t maxDistance = MAXUPDATESECTORDIST) ATTRIBUTE((nonnull(3))); -int32_t getsectordist(vec2_t const in, int const sectnum, vec2_t * const out = nullptr); extern const int16_t *chsecptr_onextwall; inline int32_t krand(void) diff --git a/source/build/src/clip.cpp b/source/build/src/clip.cpp index 78ab897fb33..6cc4e975ae6 100644 --- a/source/build/src/clip.cpp +++ b/source/build/src/clip.cpp @@ -413,11 +413,11 @@ static void clipupdatesector(vec2_t const pos, int * const sectnum, int walldist if (inside_p(pos.X, pos.Y, *sectnum)) return; - int16_t nsecs = min(getsectordist(pos, *sectnum), INT16_MAX); + double nsecs = SquareDistToSector(pos.X * inttoworld, pos.Y * inttoworld, §or[*sectnum]); - if (nsecs > (walldist + 8)) + double wd = (walldist + 8) * inttoworld; wd *= wd; + if (nsecs > wd) { - Printf("%s(): initial position (%d, %d) not within initial sector %d; shortest distance %d.\n", __func__, pos.X, pos.Y, *sectnum, nsecs); walldist = 0x7fff; } @@ -1057,9 +1057,13 @@ void getzrange(const vec3_t& pos, sectortype* sect, int32_t* ceilz, CollisionBas vec2_t closest = pos.vec2; int sectnum = ::sectnum(sect); if (enginecompatibility_mode == ENGINECOMPATIBILITY_NONE) - getsectordist(closest, sectnum, &closest); - else - getzsofslopeptr(sect,closest.X,closest.Y,ceilz,florz); + { + DVector2 v; + SquareDistToSector(closest.X * inttoworld, closest.Y * inttoworld, §or[sectnum], &v); + closest = { int(v.X * worldtoint), int(v.Y * worldtoint) }; + } + + getzsofslopeptr(sect,closest.X,closest.Y,ceilz,florz); ceilhit.setSector(sect); florhit.setSector(sect); @@ -1119,9 +1123,13 @@ void getzrange(const vec3_t& pos, sectortype* sect, int32_t* ceilz, CollisionBas int32_t daz = 0, daz2 = 0; closest = pos.vec2; if (enginecompatibility_mode == ENGINECOMPATIBILITY_NONE) - getsectordist(closest, nextsectno, &closest); - else - getzsofslopeptr(nextsect, closest.X,closest.Y, &daz,&daz2); + { + DVector2 v; + SquareDistToSector(closest.X * inttoworld, closest.Y * inttoworld, §or[nextsectno], &v); + closest = { int(v.X * worldtoint), int(v.Y * worldtoint) }; + } + + getzsofslopeptr(nextsect, closest.X,closest.Y, &daz,&daz2); { if (daz > *ceilz) diff --git a/source/build/src/engine.cpp b/source/build/src/engine.cpp index 27b409e9b42..71272b3e3f2 100644 --- a/source/build/src/engine.cpp +++ b/source/build/src/engine.cpp @@ -471,42 +471,11 @@ int32_t getwalldist(vec2_t const in, int const wallnum, vec2_t * const out) } -int32_t getsectordist(vec2_t const in, int const sectnum, vec2_t * const out /*= nullptr*/) -{ - if (inside_p(in.X, in.Y, sectnum)) - { - if (out) - *out = in; - return 0; - } - - int32_t distance = INT32_MAX; - - vec2_t closest = {}; - - for (auto& wal : wallsofsector(sectnum)) - { - vec2_t p; - int32_t const walldist = getwalldist(in, wallnum(&wal), &p); - - if (walldist < distance) - { - distance = walldist; - closest = p; - } - } - - if (out) - *out = closest; - - return distance; -} - - template void updatesectorneighborz(int32_t const x, int32_t const y, int32_t const z, int* const sectnum, int32_t maxDistance, Inside checker) { int const initialsectnum = *sectnum; + double maxDist = maxDistance * inttoworld; maxDist *= maxDist; if ((validSectorIndex(initialsectnum))) { @@ -526,7 +495,7 @@ void updatesectorneighborz(int32_t const x, int32_t const y, int32_t const z, in for (auto& wal : wallsofsector(listsectnum)) { - if (wal.nextsector >= 0 && (iter == 0 || getsectordist({ x, y }, wal.nextsector) <= maxDistance)) + if (wal.nextsector >= 0 && (iter == 0 || SquareDistToSector(x * inttoworld, y * inttoworld, wal.nextSector()) <= maxDist)) search.Add(wal.nextsector); } iter++;