enjine 0.6.4 → 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.
@@ -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
@@ -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
@@ -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
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enjine
4
- VERSION = "0.6.4"
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.4
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