enjine 0.4.1 → 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 (90) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +50 -0
  3. data/SUPPORTED.md +31 -23
  4. data/docs/guides/card-data-format.md +106 -1
  5. data/docs/plans/dsl-gaps.md +23 -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 +108 -18
  10. data/docs/reference/filters.md +19 -2
  11. data/docs/reference/keywords.md +16 -2
  12. data/docs/reference/triggers.md +25 -6
  13. data/lib/enjine/actions/play_land.rb +3 -1
  14. data/lib/enjine/card_definition.rb +4 -3
  15. data/lib/enjine/cli/text.rb +10 -3
  16. data/lib/enjine/decisions/combat.rb +4 -2
  17. data/lib/enjine/decisions/damage_order.rb +18 -0
  18. data/lib/enjine/engine/ability_layers.rb +6 -1
  19. data/lib/enjine/engine/casting/alt_casting.rb +36 -4
  20. data/lib/enjine/engine/casting/cast_choices.rb +13 -1
  21. data/lib/enjine/engine/casting/casting.rb +24 -7
  22. data/lib/enjine/engine/casting/cost_options.rb +3 -1
  23. data/lib/enjine/engine/casting/permissions.rb +13 -14
  24. data/lib/enjine/engine/casting/target_counts.rb +14 -3
  25. data/lib/enjine/engine/casting/waterbend.rb +22 -2
  26. data/lib/enjine/engine/characteristics.rb +3 -2
  27. data/lib/enjine/engine/combat.rb +26 -7
  28. data/lib/enjine/engine/copies.rb +10 -3
  29. data/lib/enjine/engine/damage.rb +49 -0
  30. data/lib/enjine/engine/entering.rb +36 -2
  31. data/lib/enjine/engine/face_down.rb +12 -3
  32. data/lib/enjine/engine/faces.rb +14 -2
  33. data/lib/enjine/engine/follow_ups.rb +4 -1
  34. data/lib/enjine/engine/resolution.rb +46 -1
  35. data/lib/enjine/engine/rules.rb +4 -3
  36. data/lib/enjine/engine/triggers.rb +5 -15
  37. data/lib/enjine/engine/turn_history.rb +16 -1
  38. data/lib/enjine/engine/turn_structure.rb +1 -0
  39. data/lib/enjine/engine/zones.rb +5 -2
  40. data/lib/enjine/events.rb +2 -0
  41. data/lib/enjine/frames.rb +2 -2
  42. data/lib/enjine/mana.rb +3 -0
  43. data/lib/enjine/mechanics/abilities/chosen_values.rb +49 -6
  44. data/lib/enjine/mechanics/abilities/damage_replacements.rb +136 -64
  45. data/lib/enjine/mechanics/abilities/entering_choices.rb +40 -21
  46. data/lib/enjine/mechanics/abilities/play_permissions.rb +52 -20
  47. data/lib/enjine/mechanics/abilities/player_rules.rb +30 -13
  48. data/lib/enjine/mechanics/abilities/static.rb +1 -1
  49. data/lib/enjine/mechanics/card_spec.rb +54 -11
  50. data/lib/enjine/mechanics/costs/base.rb +67 -9
  51. data/lib/enjine/mechanics/costs/behold.rb +21 -5
  52. data/lib/enjine/mechanics/costs/choose_creature_type.rb +43 -0
  53. data/lib/enjine/mechanics/costs/one_of.rb +3 -0
  54. data/lib/enjine/mechanics/costs/pay_life.rb +27 -8
  55. data/lib/enjine/mechanics/costs/remove_counters.rb +8 -5
  56. data/lib/enjine/mechanics/costs/tap.rb +44 -3
  57. data/lib/enjine/mechanics/effects/base.rb +10 -2
  58. data/lib/enjine/mechanics/effects/become.rb +29 -12
  59. data/lib/enjine/mechanics/effects/copy.rb +56 -19
  60. data/lib/enjine/mechanics/effects/counters.rb +8 -2
  61. data/lib/enjine/mechanics/effects/damage.rb +11 -0
  62. data/lib/enjine/mechanics/effects/earthbend.rb +2 -1
  63. data/lib/enjine/mechanics/effects/fight.rb +11 -0
  64. data/lib/enjine/mechanics/effects/library.rb +76 -18
  65. data/lib/enjine/mechanics/effects/pay.rb +7 -4
  66. data/lib/enjine/mechanics/effects/repeat.rb +92 -0
  67. data/lib/enjine/mechanics/effects/search.rb +34 -6
  68. data/lib/enjine/mechanics/effects/tokens.rb +42 -10
  69. data/lib/enjine/mechanics/filters/base.rb +22 -12
  70. data/lib/enjine/mechanics/filters/exile.rb +27 -0
  71. data/lib/enjine/mechanics/filters/stack.rb +14 -8
  72. data/lib/enjine/mechanics/keywords/alternative_costs.rb +10 -6
  73. data/lib/enjine/mechanics/keywords/cards_have.rb +65 -0
  74. data/lib/enjine/mechanics/keywords/flags.rb +3 -1
  75. data/lib/enjine/mechanics/keywords/hideaway.rb +6 -5
  76. data/lib/enjine/mechanics/keywords/kicker.rb +24 -6
  77. data/lib/enjine/mechanics/keywords/protection.rb +24 -8
  78. data/lib/enjine/mechanics/keywords/riot.rb +13 -10
  79. data/lib/enjine/mechanics/linked.rb +32 -2
  80. data/lib/enjine/mechanics/selector.rb +32 -9
  81. data/lib/enjine/mechanics/triggers/casting.rb +15 -9
  82. data/lib/enjine/mechanics/triggers/combat.rb +32 -12
  83. data/lib/enjine/mechanics/triggers/keyword_actions.rb +17 -0
  84. data/lib/enjine/mechanics/triggers/tap_and_counters.rb +20 -9
  85. data/lib/enjine/mechanics/values.rb +10 -3
  86. data/lib/enjine/replacements.rb +3 -2
  87. data/lib/enjine/rules_monitor.rb +13 -2
  88. data/lib/enjine/version.rb +1 -1
  89. data/lib/enjine/view.rb +1 -1
  90. metadata +6 -1
