enjine 0.5.0 → 0.6.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +156 -1
- data/README.md +12 -3
- data/SUPPORTED.md +44 -17
- data/docs/ARCHITECTURE.md +7 -2
- data/docs/MECHANICS_COVERAGE.md +10 -8
- data/docs/guides/api.md +2 -1
- data/docs/guides/card-data-format.md +40 -8
- data/docs/reference/abilities.md +254 -13
- data/docs/reference/costs.md +36 -0
- data/docs/reference/decisions.md +3 -1
- data/docs/reference/effects.md +93 -10
- data/docs/reference/filters.md +11 -1
- data/docs/reference/keywords.md +42 -0
- data/docs/reference/subtypes.md +18 -0
- data/docs/reference/triggers.md +69 -8
- data/docs/reference/types.md +20 -0
- data/lib/enjine/actions/special_action.rb +4 -0
- data/lib/enjine/actions/turn_face_up.rb +25 -0
- data/lib/enjine/actions/unlock.rb +23 -0
- data/lib/enjine/card_definition.rb +50 -12
- data/lib/enjine/card_face_down.rb +31 -0
- data/lib/enjine/card_faces.rb +4 -2
- data/lib/enjine/card_rooms.rb +86 -0
- data/lib/enjine/catalog.rb +9 -3
- data/lib/enjine/cli/text.rb +1 -0
- data/lib/enjine/color_identity.rb +34 -1
- data/lib/enjine/decisions/casting.rb +6 -3
- data/lib/enjine/engine/ability_layers.rb +8 -0
- data/lib/enjine/engine/casting/alt_casting.rb +12 -4
- data/lib/enjine/engine/casting/cast_choices.rb +8 -1
- data/lib/enjine/engine/casting/casting.rb +18 -22
- data/lib/enjine/engine/casting/costs.rb +8 -1
- data/lib/enjine/engine/casting/payment.rb +47 -9
- data/lib/enjine/engine/casting/permissions.rb +6 -3
- data/lib/enjine/engine/characteristics.rb +8 -9
- data/lib/enjine/engine/combat.rb +19 -7
- data/lib/enjine/engine/copies.rb +2 -1
- data/lib/enjine/engine/crimes.rb +46 -0
- data/lib/enjine/engine/damage.rb +36 -1
- data/lib/enjine/engine/face_down_permanents.rb +108 -0
- data/lib/enjine/engine/faces.rb +6 -3
- data/lib/enjine/engine/mana_sources.rb +18 -3
- data/lib/enjine/engine/options_memo.rb +140 -0
- data/lib/enjine/engine/permanent_designations.rb +29 -0
- data/lib/enjine/engine/resolution.rb +25 -5
- data/lib/enjine/engine/rooms.rb +86 -0
- data/lib/enjine/engine/rules.rb +2 -1
- data/lib/enjine/engine/state_based_actions.rb +25 -4
- data/lib/enjine/engine/triggers.rb +20 -5
- data/lib/enjine/engine/turn_history.rb +28 -5
- data/lib/enjine/engine/zones.rb +32 -6
- data/lib/enjine/engine.rb +9 -1
- data/lib/enjine/events.rb +9 -0
- data/lib/enjine/frames.rb +3 -0
- data/lib/enjine/fuzz.rb +3 -0
- data/lib/enjine/game.rb +3 -1
- data/lib/enjine/mechanics/abilities/cost_changes.rb +32 -10
- data/lib/enjine/mechanics/abilities/damage_replacements.rb +1 -1
- data/lib/enjine/mechanics/abilities/designated.rb +57 -0
- data/lib/enjine/mechanics/abilities/life_replacements.rb +74 -0
- data/lib/enjine/mechanics/abilities/mana_ability.rb +10 -4
- data/lib/enjine/mechanics/abilities/player_rules.rb +1 -1
- data/lib/enjine/mechanics/abilities/stack_abilities.rb +12 -7
- data/lib/enjine/mechanics/abilities/static.rb +18 -5
- data/lib/enjine/mechanics/card_spec.rb +12 -3
- data/lib/enjine/mechanics/costs/base.rb +3 -2
- data/lib/enjine/mechanics/costs/behold.rb +10 -3
- data/lib/enjine/mechanics/costs/choose_creature_type.rb +3 -3
- data/lib/enjine/mechanics/costs/collect_evidence.rb +60 -0
- data/lib/enjine/mechanics/costs/exile_permanent.rb +41 -0
- data/lib/enjine/mechanics/effects/base.rb +26 -5
- data/lib/enjine/mechanics/effects/choose_cards.rb +44 -14
- data/lib/enjine/mechanics/effects/copy.rb +2 -2
- data/lib/enjine/mechanics/effects/counters.rb +12 -6
- data/lib/enjine/mechanics/effects/delayed_triggers.rb +13 -6
- data/lib/enjine/mechanics/effects/may.rb +4 -2
- data/lib/enjine/mechanics/effects/pay.rb +20 -6
- data/lib/enjine/mechanics/filters/any_target.rb +6 -3
- data/lib/enjine/mechanics/filters/base.rb +14 -5
- data/lib/enjine/mechanics/filters/permanents.rb +7 -0
- data/lib/enjine/mechanics/keywords/alternative_costs.rb +70 -16
- data/lib/enjine/mechanics/keywords/cards_have.rb +4 -2
- data/lib/enjine/mechanics/keywords/craft.rb +132 -0
- data/lib/enjine/mechanics/keywords/cycling.rb +46 -7
- data/lib/enjine/mechanics/keywords/extort.rb +2 -0
- data/lib/enjine/mechanics/keywords/face_down.rb +165 -0
- data/lib/enjine/mechanics/keywords/impending.rb +75 -0
- data/lib/enjine/mechanics/keywords/increment.rb +2 -0
- data/lib/enjine/mechanics/keywords/infinity.rb +46 -0
- data/lib/enjine/mechanics/keywords/miracle.rb +142 -0
- data/lib/enjine/mechanics/keywords/ninjutsu.rb +101 -0
- data/lib/enjine/mechanics/keywords/offspring.rb +48 -0
- data/lib/enjine/mechanics/keywords/persist_and_undying.rb +42 -0
- data/lib/enjine/mechanics/keywords/split_second.rb +29 -0
- data/lib/enjine/mechanics/keywords/storm_and_cascade.rb +5 -0
- data/lib/enjine/mechanics/keywords/void.rb +34 -0
- data/lib/enjine/mechanics/linked.rb +2 -2
- data/lib/enjine/mechanics/mana_spent.rb +1 -1
- data/lib/enjine/mechanics/selector.rb +30 -9
- data/lib/enjine/mechanics/subtypes/case.rb +68 -0
- data/lib/enjine/mechanics/triggers/base.rb +13 -4
- data/lib/enjine/mechanics/triggers/crime.rb +28 -0
- data/lib/enjine/mechanics/triggers/damage.rb +33 -18
- data/lib/enjine/mechanics/triggers/draw_and_discard.rb +19 -6
- data/lib/enjine/mechanics/triggers/rooms.rb +49 -0
- data/lib/enjine/mechanics/triggers/zone_changes.rb +24 -9
- data/lib/enjine/mechanics/types/battle.rb +130 -0
- data/lib/enjine/mechanics/values.rb +74 -27
- data/lib/enjine/replacements.rb +18 -0
- data/lib/enjine/rules_monitor.rb +49 -75
- data/lib/enjine/snapshot/deep_copy.rb +23 -0
- data/lib/enjine/snapshot/player_checks.rb +10 -2
- data/lib/enjine/snapshot.rb +2 -1
- data/lib/enjine/state.rb +2 -2
- data/lib/enjine/verify.rb +10 -3
- data/lib/enjine/version.rb +1 -1
- data/lib/enjine/view.rb +49 -13
- data/lib/enjine.rb +1 -1
- metadata +29 -1
|
@@ -10,7 +10,8 @@ module Enjine
|
|
|
10
10
|
# sources tapped for it ("tapped"), so an unfinished payment can be rewound (CR 734.1). Once a
|
|
11
11
|
# mana ability with a cost other than {T} has been activated (a Treasure's sacrifice), the payment
|
|
12
12
|
# is "irreversible": it can't be cancelled any more, and only choices that keep the rest payable
|
|
13
|
-
# are offered,
|
|
13
|
+
# are offered. Should it still have no choice left, it may be cancelled after all (#stuck?), keeping
|
|
14
|
+
# the mana abilities activated (#refund_payment!).
|
|
14
15
|
module Payment
|
|
15
16
|
# The payment in progress: the top frame (a cast or a pay frame while a casting decision is pending).
|
|
16
17
|
def payment = state.frames.last
|
|
@@ -120,6 +121,7 @@ module Enjine
|
|
|
120
121
|
frame["remaining"] = after_paying(frame["remaining"], slot)
|
|
121
122
|
(frame["spent"] ||= []) << type # what was spent matters later (sunburst, CR 702.44a; 609.4b: its real type)
|
|
122
123
|
(frame["spent_from"] ||= []) << entry["from"] if entry&.key?("from") # "if mana from a Treasure was spent"
|
|
124
|
+
(frame["refund"] ||= []) << { "mana" => type, (restricted ? "restricted" : "source") => entry }.compact # #refund_payment!
|
|
123
125
|
emit Events::ManaPaid.new(player: name(frame["player"]), mana: "{#{type}}", object: frame["object"])
|
|
124
126
|
ask!(nil)
|
|
125
127
|
end
|
|
@@ -178,7 +180,9 @@ module Enjine
|
|
|
178
180
|
end
|
|
179
181
|
|
|
180
182
|
# A mana ability whose costs include more than {T} and mana (a sacrifice, life, a discard) can't be undone.
|
|
181
|
-
def irreversible_mana_ability?(id, ref)
|
|
183
|
+
def irreversible_mana_ability?(id, ref)
|
|
184
|
+
chars(id).ability(ref).costs.any? { !it.is_a?(Mechanics::Costs::Tap) && !it.is_a?(Mechanics::Costs::TapGranter) && !it.mana? }
|
|
185
|
+
end
|
|
182
186
|
|
|
183
187
|
def reserved_for_cost?(frame, id, ref)
|
|
184
188
|
return true if id == payment_excluded_source(frame)
|
|
@@ -208,16 +212,21 @@ module Enjine
|
|
|
208
212
|
else
|
|
209
213
|
produced.each { pool[it] += 1 }
|
|
210
214
|
end
|
|
211
|
-
|
|
215
|
+
used = [id, tapped_granter(option[:ability], ability)].compact
|
|
216
|
+
payable_with?(frame, remaining_cost(frame["remaining"]), pool, payment_reserved(frame) + used, restricted: rest)
|
|
212
217
|
end
|
|
213
218
|
|
|
214
219
|
# CR 605.3: activating a mana ability while paying. One whose costs include more than {T} (a
|
|
215
|
-
# sacrifice, life, a discard) can't be undone, so the payment stops being cancellable.
|
|
220
|
+
# sacrifice, life, a discard) can't be undone, so the payment stops being cancellable. Tapping the
|
|
221
|
+
# permanent that granted it can (CR 734.1: it is untapped with the source).
|
|
216
222
|
def tap_for_payment!(id, ability_ref, mana = nil)
|
|
217
223
|
frame = payment
|
|
218
224
|
frame["irreversible"] = true if irreversible_mana_ability?(id, ability_ref)
|
|
225
|
+
granter = tapped_granter(ability_ref, chars(id).ability(ability_ref))
|
|
219
226
|
activate_mana_ability!(frame["player"], id, ability_ref, mana)
|
|
220
227
|
frame["tapped"] << id
|
|
228
|
+
frame["tapped"] << granter if granter
|
|
229
|
+
(frame["mana_tapped"] ||= []).push(*[id, granter].compact) # kept tapped by #refund_payment!
|
|
221
230
|
ask!(nil)
|
|
222
231
|
end
|
|
223
232
|
|
|
@@ -245,16 +254,42 @@ module Enjine
|
|
|
245
254
|
# Undoes the mana abilities activated for the payment (CR 734.1): untaps their sources and restores
|
|
246
255
|
# the pool. Abilities that triggered meanwhile didn't trigger (CR 734.1).
|
|
247
256
|
def rewind_payment!(frame)
|
|
248
|
-
frame["
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
end
|
|
257
|
+
return refund_payment!(frame) if frame["irreversible"]
|
|
258
|
+
|
|
259
|
+
untap_for_rewind!(frame["tapped"])
|
|
252
260
|
player(frame["player"]).mana_pool = Mana::TYPES.to_h { |t| [t, frame["pool"].fetch(t.to_s, 0)] }
|
|
253
261
|
player(frame["player"]).restricted_mana = Snapshot.deep_copy(frame["restricted"]) if frame["restricted"]
|
|
254
262
|
player(frame["player"]).pool_sources = Snapshot.deep_copy(frame["pool_sources"]) if frame.key?("pool_sources")
|
|
255
263
|
state.pending_triggers.select! { frame["triggers_before"].include?(it["id"]) }
|
|
256
264
|
end
|
|
257
265
|
|
|
266
|
+
def untap_for_rewind!(ids)
|
|
267
|
+
ids.select { state.objects.key?(it) }.each do |id|
|
|
268
|
+
object(id).tapped = false
|
|
269
|
+
emit Events::Untapped.new(object: id)
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Rewinding an irreversible payment (one that can't be finished, #stuck?). CR 734.1 lets the player reverse the
|
|
274
|
+
# mana abilities activated meanwhile, but one with a cost such as life or a sacrifice can't be undone here, so
|
|
275
|
+
# none is: their sources stay tapped and their mana stays in the pool, and the mana spent on this payment goes
|
|
276
|
+
# back to the pool. What else it tapped (convoke, waterbend, a cost's choices) is untapped.
|
|
277
|
+
def refund_payment!(frame)
|
|
278
|
+
untap_for_rewind!(frame["tapped"] - frame.fetch("mana_tapped", []))
|
|
279
|
+
pl = player(frame["player"])
|
|
280
|
+
frame.fetch("refund", []).each do |spent|
|
|
281
|
+
next pl.restricted_mana << Snapshot.deep_copy(spent["restricted"]) if spent["restricted"]
|
|
282
|
+
|
|
283
|
+
pl.mana_pool[spent["mana"].to_sym] += 1
|
|
284
|
+
(pl.pool_sources ||= []) << Snapshot.deep_copy(spent["source"]) if spent["source"]
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# Whether the payment or cost choice in progress has no legal choice left: it was made irreversible (CR 605.3a)
|
|
289
|
+
# and can't be finished. It can then still be rewound (CR 734.1, #refund_payment!), so a decision always has
|
|
290
|
+
# an option.
|
|
291
|
+
def stuck?(options) = options.empty? && !payment.nil? && payment["kind"] != "trigger"
|
|
292
|
+
|
|
258
293
|
# ---- countering (CR 701.6) ----------------------------------------------------------------
|
|
259
294
|
|
|
260
295
|
# CR 701.6a: a countered spell goes to its owner's graveyard; a countered ability ceases to exist.
|
|
@@ -326,7 +361,10 @@ module Enjine
|
|
|
326
361
|
special = frame["special"]
|
|
327
362
|
if outcome == "paid" && state.objects.key?(special["object"])
|
|
328
363
|
obj = object(special["object"])
|
|
329
|
-
|
|
364
|
+
if special["unlock"] then unlock_door!(obj, special["unlock"], index) # CR 709.5e (Engine::Rooms)
|
|
365
|
+
elsif special["face_up"] then turn_face_up!(obj, index) # CR 116.2b (Engine::FaceDownPermanents)
|
|
366
|
+
else chars(obj).ability(special["ability"]).special!(self, obj, index)
|
|
367
|
+
end
|
|
330
368
|
end
|
|
331
369
|
action_taken!(index)
|
|
332
370
|
:done
|
|
@@ -66,14 +66,16 @@ module Enjine
|
|
|
66
66
|
# The options the block makes of each [candidate ref, object, play method]. While no permanent lets `index`
|
|
67
67
|
# play cards from other zones and no card was granted an ability, a card in a graveyard or library can only
|
|
68
68
|
# be played by an ability of its own (#unplayable_elsewhere?); nor while one gives cards an alternative cost.
|
|
69
|
+
# Nor an exiled card of another owner no permission effect names (Engine::OptionsMemo#unplayable_candidate?).
|
|
70
|
+
# The play methods are found once per decision: the cast and the land options both ask (Engine#options).
|
|
69
71
|
def candidate_methods(index, land: false, &)
|
|
70
72
|
sources = permission_sources(index)
|
|
71
73
|
plain = sources.empty? && own_abilities_only? && granted_alt_zones(index).empty?
|
|
72
74
|
permission_candidates(index, sources).flat_map do |ref|
|
|
73
75
|
obj = candidate_object(index, ref)
|
|
74
|
-
next [] if
|
|
76
|
+
next [] if unplayable_candidate?(index, obj, plain, land)
|
|
75
77
|
|
|
76
|
-
|
|
78
|
+
candidate_play_methods(index, ref, obj, sources).filter_map { yield ref, obj, it }
|
|
77
79
|
end
|
|
78
80
|
end
|
|
79
81
|
|
|
@@ -132,7 +134,8 @@ module Enjine
|
|
|
132
134
|
|
|
133
135
|
# Permanents `index` controls whose static abilities let them play cards from other zones:
|
|
134
136
|
# [[permanent, AbilityInstance], ...].
|
|
135
|
-
|
|
137
|
+
# Memoized per decision while legal options are computed (Engine::OptionsMemo#permission_sources).
|
|
138
|
+
def scan_permission_sources(index)
|
|
136
139
|
state.battlefield.flat_map do |id|
|
|
137
140
|
obj = object(id)
|
|
138
141
|
next [] unless obj.controller == index
|
|
@@ -23,7 +23,7 @@ module Enjine
|
|
|
23
23
|
return Enjine::Characteristics::FACE_DOWN if face_down?(object) && @face_up != object.id # CR 406.3a
|
|
24
24
|
|
|
25
25
|
# layer 1 (CR 613.2a-c): its copiable values
|
|
26
|
-
return
|
|
26
|
+
return board_chars(object) if object.zone == :battlefield # Engine::OptionsMemo
|
|
27
27
|
return @options_memo[object.id] ||= off_board_chars(object) if @options_memo # the state holds still meanwhile
|
|
28
28
|
|
|
29
29
|
off_board_chars(object)
|
|
@@ -49,10 +49,10 @@ module Enjine
|
|
|
49
49
|
# detected separately by identity. While a static ability has a condition or a computed bonus, or a
|
|
50
50
|
# permanent has a characteristic-defining power/toughness, any event may change the result, so every
|
|
51
51
|
# event drops the cache (`@volatile`, see Engine#emit).
|
|
52
|
-
LAYER_EVENTS = [Events::ZoneChanged, Events::CountersChanged, Events::EffectStarted,
|
|
53
|
-
Events::
|
|
54
|
-
Events::
|
|
55
|
-
Events::
|
|
52
|
+
LAYER_EVENTS = [Events::ZoneChanged, Events::CountersChanged, Events::EffectStarted, Events::EffectsEnded, Events::Attached,
|
|
53
|
+
Events::Unattached, Events::ControlChanged, Events::PhasedOut, Events::PhasedIn, Events::SpeedChanged,
|
|
54
|
+
Events::Transformed, Events::ClassLevelChanged, Events::EmblemCreated, Events::PermanentDesignated,
|
|
55
|
+
Events::DoorUnlocked, Events::DoorLocked, Events::TurnedFaceUp].freeze
|
|
56
56
|
|
|
57
57
|
def touch! = @epoch = @epoch.to_i + 1
|
|
58
58
|
def volatile_layers? = @volatile
|
|
@@ -70,13 +70,14 @@ module Enjine
|
|
|
70
70
|
def apply_layers
|
|
71
71
|
statics = static_sources
|
|
72
72
|
effects = state.effects
|
|
73
|
-
@volatile = statics.any? { |_, ability| ability.
|
|
73
|
+
@volatile = statics.any? { |_, ability| ability.volatile? } || defined_on_battlefield?
|
|
74
74
|
return {} if statics.empty? && effects.empty? && !@volatile && printed_board?
|
|
75
75
|
|
|
76
76
|
@layering = {}
|
|
77
77
|
ids = state.battlefield
|
|
78
78
|
active = statics.select { |source, ability| ability.active?(self, source) }
|
|
79
|
-
|
|
79
|
+
# CR 702.176a: an impending permanent's own "it's not a creature" comes first (Abilities::Impending.effects).
|
|
80
|
+
ids.each { @layering[it] = type_layers(object(it), active, Mechanics::Abilities::Impending.effects(self, object(it)) + effects) }
|
|
80
81
|
later_layers(ids, statics, ids.select { losing_abilities?(object(it), active, effects) }, effects)
|
|
81
82
|
@layering.reject { |id, c| c.equal?(definition(object(id)).printed_characteristics) }
|
|
82
83
|
ensure
|
|
@@ -112,8 +113,6 @@ module Enjine
|
|
|
112
113
|
|
|
113
114
|
def defined_on_battlefield? = state.battlefield.any? { definition(object(it)).defined_pt? }
|
|
114
115
|
|
|
115
|
-
def facts(obj, current) = Mechanics::Filters::Facts.new(chars: current, counters: obj.counters, token: token?(obj))
|
|
116
|
-
|
|
117
116
|
# ---- layer 1 (CR 613.2a-c, 707.2) ---------------------------------------------------------
|
|
118
117
|
|
|
119
118
|
# The object's copiable values (CR 707.2): the printed characteristics of its card, which is the
|
data/lib/enjine/engine/combat.rb
CHANGED
|
@@ -35,13 +35,20 @@ module Enjine
|
|
|
35
35
|
# declaration may end only once every such requirement is obeyed (no attack has a cost yet).
|
|
36
36
|
def unmet_attack_requirements = attack_candidates.select { chars(it).keyword?(:attacks_each_combat) }
|
|
37
37
|
|
|
38
|
-
# CR 506.3, 508.1b: the defending player or a
|
|
38
|
+
# CR 506.3, 508.1b: the defending player, a planeswalker they control, or a battle they protect (CR 310.5,
|
|
39
|
+
# 310.9: its protector's opponents may attack it, whoever controls it).
|
|
39
40
|
def attack_targets
|
|
40
41
|
defender = defending_player
|
|
41
42
|
[Mechanics::Filters.player_ref(self, defender)] +
|
|
42
|
-
state.battlefield.select
|
|
43
|
+
state.battlefield.select do |id|
|
|
44
|
+
current = chars(id)
|
|
45
|
+
current.type?(:battle) ? protector(object(id)) == defender : object(id).controller == defender && current.planeswalker?
|
|
46
|
+
end
|
|
43
47
|
end
|
|
44
48
|
|
|
49
|
+
# CR 310.9: the player who protects a battle (nil for a permanent that isn't one or has none).
|
|
50
|
+
def protector(obj) = obj.memory["protector"]
|
|
51
|
+
|
|
45
52
|
# What creature `id` may attack: not a player with "creatures with flying can't attack you" matching it
|
|
46
53
|
# (CR 508.1c; their planeswalkers still can be attacked).
|
|
47
54
|
def attack_targets_for(id)
|
|
@@ -76,6 +83,7 @@ module Enjine
|
|
|
76
83
|
def withdraw_attacker!(id)
|
|
77
84
|
attackers.delete(id)
|
|
78
85
|
state.attacks_declared -= 1
|
|
86
|
+
touch! # statics reading combat status (CR 506.4)
|
|
79
87
|
attack_withdrawn!(id) # Engine::TurnHistory
|
|
80
88
|
state.pending_triggers.reject! { it["event"]["type"] == "attacker_declared" && it["event"]["object"] == id }
|
|
81
89
|
end
|
|
@@ -305,11 +313,14 @@ module Enjine
|
|
|
305
313
|
|
|
306
314
|
# CR 510.2: all combat damage in a step is dealt simultaneously.
|
|
307
315
|
def deal_combat_damage!(hits)
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
316
|
+
# CR 510.2, 603.2c: one event batch for "one or more creatures deal combat damage" triggers
|
|
317
|
+
in_batch("combat_damage:#{state.next_id}") do
|
|
318
|
+
simultaneous_damage do
|
|
319
|
+
dealt = Hash.new(0)
|
|
320
|
+
lifelinked = hits.map(&:first).uniq.select { damage_source_keyword?(it, :lifelink) }
|
|
321
|
+
hits.each { |source, target, amount| dealt[source] += deal_damage!(source, target, amount, combat: true, lifelink: false) }
|
|
322
|
+
lifelinked.each { lifelink!(it, dealt[it]) if dealt[it].positive? } # CR 120.3f, 119.9: one gain per source
|
|
323
|
+
end
|
|
313
324
|
end
|
|
314
325
|
turn_based_actions_done!
|
|
315
326
|
end
|
|
@@ -327,6 +338,7 @@ module Enjine
|
|
|
327
338
|
state.combat_damage_pass = nil
|
|
328
339
|
attackers.clear
|
|
329
340
|
state.blockers.clear
|
|
341
|
+
touch! # statics reading combat status (CR 506.4)
|
|
330
342
|
end
|
|
331
343
|
end
|
|
332
344
|
include Combat
|
data/lib/enjine/engine/copies.rb
CHANGED
|
@@ -26,7 +26,8 @@ module Enjine
|
|
|
26
26
|
module Copies
|
|
27
27
|
# ---- reading -------------------------------------------------------------------------------
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
# A face-down object is its hidden card (Engine::FaceDownPermanents).
|
|
30
|
+
def own_card(obj) = obj.memory["face_down_card"] || obj.memory["own_card"] || obj.card
|
|
30
31
|
|
|
31
32
|
# A token (CR 111.1): created as one, or a copy of a permanent spell that resolved (CR 608.3f).
|
|
32
33
|
def token?(obj) = obj.memory["token"] == true || catalog.fetch(own_card(obj)).token?
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Enjine
|
|
4
|
+
class Engine
|
|
5
|
+
# Committing a crime (CR 700.13): a player commits a crime as they cast a spell, activate an ability or put a
|
|
6
|
+
# triggered ability on the stack that targets at least one opponent, a permanent, spell or ability an opponent
|
|
7
|
+
# controls, or a card in an opponent's graveyard. One spell or ability is one crime, however many of its
|
|
8
|
+
# targets qualify. Checked once the spell is cast, the ability activated or the trigger put on the stack with
|
|
9
|
+
# its targets (Events::SpellCast, AbilityActivated, TriggerPutOnStack), so a cancelled cast is no crime; a copy
|
|
10
|
+
# put on the stack (CR 707.10) or new targets chosen later (CR 115.7) aren't cast or activated, so they aren't
|
|
11
|
+
# either. The turn log counts them ("crimes", amount `crimes_this_turn`), and Events::CrimeCommitted is what
|
|
12
|
+
# "whenever you commit a crime" (trigger `commits_crime`) watches.
|
|
13
|
+
module Crimes
|
|
14
|
+
CRIME_EVENTS = [Events::SpellCast, Events::AbilityActivated, Events::TriggerPutOnStack].freeze
|
|
15
|
+
|
|
16
|
+
# Emits the crime that `event` (a spell cast, an ability activated or a trigger put on the stack) commits.
|
|
17
|
+
def check_crime!(event)
|
|
18
|
+
return unless CRIME_EVENTS.include?(event.class)
|
|
19
|
+
|
|
20
|
+
index = names.index(event.player)
|
|
21
|
+
return unless index && Array(event.targets).flatten.compact.any? { crime_target?(index, it) }
|
|
22
|
+
|
|
23
|
+
emit Events::CrimeCommitted.new(player: event.player, object: event.object)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# CR 700.13: an opponent, a permanent, spell or ability an opponent controls, or a card in an opponent's graveyard.
|
|
27
|
+
def crime_target?(index, ref)
|
|
28
|
+
player = Mechanics::Filters.player_index(self, ref)
|
|
29
|
+
return player != index unless player.nil?
|
|
30
|
+
|
|
31
|
+
obj = state.objects[ref] or return false
|
|
32
|
+
case obj.zone
|
|
33
|
+
when :battlefield, :stack then obj.controller != index
|
|
34
|
+
when :graveyard then obj.owner != index
|
|
35
|
+
else false
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
include Crimes
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
module Events
|
|
43
|
+
# CR 700.13: `player` committed a crime with the spell or ability `object`.
|
|
44
|
+
CrimeCommitted = define(:crime_committed, :player, :object)
|
|
45
|
+
end
|
|
46
|
+
end
|
data/lib/enjine/engine/damage.rb
CHANGED
|
@@ -89,6 +89,7 @@ module Enjine
|
|
|
89
89
|
obj.deathtouched = true if has[:deathtouch] # CR 702.2b
|
|
90
90
|
end
|
|
91
91
|
remove_counters!(obj, "loyalty", amount) if defn.planeswalker? # CR 120.3c
|
|
92
|
+
remove_counters!(obj, "defense", amount) if defn.type?(:battle) # CR 120.3h, 310.6
|
|
92
93
|
end
|
|
93
94
|
lifelink!(source, amount) if lifelink && has[:lifelink]
|
|
94
95
|
amount
|
|
@@ -103,12 +104,14 @@ module Enjine
|
|
|
103
104
|
end
|
|
104
105
|
|
|
105
106
|
# CR 120.6, 120.4a: damage that would be lethal to the creature `id` from `source`, counting damage already
|
|
106
|
-
# marked on it (1 if the source has deathtouch, CR 702.2c); a planeswalker's is its loyalty
|
|
107
|
+
# marked on it (1 if the source has deathtouch, CR 702.2c); a planeswalker's is its loyalty, a battle's its defense
|
|
108
|
+
# (CR 120.10).
|
|
107
109
|
# Nil for a player or an object that is neither.
|
|
108
110
|
def lethal_damage(source, id)
|
|
109
111
|
obj = state.objects[id]
|
|
110
112
|
return nil unless obj && state.battlefield.include?(id)
|
|
111
113
|
return loyalty(obj) if !chars(obj).creature? && chars(obj).planeswalker?
|
|
114
|
+
return obj.counters.fetch("defense", 0) if !chars(obj).creature? && chars(obj).type?(:battle) # CR 310.4c
|
|
112
115
|
return nil unless chars(obj).creature?
|
|
113
116
|
|
|
114
117
|
lethal = [toughness(obj) - obj.damage, 0].max
|
|
@@ -129,6 +132,16 @@ module Enjine
|
|
|
129
132
|
@damage_batch = nil
|
|
130
133
|
end
|
|
131
134
|
|
|
135
|
+
# The events of the block are one event batch for "one or more" triggers (CR 603.2c; Engine::Triggering#event_batch),
|
|
136
|
+
# unless an outer block already named one.
|
|
137
|
+
def in_batch(name)
|
|
138
|
+
outer = @event_batch
|
|
139
|
+
@event_batch ||= name
|
|
140
|
+
yield
|
|
141
|
+
ensure
|
|
142
|
+
@event_batch = outer
|
|
143
|
+
end
|
|
144
|
+
|
|
132
145
|
# Whether a damage source has `keyword` (deathtouch, lifelink, wither): a card (a permanent or a spell) as it
|
|
133
146
|
# is now; a source no longer in the zone it's expected to be in, as it last existed (CR 702.2e, 702.15c,
|
|
134
147
|
# 702.80b).
|
|
@@ -204,6 +217,28 @@ module Enjine
|
|
|
204
217
|
emit Events::PlayerCountersChanged.new(player: pl.name, counter: kind, from: before, to: before + amount)
|
|
205
218
|
end
|
|
206
219
|
|
|
220
|
+
# Removes up to `amount` counters of `kind` from a player (CR 122.1, 122.8). Returns how many came off.
|
|
221
|
+
def remove_player_counters!(index, kind, amount)
|
|
222
|
+
pl = player(index)
|
|
223
|
+
before = player_counters(index).fetch(kind, 0)
|
|
224
|
+
after = [before - amount, 0].max
|
|
225
|
+
return 0 if after == before
|
|
226
|
+
|
|
227
|
+
if kind == "poison" then pl.poison = after
|
|
228
|
+
elsif after.zero? then pl.counters.delete(kind)
|
|
229
|
+
else pl.counters[kind] = after
|
|
230
|
+
end
|
|
231
|
+
emit Events::PlayerCountersChanged.new(player: pl.name, counter: kind, from: before, to: after)
|
|
232
|
+
before - after
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# CR 122.1: every counter of `kind` (of every kind, if nil) on a player.
|
|
236
|
+
def remove_all_player_counters!(index, kind = nil)
|
|
237
|
+
(kind ? [kind] : player_counters(index).keys.sort).each do
|
|
238
|
+
remove_player_counters!(index, it, player_counters(index).fetch(it, 0))
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
207
242
|
def remove_counters!(object, kind, amount)
|
|
208
243
|
before = object.counters.fetch(kind, 0)
|
|
209
244
|
after = [before - amount, 0].max
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Enjine
|
|
4
|
+
class Engine
|
|
5
|
+
# Face-down spells and permanents (CR 708): manifested (CR 701.40), cloaked (CR 701.58), and cast face down with
|
|
6
|
+
# morph (CR 702.37) or disguise (CR 702.168). The object's `card` names its face-down characteristics
|
|
7
|
+
# (CardDefinition::FaceDownCards: a nameless 2/2 creature, with ward {2} for cloak and disguise) and they are its
|
|
8
|
+
# copiable values (CR 708.2), so every rule reads them with no special case; the card it really is waits in
|
|
9
|
+
# its memory:
|
|
10
|
+
#
|
|
11
|
+
# memory["face_down_card"] the card's own id (Engine::Copies#own_card reads it)
|
|
12
|
+
# memory["face_down_as"] "manifest" | "cloak" | "morph" | "disguise": what made it face down
|
|
13
|
+
# memory["turned_face_up"] the turn it was turned face up (it's the same object, CR 708.8)
|
|
14
|
+
#
|
|
15
|
+
# Its id doesn't name the card (Engine::FACE_DOWN_ID, like a card exiled face down). Only its controller may
|
|
16
|
+
# look at it (CR 708.5, View and Events::ZoneChanged); it is revealed as it leaves the battlefield or the stack
|
|
17
|
+
# for anywhere but the battlefield, and at the end of the game (CR 708.9).
|
|
18
|
+
module FaceDownPermanents
|
|
19
|
+
AS = { "manifest" => CardDefinition::FaceDownCards::PLAIN, "cloak" => CardDefinition::FaceDownCards::WARD,
|
|
20
|
+
"morph" => CardDefinition::FaceDownCards::PLAIN, "disguise" => CardDefinition::FaceDownCards::WARD }.freeze
|
|
21
|
+
|
|
22
|
+
def face_down_object?(obj) = obj.memory.key?("face_down_card")
|
|
23
|
+
|
|
24
|
+
# CR 708.5: you may look at a face-down spell or permanent you control.
|
|
25
|
+
def sees_face_down?(obj, index) = !face_down_object?(obj) || obj.controller == index
|
|
26
|
+
|
|
27
|
+
# CR 701.40a, 701.58a, 702.37c, 702.168b: it becomes a face-down 2/2 (with ward {2} for cloak and disguise).
|
|
28
|
+
def turn_face_down!(obj, kind)
|
|
29
|
+
obj.memory["face_down_card"] = obj.card
|
|
30
|
+
obj.memory["face_down_as"] = kind
|
|
31
|
+
obj.card = AS.fetch(kind)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The ways `obj` (a face-down permanent) can be turned face up now and their costs (CR 701.40b-d, 701.58b-d,
|
|
35
|
+
# 702.37e, 702.168d): "mana_cost" for a manifested or cloaked creature card with a mana cost, and its own
|
|
36
|
+
# "morph" / "disguise" cost (whatever made it face down).
|
|
37
|
+
def face_up_costs(obj)
|
|
38
|
+
return {} unless face_down_object?(obj) && obj.zone == :battlefield
|
|
39
|
+
|
|
40
|
+
real = catalog.fetch(obj.memory["face_down_card"])
|
|
41
|
+
costs = {}
|
|
42
|
+
costs["mana_cost"] = real.mana_cost if %w[manifest cloak].include?(obj.memory["face_down_as"]) && real.creature? && real.mana_cost
|
|
43
|
+
real.ability_instances.each do |inst|
|
|
44
|
+
ability = inst.ability
|
|
45
|
+
costs[ability.keyword] ||= ability.face_up_cost if ability.respond_to?(:face_up_cost) && ability.face_up_cost
|
|
46
|
+
end
|
|
47
|
+
costs
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# CR 116.2b: any time they have priority, a player may turn a face-down permanent they control face up.
|
|
51
|
+
def face_up_options(index)
|
|
52
|
+
state.battlefield.flat_map do |id|
|
|
53
|
+
obj = object(id)
|
|
54
|
+
next [] unless obj.controller == index && face_down_object?(obj)
|
|
55
|
+
|
|
56
|
+
face_up_costs(obj).filter_map { |via, cost| { type: "turn_face_up", object: id, via: via } if affordable?(index, cost, nil) }
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# CR 708.8: its copiable values become its card's; effects that applied to it still apply, and abilities about
|
|
61
|
+
# entering the battlefield don't trigger. An instant or sorcery card is revealed and stays face down
|
|
62
|
+
# (CR 701.40g, 701.58g).
|
|
63
|
+
def turn_face_up!(obj, index = obj.controller)
|
|
64
|
+
return false unless face_down_object?(obj) && obj.zone == :battlefield
|
|
65
|
+
|
|
66
|
+
real = obj.memory["face_down_card"]
|
|
67
|
+
defn = catalog.fetch(real)
|
|
68
|
+
if defn.type?(:instant) || defn.type?(:sorcery)
|
|
69
|
+
emit Events::CardRevealed.new(player: name(index), object: obj.id, card: real)
|
|
70
|
+
return false
|
|
71
|
+
end
|
|
72
|
+
obj.card = obj.memory.delete("face_down_card")
|
|
73
|
+
obj.memory.delete("face_down_as")
|
|
74
|
+
obj.memory["turned_face_up"] = state.turn
|
|
75
|
+
emit Events::TurnedFaceUp.new(player: name(index), object: obj.id, card: real)
|
|
76
|
+
true
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# CR 701.40a, 701.58a, 701.40f: the top card of `index`'s library is put onto the battlefield face down under
|
|
80
|
+
# their control. Returns the new permanent, or nil.
|
|
81
|
+
def manifest!(index, kind = "manifest", id = player(index).library.first)
|
|
82
|
+
return nil unless id
|
|
83
|
+
|
|
84
|
+
fresh = move!(id, :battlefield, reason: kind, controller: index, face_down_as: kind)
|
|
85
|
+
fresh&.zone == :battlefield ? fresh : nil
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# CR 708.9: what was face down is revealed to all as it leaves the battlefield (or the stack) for a zone
|
|
89
|
+
# where it isn't public.
|
|
90
|
+
def reveal_face_down_left!(old, fresh)
|
|
91
|
+
return unless face_down_object?(old) && %i[hand library].include?(fresh.zone)
|
|
92
|
+
|
|
93
|
+
emit Events::CardRevealed.new(player: name(old.owner), object: fresh.id, card: fresh.card)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# CR 708.9: at the end of the game every face-down permanent and spell is revealed to all.
|
|
97
|
+
def reveal_face_down_at_end!
|
|
98
|
+
(state.battlefield + state.stack).each do |id|
|
|
99
|
+
obj = state.objects[id]
|
|
100
|
+
next unless obj && face_down_object?(obj)
|
|
101
|
+
|
|
102
|
+
emit Events::CardRevealed.new(player: name(obj.owner), object: id, card: obj.memory["face_down_card"])
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
include FaceDownPermanents
|
|
107
|
+
end
|
|
108
|
+
end
|
data/lib/enjine/engine/faces.rb
CHANGED
|
@@ -13,7 +13,7 @@ module Enjine
|
|
|
13
13
|
# memory["melded"] on a melded permanent (its object is one card of the pair, back face up): the
|
|
14
14
|
# other card's id (CR 713.1a)
|
|
15
15
|
module Faces
|
|
16
|
-
ALTERNATIVE_SPELLS = %w[modal adventure omen].freeze
|
|
16
|
+
ALTERNATIVE_SPELLS = %w[modal adventure omen room].freeze
|
|
17
17
|
|
|
18
18
|
# ---- reading -----------------------------------------------------------------------------
|
|
19
19
|
|
|
@@ -31,6 +31,7 @@ module Enjine
|
|
|
31
31
|
def card_after_move(old, to)
|
|
32
32
|
own = own_card(old)
|
|
33
33
|
return own if old.memory["copy"] || (old.zone == :stack && to == :battlefield)
|
|
34
|
+
return room_locked_face(own) if to == :battlefield && catalog.fetch(own).layout == "room" # CR 709.5d (Engine::Rooms)
|
|
34
35
|
|
|
35
36
|
catalog.fetch(own).card.id
|
|
36
37
|
end
|
|
@@ -63,7 +64,7 @@ module Enjine
|
|
|
63
64
|
# face is an instant or sorcery (CR 701.27e). It stays the same object (CR 701.27c, 712.15).
|
|
64
65
|
def transform!(obj)
|
|
65
66
|
own = catalog.fetch(own_card(obj))
|
|
66
|
-
return false unless obj.zone == :battlefield && own.double_faced?
|
|
67
|
+
return false unless obj.zone == :battlefield && own.double_faced? && !face_down_object?(obj)
|
|
67
68
|
|
|
68
69
|
into = own.other_face
|
|
69
70
|
return false if into.type?(:instant) || into.type?(:sorcery)
|
|
@@ -133,11 +134,13 @@ module Enjine
|
|
|
133
134
|
# ---- casting another face (CR 712.8b, 716.3, 721.3) ----------------------------------------------
|
|
134
135
|
|
|
135
136
|
# The faces other than the front a card can be cast as: a modal card's nonland faces, an Adventure, an
|
|
136
|
-
# Omen. A preparation card's spell is cast only as a copy (CR 723.3).
|
|
137
|
+
# Omen, a Room's doors. A preparation card's spell is cast only as a copy (CR 723.3).
|
|
137
138
|
def alternative_faces(obj)
|
|
138
139
|
defn = catalog.fetch(obj.card)
|
|
139
140
|
return [] if obj.memory["copy"] || defn.face_index.positive? || !ALTERNATIVE_SPELLS.include?(defn.layout)
|
|
140
141
|
|
|
142
|
+
return Rooms::DOOR_FACE.values if defn.layout == "room" # CR 709.3: either door, never both (Engine::Rooms)
|
|
143
|
+
|
|
141
144
|
(1...defn.faces.size).reject { defn.face(it).land? }
|
|
142
145
|
end
|
|
143
146
|
|
|
@@ -12,6 +12,7 @@ module Enjine
|
|
|
12
12
|
# lets restricted mana count where it may be spent (CR 106.6).
|
|
13
13
|
def affordable?(index, cost, excluded_source, purpose = nil)
|
|
14
14
|
return true if cost.nil? || cost.mana_value.zero?
|
|
15
|
+
return false if @options_memo && cost.equivalents.all? { it.mana_value > mana_unit_bound(index) }
|
|
15
16
|
|
|
16
17
|
covers?(cost, mana_units(index, excluded_source, purpose))
|
|
17
18
|
end
|
|
@@ -54,8 +55,10 @@ module Enjine
|
|
|
54
55
|
|
|
55
56
|
ability = chars(id).ability(ref)
|
|
56
57
|
next if ability.mana_cost || (ability.spend_only && !ability.spend_only.allows?(self, purpose))
|
|
58
|
+
next if (granter = tapped_granter(ref, ability)) && excluded.include?(granter)
|
|
57
59
|
|
|
58
|
-
|
|
60
|
+
# Abilities that tap the permanent granting them (CR 602.2b) make mana once among them all.
|
|
61
|
+
by_source[granter ? "granter:#{granter}" : id].concat(ability.outcomes(self, object(id)))
|
|
59
62
|
end
|
|
60
63
|
units = by_source.values.flat_map { |options| Array.new(options.map(&:size).max) { options.flatten.uniq } }
|
|
61
64
|
as_any_type(units, purpose)
|
|
@@ -102,7 +105,19 @@ module Enjine
|
|
|
102
105
|
# CR 602.5b: "Activate only once each turn" counts on the source, as for other activated abilities.
|
|
103
106
|
def mana_ability_usable?(obj, inst)
|
|
104
107
|
per_turn = inst.ability.per_turn
|
|
105
|
-
(per_turn.nil? || activations_this_turn(obj, inst.ref) < per_turn) &&
|
|
108
|
+
(per_turn.nil? || activations_this_turn(obj, inst.ref) < per_turn) &&
|
|
109
|
+
inst.ability.activatable?(self, obj, mana_ability_costs(inst.ref, inst.ability))
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# A mana ability's costs, those naming the permanent that granted it bound to it ("tap the granter", CR 602.2b,
|
|
113
|
+
# 607.1a; Engine::Casting#granter_costs).
|
|
114
|
+
def mana_ability_costs(ref, ability) = granter_costs(ability.costs, AbilityLayers.granted_parts(ref)&.last)
|
|
115
|
+
|
|
116
|
+
# The permanent a granted mana ability taps besides its source ({"tap": "granter"}), or nil.
|
|
117
|
+
def tapped_granter(ref, ability)
|
|
118
|
+
return nil unless ability.costs.any?(Mechanics::Costs::TapGranter)
|
|
119
|
+
|
|
120
|
+
mana_ability_costs(ref, ability).find { it.is_a?(Mechanics::Costs::TapGranter) }&.granter
|
|
106
121
|
end
|
|
107
122
|
|
|
108
123
|
# Activation options for mana abilities: one per source, or one per mana it could add when the
|
|
@@ -126,7 +141,7 @@ module Enjine
|
|
|
126
141
|
from = mana_provenance(id) # before a cost sacrifices it (a Treasure)
|
|
127
142
|
record_activation!("source" => id, "ability" => ability_ref) if ability.per_turn
|
|
128
143
|
pay_from_pool!(index, ability.mana_cost, id) if ability.mana_cost
|
|
129
|
-
ability.
|
|
144
|
+
mana_ability_costs(ability_ref, ability).each { |cost| cost.pay!(self, obj) }
|
|
130
145
|
produced = chosen || ability.outcomes(self, obj).first # CR 605.3b: a fixed amount is counted as it resolves
|
|
131
146
|
add_mana!(index, produced, id, restriction: ability.spend_only&.data, from: from)
|
|
132
147
|
end
|