aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/lib.rs b/src/lib.rs
index ccf6692..3677871 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -19,7 +19,7 @@
//! # Crate Structure
//!
//! The crate consists of two main parts: The [`raw`][raw] parser, which is used to read structured
-//! data from binary input streams, and the higher-level abstrations provided in the root and
+//! data from binary input streams, and the higher-level abstractions provided in the root and
//! [`event`][event] submodules.
//!
//! Additionally, there are some defintions (such as IDs for various game items) in the
@@ -88,6 +88,7 @@
//! While there are legitimate use cases for writing/modification support, they are currently not
//! implemented (but might be in a future version).
+use num_traits::FromPrimitive;
use thiserror::Error;
pub mod raw;
@@ -102,6 +103,7 @@ mod processing;
pub use processing::{process, process_file, process_stream, Compression};
pub mod gamedata;
+use gamedata::Boss;
pub use gamedata::{EliteSpec, Encounter, GameMode, Profession};
pub mod analyzers;
@@ -138,6 +140,8 @@ pub enum EvtcError {
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct Log {
+ // evtclib assumes that the agents in this vector are sorted by their address. This information
+ // is used to speed up the agent_by_addr search.
agents: Vec<Agent>,
events: Vec<Event>,
boss_id: u16,
@@ -152,7 +156,12 @@ impl Log {
/// Return an agent based on its address.
pub fn agent_by_addr(&self, addr: u64) -> Option<&Agent> {
- self.agents.iter().find(|a| a.addr() == addr)
+ // We know that the agents are sorted because processing::process puts them like that. We
+ // can use the same trick here to achieve a faster agent searching:
+ self.agents
+ .binary_search_by_key(&addr, Agent::addr)
+ .ok()
+ .map(|i| &self.agents[i])
}
/// Return an agent based on the instance ID.
@@ -217,7 +226,15 @@ impl Log {
/// Check whether the given address is a boss agent.
pub fn is_boss(&self, addr: u64) -> bool {
- self.boss_agents().into_iter().any(|a| a.addr() == addr)
+ let bosses = self
+ .encounter()
+ .map(Encounter::bosses)
+ .unwrap_or(&[] as &[_]);
+ let agent = self
+ .agent_by_addr(addr)
+ .and_then(Agent::as_character)
+ .and_then(|c| Boss::from_u16(c.id()));
+ agent.map(|e| bosses.contains(&e)).unwrap_or(false)
}
/// Returns the encounter id.