Major rework of extern ref system for WASM, fixes most possible panics in WASM handling
All checks were successful
continuous-integration/drone Build is passing

This commit is contained in:
2023-06-22 15:43:41 +02:00
parent 6a2353df4c
commit 46195d3042
71 changed files with 2142 additions and 1488 deletions

View File

@@ -1,5 +1,5 @@
use anyhow::{bail, Result};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::atomic::{AtomicBool, AtomicI8, AtomicU32, Ordering};
use std::sync::Arc;
use parking_lot::RwLock;
@@ -16,12 +16,12 @@ struct CommonChoiceData {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The user of the turn choice
user: Arc<Pokemon>,
user: Pokemon,
/// The speed of the user at the beginning of the turn.
speed: u32,
speed: AtomicU32,
/// This random value is set at the beginning of the turn. It is used for tie breaking of the
/// turn order in a predictable way, regardless of implementation and hardware.
random_value: u32,
random_value: AtomicU32,
/// Whether or not the choice has failed. A failed choice will stop running, and execute special
/// fail handling during turn execution.
has_failed: AtomicBool,
@@ -57,32 +57,22 @@ impl TurnChoice {
TurnChoice::Pass(data) => &data.choice_data,
}
}
/// The shared choice data between each of the different turn choices.
fn choice_data_mut(&mut self) -> &mut Box<CommonChoiceData> {
match self {
TurnChoice::Move(data) => &mut data.choice_data,
TurnChoice::Item(data) => &mut data.choice_data,
TurnChoice::Switch(data) => &mut data.choice_data,
TurnChoice::Flee(data) => &mut data.choice_data,
TurnChoice::Pass(data) => &mut data.choice_data,
}
}
/// Get the user of the given choice.
pub fn user(&self) -> &Arc<Pokemon> {
pub fn user(&self) -> &Pokemon {
&self.choice_data().user
}
/// Get the speed of the user for the choice. Note that this speed is the speed of the Pokemon
/// at the start of the turn!
pub fn speed(&self) -> u32 {
self.choice_data().speed
self.choice_data().speed.load(Ordering::Relaxed)
}
/// Get the mutable speed of the user for the choice. Note that this speed is the speed of the Pokemon
/// at the start of the turn!
pub fn speed_mut(&mut self) -> &mut u32 {
&mut self.choice_data_mut().speed
/// Sets the speed of user for the choice. Note that this speed is the speed of the Pokemon at
/// the start of the turn!
pub fn set_speed(&self, value: u32) {
self.choice_data().speed.store(value, Ordering::Relaxed);
}
/// Gets whether or not the choice has failed. If we notice this when we execute the choice, we
@@ -101,12 +91,12 @@ impl TurnChoice {
/// breaking of turn executions. This means that choices get executed with a predictable order,
/// regardless of implementation details.
pub(crate) fn random_value(&self) -> u32 {
self.choice_data().random_value
self.choice_data().random_value.load(Ordering::Relaxed)
}
/// This sets the above random value.
pub(crate) fn set_random_value(&mut self, val: u32) {
self.choice_data_mut().random_value = val;
pub(crate) fn set_random_value(&self, val: u32) {
self.choice_data().random_value.store(val, Ordering::Relaxed)
}
/// Helper function to get the move choice data from a turn. Note that this will panic if not
@@ -174,26 +164,26 @@ pub struct MoveChoice {
/// The move script.
script: ScriptContainer,
/// The priority of the move choice at the beginning of the turn.
priority: i8,
priority: AtomicI8,
/// The common turn choice data.
choice_data: Box<CommonChoiceData>,
}
impl MoveChoice {
/// Initializes the data for a new move choice.
pub fn new(user: Arc<Pokemon>, used_move: Arc<LearnedMove>, target_side: u8, target_index: u8) -> Self {
pub fn new(user: Pokemon, used_move: Arc<LearnedMove>, target_side: u8, target_index: u8) -> Self {
let speed = user.boosted_stats().speed();
Self {
used_move,
target_side,
target_index,
script: Default::default(),
priority: 0,
priority: AtomicI8::new(0),
choice_data: Box::new(CommonChoiceData {
identifier: Default::default(),
user,
speed,
random_value: 0,
speed: AtomicU32::new(speed),
random_value: AtomicU32::new(0),
has_failed: Default::default(),
script_source_data: Default::default(),
}),
@@ -215,14 +205,14 @@ impl MoveChoice {
}
/// The priority of the move choice at the beginning of the turn.
pub fn priority(&self) -> i8 {
self.priority
self.priority.load(Ordering::Relaxed)
}
/// The priority of the move choice at the beginning of the turn.
pub fn priority_mut(&mut self) -> &mut i8 {
&mut self.priority
pub fn set_priority(&self, value: i8) {
self.priority.store(value, Ordering::Relaxed)
}
/// The user of the choice.
pub fn user(&self) -> &Arc<Pokemon> {
pub fn user(&self) -> &Pokemon {
&self.choice_data.user
}
/// The move script of the choice.
@@ -259,14 +249,14 @@ pub struct ItemChoice {
impl ItemChoice {
/// Initialised a new item choice.
pub fn new(user: Arc<Pokemon>) -> Self {
pub fn new(user: Pokemon) -> Self {
let speed = user.boosted_stats().speed();
Self {
choice_data: Box::new(CommonChoiceData {
identifier: Default::default(),
user,
speed,
random_value: 0,
speed: AtomicU32::new(speed),
random_value: AtomicU32::new(0),
has_failed: Default::default(),
script_source_data: Default::default(),
}),
@@ -299,14 +289,14 @@ pub struct SwitchChoice {
impl SwitchChoice {
/// Initialise the turn choice data.
pub fn new(user: Arc<Pokemon>) -> Self {
pub fn new(user: Pokemon) -> Self {
let speed = user.boosted_stats().speed();
Self {
choice_data: Box::new(CommonChoiceData {
identifier: Default::default(),
user,
speed,
random_value: 0,
speed: AtomicU32::new(speed),
random_value: AtomicU32::new(0),
has_failed: Default::default(),
script_source_data: Default::default(),
}),
@@ -339,13 +329,13 @@ pub struct FleeChoice {
impl FleeChoice {
/// Initialises a new flee choice.
pub fn new(user: Arc<Pokemon>) -> Self {
pub fn new(user: Pokemon) -> Self {
Self {
choice_data: Box::new(CommonChoiceData {
identifier: Default::default(),
user,
speed: 0,
random_value: 0,
speed: AtomicU32::new(0),
random_value: AtomicU32::new(0),
has_failed: Default::default(),
script_source_data: Default::default(),
}),
@@ -378,14 +368,14 @@ pub struct PassChoice {
impl PassChoice {
/// Initialised a new pass choice.
pub fn new(user: Arc<Pokemon>) -> Self {
pub fn new(user: Pokemon) -> Self {
let speed = user.boosted_stats().speed();
Self {
choice_data: Box::new(CommonChoiceData {
identifier: Default::default(),
user,
speed,
random_value: 0,
speed: AtomicU32::new(speed),
random_value: AtomicU32::new(0),
has_failed: Default::default(),
script_source_data: Default::default(),
}),
@@ -428,7 +418,10 @@ impl Ord for TurnChoice {
match self {
TurnChoice::Move(data) => {
if let TurnChoice::Move(other_data) = other {
let priority_compare = data.priority.cmp(&other_data.priority);
let priority_compare = data
.priority
.load(Ordering::Relaxed)
.cmp(&other_data.priority.load(Ordering::Relaxed));
if priority_compare != std::cmp::Ordering::Equal {
return priority_compare;
}

View File

@@ -29,6 +29,7 @@ impl EventHook {
/// listeners can exist at the same time. Note that for these functions the event will be disposed
/// of after the event is finished being sent.
pub fn register_listener(&self, func: EvtHookFn) {
#[allow(clippy::unwrap_used)] // This should never fail.
self.evt_hook_function.write().unwrap().push(func);
}
@@ -36,6 +37,7 @@ impl EventHook {
/// dispose of the event afterwards.
pub fn trigger(&self, evt: Event) {
let b = Box::new(&evt);
#[allow(clippy::unwrap_used)] // This should never fail.
let read_lock = self.evt_hook_function.read().unwrap();
for f in read_lock.iter() {
f(&b);
@@ -59,7 +61,7 @@ pub enum Event<'own> {
/// The index of the Pokemon that got switched in/out on its side
index: u8,
/// The new Pokemon that will be on the spot. If none, the spot will now be empty.
pokemon: Option<Arc<Pokemon>>,
pokemon: Option<Pokemon>,
},
/// A swap event happens when two Pokemon on a side swap positions. Note that this is rare.
Swap {

View File

@@ -6,6 +6,8 @@ use anyhow::Result;
use anyhow_ext::anyhow;
use parking_lot::lock_api::MappedRwLockReadGuard;
use parking_lot::{RawRwLock, RwLock, RwLockReadGuard};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
/// The ChoiceQueue is used to run choices one by one.
///
@@ -14,51 +16,50 @@ use parking_lot::{RawRwLock, RwLock, RwLockReadGuard};
/// helper functions to change the turn order while doing the execution. This is needed, as several
/// moves in Pokemon actively mess with this order.
#[derive(Debug)]
pub struct ChoiceQueue {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// Our storage of turn choices. Starts out completely filled, then slowly empties as turns get
/// executed.
queue: RwLock<Vec<Option<TurnChoice>>>,
queue: RwLock<Vec<Option<Arc<TurnChoice>>>>,
/// The current index of the turn we need to execute next.
current: usize,
current: AtomicUsize,
}
impl ChoiceQueue {
/// Initializes a ChoiceQueue, and sort the choices.
pub(crate) fn new(mut queue: Vec<Option<TurnChoice>>) -> Self {
pub(crate) fn new(mut queue: Vec<Option<Arc<TurnChoice>>>) -> Self {
queue.sort_unstable_by(|a, b| b.cmp(a));
Self {
identifier: Default::default(),
queue: RwLock::new(queue),
current: 0,
current: AtomicUsize::new(0),
}
}
/// Dequeues the next turn choice to be executed. This gives ownership to the callee, and replaces
/// our own reference to the turn choice with an empty spot. It also increments the current position
/// by one.
pub fn dequeue(&mut self) -> Result<Option<TurnChoice>> {
pub fn dequeue(&self) -> Result<Option<Arc<TurnChoice>>> {
let mut write_lock = self.queue.write();
if self.current >= write_lock.len() {
if self.current.load(Ordering::Relaxed) >= write_lock.len() {
return Ok(None);
}
let c = write_lock
.get_mut(self.current)
.get_mut(self.current.load(Ordering::Relaxed))
.ok_or(anyhow!("Unable to get current turn choice"))?
.take();
self.current += 1;
self.current.fetch_add(1, Ordering::Relaxed);
Ok(c)
}
/// This reads what the next choice to execute will be, without modifying state.
pub fn peek(&self) -> Result<Option<MappedRwLockReadGuard<'_, RawRwLock, TurnChoice>>> {
pub fn peek(&self) -> Result<Option<MappedRwLockReadGuard<'_, RawRwLock, Arc<TurnChoice>>>> {
let read_lock = self.queue.read();
if self.current >= read_lock.len() {
if self.current.load(Ordering::Relaxed) >= read_lock.len() {
Ok(None)
} else {
let v = RwLockReadGuard::try_map(read_lock, |a| match a.get(self.current) {
let v = RwLockReadGuard::try_map(read_lock, |a| match a.get(self.current.load(Ordering::Relaxed)) {
Some(Some(v)) => Some(v),
_ => None,
});
@@ -71,7 +72,7 @@ impl ChoiceQueue {
/// Check if we have any choices remaining.
pub fn has_next(&self) -> bool {
self.current < self.queue.read().len()
self.current.load(Ordering::Relaxed) < self.queue.read().len()
}
/// This resorts the yet to be executed choices. This can be useful for dealing with situations
@@ -80,19 +81,19 @@ impl ChoiceQueue {
pub fn resort(&mut self) -> Result<()> {
let len = self.queue.read().len();
let mut write_lock = self.queue.write();
for index in self.current..len {
for index in self.current.load(Ordering::Relaxed)..len {
let choice = &mut write_lock.get_mut_res(index)?;
if let Some(choice) = choice {
let mut speed = choice.user().boosted_stats().speed();
script_hook!(change_speed, (*choice), choice, &mut speed);
*choice.speed_mut() = speed;
choice.set_speed(speed)
}
}
write_lock
.get_mut(self.current..len)
.get_mut(self.current.load(Ordering::Relaxed)..len)
.ok_or(PkmnError::IndexOutOfBounds {
index: self.current,
index: self.current.load(Ordering::Relaxed),
len,
})?
.sort_unstable_by(|a, b| b.cmp(a));
@@ -104,7 +105,7 @@ impl ChoiceQueue {
let mut queue_lock = self.queue.write();
let mut desired_index = None;
// Find the index for the choice we want to move up.
for index in self.current..queue_lock.len() {
for index in self.current.load(Ordering::Relaxed)..queue_lock.len() {
if let Some(Some(choice)) = &queue_lock.get(index) {
if pokemon.value_identifier() == choice.user().value_identifier() {
desired_index = Some(index);
@@ -115,7 +116,7 @@ impl ChoiceQueue {
let result = match desired_index {
Some(desired_index) => {
// If the choice we want to move up is already the next choice, just return.
if desired_index == self.current {
if desired_index == self.current.load(Ordering::Relaxed) {
return Ok(true);
}
@@ -126,11 +127,14 @@ impl ChoiceQueue {
.ok_or(anyhow!("Choice was already taken"))?;
// Iterate backwards from the spot before the choice we want to move up, push them all back
// by 1 spot.
for index in (self.current..desired_index).rev() {
let current = self.current.load(Ordering::Relaxed);
for index in (current..desired_index).rev() {
queue_lock.swap(index, index + 1);
}
// Place the choice that needs to be next in the next to be executed position.
let _ = queue_lock.get_mut_res(self.current)?.insert(choice);
let _ = queue_lock
.get_mut_res(self.current.load(Ordering::Relaxed))?
.insert(choice);
true
}
None => false,
@@ -139,12 +143,14 @@ impl ChoiceQueue {
}
/// Internal helper function to be easily able to iterate over the yet to be executed choices.
pub(crate) fn get_queue(&self) -> Result<MappedRwLockReadGuard<'_, RawRwLock, [Option<TurnChoice>]>> {
pub(crate) fn get_queue(&self) -> Result<MappedRwLockReadGuard<'_, RawRwLock, [Option<Arc<TurnChoice>>]>> {
let read_lock = self.queue.read();
match RwLockReadGuard::try_map(read_lock, |a| a.get(self.current..self.queue.read().len())) {
match RwLockReadGuard::try_map(read_lock, |a| {
a.get(self.current.load(Ordering::Relaxed)..self.queue.read().len())
}) {
Ok(v) => Ok(v),
Err(_) => Err(PkmnError::IndexOutOfBounds {
index: self.current,
index: self.current.load(Ordering::Relaxed),
len: self.queue.read().len(),
}
.into()),
@@ -176,7 +182,7 @@ mod tests {
#[test]
fn dequeue_from_empty_queue() {
let mut queue = ChoiceQueue::new(Vec::new());
let queue = ChoiceQueue::new(Vec::new());
assert!(queue.dequeue().unwrap().is_none());
}
@@ -204,9 +210,9 @@ mod tests {
#[test]
fn create_queue_with_single_item() {
let user = Arc::new(get_user(10));
let user = get_user(10);
let queue = ChoiceQueue::new(vec![Some(TurnChoice::Pass(PassChoice::new(user)))]);
let queue = ChoiceQueue::new(vec![Some(Arc::new(TurnChoice::Pass(PassChoice::new(user))))]);
assert!(queue.has_next());
assert!(queue.peek().unwrap().is_some());
assert_eq!(7, queue.peek().unwrap().unwrap().speed());
@@ -214,9 +220,9 @@ mod tests {
#[test]
fn dequeue_from_queue_with_single_item() {
let user = Arc::new(get_user(10));
let user = get_user(10);
let mut queue = ChoiceQueue::new(vec![Some(TurnChoice::Pass(PassChoice::new(user)))]);
let queue = ChoiceQueue::new(vec![Some(Arc::new(TurnChoice::Pass(PassChoice::new(user))))]);
assert!(queue.has_next());
assert_eq!(7, queue.dequeue().unwrap().unwrap().speed());
assert!(!queue.has_next());
@@ -225,12 +231,12 @@ mod tests {
#[test]
fn create_queue_with_two_items_with_equal_order() {
let user1 = Arc::new(get_user(10));
let user2 = Arc::new(get_user(10));
let user1 = get_user(10);
let user2 = get_user(10);
let queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1))),
Some(TurnChoice::Pass(PassChoice::new(user2))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1)))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2)))),
]);
assert!(queue.has_next());
assert!(queue.peek().unwrap().is_some());
@@ -239,12 +245,12 @@ mod tests {
#[test]
fn create_queue_with_two_items_get_queue() {
let user1 = Arc::new(get_user(10));
let user2 = Arc::new(get_user(5));
let user1 = get_user(10);
let user2 = get_user(5);
let queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
Some(TurnChoice::Pass(PassChoice::new(user2))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2)))),
]);
let inner_queue = queue.get_queue().unwrap();
assert_eq!(
@@ -255,12 +261,12 @@ mod tests {
#[test]
fn create_queue_with_two_items_in_wrong_order_sorts_correctly() {
let user1 = Arc::new(get_user(5));
let user2 = Arc::new(get_user(100));
let user1 = get_user(5);
let user2 = get_user(100);
let mut queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1))),
Some(TurnChoice::Pass(PassChoice::new(user2))),
let queue = ChoiceQueue::new(vec![
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1)))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2)))),
]);
assert_eq!(25, queue.dequeue().unwrap().unwrap().speed());
assert_eq!(6, queue.dequeue().unwrap().unwrap().speed());
@@ -268,12 +274,12 @@ mod tests {
#[test]
fn resort_with_two_choices() {
let user1 = Arc::new(get_user(50));
let user2 = Arc::new(get_user(1));
let user1 = get_user(50);
let user2 = get_user(1);
let mut queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
Some(TurnChoice::Pass(PassChoice::new(user2.clone()))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2.clone())))),
]);
user2.change_level_by(60).unwrap();
@@ -290,18 +296,18 @@ mod tests {
#[test]
fn move_pokemon_choice_first_with_two_choices() {
let user1 = Arc::new(get_user(100));
let user2 = Arc::new(get_user(1));
let user1 = get_user(100);
let user2 = get_user(1);
let mut queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
Some(TurnChoice::Pass(PassChoice::new(user2.clone()))),
let queue = ChoiceQueue::new(vec![
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2.clone())))),
]);
assert_eq!(
user1.value_identifier(),
queue.peek().unwrap().unwrap().user().value_identifier()
);
assert!(queue.move_pokemon_choice_next(user2.as_ref()).unwrap());
assert!(queue.move_pokemon_choice_next(&user2).unwrap());
assert_eq!(
user2.value_identifier(),
@@ -315,18 +321,18 @@ mod tests {
#[test]
fn move_pokemon_choice_first_when_choice_has_already_been() {
let user1 = Arc::new(get_user(10));
let user2 = Arc::new(get_user(100));
let user1 = get_user(10);
let user2 = get_user(100);
let mut queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
Some(TurnChoice::Pass(PassChoice::new(user2.clone()))),
let queue = ChoiceQueue::new(vec![
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2.clone())))),
]);
assert_eq!(
user2.value_identifier(),
queue.dequeue().unwrap().unwrap().user().value_identifier()
);
assert!(!queue.move_pokemon_choice_next(user2.as_ref()).unwrap());
assert!(!queue.move_pokemon_choice_next(&user2).unwrap());
assert_eq!(
user1.value_identifier(),
@@ -337,18 +343,18 @@ mod tests {
#[test]
fn move_pokemon_choice_first_when_choice_is_next() {
let user1 = Arc::new(get_user(100));
let user2 = Arc::new(get_user(10));
let user1 = get_user(100);
let user2 = get_user(10);
let queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(user1.clone()))),
Some(TurnChoice::Pass(PassChoice::new(user2))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user1.clone())))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(user2)))),
]);
assert_eq!(
user1.value_identifier(),
queue.peek().unwrap().unwrap().user().value_identifier()
);
assert!(queue.move_pokemon_choice_next(user1.as_ref()).unwrap());
assert!(queue.move_pokemon_choice_next(&user1).unwrap());
assert_eq!(
user1.value_identifier(),
queue.peek().unwrap().unwrap().user().value_identifier()
@@ -358,29 +364,29 @@ mod tests {
#[test]
fn move_pokemon_choice_first_with_seven_choices() {
let users = [
Arc::new(get_user(100)),
Arc::new(get_user(90)),
Arc::new(get_user(80)),
Arc::new(get_user(70)),
Arc::new(get_user(60)),
Arc::new(get_user(50)),
Arc::new(get_user(40)),
get_user(100),
get_user(90),
get_user(80),
get_user(70),
get_user(60),
get_user(50),
get_user(40),
];
let mut queue = ChoiceQueue::new(vec![
Some(TurnChoice::Pass(PassChoice::new(users[0].clone()))),
Some(TurnChoice::Pass(PassChoice::new(users[1].clone()))),
Some(TurnChoice::Pass(PassChoice::new(users[2].clone()))),
Some(TurnChoice::Pass(PassChoice::new(users[3].clone()))),
Some(TurnChoice::Pass(PassChoice::new(users[4].clone()))),
Some(TurnChoice::Pass(PassChoice::new(users[5].clone()))),
Some(TurnChoice::Pass(PassChoice::new(users[6].clone()))),
let queue = ChoiceQueue::new(vec![
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[0].clone())))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[1].clone())))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[2].clone())))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[3].clone())))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[4].clone())))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[5].clone())))),
Some(Arc::new(TurnChoice::Pass(PassChoice::new(users[6].clone())))),
]);
assert_eq!(
users[0].value_identifier(),
queue.peek().unwrap().unwrap().user().value_identifier()
);
assert!(queue.move_pokemon_choice_next(users[4].as_ref()).unwrap());
assert!(queue.move_pokemon_choice_next(&users[4]).unwrap());
assert_eq!(
users[4].value_identifier(),

View File

@@ -1,6 +1,5 @@
use anyhow::Result;
use std::ops::Deref;
use std::sync::Arc;
use num_traits::abs;
@@ -10,7 +9,7 @@ use crate::static_data::MoveTarget;
use crate::VecExt;
/// Helper type for the vector of targ ets we will return.
pub type TargetList = Vec<Option<Arc<Pokemon>>>;
pub type TargetList = Vec<Option<Pokemon>>;
/// This returns all Pokemon in the battle.
fn get_all_targets(battle: &Battle) -> TargetList {

View File

@@ -77,9 +77,9 @@ impl Battle {
}
/// Executes a single choice.
fn execute_choice(&self, choice: &TurnChoice) -> Result<()> {
fn execute_choice(&self, choice: &Arc<TurnChoice>) -> Result<()> {
// A pass turn choice means the user does not intend to do anything. As such, return.
if let TurnChoice::Pass(..) = choice {
if let TurnChoice::Pass(..) = choice.deref() {
return Ok(());
}
if self.has_ended() {
@@ -95,7 +95,7 @@ impl Battle {
if !self.can_use(choice) {
return Ok(());
}
match choice {
match choice.deref() {
TurnChoice::Move(..) => self.execute_move_choice(choice)?,
TurnChoice::Item(_) => {}
TurnChoice::Switch(_) => {}
@@ -106,7 +106,7 @@ impl Battle {
}
/// Executes a move choice.
fn execute_move_choice<'func>(&'func self, choice: &'func TurnChoice) -> Result<()> {
fn execute_move_choice<'func>(&'func self, choice: &'func Arc<TurnChoice>) -> Result<()> {
let move_choice = choice.get_move_turn_data()?;
let used_move = move_choice.used_move();
let move_data = {
@@ -129,14 +129,14 @@ impl Battle {
if number_of_hits == 0 {
return Ok(());
}
let mut executing_move = ExecutingMove::new(
let executing_move = Arc::new(ExecutingMove::new(
targets.clone(),
number_of_hits,
choice.user().clone(),
used_move.clone(),
move_data,
move_choice.script().clone(),
);
));
let mut prevented = false;
script_hook!(prevent_move, executing_move, &executing_move, &mut prevented);
if prevented {
@@ -161,13 +161,13 @@ impl Battle {
}
script_hook!(on_before_move, executing_move, &executing_move);
for target in targets.iter().flatten() {
self.handle_move_for_target(&mut executing_move, target)?;
self.handle_move_for_target(&executing_move, target)?;
}
Ok(())
}
/// Executes a move turn choice on a single target.
fn handle_move_for_target(&self, executing_move: &mut ExecutingMove, target: &Arc<Pokemon>) -> Result<()> {
fn handle_move_for_target(&self, executing_move: &Arc<ExecutingMove>, target: &Pokemon) -> Result<()> {
{
let mut fail = false;
script_hook!(fail_incoming_move, target, executing_move, target, &mut fail);

View File

@@ -14,8 +14,8 @@ pub trait DamageLibrary: std::fmt::Debug + ValueIdentifiable {
/// Calculate the damage for a given hit on a Pokemon.
fn get_damage(
&self,
executing_move: &ExecutingMove,
target: &Arc<Pokemon>,
executing_move: &Arc<ExecutingMove>,
target: &Pokemon,
hit_number: u8,
hit_data: &HitData,
) -> Result<u32>;
@@ -23,8 +23,8 @@ pub trait DamageLibrary: std::fmt::Debug + ValueIdentifiable {
/// Calculate the base power for a given hit on a Pokemon.
fn get_base_power(
&self,
executing_move: &ExecutingMove,
target: &Arc<Pokemon>,
executing_move: &Arc<ExecutingMove>,
target: &Pokemon,
hit_number: u8,
hit_data: &HitData,
) -> Result<u8>;
@@ -33,8 +33,8 @@ pub trait DamageLibrary: std::fmt::Debug + ValueIdentifiable {
fn is_critical(
&self,
battle: &Battle,
executing_move: &ExecutingMove,
target: &Arc<Pokemon>,
executing_move: &Arc<ExecutingMove>,
target: &Pokemon,
hit_number: u8,
) -> Result<bool>;
}
@@ -61,8 +61,8 @@ impl Gen7DamageLibrary {
/// Calculates the modifier applied to damage from the statistics of the relevant Pokemon.
fn get_stat_modifier(
&self,
executing_move: &ExecutingMove,
target: &Arc<Pokemon>,
executing_move: &Arc<ExecutingMove>,
target: &Pokemon,
hit_number: u8,
hit_data: &HitData,
) -> Result<f32> {
@@ -151,8 +151,8 @@ impl Gen7DamageLibrary {
/// to apply a raw modifier to the damage.
fn get_damage_modifier(
&self,
executing_move: &ExecutingMove,
target: &Arc<Pokemon>,
executing_move: &Arc<ExecutingMove>,
target: &Pokemon,
hit_number: u8,
_hit_data: &HitData,
) -> Result<f32> {
@@ -172,8 +172,8 @@ impl Gen7DamageLibrary {
impl DamageLibrary for Gen7DamageLibrary {
fn get_damage(
&self,
executing_move: &ExecutingMove,
target: &Arc<Pokemon>,
executing_move: &Arc<ExecutingMove>,
target: &Pokemon,
hit_number: u8,
hit_data: &HitData,
) -> Result<u32> {
@@ -260,8 +260,8 @@ impl DamageLibrary for Gen7DamageLibrary {
fn get_base_power(
&self,
executing_move: &ExecutingMove,
target: &Arc<Pokemon>,
executing_move: &Arc<ExecutingMove>,
target: &Pokemon,
hit_number: u8,
_hit_data: &HitData,
) -> Result<u8> {
@@ -284,8 +284,8 @@ impl DamageLibrary for Gen7DamageLibrary {
fn is_critical(
&self,
battle: &Battle,
executing_move: &ExecutingMove,
target: &Arc<Pokemon>,
executing_move: &Arc<ExecutingMove>,
target: &Pokemon,
hit_number: u8,
) -> Result<bool> {
// Status moves can't be critical.

View File

@@ -16,15 +16,15 @@ use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
/// calculators that might be customized between different generations and implementations.
pub trait DynamicLibrary: Debug + ValueIdentifiable {
/// The static data is the immutable storage data for this library.
fn static_data(&self) -> &Box<dyn StaticData>;
fn static_data(&self) -> &Arc<dyn StaticData>;
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
fn stat_calculator(&self) -> &Box<dyn BattleStatCalculator>;
fn stat_calculator(&self) -> &Arc<dyn BattleStatCalculator>;
/// The damage calculator deals with the calculation of things relating to damage.
fn damage_calculator(&self) -> &Box<dyn DamageLibrary>;
fn damage_calculator(&self) -> &Arc<dyn DamageLibrary>;
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
fn misc_library(&self) -> &Box<dyn MiscLibrary>;
fn misc_library(&self) -> &Arc<dyn MiscLibrary>;
/// Loads a standard script with a given unique combination of category and key. If no script
/// can be created with this combination, returns None.
@@ -47,15 +47,15 @@ pub struct DynamicLibraryImpl {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The static data is the immutable storage data for this library.
static_data: Box<dyn StaticData>,
static_data: Arc<dyn StaticData>,
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
stat_calculator: Box<dyn BattleStatCalculator>,
stat_calculator: Arc<dyn BattleStatCalculator>,
/// The damage calculator deals with the calculation of things relating to damage.
damage_calculator: Box<dyn DamageLibrary>,
damage_calculator: Arc<dyn DamageLibrary>,
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
misc_library: Box<dyn MiscLibrary>,
misc_library: Arc<dyn MiscLibrary>,
/// The script resolver deals with how to resolve the scripts from specific unique key combinations.
script_resolver: Box<dyn ScriptResolver>,
@@ -64,10 +64,10 @@ pub struct DynamicLibraryImpl {
impl DynamicLibraryImpl {
/// Instantiates a new DynamicLibrary with given parameters.
pub fn new(
static_data: Box<dyn StaticData>,
stat_calculator: Box<dyn BattleStatCalculator>,
damage_calculator: Box<dyn DamageLibrary>,
misc_library: Box<dyn MiscLibrary>,
static_data: Arc<dyn StaticData>,
stat_calculator: Arc<dyn BattleStatCalculator>,
damage_calculator: Arc<dyn DamageLibrary>,
misc_library: Arc<dyn MiscLibrary>,
script_resolver: Box<dyn ScriptResolver>,
) -> Self {
Self {
@@ -83,21 +83,21 @@ impl DynamicLibraryImpl {
impl DynamicLibrary for DynamicLibraryImpl {
/// The static data is the immutable storage data for this library.
fn static_data(&self) -> &Box<dyn StaticData> {
fn static_data(&self) -> &Arc<dyn StaticData> {
&self.static_data
}
/// The stat calculator deals with the calculation of flat and boosted stats, based on the
/// Pokemons attributes.
fn stat_calculator(&self) -> &Box<dyn BattleStatCalculator> {
fn stat_calculator(&self) -> &Arc<dyn BattleStatCalculator> {
&self.stat_calculator
}
/// The damage calculator deals with the calculation of things relating to damage.
fn damage_calculator(&self) -> &Box<dyn DamageLibrary> {
fn damage_calculator(&self) -> &Arc<dyn DamageLibrary> {
&self.damage_calculator
}
/// The Misc Library holds minor functions that do not fall in any of the other libraries and
/// calculators.
fn misc_library(&self) -> &Box<dyn MiscLibrary> {
fn misc_library(&self) -> &Arc<dyn MiscLibrary> {
&self.misc_library
}
@@ -139,10 +139,10 @@ pub mod test {
#[derive(Debug)]
pub DynamicLibrary{}
impl DynamicLibrary for DynamicLibrary {
fn static_data(&self) -> &Box<dyn StaticData>;
fn stat_calculator(&self) -> &Box<dyn BattleStatCalculator>;
fn damage_calculator(&self) -> &Box<dyn DamageLibrary>;
fn misc_library(&self) -> &Box<dyn MiscLibrary>;
fn static_data(&self) -> &Arc<dyn StaticData>;
fn stat_calculator(&self) -> &Arc<dyn BattleStatCalculator>;
fn damage_calculator(&self) -> &Arc<dyn DamageLibrary>;
fn misc_library(&self) -> &Arc<dyn MiscLibrary>;
fn load_script(
&self,
owner: ScriptOwnerData,
@@ -161,10 +161,10 @@ pub mod test {
pub fn build() -> DynamicLibraryImpl {
DynamicLibraryImpl {
identifier: Default::default(),
static_data: Box::new(crate::static_data::libraries::static_data::test::build()),
stat_calculator: Box::new(Gen7BattleStatCalculator::new()),
damage_calculator: Box::new(Gen7DamageLibrary::new(false)),
misc_library: Box::new(Gen7MiscLibrary::new()),
static_data: Arc::new(crate::static_data::libraries::static_data::test::build()),
stat_calculator: Arc::new(Gen7BattleStatCalculator::new()),
damage_calculator: Arc::new(Gen7DamageLibrary::new(false)),
misc_library: Arc::new(Gen7MiscLibrary::new()),
script_resolver: Box::new(EmptyScriptResolver {
identifier: Default::default(),
}),

View File

@@ -14,7 +14,7 @@ pub trait MiscLibrary: Debug + ValueIdentifiable {
/// Returns whether or not a Pokemon is allowed to flee or switch out.
fn can_flee(&self, choice: &TurnChoice) -> bool;
/// Returns the move we need to use if we can't use another move. Typically Struggle.
fn replacement_move(&self, user: &Arc<Pokemon>, target_side: u8, target_index: u8) -> TurnChoice;
fn replacement_move(&self, user: &Pokemon, target_side: u8, target_index: u8) -> TurnChoice;
// TODO: can evolve from level up?
// TODO: get time
}
@@ -66,7 +66,7 @@ impl MiscLibrary for Gen7MiscLibrary {
todo!()
}
fn replacement_move(&self, user: &Arc<Pokemon>, target_side: u8, target_index: u8) -> TurnChoice {
fn replacement_move(&self, user: &Pokemon, target_side: u8, target_index: u8) -> TurnChoice {
self.struggle_learned_move.restore_all_uses();
TurnChoice::Move(MoveChoice::new(
user.clone(),

View File

@@ -1,6 +1,7 @@
use std::ffi::c_void;
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::Arc;
use std::sync::{Arc, Weak};
use anyhow::Result;
use anyhow_ext::anyhow;
@@ -22,15 +23,15 @@ use crate::dynamic_data::{ChoiceQueue, ScriptContainer};
use crate::dynamic_data::{ScriptCategory, ScriptSource, ScriptSourceData};
use crate::{script_hook, PkmnError, StringKey, ValueIdentifiable, ValueIdentifier, VecExt};
/// A pokemon battle, with any amount of sides and pokemon per side.
/// The data of a battle.
#[derive(Debug)]
pub struct Battle {
struct BattleData {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The library the battle uses for handling.
library: Arc<dyn DynamicLibrary>,
/// A list of all different parties in the battle.
parties: Vec<BattleParty>,
parties: Vec<Arc<BattleParty>>,
/// Whether or not Pokemon can flee from the battle.
can_flee: bool,
/// The number of sides in the battle. Typically 2.
@@ -40,9 +41,9 @@ pub struct Battle {
/// A list of all sides in the battle.
sides: Vec<BattleSide>,
/// The RNG used for the battle.
random: BattleRandom,
random: Arc<BattleRandom>,
/// A queue of the yet to be executed choices in a turn.
current_turn_queue: RwLock<Option<ChoiceQueue>>,
current_turn_queue: RwLock<Option<Arc<ChoiceQueue>>>,
/// Whether or not the battle has ended.
has_ended: AtomicBool,
/// The eventual result of the battle. Inconclusive until the battle is ended.
@@ -61,16 +62,30 @@ pub struct Battle {
script_source_data: RwLock<ScriptSourceData>,
}
/// A pokemon battle, with any amount of sides and pokemon per side.
#[derive(Clone, Debug)]
pub struct Battle {
/// The actual data of the battle.
data: Arc<BattleData>,
}
/// A weak reference to a battle.
#[derive(Clone, Debug, Default)]
pub struct WeakBattleReference {
/// A weak reference to the actual data of the battle.
data: Weak<BattleData>,
}
impl Battle {
/// Initializes a new battle.
pub fn new(
library: Arc<dyn DynamicLibrary>,
parties: Vec<BattleParty>,
parties: Vec<Arc<BattleParty>>,
can_flee: bool,
number_of_sides: u8,
pokemon_per_side: u8,
random_seed: Option<u128>,
) -> Arc<Self> {
) -> Self {
// If no seed was passed, we use the current time as seed for the RNG, otherwise we use the
// seed.
let random = if let Some(seed) = random_seed {
@@ -83,7 +98,7 @@ impl Battle {
sides.push(BattleSide::new(i, pokemon_per_side));
}
let battle = Self {
let battle = BattleData {
identifier: Default::default(),
library,
parties,
@@ -91,7 +106,7 @@ impl Battle {
number_of_sides,
pokemon_per_side,
sides,
random,
random: Arc::new(random),
current_turn_queue: RwLock::new(None),
has_ended: AtomicBool::new(false),
result: RwLock::new(BattleResult::Inconclusive),
@@ -104,73 +119,77 @@ impl Battle {
};
let battle_arc = Arc::new(battle);
let battle_ptr = Arc::as_ptr(&battle_arc) as *mut Battle;
unsafe {
for side in &mut battle_ptr.as_mut().unwrap().sides {
side.set_battle(Arc::downgrade(&battle_arc));
}
let battle = Self { data: battle_arc };
for side in &battle.data.sides {
side.set_battle(battle.weak());
}
battle_arc
battle
}
/// Returns a weak reference to the battle.
pub fn weak(&self) -> WeakBattleReference {
WeakBattleReference {
data: Arc::downgrade(&self.data),
}
}
/// The library the battle uses for handling.
pub fn library(&self) -> &Arc<dyn DynamicLibrary> {
&self.library
&self.data.library
}
/// A list of all different parties in the battle.
pub fn parties(&self) -> &Vec<BattleParty> {
&self.parties
pub fn parties(&self) -> &Vec<Arc<BattleParty>> {
&self.data.parties
}
/// Whether or not Pokemon can flee from the battle.
pub fn can_flee(&self) -> bool {
self.can_flee
self.data.can_flee
}
/// The number of sides in the battle. Typically 2.
pub fn number_of_sides(&self) -> u8 {
self.number_of_sides
self.data.number_of_sides
}
/// The number of Pokemon that can be on each side.
pub fn pokemon_per_side(&self) -> u8 {
self.pokemon_per_side
self.data.pokemon_per_side
}
/// A list of all sides in the battle.
pub fn sides(&self) -> &Vec<BattleSide> {
&self.sides
&self.data.sides
}
/// The RNG used for the battle.
pub fn random(&self) -> &BattleRandom {
&self.random
pub fn random(&self) -> &Arc<BattleRandom> {
&self.data.random
}
/// Whether or not the battle has ended.
pub fn has_ended(&self) -> bool {
self.has_ended.load(Ordering::Relaxed)
self.data.has_ended.load(Ordering::Relaxed)
}
/// The eventual result of the battle. Inconclusive until the battle is ended.
pub fn result(&self) -> BattleResult {
*self.result.read()
*self.data.result.read()
}
/// The handler to send all events to.
pub fn event_hook(&self) -> &EventHook {
&self.event_hook
&self.data.event_hook
}
/// The index of the current turn. 0 until all choices
pub fn current_turn(&self) -> u32 {
self.current_turn.load(Ordering::Relaxed)
self.data.current_turn.load(Ordering::Relaxed)
}
/// 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)
self.data.last_turn_time.load(Ordering::Relaxed)
}
/// A queue of the yet to be executed choices in a turn.
pub fn current_turn_queue(&self) -> &RwLock<Option<ChoiceQueue>> {
&self.current_turn_queue
pub fn current_turn_queue(&self) -> &RwLock<Option<Arc<ChoiceQueue>>> {
&self.data.current_turn_queue
}
/// Get a Pokemon on the battlefield, on a specific side and an index on that side.
pub fn get_pokemon(&self, side: u8, index: u8) -> Option<Arc<Pokemon>> {
let side = self.sides.get(side as usize);
pub fn get_pokemon(&self, side: u8, index: u8) -> Option<Pokemon> {
let side = self.data.sides.get(side as usize);
let pokemon_read_lock = side?.pokemon();
let pokemon = pokemon_read_lock.get(index as usize);
pokemon?.clone()
@@ -180,7 +199,7 @@ impl Battle {
/// for that slot, or a party is responsible, but has no remaining Pokemon to throw out anymore,
/// this returns false.
pub fn can_slot_be_filled(&self, side: u8, index: u8) -> bool {
for party in &self.parties {
for party in &self.data.parties {
if party.is_responsible_for_index(side, index) && party.has_pokemon_not_in_field() {
return true;
}
@@ -197,12 +216,12 @@ impl Battle {
}
let mut surviving_side_exists = false;
let mut winning_side = None;
for (side_index, side) in self.sides.iter().enumerate() {
for (side_index, side) in self.data.sides.iter().enumerate() {
// If any side has fled, the battle end.
if side.has_fled_battle() {
let mut w = self.result.write();
let mut w = self.data.result.write();
*w = BattleResult::Inconclusive;
self.has_ended.store(true, Ordering::SeqCst);
self.data.has_ended.store(true, Ordering::SeqCst);
return Ok(());
}
// If the side is not defeated
@@ -217,15 +236,15 @@ impl Battle {
}
// Everyone died :(
if !surviving_side_exists {
let mut w = self.result.write();
let mut w = self.data.result.write();
*w = BattleResult::Inconclusive;
}
// Someone survived, they won!
else {
let mut w = self.result.write();
let mut w = self.data.result.write();
*w = BattleResult::Conclusive(winning_side.ok_or(anyhow!("Winning side was not set"))?);
}
self.has_ended.store(true, Ordering::SeqCst);
self.data.has_ended.store(true, Ordering::SeqCst);
Ok(())
}
@@ -263,7 +282,7 @@ impl Battle {
let side = choice.user().get_battle_side_index();
match side {
Some(side) => {
self.sides.get_res(side as usize)?.set_choice(choice)?;
self.data.sides.get_res(side as usize)?.set_choice(choice)?;
self.check_choices_set_and_run()?;
Ok(true)
}
@@ -274,7 +293,7 @@ impl Battle {
/// Checks to see whether all Pokemon on the field have set their choices. If so, we then run
/// the turn.
fn check_choices_set_and_run(&self) -> Result<()> {
for side in &self.sides {
for side in &self.data.sides {
if !side.all_choices_set() {
return Ok(());
}
@@ -283,20 +302,19 @@ impl Battle {
}
}
let start_time = chrono::Utc::now();
let mut choices = Vec::with_capacity(self.number_of_sides as usize * self.pokemon_per_side as usize);
for side in &self.sides {
let mut choices = Vec::with_capacity(self.data.number_of_sides as usize * self.data.pokemon_per_side as usize);
for side in &self.data.sides {
let mut side_choices = side.choices().write();
for choice_opt in side_choices.deref_mut() {
let mut choice = choice_opt
let choice = choice_opt
.as_mut()
.ok_or(anyhow!("Choice was none, but all choices were set? Logic error."))?;
let c = choice.deref();
if let TurnChoice::Move(data) = c {
if let TurnChoice::Move(data) = choice.clone().deref() {
let mut change_priority = data.priority();
script_hook!(change_priority, c, c, &mut change_priority);
script_hook!(change_priority, choice, choice, &mut change_priority);
if change_priority != data.priority() {
if let TurnChoice::Move(data) = choice.deref_mut() {
*data.priority_mut() = change_priority;
if let TurnChoice::Move(data) = choice.clone().deref() {
data.set_priority(change_priority);
}
}
}
@@ -304,31 +322,34 @@ impl Battle {
let mut speed = choice.user().boosted_stats().speed();
let c = choice.deref();
script_hook!(change_speed, c, c, &mut speed);
*choice.speed_mut() = speed;
choice.set_speed(speed);
choice.set_random_value(self.random.get()? as u32);
choice.set_random_value(self.data.random.get()? as u32);
choices.push(choice_opt.take());
}
// Drop the lock guard, as we need to write into it in reset_choices.
drop(side_choices);
side.reset_choices();
}
self.current_turn.fetch_add(1, Ordering::SeqCst);
self.data.current_turn.fetch_add(1, Ordering::SeqCst);
self.current_turn_queue.write().replace(ChoiceQueue::new(choices));
self.data
.current_turn_queue
.write()
.replace(Arc::new(ChoiceQueue::new(choices)));
{
self.run_turn()?;
}
self.current_turn_queue.write().take();
self.event_hook.trigger(Event::EndTurn);
self.data.current_turn_queue.write().take();
self.data.event_hook.trigger(Event::EndTurn);
let end_time = chrono::Utc::now();
let time = end_time - start_time;
match time.num_nanoseconds() {
None => {}
Some(v) => {
self.last_turn_time.store(v as u64, Ordering::SeqCst);
self.data.last_turn_time.store(v as u64, Ordering::SeqCst);
}
}
Ok(())
@@ -341,16 +362,16 @@ impl Battle {
.library()
.load_script(self.into(), ScriptCategory::Weather, &weather)?
.ok_or(anyhow!("Couldn't find weather script by name {}", weather))?;
self.weather.set(script);
self.data.weather.set(script);
} else {
self.weather.clear();
self.data.weather.clear();
}
Ok(())
}
/// Gets the current weather of the battle. If no weather is present, this returns None.
pub fn weather_name(&self) -> Result<Option<StringKey>> {
if let Some(script) = self.weather.get() {
if let Some(script) = self.data.weather.get() {
let lock = script.read();
Ok(Some(
lock.as_ref().ok_or(PkmnError::UnableToAcquireLock)?.name()?.clone(),
@@ -361,13 +382,38 @@ impl Battle {
}
}
impl WeakBattleReference {
/// Attempts to upgrade the weak reference to a strong reference. If the strong reference has
/// been dropped, this returns None.
pub fn upgrade(&self) -> Option<Battle> {
self.data.upgrade().map(|battle| Battle { data: battle })
}
/// Gets the inner pointer to the reference counted data.
pub(crate) fn as_ptr(&self) -> *const c_void {
self.data.as_ptr() as *const c_void
}
}
unsafe impl Send for WeakBattleReference {}
unsafe impl Sync for WeakBattleReference {}
impl PartialEq for WeakBattleReference {
fn eq(&self, other: &Self) -> bool {
self.data.ptr_eq(&other.data)
}
}
impl Eq for WeakBattleReference {}
impl VolatileScriptsOwner for Battle {
fn volatile_scripts(&self) -> &Arc<ScriptSet> {
&self.volatile_scripts
&self.data.volatile_scripts
}
fn load_volatile_script(&self, key: &StringKey) -> Result<Option<Arc<dyn Script>>> {
self.library.load_script(self.into(), ScriptCategory::Battle, key)
self.data.library.load_script(self.into(), ScriptCategory::Battle, key)
}
}
@@ -377,12 +423,12 @@ impl ScriptSource for Battle {
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
&self.script_source_data
&self.data.script_source_data
}
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
scripts.push((&self.weather).into());
scripts.push((&self.volatile_scripts).into());
scripts.push((&self.data.weather).into());
scripts.push((&self.data.volatile_scripts).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
@@ -393,7 +439,7 @@ impl ScriptSource for Battle {
impl ValueIdentifiable for Battle {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
self.data.identifier
}
}

View File

@@ -49,7 +49,7 @@ impl BattleParty {
}
/// Gets a Pokemon at an index.
pub fn get_pokemon(&self, index: usize) -> Option<Arc<Pokemon>> {
pub fn get_pokemon(&self, index: usize) -> Option<Pokemon> {
self.party.at(index)
}

View File

@@ -60,8 +60,8 @@ impl BattleRandom {
pub fn effect_chance(
&self,
mut chance: f32,
executing_move: &ExecutingMove,
target: &Arc<Pokemon>,
executing_move: &Arc<ExecutingMove>,
target: &Pokemon,
hit_number: u8,
) -> Result<bool> {
script_hook!(

View File

@@ -1,3 +1,4 @@
use std::ffi::c_void;
use std::ops::Deref;
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
use std::sync::{Arc, Weak};
@@ -11,14 +12,14 @@ use crate::dynamic_data::event_hooks::Event;
use crate::dynamic_data::models::battle::Battle;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
use crate::dynamic_data::Script;
use crate::dynamic_data::ScriptSet;
use crate::dynamic_data::VolatileScriptsOwner;
use crate::dynamic_data::{Script, WeakBattleReference};
use crate::{script_hook, StringKey, ValueIdentifiable, ValueIdentifier, VecExt};
/// A side on a battle.
/// The data that is stored for a battle side.
#[derive(Debug)]
pub struct BattleSide {
struct BattleSideData {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The index of the side on the battle.
@@ -26,18 +27,18 @@ pub struct BattleSide {
/// The number of Pokemon that can be on the side.
pokemon_per_side: u8,
/// A list of pokemon currently on the battlefield.
pokemon: RwLock<Vec<Option<Arc<Pokemon>>>>,
pokemon: RwLock<Vec<Option<Pokemon>>>,
/// The currently set choices for all Pokemon on the battlefield. Cleared when the turn starts.
choices: RwLock<Vec<Option<TurnChoice>>>,
choices: RwLock<Vec<Option<Arc<TurnChoice>>>>,
/// The slots on the side that can still be filled. Once all slots are set to false, this side
/// has lost the battle.
fillable_slots: Vec<AtomicBool>,
/// The number of choices that are set.
choices_set: AtomicU8,
/// A reference to the battle we're part of.
battle: Weak<Battle>,
battle: WeakBattleReference,
/// Whether or not this side has fled.
has_fled_battle: bool,
has_fled_battle: AtomicBool,
/// The volatile scripts that are attached to the side.
volatile_scripts: Arc<ScriptSet>,
@@ -45,6 +46,20 @@ pub struct BattleSide {
script_source_data: RwLock<ScriptSourceData>,
}
/// A side on a battle.
#[derive(Debug)]
pub struct BattleSide {
/// The data that is stored for this side.
data: Arc<BattleSideData>,
}
/// A non owning reference to a battle side.
#[derive(Debug, Clone)]
pub struct WeakBattleSideReference {
/// A weak reference to the data of the battle side.
data: Weak<BattleSideData>,
}
impl BattleSide {
/// Instantiates a battle side.
pub fn new(index: u8, pokemon_per_side: u8) -> Self {
@@ -61,83 +76,92 @@ impl BattleSide {
let pokemon = RwLock::new(pokemon);
Self {
identifier: Default::default(),
index,
pokemon_per_side,
pokemon,
choices,
fillable_slots,
choices_set: AtomicU8::new(0),
battle: Weak::new(),
has_fled_battle: false,
volatile_scripts: Default::default(),
script_source_data: Default::default(),
data: Arc::new(BattleSideData {
identifier: Default::default(),
index,
pokemon_per_side,
pokemon,
choices,
fillable_slots,
choices_set: AtomicU8::new(0),
battle: WeakBattleReference::default(),
has_fled_battle: AtomicBool::new(false),
volatile_scripts: Default::default(),
script_source_data: Default::default(),
}),
}
}
/// Set the battle this side belongs to.
pub(crate) fn set_battle(&mut self, battle: Weak<Battle>) {
self.battle = battle;
pub(crate) fn set_battle(&self, battle: WeakBattleReference) {
#[allow(clippy::unwrap_used)] // Can only be Some()
unsafe {
(self.data.deref() as *const BattleSideData as *mut BattleSideData)
.as_mut()
.unwrap()
.battle = battle;
}
}
/// The index of the side on the battle.
pub fn index(&self) -> u8 {
self.index
self.data.index
}
/// The number of Pokemon that can be on the side.
pub fn pokemon_per_side(&self) -> u8 {
self.pokemon_per_side
self.data.pokemon_per_side
}
/// A list of pokemon currently on the battlefield.
pub fn pokemon(&self) -> RwLockReadGuard<'_, RawRwLock, Vec<Option<Arc<Pokemon>>>> {
self.pokemon.read()
pub fn pokemon(&self) -> RwLockReadGuard<'_, RawRwLock, Vec<Option<Pokemon>>> {
self.data.pokemon.read()
}
/// The currently set choices for all Pokemon on the battlefield. Cleared when the turn starts.
pub fn choices(&self) -> &RwLock<Vec<Option<TurnChoice>>> {
&self.choices
pub fn choices(&self) -> &RwLock<Vec<Option<Arc<TurnChoice>>>> {
&self.data.choices
}
/// The slots on the side that can still be filled. Once all slots are set to false, this side
/// has lost the battle.
pub fn fillable_slots(&self) -> &Vec<AtomicBool> {
&self.fillable_slots
&self.data.fillable_slots
}
/// The number of choices that are set.
pub fn choices_set(&self) -> u8 {
self.choices_set.load(Ordering::SeqCst)
self.data.choices_set.load(Ordering::SeqCst)
}
/// A reference to the battle we're part of.
pub fn battle(&self) -> Result<Arc<Battle>> {
self.battle
pub fn battle(&self) -> Result<Battle> {
self.data
.battle
.upgrade()
.ok_or(anyhow!("Battle was not set, but requested"))
}
/// Whether or not this side has fled.
pub fn has_fled_battle(&self) -> bool {
self.has_fled_battle
self.data.has_fled_battle.load(Ordering::SeqCst)
}
/// The volatile scripts that are attached to the side.
pub fn volatile_scripts(&self) -> &Arc<ScriptSet> {
&self.volatile_scripts
&self.data.volatile_scripts
}
/// Whether every Pokemon on this side has its choices
pub fn all_choices_set(&self) -> bool {
self.choices_set() == self.pokemon_per_side
self.choices_set() == self.data.pokemon_per_side
}
/// Returns true if there are slots that need to be filled with a new pokemon, that have parties
/// responsible for them. Returns false if all slots are filled with usable pokemon, or slots are
/// empty, but can't be filled by any party anymore.
pub fn all_slots_filled(&self) -> Result<bool> {
for (i, pokemon) in self.pokemon.read().iter().enumerate() {
for (i, pokemon) in self.data.pokemon.read().iter().enumerate() {
match pokemon {
Some(pokemon) => {
if !pokemon.is_usable() && self.battle()?.can_slot_be_filled(self.index, i as u8) {
if !pokemon.is_usable() && self.battle()?.can_slot_be_filled(self.data.index, i as u8) {
return Ok(false);
}
}
None => {
if self.battle()?.can_slot_be_filled(self.index, i as u8) {
if self.battle()?.can_slot_be_filled(self.data.index, i as u8) {
return Ok(false);
}
}
@@ -148,11 +172,11 @@ impl BattleSide {
/// Sets a choice for a Pokemon on this side.
pub(crate) fn set_choice(&self, choice: TurnChoice) -> Result<()> {
for (index, pokemon_slot) in self.pokemon.read().iter().enumerate() {
for (index, pokemon_slot) in self.data.pokemon.read().iter().enumerate() {
if let Some(pokemon) = pokemon_slot {
if std::ptr::eq(pokemon.deref(), choice.user().deref()) {
self.choices.write().get_mut_res(index)?.replace(choice);
self.choices_set.fetch_add(1, Ordering::SeqCst);
if Pokemon::eq(pokemon, choice.user()) {
self.data.choices.write().get_mut_res(index)?.replace(choice.into());
self.data.choices_set.fetch_add(1, Ordering::SeqCst);
return Ok(());
}
}
@@ -162,21 +186,21 @@ impl BattleSide {
/// Resets all choices on this side.
pub fn reset_choices(&self) {
let len = self.choices.read().len();
let len = self.data.choices.read().len();
for i in 0..len {
self.choices.write().get_mut(i).take();
self.data.choices.write().get_mut(i).take();
}
}
/// Forcibly removes a Pokemon from the field.
pub fn force_clear_pokemon(&mut self, index: u8) {
self.pokemon.write().get_mut(index as usize).take();
self.data.pokemon.write().get_mut(index as usize).take();
}
/// Switches out a spot on the field for a different Pokemon.
pub fn set_pokemon(&self, index: u8, pokemon: Option<Arc<Pokemon>>) -> Result<()> {
pub fn set_pokemon(&self, index: u8, pokemon: Option<Pokemon>) -> Result<()> {
{
let mut write_lock = self.pokemon.write();
let mut write_lock = self.data.pokemon.write();
let old = write_lock.get_mut_res(index as usize)?;
let old = match pokemon {
Some(pokemon) => old.replace(pokemon),
@@ -192,33 +216,33 @@ impl BattleSide {
}
let pokemon = {
let read_lock = self.pokemon.read();
let read_lock = self.data.pokemon.read();
&read_lock.get_res(index as usize)?.clone()
};
if let Some(pokemon) = pokemon {
pokemon.set_battle_data(self.battle.clone(), self.index);
pokemon.set_battle_data(self.data.battle.clone(), self.data.index);
pokemon.set_on_battlefield(true)?;
pokemon.set_battle_index(index);
let battle = self.battle()?;
for side in battle.sides() {
if side.index() == self.index {
if side.index() == self.data.index {
continue;
}
for opponent in side.pokemon().iter().flatten() {
opponent.mark_opponent_as_seen(Arc::downgrade(pokemon));
pokemon.mark_opponent_as_seen(Arc::downgrade(opponent));
opponent.mark_opponent_as_seen(pokemon.weak());
pokemon.mark_opponent_as_seen(opponent.weak());
}
}
battle.event_hook().trigger(Event::Switch {
side_index: self.index,
side_index: self.data.index,
index,
pokemon: Some(pokemon.clone()),
});
script_hook!(on_switch_in, pokemon, pokemon);
} else {
self.battle()?.event_hook().trigger(Event::Switch {
side_index: self.index,
side_index: self.data.index,
index,
pokemon: None,
});
@@ -227,9 +251,9 @@ impl BattleSide {
}
/// Checks whether a Pokemon is on the field in this side.
pub fn is_pokemon_on_side(&self, pokemon: Arc<Pokemon>) -> bool {
for p in self.pokemon.read().iter().flatten() {
if Arc::ptr_eq(p, &pokemon) {
pub fn is_pokemon_on_side(&self, pokemon: Pokemon) -> bool {
for p in self.data.pokemon.read().iter().flatten() {
if Pokemon::eq(p, &pokemon) {
return true;
}
}
@@ -239,18 +263,19 @@ impl BattleSide {
/// Marks a slot as unfillable. This happens when no parties are able to fill the slot anymore.
/// If this happens, the slot can not be used again.
pub(crate) fn mark_slot_as_unfillable(&self, index: u8) -> Result<()> {
self.fillable_slots
self.data
.fillable_slots
.get_res(index as usize)?
.store(false, Ordering::SeqCst);
Ok(())
}
/// Checks whether a slot is unfillable or not.
pub fn is_slot_unfillable(&self, pokemon: Arc<Pokemon>) -> Result<bool> {
for (i, slot) in self.pokemon.read().iter().enumerate() {
pub fn is_slot_unfillable(&self, pokemon: Pokemon) -> Result<bool> {
for (i, slot) in self.data.pokemon.read().iter().enumerate() {
if let Some(p) = slot {
if Arc::ptr_eq(p, &pokemon) {
return Ok(self.fillable_slots.get_res(i)?.load(Ordering::Relaxed));
if Pokemon::eq(p, &pokemon) {
return Ok(self.data.fillable_slots.get_res(i)?.load(Ordering::Relaxed));
}
}
}
@@ -259,7 +284,7 @@ impl BattleSide {
/// Checks whether the side has been defeated.
pub fn is_defeated(&self) -> bool {
for fillable_slot in &self.fillable_slots {
for fillable_slot in &self.data.fillable_slots {
if fillable_slot.load(Ordering::Relaxed) {
return false;
}
@@ -269,19 +294,20 @@ impl BattleSide {
/// Mark the side as fled.
pub fn mark_as_fled(&mut self) {
self.has_fled_battle = true;
self.data.has_fled_battle.store(true, Ordering::SeqCst);
}
/// Gets a random Pokemon on the given side.
pub fn get_random_creature_index(&self) -> Result<u8> {
// TODO: Consider adding parameter to only get index for available creatures.
Ok(self.battle()?.random().get_max(self.pokemon_per_side as i32)? as u8)
Ok(self.battle()?.random().get_max(self.data.pokemon_per_side as i32)? as u8)
}
/// Swap two Pokemon on a single side around.
pub fn swap_positions(&mut self, a: u8, b: u8) -> Result<bool> {
let data = &self.data;
// If out of range, don't allow swapping.
if a >= self.pokemon_per_side || b >= self.pokemon_per_side {
if a >= data.pokemon_per_side || b >= data.pokemon_per_side {
return Ok(false);
}
// If the two indices are the same, don't allow swapping.
@@ -294,10 +320,10 @@ impl BattleSide {
let mut party_b = None;
let battle = self.battle()?;
for party in battle.parties() {
if party.is_responsible_for_index(self.index, a) {
if party.is_responsible_for_index(data.index, a) {
party_a = Some(party);
}
if party.is_responsible_for_index(self.index, b) {
if party.is_responsible_for_index(data.index, b) {
party_b = Some(party);
}
}
@@ -315,19 +341,49 @@ impl BattleSide {
return Ok(false);
}
self.pokemon.write().swap(a as usize, b as usize);
data.pokemon.write().swap(a as usize, b as usize);
self.battle()?.event_hook().trigger(Event::Swap {
side_index: self.index,
side_index: data.index,
index_a: a,
index_b: b,
});
Ok(true)
}
/// Gets a weak reference to the side.
pub fn weak(&self) -> WeakBattleSideReference {
WeakBattleSideReference {
data: Arc::downgrade(&self.data),
}
}
}
impl WeakBattleSideReference {
/// Upgrades the weak reference to a strong reference, returning `None` if the side has been
/// dropped.
pub fn upgrade(&self) -> Option<BattleSide> {
self.data.upgrade().map(|data| BattleSide { data })
}
/// Gets the underlying pointer to the data of the side.
pub(crate) fn as_ptr(&self) -> *const c_void {
self.data.as_ptr() as *const c_void
}
}
unsafe impl Send for WeakBattleSideReference {}
unsafe impl Sync for WeakBattleSideReference {}
impl PartialEq for WeakBattleSideReference {
fn eq(&self, other: &Self) -> bool {
self.data.ptr_eq(&other.data)
}
}
impl VolatileScriptsOwner for BattleSide {
fn volatile_scripts(&self) -> &Arc<ScriptSet> {
&self.volatile_scripts
&self.data.volatile_scripts
}
fn load_volatile_script(&self, key: &StringKey) -> Result<Option<Arc<dyn Script>>> {
@@ -343,11 +399,11 @@ impl ScriptSource for BattleSide {
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
&self.script_source_data
&self.data.script_source_data
}
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
scripts.push((&self.volatile_scripts).into());
scripts.push((&self.data.volatile_scripts).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
@@ -358,6 +414,6 @@ impl ScriptSource for BattleSide {
impl ValueIdentifiable for BattleSide {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
self.data.identifier
}
}

View File

@@ -16,7 +16,6 @@ use crate::{PkmnError, ValueIdentifiable, ValueIdentifier};
/// A hit data is the data for a single hit, on a single target.
#[derive(Default, Debug)]
pub struct HitData {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
@@ -96,9 +95,9 @@ pub struct ExecutingMove {
number_of_hits: u8,
/// A list of hits for this move. For multi target multi hit moves, this stores the hits linearly,
/// for example: (target1, hit1), (target1, hit2), (target2, hit1), (target2, hit2), etc.
hits: Vec<HitData>,
hits: Vec<Arc<HitData>>,
/// The user of the move.
user: Arc<Pokemon>,
user: Pokemon,
/// The move the user has actually chosen to do.
chosen_move: Arc<LearnedMove>,
/// The move that the user is actually going to do.
@@ -116,7 +115,7 @@ impl ExecutingMove {
pub fn new(
targets: TargetList,
number_of_hits: u8,
user: Arc<Pokemon>,
user: Pokemon,
chosen_move: Arc<LearnedMove>,
use_move: Arc<dyn MoveData>,
script: ScriptContainer,
@@ -124,7 +123,7 @@ impl ExecutingMove {
let total_hits = number_of_hits as usize * targets.len();
let mut hits = Vec::with_capacity(total_hits);
for _i in 0..total_hits {
hits.push(HitData::default())
hits.push(Arc::new(HitData::default()))
}
Self {
identifier: Default::default(),
@@ -148,7 +147,7 @@ impl ExecutingMove {
self.number_of_hits
}
/// The user of the move.
pub fn user(&self) -> &Arc<Pokemon> {
pub fn user(&self) -> &Pokemon {
&self.user
}
/// The move the user has actually chosen to do.
@@ -165,10 +164,10 @@ impl ExecutingMove {
}
/// Gets a hit data for a target, with a specific index.
pub fn get_hit_data(&self, for_target: &Arc<Pokemon>, hit: u8) -> Result<&HitData> {
pub fn get_hit_data(&self, for_target: &Pokemon, hit: u8) -> Result<&Arc<HitData>> {
for (index, target) in self.targets.iter().enumerate() {
if let Some(target) = target {
if Arc::ptr_eq(target, for_target) {
if Pokemon::eq(target, for_target) {
let i = index * self.number_of_hits as usize + hit as usize;
return match self.hits.get(i) {
Some(hit) => Ok(hit),
@@ -185,9 +184,9 @@ impl ExecutingMove {
}
/// Checks whether a Pokemon is a target for this move.
pub fn is_pokemon_target(&self, pokemon: &Arc<Pokemon>) -> bool {
pub fn is_pokemon_target(&self, pokemon: &Pokemon) -> bool {
for target in self.targets.iter().flatten() {
if Arc::ptr_eq(target, pokemon) {
if Pokemon::eq(target, pokemon) {
return true;
}
}
@@ -195,10 +194,10 @@ impl ExecutingMove {
}
/// Gets the index of the hits in this move where the hits for a specific target start.
pub(crate) fn get_index_of_target(&self, for_target: &Arc<Pokemon>) -> Result<usize> {
pub(crate) fn get_index_of_target(&self, for_target: &Pokemon) -> Result<usize> {
for (index, target) in self.targets.iter().enumerate() {
if let Some(target) = target {
if Arc::ptr_eq(target, for_target) {
if Pokemon::eq(target, for_target) {
let i = index * self.number_of_hits as usize;
return Ok(i);
}

View File

@@ -1,3 +1,4 @@
use std::ffi::c_void;
use std::fmt::{Debug, Formatter};
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU8, Ordering};
@@ -12,7 +13,9 @@ use crate::dynamic_data::event_hooks::Event;
use crate::dynamic_data::models::battle::Battle;
use crate::dynamic_data::models::learned_move::{LearnedMove, MoveLearnMethod};
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
use crate::dynamic_data::{DynamicLibrary, Script, ScriptCategory, ScriptContainer, ScriptSet, VolatileScriptsOwner};
use crate::dynamic_data::{
DynamicLibrary, Script, ScriptCategory, ScriptContainer, ScriptSet, VolatileScriptsOwner, WeakBattleReference,
};
use crate::static_data::AbilityIndex;
use crate::static_data::Form;
use crate::static_data::Gender;
@@ -26,8 +29,8 @@ use crate::utils::Random;
use crate::{script_hook, PkmnError, StringKey, ValueIdentifiable, ValueIdentifier, VecExt};
use anyhow::{anyhow, bail, Result};
/// An individual Pokemon as we know and love them.
pub struct Pokemon {
/// The data of a Pokemon.
struct PokemonData {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The library data of the Pokemon.
@@ -67,16 +70,16 @@ pub struct Pokemon {
height: Atomic<f32>,
/// The stats of the Pokemon when disregarding any stat boosts.
flat_stats: StatisticSet<u32>,
flat_stats: Arc<StatisticSet<u32>>,
/// The statistics boosts of the Pokemon. Will prevent the value from going above 6, and below
/// -6.
stat_boost: ClampedStatisticSet<i8, -6, 6>,
stat_boost: Arc<ClampedStatisticSet<i8, -6, 6>>,
/// The stats of the Pokemon including the stat boosts
boosted_stats: StatisticSet<u32>,
boosted_stats: Arc<StatisticSet<u32>>,
/// The [individual values](https://bulbapedia.bulbagarden.net/wiki/Individual_values) of the Pokemon.
individual_values: ClampedStatisticSet<u8, 0, 31>,
individual_values: Arc<ClampedStatisticSet<u8, 0, 31>>,
/// The [effort values](https://bulbapedia.bulbagarden.net/wiki/Effort_values) of the Pokemon.
effort_values: ClampedStatisticSet<u8, 0, 252>,
effort_values: Arc<ClampedStatisticSet<u8, 0, 252>>,
/// The [nature](https://bulbapedia.bulbagarden.net/wiki/Nature) of the Pokemon.
nature: Arc<dyn Nature>,
@@ -118,6 +121,24 @@ pub struct Pokemon {
script_source_data: RwLock<ScriptSourceData>,
}
/// An individual Pokemon.
#[derive(Clone)]
pub struct Pokemon {
/// The data of the Pokemon.
data: Arc<PokemonData>,
}
/// A non-owning reference to a Pokemon.
#[derive(Debug, Clone)]
pub struct WeakPokemonReference {
/// The weak reference to the data.
data: Weak<PokemonData>,
}
unsafe impl Send for WeakPokemonReference {}
unsafe impl Sync for WeakPokemonReference {}
impl Pokemon {
/// Instantiates a new Pokemon.
pub fn new(
@@ -143,7 +164,7 @@ impl Pokemon {
.natures()
.get_nature(nature)
.ok_or(PkmnError::InvalidNatureName { nature: nature.clone() })?;
let mut pokemon = Self {
let pokemon_data = PokemonData {
identifier: Default::default(),
library,
species: RwLock::new(species),
@@ -180,28 +201,32 @@ impl Pokemon {
volatile: Default::default(),
script_source_data: Default::default(),
};
let pokemon = Self {
data: Arc::new(pokemon_data),
};
pokemon.recalculate_flat_stats()?;
let health = pokemon.flat_stats().hp();
pokemon.current_health = AtomicU32::new(health);
pokemon.data.current_health.store(health, Ordering::Relaxed);
Ok(pokemon)
}
/// The library data of the Pokemon.
pub fn library(&self) -> &Arc<dyn DynamicLibrary> {
&self.library
&self.data.library
}
/// The species of the Pokemon.
pub fn species(&self) -> Arc<dyn Species> {
self.species.read().clone()
self.data.species.read().clone()
}
/// The form of the Pokemon.
pub fn form(&self) -> Arc<dyn Form> {
self.form.read().clone()
self.data.form.read().clone()
}
/// The species that should be displayed to the user. This handles stuff like the Illusion ability.
pub fn display_species(&self) -> Arc<dyn Species> {
if let Some(v) = &self.display_species {
if let Some(v) = &self.data.display_species {
v.clone()
} else {
self.species()
@@ -209,7 +234,7 @@ impl Pokemon {
}
/// The form that should be displayed to the user. This handles stuff like the Illusion ability.
pub fn display_form(&self) -> Arc<dyn Form> {
if let Some(v) = &self.display_form {
if let Some(v) = &self.data.display_form {
v.clone()
} else {
self.form()
@@ -217,53 +242,57 @@ impl Pokemon {
}
/// The current level of the Pokemon.
pub fn level(&self) -> LevelInt {
self.level.load(Ordering::Relaxed)
self.data.level.load(Ordering::Relaxed)
}
/// The amount of experience of the Pokemon.
pub fn experience(&self) -> u32 {
self.experience.load(Ordering::Relaxed)
self.data.experience.load(Ordering::Relaxed)
}
/// A unique random number for this Pokemon.
pub fn unique_identifier(&self) -> u32 {
self.unique_identifier
self.data.unique_identifier
}
/// The gender of the Pokemon.
pub fn gender(&self) -> Gender {
*self.gender.read()
*self.data.gender.read()
}
/// The coloring of the Pokemon. Value 0 is the default, value 1 means shiny. Other values are
/// currently not used, and can be used for other implementations.
pub fn coloring(&self) -> u8 {
self.coloring
self.data.coloring
}
/// Gets the held item of a Pokemon
pub fn held_item(&self) -> &RwLock<Option<Arc<dyn Item>>> {
&self.held_item
&self.data.held_item
}
/// Checks whether the Pokemon is holding a specific 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.read().deref() {
if let Some(v) = self.data.held_item.read().deref() {
return v.name() == name;
}
false
}
/// Changes the held item of the Pokemon. Returns the previously held item.
pub fn set_held_item(&self, item: &Arc<dyn Item>) -> Option<Arc<dyn Item>> {
self.held_item.write().replace(item.clone())
self.data.held_item.write().replace(item.clone())
}
/// Removes the held item from the Pokemon. Returns the previously held item.
pub fn remove_held_item(&self) -> Option<Arc<dyn Item>> {
self.held_item.write().take()
self.data.held_item.write().take()
}
/// Makes the Pokemon uses its held item.
pub fn consume_held_item(&self) -> Result<bool> {
if self.held_item.read().is_none() {
if self.data.held_item.read().is_none() {
return Ok(false);
}
let script = self
.library
.load_item_script(self.held_item.read().as_ref().ok_or(PkmnError::UnableToAcquireLock)?)?;
let script = self.data.library.load_item_script(
self.data
.held_item
.read()
.as_ref()
.ok_or(PkmnError::UnableToAcquireLock)?,
)?;
if script.is_none() {
return Ok(false);
}
@@ -274,60 +303,60 @@ impl Pokemon {
/// The remaining health points of the Pokemon.
pub fn current_health(&self) -> u32 {
self.current_health.load(Ordering::Relaxed)
self.data.current_health.load(Ordering::Relaxed)
}
/// The max health points of the Pokemon.
pub fn max_health(&self) -> u32 {
self.boosted_stats.hp()
self.data.boosted_stats.hp()
}
/// The weight of the Pokemon in kilograms.
pub fn weight(&self) -> f32 {
self.weight.load(Ordering::Relaxed)
self.data.weight.load(Ordering::Relaxed)
}
/// Sets the weight of the Pokemon in kilograms.
pub fn set_weight(&self, weight: f32) {
self.weight.store(weight, Ordering::Relaxed)
self.data.weight.store(weight, Ordering::Relaxed)
}
/// The height of the Pokemon in meters.
pub fn height(&self) -> f32 {
self.height.load(Ordering::Relaxed)
self.data.height.load(Ordering::Relaxed)
}
/// An optional nickname of the Pokemon.
pub fn nickname(&self) -> &Option<String> {
&self.nickname
&self.data.nickname
}
/// An index of the ability to find the actual ability on the form.
pub fn real_ability(&self) -> &AbilityIndex {
&self.ability_index
&self.data.ability_index
}
/// The current types of the Pokemon.
pub fn types(&self) -> RwLockReadGuard<'_, RawRwLock, Vec<TypeIdentifier>> {
self.types.read()
self.data.types.read()
}
/// The moves the Pokemon has learned. This is of a set length of [`MAX_MOVES`]. Empty move slots
/// are defined by None.
pub fn learned_moves(&self) -> &RwLock<[Option<Arc<LearnedMove>>; MAX_MOVES]> {
&self.moves
&self.data.moves
}
/// The stats of the Pokemon when disregarding any stat boosts.
pub fn flat_stats(&self) -> &StatisticSet<u32> {
&self.flat_stats
pub fn flat_stats(&self) -> &Arc<StatisticSet<u32>> {
&self.data.flat_stats
}
/// The amount of boosts on a specific stat.
pub fn stat_boosts(&self) -> &ClampedStatisticSet<i8, -6, 6> {
&self.stat_boost
pub fn stat_boosts(&self) -> &Arc<ClampedStatisticSet<i8, -6, 6>> {
&self.data.stat_boost
}
/// The stats of the Pokemon including the stat boosts
pub fn boosted_stats(&self) -> &StatisticSet<u32> {
&self.boosted_stats
pub fn boosted_stats(&self) -> &Arc<StatisticSet<u32>> {
&self.data.boosted_stats
}
/// Get the stat boosts for a specific stat.
pub fn stat_boost(&self, stat: Statistic) -> i8 {
self.stat_boost.get_stat(stat)
self.data.stat_boost.get_stat(stat)
}
/// Change a boosted stat by a certain amount.
pub fn change_stat_boost(&self, stat: Statistic, mut diff_amount: i8, self_inflicted: bool) -> Result<bool> {
@@ -357,13 +386,13 @@ impl Pokemon {
}
let mut changed = false;
let old_value = self.stat_boost.get_stat(stat);
let old_value = self.data.stat_boost.get_stat(stat);
match diff_amount.cmp(&0_i8) {
std::cmp::Ordering::Less => {
changed = self.stat_boost.decrease_stat(stat, -diff_amount);
changed = self.data.stat_boost.decrease_stat(stat, -diff_amount);
}
std::cmp::Ordering::Greater => {
changed = self.stat_boost.increase_stat(stat, -diff_amount);
changed = self.data.stat_boost.increase_stat(stat, -diff_amount);
}
_ => {}
}
@@ -383,17 +412,17 @@ impl Pokemon {
}
/// The [individual values](https://bulbapedia.bulbagarden.net/wiki/Individual_values) of the Pokemon.
pub fn individual_values(&self) -> &ClampedStatisticSet<u8, 0, 31> {
&self.individual_values
pub fn individual_values(&self) -> &Arc<ClampedStatisticSet<u8, 0, 31>> {
&self.data.individual_values
}
/// The [effort values](https://bulbapedia.bulbagarden.net/wiki/Effort_values) of the Pokemon.
pub fn effort_values(&self) -> &ClampedStatisticSet<u8, 0, 252> {
&self.effort_values
pub fn effort_values(&self) -> &Arc<ClampedStatisticSet<u8, 0, 252>> {
&self.data.effort_values
}
/// Gets the battle the battle is currently in.
pub fn get_battle(&self) -> Option<Arc<Battle>> {
let r = self.battle_data.read();
pub fn get_battle(&self) -> Option<Battle> {
let r = self.data.battle_data.read();
if let Some(data) = &r.deref() {
data.battle.upgrade()
} else {
@@ -403,26 +432,31 @@ impl Pokemon {
/// Get the index of the side of the battle the Pokemon is in. Only returns a value if the Pokemon
/// is on the battlefield.
pub fn get_battle_side_index(&self) -> Option<u8> {
self.battle_data.read().as_ref().map(|data| data.battle_side_index())
self.data
.battle_data
.read()
.as_ref()
.map(|data| data.battle_side_index())
}
/// Get the index of the slot on the side of the battle the Pokemon is in. Only returns a value
/// if the Pokemon is on the battlefield.
pub fn get_battle_index(&self) -> Option<u8> {
self.battle_data.read().as_ref().map(|data| data.index())
self.data.battle_data.read().as_ref().map(|data| data.index())
}
/// Returns whether something overrides the ability.
pub fn is_ability_overriden(&self) -> bool {
self.override_ability.is_some()
self.data.override_ability.is_some()
}
/// Returns the currently active ability.
pub fn active_ability(&self) -> Result<Arc<dyn Ability>> {
if let Some(v) = &self.override_ability {
if let Some(v) = &self.data.override_ability {
return Ok(v.clone());
}
let form = self.form();
let ability = form.get_ability(self.ability_index)?;
let ability = form.get_ability(self.data.ability_index)?;
Ok(self
.data
.library
.static_data()
.abilities()
@@ -434,68 +468,70 @@ impl Pokemon {
/// The script for the status.
pub fn status(&self) -> &ScriptContainer {
&self.status_script
&self.data.status_script
}
/// Returns the script for the currently active ability.
pub fn ability_script(&self) -> &ScriptContainer {
&self.ability_script
&self.data.ability_script
}
/// Whether or not the Pokemon is allowed to gain experience.
pub fn allowed_experience_gain(&self) -> bool {
self.allowed_experience
self.data.allowed_experience
}
/// The [nature](https://bulbapedia.bulbagarden.net/wiki/Nature) of the Pokemon.
pub fn nature(&self) -> &Arc<dyn Nature> {
&self.nature
&self.data.nature
}
/// Calculates the flat stats on the Pokemon. This should be called when for example the base
/// stats, level, nature, IV, or EV changes. This has a side effect of recalculating the boosted
/// stats, as those depend on the flat stats.
pub fn recalculate_flat_stats(&self) -> Result<()> {
self.library
self.data
.library
.stat_calculator()
.calculate_flat_stats(self, &self.flat_stats)?;
.calculate_flat_stats(self, &self.data.flat_stats)?;
self.recalculate_boosted_stats()?;
Ok(())
}
/// Calculates the boosted stats on the Pokemon, _without_ recalculating the flat stats.
/// This should be called when a stat boost changes.
pub fn recalculate_boosted_stats(&self) -> Result<()> {
self.library
self.data
.library
.stat_calculator()
.calculate_boosted_stats(self, &self.boosted_stats)
.calculate_boosted_stats(self, &self.data.boosted_stats)
}
/// Change the species of the Pokemon.
pub fn change_species(&self, species: Arc<dyn Species>, form: Arc<dyn Form>) -> Result<()> {
*self.species.write() = species.clone();
*self.form.write() = form.clone();
*self.data.species.write() = species.clone();
*self.data.form.write() = form.clone();
// If the pokemon is genderless, but it's new species is not, we want to set its gender
if self.gender() != Gender::Genderless && species.gender_rate() < 0.0 {
// If we're in battle, use the battle random for predictability
let r = self.battle_data.read();
let r = self.data.battle_data.read();
if let Some(data) = r.deref() {
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()),
};
*self.gender.write() = species.get_random_gender(random.deref_mut());
*self.data.gender.write() = species.get_random_gender(random.deref_mut());
} else {
// If we're not in battle, just use a new random.
*self.gender.write() = species.get_random_gender(&mut Random::default());
*self.data.gender.write() = species.get_random_gender(&mut Random::default());
}
}
// Else if the new species is genderless, but the pokemon has a gender, make the creature genderless.
else if species.gender_rate() < 0.0 && self.gender() != Gender::Genderless {
*self.gender.write() = Gender::Genderless;
*self.data.gender.write() = Gender::Genderless;
}
let r = self.battle_data.read();
let r = self.data.battle_data.read();
if let Some(battle_data) = &r.deref() {
if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::SpeciesChange {
@@ -513,29 +549,31 @@ impl Pokemon {
if self.form().value_identifier() == form.value_identifier() {
return Ok(());
}
*self.form.write() = form.clone();
*self.data.form.write() = form.clone();
{
let mut type_lock = self.types.write();
let mut type_lock = self.data.types.write();
type_lock.clear();
for t in form.types() {
type_lock.push(*t);
}
}
self.weight.store(form.weight(), Ordering::SeqCst);
self.height.store(form.height(), Ordering::SeqCst);
self.data.weight.store(form.weight(), Ordering::SeqCst);
self.data.height.store(form.height(), Ordering::SeqCst);
let ability = self.active_ability()?;
let ability_script = self
.data
.library
.load_script(self.into(), ScriptCategory::Ability, ability.name())?;
if let Some(ability_script) = ability_script {
let script_result = self
.data
.ability_script
.set(ability_script)
.as_ref()
// Ensure the ability script gets initialized with the parameters for the ability.
.on_initialize(&self.library, ability.parameters().to_vec());
.on_initialize(&self.data.library, ability.parameters().to_vec());
match script_result {
Ok(_) => (),
Err(e) => {
@@ -543,21 +581,23 @@ impl Pokemon {
}
}
} else {
self.ability_script.clear();
self.data.ability_script.clear();
}
let old_health = self.max_health();
self.recalculate_flat_stats()?;
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);
self.data.current_health.store(0, Ordering::SeqCst);
} else if diff_health < 0 {
self.current_health.fetch_sub(-diff_health as u32, Ordering::SeqCst);
self.data
.current_health
.fetch_sub(-diff_health as u32, Ordering::SeqCst);
} else {
self.current_health.fetch_add(diff_health as u32, Ordering::SeqCst);
self.data.current_health.fetch_add(diff_health as u32, Ordering::SeqCst);
}
// TODO: consider form specific attacks?
let r = self.battle_data.read();
let r = self.data.battle_data.read();
if let Some(battle_data) = r.deref() {
if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::FormChange {
@@ -571,7 +611,7 @@ impl Pokemon {
/// Whether or not the Pokemon is useable in a battle.
pub fn is_usable(&self) -> bool {
!self.is_caught && !self.is_egg && !self.is_fainted()
!self.data.is_caught && !self.data.is_egg && !self.is_fainted()
}
/// Returns whether the Pokemon is fainted.
@@ -580,8 +620,8 @@ impl Pokemon {
}
/// Sets the current battle the Pokemon is in.
pub fn set_battle_data(&self, battle: Weak<Battle>, battle_side_index: u8) {
let mut w = self.battle_data.write();
pub fn set_battle_data(&self, battle: WeakBattleReference, battle_side_index: u8) {
let mut w = self.data.battle_data.write();
if let Some(battle_data) = w.deref_mut() {
battle_data.battle = battle;
battle_data.battle_side_index.store(battle_side_index, Ordering::SeqCst);
@@ -598,13 +638,13 @@ impl Pokemon {
/// Sets whether or not the Pokemon is on the battlefield.
pub fn set_on_battlefield(&self, value: bool) -> Result<()> {
let r = self.battle_data.read();
let r = self.data.battle_data.read();
if let Some(data) = &mut r.deref() {
data.on_battle_field.store(value, Ordering::SeqCst);
if !value {
self.volatile.clear()?;
self.weight.store(self.form().weight(), Ordering::SeqCst);
self.height.store(self.form().height(), Ordering::SeqCst);
self.data.volatile.clear()?;
self.data.weight.store(self.form().weight(), Ordering::SeqCst);
self.data.height.store(self.form().height(), Ordering::SeqCst);
}
}
Ok(())
@@ -612,7 +652,7 @@ impl Pokemon {
/// Sets the index of the slot of the side the Pokemon is on.
pub fn set_battle_index(&self, index: u8) {
let r = self.battle_data.read();
let r = self.data.battle_data.read();
if let Some(data) = r.deref() {
data.index.store(index, Ordering::SeqCst)
}
@@ -620,16 +660,20 @@ impl Pokemon {
/// Whether or not the Pokemon is on the battlefield.
pub fn is_on_battlefield(&self) -> bool {
self.battle_data.read().as_ref().is_some_and(|a| a.on_battle_field())
self.data
.battle_data
.read()
.as_ref()
.is_some_and(|a| a.on_battle_field())
}
/// Marks an opponent as seen, for use in experience gain.
pub fn mark_opponent_as_seen(&self, pokemon: Weak<Pokemon>) {
let r = self.battle_data.read();
if let Some(battle_data) = &r.deref() {
pub fn mark_opponent_as_seen(&self, pokemon: WeakPokemonReference) {
let r = self.data.battle_data.read();
if let Some(battle_data) = r.deref() {
let mut opponents = battle_data.seen_opponents().write();
for seen_opponent in opponents.deref() {
if seen_opponent.ptr_eq(&pokemon) {
if seen_opponent.eq(&pokemon) {
return;
}
}
@@ -646,7 +690,7 @@ impl Pokemon {
return Ok(());
}
let new_health = self.current_health() - damage;
if let Some(battle_data) = &self.battle_data.read().deref() {
if let Some(battle_data) = &self.data.battle_data.read().deref() {
if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::Damage {
pokemon: self,
@@ -656,11 +700,17 @@ impl Pokemon {
});
}
}
if self.battle_data.read().as_ref().is_some_and(|a| a.on_battle_field()) {
if self
.data
.battle_data
.read()
.as_ref()
.is_some_and(|a| a.on_battle_field())
{
script_hook!(on_damage, self, self, source, self.current_health(), new_health);
}
self.current_health.store(new_health, Ordering::SeqCst);
self.data.current_health.store(new_health, Ordering::SeqCst);
if self.is_fainted() && damage > 0 {
self.on_faint(source)?;
}
@@ -669,7 +719,7 @@ impl Pokemon {
/// Triggers when the Pokemon faints.
fn on_faint(&self, source: DamageSource) -> Result<()> {
let r = self.battle_data.read();
let r = self.data.battle_data.read();
if let Some(battle_data) = r.deref() {
if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::Faint { pokemon: self });
@@ -703,7 +753,7 @@ impl Pokemon {
return false;
}
let new_health = self.current_health() + max_amount;
if let Some(battle_data) = &self.battle_data.read().deref() {
if let Some(battle_data) = &self.data.battle_data.read().deref() {
if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::Heal {
pokemon: self,
@@ -712,7 +762,7 @@ impl Pokemon {
});
}
}
self.current_health.store(new_health, Ordering::SeqCst);
self.data.current_health.store(new_health, Ordering::SeqCst);
true
}
@@ -727,6 +777,7 @@ impl Pokemon {
}
};
let move_data = self
.data
.library
.static_data()
.moves()
@@ -742,12 +793,13 @@ impl Pokemon {
/// Removes the current non-volatile status from the Pokemon.
pub fn clear_status(&self) {
self.status_script.clear()
self.data.status_script.clear()
}
/// Increases the level by a certain amount
pub fn change_level_by(&self, amount: LevelInt) -> Result<()> {
self.level
self.data
.level
.fetch_update(Ordering::SeqCst, Ordering::Relaxed, |x| {
let max_level = self.library().static_data().settings().maximum_level();
if x + amount > max_level {
@@ -759,13 +811,50 @@ impl Pokemon {
.ok();
self.recalculate_flat_stats()
}
/// Take a weak reference to the Pokemon.
pub fn weak(&self) -> WeakPokemonReference {
WeakPokemonReference {
data: Arc::downgrade(&self.data),
}
}
}
impl PartialEq for Pokemon {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.data, &other.data)
}
}
impl Eq for Pokemon {}
impl PartialEq for WeakPokemonReference {
fn eq(&self, other: &Self) -> bool {
Weak::ptr_eq(&self.data, &other.data)
}
}
impl Eq for WeakPokemonReference {}
impl WeakPokemonReference {
/// Attempts to upgrade the weak reference to a strong reference.
pub fn upgrade(&self) -> Option<Pokemon> {
Some(Pokemon {
data: self.data.upgrade()?,
})
}
/// Gets the pointer to the underlying data.
pub(crate) fn as_ptr(&self) -> *const c_void {
self.data.as_ptr() as *const c_void
}
}
/// The data of the Pokemon related to being in a battle.
#[derive(Debug)]
pub struct PokemonBattleData {
/// The battle data of the Pokemon
battle: Weak<Battle>,
battle: WeakBattleReference,
/// The index of the side of the Pokemon
battle_side_index: AtomicU8,
/// The index of the slot on the side of the Pokemon.
@@ -773,16 +862,12 @@ pub struct PokemonBattleData {
/// Whether or not the Pokemon is on the battlefield.
on_battle_field: AtomicBool,
/// A list of opponents the Pokemon has seen this battle.
seen_opponents: RwLock<Vec<Weak<Pokemon>>>,
seen_opponents: RwLock<Vec<WeakPokemonReference>>,
}
impl PokemonBattleData {
/// The battle data of the Pokemon
pub fn battle_mut(&mut self) -> Option<Arc<Battle>> {
self.battle.upgrade()
}
/// The battle data of the Pokemon
pub fn battle(&self) -> Option<Arc<Battle>> {
pub fn battle(&self) -> Option<Battle> {
self.battle.upgrade()
}
@@ -799,7 +884,7 @@ impl PokemonBattleData {
self.on_battle_field.load(Ordering::Relaxed)
}
/// A list of opponents the Pokemon has seen this battle.
pub fn seen_opponents(&self) -> &RwLock<Vec<Weak<Pokemon>>> {
pub fn seen_opponents(&self) -> &RwLock<Vec<WeakPokemonReference>> {
&self.seen_opponents
}
}
@@ -807,7 +892,7 @@ impl PokemonBattleData {
impl ScriptSource for Pokemon {
fn get_script_count(&self) -> Result<usize> {
let mut c = 3;
if let Some(battle_data) = &self.battle_data.read().deref() {
if let Some(battle_data) = &self.data.battle_data.read().deref() {
if let Some(battle) = battle_data.battle() {
c += battle
.sides()
@@ -819,19 +904,19 @@ impl ScriptSource for Pokemon {
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
&self.script_source_data
&self.data.script_source_data
}
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
scripts.push((&self.held_item_trigger_script).into());
scripts.push((&self.ability_script).into());
scripts.push((&self.status_script).into());
scripts.push((&self.volatile).into());
scripts.push((&self.data.held_item_trigger_script).into());
scripts.push((&self.data.ability_script).into());
scripts.push((&self.data.status_script).into());
scripts.push((&self.data.volatile).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
self.get_own_scripts(scripts);
if let Some(battle_data) = &self.battle_data.read().deref() {
if let Some(battle_data) = &self.data.battle_data.read().deref() {
if let Some(battle) = battle_data.battle() {
battle
.sides()
@@ -845,17 +930,17 @@ impl ScriptSource for Pokemon {
impl VolatileScriptsOwner for Pokemon {
fn volatile_scripts(&self) -> &Arc<ScriptSet> {
&self.volatile
&self.data.volatile
}
fn load_volatile_script(&self, key: &StringKey) -> Result<Option<Arc<dyn Script>>> {
self.library.load_script(self.into(), ScriptCategory::Pokemon, key)
self.data.library.load_script(self.into(), ScriptCategory::Pokemon, key)
}
}
impl ValueIdentifiable for Pokemon {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
self.data.identifier
}
}
@@ -921,7 +1006,7 @@ pub mod test {
});
let mut static_lib = MockStaticData::new();
static_lib.expect_species().return_const(Box::new(species_lib));
static_lib.expect_species().return_const(Arc::new(species_lib));
let mut growth_rate_lib = MockGrowthRateLibrary::new();
growth_rate_lib
@@ -934,8 +1019,8 @@ pub mod test {
Some(Arc::new(n))
});
static_lib.expect_growth_rates().return_const(Box::new(growth_rate_lib));
static_lib.expect_natures().return_const(Box::new(nature_lib));
static_lib.expect_growth_rates().return_const(Arc::new(growth_rate_lib));
static_lib.expect_natures().return_const(Arc::new(nature_lib));
let mut stat_calculator = MockBattleStatCalculator::new();
stat_calculator.expect_calculate_flat_stats().returning(|_, _| Ok(()));
@@ -944,8 +1029,8 @@ pub mod test {
.returning(|_, _| Ok(()));
let mut lib = MockDynamicLibrary::new();
lib.expect_static_data().return_const(Box::new(static_lib));
lib.expect_stat_calculator().return_const(Box::new(stat_calculator));
lib.expect_static_data().return_const(Arc::new(static_lib));
lib.expect_stat_calculator().return_const(Arc::new(stat_calculator));
Arc::new(lib)
}

View File

@@ -1,7 +1,6 @@
use anyhow::Result;
use parking_lot::lock_api::RwLockReadGuard;
use parking_lot::{RawRwLock, RwLock};
use std::sync::Arc;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::{ValueIdentifiable, ValueIdentifier, VecExt};
@@ -12,7 +11,7 @@ pub struct PokemonParty {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The underlying list of Pokemon.
pokemon: RwLock<Vec<Option<Arc<Pokemon>>>>,
pokemon: RwLock<Vec<Option<Pokemon>>>,
}
impl PokemonParty {
@@ -29,7 +28,7 @@ impl PokemonParty {
}
/// Instantiates a party with a list.
pub fn new_from_vec(pokemon: Vec<Option<Arc<Pokemon>>>) -> Self {
pub fn new_from_vec(pokemon: Vec<Option<Pokemon>>) -> Self {
Self {
identifier: Default::default(),
pokemon: RwLock::new(pokemon),
@@ -37,7 +36,7 @@ impl PokemonParty {
}
/// Gets a Pokemon at an index in the party.
pub fn at(&self, index: usize) -> Option<Arc<Pokemon>> {
pub fn at(&self, index: usize) -> Option<Pokemon> {
let read_lock = self.pokemon.read();
let opt = read_lock.get(index);
if let Some(v) = opt {
@@ -53,7 +52,7 @@ impl PokemonParty {
}
/// Sets the Pokemon at an index to a Pokemon, returning the old Pokemon.
pub fn swap_into(&self, index: usize, pokemon: Option<Arc<Pokemon>>) -> Result<Option<Arc<Pokemon>>> {
pub fn swap_into(&self, index: usize, pokemon: Option<Pokemon>) -> Result<Option<Pokemon>> {
let mut party = self.pokemon.write();
if index >= party.len() {
return Ok(pokemon);
@@ -82,7 +81,7 @@ impl PokemonParty {
}
/// Gets the underlying list of Pokemon.
pub fn pokemon(&self) -> RwLockReadGuard<'_, RawRwLock, Vec<Option<Arc<Pokemon>>>> {
pub fn pokemon(&self) -> RwLockReadGuard<'_, RawRwLock, Vec<Option<Pokemon>>> {
self.pokemon.read()
}
@@ -112,7 +111,7 @@ impl PokemonParty {
/// Checks if the party contains a given pokemon.
pub fn has_pokemon(&self, pokemon: &Pokemon) -> bool {
for p in self.pokemon.read().iter().flatten() {
if std::ptr::eq(p.as_ref(), pokemon) {
if Pokemon::eq(p, pokemon) {
return true;
}
}

View File

@@ -236,7 +236,7 @@ impl ScriptIterator {
}
} else if let ScriptWrapper::Script(script) = wrapper {
if let Some(v) = script.upgrade() {
if let Some(..) = v.read().as_ref() {
if v.read().as_ref().is_some() {
return Ok(true);
}
}

View File

@@ -2,17 +2,17 @@ use anyhow::{anyhow, Result};
use std::any::Any;
use std::fmt::{Debug, Formatter};
use std::ops::Deref;
use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicUsize, Ordering};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread::JoinHandle;
use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard};
use crate::dynamic_data::choices::TurnChoice;
use crate::dynamic_data::ExecutingMove;
use crate::dynamic_data::Pokemon;
use crate::dynamic_data::{Battle, DynamicLibrary};
use crate::dynamic_data::{BattleSide, DamageSource};
use crate::dynamic_data::{ExecutingMove, WeakBattleReference, WeakPokemonReference};
use crate::dynamic_data::{Pokemon, WeakBattleSideReference};
use crate::static_data::{EffectParameter, TypeIdentifier};
use crate::static_data::{Item, Statistic};
use crate::StringKey;
@@ -69,77 +69,77 @@ pub trait Script: Send + Sync {
Ok(())
}
/// This function is ran when this script starts being in effect.
fn on_initialize(&self, _library: &Arc<dyn DynamicLibrary>, _pars: Vec<EffectParameter>) -> Result<()> {
fn on_initialize(&self, _library: &Arc<dyn DynamicLibrary>, _pars: Vec<Arc<EffectParameter>>) -> Result<()> {
Ok(())
}
/// This function is ran just before the start of the turn. Everyone has made its choices here,
/// and the turn is about to start. This is a great place to initialize data if you need to know
/// something has happened during a turn.
fn on_before_turn(&self, _choice: &TurnChoice) -> Result<()> {
fn on_before_turn(&self, _choice: &Arc<TurnChoice>) -> Result<()> {
Ok(())
}
/// This function allows you to modify the effective speed of the Pokemon. This is ran before
/// turn ordering, so overriding here will allow you to put certain Pokemon before others.
fn change_speed(&self, _choice: &TurnChoice, _speed: &mut u32) -> Result<()> {
fn change_speed(&self, _choice: &Arc<TurnChoice>, _speed: &mut u32) -> Result<()> {
Ok(())
}
/// This function allows you to modify the effective priority of the Pokemon. This is ran before
/// turn ordering, so overriding here will allow you to put certain Pokemon before others. Note
/// that this is only relevant on move choices, as other turn choice types do not have a priority.
fn change_priority(&self, _choice: &TurnChoice, _priority: &mut i8) -> Result<()> {
fn change_priority(&self, _choice: &Arc<TurnChoice>, _priority: &mut i8) -> Result<()> {
Ok(())
}
/// This function allows you to change the move that is used during execution. This is useful for
/// moves such as metronome, where the move chosen actually differs from the move used.
fn change_move(&self, _choice: &TurnChoice, _move_name: &mut StringKey) -> Result<()> {
fn change_move(&self, _choice: &Arc<TurnChoice>, _move_name: &mut StringKey) -> Result<()> {
Ok(())
}
/// This function allows you to change a move into a multi-hit move. The number of hits set here
/// gets used as the number of hits. If set to 0, this will behave as if the move missed on its
/// first hit.
fn change_number_of_hits(&self, _choice: &TurnChoice, _number_of_hits: &mut u8) -> Result<()> {
fn change_number_of_hits(&self, _choice: &Arc<TurnChoice>, _number_of_hits: &mut u8) -> Result<()> {
Ok(())
}
/// This function allows you to prevent a move from running. If this gets set to true, the move
/// ends execution here. No PP will be decreased in this case.
fn prevent_move(&self, _move: &ExecutingMove, _prevent: &mut bool) -> Result<()> {
fn prevent_move(&self, _move: &Arc<ExecutingMove>, _prevent: &mut bool) -> Result<()> {
Ok(())
}
/// This function makes the move fail. If the fail field gets set to true, the move ends execution,
/// and fail events get triggered.
fn fail_move(&self, _move: &ExecutingMove, _fail: &mut bool) -> Result<()> {
fn fail_move(&self, _move: &Arc<ExecutingMove>, _fail: &mut bool) -> Result<()> {
Ok(())
}
/// Similar to [`Self::prevent_move`]. This function will also stop execution, but PP will be
/// decreased.
fn stop_before_move(&self, _move: &ExecutingMove, _stop: &mut bool) -> Result<()> {
fn stop_before_move(&self, _move: &Arc<ExecutingMove>, _stop: &mut bool) -> Result<()> {
Ok(())
}
/// This function runs just before the move starts its execution.
fn on_before_move(&self, _move: &ExecutingMove) -> Result<()> {
fn on_before_move(&self, _move: &Arc<ExecutingMove>) -> Result<()> {
Ok(())
}
/// This function allows a script to prevent a move that is targeted at its owner. If set to true
/// the move fails, and fail events get triggered.
fn fail_incoming_move(&self, _move: &ExecutingMove, _target: &Arc<Pokemon>, _fail: &mut bool) -> Result<()> {
fn fail_incoming_move(&self, _move: &Arc<ExecutingMove>, _target: &Pokemon, _fail: &mut bool) -> Result<()> {
Ok(())
}
/// This function allows a script to make its owner invulnerable to an incoming move.
fn is_invulnerable(&self, _move: &ExecutingMove, _target: &Arc<Pokemon>, _invulnerable: &mut bool) -> Result<()> {
fn is_invulnerable(&self, _move: &Arc<ExecutingMove>, _target: &Pokemon, _invulnerable: &mut bool) -> Result<()> {
Ok(())
}
/// This function occurs when a move gets missed. This runs on the scripts belonging to the executing
/// move, which include the scripts that are attached to the owner of the script.
fn on_move_miss(&self, _move: &ExecutingMove, _target: &Arc<Pokemon>) -> Result<()> {
fn on_move_miss(&self, _move: &Arc<ExecutingMove>, _target: &Pokemon) -> Result<()> {
Ok(())
}
/// This function allows the script to change the actual type that is used for the move on a target.
fn change_move_type(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_move_type: &mut TypeIdentifier,
) -> Result<()> {
@@ -148,8 +148,8 @@ pub trait Script: Send + Sync {
/// This function allows the script to change how effective a move is on a target.
fn change_effectiveness(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_effectiveness: &mut f32,
) -> Result<()> {
@@ -158,8 +158,8 @@ pub trait Script: Send + Sync {
/// This function allows a script to block an outgoing move from being critical.
fn block_critical(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_block_critical: &mut bool,
) -> Result<()> {
@@ -168,8 +168,8 @@ pub trait Script: Send + Sync {
/// This function allows a script to block an incoming move from being critical.
fn block_incoming_critical(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_block_critical: &mut bool,
) -> Result<()> {
@@ -179,8 +179,8 @@ pub trait Script: Send + Sync {
/// the percentage accuracy, so anything above 100% will make it always hit.
fn change_accuracy(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_accuracy: &mut u8,
) -> Result<()> {
@@ -190,8 +190,8 @@ pub trait Script: Send + Sync {
/// This function allows a script to change the critical stage of the move used.
fn change_critical_stage(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_stage: &mut u8,
) -> Result<()> {
@@ -201,8 +201,8 @@ pub trait Script: Send + Sync {
/// run when a hit is critical.
fn change_critical_modifier(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_modifier: &mut f32,
) -> Result<()> {
@@ -212,8 +212,8 @@ pub trait Script: Send + Sync {
/// occurs when the user has the move type as one of its own types.
fn change_stab_modifier(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_modifier: &mut f32,
) -> Result<()> {
@@ -223,8 +223,8 @@ pub trait Script: Send + Sync {
/// This function allows a script to change the effective base power of a move hit.
fn change_base_power(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_base_power: &mut u8,
) -> Result<()> {
@@ -233,8 +233,8 @@ pub trait Script: Send + Sync {
/// This function allows a script to bypass defensive stat boosts for a move hit.
fn bypass_defensive_stat_boost(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_bypass: &mut bool,
) -> Result<()> {
@@ -243,8 +243,8 @@ pub trait Script: Send + Sync {
/// This function allows a script to bypass offensive stat boosts for a move hit.
fn bypass_offensive_stat_boost(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_bypass: &mut bool,
) -> Result<()> {
@@ -253,8 +253,8 @@ pub trait Script: Send + Sync {
/// This function allows a script to change the actual offensive stat values used when calculating damage
fn change_offensive_stat_value(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_amount: &mut u32,
) -> Result<()> {
@@ -263,8 +263,8 @@ pub trait Script: Send + Sync {
/// This function allows a script to change the actual defensive stat values used when calculating damage.
fn change_defensive_stat_value(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_amount: &mut u32,
) -> Result<()> {
@@ -275,8 +275,8 @@ pub trait Script: Send + Sync {
/// defender and attacker.
fn change_damage_stat_modifier(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_modifier: &mut f32,
) -> Result<()> {
@@ -285,22 +285,22 @@ pub trait Script: Send + Sync {
/// This function allows a script to apply a raw multiplier to the damage done by a move.
fn change_damage_modifier(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_modifier: &mut f32,
) -> Result<()> {
Ok(())
}
/// This function allows a script to modify the outgoing damage done by a move.
fn change_damage(&self, _move: &ExecutingMove, _target: &Arc<Pokemon>, _hit: u8, _damage: &mut u32) -> Result<()> {
fn change_damage(&self, _move: &Arc<ExecutingMove>, _target: &Pokemon, _hit: u8, _damage: &mut u32) -> Result<()> {
Ok(())
}
/// This function allows a script to modify the incoming damage done by a move.
fn change_incoming_damage(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_damage: &mut u32,
) -> Result<()> {
@@ -308,11 +308,11 @@ pub trait Script: Send + Sync {
}
/// This function triggers when an incoming hit happens. This triggers after the damage is done,
/// but before the secondary effect of the move happens.
fn on_incoming_hit(&self, _move: &ExecutingMove, _target: &Arc<Pokemon>, _hit: u8) -> Result<()> {
fn on_incoming_hit(&self, _move: &Arc<ExecutingMove>, _target: &Pokemon, _hit: u8) -> Result<()> {
Ok(())
}
/// This function triggers when an opponent on the field faints.
fn on_opponent_faints(&self, _move: &ExecutingMove, _target: &Arc<Pokemon>, _hit: u8) -> Result<()> {
fn on_opponent_faints(&self, _move: &Arc<ExecutingMove>, _target: &Pokemon, _hit: u8) -> Result<()> {
Ok(())
}
/// This function allows a script attached to a Pokemon or its parents to prevent stat boost
@@ -344,8 +344,8 @@ pub trait Script: Send + Sync {
/// secondary effect. Note that this function is not called for status moves.
fn prevent_secondary_effect(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_prevent: &mut bool,
) -> Result<()> {
@@ -357,8 +357,8 @@ pub trait Script: Send + Sync {
/// below 0 will make it never hit.
fn change_effect_chance(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_chance: &mut f32,
) -> Result<()> {
@@ -370,8 +370,8 @@ pub trait Script: Send + Sync {
/// or below 0 will make it never hit.
fn change_incoming_effect_chance(
&self,
_move: &ExecutingMove,
_target: &Arc<Pokemon>,
_move: &Arc<ExecutingMove>,
_target: &Pokemon,
_hit: u8,
_chance: &mut f32,
) -> Result<()> {
@@ -380,19 +380,19 @@ pub trait Script: Send + Sync {
/// This function triggers when the move uses its secondary effect. Moves should implement their
/// secondary effects here. Status moves should implement their actual functionality in this
/// function as well, as status moves effects are defined as secondary effects for simplicity.
fn on_secondary_effect(&self, _move: &ExecutingMove, _target: &Arc<Pokemon>, _hit: u8) -> Result<()> {
fn on_secondary_effect(&self, _move: &Arc<ExecutingMove>, _target: &Pokemon, _hit: u8) -> Result<()> {
Ok(())
}
/// This function triggers on a move or its parents when all hits on a target are finished.
fn on_after_hits(&self, _move: &ExecutingMove, _target: &Arc<Pokemon>) -> Result<()> {
fn on_after_hits(&self, _move: &Arc<ExecutingMove>, _target: &Pokemon) -> Result<()> {
Ok(())
}
/// This function prevents the Pokemon it is attached to from being able to switch out.
fn prevent_self_switch(&self, _choice: &TurnChoice, _prevent: &mut bool) -> Result<()> {
fn prevent_self_switch(&self, _choice: &Arc<TurnChoice>, _prevent: &mut bool) -> Result<()> {
Ok(())
}
/// This function allows the prevention of switching for any opponent.
fn prevent_opponent_switch(&self, _choice: &TurnChoice, _prevent: &mut bool) -> Result<()> {
fn prevent_opponent_switch(&self, _choice: &Arc<TurnChoice>, _prevent: &mut bool) -> Result<()> {
Ok(())
}
/// This function is called on a move and its parents when the move fails.
@@ -404,11 +404,11 @@ pub trait Script: Send + Sync {
Ok(())
}
/// This function allows preventing the running away of the Pokemon its attached to
fn prevent_self_run_away(&self, _choice: &TurnChoice, _prevent: &mut bool) -> Result<()> {
fn prevent_self_run_away(&self, _choice: &Arc<TurnChoice>, _prevent: &mut bool) -> Result<()> {
Ok(())
}
/// This function prevents a Pokemon on another side than where its attached to from running away.
fn prevent_opponent_run_away(&self, _choice: &TurnChoice, _prevent: &mut bool) -> Result<()> {
fn prevent_opponent_run_away(&self, _choice: &Arc<TurnChoice>, _prevent: &mut bool) -> Result<()> {
Ok(())
}
/// This function id triggered on all scripts active in the battle after all choices have finished
@@ -432,7 +432,7 @@ pub trait Script: Send + Sync {
}
/// This function is triggered on a Pokemon and its parents when the given Pokemon consumes the
/// held item it had.
fn on_after_held_item_consume(&self, _pokemon: &Pokemon, _item: &dyn Item) -> Result<()> {
fn on_after_held_item_consume(&self, _pokemon: &Pokemon, _item: &Arc<dyn Item>) -> Result<()> {
Ok(())
}
/// This function is triggered on a Pokemon and its parents when the given Pokemon gains experience,
@@ -458,7 +458,12 @@ pub trait Script: Send + Sync {
/// This function is called when a Pokeball is thrown at a Pokemon, and allows modifying the catch
/// rate of this attempt. Pokeball modifier effects should be implemented here, as well as for
/// example status effects that change capture rates.
fn change_capture_rate_bonus(&self, _target: &Pokemon, _pokeball: &dyn Item, _modifier: &mut u8) -> Result<()> {
fn change_capture_rate_bonus(
&self,
_target: &Pokemon,
_pokeball: &Arc<dyn Item>,
_modifier: &mut u8,
) -> Result<()> {
Ok(())
}
@@ -778,29 +783,29 @@ mod tests {
/// Data to store references to their owning objects on scripts.
pub enum ScriptOwnerData {
/// A script attached to a Pokemon has a reference to that Pokemon.
Pokemon(AtomicPtr<Pokemon>),
Pokemon(WeakPokemonReference),
/// A script attached to a Battle Side has a reference to that Battle Side.
BattleSide(AtomicPtr<BattleSide>),
BattleSide(WeakBattleSideReference),
/// A script attached to a Battle has a reference to that Battle.
Battle(AtomicPtr<Battle>),
Battle(WeakBattleReference),
/// A script also can have no owner.
None,
}
impl From<&Pokemon> for ScriptOwnerData {
fn from(p: &Pokemon) -> Self {
ScriptOwnerData::Pokemon(AtomicPtr::new(p as *const Pokemon as *mut Pokemon))
ScriptOwnerData::Pokemon(p.weak())
}
}
impl From<&BattleSide> for ScriptOwnerData {
fn from(p: &BattleSide) -> Self {
ScriptOwnerData::BattleSide(AtomicPtr::new(p as *const BattleSide as *mut BattleSide))
ScriptOwnerData::BattleSide(p.weak())
}
}
impl From<&Battle> for ScriptOwnerData {
fn from(p: &Battle) -> Self {
ScriptOwnerData::Battle(AtomicPtr::new(p as *const Battle as *mut Battle))
ScriptOwnerData::Battle(p.weak())
}
}