Blocks this turn ability.

This commit is contained in:
magenoxx 2012-07-23 02:17:41 +04:00
parent c783cbc41f
commit 1a60504fcc
8 changed files with 307 additions and 14 deletions

View file

@ -146,13 +146,17 @@ class OdricMasterTacticianEffect extends ReplacementEffectImpl<OdricMasterTactic
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
Player player = game.getPlayer(source.getControllerId());
//20101001 - 509.1c
game.getCombat().checkBlockRequirements(player, game);
game.getCombat().checkBlockRequirementsBefore(player, game);
for (UUID defenderId : game.getCombat().getPlayerDefenders(game)) {
game.getPlayer(source.getControllerId()).selectBlockers(game, defenderId);
if (game.isPaused() || game.isGameOver()) {
return true;
boolean choose = true;
while (choose) {
game.getPlayer(source.getControllerId()).selectBlockers(game, defenderId);
if (game.isPaused() || game.isGameOver()) {
return true;
}
game.getCombat().checkBlockRestrictions(game.getPlayer(defenderId), game);
choose = !game.getCombat().checkBlockRequirementsAfter(game.getPlayer(defenderId), player, game);
}
game.getCombat().checkBlockRestrictions(game.getPlayer(defenderId), game);
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, defenderId, defenderId));
}

View file

@ -28,7 +28,6 @@
package mage.abilities.effects;
import java.util.UUID;
import mage.Constants.Duration;
import mage.Constants.EffectType;
import mage.Constants.Outcome;
@ -36,6 +35,8 @@ import mage.abilities.Ability;
import mage.game.Game;
import mage.game.permanent.Permanent;
import java.util.UUID;
/**
*
* @author BetaSteward_at_googlemail.com
@ -62,6 +63,10 @@ public abstract class RequirementEffect<T extends RequirementEffect<T>> extends
public abstract boolean mustBlock(Game game);
public boolean mustBlockAny(Game game) {
return false;
}
public UUID mustAttackDefender(Ability source, Game game) {
return null;
}

View file

@ -0,0 +1,89 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.abilities.effects.common;
import mage.Constants.Duration;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.effects.RequirementEffect;
import mage.game.Game;
import mage.game.permanent.Permanent;
/**
*
* @author magenoxx_at_googlemail.com
*/
public class BlocksIfAbleTargetEffect extends RequirementEffect<BlocksIfAbleTargetEffect> {
public BlocksIfAbleTargetEffect(Duration duration) {
super(duration);
}
public BlocksIfAbleTargetEffect(final BlocksIfAbleTargetEffect effect) {
super(effect);
}
@Override
public BlocksIfAbleTargetEffect copy() {
return new BlocksIfAbleTargetEffect(this);
}
@Override
public boolean applies(Permanent permanent, Ability source, Game game) {
Permanent creature = game.getPermanent(source.getFirstTarget());
if (creature != null && creature.getId().equals(permanent.getId())) {
return true;
}
return false;
}
@Override
public boolean mustAttack(Game game) {
return false;
}
@Override
public boolean mustBlock(Game game) {
return false;
}
@Override
public boolean mustBlockAny(Game game) {
return true;
}
@Override
public String getText(Mode mode) {
if (this.duration == Duration.EndOfTurn)
return "Target " + mode.getTargets().get(0).getTargetName() + " blocks this turn if able";
else
return "Target " + mode.getTargets().get(0).getTargetName() + " blocks each turn if able";
}
}

View file

@ -109,7 +109,6 @@ public class DamageTargetEffect extends OneShotEffect<DamageTargetEffect> {
}
Player player = game.getPlayer(targetPointer.getFirst(game, source));
if (player != null) {
game.informPlayer(player, "You were dealt 3 damage");
player.damage(amount.calculate(game, source), source.getSourceId(), game, false, preventable);
return true;
}

View file

