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]
# 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]

View File

@ -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<BattleResult>,
result: RwLock<BattleResult>,
/// 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<ScriptSet>,
/// 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.
script_source_data: RwLock<ScriptSourceData>,
}
@ -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(())
}
}

View File

@ -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;

View File

@ -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};

View File

@ -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,