Files
wagic/projects/mtg/src/MTGCardInstance.cpp
T
omegablast2002@yahoo.com 473abd9814 im forced to do this commit in whole instead of parts as originally planned, and before my beta test period of the changes is complete BECAUSE there are people doing "clean up" and the MASSIVE amount of conflicts i have to resolve from it is WAY too much for me to take on after nearly 200 hours of coding this patch. i cant seem to get enough respect to have people hold off on "clean up" so this brings me to being forced to do a full commit before playtest period is done, so they can go ahead with there *super important* clean up.
ok i WAS going to write a full change log with code exsamples ect, but since im rushed you will get the short version of this log.

first bug fixes, and there were many, 
indestructible creature bug fixed
halimar execavator *embearessing youtube video" bug is fixed

token text now displays source name and tokens abilities

fixed a card view null pointer in an iterator when code used combinations of foreach and aslongas with CD.

epic struggle bug fixed, aslongas was only parsing one space to the right of the operator.

extra cost containing targetting fixed, cards can now have multiple extra cost in all mana...this includes giving a card 2 targeted sacrifices as its main cost.

angelic chorus bug fixed, the card will be soft coded now.

and many other minor bugs fixed, hard to remember all which were fixed.

now, new abilities = words
"legendarylandwalk",
"desertlandwalk",
"snowforestlandwalk",
"snowplainslandwalk",
"snowmountainlandwalk",
"snowislandlandwalk",
"snowswamplandwalk",
"snowlandwalk",
"nonbasiclandwalk",
"strong",//cant be blocked by creature with less power
"weak",//cant block creatures with more power
"phasing",

all true landwalks will now be supported.

new cost types:
morph which is coded as follows
[card]
name=Bloodstoke Howler
facedown={3}
autofacedown={6}{R}:morph
autofaceup=3/0 all(beast|mybattlefield))
text=Morph {6}{R} (You may cast this face down as a 2/2 creature for {3}. Turn it face up any time for its morph cost.) -- When Bloodstoke Howler is turned 
face up, Beast creatures you control get +3/+0 until end of turn.
mana={5}{R}
type=Creature
subtype=Beast
power=3
toughness=4
[/card]

you will notice new auto lines autofaceup and autofacedown
these are abilities the cards will have when theyre in that state.
the cost is coded as
facedown={cost}
when a card is faced up it gains auto= lines also.
tho is played normally it will NOT gain autofaceup=lines

card restrictions:
cards can now have restrictions placed on them the restrictions are.
all previous restrictions usable in activated abilities
with the follow additions
control two or more vampires
control less creatures
control snow land
casted a spell
one of a kind
fourth turn
before battle damage
after battle
during battle

[card]
name=Blood Frenzy
target=creature[attacking;blocking]
restriction=before battle damage
auto=4/0
auto=treason
text=Cast Blood Frenzy only before the combat damage step. -- Target attacking or blocking creature gets +4/+0 until end of turn. Destroy that creature 
at the beginning of the next end step.
mana={1}{R}
type=Instant
[/card]

other cost now can have specail restrictions also:
otherrestriction=mytypemin:1 type(swamp),opponenttypemin:1 opponenttype(plains)
these are minimums required inplay of a type
it can be just you, or you and opponent or just opponent
you can also use the words "more" and "less" and * to compare the 2 players fields.

[card]
name=Cho-Arrim Legate
abilities=protection from black
other={0}
otherrestriction=mytypemin:1 type(swamp) , opponenttypemin:1 opponenttype(plains)
text=Protection from black -- If an opponent controls a Swamp and you control a Plains, you may cast Cho-Arrim Legate without paying its mana cost.
mana={2}{W}
type=Creature
subtype=Human Soldier
power=1
toughness=2
[/card]

activated ability gained a new restriction "opponentturnonly"

variables will now be recalculated during the resolve of the major abilities to produce the most current number.
{x}:draw:x <----
new number variables words:
using draw as an exsample
draw:auras <--auras on a creature

draw:type:ally <---counts the allys in your field. self explanitory

draw:thatmuch <--mostly a triggered effects number.
when you take damage draw that much

draw:lifelost
draw:oplifelost
these return the value of the life lost that turn.

new TRIGGER restricitions
sourcenottap
sourceTap
foelostthree<--card cycle uses opponent lost life
foelosttwo<--same as above
once<--this trigger will only ever trigger one time and never again.

