enjine 0.2.0 → 0.4.1

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 (71) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +66 -1
  3. data/README.md +2 -1
  4. data/SUPPORTED.md +4 -4
  5. data/docs/ARCHITECTURE.md +2 -0
  6. data/docs/DESIGN.md +12 -5
  7. data/docs/MECHANICS_COVERAGE.md +32 -4
  8. data/docs/guides/api.md +51 -0
  9. data/docs/guides/card-data-format.md +58 -1
  10. data/docs/guides/deck-builder.md +18 -0
  11. data/docs/plans/commander.md +63 -0
  12. data/docs/plans/dsl-gaps.md +76 -0
  13. data/docs/reference/decisions.md +5 -1
  14. data/docs/reference/effects.md +12 -9
  15. data/docs/reference/triggers.md +3 -1
  16. data/lib/enjine/actions/cast.rb +12 -4
  17. data/lib/enjine/card_definition.rb +39 -3
  18. data/lib/enjine/cli/text.rb +23 -1
  19. data/lib/enjine/cli.rb +2 -1
  20. data/lib/enjine/color_identity.rb +38 -0
  21. data/lib/enjine/decisions/commander_zone.rb +36 -0
  22. data/lib/enjine/deck.rb +73 -14
  23. data/lib/enjine/deck_builder.rb +62 -18
  24. data/lib/enjine/engine/casting/alt_casting.rb +9 -0
  25. data/lib/enjine/engine/casting/casting.rb +19 -0
  26. data/lib/enjine/engine/casting/permissions.rb +19 -14
  27. data/lib/enjine/engine/characteristics.rb +29 -12
  28. data/lib/enjine/engine/commander.rb +125 -0
  29. data/lib/enjine/engine/copies.rb +16 -9
  30. data/lib/enjine/engine/damage.rb +3 -1
  31. data/lib/enjine/engine/durations.rb +47 -0
  32. data/lib/enjine/engine/emblems.rb +1 -1
  33. data/lib/enjine/engine/faces.rb +2 -0
  34. data/lib/enjine/engine/pregame.rb +1 -0
  35. data/lib/enjine/engine/priority.rb +1 -0
  36. data/lib/enjine/engine/rules.rb +3 -2
  37. data/lib/enjine/engine/state_based_actions.rb +9 -0
  38. data/lib/enjine/engine/triggers.rb +16 -9
  39. data/lib/enjine/engine/zones.rb +19 -2
  40. data/lib/enjine/events.rb +4 -1
  41. data/lib/enjine/format.rb +53 -0
  42. data/lib/enjine/fuzz.rb +17 -0
  43. data/lib/enjine/game.rb +4 -3
  44. data/lib/enjine/mechanics/abilities/double_counters.rb +1 -1
  45. data/lib/enjine/mechanics/abilities/play_permissions.rb +3 -2
  46. data/lib/enjine/mechanics/abilities/replacement_effects.rb +2 -1
  47. data/lib/enjine/mechanics/abilities/static.rb +7 -0
  48. data/lib/enjine/mechanics/card_spec.rb +54 -12
  49. data/lib/enjine/mechanics/effects/become.rb +35 -13
  50. data/lib/enjine/mechanics/effects/copy.rb +7 -4
  51. data/lib/enjine/mechanics/effects/gain_control.rb +6 -3
  52. data/lib/enjine/mechanics/effects/return_to_battlefield.rb +25 -6
  53. data/lib/enjine/mechanics/filters/base.rb +19 -2
  54. data/lib/enjine/mechanics/filters/stack.rb +1 -1
  55. data/lib/enjine/mechanics/triggers/base.rb +14 -3
  56. data/lib/enjine/mechanics/triggers/casting.rb +1 -1
  57. data/lib/enjine/mechanics/triggers/tap_and_counters.rb +21 -5
  58. data/lib/enjine/mechanics/triggers/zone_changes.rb +1 -1
  59. data/lib/enjine/mechanics/values.rb +10 -7
  60. data/lib/enjine/mechanics.rb +5 -0
  61. data/lib/enjine/player.rb +2 -1
  62. data/lib/enjine/rules_monitor.rb +29 -3
  63. data/lib/enjine/setup.rb +10 -4
  64. data/lib/enjine/snapshot/player_checks.rb +47 -0
  65. data/lib/enjine/snapshot.rb +4 -1
  66. data/lib/enjine/state.rb +33 -14
  67. data/lib/enjine/steps.rb +10 -1
  68. data/lib/enjine/version.rb +1 -1
  69. data/lib/enjine/view.rb +18 -1
  70. data/lib/enjine.rb +2 -0
  71. metadata +7 -1
@@ -17,8 +17,8 @@ module Enjine
17
17
  GRANTABLE = %i[triggered mana activated play_from additional_land_plays hexproof_from protection_from].freeze
18
18
  # Card types an effect can add (CR 205.1b); instants, sorceries and planeswalkers aren't permanents it makes.
19
19
  ADDABLE_TYPES = %w[artifact creature enchantment land].freeze
20
- CHANGES = %w[types supertypes subtypes creature_types colors loses_abilities keywords base_power_toughness
21
- abilities].freeze
20
+ CHANGES = %w[set_types types supertypes subtypes creature_types all_creature_types colors loses_abilities keywords
21
+ base_power_toughness abilities].freeze
22
22
 
23
23
  primitive :effect, :become,
24
24
  description: "Each referenced permanent becomes what the parameters say, for `duration` " \
@@ -27,8 +27,11 @@ module Enjine
27
27
  "while_source_on_battlefield: for as long as this effect's source remains on the " \
