use of mage.abilities.ActivatedAbility in project mage by magefree.
the class PatchworkCrawlerEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Permanent permanent = source.getSourcePermanentIfItStillExists(game);
ExileZone exileZone = game.getExile().getExileZone(CardUtil.getExileZoneId(game, source));
if (permanent == null || exileZone == null || exileZone.isEmpty()) {
return false;
}
for (Card card : exileZone.getCards(StaticFilters.FILTER_CARD_CREATURE, game)) {
for (Ability ability : card.getAbilities(game)) {
if (ability instanceof ActivatedAbility) {
permanent.addAbility(ability, source.getSourceId(), game);
}
}
}
return true;
}
use of mage.abilities.ActivatedAbility in project mage by magefree.
the class GainControlTargetEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
// controller no longer exists
discard();
return false;
}
boolean oneTargetStillExists = false;
for (UUID permanentId : getTargetPointer().getTargets(game, source)) {
Permanent permanent = game.getPermanent(permanentId);
if (permanent == null) {
continue;
}
oneTargetStillExists = true;
if (permanent.isControlledBy(controllingPlayerId)) {
continue;
}
boolean controlChanged = false;
if (controllingPlayerId != null) {
if (permanent.changeControllerId(controllingPlayerId, game, source)) {
controlChanged = true;
}
} else {
if (permanent.changeControllerId(source.getControllerId(), game, source)) {
controlChanged = true;
}
}
if (source instanceof ActivatedAbility && firstControlChange && !controlChanged) {
// If it was not possible to get control of target permanent by the activated ability the first time it took place
// the effect failed (e.g. because of Guardian Beast) and must be discarded
// This does not handle correctly multiple targets at once
discard();
}
}
// no valid target exists and the controller is no longer in the game, effect can be discarded
if (!oneTargetStillExists || !controller.isInGame()) {
discard();
}
firstControlChange = false;
return true;
}
use of mage.abilities.ActivatedAbility 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.abilities.ActivatedAbility 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.abilities.ActivatedAbility in project mage by magefree.
the class SimulatedPlayer2 method simulateOptions.
protected void simulateOptions(Game game) {
List<ActivatedAbility> playables = game.getPlayer(playerId).getPlayable(game, isSimulatedPlayer);
playables = filterAbilities(game, playables, suggested);
for (ActivatedAbility ability : playables) {
if (ability.getAbilityType() == AbilityType.MANA) {
continue;
}
List<Ability> options = game.getPlayer(playerId).getPlayableOptions(ability, game);
options = filterOptions(game, options, ability, suggested);
options = optimizeOptions(game, options, ability);
if (options.isEmpty()) {
allActions.add(ability);
} else {
for (Ability option : options) {
allActions.add(option);
}
}
}
}
Aggregations