use validSectorIndex checks where appropriate.

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
This commit is contained in:
Christoph Oelckers 2021-11-09 09:06:54 +01:00
parent ae8e642785
commit 999ec3c95a
36 changed files with 86 additions and 91 deletions

View file

@ -318,7 +318,7 @@ int32_t insertsprite(int16_t sectnum, int16_t statnum)
if (newspritenum >= 0) if (newspritenum >= 0)
{ {
assert((unsigned)sectnum < MAXSECTORS); assert(validSectorIndex(sectnum));
do_insertsprite_at_headofsect(newspritenum, sectnum); do_insertsprite_at_headofsect(newspritenum, sectnum);
Numsprites++; Numsprites++;

View file

@ -39,7 +39,7 @@ bool calcChaseCamPos(int* px, int* py, int* pz, spritetype* pspr, int *psectnum,
short bakcstat; short bakcstat;
int newdist; int newdist;
assert(*psectnum >= 0 && *psectnum < MAXSECTORS); assert(validSectorIndex(*psectnum));
// Calculate new pos to shoot backwards, using averaged values from the big three. // Calculate new pos to shoot backwards, using averaged values from the big three.
int nx = gi->chaseCamX(ang); int nx = gi->chaseCamX(ang);
@ -61,7 +61,7 @@ bool calcChaseCamPos(int* px, int* py, int* pz, spritetype* pspr, int *psectnum,
return false; return false;
} }
assert(*psectnum >= 0 && *psectnum < MAXSECTORS); assert(validSectorIndex(*psectnum));
// If something is in the way, make pp->camera_dist lower if necessary // If something is in the way, make pp->camera_dist lower if necessary
if (abs(nx) + abs(ny) > abs(hx) + abs(hy)) if (abs(nx) + abs(ny) > abs(hx) + abs(hy))

View file

@ -48,7 +48,7 @@ class SectIterator
public: public:
SectIterator(int stat) SectIterator(int stat)
{ {
assert(stat >= 0 && stat < MAXSECTORS); assert(validSectorIndex(stat));
next = headspritesect[stat]; next = headspritesect[stat];
} }
@ -60,7 +60,7 @@ public:
void Reset(int stat) void Reset(int stat)
{ {
assert(stat >= 0 && stat < MAXSECTORS); assert(validSectorIndex(stat));
next = headspritesect[stat]; next = headspritesect[stat];
} }

View file

@ -4173,7 +4173,7 @@ static void actKickObject(DBloodActor* kicker, DBloodActor* kicked)
static void actTouchFloor(DBloodActor* actor, int nSector) static void actTouchFloor(DBloodActor* actor, int nSector)
{ {
assert(actor != nullptr); assert(actor != nullptr);
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
sectortype* pSector = &sector[nSector]; sectortype* pSector = &sector[nSector];
XSECTOR* pXSector = nullptr; XSECTOR* pXSector = nullptr;
if (pSector->extra > 0) pXSector = &xsector[pSector->extra]; if (pSector->extra > 0) pXSector = &xsector[pSector->extra];
@ -4583,7 +4583,7 @@ void actAirDrag(DBloodActor* actor, int a2)
int wind_x = 0; int wind_x = 0;
int wind_y = 0; int wind_y = 0;
int nSector = pSprite->sectnum; int nSector = pSprite->sectnum;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
sectortype* pSector = &sector[nSector]; sectortype* pSector = &sector[nSector];
int nXSector = pSector->extra; int nXSector = pSector->extra;
if (nXSector > 0) if (nXSector > 0)
@ -4616,7 +4616,7 @@ static Collision MoveThing(DBloodActor* actor)
assert(pSprite->type >= kThingBase && pSprite->type < kThingMax); assert(pSprite->type >= kThingBase && pSprite->type < kThingMax);
const THINGINFO* pThingInfo = &thingInfo[pSprite->type - kThingBase]; const THINGINFO* pThingInfo = &thingInfo[pSprite->type - kThingBase];
int nSector = pSprite->sectnum; int nSector = pSprite->sectnum;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
int top, bottom; int top, bottom;
Collision lhit; Collision lhit;
@ -4635,7 +4635,7 @@ static Collision MoveThing(DBloodActor* actor)
assert(nSector >= 0); assert(nSector >= 0);
if (pSprite->sectnum != nSector) if (pSprite->sectnum != nSector)
{ {
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
ChangeActorSect(actor, nSector); ChangeActorSect(actor, nSector);
} }
@ -4659,7 +4659,7 @@ static Collision MoveThing(DBloodActor* actor)
} }
else else
{ {
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
FindSector(pSprite->x, pSprite->y, pSprite->z, &nSector); FindSector(pSprite->x, pSprite->y, pSprite->z, &nSector);
} }
@ -4838,7 +4838,7 @@ void MoveDude(DBloodActor* actor)
int nSector = pSprite->sectnum; int nSector = pSprite->sectnum;
int nAiStateType = (pXSprite->aiState) ? pXSprite->aiState->stateType : -1; int nAiStateType = (pXSprite->aiState) ? pXSprite->aiState->stateType : -1;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
if (actor->xvel || actor->yvel) if (actor->xvel || actor->yvel)
{ {
@ -4934,12 +4934,12 @@ void MoveDude(DBloodActor* actor)
} }
else else
{ {
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
FindSector(pSprite->x, pSprite->y, pSprite->z, &nSector); FindSector(pSprite->x, pSprite->y, pSprite->z, &nSector);
} }
if (pSprite->sectnum != nSector) if (pSprite->sectnum != nSector)
{ {
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
XSECTOR* pXSector; XSECTOR* pXSector;
int nXSector = sector[pSprite->sectnum].extra; int nXSector = sector[pSprite->sectnum].extra;
if (nXSector > 0) if (nXSector > 0)
@ -5447,7 +5447,7 @@ int MoveMissile(DBloodActor* actor)
updatesector(pos.x, pos.y, &nSector); updatesector(pos.x, pos.y, &nSector);
if (nSector >= 0 && nSector != pSprite->sectnum) if (nSector >= 0 && nSector != pSprite->sectnum)
{ {
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
ChangeActorSect(actor, nSector); ChangeActorSect(actor, nSector);
} }
CheckLink(actor); CheckLink(actor);

View file

@ -149,7 +149,7 @@ bool CanMove(DBloodActor* actor, DBloodActor* target, int nAngle, int nRange)
x += MulScale(nRange, Cos(nAngle), 30); x += MulScale(nRange, Cos(nAngle), 30);
y += MulScale(nRange, Sin(nAngle), 30); y += MulScale(nRange, Sin(nAngle), 30);
int nSector = pSprite->sectnum; int nSector = pSprite->sectnum;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
if (!FindSector(x, y, z, &nSector)) if (!FindSector(x, y, z, &nSector))
return false; return false;
int floorZ = getflorzofslope(nSector, x, y); int floorZ = getflorzofslope(nSector, x, y);

View file

@ -279,7 +279,7 @@ static tspritetype *viewAddEffect(spritetype* tsprite, int& spritesortcnt, int n
int y = MulScale(nLen, Sin(nAng), 30); int y = MulScale(nLen, Sin(nAng), 30);
pNSprite->y = pTSprite->y + y; pNSprite->y = pTSprite->y + y;
pNSprite->z = pTSprite->z; pNSprite->z = pTSprite->z;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
FindSector(pNSprite->x, pNSprite->y, pNSprite->z, &nSector); FindSector(pNSprite->x, pNSprite->y, pNSprite->z, &nSector);
pNSprite->sectnum = nSector; pNSprite->sectnum = nSector;
pNSprite->owner = pTSprite->owner; pNSprite->owner = pTSprite->owner;

View file

@ -350,7 +350,7 @@ void EnemyBubble(DBloodActor* actor, int) // 11
void CounterCheck(DBloodActor*, int nSector) // 12 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].type != kSectorCounter) return;
if (sector[nSector].extra <= 0) return; if (sector[nSector].extra <= 0) return;

View file

@ -69,7 +69,7 @@ void dbCrypt(char *pPtr, int nLength, int nKey)
void InsertSpriteSect(int nSprite, int nSector) void InsertSpriteSect(int nSprite, int nSector)
{ {
assert(nSprite >= 0 && nSprite < kMaxSprites); assert(nSprite >= 0 && nSprite < kMaxSprites);
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
int nOther = headspritesect[nSector]; int nOther = headspritesect[nSector];
if (nOther >= 0) if (nOther >= 0)
{ {
@ -91,7 +91,7 @@ void RemoveSpriteSect(int nSprite)
{ {
assert(nSprite >= 0 && nSprite < kMaxSprites); assert(nSprite >= 0 && nSprite < kMaxSprites);
int nSector = sprite[nSprite].sectnum; int nSector = sprite[nSprite].sectnum;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
int nOther = nextspritesect[nSprite]; int nOther = nextspritesect[nSprite];
if (nOther < 0) if (nOther < 0)
{ {
@ -213,7 +213,7 @@ int DeleteSprite(int nSprite)
} }
assert(sprite[nSprite].statnum >= 0 && sprite[nSprite].statnum < kMaxStatus); assert(sprite[nSprite].statnum >= 0 && sprite[nSprite].statnum < kMaxStatus);
RemoveSpriteStat(nSprite); RemoveSpriteStat(nSprite);
assert(sprite[nSprite].sectnum >= 0 && sprite[nSprite].sectnum < kMaxSectors); assert(validSectorIndex(sprite[nSprite].sectnum));
RemoveSpriteSect(nSprite); RemoveSpriteSect(nSprite);
InsertSpriteStat(nSprite, kMaxStatus); InsertSpriteStat(nSprite, kMaxStatus);
#ifdef NOONE_EXTENSIONS #ifdef NOONE_EXTENSIONS
@ -227,8 +227,8 @@ int DeleteSprite(int nSprite)
int ChangeSpriteSect(int nSprite, int nSector) int ChangeSpriteSect(int nSprite, int nSector)
{ {
assert(nSprite >= 0 && nSprite < kMaxSprites); assert(nSprite >= 0 && nSprite < kMaxSprites);
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
assert(sprite[nSprite].sectnum >= 0 && sprite[nSprite].sectnum < kMaxSectors); assert(validSectorIndex(sprite[nSprite].sectnum));
RemoveSpriteSect(nSprite); RemoveSpriteSect(nSprite);
InsertSpriteSect(nSprite, nSector); InsertSpriteSect(nSprite, nSector);
return 0; return 0;
@ -244,7 +244,7 @@ int ChangeSpriteStat(int nSprite, int nStatus)
assert(nSprite >= 0 && nSprite < kMaxSprites); assert(nSprite >= 0 && nSprite < kMaxSprites);
assert(nStatus >= 0 && nStatus < kMaxStatus); assert(nStatus >= 0 && nStatus < kMaxStatus);
assert(sprite[nSprite].statnum >= 0 && sprite[nSprite].statnum < 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); RemoveSpriteStat(nSprite);
InsertSpriteStat(nSprite, nStatus); InsertSpriteStat(nSprite, nStatus);
return 0; return 0;

View file

@ -197,7 +197,7 @@ void CFX::fxProcess(void)
spritetype *pSprite = &actor->s(); spritetype *pSprite = &actor->s();
viewBackupSpriteLoc(actor); viewBackupSpriteLoc(actor);
int nSector = pSprite->sectnum; int nSector = pSprite->sectnum;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
assert(pSprite->type < kFXMax); assert(pSprite->type < kFXMax);
FXDATA *pFXData = &gFXData[pSprite->type]; FXDATA *pFXData = &gFXData[pSprite->type];
actAirDrag(actor, pFXData->drag); actAirDrag(actor, pFXData->drag);
@ -226,7 +226,7 @@ void CFX::fxProcess(void)
} }
if (nSector != pSprite->sectnum) if (nSector != pSprite->sectnum)
{ {
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
ChangeActorSect(actor, nSector); ChangeActorSect(actor, nSector);
} }
} }

View file

@ -44,8 +44,8 @@ HITINFO gHitInfo;
bool AreSectorsNeighbors(int sect1, int sect2) bool AreSectorsNeighbors(int sect1, int sect2)
{ {
assert(sect1 >= 0 && sect1 < kMaxSectors); assert(validSectorIndex(sect1));
assert(sect2 >= 0 && sect2 < kMaxSectors); assert(validSectorIndex(sect2));
if (sector[sect1].wallnum < sector[sect2].wallnum) if (sector[sect1].wallnum < sector[sect2].wallnum)
{ {
for (int i = 0; i < sector[sect1].wallnum; i++) 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) bool FindSector(int nX, int nY, int nZ, int *nSector)
{ {
int32_t nZFloor, nZCeil; int32_t nZFloor, nZCeil;
assert(*nSector >= 0 && *nSector < kMaxSectors); assert(validSectorIndex(*nSector));
if (inside(nX, nY, *nSector)) if (inside(nX, nY, *nSector))
{ {
getzsofslope(*nSector, nX, nY, &nZCeil, &nZFloor); 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) bool FindSector(int nX, int nY, int *nSector)
{ {
assert(*nSector >= 0 && *nSector < kMaxSectors); assert(validSectorIndex(*nSector));
if (inside(nX, nY, *nSector)) if (inside(nX, nY, *nSector))
{ {
return 1; return 1;
@ -405,7 +405,7 @@ int HitScan(DBloodActor *actor, int z, int dx, int dy, int dz, unsigned int nMas
gHitInfo.set(&hitData); gHitInfo.set(&hitData);
hitscangoal.x = hitscangoal.y = 0x1ffffff; hitscangoal.x = hitscangoal.y = 0x1ffffff;
pSprite->cstat = bakCstat; pSprite->cstat = bakCstat;
if (gHitInfo.hitwall >= kMaxWalls || gHitInfo.hitsect >= kMaxSectors) if (gHitInfo.hitwall >= numwalls || gHitInfo.hitsect >= numsectors)
return -1; return -1;
if (gHitInfo.hitactor != nullptr) if (gHitInfo.hitactor != nullptr)
return 3; return 3;
@ -456,7 +456,7 @@ int VectorScan(DBloodActor *actor, int nOffset, int nZOffset, int dx, int dy, in
pSprite->cstat = bakCstat; pSprite->cstat = bakCstat;
while (nNum--) while (nNum--)
{ {
if (gHitInfo.hitwall >= kMaxWalls || gHitInfo.hitsect >= kMaxSectors) if (gHitInfo.hitwall >= numwalls || gHitInfo.hitsect >= numsectors)
return -1; return -1;
if (nRange && approxDist(gHitInfo.hitx - pSprite->x, gHitInfo.hity - pSprite->y) > nRange) if (nRange && approxDist(gHitInfo.hitx - pSprite->x, gHitInfo.hity - pSprite->y) > nRange)
return -1; return -1;

View file

@ -1697,7 +1697,7 @@ void debrisMove(int listIndex)
gPhysSpritesList[listIndex] = nullptr; gPhysSpritesList[listIndex] = nullptr;
return; return;
} }
else if (pSprite->sectnum < 0 || pSprite->sectnum >= kMaxSectors) else if (validSectorIndex(pSprite->sectnum))
{ {
gPhysSpritesList[listIndex] = nullptr; gPhysSpritesList[listIndex] = nullptr;
return; return;
@ -1760,7 +1760,7 @@ void debrisMove(int listIndex)
if (pSprite->sectnum != nSector) if (pSprite->sectnum != nSector)
{ {
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
ChangeActorSect(actor, nSector); ChangeActorSect(actor, nSector);
nSector = pSprite->sectnum; nSector = pSprite->sectnum;
} }
@ -2883,7 +2883,7 @@ void usePropertiesChanger(DBloodActor* sourceactor, int objType, int objIndex, D
pXLower = &aLower->x(); pXLower = &aLower->x();
// must be sure we found exact same upper link // 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); auto aUpper = getUpperLink(i);
if (aUpper == nullptr || aUpper->x().data1 != pXLower->data1) continue; if (aUpper == nullptr || aUpper->x().data1 != pXLower->data1) continue;
@ -3055,7 +3055,7 @@ void useTeleportTarget(DBloodActor* sourceactor, DBloodActor* actor)
if (aLink) if (aLink)
{ {
// must be sure we found exact same upper link // 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); auto aUpper = getUpperLink(i);
if (aUpper == nullptr || aUpper->x().data1 != aLink->x().data1) continue; if (aUpper == nullptr || aUpper->x().data1 != aLink->x().data1) continue;
@ -9187,7 +9187,7 @@ void clampSprite(DBloodActor* actor, int which)
{ {
auto pSprite = &actor->s(); auto pSprite = &actor->s();
int zTop, zBot; int zTop, zBot;
if (pSprite->sectnum >= 0 && pSprite->sectnum < kMaxSectors) if (validSectorIndex(pSprite->sectnum))
{ {
GetSpriteExtents(pSprite, &zTop, &zBot); GetSpriteExtents(pSprite, &zTop, &zBot);
if (which & 0x01) if (which & 0x01)

View file

@ -1678,7 +1678,7 @@ void playerProcess(PLAYER *pPlayer)
nSector = pSprite->sectnum; nSector = pSprite->sectnum;
actDamageSprite(actor, actor, kDamageFall, 500<<4); actDamageSprite(actor, actor, kDamageFall, 500<<4);
} }
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
ChangeActorSect(actor, nSector); ChangeActorSect(actor, nSector);
} }
} }

View file

@ -366,7 +366,7 @@ static void fakeMoveDude(spritetype *pSprite)
int tz = (predict.z-top)/4; int tz = (predict.z-top)/4;
int wd = pSprite->clipdist*4; int wd = pSprite->clipdist*4;
int nSector = predict.sectnum; int nSector = predict.sectnum;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
if (predict.xvel || predict.yvel) if (predict.xvel || predict.yvel)
{ {
if (pPlayer && gNoClip) if (pPlayer && gNoClip)
@ -417,7 +417,7 @@ static void fakeMoveDude(spritetype *pSprite)
} }
if (predict.sectnum != nSector) if (predict.sectnum != nSector)
{ {
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
predict.sectnum = nSector; predict.sectnum = nSector;
} }
bool bUnderwater = 0; bool bUnderwater = 0;
@ -571,7 +571,7 @@ static void fakeActAirDrag(spritetype *, int num)
int xvec = 0; int xvec = 0;
int yvec = 0; int yvec = 0;
int nSector = predict.sectnum; int nSector = predict.sectnum;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
sectortype *pSector = &sector[nSector]; sectortype *pSector = &sector[nSector];
int nXSector = pSector->extra; int nXSector = pSector->extra;
if (nXSector > 0) if (nXSector > 0)

View file

@ -261,7 +261,7 @@ void DoSectorPanning(void)
int nXSector = panList[i]; int nXSector = panList[i];
XSECTOR *pXSector = &xsector[nXSector]; XSECTOR *pXSector = &xsector[nXSector];
int nSector = pXSector->reference; int nSector = pXSector->reference;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
sectortype *pSector = &sector[nSector]; sectortype *pSector = &sector[nSector];
assert(pSector->extra == nXSector); assert(pSector->extra == nXSector);
if (pXSector->panAlways || pXSector->busy) if (pXSector->panAlways || pXSector->busy)

View file

@ -130,7 +130,7 @@ void UpdateCeiling(int nXSector, SEQFRAME* pFrame)
{ {
assert(nXSector > 0 && nXSector < kMaxXSectors); assert(nXSector > 0 && nXSector < kMaxXSectors);
int nSector = xsector[nXSector].reference; int nSector = xsector[nXSector].reference;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
sectortype* pSector = &sector[nSector]; sectortype* pSector = &sector[nSector];
assert(pSector->extra == nXSector); assert(pSector->extra == nXSector);
pSector->ceilingpicnum = seqGetTile(pFrame); pSector->ceilingpicnum = seqGetTile(pFrame);
@ -149,7 +149,7 @@ void UpdateFloor(int nXSector, SEQFRAME* pFrame)
{ {
assert(nXSector > 0 && nXSector < kMaxXSectors); assert(nXSector > 0 && nXSector < kMaxXSectors);
int nSector = xsector[nXSector].reference; int nSector = xsector[nXSector].reference;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
sectortype* pSector = &sector[nSector]; sectortype* pSector = &sector[nSector];
assert(pSector->extra == nXSector); assert(pSector->extra == nXSector);
pSector->floorpicnum = seqGetTile(pFrame); pSector->floorpicnum = seqGetTile(pFrame);

View file

@ -1431,7 +1431,7 @@ void OperateTeleport(unsigned int nSector, XSECTOR *pXSector)
spritetype *pDest = &nDest->s(); spritetype *pDest = &nDest->s();
assert(pDest->statnum == kStatMarker); assert(pDest->statnum == kStatMarker);
assert(pDest->type == kMarkerWarpDest); assert(pDest->type == kMarkerWarpDest);
assert(pDest->sectnum >= 0 && pDest->sectnum < kMaxSectors); assert(validSectorIndex(pDest->sectnum));
BloodSectIterator it(nSector); BloodSectIterator it(nSector);
while (auto actor = it.Next()) while (auto actor = it.Next())
{ {
@ -2022,7 +2022,7 @@ void trInit(void)
pXWall->busy = 65536; pXWall->busy = 65536;
} }
} }
assert((numsectors >= 0) && (numsectors < kMaxSectors));
for (int i = 0; i < numsectors; i++) for (int i = 0; i < numsectors; i++)
{ {
sectortype *pSector = &sector[i]; sectortype *pSector = &sector[i];

View file

@ -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 vZ = FixedToInt(MulScale(zm, 1280, 3))-(16<<8);
int bakCstat = pSprite->cstat; int bakCstat = pSprite->cstat;
pSprite->cstat &= ~256; pSprite->cstat &= ~256;
assert(*vsectnum >= 0 && *vsectnum < kMaxSectors); assert(validSectorIndex(*vsectnum));
FindSector(*pX, *pY, *pZ, vsectnum); FindSector(*pX, *pY, *pZ, vsectnum);
int nHSector; int nHSector;
int hX, hY; 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); int myclock = PlayClock + MulScale(4, smoothratio, 16);
othercameradist = ClipHigh(othercameradist+((myclock-othercameraclock)<<10), 65536); othercameradist = ClipHigh(othercameradist+((myclock-othercameraclock)<<10), 65536);
othercameraclock = myclock; othercameraclock = myclock;
assert(*vsectnum >= 0 && *vsectnum < kMaxSectors); assert(validSectorIndex(*vsectnum));
FindSector(*pX, *pY, *pZ, vsectnum); FindSector(*pX, *pY, *pZ, vsectnum);
pSprite->cstat = bakCstat; pSprite->cstat = bakCstat;
} }

View file

@ -163,7 +163,7 @@ void warpInit(void)
} }
#endif #endif
for (int i = 0; i < kMaxSectors; i++) for (int i = 0; i < numsectors; i++)
{ {
auto actor = getUpperLink(i); auto actor = getUpperLink(i);
if (actor && actor->hasX()) if (actor && actor->hasX())
@ -171,7 +171,7 @@ void warpInit(void)
spritetype *pSprite = &actor->s(); spritetype *pSprite = &actor->s();
XSPRITE *pXSprite = &actor->x(); XSPRITE *pXSprite = &actor->x();
int nLink = pXSprite->data1; int nLink = pXSprite->data1;
for (int j = 0; j < kMaxSectors; j++) for (int j = 0; j < numsectors; j++)
{ {
auto actor2 = getLowerLink(j); auto actor2 = getLowerLink(j);
if (actor2 && actor2->hasX()) if (actor2 && actor2->hasX())
@ -209,7 +209,7 @@ int CheckLink(DBloodActor *actor)
aLower = aUpper->GetOwner(); aLower = aUpper->GetOwner();
assert(aLower); assert(aLower);
spritetype *pLower = &aLower->s(); spritetype *pLower = &aLower->s();
assert(pLower->sectnum >= 0 && pLower->sectnum < kMaxSectors); assert(validSectorIndex(pLower->sectnum));
ChangeActorSect(actor, pLower->sectnum); ChangeActorSect(actor, pLower->sectnum);
pSprite->x += pLower->x-pUpper->x; pSprite->x += pLower->x-pUpper->x;
pSprite->y += pLower->y-pUpper->y; pSprite->y += pLower->y-pUpper->y;
@ -236,7 +236,7 @@ int CheckLink(DBloodActor *actor)
aUpper = aLower->GetOwner(); aUpper = aLower->GetOwner();
assert(aUpper); assert(aUpper);
spritetype *pUpper = &aUpper->s(); spritetype *pUpper = &aUpper->s();
assert(pUpper->sectnum >= 0 && pUpper->sectnum < kMaxSectors); assert(validSectorIndex(pUpper->sectnum));
ChangeActorSect(actor, pUpper->sectnum); ChangeActorSect(actor, pUpper->sectnum);
pSprite->x += pUpper->x-pLower->x; pSprite->x += pUpper->x-pLower->x;
pSprite->y += pUpper->y-pLower->y; pSprite->y += pUpper->y-pLower->y;
@ -270,7 +270,7 @@ int CheckLink(int *x, int *y, int *z, int *nSector)
lower = upper->GetOwner(); lower = upper->GetOwner();
assert(lower); assert(lower);
spritetype *pLower = &lower->s(); spritetype *pLower = &lower->s();
assert(pLower->sectnum >= 0 && pLower->sectnum < kMaxSectors); assert(validSectorIndex(pLower->sectnum));
*nSector = pLower->sectnum; *nSector = pLower->sectnum;
*x += pLower->x-pUpper->x; *x += pLower->x-pUpper->x;
*y += pLower->y-pUpper->y; *y += pLower->y-pUpper->y;
@ -296,7 +296,7 @@ int CheckLink(int *x, int *y, int *z, int *nSector)
upper = lower->GetOwner(); upper = lower->GetOwner();
assert(upper); assert(upper);
spritetype *pUpper = &upper->s(); spritetype *pUpper = &upper->s();
assert(pUpper->sectnum >= 0 && pUpper->sectnum < kMaxSectors); assert(validSectorIndex(pUpper->sectnum));
*nSector = pUpper->sectnum; *nSector = pUpper->sectnum;
*x += pUpper->x-pLower->x; *x += pUpper->x-pLower->x;
*y += pUpper->y-pLower->y; *y += pUpper->y-pLower->y;

View file

@ -2255,7 +2255,7 @@ bool jibs(DDukeActor *actor, int JIBS6, bool timeout, bool callsetsprite, bool f
if (callsetsprite) setsprite(actor, s->pos); if (callsetsprite) setsprite(actor, s->pos);
// this was after the slope calls, but we should avoid calling that for invalid sectors. // 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); deletesprite(actor);
return false; return false;
@ -5300,7 +5300,7 @@ void fall_common(DDukeActor *actor, int playernum, int JIBS6, int DRONE, int BLO
int j = s->sectnum; int j = s->sectnum;
pushmove(&s->pos, &j, 128, (4 << 8), (4 << 8), CLIPMASK0); 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); changeactorsect(actor, j);
S_PlayActorSound(thud, actor); S_PlayActorSound(thud, actor);

View file

@ -3048,7 +3048,7 @@ void moveactors_d(void)
auto s = act->s; auto s = act->s;
sect = s->sectnum; sect = s->sectnum;
if (s->xrepeat == 0 || sect < 0 || sect >= MAXSECTORS) if (s->xrepeat == 0 || !validSectorIndex(sect))
{ {
deletesprite(act); deletesprite(act);
continue; continue;

View file

@ -2854,7 +2854,7 @@ void moveactors_r(void)
auto s = act->s; auto s = act->s;
bool deleteafterexecute = false; // taking a cue here from RedNukem to not run scripts on deleted sprites. 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); deletesprite(act);
continue; continue;

View file

@ -2117,7 +2117,7 @@ int ParseState::parse(void)
break; break;
case concmd_spawn: case concmd_spawn:
insptr++; insptr++;
if(g_sp->sectnum >= 0 && g_sp->sectnum < MAXSECTORS) if(validSectorIndex(g_sp->sectnum))
spawn(g_ac,*insptr); spawn(g_ac,*insptr);
insptr++; insptr++;
break; break;
@ -2151,7 +2151,7 @@ int ParseState::parse(void)
insptr++; insptr++;
bool weap = fi.spawnweapondebris(g_sp->picnum, dnum); 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--) for(j=(*insptr)-1;j>=0;j--)
{ {
if(weap) if(weap)
@ -2862,7 +2862,7 @@ int ParseState::parse(void)
lIn = *insptr++; lIn = *insptr++;
lIn = GetGameVarID(lIn, g_ac, g_p); 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); lReturn = spawn(g_ac, lIn);
SetGameVarID(g_iReturnVarID, ActorToScriptIndex(lReturn), g_ac, g_p); SetGameVarID(g_iReturnVarID, ActorToScriptIndex(lReturn), g_ac, g_p);
@ -2872,7 +2872,7 @@ int ParseState::parse(void)
{ {
DDukeActor* lReturn = nullptr; DDukeActor* lReturn = nullptr;
insptr++; insptr++;
if (g_sp->sectnum >= 0 && g_sp->sectnum < MAXSECTORS) if(validSectorIndex(g_sp->sectnum))
lReturn = spawn(g_ac, *insptr); lReturn = spawn(g_ac, *insptr);
insptr++; insptr++;
SetGameVarID(g_iReturnVarID, ActorToScriptIndex(lReturn), g_ac, g_p); 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; s.killit_flag = 0;
if (actor->s->sectnum < 0 || actor->s->sectnum >= MAXSECTORS) if(!validSectorIndex(actor->s->sectnum))
{ {
deletesprite(actor); deletesprite(actor);
return; return;
@ -3761,7 +3761,7 @@ void execute(DDukeActor *actor,int p,int x)
s.killit_flag = 0; s.killit_flag = 0;
if(actor->s->sectnum < 0 || actor->s->sectnum >= MAXSECTORS) if(!validSectorIndex(actor->s->sectnum))
{ {
if(badguy(actor)) if(badguy(actor))
ps[p].actors_killed++; ps[p].actors_killed++;

View file

@ -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); 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; 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 // Shrinking code

View file

@ -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); 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; 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 // Shrinking code

View file

@ -273,7 +273,6 @@ void displayrooms(int snum, double smoothratio)
videoSetCorrectedAspect(); videoSetCorrectedAspect();
auto sect = p->cursector(); auto sect = p->cursector();
if (sect == nullptr) return;
GlobalMapFog = fogactive ? 0x999999 : 0; GlobalMapFog = fogactive ? 0x999999 : 0;
GlobalFogDensity = fogactive ? 350.f : 0.f; GlobalFogDensity = fogactive ? 350.f : 0.f;

View file

@ -1426,7 +1426,7 @@ void checkhitsprite_d(DDukeActor* targ, DDukeActor* proj)
s->xvel = -(pspr->extra << 2); s->xvel = -(pspr->extra << 2);
j = s->sectnum; j = s->sectnum;
pushmove(&s->pos, &j, 128L, (4 << 8), (4 << 8), CLIPMASK0); 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); changeactorsect(targ, j);
} }

View file

@ -149,7 +149,7 @@ void AIAnim::Tick(RunListEvent* ev)
if (pSpriteB->sectnum != pSprite->sectnum) if (pSpriteB->sectnum != pSprite->sectnum)
{ {
if (pSpriteB->sectnum < 0 || pSpriteB->sectnum >= kMaxSectors) if (!validSectorIndex(pSpriteB))
{ {
DestroyAnim(pActor); DestroyAnim(pActor);
return; return;

View file

@ -174,7 +174,7 @@ void IgniteSprite(DExhumedActor* pActor)
void BulletHitsSprite(Bullet *pBullet, DExhumedActor* pBulletActor, DExhumedActor* pHitActor, int x, int y, int z, int nSector) 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]; bulletInfo *pBulletInfo = &BulletInfo[pBullet->nType];
@ -582,8 +582,6 @@ DExhumedActor* BuildBullet(DExhumedActor* pActor, int nType, int nZOffset, int n
{ {
spritetype *pTargetSprite = &pTarget->s(); spritetype *pTargetSprite = &pTarget->s();
// assert(pTargetSprite->sectnum <= kMaxSectors);
if (pTargetSprite->cstat & 0x101) if (pTargetSprite->cstat & 0x101)
{ {
sBullet.nType = nType; sBullet.nType = nType;
@ -594,7 +592,7 @@ DExhumedActor* BuildBullet(DExhumedActor* pActor, int nType, int nZOffset, int n
int nHeight = GetActorHeight(pTarget); 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); BulletHitsSprite(&sBullet, pActor, pTarget, pTargetSprite->x, pTargetSprite->y, pTargetSprite->z - (nHeight >> 1), pTargetSprite->sectnum);
DeleteActor(sBullet.pActor); DeleteActor(sBullet.pActor);

View file

@ -830,7 +830,7 @@ loc_flag:
auto t = sPlayerInput[nPlayer].pTarget; auto t = sPlayerInput[nPlayer].pTarget;
// only autoaim if target is in front of the player. // only autoaim if target is in front of the player.
auto pTargetSprite = &t->s(); 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 angletotarget = bvectangbam(pTargetSprite->x - pPlayerSprite->x, pTargetSprite->y - pPlayerSprite->y).asbuild();
int anglediff = (pPlayerSprite->ang - angletotarget) & 2047; int anglediff = (pPlayerSprite->ang - angletotarget) & 2047;
if (anglediff < 512 || anglediff > 1536) if (anglediff < 512 || anglediff > 1536)

View file

@ -214,7 +214,7 @@ void InitLights()
void AddFlash(int nSector, int x, int y, int z, int val) void AddFlash(int nSector, int x, int y, int z, int val)
{ {
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
auto sectp = &sector[nSector]; auto sectp = &sector[nSector];
int var_28 = 0; int var_28 = 0;
@ -415,7 +415,7 @@ void UndoFlashes()
{ {
case 0: case 0:
{ {
assert(nIndex >= 0 && nIndex < kMaxSectors); assert(validSectorIndex(nIndex));
pShade = &sector[nIndex].floorshade; pShade = &sector[nIndex].floorshade;
break; break;
@ -431,7 +431,7 @@ void UndoFlashes()
case 2: case 2:
{ {
assert(nIndex >= 0 && nIndex < kMaxSectors); assert(validSectorIndex(nIndex));
pShade = &sector[nIndex].ceilingshade; pShade = &sector[nIndex].ceilingshade;
break; break;

View file

@ -300,7 +300,7 @@ Collision movespritez(DExhumedActor* pActor, int z, int height, int, int clipdis
{ {
spritetype* pSprite = &pActor->s(); spritetype* pSprite = &pActor->s();
int nSector =pSprite->sectnum; int nSector =pSprite->sectnum;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
overridesect = nSector; overridesect = nSector;
short edi = 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))) if ((SectDepth[nSector] != 0) || (edi != nSector && (SectFlag[edi] & kSectUnderwater)))
{ {
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
BuildSplash(pActor, 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 nClipDist = (int8_t)pSprite->clipdist << 2;
int nSector = pSprite->sectnum; int nSector = pSprite->sectnum;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
int floorZ = sector[nSector].floorz; 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); clipmove(&pos, &nSectorB, -xvect, -yvect, 4 * sp->clipdist, 0, 0, CLIPMASK0);
if (nSectorB >= 0 && nSectorB < kMaxSectors && nSectorB != nSector) { if (validSectorIndex(nSectorB)) {
ChangeActorSect(pActor, 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 != nSector || nFloorZ >= pSprite->z)
{ {
if (nSectorB >= 0 && nSectorB < kMaxSectors) { if (validSectorIndex(nSectorB)) {
ChangeActorSect(pActor, nSectorB); ChangeActorSect(pActor, nSectorB);
} }
} }

View file

@ -417,7 +417,7 @@ void BuildTail()
MoveQX[i] = x; MoveQX[i] = x;
MoveQZ[i] = z; MoveQZ[i] = z;
MoveQY[i] = y; MoveQY[i] = y;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
MoveQS[i] = nSector; MoveQS[i] = nSector;
} }
@ -748,7 +748,7 @@ void AIQueenHead::Tick(RunListEvent* ev)
auto pSprite = &pActor->s(); auto pSprite = &pActor->s();
int nSector = pSprite->sectnum; int nSector = pSprite->sectnum;
assert(nSector >= 0 && nSector < kMaxSectors); assert(validSectorIndex(nSector));
short nAction = QueenHead.nAction; short nAction = QueenHead.nAction;
short nHd; short nHd;
@ -941,7 +941,7 @@ void AIQueenHead::Tick(RunListEvent* ev)
auto pTSprite = &pTActor->s(); auto pTSprite = &pTActor->s();
if (var_20 != pTSprite->sectnum) if (var_20 != pTSprite->sectnum)
{ {
assert(var_20 >= 0 && var_20 < kMaxSectors); assert(validSectorIndex(var_20));
ChangeActorSect(pTActor, var_20); ChangeActorSect(pTActor, var_20);
} }

View file

@ -286,7 +286,7 @@ void JS_DrawMirrors(PLAYERp pp, int tx, int ty, int tz, fixed_t tpq16ang, fixed
mirror[cnt].mstate = m_viewon; mirror[cnt].mstate = m_viewon;
// Show teleport destination // 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! // you are outside of it!
if (mirror[cnt].mstate != m_viewon) if (mirror[cnt].mstate != m_viewon)
{ {

View file

@ -611,7 +611,7 @@ void JS_DrawCameras(PLAYERp pp, int tx, int ty, int tz, double smoothratio)
mirror[cnt].mstate = m_viewon; mirror[cnt].mstate = m_viewon;
// Show teleport destination // 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! // you are outside of it!
if (mirror[cnt].mstate == m_viewon) if (mirror[cnt].mstate == m_viewon)
{ {

View file

@ -3285,9 +3285,9 @@ void DoPlayerClimb(PLAYERp pp)
DoPlayerZrange(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; return;
} }

View file

@ -3175,10 +3175,8 @@ int SpawnShrap(DSWActor* parentActor, DSWActor* secondaryActor, int means, BREAK
return 0; return 0;
// Don't spawn shrapnel in invalid sectors gosh dern it! // 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; return 0;
} }