multiple changes:

* refactor: improved target pointer init code and logic, added docs and runtime checks;
* game: fixed miss or wrong init calls in some continuous effects;
* game: fixed wrong usage of target pointers (miss copy code, miss npe checks);
This commit is contained in:
Oleg Agafonov 2024-02-18 15:05:05 +04:00
parent b2aa4ecc08
commit 78612ddc91
115 changed files with 466 additions and 355 deletions

View file

@ -11,6 +11,7 @@ import mage.game.permanent.Permanent;
import mage.target.targetpointer.FixedTargets;
import mage.util.CardUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@ -91,7 +92,7 @@ public class AttacksWithCreaturesTriggeredAbility extends TriggeredAbilityImpl {
}
getEffects().setValue(VALUEKEY_NUMBER_ATTACKERS, attackers.size());
if (setTargetPointer) {
getEffects().setTargetPointer(new FixedTargets(attackers, game));
getEffects().setTargetPointer(new FixedTargets(new ArrayList<>(attackers), game));
}
return true;
}

View file

@ -18,6 +18,7 @@ import mage.util.CardUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* @author nantuko
@ -95,7 +96,7 @@ public class ExileFromGraveCost extends CostImpl {
CardUtil.getSourceName(game, source)
);
if (setTargetPointer) {
source.getEffects().setTargetPointer(new FixedTargets(cardsToExile, game));
source.getEffects().setTargetPointer(new FixedTargets(cardsToExile.getCards(game), game));
}
paid = true;
}

View file