@@ -21,7 +21,33 @@ module Enjine
21
21
  abilities = if id.is_a?(GameObject) || state.objects.key?(id) then chars(id).abilities
22
22
  else token_to_create(id).printed_characteristics.abilities
23
23
  end
24
- abilities.map(&:ability).select { it.kind == :entering_choice }
24
+ abilities.map(&:ability).select { it.kind == :entering_choice } + granted_entering_choices(id)
25
+ end
26
+
27
+ # CR 614.12: "as this enters" choices of keywords that static abilities on the battlefield would grant the
28
+ # permanent once it's there ("Other Spiders you control have riot"), judged as it would exist there, under
29
+ # its controller (or its owner's, for a card that has none). A token about to be created isn't judged yet.
30
+ def granted_entering_choices(id)
31
+ obj = id.is_a?(GameObject) ? id : state.objects[id]
32
+ return [] if obj.nil? || obj.zone == :battlefield
33
+
34
+ granting = board_abilities(:static).select { |_, ability| ability.keywords.any? { Entering.entering_keyword?(it) } }
35
+ return [] if granting.empty?
36
+
37
+ stand_in = obj.dup.tap { it.controller ||= it.owner }
38
+ current = chars(obj)
39
+ granting.flat_map do |source, ability|
40
+ next [] unless ability.active?(self, source) && ability.affects?(stand_in, source, facts(obj, current))
41
+
42
+ ability.keywords.flat_map { CardDefinition.keyword_instances(it) }.map(&:ability).select { it.kind == :entering_choice }
43
+ end
44
+ end
45
+
46
+ # Whether a keyword brings an "as this enters" choice (riot).
47
+ def self.entering_keyword?(keyword)
48
+ (@entering_keywords ||= {}).fetch(keyword) do
49
+ @entering_keywords[keyword] = CardDefinition.keyword_instances(keyword).any? { it.ability.kind == :entering_choice }
50
+ end
25
51
  end
26
52
 
27
53
  def entering_choice(id) = entering_choices(id).first
@@ -51,11 +77,18 @@ module Enjine
51
77
  ask!(nil)
52
78
  end
53
79
 
54
- def ask_enter_choice!(id, controller)
80
+ def ask_enter_choice!(id, controller, given = nil)
81
+ before_entering_choice!(id, controller, given)
55
82
  ask!("kind" => "enter_choice", "player" => controller, "object" => id)
56
83
  :wait
57
84
  end
58
85
 
