Skip to content

Commit

Permalink
- rewrote nextsectorneighborzptr with a better parameter interface
Browse files Browse the repository at this point in the history
Still needs to be tested.
  • Loading branch information
coelckers committed Aug 5, 2022
1 parent 8d423fd commit dc8562d
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 91 deletions.
7 changes: 0 additions & 7 deletions source/build/include/build.h
Expand Up @@ -216,13 +216,6 @@ inline constexpr uint32_t uhypsq(int32_t const dx, int32_t const dy)

void rotatepoint(vec2_t const pivot, vec2_t p, int16_t const daang, vec2_t * const p2) ATTRIBUTE((nonnull(4)));

sectortype* nextsectorneighborzptr(sectortype* sectp, int refz, int topbottom, int direction);
inline sectortype* safenextsectorneighborzptr(sectortype* sectp, int refz, int topbottom, int direction)
{
auto sect = nextsectorneighborzptr(sectp, refz, topbottom, direction);
return sect == nullptr ? sectp : sect;
}

int getceilzofslopeptr(const sectortype* sec, int dax, int day) ATTRIBUTE((nonnull(1)));
int getflorzofslopeptr(const sectortype* sec, int dax, int day) ATTRIBUTE((nonnull(1)));
void getzsofslopeptr(const sectortype* sec, int dax, int day, int *ceilz, int *florz) ATTRIBUTE((nonnull(1,4,5)));
Expand Down
32 changes: 0 additions & 32 deletions source/build/src/engine.cpp
Expand Up @@ -299,38 +299,6 @@ int32_t spriteheightofsptr(DCoreActor* spr, int32_t *height, int32_t alsotileyof
return zofs;
}

//
// nextsectorneighborz
//
// -1: ceiling or up
// 1: floor or down
sectortype* nextsectorneighborzptr(sectortype* sectp, int refz, int topbottom, int direction)
{
int nextz = (direction==1) ? INT32_MAX : INT32_MIN;
sectortype* sectortouse = nullptr;

for(auto& wal : wallsofsector(sectp))
{
if (wal.twoSided())
{
auto ns = wal.nextSector();

const int32_t testz = (topbottom == 1) ? ns->floorz : ns->ceilingz;

const int32_t update = (direction == 1) ?
(nextz > testz && testz > refz) :
(nextz < testz && testz < refz);

if (update)
{
nextz = testz;
sectortouse = ns;
}
}
}
return sectortouse;
}


//
// cansee
Expand Down
32 changes: 32 additions & 0 deletions source/core/gamefuncs.cpp
Expand Up @@ -465,6 +465,38 @@ int inside(double x, double y, const sectortype* sect)
return -1;
}

//==========================================================================
//
// find the closest neighboring sector plane in the given direction.
// Does not consider slopes, just like the original!
//
//==========================================================================

sectortype* nextsectorneighborzptr(sectortype* sectp, int startz, int flags)
{
int factor = (flags & Find_Up)? -1 : 1;
int bestz = INT_MAX;
sectortype* bestsec = (flags & Find_Safe)? sectp : nullptr;
const auto planez = (flags & Find_Ceiling)? &sectortype::ceilingz : &sectortype::floorz;

startz *= factor;
for(auto& wal : wallsofsector(sectp))
{
if (wal.twoSided())
{
auto nextsec = wal.nextSector();
auto nextz = factor * nextsec->*planez;

if (startz < nextz && nextz < bestz)
{
bestz = nextz;
bestsec = nextsec;
}
}
}
return bestsec;
}

//==========================================================================
//
//
Expand Down
18 changes: 18 additions & 0 deletions source/core/gamefuncs.h
Expand Up @@ -172,6 +172,24 @@ void dragpoint(walltype* wal, const DVector2& pos);
DVector2 rotatepoint(const DVector2& pivot, const DVector2& point, binangle angle);
int32_t inside(double x, double y, const sectortype* sect);

enum EFindNextSector
{
Find_Floor = 0,
Find_Ceiling = 1,

Find_Down = 0,
Find_Up = 2,

Find_Safe = 4,

Find_CeilingUp = Find_Ceiling | Find_Up,
Find_CeilingDown = Find_Ceiling | Find_Down,
Find_FloorUp = Find_Floor | Find_Up,
Find_FloorDown = Find_Floor | Find_Down,
};
sectortype* nextsectorneighborzptr(sectortype* sectp, int startz, int flags);



// 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!
Expand Down
2 changes: 1 addition & 1 deletion source/games/duke/src/bowling.cpp
Expand Up @@ -74,7 +74,7 @@ int pinsectorresetup(sectortype* sec)

if (j == -1)
{
j = safenextsectorneighborzptr(sec, sec->ceilingz, -1, -1)->ceilingz;
j = nextsectorneighborzptr(sec, sec->ceilingz, Find_CeilingUp | Find_Safe)->ceilingz;
setanimation(sec, anim_ceilingz, sec, j, 64);
return 1;
}
Expand Down
22 changes: 11 additions & 11 deletions source/games/duke/src/sectors.cpp
Expand Up @@ -660,10 +660,10 @@ static void handle_st16(sectortype* sptr, DDukeActor* actor)

