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
@@ -25,8 +25,10 @@ module Enjine
25
25
  "if it can. `reveal` shows the found cards to every player. `searcher` names another " \
26
26
  "player who searches their own library (e.g. {\"controller_of\": {\"target\": 0}}); " \
27
27
  "`may: true` lets the searcher decline (then nothing is shuffled). `different_names: true`: " \
28
- "the cards found must all have different names (\"basic land cards with different names\").",
29
- rules: %w[701.23a 701.23b 701.23d 701.23e 701.20a 701.24a 701.24b 118.12 110.2a],
28
+ "the cards found must all have different names (\"basic land cards with different names\"). " \
29
+ "`battlefield_instead`: {\"if\": condition, \"for\": card spec}: a found card matching " \
30
+ "`for` goes onto the battlefield instead of into your hand while the condition holds.",
31
+ rules: %w[701.23a 701.23b 701.23d 701.23e 701.20a 701.24a 701.24b 118.12 110.2a 614.1a],
30
32
  params: { "for" => "card spec: \"card\" (default), {\"types\"|\"subtypes\"|\"supertypes\": [...], " \
31
33
  "\"name\": \"...\"} or {\"any_of\": [spec, ...]}",
32
34
  "count" => "integer (default 1), \"any\" (any number), \"x\" or an amount (\"up to that many\")",
@@ -36,7 +38,8 @@ module Enjine
36
38
  "reveal" => "true: reveal the found cards",
37
39
  "searcher" => "selector of the searching player (default \"you\")",
38
40
  "may" => "true: the searcher may choose not to search",
39
- "different_names" => "true: each card found has a different name" },
41
+ "different_names" => "true: each card found has a different name",
42
+ "battlefield_instead" => "{\"if\": condition, \"for\": card spec} (with put: hand)" },
40
43
  example: { "search_library" => { "for" => { "supertypes" => ["basic"], "types" => ["land"] },
41
44
  "reveal" => true } }
42
45
 
@@ -44,9 +47,21 @@ module Enjine
44
47
 
45
48
  attr_reader :spec, :count, :put, :tapped, :reveal
46
49
 
47
- def self.keys = %w[for count put tapped counters reveal searcher may different_names if]
50
+ def self.keys = %w[for count put tapped counters reveal searcher may different_names battlefield_instead if]
48
51
  def self.default_to = "you"
49
52
 
53
+ # "If [condition] and the revealed card is [a quality], put it onto the battlefield instead" (CR 614.1a).
54
+ def parse_instead(raw, errors, path)
55
+ return nil if raw.nil?
56
+ return (errors << "#{path}: must be {\"if\": condition, \"for\": card spec}") && nil unless raw.is_a?(Hash)
57
+
58
+ Mechanics.check_keys(raw.transform_keys(&:to_s), %w[if for], errors, path)
59
+ errors << "#{path}: only a search that puts cards into a hand" unless @put == "hand"
60
+ cond = Mechanics.fetch_param(raw, :if)
61
+ { "if" => cond && Condition.build(cond, errors, "#{path}.if", targets_count: @targets_count),
62
+ "for" => CardSpec.build(Mechanics.fetch_param(raw, :for), errors, "#{path}.for") }
63
+ end
64
+
50
65
  def setup(params, errors, path)
51
66
  @spec = CardSpec.build(Mechanics.fetch_param(params, :for), errors, "#{path}.for")
52
67
  @count = parse_count(Mechanics.fetch_param(params, :count) || 1, errors, path)
@@ -61,6 +76,7 @@ module Enjine
61
76
  @counters = parse_counters(Mechanics.fetch_param(params, :counters), errors, path)
62
77
  raw = Mechanics.fetch_param(params, :searcher) || "you"
63
78
  @searcher = Selector.build(raw, errors, "#{path}.searcher", targets_count: @targets_count, results_count: @results_count)
79
+ @instead = parse_instead(Mechanics.fetch_param(params, :battlefield_instead), errors, "#{path}.battlefield_instead")
64
80
  end
65
81
 
66
82
  def parse_count(raw, errors, path)
@@ -108,7 +124,8 @@ module Enjine
108
124
  choices = candidates(engine, ctx, index, found)
109
125
  return ask(engine, locals, index, "Search your library (#{spec}): find a card", choices) unless choices.empty?
110
126
  end
111
- return :wait if entering_choices?(engine, index, locals, found)
127
+ locals["instead"] ||= found.select { instead?(engine, ctx, it) }
128
+ return :wait if engine.ask_entering_choices!(locals, entering(found, locals).map { [it, index] }) == :wait
112
129
 
