lf2_parse 0.1.0

Parses Little Fighter 2 (LF2) data files
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
use std::convert::TryFrom;

use pest::iterators::Pair;

use crate::{Error, FrameNumberNext, ObjectDataParser, Rule, SubRuleFn};

pub use self::{
    effect::{Effect, EffectParseError},
    itr_kind::ItrKind,
    itr_kind_parse_error::ItrKindParseError,
};

mod effect;
mod itr_kind;
mod itr_kind_parse_error;

/// Area that hits other objects.
///
/// See https://lf-empire.de/lf2-empire/data-changing/frame-elements/174-itr-interaction?start=1
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Itr {
    /// Interaction variants.
    pub kind: ItrKind,
    /// X coordinate.
    pub x: i32,
    /// Y coordinate.
    pub y: i32,
    /// Width.
    pub w: u32,
    /// Height.
    pub h: u32,
    /// Z Width extends in both directions + 1 center pixel.
    ///
    /// `zwidth: 10` means 10 pixels up, 10 pixels down, and one pixel for
    /// center of the shadow for 21 pixels total.
    pub z_width: u32,
    /// Acceleration to place on the hit object in the X axis.
    ///
    /// # Notes
    ///
    /// * `itr/kind:8` ([`ItrKind::HealBall`]) uses this as the frame number for
    ///   the object with this `Itr` to switch to when hitting a character.
    pub d_vx: i64,
    /// Acceleration to place on the hit object in the Y axis.
    pub d_vy: i64,
    /// Delay before another hit may happen, restricts this `Itr` to one object.
    pub a_rest: u32,
    /// Delay before another hit may happen, allows multiple objects to be hit.
    pub v_rest: u32,
    /// How much a character is "off balance".
    ///
    /// The `fall` value determines how an attacked character will react to this
    /// itr by flinching, getting into the stunned frames, or `falling`. If no
    /// value is specified, the default of `20` will be used.
    ///
    /// * If a character accumulates `20` `fall` points, he switches to
    ///   `injured1` (`220`).
    /// * If a character accumulates `40` `fall` points, he switches to
    ///   `injured2` (`222`) or `injured2back` (`224`) depending on the
    ///   direction he is hit, and will fall if he is in mid-air.
    /// * If a character accumulates `60` `fall` points, he switches into the
    ///   `stunned` (`226`) frames where he can be grabbed or hit by
    ///   `super_punch`.
    ///
    /// Attacks with `fall: 41` or more can hit `falling` characters.
    ///
    /// Here are a few values as a rule of thumb for various `fall` values:
    ///
    /// | `fall` | Description                                              |
    /// | -----: | :------------------------------------------------------- |
    /// |     -1 | Does not go into injured frames and harder to knockdown. |
    /// |      1 | Never stun, never fall (Davis DvA shrafe)                |
    /// |     20 | 3 hit stun, 4 hit fall                                   |
    /// |     25 | 2 hit stun, 3 hit fall (Dennis normal kick)              |
    /// |     40 | Does not stun, 2 hit fall (baseball bat normal swing)    |
    /// |     60 | 1 hit stun, 2 hit fall (Henry's arrow)                   |
    /// |     70 | 1 hit fall                                               |
    ///
    /// Every 1 TU, a `fall` point is deducted.
    pub fall: i32,
    /// Broken-defence points.
    ///
    /// `bdefend` points determine if a character is able to block an attack by
    /// defending or if he will go to the broken-defense-frames. As long as he
    /// has 30 or less Bdefend-points, he will be able to block the attack, if
    /// it's 31 or higher, he goes to the broken-defense-frames. If an itr hits
    /// the character while he is not in the defend-frames, his `bdefend`
    /// counter will automatically increase to 45. If you have, for example,
    /// accumulated 31 points and get hit during your defense (assuming you have
    /// specified a positive `bdefend` value in the hitting itr), the character
    /// will go to the broken-defense-frames.
    ///
    /// Here are some common values for various `bdefend` values:
    ///
    /// | `bdefend` | Description                                       |
    /// | --------: | :------------------------------------------------ |
    /// |         0 | never breaks defense (ex: John's D>J shield)      |
    /// |        12 | 4 hit break                                       |
    /// |        16 | 3 hit break                                       |
    /// |        30 | 2 hit break                                       |
    /// |        60 | 1 hit break                                       |
    /// |       100 | ignores defense, sets `bdefend` counter to `45`,\
    ///               and instantly destroys weapons.                   |
    ///
    /// Every 1 TU, a `bdefend` point is deducted, so he will be able to recover
    /// his defense.
    ///
    /// Armor will function as long as a character has not accumulated more
    /// `bdefend` points than the specific armor points of Louis(1), Knight or
    /// Julian(15) at the time of attack. For example, Julian can resist a dash
    /// attack(bdefend 60) even though he only has 15 armor points, but he will
    /// be left completely vulnerable for the next 45 TU until he regains his
    /// 1st armor point.
    pub b_defend: i32,
    /// Amount of damage to inflict on the target object.
    ///
    /// # Notes
    ///
    /// * `itr/kind:5` ([`ItrKind::WeaponStrength`]) ignores this and uses the
    ///   `injury` in `weapon_strength_list`.
    /// * `itr/kind:8` ([`ItrKind::HealBall`]) uses this for the number of HP to
    ///   heal a character by.
    pub injury: i32,
    /// Itr `effect` variants.
    pub effect: Effect,
    /// Frame numbers for where the catching object should switch to.
    ///
    /// Used in `itr/kind: 1` ([`ItrKind::CatchStunned`]) and `itr/kind: 3`
    /// ([`ItrKind::CatchForce`]).
    pub catching_act: FrameNumberNext,
    /// Frame numbers for where the caught character should switch to.
    ///
    /// Used in `itr/kind: 1` ([`ItrKind::CatchStunned`]) and `itr/kind: 3`
    /// ([`ItrKind::CatchForce`]).
    pub caught_act: FrameNumberNext,
}

