mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
476 lines
17 KiB
Rust
476 lines
17 KiB
Rust
use crate::comp::item::tool::ToolKind;
|
|
use hashbrown::{HashMap, HashSet};
|
|
use lazy_static::lazy_static;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::hash::Hash;
|
|
use tracing::warn;
|
|
|
|
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
|
|
// is requested. TODO: Externalise this data in a RON file for ease of modification
|
|
pub static ref SKILL_GROUP_DEFS: HashMap<SkillGroupType, HashSet<Skill>> = {
|
|
let mut defs = HashMap::new();
|
|
defs.insert(
|
|
SkillGroupType::General, [
|
|
Skill::General(GeneralSkill::HealthIncrease),
|
|
Skill::UnlockGroup(SkillGroupType::Weapon(ToolKind::Sword)),
|
|
Skill::UnlockGroup(SkillGroupType::Weapon(ToolKind::Axe)),
|
|
Skill::UnlockGroup(SkillGroupType::Weapon(ToolKind::Hammer)),
|
|
Skill::UnlockGroup(SkillGroupType::Weapon(ToolKind::Bow)),
|
|
Skill::UnlockGroup(SkillGroupType::Weapon(ToolKind::Staff)),
|
|
Skill::UnlockGroup(SkillGroupType::Weapon(ToolKind::Sceptre)),
|
|
].iter().cloned().collect::<HashSet<Skill>>());
|
|
defs.insert(
|
|
SkillGroupType::Weapon(ToolKind::Sword), [
|
|
Skill::Sword(SwordSkill::UnlockSpin),
|
|
].iter().cloned().collect::<HashSet<Skill>>());
|
|
defs.insert(
|
|
SkillGroupType::Weapon(ToolKind::Axe), [
|
|
Skill::Axe(AxeSkill::UnlockLeap),
|
|
].iter().cloned().collect::<HashSet<Skill>>());
|
|
defs.insert(
|
|
SkillGroupType::Weapon(ToolKind::Hammer), [
|
|
Skill::Hammer(HammerSkill::UnlockLeap),
|
|
].iter().cloned().collect::<HashSet<Skill>>());
|
|
defs.insert(
|
|
SkillGroupType::Weapon(ToolKind::Bow), [
|
|
Skill::Bow(BowSkill::UnlockRepeater),
|
|
].iter().cloned().collect::<HashSet<Skill>>());
|
|
defs.insert(
|
|
SkillGroupType::Weapon(ToolKind::Staff), [
|
|
Skill::Staff(StaffSkill::UnlockShockwave),
|
|
].iter().cloned().collect::<HashSet<Skill>>());
|
|
defs.insert(
|
|
SkillGroupType::Weapon(ToolKind::Sceptre), [
|
|
Skill::Sceptre(SceptreSkill::Unlock404),
|
|
].iter().cloned().collect::<HashSet<Skill>>());
|
|
defs
|
|
};
|
|
}
|
|
|
|
/// 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 {
|
|
General(GeneralSkill),
|
|
Sword(SwordSkill),
|
|
Axe(AxeSkill),
|
|
Hammer(HammerSkill),
|
|
Bow(BowSkill),
|
|
Staff(StaffSkill),
|
|
Sceptre(SceptreSkill),
|
|
UnlockGroup(SkillGroupType),
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum SwordSkill {
|
|
UnlockSpin,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum AxeSkill {
|
|
UnlockLeap,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum HammerSkill {
|
|
UnlockLeap,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum BowSkill {
|
|
UnlockRepeater,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum StaffSkill {
|
|
UnlockShockwave,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum SceptreSkill {
|
|
Unlock404,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum GeneralSkill {
|
|
HealthIncrease,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum SkillGroupType {
|
|
General,
|
|
Weapon(ToolKind),
|
|
}
|
|
|
|
/// 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.
|
|
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
|
|
pub struct SkillGroup {
|
|
pub skill_group_type: SkillGroupType,
|
|
pub exp: u16,
|
|
pub available_sp: u16,
|
|
}
|
|
|
|
impl SkillGroup {
|
|
fn new(skill_group_type: SkillGroupType) -> SkillGroup {
|
|
SkillGroup {
|
|
skill_group_type,
|
|
exp: 0,
|
|
available_sp: 0,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 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)]
|
|
pub struct SkillSet {
|
|
pub skill_groups: Vec<SkillGroup>,
|
|
pub skills: HashMap<Skill, Level>,
|
|
}
|
|
|
|
pub type Level = Option<u16>;
|
|
|
|
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 {
|
|
// TODO: Default skill groups for new players?
|
|
Self {
|
|
skill_groups: vec![SkillGroup::new(SkillGroupType::General)],
|
|
skills: HashMap::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl SkillSet {
|
|
// TODO: Game design to determine how skill groups are unlocked
|
|
/// 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);
|
|
/// ```
|
|
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");
|
|
}
|
|
}
|
|
|
|
/// 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);
|
|
/// ```
|
|
pub fn unlock_skill(&mut self, skill: Skill) {
|
|
if let Some(skill_group_type) = SkillSet::get_skill_group_type_for_skill(&skill) {
|
|
let level = if self.skills.contains_key(&skill) {
|
|
self.skills.get(&skill).copied().flatten().map(|l| l + 1)
|
|
} else {
|
|
skill.get_max_level().map(|_| 1)
|
|
};
|
|
let prerequisites_met = self.prerequisites_met(skill, level);
|
|
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(level) {
|
|
skill_group.available_sp -= skill.skill_cost(level);
|
|
if let Skill::UnlockGroup(group) = skill {
|
|
self.unlock_skill_group(group);
|
|
}
|
|
self.skills.insert(skill, level);
|
|
} else {
|
|
warn!("Tried to unlock skill for skill group with insufficient SP");
|
|
}
|
|
} else {
|
|
warn!("Tried to unlock skill without meeting prerequisite skills");
|
|
}
|
|
} else {
|
|
warn!("Tried to unlock skill for a skill group that player does not have");
|
|
}
|
|
} else {
|
|
warn!(
|
|
?skill,
|
|
"Tried to unlock skill that does not exist in any skill group!"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 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);
|
|
/// ```
|
|
pub fn refund_skill(&mut self, skill: Skill) {
|
|
if self.skills.contains_key(&skill) {
|
|
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)
|
|
{
|
|
// 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);
|
|
}
|
|
} 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
|
|
/// definitions.
|
|
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
|
|
}
|
|
})
|
|
}
|
|
|
|
/// 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,
|
|
number_of_skill_points: u16,
|
|
) {
|
|
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");
|
|
}
|
|
}
|
|
|
|
/// 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)
|
|
}
|
|
|
|
/// 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) {
|
|
if let Some(mut skill_group) = self
|
|
.skill_groups
|
|
.iter_mut()
|
|
.find(|x| x.skill_group_type == skill_group_type)
|
|
{
|
|
skill_group.exp = (skill_group.exp as i32 + amount) as u16;
|
|
} else {
|
|
warn!("Tried to add experience to a skill group that player does not have");
|
|
}
|
|
}
|
|
|
|
/// Checks that the skill set contains all prerequisite skills for a
|
|
/// particular skill
|
|
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)
|
|
})
|
|
}
|
|
}
|
|
|
|
impl Skill {
|
|
/// Returns a vec of prerequisite skills (it should only be necessary to
|
|
/// note direct prerequisites)
|
|
pub fn prerequisite_skills(self, level: Level) -> HashMap<Skill, Level> {
|
|
let mut prerequisites = HashMap::new();
|
|
use Skill::*;
|
|
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));
|
|
}
|
|
}
|
|
match self {
|
|
UnlockGroup(SkillGroupType::Weapon(ToolKind::Sword)) => {
|
|
prerequisites.insert(General(GeneralSkill::HealthIncrease), Some(1));
|
|
},
|
|
UnlockGroup(SkillGroupType::Weapon(ToolKind::Axe)) => {
|
|
prerequisites.insert(General(GeneralSkill::HealthIncrease), Some(1));
|
|
},
|
|
UnlockGroup(SkillGroupType::Weapon(ToolKind::Hammer)) => {
|
|
prerequisites.insert(General(GeneralSkill::HealthIncrease), Some(1));
|
|
},
|
|
UnlockGroup(SkillGroupType::Weapon(ToolKind::Bow)) => {
|
|
prerequisites.insert(General(GeneralSkill::HealthIncrease), Some(1));
|
|
},
|
|
UnlockGroup(SkillGroupType::Weapon(ToolKind::Staff)) => {
|
|
prerequisites.insert(General(GeneralSkill::HealthIncrease), Some(1));
|
|
},
|
|
UnlockGroup(SkillGroupType::Weapon(ToolKind::Sceptre)) => {
|
|
prerequisites.insert(General(GeneralSkill::HealthIncrease), Some(1));
|
|
},
|
|
_ => {},
|
|
}
|
|
prerequisites
|
|
}
|
|
|
|
/// Returns the cost in skill points of unlocking a particular skill
|
|
pub fn skill_cost(self, level: Level) -> u16 {
|
|
use Skill::*;
|
|
match self {
|
|
General(GeneralSkill::HealthIncrease) => 2 * level.unwrap_or(1),
|
|
_ => level.unwrap_or(1),
|
|
}
|
|
}
|
|
|
|
/// Returns the maximum level a skill can reach, returns None if the skill
|
|
/// doesn't level
|
|
pub fn get_max_level(self) -> Option<u16> {
|
|
use Skill::*;
|
|
match self {
|
|
General(GeneralSkill::HealthIncrease) => Some(10),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[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);
|
|
}
|
|
}
|