battler 0.8.0

Pokémon battle engine for Rust.
Documentation
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use alloc::vec::Vec;

use anyhow::Result;
use battler_data::{
    Id,
    MoveFlag,
    Type,
};

use crate::{
    battle::{
        ActiveMoveContext,
        Field,
        MonContext,
        MonHandle,
        core_battle_effects,
    },
    effect::fxlang,
};

/// The effective types for the Mon.
///
/// Non-empty. [`Type::None`] is returned when the Mon has no types
pub fn effective_types(context: &mut MonContext) -> Vec<Type> {
    if let Some(effective_types) = context
        .mon()
        .volatile_state
        .effect_cache
        .effective_types
        .clone()
    {
        return effective_types;
    }
    let effective_types = {
        let mut types = effective_types_no_added_type(context);
        if let Some(added_type) = context.mon().volatile_state.added_type {
            types.push(added_type);
        }
        types
    };
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .effective_types = Some(effective_types.clone());
    effective_types
}

/// The effective types for the Mon, disregarding the added type.
///
/// Non-empty. [`Type::None`] is returned when the Mon has no types
pub fn effective_types_no_added_type(context: &mut MonContext) -> Vec<Type> {
    if let Some(effective_types_no_added_type) = context
        .mon()
        .volatile_state
        .effect_cache
        .effective_types_no_added_type
        .clone()
    {
        return effective_types_no_added_type;
    }
    let effective_types_no_added_type = {
        let types = core_battle_effects::run_event_for_mon_expecting_types(
            context,
            fxlang::BattleEvent::ForceTypes,
            Vec::default(),
        );
        if !types.is_empty() {
            types
        } else {
            effective_types_before_forced_types(context, false)
        }
    };
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .effective_types_no_added_type = Some(effective_types_no_added_type.clone());
    effective_types_no_added_type
}

/// The effective types for the Mon, before forced types (e.g., Terastallization).
///
/// Non-empty. [`Type::None`] is returned when the Mon has no types
fn effective_types_before_forced_types(context: &mut MonContext, added_type: bool) -> Vec<Type> {
    if let Some(effective_types_before_forced_types) = context
        .mon()
        .volatile_state
        .effect_cache
        .effective_types_before_forced_types
        .clone()
    {
        return effective_types_before_forced_types;
    }
    let mut types = context.mon().volatile_state.types.clone();
    if added_type && let Some(added_type) = context.mon().volatile_state.added_type {
        types.push(added_type);
    }
    let effective_types_before_forced_types = {
        let types = core_battle_effects::run_event_for_mon_expecting_types(
            context,
            fxlang::BattleEvent::Types,
            types,
        );
        if !types.is_empty() {
            types
        } else {
            Vec::from_iter([Type::None])
        }
    };
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .effective_types_before_forced_types = Some(effective_types_before_forced_types.clone());
    effective_types_before_forced_types
}

/// Checks if the Mon has the given type.
pub fn has_type(context: &mut MonContext, typ: Type) -> bool {
    let types = effective_types(context);
    types.contains(&typ)
}

/// Checks if the Mon has the given type, before forced types (e.g., Terastallization).
pub fn has_type_before_forced_types(context: &mut MonContext, typ: Type) -> bool {
    let types = effective_types_before_forced_types(context, true);
    types.contains(&typ)
}

/// The health at which the [`Mon`][`crate::battle::Mon`] eats berries.
pub fn berry_eating_health(context: &mut MonContext) -> u16 {
    if let Some(berry_eating_health) = context
        .mon()
        .volatile_state
        .effect_cache
        .berry_eating_health
    {
        return berry_eating_health;
    }
    let berry_eating_health = {
        let health = context.mon().max_hp / 4;
        core_battle_effects::run_event_for_mon_expecting_u16(
            context,
            fxlang::BattleEvent::BerryEatingHealth,
            health,
            fxlang::VariableInput::default(),
        )
    };
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .berry_eating_health = Some(berry_eating_health);
    berry_eating_health
}

