veloren_common/comp/skillset/
mod.rs

1use crate::{
2    assets::{self, Asset, AssetExt},
3    comp::{item::tool::ToolKind, skills::Skill},
4};
5use core::borrow::{Borrow, BorrowMut};
6use hashbrown::HashMap;
7use lazy_static::lazy_static;
8use serde::{Deserialize, Serialize};
9use sha2::{Digest, Sha256};
10use specs::{Component, DerefFlaggedStorage};
11use std::{collections::BTreeSet, hash::Hash};
12use tracing::{trace, warn};
13
14pub mod skills;
15
16#[cfg(test)] mod test;
17
18/// BTreeSet is used here to ensure that skills are ordered. This is important
19/// to ensure that the hash created from it is consistent so that we don't
20/// needlessly force a respec when loading skills from persistence.
21#[derive(Clone, Debug, Serialize, Deserialize)]
22pub struct SkillTreeMap(HashMap<SkillGroupKind, BTreeSet<Skill>>);
23
24impl Asset for SkillTreeMap {
25    type Loader = assets::RonLoader;
26
27    const EXTENSION: &'static str = "ron";
28}
29
30pub struct SkillGroupDef {
31    pub skills: BTreeSet<Skill>,
32    pub total_skill_point_cost: u16,
33}
34
35#[derive(Clone, Debug, Serialize, Deserialize)]
36pub struct SkillLevelMap(HashMap<Skill, u16>);
37
38impl Asset for SkillLevelMap {
39    type Loader = assets::RonLoader;
40
41    const EXTENSION: &'static str = "ron";
42}
43
44/// Contains the prerequisite skills for each skill. It cannot currently detect
45/// cyclic dependencies, so if you modify the prerequisite map ensure that there
46/// are no cycles of prerequisites.
47#[derive(Clone, Debug, Serialize, Deserialize)]
48pub struct SkillPrerequisitesMap(HashMap<Skill, SkillPrerequisite>);
49
50impl Asset for SkillPrerequisitesMap {
51    type Loader = assets::RonLoader;
52
53    const EXTENSION: &'static str = "ron";
54}
55
56#[derive(Clone, Debug, Serialize, Deserialize)]
57pub struct SkillCostMap(HashMap<Skill, u16>);
58
59impl Asset for SkillCostMap {
60    type Loader = assets::RonLoader;
61
62    const EXTENSION: &'static str = "ron";
63}
64
65lazy_static! {
66    // Determines the skills that comprise each skill group.
67    //
68    // This data is used to determine which of a player's skill groups a
69    // particular skill should be added to when a skill unlock is requested.
70    pub static ref SKILL_GROUP_DEFS: HashMap<SkillGroupKind, SkillGroupDef> = {
71        let map = SkillTreeMap::load_expect_cloned(
72            "common.skill_trees.skills_skill-groups_manifest",
73        ).0;
74        map.iter().map(|(sgk, skills)|
75            (*sgk, SkillGroupDef { skills: skills.clone(),
76                total_skill_point_cost: skills
77                    .iter()
78                    .map(|skill| {
79                        let max_level = skill.max_level();
80                        (1..=max_level)
81                            .map(|level| skill.skill_cost(level))
82                            .sum::<u16>()
83                    })
84                    .sum()
85            })
86        )
87        .collect()
88    };
89    // Creates a hashmap for the reverse lookup of skill groups from a skill
90    pub static ref SKILL_GROUP_LOOKUP: HashMap<Skill, SkillGroupKind> = {
91        let map = SkillTreeMap::load_expect_cloned(
92            "common.skill_trees.skills_skill-groups_manifest",
93        ).0;
94        map.iter().flat_map(|(sgk, skills)| skills.iter().map(move |s| (*s, *sgk))).collect()
95    };
96    // Loads the maximum level that a skill can obtain
97    pub static ref SKILL_MAX_LEVEL: HashMap<Skill, u16> = {
98        SkillLevelMap::load_expect_cloned(
99            "common.skill_trees.skill_max_levels",
100        ).0
101    };
102    // Loads the prerequisite skills for a particular skill
103    pub static ref SKILL_PREREQUISITES: HashMap<Skill, SkillPrerequisite> = {
104        SkillPrerequisitesMap::load_expect_cloned(
105            "common.skill_trees.skill_prerequisites",
106        ).0
107    };
108    pub static ref SKILL_GROUP_HASHES: HashMap<SkillGroupKind, Vec<u8>> = {
109        let map = SkillTreeMap::load_expect_cloned(
110            "common.skill_trees.skills_skill-groups_manifest",
111        ).0;
112        let mut hashes = HashMap::new();
113        for (skill_group_kind, skills) in map.iter() {
114            let mut hasher = Sha256::new();
115            let json_input: Vec<_> = skills.iter().map(|skill| (*skill, skill.max_level())).collect();
116            let hash_input = serde_json::to_string(&json_input).unwrap_or_default();
117            hasher.update(hash_input.as_bytes());
118            let hash_result = hasher.finalize();
119            hashes.insert(*skill_group_kind, hash_result.iter().copied().collect());
120        }
121        hashes
122    };
123}
124
125#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize, Ord, PartialOrd)]
126pub enum SkillGroupKind {
127    General,
128    Weapon(ToolKind),
129}
130
131impl SkillGroupKind {
132    /// Gets the cost in experience of earning a skill point
133    /// Changing this is forward compatible with persistence and will
134    /// automatically force a respec for skill group kinds that are affected.
135    pub fn skill_point_cost(self, level: u16) -> u32 {
136        use std::f32::consts::E;
137        match self {
138            Self::Weapon(ToolKind::Sword | ToolKind::Axe | ToolKind::Hammer) => {
139                let level = level as f32;
140                ((400.0 * (level / (level + 20.0)).powi(2) + 5.0 * E.powf(0.025 * level))
141                    .min(u32::MAX as f32) as u32)
142                    .saturating_mul(25)
143            },
144            _ => {
145                const EXP_INCREMENT: f32 = 10.0;
146                const STARTING_EXP: f32 = 70.0;
147                const EXP_CEILING: f32 = 1000.0;
148                const SCALING_FACTOR: f32 = 0.125;
149                (EXP_INCREMENT
150                    * (EXP_CEILING
151                        / EXP_INCREMENT
152                        / (1.0
153                            + E.powf(-SCALING_FACTOR * level as f32)
154                                * (EXP_CEILING / STARTING_EXP - 1.0)))
155                        .floor()) as u32
156            },
157        }
158    }
159
160    /// Gets the total amount of skill points that can be spent in a particular
161    /// skill group
162    pub fn total_skill_point_cost(self) -> u16 {
163        if let Some(SkillGroupDef {
164            total_skill_point_cost,
165            ..
166        }) = SKILL_GROUP_DEFS.get(&self)
167        {
168            *total_skill_point_cost
169        } else {
170            0
171        }
172    }
173}
174
175/// A group of skills that have been unlocked by a player. Each skill group has
176/// independent exp and skill points which are used to unlock skills in that
177/// skill group.
178#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
179pub struct SkillGroup {
180    // Invariant should be maintained that this is the same kind as the key that the skill group is
181    // inserted into the skillset as.
182    pub skill_group_kind: SkillGroupKind,
183    // The invariant that (earned_exp >= available_exp) should not be violated
184    pub available_exp: u32,
185    pub earned_exp: u32,
186    // The invariant that (earned_sp >= available_sp) should not be violated
187    pub available_sp: u16,
188    pub earned_sp: u16,
189    // Used for persistence
190    pub ordered_skills: Vec<Skill>,
191}
192
193impl SkillGroup {
194    fn new(skill_group_kind: SkillGroupKind) -> SkillGroup {
195        SkillGroup {
196            skill_group_kind,
197            available_exp: 0,
198            earned_exp: 0,
199            available_sp: 0,
200            earned_sp: 0,
201            ordered_skills: Vec::new(),
202        }
203    }
204
205    /// Returns the amount of experience in a skill group that has been spent to
206    /// acquire skill points Relies on the invariant that (earned_exp >=
207    /// available_exp) to ensure function does not underflow
208    pub fn spent_exp(&self) -> u32 { self.earned_exp - self.available_exp }
209
210    /// Adds a skill point while subtracting the necessary amount of experience
211    fn earn_skill_point(&mut self) -> Result<(), SpRewardError> {
212        let sp_cost = self.skill_group_kind.skill_point_cost(self.earned_sp);
213        // If there is insufficient available exp, checked sub will fail as the result
214        // would be less than 0
215        let new_available_exp = self
216            .available_exp
217            .checked_sub(sp_cost)
218            .ok_or(SpRewardError::InsufficientExp)?;
219        let new_earned_sp = self
220            .earned_sp
221            .checked_add(1)
222            .ok_or(SpRewardError::Overflow)?;
223        let new_available_sp = self
224            .available_sp
225            .checked_add(1)
226            .ok_or(SpRewardError::Overflow)?;
227        self.available_exp = new_available_exp;
228        self.earned_sp = new_earned_sp;
229        self.available_sp = new_available_sp;
230        Ok(())
231    }
232
233    /// Also attempts to earn a skill point after adding experience. If a skill
234    /// point was earned, returns how many skill points the skill group now has
235    /// earned in total.
236    pub fn add_experience(&mut self, amount: u32) -> Option<u16> {
237        self.earned_exp = self.earned_exp.saturating_add(amount);
238        self.available_exp = self.available_exp.saturating_add(amount);
239
240        let mut return_val = None;
241        // Attempt to earn skill point
242        while self.earn_skill_point().is_ok() {
243            return_val = Some(self.earned_sp);
244        }
245        return_val
246    }
247}
248
249/// Contains all of a player's skill groups and skills. Provides methods for
250/// manipulating assigned skills and skill groups including unlocking skills,
251/// refunding skills etc.
252#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
253pub struct SkillSet {
254    skill_groups: HashMap<SkillGroupKind, SkillGroup>,
255    skills: HashMap<Skill, u16>,
256}
257
258impl Component for SkillSet {
259    type Storage = DerefFlaggedStorage<Self, specs::VecStorage<Self>>;
260}
261
262impl Default for SkillSet {
263    /// Instantiate a new skill set with the default skill groups with no
264    /// unlocked skills in them - used when adding a skill set to a new
265    /// player
266    fn default() -> Self {
267        // Create an empty skillset
268        let mut skill_group = Self {
269            skill_groups: HashMap::new(),
270            skills: SkillSet::initial_skills(),
271        };
272
273        // Insert default skill groups
274        skill_group.unlock_skill_group(SkillGroupKind::General);
275        skill_group.unlock_skill_group(SkillGroupKind::Weapon(ToolKind::Pick));
276
277        skill_group
278    }
279}
280
281impl SkillSet {
282    pub fn initial_skills() -> HashMap<Skill, u16> {
283        let mut skills = HashMap::new();
284        skills.insert(Skill::UnlockGroup(SkillGroupKind::General), 1);
285        skills.insert(
286            Skill::UnlockGroup(SkillGroupKind::Weapon(ToolKind::Pick)),
287            1,
288        );
289        skills
290    }
291
292    /// NOTE: This does *not* return an error on failure, since we can partially
293    /// recover from some failures.  Instead, it returns the error in the
294    /// second return value; make sure to handle it if present!
295    pub fn load_from_database(
296        skill_groups: HashMap<SkillGroupKind, SkillGroup>,
297        mut all_skills: HashMap<SkillGroupKind, Result<Vec<Skill>, SkillsPersistenceError>>,
298    ) -> (Self, Option<SkillsPersistenceError>) {
299        let mut skillset = SkillSet {
300            skill_groups,
301            skills: SkillSet::initial_skills(),
302        };
303        let mut persistence_load_error = None;
304
305        // Loops while checking the all_skills hashmap. For as long as it can find an
306        // entry where the skill group kind is unlocked, insert the skills corresponding
307        // to that skill group kind. When no more skill group kinds can be found, break
308        // the loop.
309        while let Some(skill_group_kind) = all_skills
310            .keys()
311            .find(|kind| skillset.has_skill(Skill::UnlockGroup(**kind)))
312            .copied()
313        {
314            // Remove valid skill group kind from the hash map so that loop eventually
315            // terminates.
316            if let Some(skills_result) = all_skills.remove(&skill_group_kind) {
317                match skills_result {
318                    Ok(skills) => {
319                        let backup_skillset = skillset.clone();
320                        // Iterate over all skills and make sure that unlocking them is successful.
321                        // If any fail, fall back to skillset before
322                        // unlocking any to allow a full respec
323                        if !skills
324                            .iter()
325                            .all(|skill| skillset.unlock_skill(*skill).is_ok())
326                        {
327                            skillset = backup_skillset;
328                            // If unlocking failed, set persistence_load_error
329                            persistence_load_error =
330                                Some(SkillsPersistenceError::SkillsUnlockFailed)
331                        }
332                    },
333                    Err(persistence_error) => persistence_load_error = Some(persistence_error),
334                }
335            }
336        }
337
338        (skillset, persistence_load_error)
339    }
340
341    /// Check if a particular skill group is accessible for an entity, *if* it
342    /// exists.
343    fn skill_group_accessible_if_exists(&self, skill_group_kind: SkillGroupKind) -> bool {
344        self.has_skill(Skill::UnlockGroup(skill_group_kind))
345    }
346
347    /// Checks if a particular skill group is accessible for an entity
348    pub fn skill_group_accessible(&self, skill_group_kind: SkillGroupKind) -> bool {
349        self.skill_groups.contains_key(&skill_group_kind)
350            && self.skill_group_accessible_if_exists(skill_group_kind)
351    }
352
353    ///  Unlocks a skill group for a player. It starts with 0 exp and 0 skill
354    ///  points.
355    fn unlock_skill_group(&mut self, skill_group_kind: SkillGroupKind) {
356        if !self.skill_groups.contains_key(&skill_group_kind) {
357            self.skill_groups
358                .insert(skill_group_kind, SkillGroup::new(skill_group_kind));
359        }
360    }
361
362    /// Returns an iterator over skill groups
363    pub fn skill_groups(&self) -> impl Iterator<Item = &SkillGroup> { self.skill_groups.values() }
364
365    /// Returns a reference to a particular skill group in a skillset
366    fn skill_group(&self, skill_group: SkillGroupKind) -> Option<&SkillGroup> {
367        self.skill_groups.get(&skill_group)
368    }
369
370    /// Returns a mutable reference to a particular skill group in a skillset
371    /// Requires that skillset contains skill that unlocks the skill group
372    fn skill_group_mut(&mut self, skill_group: SkillGroupKind) -> Option<&mut SkillGroup> {
373        // In order to mutate skill group, we check that the prerequisite skill has been
374        // acquired, as this is one of the requirements for us to consider the skill
375        // group accessible.
376        let skill_group_accessible = self.skill_group_accessible(skill_group);
377        if skill_group_accessible {
378            self.skill_groups.get_mut(&skill_group)
379        } else {
380            None
381        }
382    }
383
384    /// Adds experience to the skill group within an entity's skill set, will
385    /// attempt to earn a skill point while doing so. If a skill point was
386    /// earned, returns the number of earned skill points in the skill group.
387    pub fn add_experience(&mut self, skill_group_kind: SkillGroupKind, amount: u32) -> Option<u16> {
388        if let Some(skill_group) = self.skill_group_mut(skill_group_kind) {
389            skill_group.add_experience(amount)
390        } else {
391            warn!("Tried to add experience to a skill group that player does not have");
392            None
393        }
394    }
395
396    /// Gets the available experience for a particular skill group
397    pub fn available_experience(&self, skill_group: SkillGroupKind) -> u32 {
398        self.skill_group(skill_group)
399            .map_or(0, |s_g| s_g.available_exp)
400    }
401
402    /// Checks how much experience is needed for the next skill point in a tree
403    pub fn skill_point_cost(&self, skill_group: SkillGroupKind) -> u32 {
404        if let Some(level) = self.skill_group(skill_group).map(|sg| sg.earned_sp) {
405            skill_group.skill_point_cost(level)
406        } else {
407            skill_group.skill_point_cost(0)
408        }
409    }
410
411    /// Adds skill points to a skill group as long as the player has that skill
412    /// group type.
413    pub fn add_skill_points(
414        &mut self,
415        skill_group_kind: SkillGroupKind,
416        number_of_skill_points: u16,
417    ) {
418        for _ in 0..number_of_skill_points {
419            let exp_needed = self.skill_point_cost(skill_group_kind);
420            self.add_experience(skill_group_kind, exp_needed);
421        }
422    }
423
424    /// Gets the available points for a particular skill group
425    pub fn available_sp(&self, skill_group: SkillGroupKind) -> u16 {
426        self.skill_group(skill_group)
427            .map_or(0, |s_g| s_g.available_sp)
428    }
429
430    /// Gets the total earned points for a particular skill group
431    pub fn earned_sp(&self, skill_group: SkillGroupKind) -> u16 {
432        self.skill_group(skill_group).map_or(0, |s_g| s_g.earned_sp)
433    }
434
435    /// Checks that the skill set contains all prerequisite skills of the
436    /// required level for a particular skill
437    pub fn prerequisites_met(&self, skill: Skill) -> bool {
438        match skill.prerequisite_skills() {
439            Some(SkillPrerequisite::All(skills)) => skills
440                .iter()
441                .all(|(s, l)| self.skill_level(*s).is_ok_and(|l_b| l_b >= *l)),
442            Some(SkillPrerequisite::Any(skills)) => skills
443                .iter()
444                .any(|(s, l)| self.skill_level(*s).is_ok_and(|l_b| l_b >= *l)),
445            None => true,
446        }
447    }
448
449    /// Gets skill point cost to purchase skill of next level
450    pub fn skill_cost(&self, skill: Skill) -> u16 {
451        let next_level = self.next_skill_level(skill);
452        skill.skill_cost(next_level)
453    }
454
455    /// Checks if player has sufficient skill points to purchase a skill
456    pub fn sufficient_skill_points(&self, skill: Skill) -> bool {
457        if let Some(skill_group_kind) = skill.skill_group_kind() {
458            if let Some(skill_group) = self.skill_group(skill_group_kind) {
459                let needed_sp = self.skill_cost(skill);
460                skill_group.available_sp >= needed_sp
461            } else {
462                false
463            }
464        } else {
465            false
466        }
467    }
468
469    /// Checks the next level of a skill
470    fn next_skill_level(&self, skill: Skill) -> u16 {
471        if let Ok(level) = self.skill_level(skill) {
472            // If already has skill, then level + 1
473            level + 1
474        } else {
475            // Otherwise the next level is the first level
476            1
477        }
478    }
479
480    /// Unlocks a skill for a player, assuming they have the relevant skill
481    /// group unlocked and available SP in that skill group.
482    ///
483    /// NOTE: Please don't use pathological or clever implementations of to_mut
484    /// here.
485    pub fn unlock_skill_cow<'a, B, C>(
486        this_: &'a mut B,
487        skill: Skill,
488        to_mut: impl FnOnce(&'a mut B) -> &'a mut C,
489    ) -> Result<(), SkillUnlockError>
490    where
491        B: Borrow<SkillSet>,
492        C: BorrowMut<SkillSet> + 'a,
493    {
494        if let Some(skill_group_kind) = skill.skill_group_kind() {
495            let this = (*this_).borrow();
496            let next_level = this.next_skill_level(skill);
497            let prerequisites_met = this.prerequisites_met(skill);
498            // Check that skill is not yet at max level
499            if !matches!(this.skills.get(&skill), Some(level) if *level == skill.max_level()) {
500                if let Some(skill_group) = this.skill_groups.get(&skill_group_kind)
501                    && this.skill_group_accessible_if_exists(skill_group_kind)
502                {
503                    if prerequisites_met {
504                        if let Some(new_available_sp) = skill_group
505                            .available_sp
506                            .checked_sub(skill.skill_cost(next_level))
507                        {
508                            // Perform all mutation inside this branch, to avoid triggering a copy
509                            // on write or flagged storage in cases where this matters.
510                            let this_ = to_mut(this_);
511                            let this = this_.borrow_mut();
512                            // NOTE: Verified to exist previously when we accessed
513                            // this.skill_groups (assuming a non-pathological implementation of
514                            // ToOwned).
515                            let skill_group = this.skill_groups.get_mut(&skill_group_kind).expect(
516                                "Verified to exist when we previously accessed this.skill_groups",
517                            );
518                            skill_group.available_sp = new_available_sp;
519                            skill_group.ordered_skills.push(skill);
520                            if let Skill::UnlockGroup(group) = skill {
521                                this.unlock_skill_group(group);
522                            }
523                            this.skills.insert(skill, next_level);
524                            Ok(())
525                        } else {
526                            trace!("Tried to unlock skill for skill group with insufficient SP");
527                            Err(SkillUnlockError::InsufficientSP)
528                        }
529                    } else {
530                        trace!("Tried to unlock skill without meeting prerequisite skills");
531                        Err(SkillUnlockError::MissingPrerequisites)
532                    }
533                } else {
534                    trace!("Tried to unlock skill for a skill group that player does not have");
535                    Err(SkillUnlockError::UnavailableSkillGroup)
536                }
537            } else {
538                trace!("Tried to unlock skill the player already has");
539                Err(SkillUnlockError::SkillAlreadyUnlocked)
540            }
541        } else {
542            warn!(
543                ?skill,
544                "Tried to unlock skill that does not exist in any skill group!"
545            );
546            Err(SkillUnlockError::NoParentSkillTree)
547        }
548    }
549
550    /// Convenience function for the case where you have mutable access to the
551    /// skill.
552    pub fn unlock_skill(&mut self, skill: Skill) -> Result<(), SkillUnlockError> {
553        Self::unlock_skill_cow(self, skill, |x| x)
554    }
555
556    /// Checks if the player has available SP to spend
557    pub fn has_available_sp(&self) -> bool {
558        self.skill_groups.iter().any(|(kind, sg)| {
559            sg.available_sp > 0
560            // Subtraction in bounds because of the invariant that available_sp <= earned_sp
561                && (sg.earned_sp - sg.available_sp) < kind.total_skill_point_cost()
562        })
563    }
564
565    /// Checks if the skill is at max level in a skill set
566    pub fn is_at_max_level(&self, skill: Skill) -> bool {
567        if let Ok(level) = self.skill_level(skill) {
568            level == skill.max_level()
569        } else {
570            false
571        }
572    }
573
574    /// Checks if skill set contains a skill
575    pub fn has_skill(&self, skill: Skill) -> bool { self.skills.contains_key(&skill) }
576
577    /// Returns the level of the skill
578    pub fn skill_level(&self, skill: Skill) -> Result<u16, SkillError> {
579        if let Some(level) = self.skills.get(&skill).copied() {
580            Ok(level)
581        } else {
582            Err(SkillError::MissingSkill)
583        }
584    }
585}
586
587#[derive(Debug)]
588pub enum SkillError {
589    MissingSkill,
590}
591
592#[derive(Debug)]
593pub enum SkillUnlockError {
594    InsufficientSP,
595    MissingPrerequisites,
596    UnavailableSkillGroup,
597    SkillAlreadyUnlocked,
598    NoParentSkillTree,
599}
600
601#[derive(Debug)]
602pub enum SpRewardError {
603    InsufficientExp,
604    UnavailableSkillGroup,
605    Overflow,
606}
607
608#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize, Serialize)]
609pub enum SkillsPersistenceError {
610    HashMismatch,
611    DeserializationFailure,
612    SpentExpMismatch,
613    SkillsUnlockFailed,
614}
615
616#[derive(Debug, Clone, Deserialize, Serialize)]
617pub enum SkillPrerequisite {
618    All(HashMap<Skill, u16>),
619    Any(HashMap<Skill, u16>),
620}