use of mage.game.events.GameEvent in project mage by magefree.
the class FlameskullWatcher method watch.
@Override
public void watch(GameEvent event, Game game) {
if (event.getType() != GameEvent.EventType.SPELL_CAST || event.getAdditionalReference() == null) {
return;
}
MageObjectReference mor = event.getAdditionalReference().getApprovingMageObjectReference();
Spell spell = game.getSpell(event.getTargetId());
if (mor == null || spell == null) {
return;
}
morMap.computeIfAbsent(mor, x -> new HashSet<>()).add(new MageObjectReference(spell.getMainCard(), game, -1));
}
use of mage.game.events.GameEvent in project mage by magefree.
the class NineLivesStateTriggeredAbility method replaceEvent.
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
GameEvent preventEvent = new PreventDamageEvent(event.getTargetId(), source.getSourceId(), source, source.getControllerId(), event.getAmount(), ((DamageEvent) event).isCombatDamage());
if (!game.replaceEvent(preventEvent)) {
int damage = event.getAmount();
Player player = game.getPlayer(source.getControllerId());
if (player != null) {
Permanent nineLives = source.getSourcePermanentIfItStillExists(game);
if (nineLives != null) {
nineLives.addCounters(CounterType.INCARNATION.createInstance(1), source.getControllerId(), source, game);
}
}
event.setAmount(0);
game.fireEvent(new PreventedDamageEvent(event.getTargetId(), source.getSourceId(), source, source.getControllerId(), damage));
}
return false;
}
use of mage.game.events.GameEvent in project mage by magefree.
the class VoltaicVisionaryWatcher method watch.
@Override
public void watch(GameEvent event, Game game) {
if (event.getType() != GameEvent.EventType.ZONE_CHANGE || ((ZoneChangeEvent) event).getToZone() != Zone.EXILED) {
return;
}
Card card = game.getCard(event.getTargetId());
UUID exileId = game.getExile().getExileZones().stream().filter(exileZone -> exileZone.contains(card)).map(ExileZone::getId).findFirst().orElse(null);
if (exileId == null) {
return;
}
map.computeIfAbsent(exileId, x -> new HashSet<>()).add(new MageObjectReference(card, game));
}
use of mage.game.events.GameEvent in project mage by magefree.
the class CascadeEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
return false;
}
Card sourceCard = game.getCard(source.getSourceId());
if (sourceCard == null) {
return false;
}
// exile cards from the top of your library until you exile a nonland card whose converted mana cost is less than this spell's converted mana cost
Cards cardsToExile = new CardsImpl();
int sourceCost = sourceCard.getManaValue();
Card cardToCast = null;
for (Card card : controller.getLibrary().getCards(game)) {
cardsToExile.add(card);
// the card move is sequential, not all at once.
controller.moveCards(card, Zone.EXILED, source, game);
// Laelia, the Blade Reforged
game.getState().processAction(game);
if (!card.isLand(game) && card.getManaValue() < sourceCost) {
cardToCast = card;
break;
}
}
// set back empty draw state if that caused an empty draw
controller.getLibrary().reset();
// additional replacement effect: As you cascade, you may put a land card from among the exiled cards onto the battlefield tapped
GameEvent event = GameEvent.getEvent(GameEvent.EventType.CASCADE_LAND, source.getSourceId(), source, source.getControllerId(), 0);
game.replaceEvent(event);
if (event.getAmount() > 0) {
TargetCardInExile target = new TargetCardInExile(0, event.getAmount(), StaticFilters.FILTER_CARD_LAND, null, true);
target.withChooseHint("land to put onto battlefield tapped");
controller.choose(Outcome.PutCardInPlay, cardsToExile, target, game);
controller.moveCards(new CardsImpl(target.getTargets()).getCards(game), Zone.BATTLEFIELD, source, game, true, false, false, null);
}
// You may cast that spell without paying its mana cost if its converted mana cost is less than this spell's converted mana cost.
List<Card> partsToCast = new ArrayList<>();
if (cardToCast != null) {
if (cardToCast instanceof SplitCard) {
partsToCast.add(((SplitCard) cardToCast).getLeftHalfCard());
partsToCast.add(((SplitCard) cardToCast).getRightHalfCard());
partsToCast.add(cardToCast);
} else if (cardToCast instanceof AdventureCard) {
partsToCast.add(((AdventureCard) cardToCast).getSpellCard());
partsToCast.add(cardToCast);
} else if (cardToCast instanceof ModalDoubleFacesCard) {
partsToCast.add(((ModalDoubleFacesCard) cardToCast).getLeftHalfCard());
partsToCast.add(((ModalDoubleFacesCard) cardToCast).getRightHalfCard());
} else {
partsToCast.add(cardToCast);
}
// remove too big cmc
partsToCast.removeIf(card -> card.getManaValue() >= sourceCost);
// remove non spells
partsToCast.removeIf(card -> card.getSpellAbility() == null);
}
String partsInfo = partsToCast.stream().map(MageObject::getIdName).collect(Collectors.joining(" or "));
if (cardToCast != null && partsToCast.size() > 0 && controller.chooseUse(outcome, "Cast spell without paying its mana cost (" + partsInfo + ")?", source, game)) {
try {
// enable free cast for all compatible parts
partsToCast.forEach(card -> game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), Boolean.TRUE));
controller.cast(controller.chooseAbilityForCast(cardToCast, game, true), game, true, new ApprovingObject(source, game));
} finally {
partsToCast.forEach(card -> game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), null));
}
}
// Then put all cards exiled this way that weren't cast on the bottom of your library in a random order.
cardsToExile.removeIf(uuid -> game.getState().getZone(uuid) != Zone.EXILED);
return controller.putCardsOnBottomOfLibrary(cardsToExile, game, source, false);
}
use of mage.game.events.GameEvent in project mage by magefree.
the class CrewCost method pay.
@Override
public boolean pay(Ability ability, Game game, Ability source, UUID controllerId, boolean noMana, Cost costToPay) {
if (handleAltCost(ability, game, source, controllerId, noMana, costToPay)) {
paid = true;
return true;
}
Target target = new TargetControlledCreaturePermanent(0, Integer.MAX_VALUE, filter, true) {
@Override
public String getMessage() {
// shows selected power
int selectedPower = this.targets.entrySet().stream().map(entry -> (game.getPermanent(entry.getKey()))).filter(Objects::nonNull).mapToInt(p -> (getCrewPower(p, game))).sum();
String extraInfo = "(selected power " + selectedPower + " of " + value + ")";
if (selectedPower >= value) {
extraInfo = HintUtils.prepareText(extraInfo, Color.GREEN);
}
return super.getMessage() + " " + extraInfo;
}
};
// can cancel
if (target.choose(Outcome.Tap, controllerId, source.getSourceId(), game)) {
int sumPower = 0;
for (UUID targetId : target.getTargets()) {
GameEvent event = new GameEvent(GameEvent.EventType.CREW_VEHICLE, targetId, source, controllerId);
if (!game.replaceEvent(event)) {
Permanent permanent = game.getPermanent(targetId);
if (permanent != null && permanent.tap(source, game)) {
sumPower += getCrewPower(permanent, game);
}
}
}
paid = sumPower >= value;
if (paid) {
for (UUID targetId : target.getTargets()) {
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.CREWED_VEHICLE, targetId, source, controllerId));
}
}
} else {
return false;
}
return paid;
}
Aggregations