/// Checks if the [`Mon`][`crate::battle::Mon`] can heal.
///
/// Does not necessarily check if the Mon needs to heal.
pub fn can_heal(context: &mut MonContext) -> bool {
    if let Some(can_heal) = context.mon().volatile_state.effect_cache.can_heal {
        return can_heal;
    }
    let can_heal = core_battle_effects::run_event_for_mon_expecting_bool_quick_return(
        context,
        fxlang::BattleEvent::CanHeal,
        true,
    );
    context.mon_mut().volatile_state.effect_cache.can_heal = Some(can_heal);
    can_heal
}

/// Checks if the [`Mon`][`crate::battle::Mon`] is asleep.
pub fn is_asleep(context: &mut MonContext) -> bool {
    if let Some(is_asleep) = context.mon().volatile_state.effect_cache.is_asleep {
        return is_asleep;
    }
    let is_asleep = core_battle_effects::run_event_for_mon_expecting_bool_quick_return(
        context,
        fxlang::BattleEvent::IsAsleep,
        false,
    );
    context.mon_mut().volatile_state.effect_cache.is_asleep = Some(is_asleep);
    is_asleep
}

/// Checks if the [`Mon`][`crate::battle::Mon`] is away from the field (e.g., immobilized by Sky
/// Drop).
pub fn is_away_from_field(context: &mut MonContext) -> bool {
    if let Some(is_away_from_field) = context.mon().volatile_state.effect_cache.is_away_from_field {
        return is_away_from_field;
    }
    let is_away_from_field = core_battle_effects::run_event_for_mon_expecting_bool_quick_return(
        context,
        fxlang::BattleEvent::IsAwayFromField,
        false,
    );
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .is_away_from_field = Some(is_away_from_field);
    is_away_from_field
}

/// Checks if the [`Mon`][`crate::battle::Mon`] is behind a substitute.
pub fn is_behind_substitute(context: &mut MonContext) -> bool {
    if let Some(is_behind_substitute) = context
        .mon()
        .volatile_state
        .effect_cache
        .is_behind_substitute
    {
        return is_behind_substitute;
    }
    let is_behind_substitute = core_battle_effects::run_event_for_mon_expecting_bool_quick_return(
        context,
        fxlang::BattleEvent::IsBehindSubstitute,
        false,
    );
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .is_behind_substitute = Some(is_behind_substitute);
    is_behind_substitute
}

/// Checks if the [`Mon`][`crate::battle::Mon`] is protected from making contact with other Mons.
pub fn is_contact_proof(context: &mut MonContext) -> bool {
    if let Some(is_contact_proof) = context.mon().volatile_state.effect_cache.is_contact_proof {
        return is_contact_proof;
    }
    let is_contact_proof = core_battle_effects::run_event_for_mon_expecting_bool_quick_return(
        context,
        fxlang::BattleEvent::IsContactProof,
        false,
    );
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .is_contact_proof = Some(is_contact_proof);
    is_contact_proof
}

/// Checks if the [`Mon`][`crate::battle::Mon`] is grounded.
pub fn is_grounded(context: &mut MonContext) -> bool {
    if let Some(is_grounded) = context.mon().volatile_state.effect_cache.is_grounded {
        return is_grounded;
    }
    let is_grounded = core_battle_effects::run_event_for_mon_expecting_bool_quick_return(
        context,
        fxlang::BattleEvent::IsGrounded,
        true,
    );
    context.mon_mut().volatile_state.effect_cache.is_grounded = Some(is_grounded);
    is_grounded
}

/// Checks if the [`Mon`][`crate::battle::Mon`] is immune to entry hazards.
pub fn is_immune_to_entry_hazards(context: &mut MonContext) -> bool {
    if let Some(is_immune_to_entry_hazards) = context
        .mon()
        .volatile_state
        .effect_cache
        .is_immune_to_entry_hazards
    {
        return is_immune_to_entry_hazards;
    }
    let is_immune_to_entry_hazards =
        core_battle_effects::run_event_for_mon_expecting_bool_quick_return(
            context,
            fxlang::BattleEvent::IsImmuneToEntryHazards,
            false,
        );
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .is_immune_to_entry_hazards = Some(is_immune_to_entry_hazards);
    is_immune_to_entry_hazards
}

