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

@@ -14,8 +14,10 @@ use crate::dynamic_data::script_handling::script_set::ScriptSet;
use crate::dynamic_data::script_handling::volatile_scripts::VolatileScripts;
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
use crate::{script_hook, PkmnResult, ScriptCategory, StringKey};
use atomic::Atomic;
use parking_lot::RwLock;
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::Arc;
#[derive(Debug)]
@@ -27,14 +29,14 @@ pub struct Battle<'own, 'library> {
pokemon_per_side: u8,
sides: Vec<BattleSide<'own, 'library>>,
random: BattleRandom,
current_turn_queue: Option<ChoiceQueue<'own, 'library>>,
has_ended: bool,
result: BattleResult,
current_turn_queue: RwLock<Option<ChoiceQueue<'own, 'library>>>,
has_ended: AtomicBool,
result: Atomic<BattleResult>,
event_hook: EventHook,
history_holder: Box<HistoryHolder>,
current_turn: u32,
current_turn: AtomicU32,
volatile_scripts: Arc<RwLock<ScriptSet>>,
last_turn_time: chrono::Duration,
last_turn_time: Atomic<chrono::Duration>,
script_source_data: RwLock<ScriptSourceData>,
}
@@ -66,14 +68,14 @@ impl<'own, 'library> Battle<'own, 'library> {
pokemon_per_side,
sides,
random,
current_turn_queue: None,
has_ended: false,
result: BattleResult::Inconclusive,
current_turn_queue: RwLock::new(None),
has_ended: AtomicBool::new(false),
result: Atomic::new(BattleResult::Inconclusive),
event_hook: Default::default(),
history_holder: Box::new(HistoryHolder {}),
current_turn: 0,
current_turn: AtomicU32::new(0),
volatile_scripts: Default::default(),
last_turn_time: chrono::Duration::zero(),
last_turn_time: Atomic::new(chrono::Duration::zero()),
script_source_data: Default::default(),
};
@@ -111,10 +113,10 @@ impl<'own, 'library> Battle<'own, 'library> {
&self.random
}
pub fn has_ended(&self) -> bool {
self.has_ended
self.has_ended.load(Ordering::Relaxed)
}
pub fn result(&self) -> BattleResult {
self.result
self.result.load(Ordering::Relaxed)
}
pub fn event_hook(&self) -> &EventHook {
&self.event_hook
@@ -123,19 +125,16 @@ impl<'own, 'library> Battle<'own, 'library> {
&self.history_holder
}
pub fn current_turn(&self) -> u32 {
self.current_turn
self.current_turn.load(Ordering::Relaxed)
}
pub fn last_turn_time(&self) -> chrono::Duration {
self.last_turn_time
self.last_turn_time.load(Ordering::Relaxed)
}
pub fn current_turn_queue(&self) -> &Option<ChoiceQueue<'own, 'library>> {
pub fn current_turn_queue(&self) -> &RwLock<Option<ChoiceQueue<'own, 'library>>> {
&self.current_turn_queue
}
pub fn current_turn_queue_mut(&mut self) -> &mut Option<ChoiceQueue<'own, 'library>> {
&mut self.current_turn_queue
}
pub fn get_pokemon(&self, side: u8, index: u8) -> &Option<Arc<RwLock<Pokemon<'own, 'library>>>> {
pub fn get_pokemon(&self, side: u8, index: u8) -> &Option<Arc<Pokemon<'own, 'library>>> {
let side = self.sides.get(side as usize);
if side.is_none() {
return &None;
@@ -156,8 +155,8 @@ impl<'own, 'library> Battle<'own, 'library> {
false
}
pub fn validate_battle_state(&mut self) {
if self.has_ended {
pub fn validate_battle_state(&self) {
if self.has_ended() {
return;
}
let mut surviving_side_exists = false;
@@ -165,8 +164,8 @@ impl<'own, 'library> Battle<'own, 'library> {
for (side_index, side) in self.sides.iter().enumerate() {
// If any side has fled, the battle end.
if side.has_fled() {
self.result = BattleResult::Inconclusive;
self.has_ended = true;
self.result.store(BattleResult::Inconclusive, Ordering::SeqCst);
self.has_ended.store(true, Ordering::SeqCst);
return;
}
// If the side is not defeated
@@ -181,18 +180,19 @@ impl<'own, 'library> Battle<'own, 'library> {
}
// Everyone died :(
if !surviving_side_exists {
self.result = BattleResult::Inconclusive;
self.result.store(BattleResult::Inconclusive, Ordering::SeqCst);
}
// Someone survived, they won!
else {
self.result = BattleResult::Conclusive(winning_side.unwrap());
self.result
.store(BattleResult::Conclusive(winning_side.unwrap()), Ordering::SeqCst);
}
self.has_ended = true;
self.has_ended.store(true, Ordering::SeqCst);
}
pub fn can_use(&self, choice: &TurnChoice) -> bool {
// If the user is not usable, we obviously can;t use the choice.
if !choice.user().read().is_usable() {
if !choice.user().is_usable() {
return false;
}
if let TurnChoice::Move(data) = choice {
@@ -204,7 +204,7 @@ impl<'own, 'library> Battle<'own, 'library> {
data.target_side(),
data.target_index(),
data.used_move().move_data().target(),
choice.user().read().deref(),
choice.user().deref(),
) {
return false;
}
@@ -216,10 +216,10 @@ impl<'own, 'library> Battle<'own, 'library> {
if !self.can_use(&choice) {
return Ok(false);
}
if !choice.user().read().is_on_battlefield() {
if !choice.user().is_on_battlefield() {
return Ok(false);
}
let side = choice.user().read().get_battle_side_index();
let side = choice.user().get_battle_side_index();
if side.is_none() {
return Ok(false);
}
@@ -228,7 +228,7 @@ impl<'own, 'library> Battle<'own, 'library> {
Ok(true)
}
fn check_choices_set_and_run(&mut self) -> PkmnResult<()> {
fn check_choices_set_and_run(&self) -> PkmnResult<()> {
for side in &self.sides {
if !side.all_choices_set() {
return Ok(());
@@ -239,8 +239,9 @@ impl<'own, 'library> Battle<'own, 'library> {
}
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 &mut self.sides {
for choice_opt in side.choices_mut() {
for side in &self.sides {
let mut side_choices = side.choices().write();
for choice_opt in side_choices.deref_mut() {
if choice_opt.is_none() {
panic!("Choice was none, but all choices were set? Logic error.");
}
@@ -264,22 +265,24 @@ impl<'own, 'library> Battle<'own, 'library> {
choice.set_random_value(self.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 += 1;
self.current_turn.fetch_add(1, Ordering::SeqCst);
choices.sort_unstable_by(|a, b| b.cmp(a));
self.current_turn_queue = Some(ChoiceQueue::new(choices));
self.current_turn_queue.write().replace(ChoiceQueue::new(choices));
{
self.run_turn()?;
}
self.current_turn_queue = None;
self.current_turn_queue.write().take();
self.event_hook.trigger(Event::EndTurn);
let end_time = chrono::Utc::now();
let time = end_time - start_time;
self.last_turn_time = time;
self.last_turn_time.store(time, Ordering::SeqCst);
Ok(())
}
}

View File

@@ -1,6 +1,5 @@
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::models::pokemon_party::PokemonParty;
use parking_lot::RwLock;
use std::sync::Arc;
#[derive(Debug)]
@@ -27,7 +26,6 @@ impl<'own, 'library> BattleParty<'own, 'library> {
pub fn has_pokemon_not_in_field(&self) -> bool {
for pokemon in self.party.pokemon().iter().flatten() {
let pokemon = pokemon.read();
if pokemon.is_usable() && !pokemon.is_on_battlefield() {
return true;
}
@@ -35,7 +33,7 @@ impl<'own, 'library> BattleParty<'own, 'library> {
false
}
pub fn get_pokemon(&self, index: usize) -> &Option<Arc<RwLock<Pokemon<'own, 'library>>>> {
pub fn get_pokemon(&self, index: usize) -> &Option<Arc<Pokemon<'own, 'library>>> {
self.party.at(index)
}
}

View File

@@ -1,9 +1,8 @@
use crate::dynamic_data::models::executing_move::ExecutingMove;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::script_handling::ScriptSource;
use crate::script_hook;
use crate::utils::random::Random;
use crate::{script_hook, script_hook_on_lock};
use parking_lot::RwLock;
use std::fmt::{Debug, Formatter};
use std::sync::{Arc, Mutex};
@@ -37,7 +36,7 @@ impl BattleRandom {
&self,
mut chance: f32,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
target: &Arc<Pokemon>,
hit_number: u8,
) -> bool {
script_hook!(
@@ -48,7 +47,7 @@ impl BattleRandom {
hit_number,
&mut chance
);
script_hook_on_lock!(
script_hook!(
change_incoming_effect_chance,
target,
executing_move,

View File

@@ -9,15 +9,17 @@ use crate::dynamic_data::script_handling::volatile_scripts::VolatileScripts;
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
use crate::{script_hook, PkmnResult, StringKey};
use parking_lot::RwLock;
use std::ops::Deref;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
#[derive(Debug)]
pub struct BattleSide<'own, 'library> {
index: u8,
pokemon_per_side: u8,
pokemon: Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>>,
choices: Vec<Option<TurnChoice<'own, 'library>>>,
fillable_slots: Vec<bool>,
pokemon: Vec<Option<Arc<Pokemon<'own, 'library>>>>,
choices: RwLock<Vec<Option<TurnChoice<'own, 'library>>>>,
fillable_slots: Vec<AtomicBool>,
choices_set: u8,
battle: *mut Battle<'own, 'library>,
has_fled_battle: bool,
@@ -35,8 +37,9 @@ impl<'own, 'library> BattleSide<'own, 'library> {
for _i in 0..pokemon_per_side {
pokemon.push(None);
choices.push(None);
fillable_slots.push(true);
fillable_slots.push(AtomicBool::new(false));
}
let choices = RwLock::new(choices);
Self {
index,
@@ -45,7 +48,7 @@ impl<'own, 'library> BattleSide<'own, 'library> {
choices,
fillable_slots,
choices_set: 0,
battle: 0 as *mut Battle,
battle: std::ptr::null_mut::<Battle>(),
has_fled_battle: false,
volatile_scripts: Default::default(),
script_source_data: Default::default(),
@@ -62,17 +65,14 @@ impl<'own, 'library> BattleSide<'own, 'library> {
pub fn pokemon_per_side(&self) -> u8 {
self.pokemon_per_side
}
pub fn pokemon(&self) -> &Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>> {
pub fn pokemon(&self) -> &Vec<Option<Arc<Pokemon<'own, 'library>>>> {
&self.pokemon
}
pub fn choices(&self) -> &Vec<Option<TurnChoice<'own, 'library>>> {
pub fn choices(&self) -> &RwLock<Vec<Option<TurnChoice<'own, 'library>>>> {
&self.choices
}
pub fn choices_mut(&mut self) -> &mut Vec<Option<TurnChoice<'own, 'library>>> {
&mut self.choices
}
pub fn fillable_slots(&self) -> &Vec<bool> {
pub fn fillable_slots(&self) -> &Vec<AtomicBool> {
&self.fillable_slots
}
pub fn choices_set(&self) -> u8 {
@@ -97,7 +97,7 @@ impl<'own, 'library> BattleSide<'own, 'library> {
/// empty, but can't be filled by any party anymore.
pub fn all_slots_filled(&self) -> bool {
for (i, pokemon) in self.pokemon.iter().enumerate() {
if (!pokemon.is_none() || !pokemon.as_ref().unwrap().read().is_usable())
if (!pokemon.is_none() || !pokemon.as_ref().unwrap().is_usable())
&& self.battle().can_slot_be_filled(self.index, i as u8)
{
return false;
@@ -109,8 +109,8 @@ impl<'own, 'library> BattleSide<'own, 'library> {
pub fn set_choice(&mut self, choice: TurnChoice<'own, 'library>) {
for (index, pokemon_slot) in self.pokemon.iter().enumerate() {
if let Some(pokemon) = pokemon_slot {
if std::ptr::eq(pokemon.data_ptr(), choice.user().data_ptr()) {
self.choices[index] = Some(choice);
if std::ptr::eq(pokemon.deref(), choice.user().deref()) {
self.choices.write()[index] = Some(choice);
self.choices_set += 1;
return;
}
@@ -118,9 +118,10 @@ impl<'own, 'library> BattleSide<'own, 'library> {
}
}
pub fn reset_choices(&mut self) {
for i in 0..self.choices.len() {
self.choices[i] = None;
pub fn reset_choices(&self) {
let len = self.choices.read().len();
for i in 0..len {
self.choices.write()[i] = None;
}
}
@@ -128,17 +129,15 @@ impl<'own, 'library> BattleSide<'own, 'library> {
self.pokemon[index as usize] = None;
}
pub fn set_pokemon(&mut self, index: u8, pokemon: Option<Arc<RwLock<Pokemon<'own, 'library>>>>) {
pub fn set_pokemon(&mut self, index: u8, pokemon: Option<Arc<Pokemon<'own, 'library>>>) {
let old = &mut self.pokemon[index as usize];
if let Some(old_pokemon) = old {
let mut p = old_pokemon.write();
script_hook!(on_remove, p,);
p.set_on_battlefield(false);
script_hook!(on_remove, old_pokemon,);
old_pokemon.set_on_battlefield(false);
}
self.pokemon[index as usize] = pokemon;
let pokemon = &self.pokemon[index as usize];
if let Some(pokemon_mutex) = pokemon {
let mut pokemon = pokemon_mutex.write();
if let Some(pokemon) = pokemon {
pokemon.set_battle_data(self.battle, self.index);
pokemon.set_on_battlefield(true);
pokemon.set_battle_index(index);
@@ -148,10 +147,9 @@ impl<'own, 'library> BattleSide<'own, 'library> {
if side.index() == self.index {
continue;
}
for opponent_mutex in side.pokemon().iter().flatten() {
let mut opponent = opponent_mutex.write();
opponent.mark_opponent_as_seen(Arc::downgrade(pokemon_mutex));
pokemon.mark_opponent_as_seen(Arc::downgrade(opponent_mutex));
for opponent in side.pokemon().iter().flatten() {
opponent.mark_opponent_as_seen(Arc::downgrade(pokemon));
pokemon.mark_opponent_as_seen(Arc::downgrade(opponent));
}
}
battle.event_hook().trigger(Event::Switch {
@@ -171,22 +169,22 @@ impl<'own, 'library> BattleSide<'own, 'library> {
pub fn is_pokemon_on_side(&self, pokemon: Arc<Pokemon<'own, 'library>>) -> bool {
for p in self.pokemon.iter().flatten() {
if p.read().unique_identifier() == pokemon.unique_identifier() {
if std::ptr::eq(p.deref().deref(), pokemon.deref()) {
return true;
}
}
false
}
pub fn mark_slot_as_unfillable(&mut self, index: u8) {
self.fillable_slots[index as usize] = false;
pub fn mark_slot_as_unfillable(&self, index: u8) {
self.fillable_slots[index as usize].store(false, Ordering::SeqCst);
}
pub fn is_slot_unfillable(&self, pokemon: Arc<Pokemon<'own, 'library>>) -> bool {
for (i, slot) in self.pokemon.iter().enumerate() {
if let Some(p) = slot {
if p.read().unique_identifier() == pokemon.unique_identifier() {
return self.fillable_slots[i];
if std::ptr::eq(p.deref().deref(), pokemon.deref()) {
return self.fillable_slots[i].load(Ordering::Relaxed);
}
}
}
@@ -195,7 +193,7 @@ impl<'own, 'library> BattleSide<'own, 'library> {
pub fn is_defeated(&self) -> bool {
for fillable_slot in &self.fillable_slots {
if *fillable_slot {
if fillable_slot.load(Ordering::Relaxed) {
return false;
}
}

View File

@@ -6,6 +6,7 @@ use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, Scrip
use crate::static_data::MoveData;
use crate::{PkmnResult, PokemonError};
use parking_lot::RwLock;
use std::ops::Deref;
use std::sync::Arc;
#[derive(Default, Debug)]
@@ -62,7 +63,7 @@ impl HitData {
pub struct ExecutingMove<'own, 'battle, 'library> {
number_of_hits: u8,
hits: Vec<HitData>,
user: Arc<RwLock<Pokemon<'battle, 'library>>>,
user: Arc<Pokemon<'battle, 'library>>,
chosen_move: Arc<LearnedMove<'library>>,
use_move: &'own MoveData,
script: ScriptContainer,
@@ -74,7 +75,7 @@ impl<'own, 'battle, 'library> ExecutingMove<'own, 'battle, 'library> {
pub fn new(
targets: &'own TargetList<'battle, 'library>,
number_of_hits: u8,
user: Arc<RwLock<Pokemon<'battle, 'library>>>,
user: Arc<Pokemon<'battle, 'library>>,
chosen_move: Arc<LearnedMove<'library>>,
use_move: &'own MoveData,
script: ScriptContainer,
@@ -101,7 +102,7 @@ impl<'own, 'battle, 'library> ExecutingMove<'own, 'battle, 'library> {
pub fn number_of_hits(&self) -> u8 {
self.number_of_hits
}
pub fn user(&self) -> &Arc<RwLock<Pokemon<'battle, 'library>>> {
pub fn user(&self) -> &Arc<Pokemon<'battle, 'library>> {
&self.user
}
pub fn chosen_move(&self) -> &Arc<LearnedMove<'library>> {
@@ -117,12 +118,12 @@ impl<'own, 'battle, 'library> ExecutingMove<'own, 'battle, 'library> {
pub fn get_hit_data<'func>(
&'func self,
for_target: &'func Arc<RwLock<Pokemon<'battle, 'library>>>,
for_target: &'func Arc<Pokemon<'battle, 'library>>,
hit: u8,
) -> PkmnResult<&'func HitData> {
for (index, target) in self.targets.iter().enumerate() {
if let Some(target) = target {
if std::ptr::eq(target.data_ptr(), for_target.data_ptr()) {
if std::ptr::eq(target.deref().deref(), for_target.deref().deref()) {
let i = index * self.number_of_hits as usize + hit as usize;
return Ok(&self.hits[i]);
}
@@ -131,22 +132,19 @@ impl<'own, 'battle, 'library> ExecutingMove<'own, 'battle, 'library> {
Err(PokemonError::InvalidTargetRequested)
}
pub fn is_pokemon_target(&self, pokemon: &Arc<RwLock<Pokemon<'battle, 'library>>>) -> bool {
pub fn is_pokemon_target(&self, pokemon: &Arc<Pokemon<'battle, 'library>>) -> bool {
for target in self.targets.iter().flatten() {
if std::ptr::eq(target.data_ptr(), pokemon.data_ptr()) {
if std::ptr::eq(target.deref().deref(), pokemon.deref().deref()) {
return true;
}
}
false
}
pub(crate) fn get_index_of_target(
&self,
for_target: &Arc<RwLock<Pokemon<'battle, 'library>>>,
) -> PkmnResult<usize> {
pub(crate) fn get_index_of_target(&self, for_target: &Arc<Pokemon<'battle, 'library>>) -> PkmnResult<usize> {
for (index, target) in self.targets.iter().enumerate() {
if let Some(target) = target {
if std::ptr::eq(target.data_ptr(), for_target.data_ptr()) {
if std::ptr::eq(target.deref().deref(), for_target.deref().deref()) {
let i = index * self.number_of_hits as usize;
return Ok(i);
}
@@ -178,6 +176,6 @@ impl<'own, 'battle, 'library> ScriptSource<'own> for ExecutingMove<'own, 'battle
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
self.user.read().get_own_scripts(scripts);
self.user.get_own_scripts(scripts);
}
}

View File

@@ -19,17 +19,19 @@ use crate::static_data::statistic_set::{ClampedStatisticSet, StatisticSet};
use crate::static_data::DataLibrary;
use crate::utils::random::Random;
use crate::{script_hook, PkmnResult, ScriptCategory, StringKey};
use atomic::Atomic;
use parking_lot::RwLock;
use std::sync::atomic::{AtomicI8, AtomicU32, AtomicU8, Ordering};
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicBool, AtomicI8, AtomicU32, AtomicU8, Ordering};
use std::sync::{Arc, Weak};
#[derive(Debug)]
pub struct PokemonBattleData<'pokemon, 'library> {
battle: *mut Battle<'pokemon, 'library>,
battle_side_index: u8,
index: u8,
on_battle_field: bool,
seen_opponents: Vec<Weak<RwLock<Pokemon<'pokemon, 'library>>>>,
battle_side_index: AtomicU8,
index: AtomicU8,
on_battle_field: AtomicBool,
seen_opponents: RwLock<Vec<Weak<Pokemon<'pokemon, 'library>>>>,
}
impl<'pokemon, 'library> PokemonBattleData<'pokemon, 'library> {
@@ -41,13 +43,16 @@ impl<'pokemon, 'library> PokemonBattleData<'pokemon, 'library> {
}
pub fn battle_side_index(&self) -> u8 {
self.battle_side_index
self.battle_side_index.load(Ordering::Relaxed)
}
pub fn on_battle_field(&mut self) -> &mut bool {
&mut self.on_battle_field
pub fn index(&self) -> u8 {
self.index.load(Ordering::Relaxed)
}
pub fn seen_opponents(&mut self) -> &mut Vec<Weak<RwLock<Pokemon<'pokemon, 'library>>>> {
&mut self.seen_opponents
pub fn on_battle_field(&self) -> bool {
self.on_battle_field.load(Ordering::Relaxed)
}
pub fn seen_opponents(&self) -> &RwLock<Vec<Weak<Pokemon<'pokemon, 'library>>>> {
&self.seen_opponents
}
}
@@ -71,8 +76,8 @@ where
held_item: Option<&'own Item>,
current_health: AtomicU32,
weight: f32,
height: f32,
weight: Atomic<f32>,
height: Atomic<f32>,
stat_boost: ClampedStatisticSet<AtomicI8, -6, 6>,
flat_stats: StatisticSet<AtomicU32>,
@@ -87,9 +92,9 @@ where
is_ability_overridden: bool,
override_ability: Option<Ability>,
battle_data: Option<PokemonBattleData<'own, 'library>>,
battle_data: RwLock<Option<PokemonBattleData<'own, 'library>>>,
moves: [Option<Arc<LearnedMove<'library>>>; MAX_MOVES],
moves: RwLock<[Option<Arc<LearnedMove<'library>>>; MAX_MOVES]>,
allowed_experience: bool,
types: Vec<u8>,
@@ -141,8 +146,8 @@ impl<'own, 'library> Pokemon<'own, 'library> {
coloring,
held_item: None,
current_health: AtomicU32::new(1),
weight,
height,
weight: Atomic::new(weight),
height: Atomic::new(height),
stat_boost: Default::default(),
flat_stats: Default::default(),
boosted_stats: Default::default(),
@@ -153,8 +158,8 @@ impl<'own, 'library> Pokemon<'own, 'library> {
ability_index: ability,
is_ability_overridden: false,
override_ability: None,
battle_data: None,
moves: [None, None, None, None],
battle_data: RwLock::new(None),
moves: RwLock::new([None, None, None, None]),
allowed_experience: false,
types: form.types().to_vec(),
is_egg: false,
@@ -247,10 +252,10 @@ impl<'own, 'library> Pokemon<'own, 'library> {
self.boosted_stats.hp()
}
pub fn weight(&self) -> f32 {
self.weight
self.weight.load(Ordering::Relaxed)
}
pub fn height(&self) -> f32 {
self.height
self.height.load(Ordering::Relaxed)
}
pub fn nickname(&self) -> &Option<String> {
&self.nickname
@@ -261,7 +266,7 @@ impl<'own, 'library> Pokemon<'own, 'library> {
pub fn types(&self) -> &Vec<u8> {
&self.types
}
pub fn learned_moves(&self) -> &[Option<Arc<LearnedMove<'library>>>; MAX_MOVES] {
pub fn learned_moves(&self) -> &RwLock<[Option<Arc<LearnedMove<'library>>>; MAX_MOVES]> {
&self.moves
}
pub fn status(&self) -> &ScriptContainer {
@@ -284,17 +289,18 @@ impl<'own, 'library> Pokemon<'own, 'library> {
}
pub fn get_battle(&self) -> Option<&Battle<'own, 'library>> {
if let Some(data) = &self.battle_data {
Some(data.battle().unwrap())
let r = self.battle_data.read();
if let Some(data) = &r.deref() {
unsafe { data.battle.as_ref() }
} else {
None
}
}
pub fn get_battle_side_index(&self) -> Option<u8> {
self.battle_data.as_ref().map(|data| data.battle_side_index)
self.battle_data.read().as_ref().map(|data| data.battle_side_index())
}
pub fn get_battle_index(&self) -> Option<u8> {
self.battle_data.as_ref().map(|data| data.index)
self.battle_data.read().as_ref().map(|data| data.index())
}
pub fn is_ability_overriden(&self) -> bool {
self.is_ability_overridden
@@ -316,13 +322,9 @@ impl<'own, 'library> Pokemon<'own, 'library> {
&self.ability_script
}
pub fn seen_opponents(&self) -> Option<&Vec<Weak<RwLock<Pokemon<'own, 'library>>>>> {
if let Some(data) = &self.battle_data {
Some(&data.seen_opponents)
} else {
None
}
}
// pub fn seen_opponents(&self) -> &RwLock<Option<PokemonBattleData<'own, 'library>>> {
// &self.battle_data.read
// }
pub fn allowed_experience_gain(&self) -> bool {
self.allowed_experience
}
@@ -331,12 +333,16 @@ impl<'own, 'library> Pokemon<'own, 'library> {
self.nature
}
pub fn recalculate_flat_stats(&mut self) {
self.flat_stats = self.library.stat_calculator().calculate_flat_stats(self);
pub fn recalculate_flat_stats(&self) {
self.library
.stat_calculator()
.calculate_flat_stats(self, &self.flat_stats);
self.recalculate_boosted_stats();
}
pub fn recalculate_boosted_stats(&mut self) {
self.boosted_stats = self.library.stat_calculator().calculate_boosted_stats(self);
pub fn recalculate_boosted_stats(&self) {
self.library
.stat_calculator()
.calculate_boosted_stats(self, &self.boosted_stats);
}
pub fn change_species(&mut self, species: &'own Species, form: &'own Form) {
@@ -346,10 +352,10 @@ impl<'own, 'library> Pokemon<'own, 'library> {
// 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
if self.battle_data.is_some() {
let battle_data = self.battle_data.as_mut().unwrap();
self.gender =
species.get_random_gender(&mut battle_data.battle().unwrap().random().get_rng().lock().unwrap());
let r = self.battle_data.read();
if let Some(data) = r.deref() {
let mut random = data.battle().unwrap().random().get_rng().lock().unwrap();
self.gender = species.get_random_gender(random.deref_mut());
} else {
// If we're not in battle, just use a new random.
self.gender = species.get_random_gender(&mut Random::default());
@@ -359,7 +365,8 @@ impl<'own, 'library> Pokemon<'own, 'library> {
else if species.gender_rate() < 0.0 && self.gender != Gender::Genderless {
self.gender = Gender::Genderless;
}
if let Some(battle_data) = &self.battle_data {
let r = self.battle_data.read();
if let Some(battle_data) = &r.deref() {
if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::SpeciesChange {
pokemon: self,
@@ -380,8 +387,8 @@ impl<'own, 'library> Pokemon<'own, 'library> {
for t in form.types() {
self.types.push(*t);
}
self.weight = form.weight();
self.height = form.height();
self.weight.store(form.weight(), Ordering::SeqCst);
self.height.store(form.height(), Ordering::SeqCst);
let ability_script = self
.library
@@ -409,7 +416,8 @@ impl<'own, 'library> Pokemon<'own, 'library> {
}
// TODO: consider form specific attacks?
if let Some(battle_data) = &self.battle_data {
let r = self.battle_data.read();
if let Some(battle_data) = r.deref() {
if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::FormChange { pokemon: self, form })
}
@@ -424,58 +432,59 @@ impl<'own, 'library> Pokemon<'own, 'library> {
self.current_health() == 0
}
pub fn set_battle_data(&mut self, battle: *mut Battle<'own, 'library>, battle_side_index: u8) {
if let Some(battle_data) = &mut self.battle_data {
pub fn set_battle_data(&self, battle: *mut Battle<'own, 'library>, battle_side_index: u8) {
let mut w = self.battle_data.write();
if let Some(battle_data) = w.deref_mut() {
battle_data.battle = battle;
battle_data.battle_side_index = battle_side_index;
battle_data.battle_side_index.store(battle_side_index, Ordering::SeqCst);
} else {
self.battle_data = Some(PokemonBattleData {
w.replace(PokemonBattleData {
battle,
battle_side_index,
index: 0,
on_battle_field: false,
battle_side_index: AtomicU8::new(battle_side_index),
index: AtomicU8::new(0),
on_battle_field: AtomicBool::new(false),
seen_opponents: Default::default(),
})
});
}
}
pub fn set_on_battlefield(&mut self, value: bool) {
if let Some(data) = &mut self.battle_data {
data.on_battle_field = value;
pub fn set_on_battlefield(&self, value: bool) {
let r = self.battle_data.read();
if let Some(data) = &mut r.deref() {
data.on_battle_field.store(value, Ordering::SeqCst);
if !value {
self.volatile.write().clear();
self.weight = self.form.weight();
self.height = self.form.height();
self.weight.store(self.form.weight(), Ordering::SeqCst);
self.height.store(self.form.height(), Ordering::SeqCst);
}
}
}
pub fn set_battle_index(&mut self, index: u8) {
if let Some(data) = &mut self.battle_data {
data.index = index;
pub fn set_battle_index(&self, index: u8) {
let r = self.battle_data.read();
if let Some(data) = r.deref() {
data.index.store(index, Ordering::SeqCst)
}
}
pub fn is_on_battlefield(&self) -> bool {
if let Some(data) = &self.battle_data {
data.on_battle_field
} else {
false
}
self.battle_data.read().is_some_and(|a| a.on_battle_field())
}
pub fn mark_opponent_as_seen(&mut self, pokemon: Weak<RwLock<Pokemon<'own, 'library>>>) {
if let Some(battle_data) = &mut self.battle_data {
for seen_opponent in &battle_data.seen_opponents {
pub fn mark_opponent_as_seen(&self, pokemon: Weak<Pokemon<'own, 'library>>) {
let r = self.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) {
return;
}
}
battle_data.seen_opponents.push(pokemon);
opponents.push(pokemon);
}
}
pub fn damage(&mut self, mut damage: u32, source: DamageSource) {
pub fn damage(&self, mut damage: u32, source: DamageSource) {
if damage > self.current_health() {
damage = self.current_health();
}
@@ -483,7 +492,7 @@ impl<'own, 'library> Pokemon<'own, 'library> {
return;
}
let new_health = self.current_health() - damage;
if let Some(battle_data) = &self.battle_data {
if let Some(battle_data) = &self.battle_data.read().deref() {
if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::Damage {
pokemon: self,
@@ -492,65 +501,53 @@ impl<'own, 'library> Pokemon<'own, 'library> {
new_health,
});
// TODO: register history
script_hook!(on_damage, self, self, source, self.current_health(), new_health);
}
}
if self.battle_data.read().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);
if self.is_fainted() && damage > 0 {
self.on_faint(source);
}
}
pub fn on_faint(&mut self, source: DamageSource) {
if self.battle_data.is_some() && self.battle_data.as_ref().unwrap().battle().is_some() {
self.battle_data
.as_ref()
.unwrap()
.battle()
.unwrap()
.event_hook()
.trigger(Event::Faint { pokemon: self });
script_hook!(on_faint, self, self, source);
script_hook!(on_remove, self,);
pub fn on_faint(&self, source: DamageSource) {
let r = self.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 });
script_hook!(on_faint, self, self, source);
script_hook!(on_remove, self,);
let side_index = self.battle_data.as_ref().unwrap().battle_side_index;
let index = self.battle_data.as_ref().unwrap().index;
if !self
.battle_data
.as_ref()
.unwrap()
.battle()
.unwrap()
.can_slot_be_filled(side_index, index)
{
self.battle_data.as_mut().unwrap().battle_mut().unwrap().sides_mut()[side_index as usize]
.mark_slot_as_unfillable(index);
if !battle.can_slot_be_filled(battle_data.battle_side_index(), battle_data.index()) {
battle.sides()[battle_data.battle_side_index() as usize]
.mark_slot_as_unfillable(battle_data.index());
}
battle.validate_battle_state();
}
self.battle_data
.as_mut()
.unwrap()
.battle_mut()
.unwrap()
.validate_battle_state();
}
}
pub fn learn_move(&mut self, move_name: &StringKey, learn_method: MoveLearnMethod) {
let move_pos = self.learned_moves().iter().position(|a| a.is_none());
pub fn learn_move(&self, move_name: &StringKey, learn_method: MoveLearnMethod) {
let mut learned_moves = self.learned_moves().write();
let move_pos = learned_moves.iter().position(|a| a.is_none());
if move_pos.is_none() {
panic!("No more moves with an empty space found.");
}
let move_data = self.library.static_data().moves().get(move_name).unwrap();
self.moves[move_pos.unwrap()] = Some(Arc::new(LearnedMove::new(move_data, learn_method)));
learned_moves[move_pos.unwrap()] = Some(Arc::new(LearnedMove::new(move_data, learn_method)));
}
}
impl<'own, 'library> ScriptSource<'own> for Pokemon<'own, 'library> {
fn get_script_count(&self) -> usize {
let mut c = 3;
if let Some(battle_data) = &self.battle_data {
if let Some(battle_data) = &self.battle_data.read().deref() {
if let Some(battle) = battle_data.battle() {
c += battle.sides()[battle_data.battle_side_index as usize].get_script_count();
c += battle.sides()[battle_data.battle_side_index() as usize].get_script_count();
}
}
c
@@ -569,9 +566,9 @@ impl<'own, 'library> ScriptSource<'own> for Pokemon<'own, 'library> {
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
if let Some(battle_data) = &self.battle_data {
if let Some(battle_data) = &self.battle_data.read().deref() {
if let Some(battle) = battle_data.battle() {
battle.sides()[battle_data.battle_side_index as usize].collect_scripts(scripts);
battle.sides()[battle_data.battle_side_index() as usize].collect_scripts(scripts);
}
}
}

View File

@@ -29,7 +29,7 @@ impl<'own> PokemonBuilder<'own> {
pub fn build(self) -> Pokemon<'own, 'own> {
let species = self.library.static_data().species().get(&self.species).unwrap();
let form = species.get_default_form();
let mut p = Pokemon::new(
let p = Pokemon::new(
self.library,
species,
form,

View File

@@ -1,10 +1,9 @@
use crate::dynamic_data::models::pokemon::Pokemon;
use parking_lot::RwLock;
use std::sync::Arc;
#[derive(Debug)]
pub struct PokemonParty<'pokemon, 'library> {
pokemon: Vec<Option<Arc<RwLock<Pokemon<'pokemon, 'library>>>>>,
pokemon: Vec<Option<Arc<Pokemon<'pokemon, 'library>>>>,
}
impl<'own, 'library> PokemonParty<'own, 'library> {
@@ -16,11 +15,11 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
Self { pokemon }
}
pub fn new_from_vec(pokemon: Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>>) -> Self {
pub fn new_from_vec(pokemon: Vec<Option<Arc<Pokemon<'own, 'library>>>>) -> Self {
Self { pokemon }
}
pub fn at(&self, index: usize) -> &Option<Arc<RwLock<Pokemon<'own, 'library>>>> {
pub fn at(&self, index: usize) -> &Option<Arc<Pokemon<'own, 'library>>> {
let opt = self.pokemon.get(index);
if let Some(v) = opt {
v
@@ -36,8 +35,8 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
pub fn swap_into(
&mut self,
index: usize,
pokemon: Option<Arc<RwLock<Pokemon<'own, 'library>>>>,
) -> Option<Arc<RwLock<Pokemon<'own, 'library>>>> {
pokemon: Option<Arc<Pokemon<'own, 'library>>>,
) -> Option<Arc<Pokemon<'own, 'library>>> {
if index >= self.pokemon.len() {
return pokemon;
}
@@ -48,7 +47,7 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
pub fn has_usable_pokemon(&self) -> bool {
for pokemon in self.pokemon.iter().flatten() {
if pokemon.read().is_usable() {
if pokemon.is_usable() {
return true;
}
}
@@ -59,7 +58,7 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
self.pokemon.len()
}
pub fn pokemon(&self) -> &Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>> {
pub fn pokemon(&self) -> &Vec<Option<Arc<Pokemon<'own, 'library>>>> {
&self.pokemon
}