ChangeTargetsAi: make PhyrexianManaCheck more generic and update to new SpellAbilityAi format

This commit is contained in:
Hanmac
2017-01-22 07:16:15 +00:00
parent 59352ef487
commit 2a9def0a89

View File

@@ -5,8 +5,6 @@ import forge.ai.ComputerUtilAbility;
import forge.ai.ComputerUtilMana; import forge.ai.ComputerUtilMana;
import forge.ai.SpellAbilityAi; import forge.ai.SpellAbilityAi;
import forge.card.mana.ManaCost; import forge.card.mana.ManaCost;
import forge.card.mana.ManaCostParser;
import forge.card.mana.ManaCostShard;
import forge.game.Game; import forge.game.Game;
import forge.game.mana.ManaCostBeingPaid; import forge.game.mana.ManaCostBeingPaid;
import forge.game.player.Player; import forge.game.player.Player;
@@ -17,36 +15,24 @@ public class ChangeTargetsAi extends SpellAbilityAi {
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see * @see forge.ai.SpellAbilityAi#checkApiLogic(forge.game.player.Player,
* forge.card.abilityfactory.AbilityFactoryAlterLife.SpellAiLogic#canPlayAI * forge.game.spellability.SpellAbility)
* (forge.game.player.Player, java.util.Map,
* forge.card.spellability.SpellAbility)
*/ */
@Override @Override
protected boolean canPlayAI(Player aiPlayer, SpellAbility sa) { protected boolean checkApiLogic(Player ai, SpellAbility sa) {
final Game game = sa.getHostCard().getGame(); final Game game = sa.getHostCard().getGame();
final SpellAbility topSa = game.getStack().isEmpty() ? null : ComputerUtilAbility.getTopSpellAbilityOnStack(game, sa); final SpellAbility topSa = game.getStack().isEmpty() ? null
: ComputerUtilAbility.getTopSpellAbilityOnStack(game, sa);
if ("Self".equals(sa.getParam("DefinedMagnet"))) { if ("Self".equals(sa.getParam("DefinedMagnet"))) {
return doSpellMagnet(sa, topSa, aiPlayer); return doSpellMagnet(sa, topSa, ai);
} }
// The AI can't otherwise play this ability, but should at least not miss mandatory activations (e.g. triggers). // The AI can't otherwise play this ability, but should at least not
// miss mandatory activations (e.g. triggers).
return sa.isMandatory(); return sa.isMandatory();
} }
@Override
protected boolean doTriggerAINoCost(Player aiPlayer, SpellAbility sa, boolean mandatory) {
final Game game = sa.getHostCard().getGame();
final SpellAbility topSa = game.getStack().isEmpty() ? null : ComputerUtilAbility.getTopSpellAbilityOnStack(game, sa);
if ("Self".equals(sa.getParam("DefinedMagnet"))) {
return doSpellMagnet(sa, topSa, aiPlayer);
}
return true;
}
private boolean doSpellMagnet(SpellAbility sa, SpellAbility topSa, Player aiPlayer) { private boolean doSpellMagnet(SpellAbility sa, SpellAbility topSa, Player aiPlayer) {
// For cards like Spellskite that retarget spells to itself // For cards like Spellskite that retarget spells to itself
if (topSa == null) { if (topSa == null) {
@@ -82,11 +68,15 @@ public class ChangeTargetsAi extends SpellAbilityAi {
return false; return false;
} }
if (sa.getPayCosts().getCostMana() != null && sa.getPayCosts().getCostMana().getMana().getShardCount(ManaCostShard.PU) > 0) { if (sa.getPayCosts().getCostMana() != null && sa.getPayCosts().getCostMana().getMana().hasPhyrexian()) {
ManaCost manaCost = sa.getPayCosts().getCostMana().getMana();
int payDamage = manaCost.getPhyrexianCount() * 2;
// e.g. Spellskite or a creature receiving its ability that requires Phyrexian mana P/U // e.g. Spellskite or a creature receiving its ability that requires Phyrexian mana P/U
int potentialDmg = ComputerUtil.predictDamageFromSpell(topSa, aiPlayer); int potentialDmg = ComputerUtil.predictDamageFromSpell(topSa, aiPlayer);
boolean canPayBlue = ComputerUtilMana.canPayManaCost(new ManaCostBeingPaid(new ManaCost(new ManaCostParser("U"))), sa, aiPlayer); ManaCost normalizedMana = manaCost.getNormalizedMana();
if (potentialDmg != -1 && potentialDmg <= 2 && !canPayBlue && topSa.getTargets().getTargets().contains(aiPlayer)) { boolean canPay = ComputerUtilMana.canPayManaCost(new ManaCostBeingPaid(normalizedMana), sa, aiPlayer);
if (potentialDmg != -1 && potentialDmg <= payDamage && !canPay
&& topSa.getTargets().getTargets().contains(aiPlayer)) {
// do not pay Phyrexian mana if the spell is a damaging one but it deals less damage or the same damage as we'll pay life // do not pay Phyrexian mana if the spell is a damaging one but it deals less damage or the same damage as we'll pay life
return false; return false;
} }