use of mage.game.stack.StackAbility in project mage by magefree.
the class CantBeTargetedAttachedEffect method applies.
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
Permanent attachment = game.getPermanent(source.getSourceId());
if (attachment != null && event.getTargetId().equals(attachment.getAttachedTo())) {
if (targetController == TargetController.OPPONENT && !game.getOpponents(source.getControllerId()).contains(event.getPlayerId())) {
return false;
}
MageObject mageObject = game.getObject(event.getSourceId());
MageObject sourceObject;
if (mageObject instanceof StackAbility) {
sourceObject = ((StackAbility) mageObject).getSourceObject(game);
} else {
sourceObject = mageObject;
}
if (mageObject != null && filterSource.match(sourceObject, game)) {
return true;
}
}
return false;
}
use of mage.game.stack.StackAbility in project mage by magefree.
the class CantBeTargetedAllEffect method applies.
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
Permanent permanent = game.getPermanent(event.getTargetId());
if (permanent != null && filterTarget.match(permanent, source.getSourceId(), source.getControllerId(), game)) {
StackObject stackObject = game.getStack().getStackObject(event.getSourceId());
MageObject sourceObject;
if (stackObject instanceof StackAbility) {
if (filterSource instanceof FilterSpell) {
// only spells have to be selected
return false;
}
sourceObject = ((StackAbility) stackObject).getSourceObject(game);
} else {
sourceObject = stackObject;
}
if (filterSource.match(sourceObject, game)) {
return true;
}
}
return false;
}
use of mage.game.stack.StackAbility in project mage by magefree.
the class PlayerImpl method triggerAbility.
@Override
public boolean triggerAbility(TriggeredAbility triggeredAbility, Game game) {
if (triggeredAbility == null) {
logger.warn("Null source in triggerAbility method");
throw new IllegalArgumentException("source TriggeredAbility must not be null");
}
// 20091005 - 603.3c, 603.3d
int bookmark = game.bookmarkState();
TriggeredAbility ability = triggeredAbility.copy();
MageObject sourceObject = ability.getSourceObject(game);
if (sourceObject != null) {
sourceObject.adjustTargets(ability, game);
}
UUID triggerId = null;
if (ability.canChooseTarget(game, playerId)) {
if (ability.isUsesStack()) {
game.getStack().push(new StackAbility(ability, playerId));
}
if (ability.activate(game, false)) {
if ((ability.isUsesStack() || ability.getRuleVisible()) && !game.isSimulation()) {
game.informPlayers(getLogName() + " - " + ability.getGameLogMessage(game));
}
if (!ability.isUsesStack()) {
ability.resolve(game);
} else {
game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));
triggerId = ability.getId();
}
game.removeBookmark(bookmark);
return true;
}
}
// why restore is needed here? (to remove the triggered ability from the stack because of no possible targets)
restoreState(bookmark, triggeredAbility.getRule(), game);
GameEvent event = new GameEvent(GameEvent.EventType.ABILITY_TRIGGERED, triggerId, ability, ability.getControllerId());
game.getState().setValue(event.getId().toString(), ability.getTriggerEvent());
game.fireEvent(event);
return false;
}
use of mage.game.stack.StackAbility in project mage by magefree.
the class SimulatedPlayer2 method addAbilityNode.
protected void addAbilityNode(SimulationNode2 parent, Ability ability, int depth, Game game) {
Game sim = game.copy();
sim.getStack().push(new StackAbility(ability, playerId));
if (ability.activate(sim, false) && ability.isUsesStack()) {
game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));
}
sim.applyEffects();
SimulationNode2 newNode = new SimulationNode2(parent, sim, depth, playerId);
logger.debug("simulating -- node #:" + SimulationNode2.getCount() + " triggered ability option");
for (Target target : ability.getTargets()) {
for (UUID targetId : target.getTargets()) {
newNode.getTargets().add(targetId);
}
}
parent.children.add(newNode);
}
use of mage.game.stack.StackAbility in project mage by magefree.
the class ComputerPlayer2 method resolve.
protected void resolve(SimulationNode node, Game game) {
StackObject ability = game.getStack().pop();
if (ability instanceof StackAbility) {
SearchEffect effect = getSearchEffect((StackAbility) ability);
if (effect != null && ability.getControllerId().equals(playerId)) {
Target target = effect.getTarget();
if (!target.doneChosing()) {
for (UUID targetId : target.possibleTargets(ability.getSourceId(), ability.getControllerId(), game)) {
Game sim = game.copy();
StackAbility newAbility = (StackAbility) ability.copy();
SearchEffect newEffect = getSearchEffect((StackAbility) newAbility);
newEffect.getTarget().addTarget(targetId, newAbility, sim);
sim.getStack().push(newAbility);
SimulationNode newNode = new SimulationNode(node, sim, ability.getControllerId());
node.children.add(newNode);
newNode.getTargets().add(targetId);
logger.debug(indent(node.depth) + "simulating search -- node#: " + SimulationNode.getCount() + "for player: " + sim.getPlayer(ability.getControllerId()).getName());
}
return;
}
}
}
logger.debug(indent(node.depth) + "simulating resolve ");
ability.resolve(game);
game.applyEffects();
game.getPlayers().resetPassed();
game.getPlayerList().setCurrent(game.getActivePlayerId());
}
Aggregations