2020-12-05 22:03:42 +00:00
|
|
|
use crate::{
|
|
|
|
assets::{self, Asset, AssetExt},
|
2021-07-01 17:08:23 +00:00
|
|
|
comp::{
|
|
|
|
self,
|
|
|
|
body::{humanoid, Body},
|
|
|
|
item::tool::ToolKind,
|
|
|
|
},
|
2020-12-05 22:03:42 +00:00
|
|
|
};
|
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};
|
2021-04-14 15:35:34 +00:00
|
|
|
use specs::{Component, DerefFlaggedStorage};
|
|
|
|
use specs_idvs::IdvStorage;
|
2020-12-12 01:45:46 +00:00
|
|
|
use std::hash::Hash;
|
2021-01-05 06:03:25 +00:00
|
|
|
use tracing::{trace, warn};
|
2020-06-29 16:22:19 +00:00
|
|
|
|
2020-12-05 22:03:42 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2021-01-18 19:08:13 +00:00
|
|
|
pub struct SkillTreeMap(HashMap<SkillGroupKind, 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";
|
|
|
|
}
|
|
|
|
|
2021-01-18 19:08:13 +00:00
|
|
|
pub struct SkillGroupDef {
|
|
|
|
pub skills: HashSet<Skill>,
|
|
|
|
pub total_skill_point_cost: u16,
|
|
|
|
}
|
|
|
|
|
2020-12-06 19:10:47 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2021-01-16 17:01:57 +00:00
|
|
|
pub struct SkillLevelMap(HashMap<Skill, Option<u16>>);
|
2020-12-06 19:10:47 +00:00
|
|
|
|
|
|
|
impl Asset for SkillLevelMap {
|
|
|
|
type Loader = assets::RonLoader;
|
|
|
|
|
|
|
|
const EXTENSION: &'static str = "ron";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2021-01-16 17:01:57 +00:00
|
|
|
pub struct SkillPrerequisitesMap(HashMap<Skill, HashMap<Skill, Option<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! {
|
|
|
|
// 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.
|
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| {
|
|
|
|
if let Some(max_level) = skill.max_level() {
|
|
|
|
(1..=max_level)
|
|
|
|
.into_iter()
|
|
|
|
.map(|level| skill.skill_cost(Some(level)))
|
|
|
|
.sum()
|
|
|
|
} else {
|
|
|
|
skill.skill_cost(None)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.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;
|
|
|
|
map.iter().map(|(sgk, skills)| skills.into_iter().map(move |s| (*s, *sgk))).flatten().collect()
|
2020-12-06 19:10:47 +00:00
|
|
|
};
|
|
|
|
// Loads the maximum level that a skill can obtain
|
2021-01-16 17:01:57 +00:00
|
|
|
pub static ref SKILL_MAX_LEVEL: HashMap<Skill, Option<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-01-16 17:01:57 +00:00
|
|
|
pub static ref SKILL_PREREQUISITES: HashMap<Skill, HashMap<Skill, Option<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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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),
|
2021-01-18 19:08:13 +00:00
|
|
|
UnlockGroup(SkillGroupKind),
|
2020-12-31 18:37:25 +00:00
|
|
|
Roll(RollSkill),
|
2021-03-20 16:13:59 +00:00
|
|
|
Climb(ClimbSkill),
|
2021-03-21 14:58:38 +00:00
|
|
|
Swim(SwimSkill),
|
2021-06-09 19:07:34 +00:00
|
|
|
Pick(MiningSkill),
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 17:08:23 +00:00
|
|
|
/// Enum which returned as result from `boost` function
|
|
|
|
/// `Number` can represent values from -inf to +inf,
|
|
|
|
/// but it should generaly be in range -50..50
|
|
|
|
///
|
|
|
|
/// Number(-25) says that some value
|
|
|
|
/// will be reduced by 25% (for example energy consumption)
|
|
|
|
///
|
|
|
|
/// Number(15) says that some value
|
|
|
|
/// will be increased by 15% (for example damage)
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum BoostValue {
|
|
|
|
Number(i16),
|
|
|
|
NonDescriptive,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BoostValue {
|
|
|
|
pub fn as_mult_maybe(self) -> Option<f32> {
|
|
|
|
match self {
|
|
|
|
Self::Number(x) => Some(1.0 + x as f32 * 0.01),
|
|
|
|
Self::NonDescriptive => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_i16_maybe(self) -> Option<i16> {
|
|
|
|
match self {
|
|
|
|
Self::Number(x) => Some(x),
|
|
|
|
Self::NonDescriptive => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<i16> for BoostValue {
|
|
|
|
fn from(number: i16) -> Self { BoostValue::Number(number) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn adjust_with_level(skillset: &SkillSet, skill: Skill, effect: impl FnOnce(f32, u16)) {
|
2021-07-03 19:11:46 +00:00
|
|
|
// NOTE: We are unwrapping before checking skill level,
|
|
|
|
// because if it falls we want know it even if we don't have this level
|
|
|
|
let multiplier = match skill.boost().as_mult_maybe() {
|
|
|
|
Some(m) => m,
|
|
|
|
None => return invalid_skill_boost(skill),
|
|
|
|
};
|
2021-07-01 17:08:23 +00:00
|
|
|
if let Ok(Some(level)) = skillset.skill_level(skill) {
|
2021-07-03 19:11:46 +00:00
|
|
|
effect(multiplier, level);
|
2021-07-01 17:08:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn adjust_counter_with_level(skillset: &SkillSet, skill: Skill, effect: impl FnOnce(i16, i16)) {
|
2021-07-03 19:11:46 +00:00
|
|
|
// NOTE: We are unwrapping before checking skill level,
|
|
|
|
// because if it falls we want know it even if we don't have this level
|
|
|
|
let counter = match skill.boost().as_i16_maybe() {
|
|
|
|
Some(c) => c,
|
|
|
|
None => return invalid_skill_boost(skill),
|
|
|
|
};
|
2021-07-01 17:08:23 +00:00
|
|
|
if let Ok(Some(level)) = skillset.skill_level(skill) {
|
2021-07-03 19:11:46 +00:00
|
|
|
effect(counter, level as i16);
|
2021-07-01 17:08:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_if_has(skillset: &SkillSet, skill: Skill, effect: impl FnOnce(f32)) {
|
2021-07-03 19:11:46 +00:00
|
|
|
// NOTE: We are unwrapping before checking skill level,
|
|
|
|
// because if it falls we want know it even if we don't have this level
|
|
|
|
let multiplier = match skill.boost().as_mult_maybe() {
|
|
|
|
Some(c) => c,
|
|
|
|
None => return invalid_skill_boost(skill),
|
|
|
|
};
|
2021-07-01 17:08:23 +00:00
|
|
|
if skillset.has_skill(skill) {
|
2021-07-03 19:11:46 +00:00
|
|
|
effect(multiplier);
|
2021-07-01 17:08:23 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-03 19:11:46 +00:00
|
|
|
|
|
|
|
#[track_caller]
|
2021-07-01 17:08:23 +00:00
|
|
|
pub fn invalid_skill_boost(skill: Skill) {
|
|
|
|
let err_msg = format!(
|
2021-07-03 19:11:46 +00:00
|
|
|
r#"
|
|
|
|
|
|
|
|
{:?} produced unexpected BoostValue: {:?}
|
|
|
|
|
|
|
|
Clearly that shouldn't happen and tests should catch this.
|
|
|
|
|
|
|
|
If they didn't, probably because we've added new skills/weapons.
|
|
|
|
In this case, please find `test_adjusting_skills`,
|
|
|
|
fix tests and fix corresponding `impl Boost` for this skill.
|
|
|
|
|
|
|
|
"#,
|
2021-07-01 17:08:23 +00:00
|
|
|
skill,
|
|
|
|
skill.boost()
|
|
|
|
);
|
|
|
|
common_base::dev_panic!(err_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns value which corresponds to the boost given by this skill
|
|
|
|
pub trait Boost {
|
|
|
|
fn boost(self) -> BoostValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Boost for Skill {
|
|
|
|
fn boost(self) -> BoostValue {
|
|
|
|
match self {
|
|
|
|
// General tree boosts
|
|
|
|
Skill::General(s) => s.boost(),
|
|
|
|
// Weapon tree boosts
|
|
|
|
Skill::Sword(s) => s.boost(),
|
|
|
|
Skill::Axe(s) => s.boost(),
|
|
|
|
Skill::Hammer(s) => s.boost(),
|
|
|
|
Skill::Bow(s) => s.boost(),
|
|
|
|
Skill::Staff(s) => s.boost(),
|
|
|
|
Skill::Sceptre(s) => s.boost(),
|
|
|
|
|
|
|
|
// Movement tree boosts
|
|
|
|
Skill::Roll(s) => s.boost(),
|
|
|
|
Skill::Climb(s) => s.boost(),
|
|
|
|
Skill::Swim(s) => s.boost(),
|
|
|
|
// Non-combat tree boosts
|
|
|
|
Skill::Pick(s) => s.boost(),
|
|
|
|
// Unlock Group has more complex semantic
|
|
|
|
Skill::UnlockGroup(_) => BoostValue::NonDescriptive,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 17:01:57 +00:00
|
|
|
pub enum SkillError {
|
|
|
|
MissingSkill,
|
|
|
|
}
|
|
|
|
|
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,
|
2021-04-18 22:09:57 +00:00
|
|
|
DInfinite, // Represents charge through, not migrated because laziness
|
2020-12-06 19:10:47 +00:00
|
|
|
// Spin upgrades
|
2021-01-13 08:11:31 +00:00
|
|
|
UnlockSpin,
|
2020-12-06 19:10:47 +00:00
|
|
|
SDamage,
|
|
|
|
SSpeed,
|
|
|
|
SCost,
|
|
|
|
SSpins,
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 17:08:23 +00:00
|
|
|
impl Boost for SwordSkill {
|
|
|
|
fn boost(self) -> BoostValue {
|
|
|
|
match self {
|
|
|
|
// Dash
|
|
|
|
Self::DDamage => 20.into(),
|
|
|
|
Self::DCost => (-25_i16).into(),
|
|
|
|
Self::DDrain => (-25_i16).into(),
|
|
|
|
Self::DScaling => 20.into(),
|
|
|
|
Self::DSpeed => 15.into(),
|
|
|
|
// Spin
|
|
|
|
Self::SDamage => 40.into(),
|
|
|
|
Self::SSpeed => (-20_i16).into(),
|
|
|
|
Self::SCost => (-25_i16).into(),
|
|
|
|
// Non-descriptive values
|
|
|
|
Self::InterruptingAttacks
|
|
|
|
| Self::TsCombo
|
|
|
|
| Self::TsDamage
|
|
|
|
| Self::TsRegen
|
|
|
|
| Self::TsSpeed
|
|
|
|
| Self::DInfinite
|
|
|
|
| Self::UnlockSpin
|
|
|
|
| Self::SSpins => BoostValue::NonDescriptive,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2021-01-13 08:11:31 +00:00
|
|
|
UnlockLeap,
|
2020-12-19 22:07:02 +00:00
|
|
|
LDamage,
|
|
|
|
LKnockback,
|
|
|
|
LCost,
|
|
|
|
LDistance,
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 17:08:23 +00:00
|
|
|
impl Boost for AxeSkill {
|
|
|
|
fn boost(self) -> BoostValue {
|
|
|
|
match self {
|
|
|
|
// Spin upgrades
|
|
|
|
Self::SDamage => 30.into(),
|
|
|
|
Self::SSpeed => (-20_i16).into(),
|
|
|
|
Self::SCost => (-25_i16).into(),
|
|
|
|
// Leap upgrades
|
|
|
|
Self::LDamage => 35.into(),
|
|
|
|
Self::LKnockback => 40.into(),
|
|
|
|
Self::LCost => (-25_i16).into(),
|
|
|
|
Self::LDistance => 20.into(),
|
|
|
|
// Non-descriptive boosts
|
|
|
|
Self::UnlockLeap
|
|
|
|
| Self::DsCombo
|
|
|
|
| Self::DsDamage
|
|
|
|
| Self::DsSpeed
|
|
|
|
| Self::DsRegen
|
|
|
|
| Self::SInfinite
|
|
|
|
| Self::SHelicopter => BoostValue::NonDescriptive,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2021-01-13 08:11:31 +00:00
|
|
|
UnlockLeap,
|
2020-12-20 22:28:17 +00:00
|
|
|
LDamage,
|
|
|
|
LCost,
|
|
|
|
LDistance,
|
|
|
|
LKnockback,
|
|
|
|
LRange,
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 17:08:23 +00:00
|
|
|
impl Boost for HammerSkill {
|
|
|
|
fn boost(self) -> BoostValue {
|
|
|
|
match self {
|
|
|
|
// Single strike upgrades
|
|
|
|
Self::SsKnockback => 50.into(),
|
|
|
|
// Charged melee upgrades
|
|
|
|
Self::CDamage => 25.into(),
|
|
|
|
Self::CKnockback => 50.into(),
|
|
|
|
Self::CDrain => (-25_i16).into(),
|
|
|
|
Self::CSpeed => 25.into(),
|
|
|
|
// Leap upgrades
|
|
|
|
Self::LDamage => 40.into(),
|
|
|
|
Self::LKnockback => 50.into(),
|
|
|
|
Self::LCost => (-25_i16).into(),
|
|
|
|
Self::LDistance => 25.into(),
|
|
|
|
Self::LRange => 1.into(),
|
|
|
|
// Non-descriptive values
|
|
|
|
Self::UnlockLeap | Self::SsDamage | Self::SsSpeed | Self::SsRegen => {
|
|
|
|
BoostValue::NonDescriptive
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2021-05-22 05:05:27 +00:00
|
|
|
// Charged upgrades
|
2020-12-23 02:28:55 +00:00
|
|
|
CDamage,
|
2021-05-22 05:05:27 +00:00
|
|
|
CRegen,
|
2020-12-23 02:28:55 +00:00
|
|
|
CKnockback,
|
|
|
|
CSpeed,
|
|
|
|
CMove,
|
|
|
|
// Repeater upgrades
|
|
|
|
RDamage,
|
|
|
|
RCost,
|
2021-05-22 05:05:27 +00:00
|
|
|
RSpeed,
|
|
|
|
// Shotgun upgrades
|
|
|
|
UnlockShotgun,
|
|
|
|
SDamage,
|
|
|
|
SCost,
|
|
|
|
SArrows,
|
|
|
|
SSpread,
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 17:08:23 +00:00
|
|
|
impl Boost for BowSkill {
|
|
|
|
fn boost(self) -> BoostValue {
|
|
|
|
match self {
|
|
|
|
// Passive
|
|
|
|
Self::ProjSpeed => 20.into(),
|
|
|
|
// Charged upgrades
|
|
|
|
Self::CDamage => 20.into(),
|
|
|
|
Self::CRegen => 20.into(),
|
|
|
|
Self::CKnockback => 20.into(),
|
|
|
|
Self::CSpeed => 10.into(),
|
|
|
|
Self::CMove => 10.into(),
|
|
|
|
// Repeater upgrades
|
|
|
|
Self::RDamage => 20.into(),
|
|
|
|
Self::RCost => (-20_i16).into(),
|
|
|
|
Self::RSpeed => 20.into(),
|
|
|
|
// Shotgun upgrades
|
|
|
|
Self::SDamage => 20.into(),
|
|
|
|
Self::SCost => (-20_i16).into(),
|
|
|
|
Self::SArrows => 1.into(),
|
|
|
|
Self::SSpread => (-20_i16).into(),
|
|
|
|
// Non-descriptive values
|
|
|
|
Self::UnlockShotgun => BoostValue::NonDescriptive,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-01 17:08:23 +00:00
|
|
|
impl Boost for StaffSkill {
|
|
|
|
fn boost(self) -> BoostValue {
|
|
|
|
match self {
|
|
|
|
// Fireball upgrades
|
|
|
|
Self::BDamage => 20.into(),
|
|
|
|
Self::BRegen => 20.into(),
|
|
|
|
Self::BRadius => 15.into(),
|
|
|
|
// Flamethrower upgrades
|
|
|
|
Self::FDamage => 30.into(),
|
|
|
|
Self::FRange => 25.into(),
|
|
|
|
Self::FDrain => (-20_i16).into(),
|
|
|
|
Self::FVelocity => 25.into(),
|
|
|
|
// Shockwave upgrades
|
|
|
|
Self::SDamage => 30.into(),
|
|
|
|
Self::SKnockback => 30.into(),
|
|
|
|
Self::SRange => 20.into(),
|
|
|
|
Self::SCost => (-20_i16).into(),
|
|
|
|
// Non-descriptive values
|
|
|
|
Self::UnlockShockwave => BoostValue::NonDescriptive,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 02:05:18 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum SceptreSkill {
|
2021-03-07 02:16:35 +00:00
|
|
|
// Lifesteal beam upgrades
|
|
|
|
LDamage,
|
|
|
|
LRange,
|
|
|
|
LLifesteal,
|
|
|
|
LRegen,
|
|
|
|
// Healing beam upgrades
|
|
|
|
HHeal,
|
|
|
|
HRange,
|
2021-07-10 22:08:21 +00:00
|
|
|
HDuration,
|
|
|
|
HCost,
|
2021-03-07 02:16:35 +00:00
|
|
|
// Warding aura upgrades
|
|
|
|
UnlockAura,
|
|
|
|
AStrength,
|
|
|
|
ADuration,
|
|
|
|
ARange,
|
|
|
|
ACost,
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 17:08:23 +00:00
|
|
|
impl Boost for SceptreSkill {
|
|
|
|
fn boost(self) -> BoostValue {
|
|
|
|
match self {
|
|
|
|
// Lifesteal beam upgrades
|
|
|
|
Self::LDamage => 20.into(),
|
|
|
|
Self::LRange => 20.into(),
|
|
|
|
Self::LRegen => 20.into(),
|
|
|
|
Self::LLifesteal => 15.into(),
|
|
|
|
// Healing beam upgrades
|
|
|
|
Self::HHeal => 20.into(),
|
|
|
|
Self::HRange => 20.into(),
|
2021-07-10 22:08:21 +00:00
|
|
|
Self::HDuration => 20.into(),
|
2021-07-01 17:08:23 +00:00
|
|
|
Self::HCost => (-20_i16).into(),
|
|
|
|
// Warding aura upgrades
|
|
|
|
Self::AStrength => 15.into(),
|
|
|
|
Self::ADuration => 20.into(),
|
|
|
|
Self::ARange => 25.into(),
|
|
|
|
Self::ACost => (-15_i16).into(),
|
|
|
|
Self::UnlockAura => BoostValue::NonDescriptive,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-31 18:37:25 +00:00
|
|
|
EnergyIncrease,
|
|
|
|
}
|
|
|
|
|
2021-07-01 17:08:23 +00:00
|
|
|
impl Boost for GeneralSkill {
|
|
|
|
fn boost(self) -> BoostValue {
|
|
|
|
// NOTE: These should be used only for UI.
|
|
|
|
// Source of truth are corresponding systems
|
|
|
|
match self {
|
|
|
|
Self::HealthIncrease => {
|
|
|
|
let health_increase =
|
|
|
|
(Body::Humanoid(humanoid::Body::random()).base_health_increase() / 10) as i16;
|
|
|
|
health_increase.into()
|
|
|
|
},
|
|
|
|
Self::EnergyIncrease => {
|
|
|
|
let energy_increase = (comp::energy::ENERGY_PER_LEVEL / 10) as i16;
|
|
|
|
energy_increase.into()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 18:37:25 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum RollSkill {
|
|
|
|
Cost,
|
|
|
|
Strength,
|
|
|
|
Duration,
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 17:08:23 +00:00
|
|
|
impl Boost for RollSkill {
|
|
|
|
fn boost(self) -> BoostValue {
|
|
|
|
match self {
|
|
|
|
Self::Cost => (-10_i16).into(),
|
|
|
|
Self::Strength => 10.into(),
|
|
|
|
Self::Duration => 10.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 16:13:59 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum ClimbSkill {
|
|
|
|
Cost,
|
|
|
|
Speed,
|
|
|
|
}
|
|
|
|
|
2021-07-01 17:08:23 +00:00
|
|
|
impl Boost for ClimbSkill {
|
|
|
|
fn boost(self) -> BoostValue {
|
|
|
|
match self {
|
|
|
|
Self::Cost => (-20_i16).into(),
|
|
|
|
Self::Speed => 20.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 14:58:38 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub enum SwimSkill {
|
|
|
|
Speed,
|
|
|
|
}
|
|
|
|
|
2021-07-01 17:08:23 +00:00
|
|
|
impl Boost for SwimSkill {
|
|
|
|
fn boost(self) -> BoostValue {
|
|
|
|
match self {
|
|
|
|
Self::Speed => 40.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 05:14:20 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
2021-06-09 19:07:34 +00:00
|
|
|
pub enum MiningSkill {
|
2021-06-09 05:14:20 +00:00
|
|
|
Speed,
|
|
|
|
OreGain,
|
|
|
|
GemGain,
|
|
|
|
}
|
|
|
|
|
2021-07-01 17:08:23 +00:00
|
|
|
impl Boost for MiningSkill {
|
|
|
|
fn boost(self) -> BoostValue {
|
|
|
|
match self {
|
|
|
|
Self::Speed => 10.into(),
|
|
|
|
Self::OreGain => 5.into(),
|
|
|
|
Self::GemGain => 5.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 16:22:19 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
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-01-07 21:02:50 +00:00
|
|
|
pub fn skill_point_cost(self, level: u16) -> u16 {
|
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-01-16 17:01:57 +00:00
|
|
|
.floor()) as u16
|
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,
|
2020-11-15 21:05:02 +00:00
|
|
|
pub exp: u16,
|
|
|
|
pub available_sp: u16,
|
2021-01-02 04:11:30 +00:00
|
|
|
pub earned_sp: u16,
|
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,
|
2020-06-29 16:22:19 +00:00
|
|
|
exp: 0,
|
|
|
|
available_sp: 0,
|
2021-01-02 04:11:30 +00:00
|
|
|
earned_sp: 0,
|
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 {
|
2020-07-03 19:40:37 +00:00
|
|
|
pub skill_groups: Vec<SkillGroup>,
|
2021-01-16 17:01:57 +00:00
|
|
|
pub skills: HashMap<Skill, Option<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)),
|
|
|
|
],
|
2020-12-04 23:39:03 +00:00
|
|
|
skills: HashMap::new(),
|
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 {
|
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
|
|
|
/// use veloren_common::comp::{
|
|
|
|
/// item::tool::ToolKind,
|
|
|
|
/// skills::{SkillGroupKind, SkillSet},
|
|
|
|
/// };
|
2020-07-04 18:32:24 +00:00
|
|
|
///
|
2021-01-18 19:08:13 +00:00
|
|
|
/// let mut skillset = SkillSet::default();
|
|
|
|
/// skillset.unlock_skill_group(SkillGroupKind::Weapon(ToolKind::Sword));
|
2020-07-04 18:32:24 +00:00
|
|
|
///
|
2021-06-09 05:14:20 +00:00
|
|
|
/// assert_eq!(skillset.skill_groups.len(), 3);
|
2020-07-04 18:32:24 +00:00
|
|
|
/// ```
|
2021-01-18 19:08:13 +00:00
|
|
|
pub fn unlock_skill_group(&mut self, skill_group_kind: SkillGroupKind) {
|
|
|
|
if !self.contains_skill_group(skill_group_kind) {
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
///
|
|
|
|
/// ```
|
2021-01-18 19:08:13 +00:00
|
|
|
/// use veloren_common::comp::skills::{GeneralSkill, Skill, SkillGroupKind, SkillSet};
|
2020-07-04 18:32:24 +00:00
|
|
|
///
|
2021-01-18 19:08:13 +00:00
|
|
|
/// let mut skillset = SkillSet::default();
|
|
|
|
/// skillset.add_skill_points(SkillGroupKind::General, 1);
|
2020-07-04 18:32:24 +00:00
|
|
|
///
|
2021-01-18 19:08:13 +00:00
|
|
|
/// skillset.unlock_skill(Skill::General(GeneralSkill::HealthIncrease));
|
2020-07-04 18:32:24 +00:00
|
|
|
///
|
|
|
|
/// assert_eq!(skillset.skills.len(), 1);
|
|
|
|
/// ```
|
2020-07-03 19:40:37 +00:00
|
|
|
pub fn unlock_skill(&mut self, skill: Skill) {
|
2021-01-18 19:08:13 +00:00
|
|
|
if let Some(skill_group_kind) = skill.skill_group_kind() {
|
2021-01-16 17:01:57 +00:00
|
|
|
let next_level = self.next_skill_level(skill);
|
2021-01-05 06:03:25 +00:00
|
|
|
let prerequisites_met = self.prerequisites_met(skill);
|
2021-01-16 17:01:57 +00:00
|
|
|
if !matches!(self.skills.get(&skill), Some(level) if *level == skill.max_level()) {
|
2021-01-18 19:08:13 +00:00
|
|
|
if let Some(mut skill_group) = self.skill_group_mut(skill_group_kind) {
|
2020-12-05 22:03:42 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-12-31 18:37:25 +00:00
|
|
|
if matches!(skill, Skill::General(GeneralSkill::HealthIncrease)) {
|
|
|
|
self.modify_health = true;
|
|
|
|
}
|
|
|
|
if matches!(skill, Skill::General(GeneralSkill::EnergyIncrease)) {
|
|
|
|
self.modify_energy = true;
|
|
|
|
}
|
2020-12-05 22:03:42 +00:00
|
|
|
self.skills.insert(skill, next_level);
|
|
|
|
} else {
|
2021-01-05 06:03:25 +00:00
|
|
|
trace!("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 {
|
2021-01-05 06:03:25 +00:00
|
|
|
trace!("Tried to unlock skill without meeting prerequisite skills");
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-05 06:03:25 +00:00
|
|
|
trace!("Tried to unlock skill for a skill group that player does not have");
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-05 06:03:25 +00:00
|
|
|
trace!("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.
|
|
|
|
///
|
|
|
|
/// ```
|
2021-01-18 19:08:13 +00:00
|
|
|
/// use veloren_common::comp::skills::{GeneralSkill, Skill, SkillGroupKind, SkillSet};
|
2020-07-04 18:32:24 +00:00
|
|
|
///
|
2021-01-18 19:08:13 +00:00
|
|
|
/// let mut skillset = SkillSet::default();
|
|
|
|
/// skillset.add_skill_points(SkillGroupKind::General, 1);
|
|
|
|
/// skillset.unlock_skill(Skill::General(GeneralSkill::HealthIncrease));
|
2020-07-04 18:32:24 +00:00
|
|
|
///
|
2021-01-18 19:08:13 +00:00
|
|
|
/// skillset.refund_skill(Skill::General(GeneralSkill::HealthIncrease));
|
2020-07-04 18:32:24 +00:00
|
|
|
///
|
|
|
|
/// assert_eq!(skillset.skills.len(), 0);
|
|
|
|
/// ```
|
2020-07-03 19:40:37 +00:00
|
|
|
pub fn refund_skill(&mut self, skill: Skill) {
|
2021-01-18 19:08:13 +00:00
|
|
|
if let Ok(level) = self.skill_level(skill) {
|
|
|
|
if let Some(skill_group_kind) = skill.skill_group_kind() {
|
|
|
|
if let Some(mut skill_group) = self.skill_group_mut(skill_group_kind) {
|
|
|
|
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-12-04 23:39:03 +00:00
|
|
|
}
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
///
|
|
|
|
/// ```
|
2021-01-18 19:08:13 +00:00
|
|
|
/// use veloren_common::comp::skills::{SkillGroupKind, SkillSet};
|
2020-07-04 18:32:24 +00:00
|
|
|
///
|
2021-01-18 19:08:13 +00:00
|
|
|
/// let mut skillset = SkillSet::default();
|
|
|
|
/// skillset.add_skill_points(SkillGroupKind::General, 1);
|
2020-07-04 18:32:24 +00:00
|
|
|
///
|
|
|
|
/// assert_eq!(skillset.skill_groups[0].available_sp, 1);
|
|
|
|
/// ```
|
|
|
|
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-01-18 19:08:13 +00:00
|
|
|
if let Some(mut skill_group) = self.skill_group_mut(skill_group_kind) {
|
2021-01-08 20:53:52 +00:00
|
|
|
skill_group.available_sp = skill_group
|
|
|
|
.available_sp
|
|
|
|
.saturating_add(number_of_skill_points);
|
2021-01-08 01:27:23 +00:00
|
|
|
skill_group.earned_sp = skill_group.earned_sp.saturating_add(number_of_skill_points);
|
2020-07-04 18:32:24 +00:00
|
|
|
} else {
|
|
|
|
warn!("Tried to add skill points to a skill group that player does not have");
|
|
|
|
}
|
|
|
|
}
|
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-01-18 19:08:13 +00:00
|
|
|
pub fn earn_skill_point(&mut self, skill_group_kind: SkillGroupKind) {
|
|
|
|
let sp_cost = self.skill_point_cost(skill_group_kind);
|
|
|
|
if let Some(mut skill_group) = self.skill_group_mut(skill_group_kind) {
|
2021-01-16 17:01:57 +00:00
|
|
|
skill_group.exp = skill_group.exp.saturating_sub(sp_cost);
|
|
|
|
skill_group.available_sp = skill_group.available_sp.saturating_add(1);
|
|
|
|
skill_group.earned_sp = skill_group.earned_sp.saturating_add(1);
|
|
|
|
}
|
2021-01-08 20:53:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 02:05:18 +00:00
|
|
|
/// Checks if the skill set of an entity contains a particular skill group
|
|
|
|
/// type
|
2021-01-18 19:08:13 +00:00
|
|
|
pub fn contains_skill_group(&self, skill_group_kind: SkillGroupKind) -> bool {
|
2020-11-15 02:05:18 +00:00
|
|
|
self.skill_groups
|
|
|
|
.iter()
|
2021-01-18 19:08:13 +00:00
|
|
|
.any(|x| x.skill_group_kind == skill_group_kind)
|
2020-11-15 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 21:05:02 +00:00
|
|
|
/// Adds/subtracts experience to the skill group within an entity's skill
|
|
|
|
/// set
|
2021-01-18 19:08:13 +00:00
|
|
|
pub fn change_experience(&mut self, skill_group_kind: SkillGroupKind, amount: i32) {
|
|
|
|
if let Some(mut skill_group) = self.skill_group_mut(skill_group_kind) {
|
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
|
2021-01-05 06:03:25 +00:00
|
|
|
pub fn prerequisites_met(&self, skill: Skill) -> bool {
|
2021-01-18 19:08:13 +00:00
|
|
|
skill
|
|
|
|
.prerequisite_skills()
|
|
|
|
.all(|(s, l)| self.skill_level(s).map_or(false, |l_b| l_b >= l))
|
2020-11-19 01:26:55 +00:00
|
|
|
}
|
2021-01-02 04:11:30 +00:00
|
|
|
|
2021-01-18 19:08:13 +00:00
|
|
|
/// Returns a reference to a particular skill group in a skillset
|
|
|
|
fn skill_group(&self, skill_group: SkillGroupKind) -> Option<&SkillGroup> {
|
2021-01-16 17:01:57 +00:00
|
|
|
self.skill_groups
|
2021-01-02 04:11:30 +00:00
|
|
|
.iter()
|
2021-01-18 19:08:13 +00:00
|
|
|
.find(|s_g| s_g.skill_group_kind == skill_group)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to a particular skill group in a skillset
|
|
|
|
fn skill_group_mut(&mut self, skill_group: SkillGroupKind) -> Option<&mut SkillGroup> {
|
|
|
|
self.skill_groups
|
|
|
|
.iter_mut()
|
|
|
|
.find(|s_g| s_g.skill_group_kind == skill_group)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the available experience for a particular skill group
|
2021-01-18 19:08:13 +00:00
|
|
|
pub fn experience(&self, skill_group: SkillGroupKind) -> u16 {
|
|
|
|
self.skill_group(skill_group).map_or(0, |s_g| s_g.exp)
|
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-01-05 06:03:25 +00:00
|
|
|
if let Some(skill_group) = self
|
|
|
|
.skill_groups
|
|
|
|
.iter()
|
2021-01-18 19:08:13 +00:00
|
|
|
.find(|x| x.skill_group_kind == skill_group_kind)
|
2021-01-05 06:03:25 +00:00
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-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
|
|
|
|
|
|
|
/// Checks how much experience is needed for the next skill point in a tree
|
2021-01-18 19:08:13 +00:00
|
|
|
pub fn skill_point_cost(&self, skill_group: SkillGroupKind) -> u16 {
|
|
|
|
if let Some(level) = self.skill_group(skill_group).map(|sg| sg.earned_sp) {
|
2021-01-07 21:02:50 +00:00
|
|
|
skill_group.skill_point_cost(level)
|
|
|
|
} else {
|
|
|
|
skill_group.skill_point_cost(0)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
pub fn skill_level(&self, skill: Skill) -> Result<Option<u16>, SkillError> {
|
|
|
|
if let Some(level) = self.skills.get(&skill).copied() {
|
|
|
|
Ok(level)
|
|
|
|
} else {
|
|
|
|
Err(SkillError::MissingSkill)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks the next level of a skill
|
|
|
|
fn next_skill_level(&self, skill: Skill) -> Option<u16> {
|
|
|
|
if let Ok(level) = self.skill_level(skill) {
|
|
|
|
level.map(|l| l + 1)
|
|
|
|
} else {
|
|
|
|
skill.max_level().map(|_| 1)
|
|
|
|
}
|
|
|
|
}
|
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)
|
2021-01-18 19:08:13 +00:00
|
|
|
pub fn prerequisite_skills(&self) -> impl Iterator<Item = (Skill, Option<u16>)> {
|
|
|
|
SKILL_PREREQUISITES
|
2021-07-11 18:41:52 +00:00
|
|
|
.get(self)
|
2021-01-18 19:08:13 +00:00
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
|
|
|
.map(|(skill, level)| (*skill, *level))
|
2020-11-19 01:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the cost in skill points of unlocking a particular skill
|
2021-01-18 19:08:13 +00:00
|
|
|
pub fn skill_cost(&self, level: Option<u16>) -> u16 {
|
2021-01-04 19:29:15 +00:00
|
|
|
// TODO: Better balance the costs later
|
|
|
|
level.unwrap_or(1)
|
2020-12-04 23:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the maximum level a skill can reach, returns None if the skill
|
|
|
|
/// doesn't level
|
2021-07-11 18:41:52 +00:00
|
|
|
pub fn max_level(&self) -> Option<u16> { SKILL_MAX_LEVEL.get(self).copied().flatten() }
|
2021-01-07 19:47:29 +00:00
|
|
|
|
|
|
|
/// Returns the skill group type for a skill from the static skill group
|
|
|
|
/// definitions.
|
2021-01-18 19:08:13 +00:00
|
|
|
pub fn skill_group_kind(&self) -> Option<SkillGroupKind> {
|
2021-07-11 18:41:52 +00:00
|
|
|
SKILL_GROUP_LOOKUP.get(self).copied()
|
2021-01-07 19:47:29 +00:00
|
|
|
}
|
2020-07-04 18:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_refund_skill() {
|
2021-01-09 16:56:19 +00:00
|
|
|
let mut skillset = SkillSet::default();
|
2021-01-18 19:08:13 +00:00
|
|
|
skillset.unlock_skill_group(SkillGroupKind::Weapon(ToolKind::Axe));
|
|
|
|
skillset.add_skill_points(SkillGroupKind::Weapon(ToolKind::Axe), 1);
|
2021-01-13 08:11:31 +00:00
|
|
|
skillset.unlock_skill(Skill::Axe(AxeSkill::UnlockLeap));
|
2020-07-04 18:32:24 +00:00
|
|
|
|
2021-06-09 05:14:20 +00:00
|
|
|
assert_eq!(skillset.skill_groups[2].available_sp, 0);
|
2020-07-04 18:32:24 +00:00
|
|
|
assert_eq!(skillset.skills.len(), 1);
|
2021-05-16 08:03:09 +00:00
|
|
|
assert!(skillset.has_skill(Skill::Axe(AxeSkill::UnlockLeap)));
|
2020-07-04 18:32:24 +00:00
|
|
|
|
2021-01-13 08:11:31 +00:00
|
|
|
skillset.refund_skill(Skill::Axe(AxeSkill::UnlockLeap));
|
2020-07-04 18:32:24 +00:00
|
|
|
|
2021-06-09 05:14:20 +00:00
|
|
|
assert_eq!(skillset.skill_groups[2].available_sp, 1);
|
2021-01-13 08:11:31 +00:00
|
|
|
assert_eq!(skillset.skills.get(&Skill::Axe(AxeSkill::UnlockLeap)), None);
|
2020-07-04 18:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unlock_skillgroup() {
|
2021-01-09 16:56:19 +00:00
|
|
|
let mut skillset = SkillSet::default();
|
2021-01-18 19:08:13 +00:00
|
|
|
skillset.unlock_skill_group(SkillGroupKind::Weapon(ToolKind::Axe));
|
2020-07-04 18:32:24 +00:00
|
|
|
|
2021-06-09 05:14:20 +00:00
|
|
|
assert_eq!(skillset.skill_groups.len(), 3);
|
2020-07-04 18:32:24 +00:00
|
|
|
assert_eq!(
|
2021-06-09 05:14:20 +00:00
|
|
|
skillset.skill_groups[2],
|
2021-01-18 19:08:13 +00:00
|
|
|
SkillGroup::new(SkillGroupKind::Weapon(ToolKind::Axe))
|
2020-07-04 18:32:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unlock_skill() {
|
2021-01-09 16:56:19 +00:00
|
|
|
let mut skillset = SkillSet::default();
|
2020-07-04 18:32:24 +00:00
|
|
|
|
2021-01-18 19:08:13 +00:00
|
|
|
skillset.unlock_skill_group(SkillGroupKind::Weapon(ToolKind::Axe));
|
|
|
|
skillset.add_skill_points(SkillGroupKind::Weapon(ToolKind::Axe), 1);
|
2020-07-04 18:32:24 +00:00
|
|
|
|
2021-06-09 05:14:20 +00:00
|
|
|
assert_eq!(skillset.skill_groups[2].available_sp, 1);
|
2020-07-04 18:32:24 +00:00
|
|
|
assert_eq!(skillset.skills.len(), 0);
|
|
|
|
|
|
|
|
// Try unlocking a skill with enough skill points
|
2021-01-13 08:11:31 +00:00
|
|
|
skillset.unlock_skill(Skill::Axe(AxeSkill::UnlockLeap));
|
2020-07-04 18:32:24 +00:00
|
|
|
|
2021-06-09 05:14:20 +00:00
|
|
|
assert_eq!(skillset.skill_groups[2].available_sp, 0);
|
2020-07-04 18:32:24 +00:00
|
|
|
assert_eq!(skillset.skills.len(), 1);
|
2021-05-16 08:03:09 +00:00
|
|
|
assert!(skillset.has_skill(Skill::Axe(AxeSkill::UnlockLeap)));
|
2020-07-04 18:32:24 +00:00
|
|
|
|
|
|
|
// Try unlocking a skill without enough skill points
|
2021-01-09 16:56:19 +00:00
|
|
|
skillset.unlock_skill(Skill::Axe(AxeSkill::DsCombo));
|
2020-07-04 18:32:24 +00:00
|
|
|
|
|
|
|
assert_eq!(skillset.skills.len(), 1);
|
2021-01-09 16:56:19 +00:00
|
|
|
assert_eq!(skillset.skills.get(&Skill::Axe(AxeSkill::DsCombo)), None);
|
2020-07-04 18:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_skill_points() {
|
2021-01-09 16:56:19 +00:00
|
|
|
let mut skillset = SkillSet::default();
|
2021-01-18 19:08:13 +00:00
|
|
|
skillset.unlock_skill_group(SkillGroupKind::Weapon(ToolKind::Axe));
|
|
|
|
skillset.add_skill_points(SkillGroupKind::Weapon(ToolKind::Axe), 1);
|
2020-07-04 18:32:24 +00:00
|
|
|
|
2021-06-09 05:14:20 +00:00
|
|
|
assert_eq!(skillset.skill_groups[2].available_sp, 1);
|
2020-07-04 18:32:24 +00:00
|
|
|
}
|
2020-06-29 16:22:19 +00:00
|
|
|
}
|