From 999ec3c95a0ba5d966642f0422ec18d8cb4f169b Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 9 Nov 2021 09:06:54 +0100 Subject: [PATCH] use validSectorIndex checks where appropriate. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let’s hope that this sloppiness doesn’t have negative effects with broken maps. # Conflicts: # source/games/sw/src/sprite.cpp # Conflicts: # source/games/duke/src/prediction.cpp # source/games/duke/src/render.cpp --- source/build/src/engine.cpp | 2 +- source/core/gamefuncs.cpp | 4 ++-- source/core/iterators.h | 4 ++-- source/games/blood/src/actor.cpp | 18 +++++++++--------- source/games/blood/src/ai.cpp | 2 +- source/games/blood/src/animatesprite.cpp | 2 +- source/games/blood/src/callback.cpp | 2 +- source/games/blood/src/db.cpp | 12 ++++++------ source/games/blood/src/fx.cpp | 4 ++-- source/games/blood/src/gameutil.cpp | 12 ++++++------ source/games/blood/src/nnexts.cpp | 10 +++++----- source/games/blood/src/player.cpp | 2 +- source/games/blood/src/prediction.cpp | 6 +++--- source/games/blood/src/sectorfx.cpp | 2 +- source/games/blood/src/seq.cpp | 4 ++-- source/games/blood/src/triggers.cpp | 4 ++-- source/games/blood/src/view.cpp | 4 ++-- source/games/blood/src/warp.cpp | 12 ++++++------ source/games/duke/src/actors.cpp | 4 ++-- source/games/duke/src/actors_d.cpp | 2 +- source/games/duke/src/actors_r.cpp | 2 +- source/games/duke/src/gameexec.cpp | 12 ++++++------ source/games/duke/src/player_d.cpp | 2 +- source/games/duke/src/player_r.cpp | 2 +- source/games/duke/src/render.cpp | 1 - source/games/duke/src/sectors_d.cpp | 2 +- source/games/exhumed/src/anims.cpp | 2 +- source/games/exhumed/src/bullet.cpp | 6 ++---- source/games/exhumed/src/gun.cpp | 2 +- source/games/exhumed/src/lighting.cpp | 6 +++--- source/games/exhumed/src/move.cpp | 10 +++++----- source/games/exhumed/src/queen.cpp | 6 +++--- source/games/sw/src/_polymost.cpp | 2 +- source/games/sw/src/jsector.cpp | 2 +- source/games/sw/src/player.cpp | 4 ++-- source/games/sw/src/weapon.cpp | 4 +--- 36 files changed, 86 insertions(+), 91 deletions(-) diff --git a/source/build/src/engine.cpp b/source/build/src/engine.cpp index 4cfd21cba..b67c6fba3 100644 --- a/source/build/src/engine.cpp +++ b/source/build/src/engine.cpp @@ -318,7 +318,7 @@ int32_t insertsprite(int16_t sectnum, int16_t statnum) if (newspritenum >= 0) { - assert((unsigned)sectnum < MAXSECTORS); + assert(validSectorIndex(sectnum)); do_insertsprite_at_headofsect(newspritenum, sectnum); Numsprites++; diff --git a/source/core/gamefuncs.cpp b/source/core/gamefuncs.cpp index 488621870..7144df81c 100644 --- a/source/core/gamefuncs.cpp +++ b/source/core/gamefuncs.cpp @@ -39,7 +39,7 @@ bool calcChaseCamPos(int* px, int* py, int* pz, spritetype* pspr, int *psectnum, short bakcstat; int newdist; - assert(*psectnum >= 0 && *psectnum < MAXSECTORS); + assert(validSectorIndex(*psectnum)); // Calculate new pos to shoot backwards, using averaged values from the big three. int nx = gi->chaseCamX(ang); @@ -61,7 +61,7 @@ bool calcChaseCamPos(int* px, int* py, int* pz, spritetype* pspr, int *psectnum, return false; } - assert(*psectnum >= 0 && *psectnum < MAXSECTORS); + assert(validSectorIndex(*psectnum)); // If something is in the way, make pp->camera_dist lower if necessary if (abs(nx) + abs(ny) > abs(hx) + abs(hy)) diff --git a/source/core/iterators.h b/source/core/iterators.h index 4761b3b75..524b3e7b8 100644 --- a/source/core/iterators.h +++ b/source/core/iterators.h @@ -48,7 +48,7 @@ class SectIterator public: SectIterator(int stat) { - assert(stat >= 0 && stat < MAXSECTORS); + assert(validSectorIndex(stat)); next = headspritesect[stat]; } @@ -60,7 +60,7 @@ public: void Reset(int stat) { - assert(stat >= 0 && stat < MAXSECTORS); + assert(validSectorIndex(stat)); next = headspritesect[stat]; } diff --git a/source/games/blood/src/actor.cpp b/source/games/blood/src/actor.cpp index 0f572910a..6d8df5f49 100644 --- a/source/games/blood/src/actor.cpp +++ b/source/games/blood/src/actor.cpp @@ -4173,7 +4173,7 @@ static void actKickObject(DBloodActor* kicker, DBloodActor* kicked) static void actTouchFloor(DBloodActor* actor, int nSector) { assert(actor != nullptr); - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); sectortype* pSector = §or[nSector]; XSECTOR* pXSector = nullptr; if (pSector->extra > 0) pXSector = &xsector[pSector->extra]; @@ -4583,7 +4583,7 @@ void actAirDrag(DBloodActor* actor, int a2) int wind_x = 0; int wind_y = 0; int nSector = pSprite->sectnum; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); sectortype* pSector = §or[nSector]; int nXSector = pSector->extra; if (nXSector > 0) @@ -4616,7 +4616,7 @@ static Collision MoveThing(DBloodActor* actor) assert(pSprite->type >= kThingBase && pSprite->type < kThingMax); const THINGINFO* pThingInfo = &thingInfo[pSprite->type - kThingBase]; int nSector = pSprite->sectnum; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); int top, bottom; Collision lhit; @@ -4635,7 +4635,7 @@ static Collision MoveThing(DBloodActor* actor) assert(nSector >= 0); if (pSprite->sectnum != nSector) { - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); ChangeActorSect(actor, nSector); } @@ -4659,7 +4659,7 @@ static Collision MoveThing(DBloodActor* actor) } else { - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); FindSector(pSprite->x, pSprite->y, pSprite->z, &nSector); } @@ -4838,7 +4838,7 @@ void MoveDude(DBloodActor* actor) int nSector = pSprite->sectnum; int nAiStateType = (pXSprite->aiState) ? pXSprite->aiState->stateType : -1; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); if (actor->xvel || actor->yvel) { @@ -4934,12 +4934,12 @@ void MoveDude(DBloodActor* actor) } else { - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); FindSector(pSprite->x, pSprite->y, pSprite->z, &nSector); } if (pSprite->sectnum != nSector) { - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); XSECTOR* pXSector; int nXSector = sector[pSprite->sectnum].extra; if (nXSector > 0) @@ -5447,7 +5447,7 @@ int MoveMissile(DBloodActor* actor) updatesector(pos.x, pos.y, &nSector); if (nSector >= 0 && nSector != pSprite->sectnum) { - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); ChangeActorSect(actor, nSector); } CheckLink(actor); diff --git a/source/games/blood/src/ai.cpp b/source/games/blood/src/ai.cpp index bd3cb4ddd..62da347ca 100644 --- a/source/games/blood/src/ai.cpp +++ b/source/games/blood/src/ai.cpp @@ -149,7 +149,7 @@ bool CanMove(DBloodActor* actor, DBloodActor* target, int nAngle, int nRange) x += MulScale(nRange, Cos(nAngle), 30); y += MulScale(nRange, Sin(nAngle), 30); int nSector = pSprite->sectnum; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); if (!FindSector(x, y, z, &nSector)) return false; int floorZ = getflorzofslope(nSector, x, y); diff --git a/source/games/blood/src/animatesprite.cpp b/source/games/blood/src/animatesprite.cpp index 226e7daa9..628b4e137 100644 --- a/source/games/blood/src/animatesprite.cpp +++ b/source/games/blood/src/animatesprite.cpp @@ -279,7 +279,7 @@ static tspritetype *viewAddEffect(spritetype* tsprite, int& spritesortcnt, int n int y = MulScale(nLen, Sin(nAng), 30); pNSprite->y = pTSprite->y + y; pNSprite->z = pTSprite->z; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); FindSector(pNSprite->x, pNSprite->y, pNSprite->z, &nSector); pNSprite->sectnum = nSector; pNSprite->owner = pTSprite->owner; diff --git a/source/games/blood/src/callback.cpp b/source/games/blood/src/callback.cpp index 09383f10b..4dc35bc80 100644 --- a/source/games/blood/src/callback.cpp +++ b/source/games/blood/src/callback.cpp @@ -350,7 +350,7 @@ void EnemyBubble(DBloodActor* actor, int) // 11 void CounterCheck(DBloodActor*, int nSector) // 12 { - if (nSector < 0 || nSector >= kMaxSectors) return; + if (!validSectorIndex(nSector)) return; if (sector[nSector].type != kSectorCounter) return; if (sector[nSector].extra <= 0) return; diff --git a/source/games/blood/src/db.cpp b/source/games/blood/src/db.cpp index 1c0456b3d..796b43df0 100644 --- a/source/games/blood/src/db.cpp +++ b/source/games/blood/src/db.cpp @@ -69,7 +69,7 @@ void dbCrypt(char *pPtr, int nLength, int nKey) void InsertSpriteSect(int nSprite, int nSector) { assert(nSprite >= 0 && nSprite < kMaxSprites); - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); int nOther = headspritesect[nSector]; if (nOther >= 0) { @@ -91,7 +91,7 @@ void RemoveSpriteSect(int nSprite) { assert(nSprite >= 0 && nSprite < kMaxSprites); int nSector = sprite[nSprite].sectnum; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); int nOther = nextspritesect[nSprite]; if (nOther < 0) { @@ -213,7 +213,7 @@ int DeleteSprite(int nSprite) } assert(sprite[nSprite].statnum >= 0 && sprite[nSprite].statnum < kMaxStatus); RemoveSpriteStat(nSprite); - assert(sprite[nSprite].sectnum >= 0 && sprite[nSprite].sectnum < kMaxSectors); + assert(validSectorIndex(sprite[nSprite].sectnum)); RemoveSpriteSect(nSprite); InsertSpriteStat(nSprite, kMaxStatus); #ifdef NOONE_EXTENSIONS @@ -227,8 +227,8 @@ int DeleteSprite(int nSprite) int ChangeSpriteSect(int nSprite, int nSector) { assert(nSprite >= 0 && nSprite < kMaxSprites); - assert(nSector >= 0 && nSector < kMaxSectors); - assert(sprite[nSprite].sectnum >= 0 && sprite[nSprite].sectnum < kMaxSectors); + assert(validSectorIndex(nSector)); + assert(validSectorIndex(sprite[nSprite].sectnum)); RemoveSpriteSect(nSprite); InsertSpriteSect(nSprite, nSector); return 0; @@ -244,7 +244,7 @@ int ChangeSpriteStat(int nSprite, int nStatus) assert(nSprite >= 0 && nSprite < kMaxSprites); assert(nStatus >= 0 && nStatus < kMaxStatus); assert(sprite[nSprite].statnum >= 0 && sprite[nSprite].statnum < kMaxStatus); - assert(sprite[nSprite].sectnum >= 0 && sprite[nSprite].sectnum < kMaxSectors); + assert(validSectorIndex(sprite[nSprite].sectnum)); RemoveSpriteStat(nSprite); InsertSpriteStat(nSprite, nStatus); return 0; diff --git a/source/games/blood/src/fx.cpp b/source/games/blood/src/fx.cpp index 944f482d2..8ea89985d 100644 --- a/source/games/blood/src/fx.cpp +++ b/source/games/blood/src/fx.cpp @@ -197,7 +197,7 @@ void CFX::fxProcess(void) spritetype *pSprite = &actor->s(); viewBackupSpriteLoc(actor); int nSector = pSprite->sectnum; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); assert(pSprite->type < kFXMax); FXDATA *pFXData = &gFXData[pSprite->type]; actAirDrag(actor, pFXData->drag); @@ -226,7 +226,7 @@ void CFX::fxProcess(void) } if (nSector != pSprite->sectnum) { - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); ChangeActorSect(actor, nSector); } } diff --git a/source/games/blood/src/gameutil.cpp b/source/games/blood/src/gameutil.cpp index f6e8c0415..ffc23db59 100644 --- a/source/games/blood/src/gameutil.cpp +++ b/source/games/blood/src/gameutil.cpp @@ -44,8 +44,8 @@ HITINFO gHitInfo; bool AreSectorsNeighbors(int sect1, int sect2) { - assert(sect1 >= 0 && sect1 < kMaxSectors); - assert(sect2 >= 0 && sect2 < kMaxSectors); + assert(validSectorIndex(sect1)); + assert(validSectorIndex(sect2)); if (sector[sect1].wallnum < sector[sect2].wallnum) { for (int i = 0; i < sector[sect1].wallnum; i++) @@ -72,7 +72,7 @@ bool AreSectorsNeighbors(int sect1, int sect2) bool FindSector(int nX, int nY, int nZ, int *nSector) { int32_t nZFloor, nZCeil; - assert(*nSector >= 0 && *nSector < kMaxSectors); + assert(validSectorIndex(*nSector)); if (inside(nX, nY, *nSector)) { getzsofslope(*nSector, nX, nY, &nZCeil, &nZFloor); @@ -112,7 +112,7 @@ bool FindSector(int nX, int nY, int nZ, int *nSector) bool FindSector(int nX, int nY, int *nSector) { - assert(*nSector >= 0 && *nSector < kMaxSectors); + assert(validSectorIndex(*nSector)); if (inside(nX, nY, *nSector)) { return 1; @@ -405,7 +405,7 @@ int HitScan(DBloodActor *actor, int z, int dx, int dy, int dz, unsigned int nMas gHitInfo.set(&hitData); hitscangoal.x = hitscangoal.y = 0x1ffffff; pSprite->cstat = bakCstat; - if (gHitInfo.hitwall >= kMaxWalls || gHitInfo.hitsect >= kMaxSectors) + if (gHitInfo.hitwall >= numwalls || gHitInfo.hitsect >= numsectors) return -1; if (gHitInfo.hitactor != nullptr) return 3; @@ -456,7 +456,7 @@ int VectorScan(DBloodActor *actor, int nOffset, int nZOffset, int dx, int dy, in pSprite->cstat = bakCstat; while (nNum--) { - if (gHitInfo.hitwall >= kMaxWalls || gHitInfo.hitsect >= kMaxSectors) + if (gHitInfo.hitwall >= numwalls || gHitInfo.hitsect >= numsectors) return -1; if (nRange && approxDist(gHitInfo.hitx - pSprite->x, gHitInfo.hity - pSprite->y) > nRange) return -1; diff --git a/source/games/blood/src/nnexts.cpp b/source/games/blood/src/nnexts.cpp index 12817a4c1..baaabd50c 100644 --- a/source/games/blood/src/nnexts.cpp +++ b/source/games/blood/src/nnexts.cpp @@ -1697,7 +1697,7 @@ void debrisMove(int listIndex) gPhysSpritesList[listIndex] = nullptr; return; } - else if (pSprite->sectnum < 0 || pSprite->sectnum >= kMaxSectors) + else if (validSectorIndex(pSprite->sectnum)) { gPhysSpritesList[listIndex] = nullptr; return; @@ -1760,7 +1760,7 @@ void debrisMove(int listIndex) if (pSprite->sectnum != nSector) { - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); ChangeActorSect(actor, nSector); nSector = pSprite->sectnum; } @@ -2883,7 +2883,7 @@ void usePropertiesChanger(DBloodActor* sourceactor, int objType, int objIndex, D pXLower = &aLower->x(); // must be sure we found exact same upper link - for (int i = 0; i < kMaxSectors; i++) + for (int i = 0; i < numsectors; i++) { auto aUpper = getUpperLink(i); if (aUpper == nullptr || aUpper->x().data1 != pXLower->data1) continue; @@ -3055,7 +3055,7 @@ void useTeleportTarget(DBloodActor* sourceactor, DBloodActor* actor) if (aLink) { // must be sure we found exact same upper link - for (int i = 0; i < kMaxSectors; i++) + for (int i = 0; i < numsectors; i++) { auto aUpper = getUpperLink(i); if (aUpper == nullptr || aUpper->x().data1 != aLink->x().data1) continue; @@ -9187,7 +9187,7 @@ void clampSprite(DBloodActor* actor, int which) { auto pSprite = &actor->s(); int zTop, zBot; - if (pSprite->sectnum >= 0 && pSprite->sectnum < kMaxSectors) + if (validSectorIndex(pSprite->sectnum)) { GetSpriteExtents(pSprite, &zTop, &zBot); if (which & 0x01) diff --git a/source/games/blood/src/player.cpp b/source/games/blood/src/player.cpp index 5c3cf2345..4325f34d4 100644 --- a/source/games/blood/src/player.cpp +++ b/source/games/blood/src/player.cpp @@ -1678,7 +1678,7 @@ void playerProcess(PLAYER *pPlayer) nSector = pSprite->sectnum; actDamageSprite(actor, actor, kDamageFall, 500<<4); } - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); ChangeActorSect(actor, nSector); } } diff --git a/source/games/blood/src/prediction.cpp b/source/games/blood/src/prediction.cpp index d5838eee0..581a28609 100644 --- a/source/games/blood/src/prediction.cpp +++ b/source/games/blood/src/prediction.cpp @@ -366,7 +366,7 @@ static void fakeMoveDude(spritetype *pSprite) int tz = (predict.z-top)/4; int wd = pSprite->clipdist*4; int nSector = predict.sectnum; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); if (predict.xvel || predict.yvel) { if (pPlayer && gNoClip) @@ -417,7 +417,7 @@ static void fakeMoveDude(spritetype *pSprite) } if (predict.sectnum != nSector) { - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); predict.sectnum = nSector; } bool bUnderwater = 0; @@ -571,7 +571,7 @@ static void fakeActAirDrag(spritetype *, int num) int xvec = 0; int yvec = 0; int nSector = predict.sectnum; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); sectortype *pSector = §or[nSector]; int nXSector = pSector->extra; if (nXSector > 0) diff --git a/source/games/blood/src/sectorfx.cpp b/source/games/blood/src/sectorfx.cpp index 4718e5d2e..e03222385 100644 --- a/source/games/blood/src/sectorfx.cpp +++ b/source/games/blood/src/sectorfx.cpp @@ -261,7 +261,7 @@ void DoSectorPanning(void) int nXSector = panList[i]; XSECTOR *pXSector = &xsector[nXSector]; int nSector = pXSector->reference; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); sectortype *pSector = §or[nSector]; assert(pSector->extra == nXSector); if (pXSector->panAlways || pXSector->busy) diff --git a/source/games/blood/src/seq.cpp b/source/games/blood/src/seq.cpp index 8a12e9899..7e23e8281 100644 --- a/source/games/blood/src/seq.cpp +++ b/source/games/blood/src/seq.cpp @@ -130,7 +130,7 @@ void UpdateCeiling(int nXSector, SEQFRAME* pFrame) { assert(nXSector > 0 && nXSector < kMaxXSectors); int nSector = xsector[nXSector].reference; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); sectortype* pSector = §or[nSector]; assert(pSector->extra == nXSector); pSector->ceilingpicnum = seqGetTile(pFrame); @@ -149,7 +149,7 @@ void UpdateFloor(int nXSector, SEQFRAME* pFrame) { assert(nXSector > 0 && nXSector < kMaxXSectors); int nSector = xsector[nXSector].reference; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); sectortype* pSector = §or[nSector]; assert(pSector->extra == nXSector); pSector->floorpicnum = seqGetTile(pFrame); diff --git a/source/games/blood/src/triggers.cpp b/source/games/blood/src/triggers.cpp index 8f816562b..80fee85b3 100644 --- a/source/games/blood/src/triggers.cpp +++ b/source/games/blood/src/triggers.cpp @@ -1431,7 +1431,7 @@ void OperateTeleport(unsigned int nSector, XSECTOR *pXSector) spritetype *pDest = &nDest->s(); assert(pDest->statnum == kStatMarker); assert(pDest->type == kMarkerWarpDest); - assert(pDest->sectnum >= 0 && pDest->sectnum < kMaxSectors); + assert(validSectorIndex(pDest->sectnum)); BloodSectIterator it(nSector); while (auto actor = it.Next()) { @@ -2022,7 +2022,7 @@ void trInit(void) pXWall->busy = 65536; } } - assert((numsectors >= 0) && (numsectors < kMaxSectors)); + for (int i = 0; i < numsectors; i++) { sectortype *pSector = §or[i]; diff --git a/source/games/blood/src/view.cpp b/source/games/blood/src/view.cpp index 4636f4ccc..88bd2885f 100644 --- a/source/games/blood/src/view.cpp +++ b/source/games/blood/src/view.cpp @@ -170,7 +170,7 @@ void CalcOtherPosition(spritetype *pSprite, int *pX, int *pY, int *pZ, int *vsec int vZ = FixedToInt(MulScale(zm, 1280, 3))-(16<<8); int bakCstat = pSprite->cstat; pSprite->cstat &= ~256; - assert(*vsectnum >= 0 && *vsectnum < kMaxSectors); + assert(validSectorIndex(*vsectnum)); FindSector(*pX, *pY, *pZ, vsectnum); int nHSector; int hX, hY; @@ -204,7 +204,7 @@ void CalcOtherPosition(spritetype *pSprite, int *pX, int *pY, int *pZ, int *vsec int myclock = PlayClock + MulScale(4, smoothratio, 16); othercameradist = ClipHigh(othercameradist+((myclock-othercameraclock)<<10), 65536); othercameraclock = myclock; - assert(*vsectnum >= 0 && *vsectnum < kMaxSectors); + assert(validSectorIndex(*vsectnum)); FindSector(*pX, *pY, *pZ, vsectnum); pSprite->cstat = bakCstat; } diff --git a/source/games/blood/src/warp.cpp b/source/games/blood/src/warp.cpp index cbea85dd8..4004c5351 100644 --- a/source/games/blood/src/warp.cpp +++ b/source/games/blood/src/warp.cpp @@ -163,7 +163,7 @@ void warpInit(void) } #endif - for (int i = 0; i < kMaxSectors; i++) + for (int i = 0; i < numsectors; i++) { auto actor = getUpperLink(i); if (actor && actor->hasX()) @@ -171,7 +171,7 @@ void warpInit(void) spritetype *pSprite = &actor->s(); XSPRITE *pXSprite = &actor->x(); int nLink = pXSprite->data1; - for (int j = 0; j < kMaxSectors; j++) + for (int j = 0; j < numsectors; j++) { auto actor2 = getLowerLink(j); if (actor2 && actor2->hasX()) @@ -209,7 +209,7 @@ int CheckLink(DBloodActor *actor) aLower = aUpper->GetOwner(); assert(aLower); spritetype *pLower = &aLower->s(); - assert(pLower->sectnum >= 0 && pLower->sectnum < kMaxSectors); + assert(validSectorIndex(pLower->sectnum)); ChangeActorSect(actor, pLower->sectnum); pSprite->x += pLower->x-pUpper->x; pSprite->y += pLower->y-pUpper->y; @@ -236,7 +236,7 @@ int CheckLink(DBloodActor *actor) aUpper = aLower->GetOwner(); assert(aUpper); spritetype *pUpper = &aUpper->s(); - assert(pUpper->sectnum >= 0 && pUpper->sectnum < kMaxSectors); + assert(validSectorIndex(pUpper->sectnum)); ChangeActorSect(actor, pUpper->sectnum); pSprite->x += pUpper->x-pLower->x; pSprite->y += pUpper->y-pLower->y; @@ -270,7 +270,7 @@ int CheckLink(int *x, int *y, int *z, int *nSector) lower = upper->GetOwner(); assert(lower); spritetype *pLower = &lower->s(); - assert(pLower->sectnum >= 0 && pLower->sectnum < kMaxSectors); + assert(validSectorIndex(pLower->sectnum)); *nSector = pLower->sectnum; *x += pLower->x-pUpper->x; *y += pLower->y-pUpper->y; @@ -296,7 +296,7 @@ int CheckLink(int *x, int *y, int *z, int *nSector) upper = lower->GetOwner(); assert(upper); spritetype *pUpper = &upper->s(); - assert(pUpper->sectnum >= 0 && pUpper->sectnum < kMaxSectors); + assert(validSectorIndex(pUpper->sectnum)); *nSector = pUpper->sectnum; *x += pUpper->x-pLower->x; *y += pUpper->y-pLower->y; diff --git a/source/games/duke/src/actors.cpp b/source/games/duke/src/actors.cpp index a59ac34e6..06350d566 100644 --- a/source/games/duke/src/actors.cpp +++ b/source/games/duke/src/actors.cpp @@ -2255,7 +2255,7 @@ bool jibs(DDukeActor *actor, int JIBS6, bool timeout, bool callsetsprite, bool f if (callsetsprite) setsprite(actor, s->pos); // this was after the slope calls, but we should avoid calling that for invalid sectors. - if (s->sectnum < 0 || s->sectnum >= MAXSECTORS) + if (!validSectorIndex(s->sectnum)); { deletesprite(actor); return false; @@ -5300,7 +5300,7 @@ void fall_common(DDukeActor *actor, int playernum, int JIBS6, int DRONE, int BLO int j = s->sectnum; pushmove(&s->pos, &j, 128, (4 << 8), (4 << 8), CLIPMASK0); - if (j != s->sectnum && j >= 0 && j < MAXSECTORS) + if (j != s->sectnum && validSectorIndex(j)) changeactorsect(actor, j); S_PlayActorSound(thud, actor); diff --git a/source/games/duke/src/actors_d.cpp b/source/games/duke/src/actors_d.cpp index 1fa9a92c2..6071d1653 100644 --- a/source/games/duke/src/actors_d.cpp +++ b/source/games/duke/src/actors_d.cpp @@ -3048,7 +3048,7 @@ void moveactors_d(void) auto s = act->s; sect = s->sectnum; - if (s->xrepeat == 0 || sect < 0 || sect >= MAXSECTORS) + if (s->xrepeat == 0 || !validSectorIndex(sect)) { deletesprite(act); continue; diff --git a/source/games/duke/src/actors_r.cpp b/source/games/duke/src/actors_r.cpp index a1f74f476..88a98c075 100644 --- a/source/games/duke/src/actors_r.cpp +++ b/source/games/duke/src/actors_r.cpp @@ -2854,7 +2854,7 @@ void moveactors_r(void) auto s = act->s; bool deleteafterexecute = false; // taking a cue here from RedNukem to not run scripts on deleted sprites. - if( s->xrepeat == 0 || s->sectnum < 0 || s->sectnum >= MAXSECTORS) + if( s->xrepeat == 0 || !validSectorIndex(s->sectnum)) { deletesprite(act); continue; diff --git a/source/games/duke/src/gameexec.cpp b/source/games/duke/src/gameexec.cpp index 3f5f7164c..46673e455 100644 --- a/source/games/duke/src/gameexec.cpp +++ b/source/games/duke/src/gameexec.cpp @@ -2117,7 +2117,7 @@ int ParseState::parse(void) break; case concmd_spawn: insptr++; - if(g_sp->sectnum >= 0 && g_sp->sectnum < MAXSECTORS) + if(validSectorIndex(g_sp->sectnum)) spawn(g_ac,*insptr); insptr++; break; @@ -2151,7 +2151,7 @@ int ParseState::parse(void) insptr++; bool weap = fi.spawnweapondebris(g_sp->picnum, dnum); - if(g_sp->sectnum >= 0 && g_sp->sectnum < MAXSECTORS) + if(validSectorIndex(g_sp->sectnum)) for(j=(*insptr)-1;j>=0;j--) { if(weap) @@ -2862,7 +2862,7 @@ int ParseState::parse(void) lIn = *insptr++; lIn = GetGameVarID(lIn, g_ac, g_p); - if (g_sp->sectnum >= 0 && g_sp->sectnum < MAXSECTORS) + if(validSectorIndex(g_sp->sectnum)) lReturn = spawn(g_ac, lIn); SetGameVarID(g_iReturnVarID, ActorToScriptIndex(lReturn), g_ac, g_p); @@ -2872,7 +2872,7 @@ int ParseState::parse(void) { DDukeActor* lReturn = nullptr; insptr++; - if (g_sp->sectnum >= 0 && g_sp->sectnum < MAXSECTORS) + if(validSectorIndex(g_sp->sectnum)) lReturn = spawn(g_ac, *insptr); insptr++; SetGameVarID(g_iReturnVarID, ActorToScriptIndex(lReturn), g_ac, g_p); @@ -3672,7 +3672,7 @@ void LoadActor(DDukeActor *actor, int p, int x) s.killit_flag = 0; - if (actor->s->sectnum < 0 || actor->s->sectnum >= MAXSECTORS) + if(!validSectorIndex(actor->s->sectnum)) { deletesprite(actor); return; @@ -3761,7 +3761,7 @@ void execute(DDukeActor *actor,int p,int x) s.killit_flag = 0; - if(actor->s->sectnum < 0 || actor->s->sectnum >= MAXSECTORS) + if(!validSectorIndex(actor->s->sectnum)) { if(badguy(actor)) ps[p].actors_killed++; diff --git a/source/games/duke/src/player_d.cpp b/source/games/duke/src/player_d.cpp index e4a81a5a8..a43825f0f 100644 --- a/source/games/duke/src/player_d.cpp +++ b/source/games/duke/src/player_d.cpp @@ -2900,7 +2900,7 @@ void processinput_d(int snum) s->xvel = clamp(ksqrt((p->pos.x - p->bobposx) * (p->pos.x - p->bobposx) + (p->pos.y - p->bobposy) * (p->pos.y - p->bobposy)), 0, 512); if (p->on_ground) p->bobcounter += p->GetActor()->s->xvel >> 1; - p->backuppos(ud.clipping == 0 && (p->cursector()->floorpicnum == MIRROR || p->cursectnum < 0 || p->cursectnum >= MAXSECTORS)); + p->backuppos(ud.clipping == 0 && (p->cursector()->floorpicnum == MIRROR || !validSectorIndex(p->cursectnum))); // Shrinking code diff --git a/source/games/duke/src/player_r.cpp b/source/games/duke/src/player_r.cpp index fe2806a70..3777c7f6a 100644 --- a/source/games/duke/src/player_r.cpp +++ b/source/games/duke/src/player_r.cpp @@ -3597,7 +3597,7 @@ void processinput_r(int snum) s->xvel = clamp(ksqrt((p->pos.x - p->bobposx) * (p->pos.x - p->bobposx) + (p->pos.y - p->bobposy) * (p->pos.y - p->bobposy)), 0, 512); if (p->on_ground) p->bobcounter += p->GetActor()->s->xvel >> 1; - p->backuppos(ud.clipping == 0 && (p->cursector()->floorpicnum == MIRROR || p->cursectnum < 0 || p->cursectnum >= MAXSECTORS)); + p->backuppos(ud.clipping == 0 && (p->cursector()->floorpicnum == MIRROR || !validSectorIndex(p->cursectnum < 0))); // Shrinking code diff --git a/source/games/duke/src/render.cpp b/source/games/duke/src/render.cpp index 17d2f8d85..57c11b523 100644 --- a/source/games/duke/src/render.cpp +++ b/source/games/duke/src/render.cpp @@ -273,7 +273,6 @@ void displayrooms(int snum, double smoothratio) videoSetCorrectedAspect(); auto sect = p->cursector(); - if (sect == nullptr) return; GlobalMapFog = fogactive ? 0x999999 : 0; GlobalFogDensity = fogactive ? 350.f : 0.f; diff --git a/source/games/duke/src/sectors_d.cpp b/source/games/duke/src/sectors_d.cpp index c83891a4f..17f961105 100644 --- a/source/games/duke/src/sectors_d.cpp +++ b/source/games/duke/src/sectors_d.cpp @@ -1426,7 +1426,7 @@ void checkhitsprite_d(DDukeActor* targ, DDukeActor* proj) s->xvel = -(pspr->extra << 2); j = s->sectnum; pushmove(&s->pos, &j, 128L, (4 << 8), (4 << 8), CLIPMASK0); - if (j != s->sectnum && j >= 0 && j < MAXSECTORS) + if (j != s->sectnum && validSectorIndex(j)) changeactorsect(targ, j); } diff --git a/source/games/exhumed/src/anims.cpp b/source/games/exhumed/src/anims.cpp index e8605e685..3e08a8080 100644 --- a/source/games/exhumed/src/anims.cpp +++ b/source/games/exhumed/src/anims.cpp @@ -149,7 +149,7 @@ void AIAnim::Tick(RunListEvent* ev) if (pSpriteB->sectnum != pSprite->sectnum) { - if (pSpriteB->sectnum < 0 || pSpriteB->sectnum >= kMaxSectors) + if (!validSectorIndex(pSpriteB)) { DestroyAnim(pActor); return; diff --git a/source/games/exhumed/src/bullet.cpp b/source/games/exhumed/src/bullet.cpp index 8c48aa32b..735f192fa 100644 --- a/source/games/exhumed/src/bullet.cpp +++ b/source/games/exhumed/src/bullet.cpp @@ -174,7 +174,7 @@ void IgniteSprite(DExhumedActor* pActor) void BulletHitsSprite(Bullet *pBullet, DExhumedActor* pBulletActor, DExhumedActor* pHitActor, int x, int y, int z, int nSector) { - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); bulletInfo *pBulletInfo = &BulletInfo[pBullet->nType]; @@ -582,8 +582,6 @@ DExhumedActor* BuildBullet(DExhumedActor* pActor, int nType, int nZOffset, int n { spritetype *pTargetSprite = &pTarget->s(); -// assert(pTargetSprite->sectnum <= kMaxSectors); - if (pTargetSprite->cstat & 0x101) { sBullet.nType = nType; @@ -594,7 +592,7 @@ DExhumedActor* BuildBullet(DExhumedActor* pActor, int nType, int nZOffset, int n int nHeight = GetActorHeight(pTarget); - assert(pTargetSprite->sectnum >= 0 && pTargetSprite->sectnum < kMaxSectors); + assert(validSectorIndex(pTargetSprite->sectnum)); BulletHitsSprite(&sBullet, pActor, pTarget, pTargetSprite->x, pTargetSprite->y, pTargetSprite->z - (nHeight >> 1), pTargetSprite->sectnum); DeleteActor(sBullet.pActor); diff --git a/source/games/exhumed/src/gun.cpp b/source/games/exhumed/src/gun.cpp index 50ebdf787..51a078ff6 100644 --- a/source/games/exhumed/src/gun.cpp +++ b/source/games/exhumed/src/gun.cpp @@ -830,7 +830,7 @@ loc_flag: auto t = sPlayerInput[nPlayer].pTarget; // only autoaim if target is in front of the player. auto pTargetSprite = &t->s(); - assert(pTargetSprite->sectnum < kMaxSectors); + assert(validSectorIndex(pTargetSprite->sectnum)); int angletotarget = bvectangbam(pTargetSprite->x - pPlayerSprite->x, pTargetSprite->y - pPlayerSprite->y).asbuild(); int anglediff = (pPlayerSprite->ang - angletotarget) & 2047; if (anglediff < 512 || anglediff > 1536) diff --git a/source/games/exhumed/src/lighting.cpp b/source/games/exhumed/src/lighting.cpp index fbdb7da7d..e24e4c788 100644 --- a/source/games/exhumed/src/lighting.cpp +++ b/source/games/exhumed/src/lighting.cpp @@ -214,7 +214,7 @@ void InitLights() void AddFlash(int nSector, int x, int y, int z, int val) { - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); auto sectp = §or[nSector]; int var_28 = 0; @@ -415,7 +415,7 @@ void UndoFlashes() { case 0: { - assert(nIndex >= 0 && nIndex < kMaxSectors); + assert(validSectorIndex(nIndex)); pShade = §or[nIndex].floorshade; break; @@ -431,7 +431,7 @@ void UndoFlashes() case 2: { - assert(nIndex >= 0 && nIndex < kMaxSectors); + assert(validSectorIndex(nIndex)); pShade = §or[nIndex].ceilingshade; break; diff --git a/source/games/exhumed/src/move.cpp b/source/games/exhumed/src/move.cpp index 0444acdb9..b5980356a 100644 --- a/source/games/exhumed/src/move.cpp +++ b/source/games/exhumed/src/move.cpp @@ -300,7 +300,7 @@ Collision movespritez(DExhumedActor* pActor, int z, int height, int, int clipdis { spritetype* pSprite = &pActor->s(); int nSector =pSprite->sectnum; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); overridesect = nSector; short edi = nSector; @@ -457,7 +457,7 @@ Collision movespritez(DExhumedActor* pActor, int z, int height, int, int clipdis { if ((SectDepth[nSector] != 0) || (edi != nSector && (SectFlag[edi] & kSectUnderwater))) { - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); BuildSplash(pActor, nSector); } } @@ -499,7 +499,7 @@ Collision movesprite(DExhumedActor* pActor, int dx, int dy, int dz, int ceildist int nClipDist = (int8_t)pSprite->clipdist << 2; int nSector = pSprite->sectnum; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); int floorZ = sector[nSector].floorz; @@ -1015,7 +1015,7 @@ void MoveSector(int nSector, int nAngle, int *nXVel, int *nYVel) clipmove(&pos, &nSectorB, -xvect, -yvect, 4 * sp->clipdist, 0, 0, CLIPMASK0); - if (nSectorB >= 0 && nSectorB < kMaxSectors && nSectorB != nSector) { + if (validSectorIndex(nSectorB)) { ChangeActorSect(pActor, nSectorB); } } @@ -1041,7 +1041,7 @@ void MoveSector(int nSector, int nAngle, int *nXVel, int *nYVel) { if (nSectorB != nSector || nFloorZ >= pSprite->z) { - if (nSectorB >= 0 && nSectorB < kMaxSectors) { + if (validSectorIndex(nSectorB)) { ChangeActorSect(pActor, nSectorB); } } diff --git a/source/games/exhumed/src/queen.cpp b/source/games/exhumed/src/queen.cpp index dae2a15cf..f251cc598 100644 --- a/source/games/exhumed/src/queen.cpp +++ b/source/games/exhumed/src/queen.cpp @@ -417,7 +417,7 @@ void BuildTail() MoveQX[i] = x; MoveQZ[i] = z; MoveQY[i] = y; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); MoveQS[i] = nSector; } @@ -748,7 +748,7 @@ void AIQueenHead::Tick(RunListEvent* ev) auto pSprite = &pActor->s(); int nSector = pSprite->sectnum; - assert(nSector >= 0 && nSector < kMaxSectors); + assert(validSectorIndex(nSector)); short nAction = QueenHead.nAction; short nHd; @@ -941,7 +941,7 @@ void AIQueenHead::Tick(RunListEvent* ev) auto pTSprite = &pTActor->s(); if (var_20 != pTSprite->sectnum) { - assert(var_20 >= 0 && var_20 < kMaxSectors); + assert(validSectorIndex(var_20)); ChangeActorSect(pTActor, var_20); } diff --git a/source/games/sw/src/_polymost.cpp b/source/games/sw/src/_polymost.cpp index 03b6b7f92..58429dfda 100644 --- a/source/games/sw/src/_polymost.cpp +++ b/source/games/sw/src/_polymost.cpp @@ -286,7 +286,7 @@ void JS_DrawMirrors(PLAYERp pp, int tx, int ty, int tz, fixed_t tpq16ang, fixed mirror[cnt].mstate = m_viewon; // Show teleport destination - // NOTE: Adding MAXSECTORS lets you draw a room, even if + // NOTE: Adding true lets you draw a room, even if // you are outside of it! if (mirror[cnt].mstate != m_viewon) { diff --git a/source/games/sw/src/jsector.cpp b/source/games/sw/src/jsector.cpp index 87fdad233..be1527ee8 100644 --- a/source/games/sw/src/jsector.cpp +++ b/source/games/sw/src/jsector.cpp @@ -611,7 +611,7 @@ void JS_DrawCameras(PLAYERp pp, int tx, int ty, int tz, double smoothratio) mirror[cnt].mstate = m_viewon; // Show teleport destination - // NOTE: Adding MAXSECTORS lets you draw a room, even if + // NOTE: Adding true lets you draw a room, even if // you are outside of it! if (mirror[cnt].mstate == m_viewon) { diff --git a/source/games/sw/src/player.cpp b/source/games/sw/src/player.cpp index d147a7db1..2ffe91af0 100644 --- a/source/games/sw/src/player.cpp +++ b/source/games/sw/src/player.cpp @@ -3285,9 +3285,9 @@ void DoPlayerClimb(PLAYERp pp) DoPlayerZrange(pp); - if (pp->LadderSector < 0 || pp->LadderSector > MAXSECTORS) + if (!validSectorIndex(pp->LadderSector)) { - Printf("Bad ladder sector!\n"); + Printf("Bad ladder sector %d!\n", pp->LadderSector); return; } diff --git a/source/games/sw/src/weapon.cpp b/source/games/sw/src/weapon.cpp index 9de693c57..8cae78d2b 100644 --- a/source/games/sw/src/weapon.cpp +++ b/source/games/sw/src/weapon.cpp @@ -3175,10 +3175,8 @@ int SpawnShrap(DSWActor* parentActor, DSWActor* secondaryActor, int means, BREAK return 0; // Don't spawn shrapnel in invalid sectors gosh dern it! - if (parent->sectnum < 0 || parent->sectnum >= MAXSECTORS) + if (!validSectorIndex(parent->sectnum)) { - //DSPRINTF(ds,"SpawnShrap: Invalid sector %d, picnum=%d\n",parent->sectnum,parent->picnum); - MONO_PRINT(ds); return 0; }