diff --git a/projects/mtg/include/AIPlayer.h b/projects/mtg/include/AIPlayer.h index 6042dd9fa..089edc446 100644 --- a/projects/mtg/include/AIPlayer.h +++ b/projects/mtg/include/AIPlayer.h @@ -67,6 +67,9 @@ public: class AIPlayer: public Player{ +private: + static int totalAIDecks; //a cache that counts the number of AI deck files in the AI folder. see getTotalAIDecks() below. + protected: bool mFastTimerMode; queue clickstream; @@ -98,6 +101,9 @@ public: RandomGenerator* getRandomGenerator(){return &randomGenerator;}; bool parseLine(const string& s); + + static int getTotalAIDecks(); + static void invalidateTotalAIDecks(); }; diff --git a/projects/mtg/include/utils.h b/projects/mtg/include/utils.h index 6a12a84b0..1983487d6 100644 --- a/projects/mtg/include/utils.h +++ b/projects/mtg/include/utils.h @@ -86,7 +86,6 @@ public: }; }; -int filesize(const char * filename); int WRand(bool log = false); #ifdef LINUX diff --git a/projects/mtg/src/AIPlayer.cpp b/projects/mtg/src/AIPlayer.cpp index 5551eaaea..f3695b39a 100644 --- a/projects/mtg/src/AIPlayer.cpp +++ b/projects/mtg/src/AIPlayer.cpp @@ -14,6 +14,7 @@ #endif +int AIPlayer::totalAIDecks = -1; const char * const MTG_LAND_TEXTS[] = { "artifact", "forest", "island", "mountain", "swamp", "plains", "other lands" }; @@ -218,19 +219,7 @@ AIPlayer * AIPlayerFactory::createAIPlayer(GameObserver *observer, MTGAllCards * if (!deckid) { //random deck - int nbdecks = 0; - int found = 1; - while (found && nbdecks < options[Options::AIDECKS_UNLOCKED].number) - { - found = 0; - char buffer[512]; - sprintf(buffer, "ai/baka/deck%i.txt", nbdecks + 1); - if (FileExists(buffer)) - { - found = 1; - nbdecks++; - } - } + int nbdecks = MIN(AIPlayer::getTotalAIDecks(), options[Options::AIDECKS_UNLOCKED].number); if (!nbdecks) return NULL; deckid = 1 + WRand() % (nbdecks); @@ -343,3 +332,32 @@ AIPlayer * AIPlayerFactory::createAIPlayerTest(GameObserver *observer, MTGAllCar return baka; } #endif + + +int AIPlayer::getTotalAIDecks() +{ + if (totalAIDecks != -1) + return totalAIDecks; + + totalAIDecks = 0; + bool found = true; + while (found) + { + found = false; + char buffer[512]; + sprintf(buffer, "ai/baka/deck%i.txt", totalAIDecks + 1); + if (JFileSystem::GetInstance()->FileExists(buffer)) + { + found = true; + totalAIDecks++; + } + } + + return totalAIDecks; +} + +void AIPlayer::invalidateTotalAIDecks() +{ + totalAIDecks = -1; +} + diff --git a/projects/mtg/src/Credits.cpp b/projects/mtg/src/Credits.cpp index 2dbb0f55d..ab2f1290b 100644 --- a/projects/mtg/src/Credits.cpp +++ b/projects/mtg/src/Credits.cpp @@ -8,7 +8,7 @@ #include "MTGDeck.h" #include "GameObserver.h" #include "GameStateShop.h" -#include "PlayerData.h" +#include "AIPlayer.h" map Unlockable::unlockables; @@ -440,27 +440,19 @@ int Credits::isDifficultyUnlocked(DeckStats * stats) { if (options[Options::DIFFICULTY_MODE_UNLOCKED].number) return 0; - int nbAIDecks = 0; - int found = 1; + int nbAIDecks = AIPlayer::getTotalAIDecks(); + int wins = 0; - while (found) + for (int i = 0; i < nbAIDecks; ++i) { - found = 0; - char buffer[512]; char aiSmallDeckName[512]; - sprintf(buffer, "ai/baka/deck%i.txt", nbAIDecks + 1); - if (fileExists(buffer)) - { - found = 1; - nbAIDecks++; - sprintf(aiSmallDeckName, "ai_baka_deck%i", nbAIDecks); - int percentVictories = stats->percentVictories(string(aiSmallDeckName)); - if (percentVictories >= 67) - wins++; - if (wins >= 10) - return 1; - } + sprintf(aiSmallDeckName, "ai_baka_deck%i", i+1); + int percentVictories = stats->percentVictories(string(aiSmallDeckName)); + if (percentVictories >= 67) + wins++; + if (wins >= 10) + return 1; } return 0; } @@ -566,20 +558,8 @@ int Credits::IsMoreAIDecksUnlocked(DeckStats * stats) { // the number of currently unlocked decks in order to go through. if (stats->nbGames() < currentlyUnlocked * 1.2) return 0; - int nbdecks = 0; - int found = 1; - while (found) - { - found = 0; - char buffer[512]; - sprintf(buffer, "ai/baka/deck%i.txt", nbdecks + 1); - if (JFileSystem::GetInstance()->FileExists(buffer)) - { - found = 1; - nbdecks++; - if (nbdecks > currentlyUnlocked) - return 1; - } - } + if (AIPlayer::getTotalAIDecks() > currentlyUnlocked) + return 1; + return 0; } diff --git a/projects/mtg/src/GameStateDeckViewer.cpp b/projects/mtg/src/GameStateDeckViewer.cpp index 2f277782b..e61a174bc 100644 --- a/projects/mtg/src/GameStateDeckViewer.cpp +++ b/projects/mtg/src/GameStateDeckViewer.cpp @@ -20,6 +20,7 @@ #include "DeckEditorMenu.h" #include "SimpleMenu.h" #include "utils.h" +#include "AIPlayer.h" @@ -315,24 +316,22 @@ void GameStateDeckViewer::saveDeck() void GameStateDeckViewer::saveAsAIDeck(string deckName) { - vector aiDecks = GameState::BuildDeckList("ai/baka", "ai_baka", NULL); - int nbAiDecks = aiDecks.size() + 1; - aiDecks.clear(); + int deckId = AIPlayer::getTotalAIDecks() + 1; - string defaultAiDeckName = "deck"; std::ostringstream oss; - oss << "deck" << nbAiDecks; - defaultAiDeckName = oss.str(); + oss << "deck" <parent->meta_desc == "") - oss << endl << "Can you beat your own creations?" << endl << "User created AI Deck # " << nbAiDecks; + oss << endl << "Can you beat your own creations?" << endl << "User created AI Deck # " << deckId; else oss << myDeck->parent->meta_desc; string deckDesc = oss.str(); string filepath = "ai/baka/"; - filepath.append(defaultAiDeckName).append(".txt"); + filepath.append(aiDeckName).append(".txt"); DebugTrace("saving AI deck " << filepath); myDeck->save(filepath, true, deckName, deckDesc); + AIPlayer::invalidateTotalAIDecks(); //We added one AI deck, so we need to invalidate the count cache } void GameStateDeckViewer::Update(float dt) diff --git a/projects/mtg/src/GameStateMenu.cpp b/projects/mtg/src/GameStateMenu.cpp index 0cc36d62f..e266f9d00 100644 --- a/projects/mtg/src/GameStateMenu.cpp +++ b/projects/mtg/src/GameStateMenu.cpp @@ -26,7 +26,7 @@ #include #endif//NETWORK_SUPPORT -static const char* GAME_VERSION = "WTH?! 0.16.0 - wololo.net"; +static const char* GAME_VERSION = "WTH?! 0.17.0 - wololo.net"; enum ENUM_MENU_STATE_MAJOR { diff --git a/projects/mtg/src/utils.cpp b/projects/mtg/src/utils.cpp index 49867087b..9ff796dc6 100644 --- a/projects/mtg/src/utils.cpp +++ b/projects/mtg/src/utils.cpp @@ -88,28 +88,6 @@ int WRand(bool log) return rand(); } -int filesize(const char * filename) -{ - int file_size = 0; -#if defined (PSP) - int file = sceIoOpen(filename, PSP_O_RDONLY, 0777); - if (file > 0) - { - file_size = sceIoLseek(file, 0, PSP_SEEK_END); - sceIoClose(file); - } -#else - FILE * file = fopen(filename, "rb"); - if (file != NULL) - { - fseek(file, 0, SEEK_END); - file_size = ftell(file); - fclose(file); - } -#endif - return file_size; -} - bool fileExists(const char * filename) { return JFileSystem::GetInstance()->FileExists(filename);