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
@@ -42,7 +42,7 @@ module Enjine
42
42
  example: { "static" => { "affects" => { "each" => "creature", "controller" => "you" },
43
43
  "pump" => { "power" => 1, "toughness" => 1 } } }
44
44
 
45
- GRANTABLE = %i[triggered mana activated loyalty hexproof_from protection_from tapping_power].freeze
45
+ GRANTABLE = %i[triggered mana activated loyalty hexproof_from protection_from tapping_power cant_be_blocked_by].freeze
46
46
  # Data that would make the layer pass depend on its own result (CR 613.8).
47
47
  LIVE = %w[power toughness greatest_power attacking blocking tapped].freeze
48
48
  KEYS = %w[affects if pump keywords creature_types abilities types subtypes loses_abilities colors
@@ -20,16 +20,20 @@ module Enjine
20
20
  # {"power": {"at_most": 1}} also toughness: its power/toughness as the card is (CR 208.1)
21
21
  # {"layout": "adventure"} "a card that has an Adventure": the card's layout (CR 716,
22
22
  # CardDefinition::Faces)
23
+ # {"subtypes": "chosen"} of the creature type chosen for the source (CR 607.2d)
24
+ # {"same_name_as": filter} the same name as a permanent the filter matches, e.g. "a
25
+ # legendary permanent you control" (CR 201.2a)
26
+ # The last two read the context (an EffectContext): the source's choices and its controller.
23
27
  class CardSpec
24
28
  KEYS = %w[types not_types subtypes supertypes name any_of permanent mana_value colors color_count keywords
25
- not_subtypes power toughness layout].freeze
29
+ not_subtypes power toughness layout same_name_as].freeze
26
30
  SUBTYPE = /\A[a-z][a-z0-9_]*\z/
27
31
  # CR 110.4a: the permanent card types (battles have no primitive yet, so none matches that way).
28
32
  PERMANENT_TYPES = %i[artifact battle creature enchantment land planeswalker].freeze
29
33
  BOUNDS = %w[at_least at_most equal].freeze
30
34
 
31
35
  attr_reader :types, :not_types, :subtypes, :supertypes, :name, :any_of, :permanent, :mana_value, :colors, :color_count,
32
- :keywords, :not_subtypes, :stats, :layout
36
+ :keywords, :not_subtypes, :stats, :layout, :relations
33
37
 
34
38
  def self.build(data, errors, path)
35
39
  return ANY if data.nil? || data == "card"
@@ -38,14 +42,15 @@ module Enjine
38
42
  data = data.transform_keys(&:to_s)
39
43
  Mechanics.check_keys(data, KEYS, errors, path)
40
44
  errors << "#{path}: needs at least one of #{KEYS.join(", ")}" if data.empty?
45
+ chosen = data["subtypes"] == "chosen"
41
46
  new(types: names(data, "types", :type, errors, path), supertypes: names(data, "supertypes", :supertype, errors, path),
42
- subtypes: subtype_list(data, errors, path), name: name_param(data, errors, path),
47
+ subtypes: chosen ? [] : subtype_list(data, errors, path), name: name_param(data, errors, path),
43
48
  any_of: alternatives(data, errors, path), not_types: names(data, "not_types", :type, errors, path),
44
49
  permanent: permanent_param(data, errors, path), mana_value: bounds_param(data, "mana_value", errors, path),
45
50
  colors: colors_param(data, errors, path), color_count: bounds_param(data, "color_count", errors, path),
46
51
  keywords: names(data, "keywords", :keyword, errors, path), not_subtypes: subtype_list(data, errors, path, "not_subtypes"),
47
52
  stats: %w[power toughness].to_h { [it, bounds_param(data, it, errors, path)] }.reject { |_, b| b.empty? },
48
- layout: layout_param(data, errors, path))
53
+ layout: layout_param(data, errors, path), relations: relations_param(data, chosen, errors, path))
49
54
  end
50
55
 
51
56
  def self.layout_param(data, errors, path)
@@ -56,6 +61,13 @@ module Enjine
56
61
  value
57
62
  end
58
63
 
