Ability refactor: new code to remove abilities from permanent;

This commit is contained in:
Oleg Agafonov 2020-05-28 22:30:34 +04:00
parent 3634e87e8e
commit 978118148b
24 changed files with 131 additions and 111 deletions

View file

@ -83,7 +83,7 @@ class AnjeFalkenrathTriggeredAbility extends TriggeredAbilityImpl {
if (card == null) {
return false;
}
return card.getAbilities(game).stream().anyMatch(ability -> ability instanceof MadnessAbility);
return card.getAbilities(game).containsClass(MadnessAbility.class);
}
@Override

View file

@ -1,6 +1,8 @@
package mage.cards.b;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import mage.abilities.Ability;
import mage.abilities.common.EntersBattlefieldTriggeredAbility;
@ -70,7 +72,13 @@ class BloodSunEffect extends ContinuousEffectImpl {
for (Permanent permanent : game.getState().getBattlefield().getActivePermanents(StaticFilters.FILTER_LANDS, player.getId(), source.getSourceId(), game)) {
switch (layer) {
case AbilityAddingRemovingEffects_6:
permanent.getAbilities().removeIf(ability -> ability.getAbilityType() != AbilityType.MANA);
List<Ability> toRemove = new ArrayList<>();
permanent.getAbilities().forEach(a -> {
if (a.getAbilityType() != AbilityType.MANA) {
toRemove.add(a);
}
});
permanent.removeAbilities(toRemove, source.getSourceId(), game);
break;
}
}

View file

@ -133,17 +133,14 @@ class BronzehideLionContinuousEffect extends ContinuousEffectImpl {
if (game.getState().getZoneChangeCounter(source.getSourceId()) > zoneChangeCounter) {
discard();
}
MageObject sourceObject = game.getPermanent(source.getSourceId());
Permanent sourceObject = game.getPermanent(source.getSourceId());
if (sourceObject == null) {
sourceObject = game.getPermanentEntering(source.getSourceId());
}
if (sourceObject == null) {
return false;
}
if (!(sourceObject instanceof Permanent)) {
return true;
}
Permanent lion = (Permanent) sourceObject;
Permanent lion = sourceObject;
switch (layer) {
case TypeChangingEffects_4:
lion.getCardType().clear();
@ -158,7 +155,7 @@ class BronzehideLionContinuousEffect extends ContinuousEffectImpl {
toRemove.add(ability);
}
}
lion.getAbilities(game).removeAll(toRemove);
lion.removeAbilities(toRemove, source.getSourceId(), game);
lion.getSpellAbility().getTargets().clear();
lion.getSpellAbility().getEffects().clear();

View file

@ -89,7 +89,7 @@ class GainReboundEffect extends ContinuousEffectImpl {
private void addReboundAbility(Card card, Game game) {
if (CastThroughTime.filter.match(card, game)) {
boolean found = card.getAbilities(game).stream().anyMatch(ability -> ability instanceof ReboundAbility);
boolean found = card.getAbilities(game).containsClass(ReboundAbility.class);
if (!found) {
Ability ability = new ReboundAbility();
game.getState().addOtherAbility(card, ability);

View file

@ -240,9 +240,7 @@ class DanceOfTheDeadChangeAbilityEffect extends ContinuousEffectImpl implements
abilityToRemove = ability;
}
}
if (abilityToRemove != null) {
permanent.getAbilities().remove(abilityToRemove);
}
permanent.removeAbility(abilityToRemove, source.getSourceId(), game);
permanent.addAbility(newAbility, source.getSourceId(), game);
return true;
}

View file

@ -22,8 +22,12 @@ public final class DeadlyRecluse extends CardImpl {
this.subtype.add(SubType.SPIDER);
this.power = new MageInt(1);
this.toughness = new MageInt(2);
this.toughness = new MageInt(2);
// Reach (This creature can block creatures with flying.)
this.addAbility(ReachAbility.getInstance());
// Deathtouch (Any amount of damage this deals to a creature is enough to destroy it.)
this.addAbility(DeathtouchAbility.getInstance());
}

View file

@ -51,8 +51,6 @@ enum FlameSweepPredicate implements ObjectPlayerPredicate<ObjectPlayer<Permanent
Permanent object = input.getObject();
UUID playerId = input.getPlayerId();
return !(object.isControlledBy(playerId)
&& object.getAbilities(game).stream().anyMatch(
ability -> ability.getClass().equals(FlyingAbility.class)
));
&& object.getAbilities(game).containsClass(FlyingAbility.class));
}
}

