enjine 0.3.0 → 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 (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +91 -1
  3. data/SUPPORTED.md +34 -26
  4. data/docs/guides/card-data-format.md +143 -1
  5. data/docs/plans/dsl-gaps.md +99 -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 +118 -25
  10. data/docs/reference/filters.md +19 -2
  11. data/docs/reference/keywords.md +16 -2
  12. data/docs/reference/triggers.md +28 -7
  13. data/lib/enjine/actions/cast.rb +10 -3
  14. data/lib/enjine/actions/play_land.rb +3 -1
  15. data/lib/enjine/card_definition.rb +19 -3
  16. data/lib/enjine/cli/text.rb +10 -3
  17. data/lib/enjine/decisions/combat.rb +4 -2
  18. data/lib/enjine/decisions/damage_order.rb +18 -0
  19. data/lib/enjine/engine/ability_layers.rb +6 -1
  20. data/lib/enjine/engine/casting/alt_casting.rb +44 -4
  21. data/lib/enjine/engine/casting/cast_choices.rb +13 -1
  22. data/lib/enjine/engine/casting/casting.rb +43 -7
  23. data/lib/enjine/engine/casting/cost_options.rb +3 -1
  24. data/lib/enjine/engine/casting/permissions.rb +27 -23
  25. data/lib/enjine/engine/casting/target_counts.rb +14 -3
  26. data/lib/enjine/engine/casting/waterbend.rb +22 -2
  27. data/lib/enjine/engine/characteristics.rb +32 -14
  28. data/lib/enjine/engine/combat.rb +26 -7
  29. data/lib/enjine/engine/copies.rb +26 -12
  30. data/lib/enjine/engine/damage.rb +51 -1
  31. data/lib/enjine/engine/durations.rb +47 -0
  32. data/lib/enjine/engine/entering.rb +36 -2
  33. data/lib/enjine/engine/face_down.rb +12 -3
  34. data/lib/enjine/engine/faces.rb +16 -2
  35. data/lib/enjine/engine/follow_ups.rb +4 -1
  36. data/lib/enjine/engine/resolution.rb +46 -1
  37. data/lib/enjine/engine/rules.rb +6 -4
  38. data/lib/enjine/engine/state_based_actions.rb +8 -0
  39. data/lib/enjine/engine/triggers.rb +18 -21
  40. data/lib/enjine/engine/turn_history.rb +16 -1
  41. data/lib/enjine/engine/turn_structure.rb +1 -0
  42. data/lib/enjine/engine/zones.rb +23 -4
  43. data/lib/enjine/events.rb +6 -1
  44. data/lib/enjine/frames.rb +2 -2
  45. data/lib/enjine/mana.rb +3 -0
  46. data/lib/enjine/mechanics/abilities/chosen_values.rb +49 -6
  47. data/lib/enjine/mechanics/abilities/damage_replacements.rb +136 -64
  48. data/lib/enjine/mechanics/abilities/double_counters.rb +1 -1
  49. data/lib/enjine/mechanics/abilities/entering_choices.rb +40 -21
  50. data/lib/enjine/mechanics/abilities/play_permissions.rb +52 -19
  51. data/lib/enjine/mechanics/abilities/player_rules.rb +30 -13
  52. data/lib/enjine/mechanics/abilities/replacement_effects.rb +2 -1
  53. data/lib/enjine/mechanics/abilities/static.rb +8 -1
  54. data/lib/enjine/mechanics/card_spec.rb +99 -14
  55. data/lib/enjine/mechanics/costs/base.rb +67 -9
  56. data/lib/enjine/mechanics/costs/behold.rb +21 -5
  57. data/lib/enjine/mechanics/costs/choose_creature_type.rb +43 -0
  58. data/lib/enjine/mechanics/costs/one_of.rb +3 -0
  59. data/lib/enjine/mechanics/costs/pay_life.rb +27 -8
  60. data/lib/enjine/mechanics/costs/remove_counters.rb +8 -5
  61. data/lib/enjine/mechanics/costs/tap.rb +44 -3
  62. data/lib/enjine/mechanics/effects/base.rb +10 -2
  63. data/lib/enjine/mechanics/effects/become.rb +60 -21
  64. data/lib/enjine/mechanics/effects/copy.rb +63 -23
  65. data/lib/enjine/mechanics/effects/counters.rb +8 -2
  66. data/lib/enjine/mechanics/effects/damage.rb +11 -0
  67. data/lib/enjine/mechanics/effects/earthbend.rb +2 -1
  68. data/lib/enjine/mechanics/effects/fight.rb +11 -0
  69. data/lib/enjine/mechanics/effects/gain_control.rb +6 -3
  70. data/lib/enjine/mechanics/effects/library.rb +76 -18
  71. data/lib/enjine/mechanics/effects/pay.rb +7 -4
  72. data/lib/enjine/mechanics/effects/repeat.rb +92 -0
  73. data/lib/enjine/mechanics/effects/return_to_battlefield.rb +25 -6
  74. data/lib/enjine/mechanics/effects/search.rb +34 -6
  75. data/lib/enjine/mechanics/effects/tokens.rb +42 -10
  76. data/lib/enjine/mechanics/filters/base.rb +40 -13
  77. data/lib/enjine/mechanics/filters/exile.rb +27 -0
  78. data/lib/enjine/mechanics/filters/stack.rb +15 -9
  79. data/lib/enjine/mechanics/keywords/alternative_costs.rb +10 -6
  80. data/lib/enjine/mechanics/keywords/cards_have.rb +65 -0
  81. data/lib/enjine/mechanics/keywords/flags.rb +3 -1
  82. data/lib/enjine/mechanics/keywords/hideaway.rb +6 -5
  83. data/lib/enjine/mechanics/keywords/kicker.rb +24 -6
  84. data/lib/enjine/mechanics/keywords/protection.rb +24 -8
  85. data/lib/enjine/mechanics/keywords/riot.rb +13 -10
  86. data/lib/enjine/mechanics/linked.rb +32 -2
  87. data/lib/enjine/mechanics/selector.rb +32 -9
  88. data/lib/enjine/mechanics/triggers/base.rb +14 -3
  89. data/lib/enjine/mechanics/triggers/casting.rb +16 -10
  90. data/lib/enjine/mechanics/triggers/combat.rb +32 -12
  91. data/lib/enjine/mechanics/triggers/keyword_actions.rb +17 -0
  92. data/lib/enjine/mechanics/triggers/tap_and_counters.rb +38 -11
  93. data/lib/enjine/mechanics/triggers/zone_changes.rb +1 -1
  94. data/lib/enjine/mechanics/values.rb +20 -10
  95. data/lib/enjine/mechanics.rb +5 -0
  96. data/lib/enjine/replacements.rb +3 -2
  97. data/lib/enjine/rules_monitor.rb +17 -4
  98. data/lib/enjine/steps.rb +10 -1
  99. data/lib/enjine/version.rb +1 -1
  100. data/lib/enjine/view.rb +1 -1
  101. metadata +7 -1
@@ -9,22 +9,28 @@ module Enjine
9
9
  class ReturnToBattlefield < Effect
10
10
  UNDER = %w[owner you].freeze
11
11
  ZONES = %i[exile graveyard].freeze
12
+ # What it can be "as it enters" (`as`): a `become` without a duration, target or abilities to grant.
13
+ AS_KEYS = (Become::CHANGES - %w[abilities]).freeze
12
14
 
13
15
  primitive :effect, :return_to_battlefield,
14
16
  description: "Put each referenced card that is in exile or a graveyard onto the battlefield under its " \
15
17
  "owner's control (`under: \"you\"`: under the controller's), tapped if `tapped: true`. " \
16
18
  "A card that is no longer in the zone it was expected in, or isn't a permanent card, " \
17
19
  "stays where it is. `transformed: true`: it enters with its back face up; a card that " \
18
- "isn't double-faced, or whose back face isn't a permanent, stays where it is.",
19
- rules: %w[400.7 400.4a 110.2 111.8 603.7c 614.1c 122.6 712.11a 712.11b],
20
+ "isn't double-faced, or whose back face isn't a permanent, stays where it is. `as`: " \
21
+ "what it is as it enters, for as long as it stays (the `become` changes, without " \
22
+ "abilities): \"It's an enchantment. (It's not a creature.)\" is " \
23
+ "`{\"set_types\": [\"enchantment\"]}`; abilities that trigger on it entering see it so.",
24
+ rules: %w[400.7 400.4a 110.2 111.8 603.7c 614.1c 122.6 712.11a 712.11b 205.1a],
20
25
  params: { "to" => "selector (usually {\"result\": n})", "tapped" => "boolean", "transformed" => "boolean",
21
26
  "under" => UNDER.join(" | "),
22
- "counters" => "{counter name: n}: it enters with these counters (\"with a +1/+1 counter on it\")" },
27
+ "counters" => "{counter name: n}: it enters with these counters (\"with a +1/+1 counter on it\")",
28
+ "as" => "{#{AS_KEYS.join(", ")}}: as `become`, from the moment it enters" },
23
29
  example: { "return_to_battlefield" => { "to" => { "result" => 0 } } }
