implement [FIC] Edgar, Master Machinist (#13676)

This commit is contained in:
Susucre 2025-05-30 15:11:43 +02:00 committed by GitHub
parent 0ac37617d1
commit ba395c8385
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 580 additions and 244 deletions

View file

@ -19,7 +19,8 @@ public enum MageIdentifier {
// e.g. [[Johann, Apprentice Sorcerer]]
// "Once each turn, you may cast an instant or sorcery spell from the top of your library."
//
CastFromGraveyardOnceWatcher,
OnceOnYourTurnCastFromGraveyard,
OnceOnYourTurnCastFromGraveyardEntersTapped,
OnceEachTurnCastWatcher,
HaukensInsightWatcher,
IntrepidPaleontologistWatcher,

View file

@ -0,0 +1,190 @@
package mage.abilities.common;
import mage.MageIdentifier;
import mage.MageObjectReference;
import mage.abilities.Ability;
import mage.abilities.SpellAbility;
import mage.abilities.costs.Cost;
import mage.abilities.costs.Costs;
import mage.abilities.costs.CostsImpl;
import mage.abilities.effects.AsThoughEffectImpl;
import mage.abilities.effects.common.replacement.MorEnteringTappedEffect;
import mage.cards.Card;
import mage.constants.*;
import mage.filter.FilterCard;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.game.stack.Spell;
import mage.players.Player;
import mage.watchers.Watcher;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
/**
* Once during each of your turns, you may cast... from your graveyard
* <p>
* See Lurrus of the Dream Den and Rivaz of the Claw
*
* @author weirddan455, Susucr
*/
public class CastFromGraveyardOnceDuringEachOfYourTurnAbility extends SimpleStaticAbility {
public CastFromGraveyardOnceDuringEachOfYourTurnAbility(FilterCard filter) {
this(filter, (Cost) null);
}
public CastFromGraveyardOnceDuringEachOfYourTurnAbility(FilterCard filter, Cost additionalCost) {
this(filter, additionalCost, MageIdentifier.OnceOnYourTurnCastFromGraveyard);
}
public CastFromGraveyardOnceDuringEachOfYourTurnAbility(FilterCard filter, MageIdentifier mageIdentifier) {
this(filter, null, mageIdentifier);
}
public CastFromGraveyardOnceDuringEachOfYourTurnAbility(FilterCard filter, Cost additionalCost, MageIdentifier mageIdentifier) {
super(new CastFromGraveyardOnceEffect(filter, additionalCost, mageIdentifier));
this.addWatcher(new CastFromGraveyardOnceWatcher());
switch (mageIdentifier) {
case OnceOnYourTurnCastFromGraveyard:
case OnceOnYourTurnCastFromGraveyardEntersTapped:
this.setIdentifier(mageIdentifier);
break;
default:
throw new IllegalArgumentException("Wrong code usage: only specific MageIdentifier are currently supported");
}
}
private CastFromGraveyardOnceDuringEachOfYourTurnAbility(final CastFromGraveyardOnceDuringEachOfYourTurnAbility ability) {
super(ability);
}
@Override
public CastFromGraveyardOnceDuringEachOfYourTurnAbility copy() {
return new CastFromGraveyardOnceDuringEachOfYourTurnAbility(this);
}
}
class CastFromGraveyardOnceEffect extends AsThoughEffectImpl {
private final FilterCard filter;
private final Cost additionalCost;
private final MageIdentifier mageIdentifier;
CastFromGraveyardOnceEffect(FilterCard filter, Cost additionalCost, MageIdentifier mageIdentifier) {
super(AsThoughEffectType.CAST_FROM_NOT_OWN_HAND_ZONE, Duration.WhileOnBattlefield, Outcome.Benefit);
this.filter = filter;
this.staticText = "Once during each of your turns, you may cast " + filter.getMessage()
+ (filter.getMessage().contains("your graveyard") ? "" : " from your graveyard")
+ (additionalCost == null ? "" : " by " + additionalCost.getText() + " in addition to paying its other costs.");
this.additionalCost = additionalCost;
this.mageIdentifier = mageIdentifier;
}
private CastFromGraveyardOnceEffect(final CastFromGraveyardOnceEffect effect) {
super(effect);
this.filter = effect.filter;
this.additionalCost = effect.additionalCost;
this.mageIdentifier = effect.mageIdentifier;
}
@Override
public CastFromGraveyardOnceEffect copy() {
return new CastFromGraveyardOnceEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
return true;
}
@Override
public boolean applies(UUID objectId, Ability source, UUID affectedControllerId, Game game) {
throw new IllegalArgumentException("Wrong code usage: can't call applies method on empty affectedAbility");
}
@Override
public boolean applies(UUID objectId, Ability affectedAbility, Ability source, Game game, UUID playerId) {
Player controller = game.getPlayer(source.getControllerId());
Permanent sourcePermanent = source.getSourcePermanentIfItStillExists(game);
CastFromGraveyardOnceWatcher watcher = game.getState().getWatcher(CastFromGraveyardOnceWatcher.class);
Card cardToCast = game.getCard(objectId);
if (controller == null || sourcePermanent == null || watcher == null || cardToCast == null
|| !game.isActivePlayer(playerId) // only during your turn
|| !source.isControlledBy(playerId) // only you may cast
|| !Zone.GRAVEYARD.equals(game.getState().getZone(objectId)) // from graveyard
|| !cardToCast.getOwnerId().equals(playerId) // only your graveyard
|| !(affectedAbility instanceof SpellAbility) // characteristics to check
|| watcher.abilityUsed(new MageObjectReference(sourcePermanent, game)) // once per turn
) {
return false;
}
SpellAbility spellAbility = (SpellAbility) affectedAbility;
Card cardToCheck = spellAbility.getCharacteristics(game);
if (spellAbility.getManaCosts().isEmpty()) {
return false;
}
Set<MageIdentifier> allowedToBeCastNow = spellAbility.spellCanBeActivatedNow(playerId, game);
if (!allowedToBeCastNow.contains(MageIdentifier.Default)
|| !filter.match(cardToCheck, playerId, source, game)) {
return false;
}
if (additionalCost != null) {
Costs<Cost> costs = new CostsImpl<>();
costs.add(additionalCost);
controller.setCastSourceIdWithAlternateMana(
objectId, spellAbility.getManaCosts(),
costs, mageIdentifier);
}
return true;
}
}
class CastFromGraveyardOnceWatcher extends Watcher {
// TODO: we might want to store (approver, approving ability) instead on the odd chance there
// is more than one such ability on a given approver. (event.getApprovingObject() has
// the exact ability, but not sure its id is stable enough.)
// Set of each approver that approved casting a spell this turn (and is thus done for the turn)
private final Set<MageObjectReference> usedFrom = new HashSet<>();
CastFromGraveyardOnceWatcher() {
super(WatcherScope.GAME);
}
@Override
public void watch(GameEvent event, Game game) {
if (!GameEvent.EventType.SPELL_CAST.equals(event.getType())) {
return;
}
if (event.hasApprovingIdentifier(MageIdentifier.OnceOnYourTurnCastFromGraveyard)) {
usedFrom.add(event.getApprovingObject().getApprovingMageObjectReference());
return;
}
if (event.hasApprovingIdentifier(MageIdentifier.OnceOnYourTurnCastFromGraveyardEntersTapped)) {
usedFrom.add(event.getApprovingObject().getApprovingMageObjectReference());
// The cast (most likely permanent) spell enters the battlefield tapped.
Spell target = game.getSpell(event.getTargetId());
if (target != null) {
MageObjectReference mor = new MageObjectReference(target, game);
game.getState().addEffect(
new MorEnteringTappedEffect(mor),
event.getApprovingObject().getApprovingAbility() // ability that approved the cast is the source of the tapping.
);
}
return;
}
}
@Override
public void reset() {
super.reset();
usedFrom.clear();
}
boolean abilityUsed(MageObjectReference mor) {
return usedFrom.contains(mor);
}
}

View file

@ -1,150 +0,0 @@
package mage.abilities.common;
import mage.MageIdentifier;
import mage.MageObjectReference;
import mage.abilities.Ability;
import mage.abilities.SpellAbility;
import mage.abilities.costs.Cost;
import mage.abilities.costs.Costs;
import mage.abilities.costs.CostsImpl;
import mage.abilities.effects.AsThoughEffectImpl;
import mage.cards.Card;
import mage.constants.*;
import mage.filter.FilterCard;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.players.Player;
import mage.watchers.Watcher;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
/**
* Once during each of your turns, you may cast... from your graveyard
* <p>
* See Lurrus of the Dream Den and Rivaz of the Claw
*
* @author weirddan455
*/
public class CastFromGraveyardOnceEachTurnAbility extends SimpleStaticAbility {
public CastFromGraveyardOnceEachTurnAbility(FilterCard filter) {
this(filter, null);
}
public CastFromGraveyardOnceEachTurnAbility(FilterCard filter, Cost additionalCost) {
super(new CastFromGraveyardOnceEffect(filter, additionalCost));
this.addWatcher(new CastFromGraveyardOnceWatcher());
this.setIdentifier(MageIdentifier.CastFromGraveyardOnceWatcher);
}
private CastFromGraveyardOnceEachTurnAbility(final CastFromGraveyardOnceEachTurnAbility ability) {
super(ability);
}
@Override
public CastFromGraveyardOnceEachTurnAbility copy() {
return new CastFromGraveyardOnceEachTurnAbility(this);
}
}
class CastFromGraveyardOnceEffect extends AsThoughEffectImpl {
private final FilterCard filter;
private final Cost additionalCost;
CastFromGraveyardOnceEffect(FilterCard filter, Cost additionalCost) {
super(AsThoughEffectType.CAST_FROM_NOT_OWN_HAND_ZONE, Duration.WhileOnBattlefield, Outcome.Benefit);
this.filter = filter;
this.staticText = "Once during each of your turns, you may cast " + filter.getMessage()
+ (filter.getMessage().contains("your graveyard") ? "" : " from your graveyard")
+ (additionalCost == null ? "" : " by " + additionalCost.getText() + " in addition to paying its other costs.");
this.additionalCost = additionalCost;
}
private CastFromGraveyardOnceEffect(final CastFromGraveyardOnceEffect effect) {
super(effect);
this.filter = effect.filter;
this.additionalCost = effect.additionalCost;
}
@Override
public CastFromGraveyardOnceEffect copy() {
return new CastFromGraveyardOnceEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
return true;
}
@Override
public boolean applies(UUID objectId, Ability source, UUID affectedControllerId, Game game) {
throw new IllegalArgumentException("Wrong code usage: can't call applies method on empty affectedAbility");
}
@Override
public boolean applies(UUID objectId, Ability affectedAbility, Ability source, Game game, UUID playerId) {
Player controller = game.getPlayer(source.getControllerId());
Permanent sourcePermanent = source.getSourcePermanentIfItStillExists(game);
CastFromGraveyardOnceWatcher watcher = game.getState().getWatcher(CastFromGraveyardOnceWatcher.class);
Card cardToCast = game.getCard(objectId);
if (controller == null || sourcePermanent == null || watcher == null || cardToCast == null) {
return false;
}
if (game.isActivePlayer(playerId) // only during your turn
&& source.isControlledBy(playerId) // only you may cast
&& Zone.GRAVEYARD.equals(game.getState().getZone(objectId)) // from graveyard
&& cardToCast.getOwnerId().equals(playerId) // only your graveyard
&& affectedAbility instanceof SpellAbility // characteristics to check
&& watcher.abilityNotUsed(new MageObjectReference(sourcePermanent, game)) // once per turn
) {
SpellAbility spellAbility = (SpellAbility) affectedAbility;
Card cardToCheck = spellAbility.getCharacteristics(game);
if (spellAbility.getManaCosts().isEmpty()) {
return false;
}
Set<MageIdentifier> allowedToBeCastNow = spellAbility.spellCanBeActivatedNow(playerId, game);
if (allowedToBeCastNow.contains(MageIdentifier.Default)) {
boolean matched = filter.match(cardToCheck, playerId, source, game);
if (matched && additionalCost != null) {
Costs<Cost> costs = new CostsImpl<>();
costs.add(additionalCost);
controller.setCastSourceIdWithAlternateMana(objectId, spellAbility.getManaCosts(),
costs, MageIdentifier.CastFromGraveyardOnceWatcher);
}
return matched;
}
}
return false;
}
}
class CastFromGraveyardOnceWatcher extends Watcher {
private final Set<MageObjectReference> usedFrom = new HashSet<>();
CastFromGraveyardOnceWatcher() {
super(WatcherScope.GAME);
}
@Override
public void watch(GameEvent event, Game game) {
if (GameEvent.EventType.SPELL_CAST.equals(event.getType())
&& event.hasApprovingIdentifier(MageIdentifier.CastFromGraveyardOnceWatcher)) {
usedFrom.add(event.getApprovingObject().getApprovingMageObjectReference());
}
}
@Override
public void reset() {
super.reset();
usedFrom.clear();
}
boolean abilityNotUsed(MageObjectReference mor) {
return !usedFrom.contains(mor);
}
}

View file

@ -0,0 +1,69 @@
package mage.abilities.effects.common.replacement;
import mage.MageObjectReference;
import mage.abilities.Ability;
import mage.abilities.effects.ReplacementEffectImpl;
import mage.constants.Duration;
import mage.constants.Outcome;
import mage.game.Game;
import mage.game.events.EntersTheBattlefieldEvent;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.game.stack.Spell;
/**
* Used to affect a spell on the stack.
* The permanent it may become enters tapped.
* <p>
* Short-lived replacement effect, auto-cleanup if the mor is no longer a spell.
*
* @author Susucr
*/
public class MorEnteringTappedEffect extends ReplacementEffectImpl {
private final MageObjectReference mor;
public MorEnteringTappedEffect(MageObjectReference mor) {
super(Duration.OneUse, Outcome.Tap);
this.staticText = "That permanent enters the battlefield tapped";
this.mor = mor;
}
private MorEnteringTappedEffect(final MorEnteringTappedEffect effect) {
super(effect);
this.mor = effect.mor;
}
@Override
public MorEnteringTappedEffect copy() {
return new MorEnteringTappedEffect(this);
}
@Override
public boolean checksEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.ENTERS_THE_BATTLEFIELD;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
Spell spell = game.getSpell(event.getSourceId());
Spell morSpell = mor.getSpell(game);
if (morSpell == null) {
// cleanup if something other than resolving happens to the spell.
discard();
return false;
}
return spell != null
&& morSpell.getSourceId() == spell.getSourceId()
&& morSpell.getZoneChangeCounter(game) == spell.getZoneChangeCounter(game);
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
Permanent permanent = ((EntersTheBattlefieldEvent) event).getTarget();
if (permanent != null) {
permanent.setTapped(true);
}
return false;
}
}

View file

@ -97,6 +97,13 @@ public final class StaticFilters {
FILTER_CARD_CREATURE_A.setLockedFilter(true);
}
// for checks on cards to be cast as "a creature spell", this is a FilterCard, but the text is about spell
public static final FilterCreatureCard FILTER_CARD_A_CREATURE_SPELL = new FilterCreatureCard("a creature spell");
static {
FILTER_CARD_A_CREATURE_SPELL.setLockedFilter(true);
}
public static final FilterCreatureCard FILTER_CARD_CREATURE_YOUR_HAND = new FilterCreatureCard("a creature card from your hand");
static {