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
@@ -6,10 +6,11 @@ module Enjine
6
6
  # [quality] permanent you control. Whether a [quality] was beheld looks at the object as it was beheld
7
7
  # (CR 701.4b).
8
8
  module Beholding
9
- # The cards in `index`'s hand and the permanents they control matching the card spec.
10
- def self.candidates(engine, index, spec)
9
+ # The cards in `index`'s hand and the permanents they control matching the card spec (in `ctx`, e.g. "of the
10
+ # chosen type").
11
+ def self.candidates(engine, index, spec, ctx = nil)
11
12
  perms = engine.state.battlefield.select { engine.object(it).controller == index }
12
- (perms + engine.player(index).hand).select { spec.match?(engine, engine.object(it)) }
13
+ (perms + engine.player(index).hand).select { spec.match?(engine, engine.object(it), ctx) }
13
14
  end
14
15
 
15
16
  # Reveals a card from the hand (CR 701.4a); the entry remembers what was beheld (CR 701.4b).
@@ -53,8 +54,23 @@ module Enjine
53
54
  super
54
55
  end
55
56
 
56
- # Not the card being cast (it is on the stack once casting begins).
57
- def candidates(engine, object) = Beholding.candidates(engine, object.controller, filter) - [object.id]
57
+ # Not the card being cast (it is on the stack once casting begins). "Of the chosen type" reads the type chosen
58
+ # for it (Costs::ChooseCreatureType).
59
+ def candidates(engine, object)
60
+ ctx = EffectContext.new(object.controller, object.id, object.id, [])
61
+ Beholding.candidates(engine, object.controller, filter, ctx) - [object.id]
62
+ end
63
+
64
+ # Before a type is chosen for "behold two creatures of that type", whether some type would do (CR 601.2b).
65
+ def payable?(engine, object)
66
+ return super unless filter.relations["chosen_subtype"] && !object.memory["chosen_type"]
67
+
68
+ others = Beholding.candidates(engine, object.controller, filter.unrelated) - [object.id]
69
+ others.flat_map { engine.chars(it).subtypes.uniq }.tally.any? do |type, n|
70
+ n >= count && Keywords::Changeling::CREATURE_TYPES.include?(type)
71
+ end
72
+ end
73
+
58
74
  def choose!(engine, option) = Beholding.behold!(engine, engine.object(option[:object]).controller, option[:object])
59
75
 
60
76
  def pay_chosen!(engine, object, entries)
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enjine
4
+ module Mechanics
5
+ module Costs
6
+ # "Choose a creature type and behold two creatures of that type" (CR 601.2b, 205.3m, 607.2d): choosing the
7
+ # type is part of the cost. It is a `choose_cost` decision, `{type: "cost", creature_type: "elf"}`, made in
8
+ # cost order (so before a later `behold` of `{"subtypes": "chosen"}`), and the spell remembers the type at
9
+ # once ("chosen_type", read by "of the chosen type" filters and card specs). Cancelling the cast forgets it.
10
+ class ChooseCreatureType < Primitive
11
+ primitive :cost, :choose_creature_type,
12
+ description: "Choose a creature type as part of the cost (a `choose_cost` decision with options " \
13
+ "`{type: \"cost\", creature_type: t}`). The spell remembers it as its chosen type: later " \
14
+ "costs and its effects read it with `\"subtypes\": \"chosen\"`.",
15
+ rules: %w[601.2b 205.3m 607.2d],
16
+ example: { "choose_creature_type" => {} }
17
+
18
+ INSTANCE = new.freeze
19
+
20
+ def self.build(params, errors, path, **)
21
+ Mechanics.check_keys(params, [], errors, path)
22
+ INSTANCE
23
+ end
24
+
25
+ def mana? = false
26
+ def choices_needed = 1
27
+ def payable?(_engine, _object) = true
28
+
29
+ def choice_options(_engine, _object, _chosen) = Chosen.values("creature_type", []).map { { creature_type: it } }
30
+
31
+ # The spell being cast remembers the type now, so a later cost of "that type" can be chosen (CR 601.2b).
32
+ def choose!(engine, option)
33
+ type = option[:creature_type].to_s
34
+ spell = engine.casting && engine.state.objects[engine.casting["object"]]
35
+ spell&.memory&.store("chosen_type", type)
36
+ { "creature_type" => type }
37
+ end
38
+
39
+ def to_s = "Choose a creature type"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -38,6 +38,9 @@ module Enjine
38
38
 
