From a9e479f7dd60a893f5e049b900473e6ac950711c Mon Sep 17 00:00:00 2001 From: JOAC69 Date: Fri, 23 Sep 2016 21:18:22 -0500 Subject: [PATCH] Refactor - made card specific effects to attack random opponent shared effects --- .../mage/sets/commander/RuhanOfTheFomori.java | 91 +------------------ .../mage/sets/commander2014/RavingDead.java | 78 +--------------- ...AttacksIfAbleTargetPlayerSourceEffect.java | 50 ++++++++++ ...fAbleTargetRandomOpponentSourceEffect.java | 49 ++++++++++ 4 files changed, 103 insertions(+), 165 deletions(-) create mode 100644 Mage/src/main/java/mage/abilities/effects/common/AttacksIfAbleTargetPlayerSourceEffect.java create mode 100644 Mage/src/main/java/mage/abilities/effects/common/combat/AttackIfAbleTargetRandomOpponentSourceEffect.java diff --git a/Mage.Sets/src/mage/sets/commander/RuhanOfTheFomori.java b/Mage.Sets/src/mage/sets/commander/RuhanOfTheFomori.java index 8e051a99611..f7c1ed57ae3 100644 --- a/Mage.Sets/src/mage/sets/commander/RuhanOfTheFomori.java +++ b/Mage.Sets/src/mage/sets/commander/RuhanOfTheFomori.java @@ -27,27 +27,14 @@ */ package mage.sets.commander; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; import java.util.UUID; import mage.MageInt; -import mage.abilities.Ability; import mage.abilities.common.BeginningOfCombatTriggeredAbility; -import mage.abilities.effects.ContinuousEffect; -import mage.abilities.effects.OneShotEffect; -import mage.abilities.effects.RequirementEffect; +import mage.abilities.effects.common.combat.AttackIfAbleTargetRandomOpponentSourceEffect; import mage.cards.CardImpl; import mage.constants.CardType; -import mage.constants.Duration; -import mage.constants.Outcome; import mage.constants.Rarity; import mage.constants.TargetController; -import mage.game.Game; -import mage.game.permanent.Permanent; -import mage.players.Player; -import mage.target.targetpointer.FixedTarget; -import mage.util.RandomUtil; /** * @@ -66,7 +53,7 @@ public class RuhanOfTheFomori extends CardImpl { this.toughness = new MageInt(7); // At the beginning of combat on your turn, choose an opponent at random. Ruhan of the Fomori attacks that player this combat if able. - this.addAbility(new BeginningOfCombatTriggeredAbility(new RuhanOfTheFomoriEffect(), TargetController.YOU, false)); + this.addAbility(new BeginningOfCombatTriggeredAbility(new AttackIfAbleTargetRandomOpponentSourceEffect(), TargetController.YOU, false)); } public RuhanOfTheFomori(final RuhanOfTheFomori card) { @@ -79,77 +66,3 @@ public class RuhanOfTheFomori extends CardImpl { } } -class RuhanOfTheFomoriEffect extends OneShotEffect { - - public RuhanOfTheFomoriEffect() { - super(Outcome.Benefit); - this.staticText = "choose an opponent at random. {this} attacks that player this combat if able"; - } - - public RuhanOfTheFomoriEffect(final RuhanOfTheFomoriEffect effect) { - super(effect); - } - - @Override - public RuhanOfTheFomoriEffect copy() { - return new RuhanOfTheFomoriEffect(this); - } - - @Override - public boolean apply(Game game, Ability source) { - Player controller = game.getPlayer(source.getControllerId()); - if (controller != null) { - List opponents = new ArrayList<>(); - opponents.addAll(game.getOpponents(controller.getId())); - Player opponent = game.getPlayer(opponents.get(RandomUtil.nextInt(opponents.size()))); - if (opponent != null) { - ContinuousEffect effect = new AttacksIfAbleTargetPlayerSourceEffect(); - effect.setTargetPointer(new FixedTarget(opponent.getId())); - game.addEffect(effect, source); - return true; - } - } - return false; - } -} - -class AttacksIfAbleTargetPlayerSourceEffect extends RequirementEffect { - - public AttacksIfAbleTargetPlayerSourceEffect() { - super(Duration.EndOfTurn); - staticText = "{this} attacks that player this combat if able"; - } - - public AttacksIfAbleTargetPlayerSourceEffect(final AttacksIfAbleTargetPlayerSourceEffect effect) { - super(effect); - } - - @Override - public AttacksIfAbleTargetPlayerSourceEffect copy() { - return new AttacksIfAbleTargetPlayerSourceEffect(this); - } - - @Override - public boolean applies(Permanent permanent, Ability source, Game game) { - if (permanent.getId().equals(source.getSourceId())) { - return true; - } - return false; - } - - @Override - public boolean mustAttack(Game game) { - return true; - } - - @Override - public boolean mustBlock(Game game) { - return false; - } - - @Override - public UUID mustAttackDefender(Ability source, Game game) { - return getTargetPointer().getFirst(game, source); - } - -} \ No newline at end of file diff --git a/Mage.Sets/src/mage/sets/commander2014/RavingDead.java b/Mage.Sets/src/mage/sets/commander2014/RavingDead.java index 4d770dc60e7..4c598a9ab58 100644 --- a/Mage.Sets/src/mage/sets/commander2014/RavingDead.java +++ b/Mage.Sets/src/mage/sets/commander2014/RavingDead.java @@ -38,6 +38,7 @@ import mage.abilities.common.DealsCombatDamageToAPlayerTriggeredAbility; import mage.abilities.effects.ContinuousEffect; import mage.abilities.effects.OneShotEffect; import mage.abilities.effects.RequirementEffect; +import mage.abilities.effects.common.combat.AttackIfAbleTargetRandomOpponentSourceEffect; import mage.abilities.keyword.DeathtouchAbility; import mage.cards.CardImpl; import mage.constants.CardType; @@ -68,7 +69,7 @@ public class RavingDead extends CardImpl { // Deathtouch this.addAbility(DeathtouchAbility.getInstance()); // At the beginning of combat on your turn, choose an opponent at random. Raving Dead attacks that player this combat if able. - this.addAbility(new BeginningOfCombatTriggeredAbility(new RavingDeadEffect(), TargetController.YOU, false)); + this.addAbility(new BeginningOfCombatTriggeredAbility(new AttackIfAbleTargetRandomOpponentSourceEffect(), TargetController.YOU, false)); // Whenever Raving Dead deals combat damage to a player, that player loses half his or her life, rounded down. this.addAbility(new DealsCombatDamageToAPlayerTriggeredAbility(new RavingDeadDamageEffect(), false, true)); } @@ -83,40 +84,6 @@ public class RavingDead extends CardImpl { } } -class RavingDeadEffect extends OneShotEffect { - - public RavingDeadEffect() { - super(Outcome.Benefit); - this.staticText = "choose an opponent at random. {this} attacks that player this combat if able"; - } - - public RavingDeadEffect(final RavingDeadEffect effect) { - super(effect); - } - - @Override - public RavingDeadEffect copy() { - return new RavingDeadEffect(this); - } - - @Override - public boolean apply(Game game, Ability source) { - Player controller = game.getPlayer(source.getControllerId()); - if (controller != null) { - List opponents = new ArrayList<>(); - opponents.addAll(game.getOpponents(controller.getId())); - Player opponent = game.getPlayer(opponents.get(RandomUtil.nextInt(opponents.size()))); - if (opponent != null) { - ContinuousEffect effect = new AttacksIfAbleTargetPlayerSourceEffect(); - effect.setTargetPointer(new FixedTarget(opponent.getId())); - game.addEffect(effect, source); - return true; - } - } - return false; - } -} - class RavingDeadDamageEffect extends OneShotEffect { public RavingDeadDamageEffect() { @@ -146,44 +113,3 @@ class RavingDeadDamageEffect extends OneShotEffect { return false; } } - -class AttacksIfAbleTargetPlayerSourceEffect extends RequirementEffect { - - public AttacksIfAbleTargetPlayerSourceEffect() { - super(Duration.EndOfTurn); - staticText = "{this} attacks that player this combat if able"; - } - - public AttacksIfAbleTargetPlayerSourceEffect(final AttacksIfAbleTargetPlayerSourceEffect effect) { - super(effect); - } - - @Override - public AttacksIfAbleTargetPlayerSourceEffect copy() { - return new AttacksIfAbleTargetPlayerSourceEffect(this); - } - - @Override - public boolean applies(Permanent permanent, Ability source, Game game) { - if (permanent.getId().equals(source.getSourceId())) { - return true; - } - return false; - } - - @Override - public boolean mustAttack(Game game) { - return true; - } - - @Override - public boolean mustBlock(Game game) { - return false; - } - - @Override - public UUID mustAttackDefender(Ability source, Game game) { - return getTargetPointer().getFirst(game, source); - } - -} diff --git a/Mage/src/main/java/mage/abilities/effects/common/AttacksIfAbleTargetPlayerSourceEffect.java b/Mage/src/main/java/mage/abilities/effects/common/AttacksIfAbleTargetPlayerSourceEffect.java new file mode 100644 index 00000000000..1b49a2efa75 --- /dev/null +++ b/Mage/src/main/java/mage/abilities/effects/common/AttacksIfAbleTargetPlayerSourceEffect.java @@ -0,0 +1,50 @@ +package mage.abilities.effects.common; + +import mage.abilities.Ability; +import mage.abilities.effects.RequirementEffect; +import mage.constants.Duration; +import mage.game.Game; +import mage.game.permanent.Permanent; + +import java.util.UUID; + +public class AttacksIfAbleTargetPlayerSourceEffect extends RequirementEffect { + + public AttacksIfAbleTargetPlayerSourceEffect() { + super(Duration.EndOfTurn); + staticText = "{this} attacks that player this combat if able"; + } + + public AttacksIfAbleTargetPlayerSourceEffect(final AttacksIfAbleTargetPlayerSourceEffect effect) { + super(effect); + } + + @Override + public AttacksIfAbleTargetPlayerSourceEffect copy() { + return new AttacksIfAbleTargetPlayerSourceEffect(this); + } + + @Override + public boolean applies(Permanent permanent, Ability source, Game game) { + if (permanent.getId().equals(source.getSourceId())) { + return true; + } + return false; + } + + @Override + public boolean mustAttack(Game game) { + return true; + } + + @Override + public boolean mustBlock(Game game) { + return false; + } + + @Override + public UUID mustAttackDefender(Ability source, Game game) { + return getTargetPointer().getFirst(game, source); + } + +} diff --git a/Mage/src/main/java/mage/abilities/effects/common/combat/AttackIfAbleTargetRandomOpponentSourceEffect.java b/Mage/src/main/java/mage/abilities/effects/common/combat/AttackIfAbleTargetRandomOpponentSourceEffect.java new file mode 100644 index 00000000000..8d49f4525bc --- /dev/null +++ b/Mage/src/main/java/mage/abilities/effects/common/combat/AttackIfAbleTargetRandomOpponentSourceEffect.java @@ -0,0 +1,49 @@ +package mage.abilities.effects.common.combat; + +import mage.abilities.Ability; +import mage.abilities.effects.ContinuousEffect; +import mage.abilities.effects.OneShotEffect; +import mage.abilities.effects.common.AttacksIfAbleTargetPlayerSourceEffect; +import mage.constants.Outcome; +import mage.game.Game; +import mage.players.Player; +import mage.target.targetpointer.FixedTarget; +import mage.util.RandomUtil; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class AttackIfAbleTargetRandomOpponentSourceEffect extends OneShotEffect { + + public AttackIfAbleTargetRandomOpponentSourceEffect() { + super(Outcome.Benefit); + this.staticText = "choose an opponent at random. {this} attacks that player this combat if able"; + } + + public AttackIfAbleTargetRandomOpponentSourceEffect(final AttackIfAbleTargetRandomOpponentSourceEffect effect) { + super(effect); + } + + @Override + public AttackIfAbleTargetRandomOpponentSourceEffect copy() { + return new AttackIfAbleTargetRandomOpponentSourceEffect(this); + } + + @Override + public boolean apply(Game game, Ability source) { + Player controller = game.getPlayer(source.getControllerId()); + if (controller != null) { + List opponents = new ArrayList<>(); + opponents.addAll(game.getOpponents(controller.getId())); + Player opponent = game.getPlayer(opponents.get(RandomUtil.nextInt(opponents.size()))); + if (opponent != null) { + ContinuousEffect effect = new AttacksIfAbleTargetPlayerSourceEffect(); + effect.setTargetPointer(new FixedTarget(opponent.getId())); + game.addEffect(effect, source); + return true; + } + } + return false; + } +}