use of mage.abilities.ActivatedAbility in project mage by magefree.
the class QuickSilverElementalBlueManaEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Permanent permanent = game.getPermanent(source.getSourceId());
Permanent creature = game.getPermanent(source.getTargets().getFirstTarget());
if (permanent != null && creature != null) {
for (ActivatedAbility ability : creature.getAbilities().getActivatedAbilities(Zone.BATTLEFIELD)) {
Ability newAbility = ability.copy();
newAbility.newOriginalId();
game.addEffect(new GainAbilitySourceEffect(newAbility, Duration.EndOfTurn), source);
}
return true;
}
return false;
}
use of mage.abilities.ActivatedAbility in project mage by magefree.
the class MairsilThePretenderGainAbilitiesEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Permanent perm = game.getPermanent(source.getSourceId());
if (perm == null) {
return false;
}
for (Card card : game.getExile().getAllCards(game)) {
if (filter.match(card, game) && Objects.equals(card.getOwnerId(), perm.getControllerId())) {
for (Ability ability : card.getAbilities(game)) {
if (ability instanceof ActivatedAbility) {
ActivatedAbility copyAbility = (ActivatedAbility) ability.copy();
copyAbility.setMaxActivationsPerTurn(1);
perm.addAbility(copyAbility, source.getSourceId(), game);
}
}
}
}
return true;
}
use of mage.abilities.ActivatedAbility in project mage by magefree.
the class DrainPowerEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Player targetPlayer = game.getPlayer(source.getFirstTarget());
if (targetPlayer != null) {
List<Permanent> ignorePermanents = new ArrayList<>();
Map<Permanent, List<ActivatedManaAbilityImpl>> manaAbilitiesMap = new HashMap<>();
TargetPermanent target = null;
while (true) {
targetPlayer.setPayManaMode(true);
manaAbilitiesMap.clear();
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(filter, targetPlayer.getId(), game)) {
if (!ignorePermanents.contains(permanent)) {
List<ActivatedManaAbilityImpl> manaAbilities = new ArrayList<>();
abilitySearch: for (Ability ability : permanent.getAbilities()) {
if (ability instanceof ActivatedAbility && ability.getAbilityType() == AbilityType.MANA) {
ActivatedManaAbilityImpl manaAbility = (ActivatedManaAbilityImpl) ability;
if (manaAbility.canActivate(targetPlayer.getId(), game).canActivate()) {
// so it's necessary to filter them out manually - might be buggy in some fringe cases
for (ManaCost manaCost : manaAbility.getManaCosts()) {
if (!targetPlayer.getManaPool().getMana().includesMana(manaCost.getMana())) {
continue abilitySearch;
}
}
manaAbilities.add(manaAbility);
}
}
}
if (!manaAbilities.isEmpty()) {
manaAbilitiesMap.put(permanent, manaAbilities);
}
}
}
if (manaAbilitiesMap.isEmpty()) {
break;
}
List<Permanent> permList = new ArrayList<>(manaAbilitiesMap.keySet());
Permanent permanent;
if (permList.size() > 1 || target != null) {
FilterLandPermanent filter2 = new FilterLandPermanent("land you control to tap for mana (remaining: " + permList.size() + ')');
filter2.add(new PermanentInListPredicate(permList));
target = new TargetPermanent(1, 1, filter2, true);
while (!target.isChosen() && target.canChoose(source.getSourceId(), targetPlayer.getId(), game) && targetPlayer.canRespond()) {
targetPlayer.chooseTarget(Outcome.Neutral, target, source, game);
}
permanent = game.getPermanent(target.getFirstTarget());
} else {
permanent = permList.get(0);
}
if (permanent != null) {
int i = 0;
for (ActivatedManaAbilityImpl manaAbility : manaAbilitiesMap.get(permanent)) {
i++;
if (manaAbilitiesMap.get(permanent).size() <= i || targetPlayer.chooseUse(Outcome.Neutral, "Activate mana ability \"" + manaAbility.getRule() + "\" of " + permanent.getLogName() + "? (Choose \"no\" to activate next mana ability)", source, game)) {
boolean originalCanUndo = manaAbility.isUndoPossible();
// prevents being able to undo Drain Power
manaAbility.setUndoPossible(false);
if (targetPlayer.activateAbility(manaAbility, game)) {
ignorePermanents.add(permanent);
}
// resets undoPossible to its original state
manaAbility.setUndoPossible(originalCanUndo);
break;
}
}
}
}
targetPlayer.setPayManaMode(false);
// 106.12. One card (Drain Power) causes one player to lose unspent mana and another to add “the mana lost this way.” (Note that these may be the same player.)
// This empties the former player's mana pool and causes the mana emptied this way to be put into the latter player's mana pool. Which permanents, spells, and/or
// abilities produced that mana are unchanged, as are any restrictions or additional effects associated with any of that mana.
List<ManaPoolItem> manaItems = targetPlayer.getManaPool().getManaItems();
targetPlayer.getManaPool().emptyPool(game);
for (ManaPoolItem manaPoolItem : manaItems) {
controller.getManaPool().addMana(manaPoolItem.isConditional() ? manaPoolItem.getConditionalMana() : manaPoolItem.getMana(), game, source, Duration.EndOfTurn.equals(manaPoolItem.getDuration()));
}
return true;
}
return false;
}
use of mage.abilities.ActivatedAbility in project mage by magefree.
the class PlayableObjectRecord method load.
public void load(List<ActivatedAbility> activatedAbilities) {
this.basicManaAbilities.clear();
this.basicPlayAbilities.clear();
this.basicCastAbilities.clear();
this.other.clear();
// split abilities to types (it allows to enable or disable playable icon)
for (ActivatedAbility ability : activatedAbilities) {
List<PlayableObjectRecord> dest;
if (ability instanceof BasicManaAbility) {
dest = this.basicManaAbilities;
} else if (ability instanceof PlayLandAbility) {
dest = this.basicPlayAbilities;
} else if (ability instanceof SpellAbility && (((SpellAbility) ability).getSpellAbilityType() == SpellAbilityType.BASE)) {
dest = this.basicCastAbilities;
} else {
dest = this.other;
}
// collect info about abilities for card icons popup, must be simple online text (html symbols are possible)
// some long html tags can be miss (example: ability extra hint) -- that's ok
String shortInfo = ability.toString();
if (shortInfo.length() > 50) {
shortInfo = shortInfo.substring(0, 50 - 1) + "...";
}
shortInfo = shortInfo.replace("<br>", " ");
shortInfo = shortInfo.replace("\n", " ");
dest.add(new PlayableObjectRecord(ability.getId(), shortInfo));
}
}
use of mage.abilities.ActivatedAbility in project mage by magefree.
the class ComputerPlayerMCTS method priority.
@Override
public boolean priority(Game game) {
if (game.getStep().getType() == PhaseStep.UPKEEP) {
if (!lastPhase.equals(game.getTurn().getValue(game.getTurnNum()))) {
logList(game.getTurn().getValue(game.getTurnNum()) + name + " hand: ", new ArrayList(hand.getCards(game)));
lastPhase = game.getTurn().getValue(game.getTurnNum());
if (MCTSNode.USE_ACTION_CACHE) {
int count = MCTSNode.cleanupCache(game.getTurnNum());
if (count > 0)
logger.info("Removed " + count + " cache entries");
}
}
}
game.getState().setPriorityPlayerId(playerId);
game.firePriorityEvent(playerId);
getNextAction(game, NextAction.PRIORITY);
Ability ability = root.getAction();
if (ability == null)
logger.fatal("null ability");
activateAbility((ActivatedAbility) ability, game);
if (ability instanceof PassAbility)
return false;
logLife(game);
logger.info("choose action:" + root.getAction() + " success ratio: " + root.getWinRatio());
return true;
}
Aggregations