enjine 0.5.0 → 0.6.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 (120) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +146 -1
  3. data/README.md +12 -3
  4. data/SUPPORTED.md +44 -17
  5. data/docs/ARCHITECTURE.md +7 -2
  6. data/docs/MECHANICS_COVERAGE.md +10 -8
  7. data/docs/guides/api.md +2 -1
  8. data/docs/guides/card-data-format.md +40 -8
  9. data/docs/reference/abilities.md +254 -13
  10. data/docs/reference/costs.md +36 -0
  11. data/docs/reference/decisions.md +3 -1
  12. data/docs/reference/effects.md +93 -10
  13. data/docs/reference/filters.md +11 -1
  14. data/docs/reference/keywords.md +42 -0
  15. data/docs/reference/subtypes.md +18 -0
  16. data/docs/reference/triggers.md +69 -8
  17. data/docs/reference/types.md +20 -0
  18. data/lib/enjine/actions/special_action.rb +4 -0
  19. data/lib/enjine/actions/turn_face_up.rb +25 -0
  20. data/lib/enjine/actions/unlock.rb +23 -0
  21. data/lib/enjine/card_definition.rb +50 -12
  22. data/lib/enjine/card_face_down.rb +31 -0
  23. data/lib/enjine/card_faces.rb +4 -2
  24. data/lib/enjine/card_rooms.rb +86 -0
  25. data/lib/enjine/catalog.rb +9 -3
  26. data/lib/enjine/cli/text.rb +1 -0
  27. data/lib/enjine/color_identity.rb +34 -1
  28. data/lib/enjine/decisions/casting.rb +6 -3
  29. data/lib/enjine/engine/ability_layers.rb +8 -0
  30. data/lib/enjine/engine/casting/alt_casting.rb +12 -4
  31. data/lib/enjine/engine/casting/cast_choices.rb +8 -1
  32. data/lib/enjine/engine/casting/casting.rb +18 -22
  33. data/lib/enjine/engine/casting/costs.rb +8 -1
  34. data/lib/enjine/engine/casting/payment.rb +47 -9
  35. data/lib/enjine/engine/casting/permissions.rb +6 -3
  36. data/lib/enjine/engine/characteristics.rb +8 -9
  37. data/lib/enjine/engine/combat.rb +19 -7
  38. data/lib/enjine/engine/copies.rb +2 -1
  39. data/lib/enjine/engine/crimes.rb +46 -0
  40. data/lib/enjine/engine/damage.rb +36 -1
  41. data/lib/enjine/engine/face_down_permanents.rb +108 -0
  42. data/lib/enjine/engine/faces.rb +6 -3
  43. data/lib/enjine/engine/mana_sources.rb +18 -3
  44. data/lib/enjine/engine/options_memo.rb +140 -0
  45. data/lib/enjine/engine/permanent_designations.rb +29 -0
  46. data/lib/enjine/engine/resolution.rb +25 -5
  47. data/lib/enjine/engine/rooms.rb +86 -0
  48. data/lib/enjine/engine/rules.rb +2 -1
  49. data/lib/enjine/engine/state_based_actions.rb +25 -4
  50. data/lib/enjine/engine/triggers.rb +20 -5
  51. data/lib/enjine/engine/turn_history.rb +28 -5
  52. data/lib/enjine/engine/zones.rb +32 -6
  53. data/lib/enjine/engine.rb +5 -1
  54. data/lib/enjine/events.rb +9 -0
  55. data/lib/enjine/frames.rb +3 -0
  56. data/lib/enjine/fuzz.rb +3 -0
  57. data/lib/enjine/game.rb +3 -1
  58. data/lib/enjine/mechanics/abilities/cost_changes.rb +32 -10
  59. data/lib/enjine/mechanics/abilities/damage_replacements.rb +1 -1
  60. data/lib/enjine/mechanics/abilities/designated.rb +57 -0
  61. data/lib/enjine/mechanics/abilities/life_replacements.rb +74 -0
  62. data/lib/enjine/mechanics/abilities/mana_ability.rb +10 -4
  63. data/lib/enjine/mechanics/abilities/player_rules.rb +1 -1
  64. data/lib/enjine/mechanics/abilities/stack_abilities.rb +12 -7
  65. data/lib/enjine/mechanics/abilities/static.rb +18 -5
  66. data/lib/enjine/mechanics/card_spec.rb +12 -3
  67. data/lib/enjine/mechanics/costs/base.rb +3 -2
  68. data/lib/enjine/mechanics/costs/behold.rb +10 -3
  69. data/lib/enjine/mechanics/costs/choose_creature_type.rb +3 -3
  70. data/lib/enjine/mechanics/costs/collect_evidence.rb +60 -0
  71. data/lib/enjine/mechanics/costs/exile_permanent.rb +41 -0
  72. data/lib/enjine/mechanics/effects/base.rb +26 -5
  73. data/lib/enjine/mechanics/effects/choose_cards.rb +44 -14
  74. data/lib/enjine/mechanics/effects/copy.rb +2 -2
  75. data/lib/enjine/mechanics/effects/counters.rb +12 -6
  76. data/lib/enjine/mechanics/effects/delayed_triggers.rb +13 -6
  77. data/lib/enjine/mechanics/effects/may.rb +4 -2
  78. data/lib/enjine/mechanics/effects/pay.rb +20 -6
  79. data/lib/enjine/mechanics/filters/any_target.rb +6 -3
  80. data/lib/enjine/mechanics/filters/base.rb +14 -5
  81. data/lib/enjine/mechanics/filters/permanents.rb +7 -0
  82. data/lib/enjine/mechanics/keywords/alternative_costs.rb +70 -16
  83. data/lib/enjine/mechanics/keywords/cards_have.rb +4 -2
  84. data/lib/enjine/mechanics/keywords/craft.rb +132 -0
  85. data/lib/enjine/mechanics/keywords/cycling.rb +46 -7
  86. data/lib/enjine/mechanics/keywords/extort.rb +2 -0
  87. data/lib/enjine/mechanics/keywords/face_down.rb +165 -0
  88. data/lib/enjine/mechanics/keywords/impending.rb +75 -0
  89. data/lib/enjine/mechanics/keywords/increment.rb +2 -0
  90. data/lib/enjine/mechanics/keywords/infinity.rb +46 -0
  91. data/lib/enjine/mechanics/keywords/miracle.rb +142 -0
  92. data/lib/enjine/mechanics/keywords/ninjutsu.rb +101 -0
  93. data/lib/enjine/mechanics/keywords/offspring.rb +48 -0
  94. data/lib/enjine/mechanics/keywords/persist_and_undying.rb +42 -0
  95. data/lib/enjine/mechanics/keywords/split_second.rb +29 -0
  96. data/lib/enjine/mechanics/keywords/storm_and_cascade.rb +5 -0
  97. data/lib/enjine/mechanics/keywords/void.rb +34 -0
  98. data/lib/enjine/mechanics/linked.rb +2 -2
  99. data/lib/enjine/mechanics/mana_spent.rb +1 -1
  100. data/lib/enjine/mechanics/selector.rb +30 -9
  101. data/lib/enjine/mechanics/subtypes/case.rb +68 -0
  102. data/lib/enjine/mechanics/triggers/base.rb +13 -4
  103. data/lib/enjine/mechanics/triggers/crime.rb +28 -0
  104. data/lib/enjine/mechanics/triggers/damage.rb +33 -18
  105. data/lib/enjine/mechanics/triggers/draw_and_discard.rb +19 -6
  106. data/lib/enjine/mechanics/triggers/rooms.rb +49 -0
  107. data/lib/enjine/mechanics/triggers/zone_changes.rb +24 -9
  108. data/lib/enjine/mechanics/types/battle.rb +130 -0
  109. data/lib/enjine/mechanics/values.rb +74 -27
  110. data/lib/enjine/replacements.rb +18 -0
  111. data/lib/enjine/rules_monitor.rb +49 -75
  112. data/lib/enjine/snapshot/deep_copy.rb +23 -0
  113. data/lib/enjine/snapshot/player_checks.rb +10 -2
  114. data/lib/enjine/snapshot.rb +2 -1
  115. data/lib/enjine/state.rb +2 -2
  116. data/lib/enjine/verify.rb +10 -3
  117. data/lib/enjine/version.rb +1 -1
  118. data/lib/enjine/view.rb +49 -13
  119. data/lib/enjine.rb +1 -1
  120. metadata +29 -1
