enjine 0.4.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +196 -1
  3. data/README.md +12 -3
  4. data/SUPPORTED.md +73 -38
  5. data/docs/ARCHITECTURE.md +7 -2
  6. data/docs/MECHANICS_COVERAGE.md +10 -8
  7. data/docs/guides/api.md +2 -1
  8. data/docs/guides/card-data-format.md +146 -9
  9. data/docs/plans/dsl-gaps.md +23 -0
  10. data/docs/reference/abilities.md +299 -25
  11. data/docs/reference/costs.md +59 -6
  12. data/docs/reference/decisions.md +7 -1
  13. data/docs/reference/effects.md +200 -27
  14. data/docs/reference/filters.md +30 -3
  15. data/docs/reference/keywords.md +58 -2
  16. data/docs/reference/subtypes.md +18 -0
  17. data/docs/reference/triggers.md +94 -14
  18. data/docs/reference/types.md +20 -0
  19. data/lib/enjine/actions/play_land.rb +3 -1
  20. data/lib/enjine/actions/special_action.rb +4 -0
  21. data/lib/enjine/actions/turn_face_up.rb +25 -0
  22. data/lib/enjine/actions/unlock.rb +23 -0
  23. data/lib/enjine/card_definition.rb +54 -15
  24. data/lib/enjine/card_face_down.rb +31 -0
  25. data/lib/enjine/card_faces.rb +4 -2
  26. data/lib/enjine/card_rooms.rb +86 -0
  27. data/lib/enjine/catalog.rb +9 -3
  28. data/lib/enjine/cli/text.rb +11 -3
  29. data/lib/enjine/color_identity.rb +34 -1
  30. data/lib/enjine/decisions/casting.rb +6 -3
  31. data/lib/enjine/decisions/combat.rb +4 -2
  32. data/lib/enjine/decisions/damage_order.rb +18 -0
  33. data/lib/enjine/engine/ability_layers.rb +14 -1
  34. data/lib/enjine/engine/casting/alt_casting.rb +45 -5
  35. data/lib/enjine/engine/casting/cast_choices.rb +21 -2
  36. data/lib/enjine/engine/casting/casting.rb +42 -29
  37. data/lib/enjine/engine/casting/cost_options.rb +3 -1
  38. data/lib/enjine/engine/casting/costs.rb +8 -1
  39. data/lib/enjine/engine/casting/payment.rb +47 -9
  40. data/lib/enjine/engine/casting/permissions.rb +19 -17
  41. data/lib/enjine/engine/casting/target_counts.rb +14 -3
  42. data/lib/enjine/engine/casting/waterbend.rb +22 -2
  43. data/lib/enjine/engine/characteristics.rb +11 -11
  44. data/lib/enjine/engine/combat.rb +45 -14
  45. data/lib/enjine/engine/copies.rb +12 -4
  46. data/lib/enjine/engine/crimes.rb +46 -0
  47. data/lib/enjine/engine/damage.rb +85 -1
  48. data/lib/enjine/engine/entering.rb +36 -2
  49. data/lib/enjine/engine/face_down.rb +12 -3
  50. data/lib/enjine/engine/face_down_permanents.rb +108 -0
  51. data/lib/enjine/engine/faces.rb +20 -5
  52. data/lib/enjine/engine/follow_ups.rb +4 -1
  53. data/lib/enjine/engine/mana_sources.rb +18 -3
  54. data/lib/enjine/engine/options_memo.rb +140 -0
  55. data/lib/enjine/engine/permanent_designations.rb +29 -0
  56. data/lib/enjine/engine/resolution.rb +71 -6
  57. data/lib/enjine/engine/rooms.rb +86 -0
  58. data/lib/enjine/engine/rules.rb +6 -4
  59. data/lib/enjine/engine/state_based_actions.rb +25 -4
  60. data/lib/enjine/engine/triggers.rb +25 -20
  61. data/lib/enjine/engine/turn_history.rb +43 -5
  62. data/lib/enjine/engine/turn_structure.rb +1 -0
  63. data/lib/enjine/engine/zones.rb +37 -8
  64. data/lib/enjine/engine.rb +5 -1
  65. data/lib/enjine/events.rb +11 -0
  66. data/lib/enjine/frames.rb +5 -2
  67. data/lib/enjine/fuzz.rb +3 -0
  68. data/lib/enjine/game.rb +3 -1
  69. data/lib/enjine/mana.rb +3 -0
  70. data/lib/enjine/mechanics/abilities/chosen_values.rb +49 -6
  71. data/lib/enjine/mechanics/abilities/cost_changes.rb +32 -10
  72. data/lib/enjine/mechanics/abilities/damage_replacements.rb +137 -65
  73. data/lib/enjine/mechanics/abilities/designated.rb +57 -0
  74. data/lib/enjine/mechanics/abilities/entering_choices.rb +40 -21
  75. data/lib/enjine/mechanics/abilities/life_replacements.rb +74 -0
  76. data/lib/enjine/mechanics/abilities/mana_ability.rb +10 -4
  77. data/lib/enjine/mechanics/abilities/play_permissions.rb +52 -20
  78. data/lib/enjine/mechanics/abilities/player_rules.rb +30 -13
  79. data/lib/enjine/mechanics/abilities/stack_abilities.rb +12 -7
  80. data/lib/enjine/mechanics/abilities/static.rb +19 -6
  81. data/lib/enjine/mechanics/card_spec.rb +63 -11
  82. data/lib/enjine/mechanics/costs/base.rb +68 -9
  83. data/lib/enjine/mechanics/costs/behold.rb +28 -5
  84. data/lib/enjine/mechanics/costs/choose_creature_type.rb +43 -0
  85. data/lib/enjine/mechanics/costs/collect_evidence.rb +60 -0
  86. data/lib/enjine/mechanics/costs/exile_permanent.rb +41 -0
  87. data/lib/enjine/mechanics/costs/one_of.rb +3 -0
  88. data/lib/enjine/mechanics/costs/pay_life.rb +27 -8
  89. data/lib/enjine/mechanics/costs/remove_counters.rb +8 -5
  90. data/lib/enjine/mechanics/costs/tap.rb +44 -3
  91. data/lib/enjine/mechanics/effects/base.rb +36 -7
  92. data/lib/enjine/mechanics/effects/become.rb +29 -12
  93. data/lib/enjine/mechanics/effects/choose_cards.rb +44 -14
  94. data/lib/enjine/mechanics/effects/copy.rb +58 -21
  95. data/lib/enjine/mechanics/effects/counters.rb +18 -6
  96. data/lib/enjine/mechanics/effects/damage.rb +11 -0
  97. data/lib/enjine/mechanics/effects/delayed_triggers.rb +13 -6
  98. data/lib/enjine/mechanics/effects/earthbend.rb +2 -1
  99. data/lib/enjine/mechanics/effects/fight.rb +11 -0
  100. data/lib/enjine/mechanics/effects/library.rb +76 -18
  101. data/lib/enjine/mechanics/effects/may.rb +4 -2
  102. data/lib/enjine/mechanics/effects/pay.rb +27 -10
  103. data/lib/enjine/mechanics/effects/repeat.rb +92 -0
  104. data/lib/enjine/mechanics/effects/search.rb +34 -6
  105. data/lib/enjine/mechanics/effects/tokens.rb +42 -10
  106. data/lib/enjine/mechanics/filters/any_target.rb +6 -3
  107. data/lib/enjine/mechanics/filters/base.rb +36 -17
  108. data/lib/enjine/mechanics/filters/exile.rb +27 -0
  109. data/lib/enjine/mechanics/filters/permanents.rb +7 -0
  110. data/lib/enjine/mechanics/filters/stack.rb +14 -8
  111. data/lib/enjine/mechanics/keywords/alternative_costs.rb +80 -22
  112. data/lib/enjine/mechanics/keywords/cards_have.rb +67 -0
  113. data/lib/enjine/mechanics/keywords/craft.rb +132 -0
  114. data/lib/enjine/mechanics/keywords/cycling.rb +46 -7
  115. data/lib/enjine/mechanics/keywords/extort.rb +2 -0
  116. data/lib/enjine/mechanics/keywords/face_down.rb +165 -0
  117. data/lib/enjine/mechanics/keywords/flags.rb +3 -1
  118. data/lib/enjine/mechanics/keywords/hideaway.rb +6 -5
  119. data/lib/enjine/mechanics/keywords/impending.rb +75 -0
  120. data/lib/enjine/mechanics/keywords/increment.rb +2 -0
  121. data/lib/enjine/mechanics/keywords/infinity.rb +46 -0
  122. data/lib/enjine/mechanics/keywords/kicker.rb +24 -6
  123. data/lib/enjine/mechanics/keywords/miracle.rb +142 -0
  124. data/lib/enjine/mechanics/keywords/ninjutsu.rb +101 -0
  125. data/lib/enjine/mechanics/keywords/offspring.rb +48 -0
  126. data/lib/enjine/mechanics/keywords/persist_and_undying.rb +42 -0
  127. data/lib/enjine/mechanics/keywords/protection.rb +24 -8
  128. data/lib/enjine/mechanics/keywords/riot.rb +13 -10
  129. data/lib/enjine/mechanics/keywords/split_second.rb +29 -0
  130. data/lib/enjine/mechanics/keywords/storm_and_cascade.rb +5 -0
  131. data/lib/enjine/mechanics/keywords/void.rb +34 -0
  132. data/lib/enjine/mechanics/linked.rb +33 -3
  133. data/lib/enjine/mechanics/mana_spent.rb +1 -1
  134. data/lib/enjine/mechanics/selector.rb +62 -18
  135. data/lib/enjine/mechanics/subtypes/case.rb +68 -0
  136. data/lib/enjine/mechanics/triggers/base.rb +13 -4
  137. data/lib/enjine/mechanics/triggers/casting.rb +15 -9
  138. data/lib/enjine/mechanics/triggers/combat.rb +32 -12
  139. data/lib/enjine/mechanics/triggers/crime.rb +28 -0
  140. data/lib/enjine/mechanics/triggers/damage.rb +33 -18
  141. data/lib/enjine/mechanics/triggers/draw_and_discard.rb +19 -6
  142. data/lib/enjine/mechanics/triggers/keyword_actions.rb +17 -0
  143. data/lib/enjine/mechanics/triggers/rooms.rb +49 -0
  144. data/lib/enjine/mechanics/triggers/tap_and_counters.rb +20 -9
  145. data/lib/enjine/mechanics/triggers/zone_changes.rb +24 -9
  146. data/lib/enjine/mechanics/types/battle.rb +130 -0
  147. data/lib/enjine/mechanics/values.rb +84 -30
  148. data/lib/enjine/replacements.rb +21 -2
  149. data/lib/enjine/rules_monitor.rb +56 -71
  150. data/lib/enjine/snapshot/deep_copy.rb +23 -0
  151. data/lib/enjine/snapshot/player_checks.rb +10 -2
  152. data/lib/enjine/snapshot.rb +2 -1
  153. data/lib/enjine/state.rb +2 -2
  154. data/lib/enjine/verify.rb +10 -3
  155. data/lib/enjine/version.rb +1 -1
  156. data/lib/enjine/view.rb +50 -14
  157. data/lib/enjine.rb +1 -1
  158. metadata +34 -1
