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
@@ -10,6 +10,9 @@ module Enjine
10
10
 
11
11
  def self.player_ref(engine, index) = "#{PLAYER_PREFIX}#{engine.name(index)}"
12
12
 
13
+ # "A spell or ability with a single target" (CR 115.7): it has exactly one target (one object or player).
14
+ def self.single_target?(obj) = Array(obj.targets).flatten.compact.uniq.size == 1
15
+
13
16
  def self.player_index(engine, ref)
14
17
  return nil unless ref.is_a?(String) && ref.start_with?(PLAYER_PREFIX)
15
18
 
@@ -26,15 +29,17 @@ module Enjine
26
29
  filter
27
30
  end
28
31
 
29
- Facts = Data.define(:chars, :counters, :token, :chosen) do
30
- def initialize(chars:, counters:, token:, chosen: nil) = super
32
+ # `status`: {"attacking" =>, "blocking" =>, "tapped" => bool} for a permanent on the battlefield, nil where unknown
33
+ # (then a filter's status keys aren't checked).
34
+ Facts = Data.define(:chars, :counters, :token, :chosen, :status) do
35
+ def initialize(chars:, counters:, token:, chosen: nil, status: nil) = super
31
36
  end
32
37
 
33
38
  # The choices "the chosen ..." can refer to (CR 607.2d): the resolving spell's or ability's own
34
39
  # (an effect's "choose a creature type"), else its source's (an "as this enters" choice, CR 614.12).
35
40
  def self.chosen_memory(engine, source, ctx)
36
41
  own = engine.state.objects[source]&.memory || {}
37
- resolving = ctx && ctx.object != source ? engine.state.objects[ctx.object]&.memory : nil
42
+ resolving = ctx && ctx.object != source ? engine.find_object(ctx.object)&.memory : nil
38
43
  resolving ? own.merge(resolving.select { |k, _| k.start_with?("chosen_") }) : own
39
44
  end
40
45
 
@@ -97,23 +102,29 @@ module Enjine
97
102
  # {"creature": {"keywords": ["flying"]}} with flying (any listed); "not_keywords": without (CR 702.1)
98
103
  # {"creature": {"subtypes": "chosen"}} of the chosen creature type; also "colors", "types", "not",
99
104
  # "not_subtypes": "chosen" (CR 607.2d, see Filters.chosen_memory)
105
+ # {"land": {"name": "chosen"}} with the chosen card name (CR 201.4)
100
106
  # {"creature": {"transformed": true}} back face up (CR 701.27h); {"prepared": true} (CR 723.3a)
101
107
  # {"creature": {"saddled": true}} saddled this turn (CR 702.171b)
102
108
  # {"creature": {"entered_this_turn": true}} entered the battlefield this turn
109
+ # {"spell": {"single_target": true}} a spell with a single target (CR 115.7)
103
110
  class Restricted
104
- TYPES = %w[artifact creature enchantment land planeswalker instant sorcery].freeze
105
- BOOLEANS = %w[other attacking blocking tapped token modified transformed prepared saddled entered_this_turn].freeze
111
+ TYPES = %w[artifact battle creature enchantment kindred land planeswalker instant sorcery].freeze
112
+ BOOLEANS = %w[other attacking blocking tapped token modified transformed prepared saddled entered_this_turn
113
+ single_target].freeze
106
114
  RANGES = %w[power toughness mana_value].freeze
107
115
  TYPE_LISTS = %w[not any_of types].freeze
108
116
  NAME_LISTS = %w[subtypes not_subtypes supertypes not_supertypes attached_subtypes].freeze
109
117
  KEYWORD_LISTS = %w[keywords not_keywords].freeze
110
- KEYS = (%w[controller owner counters colors] + BOOLEANS + RANGES + TYPE_LISTS + NAME_LISTS + KEYWORD_LISTS).freeze
111
- # Lists that may be "chosen": the choice remembered under that memory key (CR 607.2d).
118
+ KEYS = (%w[controller owner counters colors name] + BOOLEANS + RANGES + TYPE_LISTS + NAME_LISTS + KEYWORD_LISTS).freeze
119
+ # Lists that may be "chosen": the choice remembered under that memory key (CR 607.2d). "name" is only
120
+ # "chosen" ("with the chosen name", CR 201.4).
112
121
  CHOSEN = { "colors" => "chosen_color", "types" => "chosen_card_type", "not" => "chosen_card_type",
113
- "subtypes" => "chosen_type", "not_subtypes" => "chosen_type" }.freeze
122
+ "subtypes" => "chosen_type", "not_subtypes" => "chosen_type", "name" => "chosen_name" }.freeze
123
+ # Status, not characteristics: a static ability may read them (they never depend on the layer pass, CR 613.8).
124
+ STATUS = %w[attacking blocking tapped].freeze
114
125
  # Keys that need current characteristics or combat/tapped status: not usable in the layer pass.
