Make the handling of counters more consistent.

This dramatically reduces the code duplication around counters handling.
This commit is contained in:
Samuel Sandeen 2016-09-03 16:52:41 -04:00
parent 148f633672
commit bec11804f5
11 changed files with 116 additions and 134 deletions

View file

@ -0,0 +1,73 @@
/*
* 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.sets.commander;
import java.util.UUID;
import mage.MageInt;
import mage.abilities.common.DealsCombatDamageToAPlayerTriggeredAbility;
import mage.abilities.effects.common.counter.AddCountersSourceEffect;
import mage.abilities.keyword.HasteAbility;
import mage.cards.CardImpl;
import mage.cards.repository.CardRepository;
import mage.constants.CardType;
import mage.constants.Rarity;
import mage.counters.CounterType;
/**
*
* @author anonymous
*/
public class SkullbriarTheWalkingGrave extends CardImpl {
public SkullbriarTheWalkingGrave(UUID ownerId) {
super(ownerId, 227, "Skullbriar, the Walking Grave", Rarity.RARE, new CardType[]{CardType.CREATURE}, "{B}{G}");
this.expansionSetCode = "CMD";
this.supertype.add("Legendary");
this.subtype.add("Zombie");
this.subtype.add("Elemental");
this.power = new MageInt(1);
this.toughness = new MageInt(1);
// Haste
this.addAbility(HasteAbility.getInstance());
// Whenever Skullbriar, the Walking Grave deals combat damage to a player, put a +1/+1 counter on it.
this.addAbility(new DealsCombatDamageToAPlayerTriggeredAbility(
new AddCountersSourceEffect(CounterType.P1P1.createInstance()), false));
// Counters remain on Skullbriar as it moves to any zone other than a player's hand or library.
}
public SkullbriarTheWalkingGrave(final SkullbriarTheWalkingGrave card) {
super(card);
}
@Override
public SkullbriarTheWalkingGrave copy() {
return new SkullbriarTheWalkingGrave(this);
}
}

View file

