Refactor and add hint for "Ability resolved X times"

Affects Ashling the Pilgrim, Inner-Flame Igniter and Soulbright Flamekin.
This commit is contained in:
emerald000 2020-05-03 09:42:16 -04:00
parent a7de11345d
commit 111114e338
9 changed files with 156 additions and 76 deletions

View file

@ -0,0 +1,39 @@
package mage.abilities.dynamicvalue.common;
import mage.abilities.Ability;
import mage.abilities.dynamicvalue.DynamicValue;
import mage.abilities.effects.Effect;
import mage.game.Game;
import mage.watchers.common.AbilityResolvedWatcher;
/**
* @author emerald000
*/
public enum AbilityResolutionCount implements DynamicValue {
instance;
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
AbilityResolvedWatcher watcher = game.getState().getWatcher(AbilityResolvedWatcher.class);
if (watcher != null) {
return watcher.getResolutionCount(game, sourceAbility);
}
return 0;
}
@Override
public AbilityResolutionCount copy() {
return instance;
}
@Override
public String toString() {
return "X";
}
@Override
public String getMessage() {
return "permanents you control";
}
}

View file

@ -0,0 +1,52 @@
package mage.abilities.effects.common;
import mage.abilities.Ability;
import mage.abilities.effects.ContinuousEffect;
import mage.abilities.effects.Effect;
import mage.abilities.effects.OneShotEffect;
import mage.constants.Outcome;
import mage.game.Game;
import mage.util.CardUtil;
import mage.watchers.common.AbilityResolvedWatcher;
/**
* @author emerald000
*/
public class IfAbilityHasResolvedXTimesEffect extends OneShotEffect {
private final int resolutionNumber;
private final Effect effect;
public IfAbilityHasResolvedXTimesEffect(Outcome outcome, int resolutionNumber, Effect effect) {
super(outcome);
this.resolutionNumber = resolutionNumber;
this.effect = effect;
this.staticText = "If this is the " + CardUtil.numberToOrdinalText(resolutionNumber) + " time this ability has resolved this turn, " +
effect.getText(null);
}
private IfAbilityHasResolvedXTimesEffect(final IfAbilityHasResolvedXTimesEffect effect) {
super(effect);
this.resolutionNumber = effect.resolutionNumber;
this.effect = effect.effect;
}
@Override
public IfAbilityHasResolvedXTimesEffect copy() {
return new IfAbilityHasResolvedXTimesEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
AbilityResolvedWatcher watcher = game.getState().getWatcher(AbilityResolvedWatcher.class);
if (watcher != null && watcher.getResolutionCount(game, source) == resolutionNumber) {
if (effect instanceof OneShotEffect) {
return effect.apply(game, source);
} else {
game.addEffect((ContinuousEffect) effect, source);
return true;
}
}
return true;
}
}

View file

@ -0,0 +1,26 @@
package mage.abilities.hint.common;
import mage.abilities.Ability;
import mage.abilities.dynamicvalue.common.AbilityResolutionCount;
import mage.abilities.hint.Hint;
import mage.abilities.hint.ValueHint;
import mage.game.Game;
/**
* @author emerald000
*/
public enum AbilityResolutionCountHint implements Hint {
instance;
private static final Hint hint = new ValueHint("Resolution count:", AbilityResolutionCount.instance);
@Override
public String getText(Game game, Ability ability) {
return hint.getText(game, ability);
}
@Override
public Hint copy() {
return instance;
}
}

View file

@ -150,7 +150,8 @@ public class GameEvent implements Serializable {
SPELL_CAST,
ACTIVATE_ABILITY, ACTIVATED_ABILITY,
TRIGGERED_ABILITY,
COPY_STACKOBJECT,COPIED_STACKOBJECT,
RESOLVED_ABILITY,
COPY_STACKOBJECT, COPIED_STACKOBJECT,
/* ADD_MANA
targetId id of the ability that added the mana
sourceId sourceId of the ability that added the mana

View file

@ -82,6 +82,7 @@ public class StackAbility extends StackObjImpl implements Ability {
@Override
public boolean resolve(Game game) {
if (ability.getTargets().stillLegal(ability, game) || !canFizzle()) {
game.fireEvent(new GameEvent(GameEvent.EventType.RESOLVED_ABILITY, ability.getOriginalId(), ability.getSourceId(), ability.getControllerId()));
boolean result = ability.resolve(game);
game.getStack().remove(this, game);
return result;

View file

@ -1,6 +1,5 @@
package mage.watchers.common;
import mage.MageObjectReference;
import mage.abilities.Ability;
import mage.constants.WatcherScope;
import mage.game.Game;
@ -9,14 +8,13 @@ import mage.watchers.Watcher;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* @author TheElk801
*/
public class AbilityResolvedWatcher extends Watcher {
private final Map<MageObjectReference, Map<UUID, Integer>> activationMap = new HashMap<>();
private final Map<String, Integer> resolutionMap = new HashMap<>();
public AbilityResolvedWatcher() {
super(WatcherScope.GAME);
@ -24,17 +22,18 @@ public class AbilityResolvedWatcher extends Watcher {
@Override
public void watch(GameEvent event, Game game) {
if (event.getType() == GameEvent.EventType.RESOLVED_ABILITY) {
resolutionMap.merge(event.getTargetId().toString() + game.getState().getZoneChangeCounter(event.getSourceId()), 1, Integer::sum);
}
}
@Override
public void reset() {
super.reset();
activationMap.clear();
resolutionMap.clear();
}
public boolean checkActivations(Ability source, Game game) {
return activationMap.computeIfAbsent(new MageObjectReference(
source.getSourceId(), source.getSourceObjectZoneChangeCounter(), game
), x -> new HashMap<>()).compute(source.getOriginalId(), (u, i) -> i == null ? 1 : i + 1).intValue() == 3;
public int getResolutionCount(Game game, Ability source) {
return resolutionMap.getOrDefault(source.getOriginalId().toString() + game.getState().getZoneChangeCounter(source.getSourceId()), 0);
}
}