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
use battler_data::Id;

use crate::battle::{
    MonHandle,
    MoveHandle,
    OutsideEffect,
    SpeedOrderable,
};

/// A Mon action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MonAction {
    pub mon: MonHandle,
    pub speed: u32,
}

impl MonAction {
    pub fn new(mon: MonHandle) -> Self {
        Self { mon, speed: 0 }
    }
}

/// A Team Preview action input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TeamActionInput {
    pub mon: MonHandle,
    pub index: usize,
    pub priority: i32,
}

/// A Team Preview action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TeamAction {
    pub mon_action: MonAction,
    pub index: usize,
    pub priority: i32,
}

impl TeamAction {
    /// Creates a new [`TeamAction`] from [`TeamActionInput`].
    pub fn new(input: TeamActionInput) -> Self {
        Self {
            mon_action: MonAction::new(input.mon),
            index: input.index,
            priority: input.priority,
        }
    }
}

/// A switch action input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SwitchActionInput {
    pub instant: bool,
    pub mon: MonHandle,
    pub switching_out: MonHandle,
    pub position: usize,
}

/// A switch action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SwitchAction {
    pub instant: bool,
    pub mon_action: MonAction,
    pub switching_out: MonHandle,
    pub position: usize,
}

impl SwitchAction {
    /// Creates a new [`SwitchAction`] from [`SwitchActionInput`].
    pub fn new(input: SwitchActionInput) -> Self {
        Self {
            instant: input.instant,
            mon_action: MonAction::new(input.mon),
            switching_out: input.switching_out,
            position: input.position,
        }
    }
}

/// A move action input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MoveActionInput {
    pub id: Id,
    pub upgraded_id: Option<Id>,
    pub mon: MonHandle,
    pub target: Option<isize>,
    pub mega: bool,
    pub z_move: bool,
    pub ultra: bool,
    pub dyna: bool,
    pub tera: bool,
}

/// A move action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MoveAction {
    pub action_id: usize,
    pub id: Id,
    pub upgraded_id: Option<Id>,
    pub mon_action: MonAction,
    pub target: Option<isize>,
    pub original_target: Option<MonHandle>,
    pub mega: bool,
    pub z_move: bool,
    pub ultra: bool,
    pub dyna: bool,
    pub tera: bool,
    pub order: Option<u32>,
    pub priority: i32,
    pub sub_priority: i32,

    // The active move that will be executed by this action.
    //
    // Only available after resolving the action and adding it to the battle queue.
    pub active_move_handle: Option<MoveHandle>,
}

impl MoveAction {
    /// Creates a new [`MoveAction`] from [`MoveActionInput`].
    pub fn new(input: MoveActionInput) -> Self {
        Self {
            action_id: usize::MAX,
            id: input.id,
            upgraded_id: input.upgraded_id,
            mon_action: MonAction::new(input.mon),
            target: input.target,
            original_target: None,
            mega: input.mega,
            z_move: input.z_move,
            ultra: input.ultra,
            dyna: input.dyna,
            tera: input.tera,
            order: None,
            priority: 0,
            sub_priority: 0,
            active_move_handle: None,
        }
    }

    /// The effective move ID
    pub fn effective_move_id(&self) -> &Id {
        if let Some(id) = &self.upgraded_id {
            return id;
        }
        &self.id
    }
}

/// A before move action input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BeforeMoveActionInput {
    pub id: Id,
    pub mon: MonHandle,
}

/// A before move action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BeforeMoveAction {
    pub id: Id,
    pub mon_action: MonAction,
    pub priority: i32,
    pub sub_priority: i32,
}

impl BeforeMoveAction {
    /// Creates a new [`BeforeMoveAction`] from [`BeforeMoveActionInput`].
    pub fn new(input: BeforeMoveActionInput) -> Self {
        Self {
            id: input.id,
            mon_action: MonAction::new(input.mon),
            priority: 0,
            sub_priority: 0,
        }
    }
}

