enjine 0.4.1 → 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 (90) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +50 -0
  3. data/SUPPORTED.md +31 -23
  4. data/docs/guides/card-data-format.md +106 -1
  5. data/docs/plans/dsl-gaps.md +23 -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 +108 -18
  10. data/docs/reference/filters.md +19 -2
  11. data/docs/reference/keywords.md +16 -2
  12. data/docs/reference/triggers.md +25 -6
  13. data/lib/enjine/actions/play_land.rb +3 -1
  14. data/lib/enjine/card_definition.rb +4 -3
  15. data/lib/enjine/cli/text.rb +10 -3
  16. data/lib/enjine/decisions/combat.rb +4 -2
  17. data/lib/enjine/decisions/damage_order.rb +18 -0
  18. data/lib/enjine/engine/ability_layers.rb +6 -1
  19. data/lib/enjine/engine/casting/alt_casting.rb +36 -4
  20. data/lib/enjine/engine/casting/cast_choices.rb +13 -1
  21. data/lib/enjine/engine/casting/casting.rb +24 -7
  22. data/lib/enjine/engine/casting/cost_options.rb +3 -1
  23. data/lib/enjine/engine/casting/permissions.rb +13 -14
  24. data/lib/enjine/engine/casting/target_counts.rb +14 -3
  25. data/lib/enjine/engine/casting/waterbend.rb +22 -2
  26. data/lib/enjine/engine/characteristics.rb +3 -2
  27. data/lib/enjine/engine/combat.rb +26 -7
  28. data/lib/enjine/engine/copies.rb +10 -3
  29. data/lib/enjine/engine/damage.rb +49 -0
  30. data/lib/enjine/engine/entering.rb +36 -2
  31. data/lib/enjine/engine/face_down.rb +12 -3
  32. data/lib/enjine/engine/faces.rb +14 -2
  33. data/lib/enjine/engine/follow_ups.rb +4 -1
  34. data/lib/enjine/engine/resolution.rb +46 -1
  35. data/lib/enjine/engine/rules.rb +4 -3
  36. data/lib/enjine/engine/triggers.rb +5 -15
  37. data/lib/enjine/engine/turn_history.rb +16 -1
  38. data/lib/enjine/engine/turn_structure.rb +1 -0
  39. data/lib/enjine/engine/zones.rb +5 -2
  40. data/lib/enjine/events.rb +2 -0
  41. data/lib/enjine/frames.rb +2 -2
  42. data/lib/enjine/mana.rb +3 -0
  43. data/lib/enjine/mechanics/abilities/chosen_values.rb +49 -6
  44. data/lib/enjine/mechanics/abilities/damage_replacements.rb +136 -64
  45. data/lib/enjine/mechanics/abilities/entering_choices.rb +40 -21
  46. data/lib/enjine/mechanics/abilities/play_permissions.rb +52 -20
  47. data/lib/enjine/mechanics/abilities/player_rules.rb +30 -13
  48. data/lib/enjine/mechanics/abilities/static.rb +1 -1
  49. data/lib/enjine/mechanics/card_spec.rb +54 -11
  50. data/lib/enjine/mechanics/costs/base.rb +67 -9
  51. data/lib/enjine/mechanics/costs/behold.rb +21 -5
  52. data/lib/enjine/mechanics/costs/choose_creature_type.rb +43 -0
  53. data/lib/enjine/mechanics/costs/one_of.rb +3 -0
  54. data/lib/enjine/mechanics/costs/pay_life.rb +27 -8
  55. data/lib/enjine/mechanics/costs/remove_counters.rb +8 -5
  56. data/lib/enjine/mechanics/costs/tap.rb +44 -3
  57. data/lib/enjine/mechanics/effects/base.rb +10 -2
  58. data/lib/enjine/mechanics/effects/become.rb +29 -12
  59. data/lib/enjine/mechanics/effects/copy.rb +56 -19
  60. data/lib/enjine/mechanics/effects/counters.rb +8 -2
  61. data/lib/enjine/mechanics/effects/damage.rb +11 -0
  62. data/lib/enjine/mechanics/effects/earthbend.rb +2 -1
  63. data/lib/enjine/mechanics/effects/fight.rb +11 -0
  64. data/lib/enjine/mechanics/effects/library.rb +76 -18
  65. data/lib/enjine/mechanics/effects/pay.rb +7 -4
  66. data/lib/enjine/mechanics/effects/repeat.rb +92 -0
  67. data/lib/enjine/mechanics/effects/search.rb +34 -6
  68. data/lib/enjine/mechanics/effects/tokens.rb +42 -10
  69. data/lib/enjine/mechanics/filters/base.rb +22 -12
  70. data/lib/enjine/mechanics/filters/exile.rb +27 -0
  71. data/lib/enjine/mechanics/filters/stack.rb +14 -8
  72. data/lib/enjine/mechanics/keywords/alternative_costs.rb +10 -6
  73. data/lib/enjine/mechanics/keywords/cards_have.rb +65 -0
  74. data/lib/enjine/mechanics/keywords/flags.rb +3 -1
  75. data/lib/enjine/mechanics/keywords/hideaway.rb +6 -5
  76. data/lib/enjine/mechanics/keywords/kicker.rb +24 -6
  77. data/lib/enjine/mechanics/keywords/protection.rb +24 -8
  78. data/lib/enjine/mechanics/keywords/riot.rb +13 -10
  79. data/lib/enjine/mechanics/linked.rb +32 -2
  80. data/lib/enjine/mechanics/selector.rb +32 -9
  81. data/lib/enjine/mechanics/triggers/casting.rb +15 -9
  82. data/lib/enjine/mechanics/triggers/combat.rb +32 -12
  83. data/lib/enjine/mechanics/triggers/keyword_actions.rb +17 -0
  84. data/lib/enjine/mechanics/triggers/tap_and_counters.rb +20 -9
  85. data/lib/enjine/mechanics/values.rb +10 -3
  86. data/lib/enjine/replacements.rb +3 -2
  87. data/lib/enjine/rules_monitor.rb +13 -2
  88. data/lib/enjine/version.rb +1 -1
  89. data/lib/enjine/view.rb +1 -1
  90. metadata +6 -1
