enjine 0.6.3 → 0.6.5

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 (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +67 -1
  3. data/SUPPORTED.md +10 -9
  4. data/docs/guides/api.md +3 -0
  5. data/docs/guides/card-data-format.md +41 -2
  6. data/docs/plans/dsl-gaps.md +12 -12
  7. data/docs/reference/abilities.md +25 -11
  8. data/docs/reference/decisions.md +1 -1
  9. data/docs/reference/effects.md +15 -11
  10. data/lib/enjine/actions/activate_mana_ability.rb +3 -2
  11. data/lib/enjine/cli/text.rb +2 -1
  12. data/lib/enjine/decisions/casting.rb +1 -1
  13. data/lib/enjine/engine/casting/casting.rb +1 -0
  14. data/lib/enjine/engine/casting/payment.rb +20 -13
  15. data/lib/enjine/engine/casting/permissions.rb +11 -9
  16. data/lib/enjine/engine/copies.rb +8 -0
  17. data/lib/enjine/engine/damage.rb +1 -0
  18. data/lib/enjine/engine/mana_helpers.rb +75 -0
  19. data/lib/enjine/engine/mana_sources.rb +19 -8
  20. data/lib/enjine/engine/options_memo.rb +15 -2
  21. data/lib/enjine/engine/turn_history.rb +8 -0
  22. data/lib/enjine/engine/zones.rb +10 -0
  23. data/lib/enjine/mechanics/abilities/cost_changes.rb +62 -17
  24. data/lib/enjine/mechanics/abilities/entering_copy.rb +45 -16
  25. data/lib/enjine/mechanics/abilities/mana_ability.rb +54 -9
  26. data/lib/enjine/mechanics/abilities/play_permissions.rb +28 -9
  27. data/lib/enjine/mechanics/abilities/replacement_effects.rb +40 -11
  28. data/lib/enjine/mechanics/card_spec.rb +28 -2
  29. data/lib/enjine/mechanics/effects/choose_cards.rb +30 -15
  30. data/lib/enjine/mechanics/effects/counter_spell.rb +12 -5
  31. data/lib/enjine/mechanics/effects/damage.rb +24 -4
  32. data/lib/enjine/mechanics/effects/delayed_triggers.rb +3 -2
  33. data/lib/enjine/mechanics/effects/draw.rb +1 -1
  34. data/lib/enjine/mechanics/effects/loot.rb +1 -1
  35. data/lib/enjine/mechanics/effects/mana.rb +4 -1
  36. data/lib/enjine/mechanics/effects/pay.rb +14 -8
  37. data/lib/enjine/mechanics/keywords/alternative_costs.rb +3 -2
  38. data/lib/enjine/mechanics/keywords/bargain.rb +39 -0
  39. data/lib/enjine/mechanics/selector.rb +29 -8
  40. data/lib/enjine/replacements.rb +23 -5
  41. data/lib/enjine/steps.rb +1 -1
  42. data/lib/enjine/version.rb +1 -1
  43. metadata +4 -2
@@ -11,7 +11,7 @@ module Enjine
11
11
  include NestedList
12
12
 
13
13
  PUT = %w[hand graveyard exile battlefield library_top library_bottom discard].freeze
14
- ZONES = %w[hand graveyard].freeze
14
+ ZONES = %w[hand graveyard library].freeze
15
15
  REVEAL = %w[chosen all].freeze
16
16
  LISTS = %w[then otherwise].freeze
17
17
 
@@ -19,7 +19,9 @@ module Enjine
19
19
  description: "You choose `count` (default 1; \"any\": any number; \"all\": every matching card, no " \
20
20
  "choice) cards matching `for` from `from`: a " \
21
21
  "selector (e.g. {\"result\": 0}, the cards an earlier effect milled) or a player's zone, " \
22
- "{\"hand\": selector} / {\"graveyard\": selector}. `optional`: you may choose fewer. Then " \
22
+ "{\"hand\": selector} / {\"graveyard\": selector} / {\"library\": selector}, or several of them " \
23
+ "(\"search its owner's graveyard, hand, and library\": one choice among them all). `optional`: you " \
24
+ "may choose fewer. Then " \
23
25
  "they are put into `put` (`discard`: from a hand, its owner discards them; `tapped` onto the " \
24
26
  "battlefield, under your control; exiled cards `link`ed to the source). `look`: you look at the " \
25
27
  "whole zone first; `reveal`: \"chosen\" reveals what you chose, \"all\" the whole set. " \
@@ -28,9 +30,10 @@ module Enjine
28
30
  "`chooser`: the player who chooses (a player selector, default \"you\"; e.g. " \
29
31
  "\"each_opponent\" or {\"target\": 0}: \"target opponent chooses a card from among " \
30
32
  "them\"); they look at the cards they choose from, and the cards still go where `put` " \
31
- "says under your control.",
32
- rules: %w[701.20a 701.20e 400.7j 608.2c 607.2a 118.12 701.9a 701.9b 608.2d],
33
- params: { "from" => "selector or {\"hand\"|\"graveyard\": player selector}", "for" => "card spec",
33
+ "says under your control. Its amount ({\"this_way\": n}) is how many of the cards came from a " \
34
+ "hand (\"draws a card for each card exiled from their hand this way\").",
35
+ rules: %w[701.20a 701.20e 400.7j 608.2c 607.2a 118.12 701.9a 701.9b 608.2d 701.23a 701.23b],
36
+ params: { "from" => "selector or {\"hand\"|\"graveyard\"|\"library\": player selector, ...}", "for" => "card spec",
34
37
  "count" => "integer, \"any\" or \"all\"", "optional" => "boolean", "put" => PUT.join(" | "),
35
38
  "tapped" => "boolean", "link" => "boolean", "look" => "boolean", "random" => "boolean",
36
39
  "reveal" => REVEAL.join(" | "), "then" => "list of effects", "otherwise" => "list of effects",
@@ -44,7 +47,8 @@ module Enjine
44
47
  def self.default_to = "you"
45
48
 
46
49
  def setup(params, errors, path)
47
- @from_zone, @from = parse_from(Mechanics.fetch_param(params, :from), errors, path)
50
+ @from_zones = parse_from(Mechanics.fetch_param(params, :from), errors, path)
51
+ @from_zone = @from_zones.keys.first if @from_zones.size == 1
48
52
  @spec = CardSpec.build(Mechanics.fetch_param(params, :for), errors, "#{path}.for")
49
53
  @count = Mechanics.fetch_param(params, :count) || 1
50
54
  unless %w[any all].include?(@count) || (@count.is_a?(Integer) && @count.positive?)
@@ -65,23 +69,30 @@ module Enjine
65
69
  results_count: @results_count)
66
70
  end
67
71
 
72
+ # {zone => player selector} for one or more players' zones (several are searched as one set, CR 701.23a), or
73
+ # {nil => selector} for objects.
68
74
  def parse_from(raw, errors, path)
69
- zone = raw.is_a?(Hash) && raw.size == 1 && ZONES.include?(raw.keys.first.to_s) ? raw.keys.first.to_s : nil
70
- inner = zone ? raw.values.first : raw
71
- return [zone, nil] if inner.nil? && (errors << "#{path}.from: required")
75
+ return { nil => nil } if raw.nil? && (errors << "#{path}.from: required")
76
+ return { nil => build_from(raw, errors, "#{path}.from") } unless raw.is_a?(Hash) && !raw.empty? &&
77
+ raw.keys.all? { ZONES.include?(it.to_s) }
72
78
 
73
- [zone, Selector.build(inner, errors, "#{path}.from#{".#{zone}" if zone}", targets_count: @targets_count,
74
- results_count: @results_count)]
79
+ raw.to_h { |zone, inner| [zone.to_s, build_from(inner, errors, "#{path}.from.#{zone}")] }
80
+ end
81
+
82
+ def build_from(raw, errors, path)
83
+ Selector.build(raw, errors, path, targets_count: @targets_count, results_count: @results_count)
75
84
  end
76
85
 
77
86
  def nested_effects = @lists.values.flatten
78
87
 
79
88
  def to_s
80
89
  many = { "any" => "any number of", "all" => "all" }.fetch(count) { "#{"up to " if @optional}#{count}" }
81
- "choose #{many} #{spec}#{" at random" if @flags["random"]} from #{"the #{@from_zone} of " if @from_zone}#{@from} " \
90
+ "choose #{many} #{spec}#{" at random" if @flags["random"]} from #{from_text} " \
82
91
  "→ #{put.tr("_", " ")}#{" (#{@chooser} chooses)" if @chooser}"
83
92
  end
84
93
 
94
+ def from_text = @from_zones.map { |zone, from| "#{"the #{zone} of " if zone}#{from}" }.join(", ")
95
+
85
96
  # CR 118.3: as the optional part of a "may", the full number of cards must be available.
86
97
  def possible?(engine, ctx) = pool(engine, ctx).size >= (count.is_a?(Integer) ? count : 1)
87
98
 
@@ -128,10 +139,12 @@ module Enjine
128
139
  end
129
140
 
130
141
  def set(engine, ctx)
131
- refs = @from.resolve(engine, ctx)
132
- return refs.select { engine.state.objects.key?(it) } unless @from_zone
142
+ @from_zones.flat_map do |zone, from|
143
+ refs = Array(from&.resolve(engine, ctx))
144
+ next refs.select { engine.state.objects.key?(it) } unless zone
133
145
 
134
- refs.filter_map { Filters.player_index(engine, it) }.flat_map { engine.player(it).public_send(@from_zone) }
146
+ refs.filter_map { Filters.player_index(engine, it) }.flat_map { engine.player(it).public_send(zone) }
147
+ end.uniq
135
148
  end
136
149
 
137
150
  # Looking at or revealing the whole set happens once, before the choice (CR 701.20a, 701.20e).
@@ -172,6 +185,8 @@ module Enjine
172
185
 
173
186
  def perform!(engine, ctx, chosen, locals)
174
187
  engine.reveal!(ctx.controller, chosen) if @reveal == "chosen"
188
+ from_hand = chosen.count { engine.state.objects[it]&.zone == :hand }
189
+ engine.record_amount!(from_hand) if from_hand.positive? # "for each card exiled from their hand this way"
175
190
  return enter!(engine, ctx, chosen, locals) if put == "battlefield"
176
191
 
177
192
  chosen.filter_map { move(engine, ctx, it) }
@@ -6,14 +6,21 @@ module Enjine
6
6
  # Counter a spell or ability (CR 701.6).
7
7
  class CounterSpell < Effect
8
8
  primitive :effect, :counter, description: "Counter each referenced spell or ability: a spell leaves the " \
9
- "stack for its owner's graveyard without resolving, an ability " \
10
- "ceases to exist. A spell that can't be countered stays.",
11
- rules: %w[701.6a 701.6b 101.2], params: { "to" => "selector" },
9
+ "stack for its owner's graveyard (for exile with `exile_instead: " \
10
+ "true`) without resolving, an ability ceases to exist. A spell " \
11
+ "that can't be countered stays.",
12
+ rules: %w[701.6a 701.6b 101.2 614.1a],
13
+ params: { "to" => "selector", "exile_instead" => "boolean: \"if that spell is " \
14
+ "countered this way, exile it instead\"" },
12
15
  example: { "counter" => { "to" => { "target" => 0 } } }
13
- def self.keys = %w[to if]
16
+ def self.keys = %w[to exile_instead if]
17
+
18
+ attr_reader :exile_instead
19
+
20
+ def setup(params, errors, path) = @exile_instead = Mechanics.flag_param(params, :exile_instead, errors, path)
14
21
 
15
22
  def apply(engine, ctx)
16
- refs(engine, ctx).each { engine.counter!(it) if engine.state.stack.include?(it) }
23
+ refs(engine, ctx).each { engine.counter!(it, exile: exile_instead) if engine.state.stack.include?(it) }
17
24
  end
18
25
  end
19
26
  end
@@ -9,11 +9,20 @@ module Enjine
9
9
  description: "Deal `amount` damage to each referenced creature, planeswalker or player. Damage to a " \
10
10
  "player causes life loss; to a planeswalker removes loyalty counters; to a creature " \
11
11
  "is marked until cleanup. {\"this_way\": n} naming it is the excess damage it dealt " \
12
- "(beyond lethal damage, or a planeswalker's loyalty).",
13
- rules: %w[120.3a 120.3c 120.3e 120.6 120.4a 120.10],
14
- params: { "amount" => "integer", "to" => "selector" },
12
+ "(beyond lethal damage, or a planeswalker's loyalty). `exile_if_dies: true`: a permanent " \
13
+ "dealt damage this way that would die this turn is exiled instead (not one whose damage " \
14
+ "was prevented).",
15
+ rules: %w[120.3a 120.3c 120.3e 120.6 120.4a 120.10 614.1a 700.4],
16
+ params: { "amount" => "integer", "to" => "selector",
17
+ "exile_if_dies" => "true: \"if a permanent dealt damage this way would die this turn, exile it instead\"" },
15
18
  example: { "deal_damage" => { "amount" => 3, "to" => { "target" => 0 } } }
16
19
 
20
+ def self.keys = %w[amount to if exile_if_dies]
21
+
22
+ def setup(params, errors, path)
23
+ @exile_if_dies = Mechanics.flag_param(params, :exile_if_dies, errors, path)
24
+ end
25
+
17
26
  # The excess damage it deals (beyond lethal damage to a creature, or a planeswalker's loyalty) is
18
27
  # what {"this_way": n} reads ("if excess damage was dealt this way", CR 120.4a, 120.10).
19
28
  def damage_hits(engine, ctx)
@@ -24,11 +33,22 @@ module Enjine
24
33
  def apply(engine, ctx)
25
34
  n = amount_for(engine, ctx)
26
35
  source = ctx.source || ctx.object
36
+ hit = []
27
37
  refs(engine, ctx).each do |ref|
28
38
  lethal = engine.lethal_damage(source, ref)
29
- engine.record_amount!(engine.excess_damage(lethal, engine.deal_damage!(source, ref, n)))
39
+ engine.record_amount!(engine.excess_damage(lethal, engine.deal_damage!(source, ref, n) { hit << it }))
30
40
  end
41
+ exile_if_dies!(engine, ctx, hit) if @exile_if_dies
31
42
  end
43
+
44
+ # "If a permanent dealt damage this way would die this turn, exile it instead" (CR 614.1a, 700.4): the
45
+ # permanents actually dealt damage (after prevention and redirection, CR 615, 614.9), until end of turn.
46
+ def exile_if_dies!(engine, ctx, hit)
47
+ permanents = hit.uniq.select { engine.state.battlefield.include?(it) }
48
+ engine.add_continuous_effect!("exile_if_dies", ctx, permanents, {}) unless permanents.empty?
49
+ end
50
+
51
+ def to_s = "#{super}#{"; if a permanent dealt damage this way would die this turn, exile it instead" if @exile_if_dies}"
32
52
  end
33
53
 
34
54
  # "Deals 3 damage divided as you choose among one, two, or three targets" (CR 601.2d): the division was
@@ -110,9 +110,10 @@ module Enjine
110
110
  def to_s = "reflexive: #{delayed_ability}"
111
111
 
112
112
  def apply(engine, ctx)
113
- # Created by a replacement's follow-up (Engine::FollowUps), its spell may be long gone: its card is remembered.
113
+ # Created by a replacement's follow-up (Engine::FollowUps), its spell may be long gone: its card is remembered,
114
+ # and is the card the ability is on (a permanent that entered as a copy has another card, Engine::Copies).
114
115
  memory = Selector.memory(engine, ctx)
115
- card = engine.find_object(ctx.object)&.card || memory["source_card"]
116
+ card = memory["source_card"] || engine.find_object(ctx.object)&.card
116
117
  source = { "id" => ctx.source, "card" => card, "controller" => ctx.controller, "ability" => ref }
117
118
  results = Snapshot.deep_copy(DelayedTrigger.linked(ctx.results, @results_count, []))
118
119
  engine.queue_pending_trigger!(source, nil, { "results" => results,
@@ -9,7 +9,7 @@ module Enjine
9
9
  rules: %w[121.1 121.2], params: { "amount" => "integer", "to" => "selector" },
10
10
  example: { "draw" => { "amount" => 2 } }
11
11
  def self.default_to = "you"
12
- def apply(engine, ctx) = players(engine, ctx).each { |i| amount_for(engine, ctx).times { engine.draw!(i) } }
12
+ def apply(engine, ctx) = players(engine, ctx).each { |i| engine.draw_cards!(i, amount_for(engine, ctx)) }
13
13
  end
14
14
  end
15
15
  end
@@ -27,7 +27,7 @@ module Enjine
27
27
  def loot(engine, locals, entry)
28
28
  index = entry["player"]
29
29
  unless locals["drawn"]
30
- entry["n"].times { engine.draw!(index) }
30
+ engine.draw_cards!(index, entry["n"])
31
31
  locals.merge!("drawn" => true, "nonland" => 0, "applied" => 0)
32
32
  end
33
33
  chosen = answers(locals)
@@ -87,7 +87,10 @@ module Enjine
87
87
  private
88
88
 
89
89
  def add(engine, index, produce, ctx)
90
- engine.add_mana!(index, produce, ctx.source, restriction: @spend_only&.data, keep_until: @until) unless produce.nil?
90
+ unless produce.nil?
91
+ restriction = @spend_only&.bound(engine, ctx.source)&.data # "of the chosen type" (CR 607.2d)
92
+ engine.add_mana!(index, produce, ctx.source, restriction: restriction, keep_until: @until)
93
+ end
91
94
  :done
92
95
  end
93
96
  end
@@ -209,20 +209,26 @@ module Enjine
209
209
  primitive :effect, :counter_unless_pays,
210
210
  description: "Counter each referenced spell or ability unless its controller pays `mana` (or " \
211
211
  "`waterbend`s that cost, CR 701.67a, or pays the list of costs `cost`) as this " \
212
- "resolves (they choose, and pay like any cost; mana abilities allowed).",
213
- rules: %w[701.6a 118.12a 605.3a 701.67a],
212
+ "resolves (they choose, and pay like any cost; mana abilities allowed). With " \
213
+ "`exile_instead: true`, a spell countered this way is exiled instead of being put " \
214
+ "into its owner's graveyard; if they pay, nothing happens to it.",
215
+ rules: %w[701.6a 118.12a 605.3a 701.67a 608.2b 614.1a],
214
216
  params: { "mana" => "mana cost string", "waterbend" => "mana cost string, instead of mana",
215
- "cost" => "list of costs, instead of mana", "to" => "selector (usually {\"target\": 0})" },
217
+ "cost" => "list of costs, instead of mana", "to" => "selector (usually {\"target\": 0})",
218
+ "exile_instead" => "boolean: \"if that spell is countered this way, exile it instead\"" },
216
219
  example: { "counter_unless_pays" => { "mana" => "{3}", "to" => { "target" => 0 } } }
217
220
 
218
- def self.keys = %w[mana waterbend cost to if]
221
+ def self.keys = %w[mana waterbend cost to exile_instead if]
219
222
 
220
- attr_reader :costs
223
+ attr_reader :costs, :exile_instead
221
224
 
222
- def setup(params, errors, path) = @costs = parse_payment(params, errors, path)
225
+ def setup(params, errors, path)
226
+ @costs = parse_payment(params, errors, path)
227
+ @exile_instead = Mechanics.flag_param(params, :exile_instead, errors, path)
228
+ end
223
229
 
224
230
  def what = costs_text(costs)
225
- def to_s = "counter #{to} unless its controller pays #{what}"
231
+ def to_s = "counter #{to} unless its controller pays #{what}#{"; if countered this way, exile it instead" if exile_instead}"
226
232
 
227
233
  def run(engine, ctx, locals)
228
234
  return :done unless locals["target"] || condition.nil? || condition.true?(engine, ctx)
@@ -237,7 +243,7 @@ module Enjine
237
243
  paid = answers(locals).first["type"] == "pay" && pay_costs(engine, ctx, locals, payer, costs_now(engine, ctx))
238
244
  return :wait if paid == :wait
239
245
 
240
- engine.counter!(target) unless paid == "paid"
246
+ engine.counter!(target, exile: exile_instead) unless paid == "paid"
241
247
  :done
242
248
  end
243
249
 
@@ -337,9 +337,10 @@ module Enjine
337
337
  KEYWORDS = %w[flashback harmonize mayhem warp evoke web_slinging sneak foretell impending miracle].freeze
338
338
  # Optional additional costs announced as the spell is cast (Engine::CastChoices#announce_kicker!):
339
339
  # "if this spell was cast using teamwork" (CR 702.194b), "if this spell's additional cost was paid" (CR 118.8b),
340
- # "if the gift was promised" (CR 702.174k).
340
+ # "if the gift was promised" (CR 702.174k), "if this spell was bargained" (CR 702.166b).
341
341
  OPTIONAL_TEXT = { "teamwork" => "it was cast using teamwork", "additional_cost" => "its additional cost was paid",
342
- "gift" => "its gift was promised", "offspring" => "its offspring cost was paid" }.freeze
342
+ "gift" => "its gift was promised", "offspring" => "its offspring cost was paid",
343
+ "bargain" => "it was bargained" }.freeze # CR 702.166b
343
344
  OPTIONAL = OPTIONAL_TEXT.keys.freeze
344
345
  ZONES = %w[hand graveyard exile library outside].freeze
345
346
 
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "kicker"
4
+
5
+ module Enjine
6
+ module Mechanics
7
+ module Abilities
8
+ # Bargain (CR 702.166a-c): "As an additional cost to cast this spell, you may sacrifice an artifact, enchantment,
9
+ # or token." It is announced like kicker (the `choose_kicker` decision) without making the spell kicked, then the
10
+ # permanent is chosen (`choose_cost`) and sacrificed as the spell becomes cast (CR 601.2h). A spell is bargained
11
+ # if that cost was paid (CR 702.166b): `{"cast_with": "bargain"}` holds then, for the abilities that say what
12
+ # happens if it was bargained (CR 702.166c).
13
+ class Bargain < SpellCost
14
+ primitive :ability, :bargain,
15
+ description: "Bargain: as you cast this spell you may sacrifice an artifact, enchantment or token " \
16
+ "(announced with the `choose_kicker` decision, the permanent chosen with `choose_cost`; " \
17
+ "the spell isn't kicked). `{\"cast_with\": \"bargain\"}` holds if it was bargained.",
18
+ rules: %w[702.166a 702.166b 702.166c 601.2b 601.2h 701.21a],
19
+ example: "bargain"
20
+
21
+ COST = [{ "sacrifice_permanent" => { "filter" => { "one_of" => [
22
+ { "permanent" => { "any_of" => %w[artifact enchantment] } }, { "permanent" => { "token" => true } }
23
+ ] } } }].freeze
24
+
25
+ def self.build(params, errors, path, **)
26
+ Mechanics.check_keys(params, [], errors, path)
27
+ new(super({ "cost" => COST }, errors, path).costs)
28
+ end
29
+
30
+ def initialize(costs)
31
+ super(costs, optional: true)
32
+ end
33
+
34
+ def kind = :kicker
35
+ def to_s = "Bargain"
36
+ end
37
+ end
38
+ end
39
+ end
@@ -23,6 +23,8 @@ module Enjine
23
23
  # "each_opponent" every opponent
24
24
  # "attached" what the source is attached to ("enchanted creature", CR 303.4m)
25
25
  # {"each": "creature", "controller": "you"} every matching object (you | opponent | any)
26
+ # {"each": "creature", "controller": {"target": 0}} every matching object the targeted player controls ("each
27
+ # creature target player controls", CR 608.2b)
26
28
  # {"result": 0} what effect 0 of this list moved or created (CR 400.7j)
27
29
  # {"result": 0, "for": card spec} only those that match ("if the exiled card is a land card")
28
30
  # {"controller_of": selector} the controller of each referenced object ("its controller");
@@ -102,17 +104,27 @@ module Enjine
102
104
  Mechanics.check_keys(data, %w[target], errors, path)
103
105
  new(:target, index)
104
106
  elsif data.key?("each")
105
- Mechanics.check_keys(data, %w[each controller], errors, path)
106
- filter = Mechanics.build_entry(:filter, data["each"], errors, "#{path}.each")
107
- controller = data.fetch("controller", "any")
108
- errors << "#{path}.controller: must be one of #{CONTROLLERS.join(", ")}" unless CONTROLLERS.include?(controller)
109
- new(:each, filter, controller)
107
+ build_each(data, errors, path, targets_count)
110
108
  else
111
109
  errors << "#{path}: needs \"target\" or \"each\""
112
110
  nil
113
111
  end
114
112
  end
115
113
 
114
+ # `controller`: you | opponent | any, or {"target": n}, a targeted player ("each creature target player
115
+ # controls").
116
+ def self.build_each(data, errors, path, targets_count)
117
+ Mechanics.check_keys(data, %w[each controller], errors, path)
118
+ filter = Mechanics.build_entry(:filter, data["each"], errors, "#{path}.each")
119
+ controller = data.fetch("controller", "any")
120
+ if controller.is_a?(Hash) && controller.transform_keys(&:to_s).keys == ["target"]
121
+ return new(:each, filter, build_hash(controller.transform_keys(&:to_s), errors, "#{path}.controller", targets_count))
122
+ end
123
+
124
+ errors << "#{path}.controller: must be one of #{CONTROLLERS.join(", ")} or {\"target\": n}" unless CONTROLLERS.include?(controller)
125
+ new(:each, filter, controller)
126
+ end
127
+
116
128
  def initialize(kind, value = nil, controller = nil, spec: nil)
117
129
  @kind = kind
118
130
  @value = value
@@ -136,9 +148,7 @@ module Enjine
136
148
  when :exiled_for_cost then "the cards exiled to pay its cost"
137
149
  when :gift_recipient then "the chosen opponent"
138
150
  when :granter then "the permanent granting it"
139
- when :each then "each #{value.primitive_name.tr("_",
140
- " ")}#{{ "you" => " you control",
141
- "opponent" => " an opponent controls" }[controller]}"
151
+ when :each then "each #{value.primitive_name.tr("_", " ")}#{controlled_by_text}"
142
152
  end
