use of mage.target.TargetPermanent in project mage by magefree.
the class ArmedAndArmoredEquipEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
return false;
}
FilterControlledPermanent dwarfFilter = new FilterControlledPermanent(SubType.DWARF);
List<Permanent> dwarves = game.getBattlefield().getAllActivePermanents(dwarfFilter, controller.getId(), game);
FilterControlledPermanent equipmentFilter = new FilterControlledPermanent(SubType.EQUIPMENT);
List<Permanent> equipment = game.getBattlefield().getAllActivePermanents(equipmentFilter, controller.getId(), game);
if (!dwarves.isEmpty() && !equipment.isEmpty()) {
TargetPermanent target = new TargetPermanent(0, 1, dwarfFilter, true);
target.withChooseHint("dwarf to be equipped");
controller.choose(outcome, target, source.getId(), game);
Permanent dwarf = game.getPermanent(target.getFirstTarget());
if (dwarf != null) {
target = new TargetPermanent(0, Integer.MAX_VALUE, equipmentFilter, true);
target.withChooseHint("equip to " + dwarf.getLogName());
controller.choose(outcome, target, source.getId(), game);
for (UUID targetId : target.getTargets()) {
dwarf.addAttachment(targetId, source, game);
game.informPlayers(game.getPermanent(targetId).getLogName() + " was attached to " + dwarf.getLogName());
}
}
}
return true;
}
use of mage.target.TargetPermanent in project mage by magefree.
the class ArmoredSkyhunterEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
if (player == null) {
return false;
}
Cards cards = new CardsImpl(player.getLibrary().getTopCards(game, 6));
TargetCardInLibrary targetCard = new TargetCardInLibrary(0, 1, filter);
player.choose(outcome, cards, targetCard, game);
Card card = game.getCard(targetCard.getFirstTarget());
if (card == null) {
return player.putCardsOnBottomOfLibrary(cards, game, source, false);
}
player.moveCards(card, Zone.BATTLEFIELD, source, game);
cards.removeIf(uuid -> game.getState().getZone(uuid) != Zone.LIBRARY);
Permanent equipment = game.getPermanent(card.getId());
if (equipment == null || !equipment.hasSubtype(SubType.EQUIPMENT, game)) {
return player.putCardsOnBottomOfLibrary(cards, game, source, false);
}
TargetPermanent targetPermanent = new TargetControlledCreaturePermanent(0, 1);
targetCard.setNotTarget(true);
player.choose(outcome, targetPermanent, source.getSourceId(), game);
Permanent permanent = game.getPermanent(targetPermanent.getFirstTarget());
if (permanent != null) {
permanent.addAttachment(equipment.getId(), source, game);
}
return player.putCardsOnBottomOfLibrary(cards, game, source, false);
}
use of mage.target.TargetPermanent in project mage by magefree.
the class ConfiscationCoupEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
new GetEnergyCountersControllerEffect(4).apply(game, source);
Permanent targetPermanent = game.getPermanent(getTargetPointer().getFirst(game, source));
if (targetPermanent != null) {
Cost cost = new PayEnergyCost(targetPermanent.getManaCost().manaValue());
if (cost.canPay(source, source, source.getControllerId(), game)) {
int manaValue = targetPermanent.getManaCost().manaValue();
StringBuilder energy = new StringBuilder(manaValue);
for (int i = 0; i < manaValue; i++) {
energy.append("{E}");
}
if (controller.chooseUse(outcome, "Pay " + energy + " to get control of " + targetPermanent.getLogName() + '?', source, game)) {
if (cost.pay(source, game, source, source.getControllerId(), true)) {
ContinuousEffect controllEffect = new GainControlTargetEffect(Duration.Custom);
controllEffect.setTargetPointer(new FixedTarget(targetPermanent, game));
game.addEffect(controllEffect, source);
}
}
}
}
return true;
}
return false;
}
use of mage.target.TargetPermanent in project mage by magefree.
the class DeadlyBrewEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
return false;
}
List<Permanent> toSacrifice = new ArrayList<>();
Permanent yours = null;
for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
Player player = game.getPlayer(playerId);
if (player == null) {
continue;
}
TargetPermanent target = new TargetPermanent(filter);
target.setNotTarget(true);
if (!target.canChoose(source.getSourceId(), playerId, game)) {
continue;
}
player.choose(outcome, target, source.getSourceId(), game);
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent == null) {
continue;
}
if (permanent.isControlledBy(source.getControllerId())) {
yours = permanent;
} else {
toSacrifice.add(permanent);
}
}
for (Permanent permanent : toSacrifice) {
if (permanent == null) {
continue;
}
permanent.sacrifice(source, game);
}
Cards yourGrave = new CardsImpl(controller.getGraveyard());
yourGrave.removeIf(uuid -> !game.getCard(uuid).isPermanent(game));
if (yours == null || !yours.sacrifice(source, game)) {
return true;
}
TargetCardInYourGraveyard target = new TargetCardInYourGraveyard(0, 1, StaticFilters.FILTER_CARD_PERMANENT, true);
controller.choose(outcome, yourGrave, target, game);
Card card = controller.getGraveyard().get(target.getFirstTarget(), game);
if (card != null) {
controller.moveCards(card, Zone.HAND, source, game);
}
return true;
}
use of mage.target.TargetPermanent in project mage by magefree.
the class DropOfHoneyStateTriggeredAbility method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Permanent sourcePermanent = game.getPermanentOrLKIBattlefield(source.getSourceId());
if (controller != null && sourcePermanent != null) {
int leastPower = Integer.MAX_VALUE;
boolean multipleExist = false;
Permanent permanentToDestroy = null;
for (Permanent permanent : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURES, source.getControllerId(), game)) {
if (permanent.getPower().getValue() < leastPower) {
permanentToDestroy = permanent;
leastPower = permanent.getPower().getValue();
multipleExist = false;
} else {
if (permanent.getPower().getValue() == leastPower) {
multipleExist = true;
}
}
}
if (multipleExist) {
FilterCreaturePermanent filter = new FilterCreaturePermanent("one of the creatures with the least power");
filter.add(new PowerPredicate(ComparisonType.EQUAL_TO, leastPower));
Target target = new TargetPermanent(filter);
target.setNotTarget(true);
if (target.canChoose(source.getSourceId(), source.getControllerId(), game)) {
if (controller.choose(outcome, target, source.getSourceId(), game)) {
permanentToDestroy = game.getPermanent(target.getFirstTarget());
}
}
}
if (permanentToDestroy != null) {
game.informPlayers(sourcePermanent.getName() + " chosen creature: " + permanentToDestroy.getName());
return permanentToDestroy.destroy(source, game, true);
}
return true;
}
return false;
}
Aggregations