enjine 0.4.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +196 -1
  3. data/README.md +12 -3
  4. data/SUPPORTED.md +73 -38
  5. data/docs/ARCHITECTURE.md +7 -2
  6. data/docs/MECHANICS_COVERAGE.md +10 -8
  7. data/docs/guides/api.md +2 -1
  8. data/docs/guides/card-data-format.md +146 -9
  9. data/docs/plans/dsl-gaps.md +23 -0
  10. data/docs/reference/abilities.md +299 -25
  11. data/docs/reference/costs.md +59 -6
  12. data/docs/reference/decisions.md +7 -1
  13. data/docs/reference/effects.md +200 -27
  14. data/docs/reference/filters.md +30 -3
  15. data/docs/reference/keywords.md +58 -2
  16. data/docs/reference/subtypes.md +18 -0
  17. data/docs/reference/triggers.md +94 -14
  18. data/docs/reference/types.md +20 -0
  19. data/lib/enjine/actions/play_land.rb +3 -1
  20. data/lib/enjine/actions/special_action.rb +4 -0
  21. data/lib/enjine/actions/turn_face_up.rb +25 -0
  22. data/lib/enjine/actions/unlock.rb +23 -0
  23. data/lib/enjine/card_definition.rb +54 -15
  24. data/lib/enjine/card_face_down.rb +31 -0
  25. data/lib/enjine/card_faces.rb +4 -2
  26. data/lib/enjine/card_rooms.rb +86 -0
  27. data/lib/enjine/catalog.rb +9 -3
  28. data/lib/enjine/cli/text.rb +11 -3
  29. data/lib/enjine/color_identity.rb +34 -1
  30. data/lib/enjine/decisions/casting.rb +6 -3
  31. data/lib/enjine/decisions/combat.rb +4 -2
  32. data/lib/enjine/decisions/damage_order.rb +18 -0
  33. data/lib/enjine/engine/ability_layers.rb +14 -1
  34. data/lib/enjine/engine/casting/alt_casting.rb +45 -5
  35. data/lib/enjine/engine/casting/cast_choices.rb +21 -2
  36. data/lib/enjine/engine/casting/casting.rb +42 -29
  37. data/lib/enjine/engine/casting/cost_options.rb +3 -1
  38. data/lib/enjine/engine/casting/costs.rb +8 -1
  39. data/lib/enjine/engine/casting/payment.rb +47 -9
  40. data/lib/enjine/engine/casting/permissions.rb +19 -17
  41. data/lib/enjine/engine/casting/target_counts.rb +14 -3
  42. data/lib/enjine/engine/casting/waterbend.rb +22 -2
  43. data/lib/enjine/engine/characteristics.rb +11 -11
  44. data/lib/enjine/engine/combat.rb +45 -14
  45. data/lib/enjine/engine/copies.rb +12 -4
  46. data/lib/enjine/engine/crimes.rb +46 -0
  47. data/lib/enjine/engine/damage.rb +85 -1
  48. data/lib/enjine/engine/entering.rb +36 -2
  49. data/lib/enjine/engine/face_down.rb +12 -3
  50. data/lib/enjine/engine/face_down_permanents.rb +108 -0
  51. data/lib/enjine/engine/faces.rb +20 -5
  52. data/lib/enjine/engine/follow_ups.rb +4 -1
  53. data/lib/enjine/engine/mana_sources.rb +18 -3
  54. data/lib/enjine/engine/options_memo.rb +140 -0
  55. data/lib/enjine/engine/permanent_designations.rb +29 -0
  56. data/lib/enjine/engine/resolution.rb +71 -6
  57. data/lib/enjine/engine/rooms.rb +86 -0
  58. data/lib/enjine/engine/rules.rb +6 -4
  59. data/lib/enjine/engine/state_based_actions.rb +25 -4
  60. data/lib/enjine/engine/triggers.rb +25 -20
  61. data/lib/enjine/engine/turn_history.rb +43 -5
  62. data/lib/enjine/engine/turn_structure.rb +1 -0
  63. data/lib/enjine/engine/zones.rb +37 -8
  64. data/lib/enjine/engine.rb +5 -1
  65. data/lib/enjine/events.rb +11 -0
  66. data/lib/enjine/frames.rb +5 -2
  67. data/lib/enjine/fuzz.rb +3 -0
  68. data/lib/enjine/game.rb +3 -1
  69. data/lib/enjine/mana.rb +3 -0
  70. data/lib/enjine/mechanics/abilities/chosen_values.rb +49 -6
  71. data/lib/enjine/mechanics/abilities/cost_changes.rb +32 -10
  72. data/lib/enjine/mechanics/abilities/damage_replacements.rb +137 -65
  73. data/lib/enjine/mechanics/abilities/designated.rb +57 -0
  74. data/lib/enjine/mechanics/abilities/entering_choices.rb +40 -21
  75. data/lib/enjine/mechanics/abilities/life_replacements.rb +74 -0
  76. data/lib/enjine/mechanics/abilities/mana_ability.rb +10 -4
  77. data/lib/enjine/mechanics/abilities/play_permissions.rb +52 -20
  78. data/lib/enjine/mechanics/abilities/player_rules.rb +30 -13
  79. data/lib/enjine/mechanics/abilities/stack_abilities.rb +12 -7
  80. data/lib/enjine/mechanics/abilities/static.rb +19 -6
  81. data/lib/enjine/mechanics/card_spec.rb +63 -11
  82. data/lib/enjine/mechanics/costs/base.rb +68 -9
  83. data/lib/enjine/mechanics/costs/behold.rb +28 -5
  84. data/lib/enjine/mechanics/costs/choose_creature_type.rb +43 -0
  85. data/lib/enjine/mechanics/costs/collect_evidence.rb +60 -0
  86. data/lib/enjine/mechanics/costs/exile_permanent.rb +41 -0
  87. data/lib/enjine/mechanics/costs/one_of.rb +3 -0
  88. data/lib/enjine/mechanics/costs/pay_life.rb +27 -8
  89. data/lib/enjine/mechanics/costs/remove_counters.rb +8 -5
  90. data/lib/enjine/mechanics/costs/tap.rb +44 -3
  91. data/lib/enjine/mechanics/effects/base.rb +36 -7
  92. data/lib/enjine/mechanics/effects/become.rb +29 -12
  93. data/lib/enjine/mechanics/effects/choose_cards.rb +44 -14
  94. data/lib/enjine/mechanics/effects/copy.rb +58 -21
  95. data/lib/enjine/mechanics/effects/counters.rb +18 -6
  96. data/lib/enjine/mechanics/effects/damage.rb +11 -0
  97. data/lib/enjine/mechanics/effects/delayed_triggers.rb +13 -6
  98. data/lib/enjine/mechanics/effects/earthbend.rb +2 -1
  99. data/lib/enjine/mechanics/effects/fight.rb +11 -0
  100. data/lib/enjine/mechanics/effects/library.rb +76 -18
  101. data/lib/enjine/mechanics/effects/may.rb +4 -2
  102. data/lib/enjine/mechanics/effects/pay.rb +27 -10
  103. data/lib/enjine/mechanics/effects/repeat.rb +92 -0
  104. data/lib/enjine/mechanics/effects/search.rb +34 -6
  105. data/lib/enjine/mechanics/effects/tokens.rb +42 -10
  106. data/lib/enjine/mechanics/filters/any_target.rb +6 -3
  107. data/lib/enjine/mechanics/filters/base.rb +36 -17
  108. data/lib/enjine/mechanics/filters/exile.rb +27 -0
  109. data/lib/enjine/mechanics/filters/permanents.rb +7 -0
  110. data/lib/enjine/mechanics/filters/stack.rb +14 -8
  111. data/lib/enjine/mechanics/keywords/alternative_costs.rb +80 -22
  112. data/lib/enjine/mechanics/keywords/cards_have.rb +67 -0
  113. data/lib/enjine/mechanics/keywords/craft.rb +132 -0
  114. data/lib/enjine/mechanics/keywords/cycling.rb +46 -7
  115. data/lib/enjine/mechanics/keywords/extort.rb +2 -0
  116. data/lib/enjine/mechanics/keywords/face_down.rb +165 -0
  117. data/lib/enjine/mechanics/keywords/flags.rb +3 -1
  118. data/lib/enjine/mechanics/keywords/hideaway.rb +6 -5
  119. data/lib/enjine/mechanics/keywords/impending.rb +75 -0
  120. data/lib/enjine/mechanics/keywords/increment.rb +2 -0
  121. data/lib/enjine/mechanics/keywords/infinity.rb +46 -0
  122. data/lib/enjine/mechanics/keywords/kicker.rb +24 -6
  123. data/lib/enjine/mechanics/keywords/miracle.rb +142 -0
  124. data/lib/enjine/mechanics/keywords/ninjutsu.rb +101 -0
  125. data/lib/enjine/mechanics/keywords/offspring.rb +48 -0
  126. data/lib/enjine/mechanics/keywords/persist_and_undying.rb +42 -0
  127. data/lib/enjine/mechanics/keywords/protection.rb +24 -8
  128. data/lib/enjine/mechanics/keywords/riot.rb +13 -10
  129. data/lib/enjine/mechanics/keywords/split_second.rb +29 -0
  130. data/lib/enjine/mechanics/keywords/storm_and_cascade.rb +5 -0
  131. data/lib/enjine/mechanics/keywords/void.rb +34 -0
  132. data/lib/enjine/mechanics/linked.rb +33 -3
  133. data/lib/enjine/mechanics/mana_spent.rb +1 -1
  134. data/lib/enjine/mechanics/selector.rb +62 -18
  135. data/lib/enjine/mechanics/subtypes/case.rb +68 -0
  136. data/lib/enjine/mechanics/triggers/base.rb +13 -4
  137. data/lib/enjine/mechanics/triggers/casting.rb +15 -9
  138. data/lib/enjine/mechanics/triggers/combat.rb +32 -12
  139. data/lib/enjine/mechanics/triggers/crime.rb +28 -0
  140. data/lib/enjine/mechanics/triggers/damage.rb +33 -18
  141. data/lib/enjine/mechanics/triggers/draw_and_discard.rb +19 -6
  142. data/lib/enjine/mechanics/triggers/keyword_actions.rb +17 -0
  143. data/lib/enjine/mechanics/triggers/rooms.rb +49 -0
  144. data/lib/enjine/mechanics/triggers/tap_and_counters.rb +20 -9
  145. data/lib/enjine/mechanics/triggers/zone_changes.rb +24 -9
  146. data/lib/enjine/mechanics/types/battle.rb +130 -0
  147. data/lib/enjine/mechanics/values.rb +84 -30
  148. data/lib/enjine/replacements.rb +21 -2
  149. data/lib/enjine/rules_monitor.rb +56 -71
  150. data/lib/enjine/snapshot/deep_copy.rb +23 -0
  151. data/lib/enjine/snapshot/player_checks.rb +10 -2
  152. data/lib/enjine/snapshot.rb +2 -1
  153. data/lib/enjine/state.rb +2 -2
  154. data/lib/enjine/verify.rb +10 -3
  155. data/lib/enjine/version.rb +1 -1
  156. data/lib/enjine/view.rb +50 -14
  157. data/lib/enjine.rb +1 -1
  158. metadata +34 -1