39
39
  def mana? = false
40
40
 
41
+ # The options with their amounts computed in `ctx` (Costs::PayLife#fixed).
42
+ def fixed(engine, ctx) = OneOf.new(options.map { it.respond_to?(:fixed) ? it.fixed(engine, ctx) : it })
43
+
41
44
  # Some option could be paid (a mana option is checked against the total cost by the engine).
42
45
  def payable?(engine, object) = options.any? { it.mana? || it.payable?(engine, object) }
43
46
  # Each option as text: "Behold a Goblin", "Pay {2}".
@@ -6,13 +6,17 @@ module Enjine
6
6
  # "Pay 2 life" / "Pay X life" as a cost (CR 119.4, 118.3b): {"pay_life": 2} or {"pay_life": "X"}. A
7
7
  # player can pay life only if their life total is at least that much; 0 life can always be paid
8
8
  # (CR 119.4b). X is announced with the spell or ability (CR 601.2b, 107.3a), at most the life total.
9
+ # An amount ("Ward—Pay life equal to Raubahn's power": {"pay_life": {"power": "self"}}) is computed as
10
+ # the cost is paid: while a spell or ability resolves (`pay`, ward) in its context (#fixed), while
11
+ # casting or activating for the object it's paid for.
9
12
  class PayLife < Primitive
10
13
  primitive :cost, :pay_life,
11
14
  description: "Pay that much life (you lose it). Only possible while your life total is at least " \
12
15
  "that much. `\"X\"` pays the X announced for the spell or ability (at most your life " \
13
- "total); it may be a spell's additional cost.",
14
- rules: %w[119.4 119.4b 118.3b 601.2b 107.3a],
15
- params: { "(value)" => "integer >= 1, or \"X\"" },
16
+ "total); it may be a spell's additional cost. An amount (e.g. {\"power\": \"self\"}, " \
17
+ "\"pay life equal to its power\") is computed as the cost is paid.",
18
+ rules: %w[119.4 119.4b 118.3b 601.2b 107.3a 702.21a],
19
+ params: { "(value)" => "integer >= 1, \"X\", or an amount" },
16
20
  example: { "pay_life" => 2 }
17
21
 
18
22
  attr_reader :amount
@@ -20,7 +24,10 @@ module Enjine
20
24
  def self.scalar_params? = true
21
25
 
22
26
  def self.build(params, errors, path, **)
23
- errors << "#{path}: must be an integer >= 1 or \"X\"" unless params == "X" || (params.is_a?(Integer) && params >= 1)
27
+ return new(Amount.build(params, errors, path, targets_count: 0)) if params.is_a?(Hash)
28
+
29
+ ok = params == "X" || (params.is_a?(Integer) && params >= 1)
30
+ errors << "#{path}: must be an integer >= 1, \"X\" or an amount" unless ok
24
31
  new(params)
25
32
  end
26
33
 
@@ -33,17 +40,29 @@ module Enjine
33
40
  def mana? = false
34
41
  def spell_cost? = true
35
42
  def x? = @amount == "X"
43
+ def computed? = @amount.respond_to?(:value)
36
44
  def x_limit(engine, object) = [engine.player(object.controller).life, 0].max
37
45
 
46
+ # The cost with its amount computed in `ctx` (an EffectContext): "its power" as a ward trigger resolves.
47
+ def fixed(engine, ctx) = computed? ? PayLife.new([@amount.value(engine, ctx), 0].max) : self
48
+
38
49
  # X is bounded by the life total as it's announced (Engine::Costs#x_limit).
39
- def payable?(engine, object) = x? || engine.player(object.controller).life >= @amount.to_i
50
+ def payable?(engine, object) = x? || engine.player(object.controller).life >= life(engine, object)
40
51
 