28
28
  "battlefield; while_you_control_source: for as long as you control it): it has " \
29
29
  "the card `types`, `supertypes` and `subtypes` in addition to its other types (layer 4, " \
30
- "\"becomes an artifact creature\"), its creature types are exactly `creature_types` " \
31
- "(layer 4), its colors are `colors` (layer 5), with `loses_abilities: true` it loses all " \
30
+ "\"becomes an artifact creature\"; with `set_types` its card types are exactly those " \
31
+ "first, \"it's an enchantment, not a creature\", and subtypes of a card type it loses go), " \
32
+ "its creature types are exactly `creature_types` ([] for \"loses all creature types\"; " \
33
+ "`all_creature_types: true` for \"all creature types\") (layer 4), its colors are " \
34
+ "`colors` (layer 5), with `loses_abilities: true` it loses all " \
32
35
  "the abilities it had (layer 6; abilities granted later, keyword counters put on it " \
33
36
  "later and this effect's own `keywords` and `abilities` stay, CR 613.7), it has the " \
34
37
  "`keywords` and `abilities` " \
@@ -37,10 +40,12 @@ module Enjine
37
40
  "power and toughness are `base_power_toughness` (layer 7b). A permanent that becomes a " \
38
41
  "creature has its printed power and toughness (a Vehicle's), or 0/0.",
39
42
  rules: %w[611.2a 611.2b 611.2c 613.1d 613.1e 613.1f 613.4b 613.7b 205.1a 205.1b 301.7b 514.2],
40
- params: { "types" => "card types it also has: #{ADDABLE_TYPES.join(", ")}",
43
+ params: { "set_types" => "card types it has instead of its others: #{ADDABLE_TYPES.join(", ")}",
44
+ "types" => "card types it also has: #{ADDABLE_TYPES.join(", ")}",
41
45
  "supertypes" => "supertypes it also has (\"legendary\")",
42
46
  "subtypes" => "subtypes it also has (\"in addition to its other types\")",
43
- "creature_types" => "list of creature types (replaces the others)",
47
+ "creature_types" => "list of creature types (replaces the others; [] removes them all)",
48
+ "all_creature_types" => "true: it has every creature type",
44
49
  "colors" => "list of W, U, B, R, G (replaces its colors)",
45
50
  "loses_abilities" => "true: it loses all abilities (\"loses all abilities\")",
46
51
  "keywords" => "list of keywords it gains",
@@ -52,12 +57,18 @@ module Enjine
52
57
  "keywords" => ["haste"], "to" => "self" } }
53
58
 
54
59
  attr_reader :ref, :creature_types, :base_power_toughness, :granted_abilities, :duration, :card_types,
55
- :supertypes, :subtypes, :colors, :keywords
60
+ :supertypes, :subtypes, :colors, :keywords, :set_types
56
61
 
57
62
  UNTIL = { "end_of_turn" => " until end of turn", "until_your_next_turn" => " until your next turn" }.freeze
58
63
 
59
64
  def self.keys = CHANGES + %w[duration to if]
60
65
 
66
+ # The creature types a "creature_types" continuous effect sets: [] for "loses all creature types", every
67
+ # creature type for "all creature types" (CR 205.3d, 702.73a).
68
+ def self.creature_types_of(effect)
69
+ effect["all"] ? Keywords::Changeling::CREATURE_TYPES : effect["types"].map(&:to_sym)
70
+ end
71
+
61
72
  # A list of registered names (card types, supertypes, keywords) as symbols; [] when absent.
62
73
  def self.names_param(params, key, category, errors, path, allowed: nil)
63
74
  raw = Mechanics.fetch_param(params, key)
@@ -75,11 +86,15 @@ module Enjine
75
86
 
76
87
  def setup(params, errors, path)
77
88
  @ref = "become:#{path}"
89
+ @set_types = (Become.names_param(params, :set_types, :type, errors, path, allowed: ADDABLE_TYPES) if params.key?("set_types"))
78
90
  @card_types = Become.names_param(params, :types, :type, errors, path, allowed: ADDABLE_TYPES)
79
91
  @supertypes = Become.names_param(params, :supertypes, :supertype, errors, path)
80
92
  @subtypes = parse_types(Mechanics.fetch_param(params, :subtypes), errors, "#{path}.subtypes", "subtypes") || []
81
93
  @creature_types = parse_types(Mechanics.fetch_param(params, :creature_types), errors, "#{path}.creature_types",
82
- "creature types")
94
+ "creature types", empty: true)
95
+ @all_creature_types = Mechanics.fetch_param(params, :all_creature_types)
96
+ errors << "#{path}.all_creature_types: must be true (or absent)" unless [nil, true].include?(@all_creature_types)
97
+ errors << "#{path}: creature_types and all_creature_types exclude each other" if @creature_types && all_creature_types?
83
98
  @colors = Become.parse_colors(Mechanics.fetch_param(params, :colors), errors, path)
84
99
  @loses_abilities = Mechanics.fetch_param(params, :loses_abilities)
85
100
  errors << "#{path}.loses_abilities: must be true (or absent)" unless [nil, true].include?(@loses_abilities)
@@ -94,6 +109,7 @@ module Enjine
94
109
  end
95
110
 
96
111
  def loses_abilities? = @loses_abilities == true
112
+ def all_creature_types? = @all_creature_types == true
97
113
 
98
114
  # The state.effects duration and the details it needs. "For as long as [source] remains on the
99
115
  # battlefield" does nothing if the source has already left as this resolves (CR 611.2b).
