enjine 0.1.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.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +55 -1
  3. data/README.md +2 -1
  4. data/SUPPORTED.md +12 -9
  5. data/docs/ARCHITECTURE.md +2 -0
  6. data/docs/DESIGN.md +12 -5
  7. data/docs/MECHANICS_COVERAGE.md +33 -5
  8. data/docs/guides/api.md +51 -0
  9. data/docs/guides/card-data-format.md +43 -4
  10. data/docs/guides/deck-builder.md +18 -0
  11. data/docs/plans/commander.md +63 -0
  12. data/docs/plans/dsl-gaps.md +606 -0
  13. data/docs/plans/standard.md +3 -0
  14. data/docs/reference/abilities.md +33 -7
  15. data/docs/reference/costs.md +11 -1
  16. data/docs/reference/decisions.md +6 -2
  17. data/docs/reference/effects.md +30 -9
  18. data/docs/reference/triggers.md +3 -2
  19. data/lib/enjine/actions/cast.rb +2 -1
  20. data/lib/enjine/actions/special_action.rb +4 -2
  21. data/lib/enjine/card_definition.rb +24 -3
  22. data/lib/enjine/cli/text.rb +23 -1
  23. data/lib/enjine/cli.rb +2 -1
  24. data/lib/enjine/color_identity.rb +38 -0
  25. data/lib/enjine/decisions/commander_zone.rb +36 -0
  26. data/lib/enjine/deck.rb +73 -14
  27. data/lib/enjine/deck_builder.rb +62 -18
  28. data/lib/enjine/engine/casting/alt_casting.rb +19 -2
  29. data/lib/enjine/engine/casting/casting.rb +3 -3
  30. data/lib/enjine/engine/casting/costs.rb +5 -1
  31. data/lib/enjine/engine/casting/permissions.rb +3 -3
  32. data/lib/enjine/engine/characteristics.rb +0 -13
  33. data/lib/enjine/engine/commander.rb +125 -0
  34. data/lib/enjine/engine/damage.rb +1 -0
  35. data/lib/enjine/engine/defined_power_toughness.rb +30 -0
  36. data/lib/enjine/engine/emblems.rb +1 -1
  37. data/lib/enjine/engine/mana_sources.rb +8 -1
  38. data/lib/enjine/engine/pregame.rb +1 -0
  39. data/lib/enjine/engine/priority.rb +1 -0
  40. data/lib/enjine/engine/resolution.rb +1 -0
  41. data/lib/enjine/engine/rules.rb +1 -1
  42. data/lib/enjine/engine/state_based_actions.rb +1 -0
  43. data/lib/enjine/engine/triggers.rb +2 -1
  44. data/lib/enjine/engine/turn_history.rb +25 -1
  45. data/lib/enjine/engine/zones.rb +17 -6
  46. data/lib/enjine/format.rb +53 -0
  47. data/lib/enjine/fuzz.rb +17 -0
  48. data/lib/enjine/game.rb +4 -3
  49. data/lib/enjine/mechanics/abilities/additional_triggers.rb +23 -11
  50. data/lib/enjine/mechanics/abilities/cost_changes.rb +20 -7
  51. data/lib/enjine/mechanics/abilities/mana_ability.rb +21 -6
  52. data/lib/enjine/mechanics/abilities/stack_abilities.rb +13 -9
  53. data/lib/enjine/mechanics/abilities/static.rb +13 -7
  54. data/lib/enjine/mechanics/costs/sacrifice.rb +28 -2
  55. data/lib/enjine/mechanics/effects/become.rb +8 -4
  56. data/lib/enjine/mechanics/effects/choose_cards.rb +9 -7
  57. data/lib/enjine/mechanics/effects/delayed_triggers.rb +2 -1
  58. data/lib/enjine/mechanics/effects/search.rb +15 -8
  59. data/lib/enjine/mechanics/keywords/plot.rb +79 -0
  60. data/lib/enjine/mechanics/triggers/casting.rb +8 -6
  61. data/lib/enjine/mechanics/values.rb +82 -14
  62. data/lib/enjine/player.rb +2 -1
  63. data/lib/enjine/rules_monitor.rb +25 -1
  64. data/lib/enjine/setup.rb +10 -4
  65. data/lib/enjine/snapshot/player_checks.rb +47 -0
  66. data/lib/enjine/snapshot.rb +4 -1
  67. data/lib/enjine/state.rb +33 -14
  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 +9 -1
@@ -38,6 +38,53 @@ module Enjine
38
38
  end
39
39
  end
40
40
 
