refactor: find targeting stack object (#13534)

* refactor: simplify finding targeting stack object
related to #11185, 8e1805c

* clarify docs

---------

Co-authored-by: xenohedron <12538125+xenohedron@users.noreply.github.com>
This commit is contained in:
xenohedron 2025-04-14 11:19:57 -04:00 committed by GitHub
parent 0f0026c375
commit 74d2265c12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 27 additions and 73 deletions

View file

@ -90,13 +90,10 @@ class AgrusKosEternalSoldierTriggeredAbility extends TriggeredAbilityImpl {
if (!event.getTargetId().equals(getSourceId())) {
return false;
}
StackObject targetingObject = CardUtil.getTargetingStackObject(this.getId().toString(), event, game);
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
if (targetingObject == null || targetingObject instanceof Spell) {
return false;
}
if (CardUtil.checkTargetedEventAlreadyUsed(this.getId().toString(), targetingObject, event, game)) {
return false;
}
Set<UUID> targets = targetingObject
.getStackAbility()
.getTargets()

View file

@ -96,13 +96,10 @@ class PawpatchRecruitTriggeredAbility extends TriggeredAbilityImpl {
if (permanent == null || !filterTarget.match(permanent, getControllerId(), this, game)) {
return false;
}
StackObject targetingObject = CardUtil.getTargetingStackObject(this.getId().toString(), event, game);
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
if (targetingObject == null || !filterStack.match(targetingObject, getControllerId(), this, game)) {
return false;
}
if (CardUtil.checkTargetedEventAlreadyUsed(this.getId().toString(), targetingObject, event, game)) {
return false;
}
this.getTargets().clear();
FilterControlledPermanent filter = new FilterControlledCreaturePermanent();
filter.add(Predicates.not(new MageObjectReferencePredicate(event.getTargetId(), game)));

View file

@ -90,9 +90,7 @@ class SurrakElusiveHunterTriggeredAbility extends TriggeredAbilityImpl {
if (!checkTargeted(event.getTargetId(), game)) {
return false;
}
StackObject targetingObject = CardUtil.getTargetingStackObject(this.getId().toString(), event, game);
return targetingObject != null
&& game.getOpponents(getControllerId()).contains(targetingObject.getControllerId())
&& !CardUtil.checkTargetedEventAlreadyUsed(this.getId().toString(), targetingObject, event, game);
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
return targetingObject != null && game.getOpponents(getControllerId()).contains(targetingObject.getControllerId());
}
}

View file

@ -70,13 +70,10 @@ public class BecomesTargetAnyTriggeredAbility extends TriggeredAbilityImpl {
if (permanent == null || !filterTarget.match(permanent, getControllerId(), this, game)) {
return false;
}
StackObject targetingObject = CardUtil.getTargetingStackObject(this.getId().toString(), event, game);
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
if (targetingObject == null || !filterStack.match(targetingObject, getControllerId(), this, game)) {
return false;
}
if (CardUtil.checkTargetedEventAlreadyUsed(this.getId().toString(), targetingObject, event, game)) {
return false;
}
switch (setTargetPointer) {
case PERMANENT:
this.getAllEffects().setTargetPointer(new FixedTarget(permanent.getId(), game));

View file

@ -54,13 +54,10 @@ public class BecomesTargetAttachedTriggeredAbility extends TriggeredAbilityImpl
if (enchantment == null || enchantment.getAttachedTo() == null || !event.getTargetId().equals(enchantment.getAttachedTo())) {
return false;
}
StackObject targetingObject = CardUtil.getTargetingStackObject(this.getId().toString(), event, game);
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
if (targetingObject == null || !filter.match(targetingObject, getControllerId(), this, game)) {
return false;
}
if (CardUtil.checkTargetedEventAlreadyUsed(this.getId().toString(), targetingObject, event, game)) {
return false;
}
switch (setTargetPointer) {
case PLAYER:
this.getAllEffects().setTargetPointer(new FixedTarget(targetingObject.getControllerId(), game));

View file

@ -63,13 +63,10 @@ public class BecomesTargetControllerTriggeredAbility extends TriggeredAbilityImp
return false;
}
}
StackObject targetingObject = CardUtil.getTargetingStackObject(this.getId().toString(), event, game);
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
if (targetingObject == null || !filterStack.match(targetingObject, getControllerId(), this, game)) {
return false;
}
if (CardUtil.checkTargetedEventAlreadyUsed(this.getId().toString(), targetingObject, event, game)) {
return false;
}
switch (setTargetPointer) {
case SPELL:
this.getAllEffects().setTargetPointer(new FixedTarget(targetingObject.getId()));

View file

@ -57,13 +57,10 @@ public class BecomesTargetSourceTriggeredAbility extends TriggeredAbilityImpl {
if (!event.getTargetId().equals(getSourceId())) {
return false;
}
StackObject targetingObject = CardUtil.getTargetingStackObject(this.getId().toString(), event, game);
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
if (targetingObject == null || !filter.match(targetingObject, getControllerId(), this, game)) {
return false;
}
if (CardUtil.checkTargetedEventAlreadyUsed(this.getId().toString(), targetingObject, event, game)) {
return false;
}
switch (setTargetPointer) {
case PLAYER:
this.getAllEffects().setTargetPointer(new FixedTarget(targetingObject.getControllerId(), game));

View file

@ -77,13 +77,10 @@ public class WardAbility extends TriggeredAbilityImpl {
if (!getSourceId().equals(event.getTargetId())) {
return false;
}
StackObject targetingObject = CardUtil.getTargetingStackObject(this.getId().toString(), event, game);
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getId().toString(), event, game);
if (targetingObject == null || !game.getOpponents(getControllerId()).contains(targetingObject.getControllerId())) {
return false;
}
if (CardUtil.checkTargetedEventAlreadyUsed(this.getId().toString(), targetingObject, event, game)) {
return false;
}
getEffects().setTargetPointer(new FixedTarget(targetingObject.getId()));
return true;
}

View file

@ -1128,14 +1128,19 @@ public final class CardUtil {
/**
* 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 not found
* @return the StackObject which targeted the source, or null if already used or not found
*/
public static StackObject getTargetingStackObject(String checkingReference, GameEvent event, Game game) {
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
// 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);
@ -1148,50 +1153,22 @@ public final class CardUtil {
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()) || targetingObjects.contains(stackObject.getId())) {
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 ensuring 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.
*
* @param checkingReference must be unique for each usage (this.id.toString() of the TriggeredAbility, or this.getKey() of the watcher)
* @param targetingObject from getTargetingStackObject
* @param event the GameEvent.EventType.TARGETED from checkTrigger() or watch()
* @param game the Game from checkTrigger() or watch()
* @return true if already triggered/watched, false if this is the first/only trigger/watch
*/
public static boolean checkTargetedEventAlreadyUsed(String checkingReference, StackObject targetingObject, GameEvent event, Game game) {
String stateKey = "targetedMap" + checkingReference;
// If a spell or ability an opponent controls targets a single permanent you control more than once,
// Battle Mammoth's triggered ability will trigger only once.
// However, if a spell or ability an opponent controls targets multiple permanents you control,
// Battle Mammoth's triggered ability will trigger once for each of those permanents. (2021-02-05)
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<>());
if (!targetingObjects.add(targetingObject.getId())) {
return true; // The trigger/watcher already recorded that target of the stack object
}
// Otherwise, store this combination of trigger/watcher + target + stack object
targetMap.put(event.getTargetId(), targetingObjects);
game.getState().setValue(stateKey, targetMap);
return false;
}
/**
* 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.

View file

@ -29,8 +29,8 @@ public class NumberOfTimesPermanentTargetedATurnWatcher extends Watcher {
if (event.getType() != GameEvent.EventType.TARGETED) {
return;
}
StackObject targetingObject = CardUtil.getTargetingStackObject(this.getKey(), event, game);
if (targetingObject == null || CardUtil.checkTargetedEventAlreadyUsed(this.getKey(), targetingObject, event, game)) {
StackObject targetingObject = CardUtil.findTargetingStackObject(this.getKey(), event, game);
if (targetingObject == null) {
return;
}
Permanent permanent = game.getPermanent(event.getTargetId());