Use explicit INBOUNDS_VEC() instead of checking sentinel -1

It's better to do INBOUNDS_VEC(i, obj.entities) instead of 'i > -1'.

'i > -1' is used in cases like obj.getplayer(), which COULD return a
sentinel value of -1 and so correct code will have to check that value.
However, I am now of the opinion that INBOUNDS_VEC() should be used and
isn't unnecessary.

Consider the case of the face() script command: it's not enough to check
i > -1, you should read the routine carefully. Because if you look
closely, you'll see that it's not guaranteed that 'i' will be initialized
at all in that command. Indeed, if you call face() with invalid
arguments, it won't be. And so, 'i' could be something like 215, and
that would index out-of-bounds, and that wouldn't be good. Therefore,
it's better to have the full bounds check instead of checking only one
bounds. Many commands are like this, after some searching I can also
name position(), changemood(), changetile(), changegravity(), etc.

It also makes the code more explicit. Now you don't have to wonder what
-1 means or why it's being checked, you can just read the 'INBOUNDS' and
go "oh, that checks if it's actually inbounds or not".
This commit is contained in:
Misa
2020-09-09 04:15:14 -07:00
committed by Ethan Lee
parent b925352864
commit b34be3f1ac
7 changed files with 236 additions and 235 deletions

View File

@@ -1677,7 +1677,7 @@ void gamerender()
}
float act_alpha = graphics.lerp(game.prev_act_fade, game.act_fade) / 10.0f;
if (game.activeactivity > -1)
if (INBOUNDS_VEC(game.activeactivity, obj.entities))
{
game.activity_lastprompt = obj.blocks[game.activeactivity].prompt;
game.activity_r = obj.blocks[game.activeactivity].r;