24
30
 
25
- attr_reader :tapped, :under, :counters, :transformed
31
+ attr_reader :tapped, :under, :counters, :transformed, :as
26
32
 
27
- def self.keys = %w[to tapped under counters transformed if]
33
+ def self.keys = %w[to tapped under counters transformed as if]
28
34
 
29
35
  def setup(params, errors, path)
30
36
  @tapped = Mechanics.fetch_param(params, :tapped) == true
@@ -32,6 +38,17 @@ module Enjine
32
38
  @under = Mechanics.fetch_param(params, :under) || "owner"
33
39
  errors << "#{path}.under: must be one of #{UNDER.join(", ")}" unless UNDER.include?(@under)
34
40
  @counters = parse_counters(Mechanics.fetch_param(params, :counters) || {}, errors, "#{path}.counters")
41
+ @as = parse_as(Mechanics.fetch_param(params, :as), errors, "#{path}.as")
42
+ end
43
+
44
+ def parse_as(raw, errors, path)
45
+ return nil if raw.nil?
46
+ return (errors << "#{path}: must be an object of become changes") && nil unless raw.is_a?(Hash)
47
+
48
+ Mechanics.check_keys(raw, AS_KEYS, errors, path)
49
+ return (errors << "#{path}: needs at least one of #{AS_KEYS.join(", ")}") && nil unless raw.keys.intersect?(AS_KEYS)
50
+
51
+ Become.build(raw.slice(*AS_KEYS).merge("to" => "self", "duration" => "indefinite"), errors, path)
35
52
  end