64
+ # What the spec compares with its context: the chosen creature type, permanents to share a name with.
65
+ def self.relations_param(data, chosen, errors, path)
66
+ raw = data["same_name_as"]
67
+ same = raw && Mechanics.build_entry(:filter, raw, errors, "#{path}.same_name_as")
68
+ { "chosen_subtype" => (true if chosen), "same_name_as" => same }.compact
69
+ end
70
+
59
71
  def self.colors_param(data, errors, path)
60
72
  list(data, "colors", errors, path).filter_map do |raw|
61
73
  color = raw.to_s.upcase.to_sym
@@ -127,11 +139,13 @@ module Enjine
127
139
  end
128
140
 
129
141
  def initialize(types: [], subtypes: [], supertypes: [], name: nil, any_of: [], not_types: [], permanent: false,
130
- mana_value: {}, colors: [], color_count: {}, keywords: [], not_subtypes: [], stats: {}, layout: nil)
142
+ mana_value: {}, colors: [], color_count: {}, keywords: [], not_subtypes: [], stats: {}, layout: nil,
143
+ relations: {})
131
144
  @keywords = keywords.freeze
132
145
  @not_subtypes = not_subtypes.freeze
133
146
  @stats = stats.freeze
134
147
  @layout = layout
148
+ @relations = relations.freeze
135
149
  @colors = colors.freeze
136
150
  @color_count = color_count.freeze
137
151
  @types = types.freeze
@@ -145,11 +159,19 @@ module Enjine
145
159
  freeze
146
160
  end
147
161
 
162
+ # The spec without what it reads from its context (the chosen type, a shared name).
163
+ def unrelated
164
+ return self if relations.empty?
165
+
166
+ CardSpec.new(types:, subtypes:, supertypes:, name:, any_of:, not_types:, permanent:, mana_value:, colors:, color_count:,
167
+ keywords:, not_subtypes:, stats:, layout:)
168
+ end
169
+
148
170
  # True when the spec states no quality: "a card" (CR 701.23d).
149
171
  def any?
150
172
  types.empty? && not_types.empty? && subtypes.empty? && supertypes.empty? && name.nil? && any_of.empty? &&
151
173
  !permanent && mana_value.empty? && colors.empty? && color_count.empty? && keywords.empty? && not_subtypes.empty? &&
152
- stats.empty? && layout.nil?
174
+ stats.empty? && layout.nil? && relations.empty?
153
175
  end
154
176
 
155
177
  # Whether a card object matches, by its current characteristics (CR 109.3). `ctx` (an EffectContext)
@@ -163,7 +185,7 @@ module Enjine
163
185
  (name.nil? || chars.name == name) && (!permanent || chars.types.intersect?(PERMANENT_TYPES)) &&
164
186
  (colors.empty? || colors.intersect?(chars.colors)) && bounded?(mana_value, chars.mana_cost&.mana_value.to_i, engine, ctx) &&
165
187
  bounded?(color_count, chars.colors.size, engine, ctx) && (any_of.empty? || any_of.any? { it.chars_match?(engine, chars, ctx) }) &&
166
- other_qualities?(engine, chars, ctx)
188
+ other_qualities?(engine, chars, ctx) && (relations.empty? || related?(engine, chars, ctx))
167
189
  end
168
190
 
169
191
  # Keywords (CR 702.1), non-subtypes and power/toughness (CR 208.1; a "*" with no value is 0 off the battlefield,
@@ -178,10 +200,8 @@ module Enjine
178
200
  return "#{any_of.map { it.to_s.delete_suffix(" card") }.join(" or ")} card" unless any_of.empty?
179
201
  return "card named #{name}" if name
180
202
 
181
- words = not_types.map { "non#{it}" } + not_subtypes.map { "non-#{it.to_s.capitalize}" } + colors + supertypes +
182
- subtypes.map { it.to_s.capitalize } + types
183
- words << "permanent" if permanent
184
- text = (words.map { it.to_s.tr("_", " ") } + ["card"]).join(" ") + quality_words
203
+ text = (type_words.map { it.to_s.tr("_", " ") } + ["card"]).join(" ") + quality_words
204
+ text += " with the same name as a #{relations["same_name_as"]}" if relations["same_name_as"]
185
205
  bounds = { "mana value" => mana_value, "number of colors" => color_count, "power" => stats["power"] || {},
186
206
  "toughness" => stats["toughness"] || {} }.reject { |_, b| b.empty? }