/// An experience action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExperienceAction {
    pub mon: MonHandle,
    pub player_index: usize,
    pub mon_index: usize,
    pub active: bool,
    pub exp: u32,
}

/// A level up action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LevelUpAction {
    pub mon: MonHandle,
    pub level: Option<u8>,
}

/// A learn move action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LearnMoveAction {
    pub mon: MonHandle,
    pub forget_move_slot: usize,
}

/// An end action, which ends the battle.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EndAction {
    pub winning_side: Option<usize>,
}

/// An escape action input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EscapeActionInput {
    pub mon: MonHandle,
}

/// An escape action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EscapeAction {
    pub mon_action: MonAction,
}

impl EscapeAction {
    /// Creates a new [`EscapeAction`] from [`EscapeActionInput`].
    pub fn new(input: EscapeActionInput) -> Self {
        Self {
            mon_action: MonAction::new(input.mon),
        }
    }
}

/// A switch events action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SwitchEventsAction {
    pub mon_action: MonAction,
}

impl SwitchEventsAction {
    /// Creates a new [`SwitchEventsAction`].
    pub fn new(mon_handle: MonHandle) -> Self {
        Self {
            mon_action: MonAction::new(mon_handle),
        }
    }
}

/// A forfeit action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ForfeitAction {
    pub player: usize,
    pub order: u32,
}

/// An item action input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ItemActionInput {
    pub mon: MonHandle,
    pub item: Id,
    pub target: Option<isize>,
}

/// An item action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ItemAction {
    pub mon_action: MonAction,
    pub item: Id,
    pub target: Option<isize>,
    pub move_slot: Option<Id>,
}

impl ItemAction {
    /// Creates a new [`ItemAction`] from [`ItemActionInput`].
    pub fn new(input: ItemActionInput) -> Self {
        Self {
            mon_action: MonAction::new(input.mon),
            item: input.item,
            target: input.target,
            move_slot: None,
        }
    }
}

/// A shift action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShiftAction {
    pub mon_action: MonAction,
    pub position: usize,
}

impl ShiftAction {
    /// Creates a new [`ShiftAction`].
    pub fn new(mon_handle: MonHandle, position: usize) -> Self {
        Self {
            mon_action: MonAction::new(mon_handle),
            position,
        }
    }
}

#[derive(Debug, Clone)]
pub struct OutsideEffectAction {
    pub outside_effect: OutsideEffect,
    pub order: u32,
}

impl PartialEq for OutsideEffectAction {
    fn eq(&self, other: &Self) -> bool {
        self.order == other.order
    }
}

impl Eq for OutsideEffectAction {}

/// An action during a battle.
///
/// Actions are the core of a battle. A turn of a battle consists of several actions running
/// sequentially. Actions can also be run outside of a turn for configuration and miscellaneous
/// purposes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Action {
    Start,
    End(EndAction),
    Pass,
    BeforeTurn,
    Residual,
    Team(TeamAction),
    Switch(SwitchAction),
    BeforeSwitchEvents(SwitchEventsAction),
    SwitchEvents(SwitchEventsAction),
    Move(MoveAction),
    BeforeTurnMove(BeforeMoveAction),
    PriorityChargeMove(BeforeMoveAction),
    MegaEvo(MonAction),
    UltraBurst(MonAction),
    Dynamax(MonAction),
    Terastallize(MonAction),
    Experience(ExperienceAction),
    LevelUp(LevelUpAction),
    LearnMove(LearnMoveAction),
    Escape(EscapeAction),
    Forfeit(ForfeitAction),
    Item(ItemAction),
    Shift(ShiftAction),
    OutsideEffect(OutsideEffectAction),
}

impl Action {
    pub fn action_id_mut(&mut self) -> Option<&mut usize> {
        match self {
            Self::Move(action) => Some(&mut action.action_id),
            _ => None,
        }
    }