36
53
 
37
54
  def parse_counters(raw, errors, path)
@@ -47,6 +64,7 @@ module Enjine
47
64
  def to_s
48
65
  extra = counters.map { |kind, n| " with #{n} #{kind} counter(s)" }.join
49
66
  status = "#{" tapped" if tapped}#{" transformed" if transformed}"
67
+ extra += " as it #{as.to_s.delete_suffix(" → self")}" if as
50
68
  "return to the battlefield#{status}#{" under your control" if under == "you"}#{extra} → #{to}"
51
69
  end
52
70
 
@@ -59,8 +77,9 @@ module Enjine
59
77
  entries = entries.select { engine.state.objects.key?(it[0]) }
60
78
  return :wait if engine.ask_entering_choices!(locals, entries) == :wait
61
79
 
80
+ # "as": the Become itself, used by the move that follows at once (never saved, Engine::Zones#enter_as!)
62
81
  enters = { "tapped" => tapped || nil, "counters" => counters.empty? ? nil : counters.dup,
63
- "face" => transformed ? 1 : nil }.compact
82
+ "face" => transformed ? 1 : nil, "as" => as }.compact
64
83
  engine.enter_battlefield!(locals, entries.map { [*it, enters.empty? ? nil : enters] }, reason: "return")
65
84
  :done
66
85
  end
@@ -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
 
@@ -19,6 +22,13 @@ module Enjine
19
22
  # What is known about an object without computing its current characteristics: its printed
20
23
  # (or last known) characteristics, its counters and whether it is a token. `chosen`: the memory of
21
24
  # the object whose choice "the chosen color/type" refers to (CR 607.2d), when the filter needs one.
25
+ # A filter matched on characteristics only (Facts) can't know who owns the object: `owner` there is an error,
26
+ # never silently ignored (CR 108.3). Returns the filter.
27
+ def self.without_owner!(filter, errors, path)
28
+ errors << "#{path}: owner can't be used here yet" if filter.respond_to?(:owner) && filter.owner
29
+ filter
30
+ end
31
+
22
32
  Facts = Data.define(:chars, :counters, :token, :chosen) do
23
33
  def initialize(chars:, counters:, token:, chosen: nil) = super
24
34
  end
@@ -79,6 +89,7 @@ module Enjine
79
89
 
80
90
  # A filter narrowed by restrictions (CR 115.1, 109.2), e.g.
81
91
  # {"creature": {"controller": "you"}} {"creature": {"other": true}}
92
+ # {"creature": {"owner": "you"}} "a creature you own" (CR 108.3); with controller: "you control but don't own"
82
93
  # {"permanent": {"not": ["land"]}} {"permanent": {"any_of": ["artifact", "creature"]}}
83
94
  # {"creature": {"attacking": true}} {"creature": {"power": {"at_least": 4}}}
84
95
  # {"permanent": {"mana_value": {"at_most": 3}}} {"creature": {"subtypes": ["human"]}}