41
+ # The format and the commanders (CR 903.3): constructed has none; a Commander game has one per player, and
42
+ # only a player's own commander card (besides emblems, CR 114.1) is ever in the command zone. Absent in
43
+ # snapshots of constructed games.
44
+ def build_commanders(state, data)
45
+ state.format = Format.fetch(data.fetch("format", Format::CONSTRUCTED.name))
46
+ state.commanders = data.fetch("commanders", [nil, nil]).dup.freeze
47
+ state.commander_returns = data.fetch("commander_returns", []).dup
48
+ end
49
+
50
+ def validate_commanders(data, catalog, errors)
51
+ format = Format::ALL.find { it.name == data.fetch("format", Format::CONSTRUCTED.name) }
52
+ return errors << "format: unknown #{data["format"].inspect}" unless format
53
+
54
+ commanders = data.fetch("commanders", [nil, nil])
55
+ listed = commanders.is_a?(Array) && commanders.size == 2 && commanders.all? { it.nil? || (it.is_a?(String) && catalog[it]&.id == it) }
56
+ return errors << "commanders: must be a list of 2 card ids or nulls" unless listed
57
+
58
+ errors << "commanders: a Commander game has one each (CR 903.3)" if format.commander? && commanders.any?(&:nil?)
59
+ errors << "commanders: only a Commander game has them (CR 903.3)" if !format.commander? && commanders.any?
60
+ return unless errors.empty? # the objects and players are well formed
61
+
62
+ validate_commander_zone(data, commanders, errors)
63
+ validate_commander_progress(data, commanders, errors)
64
+ end
65
+
66
+ def validate_commander_zone(data, commanders, errors)
67
+ data["objects"].each do |obj|
68
+ next unless obj["zone"].to_s == "command" && obj.fetch("kind", "card") == "card"
69
+
70
+ unless commanders[obj["owner"]] == obj["card"]
71
+ errors << "#{obj["id"]}: only emblems and its owner's commander are in the command zone (CR 114.1, 903.6)"
72
+ end
73
+ end
74
+ end
75
+
76
+ # Casts from the command zone (CR 903.8) and combat damage by each commander (CR 903.10a).
77
+ def validate_commander_progress(data, commanders, errors)
78
+ data["players"].each_with_index do |pl, i|
79
+ path = "players[#{i}]"
80
+ casts = pl.fetch("commander_casts", 0)
81
+ errors << "#{path}.commander_casts: must be a non-negative integer" unless non_negative?(casts)
82
+ damage = pl.fetch("commander_damage", {})
83
+ ok = damage.is_a?(Hash) && damage.all? { |card, n| commanders.include?(card) && non_negative?(n) }
84
+ errors << "#{path}.commander_damage: must map commander card ids to non-negative integers" unless ok
85
+ end
86
+ end
87
+
41
88
  # Pool mana kept apart: restricted (CR 106.6) and/or kept until end of combat (CR 702.189a):
42
89
  # [{"mana" => "C", "spend_only" => {...}, "until" => "end_of_combat"}]; absent in older snapshots.
43
90
  def validate_restricted_mana(list, errors, path)
@@ -29,6 +29,7 @@ module Enjine
29
29
  validate_extra_turns(data, errors)
30
30
  validate_extra_phases(data, errors)
31
31
  validate_outside(data, catalog, errors)
32
+ validate_commanders(data, catalog, errors)
32
33
  errors
33
34
  end
34
35
 
@@ -59,6 +60,7 @@ module Enjine
59
60
  state.extra_turns = deep_copy(data.fetch("extra_turns", { "queue" => [], "begun" => [] }))
60
61
  state.extra_phases = deep_copy(data.fetch("extra_phases", State.no_extra_phases))
61
62
  state.outside_lists = data.fetch("outside_lists", [[], []]).map { |l| l.dup.freeze }.freeze
63
+ build_commanders(state, data)
62
64
  state.repeat_cleanup = data.fetch("repeat_cleanup", false)
63
65
  state.combat_damage_pass = data["combat_damage_pass"]
64
66
  state.attacks_declared = data.fetch("attacks_declared", 0)
@@ -100,6 +102,8 @@ module Enjine
100
102
  pl.designations = hash.fetch("designations", []).dup # CR 702.195b (Engine::Designations)
101
103
  pl.counters = hash.fetch("counters", {}).dup # CR 122.1: counters other than poison
102
104
  pl.pool_sources = deep_copy(hash.fetch("pool_sources", [])) # Engine::ManaSources#add_mana!
105
+ pl.commander_casts = hash["commander_casts"] # CR 903.8; absent: never cast from the command zone
106
+ pl.commander_damage = hash["commander_damage"]&.dup # CR 903.10a; absent: none dealt
103
107
  end
