Class SpellUtils

java.lang.Object
net.demilich.metastone.game.spells.SpellUtils

public class SpellUtils extends Object
A set of utilities to help write spells.
  • Constructor Details

    • SpellUtils

      public SpellUtils()
  • Method Details

    • castChildSpell

      public static void castChildSpell(GameContext context, Player player, SpellDesc spell, Entity source, Entity target)
      Sets up the source and target references for casting a child spell, typically an "effect" of a spell defined on a card.
      Parameters:
      context - The game context.
      player - The player casting the spell.
      spell - The child spell to cast.
      source - The source of the spell, typically the spell card or minion whose battlecry is being called.
      target - The target reference.
    • playCardRandomly

      public static boolean playCardRandomly(GameContext context, Player player, Card card, Entity source, boolean summonRightmost, boolean resolveOpener, boolean onlyWhileSourceInPlay, boolean randomChooseOnes, boolean playFromHand)
      Plays a card "randomly."

      This will cause it to select random targets if, after target selection modification, it accepts targets.

      Parameters:
      context -
      player -
      card -
      source -
      summonRightmost - When true and Card.isActor(), summons the card in the rightmost position. Otherwise, summons it to a random position.
      resolveOpener - When true, also resolves the battlecry with a random target. Otherwise, the battlecry is ignored.
      onlyWhileSourceInPlay - When true,
      randomChooseOnes - When true, randomly chooses the choose-one effects. Otherwise, checks if the card has had a Attribute.CHOICES made.
      playFromHand - When true, the card is counterable, mana is deducted and played card stats are incremented. Otherwise, only the effects of the card, like putting a minion into play or the underlying spell effects, are executed.
      Returns:
    • evaluateOperation

      public static boolean evaluateOperation(ComparisonOperation operation, int actualValue, int targetValue)
      Given a filter ComparisonOperation, return a boolean representing whether that operation is satisfied.
      Parameters:
      operation - The algebraic operation.
      actualValue - The left hand side.
      targetValue - The right hand side.
      Returns:
      true if the evaluation is truue.
    • getCards

      public static CardList getCards(CardList source, Predicate<Card> filter)
      Filters a card list. Does not copy source cards.
      Parameters:
      source - A CardList source.
      filter - A function that returns true when the card should be kept, or null to include all cards.
      Returns:
      A CardList backed by a mutable, non-copy CardArrayList.
    • getCard

      public static Card getCard(GameContext context, SpellDesc spell)
      Gets a card out of a SpellDesc. Typically only consults the SpellArg.CARD property.
      Parameters:
      context - The context.
      spell - The SpellDesc.
      Returns:
      A card.
    • getCards

      public static Card[] getCards(GameContext context, SpellDesc spell)
      Retrieves the cards specified inside the SpellArg.CARD and SpellArg.CARDS arguments.
      Parameters:
      context - The game context to use for or GameContext.getOutputCard() lookups.
      spell - The spell description to retrieve the cards from.
      Returns:
      A new array of Card entities.
      See Also:
    • getCardFromContextOrDiscover

      public static Card getCardFromContextOrDiscover(GameContext context, String cardId)
      Retrieves a reference to a newly generated card currently in the Spellsource.ZonesMessage.Zones.DISCOVER if the given cardId can be found there.

      This allows spells to be cast on cards while they are in the discover zone, even though the spells casting effects on them are supposed to be executing their effects on base cards.

      Parameters:
      context -
      cardId -
      Returns:
    • discoverCard

      public static DiscoverAction discoverCard(GameContext context, Player player, Entity source, SpellDesc desc, CardList cards)
      Requests that the player chooses from a selection of cards and casts a spell (typically ReceiveCardSpell with that card.

      This method makes a network request if required.

      Parameters:
      context - The game context that hosts the player and state for this request.
      player - The player that will choose from the cards.
      source -
      desc - For every card the player can discover, this method will create a Spell from this SpellDesc and set its SpellArg.CARD argument to the discoverable card. Typically, this SpellDesc defines a ReceiveCardSpell, ReceiveCardAndDoSomethingSpell, or a ChangeHeroPowerSpell. These spells all receive cards as arguments. This argument allows a DiscoverAction to do more sophisticated things than just put cards into hands.
      cards - A CardList of cards that get copied, added to the Spellsource.ZonesMessage.Zones.DISCOVER zone of the player and shown in the discover card UI to the player.
      Returns:
      The DiscoverAction that corresponds to the card the player chose.
      See Also:
    • postDiscover

      public static DiscoverAction postDiscover(GameContext context, Player player, Iterable<? extends Card> cards, List<GameAction> discoverActions)
      Moves the card put into the Spellsource.ZonesMessage.Zones.DISCOVER by a discoverCard(GameContext, Player, Entity, SpellDesc, CardList) action back to where it came from. If it was a newly generated card it is removed from play.
      Parameters:
      context -
      player -
      cards -
      discoverActions -
      Returns:
    • getSpellDiscover

      public static DiscoverAction getSpellDiscover(GameContext context, Player player, SpellDesc desc, List<SpellDesc> spells, Entity source)
      Requests that the player chooses from a selection of cards, then returns just the spell from the cards.

      Removes all the cards from play unless otherwise specified, since these cards aren't actually used.

      Parameters:
      context - The GameContext
      player - The Player
      desc - A SpellDesc to use as the "parent" of the discovered spells. The mana cost and targets are inherited from this spell.
      spells - A list of spells from which to generate virtual cards.
      source - The source entity, typically the Card that initiated this call.
      Returns:
      A DiscoverAction whose DiscoverAction.getCard() property corresponds to the selected card. To retrieve the spell, get the card's spell with Card.getSpell().
    • getValidTargets

      public static List<Entity> getValidTargets(GameContext context, Player player, List<Entity> allTargets, EntityFilter filter, Entity host)
      Filters a list of targets.
      Parameters:
      context -
      player -
      allTargets -
      filter -
      host -
      Returns:
    • getBoardPosition

      public static int getBoardPosition(GameContext context, Player player, SpellDesc desc, Entity source)
      Interprets the SpellArg.BOARD_POSITION_ABSOLUTE or SpellArg.BOARD_POSITION_RELATIVE in a desc given the source entity.
      Parameters:
      context -
      player -
      desc -
      source -
      Returns:
      An index in the source entity's Spellsource.ZonesMessage.Zones zone.
    • castChildSpell

      public static void castChildSpell(GameContext context, Player player, SpellDesc spell, Entity source, Entity target, Entity output)
      Casts a subspell on a card that was returned by GameLogic.receiveCard(int, Card). Will not execute if the output is null or in the Spellsource.ZonesMessage.Zones.GRAVEYARD.
      Parameters:
      context - The GameContext to operate on.
      player - The player from whose point of view we are casting this sub spell. This should be passed down from the Spell.onCast(GameContext, Player, SpellDesc, Entity, Entity) player argument.
      spell - The sub spell, typically from the desc argument's SpellArg.SPELL key.
      source - The source entity.
      target -
      output - The card. When null or the card is located in the Spellsource.ZonesMessage.Zones.GRAVEYARD.
    • getCards

      public static CardList getCards(GameContext context, Player player, Entity target, Entity source, SpellDesc desc)
      Retrieves the cards specified in the SpellDesc, either in the SpellArg.CARD or SpellArg.CARDS properties or as specified by a CardSource and CardFilter. If neither of those are specified, uses the target's Entity.getSourceCard() as the targeted card.

      The number of cards randomly retrieved is equal to the SpellArg.VALUE specified in the desc argument, defaulting to 1.

      Parameters:
      context - The game context
      player - The player from whose point of view these cards should be retrieved
      target - The target, which can be null
      source - The source or host Entity, typically the origin of this spell cast.
      desc - The SpellDesc typically of the calling spell.
      Returns:
      A list of cards.
      See Also:
    • getCards

      public static CardList getCards(GameContext context, Player player, Entity target, Entity source, SpellDesc desc, int count)
      Retrieves the cards specified in the SpellDesc, either in the SpellArg.CARD or SpellArg.CARDS properties or as specified by a CardSource and CardFilter. If neither of those are specified, uses the target's Entity.getSourceCard() as the targeted card.

      The SpellDesc given in desc is inspected for a variety of arguments. If there is a SpellArg.CARD_SOURCE or SpellArg.CARD_FILTER specified, the card source generates a list of cards using CardSource.getCards(GameContext, Entity, Player), and that list is filtered using EntityFilter.matches(GameContext, Player, Entity, Entity).

      Anytime SpellArg.CARD or SpellArg.CARDS is specified, the card IDs in those args are added to the list of cards returned by this method.

      If no arguments are specified, the target entity's Entity.getSourceCard() is added to the list of cards returned by this method. This means that providing a SpellDesc that contains neither SpellArg.CARD, SpellArg.CARDS, SpellArg.CARD_SOURCE nor SpellArg.CARD_FILTER will be interpreted as trying to retrieve the target's base card.

      Cards are not generated as copies unless the CardSource has the HasCardCreationSideEffects trait and is used as an arg in the desc.

      By default, when a SpellArg.CARD_FILTER is specified and a SpellArg.CARD_SOURCE is not, the default card source used is UnweightedCatalogueSource.

      The cards are chosen randomly without replacement.

      Parameters:
      context - The game context
      player - The player from whose point of view these cards should be retrieved
      target - The target, which can be null
      source - The source or host Entity, typically the origin of this spell cast.
      desc - The SpellDesc typically of the calling spell.
      count - The maximum number of cards to return, exclusively and randomly, from the generated card list. Or, returns all the cards if count > cards.size(), where cards is all the possible cards.
      Returns:
      A list of cards.
    • hasAura

      public static <T extends Aura> boolean hasAura(GameContext context, int playerId, Class<T> auraClass)
      Determines whether any of the Entity.isInPlay() entities belonging to the playerId host an unexpired, active instance of the auraClass aura.
      Type Parameters:
      T -
      Parameters:
      context -
      playerId -
      auraClass -
      Returns:
      true if such an aura is found.
      See Also:
    • getAuras

      public static <T extends Aura> List<T> getAuras(GameContext context, int playerId, @NotNull @NotNull Class<T> auraClass)
      Retrieves all of the unexpired, active auras that are instances of the auraClass hosted by Entity.isInPlay() entities belonging to the playerId or passive auras hosted by hero powers and cards.
      Type Parameters:
      T -
      Parameters:
      context -
      playerId -
      auraClass -
      Returns:
      A list of aura instances.
    • getAuras

      public static <T extends Aura> List<T> getAuras(GameContext context, Class<T> auraClass, Entity target)
      Get the auras that are affecting the specified target of the given class.
      Type Parameters:
      T -
      Parameters:
      context -
      auraClass -
      target -
      Returns:
    • getBonusesFromAura

      public static SpellDesc[] getBonusesFromAura(GameContext context, int playerId, Class<? extends Aura> auraClass, Entity source, Entity target)
      Retrieves an array of spells corresponding to the AuraArg.APPLY_EFFECT field on an aura whose condition is null or fulfilled for the given source and target.
      Parameters:
      context -
      playerId -
      auraClass -
      source -
      target -
      Returns:
      A list of spells
    • determineCastingPlayer

      public static SpellUtils.DetermineCastingPlayer determineCastingPlayer(GameContext context, Player player, Entity source, TargetPlayer castingTargetPlayer)
      Tries to determine the currently casting player from the point of view of a source, considering if the source has changed owners or if it was destroyed.
      Parameters:
      context - The game context
      player - The casting player
      source - The source from whose point of view the casting player should be determined
      castingTargetPlayer - Whose point of view the determination should be made. For example, if TargetPlayer.OPPONENT is chosen here, then the opponent of the owner of the source will be used.
      Returns:
      An object containing information related to who is the casting player and whether or not the source has been destroyed.
    • isRecursive

      public static boolean isRecursive(Class<? extends Spell> callingClass)
      Returns true if the caller is in a recursive stack
      Parameters:
      callingClass -
    • getSpecialCards

      public static Set<String> getSpecialCards()
      Gets a list of special card IDs.
      Returns: