Skip to main content

battler_data/mons/
evolution.rs

1use alloc::string::String;
2
3use serde::{
4    Deserialize,
5    Serialize,
6};
7use serde_string_enum::{
8    DeserializeLabeledStringEnum,
9    SerializeLabeledStringEnum,
10};
11
12use crate::Gender;
13
14/// Common evolution methods.
15///
16/// This enum is encoded as a single character: `L`, `T`, `I`, `B`, or `C`.
17#[derive(Debug, Clone, PartialEq, SerializeLabeledStringEnum, DeserializeLabeledStringEnum)]
18pub enum EvolutionMethod {
19    /// Evolves on level-up.
20    #[string = "L"]
21    #[alias = "Level"]
22    Level,
23    /// Evolves after being traded.
24    #[string = "T"]
25    #[alias = "Trade"]
26    Trade,
27    /// Evolves on item use outside of battle.
28    #[string = "I"]
29    #[alias = "Item"]
30    Item,
31    /// Evolves after a battle ends, regardless of if a level up occurred.
32    #[string = "B"]
33    #[alias = "Battle"]
34    BattleEnd,
35    /// Evolves in some other custom way.
36    #[string = "C"]
37    #[alias = "Custom"]
38    Custom,
39}
40
41/// Details and conditions for one species to evolve into another.
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct EvolutionData {
44    /// The evolution method, which determines when the rest of the conditions should be checked.
45    pub method: EvolutionMethod,
46    /// A string detailing how the species evolves.
47    pub condition: String,
48    /// Minimum level that must be reached for evolution.
49    pub level: Option<u8>,
50    /// Does the species require high friendship?
51    ///
52    /// High friendship is defined as a value of at least 220.
53    pub friendship: Option<bool>,
54    /// Move that must be present on the Mon's moveset.
55    pub knows_move: Option<String>,
56    /// Time of day where evolution occurs
57    pub time_of_day: Option<String>,
58    /// Item held by the Mon.
59    pub holding_item: Option<String>,
60    /// Gender of the Mon.
61    pub gender: Option<Gender>,
62    /// Item that must be used on the Mon.
63    pub use_item: Option<String>,
64    /// The species that this Mon was traded for.
65    pub trade_for: Option<String>,
66}
67
68#[cfg(test)]
69mod evolution_method_test {
70    use crate::{
71        mons::EvolutionMethod,
72        test_util::{
73            test_string_deserialization,
74            test_string_serialization,
75        },
76    };
77
78    #[test]
79    fn serializes_to_string() {
80        test_string_serialization(EvolutionMethod::Level, "L");
81        test_string_serialization(EvolutionMethod::Trade, "T");
82        test_string_serialization(EvolutionMethod::Item, "I");
83        test_string_serialization(EvolutionMethod::BattleEnd, "B");
84        test_string_serialization(EvolutionMethod::Custom, "C");
85    }
86
87    #[test]
88    fn deserializes_full_name() {
89        test_string_deserialization("Level", EvolutionMethod::Level);
90        test_string_deserialization("Trade", EvolutionMethod::Trade);
91        test_string_deserialization("Item", EvolutionMethod::Item);
92        test_string_deserialization("Battle", EvolutionMethod::BattleEnd);
93        test_string_deserialization("Custom", EvolutionMethod::Custom);
94    }
95}