diff --git a/src/main/java/forge/card/ability/AbilityFactory.java b/src/main/java/forge/card/ability/AbilityFactory.java index ad45565fe80..ac004474d51 100644 --- a/src/main/java/forge/card/ability/AbilityFactory.java +++ b/src/main/java/forge/card/ability/AbilityFactory.java @@ -267,6 +267,9 @@ public final class AbilityFactory { abTgt.calculateStillToDivide(mapParams.get("DividedAsYouChoose"), hostC, null); abTgt.setDividedAsYouChoose(true); } + if (mapParams.containsKey("TargetsAtRandom")) { + abTgt.setRandomTarget(true); + } return abTgt; } diff --git a/src/main/java/forge/card/spellability/Target.java b/src/main/java/forge/card/spellability/Target.java index 8f31ef64ba3..bf42de7ead9 100644 --- a/src/main/java/forge/card/spellability/Target.java +++ b/src/main/java/forge/card/spellability/Target.java @@ -63,6 +63,7 @@ public class Target { private boolean sameController = false; private boolean withoutSameCreatureType = false; private boolean singleTarget = false; + private boolean randomTarget = false; private String definedController = null; // How many can be targeted? @@ -695,10 +696,25 @@ public class Target { * @return a int. */ public final int getNumCandidates(final SpellAbility sa, final boolean isTargeted) { - int candidates = 0; + return getAllCandidates(sa, isTargeted).size(); + } + + /** + *

+ * getAllCandidates. + *

+ * + * @param sa + * the sa + * @param isTargeted + * Check Valid Candidates and Targeting + * @return a List. + */ + public final List getAllCandidates(final SpellAbility sa, final boolean isTargeted) { + List candidates = new ArrayList(); for (Player player : Singletons.getModel().getGame().getPlayers()) { if (sa.canTarget(player)) { - candidates++; + candidates.add(player); } } @@ -708,7 +724,7 @@ public class Target { boolean canTarget = (!isTargeted || c.canBeTargetedBy(sa)); boolean isAlreadyTargeted = this.getTargetCards().contains(c); if (isValidTarget && canTarget && !isAlreadyTargeted) { - candidates++; + candidates.add(c); } } } else { @@ -717,7 +733,7 @@ public class Target { boolean canTarget = (!isTargeted || c.canBeTargetedBy(sa)); boolean isAlreadyTargeted = this.getTargetCards().contains(c); if (isValidTarget && canTarget && !isAlreadyTargeted) { - candidates++; + candidates.add(c); } } } @@ -808,6 +824,20 @@ public class Target { this.differentZone = different; } + /** + * @return the randomTarget + */ + public boolean isRandomTarget() { + return randomTarget; + } + + /** + * @param random the randomTarget to set + */ + public void setRandomTarget(boolean random) { + this.randomTarget = random; + } + /** * @return the differentControllers */ diff --git a/src/main/java/forge/card/spellability/TargetSelection.java b/src/main/java/forge/card/spellability/TargetSelection.java index 27b36cfc45f..f976b17c0fb 100644 --- a/src/main/java/forge/card/spellability/TargetSelection.java +++ b/src/main/java/forge/card/spellability/TargetSelection.java @@ -32,6 +32,7 @@ import forge.game.player.Player; import forge.game.zone.Zone; import forge.game.zone.ZoneType; import forge.gui.GuiChoose; +import forge.util.Aggregates; /** *

@@ -93,7 +94,12 @@ public class TargetSelection { final boolean mandatory = tgt.getMandatory() && tgt.hasCandidates(this.ability, true); final boolean choiceResult; - if (zone.size() == 1 && zone.get(0) == ZoneType.Stack) { + final boolean random = tgt.isRandomTarget(); + if (random) { + List candidates = tgt.getAllCandidates(this.ability, true); + Object choice = Aggregates.random(candidates); + return tgt.addTarget(choice); + } else if (zone.size() == 1 && zone.get(0) == ZoneType.Stack) { // If Zone is Stack, the choices are handled slightly differently. // Handle everything inside function due to interaction with StackInstance return this.chooseCardFromStack(mandatory);