@@ -113,11 +129,15 @@ module Enjine
113
129
  add = ->(kind, details) { engine.add_continuous_effect!(kind, ctx, affected, details.merge(lasting)) }
114
130
  # Removal first: this effect's own keywords and abilities come after it (CR 613.7b).
115
131
  add.call("lose_abilities", {}) if loses_abilities?
132
+ add.call("set_types", "types" => set_types.map(&:to_s)) if set_types # CR 205.1a, before the types added
116
133
  unless card_types.empty? && supertypes.empty? && subtypes.empty?
117
134
  add.call("add_types", "types" => card_types.map(&:to_s), "supertypes" => supertypes.map(&:to_s), "subtypes" => subtypes)
118
135
  end
119
136
  creatures = affected.select { engine.chars(it).creature? } # after the types above are added
120
- engine.add_continuous_effect!("creature_types", ctx, creatures, { "types" => creature_types }.merge(lasting)) if creature_types
137
+ if creature_types || all_creature_types?
138
+ details = all_creature_types? ? { "types" => [], "all" => true } : { "types" => creature_types }
139
+ engine.add_continuous_effect!("creature_types", ctx, creatures, details.merge(lasting))
140
+ end
121
141
  add.call("colors", "colors" => colors) if colors
122
142
  add.call("grant", "keywords" => keywords.map(&:to_s)) unless keywords.empty?
123
143
  engine.add_continuous_effect!("base_power_toughness", ctx, creatures, base_power_toughness.merge(lasting)) if base_power_toughness
@@ -131,8 +151,10 @@ module Enjine
131
151
  def to_s
132
152
  bits = []
133
153
  bits << "loses all abilities" if loses_abilities?
154
+ bits << "only #{set_types.join(" ")}" if set_types
134
155
  bits << [*supertypes, *colors, *subtypes, *card_types].join(" ") unless (card_types + supertypes + subtypes).empty?
135
- bits << creature_types.join(" ") if creature_types
156
+ bits << (creature_types.empty? ? "no creature types" : creature_types.join(" ")) if creature_types
157
+ bits << "all creature types" if all_creature_types?
136
158
  bits << "base #{base_power_toughness.values.join("/")}" if base_power_toughness
137
159
  bits << keywords.join(", ") unless keywords.empty?
138
160
  bits << "\"#{granted_abilities.join("; ")}\"" unless granted_abilities.empty?
@@ -162,11 +184,11 @@ module Enjine
162
184
 
163
185
  private
164
186
 
165
- def parse_types(raw, errors, path, noun)
187
+ def parse_types(raw, errors, path, noun, empty: false)
166
188
  return nil if raw.nil?
167
189
 
168
- ok = raw.is_a?(Array) && !raw.empty? && raw.all? { CardDefinition::SUBTYPE_FORMAT.match?(it.to_s) }
169
- errors << "#{path}: must be a non-empty list of lowercase #{noun}" unless ok
190
+ ok = raw.is_a?(Array) && (empty || !raw.empty?) && raw.all? { CardDefinition::SUBTYPE_FORMAT.match?(it.to_s) }
191
+ errors << "#{path}: must be a #{"non-empty " unless empty}list of lowercase #{noun}" unless ok
170
192
  ok ? raw.map(&:to_s).freeze : nil
171
193
  end
172
194
 
@@ -125,12 +125,13 @@ module Enjine
125
125
  class BecomeCopy < Effect
126
126
  include CopyOf
127
127
 
128
- DURATIONS = %w[end_of_turn permanent].freeze
128
+ DURATIONS = %w[end_of_turn until_your_next_turn permanent].freeze
129
129
 
130
130
  primitive :effect, :become_copy,
131
131
  description: "Each referenced permanent becomes a copy of the (first) object `of` refers to, with the " \
132
- "`except` modifications, until end of turn (default) or for as long as it stays on the " \
133
- "battlefield (`duration: \"permanent\"`). It keeps its counters, status and other " \
132
+ "`except` modifications, until end of turn (default), until your next turn " \
133
+ "(`until_your_next_turn`) or for as long as it stays on the battlefield " \
134
+ "(`duration: \"permanent\"`). It keeps its counters, status and other " \
134
135
  "effects; nothing enters or leaves, so no enter or leave abilities trigger. Several copy " \
135
136
  "effects apply in timestamp order.",
136
137
  rules: %w[707.2 707.4 613.2a 613.7 611.2a 514.2],
@@ -152,7 +153,9 @@ module Enjine
152
153
  def apply(engine, ctx)
153
154
  first = copy_values(engine, ctx).first or return
154
155
  values = [first[0], Engine::Copies.merge_except(first[1], @except)]
155
- objects(engine, ctx).each { engine.become_copy!(engine.object(it), values, duration: @duration) }
156
+ objects(engine, ctx).each do
157
+ engine.become_copy!(engine.object(it), values, duration: @duration, controller: ctx.controller)
158
+ end
156
159
  end
157
160
  end
158
161
 
@@ -6,7 +6,8 @@ module Enjine
6
6
  # "Gain control of target permanent" / "... until end of turn" / "Target opponent gains control of
7
7
  # target permanent you control" (CR 613.1b, 108.4, 506.4, 302.6).
8
8
  class GainControl < Effect
9
- DURATIONS = %w[permanent end_of_turn while_source_on_battlefield while_source_tapped while_you_control_source].freeze
9
+ DURATIONS = %w[permanent end_of_turn end_of_your_next_turn while_source_on_battlefield while_source_tapped
10
+ while_you_control_source].freeze
10
11
 
11
12
  primitive :effect, :gain_control,
