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
//! Data structures and logic about the DMW2 monsters and families

use std::cell::RefCell;
use std::rc::Rc;

use quick_xml::events::BytesStart;
use serde::Serialize;

use error::Error;
use crate::xml::{self, getTagAttr};

/// A location in a map
#[derive(Clone, Serialize)]
pub struct MapLocation
{
    /// Name of the map. Example: Oasis Key World.
    pub map: String,
    /// More detailed location within the map.
    pub desc: Option<String>,
}

impl MapLocation
{
    /// Parse the location tag. A location tag looks like this:
    ///
    /// ```xml
    /// <location>
    ///   <map>Oasis Key World</map>
    ///   <description>Near Door Shrine</description>
    /// </location>
    /// ```
    fn fromXML(x: &[u8]) -> Result<Self, Error>
    {
        let mut map = String::new();
        let mut desc = String::new();
        let mut p = xml::Parser::new();
        p.addTextHandler("map", |_, text| {
            map = text.to_owned();
            Ok(())
        });
        p.addTextHandler("description", |_, text| {
            desc = text.to_owned();
            Ok(())
        });
        p.parse(x)?;
        drop(p);
        Ok(Self {
            map,
            desc: if desc.is_empty() { None } else { Some(desc) }
        })
    }
}

/// Monster growth data
#[derive(Default, Clone, Serialize)]
pub struct Growth
{
    pub agility: u8,
    pub intelligence: u8,
    pub max_level: u8,
    pub attack: u8,
    pub defense: u8,
    pub mp: u8,
    pub exp: u8,
    pub hp: u8,
}

impl Growth
{
    fn fromXMLTag(tag: &BytesStart) -> Result<Self, Error>
    {
        let agility: u8 =  xml::getTagAttr(tag, "agl")?.ok_or_else(
            || xmlerr!("Agility value not found"))?;
        let intelligence: u8 =  xml::getTagAttr(tag, "int")?.ok_or_else(
            || xmlerr!("Inteligence value not found"))?;
        let max_level: u8 =  xml::getTagAttr(tag, "maxlvl")?.ok_or_else(
            || xmlerr!("Max level value not found"))?;
        let attack: u8 =  xml::getTagAttr(tag, "atk")?.ok_or_else(
            || xmlerr!("Attack value not found"))?;
        let defense: u8 =  xml::getTagAttr(tag, "def")?.ok_or_else(
            || xmlerr!("Defense value not found"))?;
        let mp: u8 =  xml::getTagAttr(tag, "mp")?.ok_or_else(
            || xmlerr!("MP value not found"))?;
        let exp: u8 =  xml::getTagAttr(tag, "exp")?.ok_or_else(
            || xmlerr!("EXP value not found"))?;
        let hp: u8 =  xml::getTagAttr(tag, "hp")?.ok_or_else(
            || xmlerr!("HP value not found"))?;
        Ok(Self {
            agility, intelligence, max_level, attack, defense, mp, exp, hp,
        })
    }
}


/// The info of a monster
#[derive(Clone, Serialize)]
pub struct Monster
{
    /// Name of the monster
    pub name: String,
    /// Whether the monster can be find in the story maps
    pub in_story: bool,
    /// Spawn location of the monster. Only monsters with `in_story ==
    /// true` has this.
    pub locations: Vec<MapLocation>,
    /// Natural skills of the monster
    pub skills: Vec<String>,
    /// Monster growth data
    pub growth: Growth,
    /// Family the monster belongs to
    pub family: String,
}

impl Monster
{
    fn fromXML(x: &[u8], family: String) -> Result<Self, Error>
    {
        let mut name = String::new();
        let mut in_story = false;
        let mut locations = Vec::new();
        let mut skills = Vec::new();
        let mut growth = Growth::default();

        let mut p = xml::Parser::new();
        p.addBeginHandler("monster", |_, tag| {
            name = xml::getTagAttr(tag, "name")?.ok_or_else(
                || xmlerr!("Monster name value not found"))?;
            in_story = xml::getTagAttr(tag, "in_story")?.ok_or_else(
                || xmlerr!("In_story value not found"))?;
            Ok(())
        });
        p.addTagHandler("location", |_, tag| {
            locations.push(MapLocation::fromXML(tag)?);
            Ok(())
        });
        p.addTextHandler("skill", |_, text| {
            skills.push(text.to_owned());
            Ok(())
        });
        p.addBeginHandler("growth", |_, tag| {
            growth = Growth::fromXMLTag(tag)?;
            Ok(())
        });
        p.parse(x)?;
        drop(p);

        Ok(Self { name, in_story, locations, skills, growth, family})
    }
}

/// A family of monsters
#[derive(Default, Clone, Serialize)]
pub struct Family
{
    /// Name of the family in *lower case*.
    pub name: String,
    /// List of name of monsters in this family.
    pub members: Vec<String>,
}

/// Data about a set of monsters and families.
#[derive(Default, Clone, Serialize)]
pub struct Info
{
    /// A list of monsters
    pub monsters: Vec<Monster>,
    /// A list of families
    pub families: Vec<Family>,
}

impl Info
{
    pub fn fromXML(x: &[u8]) -> Result<Self, Error>
    {
        let family = Rc::new(RefCell::new(Family::default()));
        let mut monsters = Vec::new();
        let mut families = Vec::new();

        let mut p = xml::Parser::new();
        p.addBeginHandler("family", |_, tag| {
            family.borrow_mut().name = getTagAttr(tag, "name")?
                .ok_or_else(|| xmlerr!("Family name not found"))?;
            Ok(())
        });
        p.addEndHandler("family", |_, _| {
            families.push(family.take());
            *family.borrow_mut() = Family::default();
            Ok(())
        });
        p.addTagHandler("monster", |_, tag| {
            let monster = Monster::fromXML(tag, family.borrow().name.clone())?;
            family.borrow_mut().members.push(monster.name.clone());
            monsters.push(monster);
            Ok(())
        });

        p.parse(x)?;
        drop(p);
        Ok(Self { monsters, families })
    }
}