@@ -89,29 +100,34 @@ module Enjine
89
100
  # {"creature": {"keywords": ["flying"]}} with flying (any listed); "not_keywords": without (CR 702.1)
90
101
  # {"creature": {"subtypes": "chosen"}} of the chosen creature type; also "colors", "types", "not",
91
102
  # "not_subtypes": "chosen" (CR 607.2d, see Filters.chosen_memory)
103
+ # {"land": {"name": "chosen"}} with the chosen card name (CR 201.4)
92
104
  # {"creature": {"transformed": true}} back face up (CR 701.27h); {"prepared": true} (CR 723.3a)
93
105
  # {"creature": {"saddled": true}} saddled this turn (CR 702.171b)
94
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)
95
108
  class Restricted
96
109
  TYPES = %w[artifact creature enchantment land planeswalker instant sorcery].freeze
97
- 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
98
112
  RANGES = %w[power toughness mana_value].freeze
99
113
  TYPE_LISTS = %w[not any_of types].freeze
100
114
  NAME_LISTS = %w[subtypes not_subtypes supertypes not_supertypes attached_subtypes].freeze
101
115
  KEYWORD_LISTS = %w[keywords not_keywords].freeze
102
- KEYS = (%w[controller counters colors] + BOOLEANS + RANGES + TYPE_LISTS + NAME_LISTS + KEYWORD_LISTS).freeze
103
- # 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).
104
119
  CHOSEN = { "colors" => "chosen_color", "types" => "chosen_card_type", "not" => "chosen_card_type",
105
- "subtypes" => "chosen_type", "not_subtypes" => "chosen_type" }.freeze
120
+ "subtypes" => "chosen_type", "not_subtypes" => "chosen_type", "name" => "chosen_name" }.freeze
106
121
  # Keys that need current characteristics or combat/tapped status: not usable in the layer pass.
107
122
  LIVE = %w[power toughness attacking blocking tapped attached_subtypes transformed prepared saddled
108
- entered_this_turn].freeze
123
+ entered_this_turn single_target].freeze
109
124
 
110
125
  attr_reader :base
111
126
 
112
127
  def self.build(base, params, errors, path)
113
128
  controller = params["controller"]
114
129
  errors << "#{path}.controller: must be you or opponent" if controller && !%w[you opponent].include?(controller)
130
+ errors << "#{path}.owner: must be you or opponent" unless [nil, "you", "opponent"].include?(params["owner"])
115
131
  validate_lists(params, errors, path)
116
132
  BOOLEANS.each do |key|
117
133
  errors << "#{path}.#{key}: must be true or false" unless params[key].nil? || [true, false].include?(params[key])
@@ -126,6 +142,7 @@ module Enjine
126
142
  (colors.is_a?(Array) && !colors.empty? && colors.all? { Mana::COLORS.include?(it.to_s.to_sym) })
127
143
  errors << "#{path}.colors: must be a non-empty list of W, U, B, R, G (any of them) or \"chosen\""
128
144
  end
145
+ errors << "#{path}.name: must be \"chosen\"" unless [nil, "chosen"].include?(params["name"])
129
146
  new(base, params, ranges)
130
147
  end
131
148
 
@@ -195,6 +212,8 @@ module Enjine
195
212
 
196
213
  def primitive_name = base.primitive_name
197
214
  def uses_chosen? = !@chosen.empty?
215
+ # Who must own it (CR 108.3); the layer pass checks it too (Abilities::Static#owner_ok?).
216
+ def owner = @params["owner"]
198
217
 
199
218
  # A computed bound depends on the game, so (like power) it isn't usable in the layer pass.
200
219
  def live_keys = LIVE.select { @params.key?(it) } + (@computed ? ["computed_range"] : [])
@@ -205,13 +224,13 @@ module Enjine
205
224
  # left the battlefield counts as it last existed, CR 608.2h).
206
225
  def candidates_for(engine, controller, source, ctx: nil)
207
226
  bounds = resolved_ranges(engine, controller, source, ctx)
208
- 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) }
209
228
  end
210
229
 
211
230
  def match?(engine, ref, controller) = match_for?(engine, ref, controller, nil)
212
231
 
213
232
  def match_for?(engine, ref, controller, source, ctx: nil)
214
- base.match?(engine, ref, controller) &&
233
+ base.match_for?(engine, ref, controller, source, ctx: ctx) &&
215
234
  allowed?(engine, ref, controller, source, resolved_ranges(engine, controller, source, ctx), ctx)
216
235
  end
