From 3dd75d660972e01c1c6842b0b4b4cd00a7cf5fb5 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Tue, 17 Sep 2013 17:25:16 +0200 Subject: [PATCH] Implemented support for MustBeBlockedByAtLeastOne...Effect. (ongoing). --- .../mage/sets/darkascension/DeadlyAllure.java | 7 ++- .../riseoftheeldrazi/IrresistiblePrey.java | 6 +-- ...MustBeBlockedByAtLeastOneSourceEffect.java | 2 +- ...MustBeBlockedByAtLeastOneTargetEffect.java | 5 +- Mage/src/mage/game/combat/Combat.java | 52 ++++++++++++++++++- 5 files changed, 61 insertions(+), 11 deletions(-) diff --git a/Mage.Sets/src/mage/sets/darkascension/DeadlyAllure.java b/Mage.Sets/src/mage/sets/darkascension/DeadlyAllure.java index b91609c981..d9b26b1fb7 100644 --- a/Mage.Sets/src/mage/sets/darkascension/DeadlyAllure.java +++ b/Mage.Sets/src/mage/sets/darkascension/DeadlyAllure.java @@ -30,9 +30,8 @@ package mage.sets.darkascension; import java.util.UUID; import mage.constants.*; -import mage.abilities.common.SimpleStaticAbility; import mage.abilities.costs.mana.ManaCostsImpl; -import mage.abilities.effects.common.combat.MustBeBlockedByAllSourceEffect; +import mage.abilities.effects.common.combat.MustBeBlockedByAtLeastOneTargetEffect; import mage.abilities.effects.common.continious.GainAbilityTargetEffect; import mage.abilities.keyword.DeathtouchAbility; import mage.abilities.keyword.FlashbackAbility; @@ -51,9 +50,9 @@ public class DeadlyAllure extends CardImpl { this.color.setBlack(true); - // Target creature gains deathtouch until end of turn and must be blocked this turn if able. - this.getSpellAbility().addEffect(new GainAbilityTargetEffect(new SimpleStaticAbility(Zone.BATTLEFIELD, new MustBeBlockedByAllSourceEffect()), Duration.EndOfTurn)); + // Target creature gains deathtouch until end of turn and must be blocked this turn if able. this.getSpellAbility().addEffect(new GainAbilityTargetEffect(DeathtouchAbility.getInstance(), Duration.EndOfTurn)); + this.getSpellAbility().addEffect(new MustBeBlockedByAtLeastOneTargetEffect(Duration.EndOfTurn)); this.getSpellAbility().addTarget(new TargetCreaturePermanent()); // Flashback {G} diff --git a/Mage.Sets/src/mage/sets/riseoftheeldrazi/IrresistiblePrey.java b/Mage.Sets/src/mage/sets/riseoftheeldrazi/IrresistiblePrey.java index c3ac237a77..98024272e5 100644 --- a/Mage.Sets/src/mage/sets/riseoftheeldrazi/IrresistiblePrey.java +++ b/Mage.Sets/src/mage/sets/riseoftheeldrazi/IrresistiblePrey.java @@ -28,16 +28,14 @@ package mage.sets.riseoftheeldrazi; import java.util.UUID; - -import mage.constants.CardType; -import mage.constants.Rarity; import mage.abilities.common.SimpleStaticAbility; import mage.abilities.effects.common.DrawCardControllerEffect; -import mage.abilities.effects.common.combat.MustBeBlockedByAllSourceEffect; import mage.abilities.effects.common.combat.MustBeBlockedByAtLeastOneTargetEffect; import mage.abilities.effects.common.continious.GainAbilityTargetEffect; import mage.cards.CardImpl; +import mage.constants.CardType; import mage.constants.Duration; +import mage.constants.Rarity; import mage.constants.Zone; import mage.target.common.TargetCreaturePermanent; diff --git a/Mage/src/mage/abilities/effects/common/combat/MustBeBlockedByAtLeastOneSourceEffect.java b/Mage/src/mage/abilities/effects/common/combat/MustBeBlockedByAtLeastOneSourceEffect.java index 582d2a3241..98fe279dfb 100644 --- a/Mage/src/mage/abilities/effects/common/combat/MustBeBlockedByAtLeastOneSourceEffect.java +++ b/Mage/src/mage/abilities/effects/common/combat/MustBeBlockedByAtLeastOneSourceEffect.java @@ -81,7 +81,7 @@ public class MustBeBlockedByAtLeastOneSourceEffect extends RequirementEffect { public boolean checkBlockRequirementsAfter(Player player, Player controller, Game game) { //20101001 - 509.1c + // check mustBeBlockedByAtLeastOne + Map> mustBeBlockedByAtLeastOne = new HashMap>(); + + // check mustBlockAny for (Permanent creature : game.getBattlefield().getActivePermanents(new FilterControlledCreaturePermanent(), player.getId(), game)) { // Does the creature not block and is an opponent of the attacker if (creature.getBlocking() == 0 && game.getOpponents(attackerId).contains(creature.getControllerId())) { // get all requiremet effects that apply to the creature - for (RequirementEffect effect : game.getContinuousEffects().getApplicableRequirementEffects(creature, game).keySet()) { + for (Map.Entry entry : game.getContinuousEffects().getApplicableRequirementEffects(creature, game).entrySet()) { + RequirementEffect effect = (RequirementEffect)entry.getKey(); + // get possible mustBeBlockedByAtLeastOne blocker + for (Ability ability: (HashSet)entry.getValue()) { + UUID toBeBlockedCreature = effect.mustBlockAttackerIfElseUnblocked(ability, game); + if (toBeBlockedCreature != null) { + Set potentialBlockers; + if (mustBeBlockedByAtLeastOne.containsKey(toBeBlockedCreature)) { + potentialBlockers = mustBeBlockedByAtLeastOne.get(toBeBlockedCreature); + } else { + potentialBlockers = new HashSet(); + mustBeBlockedByAtLeastOne.put(toBeBlockedCreature, potentialBlockers); + } + potentialBlockers.add(creature.getId()); + } + } + // check the mustBlockAny if (effect.mustBlockAny(game)) { // check that it can block at least one of the attackers @@ -376,6 +396,36 @@ public class Combat implements Serializable, Copyable { } } } + + // check mustBeBlockedByAtLeastOne + // TODO: Check if already blocking creatures that block other creatures have no madatory block and have to block here + for (UUID toBeBlockedCreatureId: mustBeBlockedByAtLeastOne.keySet()) { + for (CombatGroup combatGroup : game.getCombat().getGroups()) { + if (combatGroup.getBlockers().isEmpty() && combatGroup.getAttackers().contains(toBeBlockedCreatureId)) { + // creature is not blocked but has possible blockers + if (controller.isHuman()) { + Permanent toBeBlockedCreature = game.getPermanent(toBeBlockedCreatureId); + if (toBeBlockedCreature != null) { + game.informPlayer(controller, new StringBuilder(toBeBlockedCreature.getName()).append(" has to be blocked by at least one creature.").toString()); + } + return false; + } else { + // take the first potential blocker from the set to block for the AI + UUID blockingCreatureId = mustBeBlockedByAtLeastOne.get(toBeBlockedCreatureId).iterator().next(); + Permanent blockingCreature = game.getPermanent(blockingCreatureId); + if (blockingCreature != null) { + Player defender = game.getPlayer(blockingCreature.getControllerId()); + if (defender != null) { + defender.declareBlocker(defender.getId(), blockingCreatureId, toBeBlockedCreatureId, game); + } + } + } + } + } + + } + + return true; }