Removes atomic dependency
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-07-01 18:44:09 +02:00
parent 37f0cf8c35
commit cfabd7c790
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
5 changed files with 28 additions and 17 deletions

View File

@ -41,7 +41,7 @@ rpath = false
[dependencies] [dependencies]
# Used for PrimInt, so we can check if a generic is an integer # Used for PrimInt, so we can check if a generic is an integer
num-traits = "0.2" num-traits = "0.2"
atomig = "0.4.0" atomig = { version = "0.4.0", features = ["derive"] }
# Used for time based code (i.e. randomness) # Used for time based code (i.e. randomness)
chrono = "0.4.19" chrono = "0.4.19"
# Used for RNG # Used for RNG
@ -50,7 +50,6 @@ rand_pcg = "0.3.1"
hashbrown = "0.12.1" hashbrown = "0.12.1"
indexmap = "1.8.2" indexmap = "1.8.2"
parking_lot = "0.12.1" parking_lot = "0.12.1"
atomic = "0.5.1"
serde = { version = "1.0.137", optional = true, features = ["derive"] } serde = { version = "1.0.137", optional = true, features = ["derive"] }
[dev-dependencies] [dev-dependencies]

View File

@ -2,7 +2,7 @@ use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::Arc; use std::sync::Arc;
use atomic::Atomic; use atomig::Atomic;
use parking_lot::RwLock; use parking_lot::RwLock;
use crate::dynamic_data::choices::TurnChoice; use crate::dynamic_data::choices::TurnChoice;
@ -42,7 +42,7 @@ pub struct Battle<'own, 'library> {
/// Whether or not the battle has ended. /// Whether or not the battle has ended.
has_ended: AtomicBool, has_ended: AtomicBool,
/// The eventual result of the battle. Inconclusive until the battle is ended. /// The eventual result of the battle. Inconclusive until the battle is ended.
result: Atomic<BattleResult>, result: RwLock<BattleResult>,
/// The handler to send all events to. /// The handler to send all events to.
event_hook: EventHook, event_hook: EventHook,
/// The index of the current turn. 0 until all choices /// 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 /// All the volatile scripts attached to a Pokemon
volatile_scripts: Arc<ScriptSet>, volatile_scripts: Arc<ScriptSet>,
/// The time the last turn took to run. Defaults to 0. /// The time the last turn took to run. Defaults to 0.
last_turn_time: Atomic<chrono::Duration>, last_turn_time: Atomic<u64>,
/// Data required for this script to be a script source. /// Data required for this script to be a script source.
script_source_data: RwLock<ScriptSourceData>, script_source_data: RwLock<ScriptSourceData>,
} }
@ -87,11 +87,11 @@ impl<'own, 'library> Battle<'own, 'library> {
random, random,
current_turn_queue: RwLock::new(None), current_turn_queue: RwLock::new(None),
has_ended: AtomicBool::new(false), has_ended: AtomicBool::new(false),
result: Atomic::new(BattleResult::Inconclusive), result: RwLock::new(BattleResult::Inconclusive),
event_hook: Default::default(), event_hook: Default::default(),
current_turn: AtomicU32::new(0), current_turn: AtomicU32::new(0),
volatile_scripts: Default::default(), volatile_scripts: Default::default(),
last_turn_time: Atomic::new(chrono::Duration::zero()), last_turn_time: Default::default(),
script_source_data: 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. /// The eventual result of the battle. Inconclusive until the battle is ended.
pub fn result(&self) -> BattleResult { pub fn result(&self) -> BattleResult {
self.result.load(Ordering::Relaxed) self.result.read().clone()
} }
/// The handler to send all events to. /// The handler to send all events to.
pub fn event_hook(&self) -> &EventHook { pub fn event_hook(&self) -> &EventHook {
@ -152,7 +152,7 @@ impl<'own, 'library> Battle<'own, 'library> {
self.current_turn.load(Ordering::Relaxed) self.current_turn.load(Ordering::Relaxed)
} }
/// The time the last turn took to run. Defaults to 0. /// 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) self.last_turn_time.load(Ordering::Relaxed)
} }
/// A queue of the yet to be executed choices in a turn. /// 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() { for (side_index, side) in self.sides.iter().enumerate() {
// If any side has fled, the battle end. // If any side has fled, the battle end.
if side.has_fled() { 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); self.has_ended.store(true, Ordering::SeqCst);
return; return;
} }
@ -210,12 +213,19 @@ impl<'own, 'library> Battle<'own, 'library> {
} }
// Everyone died :( // Everyone died :(
if !surviving_side_exists { 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! // Someone survived, they won!
else { else {
self.result let _w = self.result.write();
.store(BattleResult::Conclusive(winning_side.unwrap()), Ordering::SeqCst); unsafe {
self.result
.data_ptr()
.replace(BattleResult::Conclusive(winning_side.unwrap()));
}
} }
self.has_ended.store(true, Ordering::SeqCst); self.has_ended.store(true, Ordering::SeqCst);
} }
@ -316,7 +326,8 @@ impl<'own, 'library> Battle<'own, 'library> {
self.event_hook.trigger(Event::EndTurn); self.event_hook.trigger(Event::EndTurn);
let end_time = chrono::Utc::now(); let end_time = chrono::Utc::now();
let time = end_time - start_time; 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(()) Ok(())
} }
} }

View File

@ -2,7 +2,7 @@ use std::ops::Deref;
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU8, Ordering}; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU8, Ordering};
use std::sync::Arc; use std::sync::Arc;
use atomic::Atomic; use atomig::Atomic;
use parking_lot::RwLock; use parking_lot::RwLock;
use crate::dynamic_data::models::learned_move::LearnedMove; use crate::dynamic_data::models::learned_move::LearnedMove;

View File

@ -2,7 +2,7 @@ use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU8, Ordering}; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU8, Ordering};
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
use atomic::Atomic; use atomig::Atomic;
use parking_lot::RwLock; use parking_lot::RwLock;
use crate::defines::{LevelInt, MAX_MOVES}; use crate::defines::{LevelInt, MAX_MOVES};

View File

@ -1,10 +1,11 @@
use atomig::Atom;
use hashbrown::HashMap; use hashbrown::HashMap;
use crate::StringKey; use crate::StringKey;
/// A unique key that can be used to store a reference to a type. Opaque reference to a byte /// A unique key that can be used to store a reference to a type. Opaque reference to a byte
/// internally. /// internally.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Hash, Atom)]
pub struct TypeIdentifier { pub struct TypeIdentifier {
/// The unique internal value. /// The unique internal value.
val: u8, val: u8,