foul-magics/Mage/src/main/java/mage/abilities/keyword/SurgeAbility.java
Alex Vasile a2162ec3e7
Refactor: private fields and performance tweaks (#9625)
1a. Make `costs`, `manaCosts`, and `manaCostsToPay` private in `AbilityImpl` with access through getters/setters 
1b. fix cost adjuster for imprinted cards affected by the above

2a. Lazy instantiation for rarely used `data` field in `TargetPointerImpl`

3a. Pre-allocate certain array sizes in `Modes` and `CostsImpl`

4a. Make `manaTemplate` private in `BasicManaEffect`, copy when passing outside the class 
4b. Don't copy `manaTemplate` in copy constructor since it doesn't change 
4c. Add comments explaining copy usage for `manaTemplate` 
4d. Remove redundant variable assignment and make fields final 

---------

Co-authored-by: xenohedron <xenohedron@users.noreply.github.com>
2023-08-27 17:58:19 -04:00

96 lines
3.1 KiB
Java

package mage.abilities.keyword;
import mage.abilities.SpellAbility;
import mage.abilities.costs.mana.ManaCostsImpl;
import mage.cards.Card;
import mage.constants.SpellAbilityType;
import mage.constants.Zone;
import mage.game.Game;
import mage.players.Player;
import mage.watchers.common.CastSpellLastTurnWatcher;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* @author LevelX2
*/
public class SurgeAbility extends SpellAbility {
public static final String SURGE_ACTIVATION_VALUE_KEY = "surgeActivation";
private String rule;
public SurgeAbility(Card card, String surgeCosts) {
super(card.getSpellAbility());
this.newId();
this.setCardName(card.getName() + " with surge");
zone = Zone.HAND;
spellAbilityType = SpellAbilityType.BASE_ALTERNATE;
this.clearManaCosts();
this.clearManaCostsToPay();
this.addManaCost(new ManaCostsImpl<>(surgeCosts));
this.setRuleAtTheTop(true);
this.rule = "Surge " + surgeCosts
+ " <i>(You may cast this spell for its surge cost if you or a teammate has cast another spell this turn.)</i>";
}
protected SurgeAbility(final SurgeAbility ability) {
super(ability);
this.rule = ability.rule;
}
@Override
public ActivationStatus canActivate(UUID playerId, Game game) {
// check if controller or teammate has already cast a spell this turn
CastSpellLastTurnWatcher watcher = game.getState().getWatcher(CastSpellLastTurnWatcher.class);
if (watcher != null) {
Player player = game.getPlayer(playerId);
if (player != null) {
for (UUID playerToCheckId : game.getState().getPlayersInRange(playerId, game)) {
if (!player.hasOpponent(playerToCheckId, game)) {
if (watcher.getAmountOfSpellsPlayerCastOnCurrentTurn(playerToCheckId) > 0
&& super.canActivate(playerId, game).canActivate()) {
return ActivationStatus.getTrue(this, game);
}
}
}
}
}
return ActivationStatus.getFalse();
}
@Override
@SuppressWarnings("unchecked")
public boolean activate(Game game, boolean noMana) {
if (super.activate(game, noMana)) {
List<Integer> surgeActivations = (ArrayList) game.getState().getValue(SURGE_ACTIVATION_VALUE_KEY + getSourceId());
if (surgeActivations == null) {
surgeActivations = new ArrayList<>(); // zoneChangeCounter
game.getState().setValue(SURGE_ACTIVATION_VALUE_KEY + getSourceId(), surgeActivations);
}
surgeActivations.add(game.getState().getZoneChangeCounter(getSourceId()));
return true;
}
return false;
}
@Override
public SurgeAbility copy() {
return new SurgeAbility(this);
}
@Override
public String getRule(boolean all) {
return getRule();
}
@Override
public String getRule() {
return rule;
}
}