impl Default for Itr {
    fn default() -> Self {
        Itr {
            kind: Default::default(),
            x: Default::default(),
            y: Default::default(),
            w: Default::default(),
            h: Default::default(),
            z_width: Self::Z_WIDTH_DEFAULT,
            d_vx: Default::default(),
            d_vy: Default::default(),
            a_rest: Default::default(),
            v_rest: Default::default(),
            fall: Default::default(),
            b_defend: Default::default(),
            injury: Default::default(),
            effect: Default::default(),
            catching_act: Default::default(),
            caught_act: Default::default(),
        }
    }
}

impl Itr {
    /// Default `Z_WIDTH` for `Itr` volumes.
    pub const Z_WIDTH_DEFAULT: u32 = 13;

    fn parse_tags<'i>(itr: Itr, itr_data_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        itr_data_pair.into_inner().try_fold(itr, Itr::parse_tag)
    }

    fn parse_tag<'i>(itr: Itr, itr_tag_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        ObjectDataParser::parse_as_type(
            itr,
            itr_tag_pair,
            Rule::ItrTag,
            &[Self::parse_tag_value as SubRuleFn<_>],
        )
    }

    fn parse_tag_value<'i>(mut itr: Itr, itr_tag_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        itr = match itr_tag_pair.as_rule() {
            Rule::TagKind => {
                ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_kind_value)?
            }
            Rule::TagX => ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_x_value)?,
            Rule::TagY => ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_y_value)?,
            Rule::TagW => ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_w_value)?,
            Rule::TagH => ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_h_value)?,
            Rule::TagZWidth => {
                ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_z_width_value)?
            }
            Rule::TagDVx => {
                ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_d_vx_value)?
            }
            Rule::TagDVy => {
                ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_d_vy_value)?
            }
            Rule::TagARest => {
                ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_a_rest_value)?
            }
            Rule::TagVRest => {
                ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_v_rest_value)?
            }
            Rule::TagFall => {
                ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_fall_value)?
            }
            Rule::TagBDefend => {
                ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_b_defend_value)?
            }
            Rule::TagInjury => {
                ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_injury_value)?
            }
            Rule::TagEffect => {
                ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_effect_value)?
            }
            Rule::TagCatchingAct => {
                ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_catching_act_value)?
            }
            Rule::TagCaughtAct => {
                ObjectDataParser::parse_value(itr, itr_tag_pair, Self::parse_caught_act_value)?
            }
            _ => itr,
        };
        Ok(itr)
    }

    fn parse_kind_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let kind = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseItrKind { value_pair, error })?;
        itr.kind = kind;
        Ok(itr)
    }

    fn parse_x_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let x = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(x),
                value_pair,
                error,
            })?;
        itr.x = x;
        Ok(itr)
    }

    fn parse_y_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let y = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(y),
                value_pair,
                error,
            })?;
        itr.y = y;
        Ok(itr)
    }

    fn parse_w_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let w = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(w),
                value_pair,
                error,
            })?;
        itr.w = w;
        Ok(itr)
    }

    fn parse_h_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let h = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(h),
                value_pair,
                error,
            })?;
        itr.h = h;
        Ok(itr)
    }

    fn parse_z_width_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let z_width = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(zwidth),
                value_pair,
                error,
            })?;
        itr.z_width = z_width;
        Ok(itr)
    }

    fn parse_d_vx_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let d_vx = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(d_vx),
                value_pair,
                error,
            })?;
        itr.d_vx = d_vx;
        Ok(itr)
    }

    fn parse_d_vy_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let d_vy = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(d_vy),
                value_pair,
                error,
            })?;
        itr.d_vy = d_vy;
        Ok(itr)
    }

    fn parse_a_rest_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let a_rest = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(arest),
                value_pair,
                error,
            })?;
        itr.a_rest = a_rest;
        Ok(itr)
    }

    fn parse_v_rest_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let v_rest = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(vrest),
                value_pair,
                error,
            })?;
        itr.v_rest = v_rest;
        Ok(itr)
    }

    fn parse_fall_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let fall = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(fall),
                value_pair,
                error,
            })?;
        itr.fall = fall;
        Ok(itr)
    }

    fn parse_b_defend_value<'i>(
        mut itr: Itr,
        value_pair: Pair<'i, Rule>,
    ) -> Result<Itr, Error<'i>> {
        let b_defend = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(b_defend),
                value_pair,
                error,
            })?;
        itr.b_defend = b_defend;
        Ok(itr)
    }

    fn parse_injury_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let injury = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(injury),
                value_pair,
                error,
            })?;
        itr.injury = injury;
        Ok(itr)
    }

    fn parse_effect_value<'i>(mut itr: Itr, value_pair: Pair<'i, Rule>) -> Result<Itr, Error<'i>> {
        let effect = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseItrEffect { value_pair, error })?;
        itr.effect = effect;
        Ok(itr)
    }

    fn parse_catching_act_value<'i>(
        mut itr: Itr,
        value_pair: Pair<'i, Rule>,
    ) -> Result<Itr, Error<'i>> {
        let catching_act = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(catching_act),
                value_pair,
                error,
            })?;
        itr.catching_act = catching_act;
        Ok(itr)
    }

    fn parse_caught_act_value<'i>(
        mut itr: Itr,
        value_pair: Pair<'i, Rule>,
    ) -> Result<Itr, Error<'i>> {
        let caught_act = value_pair
            .as_str()
            .parse()
            .map_err(|error| Error::ParseInt {
                field: stringify!(caught_act),
                value_pair,
                error,
            })?;
        itr.caught_act = caught_act;
        Ok(itr)
    }
}

impl<'i> TryFrom<Pair<'i, Rule>> for Itr {
    type Error = Error<'i>;

    fn try_from(pair: Pair<'i, Rule>) -> Result<Self, Self::Error> {
        let sub_rule_fns: &[SubRuleFn<_>] = &[Itr::parse_tags];
        ObjectDataParser::parse_as_type(Itr::default(), pair, Rule::Itr, sub_rule_fns)
    }
}