if (i == -1)
{
sectp = nextsectorneighborzptr(sptr, sptr->floorz, 1, 1);
sectp = nextsectorneighborzptr(sptr, sptr->floorz, Find_FloorDown);
if (sectp == nullptr)
{
sectp = nextsectorneighborzptr(sptr, sptr->floorz, 1, -1);
sectp = nextsectorneighborzptr(sptr, sptr->floorz, Find_FloorUp);
if (sectp == nullptr) return;
setanimation(sptr, anim_floorz, sptr, sectp->floorz, sptr->extra);
}
Expand All @@ -687,8 +687,8 @@ static void handle_st18(sectortype* sptr, DDukeActor* actor)

if (i == -1)
{
auto sectp = nextsectorneighborzptr(sptr, sptr->floorz, 1, -1);
if (sectp == nullptr) sectp = nextsectorneighborzptr(sptr, sptr->floorz, 1, 1);
auto sectp = nextsectorneighborzptr(sptr, sptr->floorz, Find_FloorUp);
if (sectp == nullptr) sectp = nextsectorneighborzptr(sptr, sptr->floorz, Find_FloorDown);
if (sectp == nullptr) return;
int j = sectp->floorz;
int q = sptr->extra;
Expand All @@ -710,9 +710,9 @@ static void handle_st29(sectortype* sptr, DDukeActor* actor)
int j;

if (sptr->lotag & 0x8000)
j = safenextsectorneighborzptr(sptr, sptr->ceilingz, 1, 1)->floorz;
j = nextsectorneighborzptr(sptr, sptr->ceilingz, Find_FloorDown | Find_Safe)->floorz;
else
j = safenextsectorneighborzptr(sptr, sptr->ceilingz, -1, -1)->ceilingz;
j = nextsectorneighborzptr(sptr, sptr->ceilingz, Find_CeilingUp | Find_Safe)->ceilingz;

DukeStatIterator it(STAT_EFFECTOR);
while (auto act2 = it.Next())
Expand Down Expand Up @@ -762,7 +762,7 @@ static void handle_st20(sectortype* sptr, DDukeActor* actor)
}
else
{
auto sectp = nextsectorneighborzptr(sptr, sptr->ceilingz, -1, -1);
auto sectp = nextsectorneighborzptr(sptr, sptr->ceilingz, Find_CeilingUp);

if (sectp) j = sectp->ceilingz;
else
Expand Down Expand Up @@ -791,14 +791,14 @@ static void handle_st21(sectortype* sptr, DDukeActor* actor)
if (i >= 0)
{
if (animategoal[i] == sptr->ceilingz)
animategoal[i] = safenextsectorneighborzptr(sptr, sptr->ceilingz, 1, 1)->floorz;
animategoal[i] = nextsectorneighborzptr(sptr, sptr->ceilingz, Find_FloorDown | Find_Safe)->floorz;
else animategoal[i] = sptr->ceilingz;
j = animategoal[i];
}
else
{
if (sptr->ceilingz == sptr->floorz)
j = safenextsectorneighborzptr(sptr, sptr->ceilingz, 1, 1)->floorz;
j = nextsectorneighborzptr(sptr, sptr->ceilingz, Find_FloorDown | Find_Safe)->floorz;
else j = sptr->ceilingz;

sptr->lotag ^= 0x8000;
Expand All @@ -825,9 +825,9 @@ static void handle_st22(sectortype* sptr, DDukeActor* actor)
}
else
{
q = safenextsectorneighborzptr(sptr, sptr->floorz, 1, 1)->floorz;
q = nextsectorneighborzptr(sptr, sptr->floorz, Find_FloorDown | Find_Safe)->floorz;
j = setanimation(sptr, anim_floorz, sptr, q, sptr->extra);
q = safenextsectorneighborzptr(sptr, sptr->ceilingz, -1, -1)->ceilingz;
q = nextsectorneighborzptr(sptr, sptr->ceilingz, Find_CeilingUp | Find_Safe)->ceilingz;
j = setanimation(sptr, anim_ceilingz, sptr, q, sptr->extra);
}

Expand Down
4 changes: 2 additions & 2 deletions source/games/duke/src/spawn.cpp
Expand Up @@ -750,8 +750,8 @@ void spawneffector(DDukeActor* actor, TArray<DDukeActor*>* actors)
case SE_17_WARP_ELEVATOR:
{
actor->temp_data[2] = sectp->floorz; //Stopping loc
actor->temp_data[3] = safenextsectorneighborzptr(sectp, sectp->floorz, -1, -1)->ceilingz;
actor->temp_data[4] = safenextsectorneighborzptr(sectp, sectp->ceilingz, 1, 1)->floorz;
actor->temp_data[3] = nextsectorneighborzptr(sectp, sectp->floorz, Find_CeilingUp | Find_Safe)->ceilingz;
actor->temp_data[4] = nextsectorneighborzptr(sectp, sectp->ceilingz, Find_FloorDown | Find_Safe)->floorz;

if (numplayers < 2)
{
Expand Down

0 comments on commit dc8562d

Please sign in to comment.