@@ -7,43 +7,56 @@ 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
14
15
  # "self" the source permanent, or the source card while it's still in
15
16
  # the graveyard, hand or exile ("return this card from your
16
- # graveyard", CR 113.6m; it's a new object once it moves, CR 400.7)
17
+ # graveyard", CR 113.6m; it's a new object once it moves, CR 400.7).
18
+ # In a leaves-the-battlefield trigger of the source itself ("when
19
+ # this dies, exile it"), the new object it became in the public zone
20
+ # it went to, while it's still there (CR 400.7e, 603.10a). Amounts
21
+ # read a source that has left as it last existed (Amount.refs).
17
22
  # "you" the controller
18
23
  # "each_opponent" every opponent
19
24
  # "attached" what the source is attached to ("enchanted creature", CR 303.4m)
20
25
  # {"each": "creature", "controller": "you"} every matching object (you | opponent | any)
21
26
  # {"result": 0} what effect 0 of this list moved or created (CR 400.7j)
27
+ # {"result": 0, "for": card spec} only those that match ("if the exiled card is a land card")
22
28
  # {"controller_of": selector} the controller of each referenced object ("its controller");
23
29
  # one that has left the battlefield as it last existed (CR 608.2h)
24
30
  # {"triggering": "object" | "player"} what the trigger event was about ("that creature",
