-
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.
Start implementing condition transfers
- Loading branch information
Showing
8 changed files
with
255 additions
and
36 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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.
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 |
---|---|---|
@@ -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, | ||
} |
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.