1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-23 10:09:43 +01:00

Refactor explored rooms to use setters and getters

This makes it easier to add bounds checks to all accesses of
map.explored. Also, all manually-written existing bounds checks have
been removed, because they're going to go into the new getters and
setters.

The getter is mapclass::isexplored() and the setter is
mapclass::setexplored().
This commit is contained in:
Misa 2021-03-24 12:12:39 -07:00 committed by Ethan Lee
parent b340a6ccc4
commit c5e999c1d5
5 changed files with 66 additions and 61 deletions

View file

@ -2413,7 +2413,7 @@ void teleporterinput(void)
{ {
point& tele = map.teleporters[i]; point& tele = map.teleporters[i];
if (map.explored[tele.x + tele.y*20]) if (map.isexplored(tele.x, tele.y))
{ {
any_tele_unlocked = true; any_tele_unlocked = true;
break; break;
@ -2430,7 +2430,7 @@ void teleporterinput(void)
tempx = map.teleporters[game.teleport_to_teleporter].x; tempx = map.teleporters[game.teleport_to_teleporter].x;
tempy = map.teleporters[game.teleport_to_teleporter].y; tempy = map.teleporters[game.teleport_to_teleporter].y;
} }
while (map.explored[tempx + (20 * tempy)] == 0); while (!map.isexplored(tempx, tempy));
} }
else if (game.press_right && any_tele_unlocked) else if (game.press_right && any_tele_unlocked)
{ {
@ -2441,7 +2441,7 @@ void teleporterinput(void)
tempx = map.teleporters[game.teleport_to_teleporter].x; tempx = map.teleporters[game.teleport_to_teleporter].x;
tempy = map.teleporters[game.teleport_to_teleporter].y; tempy = map.teleporters[game.teleport_to_teleporter].y;
} }
while (map.explored[tempx + (20 * tempy)] == 0); while (!map.isexplored(tempx, tempy));
} }
if (game.press_map) if (game.press_map)

View file