@@ -89,14 +89,15 @@ Rules: CR 603.2, CR 603.2c
89
89
 
90
90
  ## attacks
91
91
 
92
- The subject is declared as an attacker. `first_time_this_turn: true`: only the first time that creature attacks this turn (later combats of the turn don't trigger it).
92
+ The subject is declared as an attacker. `first_time_this_turn: true`: only the first time that creature attacks this turn (later combats of the turn don't trigger it). `alone: true`: only when it is the only creature declared as an attacker (it triggers as the declaration is complete).
93
93
 
94
- Rules: CR 508.3a, CR 603.2
94
+ Rules: CR 508.3a, CR 603.2, CR 506.5
95
95
 
96
96
  | Parameter | Description |
97
97
  | --- | --- |
98
98
  | `subject` | "self" (default) or {"each": filter, ...} |
99
99
  | `first_time_this_turn` | boolean |
100
+ | `alone` | boolean |
100
101
 
101
102
  ```json
102
103
  {
@@ -198,7 +199,7 @@ Rules: CR 603.2e, CR 701.26a, CR 702.194a
198
199
 
199
200
  The subject becomes the target of a spell or ability (controlled by `by`: opponent | you | any (default), relative to the source's controller). One trigger per matching object targeted.
200
201
 
201
- Rules: CR 115.1, CR 601.2c, CR 603.2, CR 603.2c
202
+ Rules: CR 115.1, CR 115.7, CR 601.2c, CR 603.2, CR 603.2c, CR 707.10
202
203
 
203
204
  | Parameter | Description |
204
205
  | --- | --- |
@@ -262,9 +263,9 @@ Rules: CR 509.3a, CR 509.3b, CR 509.3f, CR 603.2
262
263
 
263
264
  ## cast
264
265
 
265
- A player casts a spell. `controller`: you (default) | opponent | any; `spell`: creature | noncreature | instant_or_sorcery, or any spell filter, e.g. {"spell": {"subtypes": ["saga"]}} or {"one_of": [...]} for "creature or Spacecraft spell"; `name` / `mana_value`: "chosen" (the source's as-it-entered choice); `self: true`: "when you cast this spell" (it functions on the stack). `targets`: only a spell that targets something matching this filter ("self": this permanent), e.g. {"creature": {"controller": "you"}}. `from` / `not_from`: only a spell cast from (not from) that zone: hand | graveyard | exile | library | outside. `single_target: true`: only a spell with exactly one target.
266
+ A player casts a spell. `controller`: you (default) | opponent | any; `spell`: creature | noncreature | instant_or_sorcery, or any spell filter, e.g. {"spell": {"subtypes": ["saga"]}} or {"one_of": [...]} for "creature or Spacecraft spell"; `name` / `mana_value`: "chosen" (the source's as-it-entered choice); `self: true`: "when you cast this spell" (it functions on the stack). `targets`: only a spell that targets something matching this filter ("self": this permanent), e.g. {"creature": {"controller": "you"}}. `from` / `not_from`: only a spell cast from (not from) that zone: hand | graveyard | exile | library | outside. `single_target: true`: only a spell with exactly one target. `owner`: "not_you" only a spell you don't own, "you" only one you own.
266
267
 
267
- Rules: CR 603.2, CR 601.2i, CR 113.6, CR 202.3e, CR 201.4, CR 607.2, CR 115.1, CR 601.2a, CR 601.2c
268
+ Rules: CR 603.2, CR 601.2i, CR 113.6, CR 202.3e, CR 201.4, CR 607.2, CR 115.1, CR 601.2a, CR 601.2c, CR 108.3
268
269
 
269
270
  | Parameter | Description |
270
271
  | --- | --- |
@@ -277,6 +278,7 @@ Rules: CR 603.2, CR 601.2i, CR 113.6, CR 202.3e, CR 201.4, CR 607.2, CR 115.1, C
277
278
  | `from` | zone |
278
279
  | `not_from` | zone |
279
280
  | `single_target` | true |
281
+ | `owner` | you | not_you |
280
282
 
281
283
  ```json
282
284
  {
@@ -287,9 +289,25 @@ Rules: CR 603.2, CR 601.2i, CR 113.6, CR 202.3e, CR 201.4, CR 607.2, CR 115.1, C
287
289
  }
288
290
  ```
289
291
 
292
+ ## commits_crime
293
+
294
+ A player commits a crime: they cast a spell, activate an ability or put a triggered ability on the stack that targets an opponent, a permanent, spell or ability an opponent controls, or a card in an opponent's graveyard (once per spell or ability). `controller`: you (default) | opponent | any. {"triggering": "object"} is that spell or ability. Amount `{"crimes_this_turn": player}` counts them.
295
+
296
+ Rules: CR 700.13, CR 603.2
297
+
298
+ | Parameter | Description |
299
+ | --- | --- |
300
+ | `controller` | you | opponent | any |
301
+
302
+ ```json
303
+ {
304
+ "commits_crime": {}
305
+ }
306
+ ```
307
+
290
308
  ## counters_placed
291
309
 
292
- One or more counters (only `counter`, if given) are put on the subject, including the counters it enters the battlefield with. Triggers once per event, however many counters.
310
+ One or more counters (only `counter`, if given) are put on the subject, including the counters it enters the battlefield with. Triggers once per event, however many counters. `nth` (needs `counter`): only when the Nth such counter is put on it (the event that takes it from fewer than N to N or more).
293
311
 
294
312
  Rules: CR 122.6, CR 603.2, CR 603.2c
295
313
 
@@ -299,6 +317,7 @@ Rules: CR 122.6, CR 603.2, CR 603.2c
299
317
  | `counter` | counter name |
300
318
  | `first_time_this_turn` | true: only the first time each turn such counters are put on that permanent (an earlier placement counts even if nothing could trigger then) |
301
319
  | `by` | you | opponent: only counters that player puts ("whenever you put ...", CR 122.6a; a permanent's controller puts the counters it enters with) |
320
+ | `nth` | positive integer |
302
321
 
303
322
  ```json
304
323
  {
@@ -329,14 +348,15 @@ Rules: CR 702.122b, CR 603.2
329
348
 
330
349
  ## cycles
331
350
 
332
- A player (`controller`: you (default) | opponent | any) cycles a card (activates its cycling or typecycling ability; optional `card` filter).
351
+ A player (`controller`: you (default) | opponent | any) cycles a card (activates its cycling or typecycling ability; optional `card` filter). `self: true`: "when you cycle this card", an ability of the card with `zone: "graveyard"` (where the cycled card is); its X is the cycling ability's X.
333
352
 
334
- Rules: CR 702.29a, CR 702.29e
353
+ Rules: CR 702.29a, CR 702.29c, CR 702.29e, CR 113.6k
335
354
 
336
355
  | Parameter | Description |
337
356
  | --- | --- |
338
357
  | `controller` | you | opponent | any |
339
358
  | `card` | card filter |
359
+ | `self` | true |
340
360
 
341
361
  ```json
342
362
  {
@@ -348,7 +368,7 @@ Rules: CR 702.29a, CR 702.29e
348
368
 
349
369
  ## deals_damage
350
370
 
351
- The subject deals damage. `combat: true` only counts combat damage, `noncombat: true` only noncombat damage; `to`: player | opponent (of the source's controller) | creature | any (default). Triggers once per damage event (each combat damage step is one, so double strike can trigger twice).
371
+ The subject deals damage. `combat: true` only counts combat damage, `noncombat: true` only noncombat damage; `to`: player | opponent | you (players, relative to the source's controller) | creature | planeswalker | battle | permanent | any (default), or a list of them ("a player or planeswalker"). Triggers once per damage event (each combat damage step is one, so double strike can trigger twice). `batched: true`: "one or more ... deal damage", once per event batch (a combat damage step); then {"triggering": "objects"} are the sources that dealt it and {"triggering": "amount"} the number of damage events.
352
372
 
353
373
  Rules: CR 510.2, CR 603.2, CR 603.2c, CR 120.2a, CR 120.2b, CR 120.3
354
374
 
@@ -357,7 +377,8 @@ Rules: CR 510.2, CR 603.2, CR 603.2c, CR 120.2a, CR 120.2b, CR 120.3
357
377
  | `subject` | "self" (default) or {"each": filter, ...} |
358
378
  | `combat` | true | false |
359
379
  | `noncombat` | true | false |
360
- | `to` | player | opponent | creature | any |
380
+ | `to` | player | opponent | you | creature | planeswalker | battle | permanent | any, or a list of them |
381
+ | `batched` | true | false |
361
382
 
362
383
  ```json
363
384
  {
@@ -392,13 +413,14 @@ Rules: CR 120.3, CR 603.2, CR 603.2c, CR 120.2a, CR 120.2b
392
413
 
393
414
  ## dies
394
415
 
395
- The subject is put into a graveyard from the battlefield. Looks back in time (CR 603.10a).
416
+ The subject is put into a graveyard from the battlefield. Looks back in time (CR 603.10a). `batched: true`: "whenever one or more ... die", once per event batch (creatures dying at the same time, e.g. to lethal combat damage); {"triggering": "objects"} are the cards put into graveyards and {"triggering": "amount"} how many.
396
417
 
397
- Rules: CR 603.6c, CR 603.10a, CR 700.4
418
+ Rules: CR 603.6c, CR 603.10a, CR 700.4, CR 603.2c
398
419
 
399
420
  | Parameter | Description |
400
421
  | --- | --- |
401
422
  | `subject` | "self" (default) or {"each": filter, ...} |
423
+ | `batched` | true | false |
402
424
 
403
425
  ```json
404
426
  {
@@ -465,15 +487,16 @@ Rules: CR 701.66b
465
487
 
466
488
  ## enters
467
489
 
468
- The subject enters the battlefield. `untapped: true`: only if it entered untapped. `from`: only if it entered from that zone ("enters from exile").
490
+ The subject enters the battlefield. `untapped: true`: only if it entered untapped. `from`: only if it entered from that zone ("enters from exile"). `batched: true`: "whenever one or more ... enter", once per event batch; {"triggering": "objects"} are the permanents that entered and {"triggering": "amount"} how many.
469
491
 
470
- Rules: CR 603.6a, CR 603.2, CR 614.12
492
+ Rules: CR 603.6a, CR 603.2, CR 603.2c, CR 614.12
471
493
 
472
494
  | Parameter | Description |
473
495
  | --- | --- |
474
496
  | `subject` | "self" (default) or {"each": filter, "controller": you|opponent|any, "other": true} |
475
497
  | `untapped` | true | false |
476
498
  | `from` | hand | graveyard | exile | library | stack | outside |
499
+ | `batched` | true | false |
477
500
 
478
501
  ```json
479
502
  {
@@ -536,6 +559,22 @@ Rules: CR 119.9, CR 120.3f
536
559
  }
537
560
  ```
538
561
 
562
+ ## gives_gift
563
+
564
+ A player (`controller`: you (default) | opponent | any) gives a gift: an instant or sorcery spell they control whose gift was promised resolves, or the gift triggered ability (an enters trigger under `{"cast_with": "gift"}`) of a permanent they control resolves.
565
+
566
+ Rules: CR 702.174c, CR 702.174b
567
+
568
+ | Parameter | Description |
569
+ | --- | --- |
570
+ | `controller` | you | opponent | any |
571
+
572
+ ```json
573
+ {
574
+ "gives_gift": {}
575
+ }
576
+ ```
577
+
539
578
  ## leaves_battlefield
540
579
 
541
580
  The subject leaves the battlefield (for any zone, or only `to` that zone). Looks back in time: the permanent is matched as it last existed (types, controller, counters).
@@ -783,6 +822,47 @@ Rules: CR 701.27f, CR 603.2
783
822
  }
784
823
  ```
785
824
 
825
+ ## turned_face_up
826
+
827
+ The subject (default this permanent) is turned face up. On a card with morph, disguise or that can be manifested, it's "when this creature is turned face up".
828
+
829
+ Rules: CR 708.8, CR 702.37e, CR 702.168d, CR 603.2
830
+
831
+ | Parameter | Description |
832
+ | --- | --- |
833
+ | `subject` | "self" (default) or {"each": filter, "controller": ...} |
834
+
835
+ ```json
836
+ {
837
+ "turned_face_up": {}
838
+ }
839
+ ```
840
+
841
+ ## unlocks_door
842
+
843
+ A Room permanent (the subject, default this one) gets an unlocked designation. On a Room's door, `{}` means "when you unlock this door". `door`: left | right | any; `fully: true`: only when it now has both doors unlocked ("whenever you fully unlock a Room").
844
+
845
+ Rules: CR 709.5h, CR 709.5i, CR 709.5j, CR 603.2
846
+
847
+ | Parameter | Description |
848
+ | --- | --- |
849
+ | `door` | left | right | any (set on a door's own trigger) |
850
+ | `fully` | true |
851
+ | `subject` | "self" (default) or {"each": filter, "controller": ...} |
852
+
853
+ ```json
854
+ {
855
+ "unlocks_door": {
856
+ "door": "any",
857
+ "fully": true,
858
+ "subject": {
859
+ "each": "permanent",
860
+ "controller": "you"
861
+ }
862
+ }
863
+ }
864
+ ```
865
+
786
866
  ## waterbends
787
867
 
788
868
  A player (`controller`: you (default) | opponent | any) waterbends.
@@ -18,6 +18,26 @@ Rules: CR 301.1
18
18
  }
19
19
  ```
20
20
 
21
+ ## battle
22
+
23
+ A permanent with a printed `defense`: it enters with that many defense counters. Each battle has a protector (a Siege's is an opponent of its controller, chosen as it enters); the protector's opponents can attack it and the protector can block for it. Damage dealt to it removes defense counters; with none left it's put into its owner's graveyard (unless it's the source of a triggered ability still waiting or on the stack). Every battle needs a battle type: siege (CR 205.3q).
24
+
25
+ Rules: CR 310.1, CR 310.4, CR 310.5, CR 310.6, CR 310.7, CR 310.9, CR 120.3h, CR 704.5v, CR 205.3q
26
+
27
+ ```json
28
+ {
29
+ "name": "Example Invasion",
30
+ "mana_cost": "{2}{R}",
31
+ "types": [
32
+ "battle"
33
+ ],
34
+ "subtypes": [
35
+ "siege"
36
+ ],
37
+ "defense": 4
38
+ }
39
+ ```
40
+
21
41
  ## creature
22
42
 
23
43
  A permanent with power and toughness that can attack and block. Cast at sorcery speed. It can't attack or use {T} abilities unless controlled continuously since its controller's most recent turn began (summoning sickness).
@@ -38,7 +38,9 @@ module Enjine
38
38
  # The play_land frame: ask the "as this enters" choice, then put the land onto the battlefield.
39
39
  def self.step(engine, frame)
40
40
  land = engine.candidate_object(frame["player"], frame["object"])
41
- return engine.ask_enter_choice!(frame["object"], frame["player"]) if engine.entering_choice_pending?(land, frame["enter"])
41
+ if engine.entering_choice_pending?(land, frame["enter"])
42
+ return engine.ask_enter_choice!(frame["object"], frame["player"], frame["enter"])
43
+ end
42
44
 
43
45
  enters = engine.enters_from_answer(land, frame)
44
46
  engine.pay_entering_costs!(frame["player"], frame)
@@ -16,8 +16,12 @@ module Enjine
16
16
  end
17
17
 
18
18
  def self.options(engine, index)
19
+ own = engine.own_abilities_only?
19
20
  engine.player(index).hand.flat_map do |id|
20
21
  obj = engine.object(id)
22
+ # no face with a special action: a quick no (Engine::Casting#printed_faces_only?)
23
+ next [] if engine.printed_faces_only?(obj, own) && !engine.card_off_board(obj.card)[:special_actions]
24
+
21
25
  engine.chars(obj).abilities.filter_map do |inst|
22
26
  ability = inst.ability
23
27
  next unless ability.respond_to?(:special!) && ability.special_allowed?(engine, obj, index)
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enjine
4
+ module Actions
5
+ # CR 116.2b, 708.7: turning a face-down permanent you control face up is a special action, taken any time you
6
+ # have priority: show its cost and pay it (a "pay" frame, which can be cancelled), then you get priority again
7
+ # (CR 116.3). `via`: "mana_cost" (a manifested or cloaked creature card, CR 701.40b, 701.58b), "morph" (CR 702.37e)
8
+ # or "disguise" (CR 702.168d).
9
+ class TurnFaceUp
10
+ def self.doc
11
+ '`{type: "turn_face_up", object: id, via: "mana_cost" | "morph" | "disguise"}` — turn a face-down permanent you ' \
12
+ "control face up by paying its mana cost (a manifested or cloaked creature card) or its morph or disguise " \
13
+ "cost (a special action, any time you have priority, CR 116.2b)."
14
+ end
15
+
16
+ def self.options(engine, index) = engine.face_up_options(index)
17
+
18
+ def self.perform(engine, index, option)
19
+ cost = engine.face_up_costs(engine.object(option[:object])).fetch(option[:via])
20
+ engine.begin_payment!(index, cost, option[:object], special: { "object" => option[:object], "face_up" => option[:via] })
21
+ end
22
+ end
23
+ register("turn_face_up", TurnFaceUp)
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enjine
4
+ module Actions
5
+ # CR 116.2m, 709.5e: unlocking a locked door of a Room permanent you control is a special action, taken any time
6
+ # you have priority and the stack is empty during a main phase of your turn: you pay that door's mana cost (its
7
+ # unlock cost) in a "pay" frame, which can be cancelled, then get priority again (CR 116.3).
8
+ class Unlock
9
+ def self.doc
10
+ '`{type: "unlock", object: id, door: "left" | "right"}` — unlock a locked door of a Room you control by paying ' \
11
+ "that door's mana cost (a special action, during a main phase of your turn with an empty stack, CR 116.2m, 709.5e)."
12
+ end
13
+
14
+ def self.options(engine, index) = engine.unlock_options(index)
15
+
16
+ def self.perform(engine, index, option)
17
+ engine.begin_payment!(index, engine.door_cost(engine.object(option[:object]), option[:door]), option[:object],
18
+ special: { "object" => option[:object], "unlock" => option[:door] })
19
+ end
20
+ end
21
+ register("unlock", Unlock)
22
+ end
23
+ end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "card_faces"
4
+ require_relative "card_rooms"
5
+ require_relative "card_face_down"
4
6
 
5
7
  module Enjine
6
8
  # Immutable, validated blueprint of a card, built from a data block.
@@ -11,7 +13,7 @@ module Enjine
11
13
  # Enjine.registry. Anything unknown is reported (with its path) in a single
12
14
  # InvalidCardDefinition error.
13
15
  class CardDefinition
14
- KEYS = %w[name mana_cost colors types supertypes subtypes keywords power toughness loyalty abilities rules_text deck_limit
16
+ KEYS = %w[name mana_cost colors types supertypes subtypes keywords power toughness loyalty defense abilities rules_text deck_limit
15
17
  color_identity].freeze
16
18
  # Optional catalogue data beside the rules data, never read by the rules: rarity, and the set code and
17
19
  # collector number printed on the card ({"set" => "trk", "number" => "12"}).
@@ -19,7 +21,7 @@ module Enjine
19
21
  SUBTYPE_FORMAT = /\A[a-z][a-z0-9_]*\z/
20
22
  include Faces
21
23
 
22
- attr_reader :id, :name, :mana_cost, :types, :supertypes, :subtypes, :keywords, :power, :toughness, :loyalty,
24
+ attr_reader :id, :name, :mana_cost, :types, :supertypes, :subtypes, :keywords, :power, :toughness, :loyalty, :defense,
23
25
  :abilities, :rules_text, :type_modules, :source, :colors, :power_amount, :toughness_amount, :rarity, :printing
24
26
 
25
27
  # Canonical id derived from a card name: "Jace, the Mind Sculptor" -> "jace_the_mind_sculptor".
@@ -65,26 +67,30 @@ module Enjine
65
67
  @source = deep_freeze(@source.merge("rarity" => @rarity, "printing" => @printing).compact) if @rarity || @printing
66
68
  @printed_characteristics = Characteristics.printed(self) if errors.empty?
67
69
  @color_identity = ColorIdentity.of(self) if face.nil? && errors.empty? # a face has its card's (see #color_identity)
70
+ precompute_texts if errors.empty?
68
71
  freeze
69
72
  end
70
73
 
71
74
  def read_card(card, errors, token_id, face = nil)
72
75
  (card.keys - KEYS).each { |k| errors << "#{k}: unknown field (allowed: #{(KEYS + PRINT_KEYS).join(", ")})" }
73
- @name = read_name(card, errors)
76
+ blank = (face && Rooms.locked_face?(face, card)) || FaceDownCards.id?(token_id) # no name (CR 709.5, 708.2)
77
+ @name = blank ? "" : read_name(card, errors)
74
78
  @id = face ? "#{face[0].id}#{FACE_SEPARATOR}#{face[1]}" : token_id || self.class.slug(@name)
75
79
  @front, @face_index = face
76
- @token = !token_id.nil?
80
+ @token = !token_id.nil? && !FaceDownCards.id?(token_id)
77
81
  @types, @type_modules = read_names(card, "types", :type, errors, required: true)
78
82
  @supertypes, = read_names(card, "supertypes", :supertype, errors)
79
83
  @subtypes = read_subtypes(card, errors)
80
84
  @mana_cost = card.key?("mana_cost") ? Mana.parse_cost(card["mana_cost"], errors) : nil
81
85
  @keywords, keyword_modules = read_names(card, "keywords", :keyword, errors)
86
+ @keyword_instances = read_keyword_instances(card, keyword_modules)
82
87
  @colors = read_colors(card, errors)
83
88
  @deck_limit = read_deck_limit(card, errors)
84
89
  @declared_color_identity = read_color_identity(card, errors)
85
90
  @power, @power_amount = read_pt(card, "power", errors)
86
91
  @toughness, @toughness_amount = read_pt(card, "toughness", errors)
87
92
  @loyalty = read_stat(card, "loyalty", errors)
93
+ @defense = read_stat(card, "defense", errors) # CR 310.4
88
94
  @rules_text = card["rules_text"]
89
95
  errors << "rules_text: must be a string" unless @rules_text.nil? || @rules_text.is_a?(String)
90
96
  @abilities = read_abilities(card, errors)
@@ -168,6 +174,8 @@ module Enjine
168
174
  # This face has an ability of its own to be played from outside its owner's hand: "you may cast this card
169
175
  # from your graveyard" (CR 113.6e), beam me up (CR 702.196a) or an alternative cost (Engine::Permissions).
170
176
  def plays_itself? = @plays_itself
177
+ # This face has an ability that is a special action from the hand (foretell, suspend, plot: Actions::SpecialAction).
178
+ def special_actions? = @special_actions
171
179
  # The zones this face's activated abilities function from (CR 113.6): [:battlefield], or [:hand] for cycling.
172
180
  attr_reader :activation_zones
173
181
 
@@ -195,11 +203,11 @@ module Enjine
195
203
 
196
204
  def mana_abilities = @ability_instances.map(&:ability).select(&:mana_ability?)
197
205
 
198
- def type_line
199
- left = (@supertypes + @types).map { |t| t.to_s.capitalize }.join(" ")
200
- right = @subtypes.map { |t| t.to_s.split("_").map(&:capitalize).join(" ") }.join(" ")
201
- right.empty? ? left : "#{left} — #{right}"
202
- end
206
+ # The type line, e.g. "Legendary Creature — Elf Druid" (frozen; views show it for every object).
207
+ def type_line = @type_line || build_type_line
208
+
209
+ # The mana cost as symbols ("{2}{G}"), or nil without one (frozen; views show it for every object).
210
+ def mana_cost_text = @mana_cost_text || @mana_cost&.to_s
203
211
 
204
212
  # Canonical data form; CardDefinition.from_h(defn.to_h) == defn.
205
213
  def to_h = @source
@@ -215,11 +223,24 @@ module Enjine
215
223
 
216
224
  private
217
225
 
226
+ def build_type_line
227
+ left = (@supertypes + @types).map { |t| t.to_s.capitalize }.join(" ")
228
+ right = @subtypes.map { |t| t.to_s.split("_").map(&:capitalize).join(" ") }.join(" ")
229
+ right.empty? ? left : "#{left} — #{right}"
230
+ end
231
+
232
+ # Texts a view asks of the definition each time it shows an object: built once, as the definition is immutable.
233
+ def precompute_texts
234
+ @type_line = build_type_line.freeze
235
+ @mana_cost_text = @mana_cost&.to_s&.freeze
236
+ end
237
+
218
238
  # What lets this face be used off the battlefield, from all its abilities (keyword abilities and nested ones too).
219
239
  def precompute_off_board
220
240
  own = @ability_instances.map(&:ability) + @nested_instances.map(&:ability)
221
241
  @plays_itself = own.any? { CardDefinition.self_play?(it) }
222
242
  @activation_zones = own.filter_map { it.zone if %i[activated loyalty].include?(it.kind) }.uniq.freeze
243
+ @special_actions = own.any? { it.respond_to?(:special!) }
223
244
  end
224
245
 
225
246
  def precompute_predicates
@@ -258,6 +279,7 @@ module Enjine
258
279
  errors << "power/toughness: only creatures and Vehicles have power and toughness"
259
280
  end
260
281
  errors << "loyalty: only planeswalkers have loyalty" if !planeswalker? && @loyalty
282
+ errors << "defense: only battles have defense" if !type?(:battle) && @defense # CR 310.4
261
283
  @abilities.each_with_index do |ability, i|
262
284
  allowed = ability.allowed_types
263
285
  next if allowed.nil? || allowed.any? { type?(it) }
@@ -332,10 +354,11 @@ module Enjine
332
354
  end.uniq.freeze
333
355
  end
334
356
 
335
- # CR 202.2: a card's colors come from its mana cost, unless it has a color indicator (CR 204) or is a
336
- # token whose creating effect gives its colors (CR 111.4): `"colors": ["G"]` (`[]` for colorless).
357
+ # CR 202.2: a card's colors come from its mana cost (a hybrid symbol is each of its colors, CR 202.2d), unless it
358
+ # has a color indicator (CR 204) or is a token whose creating effect gives its colors (CR 111.4): `"colors": ["G"]`
359
+ # (`[]` for colorless).
337
360
  def read_colors(card, errors)
338
- return (@mana_cost.is_a?(Mana::Cost) ? Mana::COLORS.select { @mana_cost.colored.key?(it) } : []).freeze unless card.key?("colors")
361
+ return (@mana_cost.is_a?(Mana::Cost) ? @mana_cost.colors : []).freeze unless card.key?("colors")
339
362
 
340
363
  list = card["colors"]
341
364
  ok = list.is_a?(Array) && list.all? { Mana::COLORS.include?(it.to_s.upcase.to_sym) }
@@ -424,8 +447,23 @@ module Enjine
424
447
  end
425
448
  end
426
449
 
427
- # Keyword abilities that stand for an ability ("Station" is an activated ability, CR 702.184a).
428
- def keyword_abilities = @keywords.flat_map { CardDefinition.keyword_instances(it) }
450
+ # Keyword abilities that stand for an ability ("Station" is an activated ability, CR 702.184a). A keyword whose
451
+ # instances each trigger separately (cascade, CR 702.85c) brings its abilities once per instance listed.
452
+ def keyword_abilities
453
+ @keywords.flat_map { |kw| CardDefinition.keyword_instances(kw) * @keyword_instances.fetch(kw, 1) }
454
+ end
455
+
456
+ # How many times each keyword is listed, for the keywords where several instances aren't redundant
457
+ # (`multiple_instances?`: "each instance triggers separately", CR 702.40b, 702.85c, 702.101b, 702.191b). Any
458
+ # other keyword listed twice is one instance (redundant, as in CR 702.2f).
459
+ def read_keyword_instances(card, modules)
460
+ list = card.fetch("keywords", [])
461
+ return {}.freeze unless list.is_a?(Array)
462
+
463
+ repeatable = modules.select { it.respond_to?(:multiple_instances?) && it.multiple_instances? }.map { it.primitive_name.to_sym }
464
+ counts = list.map { it.to_s.downcase.tr(" -", "__").to_sym }.tally
465
+ counts.select { |kw, n| n > 1 && repeatable.include?(kw) }.freeze
466
+ end
429
467
 
430
468
  # Abilities nested in another ability (a station symbol's, CR 722.2), referenced as
431
469
  # "<parent ref>:<j>". They're part of the object's characteristics only while the parent applies.
@@ -444,9 +482,10 @@ module Enjine
444
482
  out["types"] = @types.map(&:to_s)
445
483
  out["supertypes"] = @supertypes.map(&:to_s) unless @supertypes.empty?
446
484
  out["subtypes"] = @subtypes.map(&:to_s) unless @subtypes.empty?
447
- out["keywords"] = @keywords.map(&:to_s) unless @keywords.empty?
485
+ out["keywords"] = @keywords.flat_map { [it.to_s] * @keyword_instances.fetch(it, 1) } unless @keywords.empty?
448
486
  %w[power toughness].each { |key| out[key] = normalized_stat(card, key) unless instance_variable_get("@#{key}").nil? }
449
487
  out["loyalty"] = @loyalty unless @loyalty.nil?
488
+ out["defense"] = @defense unless @defense.nil?
450
489
  out["abilities"] = Snapshot.deep_stringify(card["abilities"]) if card["abilities"].is_a?(Array) && !card["abilities"].empty?
451
490
  out["rules_text"] = @rules_text if @rules_text
452
491
  deep_freeze(out.merge!(deck_building_h))
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enjine
4
+ class CardDefinition
5
+ # The characteristics a face-down spell or permanent has (CR 708.2): a 2/2 creature with no text, no name, no
6
+ # subtypes and no mana cost (manifest, CR 701.40a; morph, CR 702.37a; CR 708.2a), or the same with ward {2}
7
+ # (cloak, CR 701.58a; disguise, CR 702.168a). They aren't cards of any catalog: every catalog knows them
8
+ # (Catalog#fetch), and a face-down object's `card` names one while the card it really is waits in its memory
9
+ # (Engine::FaceDownPermanents).
10
+ module FaceDownCards
11
+ PLAIN = "face_down_2_2"
12
+ WARD = "face_down_ward"
13
+ DATA = {
14
+ PLAIN => { "name" => "", "types" => ["creature"], "power" => 2, "toughness" => 2 },
15
+ WARD => { "name" => "", "types" => ["creature"], "power" => 2, "toughness" => 2,
16
+ "abilities" => [{ "ward" => { "cost" => [{ "mana" => "{2}" }] } }] }
17
+ }.freeze
18
+
19
+ module_function
20
+
21
+ def id?(id) = DATA.key?(id)
22
+
23
+ # The definition with that id, or nil.
24
+ def fetch(id)
25
+ return nil unless id?(id)
26
+
27
+ (@definitions ||= {})[id] ||= CardDefinition.new(DATA.fetch(id).dup, [], token_id: id)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -24,7 +24,7 @@ module Enjine
24
24
  # CardDefinition with the id "<card id>/<index>"; a game object whose `card` names a face has that
25
25
  # face up (see Engine::Faces).
26
26
  module Faces
27
- LAYOUTS = %w[transform modal adventure omen prepare meld].freeze
27
+ LAYOUTS = %w[transform modal adventure omen prepare meld room].freeze
28
28
  DOUBLE_FACED = %w[transform modal].freeze
29
29
  # Layouts whose faces[1] is a spell the card can be (CR 716.2, 721.2, 723.2).
30
30
  SPELL_FACE = { "adventure" => "adventure", "omen" => "omen", "prepare" => nil }.freeze
@@ -68,7 +68,7 @@ module Enjine
68
68
  def other_face = faces[1 - face_index]
69
69
 
70
70
  # "Alfa // Bravo" for a card with faces.
71
- def full_name = faces.map(&:name).join(" // ")
71
+ def full_name = (layout == "room" ? faces[1..2] : faces).map(&:name).join(" // ")
72
72
 
73
73
  private
74
74
 
@@ -86,6 +86,8 @@ module Enjine
86
86
  list << {} while list.size < 2
87
87
  end
88
88
  front, back = list.map { it.transform_keys(&:to_s) }
89
+ return read_room(card, front, back, errors, token_id) if @layout == "room" # CardDefinition::Rooms
90
+
89
91
  read_card(front, prefixed(errors, "faces[0]"), token_id)
90
92
  @faces = [self, CardDefinition.new(back, prefixed(errors, "faces[1]"), face: [self, 1])].freeze
91
93
  validate_faces(card, errors)
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enjine
4
+ class CardDefinition
5
+ # Room cards: split cards with a shared type line (CR 709.5), written as
6
+ #
7
+ # {"name": "Left // Right", "layout": "room", "faces": [{...left door...}, {...right door...}]}
8
+ #
9
+ # where each door is a card block with its own name, mana cost and abilities and the same types, supertypes
10
+ # and subtypes (the shared type line, CR 709.5a). The card's own definition (faces[0]) has both halves
11
+ # combined: every zone but the stack sees that (CR 709.4, 709.4b: the mana costs combined). The other faces
12
+ # stand for the permanent's unlocked designations (CR 709.5, 709.5c): faces[1] only the left door unlocked
13
+ # (only its name, mana cost and text), faces[2] only the right door, faces[3] both doors locked (no name, mana
14
+ # cost or text, only the type line). With both doors unlocked a Room permanent is the combined card. A door
15
+ # cast as a spell has only that door (CR 709.3b), so the spell is faces[1] or faces[2] (Engine::Rooms).
16
+ #
17
+ # "When you unlock this door" (trigger `unlocks_door`) written on a door without `door` is about that door:
18
+ # it is tagged "left" or "right" as the card is read.
19
+ module Rooms
20
+ DOORS = %w[left right].freeze
21
+ LOCKED_FACE = 3
22
+
23
+ module_function
24
+
25
+ # The fully locked face (CR 709.5: neither unlocked designation): no name.
26
+ def locked_face?(face, card) = face[1] == LOCKED_FACE && face[0].layout == "room" && card["name"] == ""
27
+
28
+ # "When you unlock this door": the door's own unlock triggers name it.
29
+ def tag_doors(value, door)
30
+ case value
31
+ when Hash
32
+ value.to_h do |key, inner|
33
+ if key.to_s == "unlocks_door" && inner.is_a?(Hash) && !inner.key?("door")
34
+ [key, inner.merge("door" => door)]
35
+ else
36
+ [key, tag_doors(inner, door)]
37
+ end
38
+ end
39
+ when Array then value.map { tag_doors(it, door) }
40
+ else value
41
+ end
42
+ end
43
+
44
+ # The two halves combined (CR 709.4, 709.4b-c): both names, the mana costs added up, every ability.
45
+ def combined(left, right)
46
+ card = left.slice("types", "supertypes", "subtypes")
47
+ card["name"] = "#{left["name"]} // #{right["name"]}"
48
+ cost = [left, right].filter_map { it["mana_cost"] if it["mana_cost"].is_a?(String) }.join
49
+ card["mana_cost"] = cost unless cost.empty?
50
+ %w[keywords abilities colors].each do |key|
51
+ list = [left, right].flat_map { it[key].is_a?(Array) ? it[key] : [] }
52
+ card[key] = key == "abilities" ? list : list.uniq unless list.empty?
53
+ end
54
+ card
55
+ end
56
+
57
+ def locked(left) = left.slice("types", "supertypes", "subtypes").merge("name" => "")
58
+ end
59
+
60
+ private
61
+
62
+ def read_room(card, left, right, errors, token_id)
63
+ left, right = [left, right].zip(Rooms::DOORS).map { |door, side| Rooms.tag_doors(door, side) }
64
+ scratch = []
65
+ read_card(Rooms.combined(left, right), scratch, token_id)
66
+ door_errors = %w[faces[0] faces[1]].map { prefixed(errors, it) }
67
+ doors = [left, right].each_with_index.map { |door, i| CardDefinition.new(door, door_errors[i], face: [self, i + 1]) }
68
+ locked = CardDefinition.new(Rooms.locked(left), scratch, face: [self, Rooms::LOCKED_FACE])
69
+ @faces = [self, *doors, locked].freeze
70
+ validate_room(card, doors, errors)
71
+ scratch.each { errors << "faces: #{it}" } if errors.empty? # what only the combination gets wrong
72
+ @source = deep_freeze({ "name" => full_name, "layout" => "room", "faces" => doors.map(&:to_h) })
73
+ end
74
+
75
+ # CR 709.5, 709.5a: one shared type line, on a permanent card.
76
+ def validate_room(card, doors, errors)
77
+ name = card["name"]
78
+ errors << "name: must be #{full_name.inspect} (the doors' names joined by \" // \")" unless name.nil? || name == full_name
79
+ left, right = doors
80
+ unless %i[types supertypes subtypes].all? { left.public_send(it) == right.public_send(it) }
81
+ errors << "faces[1].types: a Room's doors share one type line (types, supertypes, subtypes, CR 709.5a)"
82
+ end
83
+ errors << "faces[0].types: a Room is a permanent card (CR 709.5)" unless left.permanent?
84
+ end
85
+ end
86
+ end