- fixed card id collisions in Legends, Ravnica, Portal
- Changed Deck and Database structures with better design (a bit faster, hopefully a lot cleaner)
- updated Windows project file
This commit is contained in:
wagic.the.homebrew@gmail.com
2009-06-06 04:46:54 +00:00
parent 01bd44025d
commit ab42e5db06
13 changed files with 166 additions and 145 deletions

View File

@@ -8,10 +8,10 @@ DeckDataWrapper::DeckDataWrapper(MTGDeck * deck){
for (int i = 0; i <= Constants::MTG_NB_COLORS; i++){
colors[i] = 0;
}
for (int i = 0; i < deck->totalCards(); i++){
MTGCard * card = deck->_(i);
Add(card);
map<int,int>::iterator it;
for (it = deck->cards.begin(); it!=deck->cards.end(); it++){
MTGCard * card = deck->getCardById(it->first);
Add(card,it->second);
}
currentColor = -1;
}
@@ -33,7 +33,7 @@ DeckDataWrapper::~DeckDataWrapper(){
SAFE_DELETE(parent);
}
void DeckDataWrapper::updateCounts(MTGCard * card, int removed){
void DeckDataWrapper::updateCounts(MTGCard * card, int increment){
if (!card){
for (int i = 0; i < Constants::MTG_NB_COLORS+1; i++){
colors[i] = 0;
@@ -47,8 +47,6 @@ void DeckDataWrapper::updateCounts(MTGCard * card, int removed){
}
}
}else{
int increment = 1;
if (removed) increment = -1;
colors[Constants::MTG_NB_COLORS] += increment;
for (int i = 0; i < Constants::MTG_NB_COLORS; i++){
if (card->hasColor(i)) colors[i]+=increment;
@@ -56,20 +54,20 @@ void DeckDataWrapper::updateCounts(MTGCard * card, int removed){
}
}
int DeckDataWrapper::Add(MTGCard * card){
int DeckDataWrapper::Add(MTGCard * card, int quantity){
if(cards.find(card) == cards.end()){
cards[card] = 1;
cards[card] = quantity;
}else{
cards[card]++;
cards[card]+= quantity;
}
updateCounts(card);
updateCounts(card,quantity);
return cards[card];
}
int DeckDataWrapper::Remove(MTGCard * card){
if(cards.find(card) == cards.end() || cards[card] == 0) return 0;
if(cards.find(card) == cards.end() || cards[card] <= 0) return 0;
cards[card]--;
updateCounts(card,1);
updateCounts(card,-1);
return 1;
}
@@ -165,7 +163,7 @@ int DeckDataWrapper::getCount(int color){
int DeckDataWrapper::totalPrice(){
int total = 0;
PriceList * pricelist = NEW PriceList(RESPATH"/settings/prices.dat",this->parent);
PriceList * pricelist = NEW PriceList(RESPATH"/settings/prices.dat",this->parent->database);
map<MTGCard *,int,Cmp1>::iterator it;
for ( it=cards.begin() ; it != cards.end(); it++ ){
MTGCard * current = (*it).first;

View File

@@ -222,7 +222,7 @@ void GameStateMenu::fillScroller(){
scroller->Add(buff2);
}
delete ddw;
PlayerData * playerdata = NEW PlayerData(mParent->collection);
sprintf(buff2, "You currently have %i credits",playerdata->credits);
delete playerdata;

View File

@@ -304,3 +304,4 @@ void MTGCard::setToughness(int _toughness){
int MTGCard::getToughness(){
return toughness;
}

View File

@@ -153,6 +153,7 @@ void MTGAllCards::initCounters(){
}
void MTGAllCards::init(){
tempCard = NULL;
mCache = NULL;
total_cards = 0;
initCounters();
@@ -179,12 +180,16 @@ MTGAllCards::MTGAllCards(){
}
MTGAllCards::~MTGAllCards(){
//Why don't we call destroyAllCards from here ???
}
void MTGAllCards::destroyAllCards(){
for (int i= 0; i < total_cards; i++){
SAFE_DELETE(collection[i]);
};
map<int,MTGCard *>::iterator it;
for (it = collection.begin(); it!=collection.end(); it++) delete(it->second);
collection.clear();
ids.clear();
}
@@ -204,23 +209,21 @@ MTGAllCards::MTGAllCards(const char * config_file, const char * set_name, Textur
}
MTGCard * MTGAllCards::_(int i){
if (i < total_cards) return collection[i];
return NULL;
}
int MTGAllCards::randomCardId(){
int id = (rand() % total_cards);
return collection[id]->getMTGId();
int id = (rand() % ids.size());
return ids[id];
}
int MTGAllCards::countBySet(int setId){
int result = 0;
for (int i=0; i< total_cards; i++){
if(collection[i]->setId == setId){
map<int,MTGCard *>::iterator it;
for (it = collection.begin(); it!=collection.end(); it++){
MTGCard * c = it->second;
if( c->setId == setId){
result++;
}
}
@@ -230,8 +233,10 @@ int MTGAllCards::countBySet(int setId){
//TODO more efficient way ?
int MTGAllCards::countByType(const char * _type){
int result = 0;
for (int i=0; i< total_cards; i++){
if(collection[i]->hasType(_type)){
map<int,MTGCard *>::iterator it;
for (it = collection.begin(); it!=collection.end(); it++){
MTGCard * c = it->second;
if(c->hasType(_type)){
result++;
}
}
@@ -244,8 +249,10 @@ int MTGAllCards::countByColor(int color){
for (int i=0; i< Constants::MTG_NB_COLORS; i++){
colorsCount[i] = 0;
}
for (int i=0; i< total_cards; i++){
int j = collection[i]->getColor();
map<int,MTGCard *>::iterator it;
for (it = collection.begin(); it!=collection.end(); it++){
MTGCard * c = it->second;
int j = c->getColor();
colorsCount[j]++;
}
@@ -268,16 +275,26 @@ int MTGAllCards::readConfLine(std::ifstream &file, int set_id){
switch(conf_read_mode) {
case 0:
if (s[0] == '['){
collection.push_back(NEW MTGCard(mCache,set_id));
tempCard = NEW MTGCard(mCache,set_id);
conf_read_mode = 1;
}
break;
case 1:
if (s[0] == '[' && s[1] == '/'){
conf_read_mode = 0;
total_cards++;
int newId = tempCard->getId();
if (collection.find(newId) != collection.end()){
char outBuf[4096];
sprintf(outBuf,"warning, card id collision! : %i - %s\n", newId, tempCard->name.c_str());
OutputDebugString (outBuf);
delete tempCard;
}else{
ids.push_back(newId);
collection[newId] = tempCard;
total_cards++;
}
}else{
processConfLine(s, collection[total_cards]);
processConfLine(s, tempCard);
}
break;
default:
@@ -292,16 +309,18 @@ int MTGAllCards::readConfLine(std::ifstream &file, int set_id){
MTGCard * MTGAllCards::getCardById(int id){
int i;
for (i=0; i<total_cards; i++){
int cardId = collection[i]->getMTGId();
if (cardId == id){
return collection[i];
}
map<int, MTGCard *>::iterator it = collection.find(id);
if ( it != collection.end()){
return (it->second);
}
return 0;
}
MTGCard * MTGAllCards::_(int index){
if (index >= total_cards) return NULL;
return getCardById(ids[index]);
}
MTGCard * MTGAllCards::getCardByName(string name){
if (!name.size()) return NULL;
if (name[0] == '#') return NULL;
@@ -314,11 +333,13 @@ MTGCard * MTGAllCards::getCardByName(string name){
name = name.substr(0,found);
setId = MtgSets::SetsList->find(setName);
}
for (int i=0; i<total_cards; i++){
if (setId!=-1 && setId != collection[i]->setId) continue;
string cardName = collection[i]->name;
map<int,MTGCard *>::iterator it;
for (it = collection.begin(); it!=collection.end(); it++){
MTGCard * c = it->second;
if (setId!=-1 && setId != c->setId) continue;
string cardName = c->name;
std::transform(cardName.begin(), cardName.end(), cardName.begin(),::tolower );
if (cardName.compare(name) == 0) return collection[i];
if (cardName.compare(name) == 0) return c;
}
return NULL;
@@ -326,11 +347,16 @@ MTGCard * MTGAllCards::getCardByName(string name){
MTGDeck::MTGDeck(TexturesCache * cache, MTGAllCards * _allcards){
mCache = cache;
total_cards = 0;
database = _allcards;
}
MTGDeck::MTGDeck(const char * config_file, TexturesCache * cache, MTGAllCards * _allcards, int meta_only){
mCache = cache;
total_cards = 0;
allcards = _allcards;
database = _allcards;
filename = config_file;
size_t slash = filename.find_last_of("/");
size_t dot = filename.find(".");
@@ -368,7 +394,7 @@ MTGDeck::MTGDeck(const char * config_file, TexturesCache * cache, MTGAllCards *
s=s.substr(0,found);
OutputDebugString(s.c_str());
}
MTGCard * card = allcards->getCardByName(s);
MTGCard * card = database->getCardByName(s);
if (card){
for (int i = 0; i < nb; i++){
add(card);
@@ -384,15 +410,20 @@ MTGDeck::MTGDeck(const char * config_file, TexturesCache * cache, MTGAllCards *
}
int MTGDeck::totalCards(){
return total_cards;
}
MTGCard * MTGDeck::getCardById(int mtgId){
return database->getCardById(mtgId);
}
int MTGDeck::addRandomCards(int howmany, int setId, int rarity, const char * _subtype){
int collectionTotal = allcards->totalCards();
int collectionTotal = database->totalCards();
if (!collectionTotal) return 0;
if (setId == -1 && rarity == -1 && !_subtype){
for (int i = 0; i < howmany; i++){
int id = (rand() % collectionTotal);
add(allcards->_(id));
add(database->randomCardId());
}
return 1;
}
@@ -404,60 +435,69 @@ int MTGDeck::addRandomCards(int howmany, int setId, int rarity, const char * _su
vector<int> subcollection;
int subtotal = 0;
for (int i = 0; i < collectionTotal; i++){
MTGCard * card = allcards->_(i);
MTGCard * card = database->_(i);
if ((setId == -1 || card->setId == setId) &&
(rarity == -1 || card->getRarity()==rarity) &&
(!_subtype || card->hasSubtype(subtype))
){
subcollection.push_back(i);
subcollection.push_back(card->getId());
subtotal++;
}
}
if (subtotal == 0) return 0;
for (int i = 0; i < howmany; i++){
int id = (rand() % subtotal);
add(allcards->_(subcollection[id]));
add(subcollection[id]);
}
return 1;
}
int MTGDeck::add(MTGDeck * deck){
map<int,int>::iterator it;
for (it = deck->cards.begin(); it!=deck->cards.end(); it++){
for (int i = 0; i < it->second; i++){
add(it->first);
}
}
return deck->totalCards();
}
int MTGDeck::add(int cardid){
MTGCard * card = allcards->getCardById(cardid);
add(card);
if (!database->getCardById(cardid)) return 0;
if(cards.find(cardid) == cards.end()){
cards[cardid] = 1;
}else{
cards[cardid]++;
}
++total_cards;
//initCounters();
return total_cards;
}
int MTGDeck::add(MTGCard * card){
if (!card) return 0;
collection.push_back(card);
++total_cards;
initCounters();
return total_cards;
return (add(card->getId()));
}
int MTGDeck::removeAll(){
total_cards = 0;
collection.clear();
initCounters();
cards.clear();
//initCounters();
return 1;
}
int MTGDeck::remove(int cardid){
MTGCard * card = getCardById(cardid);
return remove(card);
if(cards.find(cardid) == cards.end() || cards[cardid] == 0) return 0;
cards[cardid]--;
total_cards--;
//initCounters();
return 1;
}
int MTGDeck::remove(MTGCard * card){
for (int i = 0; i<total_cards; i++){
if (collection[i] == card){
collection.erase(collection.begin()+i);
total_cards--;
initCounters();
return 1;
}
}
return 0;
if (!card) return 0;
return (remove(card->getId()));
}
int MTGDeck::save(){
@@ -467,9 +507,12 @@ int MTGDeck::save(){
#if defined (WIN32) || defined (LINUX)
OutputDebugString("saving");
#endif
for (int i = 0; i<total_cards; i++){
sprintf(writer,"%i\n", collection[i]->getMTGId());
file<<writer;
map<int,int>::iterator it;
for (it = cards.begin(); it!=cards.end(); it++){
sprintf(writer,"%i\n", it->first);
for (int j = 0; j<it->second; j++){
file<<writer;
}
}
file.close();
}

View File

@@ -35,16 +35,16 @@ MTGPlayerCards::MTGPlayerCards(MTGAllCards * _collection, int * idList, int idLi
MTGPlayerCards::MTGPlayerCards(MTGAllCards * _collection,MTGDeck * deck){
init();
collection = _collection;
for (int i=0; i<deck->totalCards(); i++){
MTGCard * card = deck->collection[i];
map<int,int>::iterator it;
for (it = deck->cards.begin(); it!=deck->cards.end(); it++){
MTGCard * card = deck->getCardById(it->first);
if (card){
MTGCardInstance * newCard = NEW MTGCardInstance(card, this);
library->addCard(newCard);
for (int i = 0; i < it->second; i++){
MTGCardInstance * newCard = NEW MTGCardInstance(card, this);
library->addCard(newCard);
}
}
}
}
MTGPlayerCards::~MTGPlayerCards(){

View File

@@ -251,17 +251,25 @@ void ShopItems::ButtonPressed(int controllerId, int controlId){
safeDeleteDisplay();
display = NEW CardDisplay(12,NULL, SCREEN_WIDTH - 200, SCREEN_HEIGHT/2,this,NULL,5);
int curNbcards = playerdata->collection->totalCards();
MTGDeck * tempDeck = NEW MTGDeck(NULL,playerdata->collection->database);
playerdata->collection->addRandomCards(1, setIds[showPriceDialog],Constants::RARITY_R);
playerdata->collection->addRandomCards(3, setIds[showPriceDialog],Constants::RARITY_U);
playerdata->collection->addRandomCards(11, setIds[showPriceDialog],Constants::RARITY_C);
tempDeck->addRandomCards(1, setIds[showPriceDialog],Constants::RARITY_R);
tempDeck->addRandomCards(3, setIds[showPriceDialog],Constants::RARITY_U);
tempDeck->addRandomCards(11, setIds[showPriceDialog],Constants::RARITY_C);
playerdata->collection->add(tempDeck);
int newNbCards = playerdata->collection->totalCards();;
for (int i = curNbcards; i < newNbCards ; i++){
MTGCardInstance * card = NEW MTGCardInstance(playerdata->collection->_(i), NULL);
displayCards[i-curNbcards] = card;
display->AddCard(card);
}
int i = 0;
for (map<int,int>::iterator it = tempDeck->cards.begin(); it!=tempDeck->cards.end(); it++){
MTGCard * c = tempDeck->getCardById(it->first);
for (int j = 0; j < it->second; j++){
MTGCardInstance * card = NEW MTGCardInstance(c, NULL);
displayCards[i] = card;
display->AddCard(card);
i++;
}
}
delete tempDeck;
}
//Remove(showPriceDialog);
showPriceDialog = -1;