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
use item::{Item, ItemType, ItemInfluence};
use std::collections::HashMap;
use inventory::Inventory;
use types::{Health, AttributeValue};

/// The influence the `Attribute::Dexterity` has on the attack_damage of the character
const DEXTERITY_INFLUENCE: f64 = 0.2;

/// The character the player is impersonating
pub struct Character {
    name: String,
    health: Health,
    attributes: HashMap<Attribute, AttributeValue>,
    armor_slot_head: Option<Item>,
    armor_slot_chest: Option<Item>,
    armor_slot_legs: Option<Item>,
    armor_slot_feet: Option<Item>,
    weapon_slot_left: Option<Item>,
    weapon_slot_right: Option<Item>,
    inventory: Inventory,
}

impl Character {
    /// Creates a new instance of `Character`
    pub fn new(name: &str) -> Character {
        let attribute_map = Self::default_attributes();
        Character {
            name: name.to_owned(),
            health: (&attribute_map)[&Attribute::Constitution] as Health,
            attributes: attribute_map,
            armor_slot_head: None,
            armor_slot_chest: None,
            armor_slot_legs: None,
            armor_slot_feet: None,
            weapon_slot_left: None,
            weapon_slot_right: None,
            inventory: Inventory::new(30),
        }
    }

    /// Updates the given attribute
    pub fn update_attribute(&mut self, attribute: &Attribute, value: AttributeValue) {
        *self.attributes.get_mut(attribute).unwrap() = value;
    }

    /// Calculates and returns the current attack damage of the character based on the attibutes
    pub fn attack_damage(&self) -> AttributeValue {
        let base_dexterity = self.attributes
            .get(&Attribute::Dexterity)
            .expect("Unable to find attribute: Attribute::Dexterity");

        let base_dexterity = ((*base_dexterity as f64) * DEXTERITY_INFLUENCE) as AttributeValue;

        let base_strength = self.attributes
            .get(&Attribute::Strength)
            .expect("Unable to find attribute: Attribute::Strength");

        let mut additional_damage: i64 = 0;
        if let Some(ref inner_item) = self.weapon_slot_left {
            if let Some(ItemInfluence { ref attribute, ref amount }) = inner_item.influence {
                let influence = if attribute == &Attribute::Dexterity {
                    DEXTERITY_INFLUENCE
                } else {
                    1_f64
                };

                additional_damage += ((*amount as f64) * influence) as i64;
            }
        }

        if let Some(ref inner_item) = self.weapon_slot_right {
            if let Some(ItemInfluence { ref attribute, ref amount }) = inner_item.influence {
                let influence = if attribute == &Attribute::Dexterity {
                    DEXTERITY_INFLUENCE
                } else {
                    1_f64
                };

                additional_damage += ((*amount as f64) * influence) as i64;
            }

        }

        base_strength + base_dexterity + additional_damage
    }

    /// Returns the value of the specified attribute
    pub fn get_attribute_value(&self, attribute: &Attribute) -> AttributeValue {
        *self.attributes.get(attribute).unwrap()
    }

    /// A setter method for the head armor slot. **Panics** whether the given item is not
    /// of type `ItemType::ArmorHead`
    pub fn set_armor_slot_head(&mut self, item: Option<Item>) {
        if let Some(ref inner_item) = item {
            assert_eq!(inner_item.item_type, ItemType::ArmorHead);
        }

        self.armor_slot_head = item;
    }

    /// A setter method for the chest armor slot. **Panics** whether the given item is not
    /// of type `ItemType::ArmorChest`
    pub fn set_armor_slot_chest(&mut self, item: Option<Item>) {
        if let Some(ref inner_item) = item {
            assert_eq!(inner_item.item_type, ItemType::ArmorChest);
        }

        self.armor_slot_chest = item;
    }

    /// A setter method for the legs armor slot. **Panics** whether the given item is not
    /// of type `ItemType::ArmorLegs`
    pub fn set_armor_slot_legs(&mut self, item: Option<Item>) {
        if let Some(ref inner_item) = item {
            assert_eq!(inner_item.item_type, ItemType::ArmorLegs);
        }

        self.armor_slot_legs = item;
    }

    /// A setter method for the feet armor slot. **Panics** whether the given item is not
    /// of type `ItemType::ArmorFeet`
    pub fn set_armor_slot_feet(&mut self, item: Option<Item>) {
        if let Some(ref inner_item) = item {
            assert_eq!(inner_item.item_type, ItemType::ArmorFeet);
        }

        self.armor_slot_feet = item;
    }

    /// A setter method for the right weapon slot
    pub fn set_weapon_slot_right(&mut self, item: Option<Item>) {
        self.weapon_slot_right = item;
    }

    /// A setter method for the left weapon slot
    pub fn set_weapon_slot_left(&mut self, item: Option<Item>) {
        self.weapon_slot_left = item;
    }