25
31
  # "that player", CR 603.2); see Triggers.event_facts
32
+ # {"triggering": "objects"} a `batched` trigger's objects, one per occurrence in the batch
33
+ # ("those creatures", CR 603.2c)
26
34
  # {"triggering": "targets"} the targets of the spell a cast trigger saw that matched its
27
35
  # `targets` filter ("those creatures", CR 601.2c)
28
36
  # {"triggering": "ability"} the activated or triggered ability on the stack the trigger
29
37
  # event was about ("copy that ability", CR 707.10)
30
38
  # "watched" what a delayed triggered ability watches (CR 603.7c)
31
39
  # "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)
40
+ # "exiled_for_cost" cards this spell or ability exiled to pay its cost, still in exile
41
+ # ("copy those exiled cards", CR 607.2a)
42
+ # "gift_recipient" the opponent chosen for the spell's gift (CR 702.174a)
33
43
  # "granter" the permanent whose static ability granted this ability ("Return
34
44
  # Trusty Boomerang to its owner's hand", CR 607.1a), while it's still
35
45
  # on the battlefield; the source itself for an ability it has printed
36
46
  class Selector
37
47
  CONTROLLERS = %w[you opponent any].freeze
38
- TRIGGERING = %w[object player targets ability].freeze
48
+ TRIGGERING = %w[object objects player targets ability].freeze
39
49
  # Where "self" finds its source (CR 113.6m); a source that changed zones is a new object (CR 400.7).
