mirror of
https://github.com/magefree/mage.git
synced 2026-01-10 21:02:08 -08:00
move findTargetingStackObject from CardUtil to Game, so saved data can be cleared with short living lki add test cases
This commit is contained in:
parent
030e8ae5d3
commit
24f030fa71
13 changed files with 181 additions and 73 deletions
|
|
@ -12,7 +12,6 @@ import mage.game.events.GameEvent;
|
|||
import mage.game.permanent.Permanent;
|
||||
import mage.game.stack.StackObject;
|
||||
import mage.target.targetpointer.FixedTarget;
|
||||
import mage.util.CardUtil;
|
||||
|
||||
/**
|
||||
* @author xenohedron
|
||||
|
|
@ -70,7 +69,7 @@ public class BecomesTargetAnyTriggeredAbility extends TriggeredAbilityImpl {
|
|||
if (permanent == null || !filterTarget.match(permanent, getControllerId(), this, game)) {
|
||||
return false;
|
||||
}
|
||||
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
|
||||
StackObject targetingObject = game.findTargetingStackObject(this.getId().toString(), event);
|
||||
if (targetingObject == null || !filterStack.match(targetingObject, getControllerId(), this, game)) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,10 +8,9 @@ import mage.filter.FilterStackObject;
|
|||
import mage.filter.StaticFilters;
|
||||
import mage.game.Game;
|
||||
import mage.game.events.GameEvent;
|
||||
import mage.game.stack.StackObject;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.game.stack.StackObject;
|
||||
import mage.target.targetpointer.FixedTarget;
|
||||
import mage.util.CardUtil;
|
||||
|
||||
/**
|
||||
* @author LoneFox
|
||||
|
|
@ -54,7 +53,7 @@ public class BecomesTargetAttachedTriggeredAbility extends TriggeredAbilityImpl
|
|||
if (enchantment == null || enchantment.getAttachedTo() == null || !event.getTargetId().equals(enchantment.getAttachedTo())) {
|
||||
return false;
|
||||
}
|
||||
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
|
||||
StackObject targetingObject = game.findTargetingStackObject(this.getId().toString(), event);
|
||||
if (targetingObject == null || !filter.match(targetingObject, getControllerId(), this, game)) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,15 +3,14 @@ package mage.abilities.common;
|
|||
import mage.abilities.TriggeredAbilityImpl;
|
||||
import mage.abilities.effects.Effect;
|
||||
import mage.constants.SetTargetPointer;
|
||||
import mage.constants.Zone;
|
||||
import mage.filter.FilterPermanent;
|
||||
import mage.filter.FilterStackObject;
|
||||
import mage.game.events.GameEvent;
|
||||
import mage.constants.Zone;
|
||||
import mage.game.Game;
|
||||
import mage.game.events.GameEvent;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.game.stack.StackObject;
|
||||
import mage.target.targetpointer.FixedTarget;
|
||||
import mage.util.CardUtil;
|
||||
|
||||
/**
|
||||
* @author xenohedron
|
||||
|
|
@ -63,7 +62,7 @@ public class BecomesTargetControllerTriggeredAbility extends TriggeredAbilityImp
|
|||
return false;
|
||||
}
|
||||
}
|
||||
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
|
||||
StackObject targetingObject = game.findTargetingStackObject(this.getId().toString(), event);
|
||||
if (targetingObject == null || !filterStack.match(targetingObject, getControllerId(), this, game)) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import mage.game.Game;
|
|||
import mage.game.events.GameEvent;
|
||||
import mage.game.stack.StackObject;
|
||||
import mage.target.targetpointer.FixedTarget;
|
||||
import mage.util.CardUtil;
|
||||
|
||||
/**
|
||||
* @author North
|
||||
|
|
@ -57,7 +56,7 @@ public class BecomesTargetSourceTriggeredAbility extends TriggeredAbilityImpl {
|
|||
if (!event.getTargetId().equals(getSourceId())) {
|
||||
return false;
|
||||
}
|
||||
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
|
||||
StackObject targetingObject = game.findTargetingStackObject(this.getId().toString(), event);
|
||||
if (targetingObject == null || !filter.match(targetingObject, getControllerId(), this, game)) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ public class WardAbility extends TriggeredAbilityImpl {
|
|||
if (!getSourceId().equals(event.getTargetId())) {
|
||||
return false;
|
||||
}
|
||||
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
|
||||
StackObject targetingObject = game.findTargetingStackObject(this.getId().toString(), event);
|
||||
if (targetingObject == null || !game.getOpponents(getControllerId()).contains(targetingObject.getControllerId())) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import mage.game.permanent.Battlefield;
|
|||
import mage.game.permanent.Permanent;
|
||||
import mage.game.stack.Spell;
|
||||
import mage.game.stack.SpellStack;
|
||||
import mage.game.stack.StackObject;
|
||||
import mage.game.turn.Phase;
|
||||
import mage.game.turn.Step;
|
||||
import mage.game.turn.Turn;
|
||||
|
|
@ -310,6 +311,19 @@ public interface Game extends MageItem, Serializable, Copyable<Game> {
|
|||
|
||||
void resetShortLivingLKI();
|
||||
|
||||
/**
|
||||
* For finding the spell or ability on the stack for "becomes the target" triggers.
|
||||
* Also ensures that spells/abilities that target the same object twice only trigger each "becomes the target" ability once.
|
||||
* If this is the first attempt at triggering for a given ability targeting a given object,
|
||||
* this method temporarily records that in the game state for later checks by this same method,
|
||||
* to not return the same object again (until short living LKI is cleared)
|
||||
*
|
||||
* @param checkingReference must be unique for each usage (this.getId().toString() of the TriggeredAbility, or this.getKey() of the watcher)
|
||||
* @param event the GameEvent.EventType.TARGETED from checkTrigger() or watch()
|
||||
* @return the StackObject which targeted the source, or null if already used or not found
|
||||
*/
|
||||
StackObject findTargetingStackObject(String checkingReference, GameEvent event);
|
||||
|
||||
void setLosingPlayer(Player player);
|
||||
|
||||
Player getLosingPlayer();
|
||||
|
|
|
|||
|
|
@ -121,6 +121,8 @@ public abstract class GameImpl implements Game {
|
|||
protected Map<UUID, Map<Integer, MageObject>> lkiExtended = new HashMap<>();
|
||||
// Used to check if an object was moved by the current effect in resolution (so Wrath like effect can be handled correctly)
|
||||
protected Map<Zone, Set<UUID>> lkiShortLiving = new EnumMap<>(Zone.class);
|
||||
// For checking "becomes the target" triggers accurately. Cleared on short living LKI reset
|
||||
protected Map<String, Map<UUID, Set<UUID>>> targetedMap = new HashMap<>();
|
||||
|
||||
// Permanents entering the Battlefield while handling replacement effects before they are added to the battlefield
|
||||
protected Map<UUID, Permanent> permanentsEntering = new HashMap<>();
|
||||
|
|
@ -202,6 +204,7 @@ public abstract class GameImpl implements Game {
|
|||
this.lkiCardState = CardUtil.deepCopyObject(game.lkiCardState);
|
||||
this.lkiExtended = CardUtil.deepCopyObject(game.lkiExtended);
|
||||
this.lkiShortLiving = CardUtil.deepCopyObject(game.lkiShortLiving);
|
||||
this.targetedMap = CardUtil.deepCopyObject(game.targetedMap);
|
||||
|
||||
this.permanentsEntering = CardUtil.deepCopyObject(game.permanentsEntering);
|
||||
this.enterWithCounters = CardUtil.deepCopyObject(game.enterWithCounters);
|
||||
|
|
@ -3687,6 +3690,39 @@ public abstract class GameImpl implements Game {
|
|||
@Override
|
||||
public void resetShortLivingLKI() {
|
||||
lkiShortLiving.clear();
|
||||
targetedMap.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public StackObject findTargetingStackObject(String checkingReference, GameEvent event) {
|
||||
// In case of multiple simultaneous triggered abilities from the same source,
|
||||
// need to get the actual one that targeted, see #8026, #8378, rulings for Battle Mammoth
|
||||
// In case of copied triggered abilities, need to trigger on each independently, see #13498
|
||||
// Also avoids triggering on cancelled selections, see #8802
|
||||
Map<UUID, Set<UUID>> targetMap = targetedMap.getOrDefault(checkingReference, null);
|
||||
// targetMap: key - targetId; value - Set of stackObject Ids
|
||||
if (targetMap == null) {
|
||||
targetMap = new HashMap<>();
|
||||
} else {
|
||||
targetMap = new HashMap<>(targetMap); // must have new object reference if saved back
|
||||
}
|
||||
Set<UUID> targetingObjects = targetMap.computeIfAbsent(event.getTargetId(), k -> new HashSet<>());
|
||||
for (StackObject stackObject : getStack()) {
|
||||
Ability stackAbility = stackObject.getStackAbility();
|
||||
if (stackAbility == null || !stackAbility.getSourceId().equals(event.getSourceId())) {
|
||||
continue;
|
||||
}
|
||||
if (CardUtil.getAllSelectedTargets(stackAbility, this).contains(event.getTargetId())) {
|
||||
if (!targetingObjects.add(stackObject.getId())) {
|
||||
continue; // The trigger/watcher already recorded that target of the stack object, check for another
|
||||
}
|
||||
// Otherwise, store this combination of trigger/watcher + target + stack object
|
||||
targetMap.put(event.getTargetId(), targetingObjects);
|
||||
targetedMap.put(checkingReference, targetMap);
|
||||
return stackObject;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -44,7 +44,6 @@ import mage.game.permanent.PermanentMeld;
|
|||
import mage.game.permanent.PermanentToken;
|
||||
import mage.game.permanent.token.Token;
|
||||
import mage.game.stack.Spell;
|
||||
import mage.game.stack.StackObject;
|
||||
import mage.players.Player;
|
||||
import mage.players.PlayerList;
|
||||
import mage.target.Target;
|
||||
|
|
@ -1133,49 +1132,6 @@ public final class CardUtil {
|
|||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* For finding the spell or ability on the stack for "becomes the target" triggers.
|
||||
* Also ensures that spells/abilities that target the same object twice only trigger each "becomes the target" ability once.
|
||||
* If this is the first attempt at triggering for a given ability targeting a given object,
|
||||
* this method records that in the game state for later checks by this same method, to not return the same object again.
|
||||
*
|
||||
* @param checkingReference must be unique for each usage (this.getId().toString() of the TriggeredAbility, or this.getKey() of the watcher)
|
||||
* @param event the GameEvent.EventType.TARGETED from checkTrigger() or watch()
|
||||
* @param game the Game from checkTrigger() or watch()
|
||||
* @return the StackObject which targeted the source, or null if already used or not found
|
||||
*/
|
||||
public static StackObject findTargetingStackObject(String checkingReference, GameEvent event, Game game) {
|
||||
// In case of multiple simultaneous triggered abilities from the same source,
|
||||
// need to get the actual one that targeted, see #8026, #8378, rulings for Battle Mammoth
|
||||
// In case of copied triggered abilities, need to trigger on each independently, see #13498
|
||||
// Also avoids triggering on cancelled selections, see #8802
|
||||
String stateKey = "targetedMap" + checkingReference;
|
||||
Map<UUID, Set<UUID>> targetMap = (Map<UUID, Set<UUID>>) game.getState().getValue(stateKey);
|
||||
// targetMap: key - targetId; value - Set of stackObject Ids
|
||||
if (targetMap == null) {
|
||||
targetMap = new HashMap<>();
|
||||
} else {
|
||||
targetMap = new HashMap<>(targetMap); // must have new object reference if saved back to game state
|
||||
}
|
||||
Set<UUID> targetingObjects = targetMap.computeIfAbsent(event.getTargetId(), k -> new HashSet<>());
|
||||
for (StackObject stackObject : game.getStack()) {
|
||||
Ability stackAbility = stackObject.getStackAbility();
|
||||
if (stackAbility == null || !stackAbility.getSourceId().equals(event.getSourceId())) {
|
||||
continue;
|
||||
}
|
||||
if (CardUtil.getAllSelectedTargets(stackAbility, game).contains(event.getTargetId())) {
|
||||
if (!targetingObjects.add(stackObject.getId())) {
|
||||
continue; // The trigger/watcher already recorded that target of the stack object, check for another
|
||||
}
|
||||
// Otherwise, store this combination of trigger/watcher + target + stack object
|
||||
targetMap.put(event.getTargetId(), targetingObjects);
|
||||
game.getState().setValue(stateKey, targetMap);
|
||||
return stackObject;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* For overriding `canTarget()` with usages such as "any number of target cards with total mana value X or less".
|
||||
* Call this after super.canTarget() returns true.
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import mage.game.Game;
|
|||
import mage.game.events.GameEvent;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.game.stack.StackObject;
|
||||
import mage.util.CardUtil;
|
||||
import mage.watchers.Watcher;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
|
@ -29,7 +28,7 @@ public class NumberOfTimesPermanentTargetedATurnWatcher extends Watcher {
|
|||
if (event.getType() != GameEvent.EventType.TARGETED) {
|
||||
return;
|
||||
}
|
||||
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getKey(), event, game);
|
||||
StackObject targetingObject = game.findTargetingStackObject(this.getKey(), event);
|
||||
if (targetingObject == null) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue