enjine 0.4.1 → 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 (158) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +196 -1
  3. data/README.md +12 -3
  4. data/SUPPORTED.md +73 -38
  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 +146 -9
  9. data/docs/plans/dsl-gaps.md +23 -0
  10. data/docs/reference/abilities.md +299 -25
  11. data/docs/reference/costs.md +59 -6
  12. data/docs/reference/decisions.md +7 -1
  13. data/docs/reference/effects.md +200 -27
  14. data/docs/reference/filters.md +30 -3
  15. data/docs/reference/keywords.md +58 -2
  16. data/docs/reference/subtypes.md +18 -0
  17. data/docs/reference/triggers.md +94 -14
  18. data/docs/reference/types.md +20 -0
  19. data/lib/enjine/actions/play_land.rb +3 -1
  20. data/lib/enjine/actions/special_action.rb +4 -0
  21. data/lib/enjine/actions/turn_face_up.rb +25 -0
  22. data/lib/enjine/actions/unlock.rb +23 -0
  23. data/lib/enjine/card_definition.rb +54 -15
  24. data/lib/enjine/card_face_down.rb +31 -0
  25. data/lib/enjine/card_faces.rb +4 -2
  26. data/lib/enjine/card_rooms.rb +86 -0
  27. data/lib/enjine/catalog.rb +9 -3
  28. data/lib/enjine/cli/text.rb +11 -3
  29. data/lib/enjine/color_identity.rb +34 -1
  30. data/lib/enjine/decisions/casting.rb +6 -3
  31. data/lib/enjine/decisions/combat.rb +4 -2
  32. data/lib/enjine/decisions/damage_order.rb +18 -0
  33. data/lib/enjine/engine/ability_layers.rb +14 -1
  34. data/lib/enjine/engine/casting/alt_casting.rb +45 -5
  35. data/lib/enjine/engine/casting/cast_choices.rb +21 -2
  36. data/lib/enjine/engine/casting/casting.rb +42 -29
  37. data/lib/enjine/engine/casting/cost_options.rb +3 -1
  38. data/lib/enjine/engine/casting/costs.rb +8 -1
  39. data/lib/enjine/engine/casting/payment.rb +47 -9
  40. data/lib/enjine/engine/casting/permissions.rb +19 -17
  41. data/lib/enjine/engine/casting/target_counts.rb +14 -3
  42. data/lib/enjine/engine/casting/waterbend.rb +22 -2
  43. data/lib/enjine/engine/characteristics.rb +11 -11
  44. data/lib/enjine/engine/combat.rb +45 -14
  45. data/lib/enjine/engine/copies.rb +12 -4
  46. data/lib/enjine/engine/crimes.rb +46 -0
  47. data/lib/enjine/engine/damage.rb +85 -1
  48. data/lib/enjine/engine/entering.rb +36 -2
  49. data/lib/enjine/engine/face_down.rb +12 -3
  50. data/lib/enjine/engine/face_down_permanents.rb +108 -0
  51. data/lib/enjine/engine/faces.rb +20 -5
  52. data/lib/enjine/engine/follow_ups.rb +4 -1
  53. data/lib/enjine/engine/mana_sources.rb +18 -3
  54. data/lib/enjine/engine/options_memo.rb +140 -0
  55. data/lib/enjine/engine/permanent_designations.rb +29 -0
  56. data/lib/enjine/engine/resolution.rb +71 -6
  57. data/lib/enjine/engine/rooms.rb +86 -0
  58. data/lib/enjine/engine/rules.rb +6 -4
  59. data/lib/enjine/engine/state_based_actions.rb +25 -4
  60. data/lib/enjine/engine/triggers.rb +25 -20
  61. data/lib/enjine/engine/turn_history.rb +43 -5
  62. data/lib/enjine/engine/turn_structure.rb +1 -0
  63. data/lib/enjine/engine/zones.rb +37 -8
  64. data/lib/enjine/engine.rb +5 -1
  65. data/lib/enjine/events.rb +11 -0
  66. data/lib/enjine/frames.rb +5 -2
  67. data/lib/enjine/fuzz.rb +3 -0
  68. data/lib/enjine/game.rb +3 -1
  69. data/lib/enjine/mana.rb +3 -0
  70. data/lib/enjine/mechanics/abilities/chosen_values.rb +49 -6
  71. data/lib/enjine/mechanics/abilities/cost_changes.rb +32 -10
  72. data/lib/enjine/mechanics/abilities/damage_replacements.rb +137 -65
  73. data/lib/enjine/mechanics/abilities/designated.rb +57 -0
  74. data/lib/enjine/mechanics/abilities/entering_choices.rb +40 -21
  75. data/lib/enjine/mechanics/abilities/life_replacements.rb +74 -0
  76. data/lib/enjine/mechanics/abilities/mana_ability.rb +10 -4
  77. data/lib/enjine/mechanics/abilities/play_permissions.rb +52 -20
  78. data/lib/enjine/mechanics/abilities/player_rules.rb +30 -13
  79. data/lib/enjine/mechanics/abilities/stack_abilities.rb +12 -7
  80. data/lib/enjine/mechanics/abilities/static.rb +19 -6
  81. data/lib/enjine/mechanics/card_spec.rb +63 -11
  82. data/lib/enjine/mechanics/costs/base.rb +68 -9
  83. data/lib/enjine/mechanics/costs/behold.rb +28 -5
  84. data/lib/enjine/mechanics/costs/choose_creature_type.rb +43 -0
  85. data/lib/enjine/mechanics/costs/collect_evidence.rb +60 -0
  86. data/lib/enjine/mechanics/costs/exile_permanent.rb +41 -0
  87. data/lib/enjine/mechanics/costs/one_of.rb +3 -0
  88. data/lib/enjine/mechanics/costs/pay_life.rb +27 -8
  89. data/lib/enjine/mechanics/costs/remove_counters.rb +8 -5
  90. data/lib/enjine/mechanics/costs/tap.rb +44 -3
  91. data/lib/enjine/mechanics/effects/base.rb +36 -7
  92. data/lib/enjine/mechanics/effects/become.rb +29 -12
  93. data/lib/enjine/mechanics/effects/choose_cards.rb +44 -14
  94. data/lib/enjine/mechanics/effects/copy.rb +58 -21
  95. data/lib/enjine/mechanics/effects/counters.rb +18 -6
  96. data/lib/enjine/mechanics/effects/damage.rb +11 -0
  97. data/lib/enjine/mechanics/effects/delayed_triggers.rb +13 -6
  98. data/lib/enjine/mechanics/effects/earthbend.rb +2 -1
  99. data/lib/enjine/mechanics/effects/fight.rb +11 -0
  100. data/lib/enjine/mechanics/effects/library.rb +76 -18
  101. data/lib/enjine/mechanics/effects/may.rb +4 -2
  102. data/lib/enjine/mechanics/effects/pay.rb +27 -10
  103. data/lib/enjine/mechanics/effects/repeat.rb +92 -0
  104. data/lib/enjine/mechanics/effects/search.rb +34 -6
  105. data/lib/enjine/mechanics/effects/tokens.rb +42 -10
  106. data/lib/enjine/mechanics/filters/any_target.rb +6 -3
  107. data/lib/enjine/mechanics/filters/base.rb +36 -17
  108. data/lib/enjine/mechanics/filters/exile.rb +27 -0
  109. data/lib/enjine/mechanics/filters/permanents.rb +7 -0
  110. data/lib/enjine/mechanics/filters/stack.rb +14 -8
  111. data/lib/enjine/mechanics/keywords/alternative_costs.rb +80 -22
  112. data/lib/enjine/mechanics/keywords/cards_have.rb +67 -0
  113. data/lib/enjine/mechanics/keywords/craft.rb +132 -0
  114. data/lib/enjine/mechanics/keywords/cycling.rb +46 -7
  115. data/lib/enjine/mechanics/keywords/extort.rb +2 -0
  116. data/lib/enjine/mechanics/keywords/face_down.rb +165 -0
  117. data/lib/enjine/mechanics/keywords/flags.rb +3 -1
  118. data/lib/enjine/mechanics/keywords/hideaway.rb +6 -5
  119. data/lib/enjine/mechanics/keywords/impending.rb +75 -0
  120. data/lib/enjine/mechanics/keywords/increment.rb +2 -0
  121. data/lib/enjine/mechanics/keywords/infinity.rb +46 -0
  122. data/lib/enjine/mechanics/keywords/kicker.rb +24 -6
  123. data/lib/enjine/mechanics/keywords/miracle.rb +142 -0
  124. data/lib/enjine/mechanics/keywords/ninjutsu.rb +101 -0
  125. data/lib/enjine/mechanics/keywords/offspring.rb +48 -0
  126. data/lib/enjine/mechanics/keywords/persist_and_undying.rb +42 -0
  127. data/lib/enjine/mechanics/keywords/protection.rb +24 -8
  128. data/lib/enjine/mechanics/keywords/riot.rb +13 -10
  129. data/lib/enjine/mechanics/keywords/split_second.rb +29 -0
  130. data/lib/enjine/mechanics/keywords/storm_and_cascade.rb +5 -0
  131. data/lib/enjine/mechanics/keywords/void.rb +34 -0
  132. data/lib/enjine/mechanics/linked.rb +33 -3
  133. data/lib/enjine/mechanics/mana_spent.rb +1 -1
  134. data/lib/enjine/mechanics/selector.rb +62 -18
  135. data/lib/enjine/mechanics/subtypes/case.rb +68 -0
  136. data/lib/enjine/mechanics/triggers/base.rb +13 -4
  137. data/lib/enjine/mechanics/triggers/casting.rb +15 -9
  138. data/lib/enjine/mechanics/triggers/combat.rb +32 -12
  139. data/lib/enjine/mechanics/triggers/crime.rb +28 -0
  140. data/lib/enjine/mechanics/triggers/damage.rb +33 -18
  141. data/lib/enjine/mechanics/triggers/draw_and_discard.rb +19 -6
  142. data/lib/enjine/mechanics/triggers/keyword_actions.rb +17 -0
  143. data/lib/enjine/mechanics/triggers/rooms.rb +49 -0
  144. data/lib/enjine/mechanics/triggers/tap_and_counters.rb +20 -9
  145. data/lib/enjine/mechanics/triggers/zone_changes.rb +24 -9
  146. data/lib/enjine/mechanics/types/battle.rb +130 -0
  147. data/lib/enjine/mechanics/values.rb +84 -30
  148. data/lib/enjine/replacements.rb +21 -2
  149. data/lib/enjine/rules_monitor.rb +56 -71
  150. data/lib/enjine/snapshot/deep_copy.rb +23 -0
  151. data/lib/enjine/snapshot/player_checks.rb +10 -2
  152. data/lib/enjine/snapshot.rb +2 -1
  153. data/lib/enjine/state.rb +2 -2
  154. data/lib/enjine/verify.rb +10 -3
  155. data/lib/enjine/version.rb +1 -1
  156. data/lib/enjine/view.rb +50 -14
  157. data/lib/enjine.rb +1 -1
  158. metadata +34 -1
@@ -23,8 +23,9 @@ module Enjine
23
23
  # several effects apply. The engine applies them in the order that player would always choose:
24
24
  # effects that let a permanent stay ("instead remove all +1/+1 counters", a shield counter) before
25
25
  # effects that send it elsewhere ("exile it instead"), "+1 counter" bonuses before doubling, and
26
- # "enters untapped" after every "enters tapped". No pair implemented so far has two orders that
27
- # each benefit that player, so no decision is asked; a frame will when one does.
26
+ # "enters untapped" after every "enters tapped". No pair of these has two orders that each benefit
27
+ # that player, so no decision is asked. Damage is where orders matter: the affected player chooses
28
+ # one before the damage is dealt (Mechanics::DamageEvents, a damage_order decision).
28
29
  module Replacements
29
30
  module_function
30
31
 
@@ -36,6 +37,7 @@ module Enjine
36
37
  when "draw" then drawing!(engine, proposal)
37
38
  when "damage" then Mechanics::DamageEvents.replace!(engine, proposal) # abilities/damage_replacements.rb
38
39
  when "create_token" then creating_token!(engine, proposal)
40
+ when "life" then gaining_life!(engine, proposal)
39
41
  end
40
42
  Mechanics::Counters.replace!(engine, proposal) unless proposal.data["cancelled"]
41
43
  dying!(engine, proposal) if proposal.kind == "zone_change" && !proposal.data["cancelled"]
@@ -65,6 +67,22 @@ module Enjine
65
67
  proposal.data["count"] = ability.amount if ability
66
68
  end
67
69
 
70
+ # "If you would gain life, you gain twice that much / that much plus 1 instead" and "if an opponent would gain
71
+ # life, that player loses that much life instead" (CR 614.1a, 119.10). Each applies once (CR 614.5), in the
72
+ # order the affected player would always choose (CR 616.1): losing instead first (then nothing else applies,
73
+ # it's no longer a life gain), then adding, then multiplying.
74
+ def gaining_life!(engine, proposal)
75
+ data = proposal.data
76
+ return unless data["delta"].positive?
77
+
78
+ found = battlefield_abilities(engine, :replaces_gain?).select { |source, ability| ability.replaces_gain?(data["player"], source) }
79
+ loser = found.find { |_, ability| ability.lose? }
80
+ return data["delta"] = -data["delta"] if loser
81
+
82
+ doubling, adding = found.partition { |_, ability| ability.multiply }
83
+ (adding + doubling).each { |source, ability| data["delta"] = ability.gained(engine, data["delta"], source) }
84
+ end
85
+
68
86
  # "If one or more tokens would be created under your control, that many [tokens] are created
69
87
  # instead" (CR 614.1a, 111.1).
70
88
  def creating_token!(engine, proposal)
@@ -170,6 +188,7 @@ module Enjine
170
188
  engine.chars(obj).subtypes.each do |sub| # e.g. a Saga enters with a lore counter (CR 715.3a)
171
189
  mod = Enjine.registry.fetch(:subtype, sub)
172
190
  mod.entering(engine.definition(obj), proposal.data["enters"]) if mod.respond_to?(:entering)
191
+ mod.entering_for(engine, controller, enters) if mod.respond_to?(:entering_for) # a Siege's protector (CR 310.12)
173
192
  end
174
193
  entering_status!(engine, obj, controller, facts, enters)
175
194
  end
@@ -6,6 +6,10 @@ module Enjine
6
6
  # decision, without calling the engine's own rules code. Any mismatch is a
7
7
  # finding: the engine did something the rules don't allow.
8
8
  #
9
+ # What a permanent is (its types, keywords and abilities: layers 1-6, CR 613.1a-f) is read from the engine's
10
+ # layer pass, so the checks agree with copy effects, type changes, keyword counters and timestamps. What follows
11
+ # from it is recomputed here: layer 7 power/toughness, land plays, hand size, loyalty costs, combat legality.
12
+ #
9
13
  # monitor = RulesMonitor.new(game)
10
14
  # game.submit(option); monitor.check!(entry) # or use Fuzz.run(..., monitor: true)
11
15
  # monitor.findings # => [] when everything matched
@@ -24,6 +28,7 @@ module Enjine
24
28
  @loyalty_activations = Hash.new(0)
25
29
  @commander_casts = Hash.new(0)
26
30
  @cast_from_command = []
31
+ @x_announced = {}
27
32
  end
28
33
 
29
34
  def state = @game.state
@@ -44,6 +49,10 @@ module Enjine
44
49
  end
45
50
 
46
51
  def defn(card) = catalog.fetch(card)
52
+ def engine = @game.instance_variable_get(:@engine)
53
+
54
+ # Its characteristics after layers 1-6 (the engine's layer pass; power and toughness aren't read from it).
55
+ def current(obj) = engine.chars(obj)
47
56
 
48
57
  # A permanent's printed abilities, the abilities of its unlocked symbols in their place.
49
58
  def abilities_of(obj)
@@ -81,6 +90,7 @@ module Enjine
81
90
  when Events::AttackerDeclared then attacker_declared!(event)
82
91
  when Events::BlockerDeclared then blocker_declared!(event)
83
92
  when Events::CountersChanged then @stats["counters_#{event.counter}"] += event.to - event.from
93
+ when Events::XAnnounced then @x_announced[event.object] = event.value
84
94
  end
85
95
  end
86
96
 
@@ -128,18 +138,12 @@ module Enjine
128
138
  finding("305.1", "#{player} played a land on #{active}'s turn") if player != active
129
139
  end
130
140
 
131
- # CR 305.2: "you may play an additional land" abilities of the player's permanents, from the raw card data
132
- # and from resolved effects that granted them (Effects::Become).
141
+ # CR 305.2: "you may play an additional land" abilities of the player's permanents: printed, unlocked (a
142
+ # station symbol, a class level, CR 722.2a, 717.2a) or granted (Effects::Become), as the layers give them.
133
143
  def additional_land_plays(index)
134
144
  state.battlefield.sum do |id|
135
145
  obj = state.objects[id]
136
- next 0 unless obj.controller == index
137
-
138
- printed = defn(obj.card).abilities.count { it.kind == :additional_land_plays }
139
- granted = state.effects.select { it["kind"] == "abilities" && it["affected"].include?(id) }.sum do |effect|
140
- defn(effect["card"]).effect_granted(effect["ref"]).count { it.ability.kind == :additional_land_plays }
141
- end
142
- printed + granted
146
+ obj.controller == index ? current(obj).abilities.count { it.ability.kind == :additional_land_plays } : 0
143
147
  end
144
148
  end
145
149
 
@@ -151,7 +155,7 @@ module Enjine
151
155
  return if card.instant_speed? || event.during_resolution # CR 608.2g: timing doesn't apply
152
156
 
153
157
  caster = event.player
154
- return if sneaked?(event, caster)
158
+ return if sneaked?(event, caster) || flashed_by_cost?(event, card)
155
159
 
156
160
  # The stack was empty as casting began (CR 601.2a) when the spell is at its bottom; what went on it since
157
161
  # ("whenever you cast" triggers, even one still waiting for its targets, CR 603.3d) doesn't count.
@@ -161,21 +165,21 @@ module Enjine
161
165
  end
162
166
  end
163
167
 
168
+ # CR 601.3c: "you may cast this spell as though it had flash if you [pay an optional additional cost]", paid.
169
+ def flashed_by_cost?(event, card)
170
+ paid = state.objects[event.object]&.memory&.[]("kicked_with") or return false
171
+ card.ability_instances.any? { it.ability.respond_to?(:flash?) && it.ability.flash? && it.ability.keyword == paid }
172
+ end
173
+
164
174
  # CR 702.190a: a spell cast for its sneak cost, at instant speed during its caster's declare blockers step.
165
175
  def sneaked?(event, caster)
166
176
  state.objects[event.object]&.memory&.[]("cast_with") == "sneak" && state.step == :declare_blockers &&
167
177
  state.players[state.active].name == caster
168
178
  end
169
179
 
170
- # Printed keywords plus those of unlocked symbols, keywords granted and those of statics. A permanent that
171
- # loses all abilities (CR 613.1f) has only the keywords granted to it (timestamps are the engine's specs').
172
- def keyword?(obj, keyword)
173
- return granted_keywords(obj.id).include?(keyword.to_sym) if stripped?(obj)
174
-
175
- defn(obj.card).keyword?(keyword) || unlocked(obj).any? { it.keywords.include?(keyword) } ||
176
- granted_keywords(obj.id).include?(keyword.to_sym) || static_keyword?(obj, keyword) ||
177
- (keyword.to_sym == :haste && obj.memory["haste_while_controlled_by"] == obj.controller) # cast with suspend, CR 702.62a
178
- end
180
+ # Its keywords after layer 6 (CR 613.1f): printed, a copy's exceptions (CR 707.9b), keyword counters
181
+ # (CR 122.1b), granted and lost in timestamp order (CR 613.7).
182
+ def keyword?(obj, keyword) = current(obj).keyword?(keyword.to_sym)
179
183
 
180
184
  # Whether an effect or an applying static ability makes it lose all abilities (CR 613.1f).
181
185
  def stripped?(obj)
@@ -187,10 +191,9 @@ module Enjine
187
191
  # Permanents and emblems, whose static abilities function in the command zone (CR 114.4).
188
192
  def static_holders = state.battlefield + state.emblem_ids
189
193
 
190
- # The static abilities of permanents on the battlefield and emblems that apply to `obj` now, matched on printed types.
194
+ # The static abilities of permanents on the battlefield and emblems that apply to `obj` now.
191
195
  def applying_statics(obj)
192
- engine = @game.instance_variable_get(:@engine)
193
- facts = facts(obj, defn(obj.card))
196
+ facts = facts(obj)
194
197
  static_holders.flat_map do |id|
195
198
  source = state.objects[id]
196
199
  abilities_of(source).select do |ability|
@@ -199,12 +202,6 @@ module Enjine
199
202
  end
200
203
  end
201
204
 
202
- # Keywords granted until end of turn or by an attached Aura or Equipment (CR 301.5f, 303.4m).
203
- def granted_keywords(id)
204
- until_eot = state.effects.select { it["kind"] == "grant" && it["affected"].include?(id) }.flat_map { it["keywords"] }
205
- (until_eot + attached_grants(id).flat_map { it["keywords"] }).map(&:to_sym)
206
- end
207
-
208
205
  def ability_activated!(event)
209
206
  source = defn(event.card)
210
207
  ability = source.ability(event.ability)
@@ -217,10 +214,10 @@ module Enjine
217
214
  end
218
215
  changes = @entry.events.grep(Events::CountersChanged).select { it.object == event.source && it.counter == "loyalty" }
219
216
  paid = changes.sum { it.to - it.from }
220
- return unless paid != ability.loyalty_cost && state.objects.key?(event.source) && changes.any?
217
+ cost = ability.x_loyalty? ? -@x_announced.fetch(event.object, 0) : ability.loyalty_cost # CR 107.3: "-X", X announced
218
+ return unless paid != cost && state.objects.key?(event.source) && changes.any?
221
219
 
222
- finding("606.4",
223
- "#{event.source} loyalty changed by #{paid}, cost was #{ability.loyalty_cost}")
220
+ finding("606.4", "#{event.source} loyalty changed by #{paid}, cost was #{cost}")
224
221
  end
225
222
 
226
223
  # CR 302.6 / 508.1a: attackers were untapped and not summoning sick (or have haste).
@@ -236,9 +233,6 @@ module Enjine
236
233
  finding("702.3b", "#{event.object} has defender but attacked")
237
234
  end
238
235
 
239
- # A keyword from an applying static ability of a permanent (CR 604.2), matched on printed types.
240
- def static_keyword?(obj, keyword) = applying_statics(obj).any? { it.keywords.include?(keyword) }
241
-
242
236
  # CR 702.9b: a creature with flying can be blocked only by creatures with flying or reach.
243
237
  def blocker_declared!(event)
244
238
  blocker = state.objects[event.object]
@@ -278,8 +272,9 @@ module Enjine
278
272
  end
279
273
  end
280
274
 
281
- # CR 613.4, recomputed independently: printed (or a 7b setting effect) + counters + active pump effects
282
- # + static ability bonuses. Characteristic-defining power/toughness (7a) is left to the engine's specs.
275
+ # CR 613.4, recomputed independently: printed (or a 7b setting effect) + power/toughness counters (CR 122.1a)
276
+ # + active pump effects + static ability bonuses. Characteristic-defining power/toughness (7a) is left to the
277
+ # engine's specs.
283
278
  def check_power_toughness!
284
279
  return if state.battlefield.any? { stripped?(state.objects[it]) } # CR 613.1f, 613.8a: the engine's specs
285
280
 
@@ -290,28 +285,20 @@ module Enjine
290
285
  card = defn(obj.card)
291
286
  next unless recomputable?(obj, card)
292
287
 
293
- net = obj.counters.fetch("+1/+1", 0) - obj.counters.fetch("-1/-1", 0)
288
+ net = Mechanics::Counters.power_toughness(obj.counters)
294
289
  pumps = state.effects.select { it["kind"] == "pump" && it["affected"].include?(id) } + attached_grants(id) +
295
- static_bonuses(obj, card, statics)
290
+ static_bonuses(obj, statics)
296
291
  base = base_power_toughness(obj, card)
297
- expected = [base[0] + net + pumps.sum { it["power"] }, base[1] + net + pumps.sum { it["toughness"] }]
292
+ expected = [base[0] + net[0] + pumps.sum { it["power"] }, base[1] + net[1] + pumps.sum { it["toughness"] }]
298
293
  actual = view.fetch(id).values_at("power", "toughness")
299
294
  finding("613.4c", "#{id} is #{actual.join("/")}, expected #{expected.join("/")}") if actual != expected
300
295
  end
301
296
  end
302
297
 
303
- # A creature whose power/toughness isn't characteristic-defining (7a) or set by a static ability (7b).
304
- # One whose creature types an effect replaced (assimilate, "becomes a Borg", layer 4) is left to the engine's
305
- # specs: which statics apply to it depends on those types.
298
+ # A printed creature that is still a creature after layer 4 (one that isn't has no power or toughness,
299
+ # CR 208.3), whose power/toughness isn't characteristic-defining (7a) or set by a static ability (7b).
306
300
  def recomputable?(obj, card)
307
- card.creature? && !card.defined_pt? && applying_statics(obj).none?(&:base_power_toughness) && !types_replaced?(obj)
308
- end
309
-
310
- # Creature types or card types an effect or static replaced (layer 4, CR 205.1a): the monitor matches statics
311
- # against printed types, and a permanent that is no longer a creature has no power or toughness.
312
- def types_replaced?(obj)
313
- applying_statics(obj).any?(&:set_types) ||
314
- state.effects.any? { %w[creature_types assimilate set_types].include?(it["kind"]) && Array(it["affected"]).include?(obj.id) }
301
+ card.creature? && current(obj).creature? && !card.defined_pt? && applying_statics(obj).none?(&:base_power_toughness)
315
302
  end
316
303
 
317
304
  # CR 613.4b: the latest setting effect, else as printed, or as a copy's exceptions or a token's creating
@@ -326,9 +313,8 @@ module Enjine
326
313
 
327
314
  # Static abilities printed on permanents that give a bonus to more than the attached permanent and
328
315
  # apply now: [[source object, ability, [power, toughness]], ...]. Conditions and amounts are the
329
- # primitives' own; which creatures get the bonus is decided here from printed types and raw counters.
316
+ # primitives' own; which creatures get the bonus is decided here (#facts).
330
317
  def static_pumps
331
- engine = @game.instance_variable_get(:@engine)
332
318
  static_holders.flat_map do |id|
333
319
  source = state.objects[id]
334
320
  abilities_of(source).filter_map do |ability|
@@ -339,34 +325,33 @@ module Enjine
339
325
  end
340
326
  end
341
327
 
342
- def static_bonuses(obj, card, statics)
343
- facts = facts(obj, card)
328
+ def static_bonuses(obj, statics)
329
+ facts = facts(obj)
344
330
  statics.filter_map do |source, ability, (power, toughness)|
345
331
  { "power" => power, "toughness" => toughness } if ability.affects?(obj, source, facts)
346
332
  end
347
333
  end
348
334
 
349
- # What statics match a permanent on: its printed characteristics, every creature type for a changeling
350
- # (CR 702.73a) and the keywords it was granted (a bonus to creatures "with flying", CR 702.1).
351
- def facts(obj, card)
352
- chars = Mechanics::Keywords::Changeling.apply(card.printed_characteristics)
353
- chars = chars.with(keywords: chars.keywords | granted_keywords(obj.id))
354
- # A crewed Vehicle is an artifact creature (CR 702.122b): the types an effect adds count.
355
- added = state.effects.select { it["kind"] == "add_types" && it["affected"].include?(obj.id) }.flat_map { it["types"] }
356
- chars = chars.with(types: chars.types | added.map(&:to_sym)) unless added.empty?
357
- Mechanics::Filters::Facts.new(chars: chars, counters: obj.counters, token: card.token?)
335
+ # What statics match a permanent on: its types and keywords after layers 1-6 ("becomes a Bear in addition to
336
+ # its other types", CR 205.1b; a changeling's every creature type, CR 702.73a; a bonus to creatures "with
337
+ # flying", CR 702.1), its printed power and toughness (not the layer 7 result being checked), its counters and
338
+ # whether it's a token, and its combat and tapped status ("attacking creatures you control get +1/+0", CR 506.4).
339
+ def facts(obj)
340
+ card = defn(obj.card)
341
+ chars = current(obj).with(power: card.power, toughness: card.toughness)
342
+ status = { "attacking" => state.attackers.key?(obj.id), "blocking" => state.blockers.key?(obj.id), "tapped" => obj.tapped }
343
+ Mechanics::Filters::Facts.new(chars: chars, counters: obj.counters, token: engine.token?(obj), status: status)
358
344
  end
359
345
 
360
346
  # "Equipped/enchanted creature gets/has ..." of every permanent attached to `id` whose condition holds
361
- # ("during your turn", CR 604.2): [{"power" => n, "toughness" => n, "keywords" => [...]}].
347
+ # ("during your turn", CR 604.2): [{"power" => n, "toughness" => n}].
362
348
  def attached_grants(id)
363
- engine = @game.instance_variable_get(:@engine)
364
349
  state.battlefield.filter_map { state.objects[it] if state.objects[it].attached_to == id }.flat_map do |att|
365
350
  abilities_of(att).filter_map do |ability|
366
351
  next unless ability.kind == :static && ability.attached? && ability.active?(engine, att)
367
352
 
368
353
  power, toughness = ability.bonus(engine, att)
369
- { "power" => power, "toughness" => toughness, "keywords" => ability.keywords }
354
+ { "power" => power, "toughness" => toughness }
370
355
  end
371
356
  end
372
357
  end
@@ -397,8 +382,7 @@ module Enjine
397
382
  end
398
383
  end
399
384
 
400
- # With replaced creature types, a static keyed on them may grant it.
401
- def indestructible?(obj) = keyword?(obj, :indestructible) || types_replaced?(obj)
385
+ def indestructible?(obj) = keyword?(obj, :indestructible)
402
386
 
403
387
  # CR 514.1: after cleanup the active player has at most seven cards (checked at the next turn's priority).
404
388
  # The player whose turn ended is the one active before (an extra turn may follow its own, CR 500.7).
@@ -413,15 +397,16 @@ module Enjine
413
397
  finding("514.1", "#{previous.name} ended their turn with #{previous.hand.size} cards") if previous.hand.size > 7
414
398
  end
415
399
 
416
- # CR 402.2: an effect or a permanent's ability may give a player no maximum hand size.
400
+ # CR 402.2: an effect, a permanent's ability or an emblem's (CR 114.4) may give a player no maximum hand size.
417
401
  def no_max_hand_size?(name)
418
402
  ref = "player:#{name}"
419
403
  return true if state.effects.any? { it["rule"] == "no_max_hand_size" && it["affected"].include?(ref) }
420
404
 
421
405
  index = state.players.index { it.name == name }
422
- state.battlefield.any? do |id|
406
+ static_holders.any? do |id|
423
407
  obj = state.objects[id]
424
- defn(obj.card).abilities.any? do |ability|
408
+ current(obj).abilities.any? do |inst|
409
+ ability = inst.ability
425
410
  ability.kind == :player_rule && ability.rule == "no_max_hand_size" && ability.applies_to?(index, obj.controller)
426
411
  end
427
412
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enjine
4
+ # Deep copies of state data (Snapshot.load, State#to_h).
5
+ module Snapshot
6
+ module_function
7
+
8
+ # An independent deep copy, equal to a Marshal round trip of the value (every String a fresh unfrozen copy) but
9
+ # several times faster on the plain data state holds: Hashes, Arrays, Strings, numbers, symbols, true, false, nil.
10
+ # Anything else (a subclass, a Hash with a default or compared by identity) takes the Marshal round trip.
11
+ def deep_copy(value)
12
+ if value.instance_of?(Hash) && value.default.nil? && value.default_proc.nil? && !value.compare_by_identity?
13
+ value.to_h { |k, v| [deep_copy(k), deep_copy(v)] }
14
+ elsif value.instance_of?(Array) then value.map { deep_copy(it) }
15
+ elsif value.instance_of?(String) then value.dup
16
+ elsif PLAIN.any? { value.instance_of?(it) } then value
17
+ else Marshal.load(Marshal.dump(value))
18
+ end
19
+ end
20
+
21
+ PLAIN = [Integer, Float, Symbol, TrueClass, FalseClass, NilClass].freeze
22
+ end
23
+ end
@@ -10,8 +10,10 @@ module Enjine
10
10
  def validate_object_id(obj, path, catalog, next_id, errors)
11
11
  id = obj["id"]
12
12
  match = /\A([a-z0-9_]+)#(\d+)\z/.match(id.to_s)
13
- own = (obj["memory"].is_a?(Hash) && obj["memory"]["own_card"]) || obj["card"]
14
- face_down = match && match[1] == Engine::FACE_DOWN_ID && obj["zone"].to_s == "exile" && obj.dig("memory", "face_down") == true
13
+ own = (obj["memory"].is_a?(Hash) && (obj["memory"]["face_down_card"] || obj["memory"]["own_card"])) || obj["card"]
14
+ memory = obj["memory"].is_a?(Hash) ? obj["memory"] : {}
15
+ face_down = match && match[1] == Engine::FACE_DOWN_ID &&
16
+ ((obj["zone"].to_s == "exile" && memory["face_down"] == true) || hidden_on_board?(obj, memory))
15
17
  named = own.to_s.split(CardDefinition::FACE_SEPARATOR).first
16
18
  errors << "#{path}.id: malformed #{id.inspect}" unless match && (match[1] == named || face_down)
17
19
  errors << "#{path}.memory.own_card: #{own.inspect} is not in the catalog" unless catalog.include?(own.to_s)
@@ -19,6 +21,12 @@ module Enjine
19
21
  errors << "#{path}.card: #{obj["card"].inspect} is not in the catalog" unless catalog.include?(obj["card"].to_s)
20
22
  end
21
23
 
24
+ # A face-down spell or permanent, or one turned face up (the same object, CR 708.8): its id doesn't name its
25
+ # card (Engine::FaceDownPermanents).
26
+ def hidden_on_board?(obj, memory)
27
+ %w[battlefield stack phased_out].include?(obj["zone"].to_s) && (memory.key?("face_down_card") || memory.key?("turned_face_up"))
28
+ end
29
+
22
30
  # Cards outside the game (CR 400.11a): definition ids only (they aren't objects, CR 400.11), each in the
23
31
  # catalog; absent in older snapshots. `outside_lists` holds what each player started with.
24
32
  def validate_outside(data, catalog, errors)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "snapshot/turn_checks"
4
4
  require_relative "snapshot/player_checks"
5
+ require_relative "snapshot/deep_copy"
5
6
 
6
7
  module Enjine
7
8
  # Converts State to and from plain data, with complete consistency validation.
@@ -85,7 +86,7 @@ module Enjine
85
86
  phased_out: hash["phased_out"]&.dup)
86
87
  end
87
88
 
88
- def deep_copy(value) = Marshal.load(Marshal.dump(value))
89
+ # deep_copy: see snapshot/deep_copy.rb
89
90
 
90
91
  def build_player(hash)
91
92
  PlayerState.new(**PLAYER_KEYS.to_h { |k| [k.to_sym, hash[k]] }).tap do |pl|
data/lib/enjine/state.rb CHANGED
@@ -15,7 +15,7 @@ module Enjine
15
15
  "tapped" => tapped, "controlled_since" => controlled_since, "kind" => kind, "counters" => counters.dup,