115
126
  LIVE = %w[power toughness attacking blocking tapped attached_subtypes transformed prepared saddled
116
- entered_this_turn].freeze
127
+ entered_this_turn single_target].freeze
117
128
 
118
129
  attr_reader :base
119
130
 
@@ -135,6 +146,7 @@ module Enjine
135
146
  (colors.is_a?(Array) && !colors.empty? && colors.all? { Mana::COLORS.include?(it.to_s.to_sym) })
136
147
  errors << "#{path}.colors: must be a non-empty list of W, U, B, R, G (any of them) or \"chosen\""
137
148
  end
149
+ errors << "#{path}.name: must be \"chosen\"" unless [nil, "chosen"].include?(params["name"])
138
150
  new(base, params, ranges)
139
151
  end
140
152
 
@@ -216,13 +228,13 @@ module Enjine
216
228
  # left the battlefield counts as it last existed, CR 608.2h).
217
229
  def candidates_for(engine, controller, source, ctx: nil)
218
230
  bounds = resolved_ranges(engine, controller, source, ctx)
219
- base.candidates(engine, controller).select { allowed?(engine, it, controller, source, bounds, ctx) }
231
+ base.candidates_for(engine, controller, source, ctx: ctx).select { allowed?(engine, it, controller, source, bounds, ctx) }
220
232
  end
221
233
 
222
234
  def match?(engine, ref, controller) = match_for?(engine, ref, controller, nil)
223
235
 
224
236
  def match_for?(engine, ref, controller, source, ctx: nil)
225
- base.match?(engine, ref, controller) &&
237
+ base.match_for?(engine, ref, controller, source, ctx: ctx) &&
226
238
  allowed?(engine, ref, controller, source, resolved_ranges(engine, controller, source, ctx), ctx)
227
239
  end
228
240
 
@@ -232,7 +244,12 @@ module Enjine
232
244
  (@lists["any_of"].empty? || types.intersect?(@lists["any_of"])) && (@lists["types"] - types).empty?
233
245
  end
234
246
 
235
- def matches_facts?(facts) = base.matches_facts?(facts) && characteristics_ok?(facts)
247
+ def matches_facts?(facts) = base.matches_facts?(facts) && characteristics_ok?(facts) && status_facts_ok?(facts.status)
248
+
249
+ # Attacking, blocking and tapped status from Facts (CR 506.4), when known.
250
+ def status_facts_ok?(status)
251
+ status.nil? || STATUS.all? { @params[it].nil? || status[it] == @params[it] }
252
+ end
236
253
 
237
254
  def to_s
238
255
  words = []
@@ -248,7 +265,7 @@ module Enjine
248
265
  words.concat(ownership_words)
249
266
  words.concat(@ranges.map { |key, range| "with #{key.tr("_", " ")} #{range_words(range)}" })
250
267
  words << "with a #{@lists["attached_subtypes"].join(" or ")} attached" unless @lists["attached_subtypes"].empty?
251
- (words + [counter_words] + keyword_words).compact.join(" ")
268
+ (words + [counter_words, ("with a single target" if @params["single_target"])] + keyword_words).compact.join(" ")
252
269
  end
253
270
 
254
271
  RANGE_WORDS = { "at_least" => "%s or greater", "at_most" => "%s or less", "equal" => "equal to %s" }.freeze
@@ -314,7 +331,7 @@ module Enjine
314
331
  return false if @params["modified"] == true && !modified?(engine, obj)
315
332
  return false unless attached_ok?(engine, obj)
316
333
 
317
- bounds.all? { |key, range| in_range?(stat(engine, obj, current, key), range) }
334
+ bounds.all? { |key, range| in_range?(stat(engine, obj, key), range) }
318
335
  end
319
336
 
320
337
  # Types, subtypes, supertypes, keywords, the chosen values, token-ness, counters and printed mana
@@ -346,6 +363,7 @@ module Enjine
346
363
  have = case key
347
364
  when "colors" then chars.colors
348
365
  when "types", "not" then chars.types
366
+ when "name" then [chars.name.to_sym]
349
367
  else chars.subtypes
350
368
  end
351
369
  hit = !value.nil? && have.include?(value.to_sym)
@@ -368,6 +386,7 @@ module Enjine
368
386
  return false if !@params["prepared"].nil? && engine.prepared?(obj) != @params["prepared"] # CR 723.3a
369
387
  return false if !@params["saddled"].nil? && engine.saddled?(obj) != @params["saddled"] # CR 702.171b
