|
| 1 | +package mage.cards.k; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Objects; |
| 5 | +import java.util.Set; |
| 6 | +import java.util.UUID; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 9 | +import mage.MageObject; |
| 10 | +import mage.abilities.Ability; |
| 11 | +import mage.abilities.LoyaltyAbility; |
| 12 | +import mage.abilities.TriggeredAbilityImpl; |
| 13 | +import mage.abilities.effects.OneShotEffect; |
| 14 | +import mage.abilities.effects.common.CopyEffect; |
| 15 | +import mage.abilities.effects.common.CreateTokenEffect; |
| 16 | +import mage.abilities.effects.common.ExileTargetEffect; |
| 17 | +import mage.abilities.effects.keyword.SurveilEffect; |
| 18 | +import mage.abilities.keyword.FlyingAbility; |
| 19 | +import mage.cards.Card; |
| 20 | +import mage.cards.Cards; |
| 21 | +import mage.cards.CardsImpl; |
| 22 | +import mage.constants.Duration; |
| 23 | +import mage.constants.Outcome; |
| 24 | +import mage.constants.SubType; |
| 25 | +import mage.constants.SuperType; |
| 26 | +import mage.cards.CardImpl; |
| 27 | +import mage.cards.CardSetInfo; |
| 28 | +import mage.constants.CardType; |
| 29 | +import mage.constants.Zone; |
| 30 | +import mage.filter.FilterPermanent; |
| 31 | +import mage.filter.StaticFilters; |
| 32 | +import mage.filter.common.FilterCreaturePermanent; |
| 33 | +import mage.filter.predicate.permanent.ControllerIdPredicate; |
| 34 | +import mage.game.Game; |
| 35 | +import mage.game.events.GameEvent; |
| 36 | +import mage.game.events.ZoneChangeBatchEvent; |
| 37 | +import mage.game.events.ZoneChangeEvent; |
| 38 | +import mage.game.permanent.Permanent; |
| 39 | +import mage.game.permanent.PermanentCard; |
| 40 | +import mage.game.permanent.token.WhiteBlackSpiritToken; |
| 41 | +import mage.players.Player; |
| 42 | +import mage.target.TargetCard; |
| 43 | +import mage.target.TargetPermanent; |
| 44 | +import mage.target.common.TargetCardInExile; |
| 45 | +import mage.target.common.TargetCardInGraveyard; |
| 46 | +import mage.target.targetadjustment.TargetAdjuster; |
| 47 | +import mage.target.targetpointer.EachTargetPointer; |
| 48 | +import mage.target.targetpointer.FixedTargets; |
| 49 | +import mage.util.CardUtil; |
| 50 | +import mage.util.functions.CopyApplier; |
| 51 | + |
| 52 | +/** |
| 53 | + * |
| 54 | + * @author DominionSpy |
| 55 | + */ |
| 56 | +public final class KayaSpiritsJustice extends CardImpl { |
| 57 | + |
| 58 | + public KayaSpiritsJustice(UUID ownerId, CardSetInfo setInfo) { |
| 59 | + super(ownerId, setInfo, new CardType[]{CardType.PLANESWALKER}, "{2}{W}{B}"); |
| 60 | + |
| 61 | + this.supertype.add(SuperType.LEGENDARY); |
| 62 | + this.subtype.add(SubType.KAYA); |
| 63 | + this.setStartingLoyalty(3); |
| 64 | + |
| 65 | + // Whenever one or more creatures you control and/or creature cards in your graveyard are put into exile, you may choose a creature card from among them. |
| 66 | + // Until end of turn, target token you control becomes a copy of it, except it has flying. |
| 67 | + Ability ability = new KayaSpiritsJusticeTriggeredAbility(); |
| 68 | + ability.addTarget(new TargetPermanent(StaticFilters.FILTER_PERMANENT_TOKEN)); |
| 69 | + this.addAbility(ability); |
| 70 | + |
| 71 | + // +2: Surveil 2, then exile a card from a graveyard. |
| 72 | + ability = new LoyaltyAbility(new SurveilEffect(2, false), 2); |
| 73 | + ability.addEffect(new KayaSpiritsJusticeExileEffect().concatBy(", then")); |
| 74 | + this.addAbility(ability); |
| 75 | + // +1: Create a 1/1 white and black Spirit creature token with flying. |
| 76 | + ability = new LoyaltyAbility(new CreateTokenEffect(new WhiteBlackSpiritToken()), 1); |
| 77 | + this.addAbility(ability); |
| 78 | + // -2: Exile target creature you control. For each other player, exile up to one target creature that player controls. |
| 79 | + ability = new LoyaltyAbility(new ExileTargetEffect() |
| 80 | + .setText("exile target creature you control. For each other player, " + |
| 81 | + "exile up to one target creature that player controls") |
| 82 | + .setTargetPointer(new EachTargetPointer()), -2); |
| 83 | + ability.setTargetAdjuster(KayaSpiritsJusticeAdjuster.instance); |
| 84 | + this.addAbility(ability); |
| 85 | + } |
| 86 | + |
| 87 | + private KayaSpiritsJustice(final KayaSpiritsJustice card) { |
| 88 | + super(card); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public KayaSpiritsJustice copy() { |
| 93 | + return new KayaSpiritsJustice(this); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +class KayaSpiritsJusticeTriggeredAbility extends TriggeredAbilityImpl { |
| 98 | + |
| 99 | + KayaSpiritsJusticeTriggeredAbility() { |
| 100 | + super(Zone.BATTLEFIELD, new KayaSpiritsJusticeCopyEffect(), false); |
| 101 | + setTriggerPhrase("Whenever one or more creatures you control and/or creature cards in your graveyard are put into exile, " + |
| 102 | + "you may choose a creature card from among them. Until end of turn, target token you control becomes a copy of it, " + |
| 103 | + "except it has flying."); |
| 104 | + } |
| 105 | + |
| 106 | + private KayaSpiritsJusticeTriggeredAbility(final KayaSpiritsJusticeTriggeredAbility ability) { |
| 107 | + super(ability); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public KayaSpiritsJusticeTriggeredAbility copy() { |
| 112 | + return new KayaSpiritsJusticeTriggeredAbility(this); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public boolean checkEventType(GameEvent event, Game game) { |
| 117 | + return event.getType() == GameEvent.EventType.ZONE_CHANGE_BATCH; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public boolean checkTrigger(GameEvent event, Game game) { |
| 122 | + ZoneChangeBatchEvent zEvent = (ZoneChangeBatchEvent) event; |
| 123 | + if (zEvent == null) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + Set<Card> battlefieldCards = zEvent.getEvents() |
| 128 | + .stream() |
| 129 | + .filter(e -> e.getFromZone() == Zone.BATTLEFIELD) |
| 130 | + .filter(e -> e.getToZone() == Zone.EXILED) |
| 131 | + .map(ZoneChangeEvent::getTargetId) |
| 132 | + .filter(Objects::nonNull) |
| 133 | + .map(game::getCard) |
| 134 | + .filter(Objects::nonNull) |
| 135 | + .filter(card -> { |
| 136 | + Permanent permanent = game.getPermanentOrLKIBattlefield(card.getId()); |
| 137 | + return StaticFilters.FILTER_PERMANENT_CREATURE |
| 138 | + .match(permanent, getControllerId(), this, game); |
| 139 | + }) |
| 140 | + .collect(Collectors.toSet()); |
| 141 | + |
| 142 | + Set<Card> graveyardCards = zEvent.getEvents() |
| 143 | + .stream() |
| 144 | + .filter(e -> e.getFromZone() == Zone.GRAVEYARD) |
| 145 | + .filter(e -> e.getToZone() == Zone.EXILED) |
| 146 | + .map(ZoneChangeEvent::getTargetId) |
| 147 | + .filter(Objects::nonNull) |
| 148 | + .map(game::getCard) |
| 149 | + .filter(Objects::nonNull) |
| 150 | + .filter(card -> StaticFilters.FILTER_CARD_CREATURE |
| 151 | + .match(card, getControllerId(), this, game)) |
| 152 | + .collect(Collectors.toSet()); |
| 153 | + |
| 154 | + Set<Card> cards = new HashSet<>(battlefieldCards); |
| 155 | + cards.addAll(graveyardCards); |
| 156 | + if (cards.isEmpty()) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + getEffects().setTargetPointer(new FixedTargets(new CardsImpl(cards), game)); |
| 161 | + return true; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +class KayaSpiritsJusticeCopyEffect extends OneShotEffect { |
| 166 | + |
| 167 | + KayaSpiritsJusticeCopyEffect() { |
| 168 | + super(Outcome.Copy); |
| 169 | + } |
| 170 | + |
| 171 | + private KayaSpiritsJusticeCopyEffect(final KayaSpiritsJusticeCopyEffect effect) { |
| 172 | + super(effect); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public KayaSpiritsJusticeCopyEffect copy() { |
| 177 | + return new KayaSpiritsJusticeCopyEffect(this); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public boolean apply(Game game, Ability source) { |
| 182 | + Player controller = game.getPlayer(source.getControllerId()); |
| 183 | + Permanent copyToPermanent = game.getPermanent(source.getFirstTarget()); |
| 184 | + Cards exiledCards = new CardsImpl(getTargetPointer().getTargets(game, source)); |
| 185 | + if (controller == null || copyToPermanent == null || exiledCards.isEmpty()) { |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + TargetCard target = new TargetCardInExile(0, 1, StaticFilters.FILTER_CARD_CREATURE, null); |
| 190 | + if (!controller.chooseTarget(outcome, exiledCards, target, source, game)) { |
| 191 | + return false; |
| 192 | + } |
| 193 | + |
| 194 | + Card copyFromCard = game.getCard(target.getFirstTarget()); |
| 195 | + if (copyFromCard == null) { |
| 196 | + return false; |
| 197 | + } |
| 198 | + |
| 199 | + Permanent newBlueprint = new PermanentCard(copyFromCard, source.getControllerId(), game); |
| 200 | + newBlueprint.assignNewId(); |
| 201 | + CopyApplier applier = new KayaSpiritsJusticeCopyApplier(); |
| 202 | + applier.apply(game, newBlueprint, source, copyToPermanent.getId()); |
| 203 | + CopyEffect copyEffect = new CopyEffect(Duration.EndOfTurn, newBlueprint, copyToPermanent.getId()); |
| 204 | + copyEffect.newId(); |
| 205 | + copyEffect.setApplier(applier); |
| 206 | + Ability newAbility = source.copy(); |
| 207 | + copyEffect.init(newAbility, game); |
| 208 | + game.addEffect(copyEffect, source); |
| 209 | + |
| 210 | + return true; |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +class KayaSpiritsJusticeCopyApplier extends CopyApplier { |
| 215 | + |
| 216 | + @Override |
| 217 | + public boolean apply(Game game, MageObject blueprint, Ability source, UUID copyToObjectId) { |
| 218 | + blueprint.getAbilities().add(FlyingAbility.getInstance()); |
| 219 | + return true; |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +class KayaSpiritsJusticeExileEffect extends OneShotEffect { |
| 224 | + |
| 225 | + KayaSpiritsJusticeExileEffect() { |
| 226 | + super(Outcome.Exile); |
| 227 | + staticText = "exile a card from a graveyard"; |
| 228 | + } |
| 229 | + |
| 230 | + private KayaSpiritsJusticeExileEffect(final KayaSpiritsJusticeExileEffect effect) { |
| 231 | + super(effect); |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public KayaSpiritsJusticeExileEffect copy() { |
| 236 | + return new KayaSpiritsJusticeExileEffect(this); |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public boolean apply(Game game, Ability source) { |
| 241 | + Player controller = game.getPlayer(source.getControllerId()); |
| 242 | + if (controller != null) { |
| 243 | + TargetCardInGraveyard target = new TargetCardInGraveyard(); |
| 244 | + target.withNotTarget(true); |
| 245 | + controller.choose(outcome, target, source, game); |
| 246 | + Card card = game.getCard(target.getFirstTarget()); |
| 247 | + if (card != null) { |
| 248 | + UUID exileId = CardUtil.getExileZoneId(game, source.getSourceId(), source.getSourceObjectZoneChangeCounter()); |
| 249 | + MageObject sourceObject = source.getSourceObject(game); |
| 250 | + String exileName = sourceObject == null ? null : sourceObject.getIdName(); |
| 251 | + return controller.moveCardsToExile(card, source, game, true, exileId, exileName); |
| 252 | + } |
| 253 | + } |
| 254 | + return false; |
| 255 | + } |
| 256 | +} |
| 257 | + |
| 258 | +enum KayaSpiritsJusticeAdjuster implements TargetAdjuster { |
| 259 | + instance; |
| 260 | + |
| 261 | + @Override |
| 262 | + public void adjustTargets(Ability ability, Game game) { |
| 263 | + ability.getTargets().clear(); |
| 264 | + |
| 265 | + Player controller = game.getPlayer(ability.getControllerId()); |
| 266 | + if (controller == null) { |
| 267 | + return; |
| 268 | + } |
| 269 | + ability.addTarget(new TargetPermanent(StaticFilters.FILTER_CONTROLLED_CREATURE)); |
| 270 | + |
| 271 | + for (UUID playerId : game.getState().getPlayersInRange(ability.getControllerId(), game)) { |
| 272 | + Player player = game.getPlayer(playerId); |
| 273 | + if (player == null || player == controller) { |
| 274 | + continue; |
| 275 | + } |
| 276 | + FilterPermanent filter = new FilterCreaturePermanent("creature that player controls"); |
| 277 | + filter.add(new ControllerIdPredicate(playerId)); |
| 278 | + ability.addTarget(new TargetPermanent(0, 1, filter) |
| 279 | + .withChooseHint("from " + player.getLogName())); |
| 280 | + } |
| 281 | + } |
| 282 | +} |
0 commit comments