From 55a5963ec8ede45f82c1b7a99b9c86c75e81550a Mon Sep 17 00:00:00 2001 From: austinio7116 Date: Fri, 26 May 2017 19:22:19 +0000 Subject: [PATCH] Change to sorting of creature types to include tokens generated from abilities so that for example Metallic Mimic select boxes will show Servo's near the top of the list in token decks. The change also means the AI will select better creature types in such decks. --- .../main/java/forge/ai/ComputerUtilCard.java | 44 +++++++++++++++ .../forge/player/PlayerControllerHuman.java | 55 +++++++++++++++++-- 2 files changed, 95 insertions(+), 4 deletions(-) diff --git a/forge-ai/src/main/java/forge/ai/ComputerUtilCard.java b/forge-ai/src/main/java/forge/ai/ComputerUtilCard.java index fd17eb71bc2..9ce176f29bf 100644 --- a/forge-ai/src/main/java/forge/ai/ComputerUtilCard.java +++ b/forge-ai/src/main/java/forge/ai/ComputerUtilCard.java @@ -8,6 +8,8 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import forge.game.ability.AbilityFactory; +import forge.game.keyword.Keyword; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.MutablePair; import org.apache.commons.lang3.tuple.Pair; @@ -616,6 +618,48 @@ public class ComputerUtilCard { } } } + //also take into account abilities that generate tokens + for(SpellAbility sa:c.getSpellAbilities()){ + if(sa.getParam("TokenName")!=null){ + for(String var:sa.getParam("TokenName").split(" ")){ + if (valid.contains(var)) { + if (!map.containsKey(var)) { + map.put(var, 1); + } else { + map.put(var, map.get(var) + 1); + } + } + } + } + } + for(Trigger t:c.getTriggers()){ + final String execute = t.getMapParams().get("Execute"); + if (execute == null) { + continue; + } + final SpellAbility sa = AbilityFactory.getAbility(c.getSVar(execute), c); + if(sa.getParam("TokenName")!=null){ + String tokenName=sa.getParam("TokenName"); + for(String var:tokenName.split(" ")){ + if (valid.contains(var)) { + if (!map.containsKey(var)) { + map.put(var, 1); + } else { + map.put(var, map.get(var) + 1); + } + } + } + } + } + for(String k: c.getCurrentState().getIntrinsicKeywords()){ + if(k.split(":")[0].equals(Keyword.FABRICATE.toString())){ + if (!map.containsKey("Servo")) { + map.put("Servo", 1); + } else { + map.put("Servo", map.get("Servo") + 1); + } + } + } } // for int max = 0; diff --git a/forge-gui/src/main/java/forge/player/PlayerControllerHuman.java b/forge-gui/src/main/java/forge/player/PlayerControllerHuman.java index 19831088a9d..870a839b275 100644 --- a/forge-gui/src/main/java/forge/player/PlayerControllerHuman.java +++ b/forge-gui/src/main/java/forge/player/PlayerControllerHuman.java @@ -18,6 +18,8 @@ import java.util.Map; import java.util.Set; import java.util.Map.Entry; +import forge.game.ability.AbilityFactory; +import forge.game.keyword.Keyword; import org.apache.commons.lang3.Range; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -820,14 +822,59 @@ public class PlayerControllerHuman //sort creature types such that those most prevalent in player's deck are sorted to the top private void sortCreatureTypes(List types) { //build map of creature types in player's main deck against the occurrences of each - CardPool pool = player.getRegisteredPlayer().getDeck().getMain(); + CardCollection pool = CardLists.filterControlledBy(game.getCardsInGame(),player); HashMap typesInDeck = new HashMap(); - for (Entry entry : pool) { - Set cardCreatureTypes = entry.getKey().getRules().getType().getCreatureTypes(); + for (Card c : pool) { + if(c.getRules()==null || c.getRules().getType()==null){ + continue; + } + Set cardCreatureTypes = c.getRules().getType().getCreatureTypes(); for (String type : cardCreatureTypes) { Integer count = typesInDeck.get(type); if (count == null) { count = 0; } - typesInDeck.put(type, count + entry.getValue()); + typesInDeck.put(type, count + 1); + } + //also take into account abilities that generate tokens + for(SpellAbility sa:c.getAllSpellAbilities()){ + if(sa.getParam("TokenName")!=null){ + for(String var:sa.getParam("TokenName").split(" ")){ + if (types.contains(var)) { + if (!typesInDeck.containsKey(var)) { + typesInDeck.put(var, 1); + } else { + typesInDeck.put(var, typesInDeck.get(var) + 1); + } + } + } + } + } + for(Trigger t:c.getTriggers()){ + final String execute = t.getMapParams().get("Execute"); + if (execute == null) { + continue; + } + final SpellAbility sa = AbilityFactory.getAbility(c.getSVar(execute), c); + if(sa.getParam("TokenName")!=null){ + String tokenName=sa.getParam("TokenName"); + for(String var:tokenName.split(" ")){ + if (types.contains(var)) { + if (!typesInDeck.containsKey(var)) { + typesInDeck.put(var, 1); + } else { + typesInDeck.put(var, typesInDeck.get(var) + 1); + } + } + } + } + } + for(String k: c.getCurrentState().getIntrinsicKeywords()){ + if(k.split(":")[0].equals(Keyword.FABRICATE.toString())){ + if (!typesInDeck.containsKey("Servo")) { + typesInDeck.put("Servo", 1); + } else { + typesInDeck.put("Servo", typesInDeck.get("Servo") + 1); + } + } } }