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

@@ -289,7 +289,7 @@ void gamelogic()
obj.upsetmode = true;
//change player to sad
int i = obj.getplayer();
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
obj.entities[i].tile = 144;
}
@@ -306,7 +306,7 @@ void gamelogic()
obj.upsetmode = false;
//change player to happy
int i = obj.getplayer();
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
obj.entities[i].tile = 0;
}
@@ -357,7 +357,7 @@ void gamelogic()
else if (map.cameramode == 4)
{
int i = obj.getplayer();
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
map.cameraseek = map.ypos - (obj.entities[i].yp - 120);
}
@@ -378,14 +378,14 @@ void gamelogic()
{
int i = obj.getplayer();
map.ypos -= map.cameraseek;
if (map.cameraseek > 0 && i > -1)
if (map.cameraseek > 0 && INBOUNDS_VEC(i, obj.entities))
{
if (map.ypos < obj.entities[i].yp - 120)
{
map.ypos = obj.entities[i].yp - 120;
}
}
else if (i > -1)
else if (INBOUNDS_VEC(i, obj.entities))
{
if (map.ypos > obj.entities[i].yp - 120)
{
@@ -398,7 +398,7 @@ void gamelogic()
else
{
int i = obj.getplayer();
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
map.ypos = obj.entities[i].yp - 120;
}
@@ -649,7 +649,7 @@ void gamelogic()
if (game.roomx == 41 + game.scmprogress) //he's in the same room
{
int i = obj.getplayer();
if (i > -1 && obj.entities[i].ax > 0 && obj.entities[i].xp > 280)
if (INBOUNDS_VEC(i, obj.entities) && obj.entities[i].ax > 0 && obj.entities[i].xp > 280)
{
obj.entities[i].ax = 0;
obj.entities[i].dir = 0;
@@ -863,7 +863,7 @@ void gamelogic()
{
game.timetrialparlost = true;
int i = obj.getplayer();
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
obj.entities[i].tile = 144;
}
@@ -924,7 +924,7 @@ void gamelogic()
//is the player standing on a moving platform?
int i = obj.getplayer();
float j = obj.entitycollideplatformfloor(i);
if (i > -1 && j > -1000)
if (INBOUNDS_VEC(i, obj.entities) && j > -1000)
{
obj.entities[i].newxp = obj.entities[i].xp + j;
obj.entitymapcollision(i);
@@ -932,7 +932,7 @@ void gamelogic()
else
{
j = obj.entitycollideplatformroof(i);
if (i > -1 && j > -1000)
if (INBOUNDS_VEC(i, obj.entities) && j > -1000)
{
obj.entities[i].newxp = obj.entities[i].xp + j;
obj.entitymapcollision(i);
@@ -957,7 +957,7 @@ void gamelogic()
{
//special for tower: is the player touching any spike blocks?
int player = obj.getplayer();
if(player > -1 && obj.checktowerspikes(player) && graphics.fademode==0)
if(INBOUNDS_VEC(player, obj.entities) && obj.checktowerspikes(player) && graphics.fademode==0)
{
game.deathseq = 30;
}
@@ -966,7 +966,7 @@ void gamelogic()
if(map.towermode && game.lifeseq==0)
{
int player = obj.getplayer();
if(!map.invincibility && player > -1)
if(!map.invincibility && INBOUNDS_VEC(player, obj.entities))
{
if (obj.entities[player].yp-map.ypos <= 0)
{
@@ -977,7 +977,7 @@ void gamelogic()
game.deathseq = 30;
}
}
else if (player > -1)
else if (INBOUNDS_VEC(player, obj.entities))
{
if (obj.entities[player].yp-map.ypos <= 0)
{
@@ -993,7 +993,7 @@ void gamelogic()
}
}
if (player > -1 && obj.entities[player].yp - map.ypos <= 40)
if (INBOUNDS_VEC(player, obj.entities) && obj.entities[player].yp - map.ypos <= 40)
{
map.spikeleveltop++;
if (map.spikeleveltop >= 8) map.spikeleveltop = 8;
@@ -1003,7 +1003,7 @@ void gamelogic()
if (map.spikeleveltop > 0) map.spikeleveltop--;
}
if (player > -1 && obj.entities[player].yp - map.ypos >= 164)
if (INBOUNDS_VEC(player, obj.entities) && obj.entities[player].yp - map.ypos >= 164)
{
map.spikelevelbottom++;
if (map.spikelevelbottom >= 8) map.spikelevelbottom = 8;
@@ -1026,7 +1026,7 @@ void gamelogic()
obj.customwarpmodevon = false;
int i = obj.getplayer();
if (i > -1 && ((game.door_down > -2 && obj.entities[i].yp >= 226-16) || (game.door_up > -2 && obj.entities[i].yp < -2+16) || (game.door_left > -2 && obj.entities[i].xp < -14+16) || (game.door_right > -2 && obj.entities[i].xp >= 308-16))){
if (INBOUNDS_VEC(i, obj.entities) && ((game.door_down > -2 && obj.entities[i].yp >= 226-16) || (game.door_up > -2 && obj.entities[i].yp < -2+16) || (game.door_left > -2 && obj.entities[i].xp < -14+16) || (game.door_right > -2 && obj.entities[i].xp >= 308-16))){
//Player is leaving room
obj.customwarplinecheck(i);
}
@@ -1132,13 +1132,13 @@ void gamelogic()
{
//Normal! Just change room
int player = obj.getplayer();
if (player > -1 && game.door_down > -2 && obj.entities[player].yp >= 238)
if (INBOUNDS_VEC(player, obj.entities) && game.door_down > -2 && obj.entities[player].yp >= 238)
{
obj.entities[player].yp -= 240;
map.gotoroom(game.roomx, game.roomy + 1);
screen_transition = true;
}
if (player > -1 && game.door_up > -2 && obj.entities[player].yp < -2)
if (INBOUNDS_VEC(player, obj.entities) && game.door_up > -2 && obj.entities[player].yp < -2)
{
obj.entities[player].yp += 240;
map.gotoroom(game.roomx, game.roomy - 1);
@@ -1150,13 +1150,13 @@ void gamelogic()
{
//Normal! Just change room
int player = obj.getplayer();
if (player > -1 && game.door_left > -2 && obj.entities[player].xp < -14)
if (INBOUNDS_VEC(player, obj.entities) && game.door_left > -2 && obj.entities[player].xp < -14)
{
obj.entities[player].xp += 320;
map.gotoroom(game.roomx - 1, game.roomy);
screen_transition = true;
}
if (player > -1 && game.door_right > -2 && obj.entities[player].xp >= 308)
if (INBOUNDS_VEC(player, obj.entities) && game.door_right > -2 && obj.entities[player].xp >= 308)
{
obj.entities[player].xp -= 320;
map.gotoroom(game.roomx + 1, game.roomy);
@@ -1171,12 +1171,12 @@ void gamelogic()
{
//This is minitower 1!
int player = obj.getplayer();
if (player > -1 && game.door_left > -2 && obj.entities[player].xp < -14)
if (INBOUNDS_VEC(player, obj.entities) && game.door_left > -2 && obj.entities[player].xp < -14)
{
obj.entities[player].xp += 320;
map.gotoroom(48, 52);
}
if (player > -1 && game.door_right > -2 && obj.entities[player].xp >= 308)
if (INBOUNDS_VEC(player, obj.entities) && game.door_right > -2 && obj.entities[player].xp >= 308)
{
obj.entities[player].xp -= 320;
obj.entities[player].yp -= (71*8);
@@ -1187,7 +1187,7 @@ void gamelogic()
{
//This is minitower 2!
int player = obj.getplayer();
if (player > -1 && game.door_left > -2 && obj.entities[player].xp < -14)
if (INBOUNDS_VEC(player, obj.entities) && game.door_left > -2 && obj.entities[player].xp < -14)
{
if (obj.entities[player].yp > 300)
{
@@ -1201,7 +1201,7 @@ void gamelogic()
map.gotoroom(50, 53);
}
}
if (player > -1 && game.door_right > -2 && obj.entities[player].xp >= 308)
if (INBOUNDS_VEC(player, obj.entities) && game.door_right > -2 && obj.entities[player].xp >= 308)
{
obj.entities[player].xp -= 320;
map.gotoroom(52, 53);
@@ -1231,13 +1231,13 @@ void gamelogic()
{
//Do not wrap! Instead, go to the correct room
int player = obj.getplayer();
if (player > -1 && game.door_left > -2 && obj.entities[player].xp < -14)
if (INBOUNDS_VEC(player, obj.entities) && game.door_left > -2 && obj.entities[player].xp < -14)
{
obj.entities[player].xp += 320;
obj.entities[player].yp -= (671 * 8);
map.gotoroom(108, 109);
}
if (player > -1 && game.door_right > -2 && obj.entities[player].xp >= 308)
if (INBOUNDS_VEC(player, obj.entities) && game.door_right > -2 && obj.entities[player].xp >= 308)
{
obj.entities[player].xp -= 320;
map.gotoroom(110, 104);
@@ -1270,7 +1270,7 @@ void gamelogic()
if (game.roomx == 117 && game.roomy == 102)
{
int i = obj.getplayer();
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
obj.entities[i].yp = 225;
}
@@ -1280,7 +1280,7 @@ void gamelogic()
else if (game.roomx == 119 && game.roomy == 100)
{
int i = obj.getplayer();
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
obj.entities[i].yp = 225;
}
@@ -1290,7 +1290,7 @@ void gamelogic()
else if (game.roomx == 119 && game.roomy == 103)
{
int i = obj.getplayer();
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
obj.entities[i].xp = 0;
}
@@ -1300,7 +1300,7 @@ void gamelogic()
else if (game.roomx == 116 && game.roomy == 103)
{
int i = obj.getplayer();
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
obj.entities[i].yp = 225;
}
@@ -1310,7 +1310,7 @@ void gamelogic()
else if (game.roomx == 116 && game.roomy == 100)
{
int i = obj.getplayer();
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
obj.entities[i].xp = 0;
}
@@ -1320,7 +1320,7 @@ void gamelogic()
else if (game.roomx == 114 && game.roomy == 102)
{
int i = obj.getplayer();
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
obj.entities[i].yp = 225;
}
@@ -1407,7 +1407,7 @@ void gamelogic()
//We've changed room? Let's bring our companion along!
game.roomchange = false;
int i = obj.getplayer();
if (game.companion > 0 && i > -1)
if (game.companion > 0 && INBOUNDS_VEC(i, obj.entities))
{
//ok, we'll presume our companion has been destroyed in the room change. So:
switch(game.companion)
@@ -1416,7 +1416,7 @@ void gamelogic()
{
obj.createentity(obj.entities[i].xp, 121.0f, 15.0f,1); //Y=121, the floor in that particular place!
int j = obj.getcompanion();
if (j > -1)
if (INBOUNDS_VEC(j, obj.entities))
{
obj.entities[j].vx = obj.entities[i].vx;
obj.entities[j].dir = obj.entities[i].dir;
@@ -1435,7 +1435,7 @@ void gamelogic()
obj.createentity(obj.entities[i].xp, 86.0f, 16.0f, 1); //Y=86, the ROOF in that particular place!
}
int j = obj.getcompanion();
if (j > -1)
if (INBOUNDS_VEC(j, obj.entities))
{
obj.entities[j].vx = obj.entities[i].vx;
obj.entities[j].dir = obj.entities[i].dir;
@@ -1449,7 +1449,7 @@ void gamelogic()
{
obj.createentity(310, 177, 17, 1);
int j = obj.getcompanion();
if (j > -1)
if (INBOUNDS_VEC(j, obj.entities))
{
obj.entities[j].vx = obj.entities[i].vx;
obj.entities[j].dir = obj.entities[i].dir;
@@ -1459,7 +1459,7 @@ void gamelogic()
{
obj.createentity(obj.entities[i].xp, 177.0f, 17.0f, 1);
int j = obj.getcompanion();
if (j > -1)
if (INBOUNDS_VEC(j, obj.entities))
{
obj.entities[j].vx = obj.entities[i].vx;
obj.entities[j].dir = obj.entities[i].dir;
@@ -1479,7 +1479,7 @@ void gamelogic()
obj.createentity(obj.entities[i].xp, 185.0f, 18.0f, 15, 0, 1);
}
int j = obj.getcompanion();
if (j > -1)
if (INBOUNDS_VEC(j, obj.entities))
{
obj.entities[j].vx = obj.entities[i].vx;
obj.entities[j].dir = obj.entities[i].dir;
@@ -1494,7 +1494,7 @@ void gamelogic()
{
obj.createentity(225.0f, 169.0f, 18, graphics.crewcolour(game.lastsaved), 0, 10);
int j = obj.getcompanion();
if (j > -1)
if (INBOUNDS_VEC(j, obj.entities))
{
obj.entities[j].vx = obj.entities[i].vx;
obj.entities[j].dir = obj.entities[i].dir;
@@ -1507,7 +1507,7 @@ void gamelogic()
{
obj.createentity(160.0f, 177.0f, 18, graphics.crewcolour(game.lastsaved), 0, 18, 1);
int j = obj.getcompanion();
if (j > -1)
if (INBOUNDS_VEC(j, obj.entities))
{
obj.entities[j].vx = obj.entities[i].vx;
obj.entities[j].dir = obj.entities[i].dir;
@@ -1518,7 +1518,7 @@ void gamelogic()
obj.flags[59] = true;
obj.createentity(obj.entities[i].xp, -20.0f, 18.0f, graphics.crewcolour(game.lastsaved), 0, 10, 0);
int j = obj.getcompanion();
if (j > -1)
if (INBOUNDS_VEC(j, obj.entities))
{
obj.entities[j].vx = obj.entities[i].vx;
obj.entities[j].dir = obj.entities[i].dir;
@@ -1601,7 +1601,7 @@ void gamelogic()
if (game.activetele && !game.advancetext && game.hascontrol && !script.running && !game.intimetrial)
{
int i = obj.getplayer();
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
obj.settemprect(i);
}
@@ -1636,7 +1636,7 @@ void gamelogic()
#endif
game.prev_act_fade = game.act_fade;
if (game.activeactivity > -1 && game.hascontrol && !script.running)
if (INBOUNDS_VEC(game.activeactivity, obj.blocks) && game.hascontrol && !script.running)
{
if (game.act_fade < 5)
{
@@ -1720,7 +1720,7 @@ void gamelogic()
GhostInfo ghost;
ghost.rx = game.roomx-100;
ghost.ry = game.roomy-100;
if (i > -1)
if (INBOUNDS_VEC(i, obj.entities))
{
ghost.x = obj.entities[i].xp;
ghost.y = obj.entities[i].yp;