enjine 0.3.0 → 0.5.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 (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +91 -1
  3. data/SUPPORTED.md +34 -26
  4. data/docs/guides/card-data-format.md +143 -1
  5. data/docs/plans/dsl-gaps.md +99 -0
  6. data/docs/reference/abilities.md +46 -13
  7. data/docs/reference/costs.md +23 -6
  8. data/docs/reference/decisions.md +4 -0
  9. data/docs/reference/effects.md +118 -25
  10. data/docs/reference/filters.md +19 -2
  11. data/docs/reference/keywords.md +16 -2
  12. data/docs/reference/triggers.md +28 -7
  13. data/lib/enjine/actions/cast.rb +10 -3
  14. data/lib/enjine/actions/play_land.rb +3 -1
  15. data/lib/enjine/card_definition.rb +19 -3
  16. data/lib/enjine/cli/text.rb +10 -3
  17. data/lib/enjine/decisions/combat.rb +4 -2
  18. data/lib/enjine/decisions/damage_order.rb +18 -0
  19. data/lib/enjine/engine/ability_layers.rb +6 -1
  20. data/lib/enjine/engine/casting/alt_casting.rb +44 -4
  21. data/lib/enjine/engine/casting/cast_choices.rb +13 -1
  22. data/lib/enjine/engine/casting/casting.rb +43 -7
  23. data/lib/enjine/engine/casting/cost_options.rb +3 -1
  24. data/lib/enjine/engine/casting/permissions.rb +27 -23
  25. data/lib/enjine/engine/casting/target_counts.rb +14 -3
  26. data/lib/enjine/engine/casting/waterbend.rb +22 -2
  27. data/lib/enjine/engine/characteristics.rb +32 -14
  28. data/lib/enjine/engine/combat.rb +26 -7
  29. data/lib/enjine/engine/copies.rb +26 -12
  30. data/lib/enjine/engine/damage.rb +51 -1
  31. data/lib/enjine/engine/durations.rb +47 -0
  32. data/lib/enjine/engine/entering.rb +36 -2
  33. data/lib/enjine/engine/face_down.rb +12 -3
  34. data/lib/enjine/engine/faces.rb +16 -2
  35. data/lib/enjine/engine/follow_ups.rb +4 -1
  36. data/lib/enjine/engine/resolution.rb +46 -1
  37. data/lib/enjine/engine/rules.rb +6 -4
  38. data/lib/enjine/engine/state_based_actions.rb +8 -0
  39. data/lib/enjine/engine/triggers.rb +18 -21
  40. data/lib/enjine/engine/turn_history.rb +16 -1
  41. data/lib/enjine/engine/turn_structure.rb +1 -0
  42. data/lib/enjine/engine/zones.rb +23 -4
  43. data/lib/enjine/events.rb +6 -1
  44. data/lib/enjine/frames.rb +2 -2
  45. data/lib/enjine/mana.rb +3 -0
  46. data/lib/enjine/mechanics/abilities/chosen_values.rb +49 -6
  47. data/lib/enjine/mechanics/abilities/damage_replacements.rb +136 -64
  48. data/lib/enjine/mechanics/abilities/double_counters.rb +1 -1
  49. data/lib/enjine/mechanics/abilities/entering_choices.rb +40 -21
  50. data/lib/enjine/mechanics/abilities/play_permissions.rb +52 -19
  51. data/lib/enjine/mechanics/abilities/player_rules.rb +30 -13
  52. data/lib/enjine/mechanics/abilities/replacement_effects.rb +2 -1
  53. data/lib/enjine/mechanics/abilities/static.rb +8 -1
  54. data/lib/enjine/mechanics/card_spec.rb +99 -14
  55. data/lib/enjine/mechanics/costs/base.rb +67 -9
  56. data/lib/enjine/mechanics/costs/behold.rb +21 -5
  57. data/lib/enjine/mechanics/costs/choose_creature_type.rb +43 -0
  58. data/lib/enjine/mechanics/costs/one_of.rb +3 -0
  59. data/lib/enjine/mechanics/costs/pay_life.rb +27 -8
  60. data/lib/enjine/mechanics/costs/remove_counters.rb +8 -5
  61. data/lib/enjine/mechanics/costs/tap.rb +44 -3
  62. data/lib/enjine/mechanics/effects/base.rb +10 -2
  63. data/lib/enjine/mechanics/effects/become.rb +60 -21
  64. data/lib/enjine/mechanics/effects/copy.rb +63 -23
  65. data/lib/enjine/mechanics/effects/counters.rb +8 -2
  66. data/lib/enjine/mechanics/effects/damage.rb +11 -0
  67. data/lib/enjine/mechanics/effects/earthbend.rb +2 -1
  68. data/lib/enjine/mechanics/effects/fight.rb +11 -0
  69. data/lib/enjine/mechanics/effects/gain_control.rb +6 -3
  70. data/lib/enjine/mechanics/effects/library.rb +76 -18
  71. data/lib/enjine/mechanics/effects/pay.rb +7 -4
  72. data/lib/enjine/mechanics/effects/repeat.rb +92 -0
  73. data/lib/enjine/mechanics/effects/return_to_battlefield.rb +25 -6
  74. data/lib/enjine/mechanics/effects/search.rb +34 -6
  75. data/lib/enjine/mechanics/effects/tokens.rb +42 -10
  76. data/lib/enjine/mechanics/filters/base.rb +40 -13
  77. data/lib/enjine/mechanics/filters/exile.rb +27 -0
  78. data/lib/enjine/mechanics/filters/stack.rb +15 -9
  79. data/lib/enjine/mechanics/keywords/alternative_costs.rb +10 -6
  80. data/lib/enjine/mechanics/keywords/cards_have.rb +65 -0
  81. data/lib/enjine/mechanics/keywords/flags.rb +3 -1
  82. data/lib/enjine/mechanics/keywords/hideaway.rb +6 -5
  83. data/lib/enjine/mechanics/keywords/kicker.rb +24 -6
  84. data/lib/enjine/mechanics/keywords/protection.rb +24 -8
  85. data/lib/enjine/mechanics/keywords/riot.rb +13 -10
  86. data/lib/enjine/mechanics/linked.rb +32 -2
  87. data/lib/enjine/mechanics/selector.rb +32 -9
  88. data/lib/enjine/mechanics/triggers/base.rb +14 -3
  89. data/lib/enjine/mechanics/triggers/casting.rb +16 -10
  90. data/lib/enjine/mechanics/triggers/combat.rb +32 -12
  91. data/lib/enjine/mechanics/triggers/keyword_actions.rb +17 -0
  92. data/lib/enjine/mechanics/triggers/tap_and_counters.rb +38 -11
  93. data/lib/enjine/mechanics/triggers/zone_changes.rb +1 -1
  94. data/lib/enjine/mechanics/values.rb +20 -10
  95. data/lib/enjine/mechanics.rb +5 -0
  96. data/lib/enjine/replacements.rb +3 -2
  97. data/lib/enjine/rules_monitor.rb +17 -4
  98. data/lib/enjine/steps.rb +10 -1
  99. data/lib/enjine/version.rb +1 -1
  100. data/lib/enjine/view.rb +1 -1
  101. metadata +7 -1
@@ -17,7 +17,9 @@ module Enjine
17
17
  %w[514.2 120.6]],
