use of mage.game.combat.Combat in project mage by magefree.
the class VortexElementalEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
Combat combat = game.getState().getCombat();
Set<UUID> creaturesToReturn = new HashSet<>();
Set<UUID> playersToShuffle = new HashSet<>();
creaturesToReturn.add(source.getSourceId());
if (combat != null) {
for (CombatGroup combatGroup : combat.getGroups()) {
if (combatGroup.getAttackers().contains(source.getSourceId())) {
creaturesToReturn.addAll(combatGroup.getBlockers());
} else if (combatGroup.getBlockers().contains(source.getSourceId())) {
creaturesToReturn.addAll(combatGroup.getAttackers());
}
}
}
for (UUID creatureId : creaturesToReturn) {
Permanent creature = game.getPermanent(creatureId);
if (creature != null) {
playersToShuffle.add(creature.getControllerId());
}
}
Cards toLib = new CardsImpl(creaturesToReturn);
controller.putCardsOnTopOfLibrary(toLib, game, source, false);
for (UUID playerId : playersToShuffle) {
Player player = game.getPlayer(playerId);
if (player != null) {
player.shuffleLibrary(source, game);
}
}
return true;
}
return false;
}
use of mage.game.combat.Combat in project mage by magefree.
the class MandateOfPeaceEndCombatEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Combat combat = game.getCombat();
List<UUID> attackerIds = combat.getAttackers();
List<UUID> blockerIds = combat.getBlockers();
Stream.concat(blockerIds.stream(), attackerIds.stream()).map(id -> game.getPermanent(id)).filter(e -> e != null).forEach(permanent -> permanent.removeFromCombat(game));
combat.endCombat(game);
if (!game.getStack().isEmpty()) {
game.getStack().stream().filter(stackObject -> stackObject instanceof Spell).forEach(stackObject -> ((Spell) stackObject).moveToExile(null, "", null, game));
game.getStack().stream().filter(stackObject -> stackObject instanceof Ability).forEach(stackObject -> game.getStack().remove(stackObject, game));
}
return true;
}
use of mage.game.combat.Combat in project mage by magefree.
the class SimulatedPlayer2 method addAttackers.
public List<Combat> addAttackers(Game game) {
Map<Integer, Combat> engagements = new HashMap<>();
// useful only for two player games - will only attack first opponent
UUID defenderId = game.getOpponents(playerId).iterator().next();
List<Permanent> attackersList = super.getAvailableAttackers(defenderId, game);
// use binary digits to calculate powerset of attackers
int powerElements = (int) Math.pow(2, attackersList.size());
StringBuilder binary = new StringBuilder();
for (int i = powerElements - 1; i >= 0; i--) {
Game sim = game.copy();
binary.setLength(0);
binary.append(Integer.toBinaryString(i));
while (binary.length() < attackersList.size()) {
binary.insert(0, '0');
}
for (int j = 0; j < attackersList.size(); j++) {
if (binary.charAt(j) == '1') {
// makes it possible to UNDO a declared attacker with costs from e.g. Propaganda
setStoredBookmark(sim.bookmarkState());
if (!sim.getCombat().declareAttacker(attackersList.get(j).getId(), defenderId, playerId, sim)) {
sim.undo(playerId);
}
}
}
if (engagements.put(sim.getCombat().getValue().hashCode(), sim.getCombat()) != null) {
logger.debug("simulating -- found redundant attack combination");
} else {
logger.debug("simulating -- attack:" + sim.getCombat().getGroups().size());
}
}
List list = new ArrayList<>(engagements.values());
Collections.sort(list, new Comparator<Combat>() {
@Override
public int compare(Combat o1, Combat o2) {
return Integer.valueOf(o2.getGroups().size()).compareTo(Integer.valueOf(o1.getGroups().size()));
}
});
return list;
}
use of mage.game.combat.Combat in project mage by magefree.
the class SimulatedPlayer2 method addBlockers.
public List<Combat> addBlockers(Game game) {
Map<Integer, Combat> engagements = new HashMap<>();
int numGroups = game.getCombat().getGroups().size();
if (numGroups == 0) {
return Collections.emptyList();
}
// add a node with no blockers
Game sim = game.copy();
engagements.put(sim.getCombat().getValue().hashCode(), sim.getCombat());
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, playerId, playerId));
List<Permanent> blockers = getAvailableBlockers(game);
addBlocker(game, blockers, engagements);
return new ArrayList<>(engagements.values());
}
use of mage.game.combat.Combat 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();
}
Aggregations