Skip to content

Commit

Permalink
Start implementing condition transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthox committed Dec 5, 2023
1 parent 2628c5a commit 74ab3d9
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 36 deletions.
5 changes: 2 additions & 3 deletions src/combat/breakbar.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::{agent::Target, skill::Skill};
use arcdps::Agent;

/// Information about a defiance damage hit.
#[derive(Debug, Clone)]
Expand All @@ -21,12 +20,12 @@ pub struct BreakbarHit {

impl BreakbarHit {
/// Creates a new breakbar hit.
pub fn new(time: i32, skill: Skill, damage: i32, target: &Agent) -> Self {
pub fn new(time: i32, skill: Skill, damage: i32, target: Target) -> Self {
Self {
time,
skill,
damage,
target: target.into(),
target,
}
}
}
6 changes: 3 additions & 3 deletions src/combat/buff.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::agent::Target;
use arcdps::{evtc::AgentKind, Agent};
use arcdps::evtc::AgentKind;

pub use crate::data::Buff;

Expand All @@ -21,12 +21,12 @@ pub struct BuffApply {

impl BuffApply {
/// Creates a new buff apply.
pub fn new(time: i32, buff: Buff, duration: i32, target: &Agent) -> Self {
pub fn new(time: i32, buff: Buff, duration: i32, target: Target) -> Self {
Self {
buff,
time,
duration,
target: target.into(),
target,
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/combat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ pub mod breakbar;
pub mod buff;
pub mod cast;
pub mod skill;
pub mod transfer;

use breakbar::BreakbarHit;
use buff::BuffApply;
use cast::Cast;
use self::breakbar::BreakbarHit;
use self::buff::BuffApply;
use self::cast::Cast;
use self::transfer::TransferTracker;

#[derive(Debug, Clone)]
pub struct CombatData {
pub casts: Vec<Cast>,
pub buffs: Vec<BuffApply>,
pub breakbar: Vec<BreakbarHit>,
pub transfers: TransferTracker,
}

impl CombatData {
Expand All @@ -21,6 +24,7 @@ impl CombatData {
casts: Vec::new(),
buffs: Vec::new(),
breakbar: Vec::new(),
transfers: TransferTracker::new(),
}
}
}
Expand Down
140 changes: 140 additions & 0 deletions src/combat/transfer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
use super::agent::Target;
use log::debug;

pub use crate::data::Condition;

/// Transfer tracking.
#[derive(Debug, Clone)]
pub struct TransferTracker {
/// Detected transfers.
pub transfers: Vec<Transfer>,

/// Condition removes.
remove: Vec<Remove>,

/// Condition applies as transfer candidates.
apply: Vec<Transfer>,
}

impl TransferTracker {
/// Time to retain candidates.
pub const RETAIN_TIME: i32 = 100;

pub const fn new() -> Self {
Self {
transfers: Vec::new(),
remove: Vec::new(),
apply: Vec::new(),
}
}

/// Adds a condition remove.
pub fn add_remove(&mut self, remove: Remove) {
self.purge(remove.time);
if let Some(apply) = Self::find_remove(&mut self.apply, |apply| apply.matches(&remove)) {
debug!("transfer: {remove:?} matches {apply:?}");
self.transfers.push(apply)
} else {
self.remove.push(remove)
}
}

/// Adds a condition apply as transfer candidate.
pub fn add_apply(&mut self, apply: Transfer) {
self.purge(apply.time);
if let Some(remove) = Self::find_remove(&mut self.remove, |remove| apply.matches(remove)) {
debug!("transfer: {apply:?} matches {remove:?}");
self.transfers.push(apply)
} else {
self.apply.push(apply)
}
}

fn find_remove<T>(vec: &mut Vec<T>, pred: impl FnMut(&T) -> bool) -> Option<T> {
if let Some(index) = vec.iter().position(pred) {
Some(vec.swap_remove(index))
} else {
None
}
}

/// Purges old information.
fn purge(&mut self, now: i32) {
self.remove.retain(|el| Self::check_time(el.time, now));
self.apply.retain(|el| Self::check_time(el.time, now));
}

/// Checks if the time should be kept.
fn check_time(time: i32, now: i32) -> bool {
time + Self::RETAIN_TIME >= now
}
}

impl Default for TransferTracker {
fn default() -> Self {
Self::new()
}
}

/// Information about a condition remove.
#[derive(Debug, Clone)]
pub struct Remove {
/// Time of the remove.
pub time: i32,

/// Condition removed.
pub condi: Condition,

/// Amount of stacks removed.
pub stacks: u32,
}

impl Remove {
/// Creates a new condition transfer.
pub fn new(time: i32, condi: Condition, stacks: u32) -> Self {
Self {
time,
condi,
stacks,
}
}
}

/// Information about a condition transfer.
#[derive(Debug, Clone)]
pub struct Transfer {
/// Time of the transfer.
pub time: i32,

/// Condition transferred.
pub condi: Condition,

/// Amount of stacks transferred.
pub stacks: u32,

/// Target the condition was transferred to.
pub target: Target,
}

impl Transfer {
/// Error margin for transfer times.
pub const TIME_EPSILON: i32 = 10;

/// Creates a new condition transfer.
pub fn new(time: i32, condi: Condition, stacks: u32, target: Target) -> Self {
Self {
time,
condi,
stacks,

target,
}
}

/// Check whether the transfer candidate matches a remove.
pub fn matches(&self, remove: &Remove) -> bool {
self.condi == remove.condi
&& self.stacks == remove.stacks
&& (self.time - remove.time) <= Self::TIME_EPSILON
}
}
File renamed without changes.
34 changes: 34 additions & 0 deletions src/data/condi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use num_enum::{IntoPrimitive, TryFromPrimitive};
use strum::AsRefStr;

/// Condition.
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
AsRefStr,
IntoPrimitive,
TryFromPrimitive,
)]
#[repr(u32)]
pub enum Condition {
Blind = 720,
Crippled = 721,
Chilled = 722,
Poison = 723,
Immobile = 727,
Bleeding = 736,
Burning = 737,
Vulnerability = 738,
Weakness = 742,
Fear = 791,
Confusion = 861,
Torment = 19426,
Slow = 26766,
Taunt = 27705,
}
6 changes: 4 additions & 2 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod buffs;
mod buff;
mod condi;
mod structs;

pub use self::buffs::*;
pub use self::buff::*;
pub use self::condi::*;
pub use self::structs::*;

use std::{
Expand Down
Loading

0 comments on commit 74ab3d9

Please sign in to comment.