104
108
  end
105
109
 
@@ -192,7 +196,6 @@ module Enjine
192
196
  errors << "#{path}: #{kind}s can only exist in the #{home} zone" unless zone == home
193
197
  errors << "#{path}.ability: must be an ability reference" unless obj["ability"].is_a?(String)
194
198
  end
195
- errors << "#{path}: only emblems are in the command zone (CR 114.1)" if zone == "command" && kind != "emblem"
196
199
  counters = obj.fetch("counters", {})
197
200
  unless counters.is_a?(Hash) && counters.all? { |k, v| Mechanics::Counters.valid?(k) && non_negative?(v) }
198
201
  errors << "#{path}.counters: must map counter kinds to non-negative integers"
data/lib/enjine/state.rb CHANGED
@@ -31,7 +31,9 @@ module Enjine
31
31
  :speed, # nil (no speed) or 1-4 (CR 702.179), see Engine::Speed
32
32
  :designations, # player designations, e.g. "enduring_story" (CR 702.195b), see Engine::Designations
33
33
  :counters, # nil or {kind => n}: counters other than poison (CR 122.1), see Engine#add_player_counters!
34
- :pool_sources) do # nil or [{"mana", "from"}]: sources of pool mana made by artifacts (Engine#add_mana!)
34
+ :pool_sources, # nil or [{"mana", "from"}]: sources of pool mana made by artifacts (Engine#add_mana!)
35
+ :commander_casts, # nil or n: times they cast their commander from the command zone (CR 903.8)
36
+ :commander_damage) do # nil or {commander card id => n}: combat damage dealt to them by each (CR 903.10a)
35
37
  def zone(name)
36
38
  case name
37
39
  when :library then library
@@ -57,14 +59,15 @@ module Enjine
57
59
  def optional_h
58
60
  { "speed" => speed, "designations" => designations.to_a.empty? ? nil : designations.dup,
59
61
  "counters" => counters.nil? || counters.empty? ? nil : counters.dup,
60
- "pool_sources" => pool_sources.nil? || pool_sources.empty? ? nil : deep(pool_sources) }.compact
62
+ "pool_sources" => pool_sources.nil? || pool_sources.empty? ? nil : deep(pool_sources),
63
+ "commander_casts" => commander_casts&.positive? ? commander_casts : nil,
64
+ "commander_damage" => commander_damage.nil? || commander_damage.empty? ? nil : commander_damage.dup }.compact
61
65
  end
62
66
  end
63
67
 
64
68
  # The complete, mutable state of one game. Plain data only: everything here
65
69
  # round-trips through #to_h / Snapshot.load, and Marshal copies it cheaply.
66
70
  class State
67
- STARTING_LIFE = 20
68
71
  PLAYER_ZONES = %i[library hand graveyard].freeze
69
72
  # `phased_out` is not a zone of the rules: phased-out permanents are on the battlefield but treated as
70
73
  # though they don't exist (CR 702.26b, 702.26d), so they are kept apart from `battlefield`. The command
@@ -82,20 +85,25 @@ module Enjine
82
85
  # The definition ids each player started the game with outside it (CR 400.11a, 100.4), like
83
86
  # `decklists`: a restart begins from these again. What is still outside is PlayerState#outside.
84
87
  attr_accessor :outside_lists
85
-
86
- def self.initial(names:, decklists:, seed:, outside: nil)
88
+ # The rules the game was set up with (Enjine::Format), and each player's commander card id or nil (CR 903.3).
89
+ # Like `decklists`, a restart begins from these again.
90
+ attr_accessor :format, :commanders
91
+ # Object ids of commanders put into a graveyard, exile, hand or library since state-based actions were last
92
+ # checked, whose owner may still send them to the command zone (CR 903.9, Engine::Commander).
93
+ attr_accessor :commander_returns
94
+
95
+ def self.initial(names:, decklists:, seed:, outside: nil, format: Format::CONSTRUCTED, commanders: nil)
87
96
  state = new
97
+ state.format = format
98
+ state.commanders = (commanders || names.map { nil }).freeze
99
+ state.commander_returns = []
88
100
  state.seed = seed
89
101
  state.rng = Rng.from_seed(seed)
90
102
  state.decklists = decklists.map { |l| l.dup.freeze }.freeze
91
103
  state.outside_lists = (outside || names.map { [] }).map { |l| l.dup.freeze }.freeze
92
- state.players = names.each_with_index.map { |name, i| new_player(name, state.outside_lists[i]) }
104
+ state.players = names.each_with_index.map { |name, i| new_player(name, state.outside_lists[i], format.starting_life) }
93
105
  state.objects = {}
