- Added cache for the count of AI decks, to reduce file access especially on victory screen. This fixes some massive lags on the PSP when winning a game

- bumped version number to 0.17
- removed some dead code in utils.h
This commit is contained in:
wagic.the.homebrew
2011-12-07 14:34:59 +00:00
parent 17e506147e
commit 69a0323c86
7 changed files with 58 additions and 78 deletions
+6
View File
@@ -67,6 +67,9 @@ public:
class AIPlayer: public Player{ 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: protected:
bool mFastTimerMode; bool mFastTimerMode;
queue<AIAction *> clickstream; queue<AIAction *> clickstream;
@@ -98,6 +101,9 @@ public:
RandomGenerator* getRandomGenerator(){return &randomGenerator;}; RandomGenerator* getRandomGenerator(){return &randomGenerator;};
bool parseLine(const string& s); bool parseLine(const string& s);
static int getTotalAIDecks();
static void invalidateTotalAIDecks();
}; };
-1
View File
@@ -86,7 +86,6 @@ public:
}; };
}; };
int filesize(const char * filename);
int WRand(bool log = false); int WRand(bool log = false);
#ifdef LINUX #ifdef LINUX
+31 -13
View File
@@ -14,6 +14,7 @@
#endif #endif
int AIPlayer::totalAIDecks = -1;
const char * const MTG_LAND_TEXTS[] = { "artifact", "forest", "island", "mountain", "swamp", "plains", "other lands" }; 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) if (!deckid)
{ {
//random deck //random deck
int nbdecks = 0; int nbdecks = MIN(AIPlayer::getTotalAIDecks(), options[Options::AIDECKS_UNLOCKED].number);
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++;
}
}
if (!nbdecks) if (!nbdecks)
return NULL; return NULL;
deckid = 1 + WRand() % (nbdecks); deckid = 1 + WRand() % (nbdecks);
@@ -343,3 +332,32 @@ AIPlayer * AIPlayerFactory::createAIPlayerTest(GameObserver *observer, MTGAllCar
return baka; return baka;
} }
#endif #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;
}
+13 -33
View File
@@ -8,7 +8,7 @@
#include "MTGDeck.h" #include "MTGDeck.h"
#include "GameObserver.h" #include "GameObserver.h"
#include "GameStateShop.h" #include "GameStateShop.h"
#include "PlayerData.h" #include "AIPlayer.h"
map <string, Unlockable *> Unlockable::unlockables; map <string, Unlockable *> Unlockable::unlockables;
@@ -440,27 +440,19 @@ int Credits::isDifficultyUnlocked(DeckStats * stats)
{ {
if (options[Options::DIFFICULTY_MODE_UNLOCKED].number) if (options[Options::DIFFICULTY_MODE_UNLOCKED].number)
return 0; return 0;
int nbAIDecks = 0; int nbAIDecks = AIPlayer::getTotalAIDecks();
int found = 1;
int wins = 0; int wins = 0;
while (found) for (int i = 0; i < nbAIDecks; ++i)
{ {
found = 0;
char buffer[512];
char aiSmallDeckName[512]; char aiSmallDeckName[512];
sprintf(buffer, "ai/baka/deck%i.txt", nbAIDecks + 1); sprintf(aiSmallDeckName, "ai_baka_deck%i", i+1);
if (fileExists(buffer)) int percentVictories = stats->percentVictories(string(aiSmallDeckName));
{ if (percentVictories >= 67)
found = 1; wins++;
nbAIDecks++; if (wins >= 10)
sprintf(aiSmallDeckName, "ai_baka_deck%i", nbAIDecks); return 1;
int percentVictories = stats->percentVictories(string(aiSmallDeckName));
if (percentVictories >= 67)
wins++;
if (wins >= 10)
return 1;
}
} }
return 0; return 0;
} }
@@ -566,20 +558,8 @@ int Credits::IsMoreAIDecksUnlocked(DeckStats * stats) {
// the number of currently unlocked decks in order to go through. // the number of currently unlocked decks in order to go through.
if (stats->nbGames() < currentlyUnlocked * 1.2) return 0; if (stats->nbGames() < currentlyUnlocked * 1.2) return 0;
int nbdecks = 0; if (AIPlayer::getTotalAIDecks() > currentlyUnlocked)
int found = 1; return 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;
}
}
return 0; return 0;
} }
+7 -8
View File
@@ -20,6 +20,7 @@
#include "DeckEditorMenu.h" #include "DeckEditorMenu.h"
#include "SimpleMenu.h" #include "SimpleMenu.h"
#include "utils.h" #include "utils.h"
#include "AIPlayer.h"
@@ -315,24 +316,22 @@ void GameStateDeckViewer::saveDeck()
void GameStateDeckViewer::saveAsAIDeck(string deckName) void GameStateDeckViewer::saveAsAIDeck(string deckName)
{ {
vector<DeckMetaData *> aiDecks = GameState::BuildDeckList("ai/baka", "ai_baka", NULL); int deckId = AIPlayer::getTotalAIDecks() + 1;
int nbAiDecks = aiDecks.size() + 1;
aiDecks.clear();
string defaultAiDeckName = "deck";
std::ostringstream oss; std::ostringstream oss;
oss << "deck" << nbAiDecks; oss << "deck" <<deckId;
defaultAiDeckName = oss.str(); string aiDeckName = oss.str();
oss.str(""); oss.str("");
if (myDeck->parent->meta_desc == "") if (myDeck->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 else
oss << myDeck->parent->meta_desc; oss << myDeck->parent->meta_desc;
string deckDesc = oss.str(); string deckDesc = oss.str();
string filepath = "ai/baka/"; string filepath = "ai/baka/";
filepath.append(defaultAiDeckName).append(".txt"); filepath.append(aiDeckName).append(".txt");
DebugTrace("saving AI deck " << filepath); DebugTrace("saving AI deck " << filepath);
myDeck->save(filepath, true, deckName, deckDesc); 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) void GameStateDeckViewer::Update(float dt)
+1 -1
View File
@@ -26,7 +26,7 @@
#include <JNetwork.h> #include <JNetwork.h>
#endif//NETWORK_SUPPORT #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 enum ENUM_MENU_STATE_MAJOR
{ {
-22
View File
@@ -88,28 +88,6 @@ int WRand(bool log)
return rand(); 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) bool fileExists(const char * filename)
{ {
return JFileSystem::GetInstance()->FileExists(filename); return JFileSystem::GetInstance()->FileExists(filename);