use of mage.abilities.Ability 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.Ability in project mage by magefree.
the class SimulatedPlayer2 method triggerAbility.
@Override
public boolean triggerAbility(TriggeredAbility source, Game game) {
Ability ability = source.copy();
List<Ability> options = getPlayableOptions(ability, game);
if (options.isEmpty()) {
logger.debug("simulating -- triggered ability:" + ability);
game.getStack().push(new StackAbility(ability, playerId));
if (ability.activate(game, false) && ability.isUsesStack()) {
game.fireEvent(new GameEvent(GameEvent.EventType.TRIGGERED_ABILITY, ability.getId(), ability, ability.getControllerId()));
}
game.applyEffects();
game.getPlayers().resetPassed();
} else {
SimulationNode2 parent = (SimulationNode2) game.getCustomData();
int depth = parent.getDepth() - 1;
if (depth == 0) {
return true;
}
logger.debug("simulating -- triggered ability - adding children:" + options.size());
for (Ability option : options) {
addAbilityNode(parent, option, depth, game);
}
}
return true;
}
use of mage.abilities.Ability in project mage by magefree.
the class SimulatedPlayer2 method optimizeOptions.
protected List<Ability> optimizeOptions(Game game, List<Ability> options, Ability ability) {
if (options.isEmpty()) {
return options;
}
// determine if all effects are bad or good
Iterator<Ability> iterator = options.iterator();
boolean bad = true;
boolean good = true;
// TODO: add custom outcome from ability?
for (Effect effect : ability.getEffects()) {
if (effect.getOutcome().isGood()) {
bad = false;
} else {
good = false;
}
}
if (bad) {
// remove its own creatures, player itself for bad effects with one target
while (iterator.hasNext()) {
Ability ability1 = iterator.next();
if (ability1.getTargets().size() == 1 && ability1.getTargets().get(0).getTargets().size() == 1) {
Permanent permanent = game.getPermanent(ability1.getFirstTarget());
if (permanent != null && !game.getOpponents(playerId).contains(permanent.getControllerId())) {
iterator.remove();
continue;
}
if (ability1.getFirstTarget().equals(playerId)) {
iterator.remove();
}
}
}
}
if (good) {
// remove opponent creatures and opponent for only good effects with one target
while (iterator.hasNext()) {
Ability ability1 = iterator.next();
if (ability1.getTargets().size() == 1 && ability1.getTargets().get(0).getTargets().size() == 1) {
Permanent permanent = game.getPermanent(ability1.getFirstTarget());
if (permanent != null && game.getOpponents(playerId).contains(permanent.getControllerId())) {
iterator.remove();
continue;
}
if (game.getOpponents(playerId).contains(ability1.getFirstTarget())) {
iterator.remove();
}
}
}
}
return options;
}
use of mage.abilities.Ability 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);
}
}
}
}
use of mage.abilities.Ability in project mage by magefree.
the class SimulatedPlayer2 method simulatePriority.
public List<Ability> simulatePriority(Game game) {
allActions = new ConcurrentLinkedQueue<>();
Game sim = game.copy();
sim.setSimulation(true);
forced = false;
simulateOptions(sim);
List<Ability> list = new ArrayList<>(allActions);
Collections.reverse(list);
if (!forced) {
list.add(pass);
}
if (logger.isTraceEnabled()) {
for (Ability a : allActions) {
logger.info("ability==" + a);
if (!a.getTargets().isEmpty()) {
MageObject mageObject = game.getObject(a.getFirstTarget());
if (mageObject != null) {
logger.info(" target=" + mageObject.getName());
} else {
Player player = game.getPlayer(a.getFirstTarget());
if (player != null) {
logger.info(" target=" + player.getName());
}
}
}
}
}
return list;
}
Aggregations