public boolean apply(Game game, Ability source) {
Card card = (Card) game.getObject(source.getSourceId());
if (card != null) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
SpellAbility spellAbility;
switch(((FlashbackAbility) source).getSpellAbilityType()) {
case SPLIT_LEFT:
spellAbility = ((SplitCard)card).getLeftHalfCard().getSpellAbility();
break;
case SPLIT_RIGHT:
spellAbility = ((SplitCard)card).getRightHalfCard().getSpellAbility();
break;
default:
spellAbility = card.getSpellAbility();
}
spellAbility.clear();
// used if flashbacked spell has a {X} cost
int amount = source.getManaCostsToPay().getX();
if (amount == 0) {
// add variable cost like Discard X cards to get the X value to the spell
// because there is currently no way to set the x value in anotehr way, it's set for the
// x mana value to be known by the spell
for (Cost cost:source.getCosts()) {
if (cost instanceof VariableCost && cost.isPaid()) {
amount = ((VariableCost) cost).getAmount();
break;
}
}
}
if (amount > 0) {
// multiplier must be taken into account because if the base spell has {X}{X} the x value would be wrongly halfed
for (VariableCost variableCost: spellAbility.getManaCostsToPay().getVariableCosts()) {
if (variableCost instanceof VariableManaCost) {
amount = amount * ((VariableManaCost)variableCost).getMultiplier();
break;
}
}
spellAbility.getManaCostsToPay().setX(amount);
}
game.informPlayers(new StringBuilder(controller.getName()).append(" flashbacks ").append(card.getName()).toString());
spellAbility.setCostModificationActive(false); // prevents to apply cost modification twice for flashbacked spells
return controller.cast(spellAbility, game, true);
}
}
return false;
}