More work on WASM handling.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-08-20 12:22:12 +02:00
parent 703fd2c147
commit 2d4253e155
36 changed files with 922 additions and 87 deletions

View File

@@ -29,6 +29,7 @@ struct CommonChoiceData<'user, 'library> {
/// This enum defines a single choice for a Pokemon for a battle turn.
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub enum TurnChoice<'user, 'library> {
/// A move choice tells a Pokemon to use a move on a target for this turn.
Move(MoveChoice<'user, 'library>),

View File

@@ -91,6 +91,15 @@ pub enum Event<'own, 'battle, 'library> {
/// The health of the Pokemon after the damage.
new_health: u32,
},
/// This event happens when a Pokemon gets healed
Heal {
/// The Pokemon that gets healed.
pokemon: &'own Pokemon<'battle, 'library>,
/// The health of the Pokemon before the heal.
original_health: u32,
/// The health of the Pokemon after the heal.
new_health: u32,
},
/// This event happens when a Pokemon faints.
Faint {
/// The pokemon that has fainted.

View File

@@ -68,7 +68,7 @@ impl<'library> Default for Gen7MiscLibrary<'library> {
impl<'library> Drop for Gen7MiscLibrary<'library> {
fn drop(&mut self) {
unsafe {
Box::from_raw(self.struggle_data as *mut MoveData);
let _ = Box::from_raw(self.struggle_data as *mut MoveData);
}
}
}

View File

@@ -5,6 +5,7 @@ use crate::static_data::MoveData;
/// A learned move is the data attached to a Pokemon for a move it has learned. It has information
/// such as the remaining amount of users, how it has been learned, etc.
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct LearnedMove<'library> {
/// The immutable move information of the move.
move_data: &'library MoveData,
@@ -18,6 +19,7 @@ pub struct LearnedMove<'library> {
/// The different ways a move can be learned.
#[derive(Copy, Clone, Debug, Default)]
#[repr(u8)]
pub enum MoveLearnMethod {
/// We do not know the learn method.
#[default]

View File

@@ -25,6 +25,7 @@ use crate::{script_hook, PkmnResult, StringKey};
/// An individual Pokemon as we know and love them.
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct Pokemon<'own, 'library>
where
'own: 'library,
@@ -306,6 +307,12 @@ impl<'own, 'library> Pokemon<'own, 'library> {
pub fn flat_stats(&self) -> &StatisticSet<u32> {
&self.flat_stats
}
/// The stats of the Pokemon including the stat boosts
pub fn stat_boosts(&self) -> &ClampedStatisticSet<i8, -6, 6> {
&self.stat_boost
}
/// The stats of the Pokemon including the stat boosts
pub fn boosted_stats(&self) -> &StatisticSet<u32> {
&self.boosted_stats
@@ -613,7 +620,6 @@ impl<'own, 'library> Pokemon<'own, 'library> {
original_health: self.current_health(),
new_health,
});
// TODO: register history
}
}
if self.battle_data.read().is_some_and(|a| a.on_battle_field()) {
@@ -645,6 +651,33 @@ impl<'own, 'library> Pokemon<'own, 'library> {
}
}
/// Heals the Pokemon by a specific amount. Unless allow_revive is set to true, this will not
/// heal if the Pokemon has 0 health. If the amount healed is 0, this will return false.
pub fn heal(&self, mut amount: u32, allow_revive: bool) -> bool {
if self.current_health() == 0 && !allow_revive {
return false;
}
let max_amount = self.max_health() - self.current_health();
if amount > max_amount {
amount = max_amount;
}
if amount == 0 {
return false;
}
let new_health = self.current_health() + max_amount;
if let Some(battle_data) = &self.battle_data.read().deref() {
if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::Heal {
pokemon: self,
original_health: self.current_health(),
new_health,
});
}
}
self.current_health.store(new_health, Ordering::SeqCst);
true
}
/// Learn a move.
pub fn learn_move(&self, move_name: &StringKey, learn_method: MoveLearnMethod) {
let mut learned_moves = self.learned_moves().write();
@@ -745,6 +778,7 @@ impl<'own, 'library> VolatileScriptsOwner<'own> for Pokemon<'own, 'library> {
/// A source of damage. This should be as unique as possible.
#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum DamageSource {
/// The damage is done by a move.
MoveDamage = 0,