More work on using interior mutability instead of exterior mutability for dynamic types (Battle, Pokemon, etc).
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-06-18 15:52:39 +02:00
parent c45c7538bf
commit 5576bc8b80
21 changed files with 324 additions and 385 deletions

View File

@@ -7,14 +7,13 @@ use crate::dynamic_data::models::executing_move::ExecutingMove;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::script_handling::{ScriptSource, ScriptWrapper};
use crate::static_data::{DataLibrary, MoveCategory};
use crate::{run_scripts, script_hook, script_hook_on_lock, PkmnResult};
use parking_lot::RwLock;
use crate::{run_scripts, script_hook, PkmnResult};
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
impl<'own, 'library> Battle<'own, 'library> {
pub(crate) fn run_turn(&mut self) -> PkmnResult<()> {
let mut choice_queue = self.current_turn_queue().as_ref().unwrap();
pub(crate) fn run_turn(&self) -> PkmnResult<()> {
let choice_queue = self.current_turn_queue();
// We are now at the very beginning of a turn. We have assigned speeds and priorities to all
// choices, and put them in the correct order.
@@ -23,17 +22,16 @@ impl<'own, 'library> Battle<'own, 'library> {
// is primarily intended to be used to reset variables on a script (for example scripts that need
// to check whether a pokemon was hit this turn. By resetting here, and setting a variable to true
// they can then know this later on.)
for choice in choice_queue.get_queue().iter().flatten() {
for choice in choice_queue.read().as_ref().unwrap().get_queue().iter().flatten() {
script_hook!(on_before_turn, choice, choice);
}
// Now we can properly begin executing choices.
// One by one dequeue the turns, and run them. If the battle has ended we do not want to
// continue running however.
while choice_queue.has_next() && !self.has_ended() {
let choice = self.current_turn_queue_mut().as_mut().unwrap().dequeue();
while choice_queue.read().as_ref().unwrap().has_next() && !self.has_ended() {
let choice = choice_queue.write().as_mut().unwrap().dequeue();
self.execute_choice(&choice)?;
choice_queue = self.current_turn_queue().as_ref().unwrap();
}
// If the battle is not ended, we have arrived at the normal end of a turn. and thus want
@@ -47,7 +45,7 @@ impl<'own, 'library> Battle<'own, 'library> {
for side in self.sides() {
for pokemon in side.pokemon().iter().flatten() {
scripts = Vec::new();
pokemon.read().get_own_scripts(&mut scripts);
pokemon.get_own_scripts(&mut scripts);
run_scripts!(on_end_turn, scripts,);
}
scripts = Vec::new();
@@ -69,7 +67,7 @@ impl<'own, 'library> Battle<'own, 'library> {
return Ok(());
}
{
let user = choice.user().read();
let user = choice.user();
if !user.is_usable() {
return Ok(());
}
@@ -149,11 +147,11 @@ impl<'own, 'library> Battle<'own, 'library> {
fn handle_move_for_target(
&self,
executing_move: &mut ExecutingMove<'_, 'own, 'library>,
target: &Arc<RwLock<Pokemon<'own, 'library>>>,
target: &Arc<Pokemon<'own, 'library>>,
) -> PkmnResult<()> {
{
let mut fail = false;
script_hook_on_lock!(fail_incoming_move, target, executing_move, target, &mut fail);
script_hook!(fail_incoming_move, target, executing_move, target, &mut fail);
if fail {
// TODO: Add fail handling
return Ok(());
@@ -161,7 +159,7 @@ impl<'own, 'library> Battle<'own, 'library> {
}
{
let mut invulnerable = false;
script_hook_on_lock!(is_invulnerable, target, executing_move, target, &mut invulnerable);
script_hook!(is_invulnerable, target, executing_move, target, &mut invulnerable);
if invulnerable {
// TODO: Add fail handling
return Ok(());
@@ -169,9 +167,9 @@ impl<'own, 'library> Battle<'own, 'library> {
}
let number_of_hits = executing_move.number_of_hits();
if number_of_hits == 0 {
script_hook_on_lock!(on_move_miss, target, executing_move, target);
script_hook!(on_move_miss, target, executing_move, target);
self.event_hook().trigger(Event::Miss {
user: executing_move.user().read().deref(),
user: executing_move.user().deref(),
});
return Ok(());
}
@@ -181,10 +179,10 @@ impl<'own, 'library> Battle<'own, 'library> {
if self.has_ended() {
return Ok(());
}
if executing_move.user().read().is_fainted() {
if executing_move.user().is_fainted() {
break;
}
if target.read().is_fainted() {
if target.is_fainted() {
break;
}
{
@@ -204,7 +202,7 @@ impl<'own, 'library> Battle<'own, 'library> {
.library()
.static_data()
.types()
.get_effectiveness(hit_type, target.read().types());
.get_effectiveness(hit_type, target.types());
script_hook!(
change_effectiveness,
executing_move,
@@ -225,7 +223,7 @@ impl<'own, 'library> Battle<'own, 'library> {
hit_index,
&mut block_critical
);
script_hook_on_lock!(
script_hook!(
block_incoming_critical,
target,
executing_move,
@@ -278,7 +276,7 @@ impl<'own, 'library> Battle<'own, 'library> {
let mut damage = executing_move
.get_hit_from_raw_index(target_hit_stat + hit_index as usize)
.damage();
let current_health = target.read().current_health();
let current_health = target.current_health();
if damage > current_health {
damage = current_health;
executing_move
@@ -286,16 +284,16 @@ impl<'own, 'library> Battle<'own, 'library> {
.set_damage(damage);
}
if damage > 0 {
target.write().damage(damage, DamageSource::AttackDamage);
if !target.read().is_fainted() {
script_hook_on_lock!(on_incoming_hit, target, executing_move, target, hit_index);
target.damage(damage, DamageSource::AttackDamage);
if !target.is_fainted() {
script_hook!(on_incoming_hit, target, executing_move, target, hit_index);
} else {
script_hook!(on_opponent_faints, executing_move, executing_move, target, hit_index);
}
if executing_move.use_move().has_secondary_effect() && !target.read().is_fainted() {
if executing_move.use_move().has_secondary_effect() && !target.is_fainted() {
let mut prevent_secondary = false;
script_hook_on_lock!(
script_hook!(
prevent_secondary_effect,
target,
executing_move,
@@ -329,7 +327,7 @@ impl<'own, 'library> Battle<'own, 'library> {
}
}
if !executing_move.user().read().is_fainted() {
if !executing_move.user().is_fainted() {
script_hook!(on_after_hits, executing_move, executing_move, target);
}