41
52
  def pay!(engine, object)
42
- life = x? ? engine.casting&.fetch("x", 0).to_i : @amount.to_i
43
- engine.change_life!(object.controller, -life) if life.positive?
53
+ n = x? ? engine.casting&.fetch("x", 0).to_i : life(engine, object)
54
+ engine.change_life!(object.controller, -n) if n.positive?
44
55
  end
45
56
 
46
- def to_s = "Pay #{@amount} life"
57
+ def to_s = computed? ? "Pay life equal to #{@amount}" : "Pay #{@amount} life"
58
+
59
+ private
60
+
61
+ def life(engine, object)
62
+ return @amount.to_i unless computed?
63
+
64
+ [@amount.value(engine, EffectContext.new(object.controller, object.id, object.id, [])), 0].max
65
+ end
47
66
  end
48
67
  end
49
68
  end
@@ -9,24 +9,27 @@ module Enjine
9
9
  primitive :cost, :remove_counters,
10
10
  description: "Remove `count` counters (of kind `counter`, or any kind) from among permanents you " \
11
11
  "control matching `filter` (default creature), one counter per choice. Removed as " \
12
- "it becomes cast.",
12
+ "it becomes cast. `self: true`: only from this permanent (\"Remove two counters from " \
13
+ "Ghost-Spider\").",
13
14
  rules: %w[602.2b 601.2h 118.3 122.1],
14
- params: { "count" => "integer", "filter" => "filter (default creature)", "counter" => "counter name (optional)" },
15
+ params: { "count" => "integer", "filter" => "filter (default creature)", "counter" => "counter name (optional)",
16
+ "self" => "true: from this permanent only" },
15
17
  example: { "remove_counters" => { "count" => 3 } }
16
18
 
17
- def self.keys = %w[filter count counter]
19
+ def self.keys = %w[filter count counter self]
18
20
  def self.default_filter = "creature"
19
21
 
20
22
  def initialize(filter, count, other, params, errors, path)
21
23
  @counter = Mechanics.fetch_param(params, :counter)
22
24
  errors << "#{path}.counter: #{Counters.error(@counter)}" if @counter && Counters.error(@counter)
25
+ @self = Mechanics.flag_param(params, :self, errors, path)
23
26
  super
24
27
  end
25
28
 
26
29
  # Counters still available: [object, kind] once per counter, minus those already chosen.
27
30
  def units(engine, object, chosen)
28
31
  taken = chosen.map { [it["object"], it["counter"]] }
29
- matching(engine, object).flat_map do |id|
32
+ (@self ? [object.id] : matching(engine, object)).flat_map do |id|
30
33
  engine.object(id).counters.flat_map do |kind, n|
31
34
  next [] unless @counter.nil? || kind == @counter
32
35
 
@@ -44,7 +47,7 @@ module Enjine
44
47
  entries.each { engine.remove_counters!(engine.object(it["object"]), it["counter"], 1) }
45
48
  end
46
49
 
47
- def to_s = "Remove #{count} #{"#{@counter} " if @counter}counters from among #{what}s you control"
50
+ def to_s = "Remove #{count} #{"#{@counter} " if @counter}counters from #{@self ? "this" : "among #{what}s you control"}"
48
51
  end
49
52
  end
50
53
  end
@@ -31,14 +31,26 @@ module Enjine
31
31
  # {T}: tap this permanent (CR 107.5, 602.5a).
32
32
  class Tap < Primitive
33
33
  primitive :cost, :tap,
34
- description: "{T} — tap the permanent the ability belongs to. Requires it to be untapped.",
35
- rules: %w[107.5 602.5a],
34
+ description: "{T} — tap the permanent the ability belongs to. Requires it to be untapped. " \
35
+ "`{\"tap\": \"granter\"}` instead taps the permanent whose static ability granted this " \
36
+ "ability (\"Tap Fishing Pole\"; not {T}, so summoning sickness doesn't matter).",
37
+ rules: %w[107.5 602.5a 602.2b 607.1a 302.6],
38
+ params: { "(value)" => "\"granter\": tap the granting permanent instead" },
36
39
  example: "tap"
