Skip to content

Commit

Permalink
Start implementing skill map
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthox committed May 6, 2024
1 parent 03ea033 commit 6ee743c
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 158 deletions.
80 changes: 8 additions & 72 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "arcdps_buddy"
version = "0.6.4"
version = "0.6.5"
edition = "2021"
authors = ["Zerthox"]
repository = "https://github.com/zerthox/arcdps-buddy"
Expand All @@ -11,7 +11,6 @@ arcdps = { git = "https://github.com/zerthox/arcdps-rs", features = ["log", "ser
log = { version = "0.4.18", features = ["release_max_level_info"] }
num_enum = "0.7.1"
once_cell = "1.17.2"
phf = { version = "0.11.2", features = ["macros"] }
semver = { version = "1.0.17", features = ["serde"] }
serde = { version = "1.0.163", features = ["derive"] }
serde_yaml = "0.9.21"
Expand Down
6 changes: 3 additions & 3 deletions src/combat/breakbar.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{agent::Agent, skill::Skill};
use super::agent::Agent;

/// Information about a defiance damage hit.
#[derive(Debug, Clone)]
Expand All @@ -7,7 +7,7 @@ pub struct BreakbarHit {
pub time: i32,

/// Skill causing the hit.
pub skill: Skill,
pub skill: u32,

/// Defiance damage dealt by the hit.
///
Expand All @@ -28,7 +28,7 @@ impl BreakbarHit {
/// Creates a new breakbar hit.
pub fn new(
time: i32,
skill: Skill,
skill: u32,
damage: i32,
attacker: Agent,
is_own: bool,
Expand Down
11 changes: 5 additions & 6 deletions src/combat/cast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::skill::Skill;
use arcdps::{evtc::AgentKind, Activation, Agent};

/// Information about a cast (activation).
Expand All @@ -8,7 +7,7 @@ pub struct Cast {
pub time: i32,

/// Casted skill.
pub skill: Skill,
pub skill: u32,

/// Current [`CastState`] of the cast.
pub state: CastState,
Expand All @@ -22,7 +21,7 @@ pub struct Cast {

impl Cast {
/// Creates a new cast from a cast start.
pub const fn from_start(time: i32, skill: Skill, state: CastState) -> Self {
pub const fn from_start(time: i32, skill: u32, state: CastState) -> Self {
Self {
time,
skill,
Expand All @@ -33,7 +32,7 @@ impl Cast {
}

/// Creates a new cast from a cast end.
pub const fn from_end(time: i32, skill: Skill, state: CastState, duration: i32) -> Self {
pub const fn from_end(time: i32, skill: u32, state: CastState, duration: i32) -> Self {
Self {
time,
skill,
Expand All @@ -44,7 +43,7 @@ impl Cast {
}

/// Creates a new cast from an individual hit.
pub fn from_hit(time: i32, skill: Skill, target: &Agent) -> Self {
pub fn from_hit(time: i32, skill: u32, target: &Agent) -> Self {
Self {
time,
skill,
Expand All @@ -60,7 +59,7 @@ impl Cast {
}

/// Completes the cast.
pub fn complete(&mut self, skill: Skill, result: CastState, duration: i32, time: i32) {
pub fn complete(&mut self, skill: u32, result: CastState, duration: i32, time: i32) {
if let CastState::Pre = self.state {
self.skill = skill;
self.time = time - duration;
Expand Down
120 changes: 108 additions & 12 deletions src/combat/skill.rs
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(),
}
}
}
14 changes: 6 additions & 8 deletions src/data/skill_names.rs
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"),
];
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ arcdps::export! {
},
release: || Plugin::lock().unload(),
combat: Plugin::area_event,
imgui: Plugin::render_windows,
imgui: Plugin::render,
options_end: |ui| Plugin::lock().render_settings(ui),
options_windows: Plugin::render_window_options,
wnd_filter: Plugin::key_event,
Expand Down
Loading

0 comments on commit 6ee743c

Please sign in to comment.