@ -39,6 +39,11 @@ public interface ContinuousEffect extends Effect {
boolean isInactive(Ability source, Game game);
/**
* Init ability data like ZCC or targets on first check in game cycle (ApplyEffects)
* <p>
* Warning, if you setup target pointer in init then must call super.init at the end (after all choices)
*/
void init(Ability source, Game game);
void init(Ability source, Game game, UUID activePlayerId);

View file

@ -3,7 +3,6 @@ package mage.abilities.effects;
import mage.MageObjectReference;
import mage.abilities.Ability;
import mage.abilities.CompoundAbility;
import mage.abilities.MageSingleton;
import mage.abilities.keyword.ChangelingAbility;
import mage.constants.*;
import mage.filter.Filter;
@ -154,6 +153,11 @@ public abstract class ContinuousEffectImpl extends EffectImpl implements Continu
this.discarded = true;
}
@Override
public final void initNewTargetPointer() {
// continuous effect uses init code, so do nothing here
}
@Override
public void init(Ability source, Game game) {
init(source, game, game.getActivePlayerId());

View file

@ -21,6 +21,8 @@ public abstract class EffectImpl implements Effect {
protected EffectType effectType;
// read related docs about static and dynamic targets in ContinuousEffectImpl.affectedObjectsSet
// warning, do not change it directly, use setTargetPointer instead
// TODO: make it private and replace all usage to getTargetPointer
protected TargetPointer targetPointer = new FirstTargetPointer();
protected String staticText = "";
@ -30,6 +32,8 @@ public abstract class EffectImpl implements Effect {
public EffectImpl(Outcome outcome) {
this.id = UUID.randomUUID();
this.outcome = outcome;
initNewTargetPointer();
}
protected EffectImpl(final EffectImpl effect) {
@ -48,6 +52,11 @@ public abstract class EffectImpl implements Effect {
}
}
/**
* Init target pointer by default (see TargetPointer for details)
*/
abstract public void initNewTargetPointer();
@Override
public UUID getId() {
return id;
@ -81,7 +90,13 @@ public abstract class EffectImpl implements Effect {
@Override
public Effect setTargetPointer(TargetPointer targetPointer) {
if (targetPointer == null) {
// first target pointer is default
throw new IllegalArgumentException("Wrong code usage: target pointer can't be set to null: " + this);
}
this.targetPointer = targetPointer;
initNewTargetPointer();
return this;
}

View file

@ -4,6 +4,7 @@ package mage.abilities.effects;
import mage.constants.EffectType;
import mage.constants.Outcome;
import mage.target.targetpointer.TargetPointer;
/**
* @author BetaSteward_at_googlemail.com
@ -15,6 +16,12 @@ public abstract class OneShotEffect extends EffectImpl {
this.effectType = EffectType.ONESHOT;
}
@Override
public final void initNewTargetPointer() {
// one short effects don't use init logic
this.targetPointer.setInitialized();
}
protected OneShotEffect(final OneShotEffect effect) {
super(effect);
}
@ -24,4 +31,10 @@ public abstract class OneShotEffect extends EffectImpl {
super.setText(staticText);
return this;
}
@Override
public Effect setTargetPointer(TargetPointer targetPointer) {
super.setTargetPointer(targetPointer);
return this;
}
}

View file

@ -25,6 +25,7 @@ import mage.util.functions.CopyTokenFunction;
import mage.util.functions.EmptyCopyApplier;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author LevelX2
@ -418,7 +419,7 @@ public class CreateTokenCopyTargetEffect extends OneShotEffect {
} else {
effect = new SacrificeTargetEffect("sacrifice the token copies", source.getControllerId());
}
effect.setTargetPointer(new FixedTargets(addedTokenPermanents, game));
effect.setTargetPointer(new FixedTargets(new ArrayList<>(addedTokenPermanents), game));
DelayedTriggeredAbility exileAbility;

View file

@ -46,8 +46,7 @@ public class DetainAllEffect extends OneShotEffect {
if (!game.isSimulation()) {
game.informPlayers("Detained permanent: " + permanent.getName());
}
FixedTarget fixedTarget = new FixedTarget(permanent, game);
detainedObjects.add(fixedTarget);
detainedObjects.add(new FixedTarget(permanent, game));
}
game.addEffect(new DetainAllRestrictionEffect(detainedObjects), source);

View file

@ -13,6 +13,7 @@ import mage.players.Player;
import mage.target.targetpointer.FixedTargets;
import mage.util.CardUtil;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
@ -74,7 +75,7 @@ public class ExileReturnBattlefieldNextEndStepTargetEffect extends OneShotEffect
Effect effect = yourControl
? new ReturnToBattlefieldUnderYourControlTargetEffect(exiledOnly)
: new ReturnToBattlefieldUnderOwnerControlTargetEffect(false, exiledOnly);
effect.setTargetPointer(new FixedTargets(new CardsImpl(toExile), game));
effect.setTargetPointer(new FixedTargets(toExile, game));
game.addDelayedTriggeredAbility(new AtTheBeginOfNextEndStepDelayedTriggeredAbility(effect), source);
return true;
}

View file

@ -1,5 +1,6 @@
package mage.abilities.effects.common;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.dynamicvalue.DynamicValue;
import mage.abilities.dynamicvalue.common.StaticValue;
@ -14,7 +15,9 @@ import mage.players.Player;
import mage.target.targetpointer.FixedTargets;
import mage.util.CardUtil;
import java.util.ArrayList;
import java.util.Set;
import java.util.stream.Collectors;
public class ExileTopXMayPlayUntilEffect extends OneShotEffect {

View file

@ -42,6 +42,7 @@ public class PreventAllDamageFromChosenSourceToYouEffect extends PreventionEffec
@Override
public void init(Ability source, Game game) {
super.init(source, game);
this.targetSource.choose(Outcome.PreventDamage, source.getControllerId(), source.getSourceId(), source, game);
// be sure to note the target source's zcc, etc, if able.
if (targetSource.getFirstTarget() != null) {

View file

@ -47,6 +47,7 @@ public class PreventDamageBySourceEffect extends PreventionEffectImpl {
@Override
public void init(Ability source, Game game) {
super.init(source, game);
this.target.choose(Outcome.PreventDamage, source.getControllerId(), source.getSourceId(), source, game);
mageObjectReference = new MageObjectReference(target.getFirstTarget(), game);
}

View file

@ -43,6 +43,7 @@ public class PreventNextDamageFromChosenSourceToTargetEffect extends PreventionE
@Override
public void init(Ability source, Game game) {
super.init(source, game);
this.targetSource.choose(Outcome.PreventDamage, source.getControllerId(), source.getSourceId(), source, game);
}

View file

@ -43,6 +43,7 @@ public class PreventNextDamageFromChosenSourceToYouEffect extends PreventionEffe
@Override
public void init(Ability source, Game game) {
super.init(source, game);
this.targetSource.choose(Outcome.PreventDamage, source.getControllerId(), source.getSourceId(), source, game);
}

View file

@ -27,7 +27,7 @@ public class ReturnToHandChosenControlledPermanentEffect extends ReturnToHandCho
@Override
public boolean apply(Game game, Ability source) {
this.targetPointer = new FixedTarget(source.getControllerId());
this.setTargetPointer(new FixedTarget(source.getControllerId()));
return super.apply(game, source);
}

View file

@ -31,7 +31,7 @@ public class SacrificeControllerEffect extends SacrificeEffect {
@Override
public boolean apply(Game game, Ability source) {
this.targetPointer = new FixedTarget(source.getControllerId());
this.setTargetPointer(new FixedTarget(source.getControllerId()));
return super.apply(game, source);
}

View file

@ -13,10 +13,7 @@ import mage.target.targetpointer.FixedTarget;
import mage.target.targetpointer.FixedTargets;
import mage.util.CardUtil;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;
/**

View file

@ -71,8 +71,11 @@ public class BecomesColorSourceEffect extends ContinuousEffectImpl {
@Override
public void init(Ability source, Game game) {
super.init(source, game);
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
discard();
return;
}
if (setColor == null) {
@ -86,7 +89,6 @@ public class BecomesColorSourceEffect extends ContinuousEffectImpl {
game.informPlayers(controller.getLogName() + " has chosen the color: " + setColor.toString());
}
}
super.init(source, game);
}
@Override

View file

@ -50,8 +50,11 @@ public class BecomesColorTargetEffect extends ContinuousEffectImpl {
@Override
public void init(Ability source, Game game) {
super.init(source, game);
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
discard();
return;
}
if (setColor == null) {
@ -65,8 +68,6 @@ public class BecomesColorTargetEffect extends ContinuousEffectImpl {
game.informPlayers(controller.getLogName() + " has chosen the color: " + setColor.toString());
}
}
super.init(source, game);
}
@Override

View file

@ -65,7 +65,7 @@ public class BoostEquippedEffect extends ContinuousEffectImpl {
this.setTargetPointer(new FixedTarget(equipment.getAttachedTo(), game));
}
}
super.init(source, game); // inits the target pointer so call it after setting the targetPointer
super.init(source, game); // must call at the end due target pointer setup
}
@Override

View file

@ -73,6 +73,8 @@ public class ExchangeControlTargetEffect extends ContinuousEffectImpl {
@Override
public void init(Ability source, Game game) {
super.init(source, game);
Permanent permanent1 = null;
Permanent permanent2 = null;

View file

@ -77,13 +77,13 @@ public class GainAbilityAttachedEffect extends ContinuousEffectImpl {
@Override
public void init(Ability source, Game game) {
super.init(source, game);
if (affectedObjectsSet) {
Permanent equipment = game.getPermanentOrLKIBattlefield(source.getSourceId());
if (equipment != null && equipment.getAttachedTo() != null) {
this.setTargetPointer(new FixedTarget(equipment.getAttachedTo(), game.getState().getZoneChangeCounter(equipment.getAttachedTo())));
}
}
super.init(source, game); // must call at the end due target pointer setup
}
@Override

View file

@ -60,13 +60,13 @@ public class GainAbilityWithAttachmentEffect extends ContinuousEffectImpl {
@Override
public void init(Ability source, Game game) {
super.init(source, game);
if (affectedObjectsSet) {
Permanent equipment = game.getPermanentOrLKIBattlefield(source.getSourceId());
if (equipment != null && equipment.getAttachedTo() != null) {
this.setTargetPointer(new FixedTarget(equipment.getAttachedTo(), game.getState().getZoneChangeCounter(equipment.getAttachedTo())));
}
}
super.init(source, game); // must call at the end due target pointer setup
}
@Override

View file

@ -94,12 +94,12 @@ public class BolsterEffect extends OneShotEffect {
return false;
}
Effect effect = new AddCountersTargetEffect(CounterType.P1P1.createInstance(amount.calculate(game, source, this)));
FixedTarget fixedTarget = new FixedTarget(selectedCreature, game);
effect.setTargetPointer(fixedTarget);
FixedTarget blueprintTarget = new FixedTarget(selectedCreature, game);
effect.setTargetPointer(blueprintTarget.copy());
effect.apply(game, source);
if (!additionalEffects.isEmpty()) {
for (Effect additionalEffect : additionalEffects) {
additionalEffect.setTargetPointer(fixedTarget);
additionalEffect.setTargetPointer(blueprintTarget.copy());
if (additionalEffect instanceof OneShotEffect) {
additionalEffect.apply(game, source);
} else {

View file

@ -104,12 +104,12 @@ class AwakenEffect extends OneShotEffect {
}
}
if (targetId != null) {
FixedTarget fixedTarget = new FixedTarget(targetId, game);
FixedTarget blueprintTarget = new FixedTarget(targetId, game);
ContinuousEffect continuousEffect = new BecomesCreatureTargetEffect(new AwakenElementalToken(), false, true, Duration.Custom);
continuousEffect.setTargetPointer(fixedTarget);
continuousEffect.setTargetPointer(blueprintTarget.copy());
game.addEffect(continuousEffect, source);
Effect effect = new AddCountersTargetEffect(CounterType.P1P1.createInstance(awakenValue));
effect.setTargetPointer(fixedTarget);
effect.setTargetPointer(blueprintTarget.copy());
effect.apply(game, source);
}
return true;

View file

@ -89,7 +89,7 @@ class MyriadEffect extends OneShotEffect {
}
if (!tokens.isEmpty()) {
ExileTargetEffect exileEffect = new ExileTargetEffect();
exileEffect.setTargetPointer(new FixedTargets(tokens, game));
exileEffect.setTargetPointer(new FixedTargets(new ArrayList<>(tokens), game));
game.addDelayedTriggeredAbility(new AtTheEndOfCombatDelayedTriggeredAbility(exileEffect), source);
}
return true;

View file

@ -118,7 +118,7 @@ class DackFaydenEmblemTriggeredAbility extends TriggeredAbilityImpl {
class DackFaydenEmblemEffect extends ContinuousEffectImpl {
protected FixedTargets fixedTargets;
protected FixedTargets fixedTargets = new FixedTargets(new ArrayList<>());
DackFaydenEmblemEffect() {
super(Duration.EndOfGame, Layer.ControlChangingEffects_2, SubLayer.NA, Outcome.GainControl);
@ -127,7 +127,7 @@ class DackFaydenEmblemEffect extends ContinuousEffectImpl {
DackFaydenEmblemEffect(final DackFaydenEmblemEffect effect) {
super(effect);
this.fixedTargets = effect.fixedTargets;
this.fixedTargets = effect.fixedTargets.copy();
}
@Override
@ -147,6 +147,6 @@ class DackFaydenEmblemEffect extends ContinuousEffectImpl {
}
public void setTargets(List<Permanent> targetedPermanents, Game game) {
this.fixedTargets = new FixedTargets(targetedPermanents, game);
this.fixedTargets = new FixedTargets(new ArrayList<>(targetedPermanents), game);
}
}

View file

@ -812,4 +812,9 @@ public class GameEvent implements Serializable {
protected void setSourceId(UUID sourceId) {
this.sourceId = sourceId;
}
@Override
public String toString() {
return this.type.toString();
}
}

View file

@ -15,11 +15,7 @@ import java.util.stream.Collectors;
public class EachTargetPointer extends TargetPointerImpl {
private Map<UUID, Integer> zoneChangeCounter = new HashMap<>();
public static EachTargetPointer getInstance() {
return new EachTargetPointer();
}
private final Map<UUID, Integer> zoneChangeCounter = new HashMap<>();
public EachTargetPointer() {
super();
@ -27,15 +23,16 @@ public class EachTargetPointer extends TargetPointerImpl {
protected EachTargetPointer(final EachTargetPointer targetPointer) {
super(targetPointer);
this.zoneChangeCounter = new HashMap<>();
for (Map.Entry<UUID, Integer> entry : targetPointer.zoneChangeCounter.entrySet()) {
this.zoneChangeCounter.put(entry.getKey(), entry.getValue());
}
this.zoneChangeCounter.putAll(targetPointer.zoneChangeCounter);
}
@Override
public void init(Game game, Ability source) {
if (isInitialized()) {
return;
}
this.setInitialized();
if (!source.getTargets().isEmpty()) {
for (UUID target : source
.getTargets()
@ -99,17 +96,6 @@ public class EachTargetPointer extends TargetPointerImpl {
return new EachTargetPointer(this);
}
@Override
public FixedTarget getFixedTarget(Game game, Ability source) {
this.init(game, source);
UUID firstId = getFirst(game, source);
if (firstId != null) {
return new FixedTarget(firstId, game.getState().getZoneChangeCounter(firstId));
}
return null;
}
@Override
public Permanent getFirstTargetPermanentOrLKI(Game game, Ability source) {
UUID targetId = source.getFirstTarget();

View file

@ -16,10 +16,11 @@ import java.util.UUID;
public class FixedTarget extends TargetPointerImpl {
private final UUID targetId;
private int zoneChangeCounter;
private boolean initialized;
private int zoneChangeCounter = 0;
/**
* Dynamic ZCC (not recommended)
* <p>
* Use this best only to target to a player or spells on the stack. Try to
* avoid this method to set the target to a specific card or permanent if
* possible. Because the zoneChangeCounter is not set immediately, it can be
@ -32,7 +33,6 @@ public class FixedTarget extends TargetPointerImpl {
public FixedTarget(UUID target) {
super();
this.targetId = target;
this.initialized = false;
}
public FixedTarget(MageObjectReference mor) {
@ -40,7 +40,9 @@ public class FixedTarget extends TargetPointerImpl {
}
/**
* Target counter is immediatly initialised with current zoneChangeCounter
* Static ZCC
* <p>
* Target counter is immediately initialised with current zoneChangeCounter
* value from the GameState Sets fixed the currect zoneChangeCounter
*
* @param card used to get the objectId
@ -50,10 +52,13 @@ public class FixedTarget extends TargetPointerImpl {
super();
this.targetId = card.getId();
this.zoneChangeCounter = card.getZoneChangeCounter(game);
this.initialized = true;
this.setInitialized(); // no need dynamic init
}
/**
* Static ZCC
* <p>
* Target counter is immediately initialized with current zoneChangeCounter
* value from the given permanent
*
@ -65,6 +70,8 @@ public class FixedTarget extends TargetPointerImpl {
}
/**
* Static ZCC
* <p>
* Use this if you already want to fix the target object to the known zone
* now (otherwise the zone will be set if the ability triggers or not at
* all) If not initialized, the object of the current zone then will be
@ -76,11 +83,14 @@ public class FixedTarget extends TargetPointerImpl {
public FixedTarget(UUID targetId, int zoneChangeCounter) {
super();
this.targetId = targetId;
this.initialized = true;
this.zoneChangeCounter = zoneChangeCounter;
this.setInitialized(); // no need dynamic init
}
/**
* Static ZCC
* <p>
* Use this to set the target to exactly the zone the target is currently in
*
* @param targetId
@ -89,8 +99,9 @@ public class FixedTarget extends TargetPointerImpl {
public FixedTarget(UUID targetId, Game game) {
super();
this.targetId = targetId;
this.initialized = true;
this.zoneChangeCounter = game.getState().getZoneChangeCounter(targetId);
this.setInitialized(); // no need dynamic init
}
protected FixedTarget(final FixedTarget targetPointer) {
@ -98,15 +109,16 @@ public class FixedTarget extends TargetPointerImpl {
this.targetId = targetPointer.targetId;
this.zoneChangeCounter = targetPointer.zoneChangeCounter;
this.initialized = targetPointer.initialized;
}
@Override
public void init(Game game, Ability source) {
if (!initialized) {
initialized = true;
this.zoneChangeCounter = game.getState().getZoneChangeCounter(targetId);
if (isInitialized()) {
return;
}
setInitialized();
this.zoneChangeCounter = game.getState().getZoneChangeCounter(targetId);
}
/**
@ -161,12 +173,6 @@ public class FixedTarget extends TargetPointerImpl {
return zoneChangeCounter;
}
@Override
public FixedTarget getFixedTarget(Game game, Ability source) {
init(game, source);
return this;
}
@Override
public Permanent getFirstTargetPermanentOrLKI(Game game, Ability source) {
init(game, source);

View file

@ -14,80 +14,61 @@ import java.util.*;
import java.util.stream.Collectors;
/**
* Targets list with static ZCC
*
* @author LevelX2
*/
public class FixedTargets extends TargetPointerImpl {
final ArrayList<MageObjectReference> targets = new ArrayList<>();
final ArrayList<UUID> targetsNotInitialized = new ArrayList<>();
private boolean initialized;
public FixedTargets(UUID targetId) {
super();
targetsNotInitialized.add(targetId);
this.initialized = false;
public FixedTargets(List<Permanent> objects, Game game) {
this(objects
.stream()
.map(o -> new MageObjectReference(o.getId(), game))
.collect(Collectors.toList()));
}
public FixedTargets(Cards cards, Game game) {
super();
if (cards != null) {
for (UUID targetId : cards) {
MageObjectReference mor = new MageObjectReference(targetId, game);
targets.add(mor);
}
}
this.initialized = true;
public FixedTargets(Set<Card> objects, Game game) {
this(objects
.stream()
.map(o -> new MageObjectReference(o.getId(), game))
.collect(Collectors.toList()));
}
public FixedTargets(Cards objects, Game game) {
this(objects.getCards(game)
.stream()
.map(o -> new MageObjectReference(o.getId(), game))
.collect(Collectors.toList()));
}
public FixedTargets(Token token, Game game) {
this(token.getLastAddedTokenIds().stream().map(game::getPermanent).collect(Collectors.toList()), game);
this(token.getLastAddedTokenIds()
.stream()
.map(game::getPermanent)
.collect(Collectors.toList()), game);
}
public FixedTargets(List<Permanent> permanents, Game game) {
public FixedTargets(List<MageObjectReference> morList) {
super();
for (Permanent permanent : permanents) {
MageObjectReference mor = new MageObjectReference(permanent.getId(), permanent.getZoneChangeCounter(game), game);
targets.add(mor);
}
this.initialized = true;
targets.addAll(morList);
this.setInitialized(); // no need dynamic init
}
public FixedTargets(Set<Card> cards, Game game) {
super();
for (Card card : cards) {
MageObjectReference mor = new MageObjectReference(card.getId(), card.getZoneChangeCounter(game), game);
targets.add(mor);
}
this.initialized = true;
}
public FixedTargets(Collection<MageObjectReference> morSet, Game game) {
super();
targets.addAll(morSet);
this.initialized = true;
}
private FixedTargets(final FixedTargets targetPointer) {
super(targetPointer);
this.targets.addAll(targetPointer.targets);
this.targetsNotInitialized.addAll(targetPointer.targetsNotInitialized);
this.initialized = targetPointer.initialized;
private FixedTargets(final FixedTargets pointer) {
super(pointer);
this.targets.addAll(pointer.targets);
}
@Override
public void init(Game game, Ability source) {
if (!initialized) {
initialized = true;
for (UUID targetId : targetsNotInitialized) {
targets.add(new MageObjectReference(targetId, game.getState().getZoneChangeCounter(targetId), game));
}
if (isInitialized()) {
return;
}
// impossible use case
throw new IllegalArgumentException("Wrong code usage: FixedTargets support only static ZCC, you can't get here");
}
@Override
@ -118,23 +99,6 @@ public class FixedTargets extends TargetPointerImpl {
return new FixedTargets(this);
}
/**
* Returns a fixed target for (and only) the first taget
*
* @param game
* @param source
* @return
*/
@Override
public FixedTarget getFixedTarget(Game game, Ability source) {
this.init(game, source);
UUID firstId = getFirst(game, source);
if (firstId != null) {
return new FixedTarget(firstId, game.getState().getZoneChangeCounter(firstId));
}
return null;
}
@Override
public Permanent getFirstTargetPermanentOrLKI(Game game, Ability source) {
UUID targetId = null;
@ -143,8 +107,6 @@ public class FixedTargets extends TargetPointerImpl {
MageObjectReference mor = targets.get(0);
targetId = mor.getSourceId();
zoneChangeCounter = mor.getZoneChangeCounter();
} else if (!targetsNotInitialized.isEmpty()) {
targetId = targetsNotInitialized.get(0);
}
if (targetId != null) {
Permanent permanent = game.getPermanent(targetId);

View file

@ -1,6 +1,6 @@
package mage.target.targetpointer;
import mage.MageObject;
import mage.MageObjectReference;
import mage.abilities.Ability;
import mage.cards.Card;
import mage.constants.Zone;
@ -15,154 +15,148 @@ import java.util.*;
*/
public abstract class NthTargetPointer extends TargetPointerImpl {
private static final Map<UUID, Integer> emptyZoneChangeCounter = Collections.unmodifiableMap(new HashMap<>(0));
private static final List<UUID> emptyTargets = Collections.unmodifiableList(new ArrayList<>(0));
private Map<UUID, Integer> zoneChangeCounter;
private final int targetNumber;
// TODO: rework to list of MageObjectReference instead zcc
private final Map<UUID, Integer> zoneChangeCounter = new HashMap<>();
private final int targetIndex; // zero-based target numbers (1 -> 0, 2 -> 1, 3 -> 2, etc)
public NthTargetPointer(int targetNumber) {
super();
this.targetNumber = targetNumber;
this.targetIndex = targetNumber - 1;
}
protected NthTargetPointer(final NthTargetPointer nthTargetPointer) {
super(nthTargetPointer);
this.targetNumber = nthTargetPointer.targetNumber;
if (nthTargetPointer.zoneChangeCounter != null) {
this.zoneChangeCounter = new HashMap<>(nthTargetPointer.zoneChangeCounter.size());
for (Map.Entry<UUID, Integer> entry : nthTargetPointer.zoneChangeCounter.entrySet()) {
addToZoneChangeCounter(entry.getKey(), entry.getValue());
}
}
this.targetIndex = nthTargetPointer.targetIndex;
this.zoneChangeCounter.putAll(nthTargetPointer.zoneChangeCounter);
}
@Override
public void init(Game game, Ability source) {
if (source.getTargets().size() < targetNumber) {
if (isInitialized()) {
return;
}
this.setInitialized();
if (source.getTargets().size() <= this.targetIndex) {
wrongTargetsUsage(source);
return;
}
for (UUID target : source.getTargets().get(targetIndex()).getTargets()) {
for (UUID target : source.getTargets().get(this.targetIndex).getTargets()) {
Card card = game.getCard(target);
if (card != null) {
addToZoneChangeCounter(target, card.getZoneChangeCounter(game));
this.zoneChangeCounter.put(target, card.getZoneChangeCounter(game));
}
}
}
private void wrongTargetsUsage(Ability source) {
if (this.targetIndex > 0) {
// first target pointer is default, so must be ignored
throw new IllegalStateException("Wrong code usage: source ability miss targets setup for target pointer - "
+ this.getClass().getSimpleName() + " - " + source.getClass().getSimpleName() + " - " + source);
}
}
@Override
public List<UUID> getTargets(Game game, Ability source) {
if (source.getTargets().size() < targetNumber) {
// can be used before effect's init (example: checking spell targets on stack before resolve like HeroicAbility)
if (source.getTargets().size() <= this.targetIndex) {
wrongTargetsUsage(source);
return emptyTargets;
}
List<UUID> targetIds = source.getTargets().get(targetIndex()).getTargets();
List<UUID> finalTargetIds = new ArrayList<>(targetIds.size());
for (UUID targetId : targetIds) {
Card card = game.getCard(targetId);
if (card != null
&& getZoneChangeCounter().containsKey(targetId)
&& card.getZoneChangeCounter(game) != getZoneChangeCounter().get(targetId)) {
// But no longer if new permanent is already on the battlefield
Permanent permanent = game.getPermanentOrLKIBattlefield(targetId);
if (permanent == null || permanent.getZoneChangeCounter(game) != getZoneChangeCounter().get(targetId)) {
continue;
}
List<UUID> res = new ArrayList<>();
for (UUID targetId : source.getTargets().get(this.targetIndex).getTargets()) {
if (!isOutdatedTarget(game, targetId)) {
res.add(targetId);
}
finalTargetIds.add(targetId);
}
return finalTargetIds;
return res;
}
private boolean isOutdatedTarget(Game game, UUID targetId) {
int needZcc = this.zoneChangeCounter.getOrDefault(targetId, 0);
if (needZcc == 0) {
// any zcc (target not init yet here)
return false;
}
// card
Card card = game.getCard(targetId);
if (card != null && card.getZoneChangeCounter(game) == needZcc) {
return false;
}
// permanent
Permanent permanent = game.getPermanentOrLKIBattlefield(targetId);
if (permanent != null && permanent.getZoneChangeCounter(game) == needZcc) {
return false;
}
// TODO: if no bug reports with die triggers and new code then remove it, 2024-02-18
// if you catch bugs then add code like if permanent.getZoneChangeCounter(game) == needZcc + 1 then return false
// old comments:
// Because if dies trigger has to trigger as permanent has already moved zone, we have to check if target
// was on the battlefield immed. before, but no longer if new permanent is already on the battlefield
// outdated
return true;
}
@Override
public UUID getFirst(Game game, Ability source) {
if (source.getTargets().size() < targetNumber) {
if (source.getTargets().size() <= this.targetIndex) {
wrongTargetsUsage(source);
return null;
}
UUID targetId = source.getTargets().get(targetIndex()).getFirstTarget();
if (getZoneChangeCounter().containsKey(targetId)) {
Card card = game.getCard(targetId);
if (card != null && getZoneChangeCounter().containsKey(targetId)
&& card.getZoneChangeCounter(game) != getZoneChangeCounter().get(targetId)) {
// Because if dies trigger has to trigger as permanent has already moved zone, we have to check if target was on the battlefield immed. before
// but no longer if new permanent is already on the battlefield
Permanent permanent = game.getPermanentOrLKIBattlefield(targetId);
if (permanent == null || permanent.getZoneChangeCounter(game) != zoneChangeCounter.get(targetId)) {
return null;
}
}
UUID targetId = source.getTargets().get(this.targetIndex).getFirstTarget();
if (isOutdatedTarget(game, targetId)) {
return null;
}
return targetId;
}
@Override
public FixedTarget getFixedTarget(Game game, Ability source) {
this.init(game, source);
UUID firstId = getFirst(game, source);
if (firstId != null) {
return new FixedTarget(firstId, game.getState().getZoneChangeCounter(firstId));
}
return null;
}
@Override
public Permanent getFirstTargetPermanentOrLKI(Game game, Ability source) {
if (source.getTargets().size() < targetNumber) {
if (source.getTargets().size() < this.targetIndex) {
wrongTargetsUsage(source);
return null;
}
UUID targetId = source.getTargets().get(this.targetIndex).getFirstTarget();
Permanent permanent;
UUID targetId = source.getTargets().get(targetIndex()).getFirstTarget();
if (getZoneChangeCounter().containsKey(targetId)) {
permanent = game.getPermanent(targetId);
if (permanent != null && permanent.getZoneChangeCounter(game) == getZoneChangeCounter().get(targetId)) {
return permanent;
}
MageObject mageObject = game.getLastKnownInformation(targetId, Zone.BATTLEFIELD, getZoneChangeCounter().get(targetId));
if (mageObject instanceof Permanent) {
return (Permanent) mageObject;
}
if (this.zoneChangeCounter.containsKey(targetId)) {
// need static zcc
MageObjectReference needRef = new MageObjectReference(targetId, this.zoneChangeCounter.getOrDefault(targetId, 0), game);
return game.getPermanentOrLKIBattlefield(needRef);
} else {
permanent = game.getPermanent(targetId);
// need any zcc
// TODO: must research, is it used at all?! Init code must fill all static zcc data before go here
Permanent permanent = game.getPermanent(targetId);
if (permanent == null) {
permanent = (Permanent) game.getLastKnownInformation(targetId, Zone.BATTLEFIELD);
}
return permanent;
}
return permanent;
}
@Override
public String describeTargets(Targets targets, String defaultDescription) {
return targets.size() < targetNumber ? defaultDescription : targets.get(targetIndex()).getDescription();
if (targets.size() <= this.targetIndex) {
// TODO: need research, is it used for non setup targets ?!
return defaultDescription;
} else {
return targets.get(this.targetIndex).getDescription();
}
}
@Override
public boolean isPlural(Targets targets) {
return targets.size() > targetIndex() && targets.get(targetIndex()).getMaxNumberOfTargets() > 1;
}
private int targetIndex() {
return targetNumber - 1;
}
private Map<UUID, Integer> getZoneChangeCounter() {
return zoneChangeCounter != null ? zoneChangeCounter : emptyZoneChangeCounter;
}
private void addToZoneChangeCounter(UUID key, Integer value) {
if (zoneChangeCounter == null) {
zoneChangeCounter = new HashMap<>();
}
getZoneChangeCounter().put(key, value);
return targets.size() > this.targetIndex && targets.get(this.targetIndex).getMaxNumberOfTargets() > 1;
}
}

View file

@ -12,15 +12,30 @@ import java.util.UUID;
public interface TargetPointer extends Serializable, Copyable<TargetPointer> {
/**
* Init dynamic targets (must save current targets zcc to fizzle it later on outdated targets)
* - one shot effects: no needs to init
* - continues effects: must use init logic
*/
void init(Game game, Ability source);
boolean isInitialized();
void setInitialized();
List<UUID> getTargets(Game game, Ability source);
/**
* Return first actual target id (null on outdated targets)
*/
UUID getFirst(Game game, Ability source);
TargetPointer copy();
/**
* Return first actual target data (null on outdated targets)
*/
FixedTarget getFirstAsFixedTarget(Game game, Ability source);
FixedTarget getFixedTarget(Game game, Ability source);
TargetPointer copy();
/**
* Retrieves the permanent according the first targetId and

View file

@ -1,7 +1,11 @@
package mage.target.targetpointer;
import mage.abilities.Ability;
import mage.game.Game;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* @author JayDi85
@ -11,6 +15,8 @@ public abstract class TargetPointerImpl implements TargetPointer {
// Store custom data here. Use it to keep unique values for ability instances on stack (example: Gruul Ragebeast)
private Map<String, String> data;
private boolean initialized = false;
public TargetPointerImpl() {
super();
}
@ -21,6 +27,17 @@ public abstract class TargetPointerImpl implements TargetPointer {
this.data = new HashMap<>();
this.data.putAll(targetPointer.data);
}
this.initialized = targetPointer.initialized;
}
@Override
public boolean isInitialized() {
return this.initialized;
}
@Override
public void setInitialized() {
this.initialized = true;
}
@Override
@ -39,4 +56,14 @@ public abstract class TargetPointerImpl implements TargetPointer {
data.put(key, value);
return this;
}
@Override
public final FixedTarget getFirstAsFixedTarget(Game game, Ability source) {
UUID firstId = this.getFirst(game, source);
if (firstId != null) {
return new FixedTarget(firstId, game.getState().getZoneChangeCounter(firstId));
}
return null;
}
}