37
40
 
38
41
  INSTANCE = new.freeze
39
42
 
43
+ # Its value may be the string "granter".
44
+ def self.scalar_params? = true
45
+
40
46
  def self.build(params, errors, path, **)
41
- Mechanics.check_keys(params, [], errors, path)
47
+ return TapGranter::UNBOUND if params == "granter"
48
+
49
+ if params.is_a?(Hash)
50
+ Mechanics.check_keys(params, [], errors, path)
51
+ else
52
+ errors << "#{path}: must be \"tap\" or {\"tap\": \"granter\"}"
53
+ end
42
54
  INSTANCE
43
55
  end
44
56
 
@@ -51,6 +63,35 @@ module Enjine
51
63
  def mana? = false
52
64
  def to_s = "{T}"
53
65
  end
66
+
67
+ # "Tap Fishing Pole" in an ability Fishing Pole grants (CR 602.2b, 607.1a): taps the granting permanent.
68
+ # The data names no permanent; the engine binds the cost to the granter of the ability being activated
69
+ # (Engine#granter_costs). Unbound (an ability printed on the permanent itself) it taps its source.
70
+ class TapGranter
71
+ attr_reader :granter
72
+
73
+ def initialize(granter = nil)
74
+ @granter = granter
75
+ freeze
76
+ end
77
+
78
+ UNBOUND = new
79
+
80
+ def for_granter(id) = id ? TapGranter.new(id) : self
81
+
82
+ def payable?(engine, object)
83
+ perm = permanent(engine, object)
84
+ !perm.nil? && perm.zone == :battlefield && !perm.tapped
85
+ end
86
+
87
+ def pay!(engine, object) = engine.tap!(permanent(engine, object))
88
+ def mana? = false
89
+ def to_s = "Tap the granting permanent"
90
+
91
+ private
92
+
93
+ def permanent(engine, object) = granter ? engine.state.objects[granter] : object
94
+ end
54
95
  end
55
96
  end
56
97
  end
@@ -64,11 +64,19 @@ module Enjine
64
64
 
65
65
  # Runs the effect during resolution. Effects that need a player's choice override this,
66
66
  # keep their progress in `locals` (plain JSON data) and return :wait after asking.
67
- def run(engine, ctx, _locals)
68
- apply(engine, ctx) if condition.nil? || condition.true?(engine, ctx)
67
+ # An effect that deals damage first asks the affected players to order the replacement and prevention
68
+ # effects where the order matters (CR 616.1, Engine::Damage#ask_damage_orders!).
69
+ def run(engine, ctx, locals)
70
+ return :done unless locals.key?("damage_orders") || condition.nil? || condition.true?(engine, ctx)
71
+ return :wait if engine.ask_damage_orders!(damage_hits(engine, ctx), locals) == :wait
72
+
73
+ engine.with_damage_orders(locals["damage_orders"]) { apply(engine, ctx) }
69
74
  :done
70
75
  end
71
76
 
77
+ # The damage it would deal now: [[source, recipient, amount]].
78
+ def damage_hits(_engine, _ctx) = []
79
+
72
80
  def refs(engine, ctx) = to ? to.resolve(engine, ctx) : []
73
81
 
74
82
  def to_s
@@ -14,11 +14,11 @@ module Enjine
14
14
  class Become < Effect
15
15
  DURATIONS = %w[end_of_turn until_your_next_turn indefinite while_source_on_battlefield
16
16
  while_you_control_source].freeze
17
- GRANTABLE = %i[triggered mana activated play_from additional_land_plays hexproof_from protection_from].freeze
17
+ GRANTABLE = %i[triggered mana activated play_from additional_land_plays hexproof_from protection_from tapping_power].freeze
18
18
  # Card types an effect can add (CR 205.1b); instants, sorceries and planeswalkers aren't permanents it makes.
19
19
  ADDABLE_TYPES = %w[artifact creature enchantment land].freeze
20
- CHANGES = %w[types supertypes subtypes creature_types colors loses_abilities keywords base_power_toughness
21
- abilities].freeze
20
+ CHANGES = %w[set_types types supertypes subtypes creature_types all_creature_types colors loses_abilities keywords
21
+ base_power_toughness abilities].freeze
22
22
 
