-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathCastSpellWithTargetOrElseSpell.java
84 lines (71 loc) · 3.34 KB
/
CastSpellWithTargetOrElseSpell.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
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.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.trigger.EnchantmentDesc;
import net.demilich.metastone.game.targeting.TargetSelection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* Tries to cast the spell card (given either by {@link SpellArg#CARD} or {@link SpellArg#SECONDARY_TARGET}) onto the
* specified target. If that is unable to happen, then the {@link SpellArg#SPELL} is cast instead, with the card's id
* passed down into the {@link SpellArg#CARD} attribute.
* <p>
* If a {@link SpellArg#TRIGGER} is specified, then it is used to delay the casting of the spell until that event
* occurs.
* <p>
* See Finale Architect
*/
public class CastSpellWithTargetOrElseSpell extends Spell {
private static Logger LOGGER = LoggerFactory.getLogger(CastSpellWithTargetOrElseSpell.class);
@Override
protected void onCast(GameContext context, Player player, SpellDesc desc, Entity source, Entity target) {
checkArguments(LOGGER, context, source, desc, SpellArg.TARGET, SpellArg.SECONDARY_TARGET, SpellArg.CARD, SpellArg.TRIGGER, SpellArg.SPELL);
Card card;
if (desc.containsKey(SpellArg.TARGET) && target instanceof Card) {
card = (Card) target;
} else {
card = SpellUtils.getCard(context, desc);
}
Entity secondaryTarget = context.resolveSingleTarget(player, source, desc.getSecondaryTarget());
if (!card.isSpell()) {
LOGGER.error("onCast {} {}: Card unexpectedly was not a spell, was {}", context.getGameId(), source, card);
}
if (card.getSpell() == null) {
LOGGER.error("onCast {} {}: Spell unexpectedly null, card was {}, target was {}", context.getGameId(), source, card, target);
return;
}
SpellDesc orElse = desc.getSpell();
EnchantmentDesc trigger = (EnchantmentDesc) desc.get(SpellArg.TRIGGER);
if (trigger != null) {
SpellDesc thisButLater = desc.removeArg(SpellArg.TRIGGER);
if (card.getTargetSelection() != TargetSelection.NONE && target != null) {
thisButLater.put(SpellArg.SECONDARY_TARGET, target.getReference());
}
thisButLater.put(SpellArg.TARGET, card.getReference());
// sometimes the card that this is cast on is from the catalogue, so we'll write down the card ID too
thisButLater.put(SpellArg.CARD, card.getCardId());
trigger.setSpell(thisButLater);
SpellDesc addEnchantmentSpellDesc = AddEnchantmentSpell.create(trigger);
SpellUtils.castChildSpell(context, player, addEnchantmentSpellDesc, source, player);
} else {
if (secondaryTarget == null) {
SpellUtils.castChildSpell(context, player, card.getSpell(), source, null);
context.getLogic().revealCard(player, card);
} else {
List<Entity> targets = context.getTargetLogic().getValidTargets(context, player, card.play());
if (targets.contains(secondaryTarget)) {
SpellUtils.castChildSpell(context, player, card.getSpell(), source, target);
context.getLogic().revealCard(player, card);
} else {
orElse.put(SpellArg.CARD, card.getCardId());
SpellUtils.castChildSpell(context, player, orElse, source, null);
}
}
}
}
}