From f7bcbb42dcddf7349f6cb29e0b7746f439450cf2 Mon Sep 17 00:00:00 2001 From: "wagic.the.homebrew@gmail.com" Date: Thu, 18 Nov 2010 14:04:24 +0000 Subject: [PATCH] Erwan - had some problems compiling for the PSP. I assume I was the only one, please let me know if the "include JLogger" lines are not needed (they were needed for me) - Fix a memory leak when playing in "random deck" mode - Prevent the AI from playing cards with a cost it cannot understand (ExtraCosts with a target). --- projects/mtg/include/AIPlayer.h | 76 ++++++++++++++------------- projects/mtg/src/AIPlayer.cpp | 36 ++++++++----- projects/mtg/src/GameApp.cpp | 1 + projects/mtg/src/GameObserver.cpp | 2 +- projects/mtg/src/GameStateMenu.cpp | 1 + projects/mtg/src/Rules.cpp | 2 + projects/mtg/src/WResourceManager.cpp | 1 + 7 files changed, 68 insertions(+), 51 deletions(-) diff --git a/projects/mtg/include/AIPlayer.h b/projects/mtg/include/AIPlayer.h index 600d7fc9e..384a33041 100644 --- a/projects/mtg/include/AIPlayer.h +++ b/projects/mtg/include/AIPlayer.h @@ -53,42 +53,46 @@ class CmpAbilities { // compares Abilities efficiency }; class AIPlayer: public Player{ - protected: - MTGCardInstance * nextCardToPlay; - queue clickstream; - void tapLandsForMana(ManaCost * cost, MTGCardInstance * card = NULL); - int orderBlockers(); - int combatDamages(); - int interruptIfICan(); - int chooseAttackers(); - int chooseBlockers(); - int canFirstStrikeKill(MTGCardInstance * card, MTGCardInstance *ennemy); - int effectBadOrGood(MTGCardInstance * card, int mode = MODE_PUTINTOPLAY, TargetChooser * tc = NULL); - int getCreaturesInfo(Player * player, int neededInfo = INFO_NBCREATURES , int untapMode = 0, int canAttack = 0); - AIStats * getStats(); - //Variables used by Test suite - public: - int agressivity; - bool Checked; - bool forceBestAbilityUse; - void End(){}; - virtual int displayStack() {return 0;}; - int receiveEvent(WEvent * event); - void Render(); - AIStats * stats; - ManaCost * getPotentialMana(MTGCardInstance * card = NULL); - AIPlayer(MTGDeck * deck, string deckFile, string deckFileSmall); - virtual ~AIPlayer(); - virtual MTGCardInstance * chooseCard(TargetChooser * tc, MTGCardInstance * source, int random = 0); - virtual int chooseTarget(TargetChooser * tc = NULL, Player * forceTarget =NULL); - virtual int Act(float dt); - virtual int affectCombatDamages(CombatStep); - int isAI(){return 1;}; - int canHandleCost(MTGAbility * ability); - int selectAbility(); - int createAbilityTargets(MTGAbility * a, MTGCardInstance * c, map * ranking); - int useAbility(); - virtual int getEfficiency(AIAction * action); +protected: + //Variables used by Test suite + MTGCardInstance * nextCardToPlay; + queue clickstream; + void tapLandsForMana(ManaCost * cost, MTGCardInstance * card = NULL); + int orderBlockers(); + int combatDamages(); + int interruptIfICan(); + int chooseAttackers(); + int chooseBlockers(); + int canFirstStrikeKill(MTGCardInstance * card, MTGCardInstance *ennemy); + int effectBadOrGood(MTGCardInstance * card, int mode = MODE_PUTINTOPLAY, TargetChooser * tc = NULL); + int getCreaturesInfo(Player * player, int neededInfo = INFO_NBCREATURES , int untapMode = 0, int canAttack = 0); + AIStats * getStats(); + + // returns 1 if the AI algorithm supports a given cost (ex:simple mana cost), 0 otherwise (ex: cost involves Sacrificing a target) + int CanHandleCost(ManaCost * cost); + +public: + AIStats * stats; + int agressivity; + bool Checked; + bool forceBestAbilityUse; + void End(){}; + virtual int displayStack() {return 0;}; + int receiveEvent(WEvent * event); + void Render(); + ManaCost * getPotentialMana(MTGCardInstance * card = NULL); + AIPlayer(MTGDeck * deck, string deckFile, string deckFileSmall); + virtual ~AIPlayer(); + virtual MTGCardInstance * chooseCard(TargetChooser * tc, MTGCardInstance * source, int random = 0); + virtual int chooseTarget(TargetChooser * tc = NULL, Player * forceTarget =NULL); + virtual int Act(float dt); + virtual int affectCombatDamages(CombatStep); + int isAI(){return 1;}; + int canHandleCost(MTGAbility * ability); + int selectAbility(); + int createAbilityTargets(MTGAbility * a, MTGCardInstance * c, map * ranking); + int useAbility(); + virtual int getEfficiency(AIAction * action); }; diff --git a/projects/mtg/src/AIPlayer.cpp b/projects/mtg/src/AIPlayer.cpp index d3ad32440..3505d1fe8 100644 --- a/projects/mtg/src/AIPlayer.cpp +++ b/projects/mtg/src/AIPlayer.cpp @@ -159,22 +159,28 @@ int AIPlayer::getEfficiency(AIAction * action) return action->getEfficiency(); } + + +//Can't yet handle extraCost objects (ex: sacrifice) if they require a target :( +int AIPlayer::CanHandleCost(ManaCost * cost) +{ + if (!cost) return 1; + + ExtraCosts * ec = cost->extraCosts; + if (!ec) return 1; + + for (size_t i = 0; i < ec->costs.size(); ++i) + { + if (ec->costs[i]->tc) + return 0; + } + + return 1; +} + int AIPlayer::canHandleCost(MTGAbility * ability) { - //Can't handle sacrifice costs that require a target yet :( - if (ability->cost) - { - ExtraCosts * ec = ability->cost->extraCosts; - if (ec) - { - for (size_t i = 0; i < ec->costs.size(); i++) - { - if (ec->costs[i]->tc) - return 0; - } - } - } - return 1; + return CanHandleCost(ability->cost); } int AIAction::getEfficiency() @@ -871,6 +877,8 @@ MTGCardInstance * AIPlayerBaka::FindCardToPlay(ManaCost * pMana, const char * ty card = NULL; while ((card = cd.nextmatch(game->hand, card))) { + if (!CanHandleCost(card->getManaCost())) + continue; if (card->hasType(Subtypes::TYPE_CREATURE) && this->castrestrictedcreature < 0 && this->castrestrictedspell < 0) continue; if (card->hasType(Subtypes::TYPE_ENCHANTMENT) && this->castrestrictedspell < 0) diff --git a/projects/mtg/src/GameApp.cpp b/projects/mtg/src/GameApp.cpp index 47babcf7b..e70f05689 100644 --- a/projects/mtg/src/GameApp.cpp +++ b/projects/mtg/src/GameApp.cpp @@ -1,6 +1,7 @@ #include "PrecompiledHeader.h" #include +#include #include #if defined (WIN32) || defined (LINUX) #include diff --git a/projects/mtg/src/GameObserver.cpp b/projects/mtg/src/GameObserver.cpp index 944e587d9..bb4c2f06a 100644 --- a/projects/mtg/src/GameObserver.cpp +++ b/projects/mtg/src/GameObserver.cpp @@ -6,7 +6,7 @@ #include "Damage.h" #include "Rules.h" #include "ExtraCost.h" - +#include #include GameObserver * GameObserver::mInstance = NULL; diff --git a/projects/mtg/src/GameStateMenu.cpp b/projects/mtg/src/GameStateMenu.cpp index d376a8353..66d3e3123 100644 --- a/projects/mtg/src/GameStateMenu.cpp +++ b/projects/mtg/src/GameStateMenu.cpp @@ -17,6 +17,7 @@ #include "PlayerData.h" #include "utils.h" #include "WFont.h" +#include static const char* GAME_VERSION = "WTH?! 0.13.1 - by wololo"; diff --git a/projects/mtg/src/Rules.cpp b/projects/mtg/src/Rules.cpp index d1d99d89a..db726d1e7 100644 --- a/projects/mtg/src/Rules.cpp +++ b/projects/mtg/src/Rules.cpp @@ -10,6 +10,7 @@ #include "MTGAbility.h" #include "DeckManager.h" #include "AIPlayer.h" +#include int Rules::getMTGId(string cardName) { @@ -297,6 +298,7 @@ Player * Rules::loadPlayerRandom(int isAI, int mode) else player = NEW AIPlayerBaka(tempDeck, deckFile, deckFileSmall, ""); + delete tempDeck; return player; } diff --git a/projects/mtg/src/WResourceManager.cpp b/projects/mtg/src/WResourceManager.cpp index 4b49bef19..d987b9934 100644 --- a/projects/mtg/src/WResourceManager.cpp +++ b/projects/mtg/src/WResourceManager.cpp @@ -12,6 +12,7 @@ #include #endif #include "WFont.h" +#include //#define FORCE_LOW_CACHE_MEMORY const unsigned int kConstrainedCacheLimit = 8 * 1024 * 1024;