@ -23,7 +23,7 @@ public class CountersSourceCount implements DynamicValue {
public int calculate(Game game, Ability sourceAbility, Effect effect) { public int calculate(Game game, Ability sourceAbility, Effect effect) {
Permanent permanent = game.getPermanentOrLKIBattlefield(sourceAbility.getSourceId()); Permanent permanent = game.getPermanentOrLKIBattlefield(sourceAbility.getSourceId());
if (permanent != null) { if (permanent != null) {
return permanent.getCounters().getCount(counter); return permanent.getCounters(game).getCount(counter);
} }
return 0; return 0;
} }

View file

@ -145,7 +145,9 @@ public class AddCountersTargetEffect extends OneShotEffect {
sb.append("s"); sb.append("s");
} }
sb.append(" on "); sb.append(" on ");
Target target = mode.getTargets().get(0); Target target = mode.getTargets().get(0);
if (target.getNumberOfTargets() == 0) { if (target.getNumberOfTargets() == 0) {
sb.append("up to "); sb.append("up to ");

View file

@ -155,10 +155,6 @@ public interface Card extends MageObject {
Counters getCounters(GameState state); Counters getCounters(GameState state);
boolean addCounters(String name, int amount, Game game);
boolean addCounters(String name, int amount, Game game, ArrayList<UUID> appliedEffects);
boolean addCounters(Counter counter, Game game); boolean addCounters(Counter counter, Game game);
boolean addCounters(Counter counter, Game game, ArrayList<UUID> appliedEffects); boolean addCounters(Counter counter, Game game, ArrayList<UUID> appliedEffects);

View file

@ -664,31 +664,11 @@ public abstract class CardImpl extends MageObjectImpl implements Card {
return state.getCardState(this.objectId).getCounters(); return state.getCardState(this.objectId).getCounters();
} }
@Override /**
public boolean addCounters(String name, int amount, Game game) { * @return The controller if available otherwise the owner.
return addCounters(name, amount, game, null); */
} protected UUID getControllerOrOwner() {
return ownerId;
@Override
public boolean addCounters(String name, int amount, Game game, ArrayList<UUID> appliedEffects) {
boolean returnCode = true;
GameEvent countersEvent = GameEvent.getEvent(GameEvent.EventType.ADD_COUNTERS, objectId, ownerId, name, amount);
countersEvent.setAppliedEffects(appliedEffects);
if (!game.replaceEvent(countersEvent)) {
for (int i = 0; i < countersEvent.getAmount(); i++) {
GameEvent event = GameEvent.getEvent(GameEvent.EventType.ADD_COUNTER, objectId, ownerId, name, 1);
event.setAppliedEffects(appliedEffects);
if (!game.replaceEvent(event)) {
getCounters(game).addCounter(name, 1);
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.COUNTER_ADDED, objectId, ownerId, name, 1));
} else {
returnCode = false;
}
}
} else {
returnCode = false;
}
return returnCode;
} }
@Override @Override
@ -699,22 +679,27 @@ public abstract class CardImpl extends MageObjectImpl implements Card {
@Override @Override
public boolean addCounters(Counter counter, Game game, ArrayList<UUID> appliedEffects) { public boolean addCounters(Counter counter, Game game, ArrayList<UUID> appliedEffects) {
boolean returnCode = true; boolean returnCode = true;
GameEvent countersEvent = GameEvent.getEvent(GameEvent.EventType.ADD_COUNTERS, objectId, ownerId, counter.getName(), counter.getCount()); GameEvent countersEvent = GameEvent.getEvent(GameEvent.EventType.ADD_COUNTERS, objectId, getControllerOrOwner(), counter.getName(), counter.getCount());
countersEvent.setAppliedEffects(appliedEffects); countersEvent.setAppliedEffects(appliedEffects);
if (!game.replaceEvent(countersEvent)) { if (!game.replaceEvent(countersEvent)) {
int amount = countersEvent.getAmount(); int amount = countersEvent.getAmount();
int finalAmount = amount;
for (int i = 0; i < amount; i++) { for (int i = 0; i < amount; i++) {
Counter eventCounter = counter.copy(); Counter eventCounter = counter.copy();
eventCounter.remove(amount - 1); eventCounter.remove(eventCounter.getCount() - 1);
GameEvent event = GameEvent.getEvent(GameEvent.EventType.ADD_COUNTER, objectId, ownerId, counter.getName(), 1); GameEvent event = GameEvent.getEvent(GameEvent.EventType.ADD_COUNTER, objectId, getControllerOrOwner(), counter.getName(), 1);
event.setAppliedEffects(appliedEffects); event.setAppliedEffects(appliedEffects);
if (!game.replaceEvent(event)) { if (!game.replaceEvent(event)) {
getCounters(game).addCounter(eventCounter); getCounters(game).addCounter(eventCounter);
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.COUNTER_ADDED, objectId, ownerId, counter.getName(), 1)); game.fireEvent(GameEvent.getEvent(GameEvent.EventType.COUNTER_ADDED, objectId, getControllerOrOwner(), counter.getName(), 1));
} else { } else {
finalAmount--;
returnCode = false; returnCode = false;
} }
} }
if(finalAmount > 0) {
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.COUNTERS_ADDED, objectId, getControllerOrOwner(), counter.getName(), amount));
}
} else { } else {
returnCode = false; returnCode = false;
} }
@ -725,7 +710,7 @@ public abstract class CardImpl extends MageObjectImpl implements Card {
public void removeCounters(String name, int amount, Game game) { public void removeCounters(String name, int amount, Game game) {
for (int i = 0; i < amount; i++) { for (int i = 0; i < amount; i++) {
getCounters(game).removeCounter(name, 1); getCounters(game).removeCounter(name, 1);
GameEvent event = GameEvent.getEvent(GameEvent.EventType.COUNTER_REMOVED, objectId, ownerId); GameEvent event = GameEvent.getEvent(GameEvent.EventType.COUNTER_REMOVED, objectId, getControllerOrOwner());
event.setData(name); event.setData(name);
game.fireEvent(event); game.fireEvent(event);
} }

View file

@ -346,21 +346,4 @@ public abstract class MeldCard extends CardImpl {
return returnState; return returnState;
} }
} }
@Override
public boolean addCounters(String name, int amount, Game game, ArrayList<UUID> appliedEffects) {
if (this.isMelded()) {
return super.addCounters(name, amount, game, appliedEffects);
} else {
// can this really happen?
boolean returnState = true;
if (topLastZoneChangeCounter == topHalfCard.getZoneChangeCounter(game)) {
returnState |= topHalfCard.addCounters(name, amount, game, appliedEffects);
}
if (bottomLastZoneChangeCounter == bottomHalfCard.getZoneChangeCounter(game)) {
returnState |= bottomHalfCard.addCounters(name, amount, game, appliedEffects);
}
return returnState;
}
}
} }

View file

