From 04da295f49e587a06f28f6e55f22ca17d5b69731 Mon Sep 17 00:00:00 2001 From: Agetian Date: Sun, 24 Sep 2017 04:48:07 +0000 Subject: [PATCH] - Some improvements to DoNotDiscardIfAble discard AI for corner cases, to avoid (very rare) situations where the AI would not discard anything or crash. git-svn-id: http://svn.slightlymagic.net/forge/trunk@35736 269b9781-a132-4a9b-9d4e-f004f1b56b58 --- .../src/main/java/forge/ai/AiController.java | 23 +++++++++++++++++++ .../main/java/forge/ai/ComputerUtilCard.java | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/forge-ai/src/main/java/forge/ai/AiController.java b/forge-ai/src/main/java/forge/ai/AiController.java index 64218470..79cf3567 100644 --- a/forge-ai/src/main/java/forge/ai/AiController.java +++ b/forge-ai/src/main/java/forge/ai/AiController.java @@ -943,6 +943,29 @@ public class AiController { // for more discard options worst = ComputerUtilCard.getCheapestSpellAI(validCards); } + if (worst == null && !validCards.isEmpty()) { + // still nothing chosen, so choose the first thing that works, trying not to make DoNotDiscardIfAble + // discards + for (Card c : validCards) { + if (!c.hasSVar("DoNotDiscardIfAble")) { + worst = c; + break; + } + } + // Only DoNotDiscardIfAble cards? If we have a duplicate for something, discard it + if (worst == null) { + for (Card c : validCards) { + if (CardLists.filter(player.getCardsIn(ZoneType.Hand), CardPredicates.nameEquals(c.getName())).size() > 1) { + worst = c; + break; + } + } + if (worst == null) { + // Otherwise just grab a random card and discard it + worst = Aggregates.random(validCards); + } + } + } discardList.add(worst); validCards.remove(worst); } diff --git a/forge-ai/src/main/java/forge/ai/ComputerUtilCard.java b/forge-ai/src/main/java/forge/ai/ComputerUtilCard.java index 41358158..b4a5e183 100644 --- a/forge-ai/src/main/java/forge/ai/ComputerUtilCard.java +++ b/forge-ai/src/main/java/forge/ai/ComputerUtilCard.java @@ -374,6 +374,10 @@ public class ComputerUtilCard { Predicates.or(CardPredicates.isType("Instant"), CardPredicates.isType("Sorcery"))); Collections.sort(cc, CardLists.CmcComparatorInv); + if (cc.isEmpty()) { + return null; + } + Card cheapest = cc.getLast(); if (cheapest.hasSVar("DoNotDiscardIfAble")) { for (int i = cc.size() - 1; i >= 0; i--) {