use of mage.abilities.effects.RequirementEffect in project mage by magefree.
the class CrashingBoarsEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player defendingPlayer = game.getPlayer(this.getTargetPointer().getFirst(game, source));
if (defendingPlayer != null) {
Target target = new TargetControlledCreaturePermanent(1, 1, filter, true);
if (target.choose(Outcome.Neutral, defendingPlayer.getId(), source.getSourceId(), game)) {
RequirementEffect effect = new MustBeBlockedByTargetSourceEffect();
effect.setTargetPointer(new FixedTarget(target.getFirstTarget(), game));
game.addEffect(effect, source);
}
return true;
}
return false;
}
use of mage.abilities.effects.RequirementEffect in project mage by magefree.
the class HumanPlayer method checkIfAttackersValid.
private boolean checkIfAttackersValid(Game game) {
if (!game.getCombat().getCreaturesForcedToAttack().isEmpty()) {
if (!game.getCombat().getAttackers().containsAll(game.getCombat().getCreaturesForcedToAttack().keySet())) {
int forcedAttackers = 0;
StringBuilder sb = new StringBuilder();
for (UUID creatureId : game.getCombat().getCreaturesForcedToAttack().keySet()) {
boolean validForcedAttacker = false;
if (game.getCombat().getAttackers().contains(creatureId)) {
Set<UUID> possibleDefender = game.getCombat().getCreaturesForcedToAttack().get(creatureId);
if (possibleDefender.isEmpty() || possibleDefender.contains(game.getCombat().getDefenderId(creatureId))) {
validForcedAttacker = true;
}
}
if (validForcedAttacker) {
forcedAttackers++;
} else {
Permanent creature = game.getPermanent(creatureId);
if (creature != null) {
sb.append(creature.getIdName()).append(' ');
}
}
}
if (game.getCombat().getMaxAttackers() > forcedAttackers) {
int requireToAttack = Math.min(game.getCombat().getMaxAttackers() - forcedAttackers, game.getCombat().getCreaturesForcedToAttack().size() - forcedAttackers);
String message = (requireToAttack == 1 ? " more attacker that is " : " more attackers that are ") + "forced to attack.\nCreature" + (requireToAttack == 1 ? "" : "s") + " forced to attack: ";
game.informPlayer(this, sb.insert(0, message).insert(0, requireToAttack).insert(0, "You have to attack with ").toString());
return false;
}
}
}
// check if enough attackers are declared
// check if players have to be attacked
Set<UUID> playersToAttackIfAble = new HashSet<>();
for (Map.Entry<RequirementEffect, Set<Ability>> entry : game.getContinuousEffects().getApplicableRequirementEffects(null, true, game).entrySet()) {
RequirementEffect effect = entry.getKey();
for (Ability ability : entry.getValue()) {
UUID playerToAttack = effect.playerMustBeAttackedIfAble(ability, game);
if (playerToAttack != null) {
playersToAttackIfAble.add(playerToAttack);
}
}
}
if (!playersToAttackIfAble.isEmpty()) {
Set<UUID> checkPlayersToAttackIfAble = new HashSet<>(playersToAttackIfAble);
for (CombatGroup combatGroup : game.getCombat().getGroups()) {
checkPlayersToAttackIfAble.remove(combatGroup.getDefendingPlayerId());
}
for (UUID forcedToAttackId : checkPlayersToAttackIfAble) {
Player forcedToAttack = game.getPlayer(forcedToAttackId);
for (Permanent attacker : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, getId(), game)) {
if (game.getContinuousEffects().checkIfThereArePayCostToAttackBlockEffects(new DeclareAttackerEvent(forcedToAttackId, attacker.getId(), attacker.getControllerId()), game)) {
continue;
}
// if attacker needs a specific defender to attack so select that one instead
if (game.getCombat().getCreaturesForcedToAttack().containsKey(attacker.getId())) {
Set<UUID> possibleDefenders = game.getCombat().getCreaturesForcedToAttack().get(attacker.getId());
if (!possibleDefenders.isEmpty() && !possibleDefenders.contains(forcedToAttackId)) {
continue;
}
}
UUID defendingPlayerId = game.getCombat().getDefendingPlayerId(attacker.getId(), game);
if (playersToAttackIfAble.contains(defendingPlayerId)) {
// already attacks other player taht has to be attacked
continue;
}
if (defendingPlayerId != null || attacker.canAttackInPrinciple(forcedToAttackId, game)) {
game.informPlayer(this, "You are forced to attack " + forcedToAttack.getName() + " or a controlled planeswalker e.g. with " + attacker.getIdName() + ".");
return false;
}
}
}
}
return true;
}
Aggregations