* Finished to change ReplacementEffects for UNTAP event to ContinuousRuleModifyingEffect.

This commit is contained in:
LevelX2 2014-07-29 17:45:48 +02:00
parent ea1a098300
commit 1f51d243ec
74 changed files with 309 additions and 244 deletions

View file

@ -82,17 +82,17 @@ public class ConditionalContinuousRuleModifyingEffect extends ContinuousRuleModi
}
@Override
public boolean applies(GameEvent event, Ability source, boolean checkPlayableMode, Game game) {
public boolean applies(GameEvent event, Ability source, Game game) {
if (lockedInCondition && !(condition instanceof FixedCondition)) {
condition = new FixedCondition(condition.apply(game, source));
}
conditionState = condition.apply(game, source);
if (conditionState) {
effect.setTargetPointer(this.targetPointer);
return effect.applies(event, source, checkPlayableMode, game);
return effect.applies(event, source, game);
} else if (otherwiseEffect != null) {
otherwiseEffect.setTargetPointer(this.targetPointer);
return otherwiseEffect.applies(event, source, checkPlayableMode, game);
return otherwiseEffect.applies(event, source, game);
}
return false;
}

View file

@ -593,18 +593,31 @@ public class ContinuousEffects implements Serializable {
}
}
/**
* Checks if an event wont't happen because of an rule modifying effect
*
* @param event
* @param game
* @param checkPlayableMode true if the event does not really happen but it#s checked if the event would be replaced
* @return
*/
public boolean preventedByRuleModification(GameEvent event, Game game, boolean checkPlayableMode) {
for (ContinuousRuleModifiyingEffect effect: continuousRuleModifyingEffects) {
for (Ability ability : continuousRuleModifyingEffects.getAbility(effect.getId())) {
if (!(ability instanceof StaticAbility) || ability.isInUseableZone(game, null, false)) {
if (effect.getDuration() != Duration.OneUse || !effect.isUsed()) {
if (effect.applies(event, ability, checkPlayableMode, game)) {
if (effect.applies(event, ability, game)) {
if (!checkPlayableMode) {
String message = effect.getInfoMessage(ability, game);
String message = effect.getInfoMessage(ability, event, game);
if (message != null && !message.isEmpty()) {
Player player = game.getPlayer(event.getPlayerId());
if (player != null) {
game.informPlayer(player, message);
if (effect.sendMessageToUser()) {
Player player = game.getPlayer(event.getPlayerId());
if (player != null) {
game.informPlayer(player, message);
}
}
if (effect.sendMessageToGameLog()) {
game.informPlayers(message);
}
}
}

View file

@ -42,18 +42,33 @@ public interface ContinuousRuleModifiyingEffect extends ContinuousEffect {
*
* @param event the event to check if it may happen
* @param source the ability of the effect
* @param checkPlayableMode is the call for checking playables or to prevent a real event
* @param game the game
* @return
*/
boolean applies(GameEvent event, Ability source, boolean checkPlayableMode, Game game);
boolean applies(GameEvent event, Ability source, Game game);
/**
* Defines if the user should get a message about the rule modifying effect
* if he was applied
*
* @return true if user should be informed
*/
boolean sendMessageToUser();
/**
* Defines if the a message should be send to game log about the rule modifying effect
* if he was applied
*
* @return true if message should go to game log
*/
boolean sendMessageToGameLog();
/**
* Returns a message text that informs the player why he can't do something.
*
* @param source the ability of the effect
* @param event
* @param game the game
* @return
*/
String getInfoMessage(Ability source, Game game);
String getInfoMessage(Ability source, GameEvent event, Game game);
}

View file

@ -34,6 +34,7 @@ import mage.constants.Duration;
import mage.constants.EffectType;
import mage.constants.Outcome;
import mage.game.Game;
import mage.game.events.GameEvent;
/**
*
@ -41,6 +42,8 @@ import mage.game.Game;
*/
public abstract class ContinuousRuleModifiyingEffectImpl extends ContinuousEffectImpl implements ContinuousRuleModifiyingEffect {
protected final boolean messageToUser;
protected final boolean messageToGameLog;
protected final String infoMessage;
// 613.10
@ -57,18 +60,26 @@ public abstract class ContinuousRuleModifiyingEffectImpl extends ContinuousEffec
// But player isn't asked to choose order of effects if multiple are applied to the same event.
public ContinuousRuleModifiyingEffectImpl(Duration duration, Outcome outcome) {
this(duration, outcome, true, false);
}
public ContinuousRuleModifiyingEffectImpl(Duration duration, Outcome outcome, boolean messageToUser, boolean messageToLog) {
super(duration, outcome);
this.effectType = EffectType.CONTINUOUS_RULE_MODIFICATION;
this.infoMessage = null;
this.messageToUser = messageToUser;
this.messageToGameLog = messageToLog;
}
public ContinuousRuleModifiyingEffectImpl(final ContinuousRuleModifiyingEffectImpl effect) {
super(effect);
this.infoMessage = effect.infoMessage;
this.messageToUser = effect.messageToUser;
this.messageToGameLog = effect.messageToGameLog;
}
@Override
public String getInfoMessage(Ability source, Game game) {
public String getInfoMessage(Ability source, GameEvent event, Game game) {
if (infoMessage == null) {
String message;
MageObject object = game.getObject(source.getSourceId());
@ -83,4 +94,15 @@ public abstract class ContinuousRuleModifiyingEffectImpl extends ContinuousEffec
}
}
@Override
public boolean sendMessageToUser() {
return messageToUser;
}
@Override
public boolean sendMessageToGameLog() {
return messageToGameLog;
}
}

View file

@ -62,7 +62,7 @@ public class CantActivateAbilitiesAttachedEffect extends ContinuousRuleModifiyin
}
@Override
public boolean applies(GameEvent event, Ability source, boolean checkPlayableMode, Game game) {
public boolean applies(GameEvent event, Ability source, Game game) {
if (event.getType() == GameEvent.EventType.ACTIVATE_ABILITY) {
Permanent enchantment = game.getPermanent(source.getSourceId());
if (enchantment != null && enchantment.getAttachedTo() != null) {

View file

@ -81,7 +81,7 @@ public class CantCounterControlledEffect extends ContinuousRuleModifiyingEffectI
}
@Override
public boolean applies(GameEvent event, Ability source, boolean checkPlayableMode, Game game) {
public boolean applies(GameEvent event, Ability source, Game game) {
if (event.getType() == EventType.COUNTER) {
Spell spell = game.getStack().getSpell(event.getTargetId());
if (spell != null && spell.getControllerId().equals(source.getControllerId())

View file

@ -64,7 +64,7 @@ public class CantCounterSourceEffect extends ContinuousRuleModifiyingEffectImpl
}
@Override
public boolean applies(GameEvent event, Ability source, boolean checkPlayableMode, Game game) {
public boolean applies(GameEvent event, Ability source, Game game) {
if (event.getType() == EventType.COUNTER) {
Card card = game.getCard(source.getSourceId());
if (card != null) {

View file

@ -82,7 +82,7 @@ public class CantTargetEffect extends ContinuousRuleModifiyingEffectImpl {
}
@Override
public boolean applies(GameEvent event, Ability source, boolean checkPlayableMode, Game game) {
public boolean applies(GameEvent event, Ability source, Game game) {
if (event.getType() == EventType.TARGET) {
Permanent permanent = game.getPermanent(event.getTargetId());
if (permanent != null && filterTarget.match(permanent, source.getSourceId(), source.getControllerId(), game)) {

View file

@ -68,7 +68,7 @@ public class CantTargetSourceEffect extends ContinuousRuleModifiyingEffectImpl {
}
@Override
public boolean applies(GameEvent event, Ability source, boolean checkPlayableMode, Game game) {
public boolean applies(GameEvent event, Ability source, Game game) {
if (event.getType() == EventType.TARGET && event.getTargetId().equals(source.getSourceId())) {
StackObject sourceObject = game.getStack().getStackObject(event.getSourceId());
if (sourceObject != null && filterSource.match(sourceObject, source.getControllerId(), game)) {

View file

@ -101,7 +101,7 @@ class EpicReplacementEffect extends ContinuousRuleModifiyingEffectImpl {
}
@Override
public String getInfoMessage(Ability source, Game game) {
public String getInfoMessage(Ability source, GameEvent event, Game game) {
MageObject mageObject = game.getObject(source.getSourceId());
if (mageObject != null) {
return "For the rest of the game, you can't cast spells (Epic - " + mageObject.getName() +")";
@ -110,7 +110,7 @@ class EpicReplacementEffect extends ContinuousRuleModifiyingEffectImpl {
}
@Override
public boolean applies(GameEvent event, Ability source, boolean checkPlayableMode, Game game) {
public boolean applies(GameEvent event, Ability source, Game game) {
if (event.getType() == GameEvent.EventType.CAST_SPELL
&& source.getControllerId() == event.getPlayerId()) {
MageObject object = game.getObject(event.getSourceId());

View file

@ -15,7 +15,7 @@ import mage.game.permanent.Permanent;
public class SkipEnchantedUntapEffect extends ContinuousRuleModifiyingEffectImpl {
public SkipEnchantedUntapEffect() {
super(Duration.WhileOnBattlefield, Outcome.Detriment);
super(Duration.WhileOnBattlefield, Outcome.Detriment, false, true);
staticText = "Enchanted permanent doesn't untap during its controller's untap step";
}
@ -34,7 +34,20 @@ public class SkipEnchantedUntapEffect extends ContinuousRuleModifiyingEffectImpl
}
@Override
public boolean applies(GameEvent event, Ability source, boolean checkPlayableMode, Game game) {
public String getInfoMessage(Ability source, GameEvent event, Game game) {
Permanent enchantment = game.getPermanent(source.getSourceId());
if (enchantment != null && enchantment.getAttachedTo() != null) {
Permanent enchanted = game.getPermanent(enchantment.getAttachedTo());
if (enchanted != null) {
return enchanted.getLogName() + " doesn't untap during its controller's untap step (" + enchantment.getLogName() + ")";
}
}
return null;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
if (game.getTurn().getStepType() == PhaseStep.UNTAP && event.getType() == GameEvent.EventType.UNTAP) {
Permanent enchantment = game.getPermanent(source.getSourceId());
if (enchantment != null && enchantment.getAttachedTo() != null) {

View file

@ -1,5 +1,6 @@
package mage.abilities.effects.common;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.effects.ContinuousRuleModifiyingEffectImpl;
import mage.constants.Duration;
@ -13,7 +14,7 @@ public class SkipNextUntapSourceEffect extends ContinuousRuleModifiyingEffectImp
private int validForTurnNum;
public SkipNextUntapSourceEffect() {
super(Duration.Custom, Outcome.Detriment);
super(Duration.Custom, Outcome.Detriment, false, true);
staticText = "{this} doesn't untap during your next untap step";
validForTurnNum = 0;
}
@ -33,21 +34,40 @@ public class SkipNextUntapSourceEffect extends ContinuousRuleModifiyingEffectImp
}
@Override
public boolean applies(GameEvent event, Ability source, boolean checkPlayableMode, Game game) {
public String getInfoMessage(Ability source, GameEvent event, Game game) {
MageObject mageObject = game.getObject(source.getSourceId());
if (mageObject != null) {
return "{this} doesn't untap (" + mageObject.getLogName() + ")";
}
return null;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
// the check for turn number is needed if multiple effects are added to prevent untap in next untap step
// if we don't check for turn number, every turn only one effect would be used instead of correctly only one time
// to skip the untap effect.
// Discard effect if related to previous turn
if (validForTurnNum > 0 && validForTurnNum < game.getTurnNum()) {
discard();
return false;
}
// remember the turn of the untap step the effect has to be applied
if (GameEvent.EventType.UNTAP_STEP.equals(event.getType())
&& game.getActivePlayerId().equals(source.getControllerId())) {
&& game.getActivePlayerId().equals(source.getControllerId())) {
if (validForTurnNum == game.getTurnNum()) { // the turn has a secon untap step but the effect is already related to the first untap step
discard();
return false;
}
validForTurnNum = game.getTurnNum();
}
// skip untap action
if (game.getTurn().getStepType() == PhaseStep.UNTAP
&& event.getType() == GameEvent.EventType.UNTAP
&& event.getTargetId().equals(source.getSourceId())) {
if (!checkPlayableMode) {
discard();
}
return true;
}
return false;

View file

@ -28,12 +28,11 @@
package mage.abilities.effects.common;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.effects.ReplacementEffectImpl;
import mage.abilities.effects.ContinuousRuleModifiyingEffectImpl;
import mage.constants.Duration;
import mage.constants.Outcome;
import mage.constants.PhaseStep;
@ -45,13 +44,17 @@ import mage.game.permanent.Permanent;
/**
* @author BetaSteward_at_googlemail.com
*/
public class SkipNextUntapTargetEffect extends ReplacementEffectImpl {
public class SkipNextUntapTargetEffect extends ContinuousRuleModifiyingEffectImpl {
protected Set<UUID> usedFor = new HashSet<>();
protected int count;
private int validForTurnNum;
/**
* Attention: This effect won't work with targets controlled by different controllers
* If this is needed, the validForTurnNum has to be saved per controller.
*
*/
public SkipNextUntapTargetEffect() {
super(Duration.OneUse, Outcome.Detriment);
super(Duration.Custom, Outcome.Detriment, false, true);
}
public SkipNextUntapTargetEffect(String text) {
@ -61,10 +64,7 @@ public class SkipNextUntapTargetEffect extends ReplacementEffectImpl {
public SkipNextUntapTargetEffect(final SkipNextUntapTargetEffect effect) {
super(effect);
for (UUID uuid : effect.usedFor) {
this.usedFor.add(uuid);
}
this.count = effect.count;
this.validForTurnNum = effect.validForTurnNum;
}
@Override
@ -78,32 +78,53 @@ public class SkipNextUntapTargetEffect extends ReplacementEffectImpl {
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
// not clear how to turn off the effect for more than one target
// especially as some targets may leave the battlefield since the effect creation
// so handling this in applies method is the only option for now for such cases
if (usedFor.size() >= targetPointer.getTargets(game, source).size()) {
// this won't work for targets disappeared before applies() return true
used = true;
public String getInfoMessage(Ability source, GameEvent event, Game game) {
MageObject mageObject = game.getObject(source.getSourceId());
Permanent permanentToUntap = game.getPermanent((event.getTargetId()));
if (permanentToUntap != null && mageObject != null) {
return permanentToUntap.getLogName() + " doesn't untap (" + mageObject.getLogName() + ")";
}
return true;
return null;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
// the check for turn number is needed if multiple effects are added to prevent untap in next untap step
// if we don't check for turn number, every turn only one effect would be used instead of correctly only one time
// to skip the untap effect.
// Discard effect if related to previous turn
if (validForTurnNum > 0 && validForTurnNum < game.getTurnNum()) {
discard();
return false;
}
// remember the turn of the untap step the effect has to be applied
if (GameEvent.EventType.UNTAP_STEP.equals(event.getType())) {
UUID controllerId = null;
for(UUID targetId : getTargetPointer().getTargets(game, source)) {
Permanent permanent = game.getPermanent(targetId);
if (permanent != null) {
controllerId = permanent.getControllerId();
}
}
if (controllerId == null) { // no more targets on the battlefield, effect can be discarded
discard();
return false;
}
if (game.getActivePlayerId().equals(controllerId)) {
if (validForTurnNum == game.getTurnNum()) { // the turn has a second untap step but the effect is already related to the first untap step
discard();
return false;
}
validForTurnNum = game.getTurnNum();
}
}
if (game.getTurn().getStepType() == PhaseStep.UNTAP && event.getType() == EventType.UNTAP) {
if (targetPointer.getTargets(game, source).contains(event.getTargetId())
&& !usedFor.contains(event.getTargetId())) {
Permanent permanent = game.getPermanent(event.getTargetId());
if (permanent == null) {
usedFor.add(event.getTargetId());
return false;
}
if (permanent.getControllerId().equals(game.getActivePlayerId())) {
usedFor.add(event.getTargetId());
return true;
}
if (targetPointer.getTargets(game, source).contains(event.getTargetId())) {
return true;
}
}
return false;

View file

@ -30,7 +30,7 @@ package mage.abilities.effects.common;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.effects.ReplacementEffectImpl;
import mage.abilities.effects.ContinuousRuleModifiyingEffectImpl;
import mage.constants.Duration;
import mage.constants.Outcome;
import mage.constants.PhaseStep;
@ -51,13 +51,13 @@ import mage.players.Player;
* @author LevelX2
*/
public class SkipUntapAllEffect extends ReplacementEffectImpl {
public class SkipUntapAllEffect extends ContinuousRuleModifiyingEffectImpl {
TargetController targetController;
FilterPermanent filter;
public SkipUntapAllEffect(Duration duration, TargetController targetController, FilterPermanent filter) {
super(duration, Outcome.Detriment);
super(duration, Outcome.Detriment, false, false);
this.targetController = targetController;
this.filter = filter;
}
@ -78,12 +78,6 @@ public class SkipUntapAllEffect extends ReplacementEffectImpl {
return false;
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
return true;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
if (game.getTurn().getStepType() == PhaseStep.UNTAP && event.getType() == EventType.UNTAP) {

View file

@ -31,6 +31,7 @@ import mage.constants.Duration;
import mage.constants.Outcome;
import mage.constants.PhaseStep;
import mage.abilities.Ability;
import mage.abilities.effects.ContinuousRuleModifiyingEffectImpl;
import mage.abilities.effects.ReplacementEffectImpl;
import mage.game.Game;
import mage.game.events.GameEvent;
@ -40,10 +41,10 @@ import mage.game.events.GameEvent.EventType;
*
* @author North
*/
public class SkipUntapSourceEffect extends ReplacementEffectImpl {
public class SkipUntapSourceEffect extends ContinuousRuleModifiyingEffectImpl {
public SkipUntapSourceEffect() {
super(Duration.WhileOnBattlefield, Outcome.Detriment);
super(Duration.WhileOnBattlefield, Outcome.Detriment, false, true);
staticText = "{this} doesn't untap during your untap step";
}
@ -61,11 +62,6 @@ public class SkipUntapSourceEffect extends ReplacementEffectImpl {
return false;
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
return true;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
if (game.getTurn().getStepType() == PhaseStep.UNTAP

View file

@ -29,9 +29,10 @@
package mage.abilities.effects.common;
import java.util.UUID;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.effects.ReplacementEffectImpl;
import mage.abilities.effects.ContinuousRuleModifiyingEffectImpl;
import mage.constants.Duration;
import mage.constants.Outcome;
import mage.constants.PhaseStep;
@ -41,9 +42,10 @@ import mage.game.events.GameEvent.EventType;
import mage.game.permanent.Permanent;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class SkipUntapTargetEffect extends ReplacementEffectImpl {
public class SkipUntapTargetEffect extends ContinuousRuleModifiyingEffectImpl {
public SkipUntapTargetEffect(Duration duration) {
super(duration, Outcome.Detriment);
@ -64,11 +66,15 @@ public class SkipUntapTargetEffect extends ReplacementEffectImpl {
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
return true;
public String getInfoMessage(Ability source, GameEvent event, Game game) {
MageObject mageObject = game.getObject(source.getSourceId());
Permanent permanentToUntap = game.getPermanent((event.getTargetId()));
if (permanentToUntap != null && mageObject != null) {
return permanentToUntap.getLogName() + " doesn't untap (" + mageObject.getLogName() + ")";
}
return null;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
if (game.getTurn().getStepType() == PhaseStep.UNTAP && event.getType() == EventType.UNTAP) {

View file

@ -72,7 +72,7 @@ public class CantCastMoreThanOneSpellEffect extends ContinuousRuleModifiyingEffe
}
@Override
public boolean applies(GameEvent event, Ability source, boolean checkPlayableMode, Game game) {
public boolean applies(GameEvent event, Ability source, Game game) {
if (event.getType() == GameEvent.EventType.CAST_SPELL) {
switch (targetController) {
case YOU:

View file

@ -49,12 +49,12 @@ class SplitSecondEffect extends ContinuousRuleModifiyingEffectImpl {
}
@Override
public String getInfoMessage(Ability source, Game game) {
public String getInfoMessage(Ability source, GameEvent event, Game game) {
return "You can't cast spells or activate abilities that aren't mana abilities (Split second).";
}
@Override
public boolean applies(GameEvent event, Ability source, boolean checkPlayableMode, Game game) {
public boolean applies(GameEvent event, Ability source, Game game) {
if (event.getType() == GameEvent.EventType.CAST_SPELL) {
return true;
}