Search in sources :

Example 41 with StackObject

use of mage.game.stack.StackObject in project mage by magefree.

the class RefractionTrapPreventDamageEffect method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    if (!this.used && super.applies(event, source, game)) {
        // check source
        MageObject object = game.getObject(event.getSourceId());
        if (object == null) {
            game.informPlayers("Couldn't find source of damage");
            return false;
        }
        // check damage source
        if (!object.getId().equals(target.getFirstTarget()) && !((object instanceof StackObject) && ((StackObject) object).getSourceId().equals(target.getFirstTarget()))) {
            return false;
        }
        // check target
        // check permanent first
        Permanent permanent = game.getPermanent(event.getTargetId());
        if (permanent != null) {
            if (permanent.isControlledBy(source.getControllerId())) {
                // it's your permanent
                return true;
            }
        }
        // check player
        Player player = game.getPlayer(event.getTargetId());
        if (player != null) {
            // it is you
            return player.getId().equals(source.getControllerId());
        }
    }
    return false;
}
Also used : Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) MageObject(mage.MageObject) StackObject(mage.game.stack.StackObject)

Example 42 with StackObject

use of mage.game.stack.StackObject in project mage by magefree.

the class SpellskiteEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget());
    MageObject sourceObject = game.getObject(source.getSourceId());
    if (stackObject != null && sourceObject != null) {
        Targets targets = new Targets();
        Ability sourceAbility;
        String oldTargetName = null;
        if (stackObject instanceof Spell) {
            Spell spell = (Spell) stackObject;
            sourceAbility = spell.getSpellAbility();
        } else if (stackObject instanceof StackAbility) {
            StackAbility stackAbility = (StackAbility) stackObject;
            sourceAbility = stackAbility;
        } else {
            return false;
        }
        for (UUID modeId : sourceAbility.getModes().getSelectedModes()) {
            Mode mode = sourceAbility.getModes().get(modeId);
            targets.addAll(mode.getTargets());
        }
        boolean twoTimesTarget = false;
        if (targets.size() == 1 && targets.get(0).getTargets().size() == 1) {
            Target target = targets.get(0);
            if (target.getFirstTarget().equals(source.getSourceId())) {
                // Target was already the same source, so no change / target event to create
                return true;
            }
            if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
                oldTargetName = getTargetName(targets.getFirstTarget(), game);
                target.clearChosen();
                // The source is still the spell on the stack
                target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
            }
        } else {
            // needed for targeted source's with multiple targets
            Player controller = game.getPlayer(source.getControllerId());
            boolean validTargets = false;
            do {
                for (Target target : targets) {
                    if (target.getTargets().contains(source.getSourceId())) {
                        // You can't change a traget to Spellskite, if Spellskite is already targeted by this target
                        continue;
                    }
                    for (UUID targetId : target.getTargets()) {
                        String name = getTargetName(targetId, game);
                        if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
                            validTargets = true;
                            if (name != null && controller.chooseUse(Outcome.Neutral, "Change target from " + name + " to " + sourceObject.getLogName() + '?', source, game)) {
                                oldTargetName = getTargetName(targetId, game);
                                int damageAmount = target.getTargetAmount(targetId);
                                target.remove(targetId);
                                // The source is still the spell on the stack
                                // add the source permanent id and set the damage if any
                                target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
                                target.setTargetAmount(source.getSourceId(), damageAmount, game);
                                break;
                            }
                        }
                    }
                    if (oldTargetName != null) {
                        break;
                    }
                }
                if (oldTargetName == null) {
                    game.informPlayer(controller, "You have to select at least one target to change to " + sourceObject.getIdName() + '!');
                }
            } while (validTargets && oldTargetName == null);
        }
        if (oldTargetName != null) {
            game.informPlayers(sourceObject.getLogName() + ": Changed target of " + stackObject.getLogName() + " from " + oldTargetName + " to " + sourceObject.getLogName());
        } else if (twoTimesTarget) {
            game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its not valid to target it twice for " + stackObject.getLogName());
        } else {
            game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its no valid target for " + stackObject.getLogName());
        }
        return true;
    }
    return false;
}
Also used : SimpleActivatedAbility(mage.abilities.common.SimpleActivatedAbility) StackAbility(mage.game.stack.StackAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Mode(mage.abilities.Mode) TargetStackObject(mage.target.TargetStackObject) StackObject(mage.game.stack.StackObject) MageObject(mage.MageObject) Targets(mage.target.Targets) Spell(mage.game.stack.Spell) StackAbility(mage.game.stack.StackAbility) Target(mage.target.Target) UUID(java.util.UUID)

Example 43 with StackObject

use of mage.game.stack.StackObject in project mage by magefree.

the class SpellSyphonEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    StackObject spell = game.getStack().getStackObject(targetPointer.getFirst(game, source));
    if (spell != null) {
        Player player = game.getPlayer(spell.getControllerId());
        Player controller = game.getPlayer(source.getControllerId());
        if (player != null && controller != null) {
            int amount = game.getBattlefield().count(filter, source.getSourceId(), controller.getId(), game);
            if (amount == 0) {
                game.informPlayers("Spell Syphon: no blue permanents in controller's battlefield.");
            } else {
                Cost cost = ManaUtil.createManaCost(amount, false);
                if (!cost.pay(source, game, source, spell.getControllerId(), false)) {
                    game.informPlayers("Spell Syphon: cost wasn't payed - countering target spell.");
                    return game.getStack().counter(source.getFirstTarget(), source, game);
                }
            }
        }
    }
    return false;
}
Also used : Player(mage.players.Player) StackObject(mage.game.stack.StackObject) Cost(mage.abilities.costs.Cost)