86
+ # What happens just before a choice is asked ("look at an opponent's hand, then choose", CR 701.20e).
87
+ def before_entering_choice!(id, controller, given)
88
+ choice = entering_choices(id)[Entering.answers(given).size]
89
+ choice.before_choice(self, controller) if choice.respond_to?(:before_choice)
90
+ end
91
+
59
92
  def enters_from_answer(id, frame) = entering_statuses(id, Entering.answers(frame["enter"]))
60
93
 
61
94
  # What a permanent enters with given its "as this enters" answers, combined (memories merged, counters
@@ -101,6 +134,7 @@ module Enjine
101
134
  return nil unless id
102
135
 
103
136
  token = state.objects.key?(id) ? {} : { "card" => token_to_create(id).id }
137
+ before_entering_choice!(id, controller, answers[id])
104
138
  ask!({ "kind" => "enter_choice", "player" => controller, "object" => id, "effect" => true }.merge(token))
105
139
  :wait
106
140
  end
@@ -6,14 +6,23 @@ module Enjine
6
6
  FACE_DOWN_ID = "face_down"
7
7
 
8
8
  # Cards exiled face down (CR 406.3, 406.3a-b). Such a card has no characteristics (Engine#chars) and
9
- # only the players in its memory "may_look" see which card it is (View, Events::ZoneChanged). The effect
9
+ # only the players in its memory "may_look" see which card it is (View, Events::ZoneChanged); a hidden-away
10
+ # card (memory "looker_source", CR 702.75a), the player who controls that permanent now. The effect
10
11
  # that exiled it may let a player play it; the card is turned face up just before it's played, so
11
12
  # whether it can be played is judged face up, and only for a player who may look at it.
12
13
  module FaceDown
13
14
  def face_down?(obj) = obj.zone == :exile && obj.memory["face_down"] == true
14
15
 
15
- # Whether `index` may look at the face-down card (always true for a face-up card).
16
- def may_look?(obj, index) = !face_down?(obj) || obj.memory.fetch("may_look", []).include?(index)
16
+ # Whether `index` may look at the face-down card (always true for a face-up card). A hidden-away card's
17
+ # looker follows its permanent's controller while that permanent is on the battlefield (CR 702.75a).
18
+ def may_look?(obj, index)
19
+ return true unless face_down?(obj)
20
+
21
+ source = state.objects[obj.memory["looker_source"]]
22
+ return source.controller == index if source&.zone == :battlefield
23
+
24
+ obj.memory.fetch("may_look", []).include?(index)
25
+ end
17
26
 
18
27
  # Evaluates the block with the face-down card `id` seen face up (CR 406.3a: "turned face up just
19
28
  # before the player announces that they are playing the card"). Nothing in the block changes state.
@@ -103,8 +103,20 @@ module Enjine
103
103
 
104
104
  state.exile.delete(second.id)
105
105
  state.objects.delete(second.id)
106
- move!(first.id, :battlefield, controller: controller, reason: "meld",
107
- enters: { "face" => 1, "memory" => { "melded" => second.card } })
106
+ melded = move!(first.id, :battlefield, controller: controller, reason: "meld",
107
+ enters: { "face" => 1, "memory" => { "melded" => second.card } })
108
+ # The second card leaves exile too; it's part of the melded permanent, not an object of its own.
109
+ emit Events::ZoneChanged.new(card: second.card, old_id: second.id, new_id: nil, owner: name(second.owner),
110
+ from: "exile", to: "battlefield", reason: "meld")
111
+ melded
112
+ end
113
+
114
+ # CR 713.5c, 202.3c: a melded permanent's mana value is the sum of its front faces' mana values. Nil for any
115
+ # other object (a copy of a melded permanent has the back face's copiable values, so its mana value is 0).
116
+ def melded_mana_value(obj)
117
+ return nil unless obj.zone == :battlefield && obj.memory["melded"] && !obj.memory["own_card"]
118
+
119
+ [obj.card, obj.memory["melded"]].sum { catalog.fetch(it).card.mana_cost&.mana_value.to_i }
108
120
  end
109
121
 
110
122
  # CR 713.8, 713.8c: a melded permanent that leaves the battlefield puts its two cards into the new zone;
@@ -17,7 +17,10 @@ module Enjine
17
17
  effects = catalog.fetch(entry["card"]).ability(entry["ref"]).effects
18
18
  memory = { "triggering" => seen, "source_card" => entry["card"] }
