- Re-factored CharmAi further

This commit is contained in:
excessum
2016-07-25 14:00:46 +00:00
parent e43fe8abc6
commit c58a1495ac

View File

@@ -28,13 +28,24 @@ public class CharmAi extends SpellAbilityAi {
// Reset the chosen list otherwise it will be locked in forever by earlier calls // Reset the chosen list otherwise it will be locked in forever by earlier calls
sa.setChosenList(null); sa.setChosenList(null);
List<AbilitySub> choices = CharmEffect.makePossibleOptions(sa); List<AbilitySub> choices = CharmEffect.makePossibleOptions(sa);
List<AbilitySub> chosenList = min > 1 ? chooseMultipleOptionsAi(choices, ai, min) List<AbilitySub> chosenList;
: chooseOptionsAi(choices, sa, ai, timingRight, num, min, sa.hasParam("CanRepeatModes"), false);
if (!ai.equals(sa.getActivatingPlayer())) {
// This branch is for "An Opponent chooses" Charm spells from Alliances
// Current just choose the first available spell, which seem generally less disastrous for the AI.
//return choices.subList(0, 1);
chosenList = choices.subList(1, choices.size());
} else if ("Triskaidekaphobia".equals(sa.getHostCard().getName())) {
chosenList = chooseTriskaidekaphobia(choices, ai);
} else {
chosenList = min > 1 ? chooseMultipleOptionsAi(choices, ai, min)
: chooseOptionsAi(choices, ai, timingRight, num, min, sa.hasParam("CanRepeatModes"), false);
}
if (chosenList.isEmpty()) { if (chosenList.isEmpty()) {
if (timingRight) { if (timingRight) {
// Set minimum choices for triggers where chooseMultipleOptionsAi() returns null // Set minimum choices for triggers where chooseMultipleOptionsAi() returns null
chosenList = chooseOptionsAi(choices, sa, ai, true, num, min, sa.hasParam("CanRepeatModes"), false); chosenList = chooseOptionsAi(choices, ai, true, num, min, sa.hasParam("CanRepeatModes"), false);
} else { } else {
return false; return false;
} }
@@ -45,16 +56,53 @@ public class CharmAi extends SpellAbilityAi {
return r.nextFloat() <= Math.pow(.6667, sa.getActivationsThisTurn()); return r.nextFloat() <= Math.pow(.6667, sa.getActivationsThisTurn());
} }
private List<AbilitySub> chooseOptionsAi(List<AbilitySub> choices, SpellAbility sa, final Player ai, private List<AbilitySub> chooseOptionsAi(List<AbilitySub> choices, final Player ai, boolean playNow, int num,
boolean playNow, int num, int min, boolean allowRepeat, boolean opponentChoser) { int min, boolean allowRepeat, boolean opponentChoser) {
List<AbilitySub> chosenList = new ArrayList<AbilitySub>(); List<AbilitySub> chosenList = new ArrayList<AbilitySub>();
// Make choice(s)
AiController aic = ((PlayerControllerAi) ai.getController()).getAi();
for (int i = 0; i < num; i++) {
AbilitySub thisPick = null;
for (SpellAbility sub : choices) {
sub.setActivatingPlayer(ai);
if (!playNow && AiPlayDecision.WillPlay == aic.canPlaySa(sub)) {
thisPick = (AbilitySub) sub;
choices.remove(sub);
playNow = true;
break;
}
if ((playNow || i < num - 1) && aic.doTrigger(sub, false)) {
thisPick = (AbilitySub) sub;
choices.remove(sub);
break;
}
}
if (thisPick != null) {
chosenList.add(thisPick);
}
}
// Set minimum choices for triggers
if (playNow && chosenList.size() < min) {
for (int i = 0; i < min; i++) {
AbilitySub thisPick = null;
for (SpellAbility sub : choices) {
sub.setActivatingPlayer(ai);
if (aic.doTrigger(sub, true)) {
thisPick = (AbilitySub) sub;
choices.remove(sub);
break;
}
}
if (thisPick != null) {
chosenList.add(thisPick);
}
}
}
return chosenList;
}
if (opponentChoser) { private List<AbilitySub> chooseTriskaidekaphobia(List<AbilitySub> choices, final Player ai) {
// This branch is for "An Opponent chooses" Charm spells from Alliances List<AbilitySub> chosenList = new ArrayList<AbilitySub>();
// Current just choose the first available spell, which seem generally less disastrous for the AI.
//return choices.subList(0, 1);
return choices.subList(1, choices.size());
} else if ("Triskaidekaphobia".equals(sa.getHostCard().getName())) {
AbilitySub gain = choices.get(0); AbilitySub gain = choices.get(0);
AbilitySub lose = choices.get(1); AbilitySub lose = choices.get(1);
FCollection<Player> opponents = ai.getOpponents(); FCollection<Player> opponents = ai.getOpponents();
@@ -138,48 +186,6 @@ public class CharmAi extends SpellAbilityAi {
return chosenList; return chosenList;
} }
// Make choice(s)
AiController aic = ((PlayerControllerAi) ai.getController()).getAi();
for (int i = 0; i < num; i++) {
AbilitySub thisPick = null;
for (SpellAbility sub : choices) {
sub.setActivatingPlayer(ai);
if (!playNow && AiPlayDecision.WillPlay == aic.canPlaySa(sub)) {
thisPick = (AbilitySub) sub;
choices.remove(sub);
playNow = true;
break;
}
if ((playNow || i < num - 1) && aic.doTrigger(sub, false)) {
thisPick = (AbilitySub) sub;
choices.remove(sub);
break;
}
}
if (thisPick != null) {
chosenList.add(thisPick);
}
}
// Set minimum choices for triggers
if (playNow && chosenList.size() < min) {
for (int i = 0; i < min; i++) {
AbilitySub thisPick = null;
for (SpellAbility sub : choices) {
sub.setActivatingPlayer(ai);
if (aic.doTrigger(sub, true)) {
thisPick = (AbilitySub) sub;
choices.remove(sub);
break;
}
}
if (thisPick != null) {
chosenList.add(thisPick);
}
}
}
return chosenList;
}
// Choice selection for charms that require multiple choices (eg. Cryptic Command, DTK commands) // Choice selection for charms that require multiple choices (eg. Cryptic Command, DTK commands)
private List<AbilitySub> chooseMultipleOptionsAi(List<AbilitySub> choices, final Player ai, int min) { private List<AbilitySub> chooseMultipleOptionsAi(List<AbilitySub> choices, final Player ai, int min) {
AbilitySub goodChoice = null; AbilitySub goodChoice = null;