2020-12-05 22:03:42 +00:00
|
|
|
use crate::{
|
|
|
|
assets::{self, Asset, AssetExt},
|
|
|
|
comp::item::tool::ToolKind,
|
|
|
|
};
|
2020-12-12 01:45:46 +00:00
|
|
|
use hashbrown::{HashMap, HashSet};
|
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};
|
2020-12-12 01:45:46 +00:00
|
|
|
use std::hash::Hash;
|
2020-06-29 16:22:19 +00:00
|
|
|
use tracing::warn;
|
|
|
|
|
2020-12-05 22:03:42 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2020-12-06 19:10:47 +00:00
|
|
|
pub struct SkillTreeMap(HashMap<SkillGroupType, HashSet<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";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct SkillLevelMap(HashMap<Skill, Level>);
|
|
|
|
|
|
|
|
impl Asset for SkillLevelMap {
|
|
|
|
type Loader = assets::RonLoader;
|
|
|
|
|
|
|
|
const EXTENSION: &'static str = "ron";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct SkillPrerequisitesMap(HashMap<Skill, HashMap<Skill, Level>>);
|
|
|
|
|
|
|
|
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! {
|
|
|
|
// 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
|
2020-12-05 22:03:42 +00:00
|
|
|
// is requested.
|
2020-07-03 19:40:37 +00:00
|
|
|
pub static ref SKILL_GROUP_DEFS: HashMap<SkillGroupType, HashSet<Skill>> = {
|
2020-12-06 19:10:47 +00:00
|
|
|
SkillTreeMap::load_expect_cloned(
|
|
|
|
"common.skill_trees.skills_skill-groups_manifest",
|
|
|
|
).0
|
|
|
|
};
|
|
|
|
// Loads the maximum level that a skill can obtain
|
|
|
|
pub static ref SKILL_MAX_LEVEL: HashMap<Skill, Level> = {
|
|
|
|
SkillLevelMap::load_expect_cloned(
|
|
|
|
"common.skill_trees.skill_max_levels",
|
|
|
|
).0
|
|
|
|
};
|
|
|
|
// Loads the prerequisite skills for a particular skill
|
|
|
|
pub static ref SKILL_PREREQUISITES: HashMap<Skill, HashMap<Skill, Level>> = {
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-29 16:22:19 +00:00
|
|
|
/// Represents a skill that a player can unlock, that either grants them some
|
|
|
|
/// kind of active ability, or a passive effect etc. Obviously because this is
|
|
|
|
/// an enum it doesn't describe what the skill actually -does-, this will be
|
|
|
|
/// handled by dedicated ECS systems.
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum Skill {
|
2020-11-15 02:05:18 +00:00
|
|
|
General(GeneralSkill),
|
|
|
|
Sword(SwordSkill),
|
|
|
|
Axe(AxeSkill),
|
|
|
|
Hammer(HammerSkill),
|
|
|
|
Bow(BowSkill),
|
|
|
|
Staff(StaffSkill),
|
|
|
|
Sceptre(SceptreSkill),
|
2020-11-15 21:05:02 +00:00
|
|
|
UnlockGroup(SkillGroupType),
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum SwordSkill {
|
2020-12-06 19:10:47 +00:00
|
|
|
// Sword passives
|
|
|
|
InterruptingAttacks,
|
|
|
|
// Triple strike upgrades
|
|
|
|
TsCombo,
|
|
|
|
TsDamage,
|
|
|
|
TsRegen,
|
|
|
|
TsSpeed,
|
|
|
|
// Dash upgrades
|
|
|
|
DCost,
|
|
|
|
DDrain,
|
|
|
|
DDamage,
|
|
|
|
DScaling,
|
|
|
|
DSpeed,
|
|
|
|
DInfinite,
|
|
|
|
// Spin upgrades
|
|
|
|
SUnlockSpin,
|
|
|
|
SDamage,
|
|
|
|
SSpeed,
|
|
|
|
SCost,
|
|
|
|
SSpins,
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum AxeSkill {
|
2020-12-19 22:07:02 +00:00
|
|
|
// Double strike upgrades
|
|
|
|
DsCombo,
|
|
|
|
DsDamage,
|
|
|
|
DsSpeed,
|
|
|
|
DsRegen,
|
|
|
|
// Spin upgrades
|
|
|
|
SInfinite,
|
|
|
|
SHelicopter,
|
|
|
|
SDamage,
|
|
|
|
SSpeed,
|
|
|
|
SCost,
|
|
|
|
// Leap upgrades
|
|
|
|
LUnlockLeap,
|
|
|
|
LDamage,
|
|
|
|
LKnockback,
|
|
|
|
LCost,
|
|
|
|
LDistance,
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum HammerSkill {
|
2020-12-20 22:28:17 +00:00
|
|
|
// Single strike upgrades
|
|
|
|
SsKnockback,
|
|
|
|
SsDamage,
|
|
|
|
SsSpeed,
|
|
|
|
SsRegen,
|
|
|
|
// Charged melee upgrades
|
|
|
|
CDamage,
|
|
|
|
CKnockback,
|
|
|
|
CDrain,
|
|
|
|
CSpeed,
|
|
|
|
// Leap upgrades
|
|
|
|
LUnlockLeap,
|
|
|
|
LDamage,
|
|
|
|
LCost,
|
|
|
|
LDistance,
|
|
|
|
LKnockback,
|
|
|
|
LRange,
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum BowSkill {
|
2020-12-23 02:28:55 +00:00
|
|
|
// Passives
|
|
|
|
ProjSpeed,
|
|
|
|
// Basic ranged upgrades
|
|
|
|
BDamage,
|
|
|
|
BRegen,
|
|
|
|
// Charged ranged upgrades
|
|
|
|
CDamage,
|
|
|
|
CKnockback,
|
|
|
|
CProjSpeed,
|
|
|
|
CDrain,
|
|
|
|
CSpeed,
|
|
|
|
CMove,
|
|
|
|
// Repeater upgrades
|
2020-11-15 02:05:18 +00:00
|
|
|
UnlockRepeater,
|
2020-12-23 02:28:55 +00:00
|
|
|
RDamage,
|
2020-12-26 11:25:50 +00:00
|
|
|
RGlide,
|
2020-12-23 02:28:55 +00:00
|
|
|
RArrows,
|
|
|
|
RCost,
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum StaffSkill {
|
2020-12-24 17:54:00 +00:00
|
|
|
// Basic ranged upgrades
|
|
|
|
BExplosion,
|
|
|
|
BDamage,
|
|
|
|
BRegen,
|
|
|
|
BRadius,
|
|
|
|
// Flamethrower upgrades
|
|
|
|
FDamage,
|
|
|
|
FRange,
|
|
|
|
FDrain,
|
|
|
|
FVelocity,
|
|
|
|
// Shockwave upgrades
|
2020-11-15 02:05:18 +00:00
|
|
|
UnlockShockwave,
|
2020-12-24 17:54:00 +00:00
|
|
|
SDamage,
|
|
|
|
SKnockback,
|
|
|
|
SRange,
|
|
|
|
SCost,
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum SceptreSkill {
|
2020-12-25 16:28:35 +00:00
|
|
|
// Beam upgrades
|
|
|
|
BHeal,
|
|
|
|
BDamage,
|
|
|
|
BRange,
|
|
|
|
BLifesteal,
|
|
|
|
BRegen,
|
|
|
|
BCost,
|
|
|
|
// Projectile upgrades
|
|
|
|
PHeal,
|
|
|
|
PDamage,
|
|
|
|
PRadius,
|
|
|
|
PCost,
|
|
|
|
PProjSpeed,
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum GeneralSkill {
|
2020-12-04 23:39:03 +00:00
|
|
|
HealthIncrease,
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum SkillGroupType {
|
2020-11-15 02:05:18 +00:00
|
|
|
General,
|
|
|
|
Weapon(ToolKind),
|
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 {
|
2020-07-03 19:40:37 +00:00
|
|
|
pub skill_group_type: SkillGroupType,
|
2020-11-15 21:05:02 +00:00
|
|
|
pub exp: u16,
|
|
|
|
pub available_sp: u16,
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:40:37 +00:00
|
|
|
impl SkillGroup {
|
|
|
|
fn new(skill_group_type: SkillGroupType) -> SkillGroup {
|
|
|
|
SkillGroup {
|
|
|
|
skill_group_type,
|
2020-06-29 16:22:19 +00:00
|
|
|
exp: 0,
|
|
|
|
available_sp: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-07-03 19:40:37 +00:00
|
|
|
pub skill_groups: Vec<SkillGroup>,
|
2020-12-04 23:39:03 +00:00
|
|
|
pub skills: HashMap<Skill, Level>,
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 23:39:03 +00:00
|
|
|
pub type Level = Option<u16>;
|
|
|
|
|
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 {
|
2020-11-15 21:05:02 +00:00
|
|
|
skill_groups: vec![SkillGroup::new(SkillGroupType::General)],
|
2020-12-04 23:39:03 +00:00
|
|
|
skills: HashMap::new(),
|
2020-07-03 19:40:37 +00:00
|
|
|
}
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SkillSet {
|
2020-07-03 19:40:37 +00:00
|
|
|
// TODO: Game design to determine how skill groups are unlocked
|
2020-07-04 18:32:24 +00:00
|
|
|
/// Unlocks a skill group for a player. It starts with 0 exp and 0 skill
|
|
|
|
/// points.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use veloren_common::comp::skills::{SkillGroupType, SkillSet};
|
|
|
|
///
|
|
|
|
/// let mut skillset = SkillSet::new();
|
|
|
|
/// skillset.unlock_skill_group(SkillGroupType::Axes);
|
|
|
|
///
|
|
|
|
/// assert_eq!(skillset.skill_groups.len(), 1);
|
|
|
|
/// ```
|
2020-07-03 19:40:37 +00:00
|
|
|
pub fn unlock_skill_group(&mut self, skill_group_type: SkillGroupType) {
|
|
|
|
if !self
|
|
|
|
.skill_groups
|
|
|
|
.iter()
|
|
|
|
.any(|x| x.skill_group_type == skill_group_type)
|
|
|
|
{
|
|
|
|
self.skill_groups.push(SkillGroup::new(skill_group_type));
|
|
|
|
} else {
|
|
|
|
warn!("Tried to unlock already known skill group");
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 16:22:19 +00:00
|
|
|
|
2020-07-04 18:32:24 +00:00
|
|
|
/// Unlocks a skill for a player, assuming they have the relevant skill
|
|
|
|
/// group unlocked and available SP in that skill group.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use veloren_common::comp::skills::{Skill, SkillGroupType, SkillSet};
|
|
|
|
///
|
|
|
|
/// let mut skillset = SkillSet::new();
|
|
|
|
/// skillset.unlock_skill_group(SkillGroupType::Axes);
|
|
|
|
/// skillset.add_skill_points(SkillGroupType::Axes, 1);
|
|
|
|
///
|
|
|
|
/// skillset.unlock_skill(Skill::TestAxeSkill2);
|
|
|
|
///
|
|
|
|
/// assert_eq!(skillset.skills.len(), 1);
|
|
|
|
/// ```
|
2020-07-03 19:40:37 +00:00
|
|
|
pub fn unlock_skill(&mut self, skill: Skill) {
|
2020-12-04 23:39:03 +00:00
|
|
|
if let Some(skill_group_type) = SkillSet::get_skill_group_type_for_skill(&skill) {
|
2020-12-05 22:03:42 +00:00
|
|
|
let next_level = if self.skills.contains_key(&skill) {
|
2020-12-04 23:39:03 +00:00
|
|
|
self.skills.get(&skill).copied().flatten().map(|l| l + 1)
|
|
|
|
} else {
|
|
|
|
skill.get_max_level().map(|_| 1)
|
|
|
|
};
|
2020-12-05 22:03:42 +00:00
|
|
|
let prerequisites_met = self.prerequisites_met(skill, next_level);
|
2020-12-06 19:10:47 +00:00
|
|
|
if !matches!(self.skills.get(&skill), Some(&None)) {
|
2020-12-05 22:03:42 +00:00
|
|
|
if let Some(mut skill_group) = self
|
|
|
|
.skill_groups
|
|
|
|
.iter_mut()
|
|
|
|
.find(|x| x.skill_group_type == skill_group_type)
|
|
|
|
{
|
|
|
|
if prerequisites_met {
|
|
|
|
if skill_group.available_sp >= skill.skill_cost(next_level) {
|
|
|
|
skill_group.available_sp -= skill.skill_cost(next_level);
|
|
|
|
if let Skill::UnlockGroup(group) = skill {
|
|
|
|
self.unlock_skill_group(group);
|
|
|
|
}
|
|
|
|
self.skills.insert(skill, next_level);
|
|
|
|
} else {
|
|
|
|
warn!("Tried to unlock skill for skill group with insufficient SP");
|
2020-11-15 21:05:02 +00:00
|
|
|
}
|
2020-06-29 16:22:19 +00:00
|
|
|
} else {
|
2020-12-05 22:03:42 +00:00
|
|
|
warn!("Tried to unlock skill without meeting prerequisite skills");
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-05 22:03:42 +00:00
|
|
|
warn!("Tried to unlock skill for a skill group that player does not have");
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-05 22:03:42 +00:00
|
|
|
warn!("Tried to unlock skill the player already has")
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-04 23:39:03 +00:00
|
|
|
warn!(
|
|
|
|
?skill,
|
|
|
|
"Tried to unlock skill that does not exist in any skill group!"
|
|
|
|
);
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-03 19:40:37 +00:00
|
|
|
|
2020-07-04 18:32:24 +00:00
|
|
|
/// Removes a skill from a player and refunds 1 skill point in the relevant
|
|
|
|
/// skill group.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use veloren_common::comp::skills::{Skill, SkillGroupType, SkillSet};
|
|
|
|
///
|
|
|
|
/// let mut skillset = SkillSet::new();
|
|
|
|
/// skillset.unlock_skill_group(SkillGroupType::Axes);
|
|
|
|
/// skillset.add_skill_points(SkillGroupType::Axes, 1);
|
|
|
|
/// skillset.unlock_skill(Skill::TestAxeSkill2);
|
|
|
|
///
|
|
|
|
/// skillset.refund_skill(Skill::TestAxeSkill2);
|
|
|
|
///
|
|
|
|
/// assert_eq!(skillset.skills.len(), 0);
|
|
|
|
/// ```
|
2020-07-03 19:40:37 +00:00
|
|
|
pub fn refund_skill(&mut self, skill: Skill) {
|
2020-12-04 23:39:03 +00:00
|
|
|
if self.skills.contains_key(&skill) {
|
2020-07-03 19:40:37 +00:00
|
|
|
if let Some(skill_group_type) = SkillSet::get_skill_group_type_for_skill(&skill) {
|
|
|
|
if let Some(mut skill_group) = self
|
|
|
|
.skill_groups
|
|
|
|
.iter_mut()
|
|
|
|
.find(|x| x.skill_group_type == skill_group_type)
|
|
|
|
{
|
2020-12-04 23:39:03 +00:00
|
|
|
// We know key is already contained, so unwrap is safe
|
|
|
|
let level = *(self.skills.get(&skill).unwrap());
|
|
|
|
skill_group.available_sp += skill.skill_cost(level);
|
|
|
|
if level.map_or(false, |l| l > 1) {
|
|
|
|
self.skills.insert(skill, level.map(|l| l - 1));
|
|
|
|
} else {
|
|
|
|
self.skills.remove(&skill);
|
|
|
|
}
|
2020-07-03 19:40:37 +00:00
|
|
|
} else {
|
|
|
|
warn!("Tried to refund skill for a skill group that player does not have");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
?skill,
|
|
|
|
"Tried to refund skill that does not exist in any skill group"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
warn!("Tried to refund skill that has not been unlocked");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the skill group type for a skill from the static skill group
|
2020-07-04 18:32:24 +00:00
|
|
|
/// definitions.
|
2020-07-03 19:40:37 +00:00
|
|
|
fn get_skill_group_type_for_skill(skill: &Skill) -> Option<SkillGroupType> {
|
|
|
|
SKILL_GROUP_DEFS.iter().find_map(|(key, val)| {
|
|
|
|
if val.contains(&skill) {
|
|
|
|
Some(*key)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
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.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use veloren_common::comp::skills::{SkillGroupType, SkillSet};
|
|
|
|
///
|
|
|
|
/// let mut skillset = SkillSet::new();
|
|
|
|
/// skillset.unlock_skill_group(SkillGroupType::Axes);
|
|
|
|
/// skillset.add_skill_points(SkillGroupType::Axes, 1);
|
|
|
|
///
|
|
|
|
/// assert_eq!(skillset.skill_groups[0].available_sp, 1);
|
|
|
|
/// ```
|
|
|
|
pub fn add_skill_points(
|
|
|
|
&mut self,
|
|
|
|
skill_group_type: SkillGroupType,
|
2020-11-15 21:05:02 +00:00
|
|
|
number_of_skill_points: u16,
|
2020-07-04 18:32:24 +00:00
|
|
|
) {
|
|
|
|
if let Some(mut skill_group) = self
|
|
|
|
.skill_groups
|
|
|
|
.iter_mut()
|
|
|
|
.find(|x| x.skill_group_type == skill_group_type)
|
|
|
|
{
|
|
|
|
skill_group.available_sp += number_of_skill_points;
|
|
|
|
} else {
|
|
|
|
warn!("Tried to add skill points to a skill group that player does not have");
|
|
|
|
}
|
|
|
|
}
|
2020-11-15 02:05:18 +00:00
|
|
|
|
|
|
|
/// Checks if the skill set of an entity contains a particular skill group
|
|
|
|
/// type
|
|
|
|
pub fn contains_skill_group(&self, skill_group_type: SkillGroupType) -> bool {
|
|
|
|
self.skill_groups
|
|
|
|
.iter()
|
|
|
|
.any(|x| x.skill_group_type == skill_group_type)
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:05:02 +00:00
|
|
|
/// Adds/subtracts experience to the skill group within an entity's skill
|
|
|
|
/// set
|
|
|
|
pub fn change_experience(&mut self, skill_group_type: SkillGroupType, amount: i32) {
|
2020-11-15 02:05:18 +00:00
|
|
|
if let Some(mut skill_group) = self
|
|
|
|
.skill_groups
|
|
|
|
.iter_mut()
|
|
|
|
.find(|x| x.skill_group_type == skill_group_type)
|
|
|
|
{
|
2020-11-15 21:05:02 +00:00
|
|
|
skill_group.exp = (skill_group.exp as i32 + amount) as u16;
|
2020-11-15 02:05:18 +00:00
|
|
|
} else {
|
|
|
|
warn!("Tried to add experience to a skill group that player does not have");
|
|
|
|
}
|
|
|
|
}
|
2020-11-19 01:26:55 +00:00
|
|
|
|
|
|
|
/// Checks that the skill set contains all prerequisite skills for a
|
|
|
|
/// particular skill
|
2020-12-04 23:39:03 +00:00
|
|
|
pub fn prerequisites_met(&self, skill: Skill, level: Level) -> bool {
|
|
|
|
skill.prerequisite_skills(level).iter().all(|(s, l)| {
|
|
|
|
self.skills.contains_key(s) && self.skills.get(s).map_or(false, |l_b| l_b >= l)
|
|
|
|
})
|
2020-11-19 01:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Skill {
|
|
|
|
/// Returns a vec of prerequisite skills (it should only be necessary to
|
|
|
|
/// note direct prerequisites)
|
2020-12-04 23:39:03 +00:00
|
|
|
pub fn prerequisite_skills(self, level: Level) -> HashMap<Skill, Level> {
|
|
|
|
let mut prerequisites = HashMap::new();
|
|
|
|
if let Some(level) = level {
|
|
|
|
if level > self.get_max_level().unwrap_or(0) {
|
|
|
|
// Sets a prerequisite of itself for skills beyond the max level
|
|
|
|
prerequisites.insert(self, Some(level));
|
|
|
|
} else if level > 1 {
|
|
|
|
// For skills above level 1, sets prerequisite of skill of lower level
|
|
|
|
prerequisites.insert(self, Some(level - 1));
|
|
|
|
}
|
|
|
|
}
|
2020-12-06 19:10:47 +00:00
|
|
|
if let Some(skills) = SKILL_PREREQUISITES.get(&self) {
|
|
|
|
prerequisites.extend(skills);
|
2020-11-19 01:26:55 +00:00
|
|
|
}
|
|
|
|
prerequisites
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the cost in skill points of unlocking a particular skill
|
2020-12-04 23:39:03 +00:00
|
|
|
pub fn skill_cost(self, level: Level) -> u16 {
|
|
|
|
use Skill::*;
|
|
|
|
match self {
|
2020-12-06 19:10:47 +00:00
|
|
|
General(GeneralSkill::HealthIncrease) => 1,
|
2020-12-04 23:39:03 +00:00
|
|
|
_ => level.unwrap_or(1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the maximum level a skill can reach, returns None if the skill
|
|
|
|
/// doesn't level
|
2020-12-06 19:10:47 +00:00
|
|
|
pub fn get_max_level(self) -> Option<u16> { SKILL_MAX_LEVEL.get(&self).copied().flatten() }
|
2020-07-04 18:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_refund_skill() {
|
|
|
|
let mut skillset = SkillSet::new();
|
|
|
|
skillset.unlock_skill_group(SkillGroupType::Axes);
|
|
|
|
skillset.add_skill_points(SkillGroupType::Axes, 1);
|
|
|
|
skillset.unlock_skill(Skill::TestAxeSkill2);
|
|
|
|
|
|
|
|
assert_eq!(skillset.skill_groups[0].available_sp, 0);
|
|
|
|
assert_eq!(skillset.skills.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
skillset.skills.get(&Skill::TestAxeSkill2),
|
|
|
|
Some(&Skill::TestAxeSkill2)
|
|
|
|
);
|
|
|
|
|
|
|
|
skillset.refund_skill(Skill::TestAxeSkill2);
|
|
|
|
|
|
|
|
assert_eq!(skillset.skill_groups[0].available_sp, 1);
|
|
|
|
assert_eq!(skillset.skills.get(&Skill::TestAxeSkill2), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unlock_skillgroup() {
|
|
|
|
let mut skillset = SkillSet::new();
|
|
|
|
skillset.unlock_skill_group(SkillGroupType::Axes);
|
|
|
|
|
|
|
|
assert_eq!(skillset.skill_groups.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
skillset.skill_groups[0],
|
|
|
|
SkillGroup::new(SkillGroupType::Axes)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unlock_skill() {
|
|
|
|
let mut skillset = SkillSet::new();
|
|
|
|
|
|
|
|
skillset.unlock_skill_group(SkillGroupType::Axes);
|
|
|
|
skillset.add_skill_points(SkillGroupType::Axes, 1);
|
|
|
|
|
|
|
|
assert_eq!(skillset.skill_groups[0].available_sp, 1);
|
|
|
|
assert_eq!(skillset.skills.len(), 0);
|
|
|
|
|
|
|
|
// Try unlocking a skill with enough skill points
|
|
|
|
skillset.unlock_skill(Skill::TestAxeSkill2);
|
|
|
|
|
|
|
|
assert_eq!(skillset.skill_groups[0].available_sp, 0);
|
|
|
|
assert_eq!(skillset.skills.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
skillset.skills.get(&Skill::TestAxeSkill2),
|
|
|
|
Some(&Skill::TestAxeSkill2)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Try unlocking a skill without enough skill points
|
|
|
|
skillset.unlock_skill(Skill::TestAxeSkill1);
|
|
|
|
|
|
|
|
assert_eq!(skillset.skills.len(), 1);
|
|
|
|
assert_eq!(skillset.skills.get(&Skill::TestAxeSkill1), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_skill_points() {
|
|
|
|
let mut skillset = SkillSet::new();
|
|
|
|
skillset.unlock_skill_group(SkillGroupType::Axes);
|
|
|
|
skillset.add_skill_points(SkillGroupType::Axes, 1);
|
|
|
|
|
|
|
|
assert_eq!(skillset.skill_groups[0].available_sp, 1);
|
|
|
|
}
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|