217
236
 
@@ -234,10 +253,10 @@ module Enjine
234
253
  words.concat(@lists["types"] + @lists["subtypes"]).concat(@lists["not_subtypes"].map { "non-#{it}" })
235
254
  words.concat(chosen_words)
236
255
  words << (@lists["any_of"].empty? ? base.primitive_name.tr("_", " ") : @lists["any_of"].join(" or "))
237
- words << { "you" => "you control", "opponent" => "an opponent controls" }[@controller]
256
+ words.concat(ownership_words)
238
257
  words.concat(@ranges.map { |key, range| "with #{key.tr("_", " ")} #{range_words(range)}" })
239
258
  words << "with a #{@lists["attached_subtypes"].join(" or ")} attached" unless @lists["attached_subtypes"].empty?
240
- (words + [counter_words] + keyword_words).compact.join(" ")
259
+ (words + [counter_words, ("with a single target" if @params["single_target"])] + keyword_words).compact.join(" ")
241
260
  end
242
261
 
243
262
  RANGE_WORDS = { "at_least" => "%s or greater", "at_most" => "%s or less", "equal" => "equal to %s" }.freeze
@@ -252,6 +271,11 @@ module Enjine
252
271
  words + { true => ["that entered this turn"], false => ["that didn't enter this turn"] }.fetch(@params["entered_this_turn"], [])
253
272
  end
254
273
 
274
+ def ownership_words
275
+ [{ "you" => "you control", "opponent" => "an opponent controls" }[@controller],
276
+ { "you" => "you own", "opponent" => "an opponent owns" }[@params["owner"]]]
277
+ end
278
+
255
279
  def range_words(range) = range.map { |k, n| format(RANGE_WORDS.fetch(k), n) }.join(" and ")
256
280
 
257
281
  def chosen_words = @chosen.keys.map { "#{"non-" if it.start_with?("not")}chosen-#{it.delete_prefix("not_")}" }
@@ -285,6 +309,7 @@ module Enjine
285
309
  owner = index || engine.state.objects[ref]&.controller
286
310
  return false if @controller && (@controller == "you") != (owner == controller)
287
311
  return true if index
312
+ return false if @params["owner"] && (@params["owner"] == "you") != (engine.object(ref).owner == controller) # CR 108.3
288
313
 
289
314
  object_allowed?(engine, ref, bounds, uses_chosen? ? Filters.chosen_memory(engine, source, ctx) : nil)
290
315
  end
@@ -297,7 +322,7 @@ module Enjine
297
322
  return false if @params["modified"] == true && !modified?(engine, obj)
298
323
  return false unless attached_ok?(engine, obj)
299
324
 
300
- bounds.all? { |key, range| in_range?(stat(engine, obj, current, key), range) }
325
+ bounds.all? { |key, range| in_range?(stat(engine, obj, key), range) }
301
326
  end
302
327
 
303
328
  # Types, subtypes, supertypes, keywords, the chosen values, token-ness, counters and printed mana
@@ -329,6 +354,7 @@ module Enjine
329
354
  have = case key
330
355
  when "colors" then chars.colors
331
356
  when "types", "not" then chars.types
357
+ when "name" then [chars.name.to_sym]
332
358
  else chars.subtypes
333
359
  end
334
360
  hit = !value.nil? && have.include?(value.to_sym)
@@ -351,6 +377,7 @@ module Enjine
351
377
  return false if !@params["prepared"].nil? && engine.prepared?(obj) != @params["prepared"] # CR 723.3a
352
378
  return false if !@params["saddled"].nil? && engine.saddled?(obj) != @params["saddled"] # CR 702.171b
353
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"]
354
381
 
355
382
  @params["tapped"].nil? || obj.tapped == @params["tapped"]
356
383
  end
@@ -367,12 +394,12 @@ module Enjine
367
394
  Counters.any?(obj.counters) || engine.modifying_attachment?(obj)
368
395
  end
369
396
 
370
- # Current power/toughness (CR 208.3) and mana value (CR 202.3; 0 without a mana cost, 202.3a).
371
- 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)
372
399
  case key
373
400
  when "power" then engine.power(obj)
374
401
  when "toughness" then engine.toughness(obj)
375
- else current.mana_cost&.mana_value.to_i
402
+ else Amount.mana_value(engine, obj)
376
403
  end
377
404
  end
378
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
- source &&= Mechanics.build_entry(:filter, source, errors, "#{path}.source")
41
- Narrowed.new(kind, controller, source)
42
+ source &&= Mechanics.characteristic_filter(source, errors, "#{path}.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