23
23
  primitive :effect, :become,
24
24
  description: "Each referenced permanent becomes what the parameters say, for `duration` " \
@@ -27,8 +27,11 @@ module Enjine
27
27
  "while_source_on_battlefield: for as long as this effect's source remains on the " \
28
28
  "battlefield; while_you_control_source: for as long as you control it): it has " \
29
29
  "the card `types`, `supertypes` and `subtypes` in addition to its other types (layer 4, " \
30
- "\"becomes an artifact creature\"), its creature types are exactly `creature_types` " \
31
- "(layer 4), its colors are `colors` (layer 5), with `loses_abilities: true` it loses all " \
30
+ "\"becomes an artifact creature\"; with `set_types` its card types are exactly those " \
31
+ "first, \"it's an enchantment, not a creature\", and subtypes of a card type it loses go), " \
32
+ "its creature types are exactly `creature_types` ([] for \"loses all creature types\"; " \
33
+ "`all_creature_types: true` for \"all creature types\") (layer 4), its colors are " \
34
+ "`colors` (layer 5), with `loses_abilities: true` it loses all " \
32
35
  "the abilities it had (layer 6; abilities granted later, keyword counters put on it " \
33
36
  "later and this effect's own `keywords` and `abilities` stay, CR 613.7), it has the " \
34
37
  "`keywords` and `abilities` " \
@@ -37,11 +40,15 @@ module Enjine
37
40
  "power and toughness are `base_power_toughness` (layer 7b). A permanent that becomes a " \
38
41
  "creature has its printed power and toughness (a Vehicle's), or 0/0.",
39
42
  rules: %w[611.2a 611.2b 611.2c 613.1d 613.1e 613.1f 613.4b 613.7b 205.1a 205.1b 301.7b 514.2],
40
- params: { "types" => "card types it also has: #{ADDABLE_TYPES.join(", ")}",
43
+ params: { "set_types" => "card types it has instead of its others: #{ADDABLE_TYPES.join(", ")}",
44
+ "types" => "card types it also has: #{ADDABLE_TYPES.join(", ")}",
41
45
  "supertypes" => "supertypes it also has (\"legendary\")",
42
46
  "subtypes" => "subtypes it also has (\"in addition to its other types\")",
43
- "creature_types" => "list of creature types (replaces the others)",
44
- "colors" => "list of W, U, B, R, G (replaces its colors)",
47
+ "creature_types" => "list of creature types (replaces the others; [] removes them all)",
48
+ "all_creature_types" => "true: it has every creature type",
49
+ "colors" => "list of W, U, B, R, G (replaces its colors), or \"chosen\": the color chosen " \
50
+ "earlier while this resolved (choose_color); abilities it grants that say " \
51
+ "\"chosen_color\" keep that color",
45
52
  "loses_abilities" => "true: it loses all abilities (\"loses all abilities\")",
46
53
  "keywords" => "list of keywords it gains",
47
54
  "base_power_toughness" => "{\"power\": n, \"toughness\": n}",
@@ -52,12 +59,18 @@ module Enjine
52
59
  "keywords" => ["haste"], "to" => "self" } }
53
60
 
54
61
  attr_reader :ref, :creature_types, :base_power_toughness, :granted_abilities, :duration, :card_types,
55
- :supertypes, :subtypes, :colors, :keywords
62
+ :supertypes, :subtypes, :colors, :keywords, :set_types
56
63
 
57
64
  UNTIL = { "end_of_turn" => " until end of turn", "until_your_next_turn" => " until your next turn" }.freeze
58
65
 
59
66
  def self.keys = CHANGES + %w[duration to if]
60
67
 
68
+ # The creature types a "creature_types" continuous effect sets: [] for "loses all creature types", every
69
+ # creature type for "all creature types" (CR 205.3d, 702.73a).
70
+ def self.creature_types_of(effect)
71
+ effect["all"] ? Keywords::Changeling::CREATURE_TYPES : effect["types"].map(&:to_sym)
72
+ end
73
+
61
74
  # A list of registered names (card types, supertypes, keywords) as symbols; [] when absent.