187
207
  return text if bounds.empty?
@@ -189,6 +209,14 @@ module Enjine
189
209
  "#{text} with #{bounds.map { |what, b| "#{what} #{b.map { |bound, n| "#{bound.tr("_", " ")} #{n}" }.join(" and ")}" }.join(", ")}"
190
210
  end
191
211
 
212
+ # "nonland", "non-Avatar", colors, supertypes, subtypes (or "chosen-type"), types and "permanent".
213
+ def type_words
214
+ chosen = relations["chosen_subtype"] ? ["chosen-type"] : []
215
+ words = not_types.map { "non#{it}" } + not_subtypes.map { "non-#{it.to_s.capitalize}" } + colors + supertypes +
216
+ subtypes.map { it.to_s.capitalize } + chosen + types
217
+ permanent ? words + ["permanent"] : words
218
+ end
219
+
192
220
  # " with flying", " that has an Adventure".
193
221
  def quality_words
194
222
  text = keywords.empty? ? "" : " with #{keywords.join(" or ").tr("_", " ")}"
@@ -199,6 +227,21 @@ module Enjine
199
227
 
200
228
  private
201
229
 
230
+ # The chosen creature type (CR 607.2d) and a shared name (CR 201.2a: an object with no name shares none); both
231
+ # need the context.
232
+ def related?(engine, chars, ctx)
233
+ return false unless ctx
234
+
235
+ if relations["chosen_subtype"]
236
+ chosen = Filters.chosen_memory(engine, ctx.source, ctx)["chosen_type"]
237
+ return false unless chosen && chars.subtypes.include?(chosen.to_sym)
238
+ end
239
+ filter = relations["same_name_as"] or return true
240
+ !chars.name.to_s.empty? && engine.state.battlefield.any? do
241
+ filter.match_for?(engine, it, ctx.controller, ctx.source) && engine.chars(it).name == chars.name
242
+ end
243
+ end
244
+
202
245
  # CR 202.3: a card's mana value (0 without a mana cost, CR 202.3a), or its number of colors (CR 105.2), within
203
246
  # the bounds.
204
247
  def bounded?(bounds, value, engine, ctx)
@@ -73,42 +73,100 @@ module Enjine
73
73
 
74
74
  # `count` cards from one of your zones (`zone`) matching the card spec `card`, never the card the spell
75
75
  # or ability belongs to: "discard a card", "exile two cards from your graveyard" (CR 601.2h, 602.2b).
76
+ #
77
+ # "Exile any number of black cards from your graveyard with fifteen or more black mana symbols among their mana
78
+ # costs" (CR 107.4, 202.1): `count: "any"` with `symbols: {"color": "B", "at_least": 15}`. The player chooses
79
+ # cards one at a time (`choose_cost`) and may stop (`done`) once the chosen cards have enough symbols.
76
80
  class CardsCost < Choosing
77
- def self.keys = %w[count card]
81
+ # Where cards exiled to pay a cost are remembered on the spell or ability (selector "exiled_for_cost").
82
+ EXILED = "exiled_for_cost"
83
+
84
+ def self.keys = %w[count card symbols]
78
85
  def self.zone = raise(NotImplementedError)
79
86
 
80
87
  # `count` "X": as many as the X announced for the spell or ability (CR 601.2b, 107.3a).
81
88
  def self.build(params, errors, path, **)
82
89
  Mechanics.check_keys(params, keys, errors, path)
83
- count = if Mechanics.fetch_param(params, :count) == "X" then "X"
84
- else Mechanics.integer_param(params, :count, errors, path, default: 1, min: 1)
85
- end
90
+ count = Mechanics.fetch_param(params, :count)
91
+ count = Mechanics.integer_param(params, :count, errors, path, default: 1, min: 1) unless %w[X any].include?(count)
86
92
  raw = Mechanics.fetch_param(params, :card)