113
130
  finish!(engine, index, found, locals)
114
131
  :done
@@ -154,9 +171,20 @@ module Enjine
154
171
  enters = {}
155
172
  enters["tapped"] = true if tapped
156
173
  enters["counters"] = @counters.dup unless @counters.empty?
157
- put_cards!(engine, locals, found, index, reason: "search", enters: enters.empty? ? nil : enters)
174
+ instead = locals["instead"]
175
+ engine.enter_battlefield!(locals, instead.map { [it, index, nil] }, reason: "search") unless instead.empty?
176
+ put_cards!(engine, locals, found - instead, index, reason: "search", enters: enters.empty? ? nil : enters)
158
177
  engine.shuffle_library!(index) # CR 701.24b: the found cards have left; the rest is shuffled
159
178
  end
179
+
180
+ # The cards entering the battlefield (their "as this enters" choices are asked first, CR 614.12).
181
+ def entering(found, locals) = put == "battlefield" ? found : locals["instead"]
182
+
183
+ def instead?(engine, ctx, id)
184
+ return false unless @instead && (@instead["if"].nil? || @instead["if"].true?(engine, ctx))
185
+
186
+ @instead["for"].match?(engine, engine.object(id), ctx)
187
+ end
160
188
  end
161
189
  end
162
190
  end
@@ -6,21 +6,25 @@ module Enjine
6
6
  module Mechanics
7
7
  module Effects
8
8
  # Create token permanents (CR 111.1, 701.7).
9
- class CreateToken < Effect
9
+ class CreateToken < ChoiceEffect
10
10
  primitive :effect, :create_token,
11
11
  description: "Create `amount` tokens under the referenced player's control (default: you). `token` is " \
12
12
  "a predefined token name (Treasure, Clue, Food) or an inline card definition. " \
13
13
  "`tapped: true` creates them tapped; `attacking: true` attacking (only for the attacking " \
14
- "player during combat, CR 506.3b).",
15
- rules: %w[111.1 111.10 701.7a 704.5d 508.4 506.3b],
14
+ "player during combat, CR 506.3b); when the defending player controls a planeswalker, " \
15
+ "the controller chooses for each token whether it attacks that player or one of their " \
16
+ "planeswalkers (CR 508.4). `size` (an amount, fixed as the effect starts) makes them N/N " \
17
+ "(\"an X/X Spirit token, where X is ...\"; part of their copiable values).",
18
+ rules: %w[111.1 111.10 701.7a 704.5d 508.4 506.3b 707.2],
16
19
  params: { "token" => "\"Treasure\" | \"Clue\" | \"Food\" | {card data}", "amount" => "integer (default 1)",
17
- "tapped" => "boolean", "attacking" => "boolean", "to" => "selector (default \"you\")" },
20
+ "tapped" => "boolean", "attacking" => "boolean", "to" => "selector (default \"you\")",
21
+ "size" => "amount: the tokens are N/N" },
18
22
  example: { "create_token" => { "token" => { "name" => "Soldier", "types" => ["creature"],
19
23
  "subtypes" => ["soldier"], "power" => 1, "toughness" => 1 } } }
20
24
 
21
25
  attr_reader :definition, :tapped, :attacking
22
26
 
23
- def self.keys = %w[token amount tapped attacking to if]
27
+ def self.keys = %w[token amount tapped attacking size to if]
24
28
  ATTACK_STEPS = %i[declare_attackers declare_blockers combat_damage end_of_combat].freeze
25
29
  def self.default_to = "you"
26
30
 
@@ -35,6 +39,9 @@ module Enjine
35
39
  data = raw.is_a?(String) ? Enjine::Tokens.fetch(raw) : raw
36
40
  @tapped = Mechanics.fetch_param(params, :tapped) == true
37
41
  @attacking = Mechanics.fetch_param(params, :attacking) == true
42
+ raw_size = Mechanics.fetch_param(params, :size)
43
+ @size = raw_size && Amount.build(raw_size, errors, "#{path}.size", targets_count: @targets_count,
44
+ results_count: @results_count)
38
45
  unless data.is_a?(Hash)
39
46
  errors << "#{path}.token: must be a predefined token (#{Enjine::Tokens::LIBRARY.keys.join(", ")}) or card data"
40
47
  return
@@ -49,27 +56,52 @@ module Enjine
49
56
  def run(engine, ctx, locals)