/// Checks if the [`Mon`][`crate::battle::Mon`] is soundproof.
pub fn is_soundproof(context: &mut MonContext) -> bool {
    if let Some(is_soundproof) = context.mon().volatile_state.effect_cache.is_soundproof {
        return is_soundproof;
    }
    let is_soundproof = core_battle_effects::run_event_for_mon_expecting_bool_quick_return(
        context,
        fxlang::BattleEvent::IsSoundproof,
        false,
    );
    context.mon_mut().volatile_state.effect_cache.is_soundproof = Some(is_soundproof);
    is_soundproof
}

/// Checks if the [`Mon`][`crate::battle::Mon`] is semi-invulnerable.
pub fn is_semi_invulnerable(context: &mut MonContext) -> bool {
    if let Some(is_semi_invulnerable) = context
        .mon()
        .volatile_state
        .effect_cache
        .is_semi_invulnerable
    {
        return is_semi_invulnerable;
    }
    let is_semi_invulnerable = core_battle_effects::run_event_for_mon_expecting_bool_quick_return(
        context,
        fxlang::BattleEvent::IsSemiInvulnerable,
        false,
    );
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .is_semi_invulnerable = Some(is_semi_invulnerable);
    is_semi_invulnerable
}

/// The weather override for the [`Mon`][`crate::battle::Mon`].
///
/// Weather can be overridden for different events by abilities and items.
pub fn override_weather(context: &mut MonContext) -> Option<Id> {
    if let Some(override_weather) = context
        .mon()
        .volatile_state
        .effect_cache
        .override_weather
        .clone()
    {
        return override_weather;
    }
    let override_weather = {
        if let Some(weather) = core_battle_effects::run_event_for_mon_expecting_string(
            context,
            fxlang::BattleEvent::OverrideWeather,
            fxlang::VariableInput::default(),
        ) {
            Some(Id::from(weather))
        } else {
            None
        }
    };
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .override_weather = Some(override_weather.clone());
    override_weather
}

/// The effective weather for the [`Mon`][`crate::battle::Mon`].
///
/// Weather can be suppressed for the Mon by abilities and items.
pub fn effective_weather(
    context: &mut MonContext,
    origin: Option<MonHandle>,
) -> Result<Option<Id>> {
    let origin = origin.unwrap_or(context.mon_handle());
    if let Some(override_weather) =
        override_weather(&mut context.as_battle_context_mut().mon_context(origin)?)
    {
        return Ok(Some(override_weather));
    }
    Ok(effective_weather_no_override(context))
}

/// The effective weather for the [`Mon`][`crate::battle::Mon`], without any overrides.
///
/// Weather can be suppressed for the Mon by abilities and items.
fn effective_weather_no_override(context: &mut MonContext) -> Option<Id> {
    if let Some(effective_weather) = context
        .mon()
        .volatile_state
        .effect_cache
        .effective_weather
        .clone()
    {
        return effective_weather;
    }
    let effective_weather = {
        if core_battle_effects::run_event_for_mon_expecting_bool_quick_return(
            context,
            fxlang::BattleEvent::SuppressMonWeather,
            false,
        ) {
            None
        } else {
            Field::effective_weather(context.as_battle_context_mut())
        }
    };
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .effective_weather = Some(effective_weather.clone());
    effective_weather
}

