Refactored ABlink: extracted common code as method "returnCardIntoPlay"

This commit is contained in:
pankdm
2013-09-27 19:25:28 +00:00
parent d4268f18af
commit 7894ca3ace
2 changed files with 80 additions and 135 deletions
+3 -1
View File
@@ -4426,13 +4426,15 @@ public:
MTGCardInstance * Blinked; MTGCardInstance * Blinked;
bool resolved; bool resolved;
MTGAbility * stored; MTGAbility * stored;
ABlink(GameObserver* observer, int _id, MTGCardInstance * card, MTGCardInstance * _target,bool blinkueot=false,bool blinkForSource = false,bool blinkhand = false,MTGAbility * stored = NULL); ABlink(GameObserver* observer, int _id, MTGCardInstance * card, MTGCardInstance * _target, bool blinkueot = false, bool blinkForSource = false, bool blinkhand = false, MTGAbility * stored = NULL);
void Update(float dt); void Update(float dt);
void resolveBlink(); void resolveBlink();
int resolve(); int resolve();
const char * getMenuText(); const char * getMenuText();
ABlink * clone() const; ABlink * clone() const;
~ABlink(); ~ABlink();
private:
void returnCardIntoPlay(MTGCardInstance *_target);
}; };
//blinkinstant //blinkinstant
+29 -86
View File
@@ -4958,7 +4958,7 @@ APhaseActionGeneric::~APhaseActionGeneric()
} }
//a blink //a blink
ABlink::ABlink(GameObserver* observer, int _id, MTGCardInstance * card, MTGCardInstance * _target,bool blinkueot,bool blinkForSource,bool blinkhand,MTGAbility * stored) : ABlink::ABlink(GameObserver* observer, int _id, MTGCardInstance * card, MTGCardInstance * _target, bool blinkueot, bool blinkForSource, bool blinkhand, MTGAbility * stored) :
MTGAbility(observer, _id, card),blinkueot(blinkueot),blinkForSource(blinkForSource),blinkhand(blinkhand),stored(stored) MTGAbility(observer, _id, card),blinkueot(blinkueot),blinkForSource(blinkForSource),blinkhand(blinkhand),stored(stored)
{ {
target = _target; target = _target;
@@ -4968,86 +4968,18 @@ MTGAbility(observer, _id, card),blinkueot(blinkueot),blinkForSource(blinkForSour
void ABlink::Update(float dt) void ABlink::Update(float dt)
{ {
if(resolved == false) if (resolved == false)
{ {
resolved = true; resolved = true;
resolveBlink(); resolveBlink();
} }
if ((blinkueot && currentPhase == MTG_PHASE_ENDOFTURN)||(blinkForSource && !source->isInPlay(game))) if ((blinkueot && currentPhase == MTG_PHASE_ENDOFTURN) || (blinkForSource && !source->isInPlay(game)))
{ {
if(Blinked == NULL) if (Blinked == NULL)
MTGAbility::Update(dt); MTGAbility::Update(dt);
MTGCardInstance * _target = Blinked; MTGCardInstance * _target = Blinked;
MTGCardInstance * Blinker = NULL; returnCardIntoPlay(_target);
if(!blinkhand)
Blinker = _target->controller()->game->putInZone(_target, _target->currentZone,
_target->owner->game->battlefield);
if(blinkhand)
{
_target->controller()->game->putInZone(_target, _target->currentZone,
_target->owner->game->hand);
return;
}
Spell * spell = NEW Spell(game, Blinker);
spell->source->counters->init();
if(spell->source->hasSubtype(Subtypes::TYPE_AURA) && !blinkhand)
{
TargetChooserFactory tcf(game);
TargetChooser * tc = tcf.createTargetChooser(spell->source->spellTargetType,spell->source);
if(!tc->validTargetsExist())
{
spell->source->owner->game->putInExile(spell->source);
SAFE_DELETE(spell);
SAFE_DELETE(tc);
this->forceDestroy = 1;
return;
}
MTGGameZone * inplay = spell->source->owner->game->inPlay;
spell->source->target = NULL;
for(int i = game->getRandomGenerator()->random()%inplay->nb_cards;;i = game->getRandomGenerator()->random()%inplay->nb_cards)
{
if(tc->canTarget(inplay->cards[i]) && spell->source->target == NULL)
{
spell->source->target = inplay->cards[i];
spell->getNextCardTarget();
spell->resolve();
SAFE_DELETE(spell);
SAFE_DELETE(tc);
this->forceDestroy = 1;
return;
}
if(!tc->validTargetsExist())
return;
}
}
spell->source->power = spell->source->origpower;
spell->source->toughness = spell->source->origtoughness;
spell->source->X = 0;
if(!spell->source->hasSubtype(Subtypes::TYPE_AURA))
{
spell->resolve();
if(stored)
{
MTGAbility * clonedStored = stored->clone();
clonedStored->target = spell->source;
if (clonedStored->oneShot)
{
clonedStored->resolve();
delete (clonedStored);
}
else
{
clonedStored->addToGame();
}
}
}
delete spell;
this->forceDestroy = 1;
Blinker = NULL;
return;
} }
MTGAbility::Update(dt); MTGAbility::Update(dt);
} }
@@ -5081,23 +5013,35 @@ void ABlink::resolveBlink()
Blinked = _target; Blinked = _target;
if(!blinkueot && !blinkForSource) if(!blinkueot && !blinkForSource)
{ {
returnCardIntoPlay(_target);
}
}
}
void ABlink::returnCardIntoPlay(MTGCardInstance* _target) {
MTGCardInstance * Blinker = NULL; MTGCardInstance * Blinker = NULL;
if(!blinkhand) if (!blinkhand)
Blinker = _target->controller()->game->putInZone(_target, _target->currentZone, Blinker = _target->controller()->game->putInZone(
_target,
_target->currentZone,
_target->owner->game->battlefield); _target->owner->game->battlefield);
if(blinkhand) if (blinkhand)
{ {
_target->controller()->game->putInZone(_target, _target->currentZone, _target->controller()->game->putInZone(
_target,
_target->currentZone,
_target->owner->game->hand); _target->owner->game->hand);
return; return;
} }
Spell * spell = NEW Spell(game, Blinker); Spell * spell = NEW Spell(game, Blinker);
spell->source->counters->init(); spell->source->counters->init();
if(spell->source->hasSubtype(Subtypes::TYPE_AURA) && !blinkhand) if (spell->source->hasSubtype(Subtypes::TYPE_AURA) && !blinkhand)
{ {
TargetChooserFactory tcf(game); TargetChooserFactory tcf(game);
TargetChooser * tc = tcf.createTargetChooser(spell->source->spellTargetType,spell->source); TargetChooser * tc = tcf.createTargetChooser(
if(!tc->validTargetsExist()) spell->source->spellTargetType,
spell->source);
if (!tc->validTargetsExist())
{ {
spell->source->owner->game->putInExile(spell->source); spell->source->owner->game->putInExile(spell->source);
SAFE_DELETE(spell); SAFE_DELETE(spell);
@@ -5108,7 +5052,7 @@ void ABlink::resolveBlink()
MTGGameZone * inplay = spell->source->owner->game->inPlay; MTGGameZone * inplay = spell->source->owner->game->inPlay;
spell->source->target = NULL; spell->source->target = NULL;
for(int i = game->getRandomGenerator()->random()%inplay->nb_cards;;i = game->getRandomGenerator()->random()%inplay->nb_cards) for (int i = game->getRandomGenerator()->random()%inplay->nb_cards;;i = game->getRandomGenerator()->random()%inplay->nb_cards)
{ {
if(tc->canTarget(inplay->cards[i]) && spell->source->target == NULL) if(tc->canTarget(inplay->cards[i]) && spell->source->target == NULL)
{ {
@@ -5125,8 +5069,9 @@ void ABlink::resolveBlink()
spell->source->power = spell->source->origpower; spell->source->power = spell->source->origpower;
spell->source->toughness = spell->source->origtoughness; spell->source->toughness = spell->source->origtoughness;
spell->source->X = 0; spell->source->X = 0;
if (!spell->source->hasSubtype(Subtypes::TYPE_AURA)) {
spell->resolve(); spell->resolve();
if(stored) if (stored)
{ {
MTGAbility * clonedStored = stored->clone(); MTGAbility * clonedStored = stored->clone();
clonedStored->target = spell->source; clonedStored->target = spell->source;
@@ -5140,14 +5085,12 @@ void ABlink::resolveBlink()
clonedStored->addToGame(); clonedStored->addToGame();
} }
} }
}
SAFE_DELETE(spell); SAFE_DELETE(spell);
SAFE_DELETE(tc); SAFE_DELETE(tc);
SAFE_DELETE(stored);
this->forceDestroy = 1; this->forceDestroy = 1;
if(stored)
delete(stored);
Blinked = NULL; Blinked = NULL;
}
}
} }
int ABlink::resolve() int ABlink::resolve()