@@ -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]
@@ -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)
@@ -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
@@ -53,21 +53,25 @@ module Enjine
53
53
  end
54
54
 
55
55
  # "Whenever one or more [kind] counters are put on ~" (CR 122.6): counters put on a permanent, including
56
- # the counters it enters with. Once per counter-placing event.
56
+ # the counters it enters with. Once per counter-placing event. `nth`: "when the fourth plan counter is put
57
+ # on this": only the event that takes the number of those counters from below N to N or more.
57
58
  class CountersPlaced < Trigger
58
59
  primitive :trigger, :counters_placed,
59
60
  description: "One or more counters (only `counter`, if given) are put on the subject, including the " \
60
- "counters it enters the battlefield with. Triggers once per event, however many counters.",
61
+ "counters it enters the battlefield with. Triggers once per event, however many counters. " \
62
+ "`nth` (needs `counter`): only when the Nth such counter is put on it (the event that " \
63
+ "takes it from fewer than N to N or more).",
61
64
  rules: %w[122.6 603.2 603.2c],
62
65
  params: { "subject" => "\"self\" (default) or {\"each\": filter, ...}", "counter" => "counter name",
63
66
  "first_time_this_turn" => "true: only the first time each turn such counters are put on " \
64
67
  "that permanent (an earlier placement counts even if nothing " \
65
68
  "could trigger then)",
66
69
  "by" => "you | opponent: only counters that player puts (\"whenever you put ...\", CR 122.6a; " \
67
- "a permanent's controller puts the counters it enters with)" },
70
+ "a permanent's controller puts the counters it enters with)",
71
+ "nth" => "positive integer" },
68
72
  example: { "counters_placed" => { "counter" => "+1/+1", "by" => "you" } }
69
73
 
70
- def self.keys = %w[subject counter first_time_this_turn by]
74
+ def self.keys = %w[subject counter first_time_this_turn by nth]
71
75
  def watches?(event) = event.is_a?(Events::CountersChanged) || event.is_a?(Events::ZoneChanged)
72
76
 
73
77
  def setup(params, errors, path)
@@ -76,11 +80,16 @@ module Enjine
76
80
  errors << "#{path}.first_time_this_turn: must be true or false" unless [nil, true, false].include?(@first_time)
77
81
  @by = params["by"]
78
82
  errors << "#{path}.by: must be you or opponent" unless [nil, "you", "opponent"].include?(@by)
83
+ @nth = params["nth"]
84
+ errors << "#{path}.nth: must be a positive integer" unless @nth.nil? || (@nth.is_a?(Integer) && @nth.positive?)
85
+ errors << "#{path}.nth: needs a counter" if @nth && @counter.nil?
79
86
  end
80
87
 
81
88
  def occurrences(engine, event, source)
82
- id, amount = placed(engine, event)
83
- return [] unless amount&.positive? && put_by_ok?(engine, event, id, source)
89
+ id, from, to = placed(engine, event)
90
+ amount = to.to_i - from.to_i
91
+ return [] unless amount.positive? && put_by_ok?(engine, event, id, source)
92
+ return [] if @nth && !(from < @nth && to >= @nth)
84
93
  return [] if @first_time && engine.counter_placements_this_turn(id, @counter) != 1
85
94
 
86
95
  obj = engine.object(id)
@@ -92,6 +101,8 @@ module Enjine
92
101
  def match?(engine, event, source) = !occurrences(engine, event, source).empty?
93
102
 
94
103
  def to_s
104
+ return "When #{@counter} counter number #{@nth} is put on #{@subject}" if @nth
105
+
95
106
  first = " for the first time each turn" if @first_time
96
107
  return "Whenever one or more #{"#{@counter} " if @counter}counters are put on #{@subject}#{first}" unless @by
97
108
 