370
388
  return false if !@params["entered_this_turn"].nil? && engine.entered_this_turn?(obj) != @params["entered_this_turn"]
389
+ return false if !@params["single_target"].nil? && Filters.single_target?(obj) != @params["single_target"]
371
390
 
372
391
  @params["tapped"].nil? || obj.tapped == @params["tapped"]
373
392
  end
@@ -384,12 +403,12 @@ module Enjine
384
403
  Counters.any?(obj.counters) || engine.modifying_attachment?(obj)
385
404
  end
386
405
 
387
- # Current power/toughness (CR 208.3) and mana value (CR 202.3; 0 without a mana cost, 202.3a).
388
- def stat(engine, obj, current, key)
406
+ # Current power/toughness (CR 208.3) and mana value (CR 202.3; 0 without a mana cost, 202.3a; Amount.mana_value).
407
+ def stat(engine, obj, key)
389
408
  case key
390
409
  when "power" then engine.power(obj)
391
410
  when "toughness" then engine.toughness(obj)
392
- else current.mana_cost&.mana_value.to_i
411
+ else Amount.mana_value(engine, obj)
393
412
  end
394
413
  end
395
414
 
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enjine
4
+ module Mechanics
5
+ module Filters
6
+ # "Target creature card exiled with [this]" (CR 607.2a, 115.1): a card in exile that an ability of the
7
+ # filter's source exiled. {"exiled_with": {"any_of": ["creature"]}} narrows it by card type. Without a
8
+ # source (an "each" selector of a static ability) nothing matches.
9
+ class ExiledWith < Filter
10
+ primitive :filter, :exiled_with,
11
+ description: "A card in exile that one of this object's abilities exiled (linked abilities: \"the " \
12
+ "cards exiled with it\"); restrict with `any_of`/`not` for its card types.",
13
+ rules: %w[607.2a 406.6 115.1],
14
+ example: { "exiled_with" => { "any_of" => ["creature"] } }
15
+
16
+ def self.zone = :exile
17
+ def self.candidates(_engine, _controller) = []
18
+
19
+ def self.candidates_for(engine, _controller, source, ctx: nil, **)
20
+ source ? Linked.exiled_with(engine, source, ctx) : []
21
+ end
22
+
23
+ def self.match_for?(engine, ref, controller, source, **) = candidates_for(engine, controller, source).include?(ref)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -18,6 +18,13 @@ module Enjine
18
18
  def self.card_types = [:planeswalker]
19
19
  end
20
20
 
21
+ # A battle on the battlefield (CR 310).
22
+ class Battle < Filter
23
+ primitive :filter, :battle, description: "A battle on the battlefield.", rules: %w[310.5], example: "battle"
24
+ def self.match_object?(engine, object, _controller) = type?(engine, object, :battle)
25
+ def self.card_types = [:battle]
26
+ end
27
+
21
28
  # A creature or planeswalker on the battlefield.
22
29
  class CreatureOrPlaneswalker < Filter
23
30
  primitive :filter, :creature_or_planeswalker, description: "A creature or planeswalker on the battlefield.",
@@ -20,9 +20,10 @@ module Enjine
20
20
  primitive :filter, :stack_ability,
21
21
  description: "An activated or triggered ability on the stack. `kind`: \"activated\" or \"triggered\" " \
22
22
  "for only one of them; `controller`: you | opponent; `source`: a filter its source " \
23
- "matches (e.g. \"creature\", \"artifact\").",
24
- rules: %w[113.1b 115.1 605.3b 113.7],
25
- params: { "kind" => KINDS.join(" | "), "controller" => CONTROLLERS.join(" | "), "source" => "filter" },
23
+ "matches (e.g. \"creature\", \"artifact\"); `single_target: true`: one with a single target.",
24
+ rules: %w[113.1b 115.1 605.3b 113.7 115.7],
25
+ params: { "kind" => KINDS.join(" | "), "controller" => CONTROLLERS.join(" | "), "source" => "filter",
26
+ "single_target" => "boolean" },
26
27
  example: { "stack_ability" => { "controller" => "you", "source" => "creature" } }
27
28
 
28
29
  def self.zone = :stack
@@ -31,22 +32,25 @@ module Enjine
31
32
  def self.build(params, errors, path, **)
32
33
  return self if params.empty?
33
34
 
34
- Mechanics.check_keys(params, %w[kind controller source], errors, path)
35
+ Mechanics.check_keys(params, %w[kind controller source single_target], errors, path)
35
36
  kind, controller, source = params.values_at("kind", "controller", "source")
37
+ single = Mechanics.flag_param(params, "single_target", errors, path)
36
38
  errors << "#{path}.kind: must be one of #{KINDS.join(", ")}" unless kind.nil? || KINDS.include?(kind)
