enjine 0.2.0 → 0.3.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -1
- data/README.md +2 -1
- data/docs/ARCHITECTURE.md +2 -0
- data/docs/DESIGN.md +12 -5
- data/docs/MECHANICS_COVERAGE.md +32 -4
- data/docs/guides/api.md +51 -0
- data/docs/guides/card-data-format.md +21 -1
- data/docs/guides/deck-builder.md +18 -0
- data/docs/plans/commander.md +63 -0
- data/docs/reference/decisions.md +5 -1
- data/lib/enjine/actions/cast.rb +2 -1
- data/lib/enjine/card_definition.rb +24 -3
- data/lib/enjine/cli/text.rb +23 -1
- data/lib/enjine/cli.rb +2 -1
- data/lib/enjine/color_identity.rb +38 -0
- data/lib/enjine/decisions/commander_zone.rb +36 -0
- data/lib/enjine/deck.rb +73 -14
- data/lib/enjine/deck_builder.rb +62 -18
- data/lib/enjine/engine/casting/alt_casting.rb +1 -0
- data/lib/enjine/engine/casting/permissions.rb +2 -2
- data/lib/enjine/engine/commander.rb +125 -0
- data/lib/enjine/engine/damage.rb +1 -0
- data/lib/enjine/engine/emblems.rb +1 -1
- data/lib/enjine/engine/pregame.rb +1 -0
- data/lib/enjine/engine/priority.rb +1 -0
- data/lib/enjine/engine/rules.rb +1 -1
- data/lib/enjine/engine/state_based_actions.rb +1 -0
- data/lib/enjine/engine/triggers.rb +1 -1
- data/lib/enjine/engine/zones.rb +1 -0
- data/lib/enjine/format.rb +53 -0
- data/lib/enjine/fuzz.rb +17 -0
- data/lib/enjine/game.rb +4 -3
- data/lib/enjine/player.rb +2 -1
- data/lib/enjine/rules_monitor.rb +25 -1
- data/lib/enjine/setup.rb +10 -4
- data/lib/enjine/snapshot/player_checks.rb +47 -0
- data/lib/enjine/snapshot.rb +4 -1
- data/lib/enjine/state.rb +33 -14
- data/lib/enjine/version.rb +1 -1
- data/lib/enjine/view.rb +18 -1
- data/lib/enjine.rb +2 -0
- metadata +6 -1
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Enjine
|
|
4
|
+
module Decisions
|
|
5
|
+
# CR 903.9: a commander went to a graveyard, exile, hand or library; its owner chooses whether to put it
|
|
6
|
+
# into the command zone instead (Engine::Commander).
|
|
7
|
+
class CommanderZone < Base
|
|
8
|
+
def self.doc
|
|
9
|
+
'Your commander is in a graveyard, exile, hand or library: `{type: "command", object: id}` puts it into ' \
|
|
10
|
+
'the command zone, `{type: "stay", object: id}` leaves it where it is (CR 903.9).'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.options(_engine, pending)
|
|
14
|
+
[{ type: "command", object: pending["object"] }, { type: "stay", object: pending["object"] }]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.submit(engine, _pending, option) = engine.choose_commander_zone!(option[:type] == "command")
|
|
18
|
+
end
|
|
19
|
+
register("commander_zone", CommanderZone)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
module Frames
|
|
23
|
+
# The owner's choice to send a commander back to the command zone (CR 903.9), before priority.
|
|
24
|
+
class CommanderZone < Base
|
|
25
|
+
def self.step(engine, frame) = engine.step_commander_zone!(frame)
|
|
26
|
+
def self.decisions = %w[commander_zone]
|
|
27
|
+
|
|
28
|
+
def self.validate(frame, data, errors)
|
|
29
|
+
errors << "frames: commander_zone object #{frame["object"]} doesn't exist" unless data["objects"].any? do
|
|
30
|
+
it["id"] == frame["object"]
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
register("commander_zone", CommanderZone)
|
|
35
|
+
end
|
|
36
|
+
end
|
data/lib/enjine/deck.rb
CHANGED
|
@@ -11,16 +11,24 @@ module Enjine
|
|
|
11
11
|
# that is not exempt (basic lands). An invalid deck can still be built and edited;
|
|
12
12
|
# it is simply not #valid?, so its player is not ready.
|
|
13
13
|
#
|
|
14
|
+
# A Commander deck (CR 903) names its commander up front, beside the 99 cards of `main`:
|
|
15
|
+
#
|
|
16
|
+
# Deck.from_h({"cards" => [...], "main" => [...], "commander" => "Wort, the Raidmother"})
|
|
17
|
+
#
|
|
18
|
+
# Which rules apply depends on the game's Format: `errors(format)` checks the deck against it, so the
|
|
19
|
+
# same deck can be valid for one format and not another.
|
|
20
|
+
#
|
|
14
21
|
# Outside the game (CR 400.11, 400.11a): the engine doesn't track a match, so a deck carries the cards
|
|
15
22
|
# its player owns outside the game as `outside`: a constructed sideboard of at most 15 cards (CR 100.4a).
|
|
16
23
|
# The four-copy limit applies to the deck and the outside list together, and outside cards don't count
|
|
17
24
|
# toward the 60. Effects that bring a card into the game from outside it take the card from this list.
|
|
18
25
|
class Deck
|
|
19
|
-
MIN_SIZE =
|
|
26
|
+
MIN_SIZE = Format::CONSTRUCTED.deck_size.min
|
|
20
27
|
MAX_COPIES = 4
|
|
21
28
|
MAX_OUTSIDE = 15
|
|
29
|
+
NOT_A_COMMANDER = "can't be a commander; it must be a legendary creature, Vehicle or Spacecraft (CR 903.3)"
|
|
22
30
|
|
|
23
|
-
attr_reader :catalog
|
|
31
|
+
attr_reader :catalog, :commander # commander: a definition id or nil (CR 903.3)
|
|
24
32
|
attr_writer :guard
|
|
25
33
|
|
|
26
34
|
def self.from_h(hash)
|
|
@@ -31,10 +39,13 @@ module Enjine
|
|
|
31
39
|
lists = %w[cards main outside].to_h { [it, doc.fetch(it, [])] }
|
|
32
40
|
lists.each { |key, list| raise InvalidDeck.new(["#{key}: must be a list"]) unless list.is_a?(Array) }
|
|
33
41
|
|
|
34
|
-
|
|
42
|
+
commander = doc["commander"]
|
|
43
|
+
raise InvalidDeck.new(["commander: must be a card name"]) unless commander.nil? || commander.is_a?(String)
|
|
44
|
+
|
|
45
|
+
new(cards: lists["cards"], main: lists["main"], outside: lists["outside"], commander: commander)
|
|
35
46
|
end
|
|
36
47
|
|
|
37
|
-
def initialize(cards: [], main: [], outside: [])
|
|
48
|
+
def initialize(cards: [], main: [], outside: [], commander: nil)
|
|
38
49
|
@catalog = Catalog.new
|
|
39
50
|
cards.each { @catalog.add(it) }
|
|
40
51
|
@entries = {}
|
|
@@ -42,6 +53,7 @@ module Enjine
|
|
|
42
53
|
@reference_errors = []
|
|
43
54
|
main.each_with_index { |entry, i| load_entry(entry, i, @entries, "main") }
|
|
44
55
|
outside.each_with_index { |entry, i| load_entry(entry, i, @outside, "outside") }
|
|
56
|
+
load_commander(commander)
|
|
45
57
|
end
|
|
46
58
|
|
|
47
59
|
# Ordered {id => count}.
|
|
@@ -57,20 +69,16 @@ module Enjine
|
|
|
57
69
|
def outside_size = @outside.values.sum
|
|
58
70
|
def outside_ids = @outside.flat_map { |id, n| [id] * n }
|
|
59
71
|
|
|
60
|
-
|
|
72
|
+
# What is wrong with the deck for `format`, as messages (empty when it is legal).
|
|
73
|
+
def errors(format = Format::CONSTRUCTED)
|
|
61
74
|
errs = @reference_errors.dup
|
|
62
|
-
errs
|
|
75
|
+
errs.concat(size_errors(format))
|
|
63
76
|
errs << "outside: #{outside_size} cards; at most #{MAX_OUTSIDE} are allowed (CR 100.4a)" if outside_size > MAX_OUTSIDE
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
next if limit.nil? || n <= limit
|
|
67
|
-
|
|
68
|
-
errs << "#{@catalog.fetch(id).name}: #{n} copies; at most #{limit} are allowed (CR 100.2a#{", 100.4a" if @outside.key?(id)})"
|
|
69
|
-
end
|
|
70
|
-
errs
|
|
77
|
+
errs.concat(copy_errors(format))
|
|
78
|
+
errs.concat(commander_errors(format))
|
|
71
79
|
end
|
|
72
80
|
|
|
73
|
-
def valid? = errors.empty?
|
|
81
|
+
def valid?(format = Format::CONSTRUCTED) = errors(format).empty?
|
|
74
82
|
|
|
75
83
|
# Adds a card definition this deck can then use.
|
|
76
84
|
def define(definition)
|
|
@@ -116,11 +124,62 @@ module Enjine
|
|
|
116
124
|
# Tokens aren't listed: the cards that create them bring them back (two tokens may share a name).
|
|
117
125
|
doc = { "cards" => @catalog.reject(&:token?).map(&:to_h), "main" => @entries.map { |id, n| { "card" => id, "count" => n } } }
|
|
118
126
|
doc["outside"] = @outside.map { |id, n| { "card" => id, "count" => n } } unless @outside.empty?
|
|
127
|
+
doc["commander"] = @commander if @commander
|
|
119
128
|
doc
|
|
120
129
|
end
|
|
121
130
|
|
|
122
131
|
private
|
|
123
132
|
|
|
133
|
+
# Cards in the deck, its commander included (CR 903.5a).
|
|
134
|
+
def total_size = size + (@commander ? 1 : 0)
|
|
135
|
+
|
|
136
|
+
def size_errors(format)
|
|
137
|
+
return [] if format.deck_size.cover?(total_size)
|
|
138
|
+
|
|
139
|
+
if format.commander?
|
|
140
|
+
["deck has #{total_size} cards; exactly #{format.deck_size.min} are required, its commander included (CR 903.5a)"]
|
|
141
|
+
else
|
|
142
|
+
["deck has #{size} cards; at least #{format.deck_size.min} are required (CR 100.2a)"]
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# The copy limit counts the deck and the cards outside the game together (CR 100.4a), and the commander (CR 903.5b).
|
|
147
|
+
def copy_errors(format)
|
|
148
|
+
counts = @entries.merge(@outside) { |_, a, b| a + b }
|
|
149
|
+
counts.merge!(@commander => 1) { |_, a, b| a + b } if @commander
|
|
150
|
+
counts.filter_map do |id, n|
|
|
151
|
+
defn = @catalog.fetch(id)
|
|
152
|
+
limit = format.copy_limit(defn) # nil: any number (basic lands, "deck_limit": "any")
|
|
153
|
+
next if limit.nil? || n <= limit
|
|
154
|
+
|
|
155
|
+
rule = format.commander? ? "CR 903.5b" : "CR 100.2a#{", 100.4a" if @outside.key?(id)}"
|
|
156
|
+
"#{defn.name}: #{n} copies; at most #{limit} are allowed (#{rule})"
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# A Commander deck has a commander that may lead it, and every card's color identity fits it (CR 903.3, 903.5c).
|
|
161
|
+
def commander_errors(format)
|
|
162
|
+
return @commander ? ["commander: only a Commander deck has one (CR 903.3)"] : [] unless format.commander?
|
|
163
|
+
return ["a Commander deck needs a commander (CR 903.3)"] unless @commander
|
|
164
|
+
|
|
165
|
+
lead = @catalog.fetch(@commander)
|
|
166
|
+
errs = Format.commander_card?(lead) ? [] : ["#{lead.name}: #{NOT_A_COMMANDER}"]
|
|
167
|
+
(@entries.keys + @outside.keys).uniq.each do |id|
|
|
168
|
+
outside = @catalog.fetch(id).color_identity - lead.color_identity
|
|
169
|
+
next if outside.empty?
|
|
170
|
+
|
|
171
|
+
errs << "#{@catalog.fetch(id).name}: color identity #{Mana.symbols(outside)} is not in its commander's (CR 903.5c)"
|
|
172
|
+
end
|
|
173
|
+
errs
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def load_commander(card)
|
|
177
|
+
return if card.nil?
|
|
178
|
+
return @reference_errors << "commander: #{card.inspect} is not defined in cards" unless @catalog.include?(card)
|
|
179
|
+
|
|
180
|
+
@commander = CardDefinition.slug(card)
|
|
181
|
+
end
|
|
182
|
+
|
|
124
183
|
def load_entry(entry, index, into, list)
|
|
125
184
|
card, count = entry.is_a?(Hash) ? entry.transform_keys(&:to_s).values_at("card", "count") : []
|
|
126
185
|
unless card.is_a?(String) && count.is_a?(Integer) && count.positive?
|
data/lib/enjine/deck_builder.rb
CHANGED
|
@@ -18,6 +18,11 @@ module Enjine
|
|
|
18
18
|
# never holds more than n copies (and never more than CR 100.2a allows). Basic lands are always
|
|
19
19
|
# available: the pool's own, else built-in ones.
|
|
20
20
|
#
|
|
21
|
+
# With `commander:` it builds a Duel Commander deck (CR 903) instead: 99 distinct cards inside that
|
|
22
|
+
# commander's color identity, plus basic lands of its colors:
|
|
23
|
+
#
|
|
24
|
+
# builder.build(commander: "Wort, the Raidmother", seed: 3)
|
|
25
|
+
#
|
|
21
26
|
# #build returns a deck document ({"name", "description", "cards", "main"}) that
|
|
22
27
|
# Enjine::Player.new(name:, deck:) accepts. Same pool, arguments and seed, same deck: randomness
|
|
23
28
|
# comes only from Enjine::Rng, and the pool is sorted by card id, so its order doesn't matter.
|
|
@@ -136,8 +141,11 @@ module Enjine
|
|
|
136
141
|
# including: pool cards (names or ids) the deck must hold, as many copies as allowed up to 4; they must fit
|
|
137
142
|
# the colors (a land: produce only deck colors). They define the deck, so a chosen color then
|
|
138
143
|
# needs no card of its own: it may only bring basics.
|
|
139
|
-
|
|
140
|
-
|
|
144
|
+
# commander: a pool card (name or id) that may lead a deck (CR 903.3): the deck is a legal Duel Commander
|
|
145
|
+
# deck inside its color identity (CR 903.5), and `colors` and `size` come from it.
|
|
146
|
+
def build(seed:, colors: :any, size: BASE_SIZE, profile: {}, including: [], commander: nil)
|
|
147
|
+
Draft.new(self, seed: seed, colors: colors, size: size, profile: profile, including: including,
|
|
148
|
+
commander: commander).deck
|
|
141
149
|
end
|
|
142
150
|
|
|
143
151
|
# Copies of a card the pool holds: nil when uncapped, 0 when it isn't in the pool.
|
|
@@ -165,22 +173,24 @@ module Enjine
|
|
|
165
173
|
|
|
166
174
|
# One #build call: the seed-dependent choices for one deck.
|
|
167
175
|
class Draft
|
|
168
|
-
def initialize(builder, seed:, colors:, size:, profile:, including:)
|
|
169
|
-
raise ArgumentError, "size must be an Integer of at least #{Deck::MIN_SIZE}" unless size.is_a?(Integer) && size >= Deck::MIN_SIZE
|
|
170
|
-
|
|
176
|
+
def initialize(builder, seed:, colors:, size:, profile:, including:, commander: nil)
|
|
171
177
|
@builder = builder
|
|
172
178
|
@pool = builder.cards
|
|
173
179
|
@rng = Rng.from_seed(seed)
|
|
174
180
|
@seed = seed
|
|
175
|
-
@
|
|
176
|
-
@
|
|
177
|
-
@
|
|
181
|
+
@commander = commander && lead_with(commander, colors, size)
|
|
182
|
+
@size = @commander ? Format::DUEL_COMMANDER.deck_size.min - 1 : check_size(size) # the commander is the 100th card
|
|
183
|
+
@profile = merge_profile(@commander ? profile.merge(copies: SINGLETON_COPIES) : profile)
|
|
184
|
+
@colors = @commander ? @commander.color_identity : resolve_colors(colors)
|
|
178
185
|
@creatures = {}
|
|
179
186
|
@others = {}
|
|
180
187
|
@lands = {}
|
|
181
188
|
include_cards(including)
|
|
182
189
|
end
|
|
183
190
|
|
|
191
|
+
# CR 903.5b: one copy of each card.
|
|
192
|
+
SINGLETON_COPIES = { 4 => 0, 3 => 0, 2 => 0, 1 => 1 }.freeze
|
|
193
|
+
|
|
184
194
|
def deck
|
|
185
195
|
check_colors! if @creatures.empty? && @others.empty? && @lands.empty?
|
|
186
196
|
lands = [scaled(sample(@profile[:lands])), @lands.values.sum].max
|
|
@@ -193,6 +203,34 @@ module Enjine
|
|
|
193
203
|
|
|
194
204
|
# --- inputs -------------------------------------------------------------------------------------
|
|
195
205
|
|
|
206
|
+
def check_size(size)
|
|
207
|
+
raise ArgumentError, "size must be an Integer of at least #{Deck::MIN_SIZE}" unless size.is_a?(Integer) && size >= Deck::MIN_SIZE
|
|
208
|
+
|
|
209
|
+
size
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# The pool card that leads the deck (CR 903.3). Its colors and the deck's size come from the format.
|
|
213
|
+
def lead_with(card, colors, size)
|
|
214
|
+
raise ArgumentError, "a Commander deck's colors and size come from its commander" unless colors == :any && size == BASE_SIZE
|
|
215
|
+
|
|
216
|
+
id = CardDefinition.slug(card)
|
|
217
|
+
raise ArgumentError, "#{card.inspect} is not in the pool" if @builder.count(id)&.zero?
|
|
218
|
+
|
|
219
|
+
@builder.definition(id).tap do |defn|
|
|
220
|
+
raise ArgumentError, "#{defn.name} can't be a commander (CR 903.3)" unless Format.commander_card?(defn)
|
|
221
|
+
if defn.color_identity.empty?
|
|
222
|
+
raise ArgumentError,
|
|
223
|
+
"#{defn.name} is colorless: a colorless commander needs Wastes, which the builder doesn't add"
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# Whether a card may go in a deck of these colors; a Commander deck needs its color identity inside the
|
|
229
|
+
# commander's (CR 903.5c-d) too.
|
|
230
|
+
def fits?(definition, colors = @colors)
|
|
231
|
+
DeckBuilder.fits?(definition, colors) && (@commander.nil? || (definition.color_identity - @colors).empty?)
|
|
232
|
+
end
|
|
233
|
+
|
|
196
234
|
def merge_profile(overrides)
|
|
197
235
|
unknown = overrides.keys.map(&:to_sym) - DEFAULT_PROFILE.keys
|
|
198
236
|
raise ArgumentError, "unknown profile keys: #{unknown.join(", ")}" unless unknown.empty?
|
|
@@ -229,12 +267,12 @@ module Enjine
|
|
|
229
267
|
capacity(spells) >= @size - scaled(@profile[:lands][:outliers].max)
|
|
230
268
|
end
|
|
231
269
|
|
|
232
|
-
def spells_for(colors) = @pool.select { it.castable? &&
|
|
270
|
+
def spells_for(colors) = @pool.select { it.castable? && fits?(it, colors) && it != @commander }
|
|
233
271
|
def capacity(definitions) = definitions.sum { limit(it) }
|
|
234
272
|
|
|
235
273
|
# Most copies of a card the deck may hold: 4 (CR 100.2a) or any number if exempt, and never
|
|
236
274
|
# more than the pool holds.
|
|
237
|
-
def limit(definition) = [definition.deck_limit || @size, @builder.count(definition.id) || @size].min
|
|
275
|
+
def limit(definition) = [@commander ? 1 : definition.deck_limit || @size, @builder.count(definition.id) || @size].min
|
|
238
276
|
|
|
239
277
|
# Puts each required card in the deck with as many copies as it may hold, up to 4 (basics come anyway).
|
|
240
278
|
def include_cards(cards)
|
|
@@ -243,7 +281,7 @@ module Enjine
|
|
|
243
281
|
raise ArgumentError, "#{card.inspect} is not in the pool" if @builder.count(id)&.zero?
|
|
244
282
|
|
|
245
283
|
defn = @builder.definition(id)
|
|
246
|
-
fits = defn.land? ?
|
|
284
|
+
fits = defn.land? ? lands_fit?(defn) : fits?(defn)
|
|
247
285
|
raise ArgumentError, "#{defn.name} does not fit a #{@colors.join} deck" unless fits
|
|
248
286
|
next if DeckBuilder.basic?(defn) # every deck has its basics
|
|
249
287
|
|
|
@@ -376,8 +414,11 @@ module Enjine
|
|
|
376
414
|
end
|
|
377
415
|
|
|
378
416
|
def nonbasic_land?(definition)
|
|
379
|
-
definition.land? && !definition.supertype?(:basic) && !definition.mana_abilities.empty? &&
|
|
380
|
-
|
|
417
|
+
definition.land? && !definition.supertype?(:basic) && !definition.mana_abilities.empty? && lands_fit?(definition)
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
def lands_fit?(definition)
|
|
421
|
+
DeckBuilder.produced_colors(definition).all? { @colors.include?(it) } && fits?(definition)
|
|
381
422
|
end
|
|
382
423
|
|
|
383
424
|
# Weight 4 for lands that produce two or more deck colors (duals), 2 for one color, 1 for colorless-only.
|
|
@@ -408,11 +449,14 @@ module Enjine
|
|
|
408
449
|
nonbasic = pick_nonbasic_lands(lands)
|
|
409
450
|
basics = split_basics(lands - nonbasic.values.sum)
|
|
410
451
|
main = basics.map { |color, n| [basic_land(color), n] } + sorted(nonbasic) + spell_entries
|
|
411
|
-
{ "name" => "Random #{@colors.join} deck",
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
452
|
+
doc = { "name" => "Random #{@colors.join} deck",
|
|
453
|
+
"description" => "random deck: colors=#{@colors.join} seed=#{@seed} — #{lands} lands, " \
|
|
454
|
+
"#{@creatures.values.sum} creatures, #{@others.values.sum} other spells, #{main.size} distinct cards",
|
|
455
|
+
"cards" => main.map { it[0].to_h },
|
|
456
|
+
"main" => main.map { |defn, n| { "card" => defn.name, "count" => n } } }
|
|
457
|
+
return doc unless @commander
|
|
458
|
+
|
|
459
|
+
doc.merge("cards" => doc["cards"] + [@commander.to_h], "commander" => @commander.name)
|
|
416
460
|
end
|
|
417
461
|
|
|
418
462
|
def sorted(chosen)
|
|
@@ -143,6 +143,7 @@ module Enjine
|
|
|
143
143
|
# trigger (CR 702.185a); the creatures that convoked it (CR 702.51c).
|
|
144
144
|
def record_cast_how!(obj, ctx)
|
|
145
145
|
obj.memory["cast_from"] = ctx.dig("origin", "zone") || "hand"
|
|
146
|
+
record_commander_cast!(ctx["player"], ctx["method"]) # CR 903.8 (Engine::Commander)
|
|
146
147
|
obj.memory["convoked"] = ctx["convoked"].dup if ctx["convoked"]
|
|
147
148
|
return unless ctx.dig("method", "alt") # mayhem without a cost: no keyword cost was paid
|
|
148
149
|
|
|
@@ -41,7 +41,7 @@ module Enjine
|
|
|
41
41
|
# distinct card) while something lets them play those.
|
|
42
42
|
def permission_candidates(index, sources = permission_sources(index))
|
|
43
43
|
pl = player(index)
|
|
44
|
-
ids = alt_cost_hand(pl) + pl.graveyard + state.exile
|
|
44
|
+
ids = alt_cost_hand(pl) + pl.graveyard + state.exile + commander_candidates(index)
|
|
45
45
|
ids += [pl.library.first] unless pl.library.empty?
|
|
46
46
|
return ids unless sources.any? { |_, inst| inst.ability.kind == :play_from && inst.ability.outside? }
|
|
47
47
|
|
|
@@ -143,7 +143,7 @@ module Enjine
|
|
|
143
143
|
def play_methods(index, id, sources = permission_sources(index))
|
|
144
144
|
obj = id.is_a?(GameObject) ? id : candidate_object(index, id)
|
|
145
145
|
granted_methods(index, obj) + own_methods(index, obj) + static_methods(index, obj, sources) +
|
|
146
|
-
beam_methods(index, obj, sources) + alt_methods(index, obj)
|
|
146
|
+
beam_methods(index, obj, sources) + alt_methods(index, obj) + command_methods(index, obj)
|
|
147
147
|
end
|
|
148
148
|
|
|
149
149
|
# The method named `via`, or an error (the option was offered from this list).
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Enjine
|
|
4
|
+
class Engine
|
|
5
|
+
# The Commander variant (CR 903) for two players: each player's commander starts in the command zone
|
|
6
|
+
# (903.6), may be cast from there for {2} more per earlier cast (903.8), may return to the command zone
|
|
7
|
+
# when it would go elsewhere (903.9), and 21 combat damage from one commander loses the game (903.10a).
|
|
8
|
+
#
|
|
9
|
+
# A commander is a *card*, not an object (CR 903.3): it stays one through zone changes, but a token or a
|
|
10
|
+
# copy of it is not. So nothing marks the object; `commander?` asks whose card it really is.
|
|
11
|
+
#
|
|
12
|
+
# State: `State#commanders` (each player's commander card id), PlayerState#commander_casts (times cast from
|
|
13
|
+
# the command zone) and #commander_damage ({card id => combat damage dealt to them}), and
|
|
14
|
+
# State#commander_returns (see #note_commander_move!).
|
|
15
|
+
module Commander
|
|
16
|
+
TAX = 2 # CR 903.8: {2} more for each previous cast from the command zone
|
|
17
|
+
DAMAGE_LOSS = 21 # CR 903.10a
|
|
18
|
+
# Zones a commander may be sent back from (CR 903.9a graveyard and exile, 903.9b hand and library).
|
|
19
|
+
RETURNABLE_ZONES = %i[graveyard exile hand library].freeze
|
|
20
|
+
|
|
21
|
+
# ---- who is a commander ---------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
# The card id of `obj` if it is its owner's commander. A card face counts as its card (a transformed
|
|
24
|
+
# commander is still one); a token, a copy and a card the object copies are not (CR 903.3 examples).
|
|
25
|
+
def commander?(obj)
|
|
26
|
+
return false unless state.format.commander? && obj.kind == "card" && !obj.memory["copy"]
|
|
27
|
+
|
|
28
|
+
own = own_card(obj).split(CardDefinition::FACE_SEPARATOR).first
|
|
29
|
+
own == state.commanders[obj.owner] && !token?(obj)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# CR 903.6: each player puts their commander face up into the command zone, before libraries are shuffled.
|
|
33
|
+
def place_commanders!
|
|
34
|
+
state.commanders.each_with_index do |card, owner|
|
|
35
|
+
state.command << create_object(card, owner, :command).id if card
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The commander `index` has waiting in the command zone, if it's there.
|
|
40
|
+
def commander_in_command_zone(index)
|
|
41
|
+
state.command.find { state.objects[it]&.owner == index && commander?(object(it)) }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# ---- casting (CR 903.8) -----------------------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
def commander_casts(index) = player(index).commander_casts.to_i
|
|
47
|
+
|
|
48
|
+
# The extra generic mana the next cast of `index`'s commander from the command zone costs.
|
|
49
|
+
def commander_tax(index) = TAX * commander_casts(index)
|
|
50
|
+
|
|
51
|
+
# Command-zone cards `index` may try to cast: their commander.
|
|
52
|
+
def commander_candidates(index) = [commander_in_command_zone(index)].compact
|
|
53
|
+
|
|
54
|
+
# The way to cast a commander from the command zone (see Engine::Permissions): its cost includes the tax.
|
|
55
|
+
def command_methods(index, obj)
|
|
56
|
+
return [] unless obj.zone == :command && obj.owner == index && commander?(obj)
|
|
57
|
+
|
|
58
|
+
[{ "via" => "command", "play" => "cast", "cost_increase" => commander_tax(index) }]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# As a spell becomes cast from the command zone (CR 601.2i): it counts toward the next tax (CR 903.8).
|
|
62
|
+
def record_commander_cast!(index, method)
|
|
63
|
+
return unless method && method["via"] == "command"
|
|
64
|
+
|
|
65
|
+
player(index).commander_casts = commander_casts(index) + 1
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# ---- 21 combat damage (CR 903.10a) ----------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
# Combat damage `source` dealt to player `index`: counted against the commander it came from.
|
|
71
|
+
def note_commander_damage!(source, index, amount)
|
|
72
|
+
obj = state.objects[source]
|
|
73
|
+
return unless obj && commander?(obj)
|
|
74
|
+
|
|
75
|
+
total = (player(index).commander_damage ||= {})
|
|
76
|
+
card = state.commanders[obj.owner]
|
|
77
|
+
total[card] = total.fetch(card, 0) + amount
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def loses_to_commander_damage?(index)
|
|
81
|
+
(player(index).commander_damage || {}).values.any? { it >= DAMAGE_LOSS }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# ---- returning to the command zone (CR 903.9) -------------------------------------------------
|
|
85
|
+
|
|
86
|
+
# A commander that moved to a graveyard, exile, hand or library: its owner may send it to the command
|
|
87
|
+
# zone before anyone gets priority (CR 903.9a is a state-based action; 903.9b a replacement effect, which
|
|
88
|
+
# the engine applies right after the move, since a replacement can't ask a player).
|
|
89
|
+
def note_commander_move!(fresh)
|
|
90
|
+
state.commander_returns << fresh.id if RETURNABLE_ZONES.include?(fresh.zone) && commander?(fresh)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Before priority: the next commander waiting to be sent back asks its owner (a frame, like the
|
|
94
|
+
# legend rule). Returns true when it started one.
|
|
95
|
+
def commander_choice_first!(index)
|
|
96
|
+
return false if state.frames.any? { it["type"] == "commander_zone" }
|
|
97
|
+
|
|
98
|
+
state.commander_returns.select! { state.objects.key?(it) } # moved on since
|
|
99
|
+
id = state.commander_returns.first or return false
|
|
100
|
+
state.frames << { "type" => "commander_zone", "then" => index, "object" => id }
|
|
101
|
+
ask!(nil)
|
|
102
|
+
true
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def step_commander_zone!(frame)
|
|
106
|
+
id = frame["object"]
|
|
107
|
+
unless frame.key?("choice")
|
|
108
|
+
ask!("kind" => "commander_zone", "player" => object(id).owner, "object" => id)
|
|
109
|
+
return :wait
|
|
110
|
+
end
|
|
111
|
+
state.commander_returns.delete(id)
|
|
112
|
+
move!(id, :command, reason: "commander") if frame["choice"] == "command" && state.objects.key?(id)
|
|
113
|
+
give_priority!(frame["then"])
|
|
114
|
+
:done
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def choose_commander_zone!(to_command_zone)
|
|
118
|
+
state.frames.rfind { it["type"] == "commander_zone" }["choice"] = to_command_zone ? "command" : "stay"
|
|
119
|
+
ask!(nil)
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
include Commander
|
|
124
|
+
end
|
|
125
|
+
end
|
data/lib/enjine/engine/damage.rb
CHANGED
|
@@ -27,6 +27,7 @@ module Enjine
|
|
|
27
27
|
index = Mechanics::Filters.player_index(self, ref)
|
|
28
28
|
if index
|
|
29
29
|
emit Events::DamageDealt.new(source: source, target: ref, amount: amount, combat: combat)
|
|
30
|
+
note_commander_damage!(source, index, amount) if combat # CR 903.10a
|
|
30
31
|
change_life!(index, -amount) # CR 120.3a
|
|
31
32
|
else
|
|
32
33
|
return 0 unless state.battlefield.include?(ref)
|
|
@@ -9,6 +9,7 @@ module Enjine
|
|
|
9
9
|
state.decklists.each_with_index do |list, owner|
|
|
10
10
|
list.each { |card| state.players[owner].library << create_object(card, owner, :library).id }
|
|
11
11
|
end
|
|
12
|
+
place_commanders! # CR 903.6
|
|
12
13
|
emit Events::GameStarted.new(players: names, seed: state.seed)
|
|
13
14
|
winner = state.rng.rand(state.players.size)
|
|
14
15
|
emit Events::DieRolled.new(winner: name(winner))
|
data/lib/enjine/engine/rules.rb
CHANGED
|
@@ -57,7 +57,7 @@ module Enjine
|
|
|
57
57
|
@board_abilities_state = state
|
|
58
58
|
@board_abilities = {}
|
|
59
59
|
end
|
|
60
|
-
@board_abilities[kind] ||= (state.battlefield +
|
|
60
|
+
@board_abilities[kind] ||= (state.battlefield + state.emblem_ids).flat_map do |id|
|
|
61
61
|
source = object(id)
|
|
62
62
|
chars(source).abilities.filter_map { [source, it.ability] if it.ability.kind == kind }
|
|
63
63
|
end
|
|
@@ -18,6 +18,7 @@ module Enjine
|
|
|
18
18
|
reason = if loses_for_life?(i) then "life" # CR 704.5a (unless an effect says otherwise)
|
|
19
19
|
elsif pl.drew_from_empty then "empty_library" # CR 704.5b
|
|
20
20
|
elsif pl.poison >= 10 then "poison" # CR 704.5c
|
|
21
|
+
elsif loses_to_commander_damage?(i) then "commander_damage" # CR 903.10a
|
|
21
22
|
end
|
|
22
23
|
pl.drew_from_empty = false
|
|
23
24
|
lose!(i, reason) if reason
|
|
@@ -128,7 +128,7 @@ module Enjine
|
|
|
128
128
|
|
|
129
129
|
@trigger_epoch = @epoch
|
|
130
130
|
@trigger_state = state
|
|
131
|
-
@trigger_sources = (state.battlefield +
|
|
131
|
+
@trigger_sources = (state.battlefield + state.emblem_ids).filter_map do |id| # emblems too (CR 114.4)
|
|
132
132
|
instances = chars(id).triggered_abilities.select { it.ability.zone == :battlefield }
|
|
133
133
|
[id, instances] unless instances.empty?
|
|
134
134
|
end
|
data/lib/enjine/engine/zones.rb
CHANGED
|
@@ -126,6 +126,7 @@ module Enjine
|
|
|
126
126
|
list = state.zone_list(to, old.owner)
|
|
127
127
|
data["position"] == :bottom ? list.push(fresh.id) : list.unshift(fresh.id)
|
|
128
128
|
record_result!(fresh.id)
|
|
129
|
+
note_commander_move!(fresh) # CR 903.9
|
|
129
130
|
event = Events::ZoneChanged.new(card: old.card, old_id: id, new_id: fresh.id, owner: name(old.owner),
|
|
130
131
|
from: old.zone.to_s, to: to.to_s, reason: data["reason"],
|
|
131
132
|
lookers: face_down&.map { name(it) })
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Enjine
|
|
4
|
+
# The rules a game is set up with: how it starts and what a legal deck is. `CONSTRUCTED` is the default
|
|
5
|
+
# (CR 100.2, 103.4); `DUEL_COMMANDER` is the two-player Commander variant (CR 903).
|
|
6
|
+
#
|
|
7
|
+
# Enjine::Setup.new(alice, bob, format: :duel_commander)
|
|
8
|
+
#
|
|
9
|
+
# A format is plain data: a game keeps only its name (State#format round-trips through snapshots).
|
|
10
|
+
Format = Data.define(:name, :starting_life, :deck_size, :commander) do
|
|
11
|
+
# `deck_size`: the sizes a deck may have, its commander included (CR 903.5a).
|
|
12
|
+
def commander? = commander
|
|
13
|
+
|
|
14
|
+
# The most copies of `definition` a deck may hold, nil for any number. Constructed: its own limit (CR 100.2a).
|
|
15
|
+
# Commander: one of each card named differently, only basic lands exempt (CR 903.5b).
|
|
16
|
+
def copy_limit(definition)
|
|
17
|
+
return definition.deck_limit unless commander?
|
|
18
|
+
|
|
19
|
+
definition.supertype?(:basic) ? nil : 1
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def to_s = name
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
class Format
|
|
26
|
+
CONSTRUCTED = new(name: "constructed", starting_life: 20, deck_size: 60.., commander: false) # CR 100.2a, 103.4
|
|
27
|
+
DUEL_COMMANDER = new(name: "duel_commander", starting_life: 40, deck_size: 100..100, commander: true) # CR 903.5a, 903.7
|
|
28
|
+
ALL = [CONSTRUCTED, DUEL_COMMANDER].freeze
|
|
29
|
+
|
|
30
|
+
# Whether a card may be a commander: a legendary creature, Vehicle, or Spacecraft with power and toughness
|
|
31
|
+
# (CR 903.3). Cards that say they can be your commander are deferred.
|
|
32
|
+
def self.commander_card?(definition)
|
|
33
|
+
return false unless definition.supertype?(:legendary)
|
|
34
|
+
|
|
35
|
+
definition.type?(:creature) || definition.subtypes.include?(:vehicle) ||
|
|
36
|
+
(definition.subtypes.include?(:spacecraft) && stationed?(definition))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# A Spacecraft has power and toughness through a station symbol (CR 722.2b).
|
|
40
|
+
def self.stationed?(definition)
|
|
41
|
+
definition.abilities.any? { it.kind == :station_symbol && it.power }
|
|
42
|
+
end
|
|
43
|
+
private_class_method :stationed?
|
|
44
|
+
|
|
45
|
+
# The format a name (or a Format) stands for.
|
|
46
|
+
def self.fetch(value)
|
|
47
|
+
return value if value.is_a?(Format)
|
|
48
|
+
|
|
49
|
+
ALL.find { it.name == value.to_s } or
|
|
50
|
+
raise ArgumentError, "unknown format #{value.inspect} (use #{ALL.map { ":#{it.name}" }.join(", ")})"
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
data/lib/enjine/fuzz.rb
CHANGED
|
@@ -39,6 +39,23 @@ module Enjine
|
|
|
39
39
|
doc
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
+
# A random legal Duel Commander deck (CR 903.5) from `cards`: a random legendary commander, then random
|
|
43
|
+
# distinct cards inside its color identity, filled up with basic lands of its colors. The cards need a
|
|
44
|
+
# legendary creature of at least one color and a basic land of each color.
|
|
45
|
+
def random_commander_deck(cards, rng:)
|
|
46
|
+
definitions = cards.map { CardDefinition.from_h(it) }.reject(&:token?)
|
|
47
|
+
leaders = definitions.select { Format.commander_card?(it) && !it.color_identity.empty? }
|
|
48
|
+
commander = leaders[rng.rand(leaders.size)]
|
|
49
|
+
identity = commander.color_identity
|
|
50
|
+
fitting = definitions.reject { it.supertype?(:basic) || it.id == commander.id || !(it.color_identity - identity).empty? }
|
|
51
|
+
chosen = rng.shuffle!(fitting).first(Format::DUEL_COMMANDER.deck_size.min / 2)
|
|
52
|
+
basics = identity.map { |color| definitions.find { it.supertype?(:basic) && ColorIdentity.land_colors(it) == [color] } }
|
|
53
|
+
counts = (Format::DUEL_COMMANDER.deck_size.min - 1 - chosen.size).times.map { basics[rng.rand(basics.size)] }.tally
|
|
54
|
+
{ "cards" => (chosen + [commander] + counts.keys).map(&:to_h),
|
|
55
|
+
"main" => chosen.map { { "card" => it.id, "count" => 1 } } + counts.map { |card, n| { "card" => card.id, "count" => n } },
|
|
56
|
+
"commander" => commander.id }
|
|
57
|
+
end
|
|
58
|
+
|
|
42
59
|
# Picks `number` random cards that still fit the four-copy rule, yielding each one's id.
|
|
43
60
|
def draw(definitions, counts, rng, number)
|
|
44
61
|
number.times do
|
data/lib/enjine/game.rb
CHANGED
|
@@ -20,8 +20,9 @@ module Enjine
|
|
|
20
20
|
|
|
21
21
|
attr_reader :catalog, :history
|
|
22
22
|
|
|
23
|
-
def self.start(catalog:, names:, decklists:, seed:, outside: nil)
|
|
24
|
-
|
|
23
|
+
def self.start(catalog:, names:, decklists:, seed:, outside: nil, format: Format::CONSTRUCTED, commanders: nil)
|
|
24
|
+
state = State.initial(names: names, decklists: decklists, seed: seed, outside: outside, format: format, commanders: commanders)
|
|
25
|
+
new(catalog, state).tap(&:start!)
|
|
25
26
|
end
|
|
26
27
|
|
|
27
28
|
# Resumes a game from #export output (a JSON String or the parsed Hash).
|
|
@@ -125,7 +126,7 @@ module Enjine
|
|
|
125
126
|
# Starts this game over from its opening, with the same or a new seed.
|
|
126
127
|
def restart(seed: nil)
|
|
127
128
|
fresh = State.initial(names: names, decklists: state.decklists, seed: seed.nil? ? state.seed : seed,
|
|
128
|
-
outside: state.outside_lists)
|
|
129
|
+
outside: state.outside_lists, format: state.format, commanders: state.commanders)
|
|
129
130
|
@engine.state = fresh
|
|
130
131
|
@history = []
|
|
131
132
|
@buffer.clear
|