143
153
  end
144
154
 
@@ -239,10 +249,21 @@ module Enjine
239
249
  to && engine.state.battlefield.include?(to) ? [to] : []
240
250
  end
241
251
 
252
+ def controlled_by_text
253
+ return " #{controller} controls" if controller.is_a?(Selector)
254
+
255
+ { "you" => " you control", "opponent" => " an opponent controls" }[controller]
256
+ end
257
+
258
+ # With a target player as the controller, what that player controls; nothing once it's an illegal target
259
+ # (CR 608.2b).
242
260
  def each(engine, ctx)
261
+ players = controller.resolve(engine, ctx).filter_map { Filters.player_index(engine, it) } if controller.is_a?(Selector)
243
262
  value.candidates_for(engine, ctx.controller, ctx.source, ctx: ctx).select do |ref|
244
263
  index = Filters.player_index(engine, ref)
245
264
  owner = index || engine.object(ref).controller
265
+ next players.include?(owner) if players
266
+
246
267
  controller == "any" || (controller == "you") == (owner == ctx.controller)
247
268
  end
248
269
  end
@@ -5,7 +5,7 @@ module Enjine
5
5
  # through replacement effects, then executed, so "enters tapped", "if it would die,
6
6
  # exile it instead" and damage prevention can modify them before they occur.