@@ -86,12 +86,16 @@ module Enjine
86
86
  def cost_text = costs.empty? ? "" : " #{costs.join(", ")}"
87
87
  def to_s = "#{keyword.split("_").map(&:capitalize).join("-")}#{cost_text}"
88
88
 
89
- # The cast method (Engine::Permissions) this ability gives.
90
- def method_for(ref)
89
+ # The mana cost string paid for the card `obj` (nil: it can't be cast this way, CR 118.6).
90
+ def alt_cost_for(_engine, _obj) = mana_cost.to_s
91
+
92
+ # The cast method (Engine::Permissions) this ability gives the card `obj`; nil when it has none.
93
+ def method_for(ref, engine: nil, obj: nil)
91
94
  method = { "via" => "alt:#{ref}", "play" => play, "keyword" => keyword }
92
95
  return method unless alternative?
93
96
 
94
- alt = { "cost" => mana_cost.to_s, "ref" => ref }
97
+ cost = alt_cost_for(engine, obj) or return nil
98
+ alt = { "cost" => cost, "ref" => ref }
95
99
  alt["exile"] = true if exiles?
96
100
  alt["returns"] = returns if returns
97
101
  alt["harmonize"] = true if is_a?(Harmonize)
@@ -106,11 +110,40 @@ module Enjine
106
110
  description: "Flashback: you may cast this instant or sorcery card from your graveyard by paying " \
107
111
  "`cost` rather than its mana cost; then it's exiled whenever it would leave the stack. " \
108
112
  "`cost` is a mana cost string or a list of costs (`[{\"mana\": \"{1}{U}\"}, " \
109
- "{\"tap_permanents\": {\"count\": 3}}]`). Cast option `via: \"alt:<ref>\"`.",
110
- rules: %w[702.34a 118.9 601.2b 601.2f 601.2h],
111
- params: { "cost" => "mana cost string, or a list of costs" },
113
+ "{\"tap_permanents\": {\"count\": 3}}]`), or `\"mana_cost\"`: the card's own mana cost " \
114
+ "(\"its flashback cost is equal to its mana cost\", e.g. granted with `grant_ability`). " \
115
+ "Cast option `via: \"alt:<ref>\"`.",
116
+ rules: %w[702.34a 118.9 118.6 601.2b 601.2f 601.2h],
117
+ params: { "cost" => "mana cost string, a list of costs, or \"mana_cost\"" },
112
118
  example: { "flashback" => { "cost" => "{2}{R}" } }
113
119
 