    pub fn mon_action_mut(&mut self) -> Option<&mut MonAction> {
        match self {
            Self::Team(action) => Some(&mut action.mon_action),
            Self::Switch(action) => Some(&mut action.mon_action),
            Self::SwitchEvents(action) => Some(&mut action.mon_action),
            Self::Move(action) => Some(&mut action.mon_action),
            Self::BeforeTurnMove(action) => Some(&mut action.mon_action),
            Self::PriorityChargeMove(action) => Some(&mut action.mon_action),
            Self::MegaEvo(action) => Some(action),
            Self::UltraBurst(action) => Some(action),
            Self::Dynamax(action) => Some(action),
            Self::Terastallize(action) => Some(action),
            Self::Escape(action) => Some(&mut action.mon_action),
            Self::Item(action) => Some(&mut action.mon_action),
            Self::Shift(action) => Some(&mut action.mon_action),
            _ => None,
        }
    }
}

impl SpeedOrderable for Action {
    fn order(&self) -> u32 {
        match self {
            Self::Team(_) => 1,
            Self::Start => 2,
            Self::LearnMove(_) => 3,
            Self::LevelUp(_) => 4,
            Self::Experience(_) => 5,
            Self::OutsideEffect(_) => 6,
            Self::Switch(action) => {
                if action.instant {
                    7
                } else {
                    100
                }
            }
            Self::End(_) => 8,
            Self::Forfeit(_) => 9,
            Self::BeforeTurn => 10,
            Self::Item(_) => 11,
            Self::BeforeTurnMove(_) => 12,
            Self::BeforeSwitchEvents(_) => 99,
            Self::Escape(_) => 101,
            Self::SwitchEvents(_) => 103,
            Self::MegaEvo(_) => 104,
            Self::UltraBurst(_) => 105,
            Self::Dynamax(_) => 106,
            Self::Terastallize(_) => 107,
            Self::PriorityChargeMove(_) => 108,
            Self::Move(action) => action.order.unwrap_or(200),
            Self::Pass => 200,
            Self::Shift(_) => 200,
            Self::Residual => 300,
        }
    }

    fn priority(&self) -> i32 {
        match self {
            Self::Team(action) => action.priority,
            Self::Move(action) => action.priority,
            Self::BeforeTurnMove(action) => action.priority,
            Self::PriorityChargeMove(action) => action.priority,
            Self::Experience(action) => action.player_index as i32,
            _ => 0,
        }
    }

    fn sub_priority(&self) -> i32 {
        match self {
            Self::Move(action) => action.sub_priority,
            Self::BeforeTurnMove(action) => action.sub_priority,
            Self::PriorityChargeMove(action) => action.sub_priority,
            _ => 0,
        }
    }

    fn speed(&self) -> u32 {
        match self {
            Self::Team(action) => action.mon_action.speed,
            Self::Switch(action) => action.mon_action.speed,
            Self::SwitchEvents(action) => action.mon_action.speed,
            Self::Move(action) => action.mon_action.speed,
            Self::BeforeTurnMove(action) => action.mon_action.speed,
            Self::PriorityChargeMove(action) => action.mon_action.speed,
            Self::MegaEvo(action) => action.speed,
            Self::UltraBurst(action) => action.speed,
            Self::Dynamax(action) => action.speed,
            Self::Escape(action) => action.mon_action.speed,
            Self::Item(action) => action.mon_action.speed,
            Self::Shift(action) => action.mon_action.speed,
            _ => 1,
        }
    }

    fn sub_order(&self) -> u32 {
        match self {
            Self::Experience(action) => {
                // Active Mons should get experience before inactive Mons.
                if action.active {
                    action.mon_index as u32
                } else {
                    action.mon_index as u32 + 65535
                }
            }
            Self::Forfeit(action) => action.order,
            Self::OutsideEffect(action) => action.order,
            _ => 0,
        }
    }
}