16
16
  "damage" => damage, "source" => source, "ability" => ability, "targets" => targets&.dup,
17
17
  "loyalty_turn" => loyalty_turn, "deathtouched" => deathtouched,
18
- "modes" => modes&.dup, "memory" => Marshal.load(Marshal.dump(memory || {})), "x" => x,
18
+ "modes" => modes&.dup, "memory" => Snapshot.deep_copy(memory || {}), "x" => x,
19
19
  "mana_spent" => mana_spent&.dup, "attached_to" => attached_to, "paid" => paid&.map(&:dup),
20
20
  "phased_out" => phased_out&.dup }
21
21
  end
@@ -53,7 +53,7 @@ module Enjine
53
53
 
54
54
  private
55
55
 
56
- def deep(value) = Marshal.load(Marshal.dump(value))
56
+ def deep(value) = Snapshot.deep_copy(value)
57
57
 
58
58
  # Fields left out while empty.
59
59
  def optional_h
data/lib/enjine/verify.rb CHANGED
@@ -131,11 +131,11 @@ module Enjine
131
131
  end
132
132
  end
133
133
 
134
- # Decks that together include every card in `cards`: grouped by the colors each needs (a land: the colors it
135
- # makes), a few spells and lands per deck. Colorless groups get random colors.
134
+ # Decks that together include every card in `cards`: grouped by the colors each needs, a few spells and lands
135
+ # per deck. Colorless groups get random colors.
136
136
  def coverage_decks(cards)
137
137
  defns = cards.map(&:definition).reject { DeckBuilder.basic?(it) } # every deck has them
138
- groups = defns.group_by { it.land? ? DeckBuilder.produced_colors(it) : DeckBuilder.required_colors(it) }
138
+ groups = defns.group_by { deck_colors(it) }
139
139
  groups.sort_by { |colors, _| colors.join }.flat_map do |colors, defns|
140
140
  lands, spells = defns.partition(&:land?)
141
141
  spell_chunks = spells.each_slice(SPELLS_PER_DECK).to_a
@@ -147,6 +147,13 @@ module Enjine
147
147
  end.compact
148
148
  end
149
149
 
150
+ # The colors a deck needs to hold the card (DeckBuilder's fit check): a spell's colors and colored activation
151
+ # costs; a land's also the colors it makes ({C} lands with a colored ability or cycling cost need that color).
152
+ def deck_colors(defn)
153
+ needed = DeckBuilder.required_colors(defn)
154
+ defn.land? ? Mana::COLORS & (needed | DeckBuilder.produced_colors(defn)) : needed
155
+ end
156
+
150
157
  def coverage_deck(colors, including)
151
158
  @builder.build(colors: colors, seed: @rng.rand(2**31), profile: PROFILE, including: including)
152
159
  rescue Error, ArgumentError => e
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enjine
4
- VERSION = "0.4.1"
4
+ VERSION = "0.6.0"
5
5
  # Comprehensive Rules document this engine follows.
6
6
  RULES_VERSION = "2026-11-06"
7
7
  end
data/lib/enjine/view.rb CHANGED
@@ -8,7 +8,10 @@ module Enjine
8
8
  module View
9
9
  module_function
10
10
 
11
- def build(engine, viewer)
11
+ # `options`: something callable returning the pending decision's legal options when the caller already has them
12
+ # (Game#view_for passes its memoized decision's), so a view doesn't generate them all again; computed here otherwise.
13
+ # Called only for a view that shows the options.
14
+ def build(engine, viewer, options: nil)
12
15
  state = engine.state
13
16
  observer = viewer == :observer
