From 0664eac7fc6b6d50b19f25a38ba3f69b5eeb579c Mon Sep 17 00:00:00 2001 From: Misa Date: Thu, 2 Jul 2020 19:17:32 -0700 Subject: [PATCH] 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. --- desktop_version/src/Entity.cpp | 22 +++++----- desktop_version/src/Entity.h | 4 +- desktop_version/src/Game.cpp | 68 +++++------------------------- desktop_version/src/Script.cpp | 10 ++--- desktop_version/src/UtilityClass.h | 1 + 5 files changed, 28 insertions(+), 77 deletions(-) diff --git a/desktop_version/src/Entity.cpp b/desktop_version/src/Entity.cpp index f9e65ab7..038f8839 100644 --- a/desktop_version/src/Entity.cpp +++ b/desktop_version/src/Entity.cpp @@ -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) diff --git a/desktop_version/src/Entity.h b/desktop_version/src/Entity.h index f5f60bc9..51c7499e 100644 --- a/desktop_version/src/Entity.h +++ b/desktop_version/src/Entity.h @@ -183,8 +183,8 @@ public: std::vector blocks; bool flags[100]; - std::vector collect; - std::vector customcollect; + bool collect[100]; + bool customcollect[100]; bool skipblocks, skipdirblocks; diff --git a/desktop_version/src/Game.cpp b/desktop_version/src/Game.cpp index 83db79de..b5bf6340 100644 --- a/desktop_version/src/Game.cpp +++ b/desktop_version/src/Game.cpp @@ -5369,19 +5369,7 @@ void Game::loadquick() LOAD_ARRAY(crewstats) - if (pKey == "collect") - { - std::string TextString = (pText); - if(TextString.length()) - { - std::vector 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 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 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 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]) { diff --git a/desktop_version/src/Script.cpp b/desktop_version/src/Script.cpp index c1540694..ddae3254 100644 --- a/desktop_version/src/Script.cpp +++ b/desktop_version/src/Script.cpp @@ -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(); diff --git a/desktop_version/src/UtilityClass.h b/desktop_version/src/UtilityClass.h index 8ab63ef1..5e2d9e35 100644 --- a/desktop_version/src/UtilityClass.h +++ b/desktop_version/src/UtilityClass.h @@ -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; \