94
- state.battlefield = []
95
- state.stack = []
96
- state.exile = []
97
- state.phased_out = []
98
- state.command = []
106
+ SHARED_ZONES.each { state.public_send("#{it}=", []) }
99
107
  state.attackers = {}
100
108
  state.blockers = {}
101
109
  state.effects = []
@@ -119,8 +127,8 @@ module Enjine
119
127
  # with after them (Engine::TurnStructure); "combats" counts combat phases begun this turn.
120
128
  def self.no_extra_phases = { "pending" => [], "queue" => [], "combats" => 0 }
121
129
 
122
- def self.new_player(name, outside = [])
123
- PlayerState.new(name: name, life: STARTING_LIFE, poison: 0, library: [], hand: [], graveyard: [],
130
+ def self.new_player(name, outside = [], life = Format::CONSTRUCTED.starting_life)
131
+ PlayerState.new(name: name, life: life, poison: 0, library: [], hand: [], graveyard: [],
124
132
  mana_pool: Mana.empty_pool, lands_played: 0, drew_from_empty: false, lost: nil,
125
133
  mulligans: 0, kept: false, mulligan_choice: nil, turn_log: {}, game_log: {}, last_turn_log: {}, restricted_mana: [],
126
134
  outside: outside.dup)
@@ -131,6 +139,10 @@ module Enjine
131
139
  end
132
140
 
133
141
  def object(id) = objects.fetch(id)
142
+
143
+ # The emblems in the command zone: the only objects there whose abilities function (CR 114.4, 113.6). A
144
+ # commander waiting there (CR 903.6) has none that do.
145
+ def emblem_ids = (command || []).select { objects[it]&.kind == "emblem" }
134
146
  def opponent(index) = 1 - index
135
147
  def over? = !result.nil?
136
148
 
@@ -145,7 +157,14 @@ module Enjine
145
157
  "attackers" => attackers.dup, "blockers" => blockers.dup, "effects" => effects.map { deep_dup(it) },
146
158
  **DEEP_FIELDS.to_h { [it, deep_dup(public_send(it))] },
147
159
  "repeat_cleanup" => repeat_cleanup, "combat_damage_pass" => combat_damage_pass,
148
- "attacks_declared" => attacks_declared }.merge("outside_lists" => (outside_lists || []).map(&:dup))
160
+ "attacks_declared" => attacks_declared }.merge("outside_lists" => (outside_lists || []).map(&:dup)).merge(format_h)
161
+ end
162
+
163
+ # What a Commander game adds to a snapshot; a constructed game's is unchanged.
164
+ def format_h
165
+ return {} unless format.commander?
166
+
167
+ { "format" => format.name, "commanders" => commanders.dup, "commander_returns" => commander_returns.dup }
149
168
  end
150
169
 
151
170
  # Nested fields #to_h deep-copies as they are.
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Enjine
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  # Comprehensive Rules document this engine follows.
6
6
  RULES_VERSION = "2026-11-06"
7
7
  end
data/lib/enjine/view.rb CHANGED
@@ -23,7 +23,7 @@ module Enjine
23
23
  "stack" => state.stack.map { object_view(engine, it) },
24
24
  "exile" => state.exile.map { exiled_view(engine, it, viewer) },
25
25
  "phased_out" => state.phased_out.map { object_view(engine, it) },
26
- "command" => engine.emblems.map { emblem_view(engine, it) },
26
+ "command" => state.command.map { command_view(engine, it) },
27
27
  "pending" => pending_view(engine, viewer),
28
28
  "result" => state.result&.dup,
29
29
  "effects" => state.effects.map { it.except("controller").merge("controller" => engine.name(it["controller"])) },
@@ -31,6 +31,11 @@ module Enjine
31
31
  }
32
32
  end
33
33
 
34
+ # What is in the command zone, all public: emblems (CR 114) and commanders (CR 903.6).
35
+ def command_view(engine, id)
36
+ engine.object(id).kind == "emblem" ? emblem_view(engine, engine.object(id)) : object_view(engine, id)
37
+ end
38
+
34
39
  # An emblem in the command zone (CR 114): public, with its owner and abilities.
35
40
  def emblem_view(engine, obj)
36
41
  { "id" => obj.id, "kind" => "emblem", "owner" => engine.name(obj.owner),
@@ -60,6 +65,7 @@ module Enjine
60
65
  "graveyard" => player.graveyard.map { object_view(engine, it) }
61
66
  }
62
67
  view.merge!(public_extras(player))