18
18
  abilities_cant_be_activated: ["Its activated abilities, mana abilities included, can't be activated (\"Activated " \
19
19
  "abilities of artifacts can't be activated\" is a static handing it out).",
20
- %w[602.5 605.3a 101.2]]
20
+ %w[602.5 605.3a 101.2]],
21
+ cant_become_untapped: ["This permanent can't become untapped: not in its controller's untap step nor by an " \
22
+ "effect (\"untap it\" does nothing).", %w[701.26b 502.3 101.2]]
21
23
  }.each { |name, (description, rules)| Evergreen.define(name, description, rules) }
22
24
 
23
25
  # Combat restrictions and requirements (CR 508.1c-d, 509.1b). They aren't keyword abilities
@@ -6,10 +6,10 @@ module Enjine
6
6
  module Mechanics
7
7
  module Abilities
8
8
  # Hideaway N (CR 702.75a): "When this permanent enters, look at the top N cards of your library. Exile one
9
- # of them face down and put the rest on the bottom of your library in a random order." Only its controller
10
- # may look at the exiled card. It's linked to the permanent (CR 607.2a): another of its abilities names it
11
- # with the selector "exiled_with", e.g. `cast_free` with `play: true` ("you may play the exiled card
12
- # without paying its mana cost").
9
+ # of them face down and put the rest on the bottom of your library in a random order." The player who
10
+ # controls the permanent may look at the exiled card (`looker`). It's linked to the permanent (CR 607.2a):
11
+ # another of its abilities names it with the selector "exiled_with", e.g. `cast_free` with `play: true`
12
+ # ("you may play the exiled card without paying its mana cost").
13
13
  class Hideaway < NumberedTrigger
14
14
  primitive :ability, :hideaway,
15
15
  description: "Hideaway `amount`: when this permanent enters, look at the top N cards of your library, " \
@@ -20,7 +20,8 @@ module Enjine
20
20
 
21
21
  def self.expansion(amount)
22
22
  { "when" => { "enters" => {} },
23
- "effects" => [{ "dig" => { "amount" => amount, "put" => "exile", "face_down" => true, "link" => true } }] }
23
+ "effects" => [{ "dig" => { "amount" => amount, "put" => "exile", "face_down" => true, "link" => true,
24
+ "looker" => "source_controller" } }] }
24
25
  end
25
26
  end
26
27
  end
@@ -68,24 +68,42 @@ module Enjine
68
68
  # An additional cost: "As an additional cost to cast this spell, sacrifice a creature." (CR 601.2b,
69
69
  # 601.2f-h, 118.8). An optional one ("you may behold a Dragon", CR 118.8b) is announced like kicker (the
70
70
  # `choose_kicker` decision) without making the spell kicked: {"cast_with": "additional_cost"} holds if it
71
- # was paid.
71
+ # was paid. "You may cast this spell as though it had flash if you behold a Dragon as an additional cost"
72
+ # (`flash: true`, CR 601.3c): it may be begun any time you could cast an instant, and then the cost must be paid.
72
73
  class AdditionalCost < SpellCost
73
74
  primitive :ability, :additional_cost,
74
75
  description: "As an additional cost to cast this spell, pay `cost` too (mana and/or chosen costs " \
75
76
  "such as `sacrifice_permanent`). The spell can't be cast without it. `optional: true`: " \
76
77
  "you may pay it (announced with the `choose_kicker` decision); the condition " \
77
- "`{\"cast_with\": \"additional_cost\"}` then holds (the spell isn't kicked).",
78
- rules: %w[601.2b 601.2f 601.2h 118.8 118.8b],
79
- params: { "cost" => "list of costs", "optional" => "true: you may pay it" },
78
+ "`{\"cast_with\": \"additional_cost\"}` then holds (the spell isn't kicked). With " \
79
+ "`flash: true` (optional only) you may cast it from your hand as though it had flash if " \
80
+ "you pay it: cast that way, only paying it is offered.",
81
+ rules: %w[601.2b 601.2f 601.2h 118.8 118.8b 601.3c],
82
+ params: { "cost" => "list of costs", "optional" => "true: you may pay it",
83
+ "flash" => "true: as though it had flash if you pay it" },
80
84
  example: { "additional_cost" => { "cost" => [{ "sacrifice_permanent" => { "filter" => "creature" } }] } }