7
7
  #
8
- # kind: "zone_change" | "damage" | "life" | "draw" | "counters" | "untap" | "create_token"
8
+ # kind: "zone_change" | "damage" | "life" | "draw" | "draw_cards" | "counters" | "untap" | "create_token"
9
9
  # data: plain Hash describing the change; a replacement that stops the change entirely
10
10
  # ("instead remove a stun counter") sets data["cancelled"] = true
11
11
  # applied: ids of replacement effects already applied (each applies once, CR 614.5)
@@ -35,6 +35,7 @@ module Enjine
35
35
  when "zone_change" then zone_change!(engine, proposal)
36
36
  when "counters" then putting_counters!(engine, proposal)
37
37
  when "draw" then drawing!(engine, proposal)
38
+ when "draw_cards" then drawing_cards!(engine, proposal)
38
39
  when "damage" then Mechanics::DamageEvents.replace!(engine, proposal) # abilities/damage_replacements.rb
39
40
  when "create_token" then creating_token!(engine, proposal)
40
41
  when "life" then gaining_life!(engine, proposal)
@@ -51,10 +52,11 @@ module Enjine
51
52
  end
52
53
 
53
54
  # The first applicable ability of a battlefield permanent controlled by `player` that responds to
54
- # `hook` and hasn't modified this event yet (CR 614.5). It is marked as applied.
55
+ # `hook` (and passes the block, if given) and hasn't modified this event yet (CR 614.5). It is marked as applied.
55
56
  def next_controlled(engine, proposal, hook, player)