@ -104,8 +104,6 @@ public interface Permanent extends Card, Controllable {
void setSecondCardFace(Card card); void setSecondCardFace(Card card);
Counters getCounters();
List<UUID> getAttachments(); List<UUID> getAttachments();
UUID getAttachedTo(); UUID getAttachedTo();

View file

@ -323,11 +323,6 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
getAbilities().clear(); getAbilities().clear();
} }
@Override
public Counters getCounters() {
return counters;
}
@Override @Override
public Counters getCounters(Game game) { public Counters getCounters(Game game) {
return counters; return counters;
@ -339,44 +334,8 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
} }
@Override @Override
public boolean addCounters(Counter counter, Game game, ArrayList<UUID> appliedEffects) { protected UUID getControllerOrOwner() {
boolean returnCode = true; return controllerId;
GameEvent countersEvent = GameEvent.getEvent(GameEvent.EventType.ADD_COUNTERS, objectId, controllerId, counter.getName(), counter.getCount());
countersEvent.setAppliedEffects(appliedEffects);
if (!game.replaceEvent(countersEvent)) {
int amount = countersEvent.getAmount();
for (int i = 0; i < amount; i++) {
Counter eventCounter = counter.copy();
eventCounter.remove(eventCounter.getCount() - 1);
GameEvent event = GameEvent.getEvent(GameEvent.EventType.ADD_COUNTER, objectId, controllerId, counter.getName(), 1);
event.setAppliedEffects(appliedEffects);
if (!game.replaceEvent(event)) {
counters.addCounter(eventCounter);
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.COUNTER_ADDED, objectId, controllerId, counter.getName(), 1));
} else {
returnCode = false;
}
}
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.COUNTERS_ADDED, objectId, controllerId, counter.getName(), amount));
} else {
returnCode = false;
}
return returnCode;
}
@Override
public void removeCounters(String name, int amount, Game game) {
for (int i = 0; i < amount; i++) {
counters.removeCounter(name, 1);
GameEvent event = GameEvent.getEvent(GameEvent.EventType.COUNTER_REMOVED, objectId, controllerId);
event.setData(name);
game.fireEvent(event);
}
}
@Override
public void removeCounters(Counter counter, Game game) {
removeCounters(counter.getName(), counter.getCount(), game);
} }
@Override @Override

View file

@ -159,18 +159,4 @@ public class PermanentMeld extends PermanentCard {
} }
return false; return false;
} }
@Override
public boolean addCounters(String name, int amount, Game game, ArrayList<UUID> appliedEffects) {
MeldCard meldCard = (MeldCard) this.getCard();
if (meldCard.isMelded()) {
return super.addCounters(name, amount, game, appliedEffects);
} else {
// can this really happen?
boolean returnState = true;
returnState |= meldCard.getTopHalfCard().addCounters(name, amount, game, appliedEffects);
returnState |= meldCard.getBottomHalfCard().addCounters(name, amount, game, appliedEffects);
return returnState;
}
}
} }

View file

@ -826,7 +826,6 @@ public class Spell extends StackObjImpl implements Card {
@Override @Override
public Counters getCounters(Game game) { public Counters getCounters(Game game) {
return card.getCounters(game); return card.getCounters(game);
} }
@ -835,16 +834,6 @@ public class Spell extends StackObjImpl implements Card {
return card.getCounters(state); return card.getCounters(state);
} }
@Override
public boolean addCounters(String name, int amount, Game game) {
return card.addCounters(name, amount, game);
}
@Override
public boolean addCounters(String name, int amount, Game game, ArrayList<UUID> appliedEffects) {
return card.addCounters(name, amount, game, appliedEffects);
}
@Override @Override
public boolean addCounters(Counter counter, Game game) { public boolean addCounters(Counter counter, Game game) {
return card.addCounters(counter, game); return card.addCounters(counter, game);

View file

@ -1838,19 +1838,30 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override @Override
public boolean addCounters(Counter counter, Game game) { public boolean addCounters(Counter counter, Game game) {
boolean returnState = true; boolean returnCode = true;
int amount = counter.getCount(); GameEvent countersEvent = GameEvent.getEvent(EventType.ADD_COUNTERS, playerId, playerId, counter.getName(), counter.getCount());
for (int i = 0; i < amount; i++) { if (!game.replaceEvent(countersEvent)) {
Counter eventCounter = counter.copy(); int amount = countersEvent.getAmount();
eventCounter.remove(amount - 1); int finalAmount = amount;
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.ADD_COUNTER, playerId, playerId, counter.getName(), counter.getCount()))) { for (int i = 0; i < amount; i++) {
counters.addCounter(eventCounter); Counter eventCounter = counter.copy();
game.fireEvent(GameEvent.getEvent(EventType.COUNTER_ADDED, playerId, playerId, counter.getName(), counter.getCount())); eventCounter.remove(amount - 1);
} else { GameEvent event = GameEvent.getEvent(EventType.ADD_COUNTER, playerId, playerId, counter.getName(), 1);
returnState = false; if (!game.replaceEvent(event)) {
getCounters().addCounter(eventCounter);
game.fireEvent(GameEvent.getEvent(EventType.COUNTER_ADDED, playerId, playerId, counter.getName(), 1));
} else {
finalAmount--;
returnCode = false;
}
} }
if(finalAmount > 0) {
game.fireEvent(GameEvent.getEvent(EventType.COUNTERS_ADDED, playerId, playerId, counter.getName(), amount));
}
} else {
returnCode = false;
} }
return returnState; return returnCode;
} }
@Override @Override