Turn obj.collect and obj.customcollect into plain arrays

Since they're always fixed-size, there's no need for them to be vectors.

Also added an INBOUNDS_ARR() macro to do bounds checks with plain
arrays.
This commit is contained in:
Misa 2020-07-02 19:17:32 -07:00 committed by Ethan Lee
parent 62203efb2c
commit 0664eac7fc
5 changed files with 28 additions and 77 deletions

View File

@ -70,8 +70,8 @@ void entityclass::init()
SDL_memset(customcrewmoods, true, sizeof(customcrewmoods));
resetallflags();
collect.resize(100);
customcollect.resize(100);
SDL_memset(collect, false, sizeof(collect));
SDL_memset(customcollect, false, sizeof(customcollect));
}
void entityclass::resetallflags()
@ -1391,7 +1391,7 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo
//Check if it's already been collected
entity.para = vx;
if (!INBOUNDS(vx, collect) || collect[vx]) return;
if (!INBOUNDS_ARR(vx, collect) || collect[(int) vx]) return;
break;
case 9: //Something Shiny
entity.rule = 3;
@ -1406,7 +1406,7 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo
//Check if it's already been collected
entity.para = vx;
if (collect[vx]) return;
if (collect[(int) vx]) return;
break;
case 10: //Savepoint
entity.rule = 3;
@ -1602,7 +1602,7 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo
//Check if it's already been collected
entity.para = vx;
if (INBOUNDS(vx, collect) && !collect[ (vx)]) return;
if (INBOUNDS_ARR(vx, collect) && !collect[(int) vx]) return;
break;
case 23: //SWN Enemies
//Given a different behavior, these enemies are especially for SWN mode and disappear outside the screen.
@ -1935,7 +1935,7 @@ void entityclass::createentity( float xp, float yp, int t, float vx /*= 0*/, flo
//Check if it's already been collected
entity.para = vx;
if (!INBOUNDS(vx, customcollect) || customcollect[vx]) return;
if (!INBOUNDS_ARR(vx, customcollect) || customcollect[(int) vx]) return;
break;
case 56: //Custom enemy
entity.rule = 1;
@ -2517,7 +2517,7 @@ bool entityclass::updateentities( int i )
if (entities[i].state == 1)
{
music.playef(4);
collect[entities[i].para] = true;
collect[(int) entities[i].para] = true;
return removeentity(i);
}
@ -2526,9 +2526,9 @@ bool entityclass::updateentities( int i )
//wait for collision
if (entities[i].state == 1)
{
if (INBOUNDS(entities[i].para, collect))
if (INBOUNDS_ARR(entities[i].para, collect))
{
collect[entities[i].para] = true;
collect[(int) entities[i].para] = true;
}
if (game.intimetrial)
@ -3159,9 +3159,9 @@ bool entityclass::updateentities( int i )
}
else if (entities[i].state == 1)
{
if (INBOUNDS(entities[i].para, customcollect))
if (INBOUNDS_ARR(entities[i].para, customcollect))
{
customcollect[entities[i].para] = true;
customcollect[(int) entities[i].para] = true;
}
if (game.intimetrial)

View File

@ -183,8 +183,8 @@ public:
std::vector<blockclass> blocks;
bool flags[100];
std::vector<bool> collect;
std::vector<bool> customcollect;
bool collect[100];
bool customcollect[100];
bool skipblocks, skipdirblocks;

View File

@ -5369,19 +5369,7 @@ void Game::loadquick()
LOAD_ARRAY(crewstats)
if (pKey == "collect")
{
std::string TextString = (pText);
if(TextString.length())
{
std::vector<std::string> values = split(TextString,',');
obj.collect.clear();
for(size_t i = 0; i < values.size(); i++)
{
obj.collect.push_back((bool) atoi(values[i].c_str()));
}
}
}
LOAD_ARRAY_RENAME(collect, obj.collect)
if (pKey == "finalmode")
{
@ -5574,33 +5562,9 @@ void Game::customloadquick(std::string savfile)
LOAD_ARRAY(crewstats)
if (pKey == "collect")
{
std::string TextString = (pText);
if(TextString.length())
{
std::vector<std::string> values = split(TextString,',');
obj.collect.clear();
for(size_t i = 0; i < values.size(); i++)
{
obj.collect.push_back((bool) atoi(values[i].c_str()));
}
}
}
LOAD_ARRAY_RENAME(collect, obj.collect)
if (pKey == "customcollect")
{
std::string TextString = (pText);
if(TextString.length())
{
std::vector<std::string> values = split(TextString,',');
obj.customcollect.clear();
for(size_t i = 0; i < values.size(); i++)
{
obj.customcollect.push_back((bool) atoi(values[i].c_str()));
}
}
}
LOAD_ARRAY_RENAME(customcollect, obj.customcollect)
if (pKey == "finalmode")
{
@ -5965,7 +5929,7 @@ void Game::savetele()
msgs->LinkEndChild( msg );
std::string collect;
for(size_t i = 0; i < obj.collect.size(); i++ )
for(size_t i = 0; i < SDL_arraysize(obj.collect); i++ )
{
collect += help.String((int) obj.collect[i]) + ",";
}
@ -6161,7 +6125,7 @@ void Game::savequick()
msgs->LinkEndChild( msg );
std::string collect;
for(size_t i = 0; i < obj.collect.size(); i++ )
for(size_t i = 0; i < SDL_arraysize(obj.collect); i++ )
{
collect += help.String((int) obj.collect[i]) + ",";
}
@ -6359,7 +6323,7 @@ void Game::customsavequick(std::string savfile)
msgs->LinkEndChild( msg );
std::string collect;
for(size_t i = 0; i < obj.collect.size(); i++ )
for(size_t i = 0; i < SDL_arraysize(obj.collect); i++ )
{
collect += help.String((int) obj.collect[i]) + ",";
}
@ -6368,7 +6332,7 @@ void Game::customsavequick(std::string savfile)
msgs->LinkEndChild( msg );
std::string customcollect;
for(size_t i = 0; i < obj.customcollect.size(); i++ )
for(size_t i = 0; i < SDL_arraysize(obj.customcollect); i++ )
{
customcollect += help.String((int) obj.customcollect[i]) + ",";
}
@ -6562,19 +6526,7 @@ void Game::loadtele()
LOAD_ARRAY(crewstats)
if (pKey == "collect")
{
std::string TextString = (pText);
if(TextString.length())
{
std::vector<std::string> values = split(TextString,',');
obj.collect.clear();
for(size_t i = 0; i < values.size(); i++)
{
obj.collect.push_back((bool) atoi(values[i].c_str()));
}
}
}
LOAD_ARRAY_RENAME(collect, obj.collect)
if (pKey == "finalmode")
{
@ -7562,7 +7514,7 @@ void Game::resetgameclock()
int Game::trinkets()
{
int temp = 0;
for (size_t i = 0; i < obj.collect.size(); i++)
for (size_t i = 0; i < SDL_arraysize(obj.collect); i++)
{
if (obj.collect[i])
{
@ -7575,7 +7527,7 @@ int Game::trinkets()
int Game::crewmates()
{
int temp = 0;
for (size_t i = 0; i < obj.customcollect.size(); i++)
for (size_t i = 0; i < SDL_arraysize(obj.customcollect); i++)
{
if (obj.customcollect[i])
{

View File

@ -1959,8 +1959,8 @@ void scriptclass::run()
music.haltdasmusik();
music.playef(3);
int trinket = ss_toi(words[1]);
if (trinket >= 0 && trinket < (int) obj.collect.size())
size_t trinket = ss_toi(words[1]);
if (trinket < SDL_arraysize(obj.collect))
{
obj.collect[trinket] = true;
}
@ -3707,10 +3707,8 @@ void scriptclass::hardreset()
obj.customcrewmoods[i]=true;
}
obj.collect.clear();
obj.collect.resize(100);
obj.customcollect.clear();
obj.customcollect.resize(100);
SDL_memset(obj.collect, false, sizeof(obj.collect));
SDL_memset(obj.customcollect, false, sizeof(obj.customcollect));
i = 100; //previously a for-loop iterating over collect/customcollect set this to 100
int theplayer = obj.getplayer();

View File

@ -16,6 +16,7 @@ bool is_positive_num(const std::string& str, bool hex);
bool endsWith(const std::string& str, const std::string& suffix);
#define INBOUNDS(index, vector) ((int) index >= 0 && (int) index < (int) vector.size())
#define INBOUNDS_ARR(index, array) ((int) index >= 0 && (int) index < (int) SDL_arraysize(array))
#define WHINE_ONCE(message) \
static bool whine = true; \