    /// Returns the default attributes for a character
    pub fn default_attributes() -> HashMap<Attribute, AttributeValue> {
        let mut attribute_map = HashMap::new();

        attribute_map.insert(Attribute::Charisma, 5);
        attribute_map.insert(Attribute::Constitution, 30);
        attribute_map.insert(Attribute::Defense, 15);
        attribute_map.insert(Attribute::Dexterity, 10);
        attribute_map.insert(Attribute::Intelligence, 5);
        attribute_map.insert(Attribute::Luck, 0);
        attribute_map.insert(Attribute::Perception, 10);
        attribute_map.insert(Attribute::Strength, 20);
        attribute_map.insert(Attribute::Willpower, 15);
        attribute_map.insert(Attribute::Wisdom, 5);

        attribute_map
    }
}

/// A list of all possible attributes
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub enum Attribute {
    /// The charisma of a character
    Charisma,
    /// The constitution of a character
    Constitution,
    /// The defense of a character
    Defense,
    /// The dexterity of a character
    Dexterity,
    /// The intelligence of a character
    Intelligence,
    /// The luck of a character
    Luck,
    /// The perception of a character
    Perception,
    /// The strength of a character
    Strength,
    /// The willpower of a character
    Willpower,
    /// The wisdom of a character
    Wisdom,
}


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

    use item_generator;
    use item::{ItemType, ItemInfluence};

    #[test]
    fn set_armor_slot_head() {
        let mut character = Character::new("TestCharacter");

        assert_eq!(character.armor_slot_head, None);

        let head_piece = item_generator::ItemGenerator::new().item_type(ItemType::ArmorHead).gen();
        let head_piece_clone = head_piece.clone();

        character.set_armor_slot_head(Some(head_piece));

        assert_eq!(character.armor_slot_head, Some(head_piece_clone));
    }

    #[test]
    fn set_armor_slot_chest() {
        let mut character = Character::new("TestCharacter");

        assert_eq!(character.armor_slot_chest, None);

        let chest_piece =
            item_generator::ItemGenerator::new().item_type(ItemType::ArmorChest).gen();
        let chest_piece_clone = chest_piece.clone();

        character.set_armor_slot_chest(Some(chest_piece));

        assert_eq!(character.armor_slot_chest, Some(chest_piece_clone));
    }

    #[test]
    fn set_armor_slot_legs() {
        let mut character = Character::new("TestCharacter");

        assert_eq!(character.armor_slot_legs, None);

        let legs_piece = item_generator::ItemGenerator::new().item_type(ItemType::ArmorLegs).gen();
        let legs_piece_clone = legs_piece.clone();

        character.set_armor_slot_legs(Some(legs_piece));

        assert_eq!(character.armor_slot_legs, Some(legs_piece_clone));
    }

    #[test]
    fn set_armor_slot_feet() {
        let mut character = Character::new("TestCharacter");

        assert_eq!(character.armor_slot_feet, None);

        let shoes_piece = item_generator::ItemGenerator::new().item_type(ItemType::ArmorFeet).gen();
        let shoes_piece_clone = shoes_piece.clone();

        character.set_armor_slot_feet(Some(shoes_piece));

        assert_eq!(character.armor_slot_feet, Some(shoes_piece_clone));
    }

    #[test]
    fn set_weapon_slot_right() {
        let mut character = Character::new("TestCharacter");

        assert_eq!(character.weapon_slot_right, None);

        let weapon = item_generator::ItemGenerator::new().item_type(ItemType::WeaponHammer).gen();
        let weapon_clone = weapon.clone();

        character.set_weapon_slot_right(Some(weapon));

        assert_eq!(character.weapon_slot_right, Some(weapon_clone));
    }

    #[test]
    fn set_weapon_slot_left() {
        let mut character = Character::new("TestCharacter");

        assert_eq!(character.weapon_slot_left, None);

        let weapon = item_generator::ItemGenerator::new().item_type(ItemType::WeaponSword).gen();
        let weapon_clone = weapon.clone();

        character.set_weapon_slot_left(Some(weapon));

        assert_eq!(character.weapon_slot_left, Some(weapon_clone));
    }

    #[test]
    fn attribute_mutation() {
        let mut character = Character::new("Wil Wheaton");

        character.update_attribute(&Attribute::Dexterity, 42);

        assert_eq!(character.get_attribute_value(&Attribute::Dexterity), 42);
    }

    #[test]
    fn basic_attack_damage() {
        let character = Character::new("Wil Wheaton");

        // 22 is the very basic attack damage
        assert_eq!(character.attack_damage(), 22);
    }

    #[test]
    fn attack_damage_with_weapons() {
        let mut character = Character::new("Wil Wheaton");

        let weapon = item_generator::ItemGenerator::new()
            .item_type(ItemType::WeaponSword)
            .influence(Some(ItemInfluence::new(Attribute::Strength, 10)))
            .gen();

        character.set_weapon_slot_left(Some(weapon.clone()));
        character.set_weapon_slot_right(Some(weapon.clone()));

        assert_eq!(character.attack_damage(), 42);
    }
}