FFI for Battle
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-10-15 17:21:24 +02:00
parent 6c976216b8
commit ddfb00d36b
10 changed files with 313 additions and 10 deletions

View File

@@ -158,7 +158,7 @@ impl Battle {
pub fn current_turn(&self) -> u32 {
self.current_turn.load(Ordering::Relaxed)
}
/// The time the last turn took to run. Defaults to 0.
/// The time in nanoseconds the last turn took to run. Defaults to 0.
pub fn last_turn_time(&self) -> u64 {
self.last_turn_time.load(Ordering::Relaxed)
}
@@ -261,7 +261,7 @@ impl Battle {
}
/// Try and set the choice for the battle. If the choice is not valid, this returns false.
pub fn try_set_choice(&mut self, choice: TurnChoice) -> PkmnResult<bool> {
pub fn try_set_choice(&self, choice: TurnChoice) -> PkmnResult<bool> {
if !self.can_use(&choice) {
return Ok(false);
}
@@ -399,7 +399,7 @@ impl ValueIdentifiable for Battle {
}
/// The result of a battle.
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum BattleResult {
/// The battle has no winner. Either the battle has not ended, or everyone is dead, or one of
/// the parties has ran away.

View File

@@ -4,13 +4,15 @@ use std::sync::{Arc, Mutex};
use crate::dynamic_data::models::executing_move::ExecutingMove;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::script_handling::ScriptSource;
use crate::script_hook;
use crate::utils::Random;
use crate::{script_hook, ValueIdentifiable, ValueIdentifier};
/// The RNG for a battle.
#[derive(Default)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct BattleRandom {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The actual underlying RNG. This is in a mutex, so it is thread safe, and can be ran
/// predictably, with guaranteed the same outputs.
random: Mutex<Random>,
@@ -20,6 +22,7 @@ impl BattleRandom {
/// Initializes a new RNG with a given seed.
pub fn new_with_seed(seed: u128) -> Self {
BattleRandom {
identifier: Default::default(),
random: Mutex::new(Random::new(seed)),
}
}
@@ -89,7 +92,14 @@ impl Debug for BattleRandom {
impl Clone for BattleRandom {
fn clone(&self) -> Self {
Self {
identifier: Default::default(),
random: Mutex::new(self.random.lock().unwrap().clone()),
}
}
}
impl ValueIdentifiable for BattleRandom {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}

View File

@@ -14,12 +14,14 @@ use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, Scrip
use crate::dynamic_data::Script;
use crate::dynamic_data::ScriptSet;
use crate::dynamic_data::VolatileScriptsOwner;
use crate::{script_hook, PkmnResult, StringKey};
use crate::{script_hook, PkmnResult, StringKey, ValueIdentifiable, ValueIdentifier};
/// A side on a battle.
#[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct BattleSide {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The index of the side on the battle.
index: u8,
/// The number of Pokemon that can be on the side.
@@ -60,6 +62,7 @@ impl BattleSide {
let pokemon = RwLock::new(pokemon);
Self {
identifier: Default::default(),
index,
pokemon_per_side,
pokemon,
@@ -322,3 +325,9 @@ impl ScriptSource for BattleSide {
self.battle().collect_scripts(scripts);
}
}
impl ValueIdentifiable for BattleSide {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}