use of mage.game.stack.StackObject 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.game.stack.StackObject in project mage by magefree.
the class ComputerPlayer6 method resolve.
protected void resolve(SimulationNode2 node, int depth, Game game) {
StackObject stackObject = game.getStack().getFirst();
if (stackObject instanceof StackAbility) {
// AI hint for search effects (calc all possible cards for best score)
SearchEffect effect = getSearchEffect((StackAbility) stackObject);
if (effect != null && stackObject.getControllerId().equals(playerId)) {
Target target = effect.getTarget();
if (!target.doneChosing()) {
for (UUID targetId : target.possibleTargets(stackObject.getSourceId(), stackObject.getControllerId(), game)) {
Game sim = game.copy();
StackAbility newAbility = (StackAbility) stackObject.copy();
SearchEffect newEffect = getSearchEffect(newAbility);
newEffect.getTarget().addTarget(targetId, newAbility, sim);
sim.getStack().push(newAbility);
SimulationNode2 newNode = new SimulationNode2(node, sim, depth, stackObject.getControllerId());
node.children.add(newNode);
newNode.getTargets().add(targetId);
logger.trace("Sim search -- node#: " + SimulationNode2.getCount() + " for player: " + sim.getPlayer(stackObject.getControllerId()).getName());
}
return;
}
}
}
stackObject.resolve(game);
if (stackObject instanceof StackAbility) {
game.getStack().remove(stackObject, game);
}
game.applyEffects();
game.getPlayers().resetPassed();
game.getPlayerList().setCurrent(game.getActivePlayerId());
}
use of mage.game.stack.StackObject in project mage by magefree.
the class InterdictCantActivateEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget());
if (stackObject != null && game.getStack().counter(source.getFirstTarget(), source, game)) {
Permanent sourcePermanent = stackObject.getStackAbility().getSourcePermanentIfItStillExists(game);
if (sourcePermanent != null) {
InterdictCantActivateEffect effect = new InterdictCantActivateEffect();
effect.setTargetPointer(new FixedTarget(sourcePermanent, game));
game.getContinuousEffects().addEffect(effect, source);
}
return true;
}
return false;
}
use of mage.game.stack.StackObject in project mage by magefree.
the class CommanderStormEffect method checkTrigger.
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (!event.getSourceId().equals(getSourceId())) {
return false;
}
StackObject spell = game.getStack().getStackObject(getSourceId());
if (!(spell instanceof Spell)) {
return false;
}
for (Effect effect : this.getEffects()) {
effect.setValue("StormSpell", spell);
effect.setValue("StormSpellRef", new MageObjectReference(spell.getId(), game));
}
return true;
}
use of mage.game.stack.StackObject in project mage by magefree.
the class FoldIntoAetherEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
UUID targetId = getTargetPointer().getFirst(game, source);
StackObject stackObject = game.getStack().getStackObject(targetId);
Player spellController = null;
if (stackObject != null) {
spellController = game.getPlayer(stackObject.getControllerId());
}
if (game.getStack().counter(targetId, source, game)) {
TargetCardInHand target = new TargetCardInHand(StaticFilters.FILTER_CARD_CREATURE);
if (spellController != null && target.canChoose(source.getSourceId(), spellController.getId(), game) && spellController.chooseUse(Outcome.Neutral, "Put a creature card from your hand in play?", source, game) && spellController.choose(Outcome.PutCreatureInPlay, target, source.getSourceId(), game)) {
Card card = game.getCard(target.getFirstTarget());
if (card != null) {
spellController.moveCards(card, Zone.BATTLEFIELD, source, game);
}
}
return true;
}
return false;
}
Aggregations