foul-magics/Mage.Sets/src/mage/cards/p/PsychicBattle.java
2019-01-16 13:48:43 +01:00

143 lines
5.3 KiB
Java

package mage.cards.p;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.effects.OneShotEffect;
import mage.cards.Card;
import mage.cards.CardImpl;
import mage.cards.CardSetInfo;
import mage.cards.CardsImpl;
import mage.constants.CardType;
import mage.constants.Outcome;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.stack.StackObject;
import mage.players.Player;
import mage.target.targetpointer.FixedTarget;
/**
*
* @author L_J
*/
public final class PsychicBattle extends CardImpl {
public PsychicBattle(UUID ownerId, CardSetInfo setInfo) {
super(ownerId, setInfo, new CardType[]{CardType.ENCHANTMENT}, "{3}{U}{U}");
// Whenever a player chooses one or more targets, each player reveals the top card of their library. The player who reveals the card with the highest converted mana cost may change the target or targets. If two or more cards are tied for highest cost, the target or targets remain unchanged. Changing targets this way doesn't trigger abilities of permanents named Psychic Battle.
this.addAbility(new PsychicBattleTriggeredAbility());
}
public PsychicBattle(final PsychicBattle card) {
super(card);
}
@Override
public PsychicBattle copy() {
return new PsychicBattle(this);
}
}
class PsychicBattleTriggeredAbility extends TriggeredAbilityImpl {
public PsychicBattleTriggeredAbility() {
super(Zone.BATTLEFIELD, new PsychicBattleEffect(), false);
}
public PsychicBattleTriggeredAbility(PsychicBattleTriggeredAbility ability) {
super(ability);
}
@Override
public PsychicBattleTriggeredAbility copy() {
return new PsychicBattleTriggeredAbility(this);
}
@Override
public boolean checkEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.TARGETED;
}
@Override
public boolean checkTrigger(GameEvent event, Game game) {
StackObject stackObject = game.getStack().getStackObject(event.getSourceId());
if (stackObject != null) {
if (!stackObject.isTargetChanged() && !stackObject.getName().equals("Psychic Battle")) {
this.getEffects().get(0).setTargetPointer(new FixedTarget(event.getSourceId()));
stackObject.setTargetChanged(false); // resets the targetChanged flag
return true;
}
}
return false;
}
@Override
public String getRule() {
return "Whenever a player chooses one or more targets, " + super.getRule() + " Changing targets this way doesn't trigger abilities of permanents named Psychic Battle.";
}
}
class PsychicBattleEffect extends OneShotEffect {
public PsychicBattleEffect() {
super(Outcome.Benefit);
this.staticText = "each player reveals the top card of their library. The player who reveals the card with the highest converted mana cost may change the target or targets. If two or more cards are tied for highest cost, the target or targets remain unchanged";
}
public PsychicBattleEffect(final PsychicBattleEffect effect) {
super(effect);
}
@Override
public PsychicBattleEffect copy() {
return new PsychicBattleEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
MageObject sourceObject = game.getObject(source.getSourceId());
if (sourceObject != null) {
Map<Player, Integer> manacostMap = new HashMap<>();
for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null && player.getLibrary().hasCards()) {
Card card = player.getLibrary().getFromTop(game);
player.revealCards(sourceObject.getIdName() + " (" + player.getName() + ')', new CardsImpl(card), game);
manacostMap.put(player, card.getConvertedManaCost());
}
}
Player highestCostPlayer = null;
int maxValue = Collections.max(manacostMap.values());
boolean tie = false;
for (Map.Entry<Player, Integer> entry : manacostMap.entrySet()) {
if (entry.getValue() == maxValue) {
if (highestCostPlayer != null) { // the result is tied, so nobody changes the targets
tie = true;
break;
} else {
highestCostPlayer = entry.getKey();
}
}
}
if (highestCostPlayer != null && !tie) {
StackObject stackObject = game.getStack().getStackObject(this.getTargetPointer().getFirst(game, source));
if (stackObject != null) {
stackObject.setTargetChanged(true); // this makes the new target "invisible" for the Psychic Battle ability
stackObject.chooseNewTargets(game, highestCostPlayer.getId(), false, false, null);
}
return true;
}
return tie;
}
return false;
}
}