87
93
  spec = raw && CardSpec.build(raw, errors, "#{path}.card")
88
94
  new(spec, count, false, params, errors, path)
89
95
  end
90
96
 
97
+ def initialize(filter, count, other, params, errors, path)
98
+ @symbols = symbols_param(Mechanics.fetch_param(params, :symbols), errors, path)
99
+ errors << "#{path}.count: \"any\" needs `symbols`" if (count == "any") != !@symbols.nil?
100
+ super
101
+ end
102
+
103
+ def symbols_param(raw, errors, path)
104
+ return nil if raw.nil?
105
+
106
+ ok = raw.is_a?(Hash) && Mana::COLORS.include?(raw["color"].to_s.upcase.to_sym) && raw["at_least"].is_a?(Integer) &&
107
+ raw["at_least"].positive? && raw.keys.sort == %w[at_least color]
108
+ return { "color" => raw["color"].to_s.upcase.to_sym, "at_least" => raw["at_least"] } if ok
109
+
110
+ errors << "#{path}.symbols: must be {\"color\": W|U|B|R|G, \"at_least\": integer >= 1}"
111
+ nil
112
+ end
113
+
114
+ # The card spec sees who pays (CR 201.2a: "the same name as a legendary permanent you control").
91
115
  def candidates(engine, object)
116
+ ctx = EffectContext.new(object.controller, object.id, object.id, [])
92
117
  engine.player(object.controller).public_send(self.class.zone).select do |id|
93
- id != object.id && (filter.nil? || filter.match?(engine, engine.object(id)))
118
+ id != object.id && (filter.nil? || filter.match?(engine, engine.object(id), ctx))
94
119
  end
95
120
  end
96
121
 
97
122
  # "Exile X cards": the engine asks for as many as X (Engine::CastChoices#cost_chosen?); X can be at
98
123
  # most the number of matching cards (Engine::Costs#x_limit).
99
124
  def x? = count == "X"
100
- def choices_needed = x? ? 0 : super
101
- def payable?(engine, object) = x? || super
125
+ def any? = count == "any"
126
+ def choices_needed = x? || any? ? 0 : super
102
127
  def x_limit(engine, object) = candidates(engine, object).size
103
128
 
129
+ def payable?(engine, object)
130
+ return true if x?
131
+ return super unless any?
132
+
133
+ symbols_among(engine, candidates(engine, object)) >= @symbols["at_least"]
134
+ end
135
+
136
+ # An "any number" choice remembers its card's symbols (CR 107.4e: a hybrid symbol is each of its colors).
137
+ def choose!(engine, option)
138
+ entry = super
139
+ any? ? entry.merge("symbols" => symbols_among(engine, [option[:object]])) : entry
140
+ end
141
+
142
+ def finishable?(entries) = any? && entries.sum { it["symbols"].to_i } >= @symbols["at_least"]
143
+
144
+ # Engine::CastChoices#cost_chosen?: "any number" is chosen once enough symbols are and the player is done.
145
+ def chosen?(engine, object, entries, done)
146
+ return entries.size >= choices_needed unless any?
147
+
148
+ finishable?(entries) && (done || choice_options(engine, object, entries).empty?)
149
+ end
150
+
151
+ def symbols_among(engine, ids) = ids.sum { engine.chars(it).mana_cost&.symbols_of(@symbols["color"]).to_i }
152
+
104
153
  # As it becomes cast, the chosen cards still where they were chosen are paid (exiled by default).
105
154
  def pay_chosen!(engine, _object, entries)
106
155
  entries.map { it["object"] }.select { engine.state.objects[it]&.zone == self.class.zone }.each { pay_card!(engine, it) }
107
156
  end
108
157
 