56
- found = battlefield_abilities(engine, hook).find do |source, _|
57
- source.controller == player && !proposal.applied.include?("#{source.id}:#{hook}")
57
+ found = battlefield_abilities(engine, hook).find do |source, ability|
58
+ source.controller == player && !proposal.applied.include?("#{source.id}:#{hook}") &&
59
+ (!block_given? || yield(source, ability))
58
60
  end
59
61
  proposal.applied << "#{found[0].id}:#{hook}" if found
60
62
  found
@@ -63,10 +65,26 @@ module Enjine
63
65
  # "If you would draw a card, draw two cards instead" (CR 614.11). Only one applies to this event;
64
66
  # the others apply to each of the draws it becomes (Engine#draw!).
65
67
  def drawing!(engine, proposal)
66
- _, ability = next_controlled(engine, proposal, :replace_draw, proposal.data["player"])
68
+ player = proposal.data["player"]
69
+ _, ability = next_controlled(engine, proposal, :replace_draw, player) do |source, found|
70
+ found.amount && found.applies?(engine, player, source)
71
+ end
67
72
  proposal.data["count"] = ability.amount if ability
68
73
  end
69
74
 
75
+ # "If you would draw one or more cards, you draw that many cards plus one instead" (CR 614.11, 121.2): an
76
+ # instruction to draw several cards is one event for it, so each such effect applies once (CR 614.5), its
77
+ # condition checked before any card is drawn. Applied before the per-card "draw two instead" effects
78
+ # (Engine#draw_cards!), the order the drawing player would choose (CR 616.1), as with life and counters.
79
+ def drawing_cards!(engine, proposal)
80
+ data = proposal.data
81
+ battlefield_abilities(engine, :replace_draw).each do |source, ability|
82
+ next unless ability.plus && ability.applies?(engine, data["player"], source)
83
+
84
+ data["count"] += ability.extra(engine, source)
85
+ end
86
+ end
87
+
70
88
  # "If you would gain life, you gain twice that much / that much plus 1 instead" and "if an opponent would gain
