enjine 0.6.3 → 0.6.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 853cd6e6c1fa6e8ea3038049debaa947f18b6c948c595d3e546604c8a3dbc865
4
- data.tar.gz: 87ae58e7c4561df98afa3a6924214e0c3bbfd1fd30fa2242be66656fdac9d019
3
+ metadata.gz: 561a05c36e12fe1f8eca6f71d25c412c975d027bec6286ff49dce6ef94b95beb
4
+ data.tar.gz: 01470fd337d6e3c71221db45b5104a3785c00d5bff3467c585d56ea7e35726d9
5
5
  SHA512:
6
- metadata.gz: c39a7f897e7c56df2a8939af69de53453751711520f005ded7f5c94d669d08e1bddc5a51c37b35a99c02316f64feb0ced04e6dfbbea74f84ceab4d2b8852af4a
7
- data.tar.gz: 698779f724842481530bd5f474e3fbaa316c3cbdcef0aceb0e58e59b1a88c975021fa220660e89f1c1f8768a5fd4ab33aa083c0c01a0ab08edcf668095e9954f
6
+ metadata.gz: a1e1dc78d50f99ba6db78b1e037c9391c2c4dff9ef79cb52f4843c7f42bf47dc8b1c563c0c7916a0c4198fc1327b126f59ea0ad139837f50f7a3bcccc9315835
7
+ data.tar.gz: 47077067bafdfabe16fa200d3e775397fbbfe8e082a81af7c92d09e875e94bf045176feb3edf464a6614dfa98f3aa3b8ffc1a1a5804da13ba2d86ab155406844
data/CHANGELOG.md CHANGED
@@ -8,6 +8,30 @@ change the public API; the stable surface is described in
8
8
 
9
9
  ## [Unreleased]
10
10
 
11
+ ## [0.6.4] - 2026-12-04
12
+
13
+ ### Added
14
+ - `enter_as_copy` may copy a card in a graveyard: `of` takes a `graveyard_card` filter ("you may have this creature
15
+ enter as a copy of any creature card in a graveyard", CR 707.2, 614.1c), with the same `except` exceptions (CR
16
+ 707.9b), and takes `then`, effects run once it has entered as a copy: a `reflexive_trigger` for "When you do, exile
17
+ that card" (CR 603.12), where `{"triggering": "object"}` is the copied object. `{"enter_as_copy": {"of":
18
+ {"graveyard_card": {"any_of": ["creature"]}}, "except": {"name": "Superior Spider-Man", "power": 4, "toughness": 4,
19
+ "subtypes": ["spider", "human", "hero"]}, "then": [{"reflexive_trigger": {"effects": [{"exile": {"to": {"triggering":
20
+ "object"}}}]}}]}}`. `with_counters` is rejected with a graveyard filter, and `of` must name permanents or graveyard cards.
21
+ - `play_permission` takes `zone: "graveyard"`: the referenced cards may be played from a graveyard (a milled card,
22
+ "mill a card. You may play that card this turn": `{"play_permission": {"to": {"result": 0}, "zone": "graveyard"}}`,
23
+ CR 601.3). A land played this way uses the land play (CR 305.2), and the permission ends at end of turn or once the card
24
+ leaves the graveyard (CR 400.7, 514.2). It can name a card in another player's graveyard. `while_exiled` is rejected with it.
25
+ - `draw_replacement` takes `plus` (an amount) instead of `amount`: "if you would draw one or more cards, you draw that
26
+ many cards plus one instead" applies once to an instruction to draw, so "draw two" becomes three, not four
27
+ (CR 614.11, 121.2, 614.5). It also takes an `if` condition, checked as the draw would happen (CR 604.2): Quantum
28
+ Riddler is `{"draw_replacement": {"plus": 1, "if": {"hand_size": "you", "at_most": 1}}}`. With a per-card `amount`
29
+ effect too, the plus applies first, then each card is multiplied (the order used for life and counters, CR 616.1).
30
+ Draw effects, connive/recruit and the draw step now draw through one draw event (`Engine#draw_cards!`).
31
+ - `counter` and `counter_unless_pays` take `"exile_instead": true`: a spell countered this way is exiled instead of
32
+ being put into its owner's graveyard (CR 701.6a, 614.1a); if its controller pays, nothing is exiled. No More Lies is
33
+ `{"counter_unless_pays": {"mana": "{3}", "to": {"target": 0}, "exile_instead": true}}`.
34
+
11
35
  ## [0.6.3] - 2026-11-29
12
36
 
13
37
  ### Added
@@ -349,7 +373,8 @@ Includes 0.4.0, which was tagged but never published to RubyGems.
349
373
  - `enjine` CLI: hot-seat and random play, `--validate`, `--verify`,
350
374
  `--random-deck`, save and load.
351
375
 
352
- [Unreleased]: https://github.com/thisiscetin/enjine/compare/v0.6.3...HEAD
376
+ [Unreleased]: https://github.com/thisiscetin/enjine/compare/v0.6.4...HEAD
377
+ [0.6.4]: https://github.com/thisiscetin/enjine/compare/v0.6.3...v0.6.4
353
378
  [0.6.3]: https://github.com/thisiscetin/enjine/compare/v0.6.2...v0.6.3
354
379
  [0.6.2]: https://github.com/thisiscetin/enjine/compare/v0.6.1...v0.6.2
355
380
  [0.6.1]: https://github.com/thisiscetin/enjine/compare/v0.6.0...v0.6.1