12
13
  description: "The player referenced by `gains` (default you) gains control of each referenced " \
@@ -40,11 +41,13 @@ module Enjine
40
41
  next engine.gain_control!(obj, player) if duration == "permanent"
41
42
 
42
43
  engine.gain_control_for!(obj, player, ctx, duration: duration,
43
- source: duration == "end_of_turn" ? nil : ctx.source)
44
+ source: %w[end_of_turn end_of_your_next_turn].include?(duration) ? nil : ctx.source)
44
45
  end
45
46
  end
46
47
 
47
- def to_s = "#{gains} gains control#{" until end of turn" if duration == "end_of_turn"} → #{to}"
48
+ UNTIL = { "end_of_turn" => " until end of turn", "end_of_your_next_turn" => " until the end of your next turn" }.freeze
49
+
50
+ def to_s = "#{gains} gains control#{UNTIL[duration]} → #{to}"
48
51
  end
49
52
 
50
53
  # "Exchange control of target artifact you control and target artifact an opponent controls" (CR 701.12a-b):
@@ -9,22 +9,28 @@ module Enjine
9
9
  class ReturnToBattlefield < Effect
10
10
  UNDER = %w[owner you].freeze
11
11
  ZONES = %i[exile graveyard].freeze
12
+ # What it can be "as it enters" (`as`): a `become` without a duration, target or abilities to grant.
13
+ AS_KEYS = (Become::CHANGES - %w[abilities]).freeze
12
14
 
13
15
  primitive :effect, :return_to_battlefield,
14
16
  description: "Put each referenced card that is in exile or a graveyard onto the battlefield under its " \
15
17
  "owner's control (`under: \"you\"`: under the controller's), tapped if `tapped: true`. " \
16
18
  "A card that is no longer in the zone it was expected in, or isn't a permanent card, " \
17
19
  "stays where it is. `transformed: true`: it enters with its back face up; a card that " \
18
- "isn't double-faced, or whose back face isn't a permanent, stays where it is.",
19
- rules: %w[400.7 400.4a 110.2 111.8 603.7c 614.1c 122.6 712.11a 712.11b],
20
+ "isn't double-faced, or whose back face isn't a permanent, stays where it is. `as`: " \
21
+ "what it is as it enters, for as long as it stays (the `become` changes, without " \
22
+ "abilities): \"It's an enchantment. (It's not a creature.)\" is " \
23
+ "`{\"set_types\": [\"enchantment\"]}`; abilities that trigger on it entering see it so.",
24
+ rules: %w[400.7 400.4a 110.2 111.8 603.7c 614.1c 122.6 712.11a 712.11b 205.1a],
20
25
  params: { "to" => "selector (usually {\"result\": n})", "tapped" => "boolean", "transformed" => "boolean",
21
26
  "under" => UNDER.join(" | "),
22
- "counters" => "{counter name: n}: it enters with these counters (\"with a +1/+1 counter on it\")" },
27
+ "counters" => "{counter name: n}: it enters with these counters (\"with a +1/+1 counter on it\")",
28
+ "as" => "{#{AS_KEYS.join(", ")}}: as `become`, from the moment it enters" },
23
29
  example: { "return_to_battlefield" => { "to" => { "result" => 0 } } }
24
30
 
25
- attr_reader :tapped, :under, :counters, :transformed
31
+ attr_reader :tapped, :under, :counters, :transformed, :as
26
32
 
27
- def self.keys = %w[to tapped under counters transformed if]
33
+ def self.keys = %w[to tapped under counters transformed as if]
28
34
 
29
35
  def setup(params, errors, path)
30
36
  @tapped = Mechanics.fetch_param(params, :tapped) == true
@@ -32,6 +38,17 @@ module Enjine
32
38
  @under = Mechanics.fetch_param(params, :under) || "owner"
33
39
  errors << "#{path}.under: must be one of #{UNDER.join(", ")}" unless UNDER.include?(@under)
34
40
  @counters = parse_counters(Mechanics.fetch_param(params, :counters) || {}, errors, "#{path}.counters")
41
+ @as = parse_as(Mechanics.fetch_param(params, :as), errors, "#{path}.as")
42
+ end
43
+
44
+ def parse_as(raw, errors, path)
45
+ return nil if raw.nil?
46
+ return (errors << "#{path}: must be an object of become changes") && nil unless raw.is_a?(Hash)
47
+
48
+ Mechanics.check_keys(raw, AS_KEYS, errors, path)
49
+ return (errors << "#{path}: needs at least one of #{AS_KEYS.join(", ")}") && nil unless raw.keys.intersect?(AS_KEYS)
50
+
51
+ Become.build(raw.slice(*AS_KEYS).merge("to" => "self", "duration" => "indefinite"), errors, path)
35
52
  end
36
53
 
37
54
  def parse_counters(raw, errors, path)
@@ -47,6 +64,7 @@ module Enjine
47
64
  def to_s
48
65
  extra = counters.map { |kind, n| " with #{n} #{kind} counter(s)" }.join
49
66
  status = "#{" tapped" if tapped}#{" transformed" if transformed}"
67
+ extra += " as it #{as.to_s.delete_suffix(" → self")}" if as
50
68
  "return to the battlefield#{status}#{" under your control" if under == "you"}#{extra} → #{to}"
51
69
  end
52
70
 
@@ -59,8 +77,9 @@ module Enjine
59
77
  entries = entries.select { engine.state.objects.key?(it[0]) }
60
78
  return :wait if engine.ask_entering_choices!(locals, entries) == :wait
61
79
 
