diff --git a/Mage.Sets/src/mage/cards/m/MaddeningHex.java b/Mage.Sets/src/mage/cards/m/MaddeningHex.java new file mode 100644 index 0000000000..9d19bcca30 --- /dev/null +++ b/Mage.Sets/src/mage/cards/m/MaddeningHex.java @@ -0,0 +1,139 @@ +package mage.cards.m; + +import mage.abilities.Ability; +import mage.abilities.TriggeredAbilityImpl; +import mage.abilities.effects.OneShotEffect; +import mage.abilities.effects.common.AttachEffect; +import mage.abilities.keyword.EnchantAbility; +import mage.cards.CardImpl; +import mage.cards.CardSetInfo; +import mage.constants.CardType; +import mage.constants.Outcome; +import mage.constants.SubType; +import mage.constants.Zone; +import mage.game.Game; +import mage.game.events.GameEvent; +import mage.game.permanent.Permanent; +import mage.game.stack.Spell; +import mage.players.Player; +import mage.target.TargetPlayer; +import mage.target.targetpointer.FixedTarget; +import mage.util.RandomUtil; + +import java.util.Set; +import java.util.UUID; + +/** + * @author TheElk801 + */ +public final class MaddeningHex extends CardImpl { + + public MaddeningHex(UUID ownerId, CardSetInfo setInfo) { + super(ownerId, setInfo, new CardType[]{CardType.ENCHANTMENT}, "{1}{R}{R}"); + + this.subtype.add(SubType.AURA); + this.subtype.add(SubType.CURSE); + + // Enchant player + TargetPlayer auraTarget = new TargetPlayer(); + this.getSpellAbility().addTarget(auraTarget); + this.getSpellAbility().addEffect(new AttachEffect(Outcome.BoostCreature)); + Ability ability = new EnchantAbility(auraTarget.getTargetName()); + this.addAbility(ability); + + // Whenever enchanted player casts a noncreature spell, roll a d6. Maddening Hex deals damage to that player equal to the result. Then attach Maddening Hex to another one of your opponents chosen at random. + this.addAbility(new MaddeningHexTriggeredAbility()); + } + + private MaddeningHex(final MaddeningHex card) { + super(card); + } + + @Override + public MaddeningHex copy() { + return new MaddeningHex(this); + } +} + +class MaddeningHexTriggeredAbility extends TriggeredAbilityImpl { + + public MaddeningHexTriggeredAbility() { + super(Zone.BATTLEFIELD, new MaddeningHexEffect()); + } + + private MaddeningHexTriggeredAbility(final MaddeningHexTriggeredAbility ability) { + super(ability); + } + + @Override + public boolean checkEventType(GameEvent event, Game game) { + return event.getType() == GameEvent.EventType.SPELL_CAST; + } + + @Override + public boolean checkTrigger(GameEvent event, Game game) { + Spell spell = game.getSpell(event.getTargetId()); + Permanent permanent = getSourcePermanentIfItStillExists(game); + if (spell == null + || permanent == null + || spell.isCreature(game) + || !event.getPlayerId().equals(permanent.getAttachedTo())) { + return false; + } + this.getEffects().setTargetPointer(new FixedTarget(event.getPlayerId())); + return true; + } + + @Override + public MaddeningHexTriggeredAbility copy() { + return new MaddeningHexTriggeredAbility(this); + } + + @Override + public String getRule() { + return "Whenever enchanted player casts a noncreature spell, roll a d6. " + + "{this} deals damage to that player equal to the result. " + + "Then attach {this} to another one of your opponents chosen at random."; + } +} + +class MaddeningHexEffect extends OneShotEffect { + + MaddeningHexEffect() { + super(Outcome.Benefit); + } + + private MaddeningHexEffect(final MaddeningHexEffect effect) { + super(effect); + } + + @Override + public MaddeningHexEffect copy() { + return new MaddeningHexEffect(this); + } + + @Override + public boolean apply(Game game, Ability source) { + Player controller = game.getPlayer(source.getControllerId()); + if (controller == null) { + return false; + } + int result = controller.rollDice(source, game, 6); + Player player = game.getPlayer(getTargetPointer().getFirst(game, source)); + if (player != null) { + player.damage(result, source.getSourceId(), source, game); + } + Permanent permanent = source.getSourcePermanentIfItStillExists(game); + if (permanent == null) { + return true; + } + Set opponents = game.getOpponents(source.getControllerId()); + if (player != null) { + opponents.remove(player.getId()); + } + if (!opponents.isEmpty()) { + permanent.attachTo(RandomUtil.randomFromSet(opponents), source, game); + } + return true; + } +} diff --git a/Mage.Sets/src/mage/sets/ForgottenRealmsCommander.java b/Mage.Sets/src/mage/sets/ForgottenRealmsCommander.java index 4a347af8c1..ef0329a71e 100644 --- a/Mage.Sets/src/mage/sets/ForgottenRealmsCommander.java +++ b/Mage.Sets/src/mage/sets/ForgottenRealmsCommander.java @@ -141,6 +141,7 @@ public final class ForgottenRealmsCommander extends ExpansionSet { cards.add(new SetCardInfo("Lightning Greaves", 331, Rarity.UNCOMMON, mage.cards.l.LightningGreaves.class)); cards.add(new SetCardInfo("Loyal Apprentice", 132, Rarity.UNCOMMON, mage.cards.l.LoyalApprentice.class)); cards.add(new SetCardInfo("Lumbering Falls", 247, Rarity.RARE, mage.cards.l.LumberingFalls.class)); + cards.add(new SetCardInfo("Maddening Hex", 32, Rarity.RARE, mage.cards.m.MaddeningHex.class)); cards.add(new SetCardInfo("Magmaquake", 133, Rarity.RARE, mage.cards.m.Magmaquake.class)); cards.add(new SetCardInfo("Marionette Master", 102, Rarity.RARE, mage.cards.m.MarionetteMaster.class)); cards.add(new SetCardInfo("Masterwork of Ingenuity", 209, Rarity.RARE, mage.cards.m.MasterworkOfIngenuity.class)); diff --git a/Mage/src/main/java/mage/util/RandomUtil.java b/Mage/src/main/java/mage/util/RandomUtil.java index 37f6d78eb9..f1febad725 100644 --- a/Mage/src/main/java/mage/util/RandomUtil.java +++ b/Mage/src/main/java/mage/util/RandomUtil.java @@ -2,6 +2,8 @@ package mage.util; import java.awt.*; import java.util.Random; +import java.util.Set; +import java.util.UUID; /** * Created by IGOUDT on 5-9-2016. @@ -40,4 +42,19 @@ public final class RandomUtil { public static void setSeed(long newSeed) { random.setSeed(newSeed); } + + public static UUID randomFromSet(Set uuids) { + if (uuids.size() < 2) { + return uuids.stream().findFirst().orElse(null); + } + int rand = nextInt(uuids.size()); + int count = 0; + for (UUID currentId : uuids) { + if (count == rand) { + return currentId; + } + count++; + } + return null; + } }