add a pair of shorthand macros to validate an index into an array

Two variations:

IndexOk(idx, array)       validate that idx is a valid index into the array

IndexOkT(idx, array)      validate that idx is a valid index into the
                          array, excluding the final Terminator element
This commit is contained in:
nhmall
2023-12-23 13:46:54 -05:00
parent 415f8c559b
commit e9e05db113
7 changed files with 20 additions and 11 deletions

View File

@@ -1283,7 +1283,7 @@ trapeffect_sqky_board(
}
} else {
seetrap(trap);
if (trap->tnote >= 0 && trap->tnote < SIZE(tsnds)) {
if (IndexOk(trap->tnote, tsnds)) {
Soundeffect(tsnds[trap->tnote], 50);
}
pline("A board beneath you %s%s%s.",
@@ -1300,7 +1300,7 @@ trapeffect_sqky_board(
/* stepped on a squeaky board */
if (in_sight) {
if (!Deaf) {
if (trap->tnote >= 0 && trap->tnote < SIZE(tsnds)) {
if (IndexOk(trap->tnote, tsnds)) {
Soundeffect(tsnds[trap->tnote], 50);
}
pline("A board beneath %s squeaks %s loudly.",
@@ -1315,7 +1315,7 @@ trapeffect_sqky_board(
int range = couldsee(mtmp->mx, mtmp->my) /* 9 or 5 */
? (BOLT_LIM + 1) : (BOLT_LIM - 3);
if (trap->tnote >= 0 && trap->tnote < SIZE(tsnds)) {
if (IndexOk(trap->tnote, tsnds)) {
Soundeffect(tsnds[trap->tnote],
((mdistu(mtmp) <= range * range)
? 40 : 20));