37
39
  unless controller.nil? || CONTROLLERS.include?(controller)
38
40
  errors << "#{path}.controller: must be one of #{CONTROLLERS.join(", ")}"
39
41
  end
40
42
  source &&= Mechanics.characteristic_filter(source, errors, "#{path}.source")
41
- Narrowed.new(kind, controller, source)
43
+ Narrowed.new(kind, controller, source, single: single)
42
44
  end
43
45
 
44
- # Only activated (loyalty included) or only triggered abilities, of a controller, from a kind of source.
46
+ # Only activated (loyalty included) or only triggered abilities, of a controller, from a kind of source,
47
+ # with a single target (CR 115.7).
45
48
  class Narrowed
46
- def initialize(kind, controller, source)
49
+ def initialize(kind, controller, source, single: false)
47
50
  @kind = kind
48
51
  @controller = controller
49
52
  @source = source
53
+ @single = single
50
54
  freeze
51
55
  end
52
56
 
@@ -54,7 +58,8 @@ module Enjine
54
58
 
55
59
  def to_s
56
60
  whose = { "you" => " you control", "opponent" => " an opponent controls" }[@controller]
57
- "#{@kind || "activated or triggered"} ability#{whose}#{" from a source: #{Triggers.filter_words(@source)}" if @source}"
61
+ "#{@kind || "activated or triggered"} ability#{whose}#{" from a source: #{Triggers.filter_words(@source)}" if @source}" \
62
+ "#{" with a single target" if @single}"
58
63
  end
59
64
 
60
65
  def candidates(engine, controller) = StackAbility.candidates(engine, controller).select { narrow?(engine, it, controller) }
@@ -76,6 +81,7 @@ module Enjine
76
81
  obj = engine.object(ref)
77
82
  return false if @kind && (engine.stack_ability(obj).kind == :triggered) != (@kind == "triggered")
78
83
  return false if @controller && (@controller == "you") != (obj.controller == controller)
84
+ return false if @single && !Filters.single_target?(obj)
79
85
 
80
86
  @source.nil? || source_ok?(engine, obj)
81
87
  end
@@ -61,8 +61,10 @@ module Enjine
61
61
  # The zone the ability lets the card be cast from (CR 113.6).
62
62
  def zone = :hand
63
63
 
64
- # Whether the card `obj` (owned by `index`) may be cast this way now, before timing and cost checks.
65
- def functions?(_engine, obj, _index) = obj.zone == zone
64
+ # Whether the card `obj` (owned by `index`) may be cast this way now, before timing and cost checks: it is
65
+ # in the ability's zone (or the zone a granting permanent names, Abilities::CardsHave) and `usable?`.
66
+ def functions?(engine, obj, index) = obj.zone == zone && usable?(engine, obj, index)
67
+ def usable?(_engine, _obj, _index) = true
66
68
 
67
69
  # Paid instead of the mana cost? (Mayhem without a cost only allows playing it.)
68
70
  def alternative? = !mana_cost.nil?
@@ -84,12 +86,16 @@ module Enjine
84
86
  def cost_text = costs.empty? ? "" : " #{costs.join(", ")}"
85
87
  def to_s = "#{keyword.split("_").map(&:capitalize).join("-")}#{cost_text}"
86
88
 
87
- # The cast method (Engine::Permissions) this ability gives.
88
- def method_for(ref)
89
+ # The mana cost string paid for the card `obj` (nil: it can't be cast this way, CR 118.6).
90
+ def alt_cost_for(_engine, _obj) = mana_cost.to_s
91
+
92
+ # The cast method (Engine::Permissions) this ability gives the card `obj`; nil when it has none.
93
+ def method_for(ref, engine: nil, obj: nil)
89
94
  method = { "via" => "alt:#{ref}", "play" => play, "keyword" => keyword }
90
95
  return method unless alternative?
91
96
 
92
- alt = { "cost" => mana_cost.to_s, "ref" => ref }
97
+ cost = alt_cost_for(engine, obj) or return nil
98
+ alt = { "cost" => cost, "ref" => ref }
93
99
  alt["exile"] = true if exiles?
94
100
  alt["returns"] = returns if returns
95
101
  alt["harmonize"] = true if is_a?(Harmonize)
@@ -104,11 +110,40 @@ module Enjine
104
110
  description: "Flashback: you may cast this instant or sorcery card from your graveyard by paying " \
105
111
  "`cost` rather than its mana cost; then it's exiled whenever it would leave the stack. " \
106
112
  "`cost` is a mana cost string or a list of costs (`[{\"mana\": \"{1}{U}\"}, " \
