2020-12-05 22:03:42 +00:00
|
|
|
use crate::{
|
|
|
|
assets::{self, Asset, AssetExt},
|
2021-10-17 04:28:39 +00:00
|
|
|
comp::{
|
|
|
|
item::tool::ToolKind,
|
|
|
|
skills::{GeneralSkill, Skill},
|
|
|
|
},
|
2020-12-05 22:03:42 +00:00
|
|
|
};
|
2021-11-14 18:32:37 +00:00
|
|
|
use hashbrown::HashMap;
|
2020-06-29 16:22:19 +00:00
|
|
|
use lazy_static::lazy_static;
|
2020-07-06 14:23:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-11-14 18:32:37 +00:00
|
|
|
use sha2::{Digest, Sha256};
|
2021-04-14 15:35:34 +00:00
|
|
|
use specs::{Component, DerefFlaggedStorage};
|
|
|
|
use specs_idvs::IdvStorage;
|
2021-11-14 18:32:37 +00:00
|
|
|
use std::{collections::BTreeSet, hash::Hash};
|
2021-01-05 06:03:25 +00:00
|
|
|
use tracing::{trace, warn};
|
2020-06-29 16:22:19 +00:00
|
|
|
|
2021-10-17 04:28:39 +00:00
|
|
|
pub mod skills;
|
|
|
|
|
2021-12-01 04:12:02 +00:00
|
|
|
/// BTreeSet is used here to ensure that skills are ordered. This is important
|
|
|
|
/// to ensure that the hash created from it is consistent so that we don't
|
|
|
|
/// needlessly force a respec when loading skills from persistence.
|
2020-12-05 22:03:42 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2021-11-14 18:32:37 +00:00
|
|
|
pub struct SkillTreeMap(HashMap<SkillGroupKind, BTreeSet<Skill>>);
|
2020-12-05 22:03:42 +00:00
|
|
|
|
2020-12-06 19:10:47 +00:00
|
|
|
impl Asset for SkillTreeMap {
|
|
|
|
type Loader = assets::RonLoader;
|
|
|
|
|
|
|
|
const EXTENSION: &'static str = "ron";
|
|
|
|
}
|
|
|
|
|
2021-01-18 19:08:13 +00:00
|
|
|
pub struct SkillGroupDef {
|
2021-11-14 18:32:37 +00:00
|
|
|
pub skills: BTreeSet<Skill>,
|
2021-01-18 19:08:13 +00:00
|
|
|
pub total_skill_point_cost: u16,
|
|
|
|
}
|
|
|
|
|
2020-12-06 19:10:47 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2021-12-01 04:12:02 +00:00
|
|
|
pub struct SkillLevelMap(HashMap<Skill, u16>);
|
2020-12-06 19:10:47 +00:00
|
|
|
|
|
|
|
impl Asset for SkillLevelMap {
|
|
|
|
type Loader = assets::RonLoader;
|
|
|
|
|
|
|
|
const EXTENSION: &'static str = "ron";
|
|
|
|
}
|
|
|
|
|
2021-12-02 04:38:33 +00:00
|
|
|
/// Contains the prerequisite skills for each skill. It cannot currently detect
|
|
|
|
/// cyclic dependencies, so if you modify the prerequisite map ensure that there
|
|
|
|
/// are no cycles of prerequisites.
|
2020-12-06 19:10:47 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2021-12-01 04:12:02 +00:00
|
|
|
pub struct SkillPrerequisitesMap(HashMap<Skill, HashMap<Skill, u16>>);
|
2020-12-06 19:10:47 +00:00
|
|
|
|
|
|
|
impl Asset for SkillPrerequisitesMap {
|
2020-12-05 22:03:42 +00:00
|
|
|
type Loader = assets::RonLoader;
|
|
|
|
|
|
|
|
const EXTENSION: &'static str = "ron";
|
|
|
|
}
|
|
|
|
|
2020-07-03 19:40:37 +00:00
|
|
|
lazy_static! {
|
2021-08-16 17:43:57 +00:00
|
|
|
// Determines the skills that comprise each skill group.
|
|
|
|
//
|
|
|
|
// This data is used to determine which of a player's skill groups a
|
|
|
|
// particular skill should be added to when a skill unlock is requested.
|
2021-01-18 19:08:13 +00:00
|
|
|
pub static ref SKILL_GROUP_DEFS: HashMap<SkillGroupKind, SkillGroupDef> = {
|
|
|
|
let map = SkillTreeMap::load_expect_cloned(
|
2020-12-06 19:10:47 +00:00
|
|
|
"common.skill_trees.skills_skill-groups_manifest",
|
2021-01-18 19:08:13 +00:00
|
|
|
).0;
|
|
|
|
map.iter().map(|(sgk, skills)|
|
|
|
|
(*sgk, SkillGroupDef { skills: skills.clone(),
|
|
|
|
total_skill_point_cost: skills
|
|
|
|
.iter()
|
|
|
|
.map(|skill| {
|
2021-12-01 04:12:02 +00:00
|
|
|
let max_level = skill.max_level();
|
|
|
|
(1..=max_level)
|
|
|
|
.into_iter()
|
|
|
|
.map(|level| skill.skill_cost(level))
|
|
|
|
.sum::<u16>()
|
2021-01-18 19:08:13 +00:00
|
|
|
})
|
|
|
|
.sum()
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.collect()
|
|
|
|
};
|
|
|
|
// Creates a hashmap for the reverse lookup of skill groups from a skill
|
|
|
|
pub static ref SKILL_GROUP_LOOKUP: HashMap<Skill, SkillGroupKind> = {
|
|
|
|
let map = SkillTreeMap::load_expect_cloned(
|
|
|
|
"common.skill_trees.skills_skill-groups_manifest",
|
|
|
|
).0;
|
2021-12-21 20:19:23 +00:00
|
|
|
map.iter().flat_map(|(sgk, skills)| skills.into_iter().map(move |s| (*s, *sgk))).collect()
|
2020-12-06 19:10:47 +00:00
|
|
|
};
|
|
|
|
// Loads the maximum level that a skill can obtain
|
2021-12-01 04:12:02 +00:00
|
|
|
pub static ref SKILL_MAX_LEVEL: HashMap<Skill, u16> = {
|
2020-12-06 19:10:47 +00:00
|
|
|
SkillLevelMap::load_expect_cloned(
|
|
|
|
"common.skill_trees.skill_max_levels",
|
|
|
|
).0
|
|
|
|
};
|
|
|
|
// Loads the prerequisite skills for a particular skill
|
2021-12-01 04:12:02 +00:00
|
|
|
pub static ref SKILL_PREREQUISITES: HashMap<Skill, HashMap<Skill, u16>> = {
|
2020-12-06 19:10:47 +00:00
|
|
|
SkillPrerequisitesMap::load_expect_cloned(
|
|
|
|
"common.skill_trees.skill_prerequisites",
|
2020-12-05 22:03:42 +00:00
|
|
|
).0
|
2020-07-03 19:40:37 +00:00
|
|
|
};
|
2021-11-14 18:32:37 +00:00
|
|
|
pub static ref SKILL_GROUP_HASHES: HashMap<SkillGroupKind, Vec<u8>> = {
|
|
|
|
let map = SkillTreeMap::load_expect_cloned(
|
|
|
|
"common.skill_trees.skills_skill-groups_manifest",
|
|
|
|
).0;
|
|
|
|
let mut hashes = HashMap::new();
|
|
|
|
for (skill_group_kind, skills) in map.iter() {
|
|
|
|
let mut hasher = Sha256::new();
|
2021-12-01 04:12:02 +00:00
|
|
|
let json_input: Vec<_> = skills.iter().map(|skill| (*skill, skill.max_level())).collect();
|
|
|
|
let hash_input = serde_json::to_string(&json_input).unwrap_or_default();
|
|
|
|
hasher.update(hash_input.as_bytes());
|
2021-11-14 18:32:37 +00:00
|
|
|
let hash_result = hasher.finalize();
|
|
|
|
hashes.insert(*skill_group_kind, hash_result.iter().copied().collect());
|
|
|
|
}
|
|
|
|
hashes
|
|
|
|
};
|
2020-07-03 19:40:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 18:32:37 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize, Ord, PartialOrd)]
|
2021-01-18 19:08:13 +00:00
|
|
|
pub enum SkillGroupKind {
|
2020-11-15 02:05:18 +00:00
|
|
|
General,
|
|
|
|
Weapon(ToolKind),
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 19:08:13 +00:00
|
|
|
impl SkillGroupKind {
|
2021-01-02 04:11:30 +00:00
|
|
|
/// Gets the cost in experience of earning a skill point
|
2021-12-01 04:12:02 +00:00
|
|
|
/// Changing this is forward compatible with persistence and will
|
|
|
|
/// automatically force a respec for skill group kinds that are affected.
|
2021-10-13 14:44:28 +00:00
|
|
|
pub fn skill_point_cost(self, level: u16) -> u32 {
|
2021-08-08 03:34:56 +00:00
|
|
|
const EXP_INCREMENT: f32 = 10.0;
|
|
|
|
const STARTING_EXP: f32 = 70.0;
|
|
|
|
const EXP_CEILING: f32 = 1000.0;
|
|
|
|
const SCALING_FACTOR: f32 = 0.125;
|
|
|
|
(EXP_INCREMENT
|
|
|
|
* (EXP_CEILING
|
|
|
|
/ EXP_INCREMENT
|
2021-01-16 17:01:57 +00:00
|
|
|
/ (1.0
|
2021-08-08 03:34:56 +00:00
|
|
|
+ std::f32::consts::E.powf(-SCALING_FACTOR * level as f32)
|
|
|
|
* (EXP_CEILING / STARTING_EXP - 1.0)))
|
2021-10-13 14:44:28 +00:00
|
|
|
.floor()) as u32
|
2021-01-07 21:02:50 +00:00
|
|
|
}
|
2021-01-04 17:53:27 +00:00
|
|
|
|
|
|
|
/// Gets the total amount of skill points that can be spent in a particular
|
|
|
|
/// skill group
|
2021-01-18 19:08:13 +00:00
|
|
|
pub fn total_skill_point_cost(self) -> u16 {
|
|
|
|
if let Some(SkillGroupDef {
|
|
|
|
total_skill_point_cost,
|
|
|
|
..
|
|
|
|
}) = SKILL_GROUP_DEFS.get(&self)
|
|
|
|
{
|
|
|
|
*total_skill_point_cost
|
2021-01-16 17:01:57 +00:00
|
|
|
} else {
|
|
|
|
0
|
2021-01-04 17:53:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-02 04:11:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 16:22:19 +00:00
|
|
|
/// A group of skills that have been unlocked by a player. Each skill group has
|
|
|
|
/// independent exp and skill points which are used to unlock skills in that
|
|
|
|
/// skill group.
|
2020-11-15 02:05:18 +00:00
|
|
|
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
|
2020-06-29 16:22:19 +00:00
|
|
|
pub struct SkillGroup {
|
2021-01-18 19:08:13 +00:00
|
|
|
pub skill_group_kind: SkillGroupKind,
|
2021-12-02 04:38:33 +00:00
|
|
|
// The invariant that (earned_exp >= available_exp) should not be violated
|
|
|
|
pub available_exp: u32,
|
2021-10-13 14:44:28 +00:00
|
|
|
pub earned_exp: u32,
|
2021-12-02 04:38:33 +00:00
|
|
|
// The invariant that (earned_sp >= available_sp) should not be violated
|
2020-11-15 21:05:02 +00:00
|
|
|
pub available_sp: u16,
|
2021-01-02 04:11:30 +00:00
|
|
|
pub earned_sp: u16,
|
2021-10-21 14:53:46 +00:00
|
|
|
// Used for persistence
|
|
|
|
pub ordered_skills: Vec<Skill>,
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:40:37 +00:00
|
|
|
impl SkillGroup {
|
2021-01-18 19:08:13 +00:00
|
|
|
fn new(skill_group_kind: SkillGroupKind) -> SkillGroup {
|
2020-07-03 19:40:37 +00:00
|
|
|
SkillGroup {
|
2021-01-18 19:08:13 +00:00
|
|
|
skill_group_kind,
|
2021-12-02 04:38:33 +00:00
|
|
|
available_exp: 0,
|
2021-10-13 14:44:28 +00:00
|
|
|
earned_exp: 0,
|
2020-06-29 16:22:19 +00:00
|
|
|
available_sp: 0,
|
2021-01-02 04:11:30 +00:00
|
|
|
earned_sp: 0,
|
2021-10-21 14:53:46 +00:00
|
|
|
ordered_skills: Vec::new(),
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-13 14:44:28 +00:00
|
|
|
|
2021-12-02 04:38:33 +00:00
|
|
|
/// Returns the amount of experience in a skill group that has been spent to
|
|
|
|
/// acquire skill points Relies on the invariant that (earned_exp >=
|
|
|
|
/// available_exp) to ensure function does not underflow
|
|
|
|
pub fn spent_exp(&self) -> u32 { self.earned_exp - self.available_exp }
|
2021-10-13 14:44:28 +00:00
|
|
|
|
|
|
|
/// Adds a skill point while subtracting the necessary amount of experience
|
|
|
|
pub fn earn_skill_point(&mut self) -> Result<(), SpRewardError> {
|
|
|
|
let sp_cost = self.skill_group_kind.skill_point_cost(self.earned_sp);
|
2021-12-02 04:38:33 +00:00
|
|
|
// If there is insufficient available exp, checked sub will fail as the result
|
|
|
|
// would be less than 0
|
|
|
|
let new_available_exp = self
|
|
|
|
.available_exp
|
|
|
|
.checked_sub(sp_cost)
|
|
|
|
.ok_or(SpRewardError::InsufficientExp)?;
|
|
|
|
let new_earned_sp = self
|
|
|
|
.earned_sp
|
|
|
|
.checked_add(1)
|
|
|
|
.ok_or(SpRewardError::Overflow)?;
|
|
|
|
let new_available_sp = self
|
|
|
|
.available_sp
|
|
|
|
.checked_add(1)
|
|
|
|
.ok_or(SpRewardError::Overflow)?;
|
|
|
|
self.available_exp = new_available_exp;
|
|
|
|
self.earned_sp = new_earned_sp;
|
|
|
|
self.available_sp = new_available_sp;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_experience(&mut self, amount: u32) {
|
|
|
|
self.earned_exp = self.earned_exp.saturating_add(amount);
|
|
|
|
self.available_exp = self.available_exp.saturating_add(amount);
|
2021-10-13 14:44:28 +00:00
|
|
|
}
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:40:37 +00:00
|
|
|
/// Contains all of a player's skill groups and skills. Provides methods for
|
|
|
|
/// manipulating assigned skills and skill groups including unlocking skills,
|
|
|
|
/// refunding skills etc.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
2020-06-29 16:22:19 +00:00
|
|
|
pub struct SkillSet {
|
2021-11-15 01:11:58 +00:00
|
|
|
skill_groups: Vec<SkillGroup>,
|
2021-12-01 04:12:02 +00:00
|
|
|
skills: HashMap<Skill, u16>,
|
2020-12-31 18:37:25 +00:00
|
|
|
pub modify_health: bool,
|
|
|
|
pub modify_energy: bool,
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 15:35:34 +00:00
|
|
|
impl Component for SkillSet {
|
|
|
|
type Storage = DerefFlaggedStorage<Self, IdvStorage<Self>>;
|
|
|
|
}
|
|
|
|
|
2020-06-29 16:22:19 +00:00
|
|
|
impl Default for SkillSet {
|
|
|
|
/// Instantiate a new skill set with the default skill groups with no
|
|
|
|
/// unlocked skills in them - used when adding a skill set to a new
|
|
|
|
/// player
|
|
|
|
fn default() -> Self {
|
2020-07-03 19:40:37 +00:00
|
|
|
Self {
|
2021-06-09 05:14:20 +00:00
|
|
|
skill_groups: vec![
|
|
|
|
SkillGroup::new(SkillGroupKind::General),
|
|
|
|
SkillGroup::new(SkillGroupKind::Weapon(ToolKind::Pick)),
|
|
|
|
],
|
2021-11-15 01:11:58 +00:00
|
|
|
skills: SkillSet::initial_skills(),
|
2020-12-31 18:37:25 +00:00
|
|
|
modify_health: false,
|
|
|
|
modify_energy: false,
|
2020-07-03 19:40:37 +00:00
|
|
|
}
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SkillSet {
|
2021-12-01 04:12:02 +00:00
|
|
|
pub fn initial_skills() -> HashMap<Skill, u16> {
|
2021-11-15 01:11:58 +00:00
|
|
|
let mut skills = HashMap::new();
|
2021-12-01 04:12:02 +00:00
|
|
|
skills.insert(Skill::UnlockGroup(SkillGroupKind::General), 1);
|
2021-11-15 01:11:58 +00:00
|
|
|
skills.insert(
|
|
|
|
Skill::UnlockGroup(SkillGroupKind::Weapon(ToolKind::Pick)),
|
2021-12-01 04:12:02 +00:00
|
|
|
1,
|
2021-11-15 01:11:58 +00:00
|
|
|
);
|
|
|
|
skills
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_from_database(
|
|
|
|
skill_groups: Vec<SkillGroup>,
|
2021-12-02 04:38:33 +00:00
|
|
|
mut all_skills: HashMap<SkillGroupKind, Result<Vec<Skill>, SkillsPersistenceError>>,
|
2021-11-15 01:11:58 +00:00
|
|
|
) -> Self {
|
|
|
|
let mut skillset = SkillSet {
|
|
|
|
skill_groups,
|
|
|
|
skills: SkillSet::initial_skills(),
|
|
|
|
modify_health: true,
|
|
|
|
modify_energy: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Loops while checking the all_skills hashmap. For as long as it can find an
|
|
|
|
// entry where the skill group kind is unlocked, insert the skills corresponding
|
|
|
|
// to that skill group kind. When no more skill group kinds can be found, break
|
|
|
|
// the loop.
|
|
|
|
while let Some(skill_group_kind) = all_skills
|
|
|
|
.keys()
|
|
|
|
.find(|kind| skillset.has_skill(Skill::UnlockGroup(**kind)))
|
|
|
|
.copied()
|
|
|
|
{
|
|
|
|
// Remove valid skill group kind from the hash map so that loop eventually
|
|
|
|
// terminates.
|
2021-12-02 04:38:33 +00:00
|
|
|
if let Some(Ok(skills)) = all_skills.remove(&skill_group_kind) {
|
2021-11-15 01:11:58 +00:00
|
|
|
let backup_skillset = skillset.clone();
|
|
|
|
// Iterate over all skills and make sure that unlocking them is successful. If
|
|
|
|
// any fail, fall back to skillset before unlocking any to allow a full respec
|
|
|
|
if !skills
|
|
|
|
.iter()
|
|
|
|
.all(|skill| skillset.unlock_skill(*skill).is_ok())
|
|
|
|
{
|
|
|
|
skillset = backup_skillset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
skillset
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if a particular skill group is accessible for an entity
|
|
|
|
pub fn skill_group_accessible(&self, skill_group_kind: SkillGroupKind) -> bool {
|
2021-10-17 04:28:39 +00:00
|
|
|
self.skill_groups
|
|
|
|
.iter()
|
|
|
|
.any(|x| x.skill_group_kind == skill_group_kind)
|
2021-11-15 01:11:58 +00:00
|
|
|
&& self.has_skill(Skill::UnlockGroup(skill_group_kind))
|
2021-10-17 04:28:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:32:24 +00:00
|
|
|
/// Unlocks a skill group for a player. It starts with 0 exp and 0 skill
|
|
|
|
/// points.
|
2021-01-18 19:08:13 +00:00
|
|
|
pub fn unlock_skill_group(&mut self, skill_group_kind: SkillGroupKind) {
|
2021-11-15 01:11:58 +00:00
|
|
|
if !self
|
|
|
|
.skill_groups
|
|
|
|
.iter()
|
|
|
|
.any(|x| x.skill_group_kind == skill_group_kind)
|
|
|
|
{
|
2021-01-18 19:08:13 +00:00
|
|
|
self.skill_groups.push(SkillGroup::new(skill_group_kind));
|
2020-07-03 19:40:37 +00:00
|
|
|
} else {
|
|
|
|
warn!("Tried to unlock already known skill group");
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 16:22:19 +00:00
|
|
|
|
2021-11-15 01:11:58 +00:00
|
|
|
/// Returns an iterator over skill groups
|
|
|
|
pub fn skill_groups(&self) -> &Vec<SkillGroup> { &self.skill_groups }
|
|
|
|
|
2021-10-21 14:53:46 +00:00
|
|
|
/// Returns a reference to a particular skill group in a skillset
|
|
|
|
fn skill_group(&self, skill_group: SkillGroupKind) -> Option<&SkillGroup> {
|
|
|
|
self.skill_groups
|
|
|
|
.iter()
|
|
|
|
.find(|s_g| s_g.skill_group_kind == skill_group)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a mutable reference to a particular skill group in a skillset
|
2021-12-01 04:12:02 +00:00
|
|
|
/// Requires that skillset contains skill that unlocks the skill group
|
2021-10-21 14:53:46 +00:00
|
|
|
fn skill_group_mut(&mut self, skill_group: SkillGroupKind) -> Option<&mut SkillGroup> {
|
2021-12-01 04:12:02 +00:00
|
|
|
// In order to mutate skill group, we check that the prerequisite skill has been
|
|
|
|
// acquired, as this is one of the requirements for us to consider the skill
|
|
|
|
// group accessible.
|
|
|
|
let skill_group_accessible = self.skill_group_accessible(skill_group);
|
2021-10-21 14:53:46 +00:00
|
|
|
self.skill_groups
|
|
|
|
.iter_mut()
|
2021-12-01 04:12:02 +00:00
|
|
|
.find(|s_g| s_g.skill_group_kind == skill_group && skill_group_accessible)
|
2021-10-21 14:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds experience to the skill group within an entity's skill set
|
|
|
|
pub fn add_experience(&mut self, skill_group_kind: SkillGroupKind, amount: u32) {
|
2021-12-02 04:38:33 +00:00
|
|
|
if let Some(skill_group) = self.skill_group_mut(skill_group_kind) {
|
|
|
|
skill_group.add_experience(amount);
|
2020-06-29 16:22:19 +00:00
|
|
|
} else {
|
2021-10-21 14:53:46 +00:00
|
|
|
warn!("Tried to add experience to a skill group that player does not have");
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-03 19:40:37 +00:00
|
|
|
|
2021-10-21 14:53:46 +00:00
|
|
|
/// Gets the available experience for a particular skill group
|
|
|
|
pub fn available_experience(&self, skill_group: SkillGroupKind) -> u32 {
|
|
|
|
self.skill_group(skill_group)
|
2021-12-02 04:38:33 +00:00
|
|
|
.map_or(0, |s_g| s_g.available_exp)
|
2021-10-21 14:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks how much experience is needed for the next skill point in a tree
|
|
|
|
pub fn skill_point_cost(&self, skill_group: SkillGroupKind) -> u32 {
|
|
|
|
if let Some(level) = self.skill_group(skill_group).map(|sg| sg.earned_sp) {
|
|
|
|
skill_group.skill_point_cost(level)
|
2020-07-03 19:40:37 +00:00
|
|
|
} else {
|
2021-10-21 14:53:46 +00:00
|
|
|
skill_group.skill_point_cost(0)
|
2020-07-03 19:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 18:32:24 +00:00
|
|
|
/// Adds skill points to a skill group as long as the player has that skill
|
|
|
|
/// group type.
|
|
|
|
pub fn add_skill_points(
|
|
|
|
&mut self,
|
2021-01-18 19:08:13 +00:00
|
|
|
skill_group_kind: SkillGroupKind,
|
2020-11-15 21:05:02 +00:00
|
|
|
number_of_skill_points: u16,
|
2020-07-04 18:32:24 +00:00
|
|
|
) {
|
2021-12-01 04:12:02 +00:00
|
|
|
for _ in 0..number_of_skill_points {
|
|
|
|
let exp_needed = self.skill_point_cost(skill_group_kind);
|
|
|
|
self.add_experience(skill_group_kind, exp_needed);
|
|
|
|
if self.earn_skill_point(skill_group_kind).is_err() {
|
|
|
|
warn!("Failed to add skill point");
|
|
|
|
break;
|
|
|
|
}
|
2020-07-04 18:32:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-15 02:05:18 +00:00
|
|
|
|
2021-01-08 20:53:52 +00:00
|
|
|
/// Adds a skill point while subtracting the necessary amount of experience
|
2021-10-13 14:44:28 +00:00
|
|
|
pub fn earn_skill_point(
|
|
|
|
&mut self,
|
|
|
|
skill_group_kind: SkillGroupKind,
|
|
|
|
) -> Result<(), SpRewardError> {
|
|
|
|
if let Some(skill_group) = self.skill_group_mut(skill_group_kind) {
|
|
|
|
skill_group.earn_skill_point()
|
|
|
|
} else {
|
|
|
|
Err(SpRewardError::UnavailableSkillGroup)
|
2021-01-16 17:01:57 +00:00
|
|
|
}
|
2021-01-08 20:53:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 19:08:13 +00:00
|
|
|
/// Gets the available points for a particular skill group
|
|
|
|
pub fn available_sp(&self, skill_group: SkillGroupKind) -> u16 {
|
|
|
|
self.skill_group(skill_group)
|
2021-01-16 17:01:57 +00:00
|
|
|
.map_or(0, |s_g| s_g.available_sp)
|
2021-01-02 04:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the total earned points for a particular skill group
|
2021-01-18 19:08:13 +00:00
|
|
|
pub fn earned_sp(&self, skill_group: SkillGroupKind) -> u16 {
|
|
|
|
self.skill_group(skill_group).map_or(0, |s_g| s_g.earned_sp)
|
2021-01-02 04:11:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 15:47:06 +00:00
|
|
|
/// Checks that the skill set contains all prerequisite skills of the
|
|
|
|
/// required level for a particular skill
|
2021-10-21 14:53:46 +00:00
|
|
|
pub fn prerequisites_met(&self, skill: Skill) -> bool {
|
|
|
|
skill
|
|
|
|
.prerequisite_skills()
|
|
|
|
.all(|(s, l)| self.skill_level(s).map_or(false, |l_b| l_b >= l))
|
2021-01-02 04:11:30 +00:00
|
|
|
}
|
2021-01-05 06:03:25 +00:00
|
|
|
|
2021-01-07 01:45:30 +00:00
|
|
|
/// Gets skill point cost to purchase skill of next level
|
2021-01-16 17:01:57 +00:00
|
|
|
pub fn skill_cost(&self, skill: Skill) -> u16 {
|
|
|
|
let next_level = self.next_skill_level(skill);
|
2021-01-07 01:45:30 +00:00
|
|
|
skill.skill_cost(next_level)
|
|
|
|
}
|
|
|
|
|
2021-01-05 06:03:25 +00:00
|
|
|
/// Checks if player has sufficient skill points to purchase a skill
|
|
|
|
pub fn sufficient_skill_points(&self, skill: Skill) -> bool {
|
2021-01-18 19:08:13 +00:00
|
|
|
if let Some(skill_group_kind) = skill.skill_group_kind() {
|
2021-12-01 04:12:02 +00:00
|
|
|
if let Some(skill_group) = self.skill_group(skill_group_kind) {
|
2021-01-16 17:01:57 +00:00
|
|
|
let needed_sp = self.skill_cost(skill);
|
2021-01-08 01:27:23 +00:00
|
|
|
skill_group.available_sp >= needed_sp
|
2021-01-05 06:03:25 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:53:46 +00:00
|
|
|
/// Checks the next level of a skill
|
2021-12-01 04:12:02 +00:00
|
|
|
fn next_skill_level(&self, skill: Skill) -> u16 {
|
2021-10-21 14:53:46 +00:00
|
|
|
if let Ok(level) = self.skill_level(skill) {
|
2021-12-01 04:12:02 +00:00
|
|
|
// If already has skill, then level + 1
|
|
|
|
level + 1
|
2021-10-21 14:53:46 +00:00
|
|
|
} else {
|
2021-12-01 04:12:02 +00:00
|
|
|
// Otherwise the next level is the first level
|
|
|
|
1
|
2021-10-21 14:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unlocks a skill for a player, assuming they have the relevant skill
|
|
|
|
/// group unlocked and available SP in that skill group.
|
2021-10-22 15:47:06 +00:00
|
|
|
pub fn unlock_skill(&mut self, skill: Skill) -> Result<(), SkillUnlockError> {
|
2021-10-21 14:53:46 +00:00
|
|
|
if let Some(skill_group_kind) = skill.skill_group_kind() {
|
|
|
|
let next_level = self.next_skill_level(skill);
|
|
|
|
let prerequisites_met = self.prerequisites_met(skill);
|
|
|
|
// Check that skill is not yet at max level
|
|
|
|
if !matches!(self.skills.get(&skill), Some(level) if *level == skill.max_level()) {
|
2021-12-01 04:12:02 +00:00
|
|
|
if let Some(mut skill_group) = self.skill_group_mut(skill_group_kind) {
|
|
|
|
if prerequisites_met {
|
|
|
|
if let Some(new_available_sp) = skill_group
|
|
|
|
.available_sp
|
|
|
|
.checked_sub(skill.skill_cost(next_level))
|
|
|
|
{
|
|
|
|
skill_group.available_sp = new_available_sp;
|
|
|
|
skill_group.ordered_skills.push(skill);
|
|
|
|
match skill {
|
|
|
|
Skill::UnlockGroup(group) => {
|
|
|
|
self.unlock_skill_group(group);
|
|
|
|
},
|
|
|
|
Skill::General(GeneralSkill::HealthIncrease) => {
|
|
|
|
self.modify_health = true;
|
|
|
|
},
|
|
|
|
Skill::General(GeneralSkill::EnergyIncrease) => {
|
|
|
|
self.modify_energy = true;
|
|
|
|
},
|
|
|
|
_ => {},
|
2021-10-21 14:53:46 +00:00
|
|
|
}
|
2021-12-01 04:12:02 +00:00
|
|
|
self.skills.insert(skill, next_level);
|
|
|
|
Ok(())
|
2021-10-21 14:53:46 +00:00
|
|
|
} else {
|
2021-12-01 04:12:02 +00:00
|
|
|
trace!("Tried to unlock skill for skill group with insufficient SP");
|
|
|
|
Err(SkillUnlockError::InsufficientSP)
|
2021-10-21 14:53:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-12-01 04:12:02 +00:00
|
|
|
trace!("Tried to unlock skill without meeting prerequisite skills");
|
|
|
|
Err(SkillUnlockError::MissingPrerequisites)
|
2021-10-21 14:53:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
trace!("Tried to unlock skill for a skill group that player does not have");
|
2021-10-22 15:47:06 +00:00
|
|
|
Err(SkillUnlockError::UnavailableSkillGroup)
|
2021-10-21 14:53:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-22 15:47:06 +00:00
|
|
|
trace!("Tried to unlock skill the player already has");
|
|
|
|
Err(SkillUnlockError::SkillAlreadyUnlocked)
|
2021-10-21 14:53:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
?skill,
|
|
|
|
"Tried to unlock skill that does not exist in any skill group!"
|
|
|
|
);
|
2021-10-22 15:47:06 +00:00
|
|
|
Err(SkillUnlockError::NoParentSkillTree)
|
2021-10-21 14:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 06:03:25 +00:00
|
|
|
/// Checks if the player has available SP to spend
|
|
|
|
pub fn has_available_sp(&self) -> bool {
|
|
|
|
self.skill_groups.iter().any(|sg| {
|
|
|
|
sg.available_sp > 0
|
2021-12-01 04:12:02 +00:00
|
|
|
// Subtraction in bounds because of the invariant that available_sp <= earned_sp
|
2021-01-18 19:08:13 +00:00
|
|
|
&& (sg.earned_sp - sg.available_sp) < sg.skill_group_kind.total_skill_point_cost()
|
2021-01-05 06:03:25 +00:00
|
|
|
})
|
|
|
|
}
|
2021-01-07 21:02:50 +00:00
|
|
|
|
2021-01-16 17:01:57 +00:00
|
|
|
/// Checks if the skill is at max level in a skill set
|
|
|
|
pub fn is_at_max_level(&self, skill: Skill) -> bool {
|
|
|
|
if let Ok(level) = self.skill_level(skill) {
|
|
|
|
level == skill.max_level()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if skill set contains a skill
|
|
|
|
pub fn has_skill(&self, skill: Skill) -> bool { self.skills.contains_key(&skill) }
|
|
|
|
|
|
|
|
/// Returns the level of the skill
|
2021-12-01 04:12:02 +00:00
|
|
|
pub fn skill_level(&self, skill: Skill) -> Result<u16, SkillError> {
|
2021-01-16 17:01:57 +00:00
|
|
|
if let Some(level) = self.skills.get(&skill).copied() {
|
|
|
|
Ok(level)
|
|
|
|
} else {
|
|
|
|
Err(SkillError::MissingSkill)
|
|
|
|
}
|
|
|
|
}
|
2020-11-19 01:26:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 04:12:02 +00:00
|
|
|
#[derive(Debug)]
|
2021-10-17 04:28:39 +00:00
|
|
|
pub enum SkillError {
|
|
|
|
MissingSkill,
|
2020-07-04 18:32:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 15:47:06 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum SkillUnlockError {
|
|
|
|
InsufficientSP,
|
|
|
|
MissingPrerequisites,
|
|
|
|
UnavailableSkillGroup,
|
|
|
|
SkillAlreadyUnlocked,
|
|
|
|
NoParentSkillTree,
|
|
|
|
}
|
|
|
|
|
2021-12-02 04:38:33 +00:00
|
|
|
#[derive(Debug)]
|
2021-10-13 14:44:28 +00:00
|
|
|
pub enum SpRewardError {
|
|
|
|
InsufficientExp,
|
|
|
|
UnavailableSkillGroup,
|
2021-12-01 04:12:02 +00:00
|
|
|
Overflow,
|
2021-10-13 14:44:28 +00:00
|
|
|
}
|
2021-12-02 04:38:33 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum SkillsPersistenceError {
|
|
|
|
HashMismatch,
|
|
|
|
DeserializationFailure,
|
|
|
|
SpentExpMismatch,
|
|
|
|
}
|