From 1b9f75f29b31515281ec487ce1c07e3989edccf2 Mon Sep 17 00:00:00 2001 From: magenoxx Date: Sun, 6 Nov 2011 12:54:08 +0300 Subject: [PATCH] Several fixed in AI targeting. Fixed Issue 371. --- .../java/mage/player/ai/ComputerPlayer.java | 147 ++++++++++++++---- 1 file changed, 118 insertions(+), 29 deletions(-) diff --git a/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java b/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java index 721e26eddbd..baadad505f3 100644 --- a/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java +++ b/Mage.Server.Plugins/Mage.Player.AI/src/main/java/mage/player/ai/ComputerPlayer.java @@ -153,13 +153,26 @@ public class ComputerPlayer> extends PlayerImpl i target.add(playerId, game); return true; } + if (target.isRequired()) { + if (target.canTarget(opponentId, game)) { + target.add(opponentId, game); + return true; + } + } } else { if (target.canTarget(opponentId, game)) { target.add(opponentId, game); return true; } + if (target.isRequired()) { + if (target.canTarget(playerId, game)) { + target.add(playerId, game); + return true; + } + } } + return false; } if (target instanceof TargetDiscard) { findPlayables(game); @@ -171,12 +184,15 @@ public class ComputerPlayer> extends PlayerImpl i } } } - if (hand.size() > 0) { - if (target.canTarget(hand.toArray(new UUID[0])[0], game)) { - target.add(hand.toArray(new UUID[0])[0], game); - return true; - } - } + if (hand.size() > 0) { + for (int i = 0; i < hand.size(); i++) { + if (target.canTarget(hand.toArray(new UUID[0])[i], game)) { + target.add(hand.toArray(new UUID[0])[i], game); + return true; + } + } + } + return false; } if (target instanceof TargetControlledPermanent) { List targets; @@ -228,18 +244,30 @@ public class ComputerPlayer> extends PlayerImpl i target.addTarget(playerId, source, game); return true; } + if (target.isRequired()) { + if (target.canTarget(opponentId, source, game)) { + target.addTarget(opponentId, source, game); + return true; + } + } } else { if (target.canTarget(opponentId, source, game)) { target.addTarget(opponentId, source, game); return true; } + if (target.isRequired()) { + if (target.canTarget(playerId, source, game)) { + target.addTarget(playerId, source, game); + return true; + } + } } return false; } if (target instanceof TargetDiscard || target instanceof TargetCardInHand) { if (outcome.isGood()) { - Card card = pickBestCard(new ArrayList(hand.getCards(game)), null); + Card card = pickBestCard(new ArrayList(hand.getCards(game)), null, target, source, game); if (card != null) { if (target.canTarget(card.getId(), source, game)) { target.addTarget(card.getId(), source, game); @@ -258,10 +286,12 @@ public class ComputerPlayer> extends PlayerImpl i } } if (hand.size() > 0) { - if (target.canTarget(hand.toArray(new UUID[0])[0], source, game)) { - target.addTarget(hand.toArray(new UUID[0])[0], source, game); - return true; - } + for (int i = 0; i < hand.size(); i++) { + if (target.canTarget(hand.toArray(new UUID[0])[i], source, game)) { + target.addTarget(hand.toArray(new UUID[0])[i], source, game); + return true; + } + } } } return false; @@ -304,15 +334,15 @@ public class ComputerPlayer> extends PlayerImpl i else { targets = threats(opponentId, ((FilterCreatureOrPlayer)t.getFilter()).getCreatureFilter(), game, target.getTargets()); } - for (Permanent permanent: targets) { - List alreadyTargetted = target.getTargets(); - if (t.canTarget(playerId, permanent.getId(), source, game)) { - if ( alreadyTargetted != null && !alreadyTargetted.contains(permanent.getId()) ) { - target.addTarget(permanent.getId(), source, game); - return true; - } - } - } + for (Permanent permanent : targets) { + List alreadyTargetted = target.getTargets(); + if (t.canTarget(playerId, permanent.getId(), source, game)) { + if (alreadyTargetted != null && !alreadyTargetted.contains(permanent.getId())) { + target.addTarget(permanent.getId(), source, game); + return true; + } + } + } if (outcome.isGood()) { if (target.canTarget(playerId, source, game)) { target.addTarget(playerId, source, game); @@ -325,7 +355,8 @@ public class ComputerPlayer> extends PlayerImpl i return true; } } - return false; + if (!target.isRequired()) + return false; } if (target instanceof TargetCardInGraveyard) { List cards = new ArrayList(); @@ -362,13 +393,13 @@ public class ComputerPlayer> extends PlayerImpl i } protected Card pickTarget(List cards, Outcome outcome, Target target, Ability source, Game game) { - Card card = null; + Card card; while (!cards.isEmpty()) { if (outcome.isGood()) { - card = pickBestCard(cards, null); + card = pickBestCard(cards, null, target, source, game); } else { - card = pickWorstCard(cards, null); + card = pickWorstCard(cards, null, target, source, game); } if (source != null) { if (target.canTarget(card.getId(), source, game)) { @@ -1016,7 +1047,7 @@ public class ComputerPlayer> extends PlayerImpl i tournament.submitDeck(playerId, deck); } - public Card pickBestCard(List cards, List chosenColors) { + public Card pickBestCard(List cards, List chosenColors) { if (cards.isEmpty()) { return null; } @@ -1024,14 +1055,72 @@ public class ComputerPlayer> extends PlayerImpl i int maxScore = 0; for (Card card : cards) { int score = RateCard.rateCard(card, chosenColors); - if (bestCard == null || score > maxScore) { - maxScore = score; - bestCard = card; - } + if (bestCard == null || score > maxScore) { + maxScore = score; + bestCard = card; + } } return bestCard; } + public Card pickBestCard(List cards, List chosenColors, Target target, Ability source, Game game) { + if (cards.isEmpty()) { + return null; + } + Card bestCard = null; + int maxScore = 0; + for (Card card : cards) { + int score = RateCard.rateCard(card, chosenColors); + boolean betterCard = false; + if (bestCard == null) { // we need any card to prevent NPE in callers + betterCard = true; + } else if (score > maxScore) { // we need better card + if (target != null && source != null && game != null) { + // but also check it can be targeted + betterCard = target.canTarget(card.getId(), source, game); + } else { + // target object wasn't provided, so acceptings it anyway + betterCard = true; + } + } + // is it better than previous one? + if (betterCard) { + maxScore = score; + bestCard = card; + } + } + return bestCard; + } + + public Card pickWorstCard(List cards, List chosenColors, Target target, Ability source, Game game) { + if (cards.isEmpty()) { + return null; + } + Card worstCard = null; + int minScore = Integer.MAX_VALUE; + for (Card card : cards) { + int score = RateCard.rateCard(card, chosenColors); + boolean worseCard = false; + if (worstCard == null) { // we need any card to prevent NPE in callers + worseCard = true; + } else if (score < minScore) { // we need worse card + if (target != null && source != null && game != null) { + // but also check it can be targeted + worseCard = target.canTarget(card.getId(), source, game); + } else { + // target object wasn't provided, so accepting it anyway + worseCard = true; + } + } + // is it worse than previous one? + if (worseCard) { + minScore = score; + worstCard = card; + } + } + return worstCard; + } + public Card pickWorstCard(List cards, List chosenColors) { if (cards.isEmpty()) { return null;