80
+ # "as": the Become itself, used by the move that follows at once (never saved, Engine::Zones#enter_as!)
62
81
  enters = { "tapped" => tapped || nil, "counters" => counters.empty? ? nil : counters.dup,
63
- "face" => transformed ? 1 : nil }.compact
82
+ "face" => transformed ? 1 : nil, "as" => as }.compact
64
83
  engine.enter_battlefield!(locals, entries.map { [*it, enters.empty? ? nil : enters] }, reason: "return")
65
84
  :done
66
85
  end
@@ -19,6 +19,13 @@ module Enjine
19
19
  # What is known about an object without computing its current characteristics: its printed
20
20
  # (or last known) characteristics, its counters and whether it is a token. `chosen`: the memory of
21
21
  # the object whose choice "the chosen color/type" refers to (CR 607.2d), when the filter needs one.
22
+ # A filter matched on characteristics only (Facts) can't know who owns the object: `owner` there is an error,
23
+ # never silently ignored (CR 108.3). Returns the filter.
24
+ def self.without_owner!(filter, errors, path)
25
+ errors << "#{path}: owner can't be used here yet" if filter.respond_to?(:owner) && filter.owner
26
+ filter
27
+ end
28
+
22
29
  Facts = Data.define(:chars, :counters, :token, :chosen) do
23
30
  def initialize(chars:, counters:, token:, chosen: nil) = super
24
31
  end
@@ -79,6 +86,7 @@ module Enjine
79
86
 
80
87
  # A filter narrowed by restrictions (CR 115.1, 109.2), e.g.
81
88
  # {"creature": {"controller": "you"}} {"creature": {"other": true}}
89
+ # {"creature": {"owner": "you"}} "a creature you own" (CR 108.3); with controller: "you control but don't own"
82
90
  # {"permanent": {"not": ["land"]}} {"permanent": {"any_of": ["artifact", "creature"]}}
83
91
  # {"creature": {"attacking": true}} {"creature": {"power": {"at_least": 4}}}
84
92
  # {"permanent": {"mana_value": {"at_most": 3}}} {"creature": {"subtypes": ["human"]}}
@@ -99,7 +107,7 @@ module Enjine
99
107
  TYPE_LISTS = %w[not any_of types].freeze
100
108
  NAME_LISTS = %w[subtypes not_subtypes supertypes not_supertypes attached_subtypes].freeze
101
109
  KEYWORD_LISTS = %w[keywords not_keywords].freeze
102
- KEYS = (%w[controller counters colors] + BOOLEANS + RANGES + TYPE_LISTS + NAME_LISTS + KEYWORD_LISTS).freeze
110
+ KEYS = (%w[controller owner counters colors] + BOOLEANS + RANGES + TYPE_LISTS + NAME_LISTS + KEYWORD_LISTS).freeze
103
111
  # Lists that may be "chosen": the choice remembered under that memory key (CR 607.2d).
104
112
  CHOSEN = { "colors" => "chosen_color", "types" => "chosen_card_type", "not" => "chosen_card_type",
105
113
  "subtypes" => "chosen_type", "not_subtypes" => "chosen_type" }.freeze
@@ -112,6 +120,7 @@ module Enjine
112
120
  def self.build(base, params, errors, path)
113
121
  controller = params["controller"]
114
122
  errors << "#{path}.controller: must be you or opponent" if controller && !%w[you opponent].include?(controller)
123
+ errors << "#{path}.owner: must be you or opponent" unless [nil, "you", "opponent"].include?(params["owner"])
115
124
  validate_lists(params, errors, path)
116
125
  BOOLEANS.each do |key|
117
126
  errors << "#{path}.#{key}: must be true or false" unless params[key].nil? || [true, false].include?(params[key])
@@ -195,6 +204,8 @@ module Enjine
195
204
 
196
205
  def primitive_name = base.primitive_name
197
206
  def uses_chosen? = !@chosen.empty?
207
+ # Who must own it (CR 108.3); the layer pass checks it too (Abilities::Static#owner_ok?).
208
+ def owner = @params["owner"]
198
209
 
199
210
  # A computed bound depends on the game, so (like power) it isn't usable in the layer pass.
200
211
  def live_keys = LIVE.select { @params.key?(it) } + (@computed ? ["computed_range"] : [])
@@ -234,7 +245,7 @@ module Enjine
234
245
  words.concat(@lists["types"] + @lists["subtypes"]).concat(@lists["not_subtypes"].map { "non-#{it}" })
235
246
  words.concat(chosen_words)
236
247
  words << (@lists["any_of"].empty? ? base.primitive_name.tr("_", " ") : @lists["any_of"].join(" or "))
237
- words << { "you" => "you control", "opponent" => "an opponent controls" }[@controller]
248
+ words.concat(ownership_words)
238
249
  words.concat(@ranges.map { |key, range| "with #{key.tr("_", " ")} #{range_words(range)}" })
239
250
  words << "with a #{@lists["attached_subtypes"].join(" or ")} attached" unless @lists["attached_subtypes"].empty?
240
251
  (words + [counter_words] + keyword_words).compact.join(" ")
@@ -252,6 +263,11 @@ module Enjine
252
263
  words + { true => ["that entered this turn"], false => ["that didn't enter this turn"] }.fetch(@params["entered_this_turn"], [])
253
264
  end
254
265
 
266
+ def ownership_words
267
+ [{ "you" => "you control", "opponent" => "an opponent controls" }[@controller],
268
+ { "you" => "you own", "opponent" => "an opponent owns" }[@params["owner"]]]
269
+ end
270
+
255
271
  def range_words(range) = range.map { |k, n| format(RANGE_WORDS.fetch(k), n) }.join(" and ")