19
19
  ctx = Mechanics::EffectContext.new(entry["controller"], entry["source"], entry["source"], [], [], [], memory)
20
- effects.each { it.run(self, ctx, {}) } # effects that need no choice (CR 615.5)
20
+ # Effects that need no choice (CR 615.5); with nothing prevented (CR 615.12) no "when damage is prevented
21
+ # this way" triggers.
22
+ effects = effects.grep_v(Mechanics::Effects::ReflexiveTrigger) if entry["unprevented"]
23
+ with_damage_orders(nil) { effects.each { it.run(self, ctx, {}) } }
21
24
  end
22
25
  end
23
26
  end
@@ -47,14 +47,24 @@ module Enjine
47
47
  frame["pc"] += 1
48
48
  end
49
49
  if !obj.ability? && definition(obj).permanent? && entering_choice_pending?(obj.id, frame["enter"])
50
- return ask_enter_choice!(obj.id, obj.controller) # CR 614.12: decided before it enters
50
+ return ask_enter_choice!(obj.id, obj.controller, frame["enter"]) # CR 614.12: decided before it enters
51
51
  end
52
52
 
53
+ gift_given!(obj, ability)
53
54
  emit Events::Resolved.new(object: obj.id, card: obj.card, kind: obj.kind)
54
55
  finish_resolution!(obj, frame)
55
56
  after_resolution!
56
57
  end
57
58
 
59
+ # CR 702.174c: a player gives a gift as their instant or sorcery spell whose gift was promised resolves, or as
60
+ # the gift triggered ability of their permanent resolves (an enters trigger checking "if the gift was promised").
61
+ def gift_given!(obj, ability)
62
+ given = if obj.ability? then ability.respond_to?(:condition) && Mechanics::Condition::CastHow.gift?(ability.condition)
63
+ else obj.memory["kicked_with"] == "gift" && (chars(obj).type?(:instant) || chars(obj).type?(:sorcery))
64
+ end
65
+ emit Events::KeywordAction.new(player: name(obj.controller), action: "give_gift", amount: nil) if given
66
+ end
67
+
58
68
  # CR 608.2b: targets are checked once, as resolution begins.
59
69
  def start_resolution!(frame, obj, ability)
60
70
  specs = ability&.targets_for(obj.modes || [], kicked: obj.memory.key?("kicked_with")) || []
@@ -93,6 +103,23 @@ module Enjine
93
103
  # Called by zone changes and token creation while an effect runs.
94
104
  def record_result!(id) = @recorded&.push(id)
95
105
 
106
+ # Runs the block recording apart what it moves or creates and how much it changes (an effect of a nested
107
+ # list with results of its own, Effects::Repeat); returns [the block's value, those objects, that amount].
108
+ # They also count for the effect running the nested list.
109
+ def recording_apart
110
+ outer = @recorded
111
+ outer_amount = @recorded_amount
112
+ @recorded = []
113
+ @recorded_amount = 0
114
+ value = yield
115
+ [value, @recorded, @recorded_amount]
116
+ ensure
117
+ inner = @recorded
118
+ inner_amount = @recorded_amount
119
+ @recorded = outer && (outer + inner)
120
+ @recorded_amount = outer_amount && (outer_amount + inner_amount)
121
+ end
122
+
96
123
  # The context for one effect; a modal ability's effects see only their mode's targets.
97
124
  # CR 603.4: a triggered ability whose intervening "if" is false on resolution does nothing.
98
125
  def intervening_if_holds?(obj, ability)
@@ -159,9 +186,27 @@ module Enjine
159
186
 
160
187
  # Abilities on the stack aren't cards; they simply cease to exist.
161
188
  def remove_stack_object!(id)
189
+ remember_ability!(object(id))
162
190
  state.stack.delete(id)
163
191
  state.objects.delete(id)
164
192
  end
193
+
194
+ # "Copy that ability" (CR 707.10) after it has left the stack: the abilities that refer to it ({"triggering":
195
+ # "ability"}) remember it as it last existed on the stack, with its choices (CR 608.2h), to copy that.
196
+ def remember_ability!(obj)
197
+ refs = [obj.id, obj.memory["trigger_id"]].compact
198
+ holders = state.stack.map { object(it).memory } + state.pending_triggers.filter_map { it["memory"] }
199
+ holders.each do |memory|
200
+ triggering = memory["triggering"]
201
+ memory["ability_lki"] = obj.to_h if triggering.is_a?(Hash) && refs.include?(triggering["ability"]) && !memory.equal?(obj.memory)
202
+ end
203
+ end
204
+
205
+ # The ability `memory` remembers (see #remember_ability!), as a detached object that is never stored.
206
+ def remembered_ability(memory)
207
+ lki = memory["ability_lki"]
208
+ lki && Snapshot.build_object(lki)
209
+ end
165
210
  end
