use of mage.target.Target in project mage by magefree.
the class RaidingPartyEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Permanent sourcePermanent = game.getPermanentOrLKIBattlefield(source.getSourceId());
if (sourcePermanent != null) {
Set<UUID> plainsToSave = new HashSet<>();
for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null) {
int countBattlefield = game.getBattlefield().getAllActivePermanents(filter, game.getActivePlayerId(), game).size();
int tappedCount = 0;
Target untappedCreatureTarget = new TargetControlledCreaturePermanent(0, Integer.MAX_VALUE, filter, true);
if (player.choose(Outcome.Benefit, untappedCreatureTarget, source.getSourceId(), game)) {
tappedCount = untappedCreatureTarget.getTargets().size();
for (UUID creatureId : untappedCreatureTarget.getTargets()) {
Permanent creature = game.getPermanentOrLKIBattlefield(creatureId);
if (creature != null) {
creature.tap(source, game);
}
}
}
if (tappedCount > 0) {
Target plainsToSaveTarget = new TargetPermanent(0, tappedCount * 2, filter2, true);
if (player.choose(Outcome.Benefit, plainsToSaveTarget, source.getSourceId(), game)) {
for (UUID plainsId : plainsToSaveTarget.getTargets()) {
plainsToSave.add(plainsId);
Permanent plains = game.getPermanent(plainsId);
if (plains != null) {
game.informPlayers(player.getLogName() + " chose " + plains.getLogName() + " to not be destroyed by " + sourcePermanent.getLogName());
}
}
}
}
}
}
for (Permanent plains : game.getBattlefield().getActivePermanents(filter2, source.getControllerId(), source.getSourceId(), game)) {
if (!plainsToSave.contains(plains.getId())) {
plains.destroy(source, game, false);
}
}
return true;
}
return false;
}
use of mage.target.Target in project mage by magefree.
the class RicochetEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Spell spell = game.getStack().getSpell(this.getTargetPointer().getFirst(game, source));
if (spell != null) {
Targets targets = new Targets();
Ability sourceAbility = spell.getSpellAbility();
for (UUID modeId : sourceAbility.getModes().getSelectedModes()) {
Mode mode = sourceAbility.getModes().get(modeId);
targets.addAll(mode.getTargets());
}
if (targets.size() != 1 || targets.get(0).getTargets().size() != 1) {
return false;
}
Map<Player, Integer> playerRolls = new HashMap<>();
for (UUID playerId : game.getPlayerList().copy()) {
Player player = game.getPlayer(playerId);
if (player != null) {
playerRolls.put(player, 7);
}
}
do {
for (Player player : playerRolls.keySet()) {
// bad outcome - ai must choose lowest value
playerRolls.put(player, player.rollDice(Outcome.Detriment, source, game, 6));
}
int minValueInMap = Collections.min(playerRolls.values());
for (Map.Entry<Player, Integer> mapEntry : new HashSet<>(playerRolls.entrySet())) {
if (mapEntry.getValue() > minValueInMap) {
playerRolls.remove(mapEntry.getKey());
}
}
} while (playerRolls.size() > 1);
if (playerRolls.size() == 1) {
Player loser = (Player) playerRolls.keySet().toArray()[0];
UUID loserId = loser.getId();
Target target = targets.get(0);
if (target.getFirstTarget().equals(loserId)) {
return true;
}
String oldTargetName = null;
if (target.canTarget(spell.getControllerId(), loserId, sourceAbility, game)) {
Player oldPlayer = game.getPlayer(targets.getFirstTarget());
if (oldPlayer != null) {
oldTargetName = oldPlayer.getLogName();
}
target.clearChosen();
target.addTarget(loserId, sourceAbility, game);
}
MageObject sourceObject = game.getObject(source.getSourceId());
if (oldTargetName != null && sourceObject != null) {
game.informPlayers(sourceObject.getLogName() + ": Changed target of " + spell.getLogName() + " from " + oldTargetName + " to " + loser.getLogName());
}
}
return true;
}
return false;
}
use of mage.target.Target in project mage by magefree.
the class ShowOfDominanceEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
int highestPower = Integer.MIN_VALUE;
Permanent selectedCreature = null;
for (Permanent permanent : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, controller.getId(), game)) {
if (highestPower < permanent.getPower().getValue()) {
highestPower = permanent.getPower().getValue();
selectedCreature = permanent;
} else if (highestPower == permanent.getPower().getValue()) {
highestPower = permanent.getPower().getValue();
selectedCreature = null;
}
}
if (highestPower != Integer.MIN_VALUE) {
if (selectedCreature == null) {
FilterPermanent filter = new FilterCreaturePermanent("creature with power " + highestPower);
filter.add(new PowerPredicate(ComparisonType.EQUAL_TO, highestPower));
Target target = new TargetPermanent(1, 1, filter, true);
if (controller.chooseTarget(outcome, target, source, game)) {
selectedCreature = game.getPermanent(target.getFirstTarget());
}
}
if (selectedCreature != null) {
FixedTarget target = new FixedTarget(selectedCreature.getId(), game);
Effect effect = new AddCountersTargetEffect(CounterType.P1P1.createInstance(4));
effect.setTargetPointer(target);
effect.apply(game, source);
ContinuousEffect continuousEffect = new GainAbilityTargetEffect(TrampleAbility.getInstance(), Duration.EndOfTurn);
continuousEffect.setTargetPointer(target);
game.addEffect(continuousEffect, source);
return true;
}
}
return true;
}
return false;
}
use of mage.target.Target in project mage by magefree.
the class SoulfireEruptionCastEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
return false;
}
for (UUID targetId : source.getTargets().stream().map(Target::getTargets).flatMap(Collection::stream).collect(Collectors.toSet())) {
Permanent permanent = game.getPermanent(targetId);
Player player = game.getPlayer(targetId);
if (permanent == null && player == null) {
continue;
}
Card card = controller.getLibrary().getFromTop(game);
if (card == null) {
continue;
}
controller.moveCards(card, Zone.EXILED, source, game);
game.addEffect(new SoulfireEruptionCastEffect().setTargetPointer(new FixedTarget(card, game)), source);
if (card.getManaValue() < 1) {
continue;
}
if (permanent != null) {
permanent.damage(card.getManaValue(), source.getSourceId(), source, game);
}
if (player != null) {
player.damage(card.getManaValue(), source.getSourceId(), source, game);
}
}
return true;
}
use of mage.target.Target in project mage by magefree.
the class VesuvanDoppelgangerCopyEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Permanent permanent = game.getPermanent(source.getSourceId());
if (permanent == null) {
permanent = game.getPermanentEntering(source.getSourceId());
}
final Permanent sourcePermanent = permanent;
if (controller != null && sourcePermanent != null) {
Target target = new TargetPermanent(new FilterCreaturePermanent("target creature (you copy from)"));
target.setRequired(true);
if (source instanceof SimpleStaticAbility) {
target = new TargetPermanent(new FilterCreaturePermanent("creature (you copy from)"));
target.setRequired(false);
target.setNotTarget(true);
}
if (target.canChoose(source.getSourceId(), source.getControllerId(), game)) {
controller.choose(Outcome.Copy, target, source.getSourceId(), game);
Permanent copyFromPermanent = game.getPermanent(target.getFirstTarget());
if (copyFromPermanent != null) {
game.copyPermanent(copyFromPermanent, sourcePermanent.getId(), source, new CopyApplier() {
@Override
public boolean apply(Game game, MageObject blueprint, Ability source, UUID copyToObjectId) {
blueprint.getColor().setColor(sourcePermanent.getColor(game));
blueprint.getAbilities().add(new BeginningOfUpkeepTriggeredAbility(Zone.BATTLEFIELD, new VesuvanDoppelgangerCopyEffect(), TargetController.YOU, true, false, rule2));
return true;
}
});
return true;
}
}
}
return false;
}
Aggregations