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
use std::collections::HashMap;
use std::convert::{From, Into};
use std::fmt;
use std::ops::RangeInclusive;

use serde::{de, Deserialize, Deserializer};

use crate::dofapi::effect::Element;

//   ____                    _____
//  / ___|__ _ _ __ __ _  __|_   _|   _ _ __   ___
// | |   / _` | '__/ _` |/ __|| || | | | '_ \ / _ \
// | |__| (_| | | | (_| | (__ | || |_| | |_) |  __/
//  \____\__,_|_|  \__,_|\___||_| \__, | .__/ \___|
//                                |___/|_|

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum CaracKind {
    AP,
    APReduction,
    APResistance,
    Critical,
    CriticalDamage,
    CriticalResistance,
    Damage(Element),
    Dodge,
    Heals,
    Initiative,
    Lock,
    MP,
    MPReduction,
    MPResistance,
    PerMeleeDamage,
    PerMeleeResistance,
    PerRangedDamage,
    PerRangedResistance,
    PerResistance(Element),
    PerSpellDamage,
    PerWeaponDamage,
    Pods,
    Power,
    Prospecting,
    PushbackDamage,
    PushbackResistance,
    Range,
    RawDamage,
    ReflectDamage,
    Resistance(Element),
    Special(String),
    Stats(Element),
    Summons,
    TrapDamage,
    TrapPower,
    Vitality,
    Wisdom,
}

impl CaracKind {
    pub fn smithmage_weight(&self) -> Result<f64, ()> {
        use CaracKind::*;

        if let Special(_) = self {
            Err(())
        } else {
            Ok(match self {
                Vitality => 1. / 5.,
                Stats(_) => 1.,
                Initiative => 1. / 10.,
                Wisdom => 3.,
                Prospecting => 3.,
                Power => 2.,
                Resistance(_) => 2.,
                PerResistance(_) => 6.,
                PushbackResistance | CriticalResistance => 2.,
                APResistance | APReduction | MPReduction | MPResistance => 7.,
                Pods => 2.5 / 10.,
                Lock | Dodge => 4.,
                RawDamage => 20.,
                Damage(_) | CriticalDamage | PushbackDamage | TrapDamage => 5.,
                PerMeleeDamage | PerMeleeResistance | PerRangedDamage
                | PerRangedResistance | PerWeaponDamage | PerSpellDamage => {
                    15.
                }
                TrapPower => 2.,
                Heals | Critical | ReflectDamage => 10.,
                Summons => 30.,
                Range => 50.,
                MP => 90.,
                AP => 100.,
                Special(_) => unreachable!(),
            })
        }
    }
}

impl From<&str> for CaracKind {
    fn from(from: &str) -> Self {
        use CaracKind::*;
        match from {
            "Agility" => Stats(Element::Air),
            "Air Damage" => Damage(Element::Air),
            "% Air Resistance" => PerResistance(Element::Air),
            "Air Resistance" => Resistance(Element::Air),
            "AP" => AP,
            "AP Reduction" => APReduction,
            "AP Resistance" | "AP Parry" => APResistance,
            "Chance" => Stats(Element::Water),
            "% Critical" | "Critical" => Critical,
            "Critical Damage" => CriticalDamage,
            "Critical Resistance" => CriticalResistance,
            "Damage" => RawDamage,
            "Dodge" => Dodge,
            "Earth Damage" => Damage(Element::Earth),
            "% Earth Resistance" => PerResistance(Element::Earth),
            "Earth Resistance" => Resistance(Element::Earth),
            "Fire Damage" => Damage(Element::Fire),
            "% Fire Resistance" => PerResistance(Element::Fire),
            "Fire Resistance" => Resistance(Element::Fire),
            "Heals" => Heals,
            "Initiative" => Initiative,
            "Intelligence" => Stats(Element::Fire),
            "Lock" => Lock,
            "% Melee Damage" => PerMeleeDamage,
            "% Melee Resistance" => PerMeleeResistance,
            "MP" => MP,
            "MP Reduction" => MPReduction,
            "MP Resistance" | "MP Parry" => MPResistance,
            "Neutral Damage" => Damage(Element::Neutral),
            "% Neutral Resistance" => PerResistance(Element::Neutral),
            "Neutral Resistance" => Resistance(Element::Neutral),
            "Pods" => Pods,
            "Power" => Power,
            "Power (traps)" => TrapPower,
            "Prospecting" => Prospecting,
            "Pushback Damage" => PushbackDamage,
            "Pushback Resistance" => PushbackResistance,
            "% Ranged Damage" => PerRangedDamage,
            "% Ranged Resistance" => PerRangedResistance,
            "Range" => Range,
            "Reflects  damage" => ReflectDamage,
            "% Spell Damage" => PerSpellDamage,
            "Strength" => Stats(Element::Earth),
            "Summons" => Summons,
            "Trap Damage" => TrapDamage,
            "Vitality" => Vitality,
            "Water Damage" => Damage(Element::Water),
            "% Water Resistance" => PerResistance(Element::Water),
            "Water Resistance" => Resistance(Element::Water),
            "% Weapon Damage" => PerWeaponDamage,
            "Wisdom" => Wisdom,
            special => Special(String::from(special)),
        }
    }
}

