-
Notifications
You must be signed in to change notification settings - Fork 150
/
actions.py
743 lines (592 loc) · 17 KB
/
actions.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
import logging
from enum import IntEnum
from .dsl import LazyNum, Picker, Selector
from .enums import CardType, Mulligan, PowSubType, Zone
from .entity import Entity
def _eval_card(source, card):
"""
Return a Card instance from \a card
The card argument can be:
- A Card instance (nothing is done)
- The string ID of the card (the card is created)
- A Picker instance (a card is dynamically picked)
"""
if isinstance(card, Picker):
card = card.pick(source)
if not isinstance(card, list):
cards = [card]
else:
cards = card
ret = []
for card in cards:
if isinstance(card, str):
ret.append(source.controller.card(card, source))
else:
ret.append(card)
return ret
class EventListener:
ON = 1
AFTER = 2
def __init__(self, trigger, actions, at):
self.trigger = trigger
self.actions = actions
self.at = at
self.once = False
def __repr__(self):
return "<EventListener %r>" % (self.trigger)
class Action: # Lawsuit
type = PowSubType.TRIGGER
class Args(IntEnum):
"""
Arguments given to Actions.
Works like an IntEnum, with the value representing the argument index.
"""
pass
def __init__(self, *args, **kwargs):
self._args = args
self._argnames = []
for e, arg in zip(self.Args, self._args):
self._argnames.append(e.name)
def __repr__(self):
args = ["%s=%r" % (k, v) for k, v in zip(self._argnames, self._args)]
return "<Action: %s(%s)>" % (self.__class__.__name__, ", ".join(args))
def after(self, *actions):
return EventListener(self, actions, EventListener.AFTER)
def on(self, *actions):
return EventListener(self, actions, EventListener.ON)
def _broadcast(self, entity, source, at, *args):
for event in entity.events:
if event.at != at:
continue
if isinstance(event.trigger, self.__class__) and event.trigger.matches(entity, args):
logging.info("%r triggers off %r from %r", entity, self, source)
entity.trigger_event(source, event, args)
def broadcast(self, source, at, *args):
for entity in source.game.entities:
self._broadcast(entity, source, at, *args)
for entity in source.game.hands:
self._broadcast(entity, source, at, *args)
def get_args(self, source):
return self._args
def matches(self, source, args):
for arg, match in zip(args, self._args):
if match is None:
# Allow matching Action(None, None, z) to Action(x, y, z)
continue
# this stuff is stupidslow
res = match.eval([arg], source)
if not res or res[0] is not arg:
return False
return True
class GameAction(Action):
def trigger(self, source):
args = self.get_args(source)
source.game.manager.action(self.type, source, *args)
self.do(source, *args)
source.game.manager.action_end(self.type, source, *args)
source.game.process_deaths()
class Attack(GameAction):
"""
Make \a ATTACKER attack \a DEFENDER
"""
class Args(Action.Args):
ATTACKER = 0
DEFENDER = 1
type = PowSubType.ATTACK
def get_args(self, source):
ret = super().get_args(source)
return ret
def do(self, source, attacker, defender):
attacker.attacking = True
defender.defending = True
source.game.proposed_attacker = attacker
source.game.proposed_defender = defender
logging.info("%r attacks %r", attacker, defender)
self.broadcast(source, EventListener.ON, attacker, defender)
source.game._attack()
class BeginTurn(GameAction):
"""
Make \a player begin the turn
"""
class Args(Action.Args):
PLAYER = 0
type = None
def do(self, source, player):
self.broadcast(source, EventListener.ON, player)
source.game._begin_turn(player)
class Deaths(GameAction):
"""
Process all deaths in the PLAY Zone.
"""
def do(self, source, *args):
source.game.process_deaths()
class Death(GameAction):
"""
Move target to the GRAVEYARD Zone.
"""
class Args(Action.Args):
ENTITY = 0
def do(self, source, target):
logging.info("Processing Death for %r", target)
self.broadcast(source, EventListener.ON, target)
if target.deathrattles:
source.game.queue_actions(source, [Deathrattle(target)])
class EndTurn(GameAction):
"""
End the current turn
"""
class Args(Action.Args):
PLAYER = 0
type = None
def do(self, source, player):
assert not player.choice, "Attempted to end a turn with a choice open"
self.broadcast(source, EventListener.ON, player)
source.game._end_turn()
class MulliganChoice(GameAction):
class Args(Action.Args):
PLAYER = 0
type = None
def do(self, source, player):
player.mulligan_state = Mulligan.INPUT
player.choice = self
# NOTE: Ideally, we give The Coin when the Mulligan is over.
# Unfortunately, that's not compatible with Blizzard's way.
self.cards = player.hand.exclude(id="GAME_005")
self.player = player
def choose(self, *cards):
self.player.draw(len(cards))
for card in cards:
assert card in self.cards
card.zone = Zone.DECK
self.player.choice = None
self.player.shuffle_deck()
self.player.mulligan_state = Mulligan.DONE
class Play(GameAction):
"""
Make the source player play \a card, on \a target or None.
Choose play action from \a choose or None.
"""
class Args(Action.Args):
PLAYER = 0
CARD = 1
TARGET = 2
CHOOSE = 3
type = PowSubType.PLAY
def _broadcast(self, entity, source, at, *args):
# Prevent cards from triggering off their own play
if entity is args[1]:
return
return super()._broadcast(entity, source, at, *args)
def get_args(self, source):
return (source, ) + super().get_args(source)
def do(self, source, player, card, target=None, choose=None):
card.target = target
if choose is not None:
# Choose One cards replace the action on the played card
chosen = player.card(choose)
logging.info("Choose One from %r: %r", card, chosen)
if chosen.has_target():
chosen.target = target
card.chosen = chosen
card.choose = choose
player.game.no_aura_refresh = True
player.game._play(card)
# NOTE: A Play is not a summon! But it sure looks like one.
# We need to fake a Summon broadcast.
summon_action = Summon(player, card)
self.broadcast(player, EventListener.ON, player, card, target, choose)
summon_action.broadcast(player, EventListener.ON, player, card)
player.game.no_aura_refresh = False
card.action()
summon_action.broadcast(player, EventListener.AFTER, player, card)
self.broadcast(player, EventListener.AFTER, player, card, target, choose)
player.combo = True
player.cards_played_this_turn += 1
if card.type == CardType.MINION:
player.minions_played_this_turn += 1
card.target = None
card.choose = None
class TargetedAction(Action):
class Args(Action.Args):
TARGETS = 0
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.times = 1
def __repr__(self):
args = ["%s=%r" % (k, v) for k, v in zip(self._argnames[1:], self._args[1:])]
return "<TargetedAction: %s(%s)>" % (self.__class__.__name__, ", ".join(args))
def __mul__(self, value):
self.times = value
return self
def eval(self, selector, source):
if isinstance(selector, Entity):
return [selector]
else:
return selector.eval(source.game, source)
def get_target_args(self, source, target):
ret = []
for k, v in zip(self.Args, self._args):
if k.name == "TARGETS":
continue
elif isinstance(v, Selector):
# evaluate Selector arguments
v = v.eval(source.game, source)
elif isinstance(v, Action.Args):
# This is used when an event listener triggers and the callback
# Action has arguments of the type Action.Args.FOO
# XXX we rely on source.event_args to be set, but it's very racey.
# If multiple events happen on an entity at once, stuff will go wrong.
assert source.event_args
v = source.event_args[v]
elif isinstance(v, LazyNum):
# evaluate LazyNum arguments into ints
v = v.evaluate(source)
elif k.name == "CARDS":
# HACK: card-likes are always named Args.CARDS
v = _eval_card(source, v)
ret.append(v)
return ret
def get_targets(self, source, t):
if isinstance(t, Entity):
return [t]
elif isinstance(t, Action.Args):
return [source.event_args[t]]
elif isinstance(t, Action):
# eg. Unstable Portal: Buff(Give(...), ...)
return t.trigger(source)[0]
else:
return t.eval(source.game, source)
def trigger(self, source):
ret = []
times = self.times
if isinstance(times, LazyNum):
times = times.evaluate(source)
for i in range(times):
args = self.get_args(source)
targets = self.get_targets(source, args[0])
args = args[1:]
source.game.manager.action(self.type, source, targets, *args)
logging.info("%r triggering %r targeting %r", source, self, targets)
for target in targets:
target_args = self.get_target_args(source, target)
ret.append(self.do(source, target, *target_args))
source.game.manager.action_end(self.type, source, targets, *self._args)
return ret
class Buff(TargetedAction):
"""
Buff character targets with Enchantment \a id
"""
class Args(Action.Args):
TARGETS = 0
BUFF = 1
def do(self, source, target, buff):
source.buff(target, buff)
class Bounce(TargetedAction):
"""
Bounce minion targets on the field back into the hand.
"""
def do(self, source, target):
target.bounce()
class Counter(TargetedAction):
"""
Counter a card, making it unplayable.
"""
def do(self, source, target):
target.cant_play = True
class Damage(TargetedAction):
"""
Damage target by \a amount.
"""
class Args(Action.Args):
TARGETS = 0
AMOUNT = 1
def do(self, source, target, amount):
amount = target._hit(source, amount)
if amount:
self.broadcast(source, EventListener.ON, target, amount, source)
class Deathrattle(TargetedAction):
"""
Trigger deathrattles on card targets.
"""
def do(self, source, target):
for deathrattle in target.deathrattles:
if callable(deathrattle):
actions = deathrattle(target)
else:
actions = deathrattle
source.game.queue_actions(target, actions)
if target.controller.extra_deathrattles:
logging.info("Triggering deathrattles for %r again", target)
source.game.queue_actions(target, actions)
class Destroy(TargetedAction):
"""
Destroy character targets.
"""
def do(self, source, target):
target._destroy()
class Discard(TargetedAction):
"""
Discard card targets in a player's hand
"""
def do(self, source, target):
self.broadcast(source, EventListener.ON, target)
target.discard()
class Draw(TargetedAction):
"""
Make player targets draw a card from their deck.
"""
class Args(Action.Args):
TARGETS = 0
CARD = 1
def do(self, source, target):
if not target.deck:
target.fatigue()
return []
card = target.deck[-1]
card.draw()
self.broadcast(source, EventListener.ON, target, card, source)
return [card]
class ForceDraw(TargetedAction):
"""
Draw card targets into their owners hand
"""
def do(self, source, target):
target.draw()
class FullHeal(TargetedAction):
"""
Fully heal character targets.
"""
def do(self, source, target):
source.heal(target, target.max_health)
class GainArmor(TargetedAction):
"""
Make hero targets gain \a amount armor.
"""
class Args(Action.Args):
TARGETS = 0
AMOUNT = 1
def do(self, source, target, amount):
target.armor += amount
self.broadcast(source, EventListener.ON, target, amount)
class GainMana(TargetedAction):
"""
Give player targets \a Mana crystals.
"""
class Args(Action.Args):
TARGETS = 0
AMOUNT = 1
def do(self, source, target, amount):
target.max_mana += amount
class Give(TargetedAction):
"""
Give player targets card \a id.
"""
class Args(Action.Args):
TARGETS = 0
CARDS = 1
def do(self, source, target, cards):
logging.debug("Giving %r to %s", cards, target)
for card in cards:
card.controller = target
card.zone = Zone.HAND
return cards
class Hit(TargetedAction):
"""
Hit character targets by \a amount.
"""
class Args(Action.Args):
TARGETS = 0
AMOUNT = 1
SOURCE = 2
def do(self, source, target, amount, attack_source=None):
if attack_source is None:
# Actions can trigger a hit from a specific source (eg. Betrayal)
attack_source = source
attack_source.hit(target, amount)
class Heal(TargetedAction):
"""
Heal character targets by \a amount.
"""
class Args(Action.Args):
TARGETS = 0
AMOUNT = 1
def do(self, source, target, amount):
if source.controller.outgoing_healing_adjustment:
# "healing as damage" (hack-ish)
return source.hit(target, amount)
amount *= (source.controller.healing_double + 1)
amount = min(amount, target.damage)
if amount:
# Undamaged targets do not receive heals
logging.info("%r heals %r for %i", source, target, amount)
target.damage -= amount
self.broadcast(source, EventListener.ON, target, amount)
class ManaThisTurn(TargetedAction):
"""
Give player targets \a amount Mana this turn.
"""
class Args(Action.Args):
TARGETS = 0
AMOUNT = 1
def do(self, source, target, amount):
target.temp_mana += min(target.max_resources - target.mana, amount)
class Mill(TargetedAction):
"""
Mill \a count cards from the top of the player targets' deck.
"""
class Args(Action.Args):
TARGETS = 0
COUNT = 1
def do(self, source, target, count):
target.mill(count)
class Morph(TargetedAction):
"""
Morph minion target into \a minion id
"""
class Args(Action.Args):
TARGETS = 0
CARD = 1
def get_target_args(self, source, target):
card = _eval_card(source, self._args[1])
assert len(card) == 1
card = card[0]
card.controller = target.controller
return (card, )
def do(self, source, target, card):
logging.info("Morphing %r into %r", target, card)
target.clear_buffs()
target.zone = Zone.SETASIDE
card.zone = Zone.PLAY
return card
class Freeze(TargetedAction):
"""
Freeze character targets.
"""
def do(self, source, target):
target.frozen = True
class FillMana(TargetedAction):
"""
Refill \a amount mana crystals from player targets.
"""
class Args(Action.Args):
TARGETS = 0
AMOUNT = 1
def do(self, source, target, amount):
target.used_mana -= amount
class Reveal(TargetedAction):
"""
Reveal secret targets.
"""
def do(self, source, target):
logging.info("Revealing secret %r", target)
self.broadcast(source, EventListener.ON, target)
target.zone = Zone.GRAVEYARD
class SetCurrentHealth(TargetedAction):
"""
Sets the current health of the character target to \a amount.
"""
class Args(Action.Args):
TARGETS = 0
AMOUNT = 1
def do(self, source, target, amount):
logging.info("Setting current health on %r to %i", target, amount)
maxhp = target.max_health
target.damage = max(0, maxhp - amount)
class SetTag(TargetedAction):
"""
Sets various targets' tags to \a values.
"""
class Args(Action.Args):
TARGETS = 0
VALUES = 1
def do(self, source, target, values):
for k, v in values.items():
if target.tags[k] != v:
target.tags[k] = v
class Silence(TargetedAction):
"""
Silence minion targets.
"""
def do(self, source, target):
target.silence()
class Summon(TargetedAction):
"""
Make player targets summon \a id onto their field.
This works for equipping weapons as well as summoning minions.
"""
class Args(Action.Args):
TARGETS = 0
CARDS = 1
def _broadcast(self, entity, source, at, *args):
# Prevent cards from triggering off their own summon
if entity is args[1]:
return
return super()._broadcast(entity, source, at, *args)
def do(self, source, target, cards):
logging.info("%s summons %r", target, cards)
if not isinstance(cards, list):
cards = [cards]
for card in cards:
if card.controller != target:
card.controller = target
if card.type == CardType.MINION and not target.minion_slots:
continue
self.broadcast(source, EventListener.ON, target, card)
if card.zone != Zone.PLAY:
card.zone = Zone.PLAY
self.broadcast(source, EventListener.AFTER, target, card)
return cards
class Shuffle(TargetedAction):
"""
Shuffle card targets into player target's deck.
"""
class Args(Action.Args):
TARGETS = 0
CARDS = 1
def do(self, source, target, cards):
logging.info("%r shuffles into %s's deck", cards, target)
if not isinstance(cards, list):
cards = [cards]
for card in cards:
if card.controller != target:
card.controller = target
card.zone = Zone.DECK
target.shuffle_deck()
class Swap(TargetedAction):
"""
Swap minion target with \a other.
Behaviour is undefined when swapping more than two minions.
"""
class Args(Action.Args):
TARGETS = 0
OTHER = 1
def get_target_args(self, source, target):
other = self.eval(self._args[1], source)
if not other:
return (None, )
assert len(other) == 1
return (other[0], )
def do(self, source, target, other):
if other is not None:
orig = target.zone
target.zone = other.zone
other.zone = orig
class Steal(TargetedAction):
"""
Make the controller take control of targets.
The controller is the controller of the source of the action.
"""
def do(self, source, target):
logging.info("%s takes control of %r", self, target)
zone = target.zone
target.zone = Zone.SETASIDE
target.controller = source.controller
target.zone = zone
class UnlockOverload(TargetedAction):
"""
Unlock the target player's overload, both current and owed.
"""
def do(self, source, target):
logging.info("%s overload gets cleared", target)
target.overloaded = 0
target.overload_locked = 0