mirror of
https://github.com/TerryCavanagh/VVVVVV.git
synced 2026-01-29 17:38:16 +03:00
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:
@@ -829,7 +829,7 @@ void mapclass::resetplayer()
|
||||
|
||||
game.deathseq = -1;
|
||||
int i = obj.getplayer();
|
||||
if(i>-1)
|
||||
if(INBOUNDS_VEC(i, obj.entities))
|
||||
{
|
||||
obj.entities[i].vx = 0;
|
||||
obj.entities[i].vy = 0;
|
||||
@@ -1097,7 +1097,7 @@ void mapclass::gotoroom(int rx, int ry)
|
||||
//continuations!
|
||||
|
||||
temp = obj.getplayer();
|
||||
if(temp>-1)
|
||||
if(INBOUNDS_VEC(temp, obj.entities))
|
||||
{
|
||||
obj.entities[temp].oldxp = obj.entities[temp].xp - int(obj.entities[temp].vx);
|
||||
obj.entities[temp].oldyp = obj.entities[temp].yp - int(obj.entities[temp].vy);
|
||||
@@ -1262,7 +1262,7 @@ void mapclass::loadlevel(int rx, int ry)
|
||||
{
|
||||
//entered from ground floor
|
||||
int player = obj.getplayer();
|
||||
if (player > -1)
|
||||
if (INBOUNDS_VEC(player, obj.entities))
|
||||
{
|
||||
obj.entities[player].yp += (671 * 8);
|
||||
}
|
||||
@@ -1494,7 +1494,7 @@ void mapclass::loadlevel(int rx, int ry)
|
||||
tower.loadminitower1();
|
||||
|
||||
int i = obj.getplayer();
|
||||
if (i > -1)
|
||||
if (INBOUNDS_VEC(i, obj.entities))
|
||||
{
|
||||
obj.entities[i].yp += (71 * 8);
|
||||
}
|
||||
@@ -1539,7 +1539,7 @@ void mapclass::loadlevel(int rx, int ry)
|
||||
obj.createentity(72, 156, 11, 200); // (horizontal gravity line)
|
||||
|
||||
int i = obj.getplayer();
|
||||
if (i > -1)
|
||||
if (INBOUNDS_VEC(i, obj.entities))
|
||||
{
|
||||
obj.entities[i].yp += (71 * 8);
|
||||
}
|
||||
@@ -2098,8 +2098,8 @@ void mapclass::twoframedelayfix()
|
||||
|| !custommode
|
||||
|| game.deathseq != -1
|
||||
// obj.checktrigger() sets obj.activetrigger and block_idx
|
||||
|| obj.checktrigger(&block_idx) <= -1
|
||||
|| block_idx <= -1
|
||||
|| !INBOUNDS_VEC(obj.checktrigger(&block_idx), obj.entities)
|
||||
|| !INBOUNDS_VEC(block_idx, obj.blocks)
|
||||
|| obj.activetrigger < 300)
|
||||
{
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user