62
75
  def self.names_param(params, key, category, errors, path, allowed: nil)
63
76
  raw = Mechanics.fetch_param(params, key)
@@ -75,12 +88,16 @@ module Enjine
75
88
 
76
89
  def setup(params, errors, path)
77
90
  @ref = "become:#{path}"
91
+ @set_types = (Become.names_param(params, :set_types, :type, errors, path, allowed: ADDABLE_TYPES) if params.key?("set_types"))
78
92
  @card_types = Become.names_param(params, :types, :type, errors, path, allowed: ADDABLE_TYPES)
79
93
  @supertypes = Become.names_param(params, :supertypes, :supertype, errors, path)
80
94
  @subtypes = parse_types(Mechanics.fetch_param(params, :subtypes), errors, "#{path}.subtypes", "subtypes") || []
81
95
  @creature_types = parse_types(Mechanics.fetch_param(params, :creature_types), errors, "#{path}.creature_types",
82
- "creature types")
83
- @colors = Become.parse_colors(Mechanics.fetch_param(params, :colors), errors, path)
96
+ "creature types", empty: true)
97
+ @all_creature_types = Mechanics.fetch_param(params, :all_creature_types)
98
+ errors << "#{path}.all_creature_types: must be true (or absent)" unless [nil, true].include?(@all_creature_types)
99
+ errors << "#{path}: creature_types and all_creature_types exclude each other" if @creature_types && all_creature_types?
100
+ @colors = Become.parse_colors(Mechanics.fetch_param(params, :colors), errors, path, chosen: true)
84
101
  @loses_abilities = Mechanics.fetch_param(params, :loses_abilities)
85
102
  errors << "#{path}.loses_abilities: must be true (or absent)" unless [nil, true].include?(@loses_abilities)
86
103
  @keywords = Become.names_param(params, :keywords, :keyword, errors, path)
@@ -94,6 +111,7 @@ module Enjine
94
111
  end
95
112
 
96
113
  def loses_abilities? = @loses_abilities == true
114
+ def all_creature_types? = @all_creature_types == true
97
115
 
98
116
  # The state.effects duration and the details it needs. "For as long as [source] remains on the
99
117
  # battlefield" does nothing if the source has already left as this resolves (CR 611.2b).
@@ -113,17 +131,35 @@ module Enjine
113
131
  add = ->(kind, details) { engine.add_continuous_effect!(kind, ctx, affected, details.merge(lasting)) }
114
132
  # Removal first: this effect's own keywords and abilities come after it (CR 613.7b).
115
133
  add.call("lose_abilities", {}) if loses_abilities?
134
+ add.call("set_types", "types" => set_types.map(&:to_s)) if set_types # CR 205.1a, before the types added
116
135
  unless card_types.empty? && supertypes.empty? && subtypes.empty?
117
136
  add.call("add_types", "types" => card_types.map(&:to_s), "supertypes" => supertypes.map(&:to_s), "subtypes" => subtypes)
118
137
  end
119
138
  creatures = affected.select { engine.chars(it).creature? } # after the types above are added
120
- engine.add_continuous_effect!("creature_types", ctx, creatures, { "types" => creature_types }.merge(lasting)) if creature_types
121
- add.call("colors", "colors" => colors) if colors
139
+ (types = creature_type_details) && engine.add_continuous_effect!("creature_types", ctx, creatures, types.merge(lasting))
140
+ chosen = Filters.chosen_memory(engine, ctx.source, ctx).select { |k, _| k.start_with?("chosen_") }
141
+ color_list = color_list(chosen)
142
+ add.call("colors", "colors" => color_list) if color_list
122
143
  add.call("grant", "keywords" => keywords.map(&:to_s)) unless keywords.empty?
123
144
  engine.add_continuous_effect!("base_power_toughness", ctx, creatures, base_power_toughness.merge(lasting)) if base_power_toughness
