romkit 0.1.1

Identify game ROMs and extract their data and assets as structured 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
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
//! Species: personal records (44 bytes each) joined with the evolution and level-up learnset
//! tables, which have one member per personal record. Abilities are derived from them: the
//! games have no ability records of their own.

use std::collections::BTreeMap;

use serde::Serialize;

use super::{AbilityId, ItemId, MoveId, SpeciesId, Type};
use crate::Result;
use crate::formats::narc::Narc;
use crate::io::Reader;

const PERSONAL_LEN: usize = 44;
const EVOLUTION_LEN: usize = 44;
const EVOLUTION_SLOTS: usize = 7;
const LEARNSET_END: u16 = 0xFFFF;

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Species {
    pub id: SpeciesId,
    /// Alternate forms have no name of their own.
    pub name: Option<String>,
    /// Single-typed species repeat their type.
    pub types: [Type; 2],
    pub base_stats: Stats,
    pub ev_yield: Stats,
    pub abilities: [Option<AbilityId>; 2],
    pub catch_rate: u8,
    pub base_experience: u8,
    pub base_friendship: u8,
    pub growth_rate: GrowthRate,
    pub gender_ratio: GenderRatio,
    pub egg_groups: [EggGroup; 2],
    pub hatch_cycles: u8,
    pub held_items: HeldItems,
    pub safari_flee_rate: u8,
    pub color: Color,
    pub flip_sprite: bool,
    /// The TMs and HMs it can learn: 0–91 are TM01–TM92, 92–99 are HM01–HM08.
    pub machines: Vec<u8>,
    pub evolutions: Vec<Evolution>,
    /// Moves learned by leveling up, by ascending level.
    pub level_up_moves: Vec<LevelUpMove>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct Stats {
    pub hp: u8,
    pub attack: u8,
    pub defense: u8,
    pub speed: u8,
    pub special_attack: u8,
    pub special_defense: u8,
}

/// Items wild Pokémon may hold.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct HeldItems {
    /// Held 50% of the time.
    pub common: Option<ItemId>,
    /// Held 5% of the time.
    pub rare: Option<ItemId>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct LevelUpMove {
    pub level: u8,
    pub r#move: MoveId,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct Evolution {
    #[serde(flatten)]
    pub method: EvolutionMethod,
    pub into: SpeciesId,
}

/// Every species but the placeholder at index 0, with its evolutions, learnset, and name.
pub(super) fn parse(
    personal: &[u8],
    evolutions: &[u8],
    learnsets: &[u8],
    names: &[String],
) -> Result<Vec<Species>> {
    let personal_narc = Narc::parse(personal)?;
    let evolution_narc = Narc::parse(evolutions)?;
    let learnset_narc = Narc::parse(learnsets)?;
    (1..personal_narc.len())
        .map(|index| {
            let mut species = parse_personal(index, &personal_narc.member(personal, index)?)?;
            species.name = names.get(index).cloned();
            species.evolutions = parse_evolutions(&evolution_narc.member(evolutions, index)?)?;
            species.level_up_moves = parse_learnset(&learnset_narc.member(learnsets, index)?)?;
            Ok(species)
        })
        .collect()
}

fn parse_personal(index: usize, bytes: &[u8]) -> Result<Species> {
    let mut record = Reader::new("Gen 4 personal record", bytes);
    if bytes.len() != PERSONAL_LEN {
        return Err(record.error_at(0, "record is not 44 bytes"));
    }
    let mut stats = || -> Result<Stats> {
        Ok(Stats {
            hp: record.u8()?,
            attack: record.u8()?,
            defense: record.u8()?,
            speed: record.u8()?,
            special_attack: record.u8()?,
            special_defense: record.u8()?,
        })
    };
    let base_stats = stats()?;
    let types = [Type::from_raw(record.u8()?), Type::from_raw(record.u8()?)];
    let catch_rate = record.u8()?;
    let base_experience = record.u8()?;
    // Two bits per stat, in the same order as the base stats.
    let ev_bits = record.u16()?;
    let ev = |stat: u16| (ev_bits >> (stat * 2) & 0b11) as u8;
    let item = |raw: u16| (raw != 0).then_some(ItemId(raw));
    let held_items = HeldItems {
        common: item(record.u16()?),
        rare: item(record.u16()?),
    };
    let gender_ratio = GenderRatio::from_raw(record.u8()?);
    let hatch_cycles = record.u8()?;
    let base_friendship = record.u8()?;
    let growth_rate = GrowthRate::from_raw(record.u8()?);
    let egg_groups = [
        EggGroup::from_raw(record.u8()?),
        EggGroup::from_raw(record.u8()?),
    ];
    let ability = |raw: u8| (raw != 0).then_some(AbilityId(raw));
    let abilities = [ability(record.u8()?), ability(record.u8()?)];
    let safari_flee_rate = record.u8()?;
    let color_and_flip = record.u8()?;
    record.skip(2)?;
    let machine_bits = u128::from_le_bytes(record.array()?);
    Ok(Species {
        id: SpeciesId(index as u16),
        name: None,
        types,
        base_stats,
        ev_yield: Stats {
            hp: ev(0),
            attack: ev(1),
            defense: ev(2),
            speed: ev(3),
            special_attack: ev(4),
            special_defense: ev(5),
        },
        abilities,
        catch_rate,
        base_experience,
        base_friendship,
        growth_rate,
        gender_ratio,
        egg_groups,
        hatch_cycles,
        held_items,
        safari_flee_rate,
        color: Color::from_raw(color_and_flip & 0x7F),
        flip_sprite: color_and_flip & 0x80 != 0,
        machines: (0..128)
            .filter(|bit| machine_bits >> bit & 1 != 0)
            .collect(),
        evolutions: Vec::new(),
        level_up_moves: Vec::new(),
    })
}

/// Up to seven `(method, parameter, species)` entries; method 0 marks an empty slot.
fn parse_evolutions(bytes: &[u8]) -> Result<Vec<Evolution>> {
    let mut record = Reader::new("Gen 4 evolution record", bytes);
    if bytes.len() != EVOLUTION_LEN {
        return Err(record.error_at(0, "record is not 44 bytes"));
    }
    let mut evolutions = Vec::new();
    for _ in 0..EVOLUTION_SLOTS {
        let (method, parameter, into) = (record.u16()?, record.u16()?, record.u16()?);
        if method != 0 {
            evolutions.push(Evolution {
                method: EvolutionMethod::decode(method, parameter),
                into: SpeciesId(into),
            });
        }
    }
    Ok(evolutions)
}

/// 16-bit entries, `level << 9 | move`, up to a `0xFFFF` terminator.
fn parse_learnset(bytes: &[u8]) -> Result<Vec<LevelUpMove>> {
    let mut entries = Reader::new("Gen 4 learnset", bytes);
    let mut moves = Vec::new();
    loop {
        match entries.u16()? {
            LEARNSET_END => return Ok(moves),
            entry => moves.push(LevelUpMove {
                level: (entry >> 9) as u8,
                r#move: MoveId(entry & 0x1FF),
            }),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Ability {
    pub id: AbilityId,
    pub name: String,
    pub description: Option<String>,
    /// The species and forms that can have it.
    pub species: Vec<SpeciesId>,
}

/// One ability per name, but the placeholder at index 0.
pub(super) fn abilities(
    species: &[Species],
    names: &[String],
    descriptions: &[String],
) -> Vec<Ability> {
    let mut holders: BTreeMap<AbilityId, Vec<SpeciesId>> = BTreeMap::new();
    for species in species {
        for ability in species.abilities.iter().flatten() {
            let holders = holders.entry(*ability).or_default();
            if holders.last() != Some(&species.id) {
                holders.push(species.id);
            }
        }
    }
    (1..names.len().min(u8::MAX as usize + 1))
        .map(|index| {
            let id = AbilityId(index as u8);
            Ability {
                id,
                name: names[index].clone(),
                description: descriptions.get(index).cloned(),
                species: holders.remove(&id).unwrap_or_default(),
            }
        })
        .collect()
}

/// How an evolution is triggered, with the parameter the method uses.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(tag = "method", rename_all = "snake_case")]
pub enum EvolutionMethod {
    /// Level up with high friendship.
    Friendship,
    FriendshipDay,
    FriendshipNight,
    Level {
        level: u16,
    },
    Trade,
    TradeHolding {
        item: ItemId,
    },
    UseItem {
        item: ItemId,
    },
    /// Level up with Attack greater than Defense.
    LevelAttackGreater {
        level: u16,
    },
    LevelAttackEqual {
        level: u16,
    },
    LevelAttackLess {
        level: u16,
    },
    /// Wurmple into Silcoon, decided by its personality value.
    LevelPersonalityLow {
        level: u16,
    },
    /// Wurmple into Cascoon.
    LevelPersonalityHigh {
        level: u16,
    },
    /// Nincada into Ninjask.
    LevelNinjask {
        level: u16,
    },
    /// Nincada into Shedinja, given a free party slot.
    LevelShedinja {
        level: u16,
    },
    Beauty {
        beauty: u16,
    },
    UseItemMale {
        item: ItemId,
    },
    UseItemFemale {
        item: ItemId,
    },
    HoldItemDay {
        item: ItemId,
    },
    HoldItemNight {
        item: ItemId,
    },
    KnowsMove {
        r#move: MoveId,
    },
    SpeciesInParty {
        species: SpeciesId,
    },
    LevelMale {
        level: u16,
    },
    LevelFemale {
        level: u16,
    },
    /// Level up at Mt. Coronet.
    MagneticField,
    /// Level up near the Moss Rock.
    MossRock,
    /// Level up near the Ice Rock.
    IceRock,
    /// A method without a known meaning, kept as stored.
    Unknown {
        raw_method: u16,
        parameter: u16,
    },
}

impl EvolutionMethod {
    pub fn decode(method: u16, parameter: u16) -> Self {
        let (level, item) = (parameter, ItemId(parameter));
        match method {
            1 => Self::Friendship,
            2 => Self::FriendshipDay,
            3 => Self::FriendshipNight,
            4 => Self::Level { level },
            5 => Self::Trade,
            6 => Self::TradeHolding { item },
            7 => Self::UseItem { item },
            8 => Self::LevelAttackGreater { level },
            9 => Self::LevelAttackEqual { level },
            10 => Self::LevelAttackLess { level },
            11 => Self::LevelPersonalityLow { level },
            12 => Self::LevelPersonalityHigh { level },
            13 => Self::LevelNinjask { level },
            14 => Self::LevelShedinja { level },
            15 => Self::Beauty { beauty: parameter },
            16 => Self::UseItemMale { item },
            17 => Self::UseItemFemale { item },
            18 => Self::HoldItemDay { item },
            19 => Self::HoldItemNight { item },
            20 => Self::KnowsMove {
                r#move: MoveId(parameter),
            },
            21 => Self::SpeciesInParty {
                species: SpeciesId(parameter),
            },
            22 => Self::LevelMale { level },
            23 => Self::LevelFemale { level },
            24 => Self::MagneticField,
            25 => Self::MossRock,
            26 => Self::IceRock,
            raw_method => Self::Unknown {
                raw_method,
                parameter,
            },
        }
    }
}

/// Which genders a species comes in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum GenderRatio {
    MaleOnly,
    FemaleOnly,
    Genderless,
    /// Female when the low byte of the personality value is below the threshold: 31 is 12.5%
    /// female, 127 is 50%, 254 would be all.
    Mixed {
        female_threshold: u8,
    },
}

impl GenderRatio {
    pub const fn from_raw(raw: u8) -> Self {
        match raw {
            0 => Self::MaleOnly,
            254 => Self::FemaleOnly,
            255 => Self::Genderless,
            female_threshold => Self::Mixed { female_threshold },
        }
    }
}

raw_enum! {
    pub enum GrowthRate: u8 {
        MediumFast = 0,
        Erratic = 1,
        Fluctuating = 2,
        MediumSlow = 3,
        Fast = 4,
        Slow = 5,
    }
}

raw_enum! {
    pub enum EggGroup: u8 {
        Monster = 1,
        Water1 = 2,
        Bug = 3,
        Flying = 4,
        Field = 5,
        Fairy = 6,
        Grass = 7,
        HumanLike = 8,
        Water3 = 9,
        Mineral = 10,
        Amorphous = 11,
        Water2 = 12,
        Ditto = 13,
        Dragon = 14,
        Undiscovered = 15,
    }
}

raw_enum! {
    /// The Pokédex search color.
    pub enum Color: u8 {
        Red = 0,
        Blue = 1,
        Yellow = 2,
        Green = 3,
        Black = 4,
        Brown = 5,
        Purple = 6,
        Gray = 7,
        White = 8,
        Pink = 9,
        /// The Egg and the Bad Egg.
        Egg = 10,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn learnsets_unpack_level_and_move_up_to_the_terminator() {
        let bytes = [(1u16 << 9 | 33), (3 << 9 | 45), LEARNSET_END, 0]
            .map(u16::to_le_bytes)
            .concat();
        let moves = parse_learnset(&bytes).unwrap();
        assert_eq!(
            moves[1],
            LevelUpMove {
                level: 3,
                r#move: MoveId(45)
            }
        );
        assert_eq!(moves.len(), 2);
        assert!(parse_learnset(&bytes[..4]).is_err());
    }

    #[test]
    fn evolutions_serialize_flat_with_their_parameter() {
        let evolution = Evolution {
            method: EvolutionMethod::decode(7, 84),
            into: SpeciesId(134),
        };
        assert_eq!(
            serde_json::to_value(evolution).unwrap(),
            serde_json::json!({"method": "use_item", "item": 84, "into": 134})
        );
        assert_eq!(
            EvolutionMethod::decode(99, 1),
            EvolutionMethod::Unknown {
                raw_method: 99,
                parameter: 1
            }
        );
    }
}