120
+ MANA_COST = "mana_cost"
121
+
122
+ # `{"cost": "mana_cost"}`: "its flashback cost is equal to its mana cost" (a granted flashback, CR 702.34a).
123
+ def self.build(params, errors, path, **)
124
+ return super unless Mechanics.fetch_param(params, :cost) == MANA_COST
125
+
126
+ Mechanics.check_keys(params, %w[cost], errors, path)
127
+ new([], path, errors, own_mana_cost: true)
128
+ end
129
+
130
+ def initialize(costs, path, errors, own_mana_cost: false)
131
+ @own_mana_cost = own_mana_cost
132
+ super(costs, path, errors)
133
+ end
134
+
135
+ def own_mana_cost? = @own_mana_cost
136
+ def alternative? = own_mana_cost? || super
137
+
138
+ # The card's own mana cost, X included (CR 107.3); a card with no mana cost can't be cast this way (CR 118.6).
139
+ def alt_cost_for(engine, obj)
140
+ return super unless own_mana_cost?
141
+
142
+ engine && obj && engine.chars(obj).mana_cost&.to_s
143
+ end
144
+
145
+ def cost_text = own_mana_cost? ? " (its mana cost)" : super
146
+
114
147
  def zone = :graveyard
115
148
  def exiles? = true
116
149
  def allowed_types = %i[instant sorcery]
@@ -160,18 +193,38 @@ module Enjine
160
193
  "cost. If you do, the permanent it becomes is exiled at the beginning of the next end " \
161
194
  "step (a delayed triggered ability), and its owner may cast it from exile after this " \
162
195
  "turn has ended for as long as it remains exiled (cast option `via: \"warped\"`). " \
163
- "Conditions `{\"cast_with\": \"warp\"}` and `{\"cast_with_this_turn\": \"warp\"}` read it.",
164
- rules: %w[702.185a 702.185b 702.185c 603.7 118.9 601.2b 601.2f 601.2h],
165
- params: { "cost" => "mana cost string, or a list of costs" },
196
+ "Conditions `{\"cast_with\": \"warp\"}` and `{\"cast_with_this_turn\": \"warp\"}` read it. " \
197
+ "`from: [\"hand\", \"graveyard\"]`: \"you may cast this card from your graveyard using " \
198
+ "its warp ability\" (CR 601.3).",
199
+ rules: %w[702.185a 702.185b 702.185c 603.7 118.9 601.2b 601.2f 601.2h 601.3],
200
+ params: { "cost" => "mana cost string, or a list of costs",
201
+ "from" => "zones it works from: \"hand\" (default), \"graveyard\", or a list of both" },
166
202
  example: { "warp" => { "cost" => "{1}{R}" } }
167
203
 
168
- attr_reader :delayed
204
+ ZONES = %w[hand graveyard].freeze
169
205
 
170
- def initialize(costs, path, errors)
206
+ attr_reader :delayed, :zones
207
+
208
+ def self.build(params, errors, path, **)
209
+ raw = Mechanics.fetch_param(params, :from) || "hand"
210
+ zones = Array(raw).map(&:to_s)
211
+ if zones.empty? || !(zones - ZONES).empty?
212
+ errors << "#{path}.from: must be hand, graveyard or a list of them"
213
+ zones = ["hand"]
214
+ end
215
+ Mechanics.check_keys(params, %w[cost from], errors, path)
216
+ new(read_costs(Mechanics.fetch_param(params, :cost), errors, path), path, errors,
217
+ zones: zones.uniq.map(&:to_sym).freeze)
218
+ end
219
+
220
+ def initialize(costs, path, errors, zones: %i[hand].freeze)
171
221
  @delayed = Effects::WarpDelayed.build({}, errors, path)
172
- super
222
+ @zones = zones
223
+ super(costs, path, errors)
173
224
  end
174
225
 
226
+ def functions?(engine, obj, index) = zones.include?(obj.zone) && usable?(engine, obj, index)
227
+
175
228
  def try_effects = [delayed]
176
229
  end
177
230
 
@@ -281,12 +334,12 @@ module Enjine
281
334
  # that permanent's triggered abilities), remembers it.
282
335
  class CastHow
283
336
  KEYS = %w[cast_with cast_from cast_with_this_turn].freeze
284
- KEYWORDS = %w[flashback harmonize mayhem warp evoke web_slinging sneak foretell].freeze
337
+ KEYWORDS = %w[flashback harmonize mayhem warp evoke web_slinging sneak foretell impending miracle].freeze
285
338
  # Optional additional costs announced as the spell is cast (Engine::CastChoices#announce_kicker!):
286
339
  # "if this spell was cast using teamwork" (CR 702.194b), "if this spell's additional cost was paid" (CR 118.8b),
287
340
  # "if the gift was promised" (CR 702.174k).
288
341
  OPTIONAL_TEXT = { "teamwork" => "it was cast using teamwork", "additional_cost" => "its additional cost was paid",
289
- "gift" => "its gift was promised" }.freeze
342
+ "gift" => "its gift was promised", "offspring" => "its offspring cost was paid" }.freeze
290
343
  OPTIONAL = OPTIONAL_TEXT.keys.freeze
291
344
  ZONES = %w[hand graveyard exile library outside].freeze
292
345
 
@@ -312,9 +365,10 @@ module Enjine
312
365
  return engine.cast_with_this_turn?(@value) if @key == "cast_with_this_turn"
313
366
 
314
367
  holder = engine.state.objects[ctx.object || ctx.source]
315
- return holder&.memory&.[]("kicked_with") == @value if OPTIONAL.include?(@value)
368
+ memory = holder ? holder.memory : engine.departed_cast_memory(ctx.source) # a dies trigger checked (CR 603.4)
369
+ return memory&.[]("kicked_with") == @value if OPTIONAL.include?(@value)
316
370
 
317
- holder&.memory&.[](@key) == @value
371
+ memory&.[](@key) == @value
318
372
  end
319
373
 
320
374
  def to_s
