public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
return false;
}
Cards cards = new CardsImpl();
Object obj = getValue(CastSourceTriggeredAbility.SOURCE_CAST_SPELL_ABILITY);
int count = 0;
if (obj != null && obj instanceof SpellAbility) {
count = ((SpellAbility) obj).getManaCostsToPay().getX();
// using other var because of tooltip
int size = Math.min(controller.getLibrary().size(), count);
for (int i = 0; i < size; i++) {
Card card = controller.getLibrary().removeFromTop(game);
cards.add(card);
}
}
if (cards.size() > 0) {
controller.revealCards("Genesis Hydra", cards, game);
}
FilterCard filter = new FilterPermanentCard("a nonland permanent card with converted mana cost " + count + " or less to put onto the battlefield");
filter.add(Predicates.not(new CardTypePredicate(CardType.LAND)));
filter.add(new ConvertedManaCostPredicate(ComparisonType.LessThan, count + 1));
TargetCard target1 = new TargetCard(Zone.LIBRARY, filter);
target1.setRequired(false);
if (cards.count(filter, controller.getId(), source.getSourceId(), game) > 0) {
if (controller.choose(Outcome.PutCardInPlay, cards, target1, game)) {
Card card = cards.get(target1.getFirstTarget(), game);
if (card != null) {
cards.remove(card);
controller.putOntoBattlefieldWithInfo(card, game, Zone.LIBRARY, source.getSourceId());
}
target1.clearChosen();
} else {
game.informPlayers(controller.getName() + " didn't choose anything");
}
} else {
game.informPlayers("No nonland permanent card with converted mana cost " + count + " or less to choose.");
}
while (cards.size() > 0) {
Card card = cards.get(cards.iterator().next(), game);
cards.remove(card);
card.moveToZone(Zone.LIBRARY, source.getSourceId(), game, true);
}
controller.shuffleLibrary(game);
return true;
}