107
- "{\"tap_permanents\": {\"count\": 3}}]`). Cast option `via: \"alt:<ref>\"`.",
108
- rules: %w[702.34a 118.9 601.2b 601.2f 601.2h],
109
- params: { "cost" => "mana cost string, or a list of costs" },
113
+ "{\"tap_permanents\": {\"count\": 3}}]`), or `\"mana_cost\"`: the card's own mana cost " \
114
+ "(\"its flashback cost is equal to its mana cost\", e.g. granted with `grant_ability`). " \
115
+ "Cast option `via: \"alt:<ref>\"`.",
116
+ rules: %w[702.34a 118.9 118.6 601.2b 601.2f 601.2h],
117
+ params: { "cost" => "mana cost string, a list of costs, or \"mana_cost\"" },
110
118
  example: { "flashback" => { "cost" => "{2}{R}" } }
111
119
 
120
+ MANA_COST = "mana_cost"
121
+
122
+ # `{"cost": "mana_cost"}`: "its flashback cost is equal to its mana cost" (a granted flashback, CR 702.34a).
123
+ def self.build(params, errors, path, **)
124
+ return super unless Mechanics.fetch_param(params, :cost) == MANA_COST
125
+
126
+ Mechanics.check_keys(params, %w[cost], errors, path)
127
+ new([], path, errors, own_mana_cost: true)
128
+ end
129
+
130
+ def initialize(costs, path, errors, own_mana_cost: false)
131
+ @own_mana_cost = own_mana_cost
132
+ super(costs, path, errors)
133
+ end
134
+
135
+ def own_mana_cost? = @own_mana_cost
136
+ def alternative? = own_mana_cost? || super
137
+
138
+ # The card's own mana cost, X included (CR 107.3); a card with no mana cost can't be cast this way (CR 118.6).
139
+ def alt_cost_for(engine, obj)
140
+ return super unless own_mana_cost?
141
+
142
+ engine && obj && engine.chars(obj).mana_cost&.to_s
143
+ end
144
+
145
+ def cost_text = own_mana_cost? ? " (its mana cost)" : super
146
+
112
147
  def zone = :graveyard
113
148
  def exiles? = true
114
149
  def allowed_types = %i[instant sorcery]
@@ -147,7 +182,7 @@ module Enjine
147
182
 
148
183
  def zone = :graveyard
149
184
  def play = alternative? ? "cast" : "play"
150
- def functions?(engine, obj, _index) = obj.zone == zone && engine.discarded_this_turn?(obj)
185
+ def usable?(engine, obj, _index) = engine.discarded_this_turn?(obj)
151
186
  end
152
187
 
153
188
  # CR 702.185a-c: warp, from the hand. The permanent is exiled at the beginning of the next end step; its
@@ -158,18 +193,38 @@ module Enjine
158
193
  "cost. If you do, the permanent it becomes is exiled at the beginning of the next end " \
159
194
  "step (a delayed triggered ability), and its owner may cast it from exile after this " \
160
195
  "turn has ended for as long as it remains exiled (cast option `via: \"warped\"`). " \
161
- "Conditions `{\"cast_with\": \"warp\"}` and `{\"cast_with_this_turn\": \"warp\"}` read it.",
162
- rules: %w[702.185a 702.185b 702.185c 603.7 118.9 601.2b 601.2f 601.2h],
163
- params: { "cost" => "mana cost string, or a list of costs" },
196
+ "Conditions `{\"cast_with\": \"warp\"}` and `{\"cast_with_this_turn\": \"warp\"}` read it. " \
197
+ "`from: [\"hand\", \"graveyard\"]`: \"you may cast this card from your graveyard using " \
198
+ "its warp ability\" (CR 601.3).",
199
+ rules: %w[702.185a 702.185b 702.185c 603.7 118.9 601.2b 601.2f 601.2h 601.3],
200
+ params: { "cost" => "mana cost string, or a list of costs",
201
+ "from" => "zones it works from: \"hand\" (default), \"graveyard\", or a list of both" },
164
202
  example: { "warp" => { "cost" => "{1}{R}" } }
165
203
 
166
- attr_reader :delayed
204
+ ZONES = %w[hand graveyard].freeze
167
205
 
168
- def initialize(costs, path, errors)
206
+ attr_reader :delayed, :zones
207
+
208
+ def self.build(params, errors, path, **)
209
+ raw = Mechanics.fetch_param(params, :from) || "hand"
210
+ zones = Array(raw).map(&:to_s)
211
+ if zones.empty? || !(zones - ZONES).empty?
212
+ errors << "#{path}.from: must be hand, graveyard or a list of them"
213
+ zones = ["hand"]
214
+ end
215
+ Mechanics.check_keys(params, %w[cost from], errors, path)
216
+ new(read_costs(Mechanics.fetch_param(params, :cost), errors, path), path, errors,
217
+ zones: zones.uniq.map(&:to_sym).freeze)
218
+ end
219
+
220
+ def initialize(costs, path, errors, zones: %i[hand].freeze)
169
221
  @delayed = Effects::WarpDelayed.build({}, errors, path)