166
211
 
167
212
  include Resolution
@@ -35,10 +35,11 @@ module Enjine
35
35
  chars(obj).abilities.any? { it.ability.kind == :cast_restriction && !it.ability.allows?(self, index, obj) }
36
36
  end
37
37
 
38
- # CR 602.5, 101.2: abilities of a permanent matching a `cant_activate` filter can't be activated, nor any of a
39
- # permanent whose activated abilities can't be activated (#mana_abilities_forbidden?).
38
+ # CR 602.5, 101.2: abilities of a source a `cant_activate` rule is about can't be activated (a filter matches
39
+ # only permanents; "sources with the chosen name" are in any zone), nor any of a permanent whose activated
40
+ # abilities can't be activated (#mana_abilities_forbidden?).
40
41
  def activation_forbidden?(index, obj)
41
- obj.zone == :battlefield && (mana_abilities_forbidden?(obj) || player_rule?(index, "cant_activate", obj))
42
+ (obj.zone == :battlefield && mana_abilities_forbidden?(obj)) || player_rule?(index, "cant_activate", obj)
42
43
  end
43
44
 
44
45
  # CR 602.5, 605.3a: "its activated abilities can't be activated" covers its mana abilities too.
@@ -10,20 +10,6 @@ module Enjine
10
10
  # A permanent that left earlier in the same simultaneous zone change is remembered too (#simultaneously).
11
11
  def last_known(id) = @last_known&.[](id) || @departed_together&.dig(id, "lki")
12
12
 
13
- # What a permanent looked like as it last existed on the battlefield (CR 603.10a, 608.2h):
14
- # controller and current characteristics, so "a creature you control dies" checks the
15
- # types it had then, not its printed ones.
16
- def last_known_info(obj)
17
- current = chars(obj)
18
- { "controller" => obj.controller, "owner" => obj.owner, "card" => obj.card, "types" => current.types,
19
- "subtypes" => current.subtypes,
20
- "power" => current.power, "toughness" => current.toughness, "counters" => obj.counters.dup,
21
- "mana_value" => Mechanics::Amount.mana_value(self, obj), # CR 202.3
22
- "keywords" => current.keywords.map(&:to_s), # deathtouch, lifelink, wither (CR 702.2e, 702.15c, 702.80b)
23
- "token" => token?(obj), "except" => copy_exceptions(obj), "abilities" => current.abilities }
24
- .merge(obj.memory.slice(Mechanics::Linked::EXILED)) # the cards exiled with it (CR 607.2a)
25
- end
26
-
27
13
  def with_last_known(id, info)
28
14
  (@last_known ||= {})[id] = info
29
15
  yield
@@ -85,13 +71,15 @@ module Enjine
85
71
 
86
72
  # CR 603.7: creates a delayed triggered ability. `entry` names the card and the ability ref
87
73
  # (see Effects::DelayedTrigger) plus what it remembers ("results", "watched" objects) and, for
88
- # one with a duration ("this turn"), the turn it lasts for ("turn").
74
+ # one with a duration ("this turn"), the turn it lasts for ("turn"). "while_watched": it can only ever
75
+ # trigger on its watched permanent, so it ends once that permanent has left the battlefield (earthbend).
89
76
  def create_delayed_trigger!(entry)
90
77
  state.delayed_triggers << entry.merge("id" => "d#{next_timestamp!}")
91
78
  end
92
79
 
93
80
  # CR 603.7b: a delayed triggered ability triggers only once, the next time its event occurs, unless
94
81
  # it has a stated duration ("whenever ... this turn"): then it triggers each time until the turn ends.
82
+ # CR 400.7, 603.7c: one "while_watched" ends when the permanent it watches leaves to any zone.
95
83
  def watch_delayed!(event)
96
84
  state.delayed_triggers.reject! { it["turn"] && it["turn"] != state.turn } # "this turn" has ended
97
85
  fired = []
