- More precise ChangeTargetsEffect replacement deduction for the purpose of DividedAsYouChoose

This commit is contained in:
Agetian
2017-01-28 14:11:22 +00:00
parent 7bc472b4c3
commit ae4e7c252a
2 changed files with 30 additions and 13 deletions

View File

@@ -82,7 +82,7 @@ public class ChangeTargetsEffect extends SpellAbilityEffect {
GameObject newTarget = Iterables.getFirst(getDefinedCardsOrTargeted(sa), null);
if (replaceIn.getSpellAbility(true).canTarget(newTarget)) {
newTargetBlock.add(newTarget);
replaceIn.updateTarget(newTargetBlock);
replaceIn.updateTarget(newTargetBlock, oldTarget, newTarget);
}
else {
replaceIn.updateTarget(oldTargetBlock);
@@ -97,7 +97,7 @@ public class ChangeTargetsEffect extends SpellAbilityEffect {
List<GameEntity> candidates = changingTgtSA.getTargetRestrictions().getAllCandidates(changingTgtSA, true);
GameEntity choice = Aggregates.random(candidates);
changingTgtSA.getTargets().add(choice);
changingTgtSI.updateTarget(changingTgtSA.getTargets());
changingTgtSI.updateTarget(changingTgtSA.getTargets(), null, choice);
}
}
else if (sa.hasParam("DefinedMagnet")){
@@ -105,7 +105,7 @@ public class ChangeTargetsEffect extends SpellAbilityEffect {
if (newTarget != null && changingTgtSA.canTarget(newTarget)) {
changingTgtSA.resetTargets();
changingTgtSA.getTargets().add(newTarget);
changingTgtSI.updateTarget(changingTgtSA.getTargets());
changingTgtSI.updateTarget(changingTgtSA.getTargets(), null, newTarget);
}
}
else {

View File

@@ -32,6 +32,7 @@ import org.apache.commons.lang3.StringUtils;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import forge.game.GameObject;
import java.util.ArrayList;
import java.util.HashMap;
@@ -285,6 +286,10 @@ public class SpellAbilityStackInstance implements IIdentifiable, IHasCardView {
}
public void updateTarget(TargetChoices target) {
updateTarget(target, null, null);
}
public void updateTarget(TargetChoices target, GameObject oldTarget, GameObject newTarget) {
if (target != null) {
tc = target;
ability.setTargets(tc);
@@ -298,20 +303,32 @@ public class SpellAbilityStackInstance implements IIdentifiable, IHasCardView {
Object toRemove = null;
Object toAdd = null;
HashMap<Object,Integer> map = ability.getTargetRestrictions().getDividedMap();
// detect which target has changed
if (oldTarget != null) {
toRemove = oldTarget;
} else {
// try to deduce which target has been replaced
// (this may be imprecise, updateTarget should specify old target if possible)
for (Object obj : map.keySet()) {
if (!target.getTargets().contains(obj)) {
if (!target.getTargets().contains((GameObject)obj)) {
toRemove = obj;
break;
}
}
// detect a new target
}
if (newTarget != null) {
toAdd = newTarget;
} else {
// try to deduce which target was added
// (this may be imprecise, updateTarget should specify new target if possible)
for (Object newTgts : target.getTargets()) {
if (!map.containsKey(newTgts)) {
toAdd = newTgts;
break;
}
}
}
if (toRemove != null && toAdd != null) {
int div = map.get(toRemove);