From d9efb408e5975db9a9fda55279e52b92983d233b Mon Sep 17 00:00:00 2001 From: "techdragon.nguyen@gmail.com" Date: Mon, 14 Feb 2011 08:14:35 +0000 Subject: [PATCH] * optimized card loading. There was some redundant code that wasn't necessary. * getCardByName seemed to have a initialization error if you tried to use it before the entire game loaded. The cache would throw an exception if you tried to use find and it was empty. I put a guard around it to avoid this issue. * refactored Zeth's "toggledifficuly" feature to be stored in meta data. http://code.google.com/p/wagic/source/detail?r=3106 -- This is slightly modified as it forces a 1 for 1 swap of cards that are specified. from the example given this is how it seemed to be used anyways. -- since all the information is stored in the meta data, there's no need to alter the deck's definition. --- projects/mtg/include/DeckMetaData.h | 2 + projects/mtg/include/MTGDeck.h | 4 +- projects/mtg/src/AIPlayer.cpp | 13 +---- projects/mtg/src/DeckMetaData.cpp | 6 +++ projects/mtg/src/MTGDeck.cpp | 74 ++++++++++++----------------- projects/mtg/src/MTGGameZones.cpp | 14 +++++- 6 files changed, 57 insertions(+), 56 deletions(-) diff --git a/projects/mtg/include/DeckMetaData.h b/projects/mtg/include/DeckMetaData.h index f26ca2282..48329373c 100644 --- a/projects/mtg/include/DeckMetaData.h +++ b/projects/mtg/include/DeckMetaData.h @@ -24,6 +24,7 @@ private: string mAvatarFilename; string mColorIndex; + map mAlternateCardMap; // statistical information int mGamesPlayed, mVictories, mPercentVictories, mDifficulty; @@ -44,6 +45,7 @@ public: string getColorIndex(); int getAvatarId(int deckId); string getStatsSummary(); + map& getAlternateMappings(); int getDeckId(); int getGamesPlayed(); diff --git a/projects/mtg/include/MTGDeck.h b/projects/mtg/include/MTGDeck.h index 5b06687cd..7da5892b0 100644 --- a/projects/mtg/include/MTGDeck.h +++ b/projects/mtg/include/MTGDeck.h @@ -120,6 +120,7 @@ public: MTGCard * _(int id); MTGCard * getCardById(int id); MTGCard * getCardByName(string name); + MTGCard * getCard(string inputText); int load(const char * config_file, const char * setName = NULL, int autoload = 1); int countByType(const char * _type); int countByColor(int color); @@ -154,13 +155,14 @@ protected: public: MTGAllCards * database; map cards; + map alternates; string meta_desc; string meta_name; int meta_id; int totalCards(); int totalPrice(); MTGDeck(MTGAllCards * _allcards); - MTGDeck(const char * config_file, MTGAllCards * _allcards, int meta_only = 0,int difficultySetting = 0); + MTGDeck(const char * config_file, MTGAllCards * _allcards, int meta_only = 0); int addRandomCards(int howmany, int * setIds = NULL, int nbSets = 0, int rarity = -1, const char * subtype = NULL, int * colors = NULL, int nbcolors = 0); int add(int cardid); diff --git a/projects/mtg/src/AIPlayer.cpp b/projects/mtg/src/AIPlayer.cpp index e1e6aca63..3ae3b5aa8 100644 --- a/projects/mtg/src/AIPlayer.cpp +++ b/projects/mtg/src/AIPlayer.cpp @@ -1180,17 +1180,8 @@ AIPlayer * AIPlayerFactory::createAIPlayer(MTGAllCards * collection, Player * op sprintf(deckFileSmall, "ai_baka_deck%i", deckid); } DeckStats * stats = DeckStats::GetInstance(); -int deckSetting = NULL; -int diff = stats->percentVictories(); - if (diff >= 65) - { - deckSetting = HARD; - } - else if (diff < 65) - { - deckSetting = EASY; - } - MTGDeck * tempDeck = NEW MTGDeck(deckFile, collection,0,deckSetting); + + MTGDeck * tempDeck = NEW MTGDeck(deckFile, collection, 0); AIPlayerBaka * baka = NEW AIPlayerBaka(tempDeck, deckFile, deckFileSmall, avatarFile); baka->deckId = deckid; SAFE_DELETE(tempDeck); diff --git a/projects/mtg/src/DeckMetaData.cpp b/projects/mtg/src/DeckMetaData.cpp index 64c854a88..251183c9f 100644 --- a/projects/mtg/src/DeckMetaData.cpp +++ b/projects/mtg/src/DeckMetaData.cpp @@ -96,6 +96,7 @@ void DeckMetaData::LoadDeck() mDescription = trim(deck.meta_desc); mDeckId = atoi((mFilename.substr(mFilename.find("deck") + 4, mFilename.find(".txt"))).c_str()); mDeckLoaded = true; + mAlternateCardMap = deck.alternates; } @@ -181,6 +182,11 @@ string DeckMetaData::getStatsSummary() return statsSummary.str(); } +map& DeckMetaData::getAlternateMappings() +{ + return mAlternateCardMap; +} + void DeckMetaData::setColorIndex(const string& colorIndex) { mColorIndex = colorIndex; diff --git a/projects/mtg/src/MTGDeck.cpp b/projects/mtg/src/MTGDeck.cpp index 1c9e86fb4..b7a912da2 100644 --- a/projects/mtg/src/MTGDeck.cpp +++ b/projects/mtg/src/MTGDeck.cpp @@ -676,16 +676,18 @@ MTGCard * MTGAllCards::getCardByName(string name) if (!name.size()) return NULL; if (name[0] == '#') return NULL; - map::iterator cached = mtgCardByNameCache.find(name); + map::iterator cached = mtgCardByNameCache.end(); + if ( mtgCardByNameCache.size() > 0 ) + cached = mtgCardByNameCache.find(name); if (cached!= mtgCardByNameCache.end()) { return cached->second; } - int cardnb = atoi(name.c_str()); - if (cardnb) + int cardId = atoi(name.c_str()); + if (cardId) { - MTGCard * result = getCardById(cardnb); + MTGCard * result = getCardById(cardId); mtgCardByNameCache[name] = result; return result; } @@ -742,7 +744,7 @@ int MTGDeck::totalPrice() return total; } -MTGDeck::MTGDeck(const char * config_file, MTGAllCards * _allcards, int meta_only,int difficultyRating) +MTGDeck::MTGDeck(const char * config_file, MTGAllCards * _allcards, int meta_only) { total_cards = 0; database = _allcards; @@ -775,58 +777,44 @@ MTGDeck::MTGDeck(const char * config_file, MTGAllCards * _allcards, int meta_onl meta_desc.append(s.substr(found + 5)); continue; } + + found = s.find("toggledifficulty:"); + if (found != string::npos) + { + string cards = s.substr(found + 17); + size_t separator = cards.find("|"); + string cardeasy = cards.substr(0,separator); + string cardhard = cards.substr(separator + 1); + MTGCard *sourceCard = MTGCollection()->getCardByName( cardeasy ); + MTGCard *alternateCard = MTGCollection()->getCardByName( cardhard ); + if ( alternateCard && sourceCard ) + alternates.insert( make_pair( sourceCard->getMTGId(), alternateCard->getMTGId())); + else + DebugTrace("Failed to map card Alternates: [" << cardeasy << "] and [" << cardhard << "]"); + continue; + } + continue; } if (meta_only) break; - int nb = 1; + int numberOfCopies = 1; size_t found = s.find(" *"); if (found != string::npos) { - nb = atoi(s.substr(found + 2).c_str()); + numberOfCopies = atoi(s.substr(found + 2).c_str()); s = s.substr(0, found); } - size_t diff = s.find("toggledifficulty:"); - if(diff != string::npos) + MTGCard *card = database->getCardByName( s ); + if ( card ) { - string cards = s.substr(diff + 17); - size_t separator = cards.find("|"); - string cardeasy = cards.substr(0,separator); - string cardhard = cards.substr(separator + 1); - if(difficultyRating == HARD) + for (int i = 0; i < numberOfCopies; i++) { - s = cardhard; + add(card); } - else - { - s = cardeasy; - } - } - int cardnb = atoi(s.c_str()); - if (cardnb) - { - add(cardnb); } else { - size_t found = s.find(" *"); - if (found != string::npos) - { - nb = atoi(s.substr(found + 2).c_str()); - s = s.substr(0, found); - } - MTGCard * card = database->getCardByName(s); - if (card) - { - for (int i = 0; i < nb; i++) - { - add(card); - } - } - - else - { - DebugTrace("could not find Card matching name: " << s); - } + DebugTrace("could not find Card matching name: " << s); } } file.close(); diff --git a/projects/mtg/src/MTGGameZones.cpp b/projects/mtg/src/MTGGameZones.cpp index 65c1628db..d45ccb828 100644 --- a/projects/mtg/src/MTGGameZones.cpp +++ b/projects/mtg/src/MTGGameZones.cpp @@ -1,5 +1,7 @@ #include "PrecompiledHeader.h" +#include "DeckManager.h" +#include "DeckMetaData.h" #include "MTGGameZones.h" #include "Player.h" #include "GameOptions.h" @@ -43,10 +45,20 @@ MTGPlayerCards::MTGPlayerCards(MTGDeck * deck) void MTGPlayerCards::initDeck(MTGDeck * deck) { resetLibrary(); + bool isAI = deck->getFilename().find("baka") != string::npos; + DeckMetaData *deckMeta = DeckManager::GetInstance()->getDeckMetaDataById( deck->meta_id, isAI); + map alternatesMap = deckMeta->getAlternateMappings(); + bool useAlternates = deckMeta->getVictoryPercentage() >= 65; map::iterator it; for (it = deck->cards.begin(); it != deck->cards.end(); it++) { - MTGCard * card = deck->getCardById(it->first); + int cardId = it->first; + if (useAlternates && isAI) + { + if ( alternatesMap.find( cardId ) != alternatesMap.end() ) + cardId = alternatesMap[ cardId ]; + } + MTGCard * card = deck->getCardById(cardId); if (card) { for (int i = 0; i < it->second; i++)