enjine 0.6.4 → 0.6.5
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 +42 -1
- data/SUPPORTED.md +5 -4
- data/docs/guides/api.md +3 -0
- data/docs/guides/card-data-format.md +11 -1
- data/docs/plans/dsl-gaps.md +8 -8
- data/docs/reference/abilities.md +16 -5
- data/docs/reference/decisions.md +1 -1
- data/docs/reference/effects.md +6 -5
- data/lib/enjine/actions/activate_mana_ability.rb +3 -2
- data/lib/enjine/cli/text.rb +2 -1
- data/lib/enjine/decisions/casting.rb +1 -1
- data/lib/enjine/engine/casting/casting.rb +1 -0
- data/lib/enjine/engine/casting/payment.rb +16 -10
- data/lib/enjine/engine/damage.rb +1 -0
- data/lib/enjine/engine/mana_helpers.rb +75 -0
- data/lib/enjine/engine/mana_sources.rb +19 -8
- data/lib/enjine/engine/turn_history.rb +8 -0
- data/lib/enjine/mechanics/abilities/cost_changes.rb +62 -17
- data/lib/enjine/mechanics/abilities/mana_ability.rb +54 -9
- data/lib/enjine/mechanics/card_spec.rb +28 -2
- data/lib/enjine/mechanics/effects/choose_cards.rb +30 -15
- data/lib/enjine/mechanics/effects/damage.rb +24 -4
- data/lib/enjine/mechanics/effects/mana.rb +4 -1
- data/lib/enjine/mechanics/keywords/alternative_costs.rb +3 -2
- data/lib/enjine/mechanics/keywords/bargain.rb +39 -0
- data/lib/enjine/mechanics/selector.rb +29 -8
- data/lib/enjine/version.rb +1 -1
- metadata +3 -1
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Enjine
|
|
4
|
+
class Engine
|
|
5
|
+
# Mana abilities whose cost taps another permanent the player chooses: "{T}, Tap an untapped creature you
|
|
6
|
+
# control: Add one mana of any color" (Springleaf Drum, Gene Pollinator; CR 605.3a, 602.2b, 601.2h).
|
|
7
|
+
# A mana ability is paid for and resolves at once (CR 605.3a), so the permanent to tap (the "helper") is
|
|
8
|
+
# part of the activation option: `tap: id`, one option per helper (times each mana choice). Tapping a
|
|
9
|
+
# permanent for a cost isn't {T}, so a creature that came under its controller's control this turn may be
|
|
10
|
+
# tapped (CR 302.6). With {T} in the same cost, the source can't also be its own helper: once tapped for {T}
|
|
11
|
+
# it is no longer untapped (CR 601.2h, 107.5). Tapping a helper can be rewound with its source (CR 734.1).
|
|
12
|
+
module ManaHelpers
|
|
13
|
+
# The helper cost of a mana ability ({"tap_permanents": ...} with count 1), or nil.
|
|
14
|
+
def self.helper_cost(costs) = costs.find { it.is_a?(Mechanics::Costs::TapPermanents) }
|
|
15
|
+
|
|
16
|
+
# The untapped permanents that could be tapped for the helper cost of the mana ability `ability` of the
|
|
17
|
+
# permanent `obj` (with its costs as bound, `costs`): those that aren't mana sources first, so the plain
|
|
18
|
+
# answer is listed first.
|
|
19
|
+
def mana_helpers(obj, costs)
|
|
20
|
+
cost = ManaHelpers.helper_cost(costs) or return []
|
|
21
|
+
ids = cost.candidates(self, obj)
|
|
22
|
+
ids -= [obj.id] if costs.any?(Mechanics::Costs::Tap) # CR 601.2h: {T} taps the source itself
|
|
23
|
+
ids.partition { chars(it).mana_abilities.empty? }.flatten
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Whether the mana ability has a helper to tap now (part of ManaAbility#activatable?).
|
|
27
|
+
def mana_helper?(obj, costs) = ManaHelpers.helper_cost(costs).nil? || !mana_helpers(obj, costs).empty?
|
|
28
|
+
|
|
29
|
+
# One activation option per helper for a mana ability with a helper cost; `options` unchanged otherwise.
|
|
30
|
+
def with_mana_helpers(options, id, ref, ability)
|
|
31
|
+
costs = mana_ability_costs(ref, ability)
|
|
32
|
+
return options unless ManaHelpers.helper_cost(costs)
|
|
33
|
+
|
|
34
|
+
helpers = mana_helpers(object(id), costs)
|
|
35
|
+
options.flat_map { |option| helpers.map { option.merge(tap: it) } }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Before any cost is paid: a mana ability with a helper cost needs `helper` (an option's `tap`) to be one it
|
|
39
|
+
# could tap now, and one without names none.
|
|
40
|
+
def check_mana_helper!(obj, costs, helper)
|
|
41
|
+
return if ManaHelpers.helper_cost(costs) ? mana_helpers(obj, costs).include?(helper) : helper.nil?
|
|
42
|
+
|
|
43
|
+
raise ArgumentError, "#{obj.id}: #{helper.inspect} can't be tapped for this mana ability's cost (tap:)"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Affordability (ManaSources#source_units) of the mana abilities with a helper cost, `deferred` as [id, ref,
|
|
47
|
+
# ability]. Each helper is tapped at most once and each source activated once:
|
|
48
|
+
# - a helper that makes no mana of its own: the source adds one unit;
|
|
49
|
+
# - another such source not yet used: it is tapped as the helper (it adds nothing itself), the source adds one;
|
|
50
|
+
# - a mana source already counted (`by_source`): tapping it for the cost only changes which mana its unit can be,
|
|
51
|
+
# so its unit widens to the source's mana and no unit is added.
|
|
52
|
+
# Greedy, in battlefield order: an estimate for which casts to offer, as convoke's is.
|
|
53
|
+
def add_mana_helper_units!(index, by_source, deferred, excluded)
|
|
54
|
+
sources = all_mana_sources(index).map(&:first)
|
|
55
|
+
used = []
|
|
56
|
+
deferred.each do |id, ref, ability|
|
|
57
|
+
next if used.include?(id)
|
|
58
|
+
|
|
59
|
+
helpers = mana_helpers(object(id), mana_ability_costs(ref, ability)) - excluded - used
|
|
60
|
+
outcomes = ability.outcomes(self, object(id))
|
|
61
|
+
free = helpers.find { !sources.include?(it) } || helpers.find { |h| deferred.any? { it.first == h } }
|
|
62
|
+
if free
|
|
63
|
+
used.push(id, free)
|
|
64
|
+
by_source[id].concat(outcomes)
|
|
65
|
+
elsif (counted = helpers.find { by_source.key?(it) })
|
|
66
|
+
used.push(id, counted)
|
|
67
|
+
by_source[counted].concat(outcomes)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
include ManaHelpers
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -51,18 +51,21 @@ module Enjine
|
|
|
51
51
|
def source_units(index, excluded, purpose = nil)
|
|
52
52
|
by_source = Hash.new { |h, k| h[k] = [] }
|
|
53
53
|
repeats = {}
|
|
54
|
+
deferred = [] # tapping another permanent too (Engine::ManaHelpers)
|
|
54
55
|
all_mana_sources(index).each do |id, ref|
|
|
55
56
|
next if excluded.include?(id)
|
|
56
57
|
|
|
57
58
|
ability = chars(id).ability(ref)
|
|
58
|
-
next if ability.mana_cost || (ability.spend_only && !ability.spend_only.allows?(self, purpose))
|
|
59
|
+
next if ability.mana_cost || (ability.spend_only && !ability.spend_only.bound(self, id).allows?(self, purpose))
|
|
59
60
|
next if (granter = tapped_granter(ref, ability)) && excluded.include?(granter)
|
|
61
|
+
next deferred << [id, ref, ability] if ManaHelpers.helper_cost(ability.costs)
|
|
60
62
|
|
|
61
63
|
# Abilities that tap the permanent granting them (CR 602.2b) make mana once among them all.
|
|
62
64
|
by_source[granter ? "granter:#{granter}" : id].concat(ability.outcomes(self, object(id)))
|
|
63
65
|
# CR 605.3a: one without {T} whose costs only remove counters from it can be activated again
|
|
64
66
|
repeats[id] = [repeats.fetch(id, 1), ability.repeats(object(id), mana_ability_costs(ref, ability))].max unless granter
|
|
65
67
|
end
|
|
68
|
+
add_mana_helper_units!(index, by_source, deferred, excluded) unless deferred.empty?
|
|
66
69
|
units = by_source.flat_map do |key, options|
|
|
67
70
|
Array.new(options.map(&:size).max.to_i * repeats.fetch(key, 1)) { options.flatten.uniq }
|
|
68
71
|
end
|
|
@@ -140,27 +143,35 @@ module Enjine
|
|
|
140
143
|
def mana_activation_options(sources)
|
|
141
144
|
sources.flat_map do |id, ref|
|
|
142
145
|
ability = chars(id).ability(ref)
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
+
options = if ability.choice?
|
|
147
|
+
ability.outcomes(self, object(id)).map { { type: "activate_mana", object: id, ability: ref, mana: Mana.symbols(it) } }
|
|
148
|
+
else
|
|
149
|
+
[{ type: "activate_mana", object: id, ability: ref }]
|
|
150
|
+
end
|
|
151
|
+
with_mana_helpers(options, id, ref, ability) # `tap`: the permanent tapped for its cost (Engine::ManaHelpers)
|
|
146
152
|
end
|
|
147
153
|
end
|
|
148
154
|
|
|
149
155
|
# CR 605.3: pay the ability's costs and add its mana immediately. Shared by priority and payment.
|
|
150
156
|
# `mana`: the mana chosen as it was activated (an option's "mana"). Restricted mana keeps its
|
|
151
157
|
# restriction in the pool (CR 106.6).
|
|
152
|
-
def activate_mana_ability!(index, id, ability_ref, mana = nil)
|
|
158
|
+
def activate_mana_ability!(index, id, ability_ref, mana = nil, helper = nil)
|
|
153
159
|
obj = object(id)
|
|
154
160
|
ability = chars(obj).ability(ability_ref) or raise ArgumentError, "#{id} has no ability #{ability_ref}"
|
|
161
|
+
costs = mana_ability_costs(ability_ref, ability)
|
|
162
|
+
check_mana_helper!(obj, costs, helper) # Engine::ManaHelpers
|
|
155
163
|
chosen = mana && Mana.parse_produce(mana, [], "mana")
|
|
156
164
|
from = mana_provenance(id) # before a cost sacrifices it (a Treasure)
|
|
157
165
|
record_activation!("source" => id, "ability" => ability_ref) if ability.per_turn
|
|
158
166
|
pay_from_pool!(index, ability.mana_cost, id) if ability.mana_cost
|
|
159
|
-
Mechanics::Costs.payment_order(
|
|
160
|
-
cost.
|
|
167
|
+
Mechanics::Costs.payment_order(costs).map(&:first).each do |cost| # CR 601.2h
|
|
168
|
+
if cost.is_a?(Mechanics::Costs::TapPermanents) then tap!(object(helper)) # the chosen helper (CR 602.2b)
|
|
169
|
+
elsif cost.respond_to?(:pay_at_once!) then cost.pay_at_once!(self, obj)
|
|
170
|
+
else cost.pay!(self, obj)
|
|
171
|
+
end
|
|
161
172
|
end
|
|
162
173
|
produced = chosen || ability.outcomes(self, obj).first # CR 605.3b: a fixed amount is counted as it resolves
|
|
163
|
-
add_mana!(index, produced, id, restriction: ability.spend_only&.data, from: from)
|
|
174
|
+
add_mana!(index, produced, id, restriction: ability.spend_only&.bound(self, id)&.data, from: from)
|
|
164
175
|
add_mana_ability_counters!(index, obj, ability)
|
|
165
176
|
end
|
|
166
177
|
|
|
@@ -15,6 +15,7 @@ module Enjine
|
|
|
15
15
|
# #game_log for the whole game):
|
|
16
16
|
# "spells" card ids of spells the player cast "life_gained" / "life_lost" totals
|
|
17
17
|
# "spells_from" the zone each of those spells was cast from (CR 601.2a), in the same order
|
|
18
|
+
# "spells_as_cast" [types, subtypes, keywords, colors] of each of those spells as cast, in the same order
|
|
18
19
|
# "lands_played" [card id, zone it was played from] of the lands the player played (CR 305.1)
|
|
19
20
|
# "entered" [card id, types, subtypes] of permanents that entered under their control
|
|
20
21
|
# "died" [card id, types] of their permanents put into a graveyard from the battlefield
|
|
@@ -198,9 +199,16 @@ module Enjine
|
|
|
198
199
|
index = names.index(event.player)
|
|
199
200
|
(turn_log(index)["spells"] ||= []) << event.card
|
|
200
201
|
(turn_log(index)["spells_from"] ||= []) << ((spell = state.objects[event.object]) ? cast_origin(spell) : "hand")
|
|
202
|
+
(turn_log(index)["spells_as_cast"] ||= []) << spell_as_cast(spell, event.card) # Abilities::CostModifier "first"
|
|
201
203
|
bump!(game_log(index), "spells", 1)
|
|
202
204
|
end
|
|
203
205
|
|
|
206
|
+
# [types, subtypes, keywords, colors] of a spell as it was cast (CR 601.2a: the face cast).
|
|
207
|
+
def spell_as_cast(spell, card)
|
|
208
|
+
current = spell ? chars(spell) : catalog.fetch(card).printed_characteristics
|
|
209
|
+
[current.types, current.subtypes, current.keywords, current.colors].map { |list| list.map(&:to_s) }
|
|
210
|
+
end
|
|
211
|
+
|
|
204
212
|
def life_recorded!(event)
|
|
205
213
|
log = log_of(event.player)
|
|
206
214
|
bump!(log, "life_gained", event.gain) if event.gain.positive? # CR 119.9
|
|
@@ -90,27 +90,33 @@ module Enjine
|
|
|
90
90
|
class CostModifier < Primitive
|
|
91
91
|
TYPES = %w[artifact creature enchantment land planeswalker instant sorcery].freeze
|
|
92
92
|
CASTERS = %w[you opponent any].freeze
|
|
93
|
-
SPEC_KEYS = %w[subtypes colors name].freeze
|
|
93
|
+
SPEC_KEYS = %w[subtypes colors name keywords not_subtypes].freeze
|
|
94
|
+
# "The first ... spell you cast each turn / during each of your turns" (CR 601.2f): only a spell no earlier
|
|
95
|
+
# matching spell of its caster this turn came before.
|
|
96
|
+
FIRSTS = %w[each_turn each_of_your_turns].freeze
|
|
94
97
|
|
|
95
98
|
primitive :ability, :cost_modifier,
|
|
96
99
|
description: "While on the battlefield: spells cast by `caster` (you, opponent or any; default you) " \
|
|
97
100
|
"whose card types match `spells` ({\"any_of\": [types], \"not\": [types]}, default all " \
|
|
98
101
|
"spells; `subtypes`, `colors` and `name` narrow it: \"Dragon spells\", \"red spells\"; " \
|
|
99
102
|
"`\"subtypes\": \"chosen\"`: of the type chosen for this permanent, `\"name\": \"chosen\"`: with the " \
|
|
100
|
-
"name its source chose
|
|
103
|
+
"name its source chose; `keywords` / `not_subtypes`: \"non-Lemur creature spells with " \
|
|
104
|
+
"flying\") cost " \
|
|
101
105
|
"`reduce` less or `increase` more generic mana. `from` / `not_from`: only " \
|
|
102
|
-
"spells cast from (not from) that zone (hand | graveyard | exile | library | outside)."
|
|
103
|
-
|
|
106
|
+
"spells cast from (not from) that zone (hand | graveyard | exile | library | outside). " \
|
|
107
|
+
"`first`: only the first matching spell its caster casts `each_turn` or " \
|
|
108
|
+
"`each_of_your_turns` (spells cast before this permanent was there count too).",
|
|
109
|
+
rules: %w[601.2f 118.7a 604.1 601.2a 601.2i],
|
|
104
110
|
params: { "spells" => "{\"any_of\": [types], \"not\": [types], \"subtypes\": [subtypes], \"colors\": [colors]} " \
|
|
105
111
|
"(optional)",
|
|
106
112
|
"caster" => "you | opponent | any", "reduce" => "amount", "increase" => "amount",
|
|
107
|
-
"from" => "zone", "not_from" => "zone" },
|
|
113
|
+
"from" => "zone", "not_from" => "zone", "first" => FIRSTS.join(" | ") },
|
|
108
114
|
example: { "cost_modifier" => { "spells" => { "not" => ["creature"] }, "reduce" => 1 } }
|
|
109
115
|
|
|
110
116
|
attr_reader :caster, :any_of, :not_types, :amount, :sign
|
|
111
117
|
|
|
112
118
|
def self.build(params, errors, path, **)
|
|
113
|
-
Mechanics.check_keys(params, %w[spells caster reduce increase] + CastFrom::KEYS, errors, path)
|
|
119
|
+
Mechanics.check_keys(params, %w[spells caster reduce increase first] + CastFrom::KEYS, errors, path)
|
|
114
120
|
spells = Mechanics.fetch_param(params, :spells) || {}
|
|
115
121
|
unless spells.is_a?(Hash)
|
|
116
122
|
errors << "#{path}.spells: must be an object"
|
|
@@ -124,8 +130,10 @@ module Enjine
|
|
|
124
130
|
end
|
|
125
131
|
caster = Mechanics.fetch_param(params, :caster) || "you"
|
|
126
132
|
errors << "#{path}.caster: must be one of #{CASTERS.join(", ")}" unless CASTERS.include?(caster)
|
|
133
|
+
first = Mechanics.fetch_param(params, :first)
|
|
134
|
+
errors << "#{path}.first: must be one of #{FIRSTS.join(", ")}" unless first.nil? || FIRSTS.include?(first)
|
|
127
135
|
new(caster, lists["any_of"], lists["not"], *build_amount(params, errors, path),
|
|
128
|
-
zones: CastFrom.build(params, errors, path), spec: build_spec(spells, errors, path))
|
|
136
|
+
zones: CastFrom.build(params, errors, path), spec: build_spec(spells, errors, path), first: first)
|
|
129
137
|
end
|
|
130
138
|
|
|
131
139
|
# "Dragon spells", "red spells": the subtypes and colors in `spells`, matched as a card spec.
|
|
@@ -143,8 +151,9 @@ module Enjine
|
|
|
143
151
|
[Amount.build(params[keys.first], errors, "#{path}.#{keys.first}", targets_count: 0), keys.first == "reduce" ? -1 : 1]
|
|
144
152
|
end
|
|
145
153
|
|
|
146
|
-
def initialize(caster, any_of, not_types, amount, sign, zones: CastFrom.new(nil, nil), spec: nil)
|
|
154
|
+
def initialize(caster, any_of, not_types, amount, sign, zones: CastFrom.new(nil, nil), spec: nil, first: nil)
|
|
147
155
|
super()
|
|
156
|
+
@first = first
|
|
148
157
|
@spec = spec
|
|
149
158
|
@caster = caster
|
|
150
159
|
@any_of = any_of.freeze
|
|
@@ -164,24 +173,60 @@ module Enjine
|
|
|
164
173
|
def change_for(engine, permanent, spell, caster_index)
|
|
165
174
|
return 0 unless caster == "any" || (caster == "you") == (caster_index == permanent.controller)
|
|
166
175
|
|
|
167
|
-
chars = engine.chars(spell)
|
|
168
|
-
types = chars.types
|
|
169
|
-
return 0 if types.intersect?(not_types) || !(any_of.empty? || types.intersect?(any_of))
|
|
170
|
-
|
|
171
176
|
ctx = EffectContext.new(permanent.controller, permanent.id, permanent.id, [])
|
|
172
|
-
return 0
|
|
173
|
-
return 0
|
|
177
|
+
return 0 unless spell_matches?(engine, engine.chars(spell), engine.cast_origin(spell), ctx)
|
|
178
|
+
return 0 if @first && !first_spell?(engine, caster_index, ctx)
|
|
174
179
|
|
|
175
180
|
sign * amount.value(engine, ctx)
|
|
176
181
|
end
|
|
177
182
|
|
|
183
|
+
# Whether a spell with these characteristics, cast from `zone`, is one it changes.
|
|
184
|
+
def spell_matches?(engine, chars, zone, ctx)
|
|
185
|
+
types = chars.types
|
|
186
|
+
return false if types.intersect?(not_types) || !(any_of.empty? || types.intersect?(any_of))
|
|
187
|
+
return false if @spec && !@spec.chars_match?(engine, chars, ctx) # the source's choices: type, name (CR 201.4, 607.2d)
|
|
188
|
+
|
|
189
|
+
@zones.match?(zone)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# "The first ... spell you cast each turn": no spell its caster cast this turn matched, as it was cast
|
|
193
|
+
# (Engine::TurnHistory "spells_as_cast"; a cancelled cast never counts, CR 601.2i); "during each of your
|
|
194
|
+
# turns": and it's the caster's turn.
|
|
195
|
+
def first_spell?(engine, caster_index, ctx)
|
|
196
|
+
return false if @first == "each_of_your_turns" && engine.state.active != caster_index
|
|
197
|
+
|
|
198
|
+
log = engine.turn_log(caster_index)
|
|
199
|
+
facts = Array(log["spells_as_cast"])
|
|
200
|
+
zones = Array(log["spells_from"])
|
|
201
|
+
Array(log["spells"]).each_with_index.none? do |card, i|
|
|
202
|
+
spell_matches?(engine, CostModifier.as_cast(engine, card, facts[i]), zones[i] || "hand", ctx)
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# A spell's characteristics as it was cast (types, subtypes, keywords and colors of the face cast, CR 601.2a);
|
|
207
|
+
# its printed ones if they weren't recorded.
|
|
208
|
+
def self.as_cast(engine, card, facts)
|
|
209
|
+
chars = engine.catalog.fetch(card).printed_characteristics
|
|
210
|
+
return chars unless facts
|
|
211
|
+
|
|
212
|
+
types, subtypes, keywords, colors = facts.map { |list| list.map(&:to_sym) }
|
|
213
|
+
chars.with(types: types, subtypes: subtypes, keywords: keywords, colors: colors)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
FIRST_TEXT = { "each_turn" => " each turn", "each_of_your_turns" => " during each of your turns" }.freeze
|
|
217
|
+
|
|
178
218
|
def to_s
|
|
179
|
-
narrow =
|
|
219
|
+
narrow = []
|
|
220
|
+
if @spec
|
|
221
|
+
narrow = [@spec.colors.map(&:to_s), @spec.not_subtypes.map { "non-#{it.to_s.capitalize}" },
|
|
222
|
+
@spec.subtypes.map { it.to_s.capitalize }]
|
|
223
|
+
end
|
|
180
224
|
narrow << "chosen-type" if @spec&.relations&.[]("chosen_subtype")
|
|
181
225
|
kinds = [not_types.map { "non#{it}" }, narrow, any_of.join(" or ")].flatten.reject(&:empty?).join(" ")
|
|
182
226
|
whose = { "you" => "you cast", "opponent" => "your opponents cast", "any" => "" }[caster]
|
|
183
|
-
"#{
|
|
184
|
-
|
|
227
|
+
with = " with #{@spec.keywords.join(" or ")}" if @spec && !@spec.keywords.empty?
|
|
228
|
+
"#{"the first " if @first}#{[kinds, "spells#{with}", whose].reject(&:empty?).join(" ")}#{@zones}" \
|
|
229
|
+
"#{FIRST_TEXT[@first]} cost {#{amount}} #{sign.negative? ? "less" : "more"}"
|
|
185
230
|
end
|
|
186
231
|
end
|
|
187
232
|
end
|
|
@@ -11,8 +11,12 @@ module Enjine
|
|
|
11
11
|
# paid for a cost while a spell or ability resolves (CR 118.12) is neither, so restricted mana can't pay it.
|
|
12
12
|
# `from` / `not_from` a zone limit the spells to those cast from (not from) it ("spend this mana only to cast
|
|
13
13
|
# a spell from anywhere other than your hand": {"not_from": "hand"}; CR 601.2a); alone, they mean spells.
|
|
14
|
+
# A spec of `"subtypes": "chosen"` reads the creature type chosen for the mana's source (CR 607.2d) as the mana is
|
|
15
|
+
# made (#bound), so the mana keeps that restriction once in the pool, wherever the source goes (CR 106.6).
|
|
16
|
+
# `"cant_be_countered": true`: a spell cast with this mana can't be countered (Cavern of Souls, CR 106.6, 101.2).
|
|
14
17
|
class SpendOnly
|
|
15
|
-
KEYS = (%w[spells abilities] + CastFrom::KEYS).freeze
|
|
18
|
+
KEYS = (%w[spells abilities cant_be_countered] + CastFrom::KEYS).freeze
|
|
19
|
+
SPECS = %w[spells abilities].freeze
|
|
16
20
|
|
|
17
21
|
attr_reader :data
|
|
18
22
|
|
|
@@ -24,11 +28,19 @@ module Enjine
|
|
|
24
28
|
end
|
|
25
29
|
data = data.transform_keys(&:to_s)
|
|
26
30
|
zones = CastFrom.build(data, errors, path)
|
|
27
|
-
specs = data.slice(
|
|
31
|
+
specs = data.slice(*SPECS).to_h { |key, spec| [key, CardSpec.build(spec, errors, "#{path}.#{key}")] }
|
|
28
32
|
specs["spells"] ||= CardSpec.build("card", errors, path) if data.keys.intersect?(CastFrom::KEYS)
|
|
33
|
+
check_rider(data, specs, errors, path)
|
|
29
34
|
new(data, specs, zones)
|
|
30
35
|
end
|
|
31
36
|
|
|
37
|
+
def self.check_rider(data, specs, errors, path)
|
|
38
|
+
return unless data.key?("cant_be_countered")
|
|
39
|
+
|
|
40
|
+
errors << "#{path}.cant_be_countered: must be true" unless data["cant_be_countered"] == true
|
|
41
|
+
errors << "#{path}.cant_be_countered: needs \"spells\" (it applies to a spell cast with the mana)" unless specs["spells"]
|
|
42
|
+
end
|
|
43
|
+
|
|
32
44
|
# The restriction kept on pool mana (plain data, CR 106.6), rebuilt once per distinct data.
|
|
33
45
|
def self.from_data(data)
|
|
34
46
|
(@cache ||= {})[data] ||= build(data, [], "spend_only")
|
|
@@ -38,9 +50,30 @@ module Enjine
|
|
|
38
50
|
@data = Snapshot.deep_copy(data).freeze
|
|
39
51
|
@specs = specs.freeze
|
|
40
52
|
@zones = zones
|
|
53
|
+
@chosen = specs.values.any? { it.relations["chosen_subtype"] }
|
|
41
54
|
freeze
|
|
42
55
|
end
|
|
43
56
|
|
|
57
|
+
# "A spell cast with this mana can't be countered" (CR 106.6, 101.2): Engine::Payment#pay_mana! marks the spell.
|
|
58
|
+
def cant_be_countered? = @data["cant_be_countered"] == true
|
|
59
|
+
|
|
60
|
+
# Whether a spent pool entry (restricted mana, or nil) carries that rider.
|
|
61
|
+
def self.uncounterable?(entry) = !entry&.[]("spend_only").nil? && from_data(entry["spend_only"]).cant_be_countered?
|
|
62
|
+
|
|
63
|
+
# The restriction of mana made by `source`: "of the chosen type" becomes the creature type chosen for it now
|
|
64
|
+
# (CR 607.2d), so mana in the pool keeps it (CR 106.6). With no type chosen, "chosen" stays and matches nothing
|
|
65
|
+
# (CardSpec reads no choice without a context).
|
|
66
|
+
def bound(engine, source)
|
|
67
|
+
return self unless @chosen
|
|
68
|
+
|
|
69
|
+
type = engine.state.objects[source]&.memory&.[]("chosen_type") or return self
|
|
70
|
+
SpendOnly.from_data(@data.to_h do |key, spec|
|
|
71
|
+
next [key, spec] unless SPECS.include?(key) && spec.is_a?(Hash) && spec["subtypes"] == "chosen"
|
|
72
|
+
|
|
73
|
+
[key, spec.merge("subtypes" => [type])]
|
|
74
|
+
end)
|
|
75
|
+
end
|
|
76
|
+
|
|
44
77
|
# `purpose`: {"spell" => id} for a spell being cast, {"ability" => source id} for an ability being
|
|
45
78
|
# activated, nil for anything else.
|
|
46
79
|
def allows?(engine, purpose)
|
|
@@ -57,7 +90,7 @@ module Enjine
|
|
|
57
90
|
parts = []
|
|
58
91
|
parts << "cast #{"#{@specs["spells"]} " unless @specs["spells"].any?}spells#{@zones}" if @specs["spells"]
|
|
59
92
|
parts << "activate abilities of #{@specs["abilities"]}s" if @specs["abilities"]
|
|
60
|
-
"spend this mana only to #{parts.join(" or ")}"
|
|
93
|
+
"spend this mana only to #{parts.join(" or ")}#{", and that spell can't be countered" if cant_be_countered?}"
|
|
61
94
|
end
|
|
62
95
|
end
|
|
63
96
|
|
|
@@ -76,15 +109,20 @@ module Enjine
|
|
|
76
109
|
"`amount` mana (default 1) of one color you choose as you activate it " \
|
|
77
110
|
"(`\"any_one\"`) or in any combination of colors (`\"any\"`); `colors` limits " \
|
|
78
111
|
"the choice. `\"chosen\"`: of the color chosen as this entered (choose_color). " \
|
|
79
|
-
"`spend_only` restricts how the mana can be spent
|
|
112
|
+
"`spend_only` restricts how the mana can be spent (a spec of `\"subtypes\": \"chosen\"`: the creature " \
|
|
113
|
+
"type chosen as this entered; `\"cant_be_countered\": true`: a spell cast with it can't be " \
|
|
114
|
+
"countered). `only_if`: \"Activate only if ...\". " \
|
|
80
115
|
"`limit: \"once_each_turn\"`: \"Activate only once each turn.\" `add_counters`: it also " \
|
|
81
|
-
"puts counters on its own source (\"Add {C}. Put a point counter on this\")."
|
|
82
|
-
|
|
116
|
+
"puts counters on its own source (\"Add {C}. Put a point counter on this\"). A " \
|
|
117
|
+
"`tap_permanents` cost (count 1) taps another permanent you choose as you activate it " \
|
|
118
|
+
"(\"{T}, Tap an untapped creature you control\"): each activation option names it (`tap`).",
|
|
119
|
+
rules: %w[605.1a 605.3a 605.3b 106.1a 106.4 106.6 607.2d 602.5b 122.1 101.2 602.2b 302.6],
|
|
83
120
|
params: { "cost" => "list of costs (e.g. [\"tap\"])",
|
|
84
121
|
"produce" => "mana symbols added, e.g. \"{G}\" or \"{C}{C}\"",
|
|
85
122
|
"amount" => "amount (default 1; with produce: how many times)",
|
|
86
123
|
"color" => CHOICES.join(" | "), "colors" => "list of colors to choose from (default all five)",
|
|
87
|
-
"spend_only" => "{\"spells\": card spec, \"abilities\": card spec, \"from\" | \"not_from\": zone
|
|
124
|
+
"spend_only" => "{\"spells\": card spec, \"abilities\": card spec, \"from\" | \"not_from\": zone, " \
|
|
125
|
+
"\"cant_be_countered\": true}",
|
|
88
126
|
"only_if" => "condition: it can be activated only while it holds (CR 602.5b)",
|
|
89
127
|
"limit" => "\"once_each_turn\" (CR 602.5b)",
|
|
90
128
|
"add_counters" => "{\"counter\": name, \"amount\": integer (default 1)}: put on this permanent" },
|
|
@@ -141,6 +179,9 @@ module Enjine
|
|
|
141
179
|
if cost.is_a?(Costs::OneOf)
|
|
142
180
|
errors << "#{path}.cost[#{i}]: a mana ability's cost can't be a one_of (a choice between costs, CR 601.2b, " \
|
|
143
181
|
"isn't supported for mana abilities, which are paid for at once, CR 605.3a)"
|
|
182
|
+
elsif cost.is_a?(Costs::TapPermanents)
|
|
183
|
+
# "Tap an untapped creature you control": the permanent is named by the activation (Engine::ManaHelpers)
|
|
184
|
+
errors << "#{path}.cost[#{i}]: a mana ability can tap only one other permanent (count 1)" unless cost.count == 1
|
|
144
185
|
elsif choosing_cost?(cost)
|
|
145
186
|
errors << "#{path}.cost[#{i}]: a mana ability's cost can't need a choice, X or {2/B}"
|
|
146
187
|
end
|
|
@@ -203,16 +244,20 @@ module Enjine
|
|
|
203
244
|
def activatable?(engine, object, bound = costs)
|
|
204
245
|
return false if @only_if && !@only_if.true?(engine, EffectContext.new(object.controller, object.id, nil, nil))
|
|
205
246
|
|
|
206
|
-
bound.all? { |c| c.payable?(engine, object) } &&
|
|
247
|
+
bound.all? { |c| c.is_a?(Costs::TapPermanents) || c.payable?(engine, object) } && engine.mana_helper?(object, bound) &&
|
|
248
|
+
(mana_cost.nil? || engine.pool_covers?(object.controller, mana_cost))
|
|
207
249
|
end
|
|
208
250
|
|
|
209
251
|
# [counter, amount] it puts on its source as it resolves, or nil.
|
|
210
252
|
attr_reader :add_counters
|
|
211
253
|
|
|
254
|
+
# Tapping its source, the granter or another permanent (Engine::ManaHelpers) can be rewound (CR 734.1).
|
|
255
|
+
def self.rewindable?(cost) = [Costs::Tap, Costs::TapGranter, Costs::TapPermanents].any? { cost.is_a?(it) }
|
|
256
|
+
|
|
212
257
|
# One whose costs include more than {T} and mana (a sacrifice, life, a discard, removing counters) or that puts
|
|
213
258
|
# counters on its source can't be undone (CR 734.1 rewinds only tapping and mana).
|
|
214
259
|
def irreversible?
|
|
215
|
-
!add_counters.nil? || costs.any? { !
|
|
260
|
+
!add_counters.nil? || costs.any? { !ManaAbility.rewindable?(it) && !it.mana? }
|
|
216
261
|
end
|
|
217
262
|
|
|
218
263
|
# How many times it could be activated in a row now, for affordability: once, unless every cost is removing
|
|
@@ -23,6 +23,8 @@ module Enjine
|
|
|
23
23
|
# {"subtypes": "chosen"} of the creature type chosen for the source (CR 607.2d)
|
|
24
24
|
# {"name": "chosen"} with the card name chosen for the source or the resolving
|
|
25
25
|
# spell (choose_card_name, CR 201.4, 607.2d)
|
|
26
|
+
# {"same_name_as": {"result": n}} the same name as a card effect n of the list moved ("cards
|
|
27
|
+
# with that name", CR 201.2)
|
|
26
28
|
# {"same_name_as": filter} the same name as a permanent the filter matches, e.g. "a
|
|
27
29
|
# legendary permanent you control" (CR 201.2a)
|
|
28
30
|
# The chosen ones and same_name_as read the context (an EffectContext): the source's choices and its controller.
|
|
@@ -67,9 +69,19 @@ module Enjine
|
|
|
67
69
|
# What the spec compares with its context: the chosen creature type, permanents to share a name with.
|
|
68
70
|
def self.relations_param(data, chosen, errors, path)
|
|
69
71
|
raw = data["same_name_as"]
|
|
70
|
-
|
|
72
|
+
result = raw.is_a?(Hash) && raw.key?("result")
|
|
73
|
+
same = raw && !result && Mechanics.build_entry(:filter, raw, errors, "#{path}.same_name_as")
|
|
71
74
|
{ "chosen_subtype" => (true if chosen), "chosen_name" => (true if data["name"] == "chosen"),
|
|
72
|
-
"same_name_as" => same }.compact
|
|
75
|
+
"same_name_as" => same || nil, "same_name_as_result" => (result_index(raw, errors, path) if result) }.compact
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# {"same_name_as": {"result": n}}: the name of a card an earlier effect of the list moved (CR 201.2, 400.7j).
|
|
79
|
+
# Whether n names an earlier effect is known only where the spec is used; a later one matches nothing.
|
|
80
|
+
def self.result_index(raw, errors, path)
|
|
81
|
+
Mechanics.check_keys(raw, %w[result], errors, "#{path}.same_name_as")
|
|
82
|
+
index = raw["result"]
|
|
83
|
+
errors << "#{path}.same_name_as.result: must be an effect index (0 or more)" unless index.is_a?(Integer) && index >= 0
|
|
84
|
+
index.to_i
|
|
73
85
|
end
|
|
74
86
|
|
|
75
87
|
def self.colors_param(data, errors, path)
|
|
@@ -206,6 +218,7 @@ module Enjine
|
|
|
206
218
|
|
|
207
219
|
text = (type_words.map { it.to_s.tr("_", " ") } + ["card"]).join(" ") + quality_words
|
|
208
220
|
text += " with the same name as a #{relations["same_name_as"]}" if relations["same_name_as"]
|
|
221
|
+
text += " with the same name as the card from effect ##{relations["same_name_as_result"] + 1}" if relations["same_name_as_result"]
|
|
209
222
|
text += " with the chosen name" if relations["chosen_name"]
|
|
210
223
|
bounds = { "mana value" => mana_value, "number of colors" => color_count, "power" => stats["power"] || {},
|
|
211
224
|
"toughness" => stats["toughness"] || {} }.reject { |_, b| b.empty? }
|
|
@@ -245,12 +258,25 @@ module Enjine
|
|
|
245
258
|
named = Filters.chosen_memory(engine, ctx.source, ctx)["chosen_name"]
|
|
246
259
|
return false unless named && chars.name == named
|
|
247
260
|
end
|
|
261
|
+
return result_names(engine, ctx).include?(chars.name) if relations["same_name_as_result"]
|
|
262
|
+
|
|
248
263
|
filter = relations["same_name_as"] or return true
|
|
249
264
|
!chars.name.to_s.empty? && engine.state.battlefield.any? do
|
|
250
265
|
filter.match_for?(engine, it, ctx.controller, ctx.source) && engine.chars(it).name == chars.name
|
|
251
266
|
end
|
|
252
267
|
end
|
|
253
268
|
|
|
269
|
+
# The names of the cards effect n moved, as they are now, or as the card they were once they've moved again
|
|
270
|
+
# (CR 400.7); an object with no name shares none (CR 201.2a).
|
|
271
|
+
def result_names(engine, ctx)
|
|
272
|
+
Array(ctx.results&.[](relations["same_name_as_result"])).filter_map do |id|
|
|
273
|
+
obj = engine.state.objects[id]
|
|
274
|
+
card = obj ? nil : ctx.result_cards&.[](id)
|
|
275
|
+
name = obj ? engine.chars(obj).name : card && engine.catalog.fetch(card).printed_characteristics.name
|
|
276
|
+
name unless name.to_s.empty?
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
|
|
254
280
|
# CR 202.3: a card's mana value (0 without a mana cost, CR 202.3a), or its number of colors (CR 105.2), within
|
|
255
281
|
# the bounds.
|
|
256
282
|
def bounded?(bounds, value, engine, ctx)
|
|
@@ -11,7 +11,7 @@ module Enjine
|
|
|
11
11
|
include NestedList
|
|
12
12
|
|
|
13
13
|
PUT = %w[hand graveyard exile battlefield library_top library_bottom discard].freeze
|
|
14
|
-
ZONES = %w[hand graveyard].freeze
|
|
14
|
+
ZONES = %w[hand graveyard library].freeze
|
|
15
15
|
REVEAL = %w[chosen all].freeze
|
|
16
16
|
LISTS = %w[then otherwise].freeze
|
|
17
17
|
|
|
@@ -19,7 +19,9 @@ module Enjine
|
|
|
19
19
|
description: "You choose `count` (default 1; \"any\": any number; \"all\": every matching card, no " \
|
|
20
20
|
"choice) cards matching `for` from `from`: a " \
|
|
21
21
|
"selector (e.g. {\"result\": 0}, the cards an earlier effect milled) or a player's zone, " \
|
|
22
|
-
"{\"hand\": selector} / {\"graveyard\": selector}
|
|
22
|
+
"{\"hand\": selector} / {\"graveyard\": selector} / {\"library\": selector}, or several of them " \
|
|
23
|
+
"(\"search its owner's graveyard, hand, and library\": one choice among them all). `optional`: you " \
|
|
24
|
+
"may choose fewer. Then " \
|
|
23
25
|
"they are put into `put` (`discard`: from a hand, its owner discards them; `tapped` onto the " \
|
|
24
26
|
"battlefield, under your control; exiled cards `link`ed to the source). `look`: you look at the " \
|
|
25
27
|
"whole zone first; `reveal`: \"chosen\" reveals what you chose, \"all\" the whole set. " \
|
|
@@ -28,9 +30,10 @@ module Enjine
|
|
|
28
30
|
"`chooser`: the player who chooses (a player selector, default \"you\"; e.g. " \
|
|
29
31
|
"\"each_opponent\" or {\"target\": 0}: \"target opponent chooses a card from among " \
|
|
30
32
|
"them\"); they look at the cards they choose from, and the cards still go where `put` " \
|
|
31
|
-
"says under your control."
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
"says under your control. Its amount ({\"this_way\": n}) is how many of the cards came from a " \
|
|
34
|
+
"hand (\"draws a card for each card exiled from their hand this way\").",
|
|
35
|
+
rules: %w[701.20a 701.20e 400.7j 608.2c 607.2a 118.12 701.9a 701.9b 608.2d 701.23a 701.23b],
|
|
36
|
+
params: { "from" => "selector or {\"hand\"|\"graveyard\"|\"library\": player selector, ...}", "for" => "card spec",
|
|
34
37
|
"count" => "integer, \"any\" or \"all\"", "optional" => "boolean", "put" => PUT.join(" | "),
|
|
35
38
|
"tapped" => "boolean", "link" => "boolean", "look" => "boolean", "random" => "boolean",
|
|
36
39
|
"reveal" => REVEAL.join(" | "), "then" => "list of effects", "otherwise" => "list of effects",
|
|
@@ -44,7 +47,8 @@ module Enjine
|
|
|
44
47
|
def self.default_to = "you"
|
|
45
48
|
|
|
46
49
|
def setup(params, errors, path)
|
|
47
|
-
@
|
|
50
|
+
@from_zones = parse_from(Mechanics.fetch_param(params, :from), errors, path)
|
|
51
|
+
@from_zone = @from_zones.keys.first if @from_zones.size == 1
|
|
48
52
|
@spec = CardSpec.build(Mechanics.fetch_param(params, :for), errors, "#{path}.for")
|
|
49
53
|
@count = Mechanics.fetch_param(params, :count) || 1
|
|
50
54
|
unless %w[any all].include?(@count) || (@count.is_a?(Integer) && @count.positive?)
|
|
@@ -65,23 +69,30 @@ module Enjine
|
|
|
65
69
|
results_count: @results_count)
|
|
66
70
|
end
|
|
67
71
|
|
|
72
|
+
# {zone => player selector} for one or more players' zones (several are searched as one set, CR 701.23a), or
|
|
73
|
+
# {nil => selector} for objects.
|
|
68
74
|
def parse_from(raw, errors, path)
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
75
|
+
return { nil => nil } if raw.nil? && (errors << "#{path}.from: required")
|
|
76
|
+
return { nil => build_from(raw, errors, "#{path}.from") } unless raw.is_a?(Hash) && !raw.empty? &&
|
|
77
|
+
raw.keys.all? { ZONES.include?(it.to_s) }
|
|
72
78
|
|
|
73
|
-
[zone,
|
|
74
|
-
|
|
79
|
+
raw.to_h { |zone, inner| [zone.to_s, build_from(inner, errors, "#{path}.from.#{zone}")] }
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def build_from(raw, errors, path)
|
|
83
|
+
Selector.build(raw, errors, path, targets_count: @targets_count, results_count: @results_count)
|
|
75
84
|
end
|
|
76
85
|
|
|
77
86
|
def nested_effects = @lists.values.flatten
|
|
78
87
|
|
|
79
88
|
def to_s
|
|
80
89
|
many = { "any" => "any number of", "all" => "all" }.fetch(count) { "#{"up to " if @optional}#{count}" }
|
|
81
|
-
"choose #{many} #{spec}#{" at random" if @flags["random"]} from #{
|
|
90
|
+
"choose #{many} #{spec}#{" at random" if @flags["random"]} from #{from_text} " \
|
|
82
91
|
"→ #{put.tr("_", " ")}#{" (#{@chooser} chooses)" if @chooser}"
|
|
83
92
|
end
|
|
84
93
|
|
|
94
|
+
def from_text = @from_zones.map { |zone, from| "#{"the #{zone} of " if zone}#{from}" }.join(", ")
|
|
95
|
+
|
|
85
96
|
# CR 118.3: as the optional part of a "may", the full number of cards must be available.
|
|
86
97
|
def possible?(engine, ctx) = pool(engine, ctx).size >= (count.is_a?(Integer) ? count : 1)
|
|
87
98
|
|
|
@@ -128,10 +139,12 @@ module Enjine
|
|
|
128
139
|
end
|
|
129
140
|
|
|
130
141
|
def set(engine, ctx)
|
|
131
|
-
|
|
132
|
-
|
|
142
|
+
@from_zones.flat_map do |zone, from|
|
|
143
|
+
refs = Array(from&.resolve(engine, ctx))
|
|
144
|
+
next refs.select { engine.state.objects.key?(it) } unless zone
|
|
133
145
|
|
|
134
|
-
|
|
146
|
+
refs.filter_map { Filters.player_index(engine, it) }.flat_map { engine.player(it).public_send(zone) }
|
|
147
|
+
end.uniq
|
|
135
148
|
end
|
|
136
149
|
|
|
137
150
|
# Looking at or revealing the whole set happens once, before the choice (CR 701.20a, 701.20e).
|
|
@@ -172,6 +185,8 @@ module Enjine
|
|
|
172
185
|
|
|
173
186
|
def perform!(engine, ctx, chosen, locals)
|
|
174
187
|
engine.reveal!(ctx.controller, chosen) if @reveal == "chosen"
|
|
188
|
+
from_hand = chosen.count { engine.state.objects[it]&.zone == :hand }
|
|
189
|
+
engine.record_amount!(from_hand) if from_hand.positive? # "for each card exiled from their hand this way"
|
|
175
190
|
return enter!(engine, ctx, chosen, locals) if put == "battlefield"
|
|
176
191
|
|
|
177
192
|
chosen.filter_map { move(engine, ctx, it) }
|