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
@@ -20,16 +20,22 @@ 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
+ # {"name": "chosen"} with the card name chosen for the source or the resolving
25
+ # spell (choose_card_name, CR 201.4, 607.2d)
26
+ # {"same_name_as": filter} the same name as a permanent the filter matches, e.g. "a
27
+ # legendary permanent you control" (CR 201.2a)
28
+ # The chosen ones and same_name_as read the context (an EffectContext): the source's choices and its controller.
23
29
  class CardSpec
24
30
  KEYS = %w[types not_types subtypes supertypes name any_of permanent mana_value colors color_count keywords
25
- not_subtypes power toughness layout].freeze
31
+ not_subtypes power toughness layout same_name_as].freeze
26
32
  SUBTYPE = /\A[a-z][a-z0-9_]*\z/
27
33
  # CR 110.4a: the permanent card types (battles have no primitive yet, so none matches that way).
28
34
  PERMANENT_TYPES = %i[artifact battle creature enchantment land planeswalker].freeze
29
35
  BOUNDS = %w[at_least at_most equal].freeze
30
36
 
31
37
  attr_reader :types, :not_types, :subtypes, :supertypes, :name, :any_of, :permanent, :mana_value, :colors, :color_count,
32
- :keywords, :not_subtypes, :stats, :layout
38
+ :keywords, :not_subtypes, :stats, :layout, :relations
33
39
 
34
40
  def self.build(data, errors, path)
35
41
  return ANY if data.nil? || data == "card"
@@ -38,14 +44,16 @@ module Enjine
38
44
  data = data.transform_keys(&:to_s)
39
45
  Mechanics.check_keys(data, KEYS, errors, path)
40
46
  errors << "#{path}: needs at least one of #{KEYS.join(", ")}" if data.empty?
47
+ chosen = data["subtypes"] == "chosen"
48
+ named = data["name"] == "chosen"
41
49
  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),
50
+ subtypes: chosen ? [] : subtype_list(data, errors, path), name: named ? nil : name_param(data, errors, path),
43
51
  any_of: alternatives(data, errors, path), not_types: names(data, "not_types", :type, errors, path),
44
52
  permanent: permanent_param(data, errors, path), mana_value: bounds_param(data, "mana_value", errors, path),
45
53
  colors: colors_param(data, errors, path), color_count: bounds_param(data, "color_count", errors, path),
46
54
  keywords: names(data, "keywords", :keyword, errors, path), not_subtypes: subtype_list(data, errors, path, "not_subtypes"),
47
55
  stats: %w[power toughness].to_h { [it, bounds_param(data, it, errors, path)] }.reject { |_, b| b.empty? },
48
- layout: layout_param(data, errors, path))
56
+ layout: layout_param(data, errors, path), relations: relations_param(data, chosen, errors, path))
49
57
  end
50
58
 
51
59
  def self.layout_param(data, errors, path)
@@ -56,6 +64,14 @@ module Enjine
56
64
  value
57
65
  end
58
66
 
67
+ # What the spec compares with its context: the chosen creature type, permanents to share a name with.
68
+ def self.relations_param(data, chosen, errors, path)
69
+ raw = data["same_name_as"]
70
+ same = raw && Mechanics.build_entry(:filter, raw, errors, "#{path}.same_name_as")
71
+ { "chosen_subtype" => (true if chosen), "chosen_name" => (true if data["name"] == "chosen"),
72
+ "same_name_as" => same }.compact
73
+ end
74
+
59
75
  def self.colors_param(data, errors, path)
60
76
  list(data, "colors", errors, path).filter_map do |raw|
61
77
  color = raw.to_s.upcase.to_sym
@@ -127,11 +143,13 @@ module Enjine
127
143
  end
128
144
 
129
145
  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)
146
+ mana_value: {}, colors: [], color_count: {}, keywords: [], not_subtypes: [], stats: {}, layout: nil,
147
+ relations: {})
131
148
  @keywords = keywords.freeze
132
149
  @not_subtypes = not_subtypes.freeze
133
150
  @stats = stats.freeze
134
151
  @layout = layout
152
+ @relations = relations.freeze
135
153
  @colors = colors.freeze
136
154
  @color_count = color_count.freeze
137
155
  @types = types.freeze
@@ -145,11 +163,19 @@ module Enjine
145
163
  freeze