Example 44 with StackObject

use of mage.game.stack.StackObject in project mage by magefree.

the class ZabazTheGlimmerwaspEffect method applies.

@Override
public boolean applies(GameEvent event, Ability source, Game game) {
    if (!event.getData().equals(CounterType.P1P1.getName()) || event.getAmount() < 1) {
        return false;
    }
    StackObject stackAbility = game.getStack().getStackObject(event.getSourceId());
    if (stackAbility == null || !(stackAbility.getStackAbility() instanceof ModularAbility)) {
        return false;
    }
    Permanent permanent = game.getPermanent(event.getTargetId());
    if (permanent == null) {
        permanent = game.getPermanentEntering(event.getTargetId());
    }
    return permanent != null && permanent.isControlledBy(source.getControllerId()) && permanent.isCreature(game);
}
Also used : Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) StackObject(mage.game.stack.StackObject) ModularAbility(mage.abilities.keyword.ModularAbility)

Example 45 with StackObject

use of mage.game.stack.StackObject in project mage by magefree.

the class YidaroWanderingMonsterWatcher method watch.

@Override
public void watch(GameEvent event, Game game) {
    if (event.getType() != GameEvent.EventType.ACTIVATED_ABILITY) {
        return;
    }
    StackObject object = game.getStack().getStackObject(event.getSourceId());
    if (object == null || !(object.getStackAbility() instanceof CyclingAbility)) {
        return;
    }
    Card card = game.getCard(object.getSourceId());
    if (card != null && "Yidaro, Wandering Monster".equals(card.getName())) {
        countMap.merge(object.getControllerId(), 1, Integer::sum);
    }
}
Also used : CyclingAbility(mage.abilities.keyword.CyclingAbility) StackObject(mage.game.stack.StackObject) Card(mage.cards.Card)

Aggregations

StackObject (mage.game.stack.StackObject)130 Player (mage.players.Player)63 Permanent (mage.game.permanent.Permanent)57 Spell (mage.game.stack.Spell)41 MageObject (mage.MageObject)37 UUID (java.util.UUID)36 Card (mage.cards.Card)29 Target (mage.target.Target)17 TargetPermanent (mage.target.TargetPermanent)15 FilterCard (mage.filter.FilterCard)14 Ability (mage.abilities.Ability)13 FilterPermanent (mage.filter.FilterPermanent)13 StackAbility (mage.game.stack.StackAbility)13 FixedTarget (mage.target.targetpointer.FixedTarget)12 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)11 HashSet (java.util.HashSet)10 Effect (mage.abilities.effects.Effect)9 Game (mage.game.Game)9 MageObjectReference (mage.MageObjectReference)8 Cost (mage.abilities.costs.Cost)8