50
57
  return :done unless definition && (locals.key?("tokens") || condition.nil? || condition.true?(engine, ctx))
51
58
 
52
- n = amount_for(engine, ctx) unless locals.key?("tokens")
59
+ unless locals.key?("tokens")
60
+ n = amount_for(engine, ctx)
61
+ locals["size"] = [@size.value(engine, ctx), 0].max if @size
62
+ end
53
63
  entries = locals["tokens"] ||= players(engine, ctx).flat_map do |index|
54
64
  Array.new(n) { [engine.token_key(definition, "#{index}.#{it}"), index] }
55
65
  end
56
66
  return :wait if engine.ask_entering_choices!(locals, entries) == :wait
67
+ return :wait if ask_attack_targets(engine, locals, entries) == :wait
57
68
 
58
69
  entries.each do |key, index|
59
70
  enters = engine.with_entering_answer(locals, key, index, nil)
60
- enters = (enters || {}).merge("attacking" => true) if attacks?(engine, index)
61
- engine.create_token!(definition, index, tapped: tapped, enters: enters)
71
+ enters = (enters || {}).merge("attacking" => locals.dig("attacks", key) || true) if attacks?(engine, index)
72
+ engine.create_token!(definition, index, tapped: tapped, enters: enters, memory: size_memory(locals))
62
73
  end
63
74
  :done
64
75
  end
65
76
 
66
77
  def to_s
67
- "create #{amount} #{definition&.name} token#{"s" if amount != 1}#{" tapped" if tapped}" \
68
- "#{" attacking" if attacking} → #{to}"
78
+ "create #{amount} #{"#{@size}/#{@size} " if @size}#{definition&.name} token#{"s" if amount != 1}" \
79
+ "#{" tapped" if tapped}#{" attacking" if attacking} → #{to}"
80
+ end
81
+
82
+ # An N/N token: N is part of its copiable values (Engine::Copies, "copy_except"; CR 707.2).
83
+ def size_memory(locals)
84
+ n = locals["size"] or return nil
85
+ { "copy_except" => { "power" => n, "toughness" => n } }
69
86
  end
70
87
 
71
88
  # CR 508.4, 506.3b: tokens enter attacking only under the attacking player's control, in combat.
72
89
  def attacks?(engine, index) = attacking && engine.state.active == index && ATTACK_STEPS.include?(engine.state.step)
90
+
91
+ # CR 508.4: for each token entering attacking, its controller chooses the defending player or a planeswalker
92
+ # they control, before any is created (kept in locals["attacks"], token key => target).
93
+ def ask_attack_targets(engine, locals, entries)
94
+ targets = engine.attack_targets
95
+ return if targets.size < 2
96
+
97
+ keys = entries.select { |_, index| attacks?(engine, index) }.map(&:first)
98
+ locals["attacks"] = keys.zip(answers(locals).map { it["target"] }).to_h.compact
99
+ entry = entries.find { |key, _| keys.include?(key) && !locals["attacks"].key?(key) }
100
+ return unless entry
101
+
102
+ ask(engine, locals, entry[1], "Choose what the #{definition.name} token attacks",
103
+ targets.map { { "type" => "attack", "target" => it } })
104
+ end
73
105
  end
74
106
  end
75
107
  end
@@ -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
 
@@ -97,23 +100,27 @@ module Enjine
97
100
  # {"creature": {"keywords": ["flying"]}} with flying (any listed); "not_keywords": without (CR 702.1)
98
101
  # {"creature": {"subtypes": "chosen"}} of the chosen creature type; also "colors", "types", "not",
99
102
  # "not_subtypes": "chosen" (CR 607.2d, see Filters.chosen_memory)
103
+ # {"land": {"name": "chosen"}} with the chosen card name (CR 201.4)
100
104
  # {"creature": {"transformed": true}} back face up (CR 701.27h); {"prepared": true} (CR 723.3a)
101
105
  # {"creature": {"saddled": true}} saddled this turn (CR 702.171b)
102
106
  # {"creature": {"entered_this_turn": true}} entered the battlefield this turn
107
+ # {"spell": {"single_target": true}} a spell with a single target (CR 115.7)
103
108
  class Restricted
104
109
  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
110
+ BOOLEANS = %w[other attacking blocking tapped token modified transformed prepared saddled entered_this_turn
111
+ single_target].freeze
106
112
  RANGES = %w[power toughness mana_value].freeze
107
113
  TYPE_LISTS = %w[not any_of types].freeze
