-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathCastRandomSpellSpell.java
103 lines (96 loc) · 4.45 KB
/
CastRandomSpellSpell.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package net.demilich.metastone.game.spells;
import net.demilich.metastone.game.GameContext;
import net.demilich.metastone.game.Player;
import net.demilich.metastone.game.cards.Card;
import net.demilich.metastone.game.cards.CardList;
import net.demilich.metastone.game.entities.Entity;
import net.demilich.metastone.game.spells.desc.SpellArg;
import net.demilich.metastone.game.spells.desc.SpellDesc;
import net.demilich.metastone.game.spells.desc.filter.CardFilter;
import net.demilich.metastone.game.spells.desc.filter.SpecificCardFilter;
import net.demilich.metastone.game.targeting.EntityReference;
import net.demilich.metastone.game.cards.Attribute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
/**
* Casts a random spell from the {@link SpellArg#CARD_SOURCE}, {@link SpellArg#CARD_FILTER} and {@link SpellArg#CARDS}
* provided.
* <p>
* If {@link SpellArg#EXCLUSIVE} is {@code true}, the {@code source} does <b>not</b> have to be in play in order for the
* spell to be cast.
* <p>
* The {@code source} of the spell written on the card that is randomly chosen is the <b>entity doing the casting</b>,
* not the card itself.
* <p>
* For example, to implement "Whenever a player casts a spell, cast a copy of it for them with random targets.":
* <pre>
* {
* "eventTrigger": {
* "class": "SpellCastedTrigger",
* "sourcePlayer": "BOTH"
* },
* "spell": {
* "class": "CastRandomSpellSpell",
* "cardFilter": {
* "class": "AndFilter",
* "filters": [
* {
* "class": "SpecificCardFilter",
* "secondaryTarget": "EVENT_SOURCE"
* },
* {
* "class": "CardFilter",
* "cardType": "SPELL"
* }
* ]
* },
* "cardSource": {
* "class": "UncollectibleCatalogueSource"
* },
* "targetPlayer": "ACTIVE"
* }
* }
* </pre>
* Observe that the {@link CardFilter} is a {@link SpecificCardFilter} that is configured, using its {@code
* "secondaryTarget"} option, to only be {@code true} for cards that are equal to {@link EntityReference#EVENT_SOURCE},
* i.e., the card that was casted. Only one card (the card that was casted) will match, so that's the card that will be
* randomly cast.
*
* @see net.demilich.metastone.game.spells.custom.PlayCardsRandomlySpell for a similar spell that works with minions,
* weapons and heroes and uses the card as the source of the effects
*/
public class CastRandomSpellSpell extends Spell {
Logger logger = LoggerFactory.getLogger(CastRandomSpellSpell.class);
public static SpellDesc create(int value) {
Map<SpellArg, Object> arguments = new SpellDesc(CastRandomSpellSpell.class);
arguments.put(SpellArg.VALUE, value);
return new SpellDesc(arguments);
}
@Override
protected void onCast(GameContext context, Player player, SpellDesc desc, Entity source, Entity target) {
checkArguments(logger, context, source, desc, SpellArg.VALUE, SpellArg.CARD, SpellArg.CARDS, SpellArg.CARD_SOURCE, SpellArg.CARD_FILTER);
TargetPlayer castingTargetPlayer = desc.getTargetPlayer() == null ? TargetPlayer.OWNER : desc.getTargetPlayer();
player.modifyAttribute(Attribute.RANDOM_CHOICES, 1);
int numberOfSpellsToCast = desc.getValue(SpellArg.VALUE, context, player, target, source, 1);
CardList spells = SpellUtils.getCards(context, player, target, source, desc, numberOfSpellsToCast);
for (int i = 0; i < numberOfSpellsToCast; i++) {
if (spells.isEmpty()) {
logger.warn("onCast {} {}: An empty number of spells were found with the filter {} and source {}", context.getGameId(), source, desc.getCardFilter(), desc.getCardSource());
break;
}
SpellUtils.DetermineCastingPlayer determineCastingPlayer = SpellUtils.determineCastingPlayer(context, player, source, castingTargetPlayer);
boolean mustBeInPlay = !desc.getBool(SpellArg.EXCLUSIVE);
if (mustBeInPlay && !determineCastingPlayer.isSourceInPlay()) {
break;
}
Player castingPlayer = determineCastingPlayer.getCastingPlayer();
// Must retrieve a copy because castWithRandomTargets mutates the incoming spell card
Card randomCard = context.getLogic().getRandom(spells).getCopy();
logger.debug("onCast {} {}: Casting random spell {}", context.getGameId(), source, randomCard);
RandomCardTargetSpell.castCardWithRandomTargets(context, castingPlayer, source, randomCard);
context.getLogic().endOfSequence();
}
player.modifyAttribute(Attribute.RANDOM_CHOICES, -1);
}
}