@@ -107,6 +95,8 @@ module Enjine
107
95
  end
108
96
  end
109
97
  state.delayed_triggers -= fired.reject { repeats?(it) }
98
+ gone = event.old_id if event.is_a?(Events::ZoneChanged) && event.from == "battlefield"
99
+ state.delayed_triggers.reject! { it["while_watched"] && Array(it["watched"]).include?(gone) } if gone
110
100
  end
111
101
 
112
102
  # "Whenever ... this turn" triggers each time; "the next ... this turn" ("once") only the first time; one
@@ -106,11 +106,26 @@ module Enjine
106
106
  "mana_value" => Mechanics::Amount.mana_value(self, obj) }
107
107
  end
108
108
 
109
+ # What a permanent looked like as it last existed on the battlefield (CR 603.10a, 608.2h):
110
+ # controller and current characteristics, so "a creature you control dies" checks the
111
+ # types it had then, not its printed ones.
112
+ def last_known_info(obj)
113
+ current = chars(obj)
114
+ { "controller" => obj.controller, "owner" => obj.owner, "card" => obj.card, "types" => current.types,
115
+ "subtypes" => current.subtypes,
116
+ "power" => current.power, "toughness" => current.toughness, "counters" => obj.counters.dup,
117
+ "mana_value" => Mechanics::Amount.mana_value(self, obj), # CR 202.3
118
+ "keywords" => current.keywords.map(&:to_s), # deathtouch, lifelink, wither (CR 702.2e, 702.15c, 702.80b)
119
+ "token" => token?(obj), "except" => copy_exceptions(obj), "abilities" => current.abilities,
120
+ "attacking" => attackers.key?(obj.id) } # "if it was attacking" (CR 506.4, 608.2h)
121
+ .merge(obj.memory.slice(Mechanics::Linked::EXILED)) # the cards exiled with it (CR 607.2a)
122
+ end
123
+
109
124
  # The part of last known information an ability keeps in its memory ("source_lki"): plain data.
110
125
  def last_known_memory(lki)
111
126
  info = { "controller" => lki["controller"], "power" => lki["power"], "toughness" => lki["toughness"],
112
127
  "mana_value" => lki["mana_value"], "counters" => lki["counters"].dup, "card" => lki["card"],
113
- "keywords" => lki["keywords"].dup }
128
+ "keywords" => lki["keywords"].dup, "attacking" => lki["attacking"] }
114
129
  info["except"] = lki["except"] if lki["except"] # what a copy of it copies (CR 707.2, Engine::Copies)
115
130
  info.merge(lki.slice(Mechanics::Linked::EXILED).transform_values(&:dup)) # CR 607.2a (Linked.exiled_with)
116
131
  end
@@ -44,6 +44,7 @@ module Enjine
44
44
  obj = object(id)
45
45
  current = chars(obj)
46
46
  obj.tapped && obj.controller == index && !current.keyword?(:doesnt_untap) && !obj.memory["exerted"] &&
47
+ !current.keyword?(:cant_become_untapped) &&
47
48
  current.abilities.any? { it.ability.kind == :may_not_untap }
48
49
  end
49
50
  end
@@ -189,6 +189,8 @@ module Enjine
189
189
  fresh.x = old.x
190
190
  fresh.memory.merge!(old.memory.slice("kicked", "kicked_with", "gift")) # gift: CR 702.174b
191
191
  fresh.mana_spent = old.mana_spent&.dup
192
+ # CR 609.7b: a prevention effect for a chosen permanent spell also applies to the permanent it becomes.
193
+ state.effects.each { it["by"] << fresh.id if it["kind"] == "prevent_damage" && it["by"]&.include?(old.id) }
192
194
  end
193
195
 
194
196
  # CR 111.1: a token enters the battlefield under its creator's control. Entering replacement
@@ -213,7 +215,7 @@ module Enjine
213
215
  obj.counters.merge!(enters["counters"])
214
216
  enter_as_copy!(obj, enters["copy"]) if enters["copy"] # CR 707.5 (Engine::Copies)
215
217
  state.battlefield.unshift(obj.id)
216
- enter_attacking!(obj) if enters["attacking"] # CR 508.4
218
+ enter_attacking!(obj, enters["attacking"]) if enters["attacking"] # CR 508.4
217
219
  record_result!(obj.id)
