forked from rspeer/dominiate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcards.coffee
3834 lines (3269 loc) · 108 KB
/
cards.coffee
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Dominion cards and their effects are defined in this file. Each card is a
# singleton, immutable object.
# We begin by creating the `c` object, an exported object with which one can
# look up any card by its name.
c = {}
this.c = c
c.allCards = []
# Defining cards
# --------------
# Many cards are defined in terms of other cards using a pattern similar to
# inheritance, except without the classes. There is no need for classes because
# there are no separate instances.
# Each Copper is a reference to the same single Copper object, for example.
#
# The `makeCard` function will define a new card and add it to the card list.
# `makeCard` works by copying an existing card object and applying a few new
# properties to it.
#
# `name` is the name of the card, which will be the card's string
# representation and the key that you look it up in the card list `c` by.
#
# To define a card independently of any existing card, let `origCard` be the
# abstract card called `basicCard`. To define a card in terms of another card,
# let `origCard` be that card object (probably a member of `c`, such as
# `c.Estate`).
#
# `props` are the properties of the card that differ from its parent.
#
# `fake` is true when this should be an abstract card, not a card in the
# supply. Fake cards are simply returned, not added to `c`.
makeCard = (name, origCard, props, fake) ->
newCard = {}
for key, value of origCard
newCard[key] = value
newCard.name = name
for key, value of props
newCard[key] = value
newCard.parent = origCard.name # for debugging
if not fake
c[name] = newCard
c.allCards.push(name)
newCard
#### The basicCard object
# `basicCard` contains all the things that are true by default
# about a card, plus many useful methods that will be available on all cards.
# All other cards should have `basicCard` as an ancestor. Many of the
# properties and methods of `basicCard` are meant to be overridden in
# real cards.
basicCard = {
# This set of boolean values defines a card's types. Cards may have any
# number of types.
isAction: false
isTreasure: false
isVictory: false
isAttack: false
isReaction: false
isDuration: false
isPrize: false
isMultiplier: false
# The **base cost** of a card is defined here. To find out what a card
# *actually* costs, use the getCost() method.
cost: 0
costPotion: 0
# These methods may be overridden by cards whose costs vary on their own,
# particularly Peddler.
costInCoins: (state) -> this.cost
costInPotions: (state) -> this.costPotion
# Card costs can change according to things external to the card, such as
# bridges and quarries in play. Therefore, any code that wants to know the
# actual cost of a card in a state should call `card.getCost(state)`.
#
# This method returns a list of two elements, which are the cost in
# coins and the cost in potions.
getCost: (state) ->
coins = this.costInCoins(state)
for modifier in state.costModifiers
coins += modifier.modify(this)
if coins < 0
coins = 0
return [coins, this.costInPotions(state)]
# These properties define simple, non-variable effects of playing a card.
# They may only have constant numeric values.
actions: 0
cards: 0
coins: 0
coinTokens: 0
buys: 0
vp: 0
trash: 0 # if the card requires trashing for no further effect
# If a card has simple effects that *vary* based on the state, define
# them by overriding these methods, which do take the state as a parameter.
# The constant properties above will be ignored in that case, but you could
# fill them in with reasonable guesses for the benefit of AI methods that
# don't want to examine the state.
getActions: (state) -> this.actions
getCards: (state) -> this.cards
getCoins: (state) -> this.coins
getCoinTokens: (state) -> this.coinTokens
getBuys: (state) -> this.buys
getTrash: (state) -> this.trash
getVP: (player) -> this.vp
getMultiplier: () ->
if this.isMultiplier then this.multiplier
else 1
# getPotion says whether the card provides a potion. There is only one
# card for which this is true, which is Potion.
getPotion: (state) -> 0
# Some cards (Grand Market) may not be bought in certain situations.
# Use `cards.mayBeBought(state)` to define when. By default, a card may be
# bought whenever it is in the supply.
mayBeBought: (state) -> true
# `card.startingSupply(state)` is called once for each card in the supply
# at the start of the game, to determine how many of them go into the supply.
# This is 10 by default, but some types of cards override it.
startingSupply: (state) -> 10
#### Complex effects
# More complex effects of a card can be defined using arbitrary functions
# that modify the state. These functions are no-ops in `basicCard`, and
# may be overridden by cards that need them:
# Card initialization that happens at the start of the game, for instance
# Black Market might set up the Black Market Deck, or Island might set up
# the Island Mat
startGameEffect: (state) ->
# - What happens when the card is bought?
buyEffect: (state) ->
# - What happens when the card is gained?
gainEffect: (state, player) ->
# - What happens (besides the simple effects defined above) when the card is
# played?
playEffect: (state) ->
# - What happens when this card is trashed?
trashEffect: (state, player) ->
# - What happens when this card is in play and another card is gained?
gainInPlayEffect: (state, card) ->
# - What happens when this card is in play and another card is specifically
# bought?
buyInPlayEffect: (state, card) ->
# - What happens when this card is cleaned up from play?
cleanupEffect: (state) ->
# - What happens when the card is in play as a Duration at the start of
# the turn?
durationEffect: (state) ->
# - What happens when the card is shuffled into the draw deck?
shuffleEffect: (state) ->
# - What happens when this card is in hand and an opponent plays an attack?
reactToAttack: (state, player, attackEvent) ->
# - What happens when this card is in the duration pile and an opponent plays an attack?
durationReactToAttack: (state, player, attackEvent) ->
# - What happens when this card is in hand and its owner gains a card?
reactToGain: (state, player, card) ->
# - What happens when this card is in hand and someone else gains a card?
reactToOpponentGain: (state, player, opponent, card) ->
# - What happens when this card is discarded?
reactToDiscard: (state, player) ->
# - What happens when a card is gained, in general?
globalGainEffect: (state, player, card, source) ->
# This defines everything that happens when a card is played, including
# basic effects and complex effects defined in `playEffect`. Cards
# should not override `onPlay`; they should override `playEffect` instead.
onPlay: (state) ->
state.current.actions += this.getActions(state)
state.current.coins += this.getCoins(state)
state.current.potions += this.getPotion(state)
state.current.coinTokens += this.getCoinTokens(state)
state.current.buys += this.getBuys(state)
cardsToDraw = this.getCards(state)
cardsToTrash = this.getTrash(state)
if cardsToDraw > 0
state.drawCards(state.current, cardsToDraw)
if cardsToTrash > 0
state.requireTrash(state.current, cardsToTrash)
if (ct = this.getCoinTokens(state)) > 0
state.log("#{state.current.ai} gains #{ct} Coin Token#{if ct > 1 then "s" else ""}")
this.playEffect(state)
# Similarly, these are other ways for the game state to interact
# with the card. Cards should override the `Effect` methods, not these.
onDuration: (state) ->
this.durationEffect(state)
onCleanup: (state) ->
this.cleanupEffect(state)
onBuy: (state) ->
this.buyEffect(state)
onGain: (state, player) ->
this.gainEffect(state, player)
onTrash: (state, player) ->
this.trashEffect(state, player)
# A card's string representation is its name.
#
# If you have a value called
# `card` that may be a string or a card object, you can ensure that it is
# a card object by looking up `c[card]`.
toString: () -> this.name
# `ai_` methods define the default AI preferences for this card. A prominent
# example is ai_playValue, which tells the AI how much to prefer playing this
# card (and of course changes with the state of the game). The higher the
# ai_playValue, the more it prefers playing it before other cards.
#
# `ai_multipliedValue` is similar, but it can be higher when it's playing an
# action with a Throne Room or King's Court.
ai_multipliedValue: (state, my) ->
unless this.ai_playValue?
throw new Error("no ai_playValue for #{this}")
result = this.ai_playValue(state, my)
return result
}
# Base cards
# ----------
# These are the cards that are not Kingdom cards. Most of them appear in every
# game; Potion, Platinum, and Colony appear in only some games.
makeCard 'Curse', basicCard, {
# Curse is the only card with no type.
cost: 0
vp: -1
startingSupply: (state) ->
switch state.nPlayers
when 1, 2 then 10
when 3 then 20
when 4 then 30
when 5 then 40
else 50
}
# To define victory cards, we define Estate and then derive other cards from
# it.
makeCard 'Estate', basicCard, {
cost: 2
isVictory: true
vp: 1
startingSupply: (state) ->
switch state.nPlayers
when 1, 2 then 8
else 12
}
makeCard 'Duchy', c.Estate, {
cost: 5, vp: 3,
# If Duchess is in the game, the player has the option of gaining it.
gainEffect: (state, player) ->
if state.supply['Duchess']?
state.gainOneOf(player, [c.Duchess, null])
}
makeCard 'Province', c.Estate, {
cost: 8
vp: 6
startingSupply: (state) ->
switch state.nPlayers
when 1, 2 then 8
when 3, 4 then 12
when 5 then 15
else 18
}
makeCard 'Colony', c.Estate, {cost: 11, vp: 10}
# Now we define the basic treasure cards. Our prototypical card here is
# Silver.
makeCard 'Silver', basicCard, {
cost: 3
isTreasure: true
coins: 2
startingSupply: (state) -> 40
ai_playValue: (state, my) -> 100
}
# Copper is actually more complex than Silver: its value can vary when modified
# by Coppersmith.
makeCard 'Copper', c.Silver, {
cost: 0
coins: 1
getCoins: (state) -> state.copperValue ? 1
startingSupply: (state) -> 60
}
makeCard 'Gold', c.Silver, {
cost: 6
coins: 3
startingSupply: (state) -> 30
}
makeCard 'Platinum', c.Silver, {
cost: 9,
coins: 5,
startingSupply: (state) -> 12
}
makeCard 'Potion', c.Silver, {
cost: 4
coins: 0
getPotion: (state) -> 1
startingSupply: (state) -> 16
}
# Vanilla cards
# -------------
#
# These cards have effects that involve no decisions, and are expressed entirely
# in +actions, +cards, +coins, +buys, and VP.
#
# Action cards may derive from the virtual card called `action`.
action = makeCard 'action', basicCard, {isAction: true}, true
makeCard 'Village', action, {
cost: 3, actions: 2, cards: 1
ai_playValue: (state, my) -> 820
}
makeCard "Worker's Village", action, {
cost: 4
actions: 2
cards: 1
buys: 1
ai_playValue: (state, my) -> 832
}
makeCard 'Laboratory', action, {
cost: 5, actions: 1, cards: 2
ai_playValue: (state, my) -> 782
}
makeCard 'Smithy', action, {
cost: 4
cards: 3
ai_playValue: (state, my) ->
if my.actions > 1 then 665 else 200
ai_multipliedValue: (state, my) ->
if my.actions > 0 then 1540 else -1
}
makeCard 'Festival', action, {
cost: 5, actions: 2, coins: 2, buys: 1
ai_playValue: (state, my) -> 845
}
makeCard 'Woodcutter', action, {
cost: 3, coins: 2, buys: 1
ai_playValue: (state, my) -> 164
}
makeCard 'Market', action, {
cost: 5, actions: 1, cards: 1, coins: 1, buys: 1
ai_playValue: (state, my) -> 775
}
makeCard 'Bazaar', action, {
cost: 5, actions: 2, cards: 1, coins: 1
ai_playValue: (state, my) -> 835
}
makeCard 'Candlestick Maker', action, {
cost: 2, actions: 1, coinTokens: 1, buys: 1
ai_playValue: (state, my) -> 734
}
# Kingdom Victory cards
# ---------------------
# These cards are all derived from Estate to insure their starting supply
# amount is correct. This goes for multi-type Victory cards too--deriving Great Hall
# from action instead of Estate results in 10 Great Halls in the supply instead of
# 8 for a 2-player game or 12 for more players.
makeCard 'Duke', c.Estate, {
cost: 5
getVP: (player) -> player.countInDeck('Duchy')
}
makeCard 'Fairgrounds', c.Estate, {
cost: 6
getVP: (player) ->
unique = []
deck = player.getDeck()
for card in deck
if card not in unique
unique.push(card)
2 * Math.floor(unique.length / 5)
}
makeCard 'Farmland', c.Estate, {
cost: 6
vp: 2
upgradeFilter: (state, oldCard, newCard) ->
[coins1, potions1] = oldCard.getCost(state)
[coins2, potions2] = newCard.getCost(state)
return (potions1 == potions2) and (coins1 + 2 == coins2)
buyEffect: (state) ->
choices = upgradeChoices(state, state.current.hand, this.upgradeFilter)
choice = state.current.ai.choose('upgrade', state, choices)
if choice isnt null
[oldCard, newCard] = choice
state.doTrash(state.current, oldCard)
state.gainCard(state.current, newCard)
}
makeCard 'Feodum', c.Estate, {
cost: 4
getVP: (player) -> Math.floor(player.countInDeck('Silver') / 3)
trashEffect: (state, player) ->
state.gainCard(player, c.Silver)
state.gainCard(player, c.Silver)
state.gainCard(player, c.Silver)
}
makeCard 'Gardens', c.Estate, {
cost: 4
getVP: (player) -> Math.floor(player.getDeck().length / 10)
}
makeCard 'Great Hall', c.Estate, {
isAction: true
cost: 3
cards: +1
actions: +1
ai_playValue: (state, my) ->
if c.Crossroads in my.hand
520
else
742
}
makeCard 'Harem', c.Estate, {
isTreasure: true
cost: 6
coins: 2
vp: 2
startingSupply: (state) -> 8
ai_playValue: (state, my) -> 100
}
makeCard 'Island', c.Estate, {
isAction: true
cost: 4
vp: 2
startGameEffect: (state) ->
for player in state.players
player.mats.island = []
playEffect: (state) ->
if state.current.hand.length == 0 # handle a weird edge case
state.log("…setting aside the Island (no other cards in hand).")
else
card = state.current.ai.choose('island', state, state.current.hand)
state.log("…setting aside the Island and a #{card}.")
state.current.hand.remove(card)
state.current.mats.island.push(card)
# removing the Island from play is conditional so it won't break with
# Throne Room and King's Court
if this in state.current.inPlay
state.current.inPlay.remove(this)
state.current.mats.island.push(this)
ai_playValue: (state, my) -> 132
}
makeCard 'Nobles', c.Estate, {
isAction: true
cost: 6
vp: 2
# Nobles is an example of a card that allows a choice from multiple
# simple effects. We implement this using the `choose('benefit')` AI method,
# which is passed a list of benefit objects, one of which it will choose
# to apply to the state.
playEffect: (state) ->
benefit = state.current.ai.choose('benefit', state, [
{actions: 2},
{cards: 3}
])
applyBenefit(state, benefit)
ai_playValue: (state, my) -> 296
ai_multipliedValue: (state, my) -> 1340
}
makeCard 'Silk Road', c.Estate, {
cost: 4
getVP: (player) -> Math.floor(player.countCardTypeInDeck('Victory') / 4)
}
# Revealing Tunnel for Gold as it is discarded is automatic.
# TODO: make this into a decision.
makeCard 'Tunnel', c.Estate, {
isReaction: true
cost: 3
vp: 2
reactToDiscard: (state, player) ->
if state.phase isnt 'cleanup'
state.log("#{player.ai} gains a Gold for discarding the Tunnel.")
state.gainCard(player, c.Gold)
}
makeCard 'Vineyard', c.Estate, {
cost: 0
costPotion: 1
getVP: (player) -> Math.floor(player.numActionCardsInDeck() / 3)
}
# Kingdom Treasure cards
# ----------------------
# Kingdom cards that are also treasure cards derive from treasure, which
# derives from Silver, but with a changed startingSupply.
treasure = makeCard 'treasure', c.Silver, {startingSupply: (state) -> 10}, true
makeCard 'Bank', treasure, {
cost: 7
getCoins: (state) ->
coins = 0
for card in state.current.inPlay
if card.isTreasure
coins += 1
coins
playEffect: (state) ->
state.log("...which is worth #{this.getCoins(state)}.")
ai_playValue: (state, my) -> 20
}
makeCard 'Cache', treasure, {
cost: 5
coins: 3
gainEffect: (state, player) ->
state.gainCard(player, c.Copper)
state.gainCard(player, c.Copper)
}
makeCard "Fool's Gold", treasure, {
isReaction: true
cost: 2
coins: 1
getCoins: (state) ->
if state.current.countInPlay("Fool's Gold") > 1
4
else
1
playEffect: (state) ->
state.current.foolsGoldInPlay = true
reactToOpponentGain: (state, player, opp, card) ->
if card is c.Province
if player.ai.choose('foolsGoldTrash', state, [yes, no])
state.doTrash(player, this)
state.gainCard(player, c.Gold, 'draw')
state.log("...putting the Gold on top of the draw pile.")
}
makeCard "Hoard", treasure, {
cost: 6
buyInPlayEffect: (state, card) ->
if card.isVictory
state.gainCard(state.current, c.Gold, 'discard', true)
state.log("...gaining a Gold.")
}
makeCard "Horn of Plenty", treasure, {
cost: 5
coins: 0
playEffect: (state) ->
limit = state.current.numUniqueCardsInPlay()
choices = []
for cardName of state.supply
card = c[cardName]
[coins, potions] = card.getCost(state)
if state.supply[cardName] > 0 and potions == 0 and coins <= limit
choices.push(card)
choice = state.gainOneOf(state.current, choices)
if choice.isVictory
transferCard(this, state.current.inPlay, state.trash)
state.log("...#{state.current.ai} trashes the Horn of Plenty.")
aiPlayValue: (state, my) ->
if my.numUniqueCardsInPlay() >= 2
10
else
-10
}
makeCard 'Ill-Gotten Gains', treasure, {
cost: 5
coins: 1
playEffect: (state) ->
if state.current.ai.choose('gainCopper', state, [yes, no])
state.gainCard(state.current, c.Copper, 'hand')
gainEffect: (state, player) ->
# For each player but the gainer: gain a curse.
for i in [0...state.nPlayers]
if state.players[i] != player
state.gainCard(state.players[i], c.Curse)
}
makeCard 'Loan', treasure, {
coins: 1
playEffect: (state) ->
drawn = state.current.dig(state,
(state, card) -> card.isTreasure
)
if drawn.length > 0
treasure = drawn[0]
trash = state.current.ai.choose('trash', state, [treasure, null])
if trash?
state.log("...trashing the #{treasure}.")
transferCard(treasure, drawn, state.trash)
else
state.log("...discarding the #{treasure}.")
state.current.discard.push(treasure)
state.handleDiscards(state.current, [treasure])
ai_playValue: (state, my) -> 70
}
makeCard "Philosopher's Stone", treasure, {
cost: 3
costPotion: 1
getCoins: (state) ->
Math.floor((state.current.draw.length + state.current.discard.length) / 5)
playEffect: (state) ->
state.log("...which is worth #{this.getCoins(state)}.")
}
makeCard 'Quarry', treasure, {
cost: 4
coins: 1
playEffect: (state) =>
state.costModifiers.push
source: this
modify: (card) ->
if card.isAction
-2
else
0
}
makeCard 'Royal Seal', treasure, {
cost: 5
gainInPlayEffect: (state, card) ->
player = state.current
return if player.gainLocation == 'trash'
source = player[player.gainLocation]
if player.ai.choose('gainOnDeck', state, [card, null])
state.log("...putting the #{card} on top of the deck.")
player.gainLocation = 'draw'
transferCardToTop(card, source, player.draw)
}
makeCard 'Spoils', treasure, {
cost: 0
coins: 3
mayBeBought: (state) -> false
startingSupply: (state) -> 0
playEffect: (state) ->
state.current.inPlay.remove(this)
state.specialSupply['Spoils'] += 1
state.log("#{state.specialSupply['Spoils']} Spoils in the supply")
ai_playValue: (state, my) ->
if my.ai.wantsToPlaySpoils(state)
81
else
null
}
makeCard 'Talisman', treasure, {
cost: 4
coins: 1
buyInPlayEffect: (state, card) ->
if card.getCost(state)[0] <= 4 and not card.isVictory
state.gainCard(state.current, card, 'discard', true)
state.log("...gaining a #{card}.")
}
makeCard 'Venture', treasure, {
cost: 5
coins: 1
playEffect: (state) ->
drawn = state.current.dig(state,
(state, card) -> card.isTreasure
)
if drawn.length > 0
treasure = drawn[0]
state.log("...playing #{treasure}.")
state.current.inPlay.push(treasure)
treasure.onPlay(state)
ai_playValue: (state, my) -> 80
}
# Duration cards
# --------------
# These cards have additional properties, such as `durationActions`, defining
# constant effects that happen when the card is resolved as a duration card.
# The virtual card `duration` specifies how to process these effects.
duration = makeCard 'duration', action, {
durationActions: 0
durationBuys: 0
durationCoins: 0
durationCards: 0
isDuration: true
durationEffect:
(state) ->
state.current.actions += this.durationActions
state.current.buys += this.durationBuys
state.current.coins += this.durationCoins
if this.durationCards > 0
state.drawCards(state.current, this.durationCards)
}, true
makeCard 'Haven', duration, {
cost: 2
cards: +1
actions: +1
startGameEffect: (state) ->
for player in state.players
# We put Haven and the cards it sets aside on a "mat"
player.mats.haven = []
playEffect: (state) ->
cardInHaven = state.current.ai.choose('putOnDeck', state, state.current.hand)
if cardInHaven?
state.log("#{state.current.ai} sets aside a #{cardInHaven} with Haven.")
transferCard(cardInHaven, state.current.hand, state.current.mats.haven)
else
if state.current.hand.length==0
state.log("#{state.current.ai} has no cards to set aside.")
else
state.warn("hand not empty but no card set aside")
durationEffect: (state) ->
cardFromHaven = state.current.mats.haven.pop()
if cardFromHaven?
state.log("#{state.current.ai} picks up a #{cardFromHaven} from Haven.")
state.current.hand.unshift(cardFromHaven)
ai_playValue: (state, my) -> 710
}
makeCard 'Caravan', duration, {
cost: 4
cards: +1
actions: +1
durationCards: +1
ai_playValue: (state, my) -> 780
}
makeCard 'Fishing Village', duration, {
cost: 3
coins: +1
actions: +2
durationActions: +1
durationCoins: +1
ai_playValue: (state, my) -> 823
}
makeCard 'Wharf', duration, {
cost: 5
cards: +2
buys: +1
durationCards: +2
durationBuys: +1
ai_playValue: (state, my) -> 275
ai_multipliedValue: (state, my) ->
if my.actions > 0 then 1740 else -1
}
makeCard 'Merchant Ship', duration, {
cost: 5
coins: +2
durationCoins: +2
ai_playValue: (state, my) -> 186
ai_multipliedValue: (state, my) ->
if my.actions > 0 then 1500 else -1
}
makeCard 'Lighthouse', duration, {
cost: 2
actions: +1
coins: +1
durationCoins: +1
ai_playValue: (state, my) -> 715
durationReactToAttack: (state, player, attackEvent) ->
# Don't bother blocking the attack if it's already blocked (avoid log spam)
unless attackEvent.blocked
state.log("#{player.ai} is protected by the Lighthouse.")
attackEvent.blocked = true
}
makeCard 'Outpost', duration, {
cost: 5
#effect implemented by gameState
ai_playValue: (state, my) ->
if state.extraTurn
-15
else
154
}
makeCard 'Tactician', duration, {
cost: 5
durationActions: +1
durationBuys: +1
durationCards: +5
playEffect: (state) ->
# If this is the first time we've played Tactician this turn, reset the count
# of active Tacticians.
if state.current.countInPlay('Tactician') == 1
state.cardState[this] =
activeTacticians: 0
cardsInHand = state.current.hand.length
# If any cards can be discarded...
if cardsInHand > 0
# Discard the hand and activate the tactician.
state.log("...discarding the whole hand.")
state.cardState[this].activeTacticians++
discards = state.current.hand
state.current.discard = state.current.discard.concat(discards)
state.current.hand = []
state.handleDiscards(state.current, discards)
# The cleanupEffect of a dead Tactician is to discard it instead of putting it in the
# duration area. It's not a duration card in this case.
cleanupEffect: (state) ->
if state.cardState[this].activeTacticians > 0
state.cardState[this].activeTacticians--
else
state.log("#{state.current.ai} discards an inactive Tactician.")
transferCard(c.Tactician, state.current.inPlay, state.current.discard)
state.handleDiscards(state.current, [c.Tactician])
ai_playValue: (state, my) ->
# FIXME: playing Tactician is extremely situational and this doesn't take
# it into account.
272
}
# Trash-for-gain cards
# --------------------
# This section describes the actions where you trash one card to gain another.
# I refer to this in general as "upgrading", which is not meant to be specific
# to the card Upgrade.
#
# The prototype on which we base these cards is Remodel. Most of the other
# cards are variants that simply change the filter for which upgrades are
# possible.
makeCard 'Remodel', action, {
cost: 4
exactCostUpgrade: false
costFunction: (coins) -> coins + 2
upgradeFilter: (state, oldCard, newCard) ->
# Given two cards, return whether upgrading from oldCard to newCard is allowed.
[coins1, potions1] = oldCard.getCost(state)
[coins2, potions2] = newCard.getCost(state)
# We'll leave the cost check in `this.costFunction`, so we can reuse this code
# for many upgrading cards with different cost requirements.
if this.exactCostUpgrade
return (potions1 == potions2) and (this.costFunction(coins1) == coins2)
else
return (potions1 >= potions2) and (this.costFunction(coins1) >= coins2)
playEffect: (state) ->
# Find the pairs of cards we're allowed to upgrade from and to.
choices = upgradeChoices(state, state.current.hand, this.upgradeFilter.bind(this))
if this.exactCostUpgrade
# If the card requires upgrading to a card with an *exact* cost, then
# we'll likely have the option to upgrade a card to nothing. Add in
# those choices.
choices2 = nullUpgradeChoices(state, state.current.hand, this.costFunction.bind(this))
choices = choices.concat(choices2)
choice = state.current.ai.choose('upgrade', state, choices)
if choice isnt null
[oldCard, newCard] = choice
state.doTrash(state.current, oldCard)
if newCard isnt null
state.gainCard(state.current, newCard)
ai_playValue: (state, my) -> 223
}
makeCard 'Develop', action, {
cost: 3
# exactCostUpgrade: true
developTarget: (state, oldCard, newCard) ->
return Math.abs(oldCard.getCost(state)[0] - newCard.getCost(state)[0])==1 and (oldCard.getCost(state)[1] == newCard.getCost(state)[1])
playEffect: (state) ->
oldChoices = state.current.hand.unique()
choices = []
for oldCard in oldChoices
newCards = []
for card in state.filledPiles()
if (this.developTarget(state, oldCard, c[card]))
newCards.push(c[card])
if newCards.length==0
choices.push([oldCard, [null, null]])
else
for newCard in newCards
partnerCards = []
for card in state.filledPiles()
if (this.developTarget(state, oldCard, c[card]) and c[card].getCost(state)[0] != c[newCard].getCost(state)[0])
partnerCards.push(c[card])
if partnerCards.length==0
choices.push([oldCard, [newCard, null]])
else
for partnerCard in partnerCards
choices.push([oldCard, [newCard,partnerCard]] )
choice = state.current.ai.choose('develop', state, choices)
if choice isnt null
[oldCard, [newCard1, newCard2]] = choice
state.doTrash(state.current, oldCard)
if newCard1 isnt null
state.gainCard(state.current, newCard1, 'draw')
if newCard2 isnt null
state.gainCard(state.current, newCard2, 'draw')
# A rough approximation to when you want to Develop: when all you've
# got to play is terminals.
ai_playValue: (state, my) -> 271
}
makeCard 'Expand', c.Remodel, {
cost: 7
costFunction: (coins) -> coins + 3
ai_playValue: (state, my) -> 226
}
# New in Dark Ages.
makeCard 'Graverobber', c.Remodel, {
cost: 5
upgradeFilter: (state, oldCard, newCard) ->
[coins1, potions1] = oldCard.getCost(state)
[coins2, potions2] = newCard.getCost(state)
return oldCard.isAction and (potions1 >= potions2) and (coins1 + 3 >= coins2)
# I'll suppose this card is a bit better to play than Remodel and worse than
# Expand, but I really don't know.
ai_playValue: (state, my) -> 225
playEffect: (state) ->
# Find the pairs of cards we're allowed to upgrade from and to.
choices = upgradeChoices(state, state.current.hand, this.upgradeFilter.bind(this))
# We can instead choose to gain cards costing 3 to 6 from the trash onto the deck.
# Consider those as "upgrades" from nothing to that card, so we can compare them
# to our upgrade choices.
#
# FIXME: This doesn't take into account the benefit (or drawback) of gaining a card
# on the deck.
for card in state.trash
[coins, potions] = card.getCost(state)
if 3 <= coins <= 6 and potions == 0
choices.push [null, card]
choice = state.current.ai.choose('upgrade', state, choices)
if choice isnt null
[oldCard, newCard] = choice