108
114
  NAME_LISTS = %w[subtypes not_subtypes supertypes not_supertypes attached_subtypes].freeze
109
115
  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).
116
+ KEYS = (%w[controller owner counters colors name] + BOOLEANS + RANGES + TYPE_LISTS + NAME_LISTS + KEYWORD_LISTS).freeze
117
+ # Lists that may be "chosen": the choice remembered under that memory key (CR 607.2d). "name" is only
118
+ # "chosen" ("with the chosen name", CR 201.4).
112
119
  CHOSEN = { "colors" => "chosen_color", "types" => "chosen_card_type", "not" => "chosen_card_type",
113
- "subtypes" => "chosen_type", "not_subtypes" => "chosen_type" }.freeze
120
+ "subtypes" => "chosen_type", "not_subtypes" => "chosen_type", "name" => "chosen_name" }.freeze
114
121
  # Keys that need current characteristics or combat/tapped status: not usable in the layer pass.
115
122
  LIVE = %w[power toughness attacking blocking tapped attached_subtypes transformed prepared saddled
116
- entered_this_turn].freeze
123
+ entered_this_turn single_target].freeze
117
124
 
118
125
  attr_reader :base
119
126
 
@@ -135,6 +142,7 @@ module Enjine
135
142
  (colors.is_a?(Array) && !colors.empty? && colors.all? { Mana::COLORS.include?(it.to_s.to_sym) })
136
143
  errors << "#{path}.colors: must be a non-empty list of W, U, B, R, G (any of them) or \"chosen\""
137
144
  end
145
+ errors << "#{path}.name: must be \"chosen\"" unless [nil, "chosen"].include?(params["name"])
138
146
  new(base, params, ranges)
139
147
  end
140
148
 
@@ -216,13 +224,13 @@ module Enjine
216
224
  # left the battlefield counts as it last existed, CR 608.2h).
217
225
  def candidates_for(engine, controller, source, ctx: nil)
218
226
  bounds = resolved_ranges(engine, controller, source, ctx)
219
- base.candidates(engine, controller).select { allowed?(engine, it, controller, source, bounds, ctx) }
227
+ base.candidates_for(engine, controller, source, ctx: ctx).select { allowed?(engine, it, controller, source, bounds, ctx) }
220
228
  end
221
229
 
222
230
  def match?(engine, ref, controller) = match_for?(engine, ref, controller, nil)
223
231
 
224
232
  def match_for?(engine, ref, controller, source, ctx: nil)
225
- base.match?(engine, ref, controller) &&
233
+ base.match_for?(engine, ref, controller, source, ctx: ctx) &&
226
234
  allowed?(engine, ref, controller, source, resolved_ranges(engine, controller, source, ctx), ctx)
227
235
  end
228
236
 
@@ -248,7 +256,7 @@ module Enjine
248
256
  words.concat(ownership_words)
249
257
  words.concat(@ranges.map { |key, range| "with #{key.tr("_", " ")} #{range_words(range)}" })
250
258
  words << "with a #{@lists["attached_subtypes"].join(" or ")} attached" unless @lists["attached_subtypes"].empty?
251
- (words + [counter_words] + keyword_words).compact.join(" ")
259
+ (words + [counter_words, ("with a single target" if @params["single_target"])] + keyword_words).compact.join(" ")
252
260
  end
253
261
 
254
262
  RANGE_WORDS = { "at_least" => "%s or greater", "at_most" => "%s or less", "equal" => "equal to %s" }.freeze
@@ -314,7 +322,7 @@ module Enjine
314
322
  return false if @params["modified"] == true && !modified?(engine, obj)
315
323
  return false unless attached_ok?(engine, obj)
316
324
 
317
- bounds.all? { |key, range| in_range?(stat(engine, obj, current, key), range) }
325
+ bounds.all? { |key, range| in_range?(stat(engine, obj, key), range) }
318
326
  end
319
327
 
320
328
  # Types, subtypes, supertypes, keywords, the chosen values, token-ness, counters and printed mana
@@ -346,6 +354,7 @@ module Enjine
346
354
  have = case key
347
355
  when "colors" then chars.colors
348
356
  when "types", "not" then chars.types
357
+ when "name" then [chars.name.to_sym]
349
358
  else chars.subtypes
350
359
  end
351
360
  hit = !value.nil? && have.include?(value.to_sym)
@@ -368,6 +377,7 @@ module Enjine
368
377
  return false if !@params["prepared"].nil? && engine.prepared?(obj) != @params["prepared"] # CR 723.3a