impl fmt::Display for CaracKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CaracKind::Damage(element) => {
                format!("{:?} damage", element).fmt(f)
            }
            CaracKind::PerResistance(element) => {
                format!("% {:?} resistance", element).fmt(f)
            }
            CaracKind::Resistance(element) => {
                format!("{:?} resistance", element).fmt(f)
            }
            CaracKind::Special(s) => s.fmt(f),
            CaracKind::Stats(element) => (match element {
                Element::Air => "Agility",
                Element::Earth => "Force",
                Element::Fire => "Intelligence",
                Element::Water => "Chance",
                Element::Neutral => {
                    panic!("Invalid `Neutral` element for stats")
                }
            })
            .fmt(f),
            _ => format!("{:?}", self).fmt(f),
        }
    }
}

//  ____                      _       _ _
// |  _ \  ___  ___  ___ _ __(_) __ _| (_)_______
// | | | |/ _ \/ __|/ _ \ '__| |/ _` | | |_  / _ \
// | |_| |  __/\__ \  __/ |  | | (_| | | |/ /  __/
// |____/ \___||___/\___|_|  |_|\__,_|_|_/___\___|
//

struct CaracKindVisitor;

impl<'de> Deserialize<'de> for CaracKind {
    fn deserialize<D>(deserializer: D) -> Result<CaracKind, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(CaracKindVisitor)
    }
}

impl<'de> de::Visitor<'de> for CaracKindVisitor {
    type Value = CaracKind;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("A sequence of item characteristics")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(v.into())
    }
}

// Deserializer for item line

#[derive(Clone, Debug)]
pub struct CaracLines(HashMap<CaracKind, RangeInclusive<i16>>);

impl CaracLines {
    /// Use the carac lines as a map
    pub fn as_map(&self) -> &HashMap<CaracKind, RangeInclusive<i16>> {
        let Self(map) = self;
        map
    }

    /// Check if this item's statistics are greater than or equal to another
    /// item. This is essentialy usefull to fix trophy conditions.
    pub fn is_stronger_than(&self, other: &Self) -> bool {
        other.as_map().iter().all(|(kind, other_bounds)| {
            // Check if all stats of `other` are covered by this item.
            self.as_map()
                .get(kind)
                .map(|self_bounds| self_bounds.start())
                .unwrap_or(&0)
                >= other_bounds.end()
        }) && self.as_map().iter().all(|(kind, self_bounds)| {
            // Check if all stats of this item are covered by `other`.
            // This is required since there may be some negative values in this
            // item that are not in `other`.
            other
                .as_map()
                .get(kind)
                .map(|other_bounds| other_bounds.end())
                .unwrap_or(&0)
                <= self_bounds.start()
        })
    }
}

impl Default for CaracLines {
    fn default() -> Self {
        CaracLines(HashMap::new())
    }
}

impl From<HashMap<CaracKind, RangeInclusive<i16>>> for CaracLines {
    fn from(map: HashMap<CaracKind, RangeInclusive<i16>>) -> Self {
        Self(map)
    }
}

#[allow(clippy::implicit_hasher)]
impl From<CaracLines> for HashMap<CaracKind, RangeInclusive<i16>> {
    fn from(caracs: CaracLines) -> Self {
        let CaracLines(map) = caracs;
        map
    }
}

struct CaracLinesVisitor;

impl<'de> Deserialize<'de> for CaracLines {
    fn deserialize<D>(deserializer: D) -> Result<CaracLines, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(CaracLinesVisitor)
    }
}

impl<'de> de::Visitor<'de> for CaracLinesVisitor {
    type Value = CaracLines;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("A sequence of item characteristics")
    }

    fn visit_seq<D>(self, mut access: D) -> Result<Self::Value, D::Error>
    where
        D: de::SeqAccess<'de>,
    {
        let mut ret = HashMap::new();

        #[derive(Deserialize)]
        struct Bounds {
            min: Option<i16>,
            max: Option<i16>,
        }

        #[derive(Deserialize)]
        #[serde(untagged)]
        enum ItemLine {
            Carac(HashMap<CaracKind, Bounds>),
            Emote { emote: String },
            Title { title: String },
        }

        while let Some(line) = access.next_element()? {
            let line: ItemLine = line;

            match line {
                ItemLine::Carac(carac) => {
                    let (carac_kind, bounds) = carac
                        .into_iter()
                        .next()
                        .expect("Invalid empty item line");

                    let min = bounds.min.unwrap_or(0);
                    let max = bounds.max.unwrap_or(min);

                    ret.insert(carac_kind, min..=max);
                }
                ItemLine::Emote { emote: _s } => (),
                ItemLine::Title { title: _s } => (),
            }
        }

        Ok(CaracLines(ret))
    }
}