109
- def pay_card!(engine, id) = engine.move!(id, :exile, reason: "cost")
158
+ # An exiled card is remembered by the spell or ability being cast ("copy those exiled cards").
159
+ def pay_card!(engine, id)
160
+ fresh = engine.move!(id, :exile, reason: "cost") or return
161
+ holder = engine.casting && engine.state.objects[engine.casting["object"]]
162
+ (holder.memory[EXILED] ||= []) << fresh.id if holder
163
+ end
110
164
 
111
- def cards_text = "#{count == 1 ? "a" : count} #{filter || "card"}#{"s" unless count == 1}"
165
+ def cards_text
166
+ many = { 1 => "a", "any" => "any number of" }.fetch(count, count)
167
+ text = "#{many} #{filter || "card"}#{"s" unless count == 1}"
168
+ @symbols ? "#{text} with #{@symbols["at_least"]} or more {#{@symbols["color"]}} symbols among their mana costs" : text
169
+ end
112
170
  end
113
171
  end
114
172
  end
@@ -6,10 +6,11 @@ module Enjine
6
6
  # [quality] permanent you control. Whether a [quality] was beheld looks at the object as it was beheld
7
7
  # (CR 701.4b).
8
8
  module Beholding
9
- # The cards in `index`'s hand and the permanents they control matching the card spec.
10
- def self.candidates(engine, index, spec)
9
+ # The cards in `index`'s hand and the permanents they control matching the card spec (in `ctx`, e.g. "of the
10
+ # chosen type").
11
+ def self.candidates(engine, index, spec, ctx = nil)
11
12
  perms = engine.state.battlefield.select { engine.object(it).controller == index }
12
- (perms + engine.player(index).hand).select { spec.match?(engine, engine.object(it)) }
13
+ (perms + engine.player(index).hand).select { spec.match?(engine, engine.object(it), ctx) }
13
14
  end
14
15
 
15
16
  # Reveals a card from the hand (CR 701.4a); the entry remembers what was beheld (CR 701.4b).
@@ -53,8 +54,23 @@ module Enjine
53
54
  super
54
55
  end
55
56
 
56
- # Not the card being cast (it is on the stack once casting begins).
57
- def candidates(engine, object) = Beholding.candidates(engine, object.controller, filter) - [object.id]
57
+ # Not the card being cast (it is on the stack once casting begins). "Of the chosen type" reads the type chosen
58
+ # for it (Costs::ChooseCreatureType).
59
+ def candidates(engine, object)
60
+ ctx = EffectContext.new(object.controller, object.id, object.id, [])
61
+ Beholding.candidates(engine, object.controller, filter, ctx) - [object.id]
62
+ end
63
+
64
+ # Before a type is chosen for "behold two creatures of that type", whether some type would do (CR 601.2b).
65
+ def payable?(engine, object)
66
+ return super unless filter.relations["chosen_subtype"] && !object.memory["chosen_type"]
67
+
68
+ others = Beholding.candidates(engine, object.controller, filter.unrelated) - [object.id]
69
+ others.flat_map { engine.chars(it).subtypes.uniq }.tally.any? do |type, n|
70
+ n >= count && Keywords::Changeling::CREATURE_TYPES.include?(type)
71
+ end
72
+ end
73
+
58
74
  def choose!(engine, option) = Beholding.behold!(engine, engine.object(option[:object]).controller, option[:object])
59
75
 