40
50
  SELF_ZONES = %i[battlefield graveyard exile hand].freeze
51
+ # Where a leaves-the-battlefield trigger can still find the object its source became (CR 400.7e).
52
+ PUBLIC_ZONES = %w[battlefield graveyard exile stack command].freeze
41
53
 
42
54
  attr_reader :kind, :value, :controller
43
55
 
44
56
  def self.build(data, errors, path, targets_count:, results_count: 0)
45
57
  case data
46
- when "self", "you", "each_opponent", "attached", "watched", "exiled_with", "gift_recipient", "granter" then new(data.to_sym)
58
+ when "self", "you", "each_opponent", "attached", "watched", "exiled_with", "exiled_for_cost", "gift_recipient", "granter"
59
+ new(data.to_sym)
47
60
  when Hash
48
61
  data = data.transform_keys(&:to_s)
49
62
  return build_result(data, errors, path, results_count) if data.key?("result")
@@ -59,12 +72,12 @@ module Enjine
59
72
 
60
73
  # {"result": n}: n must name an earlier effect of the same list (a nested list sees its parent's).
61
74
  def self.build_result(data, errors, path, results_count)
62
- Mechanics.check_keys(data, %w[result], errors, path)
75
+ Mechanics.check_keys(data, %w[result for], errors, path)
63
76
  index = data["result"]
64
77
  unless index.is_a?(Integer) && index >= 0 && index < results_count
65
78
  errors << "#{path}.result: must refer to one of the #{results_count} earlier effects"
66
79
  end
67
- new(:result, index)
80
+ new(:result, index, spec: data.key?("for") ? CardSpec.build(data["for"], errors, "#{path}.for") : nil)
68
81
  end
69
82
 
70
83
  def self.build_controller_of(data, errors, path, targets_count, results_count)
@@ -100,17 +113,18 @@ module Enjine
100
113
  end
101
114
  end
102
115
 
103
- def initialize(kind, value = nil, controller = nil)
116
+ def initialize(kind, value = nil, controller = nil, spec: nil)
104
117
  @kind = kind
105
118
  @value = value
106
119
  @controller = controller
120
+ @spec = spec
107
121
  freeze
108
122
  end
109
123
 
110
124
  def to_s
111
125
  case kind
112
126
  when :target then "target ##{value + 1}"
113
- when :result then "the object(s) from effect ##{value + 1}"
127
+ when :result then "the #{@spec ? "#{@spec}(s)" : "object(s)"} from effect ##{value + 1}"
114
128
  when :controller_of then "the controller of #{value}"
115
129
  when :self then "itself"
116
130
  when :attached then "the attached permanent"
@@ -119,6 +133,7 @@ module Enjine
119
133
  when :you then "you"
120
134
  when :each_opponent then "each opponent"
121
135
  when :exiled_with then "the cards exiled with it"
136
+ when :exiled_for_cost then "the cards exiled to pay its cost"
122
137
  when :gift_recipient then "the chosen opponent"
123
138
  when :granter then "the permanent granting it"
124
139
  when :each then "each #{value.primitive_name.tr("_",
@@ -127,12 +142,14 @@ module Enjine
127
142
  end
128
143
  end
129
144
 
130
- # Resolves to a list of references (object ids / "player:<name>") at resolution time.
131
- def resolve(engine, ctx)
145
+ # Resolves to a list of references (object ids / "player:<name>") at resolution time. `follow: false`: "self"
146
+ # never names the new object a source that left the battlefield became (an amount reads it as it last
147
+ # existed instead, CR 608.2h).
148
+ def resolve(engine, ctx, follow: true)
132
149
  case kind
133
150
  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))
135
- when :self then SELF_ZONES.include?(engine.state.objects[ctx.source]&.zone) ? [ctx.source] : []
151
+ when :result then results(engine, ctx)
152
+ when :self then own(engine, ctx, follow)
136
153
  when :attached then attached(engine, ctx)
137
154
  when :triggering then triggering(engine, ctx)
138
155
  when :watched, :gift_recipient then remembered(engine, ctx)
