Refactoring and cleaning up based on previous commit

This commit is contained in:
Alex Vasile 2022-08-12 21:33:20 -04:00
parent c0c5c63284
commit 19a896df57
3 changed files with 126 additions and 266 deletions

View file

@ -11,6 +11,7 @@ import mage.cards.CardImpl;
import mage.cards.CardSetInfo; import mage.cards.CardSetInfo;
import mage.choices.ChoiceColor; import mage.choices.ChoiceColor;
import mage.constants.CardType; import mage.constants.CardType;
import mage.constants.ManaType;
import mage.constants.Zone; import mage.constants.Zone;
import mage.counters.CounterType; import mage.counters.CounterType;
import mage.game.Game; import mage.game.Game;
@ -48,17 +49,13 @@ public final class AstralCornucopia extends CardImpl {
class AstralCornucopiaManaEffect extends ManaEffect { class AstralCornucopiaManaEffect extends ManaEffect {
private final Mana computedMana;
public AstralCornucopiaManaEffect() { public AstralCornucopiaManaEffect() {
super(); super();
computedMana = new Mana();
this.staticText = "Choose a color. Add one mana of that color for each charge counter on {this}"; this.staticText = "Choose a color. Add one mana of that color for each charge counter on {this}";
} }
public AstralCornucopiaManaEffect(final AstralCornucopiaManaEffect effect) { public AstralCornucopiaManaEffect(final AstralCornucopiaManaEffect effect) {
super(effect); super(effect);
this.computedMana = effect.computedMana.copy();
} }
@Override @Override
@ -69,59 +66,47 @@ class AstralCornucopiaManaEffect extends ManaEffect {
@Override @Override
public List<Mana> getNetMana(Game game, Ability source) { public List<Mana> getNetMana(Game game, Ability source) {
List<Mana> netMana = new ArrayList<>(); List<Mana> netMana = new ArrayList<>();
if (game != null) { if (game == null) {
Permanent sourcePermanent = game.getPermanent(source.getSourceId()); return netMana;
if (sourcePermanent != null) { }
int counters = sourcePermanent.getCounters(game).getCount(CounterType.CHARGE.getName()); Permanent sourcePermanent = game.getPermanent(source.getSourceId());
if (counters > 0) { if (sourcePermanent == null) {
netMana.add(Mana.AnyMana(counters)); return netMana;
} }
}
int counters = sourcePermanent.getCounters(game).getCount(CounterType.CHARGE.getName());
if (counters > 0) {
netMana.add(Mana.WhiteMana(counters));
netMana.add(Mana.BlueMana(counters));
netMana.add(Mana.BlackMana(counters));
netMana.add(Mana.GreenMana(counters));
netMana.add(Mana.RedMana(counters));
} }
return netMana; return netMana;
} }
@Override @Override
public Mana produceMana(Game game, Ability source) { public Mana produceMana(Game game, Ability source) {
Mana mana = new Mana();
if (game == null) { if (game == null) {
return mana; return null;
} }
Permanent sourcePermanent = game.getPermanent(source.getSourceId()); Permanent sourcePermanent = game.getPermanent(source.getSourceId());
if (sourcePermanent != null) { Player controller = game.getPlayer(source.getControllerId());
int counters = sourcePermanent.getCounters(game).getCount(CounterType.CHARGE.getName()); if (sourcePermanent == null || controller == null) {
if (counters > 0) { return null;
Player controller = game.getPlayer(source.getControllerId()); }
if (controller != null) { int counters = sourcePermanent.getCounters(game).getCount(CounterType.CHARGE.getName());
ChoiceColor choice = new ChoiceColor(); if (counters == 0) {
choice.setMessage("Choose a color to add mana of that color"); return null;
if (controller.choose(outcome, choice, game)) {
if (choice.getChoice() != null) {
String color = choice.getChoice();
switch (color) {
case "Red":
mana.setRed(counters);
break;
case "Blue":
mana.setBlue(counters);
break;
case "White":
mana.setWhite(counters);
break;
case "Black":
mana.setBlack(counters);
break;
case "Green":
mana.setGreen(counters);
break;
}
}
}
}
}
} }
return mana; ChoiceColor choice = new ChoiceColor();
} choice.setMessage("Choose a color to add mana of that color");
if (!controller.choose(outcome, choice, game)) {
return null;
}
ManaType chosenType = ManaType.findByName(choice.getChoice());
return chosenType == null ? null : new Mana(chosenType);
}
} }

View file

@ -9,18 +9,15 @@ import mage.abilities.mana.SimpleManaAbility;
import mage.cards.CardImpl; import mage.cards.CardImpl;
import mage.cards.CardSetInfo; import mage.cards.CardSetInfo;
import mage.choices.Choice; import mage.choices.Choice;
import mage.choices.ChoiceColor;
import mage.constants.CardType; import mage.constants.CardType;
import mage.constants.ColoredManaSymbol; import mage.constants.ManaType;
import mage.constants.Zone; import mage.constants.Zone;
import mage.filter.common.FilterControlledPermanent; import mage.filter.StaticFilters;
import mage.game.Game; import mage.game.Game;
import mage.game.permanent.Permanent; import mage.game.permanent.Permanent;
import mage.players.Player; import mage.players.Player;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.UUID;
/** /**
* @author anonymous * @author anonymous
@ -47,11 +44,9 @@ public final class MeteorCrater extends CardImpl {
class MeteorCraterEffect extends ManaEffect { class MeteorCraterEffect extends ManaEffect {
/** /**
* *
* 04/10/2004 You can't choose "colorless". You have to choose one of the * 04/10/2004 You can't choose "colorless". You have to choose one of the
* five colors. * five colors.
*/ */
private static final FilterControlledPermanent filter = new FilterControlledPermanent();
public MeteorCraterEffect() { public MeteorCraterEffect() {
super(); super();
@ -64,110 +59,60 @@ class MeteorCraterEffect extends ManaEffect {
@Override @Override
public List<Mana> getNetMana(Game game, Ability source) { public List<Mana> getNetMana(Game game, Ability source) {
List<Mana> netManas = new ArrayList<>(); return game == null ? new ArrayList<>() : ManaType.getManaListFromManaTypes(getManaTypes(game, source), true);
Mana types = getManaTypes(game, source);
if (types.getBlack() > 0) {
netManas.add(new Mana(ColoredManaSymbol.B));
}
if (types.getRed() > 0) {
netManas.add(new Mana(ColoredManaSymbol.R));
}
if (types.getBlue() > 0) {
netManas.add(new Mana(ColoredManaSymbol.U));
}
if (types.getGreen() > 0) {
netManas.add(new Mana(ColoredManaSymbol.G));
}
if (types.getWhite() > 0) {
netManas.add(new Mana(ColoredManaSymbol.W));
}
return netManas;
} }
@Override @Override
public Mana produceMana(Game game, Ability source) { public Mana produceMana(Game game, Ability source) {
Mana mana = new Mana();
if (game == null) { if (game == null) {
return mana; return null;
} }
Mana types = getManaTypes(game, source);
Choice choice = new ChoiceColor(true); Set<ManaType> types = getManaTypes(game, source);
choice.getChoices().clear(); if (types.isEmpty()) {
choice.setMessage("Pick a mana color"); return null;
if (types.getAny() > 0) { }
choice.getChoices().add("Black"); Choice choice = ManaType.getChoiceOfManaTypes(types, true);
choice.getChoices().add("Red"); if (choice.getChoices().size() == 1) {
choice.getChoices().add("Blue"); choice.setChoice(choice.getChoices().iterator().next());
choice.getChoices().add("Green");
choice.getChoices().add("White");
} else { } else {
if (types.getBlack() > 0) {
choice.getChoices().add("Black");
}
if (types.getRed() > 0) {
choice.getChoices().add("Red");
}
if (types.getBlue() > 0) {
choice.getChoices().add("Blue");
}
if (types.getGreen() > 0) {
choice.getChoices().add("Green");
}
if (types.getWhite() > 0) {
choice.getChoices().add("White");
}
}
if (!choice.getChoices().isEmpty()) {
Player player = game.getPlayer(source.getControllerId()); Player player = game.getPlayer(source.getControllerId());
if (choice.getChoices().size() == 1) { if (player == null || !player.choose(outcome, choice, game)) {
choice.setChoice(choice.getChoices().iterator().next()); return null;
} else {
player.choose(outcome, choice, game);
}
if (choice.getChoice() != null) {
switch (choice.getChoice()) {
case "Black":
mana.setBlack(1);
break;
case "Blue":
mana.setBlue(1);
break;
case "Red":
mana.setRed(1);
break;
case "Green":
mana.setGreen(1);
break;
case "White":
mana.setWhite(1);
break;
}
} }
} }
return mana; ManaType chosenType = ManaType.findByName(choice.getChoice());
return chosenType == null ? null : new Mana(chosenType);
} }
private Mana getManaTypes(Game game, Ability source) { private Set<ManaType> getManaTypes(Game game, Ability source) {
Mana types = new Mana(); Set<ManaType> types = new HashSet<>(5);
if (game != null) { if (game == null) {
List<Permanent> controlledPermanents = game.getBattlefield().getActivePermanents(filter, source.getControllerId(), game); return types;
for (Permanent permanent : controlledPermanents) { }
ObjectColor color = permanent.getColor(game);
if (color.isBlack()) { List<Permanent> controlledPermanents = game.getBattlefield().getActivePermanents(StaticFilters.FILTER_CONTROLLED_PERMANENT, source.getControllerId(), game);
types.add(Mana.BlackMana(1)); for (Permanent permanent : controlledPermanents) {
} ObjectColor color = permanent.getColor(game);
if (color.isBlue()) { if (color.isBlack()) {
types.add(Mana.BlueMana(1)); types.add(ManaType.BLACK);
} }
if (color.isGreen()) { if (color.isBlue()) {
types.add(Mana.GreenMana(1)); types.add(ManaType.BLUE);
} }
if (color.isRed()) { if (color.isGreen()) {
types.add(Mana.RedMana(1)); types.add(ManaType.GREEN);
} }
if (color.isWhite()) { if (color.isRed()) {
types.add(Mana.WhiteMana(1)); types.add(ManaType.RED);
} }
if (color.isWhite()) {
types.add(ManaType.WHITE);
}
// If all types are already added, exit early
if (types.size() == 5) {
break;
} }
} }
return types; return types;

View file

@ -6,8 +6,7 @@ import mage.abilities.Ability;
import mage.abilities.costs.common.TapSourceCost; import mage.abilities.costs.common.TapSourceCost;
import mage.abilities.effects.mana.ManaEffect; import mage.abilities.effects.mana.ManaEffect;
import mage.choices.Choice; import mage.choices.Choice;
import mage.choices.ChoiceColor; import mage.constants.ManaType;
import mage.constants.ColoredManaSymbol;
import mage.constants.TargetController; import mage.constants.TargetController;
import mage.constants.Zone; import mage.constants.Zone;
import mage.filter.FilterPermanent; import mage.filter.FilterPermanent;
@ -16,7 +15,9 @@ import mage.game.permanent.Permanent;
import mage.players.Player; import mage.players.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* @author CountAndromalius * @author CountAndromalius
@ -24,11 +25,7 @@ import java.util.List;
public class AnyColorPermanentTypesManaAbility extends ActivatedManaAbilityImpl { public class AnyColorPermanentTypesManaAbility extends ActivatedManaAbilityImpl {
public AnyColorPermanentTypesManaAbility(TargetController targetController, FilterPermanent permanentTypes) { public AnyColorPermanentTypesManaAbility(TargetController targetController, FilterPermanent permanentTypes) {
this(targetController, true, permanentTypes); super(Zone.BATTLEFIELD, new AnyColorPermanentTypesManaEffect(targetController, permanentTypes), new TapSourceCost());
}
public AnyColorPermanentTypesManaAbility(TargetController targetController, boolean onlyColors, FilterPermanent permanentTypes) {
super(Zone.BATTLEFIELD, new AnyColorPermanentTypesManaEffect(targetController, onlyColors, permanentTypes), new TapSourceCost());
} }
public AnyColorPermanentTypesManaAbility(final AnyColorPermanentTypesManaAbility ability) { public AnyColorPermanentTypesManaAbility(final AnyColorPermanentTypesManaAbility ability) {
@ -55,155 +52,88 @@ public class AnyColorPermanentTypesManaAbility extends ActivatedManaAbilityImpl
class AnyColorPermanentTypesManaEffect extends ManaEffect { class AnyColorPermanentTypesManaEffect extends ManaEffect {
private final FilterPermanent filter; private final FilterPermanent filter;
private final boolean onlyColors; // false if mana types can be produced (also Colorless mana), if true only colors can be produced (no Colorless mana).
private boolean inManaTypeCalculation = false; private transient boolean inManaTypeCalculation = false;
public AnyColorPermanentTypesManaEffect(TargetController targetController, boolean onlyColors, FilterPermanent permanentTypes) { public AnyColorPermanentTypesManaEffect(TargetController targetController, FilterPermanent permanentTypes) {
super(); super();
filter = permanentTypes; filter = permanentTypes;
this.onlyColors = onlyColors;
filter.add(targetController.getControllerPredicate()); filter.add(targetController.getControllerPredicate());
String text = targetController == TargetController.OPPONENT ? "an opponent controls." : "you control."; staticText = "Add one mana of any color" +
staticText = "Add one mana of any " + (this.onlyColors ? "color" : "type") + " among " + permanentTypes.getMessage() + " " + text; " among " + permanentTypes.getMessage() + " " +
(targetController == TargetController.OPPONENT ? "an opponent controls." : "you control.");
} }
public AnyColorPermanentTypesManaEffect(final AnyColorPermanentTypesManaEffect effect) { public AnyColorPermanentTypesManaEffect(final AnyColorPermanentTypesManaEffect effect) {
super(effect); super(effect);
this.filter = effect.filter.copy(); this.filter = effect.filter.copy();
this.onlyColors = effect.onlyColors;
} }
@Override @Override
public List<Mana> getNetMana(Game game, Ability source) { public List<Mana> getNetMana(Game game, Ability source) {
List<Mana> netManas = new ArrayList<>(); return game == null ? new ArrayList<>() : ManaType.getManaListFromManaTypes(getManaTypes(game, source), true);
Mana types = getManaTypes(game, source);
if (types.getBlack() > 0) {
netManas.add(new Mana(ColoredManaSymbol.B));
}
if (types.getRed() > 0) {
netManas.add(new Mana(ColoredManaSymbol.R));
}
if (types.getBlue() > 0) {
netManas.add(new Mana(ColoredManaSymbol.U));
}
if (types.getGreen() > 0) {
netManas.add(new Mana(ColoredManaSymbol.G));
}
if (types.getWhite() > 0) {
netManas.add(new Mana(ColoredManaSymbol.W));
}
if (!onlyColors && types.getColorless() > 0) {
netManas.add(Mana.ColorlessMana(1));
}
if (types.getAny() > 0) {
netManas.add(Mana.AnyMana(1));
}
return netManas;
} }
@Override @Override
public Mana produceMana(Game game, Ability source) { public Mana produceMana(Game game, Ability source) {
Mana mana = new Mana();
if (game == null) { if (game == null) {
return mana; return null;
} }
Mana types = getManaTypes(game, source);
Choice choice = new ChoiceColor(true);
choice.getChoices().clear();
choice.setMessage("Pick a mana color");
if (types.getBlack() > 0) {
choice.getChoices().add("Black");
}
if (types.getRed() > 0) {
choice.getChoices().add("Red");
}
if (types.getBlue() > 0) {
choice.getChoices().add("Blue");
}
if (types.getGreen() > 0) {
choice.getChoices().add("Green");
}
if (types.getWhite() > 0) {
choice.getChoices().add("White");
}
if (!onlyColors && types.getColorless() > 0) {
choice.getChoices().add("Colorless");
}
if (types.getAny() > 0) {
choice.getChoices().add("Black");
choice.getChoices().add("Red");
choice.getChoices().add("Blue");
choice.getChoices().add("Green");
choice.getChoices().add("White");
if (!onlyColors) {
choice.getChoices().add("Colorless");
}
Set<ManaType> types = getManaTypes(game, source);
if (types.isEmpty()) {
return null;
} }
if (!choice.getChoices().isEmpty()) {
Choice choice = ManaType.getChoiceOfManaTypes(types, true);
if (choice.getChoices().size() == 1) {
choice.setChoice(choice.getChoices().iterator().next());
} else {
Player player = game.getPlayer(source.getControllerId()); Player player = game.getPlayer(source.getControllerId());
if (choice.getChoices().size() == 1) { if (player == null || !player.choose(outcome, choice, game)) {
choice.setChoice(choice.getChoices().iterator().next()); return null;
} else {
if (!player.choose(outcome, choice, game)) {
return mana;
}
}
if (choice.getChoice() != null) {
switch (choice.getChoice()) {
case "Black":
mana.setBlack(1);
break;
case "Blue":
mana.setBlue(1);
break;
case "Red":
mana.setRed(1);
break;
case "Green":
mana.setGreen(1);
break;
case "White":
mana.setWhite(1);
break;
case "Colorless":
mana.setColorless(1);
break;
}
} }
} }
return mana;
ManaType chosenType = ManaType.findByName(choice.getChoice());
return chosenType == null ? null : new Mana(chosenType);
} }
private Mana getManaTypes(Game game, Ability source) { private Set<ManaType> getManaTypes(Game game, Ability source) {
Mana types = new Mana(); Set<ManaType> manaTypes = new HashSet<>(6);
if (game == null || game.getPhase() == null) { if (game == null || game.getPhase() == null || inManaTypeCalculation) {
return types; return manaTypes;
}
if (inManaTypeCalculation) {
return types;
} }
inManaTypeCalculation = true; inManaTypeCalculation = true;
ObjectColor permanentColor; ObjectColor permanentColor;
List<Permanent> permanents = game.getBattlefield().getActivePermanents(filter, source.getControllerId(), source, game); List<Permanent> permanents = game.getBattlefield().getActivePermanents(filter, source.getControllerId(), source, game);
for (Permanent permanent : permanents) { for (Permanent permanent : permanents) {
permanentColor = permanent.getColor(game); permanentColor = permanent.getColor(game);
if (permanentColor.isColorless()) {
types.add(Mana.ColorlessMana(1)); if (permanentColor.isBlack()) {
} else { manaTypes.add(ManaType.BLACK);
List<ObjectColor> permanentColors = permanent.getColor(game).getColors(); }
for (ObjectColor color : permanentColors) { if (permanentColor.isBlue()) {
types.add(new Mana(color.getOneColoredManaSymbol())); manaTypes.add(ManaType.BLUE);
} }
if (permanentColor.isGreen()) {
manaTypes.add(ManaType.GREEN);
}
if (permanentColor.isRed()) {
manaTypes.add(ManaType.RED);
}
if (permanentColor.isWhite()) {
manaTypes.add(ManaType.WHITE);
}
// If all types are already added, exit early
if (manaTypes.size() == 5) {
break;
} }
} }
inManaTypeCalculation = false; inManaTypeCalculation = false;
return types; return manaTypes;
} }
@Override @Override