Search in sources :

Example 1 with AttachedToPredicate

use of mage.filter.predicate.permanent.AttachedToPredicate in project mage by magefree.

the class ArmoryAutomatonEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player player = game.getPlayer(source.getControllerId());
    Permanent sourcePermanent = game.getPermanent(source.getSourceId());
    if (player != null && sourcePermanent != null) {
        // dynamic filter (can't selects own attaches and can't selects twice)
        FilterPermanent currentFilter = new FilterEquipmentPermanent();
        FilterPermanent filterSourceId = new FilterPermanent();
        filterSourceId.add(new CardIdPredicate(source.getSourceId()));
        currentFilter.add(Predicates.not(new AttachedToPredicate(filterSourceId)));
        int countBattlefield = game.getBattlefield().getAllActivePermanents(currentFilter, game).size();
        while (player.canRespond() && countBattlefield > 0 && player.chooseUse(Outcome.Benefit, "Select and attach a target Equipment?", source, game)) {
            Target targetEquipment = new TargetPermanent(currentFilter);
            targetEquipment.setRequired(false);
            if (player.choose(Outcome.Benefit, targetEquipment, source.getSourceId(), game) && targetEquipment.getFirstTarget() != null) {
                // exclude selected for next time
                currentFilter.add(Predicates.not(new PermanentIdPredicate(targetEquipment.getFirstTarget())));
                Permanent aura = game.getPermanent(targetEquipment.getFirstTarget());
                if (aura != null) {
                    Permanent attachedTo = game.getPermanent(aura.getAttachedTo());
                    if (attachedTo != null) {
                        attachedTo.removeAttachment(aura.getId(), source, game);
                    }
                    sourcePermanent.addAttachment(aura.getId(), source, game);
                }
            } else {
                break;
            }
            countBattlefield = game.getBattlefield().getAllActivePermanents(currentFilter, game).size();
        }
        return true;
    }
    return false;
}
Also used : PermanentIdPredicate(mage.filter.predicate.permanent.PermanentIdPredicate) Player(mage.players.Player) Target(mage.target.Target) FilterPermanent(mage.filter.FilterPermanent) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) FilterEquipmentPermanent(mage.filter.common.FilterEquipmentPermanent) FilterArtifactPermanent(mage.filter.common.FilterArtifactPermanent) TargetPermanent(mage.target.TargetPermanent) FilterEquipmentPermanent(mage.filter.common.FilterEquipmentPermanent) AttachedToPredicate(mage.filter.predicate.permanent.AttachedToPredicate) TargetPermanent(mage.target.TargetPermanent) CardIdPredicate(mage.filter.predicate.mageobject.CardIdPredicate)

Aggregations

FilterPermanent (mage.filter.FilterPermanent)1 FilterArtifactPermanent (mage.filter.common.FilterArtifactPermanent)1 FilterEquipmentPermanent (mage.filter.common.FilterEquipmentPermanent)1 CardIdPredicate (mage.filter.predicate.mageobject.CardIdPredicate)1 AttachedToPredicate (mage.filter.predicate.permanent.AttachedToPredicate)1 PermanentIdPredicate (mage.filter.predicate.permanent.PermanentIdPredicate)1 Permanent (mage.game.permanent.Permanent)1 Player (mage.players.Player)1 Target (mage.target.Target)1 TargetPermanent (mage.target.TargetPermanent)1