@@ -12,12 +12,12 @@ module Enjine
12
12
  # (`grants_beam_me_up` is the same idea for beam me up, whose cost is each card's own mana cost.)
13
13
  class CardsHave < Primitive
14
14
  ZONES = %w[graveyard hand exile].freeze
15
- GRANTABLE = %i[flashback harmonize mayhem web_slinging sneak].freeze
15
+ GRANTABLE = %i[flashback harmonize mayhem web_slinging sneak miracle].freeze
16
16
 
17
17
  primitive :ability, :cards_have,
18
18
  description: "While this permanent is on the battlefield, cards you own in `zone` (graveyard " \
19
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 " \
20
+ "(flashback, harmonize, mayhem, web_slinging, sneak or miracle) and may be cast with it from " \
21
21
  "that zone. Cast option `via: \"alt:cards_have:<permanent>:<ref>\"`.",
22
22
  rules: %w[611.3a 604.1 118.9 601.2b 702.190a],
23
23
  params: { "zone" => ZONES.join(" | "), "cards" => "card spec",
@@ -52,6 +52,8 @@ module Enjine
52
52
  def mana_ability? = false
53
53
  def allowed_types = nil
54
54
  def try_effects = []
55
+ # A granted keyword's own triggered ability (miracle's, Abilities::Miracle) belongs to this permanent's card.
56
+ def linked_abilities = granted.respond_to?(:linked_abilities) ? granted.linked_abilities : []
55
57
 
56
58
  # Whether the card `obj` (owned by `index`, who controls this permanent) may be cast with the granted ability now.
57
59
  def functions?(engine, obj, index)
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../costs/sacrifice"
4
+ require_relative "../effects/return_to_battlefield"
5
+
6
+ module Enjine
7
+ module Mechanics
8
+ module Costs
9
+ # Craft's "Exile this permanent" (CR 702.167a): exiles it like `exile_self`, and the craft ability being
10
+ # activated remembers the card in exile ("crafted"), which its effect returns transformed.
11
+ class CraftSelf < ExileSelf
12
+ INSTANCE = new.freeze
13
+
14
+ def pay!(engine, object)
15
+ fresh = super or return nil
16
+ holder = engine.casting && engine.state.objects[engine.casting["object"]]
17
+ holder.memory["crafted"] = fresh.id if holder && fresh.zone == :exile
18
+ fresh
19
+ end
20
+ end
21
+
22
+ # Craft's materials (CR 702.167a-b): "Exile [materials] from among permanents you control and/or cards in
23
+ # your graveyard", never the crafting permanent. `count` of them matching the card spec `card`, or with
24
+ # `or_more` that many or more (`done` once enough are chosen). Each is chosen while activating
25
+ # (`choose_cost`) and exiled as the ability becomes activated; the ability remembers them ("exiled_for_cost").
26
+ class CraftMaterials < CardsCost
27
+ ZONES = %i[battlefield graveyard].freeze
28
+
29
+ def self.zone = :graveyard
30
+
31
+ def self.from(data, errors, path)
32
+ data = {} unless data.is_a?(Hash)
33
+ Mechanics.check_keys(data, %w[card count or_more], errors, path)
34
+ count = Mechanics.integer_param(data, :count, errors, path, default: 1, min: 1)
35
+ raw = Mechanics.fetch_param(data, :card)
36
+ spec = raw && CardSpec.build(raw, errors, "#{path}.card")
37
+ new(spec, count, Mechanics.flag_param(data, :or_more, errors, path), {}, errors, path)
38
+ end
39
+
40
+ def or_more? = @other
41
+
42
+ # A permanent you control (a token too) or a card in your graveyard, other than the crafting permanent.
43
+ def candidates(engine, object)
44
+ pl = engine.player(object.controller)
45
+ graveyard = pl.graveyard.select { |id| filter.nil? || filter.match?(engine, engine.object(id)) }
46
+ permanents = engine.state.battlefield.select do |id|
47
+ id != object.id && engine.object(id).controller == object.controller &&
48
+ (filter.nil? || filter.chars_match?(engine, engine.chars(id)))
49
+ end
50
+ permanents + graveyard
51
+ end
52
+
53
+ def choices_needed = or_more? ? 0 : count
54
+ def payable?(engine, object) = candidates(engine, object).size >= count
55
+ def finishable?(entries) = or_more? && entries.size >= count
56
+
57
+ def chosen?(engine, object, entries, done)
58
+ return entries.size >= count unless or_more?
59
+
60
+ finishable?(entries) && (done || choice_options(engine, object, entries).empty?)
61
+ end
62
+
63
+ # As it becomes activated, the chosen materials still on the battlefield or in the graveyard are exiled.
64
+ def pay_chosen!(engine, _object, entries)
65
+ entries.map { it["object"] }.select { ZONES.include?(engine.state.objects[it]&.zone) }.each { pay_card!(engine, it) }
66
+ end
67
+
68
+ def to_s
69
+ many = or_more? ? "#{count} or more" : { 1 => "a" }.fetch(count, count)
70
+ "Exile #{many} #{filter || "card"}#{"s" unless count == 1} from among permanents you control and/or cards in your graveyard"
71
+ end
72
+ end
73
+ end
74
+
75
+ module Effects
76
+ # Craft's effect (CR 702.167a): "Return this card to the battlefield transformed under its owner's control" —
77
+ # the card the craft ability exiled as its cost (Costs::CraftSelf). Not a card-data primitive.
78
+ class CraftReturn < ReturnToBattlefield
79
+ def self.keys = []
80
+
81
+ def refs(engine, ctx)
82
+ id = engine.state.objects[ctx.object]&.memory&.[]("crafted")
83
+ id && engine.state.exile.include?(id) ? [id] : []
84
+ end
85
+
86
+ def to_s = "return this card to the battlefield transformed"
87
+ end
88
+ end
89
+
90
+ module Abilities
91
+ # Craft with [materials] [cost] (CR 702.167a-c): "[Cost], Exile this permanent, Exile [materials] from among
92
+ # permanents you control and/or cards in your graveyard: Return this card to the battlefield transformed under
93
+ # its owner's control. Activate only as a sorcery." A double-faced card's front face has it; a card that
94
+ # isn't double-faced stays in exile (CR 712.11a, Effects::ReturnToBattlefield).
95
+ class Craft < ActivatedAbility
96
+ primitive :ability, :craft,
97
+ description: "Craft with `materials` `cost`: pay `cost`, exile this permanent and exile the " \
98
+ "materials (`count` (default 1) permanents you control and/or cards in your graveyard " \
99
+ "matching the card spec `card`; `or_more: true`: that many or more), chosen with " \
100
+ "`choose_cost`: return this card to the battlefield transformed under its owner's " \
101
+ "control. Sorcery speed. The exiled materials are the selector \"exiled_for_cost\".",
102
+ rules: %w[702.167a 702.167b 702.167c 712.11a 602.2b 406.1],
103
+ params: { "cost" => "mana cost string, or a list of costs",
104
+ "materials" => "{\"card\": card spec, \"count\": n, \"or_more\": true}" },
105
+ example: { "craft" => { "cost" => "{3}{W}", "materials" => { "card" => { "types" => ["artifact"] } } } }
106
+
107
+ attr_reader :materials
108
+
109
+ def self.build(params, errors, path, **)
110
+ Mechanics.check_keys(params, %w[cost materials], errors, path)
111
+ raw = Mechanics.fetch_param(params, :cost)
112
+ paid = case raw
113
+ when String then [Costs::ManaPayment.build(raw, errors, "#{path}.cost")]
114
+ when Array then StackAbility.parse_costs({ "cost" => raw }, errors, path)
115
+ else (errors << "#{path}.cost: must be a mana cost string or a list of costs") && []
116
+ end
117
+ materials = Costs::CraftMaterials.from(Mechanics.fetch_param(params, :materials), errors, "#{path}.materials")
118
+ effect = Effects::CraftReturn.new(to: nil, params: { "transformed" => true }, errors: errors, path: "#{path}.return")
119
+ new(targets: [].freeze, effects: [effect].freeze, costs: (paid + [Costs::CraftSelf::INSTANCE, materials]).freeze,
120
+ sorcery: true, materials: materials)
121
+ end
122
+
123
+ def initialize(materials:, **)
124
+ @materials = materials
125
+ super(**)
126
+ end
127
+
128
+ def to_s = "Craft — #{costs.join(", ")}: return this card to the battlefield transformed"
129
+ end
130
+ end
131
+ end
132
+ end
@@ -1,20 +1,42 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../costs/discard"
4
+
3
5
  module Enjine
4
6
  module Mechanics
7
+ module Costs
8
+ # Cycling's "discard this card" (CR 702.29a): discards it like `discard_self` (a cycled card is discarded,
9
+ # CR 702.29d), and the card in the graveyard remembers which hand object it was, so its own "when you cycle
10
+ # this card" ability (the `cycles` trigger with `self: true`, CR 702.29c) knows it was the one cycled.
11
+ class CycleSelf < DiscardSelf
12
+ INSTANCE = new.freeze
13
+
14
+ def pay!(engine, object)
15
+ fresh = super
16
+ fresh.memory["cycled_from"] = object.id if fresh
17
+ fresh
18
+ end
19
+ end
20
+ end
21
+
5
22
  module Abilities
6
23
  # Cycling and typecycling (CR 702.29): activated abilities that function only while the card
7
24
  # is in its owner's hand.
8
25
  # {"cycling": {"cost": "{2}"}} [cost], discard this card: draw a card
26
+ # {"cycling": {"cost": "{X}{G}{G}"}} X is announced as it's activated (CR 107.3)
27
+ # {"cycling": {"cost": [{"pay_life": 2}]}} a cost list: "Cycling—Pay 2 life"
9
28
  # {"cycling": {"cost": "{2}", "for": {"subtypes": ["swamp"]}}} ...: search for a Swamp card (typecycling)
10
29
  # {"cycling": {"cost": "{2}", "for": {"supertypes": ["basic"], "types": ["land"]}}} basic landcycling
11
30
  class Cycling < ActivatedAbility
12
31
  primitive :ability, :cycling,
13
32
  description: "Cycling: pay `cost` and discard this card from your hand to draw a card. With `for` " \
14
33
  "it is typecycling: instead search your library for a card matching `for`, reveal " \
15
- "it, put it into your hand, then shuffle.",
16
- rules: %w[702.29a 702.29b 702.29e 702.29f 113.6m 602.2a],
17
- params: { "cost" => "mana cost string, e.g. \"{2}\"",
34
+ "it, put it into your hand, then shuffle. `cost` is a mana cost string (it may hold " \
35
+ "{X}, announced as it's activated) or a list of costs (`[{\"pay_life\": 2}]`). \"When " \
36
+ "you cycle this card\" is a triggered ability with `zone: \"graveyard\"` and the trigger " \
37
+ "`{\"cycles\": {\"self\": true}}`; its X is the cycling ability's.",
38
+ rules: %w[702.29a 702.29b 702.29c 702.29d 702.29e 702.29f 113.6m 602.2a 107.3],
39
+ params: { "cost" => "mana cost string, e.g. \"{2}\" or \"{X}{G}{G}\", or a list of costs",
18
40
  "for" => "optional card spec for typecycling (see search_library)" },
19
41
  example: { "cycling" => { "cost" => "{2}", "for" => { "supertypes" => ["basic"], "types" => ["land"] } } }
20
42
 
@@ -22,14 +44,31 @@ module Enjine
22
44
 
23
45
  def self.build(params, errors, path, **)
24
46
  Mechanics.check_keys(params, %w[cost for], errors, path)
25
- raw = Mechanics.fetch_param(params, :cost)
26
- mana = raw.nil? ? ((errors << "#{path}.cost: required") && "{0}") : raw
27
- costs = [Costs::ManaPayment.build(mana, errors, "#{path}.cost"), Costs::DiscardSelf::INSTANCE].freeze
47
+ costs = (read_costs(Mechanics.fetch_param(params, :cost), errors, path) + [Costs::CycleSelf::INSTANCE]).freeze
28
48
  spec_data = Mechanics.fetch_param(params, :for)
29
49
  spec = spec_data.nil? ? nil : CardSpec.build(spec_data, errors, "#{path}.for")
30
50
  new(targets: [].freeze, effects: [effect_for(spec_data)].compact.freeze, costs: costs, sorcery: false, spec: spec)
31
51
  end
32
52
 
53
+ # A mana cost string, or a list of costs that are paid from the hand along with the discard (no tapping,
54
+ # sacrificing or exiling a permanent: the card is in the hand, CR 602.2b).
55
+ def self.read_costs(raw, errors, path)
56
+ case raw
57
+ when nil then (errors << "#{path}.cost: required") && [Costs::ManaPayment.build("{0}", errors, "#{path}.cost")]
58
+ when String then [Costs::ManaPayment.build(raw, errors, "#{path}.cost")]
59
+ when Array
60
+ errors << "#{path}.cost: must be a non-empty list of costs" if raw.empty?
61
+ costs = StackAbility.parse_costs({ "cost" => raw }, errors, path)
62
+ costs.each_with_index do |cost, i|
63
+ next unless cost.is_a?(Costs::Tap) || Costs::LEAVING_SELF.include?(cost.class) || cost.respond_to?(:zone)
64
+
65
+ errors << "#{path}.cost[#{i}]: can't be paid while this card is in your hand"
66
+ end
67
+ costs
68
+ else (errors << "#{path}.cost: must be a mana cost string or a list of costs") && []
69
+ end
70
+ end
71
+
33
72
  # The ability's effect as card data, built like any other effect (errors in `for` are reported above).
34
73
  def self.effect_for(spec_data)
35
74
  data = if spec_data.nil?
@@ -50,7 +89,7 @@ module Enjine
50
89
 
51
90
  def to_s
52
91
  name = spec ? "#{spec.to_s.delete_suffix(" card").capitalize}cycling" : "Cycling"
53
- "#{name} #{costs.first}"
92
+ "#{name} #{costs[0...-1].join(", ")}"
54
93
  end
55
94
  end
56
95
  end
@@ -20,6 +20,8 @@ module Enjine
20
20
  } }.freeze