369
378
  return false if !@params["saddled"].nil? && engine.saddled?(obj) != @params["saddled"] # CR 702.171b
370
379
  return false if !@params["entered_this_turn"].nil? && engine.entered_this_turn?(obj) != @params["entered_this_turn"]
380
+ return false if !@params["single_target"].nil? && Filters.single_target?(obj) != @params["single_target"]
371
381
 
372
382
  @params["tapped"].nil? || obj.tapped == @params["tapped"]
373
383
  end
@@ -384,12 +394,12 @@ module Enjine
384
394
  Counters.any?(obj.counters) || engine.modifying_attachment?(obj)
385
395
  end
386
396
 
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)
397
+ # Current power/toughness (CR 208.3) and mana value (CR 202.3; 0 without a mana cost, 202.3a; Amount.mana_value).
398
+ def stat(engine, obj, key)
389
399
  case key
390
400
  when "power" then engine.power(obj)
391
401
  when "toughness" then engine.toughness(obj)
392
- else current.mana_cost&.mana_value.to_i
402
+ else Amount.mana_value(engine, obj)
393
403
  end
394
404
  end
395
405
 
@@ -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
@@ -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?
@@ -147,7 +149,7 @@ module Enjine
147
149
 
148
150
  def zone = :graveyard
149
151
  def play = alternative? ? "cast" : "play"
150
- def functions?(engine, obj, _index) = obj.zone == zone && engine.discarded_this_turn?(obj)
152
+ def usable?(engine, obj, _index) = engine.discarded_this_turn?(obj)
151
153
  end
152
154
 
153
155
  # CR 702.185a-c: warp, from the hand. The permanent is exiled at the beginning of the next end step; its
@@ -228,9 +230,7 @@ module Enjine
228
230
  def returns = "unblocked"
229
231
  def instant_speed? = true
230
232
 
231
- def functions?(engine, obj, index)
232
- obj.zone == zone && engine.state.active == index && engine.state.step == :declare_blockers
233
- end
233
+ def usable?(engine, _obj, index) = engine.state.active == index && engine.state.step == :declare_blockers
234
234
  end
235
235
  end
236
236
 
@@ -304,6 +304,10 @@ module Enjine
304
304
  freeze
305
305
  end
306
306
 
307
+ # Whether `condition` is "if the gift was promised" (the gift triggered ability of a permanent, CR 702.174b).
308
+ def self.gift?(condition) = condition.is_a?(CastHow) && condition.gift?
309
+ def gift? = @key == "cast_with" && @value == "gift"
310
+
307
311
  def true?(engine, ctx)
308
312
  return engine.cast_with_this_turn?(@value) if @key == "cast_with_this_turn"
309
313
 
@@ -0,0 +1,65 @@
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].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 or sneak) 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
+
56
+ # Whether the card `obj` (owned by `index`, who controls this permanent) may be cast with the granted ability now.
57
+ def functions?(engine, obj, index)
58
+ !granted.nil? && obj.zone == zone && cards.match?(engine, obj) && granted.usable?(engine, obj, index)
59
+ end
60
+
61
+ def to_s = "#{cards.to_s.capitalize}s in your #{zone} have #{granted}"
62
+ end
63
+ end
64
+ end
65
+ end
@@ -17,7 +17,9 @@ module Enjine
17
17
  %w[514.2 120.6]],
18
18
  abilities_cant_be_activated: ["Its activated abilities, mana abilities included, can't be activated (\"Activated " \
19
19
  "abilities of artifacts can't be activated\" is a static handing it out).",
20
- %w[602.5 605.3a 101.2]]
20
+ %w[602.5 605.3a 101.2]],
21
+ cant_become_untapped: ["This permanent can't become untapped: not in its controller's untap step nor by an " \
22
+ "effect (\"untap it\" does nothing).", %w[701.26b 502.3 101.2]]
21
23
  }.each { |name, (description, rules)| Evergreen.define(name, description, rules) }
22
24
 
23
25
  # Combat restrictions and requirements (CR 508.1c-d, 509.1b). They aren't keyword abilities
@@ -6,10 +6,10 @@ module Enjine
6
6
  module Mechanics
7
7
  module Abilities
8
8
  # Hideaway N (CR 702.75a): "When this permanent enters, look at the top N cards of your library. Exile one