81
85
 
82
86
  def self.build(params, errors, path, **)
83
87
  optional = Mechanics.flag_param(params, :optional, errors, path)
84
- new(super(params.except("optional", :optional), errors, path).costs, optional: optional)
88
+ flash = Mechanics.flag_param(params, :flash, errors, path)
89
+ errors << "#{path}.flash: needs optional: true" if flash && !optional
90
+ new(super(params.except("optional", :optional, "flash", :flash), errors, path).costs, optional: optional, flash: flash)
85
91
  end
86
92
 
93
+ def initialize(costs, optional: false, flash: false)
94
+ @flash = flash
95
+ super(costs, optional: optional)
96
+ end
97
+
98
+ # CR 601.3c: the spell may be begun as though it had flash, if this cost is paid.
99
+ def flash? = @flash
100
+
87
101
  def kind = @optional ? :kicker : :additional_cost
88
- def to_s = "As an additional cost to cast this spell#{", you may" if @optional}: #{costs.join(", ")}"
102
+
103
+ def to_s
104
+ text = "As an additional cost to cast this spell#{", you may" if @optional}: #{costs.join(", ")}"
105
+ @flash ? "#{text} (as though it had flash if you do)" : text
106
+ end
89
107
  end
90
108
  end
91
109
  end
@@ -11,13 +11,15 @@ module Enjine
11
11
  # the ability: "each_opponent" (protection from each of your opponents: from everything they control, or own
12
12
  # and nobody controls, CR 702.16i, 702.16k) and "its_colors" ("hexproof from each of its colors": anything
13
13
  # sharing a color with it, CR 702.11g, 702.16h). The engine asks for them by kind
14
- # (Engine::Rules#protected_from?).
14
+ # (Engine::Rules#protected_from?). "chosen_color" is "from the chosen color" (CR 607.2d): the color the
15
+ # holder chose as it entered, or, granted by an effect, the color chosen as that effect resolved (bound
16
+ # by #with_chosen: Mondo Gecko's "hexproof from that color").
15
17
  class QualityAbility < Primitive
16
- HOLDER_QUALITIES = %w[each_opponent its_colors].freeze
18
+ HOLDER_QUALITIES = %w[each_opponent its_colors chosen_color].freeze
17
19
 
18
20
  attr_reader :quality
19
21
 
20
- # Its value may be the string "everything", "each_opponent" or "its_colors".
22
+ # Its value may be the string "everything", "each_opponent", "its_colors" or "chosen_color".
21
23
  def self.scalar_params? = true
22
24
 
23
25
  def self.build(params, errors, path, **)
@@ -25,7 +27,7 @@ module Enjine
25
27
  return new(params.to_sym) if HOLDER_QUALITIES.include?(params)
26
28
 
27
29
  if params.is_a?(String)
28
- errors << "#{path}: must be a card spec, \"everything\", \"each_opponent\" or \"its_colors\""
30
+ errors << "#{path}: must be a card spec, \"everything\", \"each_opponent\", \"its_colors\" or \"chosen_color\""
29
31
  return new(CardSpec::ANY)
30
32
  end
31
33
  new(CardSpec.build(params, errors, path))
@@ -50,10 +52,21 @@ module Enjine
50
52
  case quality
51
53
  when :each_opponent then QualityAbility.opponents?(object, holder)
52
54
  when :its_colors then holder.is_a?(GameObject) && engine.chars(object).colors.intersect?(engine.chars(holder).colors)
55
+ when :chosen_color
56
+ color = holder.is_a?(GameObject) && holder.memory["chosen_color"]
57
+ color ? engine.chars(object).colors.include?(color.to_sym) : false
53
58
  else quality.match?(engine, object)
54
59
  end
55
60
  end
56
61
 
62
+ # The same ability with "the chosen color" fixed to the color in `memory` (chosen_* values, CR 607.2d).
63
+ def with_chosen(memory)
64
+ color = memory["chosen_color"]
65
+ return self unless quality == :chosen_color && color
66
+
67
+ self.class.new(CardSpec.build({ "colors" => [color] }, [], "chosen"))
68
+ end
69
+
57
70
  # CR 702.16k: the object's controller (or its owner, if nobody controls it) is an opponent of the holder's
58
71
  # controller.
59
72
  def self.opponents?(object, holder)
@@ -66,6 +79,7 @@ module Enjine
66
79
  case quality
67
80
  when :each_opponent then "each of your opponents"
68
81
  when :its_colors then "each of its colors"
82
+ when :chosen_color then "the chosen color"
69
83
  else everything? ? "everything" : quality.to_s.delete_suffix(" card")
70
84
  end
71
85
  end
@@ -79,9 +93,10 @@ module Enjine
79
93
  "quality, or abilities from sources with it, that its controller's opponents control. " \
80
94
  "The quality is a card spec (`{\"colors\": [\"B\"]}`, `{\"types\": [\"instant\"]}`, " \
81
95
  "`{\"color_count\": {\"equal\": 1}}` for monocolored), or \"its_colors\" (hexproof " \
82
- "from each of its colors: whatever shares a color with it).",
83
- rules: %w[702.11d 702.11f 702.11g 115.4],
84
- params: { "(value)" => "card spec of the quality, or \"its_colors\"" },
96
+ "from each of its colors: whatever shares a color with it), or \"chosen_color\" " \
97
+ "(the chosen color; granted by `become`, the color chosen as it resolved).",
98
+ rules: %w[702.11d 702.11f 702.11g 115.4 607.2d],
99
+ params: { "(value)" => "card spec of the quality, \"its_colors\" or \"chosen_color\"" },
85
100
  example: { "hexproof_from" => { "colors" => ["B"] } }