@ -721,34 +721,46 @@ int mapclass::area(int _rx, int _ry)
} }
} }
bool mapclass::isexplored(const int rx, const int ry)
{
const int roomnum = rx + ry*20;
return explored[roomnum];
}
void mapclass::setexplored(const int rx, const int ry, const bool status)
{
const int roomnum = rx + ry*20;
explored[roomnum] = status;
}
void mapclass::exploretower(void) void mapclass::exploretower(void)
{ {
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
{ {
explored[9 + (i * 20)] = 1; setexplored(9, i, true);
} }
} }
void mapclass::hideship(void) void mapclass::hideship(void)
{ {
//remove the ship from the explored areas //remove the ship from the explored areas
explored[2 + (10 * 20)] = 0; setexplored(2, 10, false);
explored[3 + (10 * 20)] = 0; setexplored(3, 10, false);
explored[4 + (10 * 20)] = 0; setexplored(4, 10, false);
explored[2 + (11 * 20)] = 0; setexplored(2, 11, false);
explored[3 + (11 * 20)] = 0; setexplored(3, 11, false);
explored[4 + (11 * 20)] = 0; setexplored(4, 11, false);
} }
void mapclass::showship(void) void mapclass::showship(void)
{ {
//remove the ship from the explored areas //show the ship in the explored areas
explored[2 + (10 * 20)] = 1; setexplored(2, 10, true);
explored[3 + (10 * 20)] = 1; setexplored(3, 10, true);
explored[4 + (10 * 20)] = 1; setexplored(4, 10, true);
explored[2 + (11 * 20)] = 1; setexplored(2, 11, true);
explored[3 + (11 * 20)] = 1; setexplored(3, 11, true);
explored[4 + (11 * 20)] = 1; setexplored(4, 11, true);
} }
void mapclass::resetplayer(void) void mapclass::resetplayer(void)
@ -1148,7 +1160,7 @@ void mapclass::loadlevel(int rx, int ry)
int t; int t;
if (!finalmode) if (!finalmode)
{ {
explored[rx - 100 + ((ry - 100) * 20)] = true; setexplored(rx - 100, ry - 100, true);
if (rx == 109 && !custommode) if (rx == 109 && !custommode)
{ {
exploretower(); exploretower();

View file

@ -92,6 +92,9 @@ public:
bool explored[20 * 20]; bool explored[20 * 20];
int vmult[30]; int vmult[30];
bool isexplored(const int rx, const int ry);
void setexplored(const int rx, const int ry, const bool status);
int background; int background;
int rcol; int rcol;
int tileset; int tileset;

View file

@ -1813,7 +1813,7 @@ void maprender(void)
if(map.customzoom==4){ if(map.customzoom==4){
for (int j = 0; j < map.customheight; j++){ for (int j = 0; j < map.customheight; j++){
for (int i = 0; i < map.customwidth; i++){ for (int i = 0; i < map.customwidth; i++){
if(map.explored[i+(j*20)]==0){ if(!map.isexplored(i, j)){
//Draw the fog of war on the map //Draw the fog of war on the map
graphics.drawimage(2, map.custommmxoff+40 + (i * 48), map.custommmyoff+21 + (j * 36), false); graphics.drawimage(2, map.custommmxoff+40 + (i * 48), map.custommmyoff+21 + (j * 36), false);
graphics.drawimage(2, map.custommmxoff+40 + 12 + (i * 48), map.custommmyoff+21 + (j * 36), false); graphics.drawimage(2, map.custommmxoff+40 + 12 + (i * 48), map.custommmyoff+21 + (j * 36), false);
@ -1840,7 +1840,7 @@ void maprender(void)
}else if(map.customzoom==2){ }else if(map.customzoom==2){
for (int j = 0; j < map.customheight; j++){ for (int j = 0; j < map.customheight; j++){
for (int i = 0; i < map.customwidth; i++){ for (int i = 0; i < map.customwidth; i++){
if(map.explored[i+(j*20)]==0){ if(!map.isexplored(i, j)){
//Draw the fog of war on the map //Draw the fog of war on the map
graphics.drawimage(2, map.custommmxoff+40 + (i * 24), map.custommmyoff+21 + (j * 18), false); graphics.drawimage(2, map.custommmxoff+40 + (i * 24), map.custommmyoff+21 + (j * 18), false);
graphics.drawimage(2, map.custommmxoff+40 + 12 + (i * 24), map.custommmyoff+21 + (j * 18), false); graphics.drawimage(2, map.custommmxoff+40 + 12 + (i * 24), map.custommmyoff+21 + (j * 18), false);
@ -1852,7 +1852,7 @@ void maprender(void)
}else{ }else{
for (int j = 0; j < map.customheight; j++){ for (int j = 0; j < map.customheight; j++){
for (int i = 0; i < map.customwidth; i++){ for (int i = 0; i < map.customwidth; i++){
if(map.explored[i+(j*20)]==0){ if(!map.isexplored(i, j)){
//Draw the fog of war on the map //Draw the fog of war on the map
graphics.drawimage(2, map.custommmxoff+40 + (i * 12), map.custommmyoff+21 + (j * 9), false); graphics.drawimage(2, map.custommmxoff+40 + (i * 12), map.custommmyoff+21 + (j * 9), false);
} }
@ -1923,7 +1923,7 @@ void maprender(void)
{ {
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
{ {
if(map.explored[i+(j*20)]==0) if(!map.isexplored(i, j))
{ {
//Draw the fog of war on the map //Draw the fog of war on the map
graphics.drawimage(2, 40 + (i * 12), 21 + (j * 9), false); graphics.drawimage(2, 40 + (i * 12), 21 + (j * 9), false);
@ -1973,15 +1973,15 @@ void maprender(void)
//draw legend details //draw legend details
for (size_t i = 0; i < map.teleporters.size(); i++) for (size_t i = 0; i < map.teleporters.size(); i++)
{ {
if (map.showteleporters && map.explored[map.teleporters[i].x + (20 * map.teleporters[i].y)] > 0) if (map.showteleporters && map.isexplored(map.teleporters[i].x, map.teleporters[i].y))
{ {
int temp = 1126 + map.explored[map.teleporters[i].x + (20 * map.teleporters[i].y)]; int temp = 1126 + (int) map.isexplored(map.teleporters[i].x, map.teleporters[i].y);
if (graphics.flipmode) temp += 3; if (graphics.flipmode) temp += 3;
graphics.drawtile(40 + 3 + (map.teleporters[i].x * 12), 22 + (map.teleporters[i].y * 9), temp); graphics.drawtile(40 + 3 + (map.teleporters[i].x * 12), 22 + (map.teleporters[i].y * 9), temp);
} }
else if(map.showtargets && map.explored[map.teleporters[i].x+(20*map.teleporters[i].y)]==0) else if(map.showtargets && map.isexplored(map.teleporters[i].x, map.teleporters[i].y))
{ {
int temp = 1126 + map.explored[map.teleporters[i].x + (20 * map.teleporters[i].y)]; int temp = 1126 + (int) map.isexplored(map.teleporters[i].x, map.teleporters[i].y);
if (graphics.flipmode) temp += 3; if (graphics.flipmode) temp += 3;
graphics.drawtile(40 + 3 + (map.teleporters[i].x * 12), 22 + (map.teleporters[i].y * 9), temp); graphics.drawtile(40 + 3 + (map.teleporters[i].x * 12), 22 + (map.teleporters[i].y * 9), temp);
} }
@ -2492,9 +2492,8 @@ void teleporterrender(void)
{ {
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
{ {
if(map.explored[i+(j*20)]==0) if(!map.isexplored(i, j))
{ {
//graphics.drawfillrect(10 + (i * 12), 21 + (j * 9), 12, 9, 16, 16, 16);
graphics.drawimage(2, 40 + (i * 12), 21 + (j * 9), false); graphics.drawimage(2, 40 + (i * 12), 21 + (j * 9), false);
} }
} }
@ -2525,15 +2524,15 @@ void teleporterrender(void)
//draw legend details //draw legend details
for (size_t i = 0; i < map.teleporters.size(); i++) for (size_t i = 0; i < map.teleporters.size(); i++)
{ {
if (map.showteleporters && map.explored[map.teleporters[i].x + (20 * map.teleporters[i].y)] > 0) if (map.showteleporters && map.isexplored(map.teleporters[i].x, map.teleporters[i].y))
{ {
temp = 1126 + map.explored[map.teleporters[i].x + (20 * map.teleporters[i].y)]; temp = 1126 + (int) map.isexplored(map.teleporters[i].x, map.teleporters[i].y);
if (graphics.flipmode) temp += 3; if (graphics.flipmode) temp += 3;
graphics.drawtile(40 + 3 + (map.teleporters[i].x * 12), 22 + (map.teleporters[i].y * 9), temp); graphics.drawtile(40 + 3 + (map.teleporters[i].x * 12), 22 + (map.teleporters[i].y * 9), temp);
} }
else if(map.showtargets && map.explored[map.teleporters[i].x+(20*map.teleporters[i].y)]==0) else if(map.showtargets && map.isexplored(map.teleporters[i].x, map.teleporters[i].y))
{ {
temp = 1126 + map.explored[map.teleporters[i].x + (20 * map.teleporters[i].y)]; temp = 1126 + (int) map.isexplored(map.teleporters[i].x, map.teleporters[i].y);
if (graphics.flipmode) temp += 3; if (graphics.flipmode) temp += 3;
graphics.drawtile(40 + 3 + (map.teleporters[i].x * 12), 22 + (map.teleporters[i].y * 9), temp); graphics.drawtile(40 + 3 + (map.teleporters[i].x * 12), 22 + (map.teleporters[i].y * 9), temp);
} }

View file

@ -1336,8 +1336,7 @@ void scriptclass::run(void)
} }
else if (words[0] == "ifexplored") else if (words[0] == "ifexplored")
{ {
int room = ss_toi(words[1]) + (20 * ss_toi(words[2])); if (map.isexplored(ss_toi(words[1]), ss_toi(words[2])))
if (INBOUNDS_ARR(room, map.explored) && map.explored[room] == 1)
{ {
load(words[3]); load(words[3]);
position--; position--;
@ -1395,19 +1394,11 @@ void scriptclass::run(void)
} }
else if (words[0] == "hidecoordinates") else if (words[0] == "hidecoordinates")
{ {
int room = ss_toi(words[1]) + (20 * ss_toi(words[2])); map.setexplored(ss_toi(words[1]), ss_toi(words[2]), false);
if (INBOUNDS_ARR(room, map.explored))
{
map.explored[room] = false;
}
} }
else if (words[0] == "showcoordinates") else if (words[0] == "showcoordinates")
{ {
int room = ss_toi(words[1]) + (20 * ss_toi(words[2])); map.setexplored(ss_toi(words[1]), ss_toi(words[2]), true);
if (INBOUNDS_ARR(room, map.explored))
{
map.explored[room] = true;
}
} }
else if (words[0] == "hideship") else if (words[0] == "hideship")
{ {
@ -1419,25 +1410,25 @@ void scriptclass::run(void)
} }
else if (words[0] == "showsecretlab") else if (words[0] == "showsecretlab")
{ {
map.explored[16 + (20 * 5)] = 1; map.setexplored(16, 5, true);
map.explored[17 + (20 * 5)] = 1; map.setexplored(17, 5, true);
map.explored[18 + (20 * 5)] = 1; map.setexplored(18, 5, true);
map.explored[17 + (20 * 6)] = 1; map.setexplored(17, 6, true);
map.explored[18 + (20 * 6)] = 1; map.setexplored(18, 6, true);
map.explored[19 + (20 * 6)] = 1; map.setexplored(19, 6, true);
map.explored[19 + (20 * 7)] = 1; map.setexplored(19, 7, true);
map.explored[19 + (20 * 8)] = 1; map.setexplored(19, 8, true);
} }
else if (words[0] == "hidesecretlab") else if (words[0] == "hidesecretlab")
{ {
map.explored[16 + (20 * 5)] = 0; map.setexplored(16, 5, false);
map.explored[17 + (20 * 5)] = 0; map.setexplored(17, 5, false);
map.explored[18 + (20 * 5)] = 0; map.setexplored(18, 5, false);
map.explored[17 + (20 * 6)] = 0; map.setexplored(17, 6, false);
map.explored[18 + (20 * 6)] = 0; map.setexplored(18, 6, false);
map.explored[19 + (20 * 6)] = 0; map.setexplored(19, 6, false);
map.explored[19 + (20 * 7)] = 0; map.setexplored(19, 7, false);
map.explored[19 + (20 * 8)] = 0; map.setexplored(19, 8, false);
} }
else if (words[0] == "showteleporters") else if (words[0] == "showteleporters")
{ {
@ -2969,7 +2960,7 @@ void scriptclass::startgamemode( int t )
obj.collect[j] = true; obj.collect[j] = true;
for (i = 0; i < 20; i++) for (i = 0; i < 20; i++)
{ {
map.explored[i + (j * 20)] = 1; map.setexplored(i, j, true);
} }
} }
game.insecretlab = true; game.insecretlab = true;