@@ -141,13 +158,13 @@ module Enjine
141
158
  engine.state.players.each_index.reject { it == ctx.controller }.map { Filters.player_ref(engine, it) }
142
159
  when :each then each(engine, ctx)
143
160
  when :controller_of then controllers(engine, ctx)
144
- when :exiled_with then Linked.exiled_with(engine, ctx.source, ctx)
161
+ when :exiled_with, :exiled_for_cost then Linked.exiled(engine, kind, ctx)
145
162
  when :granter then granter(engine, ctx)
146
163
  end
147
164
  end
148
165
 
149
166
  # What the resolving triggered ability remembers (the event facts, watched objects).
150
- def self.memory(engine, ctx) = ctx.memory || engine.state.objects[ctx.object]&.memory || {}
167
+ def self.memory(engine, ctx) = ctx.memory || engine.find_object(ctx.object)&.memory || {}
151
168
 
152
169
  # What a delayed trigger watches; the opponent chosen for a gift, kept by the spell or the permanent it
153
170
  # became (CR 702.174b).
@@ -159,22 +176,49 @@ module Enjine
159
176
 
160
177
  private
161
178
 
179
+ # CR 400.7e: an ability that triggered on its source leaving the battlefield can find the new object it became
180
+ # in the public zone it moved to ("source_moved_to", Engine::Triggering#queue_trigger), if it's still there.
181
+ def own(engine, ctx, follow)
182
+ return [ctx.source] if SELF_ZONES.include?(engine.state.objects[ctx.source]&.zone)
183
+ return [] unless follow
184
+
185
+ moved = Selector.memory(engine, ctx)["source_moved_to"]
186
+ obj = moved && engine.state.objects[moved]
187
+ obj && PUBLIC_ZONES.include?(obj.zone.to_s) && !obj.memory["face_down"] ? [moved] : []
188
+ end
189
+
190
+ def results(engine, ctx)
191
+ found = Array(ctx.results&.[](value))
192
+ @spec ? found.select { result_matches?(engine, ctx, it) } : found
193
+ end
194
+
195
+ # A result still in its zone is matched as it is now; one that has since moved (a new object, CR 400.7) as
196
+ # the card it was when the effect moved it, if the context remembers it (CR 608.2h: "if it's a land card"
197
+ # after "return it to the battlefield").
198
+ def result_matches?(engine, ctx, id)
199
+ obj = engine.state.objects[id]
200
+ return @spec.match?(engine, obj, ctx) if obj
201
+
202
+ card = ctx.result_cards&.[](id) or return false
203
+ @spec.chars_match?(engine, engine.catalog.fetch(card).printed_characteristics, ctx)
204
+ end
205
+
162
206
  def controllers(engine, ctx)
163
- Array(value&.resolve(engine, ctx)).filter_map do |ref|
207
+ Array(value&.resolve(engine, ctx, follow: false)).filter_map do |ref|
164
208
  obj = engine.state.objects[ref]
165
209
  controller = if obj && %i[battlefield
166
210
  stack].include?(obj.zone)
167
211
  obj.controller
168
212
  else
169
213
  engine.resolving_last_known(ref)&.fetch("controller") ||
170
- engine.state.objects[ctx.object]&.memory&.dig(Linked::LKI, ref) || obj&.controller
214
+ engine.find_object(ctx.object)&.memory&.dig(Linked::LKI, ref) || obj&.controller
171
215
  end
172
216
  controller && Filters.player_ref(engine, controller)
173
217
  end.uniq
174
218
  end
175
219
 
176
220
  def granter(engine, ctx)
177
- id = engine.state.objects[ctx.object]&.memory&.[]("granter") || ctx.source
221
+ id = engine.find_object(ctx.object)&.memory&.[]("granter") || ctx.source
178
222
  engine.state.battlefield.include?(id) ? [id] : []
179
223
  end
