- Restructured parts of AI discarding. All choosing is handled in ComputerUtil.AI_discardNumType. AIPlayer.discard will use this.

This commit is contained in:
Sloth
2011-09-06 08:42:44 +00:00
parent 9b8fc046ad
commit c7708509e5
2 changed files with 46 additions and 36 deletions

View File

@@ -131,30 +131,11 @@ public class AIPlayer extends Player {
public final CardList discard(final int num, final SpellAbility sa, final boolean duringResolution) { public final CardList discard(final int num, final SpellAbility sa, final boolean duringResolution) {
int max = AllZoneUtil.getPlayerHand(this).size(); int max = AllZoneUtil.getPlayerHand(this).size();
max = Math.min(max, num); max = Math.min(max, num);
CardList discarded = new CardList(); CardList discarded = ComputerUtil.AI_discardNumType(max, null, sa);
for (int i = 0; i < max; i++) { for (int i = 0; i < discarded.size(); i++) {
CardList hand = AllZoneUtil.getPlayerHand(this); doDiscard(discarded.get(i), sa);
if (hand.size() > 0) {
CardList basicLandsInPlay = AllZoneUtil.getPlayerTypeInPlay(this, "Basic");
if (basicLandsInPlay.size() > 5) {
CardList basicLandsInHand = hand.getType("Basic");
if (basicLandsInHand.size() > 0) {
discarded.add(hand.get(0));
doDiscard(basicLandsInHand.get(CardUtil.getRandomIndex(basicLandsInHand)), sa);
} else {
CardListUtil.sortAttackLowFirst(hand);
CardListUtil.sortNonFlyingFirst(hand);
discarded.add(hand.get(0));
doDiscard(hand.get(0), sa);
}
} else {
CardListUtil.sortCMC(hand);
discarded.add(hand.get(0));
doDiscard(hand.get(0), sa);
}
}
} }
return discarded; return discarded;
} //end discard } //end discard

View File

@@ -845,12 +845,15 @@ public class ComputerUtil {
* @return a {@link forge.Card} object. * @return a {@link forge.Card} object.
*/ */
static public Card getCardPreference(Card activate, String pref, CardList typeList) { static public Card getCardPreference(Card activate, String pref, CardList typeList) {
String[] prefValid = activate.getSVar("AIPreference").split("\\$");
if (prefValid[0].equals(pref)) { if (activate != null) {
CardList prefList = typeList.getValidCards(prefValid[1].split(","), activate.getController(), activate); String[] prefValid = activate.getSVar("AIPreference").split("\\$");
if (prefList.size() != 0) { if (prefValid[0].equals(pref)) {
prefList.shuffle(); CardList prefList = typeList.getValidCards(prefValid[1].split(","), activate.getController(), activate);
return prefList.get(0); if (prefList.size() != 0) {
prefList.shuffle();
return prefList.get(0);
}
} }
} }
if (pref.contains("SacCost")) { // search for permanents with SacMe if (pref.contains("SacCost")) { // search for permanents with SacMe
@@ -929,13 +932,20 @@ public class ComputerUtil {
* <p>AI_discardNumType.</p> * <p>AI_discardNumType.</p>
* *
* @param numDiscard a int. * @param numDiscard a int.
* @param uTypes an array of {@link java.lang.String} objects. * @param uTypes an array of {@link java.lang.String} objects. May be null for no restrictions.
* @param sa a {@link forge.card.spellability.SpellAbility} object. * @param sa a {@link forge.card.spellability.SpellAbility} object.
* @return a CardList of discarded cards. * @return a CardList of discarded cards.
*/ */
static public CardList AI_discardNumType(int numDiscard, String[] uTypes, SpellAbility sa) { static public CardList AI_discardNumType(int numDiscard, String[] uTypes, SpellAbility sa) {
CardList hand = AllZoneUtil.getPlayerHand(AllZone.getComputerPlayer()); CardList hand = AllZoneUtil.getPlayerHand(AllZone.getComputerPlayer());
hand = hand.getValidCards(uTypes, sa.getActivatingPlayer(), sa.getSourceCard()); Card sourceCard = null;
if (uTypes != null && sa != null) {
hand = hand.getValidCards(uTypes, sa.getActivatingPlayer(), sa.getSourceCard());
}
if (sa != null) {
sourceCard = sa.getSourceCard();
}
if (hand.size() < numDiscard){ if (hand.size() < numDiscard){
return null; return null;
@@ -944,8 +954,9 @@ public class ComputerUtil {
CardList discardList = new CardList(); CardList discardList = new CardList();
int count = 0; int count = 0;
// look for good discards
while (count < numDiscard) { while (count < numDiscard) {
Card prefCard = getCardPreference(sa.getSourceCard(), "DiscardCost", hand); Card prefCard = getCardPreference(sourceCard, "DiscardCost", hand);
if (prefCard != null) { if (prefCard != null) {
discardList.add(prefCard); discardList.add(prefCard);
hand.remove(prefCard); hand.remove(prefCard);
@@ -956,10 +967,28 @@ public class ComputerUtil {
int discardsLeft = numDiscard - count; int discardsLeft = numDiscard - count;
CardListUtil.sortCMC(hand); // chose rest
hand.reverse(); for (int i = 0; i < discardsLeft; i++) {
for (int i = 0; i < discardsLeft; i++){ if (hand.size() <= 0) {
discardList.add(hand.get(i)); continue;
}
CardList landsInPlay = AllZoneUtil.getPlayerTypeInPlay(AllZone.getComputerPlayer(), "Land");
if (landsInPlay.size() > 5) {
CardList landsInHand = hand.getType("Land");
if (landsInHand.size() > 0) { //discard lands
discardList.add(landsInHand.get(0));
hand.remove(landsInHand.get(0));
} else { //discard low costed stuff
CardListUtil.sortCMC(hand);
hand.reverse();
discardList.add(hand.get(0));
hand.remove(hand.get(0));
}
} else { //discard high costed stuff
CardListUtil.sortCMC(hand);
discardList.add(hand.get(0));
hand.remove(hand.get(0));
}
} }
return discardList; return discardList;