From 1258eb7bf4451c407631a6568889c8d8b42935a9 Mon Sep 17 00:00:00 2001 From: Misa Date: Thu, 2 Jul 2020 18:10:52 -0700 Subject: [PATCH] Turn crew rescued/mood vectors into arrays Since they're always fixed-size, they don't need to be dynamically-sized vectors. entityclass::customcrewmoods is now a proper bool instead of an int now, and I replaced the hardcoded constant 6 with a static const int Game attribute to make it easier to change. --- desktop_version/src/Entity.cpp | 5 +- desktop_version/src/Entity.h | 2 +- desktop_version/src/Game.cpp | 107 ++++++--------------------------- desktop_version/src/Game.h | 8 +-- desktop_version/src/Script.cpp | 10 +-- 5 files changed, 30 insertions(+), 102 deletions(-) diff --git a/desktop_version/src/Entity.cpp b/desktop_version/src/Entity.cpp index 4c4ff6b0..b28f9300 100644 --- a/desktop_version/src/Entity.cpp +++ b/desktop_version/src/Entity.cpp @@ -67,10 +67,7 @@ void entityclass::init() altstates = 0; - for (int i = 0; i < 6; i++) - { - customcrewmoods[i]=1; - } + SDL_memset(customcrewmoods, true, sizeof(customcrewmoods)); flags.resize(100); collect.resize(100); diff --git a/desktop_version/src/Entity.h b/desktop_version/src/Entity.h index c409ec70..fddd8673 100644 --- a/desktop_version/src/Entity.h +++ b/desktop_version/src/Entity.h @@ -207,7 +207,7 @@ public: int customplatformtile; bool customwarpmode, customwarpmodevon, customwarpmodehon; std::string customscript; - int customcrewmoods[6]; + bool customcrewmoods[Game::numcrew]; }; extern entityclass obj; diff --git a/desktop_version/src/Game.cpp b/desktop_version/src/Game.cpp index 6fab821f..72859387 100644 --- a/desktop_version/src/Game.cpp +++ b/desktop_version/src/Game.cpp @@ -209,9 +209,9 @@ void Game::init(void) } customcol=0; - crewstats.resize(6); - tele_crewstats.resize(6); - quick_crewstats.resize(6); + SDL_memset(crewstats, false, sizeof(crewstats)); + SDL_memset(tele_crewstats, false, sizeof(tele_crewstats)); + SDL_memset(quick_crewstats, false, sizeof(quick_crewstats)); SDL_memset(besttimes, -1, sizeof(besttimes)); SDL_memset(bestframes, -1, sizeof(bestframes)); SDL_memset(besttrinkets, -1, sizeof(besttrinkets)); @@ -4589,20 +4589,22 @@ void Game::unlocknum( int t ) savestats(); } -#define LOAD_ARRAY(ARRAY_NAME) \ +#define LOAD_ARRAY_RENAME(ARRAY_NAME, DEST) \ if (pKey == #ARRAY_NAME) \ { \ std::string TextString = pText; \ if (TextString.length()) \ { \ std::vector values = split(TextString, ','); \ - for (size_t i = 0; i < SDL_min(SDL_arraysize(ARRAY_NAME), values.size()); i++) \ + for (size_t i = 0; i < SDL_min(SDL_arraysize(DEST), values.size()); i++) \ { \ - ARRAY_NAME[i] = atoi(values[i].c_str()); \ + DEST[i] = atoi(values[i].c_str()); \ } \ } \ } +#define LOAD_ARRAY(ARRAY_NAME) LOAD_ARRAY_RENAME(ARRAY_NAME, ARRAY_NAME) + void Game::loadstats() { tinyxml2::XMLDocument doc; @@ -5377,19 +5379,7 @@ void Game::loadquick() } } - if (pKey == "crewstats") - { - std::string TextString = (pText); - if(TextString.length()) - { - std::vector values = split(TextString,','); - crewstats.clear(); - for(size_t i = 0; i < values.size(); i++) - { - crewstats.push_back(atoi(values[i].c_str())); - } - } - } + LOAD_ARRAY(crewstats) if (pKey == "collect") { @@ -5604,32 +5594,9 @@ void Game::customloadquick(std::string savfile) } } - if (pKey == "moods") - { - std::string TextString = (pText); - if(TextString.length()) - { - std::vector values = split(TextString,','); - for(size_t i = 0; i < 6; i++) - { - obj.customcrewmoods[i]=atoi(values[i].c_str()); - } - } - } + LOAD_ARRAY_RENAME(moods, obj.customcrewmoods) - if (pKey == "crewstats") - { - std::string TextString = (pText); - if(TextString.length()) - { - std::vector values = split(TextString,','); - crewstats.clear(); - for(size_t i = 0; i < values.size(); i++) - { - crewstats.push_back(atoi(values[i].c_str())); - } - } - } + LOAD_ARRAY(crewstats) if (pKey == "collect") { @@ -5864,19 +5831,7 @@ void Game::loadsummary() map.finalstretch = atoi(pText); } - if (pKey == "crewstats") - { - std::string TextString = (pText); - if(TextString.length()) - { - std::vector values = split(TextString,','); - tele_crewstats.clear(); - for(size_t i = 0; i < values.size(); i++) - { - tele_crewstats.push_back(atoi(values[i].c_str())); - } - } - } + LOAD_ARRAY_RENAME(crewstats, tele_crewstats) } tele_gametime = giventimestring(l_hours,l_minute, l_second); @@ -5953,19 +5908,7 @@ void Game::loadsummary() map.finalstretch = atoi(pText); } - if (pKey == "crewstats") - { - std::string TextString = (pText); - if(TextString.length()) - { - std::vector values = split(TextString,','); - quick_crewstats.clear(); - for(size_t i = 0; i < values.size(); i++) - { - quick_crewstats.push_back(atoi(values[i].c_str())); - } - } - } + LOAD_ARRAY_RENAME(crewstats, quick_crewstats) } @@ -6037,7 +5980,7 @@ void Game::savetele() msgs->LinkEndChild( msg ); std::string crewstatsString; - for(size_t i = 0; i < crewstats.size(); i++ ) + for(size_t i = 0; i < SDL_arraysize(crewstats); i++ ) { crewstatsString += help.String(crewstats[i]) + ","; } @@ -6233,7 +6176,7 @@ void Game::savequick() msgs->LinkEndChild( msg ); std::string crewstatsString; - for(size_t i = 0; i < crewstats.size(); i++ ) + for(size_t i = 0; i < SDL_arraysize(crewstats); i++ ) { crewstatsString += help.String(crewstats[i]) + ","; } @@ -6422,7 +6365,7 @@ void Game::customsavequick(std::string savfile) msgs->LinkEndChild( msg ); std::string moods; - for(int i = 0; i < 6; i++ ) + for(size_t i = 0; i < SDL_arraysize(obj.customcrewmoods); i++ ) { moods += help.String(obj.customcrewmoods[i]) + ","; } @@ -6431,7 +6374,7 @@ void Game::customsavequick(std::string savfile) msgs->LinkEndChild( msg ); std::string crewstatsString; - for(size_t i = 0; i < crewstats.size(); i++ ) + for(size_t i = 0; i < SDL_arraysize(crewstats); i++ ) { crewstatsString += help.String(crewstats[i]) + ","; } @@ -6653,19 +6596,7 @@ void Game::loadtele() } } - if (pKey == "crewstats") - { - std::string TextString = (pText); - if(TextString.length()) - { - std::vector values = split(TextString,','); - crewstats.clear(); - for(size_t i = 0; i < values.size(); i++) - { - crewstats.push_back(atoi(values[i].c_str())); - } - } - } + LOAD_ARRAY(crewstats) if (pKey == "collect") { @@ -7646,7 +7577,7 @@ void Game::swnpenalty() int Game::crewrescued() { int temp = 0; - for (size_t i = 0; i < crewstats.size(); i++) + for (size_t i = 0; i < SDL_arraysize(crewstats); i++) { if (crewstats[i]) { diff --git a/desktop_version/src/Game.h b/desktop_version/src/Game.h index 4ce16d25..ab4dba7b 100644 --- a/desktop_version/src/Game.h +++ b/desktop_version/src/Game.h @@ -196,7 +196,6 @@ public: //Menu interaction stuff bool mapheld; int menupage; - //public var crewstats:Array = new Array(); int lastsaved; int deathcounts; @@ -278,15 +277,16 @@ public: bool inintermission; - std::vector crewstats; + static const int numcrew = 6; + bool crewstats[numcrew]; bool alarmon; int alarmdelay; bool blackout; - std::vector tele_crewstats; + bool tele_crewstats[numcrew]; - std::vector quick_crewstats; + bool quick_crewstats[numcrew]; static const int numunlock = 25; bool unlock[numunlock]; diff --git a/desktop_version/src/Script.cpp b/desktop_version/src/Script.cpp index ab6d526b..4db102ab 100644 --- a/desktop_version/src/Script.cpp +++ b/desktop_version/src/Script.cpp @@ -1366,8 +1366,8 @@ void scriptclass::run() } else if (words[0] == "ifcrewlost") { - int crewmate = ss_toi(words[1]); - if (crewmate >= 0 && crewmate < (int) game.crewstats.size() && game.crewstats[crewmate]==false) + size_t crewmate = ss_toi(words[1]); + if (crewmate < SDL_arraysize(game.crewstats) && game.crewstats[crewmate]==false) { load(words[2]); position--; @@ -3570,7 +3570,7 @@ void scriptclass::hardreset() game.nodeathmode = false; game.nocutscenes = false; - for (i = 0; i < 6; i++) + for (i = 0; i < (int) SDL_arraysize(game.crewstats); i++) { game.crewstats[i] = false; } @@ -3704,8 +3704,8 @@ void scriptclass::hardreset() obj.flags.clear(); obj.flags.resize(100); - for (i = 0; i < 6; i++){ - obj.customcrewmoods[i]=1; + for (i = 0; i < (int) SDL_arraysize(obj.customcrewmoods); i++){ + obj.customcrewmoods[i]=true; } obj.collect.clear();