- 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

View File

@@ -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<AIAction *> clickstream;
@@ -98,6 +101,9 @@ public:
RandomGenerator* getRandomGenerator(){return &randomGenerator;};
bool parseLine(const string& s);
static int getTotalAIDecks();
static void invalidateTotalAIDecks();
};

View File

@@ -86,7 +86,6 @@ public:
};
};
int filesize(const char * filename);
int WRand(bool log = false);
#ifdef LINUX

View File

@@ -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;
}

View File

@@ -8,7 +8,7 @@
#include "MTGDeck.h"
#include "GameObserver.h"
#include "GameStateShop.h"
#include "PlayerData.h"
#include "AIPlayer.h"
map <string, Unlockable *> 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;
}

View File

@@ -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<DeckMetaData *> 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" <<deckId;
string aiDeckName = oss.str();
oss.str("");
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
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)

View File

@@ -26,7 +26,7 @@
#include <JNetwork.h>
#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
{

View File

@@ -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);