146
164
  end
147
165
 
166
+ # The spec without what it reads from its context (the chosen type, a shared name).
167
+ def unrelated
168
+ return self if relations.empty?
169
+
170
+ CardSpec.new(types:, subtypes:, supertypes:, name:, any_of:, not_types:, permanent:, mana_value:, colors:, color_count:,
171
+ keywords:, not_subtypes:, stats:, layout:)
172
+ end
173
+
148
174
  # True when the spec states no quality: "a card" (CR 701.23d).
149
175
  def any?
150
176
  types.empty? && not_types.empty? && subtypes.empty? && supertypes.empty? && name.nil? && any_of.empty? &&
151
177
  !permanent && mana_value.empty? && colors.empty? && color_count.empty? && keywords.empty? && not_subtypes.empty? &&
152
- stats.empty? && layout.nil?
178
+ stats.empty? && layout.nil? && relations.empty?
153
179
  end
154
180
 
155
181
  # Whether a card object matches, by its current characteristics (CR 109.3). `ctx` (an EffectContext)
@@ -163,7 +189,7 @@ module Enjine
163
189
  (name.nil? || chars.name == name) && (!permanent || chars.types.intersect?(PERMANENT_TYPES)) &&
164
190
  (colors.empty? || colors.intersect?(chars.colors)) && bounded?(mana_value, chars.mana_cost&.mana_value.to_i, engine, ctx) &&
165
191
  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)
192
+ other_qualities?(engine, chars, ctx) && (relations.empty? || related?(engine, chars, ctx))
167
193
  end
168
194
 
169
195
  # Keywords (CR 702.1), non-subtypes and power/toughness (CR 208.1; a "*" with no value is 0 off the battlefield,
@@ -178,10 +204,9 @@ module Enjine
178
204
  return "#{any_of.map { it.to_s.delete_suffix(" card") }.join(" or ")} card" unless any_of.empty?
179
205
  return "card named #{name}" if name
180
206
 
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
207
+ text = (type_words.map { it.to_s.tr("_", " ") } + ["card"]).join(" ") + quality_words
208
+ text += " with the same name as a #{relations["same_name_as"]}" if relations["same_name_as"]
209
+ text += " with the chosen name" if relations["chosen_name"]
185
210
  bounds = { "mana value" => mana_value, "number of colors" => color_count, "power" => stats["power"] || {},
186
211
  "toughness" => stats["toughness"] || {} }.reject { |_, b| b.empty? }
187
212
  return text if bounds.empty?
@@ -189,6 +214,14 @@ module Enjine
189
214
  "#{text} with #{bounds.map { |what, b| "#{what} #{b.map { |bound, n| "#{bound.tr("_", " ")} #{n}" }.join(" and ")}" }.join(", ")}"
190
215
  end
191
216
 
217
+ # "nonland", "non-Avatar", colors, supertypes, subtypes (or "chosen-type"), types and "permanent".
218
+ def type_words
219
+ chosen = relations["chosen_subtype"] ? ["chosen-type"] : []
220
+ words = not_types.map { "non#{it}" } + not_subtypes.map { "non-#{it.to_s.capitalize}" } + colors + supertypes +
221
+ subtypes.map { it.to_s.capitalize } + chosen + types
222
+ permanent ? words + ["permanent"] : words
223
+ end
224
+
192
225
  # " with flying", " that has an Adventure".
193
226
  def quality_words
194
227
  text = keywords.empty? ? "" : " with #{keywords.join(" or ").tr("_", " ")}"
@@ -199,6 +232,25 @@ module Enjine
199
232
 
200
233
  private
201
234
 
235
+ # The chosen creature type (CR 607.2d) and a shared name (CR 201.2a: an object with no name shares none); both
236
+ # need the context.
237
+ def related?(engine, chars, ctx)
238
+ return false unless ctx
239
+
240
+ if relations["chosen_subtype"]
241
+ chosen = Filters.chosen_memory(engine, ctx.source, ctx)["chosen_type"]
242
+ return false unless chosen && chars.subtypes.include?(chosen.to_sym)
243
+ end
244
+ if relations["chosen_name"] # "a card with the chosen name" (CR 201.4): nothing matches before a name is chosen
245
+ named = Filters.chosen_memory(engine, ctx.source, ctx)["chosen_name"]
246
+ return false unless named && chars.name == named
247
+ end
248
+ filter = relations["same_name_as"] or return true
249
+ !chars.name.to_s.empty? && engine.state.battlefield.any? do
250
+ filter.match_for?(engine, it, ctx.controller, ctx.source) && engine.chars(it).name == chars.name
251
+ end
252
+ end
253
+
202
254
  # 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