170
- super
222
+ @zones = zones
223
+ super(costs, path, errors)
171
224
  end
172
225
 
226
+ def functions?(engine, obj, index) = zones.include?(obj.zone) && usable?(engine, obj, index)
227
+
173
228
  def try_effects = [delayed]
174
229
  end
175
230
 
@@ -228,9 +283,7 @@ module Enjine
228
283
  def returns = "unblocked"
229
284
  def instant_speed? = true
230
285
 
231
- def functions?(engine, obj, index)
232
- obj.zone == zone && engine.state.active == index && engine.state.step == :declare_blockers
233
- end
286
+ def usable?(engine, _obj, index) = engine.state.active == index && engine.state.step == :declare_blockers
234
287
  end
235
288
  end
236
289
 
@@ -281,12 +334,12 @@ module Enjine
281
334
  # that permanent's triggered abilities), remembers it.
282
335
  class CastHow
283
336
  KEYS = %w[cast_with cast_from cast_with_this_turn].freeze
284
- KEYWORDS = %w[flashback harmonize mayhem warp evoke web_slinging sneak foretell].freeze
337
+ KEYWORDS = %w[flashback harmonize mayhem warp evoke web_slinging sneak foretell impending miracle].freeze
285
338
  # Optional additional costs announced as the spell is cast (Engine::CastChoices#announce_kicker!):
286
339
  # "if this spell was cast using teamwork" (CR 702.194b), "if this spell's additional cost was paid" (CR 118.8b),
287
340
  # "if the gift was promised" (CR 702.174k).
288
341
  OPTIONAL_TEXT = { "teamwork" => "it was cast using teamwork", "additional_cost" => "its additional cost was paid",
289
- "gift" => "its gift was promised" }.freeze
342
+ "gift" => "its gift was promised", "offspring" => "its offspring cost was paid" }.freeze
290
343
  OPTIONAL = OPTIONAL_TEXT.keys.freeze
291
344
  ZONES = %w[hand graveyard exile library outside].freeze
292
345
 
@@ -304,13 +357,18 @@ module Enjine
304
357
  freeze
305
358
  end
306
359
 
360
+ # Whether `condition` is "if the gift was promised" (the gift triggered ability of a permanent, CR 702.174b).
361
+ def self.gift?(condition) = condition.is_a?(CastHow) && condition.gift?
362
+ def gift? = @key == "cast_with" && @value == "gift"
363
+
307
364
  def true?(engine, ctx)
308
365
  return engine.cast_with_this_turn?(@value) if @key == "cast_with_this_turn"
309
366
 
310
367
  holder = engine.state.objects[ctx.object || ctx.source]
311
- return holder&.memory&.[]("kicked_with") == @value if OPTIONAL.include?(@value)
368
+ memory = holder ? holder.memory : engine.departed_cast_memory(ctx.source) # a dies trigger checked (CR 603.4)
369
+ return memory&.[]("kicked_with") == @value if OPTIONAL.include?(@value)
312
370
 
313
- holder&.memory&.[](@key) == @value
371
+ memory&.[](@key) == @value
314
372
  end
315
373
 
