1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use std::collections::HashMap;
use crate::game::GameState;
use crate::keyword::keyword_instance::Keyword;
use crate::spellability::alternative_cost::AlternativeCost;
use crate::spellability::SpellAbility;
use crate::trigger::trigger::Trigger;
/// Minimal wrapped ability shim for trigger parity.
/// Full Java parity (revalidation at resolve-time) will be implemented here.
#[derive(Debug, Clone)]
pub struct WrappedAbility {
pub wrapped: SpellAbility,
/// The trigger that created this wrapped ability.
/// Used by `get_stack_description` and similar methods.
pub trigger: Option<Trigger>,
additional_ability_lists: HashMap<String, Vec<String>>,
}
impl WrappedAbility {
pub fn new(wrapped: SpellAbility) -> Self {
Self {
wrapped,
trigger: None,
additional_ability_lists: HashMap::new(),
}
}
pub fn with_trigger(wrapped: SpellAbility, trigger: Trigger) -> Self {
Self {
wrapped,
trigger: Some(trigger),
additional_ability_lists: HashMap::new(),
}
}
pub fn has_param(&self, key: &str) -> bool {
self.get_param(key).is_some() || self.wrapped.has_additional_ability(key)
}
pub fn add_cost_to_hash_list(&mut self, cost_key: &str, value: String) {
self.wrapped
.paid_hash
.entry(cost_key.to_string())
.or_default()
.push(value);
}
pub fn reset_paid_hash(&mut self) {
self.wrapped.paid_hash.clear();
}
pub fn has_triggering_object(&self, key: &str) -> bool {
self.wrapped.has_triggering_object(key)
}
pub fn reset_triggering_objects(&mut self) {
self.wrapped.trigger_objects.clear();
}
pub fn can_play(&self) -> bool {
true
}
pub fn copy(&self) -> Self {
self.clone()
}
pub fn yield_key(&self) -> String {
if !self.wrapped.stack_description.is_empty() {
self.wrapped.stack_description.clone()
} else if !self.wrapped.description.is_empty() {
self.wrapped.description.clone()
} else {
self.wrapped.ability_text.clone()
}
}
pub fn to_unsuppressed_string(&self) -> String {
self.yield_key()
}
pub fn has_s_var(&self, game: &GameState, key: &str) -> bool {
self.wrapped
.source
.map(|cid| game.card(cid).svars.contains_key(key))
.unwrap_or(false)
}
pub fn reset_once_resolved(&mut self) {
// Placeholder for Java parity; Rust currently tracks resolve state elsewhere.
}
pub fn uses_targeting(&self) -> bool {
self.wrapped.uses_targeting()
}
pub fn has_additional_ability(&self, key: &str) -> bool {
self.wrapped.has_additional_ability(key)
|| self.additional_ability_lists.contains_key(key)
|| self.get_param(key).is_some()
}
pub fn reset_targets(&mut self) {
self.wrapped.clear_targets();
}
pub fn resolve(&self) -> bool {
true
}
// ── Delegating methods (Java WrappedAbility parity) ──────────────────
/// Mirrors Java's `WrappedAbility.getParam(String)`.
/// Delegates to `sa.getParam(key)`.
pub fn get_param(&self, key: &str) -> Option<&str> {
if self.wrapped.param_is_true(key) {
Some("True")
} else {
self.wrapped.param_value(key)
}
}
/// Mirrors Java's `WrappedAbility.getParamOrDefault(String, String)`.
/// Delegates to `sa.getParamOrDefault(key, defaultValue)`.
pub fn get_param_or_default<'a>(&'a self, key: &str, default: &'a str) -> &'a str {
self.get_param(key).unwrap_or(default)
}
/// Mirrors Java's `WrappedAbility.setPaidHash(...)`.
/// Replaces the paid hash wholesale.
pub fn set_paid_hash(&mut self, hash: HashMap<String, Vec<String>>) {
self.wrapped.paid_hash = hash;
}
/// Mirrors Java's `WrappedAbility.getPaidList(String, boolean)`.
/// Returns the list of paid cost values for the given key.
/// The `_intrinsic` flag is unused in Rust (Java uses it to pick column
/// from a `TreeBasedTable`; Rust flattens into a single `Vec`).
pub fn get_paid_list(&self, key: &str, _intrinsic: bool) -> Vec<String> {
self.wrapped.paid_hash.get(key).cloned().unwrap_or_default()
}
/// Mirrors Java's `WrappedAbility.setTriggeringObjects(Map)`.
/// Replaces all triggering objects wholesale.
pub fn set_triggering_objects(&mut self, objects: HashMap<String, String>) {
self.wrapped.trigger_objects.clear();
for (key, value) in objects {
self.wrapped.set_triggering_object(&key, value);
}
}
/// Mirrors Java's `WrappedAbility.setTriggeringObject(AbilityKey, Object)`.
/// Sets a single triggering object by key.
pub fn set_triggering_object(&mut self, key: &str, value: String) {
self.wrapped.set_triggering_object(key, value);
}
/// Mirrors Java's `WrappedAbility.getTriggeringObject(AbilityKey)`.
/// Delegates to `sa.getTriggeringObject(key)`.
pub fn get_triggering_object(&self, key: &str) -> Option<&str> {
self.wrapped.get_triggering_object(key)
}
/// Mirrors Java's `WrappedAbility.getStackDescription(boolean)`.
///
/// Simplified version: returns the trigger description (with ABILITY
/// replacement) plus important stack objects, if a trigger is available.
/// Falls back to the inner SpellAbility's stack_description.
pub fn get_stack_description(&self, game: &GameState) -> String {
if let Some(ref trigger) = self.trigger {
let source = self.wrapped.source.unwrap_or(crate::ids::CardId(0));
let player = self.wrapped.activating_player;
let base = trigger.replace_ability_text(&trigger.description, game, source, player);
let important = trigger
.mode
.get_important_stack_objects(trigger, &self.wrapped);
let mut sb = base;
if !important.is_empty() {
sb.push_str(" [");
sb.push_str(&important);
sb.push(']');
}
sb
} else if !self.wrapped.stack_description.is_empty() {
self.wrapped.stack_description.clone()
} else {
self.wrapped.description.clone()
}
}
/// Mirrors Java's `WrappedAbility.getSVar(String)`.
/// Looks up an SVar on the source card.
pub fn get_s_var(&self, game: &GameState, name: &str) -> Option<String> {
self.wrapped
.source
.and_then(|cid| game.card(cid).get_s_var(name).map(str::to_string))
}
/// Mirrors Java's `WrappedAbility.getSVarInt(String)`.
/// Returns the SVar parsed as an integer, or `None` if absent/unparseable.
pub fn get_s_var_int(&self, game: &GameState, name: &str) -> Option<i32> {
self.get_s_var(game, name)
.and_then(|v| v.parse::<i32>().ok())
}
/// Mirrors Java's `WrappedAbility.setSVar(String, String)`.
/// Sets an SVar on the source card.
pub fn set_s_var(&self, game: &mut GameState, name: &str, value: &str) {
if let Some(cid) = self.wrapped.source {
game.card_mut(cid).set_s_var(name, value);
}
}
/// Mirrors Java's `WrappedAbility.getAdditionalAbility(String)`.
/// In Java this returns a SpellAbility parsed from the named param;
/// in Rust we return the raw param value which callers can parse.
pub fn get_additional_ability(&self, key: &str) -> Option<&str> {
self.get_param(key)
}
/// Mirrors Java's `WrappedAbility.getAdditionalAbilityList(String)`.
/// Returns the param value split by `&` (the Java list separator for
/// additional ability lists in card scripts).
pub fn get_additional_ability_list(&self, name: &str) -> Vec<String> {
if let Some(list) = self.additional_ability_lists.get(name) {
return list.clone();
}
self.get_param(name)
.map(|v| v.split('&').map(|s| s.trim().to_string()).collect())
.unwrap_or_default()
}
/// Mirrors Java's `WrappedAbility.setAdditionalAbilityList(String, List)`.
/// Stores the list as an `&`-joined param value.
pub fn set_additional_ability_list(&mut self, name: &str, list: Vec<String>) {
self.additional_ability_lists.insert(name.to_string(), list);
}
/// Mirrors Java's `WrappedAbility.isAlternativeCost(AlternativeCost)`.
/// Checks whether this ability was cast using the given alternative cost.
pub fn is_alternative_cost(&self, ac: AlternativeCost) -> bool {
self.wrapped.alt_cost == Some(ac)
}
/// Mirrors Java's `WrappedAbility.isKeyword(Keyword)`.
/// Checks whether this ability's params contain a `Keyword$` entry
/// matching the given keyword.
pub fn is_keyword(&self, kw: Keyword) -> bool {
self.wrapped
.param_value("Keyword")
.map(|v| {
let kw_str = format!("{:?}", kw);
v.eq_ignore_ascii_case(&kw_str)
})
.unwrap_or(false)
}
}