use of mage.game.permanent.Permanent in project mage by magefree.
the class HumanPlayer method selectBlockers.
@Override
public void selectBlockers(Ability source, Game game, UUID defendingPlayerId) {
if (gameInCheckPlayableState(game)) {
return;
}
FilterCreatureForCombatBlock filter = filterCreatureForCombatBlock.copy();
filter.add(new ControllerIdPredicate(defendingPlayerId));
// stop skip on any/zero permanents available
int possibleBlockersCount = game.getBattlefield().count(filter, null, playerId, game);
boolean canStopOnAny = possibleBlockersCount != 0 && getControllingPlayersUserData(game).getUserSkipPrioritySteps().isStopOnDeclareBlockersWithAnyPermanents();
boolean canStopOnZero = possibleBlockersCount == 0 && getControllingPlayersUserData(game).getUserSkipPrioritySteps().isStopOnDeclareBlockersWithZeroPermanents();
// skip declare blocker step
// as opposed to declare attacker - it can be skipped by ANY skip button TODO: make same for declare attackers and rework skip buttons (normal and forced)
boolean skipButtonActivated = passedAllTurns || passedUntilEndStepBeforeMyTurn || passedTurn || passedUntilEndOfTurn || passedUntilNextMain;
if (skipButtonActivated && !canStopOnAny && !canStopOnZero) {
return;
}
while (canRespond()) {
updateGameStatePriority("selectBlockers", game);
prepareForResponse(game);
if (!isExecutingMacro()) {
Map<String, Serializable> options = new HashMap<>();
java.util.List<UUID> possibleBlockers = game.getBattlefield().getActivePermanents(filter, playerId, game).stream().map(p -> p.getId()).collect(Collectors.toList());
options.put(Constants.Option.POSSIBLE_BLOCKERS, (Serializable) possibleBlockers);
game.fireSelectEvent(playerId, "Select blockers", options);
}
waitForResponse(game);
UUID responseId = getFixedResponseUUID(game);
if (response.getBoolean() != null) {
return;
} else if (response.getInteger() != null) {
return;
} else if (responseId != null) {
Permanent blocker = game.getPermanent(responseId);
if (blocker != null) {
boolean removeBlocker = false;
// does not block yet and can block or can block more attackers
if (filter.match(blocker, null, playerId, game)) {
selectCombatGroup(defendingPlayerId, blocker.getId(), game);
} else if (filterBlock.match(blocker, null, playerId, game) && game.getStack().isEmpty()) {
removeBlocker = true;
}
if (removeBlocker) {
game.getCombat().removeBlocker(blocker.getId(), game);
}
}
}
}
}
use of mage.game.permanent.Permanent in project mage by magefree.
the class HumanPlayer method selectDefender.
/**
* Selects a defender for an attacker and adds the attacker to combat
*
* @param defenders - list of possible defender
* @param attackerId - UUID of attacker
* @param game
* @return
*/
protected boolean selectDefender(Set<UUID> defenders, UUID attackerId, Game game) {
boolean forcedToAttack = false;
Set<UUID> possibleDefender = game.getCombat().getCreaturesForcedToAttack().get(attackerId);
if (possibleDefender != null) {
forcedToAttack = true;
}
if (possibleDefender == null || possibleDefender.isEmpty()) {
possibleDefender = defenders;
}
if (possibleDefender.size() == 1) {
declareAttacker(attackerId, possibleDefender.iterator().next(), game, true);
return true;
} else {
TargetDefender target = new TargetDefender(possibleDefender, attackerId);
if (forcedToAttack) {
StringBuilder sb = new StringBuilder(target.getTargetName());
Permanent attacker = game.getPermanent(attackerId);
if (attacker != null) {
sb.append(" (").append(attacker.getName()).append(')');
target.setTargetName(sb.toString());
}
}
if (chooseTarget(Outcome.Damage, target, null, game)) {
UUID defenderId = getFixedResponseUUID(game);
declareAttacker(attackerId, defenderId, game, true);
return true;
}
}
return false;
}
use of mage.game.permanent.Permanent in project mage by magefree.
the class ComputerPlayer method findBestPermanentTargets.
protected void findBestPermanentTargets(Outcome outcome, UUID abilityControllerId, UUID sourceId, FilterPermanent filter, Game game, Target target, List<Permanent> goodList, List<Permanent> badList, List<Permanent> allList) {
// searching for most valuable/powerfull permanents
goodList.clear();
badList.clear();
allList.clear();
List<UUID> usedTargets = target.getTargets();
// search all
for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, abilityControllerId, sourceId, game)) {
if (usedTargets.contains(permanent.getId())) {
continue;
}
if (outcome.isGood()) {
// good effect
if (permanent.isControlledBy(abilityControllerId)) {
goodList.add(permanent);
} else {
badList.add(permanent);
}
} else {
// bad effect
if (permanent.isControlledBy(abilityControllerId)) {
badList.add(permanent);
} else {
goodList.add(permanent);
}
}
}
// sort from tiny to big (more valuable)
PermanentComparator comparator = new PermanentComparator(game);
goodList.sort(comparator);
badList.sort(comparator);
// most valueable goes first in good list
Collections.reverse(goodList);
// most weakest goes first in bad list (no need to reverse)
// Collections.reverse(badList);
allList.addAll(goodList);
allList.addAll(badList);
// "can target all mode" don't need your/opponent lists -- all targets goes with same value
if (outcome.isCanTargetAll()) {
// bad sort
allList.sort(comparator);
if (outcome.isGood()) {
// good sort
Collections.reverse(allList);
}
goodList.clear();
goodList.addAll(allList);
badList.clear();
badList.addAll(allList);
}
}
use of mage.game.permanent.Permanent in project mage by magefree.
the class ComputerPlayer method simulateAttack.
protected CombatSimulator simulateAttack(Attackers attackers, List<Permanent> blockers, UUID opponentId, Game game) {
log.debug("simulateAttack");
List<Permanent> attackersList = attackers.getAttackers();
CombatSimulator best = new CombatSimulator();
int bestResult = 0;
// use binary digits to calculate powerset of attackers
int powerElements = (int) Math.pow(2, attackersList.size());
for (int i = 1; i < powerElements; i++) {
String binary = Integer.toBinaryString(i);
while (binary.length() < attackersList.size()) {
binary = '0' + binary;
}
List<Permanent> trialAttackers = new ArrayList<>();
for (int j = 0; j < attackersList.size(); j++) {
if (binary.charAt(j) == '1') {
trialAttackers.add(attackersList.get(j));
}
}
CombatSimulator combat = new CombatSimulator();
for (Permanent permanent : trialAttackers) {
combat.groups.add(new CombatGroupSimulator(opponentId, Arrays.asList(permanent.getId()), new ArrayList<UUID>(), game));
}
CombatSimulator test = simulateBlock(combat, blockers, game);
if (test.evaluate() > bestResult) {
best = test;
bestResult = test.evaluate();
}
}
return best;
}
use of mage.game.permanent.Permanent in project mage by magefree.
the class ComputerPlayer method selectAttackers.
@Override
public void selectAttackers(Game game, UUID attackingPlayerId) {
log.debug("selectAttackers");
UUID opponentId = game.getCombat().getDefenders().iterator().next();
Attackers attackers = getPotentialAttackers(game);
List<Permanent> blockers = getOpponentBlockers(opponentId, game);
List<Permanent> actualAttackers = new ArrayList<>();
if (blockers.isEmpty()) {
actualAttackers = attackers.getAttackers();
} else if (attackers.size() - blockers.size() >= game.getPlayer(opponentId).getLife()) {
actualAttackers = attackers.getAttackers();
} else {
CombatSimulator combat = simulateAttack(attackers, blockers, opponentId, game);
if (combat.rating > 2) {
for (CombatGroupSimulator group : combat.groups) {
this.declareAttacker(group.attackers.get(0).id, group.defenderId, game, false);
}
}
}
for (Permanent attacker : actualAttackers) {
this.declareAttacker(attacker.getId(), opponentId, game, false);
}
}
Aggregations