From 838bed78009619b78e04b51a06e18c9fae61bef5 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 6 Dec 2021 18:17:45 +0100 Subject: [PATCH] - final cleanup of sectnum related stuff. Made sure that remaining uses of sector indices are safe to be kept and deleted a few redundant functions. --- source/build/include/build.h | 9 ++---- source/build/src/clip.cpp | 10 +++---- source/build/src/engine.cpp | 40 +++++---------------------- source/core/gamefuncs.cpp | 15 ++++++++++ source/core/gamefuncs.h | 13 +-------- source/core/rendering/hw_sections.cpp | 2 +- source/core/rendering/render.h | 3 +- source/core/sectorgeometry.cpp | 2 +- source/games/duke/src/premap.cpp | 6 ++-- source/games/exhumed/src/move.cpp | 1 - 10 files changed, 38 insertions(+), 63 deletions(-) diff --git a/source/build/include/build.h b/source/build/include/build.h index a6fb66f50..1659a8c28 100644 --- a/source/build/include/build.h +++ b/source/build/include/build.h @@ -351,7 +351,7 @@ int hitscan(const vec3_t& start, const sectortype* startsect, const vec3_t& dire void neartag(const vec3_t& pos, sectortype* sect, int angle, HitInfoBase& result, int neartagrange, int tagsearch); int cansee(int x1, int y1, int z1, sectortype* sect1, int x2, int y2, int z2, sectortype* sect2); -int32_t inside(int32_t x, int32_t y, int sectnum); +int32_t inside(int32_t x, int32_t y, const sectortype* sectnum); void dragpoint(int pointhighlight, int32_t dax, int32_t day); int32_t try_facespr_intersect(uspriteptr_t const spr, vec3_t const in, int32_t vx, int32_t vy, int32_t vz, @@ -380,9 +380,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))); -int findwallbetweensectors(int sect1, int sect2); - -inline int sectoradjacent(int sect1, int sect2) { return findwallbetweensectors(sect1, sect2) != -1; } int32_t getsectordist(vec2_t const in, int const sectnum, vec2_t * const out = nullptr); extern const int16_t *chsecptr_onextwall; @@ -556,9 +553,9 @@ extern int skiptile; static vec2_t const zerovec = { 0, 0 }; -inline int inside_p(int32_t const x, int32_t const y, int const sectnum) { return (sectnum >= 0 && inside(x, y, sectnum) == 1); } +inline int inside_p(int32_t const x, int32_t const y, int const sectnum) { return (sectnum >= 0 && inside(x, y, §or[sectnum]) == 1); } // same as above but with the same signature as inside_z_p for passing to updatesectorneighborz. -inline int inside_p0(int32_t const x, int32_t const y, int32_t const z, int const sectnum) { return (sectnum >= 0 && inside(x, y, sectnum) == 1); } +inline int inside_p0(int32_t const x, int32_t const y, int32_t const z, int const sectnum) { return inside_p(x, y, sectnum); } #define SET_AND_RETURN(Lval, Rval) \ do \ diff --git a/source/build/src/clip.cpp b/source/build/src/clip.cpp index 37aaecacd..70f09fb14 100644 --- a/source/build/src/clip.cpp +++ b/source/build/src/clip.cpp @@ -529,7 +529,7 @@ CollisionBase clipmove_(vec3_t * const pos, int * const sectnum, int32_t xvect, // We're not interested in any sector reached by portal traversal that we're "inside" of. if (enginecompatibility_mode == ENGINECOMPATIBILITY_NONE && !curspr && dasect != initialsectnum - && inside(pos->x, pos->y, dasect) == 1) + && inside(pos->x, pos->y, sec) == 1) { int k; for (k=startwall; kvec2; keepaway(&pos->x, &pos->y, i); - if (inside(pos->x,pos->y, *sectnum) != 1) + if (inside_p(pos->x,pos->y, *sectnum) != 1) pos->vec2 = vec; break; } @@ -779,7 +779,7 @@ CollisionBase clipmove_(vec3_t * const pos, int * const sectnum, int32_t xvect, if (enginecompatibility_mode != ENGINECOMPATIBILITY_NONE) { for (int j=0; jx, pos->y, clipsectorlist[j]) == 1) + if (inside_p(pos->x, pos->y, clipsectorlist[j]) == 1) { *sectnum = clipsectorlist[j]; return clipReturn; @@ -788,7 +788,7 @@ CollisionBase clipmove_(vec3_t * const pos, int * const sectnum, int32_t xvect, int32_t tempint2, tempint1 = INT32_MAX; *sectnum = -1; for (int j=numsectors-1; j>=0; j--) - if (inside(pos->x, pos->y, j) == 1) + if (inside_p(pos->x, pos->y, j) == 1) { if (enginecompatibility_mode != ENGINECOMPATIBILITY_19950829 && (sector[j].ceilingstat&2)) tempint2 = getceilzofslope(j, pos->x, pos->y) - pos->z; @@ -1234,7 +1234,7 @@ static int32_t hitscan_trysector(const vec3_t *sv, sectortype* sec, HitInfoBase if ((x1 != INT32_MAX) && (abs(x1-sv->x)+abs(y1-sv->y) < abs((hit->hitpos.x)-sv->x)+abs((hit->hitpos.y)-sv->y))) { - if (inside(x1,y1,sectnum(sec)) == 1) + if (inside(x1,y1,sec) == 1) { hit_set(hit, sec, nullptr, nullptr, x1, y1, z1); hitscan_hitsectcf = (how+1)>>1; diff --git a/source/build/src/engine.cpp b/source/build/src/engine.cpp index 0434bafd7..5b686afd9 100644 --- a/source/build/src/engine.cpp +++ b/source/build/src/engine.cpp @@ -443,21 +443,16 @@ int32_t engineInit(void) // See http://fabiensanglard.net/duke3d/build_engine_internals.php, // "Inside details" for the idea behind the algorithm. -int32_t inside(int32_t x, int32_t y, int sectnum) +int32_t inside(int32_t x, int32_t y, const sectortype* sect) { - if (validSectorIndex(sectnum)) + if (sect) { - uint32_t cnt = 0; - auto wal = (uwallptr_t)sector[sectnum].firstWall(); - int wallsleft = sector[sectnum].wallnum; - - do + unsigned cnt = 0; + vec2_t xy = { x, y }; + for(auto& wal : wallsofsector(sect)) { - // Get the x and y components of the [tested point]-->[wall - // point{1,2}] vectors. - vec2_t v1 = { wal->x - x, wal->y - y }; - auto const &wal2 = *(uwallptr_t)wal->point2Wall(); - vec2_t v2 = { wal2.x - x, wal2.y - y }; + vec2_t v1 = wal.pos - xy; + vec2_t v2 = wal.point2Wall()->pos - xy; // If their signs differ[*], ... // @@ -467,14 +462,9 @@ int32_t inside(int32_t x, int32_t y, int sectnum) // where y_m := min(y1, y2) and y_M := max(y1, y2). if ((v1.y^v2.y) < 0) cnt ^= (((v1.x^v2.x) >= 0) ? v1.x : (v1.x*v2.y-v2.x*v1.y)^v2.y); - - wal++; } - while (--wallsleft); - return cnt>>31; } - return -1; } @@ -854,22 +844,6 @@ int32_t getsectordist(vec2_t const in, int const sectnum, vec2_t * const out /*= return distance; } -int findwallbetweensectors(int sect1, int sect2) -{ - if (sector[sect1].wallnum > sector[sect2].wallnum) - std::swap(sect1, sect2); - - auto const sec = (usectorptr_t)§or[sect1]; - int const last = sec->wallptr + sec->wallnum; - - for (int i = sec->wallptr; i < last; i++) - if (wall[i].nextsector == sect2) - return i; - - return -1; -} - - template void updatesectorneighborz(int32_t const x, int32_t const y, int32_t const z, int* const sectnum, int32_t maxDistance, Inside checker) diff --git a/source/core/gamefuncs.cpp b/source/core/gamefuncs.cpp index a529e8b25..e598fdf8c 100644 --- a/source/core/gamefuncs.cpp +++ b/source/core/gamefuncs.cpp @@ -300,6 +300,21 @@ void checkRotatedWalls() } } +//========================================================================== +// +// check if two sectors share a wall connection +// +//========================================================================== + +bool sectorsConnected(int sect1, int sect2) +{ + for (auto& wal : wallsofsector(sect1)) + { + if (wal.nextsector == sect2) return true; + } + return false; +} + //========================================================================== // // vector serializers diff --git a/source/core/gamefuncs.h b/source/core/gamefuncs.h index fa65f5d77..1d36d1100 100644 --- a/source/core/gamefuncs.h +++ b/source/core/gamefuncs.h @@ -128,6 +128,7 @@ void GetWallSpritePosition(const tspritetype* spr, vec2_t pos, vec2_t* out, bool void GetFlatSpritePosition(const tspritetype* spr, vec2_t pos, vec2_t* out, bool render = false); void GetFlatSpritePosition(const spritetype* spr, vec2_t pos, vec2_t* out, bool render = false); void checkRotatedWalls(); +bool sectorsConnected(int sect1, int sect2); // y is negated so that the orientation is the same as in GZDoom, in order to use its utilities. // The render code should NOT use Build coordinates for anything! @@ -319,11 +320,6 @@ inline double SquareDistToWall(double px, double py, const walltype* wal) return SquareDist(px, py, lx1 + t * (lx2 - lx1), ly1 + t * (ly2 - ly1)); } -inline int inside(int x, int y, sectortype* sect) -{ - return inside(x, y, sectnum(sect)); -} - inline void dragpoint(walltype* pointhighlight, int32_t dax, int32_t day) { dragpoint(wallnum(pointhighlight), dax, day); @@ -344,10 +340,3 @@ inline void updatesectorneighbor(int32_t const x, int32_t const y, sectortype* * updatesectorneighbor(x, y, §no, maxDistance); *sect = sectno < 0? nullptr : §or[sectno]; } - - - -inline int findwallbetweensectors(sectortype* sect1, sectortype* sect2) -{ - return findwallbetweensectors(sectnum(sect1), sectnum(sect2)); -} diff --git a/source/core/rendering/hw_sections.cpp b/source/core/rendering/hw_sections.cpp index 12e55b8af..227b37023 100644 --- a/source/core/rendering/hw_sections.cpp +++ b/source/core/rendering/hw_sections.cpp @@ -59,7 +59,7 @@ void hw_BuildSections() { // Fix maps which do not set their wallptr to the first wall. Lo Wang In Time's map 11 is such a case. int wp = sector[i].wallptr; - while (wp > 0 && wall[wp - 1].nextwall >= 0 && wall[wall[wp - 1].nextwall].nextsector == i) + while (wp > 0 && wall[wp - 1].nextwall >= 0 && wall[wp - 1].nextWall()->nextsector == i) { sector[i].wallptr--; sector[i].wallnum++; diff --git a/source/core/rendering/render.h b/source/core/rendering/render.h index b2ded7db9..f99effc36 100644 --- a/source/core/rendering/render.h +++ b/source/core/rendering/render.h @@ -1,5 +1,6 @@ #pragma once #include "build.h" +#include "gamefuncs.h" class FSerializer; struct IntRect; @@ -56,7 +57,7 @@ inline void mergePortals() { for (unsigned t = 0; t < pt2.targets.Size(); t++) { - if (findwallbetweensectors(pt1.targets[s], pt2.targets[t]) >= 0) + if (sectorsConnected(pt1.targets[s], pt2.targets[t])) { pt1.targets.Append(pt2.targets); pt2.targets.Reset(); diff --git a/source/core/sectorgeometry.cpp b/source/core/sectorgeometry.cpp index 47e973c1e..80ef2d94a 100644 --- a/source/core/sectorgeometry.cpp +++ b/source/core/sectorgeometry.cpp @@ -54,7 +54,7 @@ static FVector3 CalcNormal(sectortype* sector, int plane) { FVector3 pt[3]; - auto wal = &wall[sector->wallptr]; + auto wal = sector->firstWall(); auto wal2 = wal->point2Wall(); pt[0] = { (float)WallStartX(wal), (float)WallStartY(wal), 0 }; diff --git a/source/games/duke/src/premap.cpp b/source/games/duke/src/premap.cpp index 3b621f338..b6d7b0ca2 100644 --- a/source/games/duke/src/premap.cpp +++ b/source/games/duke/src/premap.cpp @@ -892,7 +892,7 @@ static void SpawnPortals() { for (auto& t : pt.targets) { - if (findwallbetweensectors(i, t) >= 0) + if (sectorsConnected(i, t)) { sectp->portalflags = PORTAL_SECTOR_FLOOR; sectp->portalnum = uint8_t(1 ^ (&pt - allPortals.Data())); @@ -909,9 +909,9 @@ static void SpawnPortals() { if (pt.type == PORTAL_SECTOR_FLOOR) { - for (auto& t : pt.targets) + for (auto t : pt.targets) { - if (findwallbetweensectors(i, t) >= 0) + if (sectorsConnected(i, t)) { sectp->portalflags = PORTAL_SECTOR_CEILING; sectp->portalnum = uint8_t(1 ^ (&pt - allPortals.Data())); diff --git a/source/games/exhumed/src/move.cpp b/source/games/exhumed/src/move.cpp index 5989c934c..837b7b4bc 100644 --- a/source/games/exhumed/src/move.cpp +++ b/source/games/exhumed/src/move.cpp @@ -1021,7 +1021,6 @@ void MoveSector(sectortype* pSector, int nAngle, int *nXVel, int *nYVel) } } } - int nNextSector = sectnum(pNextSector); it.Reset(pNextSector); while (auto pActor = it.Next()) {