9
- # of them face down and put the rest on the bottom of your library in a random order." Only its controller
10
- # may look at the exiled card. It's linked to the permanent (CR 607.2a): another of its abilities names it
11
- # with the selector "exiled_with", e.g. `cast_free` with `play: true` ("you may play the exiled card
12
- # without paying its mana cost").
9
+ # of them face down and put the rest on the bottom of your library in a random order." The player who
10
+ # controls the permanent may look at the exiled card (`looker`). It's linked to the permanent (CR 607.2a):
11
+ # another of its abilities names it with the selector "exiled_with", e.g. `cast_free` with `play: true`
12
+ # ("you may play the exiled card without paying its mana cost").
13
13
  class Hideaway < NumberedTrigger
14
14
  primitive :ability, :hideaway,
15
15
  description: "Hideaway `amount`: when this permanent enters, look at the top N cards of your library, " \
@@ -20,7 +20,8 @@ module Enjine
20
20
 
21
21
  def self.expansion(amount)
22
22
  { "when" => { "enters" => {} },
23
- "effects" => [{ "dig" => { "amount" => amount, "put" => "exile", "face_down" => true, "link" => true } }] }
23
+ "effects" => [{ "dig" => { "amount" => amount, "put" => "exile", "face_down" => true, "link" => true,
24
+ "looker" => "source_controller" } }] }
24
25
  end
25
26
  end
26
27
  end
@@ -68,24 +68,42 @@ module Enjine
68
68
  # An additional cost: "As an additional cost to cast this spell, sacrifice a creature." (CR 601.2b,
69
69
  # 601.2f-h, 118.8). An optional one ("you may behold a Dragon", CR 118.8b) is announced like kicker (the
70
70
  # `choose_kicker` decision) without making the spell kicked: {"cast_with": "additional_cost"} holds if it
71
- # was paid.
71
+ # was paid. "You may cast this spell as though it had flash if you behold a Dragon as an additional cost"
72
+ # (`flash: true`, CR 601.3c): it may be begun any time you could cast an instant, and then the cost must be paid.
72
73
  class AdditionalCost < SpellCost
73
74
  primitive :ability, :additional_cost,
74
75
  description: "As an additional cost to cast this spell, pay `cost` too (mana and/or chosen costs " \
75
76
  "such as `sacrifice_permanent`). The spell can't be cast without it. `optional: true`: " \
76
77
  "you may pay it (announced with the `choose_kicker` decision); the condition " \
77
- "`{\"cast_with\": \"additional_cost\"}` then holds (the spell isn't kicked).",
78
- rules: %w[601.2b 601.2f 601.2h 118.8 118.8b],
79
- params: { "cost" => "list of costs", "optional" => "true: you may pay it" },
78
+ "`{\"cast_with\": \"additional_cost\"}` then holds (the spell isn't kicked). With " \
79
+ "`flash: true` (optional only) you may cast it from your hand as though it had flash if " \
80
+ "you pay it: cast that way, only paying it is offered.",
81
+ rules: %w[601.2b 601.2f 601.2h 118.8 118.8b 601.3c],
82
+ params: { "cost" => "list of costs", "optional" => "true: you may pay it",
83
+ "flash" => "true: as though it had flash if you pay it" },
80
84
  example: { "additional_cost" => { "cost" => [{ "sacrifice_permanent" => { "filter" => "creature" } }] } }
81
85
 
82
86
  def self.build(params, errors, path, **)
83
87
  optional = Mechanics.flag_param(params, :optional, errors, path)
84
- new(super(params.except("optional", :optional), errors, path).costs, optional: optional)
88
+ flash = Mechanics.flag_param(params, :flash, errors, path)
89
+ errors << "#{path}.flash: needs optional: true" if flash && !optional
90
+ new(super(params.except("optional", :optional, "flash", :flash), errors, path).costs, optional: optional, flash: flash)
85
91
  end
86
92
 
93
+ def initialize(costs, optional: false, flash: false)
94
+ @flash = flash
95
+ super(costs, optional: optional)
96
+ end
97
+
98
+ # CR 601.3c: the spell may be begun as though it had flash, if this cost is paid.
99
+ def flash? = @flash
100
+
87
101
  def kind = @optional ? :kicker : :additional_cost
88
- def to_s = "As an additional cost to cast this spell#{", you may" if @optional}: #{costs.join(", ")}"
102
+
103
+ def to_s
104
+ text = "As an additional cost to cast this spell#{", you may" if @optional}: #{costs.join(", ")}"
105
+ @flash ? "#{text} (as though it had flash if you do)" : text
106
+ end
89
107
  end
90
108
  end
91
109
  end