218
220
  emit Events::ZoneChanged.new(card: definition.id, old_id: nil, new_id: obj.id, owner: name(controller),
219
221
  from: "none", to: "battlefield", reason: "create_token")
@@ -263,9 +265,10 @@ module Enjine
263
265
  end
264
266
 
265
267
  # CR 701.26b: only tapped permanents can be untapped. Replacement effects (a stun counter,
266
- # CR 122.1d) may stop it.
268
+ # CR 122.1d) may stop it, and "can't become untapped" forbids it (CR 101.2).
267
269
  def untap!(object)
268
270
  return unless object.tapped
271
+ return if chars(object).keyword?(:cant_become_untapped)
269
272
  return if propose("untap", { "object" => object.id }).data["cancelled"]
270
273
 
271
274
  object.tapped = false
data/lib/enjine/events.rb CHANGED
@@ -88,6 +88,8 @@ module Enjine
88
88
  RolledDie = define(:rolled_die, :player, :sides, :result)
89
89
  # CR 707.10: a copy of a spell put on the stack (not cast).
90
90
  SpellCopied = define(:spell_copied, :player, :object, :card, :original, :targets)
91
+ # New targets chosen for a spell or ability (CR 115.7); `player`: its controller, `targets`: the added ones.
92
+ TargetsChanged = define(:targets_changed, :player, :object, :targets)
91
93
  EffectStarted = define(:effect_started, :kind, :affected, :details)
92
94
  EffectsEnded = define(:effects_ended, :count)
93
95
  AttackerDeclared = define(:attacker_declared, :object, :target)
data/lib/enjine/frames.rb CHANGED
@@ -45,7 +45,7 @@ module Enjine
45
45
  # Resolving the top object of the stack, one effect at a time (CR 608).
46
46
  class Resolve < Base
47
47
  def self.step(engine, frame) = engine.step_resolution!(frame)
48
- def self.decisions = %w[effect_choice enter_choice]
48
+ def self.decisions = %w[effect_choice enter_choice damage_order]
49
49
 
50
50
  def self.validate(frame, data, errors)
51
51
  errors << "frames: resolving object #{frame["object"]} is not on the stack" unless data["stack"].include?(frame["object"])
@@ -56,7 +56,7 @@ module Enjine
56
56
  # Assigning and dealing combat damage (CR 510).
57
57
  class CombatDamage < Base
58
58
  def self.step(engine, frame) = engine.step_combat_damage!(frame)
59
- def self.decisions = %w[assign_damage]
59
+ def self.decisions = %w[assign_damage damage_order]
60
60
  end
61
61
  register("combat_damage", CombatDamage)
62
62
 
data/lib/enjine/mana.rb CHANGED
@@ -33,6 +33,9 @@ module Enjine
33
33
  # The cost with X replaced by `value` (CR 107.3a): each {X} adds that much generic mana.
34
34
  def with_x(value) = with(generic: generic + (x_symbols * value), x_symbols: 0)
35
35
 
36
+ # How many of its mana symbols are of `color`: colored ones and hybrid ones with that color (CR 107.4e).
37
+ def symbols_of(color) = colored.fetch(color, 0) + hybrid.count { it.include?(color) }
38
+
36
39
  # The colors of the cost's colored and hybrid symbols, in WUBRG order (CR 202.2, 202.2d, 107.4e).
37
40
  def colors = COLORS.select { |c| colored.key?(c) || hybrid.any? { it.include?(c) } }
38
41
 
@@ -12,7 +12,7 @@ module Enjine
12
12
  module Chosen
13
13
  # What is chosen -> [memory key, option key].
14
14
  MEMORY = { "color" => "chosen_color", "creature_type" => "chosen_type", "card_type" => "chosen_card_type",
15
- "basic_land_type" => "chosen_type", "option" => "chosen_option" }.freeze
15
+ "basic_land_type" => "chosen_type", "option" => "chosen_option", "card_name" => "chosen_name" }.freeze
16
16
  # CR 105.4: one of the five colors.
17
17
  COLORS = %w[W U B R G].freeze
18
18
  # CR 205.2a: the card types of traditional Magic cards.
@@ -124,14 +124,23 @@ module Enjine
124
124
  module Effects
125
125
  # "Choose a creature type." while resolving: the choice is remembered on the resolving spell or
126
126
  # ability, so its later effects' filters ("of the chosen type") and conditions read it (CR 607.2d).