256
272
 
257
273
  def chosen_words = @chosen.keys.map { "#{"non-" if it.start_with?("not")}chosen-#{it.delete_prefix("not_")}" }
@@ -285,6 +301,7 @@ module Enjine
285
301
  owner = index || engine.state.objects[ref]&.controller
286
302
  return false if @controller && (@controller == "you") != (owner == controller)
287
303
  return true if index
304
+ return false if @params["owner"] && (@params["owner"] == "you") != (engine.object(ref).owner == controller) # CR 108.3
288
305
 
289
306
  object_allowed?(engine, ref, bounds, uses_chosen? ? Filters.chosen_memory(engine, source, ctx) : nil)
290
307
  end
@@ -37,7 +37,7 @@ module Enjine
37
37
  unless controller.nil? || CONTROLLERS.include?(controller)
38
38
  errors << "#{path}.controller: must be one of #{CONTROLLERS.join(", ")}"
39
39
  end
40
- source &&= Mechanics.build_entry(:filter, source, errors, "#{path}.source")
40
+ source &&= Mechanics.characteristic_filter(source, errors, "#{path}.source")
41
41
  Narrowed.new(kind, controller, source)
42
42
  end
43
43
 
@@ -10,11 +10,14 @@ module Enjine
10
10
  # the battlefield it describes the object as it last existed, CR 603.10a).
11
11
  # What an event tells a triggered ability about itself (CR 603.2): the object and player it is
12
12
  # about and a number ("that many"). Effects name them with {"triggering": "object" | "player"}
13
- # and the amount {"triggering": "amount"}; "card" is the object's card, for conditions.
13
+ # and the amount {"triggering": "amount"}; "card" is the object's card, for conditions, and "types" the card
14
+ # types a permanent that left had as it last existed ("if it was a creature", CR 603.10a).
14
15
  def self.event_facts(engine, event)
15
16
  facts = {}
16
17
  case event
17
- when Events::ZoneChanged then facts.merge!("object" => event.new_id, "player" => engine.names.index(event.owner))
18
+ when Events::ZoneChanged
19
+ facts.merge!("object" => event.new_id, "player" => engine.names.index(event.owner))
20
+ facts["types"] = engine.last_known(event.old_id)&.fetch("types")&.map(&:to_s) if event.from == "battlefield"
18
21
  when Events::SpellCast, Events::DilemmaFaced, Events::Tapped, Events::AttackerDeclared, Events::BecameBlocked,
19
22
  Events::BlockerDeclared
20
23
  facts["object"] = event.object
@@ -100,6 +103,8 @@ module Enjine
100
103
  defn = engine.catalog.fetch(card)
101
104
  chars = types ? defn.printed_characteristics.with(types: types) : defn.printed_characteristics
102
105
  lki = engine.last_known(object_id) || {}
106
+ return false unless owner_ok?(lki.fetch("owner", object_controller), source["controller"]) # CR 108.3
107
+
103
108
  token = lki.fetch("token") { defn.token? } # a token copy of a card (CR 111.1)
104
109
  @filter.matches_facts?(Filters::Facts.new(chars: chars, counters: lki.fetch("counters", nil) || {}, token: token,
105
110
  chosen: engine.state.objects[source["id"]]&.memory))
@@ -109,6 +114,12 @@ module Enjine
109
114
  @controller == "any" || (@controller == "you") == (object_controller == you)
110
115
  end
111
116
 
117
+ # "A creature you own" (CR 108.3), for an object that left: its owner as it last existed.
118
+ def owner_ok?(owner, you)
119
+ wanted = @filter.respond_to?(:owner) && @filter.owner
120
+ !wanted || (wanted == "you") == (owner == you)
121
+ end
122
+
112
123
  def to_s
113
124
  return "this" if @kind == :self
114
125
  return "the attached creature" if @kind == :attached
@@ -211,7 +222,7 @@ module Enjine
211
222
  # Matching a card (not a permanent) by its printed characteristics.
212
223
  module CardMatch
213
224
  def setup_card(params, errors, path)
214
- @card = params["card"] && Mechanics.build_entry(:filter, params["card"], errors, "#{path}.card")
225
+ @card = params["card"] && Mechanics.characteristic_filter(params["card"], errors, "#{path}.card")
215
226
  end
216
227
 
217
228
  def card_ok?(engine, card)
@@ -187,7 +187,7 @@ module Enjine
187
187
 
188
188
  def setup(params, errors, path)
189
189
  super
190
- @source = params["source"] && Mechanics.build_entry(:filter, params["source"], errors, "#{path}.source")
190
+ @source = params["source"] && Mechanics.characteristic_filter(params["source"], errors, "#{path}.source")
191
191
  setup_chosen(params, errors, path)
192
192
  @kinds = KINDS.select { |key, _| params[key] == true }.values
193
193
  KINDS.each_key { errors << "#{path}.#{it}: must be true or false" unless [nil, true, false].include?(params[it]) }
@@ -62,21 +62,25 @@ module Enjine
62
62
  params: { "subject" => "\"self\" (default) or {\"each\": filter, ...}", "counter" => "counter name",
63
63
  "first_time_this_turn" => "true: only the first time each turn such counters are put on " \
64
64
  "that permanent (an earlier placement counts even if nothing " \
65
- "could trigger then)" },
66
- example: { "counters_placed" => { "counter" => "+1/+1" } }
65
+ "could trigger then)",
66
+ "by" => "you | opponent: only counters that player puts (\"whenever you put ...\", CR 122.6a; " \
67
+ "a permanent's controller puts the counters it enters with)" },
68
+ example: { "counters_placed" => { "counter" => "+1/+1", "by" => "you" } }
67
69
 
