-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
240 additions
and
158 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,125 @@ | ||
use crate::data::SKILL_OVERRIDES; | ||
use std::collections::{hash_map::Entry, HashMap}; | ||
|
||
/// Skill map keeping skill information in memory. | ||
#[derive(Debug, Clone)] | ||
#[repr(transparent)] | ||
pub struct SkillMap { | ||
map: HashMap<u32, Skill>, | ||
} | ||
|
||
impl SkillMap { | ||
/// Creates a new skill map. | ||
pub fn new() -> Self { | ||
Self { | ||
map: Self::override_entries(), | ||
} | ||
} | ||
|
||
/// Creates skill override entries. | ||
fn override_entries() -> HashMap<u32, Skill> { | ||
SKILL_OVERRIDES | ||
.iter() | ||
.cloned() | ||
.map(|(id, name)| (id, Skill::named(name))) | ||
.collect() | ||
} | ||
|
||
/// Returns the number of skill overrides. | ||
pub fn overrides(&self) -> usize { | ||
SKILL_OVERRIDES.len() | ||
} | ||
|
||
/// Returns the number of skill entries. | ||
pub fn len(&self) -> usize { | ||
self.map.len() - self.overrides() | ||
} | ||
|
||
/// Resets the stored skill information. | ||
pub fn reset(&mut self) { | ||
self.map = Self::override_entries(); | ||
} | ||
|
||
/// Returns the skill information for the given id. | ||
pub fn get(&mut self, id: u32) -> &Skill { | ||
self.map.entry(id).or_insert_with(|| Skill::unnamed(id)) | ||
} | ||
|
||
/// Returns the skill name for the given id. | ||
pub fn get_name(&mut self, id: u32) -> &str { | ||
self.get(id).name.as_str() | ||
} | ||
|
||
/// Attempts to register a skill. | ||
/// | ||
/// Skills are replaced if unnamed. | ||
fn try_register_with(&mut self, id: u32, create: impl FnOnce() -> Skill) -> &Skill { | ||
match self.map.entry(id) { | ||
Entry::Occupied(occupied) => { | ||
let value = occupied.into_mut(); | ||
if !value.is_named { | ||
*value = create(); | ||
} | ||
value | ||
} | ||
Entry::Vacant(vacant) => vacant.insert(create()), | ||
} | ||
} | ||
|
||
/// Attempts to register a skill. | ||
pub fn try_register(&mut self, id: u32, skill_name: Option<&str>) -> &Skill { | ||
self.try_register_with(id, || Skill::from_combat(id, skill_name)) | ||
} | ||
|
||
/// Attempts to duplicate a skill. | ||
pub fn try_duplicate(&mut self, id: u32, from: u32) { | ||
if id != from { | ||
if let Some(Skill { | ||
is_named: true, | ||
name, | ||
}) = self.map.get(&from) | ||
{ | ||
let new = Skill::named(name); | ||
self.try_register_with(id, || new); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Information about a skill. | ||
#[derive(Debug, Clone)] | ||
pub struct Skill { | ||
/// Id of the skill. | ||
pub id: u32, | ||
/// Whether the skill is named. | ||
pub is_named: bool, | ||
|
||
/// Name of the skill. | ||
pub name: String, | ||
} | ||
|
||
impl Skill { | ||
/// Creates a new skill. | ||
/// Creates a new skill from combat. | ||
/// | ||
/// Name will fallback to the skill id if not present or empty. | ||
pub fn new(id: u32, name: Option<&str>) -> Self { | ||
fn from_combat(id: u32, skill_name: Option<&str>) -> Self { | ||
match skill_name { | ||
Some(name) if !name.is_empty() => Self::named(name), | ||
_ => Self::unnamed(id), | ||
} | ||
} | ||
|
||
/// Creates a new named skill. | ||
fn named(name: &str) -> Self { | ||
Self { | ||
is_named: true, | ||
name: name.into(), | ||
} | ||
} | ||
|
||
/// Creates a new unnamed skill. | ||
fn unnamed(id: u32) -> Self { | ||
Self { | ||
id, | ||
name: match SKILL_OVERRIDES.get(&id) { | ||
Some(name) => name.to_string(), | ||
None => match name { | ||
Some(name) if !name.is_empty() => name.into(), | ||
_ => id.to_string(), | ||
}, | ||
}, | ||
is_named: false, | ||
name: id.to_string(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
use phf::phf_map; | ||
|
||
/// Skill name overrides. | ||
pub static SKILL_OVERRIDES: phf::Map<u32, &'static str> = phf_map! { | ||
12815u32 => "Lightning Leap Combo", | ||
22492u32 => "Basilisk Venom", | ||
31749u32 => "Blood Moon", | ||
32410u32 => "Hunter's Verdict", | ||
}; | ||
pub static SKILL_OVERRIDES: &[(u32, &str)] = &[ | ||
(12815, "Lightning Leap Combo"), | ||
(22492, "Basilisk Venom"), | ||
(31749, "Blood Moon"), | ||
(32410, "Hunter's Verdict"), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.