use of mage.game.events.GameEvent in project mage by magefree.
the class ComputerPlayer method findPlayables.
protected void findPlayables(Game game) {
playableInstant.clear();
playableNonInstant.clear();
unplayable.clear();
playableAbilities.clear();
Set<Card> nonLands = hand.getCards(new FilterNonlandCard(), game);
ManaOptions available = getManaAvailable(game);
for (Card card : nonLands) {
ManaOptions options = card.getManaCost().getOptions();
if (!card.getManaCost().getVariableCosts().isEmpty()) {
// don't use variable mana costs unless there is at least 3 extra mana for X
for (Mana option : options) {
option.add(Mana.GenericMana(3));
}
}
for (Mana mana : options) {
for (Mana avail : available) {
if (mana.enough(avail)) {
SpellAbility ability = card.getSpellAbility();
GameEvent castEvent = GameEvent.getEvent(GameEvent.EventType.CAST_SPELL, ability.getId(), ability, playerId);
castEvent.setZone(game.getState().getZone(card.getMainCard().getId()));
if (ability != null && ability.canActivate(playerId, game).canActivate() && !game.getContinuousEffects().preventedByRuleModification(castEvent, ability, game, true)) {
if (card.isInstant(game) || card.hasAbility(FlashAbility.getInstance(), game)) {
playableInstant.add(card);
} else {
playableNonInstant.add(card);
}
}
} else if (!playableInstant.contains(card) && !playableNonInstant.contains(card)) {
unplayable.put(mana.needed(avail), card);
}
}
}
}
// TODO: wtf?! change to player.getPlayable
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(playerId)) {
for (ActivatedAbility ability : permanent.getAbilities().getActivatedAbilities(Zone.BATTLEFIELD)) {
if (!(ability instanceof ActivatedManaAbilityImpl) && ability.canActivate(playerId, game).canActivate()) {
if (ability instanceof EquipAbility && permanent.getAttachedTo() != null) {
continue;
}
ManaOptions abilityOptions = ability.getManaCosts().getOptions();
if (!ability.getManaCosts().getVariableCosts().isEmpty()) {
// don't use variable mana costs unless there is at least 3 extra mana for X
for (Mana option : abilityOptions) {
option.add(Mana.GenericMana(3));
}
}
if (abilityOptions.isEmpty()) {
playableAbilities.add(ability);
} else {
for (Mana mana : abilityOptions) {
for (Mana avail : available) {
if (mana.enough(avail)) {
playableAbilities.add(ability);
}
}
}
}
}
}
}
for (Card card : graveyard.getCards(game)) {
for (ActivatedAbility ability : card.getAbilities(game).getActivatedAbilities(Zone.GRAVEYARD)) {
if (ability.canActivate(playerId, game).canActivate()) {
ManaOptions abilityOptions = ability.getManaCosts().getOptions();
if (abilityOptions.isEmpty()) {
playableAbilities.add(ability);
} else {
for (Mana mana : abilityOptions) {
for (Mana avail : available) {
if (mana.enough(avail)) {
playableAbilities.add(ability);
}
}
}
}
}
}
}
if (log.isDebugEnabled()) {
log.debug("findPlayables: " + playableInstant.toString() + "---" + playableNonInstant.toString() + "---" + playableAbilities.toString());
}
}
use of mage.game.events.GameEvent 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.events.GameEvent in project mage by magefree.
the class ComputerPlayer2 method playNext.
/*@Override
public boolean playXMana(VariableManaCost cost, ManaCosts<ManaCost> costs, Game game) {
//SimulatedPlayer.simulateVariableCosts method adds a generic mana cost for each option
for (ManaCost manaCost: costs) {
if (manaCost instanceof GenericManaCost) {
cost.setPayment(manaCost.getPayment());
logger.debug("using X = " + cost.getPayment().count());
break;
}
}
game.informPlayers(getName() + " payed " + cost.getPayment().count() + " for " + cost.getText());
cost.setPaid();
return true;
}*/
public void playNext(Game game, UUID activePlayerId, SimulationNode node) {
boolean skip = false;
while (true) {
Phase currentPhase = game.getPhase();
if (!skip) {
currentPhase.getStep().endStep(game, activePlayerId);
}
game.applyEffects();
switch(currentPhase.getStep().getType()) {
case UNTAP:
game.getPhase().setStep(new UpkeepStep());
break;
case UPKEEP:
game.getPhase().setStep(new DrawStep());
break;
case DRAW:
game.getTurn().setPhase(new PreCombatMainPhase());
game.getPhase().setStep(new PreCombatMainStep());
break;
case PRECOMBAT_MAIN:
game.getTurn().setPhase(new CombatPhase());
game.getPhase().setStep(new BeginCombatStep());
break;
case BEGIN_COMBAT:
game.getPhase().setStep(new DeclareAttackersStep());
break;
case DECLARE_ATTACKERS:
game.getPhase().setStep(new DeclareBlockersStep());
break;
case DECLARE_BLOCKERS:
game.getPhase().setStep(new FirstCombatDamageStep());
break;
case FIRST_COMBAT_DAMAGE:
game.getPhase().setStep(new CombatDamageStep());
break;
case COMBAT_DAMAGE:
game.getPhase().setStep(new EndOfCombatStep());
break;
case END_COMBAT:
game.getTurn().setPhase(new PostCombatMainPhase());
game.getPhase().setStep(new PostCombatMainStep());
break;
case POSTCOMBAT_MAIN:
game.getTurn().setPhase(new EndPhase());
game.getPhase().setStep(new EndStep());
break;
case END_TURN:
game.getPhase().setStep(new CleanupStep());
break;
case CLEANUP:
game.getPhase().getStep().beginStep(game, activePlayerId);
if (!game.checkStateAndTriggered() && !game.checkIfGameIsOver()) {
game.getState().setActivePlayerId(game.getState().getPlayerList(game.getActivePlayerId()).getNext());
game.getTurn().setPhase(new BeginningPhase());
game.getPhase().setStep(new UntapStep());
}
}
if (!game.getStep().skipStep(game, game.getActivePlayerId())) {
if (game.getTurn().getStepType() == PhaseStep.DECLARE_ATTACKERS) {
game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_ATTACKERS_STEP_PRE, null, null, activePlayerId));
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_ATTACKERS, activePlayerId, activePlayerId))) {
for (Combat engagement : ((SimulatedPlayer) game.getPlayer(activePlayerId)).addAttackers(game)) {
Game sim = game.copy();
UUID defenderId = game.getOpponents(playerId).iterator().next();
for (CombatGroup group : engagement.getGroups()) {
for (UUID attackerId : group.getAttackers()) {
sim.getPlayer(activePlayerId).declareAttacker(attackerId, defenderId, sim, false);
}
}
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_ATTACKERS, playerId, playerId));
SimulationNode newNode = new SimulationNode(node, sim, activePlayerId);
logger.debug(indent(node.depth) + "simulating -- node #:" + SimulationNode.getCount() + " declare attakers");
newNode.setCombat(sim.getCombat());
node.children.add(newNode);
}
}
} else if (game.getTurn().getStepType() == PhaseStep.DECLARE_BLOCKERS) {
game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_BLOCKERS_STEP_PRE, null, null, activePlayerId));
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_BLOCKERS, activePlayerId, activePlayerId))) {
for (UUID defenderId : game.getCombat().getDefenders()) {
// check if defender is being attacked
if (game.getCombat().isAttacked(defenderId, game)) {
for (Combat engagement : ((SimulatedPlayer) game.getPlayer(defenderId)).addBlockers(game)) {
Game sim = game.copy();
for (CombatGroup group : engagement.getGroups()) {
for (UUID blockerId : group.getBlockers()) {
group.addBlocker(blockerId, defenderId, sim);
}
}
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, playerId, playerId));
SimulationNode newNode = new SimulationNode(node, sim, defenderId);
logger.debug(indent(node.depth) + "simulating -- node #:" + SimulationNode.getCount() + " declare blockers");
newNode.setCombat(sim.getCombat());
node.children.add(newNode);
}
}
}
}
} else {
game.getStep().beginStep(game, activePlayerId);
}
if (game.getStep().getHasPriority())
break;
} else {
skip = true;
}
}
game.checkStateAndTriggered();
}
use of mage.game.events.GameEvent in project mage by magefree.
the class AcolytesRewardEffect method replaceEvent.
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
boolean result = false;
int toPrevent = amount;
if (event.getAmount() < this.amount) {
toPrevent = event.getAmount();
amount -= event.getAmount();
} else {
amount = 0;
}
GameEvent preventEvent = new PreventDamageEvent(event.getTargetId(), source.getSourceId(), source, source.getControllerId(), toPrevent, ((DamageEvent) event).isCombatDamage());
if (game.replaceEvent(preventEvent)) {
return result;
}
Permanent targetCreature = game.getPermanent(event.getTargetId());
if (targetCreature == null) {
return result;
}
if (amount == 0) {
this.used = true;
this.discard();
}
if (event.getAmount() >= toPrevent) {
event.setAmount(event.getAmount() - toPrevent);
} else {
event.setAmount(0);
result = true;
}
if (toPrevent == 0) {
return result;
}
game.informPlayers("Acolyte's Reward prevented " + toPrevent + " to " + targetCreature.getName());
game.fireEvent(new PreventedDamageEvent(event.getTargetId(), source.getSourceId(), source, source.getControllerId(), toPrevent));
Player targetPlayer = game.getPlayer(source.getTargets().get(1).getFirstTarget());
if (targetPlayer != null) {
targetPlayer.damage(toPrevent, source.getSourceId(), source, game);
game.informPlayers("Acolyte's Reward deals " + toPrevent + " damage to " + targetPlayer.getLogName());
return result;
}
Permanent targetDamageCreature = game.getPermanent(source.getTargets().get(1).getFirstTarget());
if (targetDamageCreature == null) {
return result;
}
targetDamageCreature.damage(toPrevent, source.getSourceId(), source, game, false, true);
game.informPlayers("Acolyte's Reward deals " + toPrevent + " damage to " + targetDamageCreature.getName());
return result;
}
use of mage.game.events.GameEvent in project mage by magefree.
the class AncientGreenwardenEffect method applies.
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
if (!(event instanceof NumberOfTriggersEvent)) {
return false;
}
NumberOfTriggersEvent numberOfTriggersEvent = (NumberOfTriggersEvent) event;
if (!source.isControlledBy(event.getPlayerId())) {
return false;
}
GameEvent sourceEvent = numberOfTriggersEvent.getSourceEvent();
if (sourceEvent == null || sourceEvent.getType() != GameEvent.EventType.ENTERS_THE_BATTLEFIELD || !(sourceEvent instanceof EntersTheBattlefieldEvent) || !((EntersTheBattlefieldEvent) sourceEvent).getTarget().isLand(game)) {
return false;
}
return game.getPermanent(numberOfTriggersEvent.getSourceId()) != null;
}
Aggregations