86
101
 
87
102
  def kind = :hexproof_from
@@ -100,7 +115,8 @@ module Enjine
100
115
  "already attached fall off as a state-based action).",
101
116
  rules: %w[702.16a 702.16b 702.16c 702.16d 702.16e 702.16f 702.16g 702.16h 702.16i 702.16j 702.16k
102
117
  704.5m 704.5n],
103
- params: { "(value)" => "card spec of the quality, \"everything\", \"each_opponent\" or \"its_colors\"" },
118
+ params: { "(value)" => "card spec of the quality, \"everything\", \"each_opponent\", \"its_colors\" or " \
119
+ "\"chosen_color\"" },
104
120
  example: { "protection_from" => { "colors" => ["W"] } }
105
121
 
106
122
  def kind = :protection_from
@@ -6,29 +6,32 @@ module Enjine
6
6
  module Mechanics
7
7
  module Abilities
8
8
  # Riot's "as this enters" choice (CR 702.136a): "counter" (it enters with an additional +1/+1 counter) or
9
- # "haste". The choice is remembered like choose_option's ({"chosen": "haste"}).
9
+ # "haste": it gains haste as it enters (memory "riot_haste", see Engine::AbilityLayers), an effect that
10
+ # stays when it later loses riot ("Other Spiders you control have riot" leaving).
10
11
  class RiotChoice < ChooseOption
11
12
  INSTANCE = new(%w[counter haste].freeze)
12
13
 
13
14
  def to_s = "as this enters, choose a +1/+1 counter or haste"
14
- def enters_for(answer) = answer[:option] == "counter" ? super.merge("counters" => { "+1/+1" => 1 }) : super
15
+
16
+ def enters_for(answer)
17
+ answer[:option] == "counter" ? { "counters" => { "+1/+1" => 1 } } : { "memory" => { "riot_haste" => true } }
18
+ end
15
19
  end
16
20
  end
17
21
 
18
22
  module Keywords
19
23
  # Riot (CR 702.136a): "You may have this permanent enter with an additional +1/+1 counter on it. If you
20
- # don't, it gains haste." Only one "as this enters" choice of a permanent is supported.
24
+ # don't, it gains haste." Each instance works separately (CR 702.136b); riot granted by a static ability
25
+ # is chosen as the permanent enters too (CR 614.12, Engine::Entering#granted_entering_choices).
21
26
  class Riot < Primitive
22
27
  primitive :keyword, :riot,
23
28
  description: "As this permanent enters, its controller chooses an additional +1/+1 counter or haste " \
24
- "(an enter_choice: `{type: \"option\", option: \"counter\" | \"haste\"}`).",
25
- rules: %w[702.136a 614.12], example: { "keywords" => ["riot"] }
29
+ "(an enter_choice: `{type: \"option\", option: \"counter\" | \"haste\"}`). Riot granted " \
30
+ "by a static ability (\"Other Spiders you control have riot\") is chosen as it enters too, " \
31
+ "and haste chosen this way stays if it later loses riot.",
32
+ rules: %w[702.136a 702.136b 614.12], example: { "keywords" => ["riot"] }
26
33
 
27
- HASTE = { "static" => { "affects" => "self", "if" => { "chosen" => "haste" }, "keywords" => ["haste"] } }.freeze
28
-
29
- def self.intrinsic_abilities
30
- @intrinsic_abilities ||= [Abilities::RiotChoice::INSTANCE, Keywords.ability_from_data(HASTE, "riot")].freeze
31
- end
34
+ def self.intrinsic_abilities = @intrinsic_abilities ||= [Abilities::RiotChoice::INSTANCE].freeze
32
35
  end
33
36
  end
34
37
  end
@@ -19,6 +19,14 @@ module Enjine
19
19
  Array(ids).select { engine.state.exile.include?(it) }
20
20
  end
21
21
 
22
+ # Selector "exiled_with" (the source's linked cards) or "exiled_for_cost" (cards the resolving spell or
23
+ # ability exiled to pay its cost, Costs::CardsCost), still in exile.
24
+ def self.exiled(engine, kind, ctx)
25
+ return exiled_with(engine, ctx.source, ctx) if kind == :exiled_with
26
+
27
+ Array(engine.state.objects[ctx.object]&.memory&.[](Costs::CardsCost::EXILED)).select { engine.state.exile.include?(it) }
28
+ end
29
+
22
30
  # Called by a resolving effect before it moves an object: who controlled it (CR 608.2h), kept on the
23
31
  # resolving spell or ability. Only a permanent has a controller other than its owner.
24
32
  def self.remember!(engine, ctx, id)
@@ -39,12 +47,12 @@ module Enjine
39
47
  # Whether the referenced objects (e.g. the cards exiled with a source, CR 607.2a) have at least one
40
48
  # card type in common. No objects share nothing.
41
49
  class ShareCardType
42
- def self.build(data, errors, path, targets_count)
50
+ def self.build(data, errors, path, **context)
43
51
  Mechanics.check_keys(data, %w[share_card_type], errors, path)
44
52
  raw = data["share_card_type"]
45
53
  list = raw.is_a?(Array) ? raw : [raw]
