From bd1afebbea247aec2cd31fc58c5260bf0fa1595a Mon Sep 17 00:00:00 2001 From: "Xawotihs@gmail.com" Date: Sun, 12 Feb 2012 22:20:05 +0000 Subject: [PATCH] Hid Subtypes behind MTGAllCards and added a mutex to be able to use both singleton from several threads. --- projects/mtg/include/AllAbilities.h | 6 ++--- projects/mtg/include/MTGDeck.h | 33 ++++++++++++++++++++++++++-- projects/mtg/include/Subtypes.h | 2 +- projects/mtg/src/AllAbilities.cpp | 8 +++---- projects/mtg/src/CardDescriptor.cpp | 10 ++++----- projects/mtg/src/CardGui.cpp | 8 +++---- projects/mtg/src/CardPrimitive.cpp | 14 ++++++------ projects/mtg/src/GameApp.cpp | 2 -- projects/mtg/src/MTGAbility.cpp | 2 +- projects/mtg/src/MTGCardInstance.cpp | 16 +++++++------- projects/mtg/src/MTGDeck.cpp | 9 ++------ projects/mtg/src/MTGGameZones.cpp | 2 +- projects/mtg/src/Subtypes.cpp | 4 +--- projects/mtg/src/TargetChooser.cpp | 10 ++++----- projects/mtg/src/WGui.cpp | 2 +- 15 files changed, 74 insertions(+), 54 deletions(-) diff --git a/projects/mtg/include/AllAbilities.h b/projects/mtg/include/AllAbilities.h index 197bf77ad..2da07339a 100644 --- a/projects/mtg/include/AllAbilities.h +++ b/projects/mtg/include/AllAbilities.h @@ -2506,13 +2506,13 @@ public: size_t found = s.find(" "); if (found != string::npos) { - int id = Subtypes::subtypesList->find(s.substr(0, found)); + int id = MTGAllCards::findType(s.substr(0, found)); types.push_back(id); s = s.substr(found + 1); } else { - int id = Subtypes::subtypesList->find(s); + int id = MTGAllCards::findType(s); types.push_back(id); s = ""; } @@ -5066,6 +5066,6 @@ public: void PopulateColorIndexVector(list& colors, const string& colorsString, char delimiter = ','); void PopulateAbilityIndexVector(list& abilities, const string& abilitiesString, char delimiter = ','); -void PopulateSubtypesIndexVector(list& subtypes, const string& subtypesString, char delimiter = ' '); +void PopulateSubtypesIndexVector(list& types, const string& subtypesString, char delimiter = ' '); #endif diff --git a/projects/mtg/include/MTGDeck.h b/projects/mtg/include/MTGDeck.h index a18e3cd5b..4318cf560 100644 --- a/projects/mtg/include/MTGDeck.h +++ b/projects/mtg/include/MTGDeck.h @@ -7,7 +7,8 @@ #include "GameApp.h" #include "WResourceManager.h" #include - +#include +#include #include using std::string; @@ -104,7 +105,6 @@ protected: void init(); void initCounters(); MTGAllCards(); - MTGAllCards(const char * config_file, const char * set_name); ~MTGAllCards(); public: @@ -132,11 +132,40 @@ public: int totalCards(); int randomCardId(); + static int findType(string subtype, bool forceAdd = true) { + boost::mutex::scoped_lock lock(instance->mMutex); + return instance->subtypesList.find(subtype, forceAdd); + }; + static int add(string value, unsigned int parentType) { + boost::mutex::scoped_lock lock(instance->mMutex); + return instance->subtypesList.add(value, parentType); + }; + static string findType(unsigned int id) { + return instance->subtypesList.find(id); + }; + static const vector& getValuesById() { + return instance->subtypesList.getValuesById(); + }; + static bool isSubtypeOfType(unsigned int subtype, unsigned int type) { + return instance->subtypesList.isSubtypeOfType(subtype, type); + }; + static bool isSuperType(unsigned int type) { + return instance->subtypesList.isSuperType(type); + }; + static bool isType(unsigned int type) { + return instance->subtypesList.isType(type); + }; + static bool isSubType(unsigned int type) { + return instance->subtypesList.isSubType(type); + }; + static void loadInstance(); static void unloadAll(); static inline MTGAllCards* getInstance() { return instance; }; private: + boost::mutex mMutex; + Subtypes subtypesList; map mtgCardByNameCache; int processConfLine(string &s, MTGCard* card, CardPrimitive * primitive); bool addCardToCollection(MTGCard * card, int setId); diff --git a/projects/mtg/include/Subtypes.h b/projects/mtg/include/Subtypes.h index ebcefb29e..e61923977 100644 --- a/projects/mtg/include/Subtypes.h +++ b/projects/mtg/include/Subtypes.h @@ -4,6 +4,7 @@ #include #include #include + using namespace std; class Subtypes @@ -37,7 +38,6 @@ protected: vector valuesById; vector subtypesToType; public: - static Subtypes * subtypesList; Subtypes(); int find(string subtype, bool forceAdd = true); string find(unsigned int id); diff --git a/projects/mtg/src/AllAbilities.cpp b/projects/mtg/src/AllAbilities.cpp index d3fcd6c00..6a7d349ce 100644 --- a/projects/mtg/src/AllAbilities.cpp +++ b/projects/mtg/src/AllAbilities.cpp @@ -2978,10 +2978,10 @@ ATransformer::ATransformer(GameObserver* observer, int id, MTGCardInstance * sou if (stypes.find("allsubtypes") != string::npos || stypes.find("removecreaturesubtypes") != string::npos) { - const vector values = Subtypes::subtypesList->getValuesById(); + const vector values = MTGAllCards::getValuesById(); for (size_t i = 0; i isSubtypeOfType(i,Subtypes::TYPE_CREATURE)) + if (!MTGAllCards::isSubtypeOfType(i,Subtypes::TYPE_CREATURE)) continue; types.push_back(i); @@ -3482,7 +3482,7 @@ int ALoseSubtypes::addToGame() for (int i = ((int)_target->types.size())-1; i >= 0; --i) { int subtype = _target->types[i]; - if (Subtypes::subtypesList->isSubtypeOfType(subtype, parentType)) + if (MTGAllCards::isSubtypeOfType(subtype, parentType)) { storedSubtypes.push_back(subtype); _target->removeType(subtype); @@ -4506,7 +4506,7 @@ void PopulateSubtypesIndexVector(list& types, const string& subTypesStringL for (vector::iterator it = subTypesList.begin(); it != subTypesList.end(); ++it) { string subtype = *it; - size_t id = Subtypes::subtypesList->find(subtype); + size_t id = MTGAllCards::findType(subtype); if (id != string::npos) types.push_back(id); } diff --git a/projects/mtg/src/CardDescriptor.cpp b/projects/mtg/src/CardDescriptor.cpp index cfecee6d4..367f6bece 100644 --- a/projects/mtg/src/CardDescriptor.cpp +++ b/projects/mtg/src/CardDescriptor.cpp @@ -55,7 +55,7 @@ void CardDescriptor::setisMultiColored(int w) void CardDescriptor::setNegativeSubtype(string value) { - int id = Subtypes::subtypesList->find(value); + int id = MTGAllCards::findType(value); addType(-id); } @@ -91,7 +91,7 @@ MTGCardInstance * CardDescriptor::match_or(MTGCardInstance * card) if (types[i] >= 0) { - if (card->hasSubtype(types[i]) || (Subtypes::subtypesList->find(card->getLCName(), false) == types[i])) + if (card->hasSubtype(types[i]) || (MTGAllCards::findType(card->getLCName(), false) == types[i])) { found = 1; break; @@ -99,7 +99,7 @@ MTGCardInstance * CardDescriptor::match_or(MTGCardInstance * card) } else { - if (!card->hasSubtype(-types[i]) && (Subtypes::subtypesList->find(card->getLCName(), false) != -types[i])) + if (!card->hasSubtype(-types[i]) && (MTGAllCards::findType(card->getLCName(), false) != -types[i])) { found = 1; break; @@ -142,14 +142,14 @@ MTGCardInstance * CardDescriptor::match_and(MTGCardInstance * card) { if (types[i] >= 0) { - if (!card->hasSubtype(types[i]) && !(Subtypes::subtypesList->find(card->getLCName(), false) == types[i])) + if (!card->hasSubtype(types[i]) && !(MTGAllCards::findType(card->getLCName(), false) == types[i])) { match = NULL; } } else { - if (card->hasSubtype(-types[i]) || (Subtypes::subtypesList->find(card->getLCName(), false) == -types[i])) + if (card->hasSubtype(-types[i]) || (MTGAllCards::findType(card->getLCName(), false) == -types[i])) { match = NULL; } diff --git a/projects/mtg/src/CardGui.cpp b/projects/mtg/src/CardGui.cpp index d111b64e5..6b326113c 100644 --- a/projects/mtg/src/CardGui.cpp +++ b/projects/mtg/src/CardGui.cpp @@ -544,12 +544,12 @@ void CardGui::AlternateRender(MTGCard * card, const Pos& pos) } else { - s += _(Subtypes::subtypesList->find(card->data->types[i])); + s += _(MTGAllCards::findType(card->data->types[i])); s += _(" - "); } } if (card->data->types.size()) - s += _(Subtypes::subtypesList->find(card->data->types[0])); + s += _(MTGAllCards::findType(card->data->types[0])); else { DebugTrace("Typeless card: " << setlist[card->setId].c_str() << card->data->getName() << card->getId()); @@ -876,12 +876,12 @@ void CardGui::TinyCropRender(MTGCard * card, const Pos& pos, JQuad * quad) } else { - s += _(Subtypes::subtypesList->find(card->data->types[i])); + s += _(MTGAllCards::findType(card->data->types[i])); s += _(" - "); } } if (card->data->types.size()) - s += _(Subtypes::subtypesList->find(card->data->types[0])); + s += _(MTGAllCards::findType(card->data->types[0])); else { DebugTrace("Typeless card: " << setlist[card->setId].c_str() << card->data->getName() << card->getId()); diff --git a/projects/mtg/src/CardPrimitive.cpp b/projects/mtg/src/CardPrimitive.cpp index fb6030c33..4d814cd95 100644 --- a/projects/mtg/src/CardPrimitive.cpp +++ b/projects/mtg/src/CardPrimitive.cpp @@ -209,13 +209,13 @@ void CardPrimitive::setSubtype(const string& value) int parentType = 0; for (size_t i = 0; i < types.size(); ++i) { - if (Subtypes::subtypesList->isType(types[i])) + if (MTGAllCards::isType(types[i])) { parentType = types[i]; break; } } - int id = Subtypes::subtypesList->add(value, parentType); + int id = MTGAllCards::add(value, parentType); addType(id); } @@ -230,7 +230,7 @@ void CardPrimitive::addType(int id) int CardPrimitive::removeType(string value, int removeAll) { - int id = Subtypes::subtypesList->find(value); + int id = MTGAllCards::findType(value); return removeType(id, removeAll); } @@ -335,19 +335,19 @@ bool CardPrimitive::hasSubtype(int _subtype) bool CardPrimitive::hasType(const char * _type) { - int id = Subtypes::subtypesList->find(_type); + int id = MTGAllCards::findType(_type); return hasType(id); } bool CardPrimitive::hasSubtype(const char * _subtype) { - int id = Subtypes::subtypesList->find(_subtype); + int id = MTGAllCards::findType(_subtype); return hasType(id); } bool CardPrimitive::hasSubtype(const string& _subtype) { - int id = Subtypes::subtypesList->find(_subtype); + int id = MTGAllCards::findType(_subtype); return hasType(id); } @@ -417,4 +417,4 @@ uint8_t CardPrimitive::ConvertColorToBitMask(int inColor) } return value; -} \ No newline at end of file +} diff --git a/projects/mtg/src/GameApp.cpp b/projects/mtg/src/GameApp.cpp index 3b08ee9bc..ebaad8584 100644 --- a/projects/mtg/src/GameApp.cpp +++ b/projects/mtg/src/GameApp.cpp @@ -311,8 +311,6 @@ void GameApp::Destroy() DeckManager::EndInstance(); DeckStats::EndInstance(); - SAFE_DELETE(Subtypes::subtypesList); - stopMusic(); Translator::EndInstance(); diff --git a/projects/mtg/src/MTGAbility.cpp b/projects/mtg/src/MTGAbility.cpp index 8bb158886..a8f89c37f 100644 --- a/projects/mtg/src/MTGAbility.cpp +++ b/projects/mtg/src/MTGAbility.cpp @@ -2008,7 +2008,7 @@ MTGAbility * AbilityFactory::parseMagicLine(string s, int id, Spell * spell, MTG vector splitLoseTypes = parseBetween(s, "losesubtypesof(", ")"); if (splitLoseTypes.size()) { - int parentType = Subtypes::subtypesList->find(splitLoseTypes[1]); + int parentType = MTGAllCards::findType(splitLoseTypes[1]); return NEW ALoseSubtypes(observer, id, card, target, parentType); } diff --git a/projects/mtg/src/MTGCardInstance.cpp b/projects/mtg/src/MTGCardInstance.cpp index f103e09b5..5218fcaa2 100644 --- a/projects/mtg/src/MTGCardInstance.cpp +++ b/projects/mtg/src/MTGCardInstance.cpp @@ -178,10 +178,10 @@ void MTGCardInstance::initMTGCI() if (basicAbilities[(int)Constants::CHANGELING]) {//if the card is a changeling, it gains all creature subtypes - const vector values = Subtypes::subtypesList->getValuesById(); + const vector values = MTGAllCards::getValuesById(); for (size_t i = 0; i < values.size(); ++i) { - if (!Subtypes::subtypesList->isSubtypeOfType(i,Subtypes::TYPE_CREATURE)) + if (!MTGAllCards::isSubtypeOfType(i,Subtypes::TYPE_CREATURE)) continue; //Don' want to send any event to the gameObserver inside of initMCGI, so calling the parent addType method instead of mine @@ -225,7 +225,7 @@ void MTGCardInstance::addType(int type) //This is a hack for "transform", used in combination with "losesubtypes" on Basic Lands //See removeType below if (!name.length()) - setName(Subtypes::subtypesList->find(type)); + setName(MTGAllCards::findType(type)); WEvent * e = NEW WEventCardChangeType(this, type, before, true); if (observer) @@ -246,12 +246,12 @@ void MTGCardInstance::setType(const char * type_text) void MTGCardInstance::setSubtype(string value) { - int id = Subtypes::subtypesList->find(value); + int id = MTGAllCards::findType(value); addType(id); } int MTGCardInstance::removeType(string value, int removeAll) { - int id = Subtypes::subtypesList->find(value); + int id = MTGAllCards::findType(value); return removeType(id, removeAll); } @@ -267,7 +267,7 @@ int MTGCardInstance::removeType(int id, int removeAll) // so if we remove a subtype "Forest", we also need to remove its name. //This means the card might lose its name, but usually when we force remove a type, we add another one just after that. //see "AddType" above which force sets a name if necessary - if (name.compare(Subtypes::subtypesList->find(id)) == 0) + if (name.compare(MTGAllCards::findType(id)) == 0) setName(""); } WEvent * e = NEW WEventCardChangeType(this, id, before, after); @@ -1030,7 +1030,7 @@ const string& MTGCardInstance::getSample() for (int i = types.size() - 1; i > 0; i--) { - string type = Subtypes::subtypesList->find(types[i]); + string type = MTGAllCards::findType(types[i]); std::transform(type.begin(), type.end(), type.begin(), ::tolower); type = type + ".wav"; if(getObserver() && getObserver()->getResourceManager()) @@ -1065,7 +1065,7 @@ const string& MTGCardInstance::getSample() string type = ""; if(!types.size()) return sample; - type = Subtypes::subtypesList->find(types[0]); + type = MTGAllCards::findType(types[0]); std::transform(type.begin(), type.end(), type.begin(), ::tolower); type.append(".wav"); if(getObserver() && getObserver()->getResourceManager()) diff --git a/projects/mtg/src/MTGDeck.cpp b/projects/mtg/src/MTGDeck.cpp index 1e3268429..dc3983fa9 100644 --- a/projects/mtg/src/MTGDeck.cpp +++ b/projects/mtg/src/MTGDeck.cpp @@ -419,12 +419,6 @@ MTGAllCards::MTGAllCards() init(); } -MTGAllCards::MTGAllCards(const char * config_file, const char * set_name) -{ - init(); - load(config_file, set_name, 0); -} - MTGAllCards::~MTGAllCards() { for (map::iterator it = collection.begin(); it != collection.end(); it++) @@ -637,6 +631,7 @@ void MTGAllCards::prefetchCardNameCache() MTGCard * MTGAllCards::getCardByName(string nameDescriptor) { + boost::mutex::scoped_lock lock(instance->mMutex); if (!nameDescriptor.size()) return NULL; if (nameDescriptor[0] == '#') return NULL; @@ -1085,7 +1080,7 @@ void MTGDeck::printDetailedDeckText(std::ofstream& file ) // Add the card's types vector::iterator typeIter; for ( typeIter = card->data->types.begin(); typeIter != card->data->types.end(); ++typeIter ) - types << Subtypes::subtypesList->find( *typeIter ) << " "; + types << MTGAllCards::findType( *typeIter ) << " "; currentCard << trim(types.str()) << ", "; types.str(""); // reset the buffer. diff --git a/projects/mtg/src/MTGGameZones.cpp b/projects/mtg/src/MTGGameZones.cpp index 9a2793a46..03b449566 100644 --- a/projects/mtg/src/MTGGameZones.cpp +++ b/projects/mtg/src/MTGGameZones.cpp @@ -504,7 +504,7 @@ size_t MTGGameZone::getIndex(MTGCardInstance * card) unsigned int MTGGameZone::countByType(const char * value) { int result = 0; - int subTypeId = Subtypes::subtypesList->find(value); + int subTypeId = MTGAllCards::findType(value); for (int i = 0; i < (nb_cards); i++) { if (cards[i]->hasType(subTypeId)) diff --git a/projects/mtg/src/Subtypes.cpp b/projects/mtg/src/Subtypes.cpp index a23ead76c..f319c9f10 100644 --- a/projects/mtg/src/Subtypes.cpp +++ b/projects/mtg/src/Subtypes.cpp @@ -2,8 +2,6 @@ #include "Subtypes.h" -Subtypes * Subtypes::subtypesList = NEW Subtypes(); - Subtypes::Subtypes() { //Add the more common types, so that they can be accessed through ints @@ -101,4 +99,4 @@ bool Subtypes::isSubType(unsigned int type) const vector& Subtypes::getValuesById() { return valuesById; -} \ No newline at end of file +} diff --git a/projects/mtg/src/TargetChooser.cpp b/projects/mtg/src/TargetChooser.cpp index b03852b9c..d1dc26d24 100644 --- a/projects/mtg/src/TargetChooser.cpp +++ b/projects/mtg/src/TargetChooser.cpp @@ -930,7 +930,7 @@ bool CardTargetChooser::equals(TargetChooser * tc) TypeTargetChooser::TypeTargetChooser(GameObserver *observer, const char * _type, MTGCardInstance * card, int _maxtargets, bool other,bool targetMin) : TargetZoneChooser(observer, card, _maxtargets, other,targetMin) { - int id = Subtypes::subtypesList->find(_type); + int id = MTGAllCards::findType(_type); nbtypes = 0; addType(id); int default_zones[] = { MTGGameZone::MY_BATTLEFIELD, MTGGameZone::OPPONENT_BATTLEFIELD }; @@ -941,7 +941,7 @@ TypeTargetChooser::TypeTargetChooser(GameObserver *observer, const char * _type, bool other,bool targetMin) : TargetZoneChooser(observer, card, _maxtargets, other,targetMin) { - int id = Subtypes::subtypesList->find(_type); + int id = MTGAllCards::findType(_type); nbtypes = 0; addType(id); if (nbzones == 0) @@ -957,7 +957,7 @@ TypeTargetChooser::TypeTargetChooser(GameObserver *observer, const char * _type, void TypeTargetChooser::addType(const char * _type) { - int id = Subtypes::subtypesList->find(_type); + int id = MTGAllCards::findType(_type); addType(id); } @@ -978,7 +978,7 @@ bool TypeTargetChooser::canTarget(Targetable * target,bool withoutProtections) if (card->hasSubtype(types[i])) return true; if(card->getLCName().size()) { - if (Subtypes::subtypesList->find(card->getLCName()) == types[i]) return true; + if (MTGAllCards::findType(card->getLCName()) == types[i]) return true; } } return false; @@ -993,7 +993,7 @@ bool TypeTargetChooser::canTarget(Targetable * target,bool withoutProtections) for (int i = 0; i < nbtypes; i++) { if (card->hasSubtype(types[i])) return true; - if (Subtypes::subtypesList->find(card->name) == types[i]) return true; + if (MTGAllCards::findType(card->name) == types[i]) return true; } return false; } diff --git a/projects/mtg/src/WGui.cpp b/projects/mtg/src/WGui.cpp index 12b113346..9d1ee3f17 100644 --- a/projects/mtg/src/WGui.cpp +++ b/projects/mtg/src/WGui.cpp @@ -2057,7 +2057,7 @@ void WGuiFilterItem::updateValue() vector stlist; for (int i = Subtypes::LAST_TYPE + 1;; i++) { - string s = Subtypes::subtypesList->find(i); + string s = MTGAllCards::findType(i); if (s == "") break; if (s.find(" ") != string::npos) continue; if (s == "Nothing")