71
89
  # life, that player loses that much life instead" (CR 614.1a, 119.10). Each applies once (CR 614.5), in the
72
90
  # order the affected player would always choose (CR 616.1): losing instead first (then nothing else applies,
data/lib/enjine/steps.rb CHANGED
@@ -38,7 +38,7 @@ module Enjine
38
38
  def skip?(engine) = engine.state.then { it.turn == 1 && it.active == it.starting_player }
39
39
 
40
40
  def begin(engine)
41
- engine.draw!(engine.state.active)
41
+ engine.draw_cards!(engine.state.active, 1)
42
42
  engine.turn_based_actions_done!
43
43
  end
44
44
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enjine
4
- VERSION = "0.6.3"
4
+ VERSION = "0.6.5"
5
5
  # Comprehensive Rules document this engine follows.
6
6
  RULES_VERSION = "2026-11-06"
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enjine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mehmet Cetin
@@ -111,6 +111,7 @@ files:
111
111
  - lib/enjine/engine/face_down_permanents.rb
112
112
  - lib/enjine/engine/faces.rb
113
113
  - lib/enjine/engine/follow_ups.rb
114
+ - lib/enjine/engine/mana_helpers.rb
114
115
  - lib/enjine/engine/mana_sources.rb
115
116
  - lib/enjine/engine/options_memo.rb
116
117
  - lib/enjine/engine/permanent_designations.rb
@@ -238,6 +239,7 @@ files:
238
239
  - lib/enjine/mechanics/filters/stack.rb
239
240
  - lib/enjine/mechanics/follow_up.rb
240
241
  - lib/enjine/mechanics/keywords/alternative_costs.rb
242
+ - lib/enjine/mechanics/keywords/bargain.rb
241
243
  - lib/enjine/mechanics/keywords/beam_me_up.rb
242
244
  - lib/enjine/mechanics/keywords/cards_have.rb
243
245
  - lib/enjine/mechanics/keywords/changeling.rb
@@ -350,7 +352,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
350
352
  - !ruby/object:Gem::Version
351
353
  version: '0'
352
354
  requirements: []
353
- rubygems_version: 4.0.3
355
+ rubygems_version: 4.0.20
354
356
  specification_version: 4
355
357
  summary: 'A deterministic, rules-accurate Magic: The Gathering engine for the Standard
356
358
  format.'