Skip to content

Commit

Permalink
- replaced sectordist with a floating point variant with better preci…
Browse files Browse the repository at this point in the history
…sion.
  • Loading branch information
coelckers committed Aug 5, 2022
1 parent b31e6c0 commit e1eb54e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 43 deletions.
1 change: 0 additions & 1 deletion source/build/include/build.h
Expand Up @@ -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)
Expand Down
26 changes: 17 additions & 9 deletions source/build/src/clip.cpp
Expand Up @@ -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<int16_t>(getsectordist(pos, *sectnum), INT16_MAX);
double nsecs = SquareDistToSector(pos.X * inttoworld, pos.Y * inttoworld, &sector[*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;
}

Expand Down Expand Up @@ -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, &sector[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);

Expand Down Expand Up @@ -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, &sector[nextsectno], &v);
closest = { int(v.X * worldtoint), int(v.Y * worldtoint) };
}

getzsofslopeptr(nextsect, closest.X,closest.Y, &daz,&daz2);

{
if (daz > *ceilz)
Expand Down
35 changes: 2 additions & 33 deletions source/build/src/engine.cpp
Expand Up @@ -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<class Inside>
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)))
{
Expand All @@ -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++;
Expand Down

0 comments on commit e1eb54e

Please sign in to comment.