21
21
 
22
22
  def self.intrinsic_abilities = @intrinsic_abilities ||= [Keywords.ability_from_data(ABILITY, "extort")].freeze
23
+ # CR 702.101b: list it once per instance
24
+ def self.multiple_instances? = true
23
25
  end
24
26
  end
25
27
  end
@@ -0,0 +1,165 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "alternative_costs"
4
+
5
+ module Enjine
6
+ module Mechanics
7
+ module Effects
8
+ # Manifest (CR 701.40a) and cloak (CR 701.58a): each referenced player puts the top card of their library onto
9
+ # the battlefield face down, `amount` times one at a time (CR 701.40e, 701.58e). See Engine::FaceDownPermanents.
10
+ class Manifest < Effect
11
+ primitive :effect, :manifest,
12
+ description: "Each referenced player (default you) manifests the top card of their library `amount` " \
13
+ "times (default 1): it enters face down as a nameless colorless 2/2 creature with no " \
14
+ "abilities. Its controller may turn it face up for its mana cost if it's a creature card " \
15
+ "(special action `turn_face_up`). The new permanents are this effect's results.",
16
+ rules: %w[701.40a 701.40b 701.40e 701.40f 701.40g 708.2 708.3],
17
+ params: { "amount" => "amount (default 1)", "to" => "players (default you)" },
18
+ example: { "manifest" => {} }
19
+
20
+ def self.keys = %w[amount to if]
21
+ def self.default_to = "you"
22
+ def self.amount_optional?(_params) = true
23
+ def as = "manifest"
24
+
25
+ def setup(params, _errors, _path)
26
+ @amount = Amount::Fixed.new(1) if Mechanics.fetch_param(params, :amount).nil?
27
+ end
28
+
29
+ def apply(engine, ctx)
30
+ n = amount_for(engine, ctx)
31
+ players(engine, ctx).each { |index| n.times { engine.manifest!(index, as) } }
32
+ end
33
+
34
+ def to_s = "#{to} #{as}#{"s" unless to.to_s == "you"} the top card of their library#{" #{amount} times" unless amount == 1}"
35
+ end
36
+
37
+ # Cloak (CR 701.58a): manifest, with ward {2} while face down.
38
+ class Cloak < Manifest
39
+ primitive :effect, :cloak,
40
+ description: "Each referenced player (default you) cloaks the top card of their library `amount` times " \
41
+ "(default 1): it enters face down as a nameless colorless 2/2 creature with ward {2}. Its " \
42
+ "controller may turn it face up for its mana cost if it's a creature card.",
43
+ rules: %w[701.58a 701.58b 701.58e 701.58f 701.58g 708.2 708.3],
44
+ params: { "amount" => "amount (default 1)", "to" => "players (default you)" },
45
+ example: { "cloak" => {} }
46
+
47
+ def as = "cloak"
48
+ end
49
+
50
+ # Manifest dread (CR 701.62a): look at the top two cards of your library, manifest one of them, put the rest
51
+ # into your graveyard. The choice is an `effect_choice` among the cards looked at (only one card: no choice).
52
+ class ManifestDread < ChoiceEffect
53
+ primitive :effect, :manifest_dread,
54
+ description: "Each referenced player (default you) manifests dread: looks at the top two cards of their " \
55
+ "library, manifests one of them (decision `effect_choice`) and puts the other into their " \
56
+ "graveyard. Triggers on it (`keyword_action` \"manifest_dread\") see it after it's complete.",
57
+ rules: %w[701.62a 701.62b 701.40a 701.20e],
58
+ params: { "to" => "players (default you)" },
59
+ example: { "manifest_dread" => {} }
60
+
61
+ def self.keys = %w[to if]
62
+ def self.default_to = "you"
63
+
64
+ def run(engine, ctx, locals)
65
+ unless locals.key?("queue")
66
+ return :done unless condition.nil? || condition.true?(engine, ctx)
67
+
68
+ locals["queue"] = players(engine, ctx)
69
+ end
70
+ while (index = locals["queue"].first)
71
+ return :wait if dread!(engine, index, locals) == :wait
72
+
73
+ locals["queue"].shift
74
+ %w[answers looked asked].each { locals.delete(it) }
75
+ end
76
+ :done
77
+ end
78
+
79
+ def to_s = "manifest dread"
80
+
81
+ private
82
+
83
+ def dread!(engine, index, locals)
84
+ looked = locals["looked"] ||= engine.player(index).library.first(2).tap { engine.look_at!(index, index, it) }
85
+ chosen = looked.size == 1 ? looked.first : answers(locals).first&.fetch("object")
86
+ if chosen.nil? && looked.size == 2
87
+ return ask(engine, locals, index, "Manifest dread: choose the card to manifest; the other goes to your graveyard",
88
+ looked.map { { "type" => "manifest", "object" => it } })
89
+ end
90
+ engine.manifest!(index, "manifest", chosen) if chosen
91
+ (looked - [chosen]).each { engine.move!(it, :graveyard, reason: "manifest_dread") if engine.state.objects[it]&.zone == :library }
92
+ engine.emit Events::KeywordAction.new(player: engine.name(index), action: "manifest_dread", amount: 1) # CR 701.62b
93
+ end
94
+ end
95
+ end
96
+
97
+ module Abilities
98
+ # Morph (CR 702.37a) and disguise (CR 702.168a): "You may cast this card as a 2/2 face-down creature with no text,
99
+ # no name, no subtypes, and no mana cost [with ward {2}] by paying {3} rather than paying its mana cost." The cast
100
+ # option is `via: "alt:<ref>"` (a sorcery-speed creature spell); `cost` is what turning it face up costs later
101
+ # (special action `turn_face_up`, CR 702.37e, 702.168d).
102
+ class Morph < AlternativeCost
103
+ primitive :ability, :morph,
104
+ description: "Morph: you may cast this card face down as a nameless colorless 2/2 creature spell by " \
105
+ "paying {3} rather than its mana cost (cast option `via: \"alt:<ref>\"`). Any time you have " \
106
+ "priority you may turn the face-down permanent face up by paying `cost` (special action " \
107
+ "`{type: \"turn_face_up\", via: \"morph\"}`); abilities about entering don't trigger then.",
108
+ rules: %w[702.37a 702.37c 702.37d 702.37e 708.2 708.4 708.8 116.2b],
109
+ params: { "cost" => "mana cost string: the cost to turn it face up" },
110
+ example: { "morph" => { "cost" => "{1}{G}" } }
111
+
112
+ CAST_FACE_DOWN = "{3}"
113
+
114
+ attr_reader :face_up_cost
115
+
116
+ def initialize(costs, path, errors)
117
+ costs.each_index { |i| errors << "#{path}.cost[#{i}]: must be mana" unless costs[i].mana? }
118
+ @face_up_cost = costs.empty? ? nil : Mana.sum_costs(costs.select(&:mana?).map(&:mana_cost))
119
+ super([Costs::ManaPayment.build(CAST_FACE_DOWN, errors, path)], path, errors)
120
+ end
121
+
122
+ # Cast face down, a 2/2 creature spell without flash: at sorcery speed (CR 702.37c, 708.4).
123
+ def method_for(ref, **)
124
+ method = super
125
+ method.merge("sorcery" => true, "alt" => method["alt"].merge("face_down" => keyword))
126
+ end
127
+
128
+ def to_s = "#{keyword.capitalize} #{face_up_cost}"
129
+ end
130
+
131
+ # Disguise (CR 702.168a): morph with ward {2} while face down.
132
+ class Disguise < Morph
133
+ primitive :ability, :disguise,
134
+ description: "Disguise: you may cast this card face down as a nameless colorless 2/2 creature spell " \
135
+ "with ward {2} by paying {3} rather than its mana cost (cast option `via: \"alt:<ref>\"`). " \
136
+ "Any time you have priority you may turn the face-down permanent face up by paying `cost` " \
137
+ "(special action `{type: \"turn_face_up\", via: \"disguise\"}`).",
138
+ rules: %w[702.168a 702.168b 702.168c 702.168d 708.2 708.4 708.8 116.2b],
139
+ params: { "cost" => "mana cost string: the cost to turn it face up" },
140
+ example: { "disguise" => { "cost" => "{4}{G}" } }
141
+ end
142
+ end
143
+
144
+ module Triggers
145
+ # "When this creature is turned face up" (CR 708.8: turning it face up isn't entering the battlefield).
146
+ class TurnedFaceUp < Trigger
147
+ primitive :trigger, :turned_face_up,
148
+ description: "The subject (default this permanent) is turned face up. On a card with morph, disguise or " \
149
+ "that can be manifested, it's \"when this creature is turned face up\".",
150
+ rules: %w[708.8 702.37e 702.168d 603.2],
151
+ params: { "subject" => "\"self\" (default) or {\"each\": filter, \"controller\": ...}" },
152
+ example: { "turned_face_up" => {} }
153
+
154
+ def event_class = Events::TurnedFaceUp
155
+
156
+ def match?(engine, event, source)
157
+ obj = engine.state.objects[event.object] or return false
158
+ @subject.match?(engine, obj.id, obj.controller, obj.card, source)
159
+ end
160
+
161
+ def to_s = "When #{@subject} is turned face up"
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "alternative_costs"
4
+
5
+ module Enjine
6
+ module Mechanics
7
+ module Abilities
8
+ # Impending N—[cost] (CR 702.176a): "You may choose to pay [cost] rather than pay this spell's mana cost",
9
+ # "If you chose to pay this permanent's impending cost, it enters with N time counters on it", "As long as
10
+ # this permanent's impending cost was paid and it has a time counter on it, it's not a creature" and "At the
11
+ # beginning of your end step, remove a time counter from this permanent". The alternative cost is a cast
12
+ # option (`via: "alt:<ref>"`) that enters with the counters (the method's `enters_with`); the end step
13
+ # trigger comes with the keyword; not being a creature is a layer 4 change of the permanent itself
14
+ # (Engine::Characteristics#own_layers, CR 613.1d), which keeps applying even if it loses its abilities
15
+ # later in the layer order (CR 613.6). `{"cast_with": "impending"}` holds for it.
16
+ class Impending < AlternativeCost
17
+ primitive :ability, :impending,
18
+ description: "Impending `amount`—`cost`: you may cast this card by paying `cost` rather than its " \
19
+ "mana cost. If you do, it enters with `amount` time counters, and while its impending " \
20
+ "cost was paid and it has a time counter on it, it isn't a creature. At the beginning of " \
21
+ "your end step, remove a time counter from it (a triggered ability that comes with the " \
22
+ "keyword). Cast option `via: \"alt:<ref>\"`; `{\"cast_with\": \"impending\"}` reads it.",
23
+ rules: %w[702.176a 118.9 601.2b 601.2f 601.2h 613.1d 613.6 122.6],
24
+ params: { "amount" => "integer >= 1: the time counters", "cost" => "mana cost string, or a list of costs" },
25
+ example: { "impending" => { "amount" => 4, "cost" => "{1}{G}{G}" } }
26
+
27
+ END_STEP = { "triggered" => { "when" => { "step_begins" => { "step" => "end", "whose" => "your" } },
28
+ "effects" => [{ "remove_counters" => { "counter" => "time", "amount" => 1,
29
+ "to" => "self" } }] } }.freeze
30
+
31
+ attr_reader :amount, :linked_abilities
32
+
33
+ def self.build(params, errors, path, **)
34
+ Mechanics.check_keys(params, %w[amount cost], errors, path)
35
+ amount = Mechanics.fetch_param(params, :amount)
36
+ unless amount.is_a?(Integer) && amount.positive?
37
+ errors << "#{path}.amount: must be a positive integer"
38
+ amount = 1
39
+ end
40
+ new(read_costs(Mechanics.fetch_param(params, :cost), errors, path), path, errors, amount: amount)
41
+ end
42
+
43
+ # Whether `obj` is a permanent whose impending cost was paid and that has a time counter (CR 702.176a).
44
+ def self.not_creature?(obj)
45
+ obj.zone == :battlefield && obj.memory["cast_with"] == "impending" && obj.counters.fetch("time", 0).positive?
46
+ end
47
+
48
+ # "It's not a creature" as the permanent's own layer 4 change (Engine::Characteristics#own_layers, CR 613.1d),
49
+ # applied before resolved effects that change its types: its card types without creature (losing its
50
+ # creature types, CR 205.1a). Empty unless `not_creature?`.
51
+ def self.effects(engine, obj)
52
+ return [] unless not_creature?(obj)
53
+
54
+ types = engine.catalog.fetch(obj.card).types - [:creature] # printed: this is the layer pass itself
55
+ [{ "kind" => "set_types", "types" => types.map(&:to_s), "affected" => [obj.id] }]
56
+ end
57
+
58
+ def initialize(costs, path, errors, amount: 1)
59
+ @amount = amount
60
+ @linked_abilities = [Mechanics.build_entry(:ability, END_STEP, errors, "#{path}.end_step")].compact.freeze
61
+ super(costs, path, errors)
62
+ end
63
+
64
+ def allowed_types = %i[artifact creature enchantment planeswalker]
65
+
66
+ def method_for(ref, **)
67
+ method = super
68
+ method&.merge("enters_with" => { "time" => amount })
69
+ end
70
+
71
+ def to_s = "Impending #{amount}—#{costs.join(", ")}"
72
+ end
73
+ end
74
+ end
75
+ end
@@ -25,6 +25,8 @@ module Enjine
25
25
  } }.freeze
26
26
 
27
27
  def self.intrinsic_abilities = @intrinsic_abilities ||= [Keywords.ability_from_data(ABILITY, "increment")].freeze
28
+ # CR 702.191b: list it once per instance
29
+ def self.multiple_instances? = true
28
30
  end
29
31
  end
30
32
  end