use of mage.game.combat.CombatGroup in project mage by magefree.
the class FlashFoliageEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
Token token = new SaprolingToken();
token.putOntoBattlefield(1, game, source, source.getControllerId());
Permanent attackingCreature = game.getPermanent(getTargetPointer().getFirst(game, source));
if (attackingCreature != null && game.getState().getCombat() != null) {
// Possible ruling (see Aetherplasm)
// The token you created is blocking the attacking creature,
// even if the block couldn't legally be declared (for example, if that creature
// enters the battlefield tapped, or it can't block, or the attacking creature
// has protection from it)
CombatGroup combatGroup = game.getState().getCombat().findGroup(attackingCreature.getId());
if (combatGroup != null) {
for (UUID tokenId : token.getLastAddedTokenIds()) {
Permanent saprolingToken = game.getPermanent(tokenId);
if (saprolingToken != null) {
combatGroup.addBlocker(tokenId, source.getControllerId(), game);
game.getCombat().addBlockingGroup(tokenId, attackingCreature.getId(), controller.getId(), game);
}
}
combatGroup.pickBlockerOrder(attackingCreature.getControllerId(), game);
}
}
return true;
}
return false;
}
use of mage.game.combat.CombatGroup in project mage by magefree.
the class GeneralJarkeldSwitchBlockersEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
List<UUID> targets = source.getTargets().get(0).getTargets();
if (controller != null && targets != null) {
Permanent attacker1 = game.getPermanent(targets.get(0));
Permanent attacker2 = game.getPermanent(targets.get(1));
if (attacker1 != null && attacker2 != null) {
CombatGroup chosenGroup1 = game.getCombat().findGroup(attacker1.getId());
CombatGroup chosenGroup2 = game.getCombat().findGroup(attacker2.getId());
if (chosenGroup1 != null && chosenGroup2 != null) {
Set<Permanent> blockers1 = new HashSet<>();
Set<Permanent> blockers2 = new HashSet<>();
Set<Permanent> multiBlockers = new HashSet<>();
blockerSearch1: for (UUID blockerId : chosenGroup1.getBlockers()) {
Permanent blocker = game.getPermanent(blockerId);
if (blocker != null) {
if (game.getCombat().blockingGroupsContains(blocker.getId())) {
// if (blocker.getBlocking() > 1) {
for (CombatGroup group : game.getCombat().getBlockingGroups()) {
if (group.getBlockers().contains(blocker.getId())) {
int attackerCount = group.getAttackers().size();
if (attackerCount > 1) {
multiBlockers.add(blocker);
} else if (attackerCount == 1) {
blockers1.add(blocker);
}
continue blockerSearch1;
}
}
} else {
blockers1.add(blocker);
}
}
}
blockerSearch2: for (UUID blockerId : chosenGroup2.getBlockers()) {
Permanent blocker = game.getPermanent(blockerId);
if (blocker != null) {
if (game.getCombat().blockingGroupsContains(blocker.getId())) {
// if (blocker.getBlocking() > 1) {
for (CombatGroup group : game.getCombat().getBlockingGroups()) {
if (group.getBlockers().contains(blocker.getId())) {
int attackerCount = group.getAttackers().size();
if (attackerCount > 1) {
multiBlockers.add(blocker);
} else if (attackerCount == 1) {
blockers2.add(blocker);
}
continue blockerSearch2;
}
}
} else {
blockers2.add(blocker);
}
}
}
handleSingleBlockers(blockers1, chosenGroup1, chosenGroup2, controller, game);
handleSingleBlockers(blockers2, chosenGroup2, chosenGroup1, controller, game);
handleMultiBlockers(multiBlockers, chosenGroup1, chosenGroup2, controller, game);
// the ability doesn't unblock a group that loses all blockers, however it will newly block a previously unblocked group if it gains a blocker this way
if (!(chosenGroup1.getBlockers().isEmpty())) {
chosenGroup1.setBlocked(true, game);
chosenGroup1.pickBlockerOrder(attacker1.getControllerId(), game);
}
if (!(chosenGroup2.getBlockers().isEmpty())) {
chosenGroup2.setBlocked(true, game);
chosenGroup2.pickBlockerOrder(attacker2.getControllerId(), game);
}
return true;
}
}
}
return false;
}
use of mage.game.combat.CombatGroup in project mage by magefree.
the class MirrorMatchEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
for (UUID attackerId : game.getCombat().getAttackers()) {
Permanent attacker = game.getPermanent(attackerId);
if (attacker != null && source.isControlledBy(game.getCombat().getDefendingPlayerId(attackerId, game))) {
CreateTokenCopyTargetEffect effect = new CreateTokenCopyTargetEffect(source.getControllerId(), null, false);
effect.setTargetPointer(new FixedTarget(attacker, game));
effect.apply(game, source);
CombatGroup group = game.getCombat().findGroup(attacker.getId());
boolean isCreature = false;
if (group != null) {
for (Permanent addedToken : effect.getAddedPermanents()) {
if (addedToken.isCreature(game)) {
group.addBlockerToGroup(addedToken.getId(), attackerId, game);
isCreature = true;
}
}
ExileTargetEffect exileEffect = new ExileTargetEffect("Exile those tokens at end of combat");
exileEffect.setTargetPointer(new FixedTargets(effect.getAddedPermanents(), game));
game.addDelayedTriggeredAbility(new AtTheEndOfCombatDelayedTriggeredAbility(exileEffect), source);
if (isCreature) {
group.pickBlockerOrder(attacker.getControllerId(), game);
}
}
}
}
return true;
}
return false;
}
use of mage.game.combat.CombatGroup in project mage by magefree.
the class PortalMageEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
Permanent attackingCreature = game.getPermanent(getTargetPointer().getFirst(game, source));
if (attackingCreature != null) {
CombatGroup combatGroupTarget = null;
for (CombatGroup combatGroup : game.getCombat().getGroups()) {
if (combatGroup.getAttackers().contains(attackingCreature.getId())) {
combatGroupTarget = combatGroup;
break;
}
}
if (combatGroupTarget == null) {
return false;
}
// Reselecting which player or planeswalker a creature is attacking ignores all requirements,
// restrictions, and costs associated with attacking.
// Update possible defender
Set<UUID> defenders = new LinkedHashSet<>();
for (UUID playerId : game.getCombat().getAttackablePlayers(game)) {
defenders.add(playerId);
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_PERMANENT_PLANESWALKER, playerId, game)) {
defenders.add(permanent.getId());
}
}
// Select the new defender
TargetDefender target = new TargetDefender(defenders, null);
if (controller.chooseTarget(Outcome.Damage, target, source, game)) {
if (!combatGroupTarget.getDefenderId().equals(target.getFirstTarget())) {
if (combatGroupTarget.changeDefenderPostDeclaration(target.getFirstTarget(), game)) {
String attacked = "";
Player player = game.getPlayer(target.getFirstTarget());
if (player != null) {
attacked = player.getLogName();
} else {
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent != null) {
attacked = permanent.getLogName();
}
}
game.informPlayers(attackingCreature.getLogName() + " now attacks " + attacked);
return true;
}
}
}
}
return true;
}
return false;
}
use of mage.game.combat.CombatGroup in project mage by magefree.
the class YdwenEfreetEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Permanent creature = game.getPermanent(source.getSourceId());
if (controller != null && creature != null) {
if (!controller.flipCoin(source, game, true)) {
creature.removeFromCombat(game);
creature.setMaxBlocks(0);
// Make blocked creatures unblocked
BlockedByOnlyOneCreatureThisCombatWatcher watcher = game.getState().getWatcher(BlockedByOnlyOneCreatureThisCombatWatcher.class);
if (watcher != null) {
Set<CombatGroup> combatGroups = watcher.getBlockedOnlyByCreature(creature.getId());
if (combatGroups != null) {
for (CombatGroup combatGroup : combatGroups) {
if (combatGroup != null) {
combatGroup.setBlocked(false, game);
}
}
}
}
}
return true;
}
return false;
}
Aggregations