/// The effective terrain for the [`Mon`][`crate::battle::Mon`].
///
/// Terrain can be suppressed for the Mon by abilities and items.
pub fn effective_terrain(context: &mut MonContext) -> Option<Id> {
    if let Some(effective_terrain) = context
        .mon()
        .volatile_state
        .effect_cache
        .effective_terrain
        .clone()
    {
        return effective_terrain;
    }
    let terrain = Field::effective_terrain(context.as_battle_context_mut()).clone()?;
    let effective_terrain = {
        if !is_grounded(context) || is_semi_invulnerable(context) {
            None
        } else if core_battle_effects::run_event_for_mon_expecting_bool_quick_return(
            context,
            fxlang::BattleEvent::SuppressMonTerrain,
            false,
        ) {
            None
        } else {
            Some(terrain)
        }
    };
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .effective_terrain = Some(effective_terrain.clone());
    effective_terrain
}

fn check_ability_suppression(context: &mut MonContext) -> (Option<Id>, bool) {
    if let Some(effective_ability) = context
        .mon()
        .volatile_state
        .effect_cache
        .effective_ability
        .clone()
        && let Some(can_suppress_ability) = context
            .mon()
            .volatile_state
            .effect_cache
            .can_suppress_ability
    {
        return (effective_ability, can_suppress_ability);
    }
    let ability = context.mon().volatile_state.ability.id.clone();
    let (effective_ability, can_suppress_ability) = {
        let suppress_ability =
            core_battle_effects::run_event_for_mon_expecting_bool_quick_return_no_default(
                context,
                fxlang::BattleEvent::SuppressMonAbility,
            );
        match suppress_ability {
            Some(true) => (None, false),
            Some(false) => (Some(ability), false),
            None => (Some(ability), true),
        }
    };
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .can_suppress_ability = Some(can_suppress_ability);
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .effective_ability = Some(effective_ability.clone());
    (effective_ability, can_suppress_ability)
}

/// Checks if the [`Mon`][`crate::battle::Mon`]'s ability can be suppressed.
pub fn can_suppress_ability(context: &mut MonContext) -> bool {
    check_ability_suppression(context).1
}

/// The effective ability of the [`Mon`][`crate::battle::Mon`].
///
/// Abilities can be suppressed by other effects and abilities.
pub fn effective_ability(context: &mut MonContext) -> Option<Id> {
    check_ability_suppression(context).0
}

fn check_item_suppression(context: &mut MonContext) -> (Option<Id>, bool) {
    if let Some(effective_item) = context
        .mon()
        .volatile_state
        .effect_cache
        .effective_item
        .clone()
        && let Some(can_suppress_item) = context.mon().volatile_state.effect_cache.can_suppress_item
    {
        return (effective_item, can_suppress_item);
    }
    let item = match context.mon().item.clone() {
        Some(item) => item,
        None => return (None, false),
    };
    let (effective_item, can_suppress_item) = {
        let suppress_item =
            core_battle_effects::run_event_for_mon_expecting_bool_quick_return_no_default(
                context,
                fxlang::BattleEvent::SuppressMonItem,
            );
        match suppress_item {
            Some(true) => (None, false),
            Some(false) => (Some(item), false),
            None => (Some(item), true),
        }
    };
    context
        .mon_mut()
        .volatile_state
        .effect_cache
        .can_suppress_item = Some(can_suppress_item);
    context.mon_mut().volatile_state.effect_cache.effective_item = Some(effective_item.clone());
    (effective_item, can_suppress_item)
}

/// Checks if the [`Mon`][`crate::battle::Mon`]'s item can be suppressed.
pub fn can_suppress_item(context: &mut MonContext) -> bool {
    check_item_suppression(context).1
}

/// The effective item of the [`Mon`][`crate::battle::Mon`].
///
/// Items can be suppressed by other effects and abilities.
pub fn effective_item(context: &mut MonContext) -> Option<Id> {
    check_item_suppression(context).0
}

/// Checks if the [`Move`][`crate::moves::Move`] makes contact with its targets.
pub fn move_makes_contact(context: &mut ActiveMoveContext) -> bool {
    if !context
        .active_move()
        .data
        .flags
        .contains(&MoveFlag::Contact)
    {
        return false;
    }

    // Check if the attacker is contact-proof.
    return !is_contact_proof(context.as_mon_context_mut());
}