@@ -110,15 +121,15 @@ module Enjine
110
121
  (@by == "you") == (putter == source["controller"])
111
122
  end
112
123
 
113
- # [object id, how many matching counters were put on it] for this event, or nil.
124
+ # [object id, how many matching counters it had before, how many after] for this event, or nil.
114
125
  def placed(engine, event)
115
126
  if event.is_a?(Events::CountersChanged)
116
127
  return nil if @counter && event.counter != @counter
117
128
 
118
- [event.object, event.to - event.from]
129
+ [event.object, event.from, event.to]
119
130
  elsif event.to == "battlefield" && (obj = engine.state.objects[event.new_id])
120
131
  counts = @counter ? [obj.counters.fetch(@counter, 0)] : obj.counters.values
121
- [obj.id, counts.sum]
132
+ [obj.id, 0, counts.sum]
122
133
  end
123
134
  end
124
135
  end
@@ -112,11 +112,14 @@ module Enjine
112
112
 
113
113
  def to_s = "the #{@kind.tr("_", " ")} of #{@selector}"
114
114
 
115
- # CR 202.3: the mana value of an object (0 without a mana cost); a spell on the stack counts its X (CR 202.3e).
116
- # Nil for an object that's gone.
115
+ # CR 202.3: the mana value of an object (0 without a mana cost); a spell on the stack counts its X (CR 202.3e);
116
+ # a melded permanent adds up its front faces (CR 713.5c, Engine::Faces). Nil for an object that's gone.
117
117
  def self.mana_value(engine, obj)
118
118
  return nil if obj.nil?
119
119
 
120
+ melded = engine.melded_mana_value(obj)
121
+ return melded if melded
122
+
120
123
  cost = engine.chars(obj).mana_cost or return 0
121
124
  (obj.zone == :stack ? cost.with_x(obj.x.to_i) : cost).mana_value
122
125
  end
@@ -463,6 +466,8 @@ module Enjine
463
466
  # {"share_card_type": selector | [selectors]} the referenced objects share a card type ("exiled_with": linked cards)
464
467
  # {"chosen": word} that word was chosen for this object (choose_option, CR 607.2f)
465
468
  # {"meld_with": name} you both own and control the source and a creature named so (CR 713.1a)
469
+ # {"attacking": selector} a referenced permanent is attacking, or was as it last existed ("if it was
470
+ # attacking", CR 608.2h)
466
471
  # {"not": condition}, {"all": [conditions]}, {"any": [conditions]}
467
472
  class Condition
468
473
  FLAGS = %w[your_turn was_cast attacked attacked_this_turn max_speed
@@ -472,6 +477,8 @@ module Enjine
472
477
  KEYED = { "targets" => :Targets, "triggering" => :TriggeringIs, "source" => :Source,
473
478
  "nth_resolution" => :NthResolution, "chosen" => :ChosenIs, "designation" => :Designation,
474
479
  "first_combat_phase" => :FirstCombatPhase, "meld_with" => :MeldWith }.freeze # MeldWith: effects/meld.rb
480
+ # Kinds about the objects a selector names (both in linked.rb).
481
+ SELECTED = { "share_card_type" => :ShareCardType, "attacking" => :Attacking }.freeze
475
482
 
476
483
  def self.build(data, errors, path, targets_count: 0, results_count: 0)
477
484
  unless data.is_a?(Hash)
@@ -489,7 +496,7 @@ module Enjine
489
496
  elsif data.key?("sacrificed") then Sacrificed.build(data, errors, path)
490
497
  elsif (flag = FLAGS.find { data.key?(it) }) then Flag.new(flag, data[flag] != false)
491
498
  elsif data.key?("began_extra_turn") then BeganExtraTurn.new(data["began_extra_turn"] != false) # CR 500.7
492
- elsif data.key?("share_card_type") then ShareCardType.build(data, errors, path, targets_count) # linked.rb
499
+ elsif (key = SELECTED.keys.find { data.key?(it) }) then const_get(SELECTED[key]).build(data, errors, path, **context)
493
500
  elsif (key = KEYED.keys.find { data.key?(it) }) then const_get(KEYED[key]).build(data, errors, path)
494
501
  else build_more(data, errors, path, context)
495
502
  end
@@ -23,8 +23,9 @@ module Enjine
23
23
  # several effects apply. The engine applies them in the order that player would always choose:
24
24
  # effects that let a permanent stay ("instead remove all +1/+1 counters", a shield counter) before
25
25
  # effects that send it elsewhere ("exile it instead"), "+1 counter" bonuses before doubling, and
26
- # "enters untapped" after every "enters tapped". No pair implemented so far has two orders that
27
- # each benefit that player, so no decision is asked; a frame will when one does.
26
+ # "enters untapped" after every "enters tapped". No pair of these has two orders that each benefit
27
+ # that player, so no decision is asked. Damage is where orders matter: the affected player chooses
28
+ # one before the damage is dealt (Mechanics::DamageEvents, a damage_order decision).
28
29
  module Replacements
29
30
  module_function
30
31