Major work on WASM results

This commit is contained in:
2023-06-17 19:05:27 +02:00
parent 0d3d5bcbe7
commit 6a2353df4c
34 changed files with 818 additions and 472 deletions

View File

@@ -1,3 +1,4 @@
use std::fmt::{Debug, Formatter};
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU8, Ordering};
use std::sync::{Arc, Weak};
@@ -26,7 +27,6 @@ use crate::{script_hook, PkmnError, StringKey, ValueIdentifiable, ValueIdentifie
use anyhow::{anyhow, bail, Result};
/// An individual Pokemon as we know and love them.
#[derive(Debug)]
pub struct Pokemon {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
@@ -392,10 +392,10 @@ impl Pokemon {
}
/// Gets the battle the battle is currently in.
pub fn get_battle(&self) -> Option<&Battle> {
pub fn get_battle(&self) -> Option<Arc<Battle>> {
let r = self.battle_data.read();
if let Some(data) = &r.deref() {
unsafe { data.battle.as_ref() }
data.battle.upgrade()
} else {
None
}
@@ -480,13 +480,8 @@ impl Pokemon {
// If we're in battle, use the battle random for predictability
let r = self.battle_data.read();
if let Some(data) = r.deref() {
let mut random = match data
.battle()
.ok_or(anyhow!("Battle not set"))?
.random()
.get_rng()
.lock()
{
let battle = data.battle().ok_or(anyhow!("Battle not set"))?;
let mut random = match battle.random().get_rng().lock() {
Ok(v) => v,
Err(_) => return Err(PkmnError::UnableToAcquireLock.into()),
};
@@ -585,7 +580,7 @@ impl Pokemon {
}
/// Sets the current battle the Pokemon is in.
pub fn set_battle_data(&self, battle: *mut Battle, battle_side_index: u8) {
pub fn set_battle_data(&self, battle: Weak<Battle>, battle_side_index: u8) {
let mut w = self.battle_data.write();
if let Some(battle_data) = w.deref_mut() {
battle_data.battle = battle;
@@ -770,7 +765,7 @@ impl Pokemon {
#[derive(Debug)]
pub struct PokemonBattleData {
/// The battle data of the Pokemon
battle: *mut Battle,
battle: Weak<Battle>,
/// The index of the side of the Pokemon
battle_side_index: AtomicU8,
/// The index of the slot on the side of the Pokemon.
@@ -783,12 +778,12 @@ pub struct PokemonBattleData {
impl PokemonBattleData {
/// The battle data of the Pokemon
pub fn battle_mut(&mut self) -> Option<&mut Battle> {
unsafe { self.battle.as_mut() }
pub fn battle_mut(&mut self) -> Option<Arc<Battle>> {
self.battle.upgrade()
}
/// The battle data of the Pokemon
pub fn battle(&self) -> Option<&Battle> {
unsafe { self.battle.as_ref() }
pub fn battle(&self) -> Option<Arc<Battle>> {
self.battle.upgrade()
}
/// The index of the side of the Pokemon
@@ -876,6 +871,18 @@ pub enum DamageSource {
Struggle = 2,
}
impl Debug for Pokemon {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("Pokemon(")?;
write!(f, "Species: {}, ", self.species().name())?;
write!(f, "Form: {}, ", self.form().name())?;
write!(f, "Level: {}, ", self.level())?;
f.write_str(")")?;
Ok(())
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
pub mod test {