use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class NomadMythmakerEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Card aura = game.getCard(source.getFirstTarget());
if (controller == null || aura == null) {
return false;
}
FilterControlledCreaturePermanent FILTER = new FilterControlledCreaturePermanent("Choose a creature you control");
TargetControlledPermanent target = new TargetControlledPermanent(FILTER);
target.setNotTarget(true);
if (controller.choose(Outcome.PutCardInPlay, target, source.getSourceId(), game)) {
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent != null && !permanent.cantBeAttachedBy(aura, source, game, false)) {
game.getState().setValue("attachTo:" + aura.getId(), permanent);
controller.moveCards(aura, Zone.BATTLEFIELD, source, game);
return permanent.addAttachment(aura.getId(), source, game);
}
}
return false;
}
use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class NobleStandAbility method checkTrigger.
@Override
public boolean checkTrigger(GameEvent event, Game game) {
FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent();
filter.add(TokenPredicate.FALSE);
Permanent permanent = game.getPermanent(event.getSourceId());
return permanent != null && filter.match(permanent, sourceId, controllerId, game);
}
use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class SlaughterTheStrongEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null) {
boolean selectionDone = false;
Set<UUID> selectedCreatures = new HashSet<>();
while (player.canRespond() && selectionDone == false) {
int powerSum = 0;
for (UUID creatureId : selectedCreatures) {
Permanent creature = game.getPermanent(creatureId);
if (creature != null) {
powerSum += creature.getPower().getValue();
}
}
FilterControlledCreaturePermanent currentFilter = new FilterControlledCreaturePermanent("any number of creatures you control with total power 4 or less (already selected total power " + powerSum + ")");
Set<PermanentIdPredicate> alreadySelectedCreatures = new HashSet<>();
if (!selectedCreatures.isEmpty()) {
for (UUID creatureId : selectedCreatures) {
alreadySelectedCreatures.add(new PermanentIdPredicate(creatureId));
}
currentFilter.add(Predicates.or(new PowerPredicate(ComparisonType.FEWER_THAN, 5 - powerSum), Predicates.or(alreadySelectedCreatures)));
} else {
currentFilter.add(new PowerPredicate(ComparisonType.FEWER_THAN, 5 - powerSum));
}
// human can de-select targets, but AI must choose only one time
Target target;
if (player.isComputer()) {
// AI settings
FilterControlledCreaturePermanent strictFilter = currentFilter.copy();
selectedCreatures.stream().forEach(id -> {
strictFilter.add(Predicates.not(new PermanentIdPredicate(id)));
});
target = new TargetPermanent(0, 1, strictFilter, true);
} else {
// Human settings
target = new TargetPermanent(0, 1, currentFilter, true);
}
player.chooseTarget(Outcome.BoostCreature, target, source, game);
if (target.getFirstTarget() != null) {
if (selectedCreatures.contains(target.getFirstTarget())) {
selectedCreatures.remove(target.getFirstTarget());
} else {
selectedCreatures.add(target.getFirstTarget());
}
} else {
if (player.isComputer()) {
// AI stops
selectionDone = true;
} else {
// Human can continue
String selected = "Selected: ";
for (UUID creatureId : selectedCreatures) {
Permanent creature = game.getPermanent(creatureId);
if (creature != null) {
selected += creature.getLogName() + " ";
}
}
selectionDone = player.chooseUse(Outcome.Detriment, "Creature selection", selected, "End the selection", "Continue the selection", source, game);
}
}
}
for (Permanent creature : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, playerId, game)) {
if (!selectedCreatures.contains(creature.getId())) {
creature.sacrifice(source, game);
}
}
}
}
return true;
}
return false;
}
use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class MeldCondition method apply.
@Override
public boolean apply(Game game, Ability source) {
MageObject sourceMageObject = source.getSourceObjectIfItStillExists(game);
if (sourceMageObject instanceof Permanent) {
Permanent sourcePermanent = (Permanent) sourceMageObject;
if (sourcePermanent.isControlledBy(source.getControllerId()) && sourcePermanent.isOwnedBy(source.getControllerId())) {
FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent();
filter.add(new NamePredicate(this.meldWithName));
filter.add(new OwnerIdPredicate(source.getControllerId()));
return game.getBattlefield().count(filter, source.getSourceId(), source.getControllerId(), game) > 0;
}
}
return false;
}
use of mage.filter.common.FilterControlledCreaturePermanent in project mage by magefree.
the class MeldEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
// Find the two permanents to meld.
UUID sourceId = source.getSourceId();
FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("creature named " + meldWithName);
filter.add(new NamePredicate(meldWithName));
TargetPermanent target = new TargetControlledCreaturePermanent(filter);
Set<UUID> meldWithList = target.possibleTargets(sourceId, source.getControllerId(), game);
if (meldWithList.isEmpty()) {
// possible permanent has left the battlefield meanwhile
return false;
}
UUID meldWithId = null;
if (meldWithList.size() == 1) {
meldWithId = meldWithList.iterator().next();
} else {
if (controller.choose(Outcome.BoostCreature, target, sourceId, game)) {
meldWithId = target.getFirstTarget();
}
}
// Exile the two permanents to meld.
Permanent sourcePermanent = game.getPermanent(sourceId);
Permanent meldWithPermanent = game.getPermanent(meldWithId);
if (sourcePermanent != null && meldWithPermanent != null) {
Set<Card> toExile = new HashSet<>();
toExile.add(sourcePermanent);
toExile.add(meldWithPermanent);
controller.moveCards(toExile, Zone.EXILED, source, game);
// Create the meld card and move it to the battlefield.
Card sourceCard = game.getExile().getCard(sourceId, game);
Card meldWithCard = game.getExile().getCard(meldWithId, game);
if (sourceCard != null && !sourceCard.isCopy() && meldWithCard != null && !meldWithCard.isCopy()) {
meldCard.setOwnerId(controller.getId());
meldCard.setTopHalfCard(meldWithCard, game);
meldCard.setBottomHalfCard(sourceCard, game);
meldCard.setMelded(true, game);
game.addMeldCard(meldCard.getId(), meldCard);
game.getState().addCard(meldCard);
meldCard.setZone(Zone.EXILED, game);
controller.moveCards(meldCard, Zone.BATTLEFIELD, source, game);
}
return true;
}
}
return false;
}
Aggregations