255
  # the bounds.
204
256
  def bounded?(bounds, value, engine, ctx)
@@ -73,42 +73,101 @@ 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, or resolving for a cost paid then (CR 118.12):
159
+ # "copy those exiled cards".
160
+ def pay_card!(engine, id)
161
+ fresh = engine.move!(id, :exile, reason: "cost") or return
162
+ holder = engine.cost_holder
163
+ (holder.memory[EXILED] ||= []) << fresh.id if holder
164
+ end
110
165
 
111
- def cards_text = "#{count == 1 ? "a" : count} #{filter || "card"}#{"s" unless count == 1}"
166
+ def cards_text
167
+ many = { 1 => "a", "any" => "any number of" }.fetch(count, count)
168
+ text = "#{many} #{filter || "card"}#{"s" unless count == 1}"
169
+ @symbols ? "#{text} with #{@symbols["at_least"]} or more {#{@symbols["color"]}} symbols among their mana costs" : text
170
+ end
112
171
  end
113
172
  end
114
173
  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,30 @@ 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 the spell or ability being paid for (Costs::ChooseCreatureType, Engine#cost_holder).
59
+ def candidates(engine, object)
60
+ ctx = EffectContext.new(object.controller, object.id, holder(engine, object).id, [])
61
+ Beholding.candidates(engine, object.controller, filter, ctx) - [object.id]
62
+ end
63
+
64
+ # The spell or ability being paid for, when `object` is it or its source (an ability's costs are paid for its
65
+ # source); otherwise `object`.
66
+ def holder(engine, object)
67
+ held = engine.cost_holder
68
+ held && (held.id == object.id || held.source == object.id) ? held : object
69
+ end
70
+
71
+ # Before a type is chosen for "behold two creatures of that type", whether some type would do (CR 601.2b).
72
+ def payable?(engine, object)
73
+ return super unless filter.relations["chosen_subtype"] && !holder(engine, object).memory["chosen_type"]
74
+
75
+ others = Beholding.candidates(engine, object.controller, filter.unrelated) - [object.id]
76
+ others.flat_map { engine.chars(it).subtypes.uniq }.tally.any? do |type, n|
77
+ n >= count && Keywords::Changeling::CREATURE_TYPES.include?(type)
78
+ end
79
+ end
80
+
58
81
  def choose!(engine, option) = Beholding.behold!(engine, engine.object(option[:object]).controller, option[:object])
59
82
 
60
83
  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 or ability being cast (or resolving, for a cost paid then, CR 118.12) remembers the type now, so a
32
+ # later cost of "that type" can be chosen (CR 601.2b).
33
+ def choose!(engine, option)
34
+ type = option[:creature_type].to_s
35
+ engine.cost_holder&.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
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enjine
4
+ module Mechanics
5
+ module Costs
6
+ # Collect evidence N (CR 701.59a): "exile any number of cards from your graveyard with total mana value N or
7
+ # greater" (CR 202.3), never the card the spell or ability belongs to. The player chooses cards one at a time
8
+ # (`choose_cost`) and may stop (`done`) once they total N; the cards are exiled as the spell becomes cast
9
+ # (CR 601.2h). It can't be paid without enough mana value in the graveyard (CR 701.59b, 118.3). As an
10
+ # optional additional cost (`additional_cost` with `optional: true`), "if evidence was collected" is
11
+ # `{"cast_with": "additional_cost"}`; inside `pay` (and so ward) it is "you may collect evidence N. If you do".
12
+ class CollectEvidence < CardsCost
13
+ primitive :cost, :collect_evidence,
14
+ description: "Collect evidence `N`: exile any number of other cards from your graveyard with total " \
15
+ "mana value N or greater, chosen one at a time (`choose_cost`; `done` once they total " \
16
+ "N). Not payable without that much mana value in your graveyard. \"If evidence was " \
17
+ "collected\": make it an optional `additional_cost` and read `{\"cast_with\": " \
18
+ "\"additional_cost\"}`.",
19
+ rules: %w[701.59a 701.59b 701.59c 202.3 601.2h 118.3],
20
+ params: { "(value)" => "integer >= 1: N" },
21
+ example: { "collect_evidence" => 6 }
22
+
23
+ attr_reader :evidence
24
+
25
+ def self.scalar_params? = true
26
+ def self.zone = :graveyard
27
+
28
+ def self.build(params, errors, path, **)
29
+ errors << "#{path}: must be an integer >= 1" unless params.is_a?(Integer) && params.positive?
30
+ new(nil, "any", false, { "evidence" => params.is_a?(Integer) ? params : 1 }, errors, path)
31
+ end
32
+
33
+ def initialize(filter, count, other, params, errors, path)
34
+ @evidence = params["evidence"]
35
+ super(filter, count, other, {}, errors, path)
36
+ end
37
+
38
+ # CardsCost's "any number" check reads `symbols`; collect evidence reads mana value instead.
39
+ def symbols_param(_raw, _errors, _path) = { "evidence" => true }
40
+
41
+ def mana_value_among(engine, ids) = ids.sum { engine.chars(it).mana_cost&.mana_value.to_i }
42
+
43
+ def payable?(engine, object) = mana_value_among(engine, candidates(engine, object)) >= evidence
44
+
45
+ # Each chosen card remembers its mana value, so the total is known without the engine.
46
+ def choose!(engine, option) = { "object" => option[:object], "mana_value" => mana_value_among(engine, [option[:object]]) }
47
+
48
+ # Engine::CastChoices: `done` is offered once the chosen cards total N; with nothing left to choose it's done.
49
+ def finishable?(entries) = entries.sum { it["mana_value"].to_i } >= evidence
50
+
51
+ def chosen?(engine, object, entries, done)
52
+ finishable?(entries) && (done || choice_options(engine, object, entries).empty?)
53
+ end
54
+
55
+ def cards_text = "cards with total mana value #{evidence} or greater"
56
+ def to_s = "Collect evidence #{evidence}"
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enjine
4
+ module Mechanics
5
+ module Costs
6
+ # "Exile a creature you control" / "Exile another artifact you control" as a cost (CR 406.1, 601.2h, 602.2b).
7
+ # The permanent is chosen while casting or activating and exiled as the spell or ability becomes cast. It's not
8
+ # a sacrifice (nothing watching sacrifices sees it). What it was is kept like a sacrificed permanent's (types,
9
+ # subtypes, power, toughness, mana value: amount `sacrificed`), and the card in exile is remembered by the
10
+ # spell or ability (selector "exiled_for_cost").
11
+ class ExilePermanent < Choosing
12
+ primitive :cost, :exile_permanent,
13
+ description: "Exile `count` permanents you control matching `filter` (`other: true`: not the source). " \
14
+ "Chosen while casting or activating, exiled as it becomes cast; the exiled cards are " \
15
+ "`exiled_for_cost`, and their last known power, toughness and mana value are amount " \
16
+ "`sacrificed`.",
17
+ rules: %w[406.1 601.2h 602.2b 118.3 608.2h],
18
+ params: { "filter" => "filter (default permanent)", "count" => "integer (default 1)",
19
+ "other" => "true: not the source itself" },
20
+ example: { "exile_permanent" => { "filter" => "creature" } }
21
+
22
+ def pay_chosen!(engine, _object, entries)
23
+ entries.each do |entry|
24
+ perm = engine.state.objects[entry["object"]]
25
+ next unless perm&.zone == :battlefield
26
+
27
+ facts = engine.chars(perm)
28
+ entry.merge!("types" => facts.types.map(&:to_s), "subtypes" => facts.subtypes.map(&:to_s),
29
+ "power" => engine.power(perm), "toughness" => engine.toughness(perm),
30
+ "mana_value" => Amount.mana_value(engine, perm))
31
+ fresh = engine.move!(perm.id, :exile, reason: "cost") or next
32
+ holder = engine.casting && engine.state.objects[engine.casting["object"]]
33
+ (holder.memory[CardsCost::EXILED] ||= []) << fresh.id if holder && fresh.zone == :exile
34
+ end
35
+ end
36
+
37
+ def to_s = "Exile #{amount_words} #{what} you control"
38
+ end
39
+ end
40
+ end
41
+ 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