@ -48,6 +48,8 @@ import java.util.UUID;
public class GainAbilityTargetEffect extends ContinuousEffectImpl<GainAbilityTargetEffect> {
protected Ability ability;
protected String rule;
protected boolean hasRule;
public GainAbilityTargetEffect(Ability ability, Duration duration) {
super(duration, Layer.AbilityAddingRemovingEffects_6, SubLayer.NA,
@ -55,9 +57,19 @@ public class GainAbilityTargetEffect extends ContinuousEffectImpl<GainAbilityTar
this.ability = ability;
}
public GainAbilityTargetEffect(Ability ability, Duration duration, String rule) {
super(duration, Layer.AbilityAddingRemovingEffects_6, SubLayer.NA,
ability.getEffects().size() > 0 ? ability.getEffects().get(0).getOutcome() : Outcome.AddAbility);
this.ability = ability;
this.rule = rule;
this.hasRule = true;
}
public GainAbilityTargetEffect(final GainAbilityTargetEffect effect) {
super(effect);
this.ability = effect.ability.copy();
this.rule = effect.rule;
this.hasRule = effect.hasRule;
}
@Override
@ -86,6 +98,9 @@ public class GainAbilityTargetEffect extends ContinuousEffectImpl<GainAbilityTar
@Override
public String getText(Mode mode) {
if (hasRule) {
return rule;
}
StringBuilder sb = new StringBuilder();
Target target = mode.getTargets().get(0);
if(target.getNumberOfTargets() > 1){

View file

@ -0,0 +1,69 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.abilities.keyword;
import mage.Constants.Zone;
import mage.abilities.MageSingleton;
import mage.abilities.StaticAbility;
import java.io.ObjectStreamException;
/**
* This is marker ability that is used only for rule display.
* You should use {@see AttacksIfAbleTargetEffect} for real effect.
*
* @author magenoxx_at_googlemail.com
*/
public class AttacksThisTurnMarkerAbility extends StaticAbility<AttacksThisTurnMarkerAbility> implements MageSingleton {
private static final AttacksThisTurnMarkerAbility fINSTANCE = new AttacksThisTurnMarkerAbility();
private Object readResolve() throws ObjectStreamException {
return fINSTANCE;
}
public static AttacksThisTurnMarkerAbility getInstance() {
return fINSTANCE;
}
private AttacksThisTurnMarkerAbility() {
super(Zone.BATTLEFIELD, null);
}
@Override
public String getRule() {
return "{this} attacks this turn if able";
}
@Override
public AttacksThisTurnMarkerAbility copy() {
return fINSTANCE;
}
}

View file

@ -0,0 +1,69 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.abilities.keyword;
import mage.Constants.Zone;
import mage.abilities.MageSingleton;
import mage.abilities.StaticAbility;
import java.io.ObjectStreamException;
/**
* This is marker ability that is used only for rule display.
* You should use {@see BlocksIfAbleTargetEffect} for real effect.
*
* @author magenoxx_at_googlemail.com
*/
public class BlocksThisTurnMarkerAbility extends StaticAbility<BlocksThisTurnMarkerAbility> implements MageSingleton {
private static final BlocksThisTurnMarkerAbility fINSTANCE = new BlocksThisTurnMarkerAbility();
private Object readResolve() throws ObjectStreamException {
return fINSTANCE;
}
public static BlocksThisTurnMarkerAbility getInstance() {
return fINSTANCE;
}
private BlocksThisTurnMarkerAbility() {
super(Zone.BATTLEFIELD, null);
}
@Override
public String getRule() {
return "{this} blocks this turn if able";
}
@Override
public BlocksThisTurnMarkerAbility copy() {
return fINSTANCE;
}
}

View file

@ -32,6 +32,7 @@ import mage.Constants.Outcome;
import mage.abilities.effects.RequirementEffect;
import mage.abilities.keyword.CantAttackAloneAbility;
import mage.abilities.keyword.VigilanceAbility;
import mage.filter.common.FilterControlledCreaturePermanent;
import mage.filter.common.FilterCreatureForCombat;
import mage.filter.common.FilterPlaneswalkerPermanent;
import mage.game.Game;
@ -195,12 +196,18 @@ public class Combat implements Serializable, Copyable<Combat> {
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_BLOCKERS, attackerId, attackerId))) {
Player player = game.getPlayer(attackerId);
//20101001 - 509.1c
checkBlockRequirements(player, game);
checkBlockRequirementsBefore(player, game);
for (UUID defenderId : getPlayerDefenders(game)) {
game.getPlayer(defenderId).selectBlockers(game, defenderId);
if (game.isPaused() || game.isGameOver())
return;
checkBlockRestrictions(game.getPlayer(defenderId), game);
boolean choose = true;
Player defender = game.getPlayer(defenderId);
while (choose) {
game.getPlayer(defenderId).selectBlockers(game, defenderId);
if (game.isPaused() || game.isGameOver()) {
return;
}
checkBlockRestrictions(game.getPlayer(defenderId), game);
choose = !checkBlockRequirementsAfter(defender, defender, game);
}
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, defenderId, defenderId));
}
}
@ -229,9 +236,8 @@ public class Combat implements Serializable, Copyable<Combat> {
}
}
public void checkBlockRequirements(Player player, Game game) {
public void checkBlockRequirementsBefore(Player player, Game game) {
//20101001 - 509.1c
//TODO: handle case where more than one attacker must be blocked
for (Permanent creature : game.getBattlefield().getActivePermanents(filterBlockers, player.getId(), game)) {
if (game.getOpponents(attackerId).contains(creature.getControllerId())) {
for (RequirementEffect effect : game.getContinuousEffects().getApplicableRequirementEffects(creature, game)) {
@ -247,6 +253,43 @@ public class Combat implements Serializable, Copyable<Combat> {
}
}
public boolean checkBlockRequirementsAfter(Player player, Player controller, Game game) {
//20101001 - 509.1c
for (Permanent creature : game.getBattlefield().getActivePermanents(new FilterControlledCreaturePermanent(), player.getId(), game)) {
if (creature.getBlocking() == 0 && game.getOpponents(attackerId).contains(creature.getControllerId())) {
for (RequirementEffect effect : game.getContinuousEffects().getApplicableRequirementEffects(creature, game)) {
if (effect.mustBlockAny(game)) {
// check that it can block an attacker
boolean mayBlock = false;
for (UUID attackingCreatureId : getAttackers()) {
if (creature.canBlock(attackingCreatureId, game)) {
mayBlock = true;
break;
}
}
if (mayBlock) {
if (controller.isHuman()) {
game.informPlayer(controller, "Creature should block this turn: " + creature.getName());
} else {
Player defender = game.getPlayer(creature.getControllerId());
if (defender != null) {
for (UUID attackingCreatureId : getAttackers()) {
if (creature.canBlock(attackingCreatureId, game)) {
defender.declareBlocker(creature.getId(), attackingCreatureId, game);
break;
}
}
}
}
return false;
}
}
}
}
}
return true;
}
public void setDefenders(Game game) {
Set<UUID> opponents = game.getOpponents(attackerId);
PlayerList players;