46
54
  new(list.each_with_index.map do |sel, i|
47
- Selector.build(sel, errors, "#{path}.share_card_type#{"[#{i}]" if raw.is_a?(Array)}", targets_count: targets_count)
55
+ Selector.build(sel, errors, "#{path}.share_card_type#{"[#{i}]" if raw.is_a?(Array)}", **context)
48
56
  end)
49
57
  end
50
58
 
@@ -62,6 +70,28 @@ module Enjine
62
70
 
63
71
  def to_s = "#{@selectors.join(" and ")} share a card type"
64
72
  end
73
+
74
+ # {"attacking": selector}: "if it was attacking" (CR 506.4, 608.2h). A referenced permanent is attacking,
75
+ # or one that has left the battlefield during the resolving spell or ability was as it last existed.
76
+ class Attacking
77
+ def self.build(data, errors, path, **context)
78
+ Mechanics.check_keys(data, %w[attacking], errors, path)
79
+ new(Selector.build(data["attacking"], errors, "#{path}.attacking", **context))
80
+ end
81
+
82
+ def initialize(selector)
83
+ @selector = selector
84
+ freeze
85
+ end
86
+
87
+ def true?(engine, ctx)
88
+ @selector&.resolve(engine, ctx).to_a.any? do |ref|
89
+ engine.state.battlefield.include?(ref) ? engine.attackers.key?(ref) : engine.resolving_last_known(ref)&.[]("attacking") == true
90
+ end
91
+ end
92
+
93
+ def to_s = "#{@selector} was attacking"
94
+ end
65
95
  end
66
96
  end
67
97
  end
@@ -7,7 +7,8 @@ module Enjine
7
7
  # moved or created (CR 400.7j: "the card exiled this way"); see Selector {"result": n}.
8
8
  # `amounts`: per earlier effect, how much it changed (life an earlier effect took); see Amount {"this_way": n}.
9
9
  # `memory`: stands in for the resolving ability's memory while a trigger condition is checked (CR 603.4).
10
- EffectContext = Struct.new(:controller, :source, :object, :targets, :results, :amounts, :memory)
10
+ # `result_cards`: the card each result object was, for results that have since left their zone (Effects::Repeat).
11
+ EffectContext = Struct.new(:controller, :source, :object, :targets, :results, :amounts, :memory, :result_cards)
11
12
 
12
13
  # Data form of "who/what does this effect apply to":
13
14
  # {"target": 0} the first chosen target
@@ -19,6 +20,7 @@ module Enjine
19
20
  # "attached" what the source is attached to ("enchanted creature", CR 303.4m)
20
21
  # {"each": "creature", "controller": "you"} every matching object (you | opponent | any)
21
22
  # {"result": 0} what effect 0 of this list moved or created (CR 400.7j)
23
+ # {"result": 0, "for": card spec} only those that match ("if the exiled card is a land card")
22
24
  # {"controller_of": selector} the controller of each referenced object ("its controller");
23
25
  # one that has left the battlefield as it last existed (CR 608.2h)
24
26
  # {"triggering": "object" | "player"} what the trigger event was about ("that creature",
@@ -29,7 +31,9 @@ module Enjine
29
31
  # event was about ("copy that ability", CR 707.10)
30
32
  # "watched" what a delayed triggered ability watches (CR 603.7c)
31
33
  # "exiled_with" cards in exile exiled with the source (linked abilities, CR 607.2a)
32
- # "gift_recipient" the opponent chosen for the spell's gift (CR 702.174a)
34
+ # "exiled_for_cost" cards this spell or ability exiled to pay its cost, still in exile
35
+ # ("copy those exiled cards", CR 607.2a)
36
+ # "gift_recipient" the opponent chosen for the spell's gift (CR 702.174a)
33
37
  # "granter" the permanent whose static ability granted this ability ("Return
34
38
  # Trusty Boomerang to its owner's hand", CR 607.1a), while it's still
35
39
  # on the battlefield; the source itself for an ability it has printed
@@ -43,7 +47,8 @@ module Enjine
43
47
 
44
48
  def self.build(data, errors, path, targets_count:, results_count: 0)
45
49
  case data
46
- when "self", "you", "each_opponent", "attached", "watched", "exiled_with", "gift_recipient", "granter" then new(data.to_sym)
50
+ when "self", "you", "each_opponent", "attached", "watched", "exiled_with", "exiled_for_cost", "gift_recipient", "granter"
51
+ new(data.to_sym)
47
52
  when Hash
48
53
  data = data.transform_keys(&:to_s)
49
54
  return build_result(data, errors, path, results_count) if data.key?("result")
@@ -59,12 +64,12 @@ module Enjine
59
64
 
60
65
  # {"result": n}: n must name an earlier effect of the same list (a nested list sees its parent's).
61
66
  def self.build_result(data, errors, path, results_count)
62
- Mechanics.check_keys(data, %w[result], errors, path)
67
+ Mechanics.check_keys(data, %w[result for], errors, path)
63
68
  index = data["result"]
64
69
  unless index.is_a?(Integer) && index >= 0 && index < results_count
65
70
  errors << "#{path}.result: must refer to one of the #{results_count} earlier effects"
66
71
  end
67
- new(:result, index)
72
+ new(:result, index, spec: data.key?("for") ? CardSpec.build(data["for"], errors, "#{path}.for") : nil)
68
73
  end
69
74
 
70
75
  def self.build_controller_of(data, errors, path, targets_count, results_count)
@@ -100,17 +105,18 @@ module Enjine
100
105
  end
101
106
  end
102
107
 
103
- def initialize(kind, value = nil, controller = nil)
108
+ def initialize(kind, value = nil, controller = nil, spec: nil)
104
109
  @kind = kind
105
110
  @value = value
106
111
  @controller = controller
112
+ @spec = spec
107
113
  freeze
108
114
  end
109
115
 
110
116
  def to_s
111
117
  case kind
112
118
  when :target then "target ##{value + 1}"
113
- when :result then "the object(s) from effect ##{value + 1}"
119
+ when :result then "the #{@spec ? "#{@spec}(s)" : "object(s)"} from effect ##{value + 1}"
114
120
  when :controller_of then "the controller of #{value}"
115
121
  when :self then "itself"
116
122
  when :attached then "the attached permanent"
@@ -119,6 +125,7 @@ module Enjine
119
125
  when :you then "you"
120
126
  when :each_opponent then "each opponent"
121
127
  when :exiled_with then "the cards exiled with it"
128
+ when :exiled_for_cost then "the cards exiled to pay its cost"
122
129
  when :gift_recipient then "the chosen opponent"
123
130
  when :granter then "the permanent granting it"
124
131
  when :each then "each #{value.primitive_name.tr("_",
@@ -131,7 +138,7 @@ module Enjine
131
138
  def resolve(engine, ctx)
132
139
  case kind
133
140
  when :target then Array(ctx.targets[value]).compact # a counted target's slot holds a list (CR 601.2c)
134
- when :result then Array(ctx.results&.[](value))
141
+ when :result then results(engine, ctx)
135
142
  when :self then SELF_ZONES.include?(engine.state.objects[ctx.source]&.zone) ? [ctx.source] : []
136
143
  when :attached then attached(engine, ctx)
137
144
  when :triggering then triggering(engine, ctx)
@@ -141,7 +148,7 @@ module Enjine
141
148
  engine.state.players.each_index.reject { it == ctx.controller }.map { Filters.player_ref(engine, it) }
142
149
  when :each then each(engine, ctx)
143
150
  when :controller_of then controllers(engine, ctx)
144
- when :exiled_with then Linked.exiled_with(engine, ctx.source, ctx)
151
+ when :exiled_with, :exiled_for_cost then Linked.exiled(engine, kind, ctx)
145
152
  when :granter then granter(engine, ctx)
146
153
  end
147
154
  end
@@ -159,6 +166,22 @@ module Enjine
159
166
 
160
167
  private
161
168
 
169
+ def results(engine, ctx)
170
+ found = Array(ctx.results&.[](value))
171
+ @spec ? found.select { result_matches?(engine, ctx, it) } : found
172
+ end
173
+
174
+ # A result still in its zone is matched as it is now; one that has since moved (a new object, CR 400.7) as
175
+ # the card it was when the effect moved it, if the context remembers it (CR 608.2h: "if it's a land card"
176
+ # after "return it to the battlefield").
177
+ def result_matches?(engine, ctx, id)
178
+ obj = engine.state.objects[id]
179
+ return @spec.match?(engine, obj, ctx) if obj
180
+
181
+ card = ctx.result_cards&.[](id) or return false
182
+ @spec.chars_match?(engine, engine.catalog.fetch(card).printed_characteristics, ctx)
183
+ end
184
+
162
185
  def controllers(engine, ctx)
163
186
  Array(value&.resolve(engine, ctx)).filter_map do |ref|
164
187
  obj = engine.state.objects[ref]
@@ -10,11 +10,14 @@ module Enjine
10
10
  # the battlefield it describes the object as it last existed, CR 603.10a).
11
11
  # What an event tells a triggered ability about itself (CR 603.2): the object and player it is
12
12
  # about and a number ("that many"). Effects name them with {"triggering": "object" | "player"}
13
- # and the amount {"triggering": "amount"}; "card" is the object's card, for conditions.
13
+ # and the amount {"triggering": "amount"}; "card" is the object's card, for conditions, and "types" the card
14
+ # types a permanent that left had as it last existed ("if it was a creature", CR 603.10a).
14
15
  def self.event_facts(engine, event)
15
16
  facts = {}
16
17
  case event
17
- when Events::ZoneChanged then facts.merge!("object" => event.new_id, "player" => engine.names.index(event.owner))
18
+ when Events::ZoneChanged
19
+ facts.merge!("object" => event.new_id, "player" => engine.names.index(event.owner))
20
+ facts["types"] = engine.last_known(event.old_id)&.fetch("types")&.map(&:to_s) if event.from == "battlefield"
18
21
  when Events::SpellCast, Events::DilemmaFaced, Events::Tapped, Events::AttackerDeclared, Events::BecameBlocked,
19
22
  Events::BlockerDeclared
20
23
  facts["object"] = event.object
@@ -100,6 +103,8 @@ module Enjine
100
103
  defn = engine.catalog.fetch(card)
101
104
  chars = types ? defn.printed_characteristics.with(types: types) : defn.printed_characteristics
102
105
  lki = engine.last_known(object_id) || {}
106
+ return false unless owner_ok?(lki.fetch("owner", object_controller), source["controller"]) # CR 108.3
107
+
103
108
  token = lki.fetch("token") { defn.token? } # a token copy of a card (CR 111.1)
104
109
  @filter.matches_facts?(Filters::Facts.new(chars: chars, counters: lki.fetch("counters", nil) || {}, token: token,
105
110
  chosen: engine.state.objects[source["id"]]&.memory))
@@ -109,6 +114,12 @@ module Enjine
109
114
  @controller == "any" || (@controller == "you") == (object_controller == you)
110
115
  end
111
116
 
117
+ # "A creature you own" (CR 108.3), for an object that left: its owner as it last existed.
118
+ def owner_ok?(owner, you)
119
+ wanted = @filter.respond_to?(:owner) && @filter.owner
120
+ !wanted || (wanted == "you") == (owner == you)
121
+ end
122
+
112
123
  def to_s
113
124
  return "this" if @kind == :self
114
125
  return "the attached creature" if @kind == :attached
@@ -211,7 +222,7 @@ module Enjine
211
222
  # Matching a card (not a permanent) by its printed characteristics.
212
223
  module CardMatch
213
224
  def setup_card(params, errors, path)
214
- @card = params["card"] && Mechanics.build_entry(:filter, params["card"], errors, "#{path}.card")
225
+ @card = params["card"] && Mechanics.characteristic_filter(params["card"], errors, "#{path}.card")
215
226
  end
216
227
 
217
228
  def card_ok?(engine, card)
@@ -17,17 +17,18 @@ module Enjine
17
17
  "matching this filter (\"self\": this permanent), e.g. {\"creature\": {\"controller\": " \
18
18
  "\"you\"}}. `from` / `not_from`: only a spell cast from (not from) that zone: hand | " \
19
19
  "graveyard | exile | library | outside. `single_target: true`: only a spell with exactly " \
20
- "one target.",
21
- rules: %w[603.2 601.2i 113.6 202.3e 201.4 607.2 115.1 601.2a 601.2c],
20
+ "one target. `owner`: \"not_you\" only a spell you don't own, \"you\" only one you own.",
21
+ rules: %w[603.2 601.2i 113.6 202.3e 201.4 607.2 115.1 601.2a 601.2c 108.3],
22
22
  params: { "controller" => "you | opponent | any", "spell" => "creature | noncreature | instant_or_sorcery | filter",
23
23
  "name" => "\"chosen\"", "mana_value" => "\"chosen\"", "self" => "true",
24
24
  "targets" => "\"self\" or a target filter", "from" => "zone", "not_from" => "zone",
25
- "single_target" => "true" },
25
+ "single_target" => "true", "owner" => "you | not_you" },
26
26
  example: { "cast" => { "controller" => "you", "spell" => "noncreature" } }
27
27
 
28
28
  SPELLS = %w[creature noncreature instant_or_sorcery].freeze
29
+ OWNERS = %w[you not_you].freeze
29
30
 
30
- def self.keys = %w[controller spell name mana_value self targets from not_from single_target]
31
+ def self.keys = %w[controller spell name mana_value self targets from not_from single_target owner]
31
32
  def event_class = Events::SpellCast
32
33
  def own_spell? = @self
33
34
 
@@ -46,6 +47,8 @@ module Enjine
46
47
  @single = params["single_target"] == true
47
48
  errors << "#{path}.single_target: must be true" unless params["single_target"].nil? || @single
48
49
  @zones = CastFrom.build(params, errors, path)
50
+ @owner = params["owner"]
51
+ errors << "#{path}.owner: must be one of #{OWNERS.join(", ")}" unless @owner.nil? || OWNERS.include?(@owner)
49
52
  end
50
53
 
51
54
  def match?(engine, event, source)
@@ -55,6 +58,8 @@ module Enjine
55
58
  return false unless @controller == "any" || (@controller == "you") == (caster == source["controller"])
56
59
  return false unless spell_ok?(engine, event, caster, source) && targets_ok?(engine, event, source)
57
60
  return false unless @zones.match?((spell = engine.state.objects[event.object]) && engine.cast_origin(spell))
61
+ # "A spell you don't own" (CR 108.3): its owner against this source's controller.
62
+ return false unless @owner.nil? || (spell&.owner == source["controller"]) == (@owner == "you")
58
63
 
59
64
  chosen_ok?(engine, source, event.card, engine.state.objects[event.object]&.x)
60
65
  end
@@ -105,7 +110,7 @@ module Enjine
105
110
 
106
111
  kind = Triggers.filter_words(@filter) || @spell || "spell"
107
112
  "Whenever #{{ "you" => "you cast", "opponent" => "an opponent casts" }.fetch(@controller, "a player casts")} a #{kind}" \
108
- "#{@zones}#{target_words}#{chosen_words}"
113
+ "#{{ "you" => " you own", "not_you" => " you don't own" }[@owner]}#{@zones}#{target_words}#{chosen_words}"
109
114
  end
110
115
 
111
116
  def target_words
@@ -119,17 +124,18 @@ module Enjine
119
124
 
120
125
  # "Whenever a creature you control becomes the target of a spell or ability an opponent controls"
121
126
  # (CR 115.1, 603.2): checked once the spell is cast, the ability activated or the triggered ability
122
- # put on the stack with its targets, so a cancelled cast never triggers it. Each matching object
127
+ # put on the stack with its targets, so a cancelled cast never triggers it; also a copy put on the stack with
128
+ # its targets (CR 707.10) and new targets chosen for one (CR 115.7). Each matching object
123
129
  # that became a target is one occurrence.
124
130
  class BecomesTarget < Trigger
125
131
  BY = %w[opponent you any].freeze
126
- EVENTS = %i[SpellCast AbilityActivated TriggerPutOnStack].freeze
132
+ EVENTS = %i[SpellCast AbilityActivated TriggerPutOnStack SpellCopied TargetsChanged].freeze
127
133
 
128
134
  primitive :trigger, :becomes_target,
129
135
  description: "The subject becomes the target of a spell or ability (controlled by `by`: opponent | you | " \
130
136
  "any (default), relative to the source's controller). One trigger per matching object " \
131
137
  "targeted.",
132
- rules: %w[115.1 601.2c 603.2 603.2c],
138
+ rules: %w[115.1 115.7 601.2c 603.2 603.2c 707.10],
133
139
  params: { "subject" => "\"self\" (default) or {\"each\": filter, ...}", "by" => BY.join(" | ") },
134
140
  example: { "becomes_target" => { "subject" => { "each" => "creature", "controller" => "you" },
135
141
  "by" => "opponent" } }
@@ -146,7 +152,7 @@ module Enjine
146
152
  player = engine.names.index(event.player)
147
153
  return [] unless @by == "any" || (@by == "you") == (player == source["controller"])
148
154
 
149
- Array(event.targets).uniq.filter_map do |ref|
155
+ Array(event.targets).flatten.uniq.filter_map do |ref|
150
156
  obj = engine.state.objects[ref]
151
157
  next unless obj && obj.zone == :battlefield
152
158
  next unless @subject.match?(engine, ref, obj.controller, obj.card, source, types: engine.chars(obj).types)
@@ -187,7 +193,7 @@ module Enjine
187
193
 
188
194
  def setup(params, errors, path)
189
195
  super
190
- @source = params["source"] && Mechanics.build_entry(:filter, params["source"], errors, "#{path}.source")
196
+ @source = params["source"] && Mechanics.characteristic_filter(params["source"], errors, "#{path}.source")
191
197
  setup_chosen(params, errors, path)
192
198
  @kinds = KINDS.select { |key, _| params[key] == true }.values
193
199
  KINDS.each_key { errors << "#{path}.#{it}: must be true or false" unless [nil, true, false].include?(params[it]) }
@@ -3,31 +3,51 @@
3
3
  module Enjine
4
4
  module Mechanics
5
5
  module Triggers
6
- # "Whenever ~ attacks" / "Whenever ~ attacks for the first time each turn".
6
+ # "Whenever ~ attacks" / "Whenever ~ attacks for the first time each turn". "Whenever a creature you control
7
+ # attacks alone" (CR 506.5) is known only once the declaration is complete, so it watches that event.
7
8
  class Attacks < Trigger
8
9
  primitive :trigger, :attacks,
9
10
  description: "The subject is declared as an attacker. `first_time_this_turn: true`: only the first " \
10
- "time that creature attacks this turn (later combats of the turn don't trigger it).",
11
- rules: %w[508.3a 603.2],
12
- params: { "subject" => "\"self\" (default) or {\"each\": filter, ...}", "first_time_this_turn" => "boolean" },
11
+ "time that creature attacks this turn (later combats of the turn don't trigger it). " \
12
+ "`alone: true`: only when it is the only creature declared as an attacker (it triggers " \
13
+ "as the declaration is complete).",
14
+ rules: %w[508.3a 603.2 506.5],
15
+ params: { "subject" => "\"self\" (default) or {\"each\": filter, ...}", "first_time_this_turn" => "boolean",
16
+ "alone" => "boolean" },
13
17
  example: { "attacks" => { "subject" => "self", "first_time_this_turn" => true } }
14
18
 
15
- def self.keys = %w[subject first_time_this_turn]
16
- def event_class = Events::AttackerDeclared
19
+ def self.keys = %w[subject first_time_this_turn alone]
20
+ def event_class = @alone ? Events::AttacksDeclared : Events::AttackerDeclared
17
21
 
18
22
  def setup(params, errors, path)
19
- @first_time = params["first_time_this_turn"] == true
20
- errors << "#{path}.first_time_this_turn: must be true or false" unless [nil, true, false].include?(params["first_time_this_turn"])
23
+ @first_time = Mechanics.flag_param(params, "first_time_this_turn", errors, path)
24
+ @alone = Mechanics.flag_param(params, "alone", errors, path)
21
25
  end
22
26
 
23
27
  def match?(engine, event, source)
24
- obj = engine.state.objects[event.object] or return false
25
- return false if @first_time && engine.attacks_this_turn(event.object) != 1
28
+ id = attacker(event) or return false
29
+ obj = engine.state.objects[id] or return false
30
+ return false if @first_time && engine.attacks_this_turn(id) != 1
26
31
 
27
- @subject.match?(engine, event.object, obj.controller, obj.card, source, types: engine.chars(obj).types)
32
+ @subject.match?(engine, id, obj.controller, obj.card, source, types: engine.chars(obj).types)
28
33
  end
29
34
 
30
- def to_s = "Whenever #{@subject} attacks#{" for the first time each turn" if @first_time}"
35
+ # "It" is the creature attacking alone.
36
+ def occurrences(engine, event, source)
37
+ return super unless @alone && match?(engine, event, source)
38
+
39
+ [Triggers.event_facts(engine, event).merge("object" => attacker(event), "card" => engine.object(attacker(event)).card)]
40
+ end
41
+
42
+ def to_s = "Whenever #{@subject} attacks#{" alone" if @alone}#{" for the first time each turn" if @first_time}"
43
+
44
+ private
45
+
46
+ def attacker(event)
47
+ return event.object unless @alone
48
+
49
+ event.attackers.first if event.attackers.size == 1
50
+ end
31
51
  end
32
52
 
33
53
  # "Whenever you attack" (CR 508.3d): once per combat, if one or more creatures attack.
@@ -36,6 +36,23 @@ module Enjine
36
36
  def self.action = "surveil"
37
37
  end
38
38
 
39
+ # "Whenever you give a gift" (CR 702.174c): an instant or sorcery spell you control whose gift cost was paid
40
+ # resolves, or the gift triggered ability of a permanent you control (its "enters" trigger with
41
+ # `"if": {"cast_with": "gift"}`) resolves (Engine::Resolution#gift_given!).
42
+ class GivesGift < KeywordActionTrigger
43
+ primitive :trigger, :gives_gift,
44
+ description: "A player (`controller`: you (default) | opponent | any) gives a gift: an instant or " \
45
+ "sorcery spell they control whose gift was promised resolves, or the gift triggered " \
46
+ "ability (an enters trigger under `{\"cast_with\": \"gift\"}`) of a permanent they control " \
47
+ "resolves.",
48
+ rules: %w[702.174c 702.174b],
49
+ params: { "controller" => PLAYERS.join(" | ") },
50
+ example: { "gives_gift" => {} }
51
+
52
+ def self.action = "give_gift"
53
+ def to_s = "Whenever #{who} give#{"s" unless @controller == "you"} a gift"
54
+ end
55
+
39
56
  # "Whenever you earthbend / waterbend / airbend / firebend" (CR 701.66b, 701.67c, 701.65b, 702.189b): the
40
57
  # bending happened (the earthbend delayed trigger was created, a waterbend cost was paid, objects were
41
58
  # exiled by an airbend, a firebending ability resolved). "Whenever you waterbend, earthbend, firebend, or