124
- return if granted_abilities.empty?
145
+ add.call("abilities", granted_details(engine, ctx, chosen)) unless granted_abilities.empty?
146
+ end
147
+
148
+ # Its creature types (CR 205.3d: all of them), or nil when it doesn't change them.
149
+ def creature_type_details
150
+ return { "types" => [], "all" => true } if all_creature_types?
151
+
152
+ creature_types && { "types" => creature_types }
153
+ end
154
+
155
+ # "Becomes the color of your choice": the color chosen while this resolved.
156
+ def color_list(chosen) = colors == "chosen" ? chosen["chosen_color"]&.then { [it] } : colors
125
157
 
126
- add.call("abilities", "card" => engine.object(ctx.object).card, "ref" => ref)
158
+ # What was chosen while this resolved is bound into the abilities it grants (CR 607.2d: "hexproof from
159
+ # that color"), so a later choice doesn't change them.
160
+ def granted_details(engine, ctx, chosen)
161
+ granted = { "card" => engine.object(ctx.object).card, "ref" => ref }
162
+ chosen.empty? ? granted : granted.merge("chosen" => chosen)
127
163
  end
128
164
 
129
165
  def nested_effects = granted_abilities.flat_map(&:try_effects)
@@ -131,16 +167,19 @@ module Enjine
131
167
  def to_s
132
168
  bits = []
133
169
  bits << "loses all abilities" if loses_abilities?
170
+ bits << "only #{set_types.join(" ")}" if set_types
134
171
  bits << [*supertypes, *colors, *subtypes, *card_types].join(" ") unless (card_types + supertypes + subtypes).empty?
135
- bits << creature_types.join(" ") if creature_types
172
+ bits << (creature_types.empty? ? "no creature types" : creature_types.join(" ")) if creature_types
173
+ bits << "all creature types" if all_creature_types?
136
174
  bits << "base #{base_power_toughness.values.join("/")}" if base_power_toughness
137
175
  bits << keywords.join(", ") unless keywords.empty?
138
176
  bits << "\"#{granted_abilities.join("; ")}\"" unless granted_abilities.empty?
139
177
  "becomes #{bits.join(", ")}#{UNTIL[duration]} → #{to}"
140
178
  end
141
179
 
142
- def self.parse_colors(raw, errors, path)
180
+ def self.parse_colors(raw, errors, path, chosen: false)
143
181
  return nil if raw.nil?
182
+ return raw if chosen && raw == "chosen"
144
183
 
145
184
  ok = raw.is_a?(Array) && raw.all? { Mana::COLORS.include?(it.to_s.upcase.to_sym) }
146
185
  errors << "#{path}.colors: must be a list of W, U, B, R, G ([] for colorless)" unless ok
@@ -162,11 +201,11 @@ module Enjine
162
201
 
163
202
  private
164
203
 
165
- def parse_types(raw, errors, path, noun)
204
+ def parse_types(raw, errors, path, noun, empty: false)
166
205
  return nil if raw.nil?
167
206
 
168
- ok = raw.is_a?(Array) && !raw.empty? && raw.all? { CardDefinition::SUBTYPE_FORMAT.match?(it.to_s) }
169
- errors << "#{path}: must be a non-empty list of lowercase #{noun}" unless ok
207
+ ok = raw.is_a?(Array) && (empty || !raw.empty?) && raw.all? { CardDefinition::SUBTYPE_FORMAT.match?(it.to_s) }
208
+ errors << "#{path}: must be a #{"non-empty " unless empty}list of lowercase #{noun}" unless ok
170
209
  ok ? raw.map(&:to_s).freeze : nil
171
210
  end
172
211
 
@@ -176,7 +215,7 @@ module Enjine
176
215
  next ability if ability.nil? || (GRANTABLE.include?(ability.kind) && !(ability.kind == :play_from && ability.self?))
177
216
 
178
217
  errors << "#{path}.abilities[#{j}]: only triggered, activated, mana, play_from (for other cards), " \
179
- "additional_land_plays, hexproof_from and protection_from abilities can be gained"
218
+ "additional_land_plays, hexproof_from, protection_from and tapping_power abilities can be gained"
180
219
  nil
181
220
  end.freeze
182
221
  end