diff --git a/Cargo.toml b/Cargo.toml index 54b98c1..ffa0fb0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ rpath = false [dependencies] # Used for PrimInt, so we can check if a generic is an integer num-traits = "0.2" -atomig = "0.4.0" +atomig = { version = "0.4.0", features = ["derive"] } # Used for time based code (i.e. randomness) chrono = "0.4.19" # Used for RNG @@ -50,7 +50,6 @@ rand_pcg = "0.3.1" hashbrown = "0.12.1" indexmap = "1.8.2" parking_lot = "0.12.1" -atomic = "0.5.1" serde = { version = "1.0.137", optional = true, features = ["derive"] } [dev-dependencies] diff --git a/src/dynamic_data/models/battle.rs b/src/dynamic_data/models/battle.rs index 9966f70..1aece1d 100644 --- a/src/dynamic_data/models/battle.rs +++ b/src/dynamic_data/models/battle.rs @@ -2,7 +2,7 @@ use std::ops::{Deref, DerefMut}; use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::sync::Arc; -use atomic::Atomic; +use atomig::Atomic; use parking_lot::RwLock; use crate::dynamic_data::choices::TurnChoice; @@ -42,7 +42,7 @@ pub struct Battle<'own, 'library> { /// Whether or not the battle has ended. has_ended: AtomicBool, /// The eventual result of the battle. Inconclusive until the battle is ended. - result: Atomic, + result: RwLock, /// The handler to send all events to. event_hook: EventHook, /// The index of the current turn. 0 until all choices @@ -50,7 +50,7 @@ pub struct Battle<'own, 'library> { /// All the volatile scripts attached to a Pokemon volatile_scripts: Arc, /// The time the last turn took to run. Defaults to 0. - last_turn_time: Atomic, + last_turn_time: Atomic, /// Data required for this script to be a script source. script_source_data: RwLock, } @@ -87,11 +87,11 @@ impl<'own, 'library> Battle<'own, 'library> { random, current_turn_queue: RwLock::new(None), has_ended: AtomicBool::new(false), - result: Atomic::new(BattleResult::Inconclusive), + result: RwLock::new(BattleResult::Inconclusive), event_hook: Default::default(), current_turn: AtomicU32::new(0), volatile_scripts: Default::default(), - last_turn_time: Atomic::new(chrono::Duration::zero()), + last_turn_time: Default::default(), script_source_data: Default::default(), }; @@ -141,7 +141,7 @@ impl<'own, 'library> Battle<'own, 'library> { } /// The eventual result of the battle. Inconclusive until the battle is ended. pub fn result(&self) -> BattleResult { - self.result.load(Ordering::Relaxed) + self.result.read().clone() } /// The handler to send all events to. pub fn event_hook(&self) -> &EventHook { @@ -152,7 +152,7 @@ impl<'own, 'library> Battle<'own, 'library> { self.current_turn.load(Ordering::Relaxed) } /// The time the last turn took to run. Defaults to 0. - pub fn last_turn_time(&self) -> chrono::Duration { + pub fn last_turn_time(&self) -> u64 { self.last_turn_time.load(Ordering::Relaxed) } /// A queue of the yet to be executed choices in a turn. @@ -194,7 +194,10 @@ impl<'own, 'library> Battle<'own, 'library> { for (side_index, side) in self.sides.iter().enumerate() { // If any side has fled, the battle end. if side.has_fled() { - self.result.store(BattleResult::Inconclusive, Ordering::SeqCst); + let _w = self.result.write(); + unsafe { + self.result.data_ptr().replace(BattleResult::Inconclusive); + } self.has_ended.store(true, Ordering::SeqCst); return; } @@ -210,12 +213,19 @@ impl<'own, 'library> Battle<'own, 'library> { } // Everyone died :( if !surviving_side_exists { - self.result.store(BattleResult::Inconclusive, Ordering::SeqCst); + let _w = self.result.write(); + unsafe { + self.result.data_ptr().replace(BattleResult::Inconclusive); + } } // Someone survived, they won! else { - self.result - .store(BattleResult::Conclusive(winning_side.unwrap()), Ordering::SeqCst); + let _w = self.result.write(); + unsafe { + self.result + .data_ptr() + .replace(BattleResult::Conclusive(winning_side.unwrap())); + } } self.has_ended.store(true, Ordering::SeqCst); } @@ -316,7 +326,8 @@ impl<'own, 'library> Battle<'own, 'library> { self.event_hook.trigger(Event::EndTurn); let end_time = chrono::Utc::now(); let time = end_time - start_time; - self.last_turn_time.store(time, Ordering::SeqCst); + self.last_turn_time + .store(time.num_nanoseconds().unwrap() as u64, Ordering::SeqCst); Ok(()) } } diff --git a/src/dynamic_data/models/executing_move.rs b/src/dynamic_data/models/executing_move.rs index 3fceca7..c893173 100644 --- a/src/dynamic_data/models/executing_move.rs +++ b/src/dynamic_data/models/executing_move.rs @@ -2,7 +2,7 @@ use std::ops::Deref; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU8, Ordering}; use std::sync::Arc; -use atomic::Atomic; +use atomig::Atomic; use parking_lot::RwLock; use crate::dynamic_data::models::learned_move::LearnedMove; diff --git a/src/dynamic_data/models/pokemon.rs b/src/dynamic_data/models/pokemon.rs index 80d7f06..f67b0b1 100644 --- a/src/dynamic_data/models/pokemon.rs +++ b/src/dynamic_data/models/pokemon.rs @@ -2,7 +2,7 @@ use std::ops::{Deref, DerefMut}; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU8, Ordering}; use std::sync::{Arc, Weak}; -use atomic::Atomic; +use atomig::Atomic; use parking_lot::RwLock; use crate::defines::{LevelInt, MAX_MOVES}; diff --git a/src/static_data/libraries/type_library.rs b/src/static_data/libraries/type_library.rs index 224aea6..731599c 100644 --- a/src/static_data/libraries/type_library.rs +++ b/src/static_data/libraries/type_library.rs @@ -1,10 +1,11 @@ +use atomig::Atom; use hashbrown::HashMap; use crate::StringKey; /// A unique key that can be used to store a reference to a type. Opaque reference to a byte /// internally. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Hash, Atom)] pub struct TypeIdentifier { /// The unique internal value. val: u8,