use of mage.abilities.Mode in project mage by magefree.
the class BattlefieldThaumaturgeSpellsCostReductionEffect method getMaxPossibleTargetCreatures.
private int getMaxPossibleTargetCreatures(Ability ability, Game game) {
// checks only one mode, so it can be wrong in rare use cases with multi-modes (example: mode one gives +2 and mode two gives another +1 -- total +3)
int maxAmount = 0;
for (Mode mode : ability.getModes().values()) {
for (Target target : mode.getTargets()) {
if (target.isNotTarget()) {
continue;
}
Set<UUID> possibleList = target.possibleTargets(ability.getSourceId(), ability.getControllerId(), game);
possibleList.removeIf(id -> {
Permanent permanent = game.getPermanent(id);
return permanent == null || !permanent.isCreature(game);
});
int possibleAmount = Math.min(possibleList.size(), target.getMaxNumberOfTargets());
maxAmount = Math.max(maxAmount, possibleAmount);
}
}
return maxAmount;
}
use of mage.abilities.Mode in project mage by magefree.
the class BeamsplitterMageApplier method checkTrigger.
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (!isControlledBy(event.getPlayerId())) {
return false;
}
Spell spell = game.getSpellOrLKIStack(event.getTargetId());
if (spell == null || !spell.isInstantOrSorcery(game)) {
return false;
}
if (spell.getSpellAbilities().stream().map(AbilityImpl::getModes).flatMap(m -> m.getSelectedModes().stream().map(m::get)).filter(Objects::nonNull).map(Mode::getTargets).flatMap(Collection::stream).filter(t -> !t.isNotTarget()).map(Target::getTargets).flatMap(Collection::stream).anyMatch(uuid -> !getSourceId().equals(uuid) && uuid != null)) {
return false;
}
this.getEffects().setValue("spellCast", spell);
return true;
}
use of mage.abilities.Mode in project mage by magefree.
the class CemeteryDesecratorRemoveCountersEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
TargetCardInGraveyard target = new TargetCardInGraveyard(filter);
target.setNotTarget(true);
controller.choose(outcome, target, source.getSourceId(), game);
Card card = game.getCard(target.getFirstTarget());
if (card != null) {
int manaValue = card.getManaValue();
if (controller.moveCards(card, Zone.EXILED, source, game)) {
ReflexiveTriggeredAbility ability = new ReflexiveTriggeredAbility(new CemeteryDesecratorRemoveCountersEffect(manaValue), false, triggerText);
ability.addTarget(new TargetPermanent());
Mode mode = new Mode(new BoostTargetEffect(-manaValue, -manaValue, Duration.EndOfTurn).setText("Target creature an opponent controls gets -X/-X until end of turn, where X is the mana value of the exiled card"));
mode.addTarget(new TargetOpponentsCreaturePermanent());
ability.addMode(mode);
game.fireReflexiveTriggeredAbility(ability, source);
return true;
}
}
}
return false;
}
use of mage.abilities.Mode in project mage by magefree.
the class GripOfChaosEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
StackObject stackObject = game.getStack().getStackObject(this.getTargetPointer().getFirst(game, source));
if (stackObject != null) {
for (UUID modeId : stackObject.getStackAbility().getModes().getSelectedModes()) {
Mode mode = stackObject.getStackAbility().getModes().get(modeId);
for (Target target : mode.getTargets()) {
UUID oldTargetId = target.getFirstTarget();
Set<UUID> possibleTargets = target.possibleTargets(stackObject.getSourceId(), stackObject.getControllerId(), game);
if (possibleTargets.contains(stackObject.getId())) {
// The stackObject can't target itself
possibleTargets.remove(stackObject.getId());
}
if (!possibleTargets.isEmpty()) {
int i = 0;
int rnd = RandomUtil.nextInt(possibleTargets.size());
Iterator<UUID> it = possibleTargets.iterator();
while (i < rnd) {
it.next();
i++;
}
UUID newTargetId = it.next();
target.remove(oldTargetId);
target.add(newTargetId, game);
}
}
}
return true;
}
return false;
}
use of mage.abilities.Mode in project mage by magefree.
the class RicochetEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Spell spell = game.getStack().getSpell(this.getTargetPointer().getFirst(game, source));
if (spell != null) {
Targets targets = new Targets();
Ability sourceAbility = spell.getSpellAbility();
for (UUID modeId : sourceAbility.getModes().getSelectedModes()) {
Mode mode = sourceAbility.getModes().get(modeId);
targets.addAll(mode.getTargets());
}
if (targets.size() != 1 || targets.get(0).getTargets().size() != 1) {
return false;
}
Map<Player, Integer> playerRolls = new HashMap<>();
for (UUID playerId : game.getPlayerList().copy()) {
Player player = game.getPlayer(playerId);
if (player != null) {
playerRolls.put(player, 7);
}
}
do {
for (Player player : playerRolls.keySet()) {
// bad outcome - ai must choose lowest value
playerRolls.put(player, player.rollDice(Outcome.Detriment, source, game, 6));
}
int minValueInMap = Collections.min(playerRolls.values());
for (Map.Entry<Player, Integer> mapEntry : new HashSet<>(playerRolls.entrySet())) {
if (mapEntry.getValue() > minValueInMap) {
playerRolls.remove(mapEntry.getKey());
}
}
} while (playerRolls.size() > 1);
if (playerRolls.size() == 1) {
Player loser = (Player) playerRolls.keySet().toArray()[0];
UUID loserId = loser.getId();
Target target = targets.get(0);
if (target.getFirstTarget().equals(loserId)) {
return true;
}
String oldTargetName = null;
if (target.canTarget(spell.getControllerId(), loserId, sourceAbility, game)) {
Player oldPlayer = game.getPlayer(targets.getFirstTarget());
if (oldPlayer != null) {
oldTargetName = oldPlayer.getLogName();
}
target.clearChosen();
target.addTarget(loserId, sourceAbility, game);
}
MageObject sourceObject = game.getObject(source.getSourceId());
if (oldTargetName != null && sourceObject != null) {
game.informPlayers(sourceObject.getLogName() + ": Changed target of " + spell.getLogName() + " from " + oldTargetName + " to " + loser.getLogName());
}
}
return true;
}
return false;
}
Aggregations