60
76
  def pay_chosen!(engine, object, entries)
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enjine
4
+ module Mechanics
5
+ module Costs
6
+ # "Choose a creature type and behold two creatures of that type" (CR 601.2b, 205.3m, 607.2d): choosing the
7
+ # type is part of the cost. It is a `choose_cost` decision, `{type: "cost", creature_type: "elf"}`, made in
8
+ # cost order (so before a later `behold` of `{"subtypes": "chosen"}`), and the spell remembers the type at
9
+ # once ("chosen_type", read by "of the chosen type" filters and card specs). Cancelling the cast forgets it.
10
+ class ChooseCreatureType < Primitive
11
+ primitive :cost, :choose_creature_type,
12
+ description: "Choose a creature type as part of the cost (a `choose_cost` decision with options " \
13
+ "`{type: \"cost\", creature_type: t}`). The spell remembers it as its chosen type: later " \
14
+ "costs and its effects read it with `\"subtypes\": \"chosen\"`.",
15
+ rules: %w[601.2b 205.3m 607.2d],
16
+ example: { "choose_creature_type" => {} }
17
+
18
+ INSTANCE = new.freeze
19
+
20
+ def self.build(params, errors, path, **)
21
+ Mechanics.check_keys(params, [], errors, path)
22
+ INSTANCE
23
+ end
24
+
25
+ def mana? = false
26
+ def choices_needed = 1
27
+ def payable?(_engine, _object) = true
28
+
29
+ def choice_options(_engine, _object, _chosen) = Chosen.values("creature_type", []).map { { creature_type: it } }
30
+
31
+ # The spell being cast remembers the type now, so a later cost of "that type" can be chosen (CR 601.2b).
32
+ def choose!(engine, option)
33
+ type = option[:creature_type].to_s
34
+ spell = engine.casting && engine.state.objects[engine.casting["object"]]
35
+ spell&.memory&.store("chosen_type", type)
36
+ { "creature_type" => type }
37
+ end
38
+
39
+ def to_s = "Choose a creature type"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -38,6 +38,9 @@ module Enjine
38
38
 
39
39
  def mana? = false
40
40
 
41
+ # The options with their amounts computed in `ctx` (Costs::PayLife#fixed).
42
+ def fixed(engine, ctx) = OneOf.new(options.map { it.respond_to?(:fixed) ? it.fixed(engine, ctx) : it })
43
+
41
44
  # Some option could be paid (a mana option is checked against the total cost by the engine).
42
45
  def payable?(engine, object) = options.any? { it.mana? || it.payable?(engine, object) }
43
46
  # Each option as text: "Behold a Goblin", "Pay {2}".
@@ -6,13 +6,17 @@ module Enjine
6
6
  # "Pay 2 life" / "Pay X life" as a cost (CR 119.4, 118.3b): {"pay_life": 2} or {"pay_life": "X"}. A
7
7
  # player can pay life only if their life total is at least that much; 0 life can always be paid
8
8
  # (CR 119.4b). X is announced with the spell or ability (CR 601.2b, 107.3a), at most the life total.
9
+ # An amount ("Ward—Pay life equal to Raubahn's power": {"pay_life": {"power": "self"}}) is computed as
10
+ # the cost is paid: while a spell or ability resolves (`pay`, ward) in its context (#fixed), while
11
+ # casting or activating for the object it's paid for.
9
12
  class PayLife < Primitive
10
13
  primitive :cost, :pay_life,
11
14
  description: "Pay that much life (you lose it). Only possible while your life total is at least " \
12
15
  "that much. `\"X\"` pays the X announced for the spell or ability (at most your life " \
13
- "total); it may be a spell's additional cost.",
14
- rules: %w[119.4 119.4b 118.3b 601.2b 107.3a],
15
- params: { "(value)" => "integer >= 1, or \"X\"" },
16
+ "total); it may be a spell's additional cost. An amount (e.g. {\"power\": \"self\"}, " \
17
+ "\"pay life equal to its power\") is computed as the cost is paid.",
18
+ rules: %w[119.4 119.4b 118.3b 601.2b 107.3a 702.21a],
19
+ params: { "(value)" => "integer >= 1, \"X\", or an amount" },
16
20
  example: { "pay_life" => 2 }
17
21
 
18
22
  attr_reader :amount
@@ -20,7 +24,10 @@ module Enjine
20
24
  def self.scalar_params? = true
21
25
 
22
26
  def self.build(params, errors, path, **)
23
- errors << "#{path}: must be an integer >= 1 or \"X\"" unless params == "X" || (params.is_a?(Integer) && params >= 1)
27
+ return new(Amount.build(params, errors, path, targets_count: 0)) if params.is_a?(Hash)
28
+
29
+ ok = params == "X" || (params.is_a?(Integer) && params >= 1)
30
+ errors << "#{path}: must be an integer >= 1, \"X\" or an amount" unless ok
24
31
  new(params)
25
32
  end
26
33
 
@@ -33,17 +40,29 @@ module Enjine
33
40
  def mana? = false
