use of mage.target.Target in project mage by magefree.
the class AmassEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
int xValue = amassNumber.calculate(game, source, this);
Player player = game.getPlayer(source.getControllerId());
if (player == null) {
return false;
}
if (!game.getBattlefield().containsControlled(filter, source, game, 1)) {
new CreateTokenEffect(new ZombieArmyToken()).apply(game, source);
}
Target target = new TargetPermanent(filter);
target.setNotTarget(true);
if (!player.choose(outcome, target, source.getSourceId(), game)) {
return false;
}
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent == null) {
return false;
}
permanent.addCounters(CounterType.P1P1.createInstance(xValue), source.getControllerId(), source, game);
this.amassedCreatureId = permanent.getId();
return true;
}
use of mage.target.Target in project mage by magefree.
the class TargetsHaveToTargetPermanentIfAbleEffect method applies.
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
Player controller = game.getPlayer(source.getControllerId());
Player targetingPlayer = game.getPlayer(event.getPlayerId());
if (controller != null && // TODO: This target handling does only work for non AI players so AI logic
targetingPlayer.isHuman() && controller.hasOpponent(event.getPlayerId(), game)) {
StackObject stackObject = game.getStack().getStackObject(event.getSourceId());
if (stackObject.isCopy()) {
return false;
}
Ability stackAbility = stackObject.getStackAbility();
// Ensure that this ability is activated or a cast spell, because Flag Bearer effects don't require triggered abilities to choose a Standard Bearer
if (!(stackAbility instanceof ActivatedAbility) && !(stackAbility instanceof SpellAbility)) {
return false;
}
// Also check that targeting player controls the ability
if (!stackAbility.isControlledBy(targetingPlayer.getId())) {
return false;
}
Ability ability = (Ability) getValue("targetAbility");
if (ability != null) {
// Get all the allowed permanents on the battlefield in range of the abilities controller
List<Permanent> allowedPermanents = game.getBattlefield().getActivePermanents(filter, event.getPlayerId(), event.getSourceId(), game);
if (!allowedPermanents.isEmpty()) {
boolean canTargetAllowedPermanent = false;
for (UUID modeId : ability.getModes().getSelectedModes()) {
ability.getModes().setActiveMode(modeId);
for (Target target : ability.getTargets()) {
// Check if already targeted
for (Permanent allowedPermanent : allowedPermanents) {
if (target.getTargets().contains(allowedPermanent.getId())) {
return false;
}
if (target.canTarget(stackObject.getControllerId(), allowedPermanent.getId(), source, game)) {
canTargetAllowedPermanent = true;
}
}
}
}
return canTargetAllowedPermanent;
}
}
}
return false;
}
use of mage.target.Target in project mage by magefree.
the class DiscardEachPlayerEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
// Store for each player the cards to discard, that's important because all discard shall happen at the same time
Map<UUID, Cards> cardsToDiscard = new HashMap<>();
if (controller == null) {
return true;
}
int toDiscard = amount.calculate(game, source, this);
// choose cards to discard
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player == null) {
continue;
}
switch(targetController) {
case NOT_YOU:
if (playerId.equals(source.getControllerId())) {
continue;
}
break;
case OPPONENT:
if (!game.getOpponents(source.getControllerId()).contains(playerId)) {
continue;
}
break;
}
if (randomDiscard) {
player.discard(toDiscard, true, false, source, game);
continue;
}
int numberOfCardsToDiscard = Math.min(toDiscard, player.getHand().size());
Cards cards = new CardsImpl();
if (numberOfCardsToDiscard > 0) {
Target target = new TargetDiscard(numberOfCardsToDiscard, numberOfCardsToDiscard, StaticFilters.FILTER_CARD, playerId);
player.chooseTarget(outcome, target, source, game);
cards.addAll(target.getTargets());
}
cardsToDiscard.put(playerId, cards);
}
if (randomDiscard) {
return true;
}
// discard all choosen cards
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player == null) {
continue;
}
player.discard(cardsToDiscard.get(playerId), false, source, game);
}
return true;
}
use of mage.target.Target in project mage by magefree.
the class ComputerPlayer6 method act.
protected void act(Game game) {
if (actions == null || actions.isEmpty()) {
pass(game);
} else {
boolean usedStack = false;
while (actions.peek() != null) {
Ability ability = actions.poll();
// example: ===> SELECTED ACTION for PlayerA: Play Swamp
logger.info(String.format("===> SELECTED ACTION for %s: %s", getName(), ability.toString() + listTargets(game, ability.getTargets(), " (targeting %s)", "")));
if (!ability.getTargets().isEmpty()) {
for (Target target : ability.getTargets()) {
for (UUID id : target.getTargets()) {
target.updateTarget(id, game);
if (!target.isNotTarget()) {
game.addSimultaneousEvent(GameEvent.getEvent(GameEvent.EventType.TARGETED, id, ability, ability.getControllerId()));
}
}
}
Player player = game.getPlayer(ability.getFirstTarget());
if (player != null) {
logger.info("targets = " + player.getName());
}
}
this.activateAbility((ActivatedAbility) ability, game);
if (ability.isUsesStack()) {
usedStack = true;
}
if (!suggestedActions.isEmpty() && !(ability instanceof PassAbility)) {
Iterator<String> it = suggestedActions.iterator();
while (it.hasNext()) {
String action = it.next();
Card card = game.getCard(ability.getSourceId());
if (card != null && action.equals(card.getName())) {
logger.info("-> removed from suggested=" + action);
it.remove();
}
}
}
}
if (usedStack) {
pass(game);
}
}
}
use of mage.target.Target in project mage by magefree.
the class ComputerPlayer6 method listTargets.
/**
* Return info about targets list (targeting objects)
*
* @param game
* @param targets
* @param format example: my %s in data
* @param emptyText default text for empty targets list
* @return
*/
protected String listTargets(Game game, Targets targets, String format, String emptyText) {
List<String> res = new ArrayList<>();
for (Target target : targets) {
for (UUID id : target.getTargets()) {
MageObject object = game.getObject(id);
if (object != null) {
String prefix = "";
if (target instanceof TargetAmount) {
prefix = " " + target.getTargetAmount(id) + "x ";
}
res.add(prefix + object.getIdName());
}
}
}
String info = String.join("; ", res);
if (info.isEmpty()) {
return emptyText;
} else {
return String.format(format, info);
}
}
Aggregations