View file

@ -149,7 +149,7 @@ class MeliraSylvokOutcastEffect3 extends ContinuousEffectImpl {
Set<UUID> opponents = game.getOpponents(source.getControllerId());
for (Permanent perm : game.getBattlefield().getActivePermanents(filter, source.getControllerId(), game)) {
if (opponents.contains(perm.getControllerId())) {
perm.getAbilities().remove(InfectAbility.getInstance());
perm.removeAbility(InfectAbility.getInstance(), source.getSourceId(), game);
}
}
return true;

View file

@ -67,7 +67,7 @@ class ShoalSerpentEffect extends ContinuousEffectImpl {
switch (layer) {
case AbilityAddingRemovingEffects_6:
if (sublayer == SubLayer.NA) {
permanent.getAbilities().removeIf(entry -> entry.getId().equals(DefenderAbility.getInstance().getId()));
permanent.removeAbility(DefenderAbility.getInstance(), source.getSourceId(), game);
}
break;
}

View file

@ -68,7 +68,7 @@ class WishfulMerfolkEffect extends ContinuousEffectImpl {
switch (layer) {
case AbilityAddingRemovingEffects_6:
if (sublayer == SubLayer.NA) {
permanent.getAbilities().removeIf(entry -> entry.getId().equals(DefenderAbility.getInstance().getId()));
permanent.removeAbility(DefenderAbility.getInstance(), source.getSourceId(), game);
}
break;
case TypeChangingEffects_4:

View file

@ -2,9 +2,12 @@
package mage.abilities;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;
import mage.abilities.keyword.ProtectionAbility;
import mage.abilities.mana.ActivatedManaAbilityImpl;
import mage.constants.Zone;
@ -255,7 +258,8 @@ public interface Abilities<T extends Ability> extends List<T>, Serializable {
boolean containsAll(Abilities<T> abilities);
/**
* Searches this set of abilities for the existence of the give class
* Searches this set of abilities for the existence of the given class
* Warning, it doesn't work with inherited classes (e.g. it's not equal to instanceOf command)
*
* @param classObject
* @return True if the passed in class is also in this set of abilities.
@ -271,4 +275,13 @@ public interface Abilities<T extends Ability> extends List<T>, Serializable {
Abilities<T> copy();
String getValue();
@Deprecated // use permanent.removeAbility instead
boolean remove(Object o);
@Deprecated // use permanent.removeAbility instead
boolean removeAll(Collection<?> c);
@Deprecated // use permanent.removeAbility instead
boolean removeIf(Predicate<? super T> filter);
}

View file

@ -232,7 +232,8 @@ public class AbilitiesImpl<T extends Ability> extends ArrayList<T> implements Ab
if (ability.getId().equals(test.getId())) {
return true;
}
if (ability.getOriginalId().equals(test.getId())) {
if (ability.getOriginalId().equals(test.getOriginalId())) {
// on ability resolve: engine creates ability's copy and generates newId(), so you must use originalId to find that ability in card later
return true;
}
if (ability instanceof MageSingleton && test instanceof MageSingleton && ability.getRule().equals(test.getRule())) {
@ -243,7 +244,7 @@ public class AbilitiesImpl<T extends Ability> extends ArrayList<T> implements Ab
}
@Override
public boolean containsRule(T ability) {
public boolean containsRule(T ability) { // TODO: remove
return stream().anyMatch(rule -> rule.getRule().equals(ability.getRule()));
}
@ -262,7 +263,7 @@ public class AbilitiesImpl<T extends Ability> extends ArrayList<T> implements Ab
}
@Override
public boolean containsKey(UUID abilityId) {
public boolean containsKey(UUID abilityId) { // TODO: remove
return stream().anyMatch(ability -> abilityId.equals(ability.getId()));
}

View file

@ -522,4 +522,11 @@ public interface Ability extends Controllable, Serializable {
Ability addCustomOutcome(Outcome customOutcome);
Outcome getCustomOutcome();
/**
* For mtg's instances search, see rules example in 112.10b
* @param ability
* @return
*/
boolean isSameInstance(Ability ability);
}

View file

@ -938,6 +938,10 @@ public abstract class AbilityImpl implements Ability {
@Override
public boolean hasSourceObjectAbility(Game game, MageObject source, GameEvent event) {
// if source object have this ability
// uses for ability.isInUseableZone
// replacement and other continues effects can be without source, but active (must return true)
MageObject object = source;
// for singleton abilities like Flying we can't rely on abilities' source because it's only once in continuous effects
// so will use the sourceId of the object itself that came as a parameter if it is not null
@ -949,16 +953,10 @@ public abstract class AbilityImpl implements Ability {
}
if (object != null) {
if (object instanceof Permanent) {
if (!((Permanent) object).getAbilities(game).contains(this)) {
return false;
}
return ((Permanent) object).isPhasedIn();
} else if (object instanceof Card) {
return ((Card) object).getAbilities(game).contains(this);
} else if (!object.getAbilities().contains(this)) { // not sure which object it can still be
// check if it's an ability that is temporary gained to a card
Abilities<Ability> otherAbilities = game.getState().getAllOtherAbilities(this.getSourceId());
return otherAbilities != null && otherAbilities.contains(this);
return object.hasAbility(this, game) && ((Permanent) object).isPhasedIn();
} else {
// cards and other objects
return object.hasAbility(this, game);
}
}
return true;
@ -1264,4 +1262,17 @@ public abstract class AbilityImpl implements Ability {
public Outcome getCustomOutcome() {
return this.customOutcome;
}
@Override
public boolean isSameInstance(Ability ability) {
// same instance (by mtg rules) = same object, ID or class+text (you can't check class only cause it can be different by params/text)
if (ability == null) {
return false;
}
return (this == ability)
|| (this.getId().equals(ability.getId()))
|| (this.getOriginalId().equals(ability.getOriginalId()))
|| (this.getClass() == ability.getClass() && this.getRule(true).equals(ability.getRule(true)));
}
}

View file

@ -130,7 +130,8 @@ class LicidContinuousEffect extends ContinuousEffectImpl {
}
}
}
licid.getAbilities(game).removeAll(toRemove);
licid.removeAbilities(toRemove, source.getSourceId(), game);
Ability ability = new EnchantAbility("creature");
ability.setRuleAtTheTop(true);
licid.addAbility(ability, source.getSourceId(), game);

View file

@ -45,9 +45,7 @@ public class CreaturesCantGetOrHaveAbilityEffect extends ContinuousEffectImpl {
if (controller != null) {
for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, source.getControllerId(), source.getSourceId(), game)) {
if (permanent != null) {
while (permanent.getAbilities().remove(ability)) {
// repeat as long as ability can be removed
}
permanent.removeAbility(ability, source.getSourceId(), game);
}
}
return true;

View file

@ -85,9 +85,7 @@ public class LoseAbilityAllEffect extends ContinuousEffectImpl {
for (Iterator<MageObjectReference> it = affectedObjectList.iterator(); it.hasNext();) { // filter may not be used again, because object can have changed filter relevant attributes but still geets boost
Permanent perm = it.next().getPermanentOrLKIBattlefield(game); //LKI is neccessary for "dies triggered abilities" to work given to permanets (e.g. Showstopper)
if (perm != null) {
for (Ability ability : ability) {
perm.getAbilities().removeIf(entry -> entry.getId().equals(ability.getId()));
}
perm.removeAbilities(ability, source.getSourceId(), game);
} else {
it.remove();
if (affectedObjectList.isEmpty()) {
@ -99,9 +97,7 @@ public class LoseAbilityAllEffect extends ContinuousEffectImpl {
for (Permanent perm : game.getBattlefield().getActivePermanents(filter, source.getControllerId(), source.getSourceId(), game)) {
if (!(excludeSource && perm.getId().equals(source.getSourceId()))) {
System.out.println(game.getTurn() + ", " + game.getPhase() + ": " + "remove from size " + perm.getAbilities().size());
for (Ability ability : ability) {
perm.getAbilities().removeIf(entry -> entry.getId().equals(ability.getId()));
}
perm.removeAbilities(ability, source.getSourceId(), game);
}
}
// still as long as the prev. permanent is known to the LKI (e.g. Mikaeus, the Unhallowed) so gained dies triggered ability will trigger
@ -111,9 +107,7 @@ public class LoseAbilityAllEffect extends ContinuousEffectImpl {
Permanent perm = (Permanent) mageObject;
if (!(excludeSource && perm.getId().equals(source.getSourceId()))) {
if (filter.match(perm, source.getSourceId(), source.getControllerId(), game)) {
for (Ability ability : ability) {
perm.getAbilities().removeIf(entry -> entry.getId().equals(ability.getId()));
}
perm.removeAbilities(ability, source.getSourceId(), game);
}
}
}

View file

@ -43,12 +43,7 @@ public class LoseAbilityAttachedEffect extends ContinuousEffectImpl {
if (equipment != null && equipment.getAttachedTo() != null) {
Permanent creature = game.getPermanent(equipment.getAttachedTo());
if (creature != null) {
while (creature.getAbilities().contains(ability)) {
if (!creature.getAbilities().remove(ability)) {
// Something went wrong - ability has an other id?
logger.warn("ability" + ability.getRule() + "couldn't be removed.");
}
}
creature.removeAbility(ability, source.getSourceId(), game);
}
}
return true;

View file

@ -63,13 +63,9 @@ public class LoseAbilityOrAnotherAbilityTargetEffect extends LoseAbilityTargetEf
if (player.choose(outcome, chooseAbility, game)) {
String chosenAbility = chooseAbility.getChoice();
if (chosenAbility.equals(ability.getRule())) {
while (permanent.getAbilities().contains(ability)) {
permanent.getAbilities().remove(ability);
}
permanent.removeAbility(ability, source.getSourceId(), game);
} else if (chosenAbility.equals(ability2.getRule())) {
while (permanent.getAbilities().contains(ability2)) {
permanent.getAbilities().remove(ability2);
}
permanent.removeAbility(ability2, source.getSourceId(), game);
}
} else {
return false;

View file

@ -56,10 +56,7 @@ public class LoseAbilitySourceEffect extends ContinuousEffectImpl {
public boolean apply(Game game, Ability source) {
Permanent permanent = game.getPermanent(source.getSourceId());
if (permanent != null) {
// 112.10
while (permanent.getAbilities().remove(ability)) {
}
permanent.removeAbility(ability, source.getSourceId(), game);
}
return true;
}

View file

@ -45,18 +45,7 @@ public class LoseAbilityTargetEffect extends ContinuousEffectImpl {
public boolean apply(Game game, Ability source) {
Permanent permanent = game.getPermanent(getTargetPointer().getFirst(game, source));
if (permanent != null) {
if (ability instanceof MageSingleton) {
while (permanent.getAbilities().contains(ability)) {
permanent.getAbilities().remove(ability);
}
} else {
for (Iterator<Ability> iter = permanent.getAbilities().iterator(); iter.hasNext();) {
Ability ab = iter.next();
if (ab.getClass().equals(ability.getClass())) {
iter.remove();
}
}
}
permanent.removeAbility(ability, source.getSourceId(), game);
}
return true;
}

View file

@ -26,6 +26,10 @@ public interface Card extends MageObject {
void setOwnerId(UUID ownerId);
/**
* For cards: return all basic and dynamic abilities
* For permanents: return all basic and dynamic abilities
*/
Abilities<Ability> getAbilities(Game game);
void setSpellAbility(SpellAbility ability);

View file

@ -154,15 +154,16 @@ public interface Permanent extends Card, Controllable {
String getValue(GameState state);
@Deprecated
void addAbility(Ability ability, Game game);
void addAbility(Ability ability, UUID sourceId, Game game);
void addAbility(Ability ability, UUID sourceId, Game game, boolean createNewId);
void removeAllAbilities(UUID sourceId, Game game);
void removeAbility(Ability abilityToRemove, UUID sourceId, Game game);
void removeAbilities(List<Ability> abilitiesToRemove, UUID sourceId, Game game);
void addLoyaltyUsed();
boolean canLoyaltyBeUsed(Game game);

View file

@ -351,62 +351,70 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
@Override
public Abilities<Ability> getAbilities() {
return abilities;
return super.getAbilities();
}
@Override
public Abilities<Ability> getAbilities(Game game) {
return abilities;
}
/**
* @param ability
* @param game
*/
@Override
public void addAbility(Ability ability, Game game) {
if (!abilities.containsKey(ability.getId())) {
Ability copyAbility = ability.copy();
copyAbility.setControllerId(controllerId);
copyAbility.setSourceId(objectId);
if (game != null) {
game.getState().addAbility(copyAbility, this);
}
abilities.add(copyAbility);
}
return super.getAbilities(game);
}
@Override
public void addAbility(Ability ability, UUID sourceId, Game game) {
addAbility(ability, sourceId, game, true);
addAbility(ability, sourceId, game, false);
}
@Override
@Deprecated // use addAbility(Ability ability, UUID sourceId, Game game) instead
public void addAbility(Ability ability, UUID sourceId, Game game, boolean createNewId) {
// singleton abilities -- only one instance
// other abilities -- any amount of instances
// TODO: no needs in createNewId, so move code to addAbility(Ability ability, UUID sourceId, Game game)
if (!abilities.containsKey(ability.getId())) {
Ability copyAbility = ability.copy();
if (createNewId) {
copyAbility.newId(); // needed so that source can get an ability multiple times (e.g. Raging Ravine)
}
copyAbility.newId(); // needed so that source can get an ability multiple times (e.g. Raging Ravine)
copyAbility.setControllerId(controllerId);
copyAbility.setSourceId(objectId);
// triggered abilities must be added to the state().triggers
// still as long as the prev. permanent is known to the LKI (e.g. Showstopper) so gained dies triggered ability will trigger
game.getState().addAbility(copyAbility, sourceId, this);
abilities.add(copyAbility);
} else if (!createNewId) {
// triggered abilities must be added to the state().triggerdAbilities
// still as long as the prev. permanent is known to the LKI (e.g. Showstopper) so gained dies triggered ability will trigger
if (!game.getBattlefield().containsPermanent(this.getId())) {
Ability copyAbility = ability.copy();
copyAbility.setControllerId(controllerId);
copyAbility.setSourceId(objectId);
game.getState().addAbility(copyAbility, sourceId, this);
}
}
}
@Override
public void removeAllAbilities(UUID sourceId, Game game) {
getAbilities().clear();
// can't use getAbilities() here -- cause it's can be auto-generated list potentially
// TODO: what about triggered abilities? See addAbility above -- triggers adds to GameState
abilities.clear();
}
@Override
public void removeAbility(Ability abilityToRemove, UUID sourceId, Game game) {
if (abilityToRemove == null) {
return;
}
// 112.10b Effects that remove an ability remove all instances of it.
List<Ability> toRemove = new ArrayList<>();
abilities.forEach(a -> {
if (a.isSameInstance(abilityToRemove)) {
toRemove.add(a);
}
});
// can't use getAbilities() here -- cause it's can be auto-generated list potentially
// TODO: what about triggered abilities? See addAbility above -- triggers adds to GameState
toRemove.forEach(r -> abilities.remove(r));
}
@Override
public void removeAbilities(List<Ability> abilitiesToRemove, UUID sourceId, Game game){
if (abilitiesToRemove == null) {
return;
}
abilitiesToRemove.forEach(a -> removeAbility(a, sourceId, game));
}
@Override
@ -728,7 +736,7 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
game.fireEvent(new GameEvent(EventType.GAINED_CONTROL, objectId, objectId, controllerId));
return true;
} else if (isCopy()) {// Because the previous copied abilities can be from another controller chnage controller in any case for abilities
} else if (isCopy()) {// Because the previous copied abilities can be from another controller - change controller in any case for abilities
this.getAbilities(game).setControllerId(controllerId);
game.getContinuousEffects().setController(objectId, controllerId);
}