34
41
  def spell_cost? = true
35
42
  def x? = @amount == "X"
43
+ def computed? = @amount.respond_to?(:value)
36
44
  def x_limit(engine, object) = [engine.player(object.controller).life, 0].max
37
45
 
46
+ # The cost with its amount computed in `ctx` (an EffectContext): "its power" as a ward trigger resolves.
47
+ def fixed(engine, ctx) = computed? ? PayLife.new([@amount.value(engine, ctx), 0].max) : self
48
+
38
49
  # X is bounded by the life total as it's announced (Engine::Costs#x_limit).
39
- def payable?(engine, object) = x? || engine.player(object.controller).life >= @amount.to_i
50
+ def payable?(engine, object) = x? || engine.player(object.controller).life >= life(engine, object)
40
51
 
41
52
  def pay!(engine, object)
42
- life = x? ? engine.casting&.fetch("x", 0).to_i : @amount.to_i
43
- engine.change_life!(object.controller, -life) if life.positive?
53
+ n = x? ? engine.casting&.fetch("x", 0).to_i : life(engine, object)
54
+ engine.change_life!(object.controller, -n) if n.positive?
44
55
  end
45
56
 
46
- def to_s = "Pay #{@amount} life"
57
+ def to_s = computed? ? "Pay life equal to #{@amount}" : "Pay #{@amount} life"
58
+
59
+ private
60
+
61
+ def life(engine, object)
62
+ return @amount.to_i unless computed?
63
+
64
+ [@amount.value(engine, EffectContext.new(object.controller, object.id, object.id, [])), 0].max
65
+ end
47
66
  end
48
67
  end
49
68
  end
@@ -9,24 +9,27 @@ module Enjine
9
9
  primitive :cost, :remove_counters,
10
10
  description: "Remove `count` counters (of kind `counter`, or any kind) from among permanents you " \
11
11
  "control matching `filter` (default creature), one counter per choice. Removed as " \
12
- "it becomes cast.",
12
+ "it becomes cast. `self: true`: only from this permanent (\"Remove two counters from " \
13
+ "Ghost-Spider\").",
13
14
  rules: %w[602.2b 601.2h 118.3 122.1],
14
- params: { "count" => "integer", "filter" => "filter (default creature)", "counter" => "counter name (optional)" },
15
+ params: { "count" => "integer", "filter" => "filter (default creature)", "counter" => "counter name (optional)",
16
+ "self" => "true: from this permanent only" },
15
17
  example: { "remove_counters" => { "count" => 3 } }
16
18
 
17
- def self.keys = %w[filter count counter]
19
+ def self.keys = %w[filter count counter self]
18
20
  def self.default_filter = "creature"
19
21
 
20
22
  def initialize(filter, count, other, params, errors, path)
21
23
  @counter = Mechanics.fetch_param(params, :counter)
22
24
  errors << "#{path}.counter: #{Counters.error(@counter)}" if @counter && Counters.error(@counter)
25
+ @self = Mechanics.flag_param(params, :self, errors, path)
23
26
  super
24
27
  end
25
28
 
26
29
  # Counters still available: [object, kind] once per counter, minus those already chosen.
27
30
  def units(engine, object, chosen)
28
31
  taken = chosen.map { [it["object"], it["counter"]] }
29
- matching(engine, object).flat_map do |id|
32
+ (@self ? [object.id] : matching(engine, object)).flat_map do |id|
30
33
  engine.object(id).counters.flat_map do |kind, n|
31
34
  next [] unless @counter.nil? || kind == @counter
32
35
 
@@ -44,7 +47,7 @@ module Enjine
44
47
  entries.each { engine.remove_counters!(engine.object(it["object"]), it["counter"], 1) }
45
48
  end
46
49
 
47
- def to_s = "Remove #{count} #{"#{@counter} " if @counter}counters from among #{what}s you control"
50
+ def to_s = "Remove #{count} #{"#{@counter} " if @counter}counters from #{@self ? "this" : "among #{what}s you control"}"
48
51
  end
49
52
  end