180
224
 
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../abilities/designated"
4
+
5
+ module Enjine
6
+ module Mechanics
7
+ module Effects
8
+ # "This Case becomes solved" (CR 720.3a-b): each referenced permanent that isn't solved becomes solved.
9
+ class Solve < Effect
10
+ primitive :effect, :solve,
11
+ description: "Each referenced permanent (default: this one) becomes solved if it isn't (a designation it " \
12
+ "keeps until it leaves the battlefield). Usually written through `to_solve`.",
13
+ rules: %w[720.3a 720.3b],
14
+ params: { "to" => "selector of permanents (default self)" },
15
+ example: { "solve" => {} }
16
+
17
+ def self.keys = %w[to if]
18
+ def self.default_to = "self"
19
+
20
+ def apply(engine, ctx)
21
+ refs(engine, ctx).each { (obj = engine.state.objects[it]) && engine.designate!(obj, "solved") }
22
+ end
23
+
24
+ def to_s = "#{to} becomes solved"
25
+ end
26
+ end
27
+
28
+ module Abilities
29
+ # A Case's "To solve — [condition]" (CR 720.3a): "At the beginning of your end step, if [condition] and this
30
+ # Case is not solved, this Case becomes solved." Written {"to_solve": condition}; it builds that triggered ability.
31
+ class ToSolve < Primitive
32
+ primitive :ability, :to_solve,
33
+ description: "A Case's \"To solve — [condition]\": at the beginning of your end step, if the condition " \
34
+ "holds and this Case isn't solved, it becomes solved (an intervening-if triggered ability). " \
35
+ "The parameter is the condition itself.",
36
+ rules: %w[720.3a 720.3b 603.4],
37
+ params: { "(condition)" => "any condition, e.g. {\"hand_size\": \"you\", \"at_most\": 0}" },
38
+ example: { "to_solve" => { "amount" => { "life_gained_this_turn" => "you" }, "at_least" => 5 } }
39
+
40
+ def self.build(params, errors, path, **)
41
+ unless params.is_a?(Hash) && !params.empty?
42
+ errors << "#{path}: must be a condition, e.g. {\"amount\": {\"life_gained_this_turn\": \"you\"}, \"at_least\": 5}"
43
+ return nil
44
+ end
45
+ data = { "when" => { "step_begins" => { "step" => "end", "whose" => "your" } },
46
+ "if" => { "all" => [params, { "solved" => false }] }, "effects" => [{ "solve" => {} }] }
47
+ TriggeredAbility.build(data, errors, path)
48
+ end
49
+ end
50
+
51
+ # "Solved — [ability]" (CR 702.169, 720.3c): the ability works only while this Case is solved (see
52
+ # DesignatedAbility). {"solved": {"activated": {...}}}.
53
+ class Solved < Primitive
54
+ primitive :ability, :solved,
55
+ description: "A Case's \"Solved — [ability]\": one static, triggered or activated ability that works " \
56
+ "only while this permanent is solved (a static applies as long as it's solved, a triggered " \
57
+ "ability triggers only if it's solved, an activated ability can be activated only if it's " \
58
+ "solved). Condition `{\"solved\": true}` reads the designation.",
59
+ rules: %w[702.169a 702.169b 702.169c 702.169d 720.3b 720.3c],
60
+ params: { "(ability)" => "{\"static\" | \"triggered\" | \"activated\": {...}}" },
61
+ example: { "solved" => { "activated" => { "cost" => ["sacrifice"],
62
+ "effects" => [{ "draw" => { "amount" => 1 } }] } } }
63
+
64
+ def self.build(params, errors, path, **) = DesignatedAbility.build("solved", params, errors, path)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -11,15 +11,17 @@ module Enjine
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
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
+ # types a permanent that left had as it last existed ("if it was a creature", CR 603.10a), and "lki" how it
15
+ # last existed (its counters, power, toughness, mana value and whether it was a token, CR 603.10a, 608.2h), so
16
+ # "the +1/+1 counters it had" reads them after it left ({"counters": {"of": {"triggering": "object"}}}).
15
17
  def self.event_facts(engine, event)
16
18
  facts = {}
17
19
  case event
18
20
  when Events::ZoneChanged
19
21
  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"
21
- when Events::SpellCast, Events::DilemmaFaced, Events::Tapped, Events::AttackerDeclared, Events::BecameBlocked,
22
- Events::BlockerDeclared
22
+ departed_facts(engine, event, facts) if event.from == "battlefield"
23
+ when Events::SpellCast, Events::DilemmaFaced, Events::Tapped, Events::Untapped, Events::AttackerDeclared,
24
+ Events::BecameBlocked, Events::BlockerDeclared, Events::CrimeCommitted
23
25
  facts["object"] = event.object
24
26
  when Events::AbilityActivated then facts.merge!("object" => event.source, "ability" => event.object)
25
27
  when Events::TriggerPutOnStack then facts["ability"] = event.object
@@ -35,6 +37,13 @@ module Enjine
35
37
  facts.compact
36
38
  end
37
39
 
40
+ # A permanent that left the battlefield, as it last existed (CR 603.10a): plain data, kept by the trigger.
41
+ def self.departed_facts(engine, event, facts)
42
+ lki = engine.last_known(event.old_id) or return
43
+ facts["types"] = lki["types"].map(&:to_s)
44
+ facts["lki"] = engine.last_known_memory(lki).merge("token" => lki["token"] == true)
45
+ end
46
+
38
47
  # CR 601.2h: the mana spent to cast "that spell" and the sources of what artifacts made, as it was cast;