68
- def self.keys = %w[subject counter first_time_this_turn]
70
+ def self.keys = %w[subject counter first_time_this_turn by]
69
71
  def watches?(event) = event.is_a?(Events::CountersChanged) || event.is_a?(Events::ZoneChanged)
70
72
 
71
73
  def setup(params, errors, path)
72
74
  @counter = params["counter"] && Counters.parse(params["counter"], errors, "#{path}.counter")
73
75
  @first_time = params["first_time_this_turn"]
74
76
  errors << "#{path}.first_time_this_turn: must be true or false" unless [nil, true, false].include?(@first_time)
77
+ @by = params["by"]
78
+ errors << "#{path}.by: must be you or opponent" unless [nil, "you", "opponent"].include?(@by)
75
79
  end
76
80
 
77
81
  def occurrences(engine, event, source)
78
82
  id, amount = placed(engine, event)
79
- return [] unless amount&.positive?
83
+ return [] unless amount&.positive? && put_by_ok?(engine, event, id, source)
80
84
  return [] if @first_time && engine.counter_placements_this_turn(id, @counter) != 1
81
85
 
82
86
  obj = engine.object(id)
@@ -89,11 +93,23 @@ module Enjine
89
93
 
90
94
  def to_s
91
95
  first = " for the first time each turn" if @first_time
92
- "Whenever one or more #{"#{@counter} " if @counter}counters are put on #{@subject}#{first}"
96
+ return "Whenever one or more #{"#{@counter} " if @counter}counters are put on #{@subject}#{first}" unless @by
97
+
98
+ who = @by == "you" ? "you put" : "an opponent puts"
99
+ "Whenever #{who} one or more #{"#{@counter} " if @counter}counters on #{@subject}#{first}"
93
100
  end
94
101
 
95
102
  private
96
103
 
104
+ # CR 122.6a: who put the counters, relative to the source's controller. Counters a permanent enters with are
105
+ # put by its controller.
106
+ def put_by_ok?(engine, event, id, source)
107
+ return true unless @by
108
+
109
+ putter = event.is_a?(Events::CountersChanged) ? engine.names.index(event.by) : engine.object(id).controller
110
+ (@by == "you") == (putter == source["controller"])
111
+ end
112
+
97
113
  # [object id, how many matching counters were put on it] for this event, or nil.
98
114
  def placed(engine, event)
99
115
  if event.is_a?(Events::CountersChanged)
@@ -227,7 +227,7 @@ module Enjine
227
227
  super
228
228
  @batched = params.fetch("batched", false)
229
229
  errors << "#{path}.batched: must be true or false" unless [true, false].include?(@batched)
230
- @card = params["card"] && Mechanics.build_entry(:filter, params["card"], errors, "#{path}.card")
230
+ @card = params["card"] && Mechanics.characteristic_filter(params["card"], errors, "#{path}.card")
231
231
  end
232
232
 
233
233
  def match?(engine, event, source)
@@ -354,7 +354,7 @@ module Enjine
354
354
  data = data.is_a?(Hash) ? data.transform_keys(&:to_s) : {}
355
355
  Mechanics.check_keys(data, %w[by matching from not_from], errors, path)
356
356
  casters = Selector.build(data.fetch("by", "you"), errors, "#{path}.by", **context)
357
- filter = data["matching"] && Mechanics.build_entry(:filter, data["matching"], errors, "#{path}.matching")
357
+ filter = data["matching"] && Mechanics.characteristic_filter(data["matching"], errors, "#{path}.matching")
358
358
  new(casters, filter, CastFrom.build(data, errors, path), LOGS.fetch(kind))
359
359
  end
360
360
 
@@ -401,7 +401,7 @@ module Enjine
401
401
  errors << "#{path}: must be an object {\"each\": filter, \"controller\": ...}" unless data.is_a?(Hash)
402
402
  data = data.is_a?(Hash) ? data.transform_keys(&:to_s) : {}
403
403
  Mechanics.check_keys(data, %w[each controller], errors, path)
404
- filter = data.key?("each") ? Mechanics.build_entry(:filter, data["each"], errors, "#{path}.each") : nil
404
+ filter = data.key?("each") ? Mechanics.characteristic_filter(data["each"], errors, "#{path}.each") : nil
405
405
  controller = data.fetch("controller", "any")
406
406
  errors << "#{path}.controller: must be one of you, opponent, any" unless %w[you opponent any].include?(controller)
407
407
  new(DEPARTED.fetch(kind), filter, controller)
@@ -697,7 +697,7 @@ module Enjine
697
697
  class TriggeringIs
698
698
  def self.build(data, errors, path)
699
699
  Mechanics.check_keys(data, %w[triggering], errors, path)
700
- new(Mechanics.build_entry(:filter, data["triggering"], errors, "#{path}.triggering"))
700
+ new(Mechanics.characteristic_filter(data["triggering"], errors, "#{path}.triggering"))
701
701
  end
702
702
 
703
703
  def initialize(filter)
@@ -705,12 +705,15 @@ module Enjine
705
705
  freeze
706
706
  end
707
707
 
708
+ # A permanent that left is matched with the card types it last had (CR 603.10a: "if it was a creature").
708
709
  def true?(engine, ctx)