50
53
  end
@@ -31,14 +31,26 @@ module Enjine
31
31
  # {T}: tap this permanent (CR 107.5, 602.5a).
32
32
  class Tap < Primitive
33
33
  primitive :cost, :tap,
34
- description: "{T} — tap the permanent the ability belongs to. Requires it to be untapped.",
35
- rules: %w[107.5 602.5a],
34
+ description: "{T} — tap the permanent the ability belongs to. Requires it to be untapped. " \
35
+ "`{\"tap\": \"granter\"}` instead taps the permanent whose static ability granted this " \
36
+ "ability (\"Tap Fishing Pole\"; not {T}, so summoning sickness doesn't matter).",
37
+ rules: %w[107.5 602.5a 602.2b 607.1a 302.6],
38
+ params: { "(value)" => "\"granter\": tap the granting permanent instead" },
36
39
  example: "tap"
37
40
 
38
41
  INSTANCE = new.freeze
39
42
 
43
+ # Its value may be the string "granter".
44
+ def self.scalar_params? = true
45
+
40
46
  def self.build(params, errors, path, **)
41
- Mechanics.check_keys(params, [], errors, path)
47
+ return TapGranter::UNBOUND if params == "granter"
48
+
49
+ if params.is_a?(Hash)
50
+ Mechanics.check_keys(params, [], errors, path)
51
+ else
52
+ errors << "#{path}: must be \"tap\" or {\"tap\": \"granter\"}"
53
+ end
42
54
  INSTANCE
43
55
  end
44
56
 
@@ -51,6 +63,35 @@ module Enjine
51
63
  def mana? = false
52
64
  def to_s = "{T}"
53
65
  end
66
+
67
+ # "Tap Fishing Pole" in an ability Fishing Pole grants (CR 602.2b, 607.1a): taps the granting permanent.
68
+ # The data names no permanent; the engine binds the cost to the granter of the ability being activated
69
+ # (Engine#granter_costs). Unbound (an ability printed on the permanent itself) it taps its source.
70
+ class TapGranter
71
+ attr_reader :granter
72
+
73
+ def initialize(granter = nil)
74
+ @granter = granter
75
+ freeze
76
+ end
77
+
78
+ UNBOUND = new
79
+
80
+ def for_granter(id) = id ? TapGranter.new(id) : self
81
+
82
+ def payable?(engine, object)
83
+ perm = permanent(engine, object)
84
+ !perm.nil? && perm.zone == :battlefield && !perm.tapped
85
+ end
86
+
87
+ def pay!(engine, object) = engine.tap!(permanent(engine, object))
88
+ def mana? = false
89
+ def to_s = "Tap the granting permanent"
90
+
91
+ private
92
+
93
+ def permanent(engine, object) = granter ? engine.state.objects[granter] : object
94
+ end
54
95
  end
55
96
  end
56
97
  end
@@ -64,11 +64,19 @@ module Enjine
64
64
 
65
65
  # Runs the effect during resolution. Effects that need a player's choice override this,
66
66
  # keep their progress in `locals` (plain JSON data) and return :wait after asking.
67
- def run(engine, ctx, _locals)
68
- apply(engine, ctx) if condition.nil? || condition.true?(engine, ctx)
67
+ # An effect that deals damage first asks the affected players to order the replacement and prevention
68
+ # effects where the order matters (CR 616.1, Engine::Damage#ask_damage_orders!).
69
+ def run(engine, ctx, locals)
70
+ return :done unless locals.key?("damage_orders") || condition.nil? || condition.true?(engine, ctx)
71
+ return :wait if engine.ask_damage_orders!(damage_hits(engine, ctx), locals) == :wait
72
+
73
+ engine.with_damage_orders(locals["damage_orders"]) { apply(engine, ctx) }
69
74
  :done
70
75
  end
71
76
 
77
+ # The damage it would deal now: [[source, recipient, amount]].
78
+ def damage_hits(_engine, _ctx) = []
79
+
72
80
  def refs(engine, ctx) = to ? to.resolve(engine, ctx) : []
73
81
 
74
82
  def to_s