use of mage.abilities.mana.ActivatedManaAbilityImpl in project mage by magefree.
the class PithingNeedleEffect method applies.
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
MageObject object = game.getObject(event.getSourceId());
String cardName = (String) game.getState().getValue(source.getSourceId().toString() + ChooseACardNameEffect.INFO_KEY);
Optional<Ability> ability = game.getAbility(event.getTargetId(), event.getSourceId());
if (ability.isPresent() && object != null) {
return // controller in range
game.getState().getPlayersInRange(source.getControllerId(), game).contains(event.getPlayerId()) && // not an activated mana ability
!(ability.get() instanceof ActivatedManaAbilityImpl) && CardUtil.haveSameNames(object, cardName, game);
}
return false;
}
use of mage.abilities.mana.ActivatedManaAbilityImpl in project mage by magefree.
the class PowerSinkCounterUnlessPaysEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
StackObject spell = game.getStack().getStackObject(targetPointer.getFirst(game, source));
if (spell != null) {
Player player = game.getPlayer(spell.getControllerId());
Player controller = game.getPlayer(source.getControllerId());
MageObject sourceObject = game.getObject(source.getSourceId());
if (player != null && controller != null && sourceObject != null) {
int amount = source.getManaCostsToPay().getX();
if (amount > 0) {
Cost cost = ManaUtil.createManaCost(amount, true);
if (player.chooseUse(Outcome.Benefit, "Pay " + cost.getText() + " to prevent?", source, game)) {
if (cost.pay(source, game, source, player.getId(), false)) {
game.informPlayers(sourceObject.getName() + ": additional cost was paid");
return true;
}
}
game.informPlayers(sourceObject.getName() + ": additional cost wasn't paid - countering " + spell.getName());
// Counter target spell unless its controller pays {X}
game.getStack().counter(source.getFirstTarget(), source, game);
// that player taps all lands with mana abilities they control...
List<Permanent> lands = game.getBattlefield().getAllActivePermanents(new FilterLandPermanent(), player.getId(), game);
for (Permanent land : lands) {
Abilities<Ability> landAbilities = land.getAbilities();
for (Ability ability : landAbilities) {
if (ability instanceof ActivatedManaAbilityImpl) {
land.tap(source, game);
break;
}
}
}
// ...and empties their mana pool
player.getManaPool().emptyPool(game);
}
return true;
}
}
return false;
}
use of mage.abilities.mana.ActivatedManaAbilityImpl in project mage by magefree.
the class PlayerImpl method findActivatedAbilityFromPlayable.
protected ActivatedAbility findActivatedAbilityFromPlayable(MageObject object, ManaOptions availableMana, Ability ability, Game game) {
// special mana to pay spell cost
ManaOptions manaFull = availableMana.copy();
if (ability instanceof SpellAbility) {
for (AlternateManaPaymentAbility altAbility : CardUtil.getAbilities(object, game).stream().filter(a -> a instanceof AlternateManaPaymentAbility).map(a -> (AlternateManaPaymentAbility) a).collect(Collectors.toList())) {
ManaOptions manaSpecial = altAbility.getManaOptions(ability, game, ability.getManaCostsToPay());
manaFull.addMana(manaSpecial);
}
}
// replace alternative abilities by real play abilities (e.g. morph/facedown static ability by play land)
if (ability instanceof ActivatedManaAbilityImpl) {
// mana ability
if (((ActivatedManaAbilityImpl) ability).canActivate(this.getId(), game).canActivate()) {
return (ActivatedManaAbilityImpl) ability;
}
} else if (ability instanceof AlternativeSourceCosts) {
// alternative cost must be replaced by real play ability
return findActivatedAbilityFromAlternativeSourceCost(object, manaFull, ability, game);
} else if (ability instanceof ActivatedAbility) {
// all other activated ability
if (canPlay((ActivatedAbility) ability, manaFull, object, game)) {
return (ActivatedAbility) ability;
}
}
// non playable abilities like static
return null;
}
use of mage.abilities.mana.ActivatedManaAbilityImpl in project mage by magefree.
the class PlayerImpl method getAvailableManaProducers.
// returns only mana producers that don't require mana payment
protected List<MageObject> getAvailableManaProducers(Game game) {
List<MageObject> result = new ArrayList<>();
for (Permanent permanent : game.getBattlefield().getActivePermanents(playerId, game)) {
// Some permanents allow use of abilities from non controlling players. so check all permanents in range
Boolean canUse = null;
boolean canAdd = false;
for (ActivatedManaAbilityImpl ability : permanent.getAbilities().getActivatedManaAbilities(Zone.BATTLEFIELD)) {
if (!ability.getManaCosts().isEmpty()) {
canAdd = false;
break;
}
if (canUse == null) {
canUse = permanent.canUseActivatedAbilities(game);
}
if (canUse && ability.canActivate(playerId, game).canActivate()) {
canAdd = true;
}
}
if (canAdd) {
result.add(permanent);
}
}
for (Card card : getHand().getCards(game)) {
boolean canAdd = false;
for (ActivatedManaAbilityImpl ability : card.getAbilities(game).getActivatedManaAbilities(Zone.HAND)) {
if (!ability.getManaCosts().isEmpty()) {
canAdd = false;
break;
}
if (ability.canActivate(playerId, game).canActivate()) {
canAdd = true;
}
}
if (canAdd) {
result.add(card);
}
}
return result;
}
use of mage.abilities.mana.ActivatedManaAbilityImpl in project mage by magefree.
the class PlayerImpl method getManaAvailable.
/**
* Returns the mana options the player currently has. That means which
* combinations of mana are available to cast spells or activate abilities
* etc.
*
* @param game
* @return
*/
@Override
public ManaOptions getManaAvailable(Game game) {
boolean oldState = game.inCheckPlayableState();
game.setCheckPlayableState(true);
ManaOptions availableMana = new ManaOptions();
availableMana.addMana(manaPool.getMana());
// conditional mana
for (ConditionalMana conditionalMana : manaPool.getConditionalMana()) {
availableMana.addMana(conditionalMana);
}
List<Abilities<ActivatedManaAbilityImpl>> sourceWithoutManaCosts = new ArrayList<>();
List<Abilities<ActivatedManaAbilityImpl>> sourceWithCosts = new ArrayList<>();
for (Card card : getHand().getCards(game)) {
Abilities<ActivatedManaAbilityImpl> manaAbilities = card.getAbilities(game).getAvailableActivatedManaAbilities(Zone.HAND, playerId, game);
for (Iterator<ActivatedManaAbilityImpl> it = manaAbilities.iterator(); it.hasNext(); ) {
ActivatedManaAbilityImpl ability = it.next();
Abilities<ActivatedManaAbilityImpl> noTapAbilities = new AbilitiesImpl<>(ability);
if (ability.getManaCosts().isEmpty() && !ability.isPoolDependant()) {
sourceWithoutManaCosts.add(noTapAbilities);
} else {
sourceWithCosts.add(noTapAbilities);
}
}
}
for (Permanent permanent : game.getBattlefield().getActivePermanents(playerId, game)) {
// Some permanents allow use of abilities from non controlling players. so check all permanents in range
Boolean canUse = null;
boolean canAdd = false;
// sources with mana costs or mana pool dependency
boolean useLater = false;
Abilities<ActivatedManaAbilityImpl> manaAbilities = // returns ability only if canActivate is true
permanent.getAbilities(game).getAvailableActivatedManaAbilities(Zone.BATTLEFIELD, playerId, game);
for (Iterator<ActivatedManaAbilityImpl> it = manaAbilities.iterator(); it.hasNext(); ) {
ActivatedManaAbilityImpl ability = it.next();
if (canUse == null) {
canUse = permanent.canUseActivatedAbilities(game);
}
if (canUse) {
// abilities without Tap costs have to be handled as separate sources, because they can be used also
if (!ability.hasTapCost()) {
it.remove();
Abilities<ActivatedManaAbilityImpl> noTapAbilities = new AbilitiesImpl<>(ability);
if (ability.getManaCosts().isEmpty() && !ability.isPoolDependant()) {
sourceWithoutManaCosts.add(noTapAbilities);
} else {
sourceWithCosts.add(noTapAbilities);
}
continue;
}
canAdd = true;
if (!ability.getManaCosts().isEmpty() || ability.isPoolDependant()) {
useLater = true;
break;
}
}
}
if (canAdd) {
if (useLater) {
sourceWithCosts.add(manaAbilities);
} else {
sourceWithoutManaCosts.add(manaAbilities);
}
}
}
for (Abilities<ActivatedManaAbilityImpl> manaAbilities : sourceWithoutManaCosts) {
availableMana.addMana(manaAbilities, game);
}
boolean anAbilityWasUsed = true;
// use such abilities later than other if possible because it can maximize mana production
boolean usePoolDependantAbilities = false;
while (anAbilityWasUsed && !sourceWithCosts.isEmpty()) {
anAbilityWasUsed = false;
for (Iterator<Abilities<ActivatedManaAbilityImpl>> iterator = sourceWithCosts.iterator(); iterator.hasNext(); ) {
Abilities<ActivatedManaAbilityImpl> manaAbilities = iterator.next();
if (usePoolDependantAbilities || !manaAbilities.hasPoolDependantAbilities()) {
boolean used;
if (manaAbilities.hasPoolDependantAbilities()) {
used = availableMana.addManaPoolDependant(manaAbilities, game);
} else {
used = availableMana.addManaWithCost(manaAbilities, game);
}
if (used) {
iterator.remove();
availableMana.removeDuplicated();
anAbilityWasUsed = true;
}
}
}
if (!anAbilityWasUsed && !usePoolDependantAbilities) {
usePoolDependantAbilities = true;
anAbilityWasUsed = true;
}
}
// remove duplicated variants (see ManaOptionsTest for info - when that rises)
availableMana.removeDuplicated();
game.setCheckPlayableState(oldState);
return availableMana;
}
Aggregations