Page 1 of 1

Ability code not working as expected, any ideas?

PostPosted: 11 Nov 2016, 07:16
by MTGfan
I tested this against snow covered islands to see what would happen. Card ruling says it doesn't change the snow covered status. I found Kavu Recluse has the same ability except it makes the land a forest until end of turn. So I copied this code from that card changing the duration to while on the battlefield. I clicked on Gaea's Liege. I was then prompted to pick the ability. I did so and it asked me to pick the target. I clicked on one of the 10 snow covered islands that I put in play on the opponents side. I finished the turn and then on my next turn I attacked with Gaea's Liege to test the results. To discover that it became a 10/10 but should have turned into a 1/1.

I performed a second test. Having only one snow covered island in play. Then used the tap ability on that one. Then on the opponents next turn I put into play a second snow covered island. Then on my next turn attacked with the Gaea's Liege and it became a 2/2. It appears that the tap ability is correctly changing the target land. So I assume that the code for changing the power and toughness is not correctly counting only forests. Probably me misunderstanding the code and using the wrong thing. I copied then modified the code from Terra Ravager which counts all lands defender controls. Any idea what I need to change so that it only counts forests the defender controls?

Also card ruling states "When being declared as an attacker, use the “not attacking” power and toughness. It only changes after declaration is complete." In testing the sequence is as follows:

declare attackers (attacker prompt)
declare attackers play stuff (attacker prompt)
declare attackers play stuff (opponent prompt)
then power and toughness change
declare attackers play stuff (attacker prompt)
declare attackers play stuff (opponent prompt)
declare blockers (opponent prompt)

Is this the correct place for it to change?

Code: Select all
package mage.cards.g;

import java.util.UUID;
import mage.MageInt;
import mage.abilities.Ability;
import mage.abilities.common.AttacksTriggeredAbility;
import mage.abilities.common.SimpleActivatedAbility;
import mage.abilities.common.SimpleStaticAbility;
import mage.abilities.costs.common.TapSourceCost;
import mage.abilities.dynamicvalue.DynamicValue;
import mage.abilities.dynamicvalue.common.PermanentsOnBattlefieldCount;
import mage.abilities.effects.Effect;
import mage.abilities.effects.common.continuous.BecomesBasicLandTargetEffect;
import mage.abilities.effects.common.continuous.SetPowerToughnessSourceEffect;
import mage.cards.CardImpl;
import mage.cards.CardSetInfo;
import mage.constants.CardType;
import mage.constants.Duration;
import mage.constants.Zone;
import mage.filter.common.FilterControlledPermanent;
import mage.filter.common.FilterLandPermanent;
import mage.filter.predicate.mageobject.SubtypePredicate;
import mage.game.Game;
import mage.game.combat.CombatGroup;
import mage.game.permanent.Permanent;
import mage.target.common.TargetLandPermanent;

/**
 *
 * @author anonymous
 */
public class GaeasLiege extends CardImpl {
   
    final static FilterControlledPermanent filterLands = new FilterControlledPermanent("Forests you control");

    static {
        filterLands.add(new SubtypePredicate("Forest"));
    }

    public GaeasLiege(UUID ownerId, CardSetInfo setInfo) {
        super(ownerId, setInfo, new CardType[]{CardType.CREATURE}, "{3}{G}{G}{G}");
       
        this.subtype.add("Avatar");
        this.power = new MageInt(0);
        this.toughness = new MageInt(0);

        // As long as Gaea's Liege isn't attacking, its power and toughness are each equal to the number of Forests you control. As long as Gaea's Liege is attacking, its power and toughness are each equal to the number of Forests defending player controls.
        this.addAbility(new SimpleStaticAbility(Zone.ALL, new SetPowerToughnessSourceEffect(new PermanentsOnBattlefieldCount(filterLands), Duration.EndOfGame)));
        this.addAbility(new AttacksTriggeredAbility(new SetPowerToughnessSourceEffect(new DefendersForestCount(), Duration.EndOfCombat), false));
        // {tap}: Target land becomes a Forest until Gaea's Liege leaves the battlefield.
        Ability ability = new SimpleActivatedAbility(Zone.BATTLEFIELD, new BecomesBasicLandTargetEffect(Duration.WhileOnBattlefield, "Forest"), new TapSourceCost());
        ability.addTarget(new TargetLandPermanent());
        this.addAbility(ability);
    }

    public GaeasLiege(final GaeasLiege card) {
        super(card);
    }

    @Override
    public GaeasLiege copy() {
        return new GaeasLiege(this);
    }
}

class DefendersForestCount implements DynamicValue {

    @Override
    public int calculate(Game game, Ability sourceAbility, Effect effect) {
        for (CombatGroup group :game.getCombat().getGroups()) {
            if (group.getAttackers().contains(sourceAbility.getSourceId())) {
                UUID defenderId = group.getDefenderId();
                if (group.isDefenderIsPlaneswalker()) {
                    Permanent permanent = game.getPermanent(defenderId);
                    if (permanent != null) {
                        defenderId = permanent.getControllerId();
                    }
                }
                return game.getBattlefield().countAll(new FilterLandPermanent("Forest"), defenderId, game);

            }
        }
        return 0;
    }

    @Override
    public DynamicValue copy() {
        return new DefendersForestCount();
    }

    @Override
    public String toString() {
        return "X";
    }

    @Override
    public String getMessage() {
        return "the number of Forests defending player controls";
    }
}

Re: Ability code not working as expected, any ideas?

PostPosted: 11 Nov 2016, 20:17
by MTGfan
Ok problem solved with the following change.

Code: Select all
FilterLandPermanent filter = new FilterLandPermanent("forest");
filter.add(new SubtypePredicate("Forest"));
return game.getBattlefield().countAll(filter, defenderId, game);