39
48
  # storm's "that many" (CR 702.40a).
40
49
  def self.cast_facts(engine, event, facts)
@@ -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.
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enjine
4
+ module Mechanics
5
+ module Triggers
6
+ # "Whenever you commit a crime" / "whenever an opponent commits a crime" (CR 700.13): once per spell cast,
7
+ # ability activated or triggered ability put on the stack that targets an opponent, something an opponent
8
+ # controls or a card in an opponent's graveyard (Engine::Crimes). "That spell or ability" is
9
+ # {"triggering": "object"}; the criminal is {"triggering": "player"}.
10
+ class CommitsCrime < PlayerTrigger
11
+ primitive :trigger, :commits_crime,
12
+ description: "A player commits a crime: they cast a spell, activate an ability or put a triggered " \
13
+ "ability on the stack that targets an opponent, a permanent, spell or ability an " \
14
+ "opponent controls, or a card in an opponent's graveyard (once per spell or ability). " \
15
+ "`controller`: you (default) | opponent | any. {\"triggering\": \"object\"} is that " \
16
+ "spell or ability. Amount `{\"crimes_this_turn\": player}` counts them.",
17
+ rules: %w[700.13 603.2],
18
+ params: { "controller" => "you | opponent | any" },
19
+ example: { "commits_crime" => {} }
20
+
21
+ def event_class = Events::CrimeCommitted
22
+ def match?(engine, event, source) = player_ok?(engine, event.player, source)
23
+
24
+ def to_s = "Whenever #{who} commit#{"s" unless @controller == "you"} a crime"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -20,49 +20,64 @@ module Enjine
20
20
  end
21
21
 
22
22
  # "Whenever ~ deals combat damage to a player" / "Whenever a source you control deals noncombat damage
23
- # to an opponent".
23
+ # to an opponent" / "Whenever one or more creatures you control deal combat damage to a player" (`batched`).
24
24
  class DealsDamage < Trigger
25
25
  include DamageKind
26
+ include Batched
26
27
 
27
- RECIPIENTS = %w[player opponent creature any].freeze
28
+ RECIPIENTS = %w[player opponent you creature planeswalker battle permanent any].freeze
29
+ PERMANENT_TYPES = %w[creature planeswalker battle].freeze
28
30
 
29
31
  primitive :trigger, :deals_damage,
30
32
  description: "The subject deals damage. `combat: true` only counts combat damage, `noncombat: true` " \
31
- "only noncombat damage; `to`: player | opponent (of the source's controller) | creature " \
32
- "| any (default). Triggers once per damage event (each combat damage step is one, so " \
33
- "double strike can trigger twice).",
33
+ "only noncombat damage; `to`: player | opponent | you (players, relative to the " \
34
+ "source's controller) | creature | planeswalker | battle | permanent | any (default), " \
35
+ "or a list of them (\"a player or planeswalker\"). Triggers once per damage event " \
36
+ "(each combat damage step is one, so double strike can trigger twice). `batched: true`: " \
37
+ "\"one or more ... deal damage\", once per event batch (a combat damage step); then " \
38
+ "{\"triggering\": \"objects\"} are the sources that dealt it and {\"triggering\": " \
39
+ "\"amount\"} the number of damage events.",
34
40
  rules: %w[510.2 603.2 603.2c 120.2a 120.2b 120.3],
35
41
  params: { "subject" => "\"self\" (default) or {\"each\": filter, ...}", "combat" => "true | false",
36
- "noncombat" => "true | false", "to" => RECIPIENTS.join(" | ") },
42
+ "noncombat" => "true | false", "to" => "#{RECIPIENTS.join(" | ")}, or a list of them",
43
+ "batched" => "true | false" },
37
44
  example: { "deals_damage" => { "combat" => true, "to" => "player" } }
38
45
 
39
- def self.keys = %w[subject combat noncombat to]
46
+ def self.keys = %w[subject combat noncombat to batched]
40
47
  def event_class = Events::DamageDealt
41
48
 
42
49
  def setup(params, errors, path)
43
50
  setup_kind(params, errors, path)
44
- @to = params.fetch("to", "any")
45
- errors << "#{path}.to: must be one of #{RECIPIENTS.join(", ")}" unless RECIPIENTS.include?(@to)
51
+ setup_batched(params, errors, path)
52
+ @to = Array(params.fetch("to", "any"))
53
+ return if !@to.empty? && (@to - RECIPIENTS).empty?
54
+
55
+ errors << "#{path}.to: must be one of #{RECIPIENTS.join(", ")}, or a list of them"
46
56
  end