data/SUPPORTED.md CHANGED
@@ -130,9 +130,9 @@ Rules version: Comprehensive Rules effective 2026-11-06.
130
130
  | [`disguise`](docs/reference/abilities.md#disguise) | 702.168a, 702.168b, 702.168c, 702.168d, 708.2, 708.4, 708.8, 116.2b | Disguise: you may cast this card face down as a nameless colorless 2/2 creature spell with ward {2} by paying {3} rather than its mana cost (cast option `via: "alt:<ref>"`). Any time you have priority you may turn the face-down permanent face up by paying `cost` (special action `{type: "turn_face_up", via: "disguise"}`). |
131
131
  | [`double_counters`](docs/reference/abilities.md#double_counters) | 614.1a, 122.6, 122.6a, 616.1 | While this permanent is on the battlefield, if its controller would put one or more counters (only `counter`, if given) on a permanent matching `affects` ({"each": filter, "controller": you|opponent|any}, relative to this permanent's controller), the number put is doubled. This includes counters a permanent enters with. |
132
132
  | [`double_tokens`](docs/reference/abilities.md#double_tokens) | 614.1a, 614.5, 111.1, 616.1 | While this permanent is on the battlefield, if tokens would be created under its controller's control, twice that many of those tokens are created instead. Several apply one after another (two make four times as many). |
133
- | [`draw_replacement`](docs/reference/abilities.md#draw_replacement) | 614.11, 614.11a, 614.5, 121.2, 616.1 | While this permanent is on the battlefield, each time its controller would draw a card, they draw `amount` cards instead (even with an empty library). The draws it makes aren't replaced by it again, but another such effect applies to each of them. |
133
+ | [`draw_replacement`](docs/reference/abilities.md#draw_replacement) | 614.11, 614.11a, 614.5, 121.2, 616.1, 604.2 | While this permanent is on the battlefield (and its `if` condition holds as the draw would happen), each time its controller would draw: with `amount`, each card they would draw becomes `amount` cards (even with an empty library; the draws it makes aren't replaced by it again, but another such effect applies to each of them); with `plus`, an instruction to draw one or more cards draws that many plus `plus` instead, once for the whole instruction ("draw two" becomes three, not four). The affected player's best order is used: adding before multiplying each card. |
134
134
  | [`enchant`](docs/reference/abilities.md#enchant) | 702.5a, 115.1b, 303.4a | The Aura's enchant ability: a target filter for what its spell targets and what the Aura may be attached to, e.g. "creature" or {"permanent": {"any_of": ["artifact", "creature"]}}. Only Auras have it. |
135
- | [`enter_as_copy`](docs/reference/abilities.md#enter_as_copy) | 707.5, 707.9b, 205.1a, 614.1c, 614.12 | As this permanent enters, its controller may choose a permanent on the battlefield matching `of`; it enters as a copy of that permanent (its copiable values, with the `except` modifications) and its enter abilities trigger. `with_counters: true`: it also enters with the counters the copied permanent has. Choosing none: it enters as itself. |
135
+ | [`enter_as_copy`](docs/reference/abilities.md#enter_as_copy) | 707.5, 707.2, 707.9b, 205.1a, 614.1c, 614.12, 603.12 | As this permanent enters, its controller may choose a permanent on the battlefield matching `of` (or, with a `graveyard_card` filter, a card in a graveyard); it enters as a copy of it (its copiable values, with the `except` modifications) and its enter abilities trigger. `with_counters: true`: it also enters with the counters the copied permanent has. `then`: effects once it entered as a copy, usually a reflexive_trigger ("When you do, exile that card"), reading {"triggering": "object"} (what it copied) and "player" (its owner). Choosing none: it enters as itself. |
136
136
  | [`enters_attacking`](docs/reference/abilities.md#enters_attacking) | 614.1c, 508.4, 506.4 | While this permanent's controller controls an attacking creature, permanents matching `affects` enter the battlefield tapped and attacking (the defending player). They never "attacked", so attack triggers don't see them. |
137
137
  | [`enters_prepared`](docs/reference/abilities.md#enters_prepared) | 723.3a, 614.1c | This permanent enters the battlefield prepared (it needs a prepare spell, see `layout: "prepare"`). |
138
138
  | [`enters_tapped`](docs/reference/abilities.md#enters_tapped) | 614.1c, 614.12 | This permanent enters the battlefield tapped (a self-replacement effect). |
@@ -303,9 +303,9 @@ Rules version: Comprehensive Rules effective 2026-11-06.
303
303
  | [`connive`](docs/reference/effects.md#connive) | 701.50a, 701.50b, 701.50d, 701.50e | Each referenced permanent (default: this one) connives `amount` (default 1): its controller draws that many cards, then discards that many, then it gets a +1/+1 counter for each nonland card discarded. |
304
304
  | [`copy_card`](docs/reference/effects.md#copy_card) | 707.12, 707.12a, 707.10a, 704.5d | Create `amount` copies of each card `of` refers to ("self": the source card), in the zone that card is in, owned by you. They are the effect's result, e.g. for `cast_free` or `cast`. A card copy ceases to exist if it is anywhere but the stack or the battlefield (so one that isn't cast now is gone at the next state-based-action check). |
305
305
  | [`copy_spell`](docs/reference/effects.md#copy_spell) | 707.10, 707.10a, 707.10b, 707.10c, 707.10f, 608.3f | Copy each spell or ability on the stack `of` refers to ("self": the spell this ability or spell belongs to; a `stack_ability` target: an activated or triggered ability) `amount` times. The copies are put on the stack under the referenced player's control (default: you) with the same modes, X, kicker and targets; they aren't cast or activated, and an ability's copy has the same source. With `new_targets: true` the controller may choose new targets for each copy. A copy of a permanent spell becomes a token as it resolves. `except`: the copies have these differences (e.g. {"keywords": ["wither"]}: "the copy gains wither"). |
306
- | [`counter`](docs/reference/effects.md#counter) | 701.6a, 701.6b, 101.2 | Counter each referenced spell or ability: a spell leaves the stack for its owner's graveyard without resolving, an ability ceases to exist. A spell that can't be countered stays. |
306
+ | [`counter`](docs/reference/effects.md#counter) | 701.6a, 701.6b, 101.2, 614.1a | Counter each referenced spell or ability: a spell leaves the stack for its owner's graveyard (for exile with `exile_instead: true`) without resolving, an ability ceases to exist. A spell that can't be countered stays. |
307
307
  | [`counter_bonus`](docs/reference/effects.md#counter_bonus) | 614.1a, 614.16, 611.2a, 122.6, 616.1 | Until end of turn, whenever one or more counters would be put on a permanent the controller controls (including counters it enters with), that many plus `amount` of each of those kinds are put on it instead. |
308
- | [`counter_unless_pays`](docs/reference/effects.md#counter_unless_pays) | 701.6a, 118.12a, 605.3a, 701.67a | Counter each referenced spell or ability unless its controller pays `mana` (or `waterbend`s that cost, CR 701.67a, or pays the list of costs `cost`) as this resolves (they choose, and pay like any cost; mana abilities allowed). |
308
+ | [`counter_unless_pays`](docs/reference/effects.md#counter_unless_pays) | 701.6a, 118.12a, 605.3a, 701.67a, 608.2b, 614.1a | Counter each referenced spell or ability unless its controller pays `mana` (or `waterbend`s that cost, CR 701.67a, or pays the list of costs `cost`) as this resolves (they choose, and pay like any cost; mana abilities allowed). With `exile_instead: true`, a spell countered this way is exiled instead of being put into its owner's graveyard; if they pay, nothing happens to it. |
309
309
  | [`create_emblem`](docs/reference/effects.md#create_emblem) | 114.1, 114.2, 114.3, 114.4, 113.6p | Each referenced player (default you) gets an emblem with the `abilities` (triggered, static or player_rule abilities). An emblem is in the command zone for the rest of the game; its abilities work there, and "this emblem" is the source of what they do. |
310
310
  | [`create_token`](docs/reference/effects.md#create_token) | 111.1, 111.10, 701.7a, 704.5d, 508.4, 506.3b, 707.2 | Create `amount` tokens under the referenced player's control (default: you). `token` is a predefined token name (Treasure, Clue, Food) or an inline card definition. `tapped: true` creates them tapped; `attacking: true` attacking (only for the attacking player during combat, CR 506.3b); when the defending player controls a planeswalker, the controller chooses for each token whether it attacks that player or one of their planeswalkers (CR 508.4). `size` (an amount, fixed as the effect starts) makes them N/N ("an X/X Spirit token, where X is ..."; part of their copiable values). |
311
311
  | [`create_token_copy`](docs/reference/effects.md#create_token_copy) | 707.2, 707.8, 707.9a, 707.9b, 205.1a, 205.1b, 111.1, 614.1c | For each object `of` refers to (a permanent, a card in any zone, or the source with "self"), the referenced players (default: you) each create `amount` tokens that are copies of it: its copiable values (its card as modified by other copy effects, not counters, status or other effects), with the `except` modifications. The tokens are the effect's result, for later effects such as granting haste or a delayed sacrifice. |
@@ -350,7 +350,7 @@ Rules version: Comprehensive Rules effective 2026-11-06.
350
350
  | [`mill`](docs/reference/effects.md#mill) | 701.17a | Each referenced player puts the top `amount` cards of their library into their graveyard (default: you). |
351
351
  | [`pay`](docs/reference/effects.md#pay) | 118.12, 118.12a, 118.3, 119.4, 605.3a, 701.67a, 701.67c | You may pay `cost` (a list of costs: mana, life, discarding, sacrificing, a `one_of` choice, ...) as it resolves; `mana`, `life` and `waterbend` are shorthands for one cost. If you do, `then` happens; if you don't, `otherwise`. Paying mana works like paying a spell's cost (mana abilities may be activated; a `waterbend` cost may also be paid by tapping artifacts and creatures, CR 701.67a); a cost that can't be paid can't be chosen (CR 118.3). For another player paying, see `counter_unless_pays`. |
352
352
  | [`phase_out`](docs/reference/effects.md#phase_out) | 702.26a, 702.26b, 702.26d, 702.26g, 702.26h, 502.1, 506.4 | Each referenced permanent phases out, with the Auras and Equipment attached to it (indirectly). It phases in during the untap step of the player who controlled it as it phased out. Phasing isn't a zone change: no enters or leaves triggers. |
353
- | [`play_permission`](docs/reference/effects.md#play_permission) | 611.2a, 611.2b, 601.3, 305.1, 400.7, 514.2, 601.2f, 108.3, 406.3b, 609.4b, 118.9, 701.67a | A player may play (`play: "play"`, default) or cast (`"cast"`) each referenced exiled card matching `for`/`not_types`, paying its costs and following timing rules, for `duration`: end_of_turn (default), end_of_your_next_turn, until_your_next_end_step, while_exiled or while_you_control_source ("for as long as you control this creature"). With `choose: "one_per_owner"` only one chosen card per owner. `to_player`: who may play it, "controller" (default, the effect's controller), "owner" (the card's owner) or a player selector (a face-down card: only someone who may look at it). `cost_increase`: spells cast this way cost that much generic mana more. `any_mana_type: true`: mana of any type can be spent to cast those spells. `during: "your_turn"`: only during your turn. `waterbend: "mana_value"`: cast it by waterbending {X} rather than paying its mana cost, where X is its mana value. |
353
+ | [`play_permission`](docs/reference/effects.md#play_permission) | 611.2a, 611.2b, 601.3, 305.1, 305.2, 400.7, 514.2, 601.2f, 108.3, 406.3b, 609.4b, 118.9, 701.67a | A player may play (`play: "play"`, default) or cast (`"cast"`) each referenced exiled card (`zone: "graveyard"`: card in a graveyard) matching `for`/`not_types`, paying its costs and following timing rules (a land uses a land play), for `duration`: end_of_turn (default), end_of_your_next_turn, until_your_next_end_step, while_exiled or while_you_control_source ("for as long as you control this creature"). With `choose: "one_per_owner"` only one chosen card per owner. `to_player`: who may play it, "controller" (default, the effect's controller), "owner" (the card's owner) or a player selector (a face-down card: only someone who may look at it). `cost_increase`: spells cast this way cost that much generic mana more. `any_mana_type: true`: mana of any type can be spent to cast those spells. `during: "your_turn"`: only during your turn. `waterbend: "mana_value"`: cast it by waterbending {X} rather than paying its mana cost, where X is its mana value. |
354
354
  | [`player_rule`](docs/reference/effects.md#player_rule) | 702.11c, 115.4, 508.1c, 402.2, 514.1, 615.1, 615.1a, 704.5a, 104.3b, 701.21a, 614.17b, 101.2, 601.3, 701.6a, 119.7, 602.5, 615.12, 702.16b, 702.16e, 702.16k, 506.3, 611.2a, 514.2 | Each referenced player (default you) gets `rule` for the `duration`: end_of_turn (default) or until_your_next_turn (until the controller's next turn begins). Rules: hexproof, cant_attack, no_max_hand_size, prevent_all_damage, cant_lose_for_life, cant_cast, cant_cast_except_from_hand, spells_cant_be_countered, abilities_cant_be_countered, cant_gain_life, cant_activate, damage_cant_be_prevented, combat_damage_cant_be_prevented, protection_from_opponents. |
355
355
  | [`plot`](docs/reference/effects.md#plot) | 702.170c, 702.170d, 406.1 | Each referenced card or spell is exiled (if it isn't already) and becomes plotted: its owner may cast it from exile without paying its mana cost during their main phase while the stack is empty, on any later turn (cast option `via: "plotted"`). |
356
356
  | [`prepare`](docs/reference/effects.md#prepare) | 723.3a, 723.3c | Each referenced permanent (default: this one) becomes prepared, if it has a prepare spell and isn't prepared already: its controller creates a copy of the prepare spell in exile and may cast that copy while the permanent stays prepared. Casting it unprepares the permanent. |
@@ -397,7 +397,7 @@ See `docs/reference/effects.md` for the generated list. Currently:
397
397
  | `lose_life` | "each opponent loses N life" | `{ "lose_life": { "amount": N, "to": "each_opponent" } }` |
398
398
  | `draw` | "draw N cards" | `{ "draw": { "amount": N } }` (`to` defaults to `"you"`) |
399
399
  | `destroy` | "destroy target …" | `{ "destroy": { "to": … } }` |
400
- | `counter` | "counter target spell" | `{ "counter": { "to": { "target": 0 } } }` |
400
+ | `counter` | "counter target spell" | `{ "counter": { "to": { "target": 0 } } }` (`"exile_instead": true`: "if that spell is countered this way, exile it instead of putting it into its owner's graveyard") |
401
401
 
402
402
  ### 4.7 Mana effects, X and cost changes
403
403
 
@@ -577,6 +577,17 @@ equips for a non-mana cost, and granted activated abilities ("equipped creature
577
577
  in exile). `not_types: ["land"]` keeps only the nonland card an exile-until found;
578
578
  `"choose": "one_per_owner"` with `"for": { "types": ["land"] }` makes the controller
579
579
  pick one matching card per owner among the exiled ones.
580
+ - **Playing a milled card** (CR 601.3, 305.2, 400.7): `"zone": "graveyard"` on
581
+ `play_permission` grants it for the referenced cards in a graveyard instead of exile
582
+ (any duration but `while_exiled`). A land played this way uses the turn's land play; the
583
+ permission ends at end of turn (or its `duration`) and as soon as the card leaves the
584
+ graveyard, a spell cast this way included. "When this artifact enters, mill a card. You
585
+ may play that card this turn":
586
+ ```json
587
+ { "triggered": { "when": { "enters": {} }, "effects": [
588
+ { "mill": { "amount": 1 } },
589
+ { "play_permission": { "to": { "result": 0 }, "zone": "graveyard" } } ] } }
590
+ ```
580
591
  - **Exiled face down** (CR 406.3): `"face_down": true` on `exile_top`; `looker` names the
581
592
  players who may look at the cards (default: you). A face-down card has no
582
593
  characteristics, its id doesn't name it (`face_down#12`), and views and events show
@@ -733,6 +744,7 @@ types; other subtypes (Equipment, Vehicle) stay unless `types` is set too.
733
744
  | "That token gains haste. Sacrifice it at the beginning of the next end step." | `grant` and `delayed_trigger` with `{ "result": n }` (the tokens are the effect's result) |
734
745
  | "exile target creature card from your graveyard, create a token that's a copy of it" | `exile`, then `{ "create_token_copy": { "of": { "result": 0 } } }` |
735
746
  | "you may have this creature enter as a copy of any artifact or creature on the battlefield …, with the same counters" | `{ "enter_as_copy": { "of": { "permanent": { "any_of": ["artifact", "creature"] } }, "except": { "legendary": false }, "with_counters": true } }` |
747
+ | "you may have this creature enter as a copy of any creature card in a graveyard, except … When you do, exile that card" | `{ "enter_as_copy": { "of": { "graveyard_card": { "any_of": ["creature"] } }, "except": { … }, "then": [{ "reflexive_trigger": { "effects": [{ "exile": { "to": { "triggering": "object" } } }] } }] } }` (CR 707.2, 603.12; no `with_counters`) |
736
748
  | "… becomes a copy of target Equipment until end of turn" | `{ "become_copy": { "to": …, "of": { "target": 0 } } }` (`"duration": "permanent"` while it stays) |
737
749
  | "When you cast this spell, copy it." | a `{ "cast": { "self": true } }` trigger with `{ "copy_spell": { "of": "self" } }` |
738
750
  | "copy target instant or sorcery spell; you may choose new targets for the copy" | `{ "copy_spell": { "of": { "target": 0 }, "new_targets": true } }` |
@@ -743,6 +755,20 @@ types; other subtypes (Equipment, Vehicle) stay unless `types` is set too.
743
755
  | "activate only if this card is exiled" | `"zone": "exile"` on the activated ability |
744
756
  | "Changeling (This object is every creature type.)" | `"keywords": ["changeling"]` |
745
757
 
758
+ `enter_as_copy` copies a permanent on the battlefield, or with a `graveyard_card` filter in
759
+ `of` a card in a graveyard (its copiable values are the card's, CR 707.2). Its `then` list
760
+ runs once the permanent has entered as a copy, so "When you do, …" is a `reflexive_trigger`
761
+ there (CR 603.12). `{"triggering": "object"}` is what it copied and `"player"` is that object's owner. Declining
762
+ the copy runs nothing. Superior Spider-Man ("… except his name is Superior Spider-Man and he's a
763
+ 4/4 Spider Human Hero in addition to his other types. When you do, exile that card"):
764
+
765
+ ```json
766
+ { "enter_as_copy": { "of": { "graveyard_card": { "any_of": ["creature"] } },
767
+ "except": { "name": "Superior Spider-Man", "power": 4, "toughness": 4,
768
+ "subtypes": ["spider", "human", "hero"] },
769
+ "then": [{ "reflexive_trigger": { "effects": [{ "exile": { "to": { "triggering": "object" } } }] } }] } }
770
+ ```
771
+
746
772
  A token copy is a token (the filter `"token": true` matches it). A copy of a permanent
747
773
  spell becomes a token as it resolves (CR 608.3f). A spell copy that leaves the stack, or
748
774
  a card copy outside the stack and battlefield, ceases to exist (CR 707.10a).
@@ -1193,6 +1219,8 @@ opponent or a permanent an opponent controls". `combat` is `"combat"` or `"nonco
1193
1219
  | "After this phase, there is an additional combat phase [followed by an additional main phase]" | `{ "additional_phases": { "phases": ["combat", "main"] } }`; "if it's the first combat phase of the turn" is `{ "first_combat_phase": true }` |
1194
1220
  | "If a creature an opponent controls would die, exile it instead" | `{ "exile_if_dies": { "affects": { "each": "creature", "controller": "opponent" } } }` |
1195
1221
  | "If one or more tokens would be created under your control, twice that many ... instead" | `"double_tokens"` |
1222
+ | "If you would draw a card, draw two cards instead" (CR 614.11) | `{ "draw_replacement": { "amount": 2 } }` (each card drawn becomes two) |
1223
+ | "As long as you have one or fewer cards in hand, if you would draw one or more cards, you draw that many cards plus one instead" (CR 614.11, 121.2) | `{ "draw_replacement": { "plus": 1, "if": { "hand_size": "you", "at_most": 1 } } }`: applies once to the whole instruction ("draw two" becomes three), the condition checked as the draw would happen; `if` takes any condition (also with `amount`). With a per-card `amount` effect too, the plus applies first, then each card is multiplied |
1196
1224
 
1197
1225
  ```json
1198
1226
  { "name": "Example Pyre Warden", "mana_cost": "{2}{R}{W}", "types": ["creature"], "subtypes": ["human", "wizard"],
@@ -1249,6 +1277,7 @@ its seed and decisions, and an exported game continues with the same results.
1249
1277
  | "You may discard your hand. If you do, draw …" / "… Draw X cards, where X is the number of cards discarded this way" | `{ "may": { "effects": [{ "discard": { "hand": true } }], "then": [ … ] } }`; X is `{ "this_way": n }` of that effect (n: its position in the list) |
1250
1278
  | "Noncreature spells you cast have improvise." (CR 702.126a) | `{ "spells_have": { "spells": { "not_types": ["creature"] }, "keywords": ["improvise"] } }` (convoke too) |
1251
1279
  | "Counter target spell unless its controller pays {2}. If …, {4} instead" | two `counter_unless_pays` with opposite `if` conditions (`cost` takes a list of costs too) |
1280
+ | "Counter target spell unless its controller pays {3}. If that spell is countered this way, exile it instead of putting it into its owner's graveyard." (CR 701.6a, 614.1a) | `{ "counter_unless_pays": { "mana": "{3}", "to": { "target": 0 }, "exile_instead": true } }` (if they pay, nothing is exiled) |
1252
1281
 
1253
1282
  Which `one_of` option is paid is announced while casting or activating, after modes and before X
1254
1283
  (the `choose_cost_option` decision, CR 601.2b); only options that could be paid are offered, and a
@@ -234,7 +234,7 @@ Keyword breakdown: plot 7, craft 4, Case / suspect 3, leyline / opening hand 3,
234
234
  | Omniscience | FAIL | 'You may cast spells from your hand without paying their mana costs' as a static permission: no free-cast static ability (cast_free only works while an effect resolves) |
235
235
  | Radagast of Rhosgobel | FAIL | 'The first creature spell you cast each turn costs {2} less and can be cast as though it had flash': cost_modifier has no first-spell-each-turn limit and there is no flash-granting permission for spells |
236
236
  | Raphael, Most Attitude | FAIL | 'you may play a card exiled with Raphael' (a single card) — play_permission can't limit the permission to one card among the exiled ones |
237
- | Tablet of Discovery | FAIL | "You may play that card this turn" for a milled card: play_permission only works for cards in exile, not the graveyard |
237
+ | Tablet of Discovery | FAIL | "You may play that card this turn" for a milled card: play_permission only works for cards in exile, not the graveyard (**done, Unreleased**: `play_permission` `zone: "graveyard"`) |
238
238
  | Timeline Culler | PARTIAL | 'You may cast this card from your graveyard using its warp ability' is left out: warp works from the hand only |
239
239
  | Tinybones, Bauble Burglar | PARTIAL | the play permission for stash cards isn't limited to your turn and doesn't end when Tinybones leaves (play_permission while_exiled per card, not a static ability of Tinybones) |
240
240
 
@@ -438,8 +438,8 @@ Keyword breakdown: plot 7, craft 4, Case / suspect 3, leyline / opening hand 3,
438
438
  | Bloodletter of Aclazotz | PARTIAL | the life-loss doubling is approximated: at the beginning of your upkeep (and when it enters during your turn) damage dealt to an opponent is doubled until end of turn. Non-damage life loss isn't doubled, and the doubling lasts the rest of the turn even if Bloodletter leaves |
439
439
  | Draconic Visitor | FAIL | token_replacement replaces every token; no filter for only artifact tokens ('If one or more artifact tokens would be created') |
440
440
  | Leyline of the Void | PARTIAL | the opening-hand clause, and the replacement covers only opponents' creatures dying (exile_if_dies) instead of every card going to an opponent's graveyard from anywhere |
441
- | No More Lies | FAIL | counter_unless_pays can't exile the countered spell instead of putting it into the graveyard |
442
- | Quantum Riddler | FAIL | conditional draw replacement 'draw that many plus one' (draw_replacement only multiplies per card, no condition) |
441
+ | No More Lies | FAIL | counter_unless_pays can't exile the countered spell instead of putting it into the graveyard (**done, Unreleased**: `exile_instead`) |
442
+ | Quantum Riddler | FAIL | conditional draw replacement 'draw that many plus one' (draw_replacement only multiplies per card, no condition) (**done, Unreleased**: `plus` and `if`) |
443
443
  | Rest in Peace | FAIL | Replacement 'if a card or token would be put into a graveyard from anywhere, exile it instead' (exile_if_dies covers only permanents dying) |
444
444
  | Wolverine, Fierce Fighter | FAIL | Damage replacement 'that damage is dealt, but all other damage already dealt to him is healed' (damage_replacement has no heal-prior-damage option) |
445
445
  | Zhao, the Moon Slayer | PARTIAL | 'Nonbasic lands enter tapped' (no enters-tapped replacement for other permanents) and the conqueror-counter static making nonbasic lands Mountains that lose their other land types and abilities (no land-type-setting static) |
@@ -639,7 +639,7 @@ Keyword breakdown: plot 7, craft 4, Case / suspect 3, leyline / opening hand 3,
639
639
  | Ardyn, the Usurper | FAIL | token copy 'except it's a 5/5 black Demon' must replace creature types; copy `except.subtypes` only adds subtypes |
640
640
  | Face Yourself | FAIL | token copies 'except it has haste and "At the beginning of the end step, if you don't control a planeswalker, sacrifice this creature"': create_token_copy except can't add a triggered ability; also no selector for 'each creature target player controls' |
641
641
  | Mockingbird | PARTIAL | it may copy only a creature with mana value 1 or less (as if cast for X=0): the 'mana value <= amount of mana spent to cast this creature' bound can't be computed while choosing the copy (the enter_as_copy filter has no source, so mana_spent reads 0) |
642
- | Superior Spider-Man | FAIL | enter_as_copy only copies permanents on the battlefield, not a creature card in a graveyard (plus 'when you do, exile that card') |
642
+ | Superior Spider-Man | FAIL | enter_as_copy only copies permanents on the battlefield, not a creature card in a graveyard (plus 'when you do, exile that card') (**done, Unreleased**: `enter_as_copy` `graveyard_card` + `then`) |
643
643
 
644
644
  <a id="26"></a>
645
645
  ## 26. Modal (per-turn, up to X, repeat) (4)
@@ -706,13 +706,15 @@ Rules: CR 614.1a, CR 614.5, CR 111.1, CR 616.1
706
706
 
707
707
  ## draw_replacement
708
708
 
709
- While this permanent is on the battlefield, each time its controller would draw a card, they draw `amount` cards instead (even with an empty library). The draws it makes aren't replaced by it again, but another such effect applies to each of them.
709
+ While this permanent is on the battlefield (and its `if` condition holds as the draw would happen), each time its controller would draw: with `amount`, each card they would draw becomes `amount` cards (even with an empty library; the draws it makes aren't replaced by it again, but another such effect applies to each of them); with `plus`, an instruction to draw one or more cards draws that many plus `plus` instead, once for the whole instruction ("draw two" becomes three, not four). The affected player's best order is used: adding before multiplying each card.
710
710
 
711
- Rules: CR 614.11, CR 614.11a, CR 614.5, CR 121.2, CR 616.1
711
+ Rules: CR 614.11, CR 614.11a, CR 614.5, CR 121.2, CR 616.1, CR 604.2
712
712
 
713
713
  | Parameter | Description |
714
714
  | --- | --- |
715
- | `amount` | integer >= 2 |
715
+ | `amount` | integer >= 2 (each card drawn: that many instead) |
716
+ | `plus` | amount ("that many cards plus one"), instead of amount |
717
+ | `if` | condition, e.g. {"hand_size": "you", "at_most": 1} |
716
718
 
717
719
  ```json
718
720
  {
@@ -740,15 +742,16 @@ Rules: CR 702.5a, CR 115.1b, CR 303.4a
740
742
 
741
743
  ## enter_as_copy
742
744
 
743
- As this permanent enters, its controller may choose a permanent on the battlefield matching `of`; it enters as a copy of that permanent (its copiable values, with the `except` modifications) and its enter abilities trigger. `with_counters: true`: it also enters with the counters the copied permanent has. Choosing none: it enters as itself.
745
+ As this permanent enters, its controller may choose a permanent on the battlefield matching `of` (or, with a `graveyard_card` filter, a card in a graveyard); it enters as a copy of it (its copiable values, with the `except` modifications) and its enter abilities trigger. `with_counters: true`: it also enters with the counters the copied permanent has. `then`: effects once it entered as a copy, usually a reflexive_trigger ("When you do, exile that card"), reading {"triggering": "object"} (what it copied) and "player" (its owner). Choosing none: it enters as itself.
744
746
 
745
- Rules: CR 707.5, CR 707.9b, CR 205.1a, CR 614.1c, CR 614.12
747
+ Rules: CR 707.5, CR 707.2, CR 707.9b, CR 205.1a, CR 614.1c, CR 614.12, CR 603.12
746
748
 
747
749
  | Parameter | Description |
748
750
  | --- | --- |
749
- | `of` | filter (the permanents it may copy) |
751
+ | `of` | filter (the permanents, or graveyard cards, it may copy) |
750
752
  | `except` | {"legendary": false, "keywords": [...], "types": [...], "subtypes": [...], "supertypes": [...], "name": ..., "power": n, "toughness": n, "colors": [...], "replace": ["types" | "subtypes" | "supertypes"]} |
751
753
  | `with_counters` | boolean |
754
+ | `then` | list of effects (a reflexive_trigger for "When you do, ...") |
752
755
 
753
756
  ```json
754
757
  {
@@ -700,13 +700,14 @@ Rules: CR 707.10, CR 707.10a, CR 707.10b, CR 707.10c, CR 707.10f, CR 608.3f
700
700
 
701
701
  ## counter
702
702
 
703
- Counter each referenced spell or ability: a spell leaves the stack for its owner's graveyard without resolving, an ability ceases to exist. A spell that can't be countered stays.
703
+ Counter each referenced spell or ability: a spell leaves the stack for its owner's graveyard (for exile with `exile_instead: true`) without resolving, an ability ceases to exist. A spell that can't be countered stays.
704
704
 
705
- Rules: CR 701.6a, CR 701.6b, CR 101.2
705
+ Rules: CR 701.6a, CR 701.6b, CR 101.2, CR 614.1a
706
706
 
707
707
  | Parameter | Description |
708
708
  | --- | --- |
709
709
  | `to` | selector |
710
+ | `exile_instead` | boolean: "if that spell is countered this way, exile it instead" |
710
711
 
711
712
  ```json
712
713
  {
@@ -738,9 +739,9 @@ Rules: CR 614.1a, CR 614.16, CR 611.2a, CR 122.6, CR 616.1
738
739
 
739
740
  ## counter_unless_pays
740
741
 
741
- Counter each referenced spell or ability unless its controller pays `mana` (or `waterbend`s that cost, CR 701.67a, or pays the list of costs `cost`) as this resolves (they choose, and pay like any cost; mana abilities allowed).
742
+ Counter each referenced spell or ability unless its controller pays `mana` (or `waterbend`s that cost, CR 701.67a, or pays the list of costs `cost`) as this resolves (they choose, and pay like any cost; mana abilities allowed). With `exile_instead: true`, a spell countered this way is exiled instead of being put into its owner's graveyard; if they pay, nothing happens to it.
742
743
 
743
- Rules: CR 701.6a, CR 118.12a, CR 605.3a, CR 701.67a
744
+ Rules: CR 701.6a, CR 118.12a, CR 605.3a, CR 701.67a, CR 608.2b, CR 614.1a
744
745
 
745
746
  | Parameter | Description |
746
747
  | --- | --- |
@@ -748,6 +749,7 @@ Rules: CR 701.6a, CR 118.12a, CR 605.3a, CR 701.67a
748
749
  | `waterbend` | mana cost string, instead of mana |
749
750
  | `cost` | list of costs, instead of mana |
750
751
  | `to` | selector (usually {"target": 0}) |
752
+ | `exile_instead` | boolean: "if that spell is countered this way, exile it instead" |
751
753
 
752
754
  ```json
753
755
  {
@@ -1727,9 +1729,9 @@ Rules: CR 702.26a, CR 702.26b, CR 702.26d, CR 702.26g, CR 702.26h, CR 502.1, CR
1727
1729
 
1728
1730
  ## play_permission
1729
1731
 
1730
- A player may play (`play: "play"`, default) or cast (`"cast"`) each referenced exiled card matching `for`/`not_types`, paying its costs and following timing rules, for `duration`: end_of_turn (default), end_of_your_next_turn, until_your_next_end_step, while_exiled or while_you_control_source ("for as long as you control this creature"). With `choose: "one_per_owner"` only one chosen card per owner. `to_player`: who may play it, "controller" (default, the effect's controller), "owner" (the card's owner) or a player selector (a face-down card: only someone who may look at it). `cost_increase`: spells cast this way cost that much generic mana more. `any_mana_type: true`: mana of any type can be spent to cast those spells. `during: "your_turn"`: only during your turn. `waterbend: "mana_value"`: cast it by waterbending {X} rather than paying its mana cost, where X is its mana value.
1732
+ A player may play (`play: "play"`, default) or cast (`"cast"`) each referenced exiled card (`zone: "graveyard"`: card in a graveyard) matching `for`/`not_types`, paying its costs and following timing rules (a land uses a land play), for `duration`: end_of_turn (default), end_of_your_next_turn, until_your_next_end_step, while_exiled or while_you_control_source ("for as long as you control this creature"). With `choose: "one_per_owner"` only one chosen card per owner. `to_player`: who may play it, "controller" (default, the effect's controller), "owner" (the card's owner) or a player selector (a face-down card: only someone who may look at it). `cost_increase`: spells cast this way cost that much generic mana more. `any_mana_type: true`: mana of any type can be spent to cast those spells. `during: "your_turn"`: only during your turn. `waterbend: "mana_value"`: cast it by waterbending {X} rather than paying its mana cost, where X is its mana value.
1731
1733
 
1732
- Rules: CR 611.2a, CR 611.2b, CR 601.3, CR 305.1, CR 400.7, CR 514.2, CR 601.2f, CR 108.3, CR 406.3b, CR 609.4b, CR 118.9, CR 701.67a
1734
+ Rules: CR 611.2a, CR 611.2b, CR 601.3, CR 305.1, CR 305.2, CR 400.7, CR 514.2, CR 601.2f, CR 108.3, CR 406.3b, CR 609.4b, CR 118.9, CR 701.67a
1733
1735
 
1734
1736
  | Parameter | Description |
1735
1737
  | --- | --- |
@@ -1745,6 +1747,7 @@ Rules: CR 611.2a, CR 611.2b, CR 601.3, CR 305.1, CR 400.7, CR 514.2, CR 601.2f,
1745
1747
  | `exile_instead` | true or a card spec: if a spell cast this way (matching it) would be put into a graveyard, it's exiled instead (CR 614.1a) |
1746
1748
  | `during` | your_turn |
1747
1749
  | `waterbend` | mana_value |
1750
+ | `zone` | exile (default) | graveyard: where the referenced cards are |
1748
1751
 
1749
1752
  ```json
1750
1753
  {
@@ -293,8 +293,9 @@ module Enjine
293
293
 
294
294
  # CR 701.6a: a countered spell goes to its owner's graveyard; a countered ability ceases to exist.
295
295
  # "This spell can't be countered" and "Spells [and abilities] you control can't be countered" win (CR 101.2).
296
- # Returns whether it was countered.
297
- def counter!(id)
296
+ # `exile`: "If that spell is countered this way, exile it instead of putting it into its owner's graveyard"
297
+ # (CR 614.1a). Returns whether it was countered.
298
+ def counter!(id, exile: false)
298
299
  obj = object(id)
299
300
  return false if obj.ability? && player_rule?(obj.controller, "abilities_cant_be_countered")
300
301
  return false if !obj.ability? && (chars(obj).keyword?(:cant_be_countered) ||
@@ -304,7 +305,7 @@ module Enjine
304
305
  emit Events::AbilityCountered.new(object: id, card: obj.card, source: obj.source)
305
306
  remove_stack_object!(id)
306
307
  else
307
- move!(id, :graveyard, reason: "countered")
308
+ move!(id, exile ? :exile : :graveyard, reason: "countered")
308
309
  end
309
310
  true
310
311
  end
@@ -41,7 +41,7 @@ module Enjine
41
41
  # distinct card) while something lets them play those.
42
42
  def permission_candidates(index, sources = permission_sources(index))
43
43
  pl = player(index)
44
- ids = alt_cost_hand(index) + pl.graveyard + state.exile + commander_candidates(index)
44
+ ids = alt_cost_hand(index) + pl.graveyard + others_graveyard_permitted(index) + state.exile + commander_candidates(index)
45
45
  ids += [pl.library.first] unless pl.library.empty?
46
46
  return ids unless sources.any? { |_, inst| inst.ability.kind == :play_from && inst.ability.outside? }
47
47
 
@@ -113,7 +113,7 @@ module Enjine
113
113
  record_result!(obj.id)
114
114
  emit Events::ZoneChanged.new(card: card, old_id: nil, new_id: obj.id, owner: name(index), from: "outside",
115
115
  to: to.to_s, reason: reason)
116
- obj
116
+ obj.tap { copy_follow_up!(it, data["enters"]) } # "When you do, ..." of entering as a copy (Engine::Copies)
117
117
  end
118
118
 
119
119
  # CR 601.2a: a spell cast from outside the game is brought into the game onto the stack.
@@ -217,13 +217,14 @@ module Enjine
217
217
 
218
218
  # ---- where methods come from ------------------------------------------------------------
219
219
 
220
- # Permissions from resolved effects (impulse draw, CR 611.2): the card is still the exiled object. A
220
+ # Permissions from resolved effects (impulse draw, CR 611.2): the card is still the exiled object (or, for a
221
+ # graveyard permission, the object in the graveyard: "mill a card. You may play that card this turn"). A
221
222
  # face-down card only for a player who may look at it (CR 406.3b). `any_mana_type`: mana of any type
222
223
  # can be spent to cast it (CR 609.4b).
223
224
  def granted_methods(index, obj)
224
225
  state.effects.filter_map do |e|
225
226
  next unless e["kind"] == "permission" && e["controller"] == index && e["affected"].include?(obj.id)
226
- next unless obj.zone == :exile && may_look?(obj, index) && (e["during"] != "your_turn" || state.active == index)
227
+ next unless obj.zone == permission_zone(e) && may_look?(obj, index) && (e["during"] != "your_turn" || state.active == index)
227
228
 
228
229
  with_waterbend_alt(obj, { "via" => "effect:#{e["timestamp"]}", "play" => e["play"], **e.slice(*RIDERS) })
229
230
  end
@@ -293,25 +294,26 @@ module Enjine
293
294
 
294
295
  # Permission to play exiled cards for a duration (CR 611.2): a continuous effect for the exiled
295
296
  # objects (a card that leaves exile is a new object, CR 400.7, so the permission ends with it).
296
- # `riders` (RIDERS) change how a card is played with this permission, and only then (`source`: CR 611.2b).
297
+ # `riders` (RIDERS) change how a card is played with this permission, and only then (`source`: CR 611.2b);
298
+ # "zone" => "graveyard" is kept on the entry: the permission is for cards in a graveyard instead.
297
299
  def grant_play_permission!(index, ids, duration:, play:, source: nil, **riders)
298
300
  return if ids.empty?
299
301
 
300
302
  state.effects << { "kind" => "permission", "timestamp" => next_timestamp!, "controller" => index, "affected" => ids,
301
303
  "duration" => duration, "turn" => state.turn, "play" => play, "source" => source,
302
- **end_step_timing(index, duration), **riders.transform_keys(&:to_s).slice(*RIDERS) }.compact
304
+ **end_step_timing(index, duration), **riders.transform_keys(&:to_s).slice(*RIDERS, "zone") }.compact
303
305
  emit Events::EffectStarted.new(kind: "permission", affected: ids, details: { "duration" => duration, "play" => play })
304
306
  end
305
307
 
306
308
  # CR 514.2: at cleanup, an end_of_your_next_turn permission ends on its controller's next turn, and a
307
- # permission whose cards all left exile is gone. (end_of_turn ones end with the other
308
- # until-end-of-turn effects.)
309
+ # permission whose cards all left their zone (exile, or the graveyard) is gone (CR 400.7). (end_of_turn
310
+ # ones end with the other until-end-of-turn effects.)
309
311
  def expire_permissions!
310
312
  state.effects.reject! do |e|
311
313
  next false unless e["kind"] == "permission"
312
314
 
313
315
  (e["duration"] == "end_of_your_next_turn" && state.active == e["controller"] && state.turn > e["turn"]) ||
314
- e["affected"].none? { state.exile.include?(it) }
316
+ e["affected"].none? { state.objects[it]&.zone == permission_zone(e) }
315
317
  end
316
318
  end
317
319
 
@@ -113,6 +113,14 @@ module Enjine
113
113
  fresh.card = copy["card"]
114
114
  end
115
115
 
116
+ # CR 603.12: the follow-up of an "enter as a copy" ability ("When you do, exile that card"), run once `fresh`
117
+ # entered as a copy (Abilities::EnterAsCopy#enters_with). Its ability is on the card the permanent really is.
118
+ def copy_follow_up!(fresh, enters)
119
+ entry = enters && enters["copy_then"] or return
120
+ run_follow_ups!("follow_ups" => [entry.merge("card" => own_card(fresh), "controller" => fresh.controller,
121
+ "source" => fresh.id)])
122
+ end
123
+
116
124
  # CR 707.12: a copy of a card, created in the zone the card is in (owned by `controller`).
117
125
  def create_card_copy!(values, controller, zone)
118
126
  card, except = values
@@ -44,14 +44,27 @@ module Enjine
44
44
  # only lands are asked for):
45
45
  # - an exiled card `index` doesn't own can only be played by a permission effect of theirs naming it
46
46
  # (Engine::Permissions#granted_methods; every other method needs them to own the card, CR 108.3);
47
- # - while `plain`, a card in a graveyard or library only by an ability of its own (#unplayable_elsewhere?);
47
+ # - while `plain`, a card in a graveyard or library only by an ability of its own (#unplayable_elsewhere?) or a
48
+ # permission effect naming it (a milled card, `play_permission` `zone: "graveyard"`);
48
49
  # - for the land options, a card that isn't a land (seen face up, CR 406.3a).
49
50
  def unplayable_candidate?(index, obj, plain, land)
50
51
  return true if obj.zone == :exile && obj.owner != index && !permission_named_ids(index).include?(obj.id)
51
52
 
52
- (plain && unplayable_elsewhere?(obj)) || (land && !face_up(obj.id) { chars(obj).land? })
53
+ (plain && unplayable_elsewhere?(obj) && !permission_named_ids(index).include?(obj.id)) ||
54
+ (land && !face_up(obj.id) { chars(obj).land? })
53
55
  end
54
56
 
57
+ # Cards in other players' graveyards a permission effect of `index` names ("you may play that card",
58
+ # Engine::Permissions#permission_candidates).
59
+ def others_graveyard_permitted(index)
60
+ return [] if state.effects.none? { it["kind"] == "permission" && it["zone"] == "graveyard" }
61
+
62
+ permission_named_ids(index).select { (obj = state.objects[it]) && obj.zone == :graveyard && obj.owner != index }
63
+ end
64
+
65
+ # The zone a permission effect's cards are played from (CR 601.3): exile unless it says "graveyard".
66
+ def permission_zone(effect) = effect["zone"] == "graveyard" ? :graveyard : :exile
67
+
55
68
  # The ids of the objects permission effects of `index` name (Engine::Permissions#granted_methods), once per
56
69
  # decision.
57
70
  def permission_named_ids(index)
@@ -148,6 +148,7 @@ module Enjine
148
148
  prepare!(fresh) if to == :battlefield && data["enters"]["prepared"] # "enters prepared" (CR 723.3a)
149
149
  room_entered!(fresh, old) if to == :battlefield # CR 709.5d, 709.5h (Engine::Rooms)
150
150
  run_follow_ups!(data, "object" => fresh.id) # "exile it instead. When you do, ..." (Engine::FollowUps)
151
+ copy_follow_up!(fresh, data["enters"]) if to == :battlefield # "When you do, exile that card" (Engine::Copies)
151
152
  fresh
152
153
  end
153
154
 
@@ -246,6 +247,7 @@ module Enjine
246
247
  emit Events::ZoneChanged.new(card: definition.id, old_id: nil, new_id: obj.id, owner: name(controller),
247
248
  from: "none", to: "battlefield", reason: "create_token")
248
249
  prepare!(obj) if enters["prepared"] # CR 723.3a (Engine::Faces)
250
+ copy_follow_up!(obj, enters) # CR 603.12 (Engine::Copies)
249
251
  obj
250
252
  end
251
253
 
@@ -260,6 +262,14 @@ module Enjine
260
262
  definition
261
263
  end
262
264
 
265
+ # An instruction to draw `count` cards (CR 121.2): "if you would draw one or more cards" replacements apply
266
+ # once to it (Replacements.drawing_cards!, CR 614.11), then the cards are drawn one at a time.
267
+ def draw_cards!(index, count)
268
+ return unless count.positive?
269
+
270
+ propose("draw_cards", { "player" => index, "count" => count }).data["count"].times { draw!(index) }
271
+ end
272
+
263
273
  # CR 121.2: one card at a time. A replacement may turn one draw into several (CR 614.11); those
264
274
  # draws are the modified event, so the replacement that made them doesn't apply again (CR 614.5).
265
275
  def draw!(index, applied: [])
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "entering_choices"
4
4
  require_relative "../copy_except"
5
+ require_relative "../follow_up"
5
6
 
6
7
  module Enjine
7
8
  module Mechanics
@@ -9,56 +10,84 @@ module Enjine
9
10
  # "You may have this creature enter as a copy of any artifact or creature on the battlefield" (CR 707.5,
10
11
  # 614.1c, 614.12): as it would enter, its controller chooses a permanent matching `of` (or none); it
11
12
  # enters as a copy of it, with the `except` modifications, and with `with_counters: true` also with
12
- # the same number and kinds of counters the copied permanent has.
13
+ # the same number and kinds of counters the copied permanent has. `of` may name cards in a graveyard
14
+ # instead ("enter as a copy of any creature card in a graveyard", {"graveyard_card": ...}): the copy has
15
+ # the card's copiable values (CR 707.2). `then`: effects right after it entered as a copy, usually a
16
+ # reflexive_trigger ("When you do, exile that card", CR 603.12), reading {"triggering": "object"} (the
17
+ # copied object) and "player" (its owner).
13
18
  class EnterAsCopy < EnteringChoice
19
+ # Where the copied object may be.
20
+ ZONES = %i[battlefield graveyard].freeze
21
+
14
22
  primitive :ability, :enter_as_copy,
15
23
  description: "As this permanent enters, its controller may choose a permanent on the battlefield " \
16
- "matching `of`; it enters as a copy of that permanent (its copiable values, with the " \
17
- "`except` modifications) and its enter abilities trigger. `with_counters: true`: it " \
18
- "also enters with the counters the copied permanent has. Choosing none: it enters as " \
19
- "itself.",
20
- rules: %w[707.5 707.9b 205.1a 614.1c 614.12],
21
- params: { "of" => "filter (the permanents it may copy)", "except" => Mechanics::CopyExcept::PARAM,
22
- "with_counters" => "boolean" },
24
+ "matching `of` (or, with a `graveyard_card` filter, a card in a graveyard); it enters " \
25
+ "as a copy of it (its copiable values, with the `except` modifications) and its enter " \
26
+ "abilities trigger. `with_counters: true`: it also enters with the counters the copied " \
27
+ "permanent has. `then`: effects once it entered as a copy, usually a reflexive_trigger " \
28
+ "(\"When you do, exile that card\"), reading {\"triggering\": \"object\"} (what it " \
29
+ "copied) and \"player\" (its owner). Choosing none: it enters as itself.",
30
+ rules: %w[707.5 707.2 707.9b 205.1a 614.1c 614.12 603.12],
31
+ params: { "of" => "filter (the permanents, or graveyard cards, it may copy)",
32
+ "except" => Mechanics::CopyExcept::PARAM, "with_counters" => "boolean",
33
+ "then" => "list of effects (a reflexive_trigger for \"When you do, ...\")" },
23
34
  example: { "enter_as_copy" => { "of" => { "permanent" => { "any_of" => %w[artifact creature] } },
24
35
  "except" => { "legendary" => false }, "with_counters" => true } }
25
36
 
26
- attr_reader :filter, :except, :with_counters
37
+ attr_reader :filter, :except, :with_counters, :follow_up
27
38
 
28
39
  def self.build(params, errors, path, **)
29
- Mechanics.check_keys(params, %w[of except with_counters], errors, path)
40
+ Mechanics.check_keys(params, %w[of except with_counters then], errors, path)
30
41
  raw = Mechanics.fetch_param(params, :of)
31
42
  filter = raw.nil? ? (errors << "#{path}.of: required") && nil : Mechanics.build_entry(:filter, raw, errors, "#{path}.of")
43
+ zones = filter ? zones(filter) : []
44
+ errors << "#{path}.of: must name permanents or graveyard cards" unless (zones - ZONES).empty?
32
45
  except = Mechanics::CopyExcept.build(Mechanics.fetch_param(params, :except), errors, "#{path}.except")
33
- new(filter, except, Mechanics.flag_param(params, :with_counters, errors, path))
46
+ with_counters = Mechanics.flag_param(params, :with_counters, errors, path)
47
+ errors << "#{path}.with_counters: a card in a graveyard has no counters" if with_counters && zones.include?(:graveyard)
48
+ new(filter, except, with_counters, FollowUp.build(params, errors, path))
34
49
  end
35
50
 
36
- def initialize(filter, except, with_counters)
51
+ # The zones a filter's objects are in.
52
+ def self.zones(filter)
53
+ return filter.filters.flat_map { zones(it) }.uniq if filter.respond_to?(:filters)
54
+
55
+ [(filter.respond_to?(:base) ? filter.base : filter).zone]
56
+ end
57
+
58
+ def initialize(filter, except, with_counters, follow_up = nil)
37
59
  super()
38
60
  @filter = filter
39
61
  @except = except
40
62
  @with_counters = with_counters
63
+ @follow_up = follow_up
41
64
  freeze
42
65
  end
43
66
 
67
+ def try_effects = [follow_up].compact
68
+
44
69
  def to_s
45
70
  what = filter.is_a?(Class) ? filter.primitive_name : filter
46
- "you may have it enter as a copy of a #{what}#{Mechanics::CopyExcept.to_s(except)}#{" with its counters" if with_counters}"
71
+ what = "#{what} card in a graveyard" if filter.is_a?(Filters::Restricted) && filter.base == Filters::GraveyardCard
72
+ "you may have it enter as a copy of a #{what}#{Mechanics::CopyExcept.to_s(except)}" \
73
+ "#{" with its counters" if with_counters}#{"; then #{follow_up}" if follow_up}"
47
74
  end
48
75
 
49
76
  def options(engine, controller)
50
77
  candidates = filter ? filter.candidates_for(engine, controller, nil) : []
51
- candidates.select { engine.state.battlefield.include?(it) }.map { { type: "copy", object: it } } +
78
+ candidates.select { ZONES.include?(engine.state.objects[it]&.zone) }.map { { type: "copy", object: it } } +
52
79
  [{ type: "dont_copy" }]
53
80
  end
54
81
 
55
- # What it enters with (Engine#enters_from_answer): the copy, and the copied permanent's counters.
82
+ # What it enters with (Engine#enters_from_answer): the copy, the copied permanent's counters, and the
83
+ # follow-up to run once it entered (Engine::Copies#copy_follow_up!).
56
84
  def enters_with(engine, answer)
57
85
  obj = answer[:type] == "copy" && engine.state.objects[answer[:object]]
58
- return nil unless obj && obj.zone == :battlefield
86
+ return nil unless obj && ZONES.include?(obj.zone)
59
87
 
60
88
  card, except = engine.copiable_values(obj)
61
89
  enters = { "copy" => { "card" => card, "except" => Engine::Copies.merge_except(except, @except) } }
90
+ enters["copy_then"] = { "ref" => follow_up.ref, "facts" => { "object" => obj.id, "player" => obj.owner } } if follow_up
62
91
  with_counters && !obj.counters.empty? ? enters.merge("counters" => obj.counters.dup) : enters
63
92
  end
64
93
 
@@ -200,7 +200,9 @@ module Enjine
200
200
  end
201
201
 
202
202
  # Permission to play exiled cards for a duration (CR 611.2, 601.3; see Engine::Permissions). With
203
- # `choose`, the controller first chooses one matching card per owner among them.
203
+ # `choose`, the controller first chooses one matching card per owner among them. `zone: "graveyard"`: the
204
+ # referenced cards in a graveyard instead ("mill a card. You may play that card this turn"); the permission
205
+ # ends once the card leaves it (a new object, CR 400.7).
204
206
  #
205
207
  # Who may play them and how (riders):
206
208
  # `to_player` "controller" (default), "owner" (each card's owner, CR 108.3) or a player selector
@@ -216,10 +218,12 @@ module Enjine
216
218
  DURATIONS = %w[end_of_turn end_of_your_next_turn until_your_next_end_step while_exiled
217
219
  while_you_control_source].freeze
218
220
  PLAYERS = %w[controller owner].freeze
221
+ ZONES = %w[exile graveyard].freeze
219
222
 
220
223
  primitive :effect, :play_permission,
221
224
  description: "A player may play (`play: \"play\"`, default) or cast (`\"cast\"`) each referenced " \
222
- "exiled card matching `for`/`not_types`, paying its costs and following timing rules, " \
225
+ "exiled card (`zone: \"graveyard\"`: card in a graveyard) matching `for`/`not_types`, " \
226
+ "paying its costs and following timing rules (a land uses a land play), " \
223
227
  "for `duration`: end_of_turn (default), end_of_your_next_turn, until_your_next_end_step, " \
224
228
  "while_exiled or while_you_control_source (\"for as long as you control this creature\"). " \
225
229
  "With " \
@@ -230,19 +234,20 @@ module Enjine
230
234
  "`any_mana_type: true`: mana of any type can be spent to cast those spells. " \
231
235
  "`during: \"your_turn\"`: only during your turn. `waterbend: \"mana_value\"`: cast it by " \
232
236
  "waterbending {X} rather than paying its mana cost, where X is its mana value.",
233
- rules: %w[611.2a 611.2b 601.3 305.1 400.7 514.2 601.2f 108.3 406.3b 609.4b 118.9 701.67a],
237
+ rules: %w[611.2a 611.2b 601.3 305.1 305.2 400.7 514.2 601.2f 108.3 406.3b 609.4b 118.9 701.67a],
234
238
  params: { "to" => "selector (usually {\"result\": n})", "duration" => DURATIONS.join(" | "),
235
239
  "play" => "play | cast", "for" => "card spec", "not_types" => "list of card types",
236
240
  "choose" => "one_per_owner", "to_player" => "controller | owner | player selector",
237
241
  "cost_increase" => "integer >= 1", "any_mana_type" => "boolean",
238
242
  "exile_instead" => "true or a card spec: if a spell cast this way (matching it) would be put " \
239
243
  "into a graveyard, it's exiled instead (CR 614.1a)",
240
- "during" => "your_turn", "waterbend" => "mana_value" },
244
+ "during" => "your_turn", "waterbend" => "mana_value",
245
+ "zone" => "exile (default) | graveyard: where the referenced cards are" },
241
246
  example: { "play_permission" => { "to" => { "result" => 0 }, "duration" => "end_of_your_next_turn" } }
242
247
 
243
248
  def self.keys
244
249
  %w[to duration play for not_types choose to_player cost_increase any_mana_type exile_instead during
245
- waterbend if]
250
+ waterbend zone if]
246
251
  end
247
252
 
248
253
  def setup(params, errors, path)
@@ -256,6 +261,8 @@ module Enjine
256
261
  @filter = CardFilter.build(params, errors, path)
257
262
  @to_player = player_param(Mechanics.fetch_param(params, :to_player) || "controller", errors, path)
258
263
  @riders = riders(params, errors, path)
264
+ @zone = zone_param(params, errors, path)
265
+ @riders["zone"] = @zone if @zone == "graveyard"
259
266
  end
260
267
 
261
268
  def to_s
@@ -265,7 +272,8 @@ module Enjine
265
272
  more += ", #{ExileInstead.words(@riders["exile_instead"])}" if @riders["exile_instead"]
266
273
  more += ", during your turn" if @riders["during"]
267
274
  more += ", by waterbending {X} (X: its mana value)" if @riders["waterbend"]
268
- "#{who} may #{@play} #{@filter} (#{@duration.tr("_", " ")}#{more}) → #{to}"
275
+ where = @zone == "graveyard" ? " in a graveyard" : ""
276
+ "#{who} may #{@play} #{@filter}#{where} (#{@duration.tr("_", " ")}#{more}) → #{to}"
269
277
  end
270
278
 
271
279
  def run(engine, ctx, locals)
@@ -273,7 +281,7 @@ module Enjine
273
281
  # CR 611.2b: "for as long as you control [this]" that has already ended never starts.
274
282
  return :done unless engine.source_duration_holds?(@duration, ctx.source, ctx.controller)
275
283
 
276
- cards = (locals["cards"] ||= refs(engine, ctx).select { exiled_match?(engine, it) })
284
+ cards = (locals["cards"] ||= refs(engine, ctx).select { card_match?(engine, it) })
277
285
  picks = @choose ? choose(engine, ctx, locals, cards) : cards
278
286
  return :wait if picks == :wait
279
287
 
@@ -286,6 +294,17 @@ module Enjine
286
294
 
287
295
  private
288
296
 
297
+ # CR 400.7: a card in a graveyard keeps its permission only while it stays there ("while_exiled" is exile's).
298
+ def zone_param(params, errors, path)
299
+ zone = Mechanics.fetch_param(params, :zone) || "exile"
300
+ return zone if ZONES.include?(zone) && !(zone == "graveyard" && @duration == "while_exiled")
301
+
302
+ errors << if ZONES.include?(zone) then "#{path}.duration: while_exiled needs zone exile"
303
+ else "#{path}.zone: must be one of #{ZONES.join(", ")}"
304
+ end
305
+ "exile"
306
+ end
307
+
289
308
  def player_param(raw, errors, path)
290
309
  return raw if PLAYERS.include?(raw)
291
310
 
@@ -319,9 +338,9 @@ module Enjine
319
338
  end
320
339
 
321
340
  # A face-down card is judged face up (the permission is to play it face up, CR 406.3a).
322
- def exiled_match?(engine, id)
341
+ def card_match?(engine, id)
323
342
  obj = engine.state.objects[id]
324
- obj && obj.zone == :exile && engine.face_up(id) { @filter.match?(engine, obj) }
343
+ obj && obj.zone.to_s == @zone && engine.face_up(id) { @filter.match?(engine, obj) }
325
344
  end
326
345
 
327
346
  # One card per owner, in seat order; asked only when there's a choice.
@@ -59,32 +59,61 @@ module Enjine
59
59
  def try_effects = []
60
60
  end
61
61
 
62
- # "If you would draw a card, draw two cards instead." (CR 614.11)
62
+ # "If you would draw a card, draw two cards instead." (CR 614.11) / "As long as you have one or fewer cards in
63
+ # hand, if you would draw one or more cards, you draw that many cards plus one instead." (CR 614.11, 121.2)
63
64
  class DrawReplacement < StaticReplacement
64
65
  primitive :ability, :draw_replacement,
65
- description: "While this permanent is on the battlefield, each time its controller would draw a " \
66
- "card, they draw `amount` cards instead (even with an empty library). The draws it " \
67
- "makes aren't replaced by it again, but another such effect applies to each of them.",
68
- rules: %w[614.11 614.11a 614.5 121.2 616.1],
69
- params: { "amount" => "integer >= 2" },
66
+ description: "While this permanent is on the battlefield (and its `if` condition holds as the draw " \
67
+ "would happen), each time its controller would draw: with `amount`, each card they " \
68
+ "would draw becomes `amount` cards (even with an empty library; the draws it makes " \
69
+ "aren't replaced by it again, but another such effect applies to each of them); with " \
70
+ "`plus`, an instruction to draw one or more cards draws that many plus `plus` " \
71
+ "instead, once for the whole instruction (\"draw two\" becomes three, not four). " \
72
+ "The affected player's best order is used: adding before multiplying each card.",
73
+ rules: %w[614.11 614.11a 614.5 121.2 616.1 604.2],
74
+ params: { "amount" => "integer >= 2 (each card drawn: that many instead)",
75
+ "plus" => "amount (\"that many cards plus one\"), instead of amount",
76
+ "if" => "condition, e.g. {\"hand_size\": \"you\", \"at_most\": 1}" },
70
77
  example: { "draw_replacement" => { "amount" => 2 } }
71
78
 
72
- attr_reader :amount
79
+ attr_reader :amount, :plus, :condition
73
80
 
74
81
  def self.build(params, errors, path, **)
75
- Mechanics.check_keys(params, %w[amount], errors, path)
76
- new(Mechanics.integer_param(params, :amount, errors, path, min: 2))
82
+ Mechanics.check_keys(params, %w[amount plus if], errors, path)
83
+ errors << "#{path}: needs exactly one of amount, plus" unless %w[amount plus].one? { params.key?(it) }
84
+ amount = params.key?("amount") ? Mechanics.integer_param(params, :amount, errors, path, min: 2) : nil
85
+ plus = params.key?("plus") ? Amount.build(params["plus"], errors, "#{path}.plus", targets_count: 0) : nil
86
+ raw_if = Mechanics.fetch_param(params, :if)
87
+ new(amount, plus, raw_if && Condition.build(raw_if, errors, "#{path}.if"))
77
88
  end
78
89
 
79
- def initialize(amount)
90
+ def initialize(amount, plus = nil, condition = nil)
80
91
  super()
81
92
  @amount = amount
93
+ @plus = plus
94
+ @condition = condition
82
95
  freeze
83
96
  end
84
97
 
85
98
  def kind = :draw_replacement
86
99
  def replace_draw = true
87
- def to_s = "if you would draw a card, draw #{amount} instead"
100
+
101
+ # Whether it applies to a draw of `index` now: its controller's draw while its "as long as" condition
102
+ # holds (CR 604.2), checked as the draw would happen (CR 614.11).
103
+ def applies?(engine, index, source)
104
+ index == source.controller &&
105
+ (condition.nil? || condition.true?(engine, EffectContext.new(source.controller, source.id, source.id, [], [])))
106
+ end
107
+
108
+ # The extra cards an instruction to draw gets ("that many plus one"), at least 0.
109
+ def extra(engine, source) = [plus.value(engine, EffectContext.new(source.controller, source.id, source.id, [], [])), 0].max
110
+
111
+ def to_s
112
+ instead = if plus then "if you would draw one or more cards, draw that many plus #{plus} instead"
113
+ else "if you would draw a card, draw #{amount} instead"
114
+ end
115
+ condition ? "as long as #{condition}, #{instead}" : instead
116
+ end
88
117
  end
89
118
 
90
119
  # "If another creature you control with a +1/+1 counter on it would be destroyed, instead remove all
@@ -6,14 +6,21 @@ module Enjine
6
6
  # Counter a spell or ability (CR 701.6).
7
7
  class CounterSpell < Effect
8
8
  primitive :effect, :counter, description: "Counter each referenced spell or ability: a spell leaves the " \
9
- "stack for its owner's graveyard without resolving, an ability " \
10
- "ceases to exist. A spell that can't be countered stays.",
11
- rules: %w[701.6a 701.6b 101.2], params: { "to" => "selector" },
9
+ "stack for its owner's graveyard (for exile with `exile_instead: " \
10
+ "true`) without resolving, an ability ceases to exist. A spell " \
11
+ "that can't be countered stays.",
12
+ rules: %w[701.6a 701.6b 101.2 614.1a],
13
+ params: { "to" => "selector", "exile_instead" => "boolean: \"if that spell is " \
14
+ "countered this way, exile it instead\"" },
12
15
  example: { "counter" => { "to" => { "target" => 0 } } }
13
- def self.keys = %w[to if]
16
+ def self.keys = %w[to exile_instead if]
17
+
18
+ attr_reader :exile_instead
19
+
20
+ def setup(params, errors, path) = @exile_instead = Mechanics.flag_param(params, :exile_instead, errors, path)
14
21
 
15
22
  def apply(engine, ctx)
16
- refs(engine, ctx).each { engine.counter!(it) if engine.state.stack.include?(it) }
23
+ refs(engine, ctx).each { engine.counter!(it, exile: exile_instead) if engine.state.stack.include?(it) }
17
24
  end
18
25
  end
19
26
  end
@@ -110,9 +110,10 @@ module Enjine
110
110
  def to_s = "reflexive: #{delayed_ability}"
111
111
 
112
112
  def apply(engine, ctx)
113
- # Created by a replacement's follow-up (Engine::FollowUps), its spell may be long gone: its card is remembered.
113
+ # Created by a replacement's follow-up (Engine::FollowUps), its spell may be long gone: its card is remembered,
114
+ # and is the card the ability is on (a permanent that entered as a copy has another card, Engine::Copies).
114
115
  memory = Selector.memory(engine, ctx)
115
- card = engine.find_object(ctx.object)&.card || memory["source_card"]
116
+ card = memory["source_card"] || engine.find_object(ctx.object)&.card
116
117
  source = { "id" => ctx.source, "card" => card, "controller" => ctx.controller, "ability" => ref }
117
118
  results = Snapshot.deep_copy(DelayedTrigger.linked(ctx.results, @results_count, []))
118
119
  engine.queue_pending_trigger!(source, nil, { "results" => results,
@@ -9,7 +9,7 @@ module Enjine
9
9
  rules: %w[121.1 121.2], params: { "amount" => "integer", "to" => "selector" },
10
10
  example: { "draw" => { "amount" => 2 } }
11
11
  def self.default_to = "you"
12
- def apply(engine, ctx) = players(engine, ctx).each { |i| amount_for(engine, ctx).times { engine.draw!(i) } }
12
+ def apply(engine, ctx) = players(engine, ctx).each { |i| engine.draw_cards!(i, amount_for(engine, ctx)) }
13
13
  end
14
14
  end
15
15
  end
@@ -27,7 +27,7 @@ module Enjine
27
27
  def loot(engine, locals, entry)
28
28
  index = entry["player"]
29
29
  unless locals["drawn"]
30
- entry["n"].times { engine.draw!(index) }
30
+ engine.draw_cards!(index, entry["n"])
31
31
  locals.merge!("drawn" => true, "nonland" => 0, "applied" => 0)
32
32
  end
33
33
  chosen = answers(locals)
@@ -209,20 +209,26 @@ module Enjine
209
209
  primitive :effect, :counter_unless_pays,
210
210
  description: "Counter each referenced spell or ability unless its controller pays `mana` (or " \
211
211
  "`waterbend`s that cost, CR 701.67a, or pays the list of costs `cost`) as this " \
212
- "resolves (they choose, and pay like any cost; mana abilities allowed).",
213
- rules: %w[701.6a 118.12a 605.3a 701.67a],
212
+ "resolves (they choose, and pay like any cost; mana abilities allowed). With " \
213
+ "`exile_instead: true`, a spell countered this way is exiled instead of being put " \
214
+ "into its owner's graveyard; if they pay, nothing happens to it.",
215
+ rules: %w[701.6a 118.12a 605.3a 701.67a 608.2b 614.1a],
214
216
  params: { "mana" => "mana cost string", "waterbend" => "mana cost string, instead of mana",
215
- "cost" => "list of costs, instead of mana", "to" => "selector (usually {\"target\": 0})" },
217
+ "cost" => "list of costs, instead of mana", "to" => "selector (usually {\"target\": 0})",
218
+ "exile_instead" => "boolean: \"if that spell is countered this way, exile it instead\"" },
216
219
  example: { "counter_unless_pays" => { "mana" => "{3}", "to" => { "target" => 0 } } }
217
220
 
218
- def self.keys = %w[mana waterbend cost to if]
221
+ def self.keys = %w[mana waterbend cost to exile_instead if]
219
222
 
220
- attr_reader :costs
223
+ attr_reader :costs, :exile_instead
221
224
 
222
- def setup(params, errors, path) = @costs = parse_payment(params, errors, path)
225
+ def setup(params, errors, path)
226
+ @costs = parse_payment(params, errors, path)
227
+ @exile_instead = Mechanics.flag_param(params, :exile_instead, errors, path)
228
+ end
223
229
 
224
230
  def what = costs_text(costs)
225
- def to_s = "counter #{to} unless its controller pays #{what}"
231
+ def to_s = "counter #{to} unless its controller pays #{what}#{"; if countered this way, exile it instead" if exile_instead}"
226
232
 
227
233
  def run(engine, ctx, locals)
228
234
  return :done unless locals["target"] || condition.nil? || condition.true?(engine, ctx)
@@ -237,7 +243,7 @@ module Enjine
237
243
  paid = answers(locals).first["type"] == "pay" && pay_costs(engine, ctx, locals, payer, costs_now(engine, ctx))
238
244
  return :wait if paid == :wait
239
245
 
240
- engine.counter!(target) unless paid == "paid"
246
+ engine.counter!(target, exile: exile_instead) unless paid == "paid"
241
247
  :done
242
248
  end
243
249
 
@@ -5,7 +5,7 @@ module Enjine
5
5
  # through replacement effects, then executed, so "enters tapped", "if it would die,
6
6
  # exile it instead" and damage prevention can modify them before they occur.
7
7
  #
8
- # kind: "zone_change" | "damage" | "life" | "draw" | "counters" | "untap" | "create_token"
8
+ # kind: "zone_change" | "damage" | "life" | "draw" | "draw_cards" | "counters" | "untap" | "create_token"
9
9
  # data: plain Hash describing the change; a replacement that stops the change entirely
10
10
  # ("instead remove a stun counter") sets data["cancelled"] = true
11
11
  # applied: ids of replacement effects already applied (each applies once, CR 614.5)
@@ -35,6 +35,7 @@ module Enjine
35
35
  when "zone_change" then zone_change!(engine, proposal)
36
36
  when "counters" then putting_counters!(engine, proposal)
37
37
  when "draw" then drawing!(engine, proposal)
38
+ when "draw_cards" then drawing_cards!(engine, proposal)
38
39
  when "damage" then Mechanics::DamageEvents.replace!(engine, proposal) # abilities/damage_replacements.rb
39
40
  when "create_token" then creating_token!(engine, proposal)
40
41
  when "life" then gaining_life!(engine, proposal)
@@ -51,10 +52,11 @@ module Enjine
51
52
  end
52
53
 
53
54
  # The first applicable ability of a battlefield permanent controlled by `player` that responds to
54
- # `hook` and hasn't modified this event yet (CR 614.5). It is marked as applied.
55
+ # `hook` (and passes the block, if given) and hasn't modified this event yet (CR 614.5). It is marked as applied.
55
56
  def next_controlled(engine, proposal, hook, player)
56
- found = battlefield_abilities(engine, hook).find do |source, _|
57
- source.controller == player && !proposal.applied.include?("#{source.id}:#{hook}")
57
+ found = battlefield_abilities(engine, hook).find do |source, ability|
58
+ source.controller == player && !proposal.applied.include?("#{source.id}:#{hook}") &&
59
+ (!block_given? || yield(source, ability))
58
60
  end
59
61
  proposal.applied << "#{found[0].id}:#{hook}" if found
60
62
  found
@@ -63,10 +65,26 @@ module Enjine
63
65
  # "If you would draw a card, draw two cards instead" (CR 614.11). Only one applies to this event;
64
66
  # the others apply to each of the draws it becomes (Engine#draw!).
65
67
  def drawing!(engine, proposal)
66
- _, ability = next_controlled(engine, proposal, :replace_draw, proposal.data["player"])
68
+ player = proposal.data["player"]
69
+ _, ability = next_controlled(engine, proposal, :replace_draw, player) do |source, found|
70
+ found.amount && found.applies?(engine, player, source)
71
+ end
67
72
  proposal.data["count"] = ability.amount if ability
68
73
  end
69
74
 
75
+ # "If you would draw one or more cards, you draw that many cards plus one instead" (CR 614.11, 121.2): an
76
+ # instruction to draw several cards is one event for it, so each such effect applies once (CR 614.5), its
77
+ # condition checked before any card is drawn. Applied before the per-card "draw two instead" effects
78
+ # (Engine#draw_cards!), the order the drawing player would choose (CR 616.1), as with life and counters.
79
+ def drawing_cards!(engine, proposal)
80
+ data = proposal.data
81
+ battlefield_abilities(engine, :replace_draw).each do |source, ability|
82
+ next unless ability.plus && ability.applies?(engine, data["player"], source)
83
+
84
+ data["count"] += ability.extra(engine, source)
85
+ end
86
+ end
87
+
70
88
  # "If you would gain life, you gain twice that much / that much plus 1 instead" and "if an opponent would gain
71
89
  # life, that player loses that much life instead" (CR 614.1a, 119.10). Each applies once (CR 614.5), in the
72
90
  # order the affected player would always choose (CR 616.1): losing instead first (then nothing else applies,
data/lib/enjine/steps.rb CHANGED
@@ -38,7 +38,7 @@ module Enjine
38
38
  def skip?(engine) = engine.state.then { it.turn == 1 && it.active == it.starting_player }
39
39
 
40
40
  def begin(engine)
41
- engine.draw!(engine.state.active)
41
+ engine.draw_cards!(engine.state.active, 1)
42
42
  engine.turn_based_actions_done!
43
43
  end
44
44
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enjine
4
- VERSION = "0.6.3"
4
+ VERSION = "0.6.4"
5
5
  # Comprehensive Rules document this engine follows.
6
6
  RULES_VERSION = "2026-11-06"
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enjine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mehmet Cetin
@@ -350,7 +350,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
350
350
  - !ruby/object:Gem::Version
351
351
  version: '0'
352
352
  requirements: []
353
- rubygems_version: 4.0.3
353
+ rubygems_version: 4.0.20
354
354
  specification_version: 4
355
355
  summary: 'A deterministic, rules-accurate Magic: The Gathering engine for the Standard
356
356
  format.'