316
374
  def to_s
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "alternative_costs"
4
+
5
+ module Enjine
6
+ module Mechanics
7
+ module Abilities
8
+ # "Creature cards in your graveyard have sneak {3}{B}. You may cast creature spells from your graveyard using
9
+ # their sneak abilities." (CR 611.3a, 604.1, 118.9): while this permanent is on the battlefield, its
10
+ # controller's cards in `zone` matching `cards` have the keyword alternative cost `ability`, and it works from
11
+ # that zone. Each is a cast method of Engine::AltCasting with `via: "alt:cards_have:<permanent>:<ref>"`.
12
+ # (`grants_beam_me_up` is the same idea for beam me up, whose cost is each card's own mana cost.)
13
+ class CardsHave < Primitive
14
+ ZONES = %w[graveyard hand exile].freeze
15
+ GRANTABLE = %i[flashback harmonize mayhem web_slinging sneak miracle].freeze
16
+
17
+ primitive :ability, :cards_have,
18
+ description: "While this permanent is on the battlefield, cards you own in `zone` (graveyard " \
19
+ "(default), hand or exile) matching `cards` have the keyword alternative cost `ability` " \
20
+ "(flashback, harmonize, mayhem, web_slinging, sneak or miracle) and may be cast with it from " \
21
+ "that zone. Cast option `via: \"alt:cards_have:<permanent>:<ref>\"`.",
22
+ rules: %w[611.3a 604.1 118.9 601.2b 702.190a],
23
+ params: { "zone" => ZONES.join(" | "), "cards" => "card spec",
24
+ "ability" => "a keyword alternative cost, e.g. {\"sneak\": {\"cost\": \"{3}{B}\"}}" },
25
+ example: { "cards_have" => { "cards" => { "types" => ["creature"] },
26
+ "ability" => { "sneak" => { "cost" => "{3}{B}" } } } }
27
+
28
+ attr_reader :zone, :cards, :granted
29
+
30
+ def self.build(params, errors, path, **)
31
+ Mechanics.check_keys(params, %w[zone cards ability], errors, path)
32
+ zone = Mechanics.fetch_param(params, :zone) || "graveyard"
33
+ errors << "#{path}.zone: must be one of #{ZONES.join(", ")}" unless ZONES.include?(zone)
34
+ raw = Mechanics.fetch_param(params, :ability)
35
+ granted = raw && Mechanics.build_entry(:ability, raw, errors, "#{path}.ability")
36
+ unless granted.is_a?(AlternativeCost) && GRANTABLE.include?(granted.keyword.to_sym)
37
+ errors << "#{path}.ability: must be one of #{GRANTABLE.join(", ")}"
38
+ granted = nil
39
+ end
40
+ new(zone.to_sym, CardSpec.build(Mechanics.fetch_param(params, :cards), errors, "#{path}.cards"), granted)
41
+ end
42
+
43
+ def initialize(zone, cards, granted)
44
+ super()
45
+ @zone = zone
46
+ @cards = cards
47
+ @granted = granted
48
+ freeze
49
+ end
50
+
51
+ def kind = :cards_have
52
+ def mana_ability? = false
53
+ def allowed_types = nil
54
+ def try_effects = []
55
+ # A granted keyword's own triggered ability (miracle's, Abilities::Miracle) belongs to this permanent's card.
56
+ def linked_abilities = granted.respond_to?(:linked_abilities) ? granted.linked_abilities : []
57
+
58
+ # Whether the card `obj` (owned by `index`, who controls this permanent) may be cast with the granted ability now.
59
+ def functions?(engine, obj, index)
60
+ !granted.nil? && obj.zone == zone && cards.match?(engine, obj) && granted.usable?(engine, obj, index)
61
+ end
62
+
63
+ def to_s = "#{cards.to_s.capitalize}s in your #{zone} have #{granted}"
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../costs/sacrifice"
4
+ require_relative "../effects/return_to_battlefield"
5
+
6
+ module Enjine
7
+ module Mechanics
8
+ module Costs
9
+ # Craft's "Exile this permanent" (CR 702.167a): exiles it like `exile_self`, and the craft ability being
10
+ # activated remembers the card in exile ("crafted"), which its effect returns transformed.
11
+ class CraftSelf < ExileSelf
12
+ INSTANCE = new.freeze
13
+
14
+ def pay!(engine, object)
15
+ fresh = super or return nil
16
+ holder = engine.casting && engine.state.objects[engine.casting["object"]]
17
+ holder.memory["crafted"] = fresh.id if holder && fresh.zone == :exile
18
+ fresh
19
+ end
20
+ end
21
+
22
+ # Craft's materials (CR 702.167a-b): "Exile [materials] from among permanents you control and/or cards in
23
+ # your graveyard", never the crafting permanent. `count` of them matching the card spec `card`, or with
24
+ # `or_more` that many or more (`done` once enough are chosen). Each is chosen while activating
25
+ # (`choose_cost`) and exiled as the ability becomes activated; the ability remembers them ("exiled_for_cost").
26
+ class CraftMaterials < CardsCost
27
+ ZONES = %i[battlefield graveyard].freeze
28
+
29
+ def self.zone = :graveyard
30
+
31
+ def self.from(data, errors, path)
32
+ data = {} unless data.is_a?(Hash)
33
+ Mechanics.check_keys(data, %w[card count or_more], errors, path)
34
+ count = Mechanics.integer_param(data, :count, errors, path, default: 1, min: 1)
35
+ raw = Mechanics.fetch_param(data, :card)
36
+ spec = raw && CardSpec.build(raw, errors, "#{path}.card")
37
+ new(spec, count, Mechanics.flag_param(data, :or_more, errors, path), {}, errors, path)
38
+ end
39
+
40
+ def or_more? = @other
41
+
42
+ # A permanent you control (a token too) or a card in your graveyard, other than the crafting permanent.
43
+ def candidates(engine, object)
44
+ pl = engine.player(object.controller)
45
+ graveyard = pl.graveyard.select { |id| filter.nil? || filter.match?(engine, engine.object(id)) }
46
+ permanents = engine.state.battlefield.select do |id|
47
+ id != object.id && engine.object(id).controller == object.controller &&
48
+ (filter.nil? || filter.chars_match?(engine, engine.chars(id)))
49
+ end
50
+ permanents + graveyard
51
+ end
52
+
53
+ def choices_needed = or_more? ? 0 : count
54
+ def payable?(engine, object) = candidates(engine, object).size >= count
55
+ def finishable?(entries) = or_more? && entries.size >= count
56
+
57
+ def chosen?(engine, object, entries, done)
58
+ return entries.size >= count unless or_more?
59
+
60
+ finishable?(entries) && (done || choice_options(engine, object, entries).empty?)
61
+ end
62
+
63
+ # As it becomes activated, the chosen materials still on the battlefield or in the graveyard are exiled.
64
+ def pay_chosen!(engine, _object, entries)
65
+ entries.map { it["object"] }.select { ZONES.include?(engine.state.objects[it]&.zone) }.each { pay_card!(engine, it) }
66
+ end
67
+
68
+ def to_s
69
+ many = or_more? ? "#{count} or more" : { 1 => "a" }.fetch(count, count)
70
+ "Exile #{many} #{filter || "card"}#{"s" unless count == 1} from among permanents you control and/or cards in your graveyard"
71
+ end
72
+ end
73
+ end
74
+
75
+ module Effects
76
+ # Craft's effect (CR 702.167a): "Return this card to the battlefield transformed under its owner's control" —
77
+ # the card the craft ability exiled as its cost (Costs::CraftSelf). Not a card-data primitive.
78
+ class CraftReturn < ReturnToBattlefield
79
+ def self.keys = []
80
+
81
+ def refs(engine, ctx)
82
+ id = engine.state.objects[ctx.object]&.memory&.[]("crafted")
83
+ id && engine.state.exile.include?(id) ? [id] : []
84
+ end
85
+
86
+ def to_s = "return this card to the battlefield transformed"
87
+ end
88
+ end
89
+
90
+ module Abilities
91
+ # Craft with [materials] [cost] (CR 702.167a-c): "[Cost], Exile this permanent, Exile [materials] from among
92
+ # permanents you control and/or cards in your graveyard: Return this card to the battlefield transformed under
93
+ # its owner's control. Activate only as a sorcery." A double-faced card's front face has it; a card that
94
+ # isn't double-faced stays in exile (CR 712.11a, Effects::ReturnToBattlefield).
95
+ class Craft < ActivatedAbility
96
+ primitive :ability, :craft,
97
+ description: "Craft with `materials` `cost`: pay `cost`, exile this permanent and exile the " \
98
+ "materials (`count` (default 1) permanents you control and/or cards in your graveyard " \
99
+ "matching the card spec `card`; `or_more: true`: that many or more), chosen with " \
100
+ "`choose_cost`: return this card to the battlefield transformed under its owner's " \
101
+ "control. Sorcery speed. The exiled materials are the selector \"exiled_for_cost\".",
102
+ rules: %w[702.167a 702.167b 702.167c 712.11a 602.2b 406.1],
103
+ params: { "cost" => "mana cost string, or a list of costs",
104
+ "materials" => "{\"card\": card spec, \"count\": n, \"or_more\": true}" },
105
+ example: { "craft" => { "cost" => "{3}{W}", "materials" => { "card" => { "types" => ["artifact"] } } } }
106
+
107
+ attr_reader :materials
108
+
109
+ def self.build(params, errors, path, **)
110
+ Mechanics.check_keys(params, %w[cost materials], errors, path)
111
+ raw = Mechanics.fetch_param(params, :cost)
112
+ paid = case raw
113
+ when String then [Costs::ManaPayment.build(raw, errors, "#{path}.cost")]
114
+ when Array then StackAbility.parse_costs({ "cost" => raw }, errors, path)
115
+ else (errors << "#{path}.cost: must be a mana cost string or a list of costs") && []
116
+ end
117
+ materials = Costs::CraftMaterials.from(Mechanics.fetch_param(params, :materials), errors, "#{path}.materials")
118
+ effect = Effects::CraftReturn.new(to: nil, params: { "transformed" => true }, errors: errors, path: "#{path}.return")
119
+ new(targets: [].freeze, effects: [effect].freeze, costs: (paid + [Costs::CraftSelf::INSTANCE, materials]).freeze,
120
+ sorcery: true, materials: materials)
121
+ end
122
+
123
+ def initialize(materials:, **)
124
+ @materials = materials
125
+ super(**)
126
+ end
127
+
128
+ def to_s = "Craft — #{costs.join(", ")}: return this card to the battlefield transformed"
129
+ end
130
+ end
131
+ end
132
+ end