diff --git a/projects/mtg/include/AllAbilities.h b/projects/mtg/include/AllAbilities.h index 6521eec2a..8ca14a32e 100644 --- a/projects/mtg/include/AllAbilities.h +++ b/projects/mtg/include/AllAbilities.h @@ -719,11 +719,8 @@ public: return 0; if (!tc->canTarget(e->player)) return 0; if (fromTc && !fromTc->canTarget(e->player)) return 0; - if (type == 1 && (e->amount > 0 || e->Ltype == 0)) return 0; - if (type == 0 && (e->amount < 0 || e->Ltype == 1)) return 0; - if(type != 1) - e->player->thatmuch = e->amount; - else + if (type == 1 && (e->amount > 0)) return 0; + if (type == 0 && (e->amount < 0)) return 0; e->player->thatmuch = abs(e->amount); return 1; } @@ -5742,12 +5739,7 @@ public: MTGCardInstance * card = d->source; if (d->damage > 0 && card && (card == source || card == source->target)) { - source->owner->life += d->damage; - source->owner->thatmuch = d->damage; - WEvent * lifed = NULL; - lifed = NEW WEventLife(source->owner,d->damage); - GameObserver * game = GameObserver::GetInstance(); - game->receiveEvent(lifed); + source->owner->gainLife(d->damage); return 1; } } diff --git a/projects/mtg/include/Player.h b/projects/mtg/include/Player.h index 474dec2a9..68aaa451f 100644 --- a/projects/mtg/include/Player.h +++ b/projects/mtg/include/Player.h @@ -62,6 +62,11 @@ public: } int afterDamage(); + + int gainLife(int value); + int loseLife(int value); + int gainOrLoseLife(int value); + int poisoned(); int damaged(); int prevented(); diff --git a/projects/mtg/include/WEvent.h b/projects/mtg/include/WEvent.h index eee7e8872..8f93e0e66 100644 --- a/projects/mtg/include/WEvent.h +++ b/projects/mtg/include/WEvent.h @@ -54,8 +54,7 @@ struct WEventDamage : public WEvent { struct WEventLife : public WEvent { Player * player; int amount; - int Ltype; - WEventLife(Player * player,int amount,int Ltype = 0); + WEventLife(Player * player,int amount); virtual Targetable * getTarget(int target); }; diff --git a/projects/mtg/src/AllAbilities.cpp b/projects/mtg/src/AllAbilities.cpp index 6c5706abf..6f5008363 100644 --- a/projects/mtg/src/AllAbilities.cpp +++ b/projects/mtg/src/AllAbilities.cpp @@ -1254,25 +1254,19 @@ ActivatedAbilityTP(_id, card, _target, _cost, _tap, who),life_s(life_s), life(li } int AALifer::resolve() -{ - RefreshedLife = NEW WParsedInt(life_s, NULL, source); +{ Damageable * _target = (Damageable *) getTarget(); - if (_target) + if (!_target) + return 0; + + RefreshedLife = NEW WParsedInt(life_s, NULL, source); + if (_target->type_as_damageable == DAMAGEABLE_MTGCARDINSTANCE) { - if (_target->type_as_damageable == DAMAGEABLE_MTGCARDINSTANCE) - { - _target = ((MTGCardInstance *) _target)->controller(); - } - Player *player = (Player*)_target; - player->thatmuch = abs(RefreshedLife->getValue()); - WEvent * lifed = NULL; - lifed = NEW WEventLife(player,RefreshedLife->getValue()); - GameObserver * game = GameObserver::GetInstance(); - game->receiveEvent(lifed); - _target->life += RefreshedLife->getValue(); - if(life->getValue() < 0) - _target->lifeLostThisTurn += abs(RefreshedLife->getValue()); + _target = ((MTGCardInstance *) _target)->controller(); } + Player *player = (Player*)_target; + player->gainOrLoseLife(RefreshedLife->getValue()); + delete RefreshedLife; return 1; } @@ -1307,30 +1301,23 @@ AALifeSet::AALifeSet(int _id, MTGCardInstance * _source, Targetable * _target, W int AALifeSet::resolve() { Damageable * _target = (Damageable *) getTarget(); - if (_target) - { - if (_target->type_as_damageable == DAMAGEABLE_MTGCARDINSTANCE) - { - _target = ((MTGCardInstance *) _target)->controller(); - } + if (!_target) + return 0; - int lifeDiff = life->getValue() - _target->life ; - WEvent * lifed = NULL; - if(lifeDiff < 0) - { - _target->lifeLostThisTurn += abs(lifeDiff); - lifed = NEW WEventLife((Player*)_target,lifeDiff,1); - } - else - { - lifed = NEW WEventLife((Player*)_target,lifeDiff,0); - } - _target->thatmuch = abs(lifeDiff); - GameObserver * game = GameObserver::GetInstance(); - game->receiveEvent(lifed); - _target->life = life->getValue(); + Player * p = NULL; + if (_target->type_as_damageable == DAMAGEABLE_MTGCARDINSTANCE) + { + p = ((MTGCardInstance *) _target)->controller(); } - return 0; + else + { + p = (Player*)_target; + } + + int lifeDiff = life->getValue() - p->life ; + p->gainOrLoseLife(lifeDiff); + + return 1; } const char * AALifeSet::getMenuText() diff --git a/projects/mtg/src/ExtraCost.cpp b/projects/mtg/src/ExtraCost.cpp index 0f3a0a66b..ceb095ac1 100644 --- a/projects/mtg/src/ExtraCost.cpp +++ b/projects/mtg/src/ExtraCost.cpp @@ -76,22 +76,16 @@ LifeCost::LifeCost(TargetChooser *_tc) : int LifeCost::doPay() { + if (!target) + return 0; + MTGCardInstance * _target = (MTGCardInstance *) target; - if (target) - { - _target->controller()->thatmuch = 1; - WEvent * lifed = NULL; - lifed = NEW WEventLife(_target->controller(),-1,1); - GameObserver * game = GameObserver::GetInstance(); - game->receiveEvent(lifed); - _target->controller()->life -= 1; - _target->controller()->lifeLostThisTurn += 1; - target = NULL; - if (tc) - tc->initTargets(); - return 1; - } - return 0; + + _target->controller()->loseLife(1); + target = NULL; + if (tc) + tc->initTargets(); + return 1; } //discard a card at random as a cost diff --git a/projects/mtg/src/MTGAbility.cpp b/projects/mtg/src/MTGAbility.cpp index 5bcdc1c0d..112b3b755 100644 --- a/projects/mtg/src/MTGAbility.cpp +++ b/projects/mtg/src/MTGAbility.cpp @@ -3269,12 +3269,7 @@ void AbilityFactory::addAbilities(int _id, Spell * spell) game->mLayers->stackLayer()->addDamage(card, target, x); if (target->life < x) x = target->life; - game->currentlyActing()->life += x; - game->currentlyActing()->thatmuch = x; - WEvent * lifed = NULL; - lifed = NEW WEventLife(game->currentlyActing(),x); - GameObserver * game = GameObserver::GetInstance(); - game->receiveEvent(lifed); + game->currentlyActing()->gainLife(x); break; } case 1159: //Erg Raiders @@ -3485,12 +3480,7 @@ void AbilityFactory::addAbilities(int _id, Spell * spell) if (current->hasType("Artifact")) { game->players[i]->game->putInGraveyard(current); - current->controller()->life += current->getManaCost()->getConvertedCost(); - current->controller()->thatmuch = current->getManaCost()->getConvertedCost(); - WEvent * lifed = NULL; - lifed = NEW WEventLife(current->controller(),current->getManaCost()->getConvertedCost()); - GameObserver * game = GameObserver::GetInstance(); - game->receiveEvent(lifed); + current->controller()->gainLife(current->getManaCost()->getConvertedCost()); } } } @@ -3603,12 +3593,7 @@ void AbilityFactory::addAbilities(int _id, Spell * spell) if (library->nb_cards) player->game->putInZone(library->cards[library->nb_cards - 1], library, player->game->graveyard); } - game->currentlyActing()->life += x; - game->currentlyActing()->thatmuch = x; - WEvent * lifed = NULL; - lifed = NEW WEventLife(game->currentlyActing(),x); - GameObserver * game = GameObserver::GetInstance(); - game->receiveEvent(lifed); + game->currentlyActing()->gainLife(x); break; } diff --git a/projects/mtg/src/MTGRules.cpp b/projects/mtg/src/MTGRules.cpp index b2946ef37..083dc7e09 100644 --- a/projects/mtg/src/MTGRules.cpp +++ b/projects/mtg/src/MTGRules.cpp @@ -1868,12 +1868,7 @@ int MTGLifelinkRule::receiveEvent(WEvent * event) MTGCardInstance * card = d->source; if (d->damage > 0 && card && card->basicAbilities[Constants::LIFELINK]) { - card->controller()->thatmuch = d->damage; - WEvent * lifed = NULL; - lifed = NEW WEventLife(card->controller(),d->damage); - GameObserver * game = GameObserver::GetInstance(); - game->receiveEvent(lifed); - card->controller()->life += d->damage; + card->controller()->gainLife(d->damage); return 1; } } diff --git a/projects/mtg/src/Player.cpp b/projects/mtg/src/Player.cpp index 3fb8b1d75..ccaefcac8 100644 --- a/projects/mtg/src/Player.cpp +++ b/projects/mtg/src/Player.cpp @@ -116,6 +116,45 @@ ManaPool * Player::getManaPool() return manaPool; } +int Player::gainOrLoseLife(int value) +{ + if (!value) + return 0; //Don't do anything if there's no actual life change + + thatmuch = abs(value); //What is thatmuch used for? + life+=value; + if (value<0) + lifeLostThisTurn -= value; + + //Send life event to listeners + WEvent * lifed = NEW WEventLife(this,value); + GameObserver * game = GameObserver::GetInstance(); + game->receiveEvent(lifed); + + return value; +}; + +int Player::gainLife(int value) +{ + if (value <0) + { + DebugTrace("PLAYER.CPP: don't call gainLife on a negative value, use loseLife instead"); + return 0; + } + return gainOrLoseLife(value); +}; + +int Player::loseLife(int value) +{ + if (value <0) + { + DebugTrace("PLAYER.CPP: don't call loseLife on a negative value, use gainLife instead"); + return 0; + } + return gainOrLoseLife(-value); +}; + + int Player::afterDamage() { return life; diff --git a/projects/mtg/src/WEvent.cpp b/projects/mtg/src/WEvent.cpp index cd2e498e9..08ba3863e 100644 --- a/projects/mtg/src/WEvent.cpp +++ b/projects/mtg/src/WEvent.cpp @@ -21,8 +21,8 @@ WEventDamage::WEventDamage(Damage *damage) : { } -WEventLife::WEventLife(Player * player,int amount,int Ltype) : - WEvent(), player(player),amount(amount), Ltype(Ltype) +WEventLife::WEventLife(Player * player,int amount) : + WEvent(), player(player),amount(amount) { }