[DTK] updated implementation of spells which reveal dragon cards as a cost

This commit is contained in:
Evan Kranzler 2021-03-23 19:44:45 -04:00
parent 707de23436
commit d54e1c6eac
10 changed files with 377 additions and 466 deletions

View file

@ -0,0 +1,30 @@
package mage.abilities.condition.common;
import mage.abilities.Ability;
import mage.abilities.condition.Condition;
import mage.abilities.costs.common.RevealDragonFromHandCost;
import mage.constants.AbilityType;
import mage.game.Game;
import mage.watchers.common.DragonOnTheBattlefieldWhileSpellWasCastWatcher;
/**
* @author TheElk801
*/
public enum RevealedOrControlledDragonCondition implements Condition {
instance;
@Override
public boolean apply(Game game, Ability source) {
if (source.getAbilityType() == AbilityType.SPELL) {
return source
.getCosts()
.stream()
.filter(RevealDragonFromHandCost.class::isInstance)
.map(RevealDragonFromHandCost.class::cast)
.anyMatch(RevealDragonFromHandCost::isRevealedOrControlled);
}
DragonOnTheBattlefieldWhileSpellWasCastWatcher watcher
= game.getState().getWatcher(DragonOnTheBattlefieldWhileSpellWasCastWatcher.class);
return watcher != null && watcher.checkCondition(source, game);
}
}

View file

@ -0,0 +1,58 @@
package mage.abilities.costs.common;
import mage.abilities.Ability;
import mage.abilities.costs.Cost;
import mage.constants.SubType;
import mage.filter.FilterCard;
import mage.filter.FilterPermanent;
import mage.filter.common.FilterControlledPermanent;
import mage.game.Game;
import mage.target.common.TargetCardInHand;
import java.util.UUID;
/**
* @author TheElk801
*/
public class RevealDragonFromHandCost extends RevealTargetFromHandCost {
private static final FilterCard filter = new FilterCard("a Dragon card from your hand");
private static final FilterPermanent filter2 = new FilterControlledPermanent(SubType.DRAGON);
static {
filter.add(SubType.DRAGON.getPredicate());
}
private boolean revealedOrControlled = false;
public RevealDragonFromHandCost() {
super(new TargetCardInHand(0, 1, filter));
}
private RevealDragonFromHandCost(final RevealDragonFromHandCost cost) {
super(cost);
this.revealedOrControlled = cost.revealedOrControlled;
}
@Override
public boolean canPay(Ability ability, Ability source, UUID controllerId, Game game) {
return true;
}
@Override
public boolean pay(Ability ability, Game game, Ability source, UUID controllerId, boolean noMana, Cost costToPay) {
super.pay(ability, game, source, controllerId, noMana, costToPay);
revealedOrControlled = numberCardsRevealed > 0
|| game.getBattlefield().count(filter2, source.getSourceId(), controllerId, game) > 0;
return paid = true;
}
@Override
public RevealDragonFromHandCost copy() {
return new RevealDragonFromHandCost(this);
}
public boolean isRevealedOrControlled() {
return revealedOrControlled;
}
}

View file

@ -1,10 +1,9 @@
package mage.watchers.common;
import mage.abilities.costs.Cost;
import mage.abilities.costs.common.RevealTargetFromHandCost;
import mage.constants.SubType;
import mage.MageObjectReference;
import mage.abilities.Ability;
import mage.abilities.costs.common.RevealDragonFromHandCost;
import mage.constants.WatcherScope;
import mage.filter.FilterPermanent;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.stack.Spell;
@ -12,16 +11,13 @@ import mage.watchers.Watcher;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
/**
* @author LevelX2
*/
public class DragonOnTheBattlefieldWhileSpellWasCastWatcher extends Watcher {
private static final FilterPermanent filter = new FilterPermanent(SubType.DRAGON, "Dragons");
private final Set<UUID> castWithDragonOnTheBattlefield = new HashSet<>();
private final Set<MageObjectReference> castWithDragonOnTheBattlefield = new HashSet<>();
public DragonOnTheBattlefieldWhileSpellWasCastWatcher() {
super(WatcherScope.GAME);
@ -29,28 +25,22 @@ public class DragonOnTheBattlefieldWhileSpellWasCastWatcher extends Watcher {
@Override
public void watch(GameEvent event, Game game) {
if (event.getType() == GameEvent.EventType.SPELL_CAST) {
// targetId is the unique ID of the spell
Spell spell = game.getState().getStack().getSpell(event.getTargetId());
// revealed a Dragon card or controlled a Dragon as you cast the spell
if (spell != null) {
boolean revealedOrOnBattlefield = false;
if (spell.getSpellAbility() != null) {
for (Cost cost : spell.getSpellAbility().getCosts()) {
if (cost instanceof RevealTargetFromHandCost) {
revealedOrOnBattlefield = ((RevealTargetFromHandCost) cost).getNumberRevealedCards() > 0;
break;
}
}
}
if (!revealedOrOnBattlefield) {
revealedOrOnBattlefield = game.getBattlefield().countAll(filter, spell.getControllerId(), game) > 0;
}
if (revealedOrOnBattlefield) {
castWithDragonOnTheBattlefield.add(spell.getId());
}
}
if (event.getType() != GameEvent.EventType.SPELL_CAST) {
return;
}
// targetId is the unique ID of the spell
Spell spell = game.getSpell(event.getTargetId());
// revealed a Dragon card or controlled a Dragon as you cast the spell
if (spell != null
&& spell
.getSpellAbility()
.getCosts()
.stream()
.filter(RevealDragonFromHandCost.class::isInstance)
.map(RevealDragonFromHandCost.class::cast)
.anyMatch(RevealDragonFromHandCost::isRevealedOrControlled)) {
castWithDragonOnTheBattlefield.add(new MageObjectReference(spell.getCard(), game, 0));
castWithDragonOnTheBattlefield.add(new MageObjectReference(spell.getCard(), game, 1));
}
}
@ -60,7 +50,9 @@ public class DragonOnTheBattlefieldWhileSpellWasCastWatcher extends Watcher {
castWithDragonOnTheBattlefield.clear();
}
public boolean castWithConditionTrue(UUID spellId) {
return castWithDragonOnTheBattlefield.contains(spellId);
public boolean checkCondition(Ability source, Game game) {
return castWithDragonOnTheBattlefield
.stream()
.anyMatch(mor -> mor.refersTo(source.getSourceObject(game), game));
}
}