new card discriptor words
[multicolor]
[leveler]
[enchanted]
[blackandgreen]
[blackandwhite]
[redandblue]
[blueandgreen]
[redandwhite]
CD will now recalculate the number again on resolve
meaning {x}:target(CreatureTargetChooser[manacost <=x]) will work, with an added bonus {x}:target(CreatureTargetChooser[manacost <=any word variable])

new this(:
this(tapped)<--for strange case cards.
this(untapped)
this(auras)

new MTGAbility keywords
(blink)
(blink)forsrc <--stay blinked while source inplay
hand(blink <---adding hand to the front makes it target hand.

livingweapon
this is an extension of token, simple attach the words "livingweapon" to the front of token( and it will autoamtically token that and attach the card to it.

token( gained:
"targetcontroller" targetting.
"battleready" if put in the tokens abilities it will be a attacker and tapped as it is entering play.


phaseout <--self explanitory

spiritlink <--stacking lifelink style effect that benifits the OWNER of the card.
combatspiritlink same as above.

stacking flanking, requires 2 abilities unfortunately

[card]
name=Agility
target=creature
auto=teach(creature) flanker
auto=teach(creature) flanking
text=Enchant creature -- Enchanted creature gets +1/+1 and has flanking. (Whenever a creature without flanking blocks this creature, the blocking 
creature gets -1/-1 until end of turn.)
mana={1}{R}
type=Enchantment
subtype=Aura
[/card]

removeallcounters(number/number,name)
removes all counters of the type from a card, can all be
"all"
vampire hexmage effect.

added new tools for transforms
,setpower=number
,settoughness=number
removetypes

morph
autofacedown={0}:morph

eradicate <---same as the card name.

cumulativeupcost[ <--self explanitory

upcostmulti[ <--an upcost that will resolve with a && ability

phaseaction[ phase name ] ability

an ability that will trigger on the stated phase name.
also support for phaseactionmulti[

new triggers added:
@vampired( <--sengir vampire effect
@targeted( 
@lifeloss(
@lifed(

add a special ability builder called dynamicability
it acts alot like a choose your own adventure book

dynamicability<! variable 1, variable 2, variable 3,variable 4!> optional ability targetting the original target.

variable list 1:
this is the primary amount source
source
mytgt
myself
myfoe
variable list 2:
this is the variable we're after, or the amount
power
toughness
manacost
colors
age
charge
oneonecounters
thatmuch
variable list 3:
this is the main effect
strike
draw
lifeloss
lifegain
pumppow
pumptough
pumpboth
deplete
countersoneone
variable list 4:
how it will do this effect to.
itself
eachother
targetcontroller
targetopponent
tosrc
srccontroller
srcopponent

the best way to explain its usage is to look at cards coded with this ability. or experiment with combinations.

new gameoption
First turn player:player, opponent, random
who takes the first turn

added poisoned status, tho not complete since MBS hasnt spoiled enough cards to see where this variable will be used.

taught ai how to counter spell
improved ai, it will now cast instants during interupts and during your turn.
previously ai treated instant cards the same as it treated sorceries, which was not fair to the ai.

im sure there is some messed items, but the rev directly before this one had formatting in the code that created hundreds of conflicts with this one, so i had to dig this info out of red and green sections.

cards and test are coming soon, i ask PLEASE do not alter these new additions until the test are commited.
im commiting without the test because instead of allowing me to proceed with my beta test period, there are some that wish to rush me into a commit. if you do not like this commit revert it, i absolutely on no grounds give permission to recommit afterwards. and i will not recommit if a revert is called.
2011-01-21 20:18:56 +00:00

1042 lines
27 KiB
C++

/*---------------------------------------------
Card Instance
Instance of a given MTGCard in the game
Although there is only one MTGCard of each type, there can be as much Instances of it as needed in the game
--------------------------------------------
*/
#include "PrecompiledHeader.h"
#include "MTGCardInstance.h"
#include "CardDescriptor.h"
#include "Counters.h"
#include "Subtypes.h"
using namespace std;
MTGCardInstance MTGCardInstance::AnyCard = MTGCardInstance();
MTGCardInstance MTGCardInstance::NoCard = MTGCardInstance();
MTGCardInstance MTGCardInstance::ExtraRules[] = { MTGCardInstance(), MTGCardInstance() };
MTGCardInstance::MTGCardInstance() :
CardPrimitive(), MTGCard(), Damageable(0), view(NULL)
{
initMTGCI();
}
MTGCardInstance::MTGCardInstance(MTGCard * card, MTGPlayerCards * arg_belongs_to) :
CardPrimitive(card->data), MTGCard(card), Damageable(card->data->getToughness()), view(NULL)
{
initMTGCI();
model = card;
attacker = 0;
lifeOrig = life;
origpower = power;
origtoughness = toughness;
belongs_to = arg_belongs_to;
owner = NULL;
if (arg_belongs_to)
owner = arg_belongs_to->library->owner;
lastController = owner;
defenser = NULL;
banding = NULL;
life = toughness;
preventable = 0;
flanked = 0;
}
void MTGCardInstance::copy(MTGCardInstance * card)
{
MTGCard * source = card->model;
CardPrimitive * data = source->data;
for (map<int, int>::const_iterator it = data->basicAbilities.begin(); it != data->basicAbilities.end(); ++it)
{
int i = it->first;
basicAbilities[i] = data->basicAbilities[i];
}
for (size_t i = 0; i < data->types.size(); i++)
{
types.push_back(data->types[i]);
}
for (int i = 0; i < Constants::MTG_NB_COLORS; i++)
{
colors[i] = data->colors[i];
}
manaCost.copy(data->getManaCost());
text = data->text;
setName(data->name);
power = data->power;
toughness = data->toughness;
life = toughness;
lifeOrig = life;
magicText = data->magicText;
spellTargetType = data->spellTargetType;
alias = data->alias;
//Now this is dirty...
int backupid = mtgid;
mtgid = source->getId();
Spell * spell = NEW Spell(this);
AbilityFactory af;
GameObserver * g = GameObserver::GetInstance();
af.addAbilities(g->mLayers->actionLayer()->getMaxId(), spell);
delete spell;
mtgid = backupid;
}
MTGCardInstance::~MTGCardInstance()
{
SAFE_DELETE(counters);
if (previous != NULL)
{
//DebugTrace("MTGCardInstance::~MTGCardInstance(): deleting " << ToHex(previous));
SAFE_DELETE(previous);
}
}
int MTGCardInstance::init()
{
MTGCard::init();
CardPrimitive::init();
data = this;
X = 0;
XX = 0;
return 1;
}
void MTGCardInstance::initMTGCI()
{
sample = "";
model = NULL;
imprint = NULL;
isToken = false;
lifeOrig = 0;
doDamageTest = 1;
belongs_to = NULL;
tapped = 0;
untapping = 0;
frozen = 0;
fresh = 0;
isMultiColored = 0;
isBlackAndWhite = 0;
isRedAndBlue = 0;
isBlackAndGreen = 0;
isBlueAndGreen = 0;
isRedAndWhite = 0;
isLeveler = 0;
enchanted = false;
CDenchanted = NULL;
blinked = false;
isExtraCostTarget = false;
morphed = false;
turningOver = false;
isMorphed = false;
isPhased = false;
isTempPhased = false;
phasedTurn = -1;
didattacked = 0;
didblocked = 0;
notblocked = 0;
sunburst = NULL;
equipment = 0;
auras = 0;
for (int i = 0; i < ManaCost::MANA_PAID_WITH_RETRACE +1; i++)
alternateCostPaid[i] = 0;
paymenttype = MTGAbility::PUT_INTO_PLAY;
reduxamount = 0;
summoningSickness = 1;
preventable = 0;
flanked = 0;
target = NULL;
type_as_damageable = DAMAGEABLE_MTGCARDINSTANCE;
banding = NULL;
owner = NULL;
counters = NEW Counters(this);
previousZone = NULL;
previous = NULL;
next = NULL;
lastController = NULL;
regenerateTokens = 0;
blocked = false;
currentZone = NULL;
data = this; //an MTGCardInstance point to itself for data, allows to update it without killing the underlying database item
if (basicAbilities[Constants::CHANGELING])
{//if the card is a changeling.
for (int i = Subtypes::LAST_TYPE + 1;; i++)
{
string s = Subtypes::subtypesList->find(i);
if (!hasSubtype(i))
{
if (s == "")
break;
if (s.find(" ") != string::npos)
continue;
if (s == "Nothing" || s == "Swamp" || s == "Plains" || s == "Mountain" || s == "Forest" ||
s == "Island" || s == "Shrine" || s == "Basic" || s == "Colony" || s == "Desert" ||
s == "Dismiss" || s == "Equipment" || s == "Everglades" || s == "Grasslands" || s == "Lair" ||
s == "Level" || s == "Levelup" || s == "Mine" || s == "Oasis" || s == "World" || s == "Aura"
)
continue;
addType(i);
}
}
}
int colored = 0;
for (int i = Constants::MTG_COLOR_GREEN; i <= Constants::MTG_COLOR_WHITE; ++i)
{
if (this->hasColor(i))
++colored;
}
if(colored > 1)
{
isMultiColored = 1;
}
if(this->hasColor(Constants::MTG_COLOR_WHITE) && this->hasColor(Constants::MTG_COLOR_BLACK))
isBlackAndWhite = 1;
if(this->hasColor(Constants::MTG_COLOR_RED) && this->hasColor(Constants::MTG_COLOR_BLUE))
isRedAndBlue = 1;
if(this->hasColor(Constants::MTG_COLOR_GREEN) && this->hasColor(Constants::MTG_COLOR_BLACK))
isBlackAndGreen = 1;
if(this->hasColor(Constants::MTG_COLOR_BLUE) && this->hasColor(Constants::MTG_COLOR_GREEN))
isBlueAndGreen = 1;
if(this->hasColor(Constants::MTG_COLOR_RED) && this->hasColor(Constants::MTG_COLOR_WHITE))
isRedAndWhite = 1;
if(previous && previous->morphed == true && !turningOver)
{
morphed = true;
isMorphed = true;
}
}
const string MTGCardInstance::getDisplayName() const
{
return getName();
}
void MTGCardInstance::addType(int type)
{
bool before = hasType(type);
CardPrimitive::addType(type);
WEvent * e = NEW WEventCardChangeType(this, type, before, true);
GameObserver * go = GameObserver::GetInstance();
if (go)
go->receiveEvent(e);
else
SAFE_DELETE(e);
}
void MTGCardInstance::addType(char * type_text)
{
setSubtype(type_text);
}
void MTGCardInstance::setType(const char * type_text)
{
setSubtype(type_text);
}
void MTGCardInstance::setSubtype(string value)
{
int id = Subtypes::subtypesList->find(value);
addType(id);
}
int MTGCardInstance::removeType(string value, int removeAll)
{
int id = Subtypes::subtypesList->find(value);
return removeType(id, removeAll);
}
int MTGCardInstance::removeType(int id, int removeAll)
{
bool before = hasType(id);
int result = CardPrimitive::removeType(id, removeAll);
bool after = hasType(id);
WEvent * e = NEW WEventCardChangeType(this, id, before, after);
GameObserver * go = GameObserver::GetInstance();
if (go)
go->receiveEvent(e);
else
SAFE_DELETE(e);
return result;
}
int MTGCardInstance::isInPlay()
{
GameObserver * game = GameObserver::GetInstance();
for (int i = 0; i < 2; i++)
{
MTGGameZone * zone = game->players[i]->game->inPlay;
if (zone->hasCard(this))
return 1;
}
return 0;
}
int MTGCardInstance::afterDamage()
{
if (!doDamageTest)
return 0;
doDamageTest = 0;
if (!isCreature())
return 0;
if (life <= 0 && isInPlay())
{
return destroy();
}
return 0;
}
int MTGCardInstance::bury()
{
Player * p = controller();
if (basicAbilities[Constants::EXILEDEATH])
{
p->game->putInZone(this, p->game->inPlay, owner->game->exile);
return 1;
}
if (!basicAbilities[Constants::INDESTRUCTIBLE])
{
p->game->putInZone(this, p->game->inPlay, owner->game->graveyard);
return 1;
}
return 0;
}
int MTGCardInstance::destroy()
{
if (!triggerRegenerate())
return bury();
return 0;
}
MTGGameZone * MTGCardInstance::getCurrentZone()
{
return currentZone;
}
int MTGCardInstance::has(int basicAbility)
{
return basicAbilities[basicAbility];
}
//sets card as attacked and sends events
void MTGCardInstance::eventattacked()
{
didattacked = 1;
WEvent * e = NEW WEventCardAttacked(this);
GameObserver * game = GameObserver::GetInstance();
game->receiveEvent(e);
}
//sets card as attacked alone and sends events
void MTGCardInstance::eventattackedAlone()
{
WEvent * e = NEW WEventCardAttackedAlone(this);
GameObserver * game = GameObserver::GetInstance();
game->receiveEvent(e);
}
//sets card as attacked and sends events
void MTGCardInstance::eventattackednotblocked()
{
didattacked = 1;
WEvent * e = NEW WEventCardAttackedNotBlocked(this);
GameObserver * game = GameObserver::GetInstance();
game->receiveEvent(e);
}
//sets card as attacked and sends events
void MTGCardInstance::eventattackedblocked()
{
didattacked = 1;
WEvent * e = NEW WEventCardAttackedBlocked(this);
GameObserver * game = GameObserver::GetInstance();
game->receiveEvent(e);
}
//sets card as blocking and sends events
void MTGCardInstance::eventblocked()
{
didblocked = 1;
WEvent * e = NEW WEventCardBlocked(this);
GameObserver * game = GameObserver::GetInstance();
game->receiveEvent(e);
}
//Taps the card
void MTGCardInstance::tap()
{
if (tapped)
return;
tapped = 1;
WEvent * e = NEW WEventCardTap(this, 0, 1);
GameObserver * game = GameObserver::GetInstance();
game->receiveEvent(e);
}
void MTGCardInstance::untap()
{
if (!tapped)
return;
tapped = 0;
WEvent * e = NEW WEventCardTap(this, 1, 0);
GameObserver * game = GameObserver::GetInstance();
game->receiveEvent(e);
}
void MTGCardInstance::setUntapping()
{
untapping = 1;
}
int MTGCardInstance::isUntapping()
{
return untapping;
}
//Tries to Untap the card
void MTGCardInstance::attemptUntap()
{
if (untapping)
{
untap();
untapping = 0;
}
}
//Tells if the card is tapped or not
int MTGCardInstance::isTapped()
{
return tapped;
}
int MTGCardInstance::regenerate()
{
if (has(Constants::CANTREGEN))
return 0;
return ++regenerateTokens;
}
int MTGCardInstance::triggerRegenerate()
{
if (!regenerateTokens)
return 0;
if (has(Constants::CANTREGEN))
return 0;
regenerateTokens--;
tap();
life = toughness;
initAttackersDefensers();
if (life < 1)
return 0; //regeneration didn't work (wither ?)
return 1;
}
int MTGCardInstance::initAttackersDefensers()
{
setAttacker(0);
setDefenser(NULL);
banding = NULL;
blockers.clear();
blocked = false;
didattacked = 0;
didblocked = 0;
return 1;
}
//Function to call to remove all damages, etc to a card (generally at the end of the turn)
int MTGCardInstance::cleanup()
{
initAttackersDefensers();
life = toughness;
GameObserver * game = GameObserver::GetInstance();
if (!game || game->currentPlayer == controller())
{
summoningSickness = 0;
}
if (previous && !previous->stillInUse())
{
//DebugTrace("MTGCardInstance::cleanup(): deleting " << ToHex(previous));
SAFE_DELETE(previous);
}
regenerateTokens = 0;
preventable = 0;
return 1;
}
int MTGCardInstance::stillInUse()
{
GameObserver * game = GameObserver::GetInstance();
if (game->mLayers->actionLayer()->stillInUse(this))
return 1;
if (!previous)
return 0;
return previous->stillInUse();
}
/* Summoning Sickness
* 212.3f A creature's activated ability with the tap symbol or the untap symbol in its activation cost
* can't be played unless the creature has been under its controller's control since the start of his or
* her most recent turn. A creature can't attack unless it has been under its controller's control
* since the start of his or her most recent turn. This rule is informally called the "summoning
* sickness" rule. Ignore this rule for creatures with haste (see rule 502.5).
*/
int MTGCardInstance::hasSummoningSickness()
{
if (!summoningSickness)
return 0;
if (basicAbilities[Constants::HASTE])
return 0;
if (!isCreature())
return 0;
return 1;
}
MTGCardInstance * MTGCardInstance::changeController(Player * newController)
{
Player * originalOwner = controller();
if (originalOwner == newController)
return this;
MTGCardInstance * copy = originalOwner->game->putInZone(this, originalOwner->game->inPlay, newController->game->inPlay);
copy->summoningSickness = 1;
return copy;
}
Player * MTGCardInstance::controller()
{
return lastController;
}
int MTGCardInstance::canAttack()
{
if (tapped)
return 0;
if (hasSummoningSickness())
return 0;
if (basicAbilities[Constants::DEFENSER] || basicAbilities[Constants::CANTATTACK])
return 0;
if (!isCreature())
return 0;
if (!isInPlay())
return 0;
return 1;
}
int MTGCardInstance::addToToughness(int value)
{
toughness += value;
life += value;
doDamageTest = 1;
return 1;
}
int MTGCardInstance::setToughness(int value)
{
toughness = value;
life = value;
doDamageTest = 1;
return 1;
}
int MTGCardInstance::canBlock()
{
if (tapped)
return 0;
if (basicAbilities[Constants::CANTBLOCK])
return 0;
if (!isCreature())
return 0;
if (!isInPlay())
return 0;
return 1;
}
int MTGCardInstance::canBlock(MTGCardInstance * opponent)
{
if (!canBlock())
return 0;
if (!opponent)
return 1;
if (!opponent->isAttacker())
return 0;
// Comprehensive rule 502.7f : If a creature with protection attacks, it can't be blocked by creatures that have the stated quality.
if (opponent->protectedAgainst(this))
return 0;
if (opponent->cantBeBlockedBy(this))
return 0;
if (opponent->basicAbilities[Constants::UNBLOCKABLE])
return 0;
if (opponent->basicAbilities[Constants::ONEBLOCKER] && opponent->blocked)
return 0;
if(opponent->basicAbilities[Constants::STRONG] && power < opponent->power)
return 0;
if(has(basicAbilities[Constants::WEAK]) && power < opponent->power)
return 0;
if (opponent->basicAbilities[Constants::FEAR] && !(hasType(Subtypes::TYPE_ARTIFACT) || hasColor(Constants::MTG_COLOR_BLACK)))
return 0;
//intimidate
if (opponent->basicAbilities[Constants::INTIMIDATE] && !(hasType(Subtypes::TYPE_ARTIFACT)))
{
int canblock = 0;
for (int i = Constants::MTG_COLOR_GREEN; i <= Constants::MTG_COLOR_WHITE; ++i)
{
if (hasColor(i) && opponent->hasColor(i))
{
canblock = 1;
break;
}
}
if (!canblock)
return 0;
}
if (opponent->basicAbilities[Constants::FLYING] && !(basicAbilities[Constants::FLYING] || basicAbilities[Constants::REACH]))
return 0;
//Can block only creatures with flying if has cloud
if (basicAbilities[Constants::CLOUD] && !(opponent->basicAbilities[Constants::FLYING]))
return 0;
// If opponent has shadow and a creature does not have either shadow or reachshadow it cannot be blocked
if (opponent->basicAbilities[Constants::SHADOW] && !(basicAbilities[Constants::SHADOW]
|| basicAbilities[Constants::REACHSHADOW]))
return 0;
// If opponent does not have shadow and a creature has shadow it cannot be blocked
if (!opponent->basicAbilities[Constants::SHADOW] && basicAbilities[Constants::SHADOW])
return 0;
if (opponent->basicAbilities[Constants::HORSEMANSHIP] && !basicAbilities[Constants::HORSEMANSHIP])
return 0;
if (opponent->basicAbilities[Constants::SWAMPWALK] && controller()->game->inPlay->hasType("swamp"))
return 0;
if (opponent->basicAbilities[Constants::FORESTWALK] && controller()->game->inPlay->hasType("forest"))
return 0;
if (opponent->basicAbilities[Constants::ISLANDWALK] && controller()->game->inPlay->hasType("island"))
return 0;
if (opponent->basicAbilities[Constants::MOUNTAINWALK] && controller()->game->inPlay->hasType("mountain"))
return 0;
if (opponent->basicAbilities[Constants::PLAINSWALK] && controller()->game->inPlay->hasType("plains"))
return 0;
if (opponent->basicAbilities[Constants::LEGENDARYWALK] && controller()->game->inPlay->hasPrimaryType("legendary","land"))
return 0;
if (opponent->basicAbilities[Constants::DESERTWALK] && controller()->game->inPlay->hasSpecificType("land","desert"))
return 0;
if (opponent->basicAbilities[Constants::SNOWSWAMPWALK] && controller()->game->inPlay->hasSpecificType("snow","swamp"))
return 0;
if (opponent->basicAbilities[Constants::SNOWFORESTWALK] && controller()->game->inPlay->hasSpecificType("snow","forest"))
return 0;
if (opponent->basicAbilities[Constants::SNOWISLANDWALK] && controller()->game->inPlay->hasSpecificType("snow","island"))
return 0;
if (opponent->basicAbilities[Constants::SNOWMOUNTAINWALK] && controller()->game->inPlay->hasSpecificType("snow","mountain"))
return 0;
if (opponent->basicAbilities[Constants::SNOWPLAINSWALK] && controller()->game->inPlay->hasSpecificType("snow","plains"))
return 0;
if (opponent->basicAbilities[Constants::SNOWWALK] && controller()->game->inPlay->hasPrimaryType("snow","land"))
return 0;
if (opponent->basicAbilities[Constants::NONBASICWALK] && controller()->game->inPlay->hasTypeButNotType("land","basic"))
return 0;
return 1;
}
JQuad * MTGCardInstance::getIcon()
{
return WResourceManager::Instance()->RetrieveCard(this, CACHE_THUMB);
}
MTGCardInstance * MTGCardInstance::getNextPartner()
{
MTGInPlay * inplay = controller()->game->inPlay;
MTGCardInstance * bandingPartner = inplay->getNextAttacker(banding);
while (bandingPartner)
{
if (basicAbilities[Constants::BANDING] || bandingPartner->basicAbilities[Constants::BANDING])
return bandingPartner;
bandingPartner = inplay->getNextAttacker(bandingPartner);
}
return NULL;
}
int MTGCardInstance::DangerRanking()
{
int danger;
int result;
danger = 0;
result = 0;
result += power;
result += toughness;
result += getManaCost()->getConvertedCost();
for (int j = 0; j < Constants::NB_BASIC_ABILITIES; j++)
{
if (basicAbilities[j])
{
result += 1;
}
}
if (result > 1)
danger += 1;
if (result > 2)
danger += 1;
if (result > 4)
danger += 1;
if (result > 6)
danger += 1;
if (result > 10)
danger += 1;
return danger;
}
int MTGCardInstance::setAttacker(int value)
{
Targetable * previousTarget = NULL;
Targetable * target = NULL;
Player * p = controller()->opponent();
if (value)
target = p;
if (attacker)
previousTarget = p;
attacker = value;
WEvent * e = NEW WEventCreatureAttacker(this, previousTarget, target);
GameObserver * go = GameObserver::GetInstance();
if (go)
go->receiveEvent(e);
else
SAFE_DELETE(e);
return 1;
}
int MTGCardInstance::toggleAttacker()
{
if (!attacker)
{
//if (!basicAbilities[Constants::VIGILANCE]) tap();
setAttacker(1);
return 1;
}
else
{
//untap();
setAttacker(0);
return 1;
}
return 0;
}
int MTGCardInstance::isAttacker()
{
return attacker;
}
MTGCardInstance * MTGCardInstance::isDefenser()
{
return defenser;
}
int MTGCardInstance::nbOpponents()
{
int result = 0;
MTGCardInstance* opponent = getNextOpponent();
while (opponent)
{
result++;
opponent = getNextOpponent(opponent);
}
return result;
}
int MTGCardInstance::raiseBlockerRankOrder(MTGCardInstance * blocker)
{
list<MTGCardInstance *>::iterator it1 = find(blockers.begin(), blockers.end(), blocker);
list<MTGCardInstance *>::iterator it2 = it1;
if (blockers.begin() == it2)
++it2;
else
--it2;
std::iter_swap(it1, it2);
WEvent* e = NEW WEventCreatureBlockerRank(*it1, *it2, this);
GameObserver * go = GameObserver::GetInstance();
if (go)
go->receiveEvent(e);
else
SAFE_DELETE(e);
//delete(e);
return 1;
}
int MTGCardInstance::getDefenserRank(MTGCardInstance * blocker)
{
int result = 0;
for (list<MTGCardInstance *>::iterator it1 = blockers.begin(); it1 != blockers.end(); ++it1)
{
result++;
if ((*it1) == blocker)
return result;
}
return 0;
}
;
int MTGCardInstance::removeBlocker(MTGCardInstance * blocker)
{
blockers.remove(blocker);
if (!blockers.size())
{
blocked = false;
}
return 1;
}
int MTGCardInstance::addBlocker(MTGCardInstance * blocker)
{
blockers.push_back(blocker);
blocked = true;
return 1;
}
//Returns opponents to this card for this turn. This * should * take into account banding
MTGCardInstance * MTGCardInstance::getNextOpponent(MTGCardInstance * previous)
{
GameObserver * game = GameObserver::GetInstance();
int foundprevious = 0;
if (!previous)
foundprevious = 1;
if (attacker)
{
MTGInPlay * inPlay = game->opponent()->game->inPlay;
for (int i = 0; i < inPlay->nb_cards; i++)
{
MTGCardInstance * current = inPlay->cards[i];
if (current == previous)
{
foundprevious = 1;
}
else if (foundprevious)
{
MTGCardInstance * defensersOpponent = current->isDefenser();
if (defensersOpponent && (defensersOpponent == this || (banding && defensersOpponent->banding == banding)))
{
return current;
}
}
}
}
else if (defenser)
{
MTGInPlay * inPlay = game->currentPlayer->game->inPlay;
for (int i = 0; i < inPlay->nb_cards; i++)
{
MTGCardInstance * current = inPlay->cards[i];
if (current == previous)
{
foundprevious = 1;
}
else if (foundprevious)
{
if (defenser == current || (current->banding && defenser->banding == current->banding))
{
return current;
}
}
}
}
return NULL;
}
int MTGCardInstance::setDefenser(MTGCardInstance * opponent)
{
GameObserver * g = GameObserver::GetInstance();
if (defenser)
{
if (g->players[0]->game->battlefield->hasCard(defenser) || g->players[1]->game->battlefield->hasCard(defenser))
{
defenser->removeBlocker(this);
}
}
WEvent * e = NULL;
if (defenser != opponent)
e = NEW WEventCreatureBlocker(this, defenser, opponent);
defenser = opponent;
if (defenser)
defenser->addBlocker(this);
if (e)
g->receiveEvent(e);
return 1;
}
int MTGCardInstance::toggleDefenser(MTGCardInstance * opponent)
{
if (canBlock())
{
if (canBlock(opponent))
{
setDefenser(opponent);
didblocked = 1;
if (opponent && opponent->controller()->isAI())
{
if(opponent->view != NULL)
{
opponent->view->actZ += .8f;
opponent->view->actT -= .2f;
}
}
if (!opponent)
didblocked = 0;
return 1;
}
}
return 0;
}
int MTGCardInstance::addProtection(TargetChooser * tc)
{
tc->targetter = NULL;
protections.push_back(tc);
return protections.size();
}
int MTGCardInstance::removeProtection(TargetChooser * tc, int erase)
{
for (size_t i = 0; i < protections.size(); i++)
{
if (protections[i] == tc)
{
if (erase)
delete (protections[i]);
protections.erase(protections.begin() + i);
return 1;
}
}
return 0;
}
int MTGCardInstance::protectedAgainst(MTGCardInstance * card)
{
//Basic protections
for (int i = Constants::PROTECTIONGREEN; i <= Constants::PROTECTIONWHITE; i++)
{
if (basicAbilities[i] && card->hasColor(i - Constants::PROTECTIONGREEN + Constants::MTG_COLOR_GREEN))
return 1;
}
//General protections
for (size_t i = 0; i < protections.size(); i++)
{
if (protections[i]->canTarget(card))
return 1;
}
return 0;
}
int MTGCardInstance::addCantBeBlockedBy(TargetChooser * tc)
{
cantBeBlockedBys.push_back(tc);
return cantBeBlockedBys.size();
}
int MTGCardInstance::removeCantBeBlockedBy(TargetChooser * tc, int erase)
{
for (size_t i = 0; i < cantBeBlockedBys.size(); i++)
{
if (cantBeBlockedBys[i] == tc)
{
if (erase)
delete (cantBeBlockedBys[i]);
cantBeBlockedBys.erase(cantBeBlockedBys.begin() + i);
return 1;
}
}
return 0;
}
int MTGCardInstance::cantBeBlockedBy(MTGCardInstance * card)
{
for (size_t i = 0; i < cantBeBlockedBys.size(); i++)
{
if (cantBeBlockedBys[i]->canTarget(card))
return 1;
}
return 0;
}
/* Choose a sound sample to associate to that card */
JSample * MTGCardInstance::getSample()
{
JSample * js;
if (sample.size())
return WResourceManager::Instance()->RetrieveSample(sample);
for (int i = types.size() - 1; i > 0; i--)
{
string type = Subtypes::subtypesList->find(types[i]);
type = type + ".wav";
js = WResourceManager::Instance()->RetrieveSample(type);
if (js)
{
sample = string(type);
return js;
}
}
for (map<int, int>::const_iterator it = basicAbilities.begin(); it != basicAbilities.end(); ++it)
{
int i = it->first;
if (!basicAbilities[i])
continue;
string type = Constants::MTGBasicAbilities[i];
type = type + ".wav";
js = WResourceManager::Instance()->RetrieveSample(type);
if (js)
{
sample = string(type);
return js;
}
}
string type = Subtypes::subtypesList->find(types[0]);
type = type + ".wav";
js = WResourceManager::Instance()->RetrieveSample(type);
if (js)
{
sample = string(type);
return js;
}
return NULL;
}
int MTGCardInstance::stepPower(CombatStep step)
{
switch (step)
{
case FIRST_STRIKE:
case END_FIRST_STRIKE:
if (has(Constants::FIRSTSTRIKE) || has(Constants::DOUBLESTRIKE))
return MAX(0, power);
else
return 0;
case DAMAGE:
case END_DAMAGE:
default:
if (has(Constants::FIRSTSTRIKE) && !has(Constants::DOUBLESTRIKE))
return 0;
else
return MAX(0, power);
}
}
std::ostream& MTGCardInstance::toString(std::ostream& out) const
{
return out << name;
}
std::ostream& operator<<(std::ostream& out, const MTGCardInstance& c)
{
return c.toString(out);
}