Search in sources :

Example 1 with Abilities

use of mage.abilities.Abilities in project mage by magefree.

the class PermanentImpl method doDamage.

/**
 * @param damageAmount
 * @param attackerId   id of the permanent or player who make damage (source.getSourceId() in most cases)
 * @param source
 * @param game
 * @param preventable
 * @param combat
 * @param markDamage   If true, damage will be dealt later in applyDamage
 *                     method, uses only in inner markDamage.
 * @return
 */
private int doDamage(int damageAmount, UUID attackerId, Ability source, Game game, boolean preventable, boolean combat, boolean markDamage, List<UUID> appliedEffects) {
    int damageDone = 0;
    if (damageAmount < 1 || !canDamage(game.getObject(attackerId), game)) {
        return 0;
    }
    DamageEvent event = new DamagePermanentEvent(objectId, attackerId, controllerId, damageAmount, preventable, combat);
    event.setAppliedEffects(appliedEffects);
    if (game.replaceEvent(event)) {
        return 0;
    }
    int actualDamage = checkProtectionAbilities(event, attackerId, source, game);
    if (actualDamage < 1) {
        return 0;
    }
    int lethal = getLethalDamage(attackerId, game);
    MageObject attacker = game.getObject(attackerId);
    if (this.isCreature(game)) {
        if (checkWither(event, attacker, game)) {
            if (markDamage) {
                // mark damage only
                markDamage(CounterType.M1M1.createInstance(actualDamage), attacker, true);
            } else {
                Ability damageSourceAbility = null;
                if (attacker instanceof Permanent) {
                    damageSourceAbility = ((Permanent) attacker).getSpellAbility();
                }
                // deal damage immediately
                addCounters(CounterType.M1M1.createInstance(actualDamage), game.getControllerId(attackerId), damageSourceAbility, game);
            }
        } else {
            this.damage = CardUtil.overflowInc(this.damage, actualDamage);
        }
    }
    if (this.isPlaneswalker(game)) {
        int loyalty = getCounters(game).getCount(CounterType.LOYALTY);
        int countersToRemove = Math.min(actualDamage, loyalty);
        if (attacker != null && markDamage) {
            markDamage(CounterType.LOYALTY.createInstance(countersToRemove), attacker, false);
        } else {
            removeCounters(CounterType.LOYALTY.getName(), countersToRemove, source, game);
        }
    }
    DamagedEvent damagedEvent = new DamagedPermanentEvent(this.getId(), attackerId, this.getControllerId(), actualDamage, combat);
    damagedEvent.setExcess(actualDamage - lethal);
    game.fireEvent(damagedEvent);
    game.getState().addSimultaneousDamage(damagedEvent, game);
    damageDone = actualDamage;
    if (damageDone < 1) {
        return 0;
    }
    UUID sourceControllerId = null;
    Abilities sourceAbilities = null;
    attacker = game.getPermanentOrLKIBattlefield(attackerId);
    if (attacker == null) {
        StackObject stackObject = game.getStack().getStackObject(attackerId);
        if (stackObject != null) {
            attacker = stackObject.getStackAbility().getSourceObject(game);
        } else {
            attacker = game.getObject(attackerId);
        }
        if (attacker instanceof Spell) {
            sourceAbilities = ((Spell) attacker).getAbilities(game);
            sourceControllerId = ((Spell) attacker).getControllerId();
        } else if (attacker instanceof Card) {
            sourceAbilities = ((Card) attacker).getAbilities(game);
            sourceControllerId = ((Card) attacker).getOwnerId();
        } else if (attacker instanceof CommandObject) {
            sourceControllerId = ((CommandObject) attacker).getControllerId();
            sourceAbilities = attacker.getAbilities();
        } else {
            attacker = null;
        }
    } else {
        sourceAbilities = ((Permanent) attacker).getAbilities(game);
        sourceControllerId = ((Permanent) attacker).getControllerId();
    }
    if (attacker != null && sourceAbilities != null) {
        if (sourceAbilities.containsKey(LifelinkAbility.getInstance().getId())) {
            if (markDamage) {
                game.getPermanent(attackerId).markLifelink(damageDone);
            } else {
                Player player = game.getPlayer(sourceControllerId);
                player.gainLife(damageDone, game, source);
            }
        }
        if (sourceAbilities.containsKey(DeathtouchAbility.getInstance().getId())) {
            deathtouched = true;
        }
        if (dealtDamageByThisTurn == null) {
            dealtDamageByThisTurn = new HashSet<>();
        }
        // Unstable ability - Earl of Squirrel
        if (sourceAbilities.containsKey(SquirrellinkAbility.getInstance().getId())) {
            Player player = game.getPlayer(sourceControllerId);
            new SquirrelToken().putOntoBattlefield(damageDone, game, source, player.getId());
        }
        dealtDamageByThisTurn.add(new MageObjectReference(attacker, game));
    }
    if (attacker == null) {
        game.informPlayers(getLogName() + " gets " + damageDone + " damage");
    } else {
        game.informPlayers(attacker.getLogName() + " deals " + damageDone + " damage to " + getLogName());
    }
    return damageDone;
}
Also used : SpellAbility(mage.abilities.SpellAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Abilities(mage.abilities.Abilities) MageObject(mage.MageObject) StackObject(mage.game.stack.StackObject) Hint(mage.abilities.hint.Hint) Spell(mage.game.stack.Spell) TargetCard(mage.target.TargetCard) Card(mage.cards.Card) CommandObject(mage.game.command.CommandObject) SquirrelToken(mage.game.permanent.token.SquirrelToken) MageObjectReference(mage.MageObjectReference)

Example 2 with Abilities

use of mage.abilities.Abilities in project mage by magefree.

the class AkromaVisionOfIxidorEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    for (Permanent permanent : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_CONTROLLED_ANOTHER_CREATURE, source.getControllerId(), source.getSourceId(), game)) {
        Abilities abilities = permanent.getAbilities(game);
        int count = classes.stream().map(clazz -> abilities.stream().anyMatch(clazz::isInstance)).mapToInt(b -> b ? 1 : 0).sum();
        if (count > 0) {
            ContinuousEffect effect = new BoostTargetEffect(count, count, Duration.EndOfTurn);
            effect.setTargetPointer(new FixedTarget(permanent.getId(), game));
            game.addEffect(effect, source);
        }
    }
    return true;
}
Also used : BeginningOfCombatTriggeredAbility(mage.abilities.common.BeginningOfCombatTriggeredAbility) BoostTargetEffect(mage.abilities.effects.common.continuous.BoostTargetEffect) StaticFilters(mage.filter.StaticFilters) Arrays(java.util.Arrays) mage.abilities.keyword(mage.abilities.keyword) Set(java.util.Set) OneShotEffect(mage.abilities.effects.OneShotEffect) UUID(java.util.UUID) MageInt(mage.MageInt) ContinuousEffect(mage.abilities.effects.ContinuousEffect) FixedTarget(mage.target.targetpointer.FixedTarget) CardSetInfo(mage.cards.CardSetInfo) HashSet(java.util.HashSet) Game(mage.game.Game) CardImpl(mage.cards.CardImpl) Permanent(mage.game.permanent.Permanent) mage.constants(mage.constants) Abilities(mage.abilities.Abilities) Ability(mage.abilities.Ability) FixedTarget(mage.target.targetpointer.FixedTarget) Abilities(mage.abilities.Abilities) Permanent(mage.game.permanent.Permanent) BoostTargetEffect(mage.abilities.effects.common.continuous.BoostTargetEffect) ContinuousEffect(mage.abilities.effects.ContinuousEffect)

Aggregations

Abilities (mage.abilities.Abilities)2 Ability (mage.abilities.Ability)2 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 UUID (java.util.UUID)1 MageInt (mage.MageInt)1 MageObject (mage.MageObject)1 MageObjectReference (mage.MageObjectReference)1 SpellAbility (mage.abilities.SpellAbility)1 BeginningOfCombatTriggeredAbility (mage.abilities.common.BeginningOfCombatTriggeredAbility)1 ContinuousEffect (mage.abilities.effects.ContinuousEffect)1 OneShotEffect (mage.abilities.effects.OneShotEffect)1 BoostTargetEffect (mage.abilities.effects.common.continuous.BoostTargetEffect)1 Hint (mage.abilities.hint.Hint)1 mage.abilities.keyword (mage.abilities.keyword)1 Card (mage.cards.Card)1 CardImpl (mage.cards.CardImpl)1 CardSetInfo (mage.cards.CardSetInfo)1 mage.constants (mage.constants)1