mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 03:38:01 +00:00
ChooseCardAi: updated AI for new SpellAbilityAi format
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
package forge.ai.ability;
|
package forge.ai.ability;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import com.google.common.base.Predicate;
|
import com.google.common.base.Predicate;
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
import forge.ai.ComputerUtilCard;
|
import forge.ai.ComputerUtilCard;
|
||||||
import forge.ai.ComputerUtilCombat;
|
import forge.ai.ComputerUtilCombat;
|
||||||
@@ -15,31 +20,41 @@ import forge.game.card.CounterType;
|
|||||||
import forge.game.combat.Combat;
|
import forge.game.combat.Combat;
|
||||||
import forge.game.phase.PhaseType;
|
import forge.game.phase.PhaseType;
|
||||||
import forge.game.player.Player;
|
import forge.game.player.Player;
|
||||||
|
import forge.game.player.PlayerPredicates;
|
||||||
import forge.game.spellability.SpellAbility;
|
import forge.game.spellability.SpellAbility;
|
||||||
import forge.game.spellability.TargetRestrictions;
|
|
||||||
import forge.game.zone.ZoneType;
|
import forge.game.zone.ZoneType;
|
||||||
import forge.util.Aggregates;
|
import forge.util.Aggregates;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
public class ChooseCardAi extends SpellAbilityAi {
|
public class ChooseCardAi extends SpellAbilityAi {
|
||||||
@Override
|
|
||||||
protected boolean canPlayAI(final Player ai, SpellAbility sa) {
|
|
||||||
final Card host = sa.getHostCard();
|
|
||||||
final Game game = ai.getGame();
|
|
||||||
|
|
||||||
final TargetRestrictions tgt = sa.getTargetRestrictions();
|
/**
|
||||||
if (tgt != null) {
|
* The rest of the logic not covered by the canPlayAI template is defined here
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected boolean checkApiLogic(final Player ai, final SpellAbility sa) {
|
||||||
|
if (sa.usesTargeting()) {
|
||||||
sa.resetTargets();
|
sa.resetTargets();
|
||||||
if (sa.canTarget(ai.getOpponent())) {
|
// search targetable Opponents
|
||||||
sa.getTargets().add(ai.getOpponent());
|
final List<Player> oppList = Lists.newArrayList(Iterables.filter(
|
||||||
} else {
|
ai.getOpponents(), PlayerPredicates.isTargetableBy(sa)));
|
||||||
|
|
||||||
|
if (oppList.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sa.getTargets().add(Iterables.getFirst(oppList, null));
|
||||||
}
|
}
|
||||||
if (sa.hasParam("AILogic")) {
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the AI will play a SpellAbility with the specified AiLogic
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected boolean checkAiLogic(final Player ai, final SpellAbility sa, final String aiLogic) {
|
||||||
|
final Card host = sa.getHostCard();
|
||||||
|
final Game game = ai.getGame();
|
||||||
ZoneType choiceZone = ZoneType.Battlefield;
|
ZoneType choiceZone = ZoneType.Battlefield;
|
||||||
String logic = sa.getParam("AILogic");
|
|
||||||
if (sa.hasParam("ChoiceZone")) {
|
if (sa.hasParam("ChoiceZone")) {
|
||||||
choiceZone = ZoneType.smartValueOf(sa.getParam("ChoiceZone"));
|
choiceZone = ZoneType.smartValueOf(sa.getParam("ChoiceZone"));
|
||||||
}
|
}
|
||||||
@@ -48,27 +63,27 @@ public class ChooseCardAi extends SpellAbilityAi {
|
|||||||
choices = CardLists.getValidCards(choices, sa.getParam("Choices"), host.getController(), host);
|
choices = CardLists.getValidCards(choices, sa.getParam("Choices"), host.getController(), host);
|
||||||
}
|
}
|
||||||
if (sa.hasParam("TargetControls")) {
|
if (sa.hasParam("TargetControls")) {
|
||||||
choices = CardLists.filterControlledBy(choices, ai.getOpponent());
|
choices = CardLists.filterControlledBy(choices, ai.getOpponents());
|
||||||
}
|
}
|
||||||
if (logic.equals("AtLeast1") || logic.equals("OppPreferred")) {
|
if (aiLogic.equals("AtLeast1") || aiLogic.equals("OppPreferred")) {
|
||||||
if (choices.isEmpty()) {
|
if (choices.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if (logic.equals("AtLeast2") || logic.equals("BestBlocker")) {
|
} else if (aiLogic.equals("AtLeast2") || aiLogic.equals("BestBlocker")) {
|
||||||
if (choices.size() < 2) {
|
if (choices.size() < 2) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if (logic.equals("Clone") || logic.equals("Vesuva")) {
|
} else if (aiLogic.equals("Clone") || aiLogic.equals("Vesuva")) {
|
||||||
final String filter = logic.equals("Clone") ? "Permanent.YouDontCtrl,Permanent.nonLegendary" :
|
final String filter = aiLogic.equals("Clone") ? "Permanent.YouDontCtrl,Permanent.nonLegendary"
|
||||||
"Permanent.YouDontCtrl+notnamedVesuva,Permanent.nonLegendary+notnamedVesuva";
|
: "Permanent.YouDontCtrl+notnamedVesuva,Permanent.nonLegendary+notnamedVesuva";
|
||||||
|
|
||||||
choices = CardLists.getValidCards(choices, filter, host.getController(), host);
|
choices = CardLists.getValidCards(choices, filter, host.getController(), host);
|
||||||
if (choices.isEmpty()) {
|
if (choices.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if (logic.equals("Never")) {
|
} else if (aiLogic.equals("Never")) {
|
||||||
return false;
|
return false;
|
||||||
} else if (logic.equals("NeedsPrevention")) {
|
} else if (aiLogic.equals("NeedsPrevention")) {
|
||||||
if (!game.getPhaseHandler().is(PhaseType.COMBAT_DECLARE_BLOCKERS)) {
|
if (!game.getPhaseHandler().is(PhaseType.COMBAT_DECLARE_BLOCKERS)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -86,7 +101,7 @@ public class ChooseCardAi extends SpellAbilityAi {
|
|||||||
if (choices.isEmpty()) {
|
if (choices.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if (logic.equals("Ashiok")) {
|
} else if (aiLogic.equals("Ashiok")) {
|
||||||
final int loyalty = host.getCounters(CounterType.LOYALTY) - 1;
|
final int loyalty = host.getCounters(CounterType.LOYALTY) - 1;
|
||||||
for (int i = loyalty; i >= 0; i--) {
|
for (int i = loyalty; i >= 0; i--) {
|
||||||
host.setSVar("ChosenX", "Number$" + i);
|
host.setSVar("ChosenX", "Number$" + i);
|
||||||
@@ -100,11 +115,11 @@ public class ChooseCardAi extends SpellAbilityAi {
|
|||||||
if (choices.isEmpty()) {
|
if (choices.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if (logic.equals("RandomNonLand")) {
|
} else if (aiLogic.equals("RandomNonLand")) {
|
||||||
if (CardLists.getValidCards(choices, "Card.nonLand", host.getController(), host).isEmpty()) {
|
if (CardLists.getValidCards(choices, "Card.nonLand", host.getController(), host).isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if (logic.equals("Duneblast")) {
|
} else if (aiLogic.equals("Duneblast")) {
|
||||||
CardCollection aiCreatures = ai.getCreaturesInPlay();
|
CardCollection aiCreatures = ai.getCreaturesInPlay();
|
||||||
CardCollection oppCreatures = ai.getOpponent().getCreaturesInPlay();
|
CardCollection oppCreatures = ai.getOpponent().getCreaturesInPlay();
|
||||||
aiCreatures = CardLists.getNotKeyword(aiCreatures, "Indestructible");
|
aiCreatures = CardLists.getNotKeyword(aiCreatures, "Indestructible");
|
||||||
@@ -124,7 +139,6 @@ public class ChooseCardAi extends SpellAbilityAi {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,6 +153,7 @@ public class ChooseCardAi extends SpellAbilityAi {
|
|||||||
@Override
|
@Override
|
||||||
public Card chooseSingleCard(final Player ai, SpellAbility sa, Iterable<Card> options, boolean isOptional, Player targetedPlayer) {
|
public Card chooseSingleCard(final Player ai, SpellAbility sa, Iterable<Card> options, boolean isOptional, Player targetedPlayer) {
|
||||||
final Card host = sa.getHostCard();
|
final Card host = sa.getHostCard();
|
||||||
|
final Player ctrl = host.getController();
|
||||||
final String logic = sa.getParam("AILogic");
|
final String logic = sa.getParam("AILogic");
|
||||||
Card choice = null;
|
Card choice = null;
|
||||||
if (logic == null) {
|
if (logic == null) {
|
||||||
@@ -155,8 +170,9 @@ public class ChooseCardAi extends SpellAbilityAi {
|
|||||||
final String filter = logic.equals("Clone") ? "Permanent.YouDontCtrl,Permanent.nonLegendary" :
|
final String filter = logic.equals("Clone") ? "Permanent.YouDontCtrl,Permanent.nonLegendary" :
|
||||||
"Permanent.YouDontCtrl+notnamedVesuva,Permanent.nonLegendary+notnamedVesuva";
|
"Permanent.YouDontCtrl+notnamedVesuva,Permanent.nonLegendary+notnamedVesuva";
|
||||||
|
|
||||||
if (!CardLists.getValidCards(options, filter, host.getController(), host).isEmpty()) {
|
CardCollection newOptions = CardLists.getValidCards(options, filter.split(","), ctrl, host, sa);
|
||||||
options = CardLists.getValidCards(options, filter, host.getController(), host);
|
if (!newOptions.isEmpty()) {
|
||||||
|
options = newOptions;
|
||||||
}
|
}
|
||||||
choice = ComputerUtilCard.getBestAI(options);
|
choice = ComputerUtilCard.getBestAI(options);
|
||||||
if (logic.equals("Vesuva") && "Vesuva".equals(choice.getName())) {
|
if (logic.equals("Vesuva") && "Vesuva".equals(choice.getName())) {
|
||||||
@@ -166,8 +182,10 @@ public class ChooseCardAi extends SpellAbilityAi {
|
|||||||
options = CardLists.getValidCards(options, "Card.nonLand", host.getController(), host);
|
options = CardLists.getValidCards(options, "Card.nonLand", host.getController(), host);
|
||||||
choice = Aggregates.random(options);
|
choice = Aggregates.random(options);
|
||||||
} else if (logic.equals("Untap")) {
|
} else if (logic.equals("Untap")) {
|
||||||
if (!CardLists.getValidCards(options, "Permanent.YouCtrl,Permanent.tapped", host.getController(), host).isEmpty()) {
|
final String filter = "Permanent.YouCtrl,Permanent.tapped";
|
||||||
options = CardLists.getValidCards(options, "Permanent.YouCtrl,Permanent.tapped", host.getController(), host);
|
CardCollection newOptions = CardLists.getValidCards(options, filter.split(","), ctrl, host, sa);
|
||||||
|
if (!newOptions.isEmpty()) {
|
||||||
|
options = newOptions;
|
||||||
}
|
}
|
||||||
choice = ComputerUtilCard.getBestAI(options);
|
choice = ComputerUtilCard.getBestAI(options);
|
||||||
} else if (logic.equals("NeedsPrevention")) {
|
} else if (logic.equals("NeedsPrevention")) {
|
||||||
|
|||||||
Reference in New Issue
Block a user