More work on switching battle data to interior mutability, instead of exterior mutability.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-06-19 12:07:54 +02:00
parent 2e19005a30
commit 715f16e2b8
14 changed files with 256 additions and 280 deletions

View File

@@ -73,7 +73,7 @@ where
unique_identifier: u32,
gender: Gender,
coloring: u8,
held_item: Option<&'own Item>,
held_item: RwLock<Option<&'own Item>>,
current_health: AtomicU32,
weight: Atomic<f32>,
@@ -104,7 +104,7 @@ where
held_item_trigger_script: ScriptContainer,
ability_script: ScriptContainer,
status_script: ScriptContainer,
volatile: Arc<RwLock<ScriptSet>>,
volatile: Arc<ScriptSet>,
script_source_data: RwLock<ScriptSourceData>,
}
@@ -144,7 +144,7 @@ impl<'own, 'library> Pokemon<'own, 'library> {
unique_identifier,
gender,
coloring,
held_item: None,
held_item: RwLock::new(None),
current_health: AtomicU32::new(1),
weight: Atomic::new(weight),
height: Atomic::new(height),
@@ -216,27 +216,27 @@ impl<'own, 'library> Pokemon<'own, 'library> {
pub fn coloring(&self) -> u8 {
self.coloring
}
pub fn held_item(&self) -> Option<&'own Item> {
self.held_item
pub fn held_item(&self) -> &RwLock<Option<&'own Item>> {
&self.held_item
}
pub fn has_held_item(&self, name: &StringKey) -> bool {
// Only true if we have an item, and the item name is the same as the requested item.
if let Some(v) = self.held_item {
if let Some(v) = self.held_item.read().deref() {
return v.name() == name;
}
false
}
pub fn set_held_item(&mut self, item: &'own Item) {
self.held_item = Some(item);
pub fn set_held_item(&self, item: &'own Item) -> Option<&'own Item> {
self.held_item.write().replace(item)
}
pub fn remove_held_item(&mut self) {
self.held_item = None;
pub fn remove_held_item(&self) -> Option<&'own Item> {
self.held_item.write().take()
}
pub fn consume_held_item(&mut self) -> bool {
if self.held_item.is_none() {
pub fn consume_held_item(&self) -> bool {
if self.held_item.read().is_none() {
return false;
}
let script = self.library.load_item_script(self.held_item.unwrap()).unwrap();
let script = self.library.load_item_script(self.held_item.read().unwrap()).unwrap();
if script.is_none() {
return false;
}
@@ -408,8 +408,10 @@ impl<'own, 'library> Pokemon<'own, 'library> {
let diff_health = (self.max_health() - old_health) as i32;
if self.current_health() == 0 && (self.current_health() as i32) < -diff_health {
self.current_health.store(0, Ordering::SeqCst);
} else if diff_health < 0 {
self.current_health.fetch_sub(-diff_health as u32, Ordering::SeqCst);
} else {
self.current_health.fetch_add(diff_health as u32, Ordering::Acquire);
self.current_health.fetch_add(diff_health as u32, Ordering::SeqCst);
}
// TODO: consider form specific attacks?
@@ -450,7 +452,7 @@ impl<'own, 'library> Pokemon<'own, 'library> {
if let Some(data) = &mut r.deref() {
data.on_battle_field.store(value, Ordering::SeqCst);
if !value {
self.volatile.write().clear();
self.volatile.clear();
self.weight.store(self.form.weight(), Ordering::SeqCst);
self.height.store(self.form.height(), Ordering::SeqCst);
}
@@ -572,7 +574,7 @@ impl<'own, 'library> ScriptSource<'own> for Pokemon<'own, 'library> {
}
impl<'own, 'library> VolatileScripts<'own> for Pokemon<'own, 'library> {
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>> {
fn volatile_scripts(&self) -> &Arc<ScriptSet> {
&self.volatile
}