doUntap.
()
| 84 | * </p> |
| 85 | */ |
| 86 | private void doUntap() { |
| 87 | final Player active = game.getPhaseHandler().getPlayerTurn(); |
| 88 | Map<Player, CardCollection> untapMap = Maps.newHashMap(); |
| 89 | |
| 90 | CardCollection untapList = new CardCollection(active.getCardsIn(ZoneType.Battlefield)); |
| 91 | |
| 92 | CardZoneTable triggerList = new CardZoneTable(game.getLastStateBattlefield(), game.getLastStateGraveyard()); |
| 93 | CardCollection bounceList = CardLists.getKeyword(untapList, "During your next untap step, as you untap your permanents, return CARDNAME to its owner's hand."); |
| 94 | for (final Card c : bounceList) { |
| 95 | Card moved = game.getAction().moveToHand(c, null); |
| 96 | triggerList.put(ZoneType.Battlefield, moved.getZone().getZoneType(), moved); |
| 97 | } |
| 98 | triggerList.triggerChangesZoneAll(game, null); |
| 99 | untapList.removeAll(bounceList); |
| 100 | |
| 101 | final Map<String, Integer> restrictUntap = Maps.newHashMap(); |
| 102 | for (KeywordInterface ki : active.getKeywords()) { |
| 103 | String kw = ki.getOriginal(); |
| 104 | if (kw.startsWith("UntapAdjust")) { |
| 105 | String[] parse = kw.split(":"); |
| 106 | if (!restrictUntap.containsKey(parse[1]) |
| 107 | || Integer.parseInt(parse[2]) < restrictUntap.get(parse[1])) { |
| 108 | restrictUntap.put(parse[1], Integer.parseInt(parse[2])); |
| 109 | } |
| 110 | } |
| 111 | if (kw.startsWith("OnlyUntapChosen")) { |
| 112 | List<String> validTypes = Arrays.asList(kw.split(":")[1].split(",")); |
| 113 | final String chosen = active.getController().chooseSomeType("Card", new SpellAbility.EmptySa(ApiType.ChooseType, null, active), validTypes); |
| 114 | untapList = CardLists.getType(untapList, chosen); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | untapList = CardLists.filter(untapList, c -> c.canUntap(active, false)); |
| 119 | |
| 120 | final String[] restrict = restrictUntap.keySet().toArray(new String[0]); |
| 121 | final CardCollection restrictList = CardLists.getValidCards(untapList, restrict, active, null, null); |
| 122 | untapList.removeAll(restrictList); |
| 123 | CardCollection restrictUntapped = new CardCollection(); |
| 124 | while (!restrictList.isEmpty()) { |
| 125 | Map<String, Integer> remaining = Maps.newHashMap(restrictUntap); |
| 126 | for (Entry<String, Integer> entry : remaining.entrySet()) { |
| 127 | if (entry.getValue() == 0) { |
| 128 | restrictList.removeAll(CardLists.getValidCards(restrictList, entry.getKey(), active, null, null)); |
| 129 | restrictUntap.remove(entry.getKey()); |
| 130 | } |
| 131 | } |
| 132 | Card chosen = active.getController().chooseSingleEntityForEffect(restrictList, new SpellAbility.EmptySa(ApiType.Untap, null, active), |
| 133 | "Select a card to untap\r\n(Selected:" + restrictUntapped + ")\r\n" + "Remaining cards that can untap: " + remaining, null); |
| 134 | if (chosen != null) { |
| 135 | for (Entry<String, Integer> rest : restrictUntap.entrySet()) { |
| 136 | if (chosen.isValid(rest.getKey(), active, null, null)) { |
| 137 | restrictUntap.put(rest.getKey(), rest.getValue() - 1); |
| 138 | } |
| 139 | } |
| 140 | untapList.add(chosen); |
| 141 | restrictList.remove(chosen); |
| 142 | } |
| 143 | } |
no test coverage detected