14
17
  {
@@ -19,12 +22,12 @@ module Enjine
19
22
  "phase" => state.step && Steps.fetch(state.step).phase.to_s,
20
23
  "priority" => state.priority && engine.name(state.priority),
21
24
  "players" => state.players.map { |pl| player_view(engine, pl, observer || pl.name == viewer, observer) },
22
- "battlefield" => state.battlefield.map { object_view(engine, it) },
23
- "stack" => state.stack.map { object_view(engine, it) },
25
+ "battlefield" => state.battlefield.map { board_view(engine, it, viewer) },
26
+ "stack" => state.stack.map { board_view(engine, it, viewer) },
24
27
  "exile" => state.exile.map { exiled_view(engine, it, viewer) },
25
- "phased_out" => state.phased_out.map { object_view(engine, it) },
28
+ "phased_out" => state.phased_out.map { board_view(engine, it, viewer) },
26
29
  "command" => state.command.map { command_view(engine, it) },
27
- "pending" => pending_view(engine, viewer),
30
+ "pending" => pending_view(engine, viewer, options),
28
31
  "result" => state.result&.dup,
29
32
  "effects" => state.effects.map { it.except("controller").merge("controller" => engine.name(it["controller"])) },
30
33
  "delayed_triggers" => state.delayed_triggers.map { delayed_view(engine, it) }
@@ -95,7 +98,7 @@ module Enjine
95
98
  def outside_view(engine, player)
96
99
  player.outside.map do |card|
97
100
  defn = engine.catalog.fetch(card)
98
- { "card" => card, "name" => defn.name, "type_line" => defn.type_line, "mana_cost" => defn.mana_cost&.to_s }.compact
101
+ { "card" => card, "name" => defn.name, "type_line" => defn.type_line, "mana_cost" => defn.mana_cost_text }.compact
99
102
  end
100
103
  end
101
104
 
@@ -111,10 +114,25 @@ module Enjine
111
114
  return object_view(engine, id) unless engine.face_down?(obj)
112
115
 
113
116
  hidden = { "id" => id, "face_down" => true, "owner" => engine.name(obj.owner) }
114
- looks = viewer == :observer || obj.memory.fetch("may_look", []).map { engine.name(it) }.include?(viewer)
117
+ looks = viewer == :observer || ((index = engine.names.index(viewer)) && engine.may_look?(obj, index))
115
118
  looks ? object_view(engine, id).merge(hidden) : hidden
116
119
  end
117
120
 
121
+ # A spell or permanent. A face-down one (CR 708) shows its face-down characteristics to everyone; only its
122
+ # controller (CR 708.5) and an observer also see which card it is.
123
+ def board_view(engine, id, viewer)
124
+ view = object_view(engine, id)
125
+ obj = engine.object(id)
126
+ return view unless !obj.ability? && engine.face_down_object?(obj)
127
+
128
+ view["face_down"] = true
129
+ index = engine.names.index(viewer)
130
+ return view unless viewer == :observer || (index && engine.sees_face_down?(obj, index))
131
+
132
+ card = obj.memory["face_down_card"]
133
+ view.merge("face_down_card" => card, "face_down_name" => engine.catalog.fetch(card).name)
134
+ end
135
+
118
136
  def object_view(engine, id)
119
137
  obj = engine.object(id)
120
138
  defn = engine.definition(obj)
@@ -122,8 +140,8 @@ module Enjine
122
140
 
123
141
  view = { "id" => id, "card" => obj.card, "name" => defn.name, "type_line" => defn.type_line,
124
142
  "owner" => engine.name(obj.owner) }
125
- view["mana_cost"] = defn.mana_cost.to_s if defn.mana_cost
126
- view["keywords"] = defn.keywords.map(&:to_s) unless defn.keywords.empty?
143
+ view["mana_cost"] = defn.mana_cost_text if defn.mana_cost
144
+ view["keywords"] = defn.keywords.map(&:name) unless defn.keywords.empty?
127
145
  view["rules_text"] = defn.rules_text if defn.rules_text
128
146
  case obj.zone
129
147
  when :battlefield then permanent_view(engine, obj, defn, view)
@@ -135,6 +153,15 @@ module Enjine
135
153
  view
136
154
  end
137
155
 
156
+ # A planeswalker's loyalty; a battle's defense and protector (CR 306.5b, 310.4c, 310.9).
157
+ def loyalty_and_defense(engine, obj, defn, view)
158
+ view["loyalty"] = engine.loyalty(obj) if defn.planeswalker?
159
+ return unless defn.type?(:battle)
160
+
161
+ view["defense"] = obj.counters.fetch("defense", 0)
162
+ view["protector"] = engine.name(engine.protector(obj)) if engine.protector(obj)
163
+ end
164
+
138
165
  def permanent_view(engine, obj, defn, view)
139
166
  view["controller"] = engine.name(obj.controller)
140
167
  view["tapped"] = obj.tapped
@@ -146,17 +173,26 @@ module Enjine
146
173
  view.merge!("power" => power, "toughness" => toughness, "damage" => obj.damage,
147
174
  "summoning_sick" => engine.summoning_sick_for_tap?(obj))
148
175
  end
149
- view["loyalty"] = engine.loyalty(obj) if defn.planeswalker?
176
+ loyalty_and_defense(engine, obj, defn, view)
150
177
  view["counters"] = obj.counters.reject { |_, n| n.zero? } unless obj.counters.values.all?(&:zero?)
151
178
  view["attached_to"] = obj.attached_to if obj.attached_to
152
- view["transformed"] = true if engine.transformed?(obj) # CR 701.27h
153
- view["prepared"] = true if engine.prepared?(obj) # CR 723.3a
179
+ designations_view(engine, obj, view)
154
180
  if (attack = engine.attackers[obj.id])
155
181
  view["attacking"] = attack["target"]
156
182
  end
157
183
  view["blocking"] = engine.state.blockers[obj.id] if engine.state.blockers.key?(obj.id)
158
184
  end
159
185
 
186
+ # A permanent's status: transformed, prepared, its designations (solved, CR 720.3b; harnessed, CR 701.64b) and a
187
+ # Room's unlocked doors (CR 709.5c).
188
+ def designations_view(engine, obj, view)
189
+ view["transformed"] = true if engine.transformed?(obj) # CR 701.27h
190
+ view["prepared"] = true if engine.prepared?(obj) # CR 723.3a
191
+ marks = engine.permanent_designations(obj)
192
+ view["designations"] = marks unless marks.empty?
193
+ view["unlocked"] = engine.unlocked_doors(obj) if engine.room?(obj)
194
+ end
195
+
160
196
  def ability_view(engine, obj, defn)
161
197
  ability = defn.ability(obj.ability)
162
198
  { "id" => obj.id, "card" => obj.card, "name" => "#{defn.name} ability", "kind" => "ability",
@@ -164,7 +200,7 @@ module Enjine
164
200
  "controller" => engine.name(obj.controller), "targets" => (obj.targets || []).flatten }.compact
165
201
  end
166
202
 
167
- def pending_view(engine, viewer)
203
+ def pending_view(engine, viewer, options = nil)
168
204
  pending = engine.state.pending
169
205
  return nil unless pending
170
206
 
@@ -172,7 +208,7 @@ module Enjine
172
208
  view = pending.merge("player" => decider)
173
209
  return view.except("choices") unless viewer == :observer || viewer == decider # e.g. the cards a search sees
174
210
 
175
- view["options"] = engine.options.map { it.transform_keys(&:to_s) }
211
+ view["options"] = (options ? options.call : engine.options).map { it.transform_keys(&:to_s) }
176
212
  view
177
213
  end
178
214
  end
data/lib/enjine.rb CHANGED
@@ -28,7 +28,7 @@ require_relative "enjine/frames"
28
28
  require_relative "enjine/decisions"
29
29
  Dir[File.join(__dir__, "enjine/decisions/*.rb")].each { |f| require f }
30
30
  require_relative "enjine/actions"
31
- %w[pass play_land activate_mana_ability cast special_action].each { |f| require_relative "enjine/actions/#{f}" }
31
+ %w[pass play_land activate_mana_ability cast special_action unlock turn_face_up].each { |f| require_relative "enjine/actions/#{f}" }
32
32
  require_relative "enjine/replacements"
33
33
  require_relative "enjine/engine"
34
34
  Dir[File.join(__dir__, "enjine/engine/**/*.rb")].each { |f| require f }