127
+ # `remember: "source"` also remembers it on the source permanent, for its other abilities ("When this
128
+ # land enters, choose a land card name. ... Lands with the chosen name have ...", CR 607.2d).
127
129
  class ResolvingChosen < ChoiceEffect
128
130
  def self.kind_name = raise(NotImplementedError)
129
131
  def self.default_to = "you"
130
- def self.keys = kind_name == "option" ? %w[options to if] : %w[to if]
132
+ def self.keys = (kind_name == "option" ? %w[options] : []) + %w[remember to if]
131
133
 
132
- def setup(params, errors, path) = @words = Chosen.words(self.class.kind_name, params, errors, path)
134
+ def setup(params, errors, path)
135
+ @words = Chosen.words(self.class.kind_name, params, errors, path)
136
+ @remember = Mechanics.fetch_param(params, :remember)
137
+ errors << "#{path}.remember: must be \"source\"" unless [nil, "source"].include?(@remember)
138
+ end
139
+
140
+ def to_s = "choose #{Chosen.words_text(self.class.kind_name, @words)}#{" (remembered by the source)" if @remember}"
133
141
 
134
- def to_s = "choose #{Chosen.words_text(self.class.kind_name, @words)}"
142
+ # The values offered.
143
+ def choice_values(_engine) = Chosen.values(self.class.kind_name, @words)
135
144
 
136
145
  def run(engine, ctx, locals)
137
146
  return :done unless locals["asked"] || condition.nil? || condition.true?(engine, ctx)
@@ -141,11 +150,22 @@ module Enjine
141
150
  unless answer
142
151
  chooser = players(engine, ctx).first || ctx.controller
143
152
  return ask(engine, locals, chooser, "Choose #{Chosen.words_text(kind, @words)}",
144
- Chosen.values(kind, @words).map { { "type" => kind, kind => it } })
153
+ choice_values(engine).map { { "type" => kind, kind => it } })
145
154
  end
146
- engine.state.objects[ctx.object]&.memory&.merge!(Chosen.memory(kind, answer[kind]))
155
+ remember!(engine, ctx, Chosen.memory(kind, answer[kind]))
147
156
  :done
148
157
  end
158
+
159
+ private
160
+
161
+ def remember!(engine, ctx, memory)
162
+ engine.state.objects[ctx.object]&.memory&.merge!(memory)
163
+ source = @remember && engine.state.objects[ctx.source]
164
+ return unless source&.zone == :battlefield
165
+
166
+ source.memory.merge!(memory)
167
+ engine.touch! # its static abilities may now apply to other permanents (the layer pass is cached)
168
+ end
149
169
  end
150
170
 
151
171
  # "Choose a color."
@@ -178,6 +198,29 @@ module Enjine
178
198
  def self.kind_name = "card_type"
179
199
  end
180
200
 
201
+ # "Choose a land card name." among the cards in the game (CR 201.4, as Abilities::ChooseCardName).
202
+ class ChooseCardNameEffect < ResolvingChosen
203
+ primitive :effect, :choose_card_name,
204
+ description: "The player (`to`, default you) chooses a card name among the cards in the game (`types`: " \
205
+ "the named card has one of these; `not`: it has none of these); \"the chosen name\" " \
206
+ "(`name: \"chosen\"` in filters) reads it. `remember: \"source\"`: the source permanent " \
207
+ "remembers it for its other abilities.",
208
+ rules: %w[201.4 607.2d 608.2c],
209
+ params: { "types" => "list of card types", "not" => "list of card types",
210
+ "remember" => "\"source\"", "to" => "player selector (default you)" },
211
+ example: { "choose_card_name" => { "types" => ["land"], "remember" => "source" } }
212
+ def self.kind_name = "card_name"
213
+ def self.keys = super + %w[types not]
214
+
215
+ def setup(params, errors, path)
216
+ super
217
+ @types = Abilities::ChooseCardName.type_list(params, :types, errors, path)
218
+ @excluded = Abilities::ChooseCardName.type_list(params, :not, errors, path)
219
+ end
220
+
221
+ def choice_values(engine) = Abilities::ChooseCardName.names(engine, @types, @excluded)
222
+ end
223
+
181
224
  # "Choose odd or even." Later effects check the word with {"if": {"chosen": word}}.
182
225
  class ChooseOptionEffect < ResolvingChosen
183
226
  primitive :effect, :choose_option,