47
57
 
48
58
  def match?(engine, event, source)
49
- return false unless kind_ok?(event) && recipient_ok?(engine, event.target, source)
59
+ return false unless kind_ok?(event) && @to.any? { recipient_ok?(engine, event.target, source, it) }
50
60
 
51
61
  obj = engine.state.objects[event.source] or return false
52
62
  @subject.match?(engine, event.source, obj.controller, obj.card, source, types: engine.chars(obj).types)
53
63
  end
54
64
 
55
- def recipient_ok?(engine, ref, source)
65
+ # The recipient as it is dealt damage (CR 120.3): a player relative to the source's controller, or a
66
+ # permanent by its card types.
67
+ def recipient_ok?(engine, ref, source, to)
68
+ return true if to == "any"
69
+
56
70
  index = Filters.player_index(engine, ref)
57
- case @to
58
- when "player" then !index.nil?
59
- when "opponent" then !index.nil? && index != source["controller"]
60
- when "creature" then engine.state.objects.key?(ref) && engine.chars(ref).creature?
61
- else true
62
- end
71
+ return %w[player opponent you].include?(to) && (to == "player" || (to == "you") == (index == source["controller"])) if index
72
+ return false unless engine.state.objects.key?(ref)
73
+
74
+ to == "permanent" || (PERMANENT_TYPES.include?(to) && engine.chars(ref).type?(to.to_sym))
63
75
  end
64
76
 
65
- def to_s = "Whenever #{@subject} deals #{kind_words}damage#{" to a#{"n" if @to == "opponent"} #{@to}" unless @to == "any"}"
77
+ def to_s
78
+ to = @to == ["any"] ? "" : " to a#{"n" if @to.first == "opponent"} #{@to.join(" or ")}"
79
+ "Whenever #{"one or more of " if @batched}#{@subject} deal#{"s" unless @batched} #{kind_words}damage#{to}"
80
+ end
66
81
  end
67
82
 
68
83
  # "Whenever this creature is dealt damage", "Whenever enchanted creature is dealt damage", "Whenever an
@@ -59,26 +59,39 @@ module Enjine
59
59
 
60
60
  primitive :trigger, :cycles,
61
61
  description: "A player (`controller`: you (default) | opponent | any) cycles a card (activates its " \
62
- "cycling or typecycling ability; optional `card` filter).",
63
- rules: %w[702.29a 702.29e],
64
- params: { "controller" => PLAYERS.join(" | "), "card" => "card filter" },
62
+ "cycling or typecycling ability; optional `card` filter). `self: true`: \"when you " \
63
+ "cycle this card\", an ability of the card with `zone: \"graveyard\"` (where the cycled " \
64
+ "card is); its X is the cycling ability's X.",
65
+ rules: %w[702.29a 702.29c 702.29e 113.6k],
66
+ params: { "controller" => PLAYERS.join(" | "), "card" => "card filter", "self" => "true" },
65
67
  example: { "cycles" => { "controller" => "you" } }
66
68
 
67
- def self.keys = %w[controller card]
69
+ def self.keys = %w[controller card self]
68
70
  def event_class = Events::AbilityActivated
69
71
 
70
72
  def setup(params, errors, path)
71
73
  super
72
74
  setup_card(params, errors, path)
75
+ @self = params["self"] == true
76
+ errors << "#{path}.self: must be true" unless params["self"].nil? || @self
73
77
  end
74
78
 
75
79
  def match?(engine, event, source)
76
80
  ability = engine.catalog.fetch(event.card).ability(event.ability)
77
81
  ability.respond_to?(:cycling?) && ability.cycling? && player_ok?(engine, event.player, source) &&
78
- card_ok?(engine, event.card)
82
+ card_ok?(engine, event.card) && (!@self || cycled_self?(engine, event, source))
79
83
  end
80
84
 
81
- def to_s = "Whenever #{who} cycle#{"s" unless @controller == "you"} a #{card_words}"
85
+ # CR 702.29c: the source is the very card cycled (Costs::CycleSelf remembers the hand object it was).
86
+ def cycled_self?(engine, event, source)
87
+ engine.state.objects[source["id"]]&.memory&.[]("cycled_from") == event.source
88
+ end
89
+
90
+ def to_s
91
+ return "When you cycle this card" if @self
92
+
93
+ "Whenever #{who} cycle#{"s" unless @controller == "you"} a #{card_words}"
94
+ end
82
95
  end
83
96
  end
84
97
  end
@@ -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