Hid Subtypes behind MTGAllCards and added a mutex to be able to use both singleton from several threads.

This commit is contained in:
Xawotihs@gmail.com
2012-02-12 22:20:05 +00:00
parent ebc4eb331f
commit bd1afebbea
15 changed files with 74 additions and 54 deletions
+4 -4
View File
@@ -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<string> values = Subtypes::subtypesList->getValuesById();
const vector<string> 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;
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<int>& types, const string& subTypesStringL
for (vector<string>::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);
}
+5 -5
View File
@@ -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;
}
+4 -4
View File
@@ -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());
+7 -7
View File
@@ -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;
}
}
-2
View File
@@ -311,8 +311,6 @@ void GameApp::Destroy()
DeckManager::EndInstance();
DeckStats::EndInstance();
SAFE_DELETE(Subtypes::subtypesList);
stopMusic();
Translator::EndInstance();
+1 -1
View File
@@ -2008,7 +2008,7 @@ MTGAbility * AbilityFactory::parseMagicLine(string s, int id, Spell * spell, MTG
vector<string> 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);
}
+8 -8
View File
@@ -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<string> values = Subtypes::subtypesList->getValuesById();
const vector<string> 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())
+2 -7
View File
@@ -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<int, MTGCard *>::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<int>::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.
+1 -1
View File
@@ -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))
+1 -3
View File
@@ -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<string>& Subtypes::getValuesById()
{
return valuesById;
}
}
+5 -5
View File
@@ -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;
}
+1 -1
View File
@@ -2057,7 +2057,7 @@ void WGuiFilterItem::updateValue()
vector<string> 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")