Jeck - Fixed an error with packs, fixed basic info in deck editor, removed duplicated code in DeckDataWrapper.

This commit is contained in:
wagic.jeck
2010-02-17 03:10:56 +00:00
parent ebaeb96450
commit a123cebfe5
11 changed files with 113 additions and 125 deletions

View File

@@ -12,31 +12,10 @@ using std::string;
class MTGDeck;
class Cmp1 { // compares cards by their name
public:
bool operator()(MTGCard * card1, MTGCard * card2) const {
if (!card2) return true;
if (!card1) return false;
int result = card1->data->name.compare(card2->data->name);
if (!result) return card1->getMTGId() < card2->getMTGId();
return ( result < 0);
}
};
class DeckDataWrapper: public WSrcDeck {
public:
MTGDeck * parent;
int counts[Constants::MTG_NB_COLORS];
unsigned short minCards; //Fewest number of copies of any given card in the deck.
DeckDataWrapper(MTGDeck * deck);
int Add(MTGCard * c, int quantity=1);
int Remove(MTGCard * c, int quantity=1, bool erase=false);
int Add(MTGDeck * deck);
int getCount(int color=-1);
void updateCounts();
bool next() {currentPos++; return true;};
bool prev() {currentPos--; return true;};
void save();

View File

@@ -131,6 +131,7 @@ class MTGDeck{
string meta_desc;
string meta_name;
int totalCards();
int totalPrice();
MTGDeck(MTGAllCards * _allcards);
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);

View File

@@ -62,7 +62,8 @@ public:
virtual JQuad * getImage(int offset=0);
virtual JQuad * getThumb(int offset=0);
virtual MTGCard * getCard(int offset=0, bool ignore=false);
virtual int Size(bool all=false); //Returns the number of cards currently matched
virtual int Size(bool all=false); //Returns the number of cards, or the number of cards that match the filter.
virtual void Shuffle();
virtual bool thisCard(int mtgid);
@@ -76,16 +77,20 @@ public:
virtual void clearFilters();
virtual WCardFilter* unhookFilters();
virtual bool matchesFilters(MTGCard * c);
virtual void validateFilters();
virtual void validate();
virtual void bakeFilters(); //Discards all invalidated cards.
virtual float filterFee();
virtual void updateCounts() {};
virtual void clearCounts() {};
virtual void addCount(MTGCard * c, int qty=1) {};
//Loads into us.
//Loads into us. Calls validate()
virtual int loadMatches(MTGAllCards* ac); //loadMatches adds the cards from something
virtual int loadMatches(MTGDeck * deck); //into this, if it matches our filter
virtual int loadMatches(WSrcCards* src, bool all=false); //If all==true, ignore filters on src.
//We load it
//We put it into something else
virtual int addRandomCards(MTGDeck * i, int howmany=1);
virtual int addToDeck(MTGDeck * i, int num=-1); //Returns num that didn't add
@@ -109,7 +114,7 @@ public:
class WSrcDeck: public WSrcCards{
public:
WSrcDeck(float delay=0.2) : WSrcCards(delay) {totalCards=0;};
WSrcDeck(float delay=0.2) : WSrcCards(delay) {clearCounts();};
virtual int loadMatches(MTGDeck * deck);
virtual int Add(MTGCard * c, int quantity=1);
virtual int Remove(MTGCard * c, int quantity=1, bool erase=false);
@@ -117,10 +122,23 @@ public:
int count(MTGCard * c);
int countByName(MTGCard * card, bool editions=false);
int totalPrice();
int totalCopies();
enum {
//0 to MTG_NB_COLORS are colors. See MTG_COLOR_ in Constants::.
UNFILTERED_COPIES = Constants::MTG_NB_COLORS,
UNFILTERED_UNIQUE,
UNFILTERED_MIN_COPIES, //For 'unlock all' cheat, awards screen
UNFILTERED_MAX_COPIES, //future use in format restriction, awards screen
FILTERED_COPIES,
FILTERED_UNIQUE,
MAX_COUNTS
};
void clearCounts();
void updateCounts();
void addCount(MTGCard * c, int qty=1);
int getCount(int count=UNFILTERED_COPIES);
protected:
map<int,int> copies; //Maps MTGID to card counts.
int totalCards;
int counts[MAX_COUNTS];
};
struct WCSortCollector{

View File

@@ -6,61 +6,9 @@
DeckDataWrapper::DeckDataWrapper(MTGDeck * deck){
parent = deck;
for(int c=0;c<Constants::MTG_NB_COLORS;c++)
counts[c] = 0;
Add(deck);
loadMatches(deck);
}
int DeckDataWrapper::Add(MTGDeck * deck){
if(loadMatches(deck))
return 1;
return 0;
}
int DeckDataWrapper::Remove(MTGCard * c, int quantity,bool erase){
if(WSrcDeck::Remove(c,quantity,erase)){
for(int i=0;i<Constants::MTG_NB_COLORS;i++){
if(c->data->hasColor(i))
counts[i]-=quantity;
}
return 1;
}
return 0;
}
int DeckDataWrapper::Add(MTGCard * c, int quantity){
if(WSrcDeck::Add(c,quantity)){
for(int i=0;i<Constants::MTG_NB_COLORS;i++){
if(c->data && c->data->hasColor(i))
counts[i]+=quantity;
}
return 1;
}
return 0;
}
int DeckDataWrapper::getCount(int color){
if(color < 0 || color >=Constants::MTG_NB_COLORS)
return Size(true);
return counts[color];
}
void DeckDataWrapper::updateCounts(){
map<int,int>::iterator it;
for(int c=0;c<Constants::MTG_NB_COLORS;c++)
counts[c] = 0;
minCards = 65500;
for(int i=0;i<Size(true);i++){
for(int c=0;c<Constants::MTG_NB_COLORS;c++){
MTGCard * card = getCard(c,true);
if(card->data->hasColor(c)){
it = copies.find(card->getMTGId());
if(it != copies.end())
counts[c]+=it->second;
if(it->second < minCards)
minCards = it->second;
}
}
}
}
void DeckDataWrapper::save(){
if(parent){
Rebuild(parent);

View File

@@ -247,9 +247,7 @@ bool GameStateAwards::enterStats(int option){
detailview->Add(NEW WGuiHeader("Collection Stats"));
detailview->Entering(JGE_BTN_NONE);
//Discover favorite set and unique cards
int unique = 0;
//Discover favorite set
if(setlist.size() > 0){
int * counts = (int*)calloc(setlist.size(),sizeof(int));
int setid = -1;
@@ -268,7 +266,6 @@ bool GameStateAwards::enterStats(int option){
many = c;
dupes = count;
}
unique++;
counts[c->setId]+=count;
if(costly == NULL
|| c->data->getManaCost()->getConvertedCost() > costly->data->getManaCost()->getConvertedCost())
@@ -279,7 +276,6 @@ bool GameStateAwards::enterStats(int option){
if(c->data->isCreature() && (tough == NULL || c->data->getToughness() > tough->data->getToughness()))
tough = c;
}
for(int i=0;i<setlist.size();i++){
if(setid < 0 || counts[i] > counts[setid])
@@ -291,10 +287,10 @@ bool GameStateAwards::enterStats(int option){
sprintf(buf,_("Total Value: %ic").c_str(),ddw->totalPrice());
detailview->Add(NEW WGuiItem(buf,WGuiItem::NO_TRANSLATE));//ddw->colors
sprintf(buf,_("Total Cards (including duplicates): %i").c_str(),ddw->totalCopies());
sprintf(buf,_("Total Cards (including duplicates): %i").c_str(),ddw->getCount(WSrcDeck::UNFILTERED_COPIES));
detailview->Add(NEW WGuiItem(buf,WGuiItem::NO_TRANSLATE));//ddw->colors
sprintf(buf,_("Unique Cards: %i").c_str(),unique);
sprintf(buf,_("Unique Cards: %i").c_str(),ddw->getCount(WSrcDeck::UNFILTERED_UNIQUE));
detailview->Add(NEW WGuiItem(buf,WGuiItem::NO_TRANSLATE));
if(many){

View File

@@ -132,7 +132,7 @@ void GameStateDeckViewer::updateDecks(){
welcome_menu = NEW SimpleMenu(10,this,Constants::MENU_FONT,20,20);
welcome_menu->Add(nbDecks+1, _("--NEW--").c_str());
if(options[Options::CHEATMODE].number && (!myCollection || myCollection->minCards < 4))
if(options[Options::CHEATMODE].number && (!myCollection || myCollection->getCount(WSrcDeck::UNFILTERED_MIN_COPIES) < 4))
welcome_menu->Add(-12,"--UNLOCK ALL--");
nbDecks = fillDeckMenu(welcome_menu,options.profileFile());
deckNum = 0;
@@ -490,13 +490,14 @@ void GameStateDeckViewer::renderOnScreenBasicInfo(){
int myD = (displayed_deck == myDeck);
float y = 0;
int now, total;
now = displayed_deck->Size();
total = displayed_deck->Size(true);
if(now != total)
sprintf(buffer, "%s%i cards (%i of %i unique)", (displayed_deck == myDeck) ? "DECK: " : " ", displayed_deck->totalCopies(),now, total);
int allCopies, nowCopies;
nowCopies = displayed_deck->getCount(WSrcDeck::FILTERED_COPIES);
allCopies = displayed_deck->getCount(WSrcDeck::UNFILTERED_COPIES);
if(allCopies != nowCopies)
sprintf(buffer, "%s %i of %i cards (%i unique)", (displayed_deck == myDeck) ? "DECK: " : " ", nowCopies,allCopies, WSrcDeck::FILTERED_UNIQUE);
else
sprintf(buffer, "%s%i cards (%i unique)", (displayed_deck == myDeck) ? "DECK: " : " " , displayed_deck->totalCopies(),total);
sprintf(buffer, "%s%i cards (%i unique)", (displayed_deck == myDeck) ? "DECK: " : " " , allCopies, WSrcDeck::UNFILTERED_UNIQUE);
float w = mFont->GetStringWidth(buffer);
JRenderer::GetInstance()->FillRoundRect(SCREEN_WIDTH-(w+27),y-5,w+10,15,5,ARGB(128,0,0,0));
mFont->DrawString(buffer, SCREEN_WIDTH-22, y+5,JGETEXT_RIGHT);
@@ -513,7 +514,6 @@ void GameStateDeckViewer::renderSlideBar(){
float y = SCREEN_HEIGHT_F-25;
float bar_size = SCREEN_WIDTH_F - 2*filler;
JRenderer * r = JRenderer::GetInstance();
typedef map<MTGCard *,int,Cmp1>::reverse_iterator rit;
int currentPos = displayed_deck->getOffset();
if(total == 0)
return;
@@ -645,7 +645,7 @@ void GameStateDeckViewer::renderOnScreenMenu(){
if (value > 9){nb_letters += 3;}else{nb_letters+=2;}
}
}
int value = myDeck->totalCopies();
int value = myDeck->getCount(WSrcDeck::UNFILTERED_COPIES);
sprintf(buffer, _("Your Deck: %i cards").c_str(), value);
font->DrawString(buffer, SCREEN_WIDTH-200+rightTransition, SCREEN_HEIGHT/2 + 25);
@@ -1087,10 +1087,10 @@ void GameStateDeckViewer::updateStats() {
AbilityFactory * af = NEW AbilityFactory();
myDeck->updateCounts();
myDeck->validate();
stw.needUpdate = false;
stw.cardCount = myDeck->totalCopies();
stw.cardCount = myDeck->getCount(WSrcDeck::UNFILTERED_COPIES);
stw.countLands = myDeck->getCount(Constants::MTG_COLOR_LAND);
stw.totalPrice = myDeck->totalPrice();

View File

@@ -9,8 +9,6 @@
#include "../include/DeckStats.h"
#include "../include/PlayerData.h"
#include "../include/utils.h"
#include "../include/DeckDataWrapper.h"
#include "../include/MTGPack.h"
static const char* GAME_VERSION = "WTH?! 0.11.0 - by wololo";
@@ -224,20 +222,18 @@ void GameStateMenu::fillScroller(){
scroller->Add(buff2);
PlayerData * playerdata = NEW PlayerData(mParent->collection);
DeckDataWrapper* ddw = NEW DeckDataWrapper(playerdata->collection);
int totalCards = ddw->getCount();
int totalCards = playerdata->collection->totalCards();
if (totalCards){
sprintf(buff2, _("You have a total of %i cards in your collection").c_str(),totalCards);
scroller->Add(buff2);
int estimatedValue = ddw->totalPrice();
int estimatedValue = playerdata->collection->totalPrice();
sprintf(buff2, _("The shopkeeper would buy your entire collection for around %i credits").c_str(),estimatedValue/2);
scroller->Add(buff2);
sprintf(buff2, _("The cards in your collection have an average value of %i credits").c_str(),estimatedValue/totalCards);
scroller->Add(buff2);
}
SAFE_DELETE(ddw);
sprintf(buff2, _("You currently have %i credits").c_str(),playerdata->credits);
SAFE_DELETE(playerdata);

View File

@@ -222,7 +222,7 @@ void GameStateShop::purchaseBooster(int controlId){
}
SAFE_DELETE(ddw);
myCollection->Add(booster);
myCollection->loadMatches(booster);
mTouched = true;
save(true);
menu->Close();

View File

@@ -473,7 +473,17 @@ MTGDeck::MTGDeck(MTGAllCards * _allcards){
filename ="";
meta_name = "";
}
int MTGDeck::totalPrice(){
int total = 0;
PriceList * pricelist = NEW PriceList(RESPATH"/settings/prices.dat",GameApp::collection);
map<int,int>::iterator it;
for ( it=cards.begin() ; it != cards.end(); it++ ){
int nb = it->second;
if (nb) total += pricelist->getPrice(it->first);
}
SAFE_DELETE(pricelist);
return total;
}
MTGDeck::MTGDeck(const char * config_file, MTGAllCards * _allcards, int meta_only){
total_cards = 0;
database = _allcards;

View File

@@ -37,7 +37,9 @@ int MTGPackSlot::add(WSrcCards * ocean, MTGDeck *to, int carryover){
if(!entries.size()) return copies;
int fails = 0;
int amt = copies + carryover;
WSrcCards * myPool = MTGPack::getPool(pool);
WSrcCards * myPool = NULL;
if(pool.size())
MTGPack::getPool(pool);
if(!myPool) myPool = ocean;
for(int i=0;i<amt;i++){
size_t pos = rand() % entries.size();

View File

@@ -122,7 +122,8 @@ int WSrcCards::loadMatches(MTGAllCards* ac){
count++;
}
}
validateFilters();
validate();
updateCounts();
return count;
}
int WSrcCards::loadMatches(MTGDeck * deck){
@@ -137,7 +138,8 @@ int WSrcCards::loadMatches(MTGDeck * deck){
count++;
}
}
validateFilters();
validate();
updateCounts();
return count;
}
int WSrcCards::loadMatches(WSrcCards* src, bool all){
@@ -157,7 +159,8 @@ int WSrcCards::loadMatches(WSrcCards* src, bool all){
}
}
src->setOffset(oldp);
validateFilters();
validate();
updateCounts();
return count;
}
int WSrcCards::addRandomCards(MTGDeck * i, int howmany){
@@ -166,7 +169,8 @@ int WSrcCards::addRandomCards(MTGDeck * i, int howmany){
for(int x=0;x<howmany;x++){
if(validated.size()){
size_t pos = rand() % validated.size();
i->add(cards[validated[pos]]);
MTGCard * c = cards[validated[pos]];
i->add(c);
}
else{
size_t pos = rand() % cards.size();
@@ -268,11 +272,11 @@ void WSrcCards::Shuffle(){
#else //PSP does a straight swap for speed.
cards.swap(a);
#endif
validateFilters();
validate();
}
void WSrcCards::validateFilters(){
void WSrcCards::validate(){
validated.clear();
if(filtersRoot == NULL) return; //No filter, no validation
if(!filtersRoot) return;
for(size_t t=0;t<cards.size();t++){
if(matchesFilters(cards[t]))
validated.push_back(t);
@@ -306,7 +310,8 @@ void WSrcCards::addFilter(WCardFilter * f) {
filtersRoot = f;
else
filtersRoot = NEW WCFilterAND(f,filtersRoot);
validateFilters();
validate();
updateCounts();
currentPos = 0;
}
float WSrcCards::filterFee(){
@@ -337,7 +342,7 @@ void WSrcCards::Sort(int method){
std::sort(cards.begin(),cards.end(),WCSortAlpha());
break;
}
validateFilters();
validate();
}
//WSrcUnlockedCards
WSrcUnlockedCards::WSrcUnlockedCards(float delay): WSrcCards(delay){
@@ -380,17 +385,48 @@ int WSrcDeck::loadMatches(MTGDeck * deck){
count++;
}
}
validateFilters();
validate();
updateCounts();
return count;
}
void WSrcDeck::updateCounts(){
vector<MTGCard*>::iterator it;
map<int,int>::iterator ccount;
clearCounts();
for(it=cards.begin();it!=cards.end();it++){
ccount = copies.find((*it)->getMTGId());
if(ccount == copies.end()) continue;
addCount((*it),ccount->second);
}
}
void WSrcDeck::clearCounts(){
counts[UNFILTERED_MIN_COPIES] = -1;
counts[UNFILTERED_MAX_COPIES] = 0;
for(int i=0;i<MAX_COUNTS;i++)
counts[i] = 0;
}
void WSrcDeck::addCount(MTGCard * c, int qty){
if(!c || !c->data) return;
if(matchesFilters(c)){
counts[FILTERED_COPIES]+=qty;
counts[FILTERED_UNIQUE]++;
}
counts[UNFILTERED_COPIES] += qty;
counts[UNFILTERED_UNIQUE]++;
for(int i=Constants::MTG_COLOR_GREEN;i<=Constants::MTG_COLOR_WHITE;i++)
if (c->data->hasColor(i)) counts[i]+= qty;
if(counts[UNFILTERED_MIN_COPIES] < 0 || qty < counts[UNFILTERED_MIN_COPIES]) counts[UNFILTERED_MIN_COPIES] = qty;
if(qty > counts[UNFILTERED_MAX_COPIES]) counts[UNFILTERED_MAX_COPIES] = qty;
}
int WSrcDeck::Add(MTGCard * c, int quantity){
if(!c)
return 0;
if(copies.find(c->getMTGId()) == copies.end())
cards.push_back(c); //FIXME Make sure these two stay synced.
copies[c->getMTGId()] += quantity;
totalCards += quantity;
addCount(c,quantity);
validate();
return 1;
}
@@ -409,7 +445,8 @@ int WSrcDeck::Remove(MTGCard * c, int quantity, bool erase){
if(i != cards.end())
cards.erase(i);
}
totalCards -= quantity;
validate();
addCount(c,-quantity);
return 1;
}
@@ -425,7 +462,7 @@ void WSrcDeck::Rebuild(MTGDeck * d){
int WSrcDeck::count(MTGCard * c){
if(!c)
return totalCopies();
return counts[UNFILTERED_COPIES];
if(copies.find(c->getMTGId()) == copies.end())
return 0;
return copies[c->getMTGId()];
@@ -448,7 +485,11 @@ int WSrcDeck::countByName(MTGCard * card, bool editions){
}
return total;
}
int WSrcDeck::getCount(int count){
if(count < 0 || count >=MAX_COUNTS)
return counts[UNFILTERED_COPIES];
return counts[count];
}
int WSrcDeck::totalPrice(){
int total = 0;
PriceList * pricelist = NEW PriceList(RESPATH"/settings/prices.dat",GameApp::collection);
@@ -461,9 +502,6 @@ int WSrcDeck::totalPrice(){
return total;
}
int WSrcDeck::totalCopies(){
return totalCards;
}
//Sorting methods:
int WCSortRarity::rareToInt(char r){
switch(r){