- Moved Cast Restriction variables "out" of CardPrimitives (replaced with a pointer). This frees 64bytes off of CardPrimitives on Windows, I assume something similar on the PSP, which gives a result of more than 500kB freed

- fix for issue 716 (text not rendered in some cases) 
Test suite passes
This commit is contained in:
wagic.the.homebrew
2011-08-07 05:05:28 +00:00
parent 9f3a42d0c6
commit 765b6b0412
4 changed files with 99 additions and 69 deletions
+19 -5
View File
@@ -1,3 +1,8 @@
/* CardPrimitive objects represent the cards database.
* For MTG we have thousands of those, that stay constantly in Ram
* on low-end devices such as the PSP, adding stuff to this class can have a very high cost
* As an example, with 16'000 card primitives (the rough number of cards in MTG), adding a simple 16 bytes attribute costs 250kB (2% of the total available ram on the PSP)
*/
#ifndef _CARDPRIMITIVE_H_ #ifndef _CARDPRIMITIVE_H_
#define _CARDPRIMITIVE_H_ #define _CARDPRIMITIVE_H_
@@ -21,6 +26,17 @@ const uint8_t kColorBitMask_White = 0x20;
const uint8_t kColorBitMask_Land = 0x40; const uint8_t kColorBitMask_Land = 0x40;
class CastRestrictions {
public:
string restriction;
string otherrestriction;
CastRestrictions * clone() const
{
return NEW CastRestrictions(*this);
};
};
class CardPrimitive class CardPrimitive
#ifdef TRACK_OBJECT_USAGE #ifdef TRACK_OBJECT_USAGE
: public InstanceCounter<CardPrimitive> : public InstanceCounter<CardPrimitive>
@@ -29,6 +45,7 @@ class CardPrimitive
private: private:
string text; string text;
vector<string> formattedText; vector<string> formattedText;
CastRestrictions * restrictions;
protected: protected:
string lcname; string lcname;
@@ -48,9 +65,6 @@ public:
string spellTargetType; string spellTargetType;
int power; int power;
int toughness; int toughness;
bool hasRestriction;
string restriction;
string otherrestriction;
int suspendedTime; int suspendedTime;
vector<int>types; vector<int>types;
@@ -102,9 +116,9 @@ public:
void setToughness(int _toughness); void setToughness(int _toughness);
int getToughness(); int getToughness();
void setRestrictions(string _restriction); void setRestrictions(string _restriction);
void getRestrictions(); const string getRestrictions();
void setOtherRestrictions(string _restriction); void setOtherRestrictions(string _restriction);
void getOtherRestrictions(); const string getOtherRestrictions();
}; };
+22 -9
View File
@@ -49,12 +49,12 @@ CardPrimitive::CardPrimitive(CardPrimitive * source)
manaCost.alternative->alternativeName = source->getManaCost()->alternative->alternativeName; manaCost.alternative->alternativeName = source->getManaCost()->alternative->alternativeName;
text = source->text; text = source->text;
formattedText = source->formattedText;
setName(source->name); setName(source->name);
power = source->power; power = source->power;
toughness = source->toughness; toughness = source->toughness;
restriction = source->restriction; restrictions = source->restrictions ? source->restrictions->clone() : NULL;
otherrestriction = source->otherrestriction;
suspendedTime = source->suspendedTime; suspendedTime = source->suspendedTime;
magicText = source->magicText; magicText = source->magicText;
@@ -66,6 +66,7 @@ CardPrimitive::CardPrimitive(CardPrimitive * source)
CardPrimitive::~CardPrimitive() CardPrimitive::~CardPrimitive()
{ {
SAFE_DELETE(restrictions);
} }
int CardPrimitive::init() int CardPrimitive::init()
@@ -78,7 +79,7 @@ int CardPrimitive::init()
magicTexts.clear(); magicTexts.clear();
spellTargetType = ""; spellTargetType = "";
alias = 0; alias = 0;
hasRestriction = false; restrictions = NULL;
return 1; return 1;
} }
@@ -99,22 +100,34 @@ bool CardPrimitive::isSpell()
void CardPrimitive::setRestrictions(string _restriction) void CardPrimitive::setRestrictions(string _restriction)
{ {
restriction = _restriction; if (!restrictions)
restrictions = NEW CastRestrictions();
restrictions->restriction = _restriction;
} }
void CardPrimitive::getRestrictions() const string CardPrimitive::getRestrictions()
{ {
restriction; if (!restrictions)
return "";
return restrictions->restriction;
} }
void CardPrimitive::setOtherRestrictions(string _restriction) void CardPrimitive::setOtherRestrictions(string _restriction)
{ {
otherrestriction = _restriction; if (!restrictions)
restrictions = NEW CastRestrictions();
restrictions->otherrestriction = _restriction;
} }
void CardPrimitive::getOtherRestrictions() const string CardPrimitive::getOtherRestrictions()
{ {
otherrestriction; if (!restrictions)
return "";
return restrictions->otherrestriction;
} }
void CardPrimitive::setColor(const string& _color, int removeAllOthers) void CardPrimitive::setColor(const string& _color, int removeAllOthers)
+2 -2
View File
@@ -67,12 +67,12 @@ int MTGAbility::parseCastRestrictions(MTGCardInstance * card,Player * player,str
int MTGAbility::allowedToCast(MTGCardInstance * card,Player * player) int MTGAbility::allowedToCast(MTGCardInstance * card,Player * player)
{ {
return parseCastRestrictions(card,player,card->restriction,""); return parseCastRestrictions(card,player,card->getRestrictions(),"");
} }
int MTGAbility::allowedToAltCast(MTGCardInstance * card,Player * player) int MTGAbility::allowedToAltCast(MTGCardInstance * card,Player * player)
{ {
return parseCastRestrictions(card,player,"",card->otherrestriction); return parseCastRestrictions(card,player,"",card->getOtherRestrictions());
} }
int AbilityFactory::parseCastRestrictions(MTGCardInstance * card,Player * player,string restrictions,string otherRestrictions) int AbilityFactory::parseCastRestrictions(MTGCardInstance * card,Player * player,string restrictions,string otherRestrictions)
+56 -53
View File
@@ -94,6 +94,16 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
} }
break; break;
case 'b': //buyback
if (!primitive) primitive = NEW CardPrimitive();
if (ManaCost * cost = primitive->getManaCost())
{
string value = val;
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
cost->BuyBack = ManaCost::parseManaCost(value);
}
break;
case 'c': //color case 'c': //color
if (!primitive) primitive = NEW CardPrimitive(); if (!primitive) primitive = NEW CardPrimitive();
{ {
@@ -109,10 +119,36 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
} }
break; break;
case 'f': //flashback//morph
{
if (!primitive) primitive = NEW CardPrimitive();
if(ManaCost * cost = primitive->getManaCost())
{
if( s.find("facedown") != string::npos)//morph
{
string value = val;
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
cost->morph = ManaCost::parseManaCost(value);
}
else
{
string value = val;
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
cost->FlashBack = ManaCost::parseManaCost(value);
}
}
break;
}
case 'g': //grade case 'g': //grade
if (s.size() - i - 1 > 2) currentGrade = getGrade(val[2]); if (s.size() - i - 1 > 2) currentGrade = getGrade(val[2]);
break; break;
case 'i': //id
if (!card) card = NEW MTGCard();
card->setMTGId(atoi(val));
break;
case 'k': //kicker case 'k': //kicker
if (!primitive) primitive = NEW CardPrimitive(); if (!primitive) primitive = NEW CardPrimitive();
if (ManaCost * cost = primitive->getManaCost()) if (ManaCost * cost = primitive->getManaCost())
@@ -123,7 +159,21 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
} }
break; break;
case 'o': //othercost case 'm': //mana
if (!primitive) primitive = NEW CardPrimitive();
{
string value = val;
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
primitive->setManaCost(value);
}
break;
case 'n': //name
if (!primitive) primitive = NEW CardPrimitive();
primitive->setName(val);
break;
case 'o': //othercost/otherrestriction
if (!primitive) primitive = NEW CardPrimitive(); if (!primitive) primitive = NEW CardPrimitive();
if(key[5] == 'r')//otherrestrictions if(key[5] == 'r')//otherrestrictions
{ {
@@ -151,54 +201,6 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
} }
break; break;
case 'b': //buyback
if (!primitive) primitive = NEW CardPrimitive();
if (ManaCost * cost = primitive->getManaCost())
{
string value = val;
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
cost->BuyBack = ManaCost::parseManaCost(value);
}
break;
case 'f': //flashback//morph
{
if (!primitive) primitive = NEW CardPrimitive();
if(ManaCost * cost = primitive->getManaCost())
{
if( s.find("facedown") != string::npos)//morph
{
string value = val;
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
cost->morph = ManaCost::parseManaCost(value);
}
else
{
string value = val;
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
cost->FlashBack = ManaCost::parseManaCost(value);
}
}
break;
}
case 'i': //id
if (!card) card = NEW MTGCard();
card->setMTGId(atoi(val));
break;
case 'm': //mana
if (!primitive) primitive = NEW CardPrimitive();
{
string value = val;
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
primitive->setManaCost(value);
}
break;
case 'n': //name
if (!primitive) primitive = NEW CardPrimitive();
primitive->setName(val);
break;
case 'p': case 'p':
if ('r' == key[1]) if ('r' == key[1])
{ // primitive { // primitive
@@ -213,13 +215,12 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
} }
break; break;
case 'r': //retrace/rarity case 'r': //retrace/rarity//restrictions
if('s' == key[2] && 't' == key[3])//restrictions if('s' == key[2] && 't' == key[3])//restrictions
{ {
if (!primitive) primitive = NEW CardPrimitive(); if (!primitive) primitive = NEW CardPrimitive();
string value = val; string value = val;
primitive->setRestrictions(value); primitive->setRestrictions(value);
primitive->hasRestriction = true;
} }
else if ('e' == key[1] && 't' == key[2]) else if ('e' == key[1] && 't' == key[2])
{ //retrace { //retrace
@@ -237,6 +238,7 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
card->setRarity(val[0]); card->setRarity(val[0]);
} }
break; break;
case 's': //subtype, suspend case 's': //subtype, suspend
{ {
if (s.find("suspend") != string::npos) if (s.find("suspend") != string::npos)
@@ -263,6 +265,7 @@ int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primi
} }
break; break;
} }
case 't': case 't':
if (!primitive) primitive = NEW CardPrimitive(); if (!primitive) primitive = NEW CardPrimitive();
if (0 == strcmp("target", key)) if (0 == strcmp("target", key))
@@ -1068,8 +1071,8 @@ void MTGDeck::printDetailedDeckText(std::ofstream& file )
currentCard << card->data->getPower() << "/" << card->data->getToughness() << ", "; currentCard << card->data->getPower() << "/" << card->data->getToughness() << ", ";
if ( card->data->hasRestriction ) if ( card->data->getOtherRestrictions().size() )
currentCard << ", " << card->data->otherrestriction; currentCard << ", " << card->data->getOtherRestrictions();
for (size_t x = 0; x < card->data->basicAbilities.size(); ++x) for (size_t x = 0; x < card->data->basicAbilities.size(); ++x)
{ {