68
+ view.merge!(commander_view(engine, engine.state.players.index(player)))
63
69
  view["hand"] = player.hand.map { object_view(engine, it) } if sees_hand
64
70
  view["outside"] = outside_view(engine, player) if sees_hand # CR 400.11a: only its owner knows their sideboard
65
71
  view["library"] = player.library.map { object_view(engine, it) } if sees_library
@@ -74,6 +80,17 @@ module Enjine
74
80
  "pool_sources" => Snapshot.deep_copy(player.pool_sources) }.reject { |_, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) }
75
81
  end
76
82
 
83
+ # A Commander game's public facts about a player (CR 903): their commander, what it would cost to cast from
84
+ # the command zone (CR 903.8) and the combat damage each commander has dealt them (CR 903.10a).
85
+ def commander_view(engine, index)
86
+ card = engine.state.commanders[index]
87
+ return {} unless card
88
+
89
+ damage = engine.player(index).commander_damage || {}
90
+ { "commander" => engine.catalog.fetch(card).name, "commander_tax" => engine.commander_tax(index),
91
+ "commander_damage" => damage.to_h { |id, n| [engine.catalog.fetch(id).name, n] } }
92
+ end
93
+
77
94
  # The cards a player owns outside the game (CR 400.11a): not objects, so no ids (CR 400.11).
78
95
  def outside_view(engine, player)
79
96
  player.outside.map do |card|
data/lib/enjine.rb CHANGED
@@ -12,6 +12,8 @@ require_relative "enjine/mechanics"
12
12
  end
13
13
  Dir[File.join(__dir__, "enjine/mechanics/**/*.rb")].each { |f| require f }
14
14
  require_relative "enjine/characteristics"
15
+ require_relative "enjine/format"
16
+ require_relative "enjine/color_identity"
15
17
  require "json"
16
18
  require "digest"
17
19
  require_relative "enjine/tokens"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enjine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mehmet Cetin
@@ -30,6 +30,8 @@ files:
30
30
  - docs/guides/api.md
31
31
  - docs/guides/card-data-format.md
32
32
  - docs/guides/deck-builder.md
33
+ - docs/plans/commander.md
34
+ - docs/plans/dsl-gaps.md
33
35
  - docs/plans/standard.md
34
36
  - docs/reference/abilities.md
35
37
  - docs/reference/costs.md
@@ -58,10 +60,12 @@ files:
58
60
  - lib/enjine/characteristics.rb
59
61
  - lib/enjine/cli.rb
60
62
  - lib/enjine/cli/text.rb
63
+ - lib/enjine/color_identity.rb
61
64
  - lib/enjine/decisions.rb
62
65
  - lib/enjine/decisions/bottom.rb
63
66
  - lib/enjine/decisions/casting.rb
64
67
  - lib/enjine/decisions/combat.rb
68
+ - lib/enjine/decisions/commander_zone.rb
65
69
  - lib/enjine/decisions/discard.rb
66
70
  - lib/enjine/decisions/effect_choice.rb
67
71
  - lib/enjine/decisions/enter_choice.rb
@@ -89,8 +93,10 @@ files:
89
93
  - lib/enjine/engine/characteristics.rb
90
94
  - lib/enjine/engine/class_levels.rb
91
95
  - lib/enjine/engine/combat.rb
96
+ - lib/enjine/engine/commander.rb
92
97
  - lib/enjine/engine/copies.rb
93
98
  - lib/enjine/engine/damage.rb
99
+ - lib/enjine/engine/defined_power_toughness.rb
94
100
  - lib/enjine/engine/designations.rb
95
101
  - lib/enjine/engine/emblems.rb
96
102
  - lib/enjine/engine/entering.rb
@@ -113,6 +119,7 @@ files:
113
119
  - lib/enjine/engine/zones.rb
114
120
  - lib/enjine/errors.rb
115
121
  - lib/enjine/events.rb
122
+ - lib/enjine/format.rb
116
123
  - lib/enjine/frames.rb
117
124
  - lib/enjine/fuzz.rb
118
125
  - lib/enjine/game.rb
@@ -231,6 +238,7 @@ files:
231
238
  - lib/enjine/mechanics/keywords/kicker.rb
232
239
  - lib/enjine/mechanics/keywords/mobilize_and_firebending.rb
233
240
  - lib/enjine/mechanics/keywords/paradigm.rb
241
+ - lib/enjine/mechanics/keywords/plot.rb
234
242
  - lib/enjine/mechanics/keywords/power_up.rb
235
243
  - lib/enjine/mechanics/keywords/protection.rb
236
244
  - lib/enjine/mechanics/keywords/prowess.rb