709
- card = Selector.memory(engine, ctx).fetch("triggering", {})["card"]
710
- return false unless card && @filter
710
+ facts = Selector.memory(engine, ctx).fetch("triggering", {})
711
+ return false unless facts["card"] && @filter
711
712
 
712
- defn = engine.catalog.fetch(card)
713
- @filter.matches_facts?(Filters::Facts.new(chars: defn.printed_characteristics, counters: {}, token: defn.token?))
713
+ defn = engine.catalog.fetch(facts["card"])
714
+ chars = defn.printed_characteristics
715
+ chars = chars.with(types: facts["types"].map(&:to_sym)) if facts["types"]
716
+ @filter.matches_facts?(Filters::Facts.new(chars: chars, counters: {}, token: defn.token?))
714
717
  end
715
718
 
716
719
  def to_s = "it's a #{@filter.respond_to?(:primitive_name) ? @filter.primitive_name.tr("_", " ") : @filter}"
@@ -7,6 +7,11 @@ module Enjine
7
7
 
8
8
  # Builds a primitive from a data entry. An entry is either a bare name ("tap")
9
9
  # or a single-key Hash ({"mana" => {...params}}).
10
+ # A filter matched by characteristics only (Filters::Facts): `owner` can't be honoured there and is an error.
11
+ def characteristic_filter(data, errors, path)
12
+ Filters.without_owner!(build_entry(:filter, data, errors, path), errors, path)
13
+ end
14
+
10
15
  def build_entry(category, entry, errors, path, **context)
11
16
  name, params = split_entry(entry)
12
17
  unless name
data/lib/enjine/player.rb CHANGED
@@ -17,7 +17,8 @@ module Enjine
17
17
  }
18
18
  end
19
19
 
20
- def ready? = @deck.valid?
20
+ # Whether the deck is legal for `format` (Enjine::Format).
21
+ def ready?(format = Format::CONSTRUCTED) = @deck.valid?(format)
21
22
 
22
23
  # True while any game this player is seated in has not ended.
23
24
  def playing? = @games.keys.any? { |game| !game.over? }
@@ -22,6 +22,8 @@ module Enjine
22
22
  @stats = Hash.new(0)
23
23
  @lands_this_turn = Hash.new(0)
24
24
  @loyalty_activations = Hash.new(0)
25
+ @commander_casts = Hash.new(0)
26
+ @cast_from_command = []
25
27
  end
26
28
 
27
29
  def state = @game.state
@@ -109,6 +111,7 @@ module Enjine
109
111
 
110
112
  def zone_changed!(event)
111
113
  land_played!(event) if event.reason == "play_land"
114
+ @cast_from_command << event.new_id if event.reason == "cast" && event.from == "command"
112
115
  return unless event.reason == "resolve" && event.to == "graveyard" && defn(event.card).permanent?
113
116
 
114
117
  finding("608.3", "permanent spell #{event.card} went to the graveyard on resolution")
@@ -144,6 +147,7 @@ module Enjine
144
147
  def spell_cast!(event)
145
148
  card = defn(event.card)
146
149
  @stats["cast:#{event.card}"] += 1
150
+ @commander_casts[event.player] += 1 if @cast_from_command.delete(event.object) # CR 903.8
147
151
  return if card.instant_speed? || event.during_resolution # CR 608.2g: timing doesn't apply
148
152
 
149
153
  caster = event.player
@@ -181,7 +185,7 @@ module Enjine
181
185
  end
182
186
 
183
187
  # Permanents and emblems, whose static abilities function in the command zone (CR 114.4).
184
- def static_holders = state.battlefield + (state.command || [])
188
+ def static_holders = state.battlefield + state.emblem_ids
185
189
 
186
190
  # The static abilities of permanents on the battlefield and emblems that apply to `obj` now, matched on printed types.
187
191
  def applying_statics(obj)
@@ -252,6 +256,26 @@ module Enjine
252
256
  check_damage_life!
253
257
  check_sba! if @game.pending_decision&.kind == "priority"
254
258
  check_hand_size!
259
+ check_commander!
260
+ end
261
+
262
+ # CR 903.8, 903.10a, independently of the engine's counters: the casts from the command zone seen as events
263
+ # are the tax's count, and nobody survives 21 combat damage from one commander.
264
+ def check_commander!
265
+ return unless state.format.commander?
266
+
267
+ state.players.each do |pl|
268
+ casts = @commander_casts[pl.name]
269
+ if pl.commander_casts.to_i != casts
270
+ finding("903.8",
271
+ "#{pl.name} has cast their commander #{casts} times but the tax counts #{pl.commander_casts.to_i}")
272
+ end
273
+ next if pl.lost || state.over?
274
+
275
+ finding("903.10a", "#{pl.name} survived 21 combat damage from one commander") if (pl.commander_damage || {}).values.any? do
276
+ it >= 21
277
+ end
278
+ end
255
279
  end
256
280
 
257
281
  # CR 613.4, recomputed independently: printed (or a 7b setting effect) + counters + active pump effects
@@ -283,9 +307,11 @@ module Enjine
283
307
  card.creature? && !card.defined_pt? && applying_statics(obj).none?(&:base_power_toughness) && !types_replaced?(obj)
284
308
  end
285
309
 
286
- # Creature types an effect replaced (layer 4): the monitor matches statics against printed types.
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.
287
312
  def types_replaced?(obj)
288
- state.effects.any? { %w[creature_types assimilate].include?(it["kind"]) && Array(it["affected"]).include?(obj.id) }
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) }
289
315
  end
290
316
 
291
317
  # CR 613.4b: the latest setting effect, else as printed, or as a copy's exceptions or a token's creating