Loads more work on battling, initial stretch to run a turn done.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-06-16 17:59:33 +02:00
parent a33369afcc
commit ff541b0696
50 changed files with 105871 additions and 497 deletions

View File

@@ -1,5 +1,5 @@
use crate::dynamic_data::choices::TurnChoice;
use crate::dynamic_data::event_hooks::event_hook::EventHook;
use crate::dynamic_data::event_hooks::event_hook::{Event, EventHook};
use crate::dynamic_data::flow::choice_queue::ChoiceQueue;
use crate::dynamic_data::flow::target_resolver::is_valid_target;
use crate::dynamic_data::history::history_holder::HistoryHolder;
@@ -13,35 +13,36 @@ use crate::dynamic_data::script_handling::script::Script;
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::{PkmnResult, ScriptCategory, StringKey};
use crate::{script_hook, PkmnResult, ScriptCategory, StringKey};
use parking_lot::RwLock;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
#[derive(Debug)]
pub struct Battle<'a> {
library: &'a DynamicLibrary<'a>,
parties: Vec<BattleParty<'a>>,
pub struct Battle<'own, 'library> {
library: &'own DynamicLibrary,
parties: Vec<BattleParty<'own, 'library>>,
can_flee: bool,
number_of_sides: u8,
pokemon_per_side: u8,
sides: Vec<BattleSide<'a>>,
sides: Vec<BattleSide<'own, 'library>>,
random: BattleRandom,
current_turn_queue: Option<ChoiceQueue>,
current_turn_queue: Option<Arc<RwLock<ChoiceQueue<'own, 'library>>>>,
has_ended: bool,
result: BattleResult,
event_hook: EventHook,
history_holder: Box<HistoryHolder>,
current_turn: u32,
volatile_scripts: Arc<RwLock<ScriptSet>>,
last_turn_time: i64,
last_turn_time: chrono::Duration,
script_source_data: RwLock<ScriptSourceData>,
}
impl<'a> Battle<'a> {
impl<'own, 'library> Battle<'own, 'library> {
pub fn new(
library: &'a DynamicLibrary<'a>,
parties: Vec<BattleParty<'a>>,
library: &'own DynamicLibrary,
parties: Vec<BattleParty<'own, 'library>>,
can_flee: bool,
number_of_sides: u8,
pokemon_per_side: u8,
@@ -68,21 +69,20 @@ impl<'a> Battle<'a> {
history_holder: Box::new(HistoryHolder {}),
current_turn: 0,
volatile_scripts: Default::default(),
last_turn_time: 0,
last_turn_time: chrono::Duration::zero(),
script_source_data: Default::default(),
}));
for i in 0..number_of_sides {
battle.write().sides[i as usize] =
BattleSide::new(i, Arc::downgrade(&battle), pokemon_per_side);
battle.write().sides[i as usize] = BattleSide::new(i, Arc::downgrade(&battle), pokemon_per_side);
}
battle
}
pub fn library(&self) -> &'a DynamicLibrary<'a> {
pub fn library(&self) -> &'own DynamicLibrary {
self.library
}
pub fn parties(&self) -> &Vec<BattleParty<'a>> {
pub fn parties(&self) -> &Vec<BattleParty<'own, 'library>> {
&self.parties
}
pub fn can_flee(&self) -> bool {
@@ -94,10 +94,10 @@ impl<'a> Battle<'a> {
pub fn pokemon_per_side(&self) -> u8 {
self.pokemon_per_side
}
pub fn sides(&self) -> &Vec<BattleSide<'a>> {
pub fn sides(&self) -> &Vec<BattleSide<'own, 'library>> {
&self.sides
}
pub fn sides_mut(&mut self) -> &mut Vec<BattleSide<'a>> {
pub fn sides_mut(&mut self) -> &mut Vec<BattleSide<'own, 'library>> {
&mut self.sides
}
@@ -119,14 +119,14 @@ impl<'a> Battle<'a> {
pub fn current_turn(&self) -> u32 {
self.current_turn
}
pub fn last_turn_time(&self) -> i64 {
pub fn last_turn_time(&self) -> chrono::Duration {
self.last_turn_time
}
pub fn current_turn_queue(&self) -> &Option<ChoiceQueue> {
pub fn current_turn_queue(&self) -> &Option<Arc<RwLock<ChoiceQueue<'own, 'library>>>> {
&self.current_turn_queue
}
pub fn get_pokemon(&self, side: u8, index: u8) -> &Option<Arc<RwLock<Pokemon<'a>>>> {
pub fn get_pokemon(&self, side: u8, index: u8) -> &Option<Arc<RwLock<Pokemon<'own, 'library>>>> {
let side = self.sides.get(side as usize);
if side.is_none() {
return &None;
@@ -183,34 +183,99 @@ impl<'a> Battle<'a> {
pub fn can_use(&self, choice: &TurnChoice) -> bool {
// If the user is not usable, we obviously can;t use the choice.
if !choice.user().is_usable() {
if !choice.user().read().is_usable() {
return false;
}
if let TurnChoice::Move {
used_move,
target_side,
target_index,
user,
} = choice
{
if let TurnChoice::Move(data) = choice {
// TODO: Hook to change number of PP needed.
if used_move.remaining_pp() < 1 {
if data.used_move().read().remaining_pp() < 1 {
return false;
}
if !is_valid_target(
*target_side,
*target_index,
used_move.move_data().target(),
user,
data.target_side(),
data.target_index(),
data.used_move().read().move_data().target(),
choice.user().read().deref(),
) {
return false;
}
}
true
}
pub fn try_set_choice(&mut self, choice: TurnChoice<'own, 'library>) -> PkmnResult<bool> {
if !self.can_use(&choice) {
return Ok(false);
}
if !choice.user().read().is_on_battlefield() {
return Ok(false);
}
let side = choice.user().read().get_battle_side_index();
if side.is_none() {
return Ok(false);
}
self.sides[side.unwrap() as usize].set_choice(choice);
self.check_choices_set_and_run()?;
Ok(true)
}
fn check_choices_set_and_run(&mut self) -> PkmnResult<()> {
for side in &self.sides {
if !side.all_choices_set() {
return Ok(());
}
if !side.all_slots_filled() {
return Ok(());
}
}
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 in side.choices() {
if choice.is_none() {
panic!("Choice was none, but all choices were set? Logic error.");
}
let mut choice_guard = choice.as_ref().unwrap().write();
let c = choice_guard.deref();
if let TurnChoice::Move(data) = c {
let mut change_priority = data.priority();
script_hook!(change_priority, c, c, &mut change_priority);
if change_priority != data.priority() {
if let TurnChoice::Move(data) = choice_guard.deref_mut() {
*data.priority_mut() = change_priority;
}
}
}
let mut speed = choice_guard.speed();
let c = choice_guard.deref();
script_hook!(change_speed, c, c, &mut speed);
*choice_guard.speed_mut() = speed;
choice_guard.set_random_value(self.random.get() as u32);
choices.push(choice.as_ref().unwrap().clone());
}
side.reset_choices();
}
self.current_turn += 1;
choices.sort_unstable_by(|a, b| b.read().deref().cmp(a.read().deref()));
self.current_turn_queue = Some(Arc::new(RwLock::new(ChoiceQueue::new(choices))));
{
self.run_turn()?;
}
self.current_turn_queue = None;
self.event_hook.trigger(Event::EndTurn);
let end_time = chrono::Utc::now();
let time = end_time - start_time;
self.last_turn_time = time;
Ok(())
}
}
impl<'a> VolatileScripts<'a> for Battle<'a> {
impl<'own, 'library> VolatileScripts<'own> for Battle<'own, 'library> {
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>> {
&self.volatile_scripts
}
@@ -220,7 +285,7 @@ impl<'a> VolatileScripts<'a> for Battle<'a> {
}
}
impl<'a> ScriptSource<'a> for Battle<'a> {
impl<'own, 'library> ScriptSource<'own> for Battle<'own, 'library> {
fn get_script_count(&self) -> usize {
1
}

View File

@@ -1,12 +1,12 @@
use crate::dynamic_data::models::pokemon_party::PokemonParty;
#[derive(Debug)]
pub struct BattleParty<'a> {
party: &'a PokemonParty<'a>,
pub struct BattleParty<'own, 'library> {
party: &'own PokemonParty<'own, 'library>,
responsible_indices: Vec<(u8, u8)>,
}
impl<'a> BattleParty<'a> {
impl<'own, 'library> BattleParty<'own, 'library> {
pub fn is_responsible_for_index(&self, side: u8, index: u8) -> bool {
for responsible_index in &self.responsible_indices {
if responsible_index.0 == side && responsible_index.1 == index {

View File

@@ -1,6 +1,11 @@
use crate::dynamic_data::models::executing_move::ExecutingMove;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::script_handling::ScriptSource;
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::Mutex;
use std::sync::{Arc, Mutex};
#[derive(Default)]
pub struct BattleRandom {
@@ -27,6 +32,32 @@ impl BattleRandom {
pub fn get_between(&self, min: i32, max: i32) -> i32 {
return self.get_rng().lock().unwrap().get_between(min, max);
}
pub fn effect_chance(
&self,
mut chance: f32,
executing_move: &ExecutingMove,
target: &Arc<RwLock<Pokemon>>,
hit_number: u8,
) -> bool {
script_hook!(
change_effect_chance,
executing_move,
executing_move,
target,
hit_number,
&mut chance
);
script_hook_on_lock!(
change_incoming_effect_chance,
target,
executing_move,
target,
hit_number,
&mut chance
);
self.get_rng().lock().unwrap().get_float() < (chance / 100.0)
}
}
impl Debug for BattleRandom {

View File

@@ -13,22 +13,22 @@ use std::ops::Deref;
use std::sync::{Arc, Weak};
#[derive(Debug)]
pub struct BattleSide<'a> {
pub struct BattleSide<'own, 'library> {
index: u8,
pokemon_per_side: u8,
pokemon: Vec<Option<Arc<RwLock<Pokemon<'a>>>>>,
choices: Vec<Option<Arc<TurnChoice<'a>>>>,
pokemon: Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>>,
choices: Vec<Option<Arc<RwLock<TurnChoice<'own, 'library>>>>>,
fillable_slots: Vec<bool>,
choices_set: u8,
battle: Weak<RwLock<Battle<'a>>>,
battle: Weak<RwLock<Battle<'own, 'library>>>,
has_fled_battle: bool,
volatile_scripts: Arc<RwLock<ScriptSet>>,
script_source_data: RwLock<ScriptSourceData>,
}
impl<'a> BattleSide<'a> {
pub fn new(index: u8, battle: Weak<RwLock<Battle<'a>>>, pokemon_per_side: u8) -> Self {
impl<'own, 'library> BattleSide<'own, 'library> {
pub fn new(index: u8, battle: Weak<RwLock<Battle<'own, 'library>>>, pokemon_per_side: u8) -> Self {
let mut pokemon = Vec::with_capacity(pokemon_per_side as usize);
let mut choices = Vec::with_capacity(pokemon_per_side as usize);
let mut fillable_slots = Vec::with_capacity(pokemon_per_side as usize);
@@ -58,19 +58,23 @@ impl<'a> BattleSide<'a> {
pub fn pokemon_per_side(&self) -> u8 {
self.pokemon_per_side
}
pub fn pokemon(&self) -> &Vec<Option<Arc<RwLock<Pokemon<'a>>>>> {
pub fn pokemon(&self) -> &Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>> {
&self.pokemon
}
pub fn choices(&self) -> &Vec<Option<Arc<TurnChoice<'a>>>> {
pub fn choices(&self) -> &Vec<Option<Arc<RwLock<TurnChoice<'own, 'library>>>>> {
&self.choices
}
pub fn choices_mut(&mut self) -> &mut Vec<Option<Arc<RwLock<TurnChoice<'own, 'library>>>>> {
&mut self.choices
}
pub fn fillable_slots(&self) -> &Vec<bool> {
&self.fillable_slots
}
pub fn choices_set(&self) -> u8 {
self.choices_set
}
pub fn battle(&self) -> &Weak<RwLock<Battle<'a>>> {
pub fn battle(&self) -> &Weak<RwLock<Battle<'own, 'library>>> {
&self.battle
}
pub fn has_fled_battle(&self) -> bool {
@@ -103,11 +107,11 @@ impl<'a> BattleSide<'a> {
true
}
pub fn set_choice(&mut self, choice: TurnChoice<'a>) {
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 pokemon.read().unique_identifier() == choice.user().unique_identifier() {
self.choices[index] = Some(Arc::new(choice));
if std::ptr::eq(pokemon.data_ptr(), choice.user().data_ptr()) {
self.choices[index] = Some(Arc::new(RwLock::new(choice)));
self.choices_set += 1;
return;
}
@@ -115,11 +119,17 @@ impl<'a> BattleSide<'a> {
}
}
pub fn reset_choices(&mut self) {
for i in 0..self.choices.len() {
self.choices[i] = None;
}
}
pub fn force_clear_pokemon(&mut self, index: u8) {
self.pokemon[index as usize] = None;
}
pub fn set_pokemon(&mut self, index: u8, pokemon: Option<Arc<RwLock<Pokemon<'a>>>>) {
pub fn set_pokemon(&mut self, index: u8, pokemon: Option<Arc<RwLock<Pokemon<'own, 'library>>>>) {
let old = &mut self.pokemon[index as usize];
if let Some(old_pokemon) = old {
let mut p = old_pokemon.write();
@@ -163,7 +173,7 @@ impl<'a> BattleSide<'a> {
}
}
pub fn is_pokemon_on_side(&self, pokemon: Arc<Pokemon<'a>>) -> bool {
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() {
return true;
@@ -172,7 +182,7 @@ impl<'a> BattleSide<'a> {
false
}
pub fn mark_slot_as_unfillable(&mut self, pokemon: &Pokemon<'a>) {
pub fn mark_slot_as_unfillable(&mut self, pokemon: &Pokemon<'own, 'library>) {
for (i, slot) in self.pokemon.iter().enumerate() {
if let Some(p) = slot {
if p.read().deref() as *const Pokemon == pokemon as *const Pokemon {
@@ -183,7 +193,7 @@ impl<'a> BattleSide<'a> {
}
}
pub fn is_slot_unfillable(&self, pokemon: Arc<Pokemon<'a>>) -> bool {
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() {
@@ -263,7 +273,7 @@ impl<'a> BattleSide<'a> {
}
}
impl<'a> VolatileScripts<'a> for BattleSide<'a> {
impl<'own, 'library> VolatileScripts<'own> for BattleSide<'own, 'library> {
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>> {
&self.volatile_scripts
}
@@ -278,7 +288,7 @@ impl<'a> VolatileScripts<'a> for BattleSide<'a> {
}
}
impl<'a> ScriptSource<'a> for BattleSide<'a> {
impl<'own, 'library> ScriptSource<'own> for BattleSide<'own, 'library> {
fn get_script_count(&self) -> usize {
self.battle.upgrade().unwrap().read().get_script_count() + 1
}
@@ -293,10 +303,6 @@ impl<'a> ScriptSource<'a> for BattleSide<'a> {
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
self.battle
.upgrade()
.unwrap()
.read()
.collect_scripts(scripts);
self.battle.upgrade().unwrap().read().collect_scripts(scripts);
}
}

View File

@@ -1,3 +1,4 @@
use crate::dynamic_data::flow::target_resolver::TargetList;
use crate::dynamic_data::models::learned_move::LearnedMove;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::script_handling::script::ScriptContainer;
@@ -5,9 +6,9 @@ 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)]
#[derive(Default, Debug)]
pub struct HitData {
critical: bool,
base_power: u8,
@@ -57,24 +58,25 @@ impl HitData {
}
}
pub struct ExecutingMove<'a, 'b> {
#[derive(Debug)]
pub struct ExecutingMove<'own, 'battle, 'library> {
number_of_hits: u8,
hits: Vec<HitData>,
user: &'a Pokemon<'b>,
chosen_move: &'a LearnedMove<'a>,
use_move: &'a MoveData,
user: Arc<RwLock<Pokemon<'battle, 'library>>>,
chosen_move: Arc<RwLock<LearnedMove<'library>>>,
use_move: &'own MoveData,
script: ScriptContainer,
targets: Vec<Option<&'a Pokemon<'b>>>,
targets: &'own TargetList<'battle, 'library>,
script_source_data: RwLock<ScriptSourceData>,
}
impl<'a, 'b> ExecutingMove<'a, 'b> {
impl<'own, 'battle, 'library> ExecutingMove<'own, 'battle, 'library> {
pub fn new(
targets: Vec<Option<&'a Pokemon<'b>>>,
targets: &'own TargetList<'battle, 'library>,
number_of_hits: u8,
user: &'a Pokemon<'b>,
chosen_move: &'a LearnedMove,
use_move: &'a MoveData,
user: Arc<RwLock<Pokemon<'battle, 'library>>>,
chosen_move: Arc<RwLock<LearnedMove<'library>>>,
use_move: &'own MoveData,
script: ScriptContainer,
) -> Self {
let total_hits = number_of_hits as usize * targets.len();
@@ -99,23 +101,28 @@ impl<'a, 'b> ExecutingMove<'a, 'b> {
pub fn number_of_hits(&self) -> u8 {
self.number_of_hits
}
pub fn user(&self) -> &'a Pokemon<'b> {
self.user
pub fn user(&self) -> &Arc<RwLock<Pokemon<'battle, 'library>>> {
&self.user
}
pub fn chosen_move(&self) -> &'a LearnedMove {
self.chosen_move
pub fn chosen_move(&self) -> &Arc<RwLock<LearnedMove<'library>>> {
&self.chosen_move
}
pub fn use_move(&self) -> &'a MoveData {
pub fn use_move(&self) -> &'own MoveData {
self.use_move
}
pub fn script(&self) -> &ScriptContainer {
&self.script
}
pub fn get_hit_data(&self, for_target: &Pokemon<'b>, hit: u8) -> PkmnResult<&HitData> {
pub fn get_hit_data<'func>(
&'func self,
for_target: &'func Arc<RwLock<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.deref(), for_target) {
if std::ptr::eq(target.data_ptr(), for_target.data_ptr()) {
let i = index * self.number_of_hits as usize + hit as usize;
return Ok(&self.hits[i]);
}
@@ -124,29 +131,39 @@ impl<'a, 'b> ExecutingMove<'a, 'b> {
Err(PokemonError::InvalidTargetRequested)
}
pub fn get_target_slice(&self, for_target: &Pokemon<'b>) -> PkmnResult<&[HitData]> {
pub fn is_pokemon_target(&self, pokemon: &Arc<RwLock<Pokemon<'battle, 'library>>>) -> bool {
for target in self.targets.iter().flatten() {
if std::ptr::eq(target.data_ptr(), pokemon.data_ptr()) {
return true;
}
}
false
}
pub(crate) fn get_index_of_target(
&self,
for_target: &Arc<RwLock<Pokemon<'battle, 'library>>>,
) -> PkmnResult<usize> {
for (index, target) in self.targets.iter().enumerate() {
if let Some(target) = target {
if std::ptr::eq(target.deref(), for_target) {
if std::ptr::eq(target.data_ptr(), for_target.data_ptr()) {
let i = index * self.number_of_hits as usize;
return Ok(&self.hits[i..i + self.number_of_hits as usize]);
return Ok(i);
}
}
}
Err(PokemonError::InvalidTargetRequested)
}
pub fn is_pokemon_target(&self, pokemon: &Pokemon<'b>) -> bool {
for target in self.targets.iter().flatten() {
if std::ptr::eq(target.deref(), pokemon) {
return true;
}
}
false
pub(crate) fn get_hit_from_raw_index(&self, index: usize) -> &HitData {
&self.hits[index]
}
pub(crate) fn get_hit_from_raw_index_mut(&mut self, index: usize) -> &mut HitData {
&mut self.hits[index]
}
}
impl<'a, 'b> ScriptSource<'a> for ExecutingMove<'a, 'b> {
impl<'own, 'battle, 'library> ScriptSource<'own> for ExecutingMove<'own, 'battle, 'library> {
fn get_script_count(&self) -> usize {
1
}
@@ -161,6 +178,6 @@ impl<'a, 'b> ScriptSource<'a> for ExecutingMove<'a, 'b> {
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
self.user.get_own_scripts(scripts);
self.user.read().get_own_scripts(scripts);
}
}

View File

@@ -1,8 +1,8 @@
use crate::static_data::MoveData;
#[derive(Debug)]
pub struct LearnedMove<'a> {
move_data: &'a MoveData,
pub struct LearnedMove<'library> {
move_data: &'library MoveData,
max_pp: u8,
remaining_pp: u8,
learn_method: MoveLearnMethod,
@@ -37,4 +37,12 @@ impl<'a> LearnedMove<'a> {
pub fn learn_method(&self) -> MoveLearnMethod {
self.learn_method
}
pub fn try_use(&mut self, amount: u8) -> bool {
if amount > self.remaining_pp {
return false;
}
self.remaining_pp -= amount;
return true;
}
}

View File

@@ -8,3 +8,4 @@ pub mod executing_move;
pub mod learned_move;
pub mod pokemon;
pub mod pokemon_party;
pub mod pokemon_builder;

View File

@@ -23,44 +23,47 @@ use parking_lot::RwLock;
use std::sync::{Arc, Weak};
#[derive(Debug)]
pub struct PokemonBattleData<'a> {
battle: Weak<RwLock<Battle<'a>>>,
pub struct PokemonBattleData<'pokemon, 'library> {
battle: Weak<RwLock<Battle<'pokemon, 'library>>>,
battle_side_index: u8,
index: u8,
on_battle_field: bool,
seen_opponents: Vec<Weak<RwLock<Pokemon<'a>>>>,
seen_opponents: Vec<Weak<RwLock<Pokemon<'pokemon, 'library>>>>,
}
impl<'a> PokemonBattleData<'a> {
pub fn battle(&'a mut self) -> &'a mut Weak<RwLock<Battle<'a>>> {
impl<'pokemon, 'library> PokemonBattleData<'pokemon, 'library> {
pub fn battle(&mut self) -> &mut Weak<RwLock<Battle<'pokemon, 'library>>> {
&mut self.battle
}
pub fn battle_side_index(&self) -> u8 {
self.battle_side_index
}
pub fn on_battle_field(&'a mut self) -> &mut bool {
pub fn on_battle_field(&mut self) -> &mut bool {
&mut self.on_battle_field
}
pub fn seen_opponents(&'a mut self) -> &'a mut Vec<Weak<RwLock<Pokemon<'a>>>> {
pub fn seen_opponents(&mut self) -> &mut Vec<Weak<RwLock<Pokemon<'pokemon, 'library>>>> {
&mut self.seen_opponents
}
}
#[derive(Debug)]
pub struct Pokemon<'a> {
library: &'a DynamicLibrary<'a>,
species: &'a Species<'a>,
form: &'a Form<'a>,
pub struct Pokemon<'own, 'library>
where
'own: 'library,
{
library: &'own DynamicLibrary,
species: &'own Species<'library>,
form: &'own Form<'library>,
display_species: Option<&'a Species<'a>>,
display_form: Option<&'a Form<'a>>,
display_species: Option<&'own Species<'library>>,
display_form: Option<&'own Form<'library>>,
level: LevelInt,
experience: u32,
unique_identifier: u32,
gender: Gender,
coloring: u8,
held_item: Option<&'a Item>,
held_item: Option<&'own Item>,
current_health: u32,
weight: f32,
@@ -71,7 +74,7 @@ pub struct Pokemon<'a> {
boosted_stats: StatisticSet<u32>,
individual_values: ClampedStatisticSet<u8, 0, 31>,
effort_values: ClampedStatisticSet<u8, 0, 252>,
nature: &'a Nature,
nature: &'own Nature,
nickname: Option<String>,
@@ -79,9 +82,9 @@ pub struct Pokemon<'a> {
is_ability_overridden: bool,
override_ability: Option<Ability>,
battle_data: Option<PokemonBattleData<'a>>,
battle_data: Option<PokemonBattleData<'own, 'library>>,
moves: [Option<LearnedMove<'a>>; MAX_MOVES],
moves: [Option<LearnedMove<'library>>; MAX_MOVES],
allowed_experience: bool,
types: Vec<u8>,
@@ -96,18 +99,18 @@ pub struct Pokemon<'a> {
script_source_data: RwLock<ScriptSourceData>,
}
impl<'a> Pokemon<'a> {
impl<'own, 'library> Pokemon<'own, 'library> {
pub fn new(
library: &'a DynamicLibrary,
species: &'a Species,
form: &'a Form,
library: &'own DynamicLibrary,
species: &'own Species,
form: &'own Form,
ability: AbilityIndex,
level: LevelInt,
unique_identifier: u32,
gender: Gender,
coloring: u8,
nature: &StringKey,
) -> Pokemon<'a> {
) -> Self {
// Calculate experience from the level for the specified growth rate.
let experience = library
.static_data()
@@ -121,7 +124,7 @@ impl<'a> Pokemon<'a> {
.natures()
.get_nature(&nature)
.expect("Unknown nature name was given.");
let mut pokemon = Pokemon {
let mut pokemon = Self {
library,
species,
form,
@@ -162,23 +165,23 @@ impl<'a> Pokemon<'a> {
pokemon
}
pub fn library(&self) -> &'a DynamicLibrary<'a> {
pub fn library(&self) -> &'own DynamicLibrary {
self.library
}
pub fn species(&self) -> &'a Species<'a> {
pub fn species(&self) -> &'own Species<'library> {
self.species
}
pub fn form(&self) -> &'a Form<'a> {
pub fn form(&self) -> &'own Form<'library> {
self.form
}
pub fn display_species(&self) -> &'a Species<'a> {
pub fn display_species(&self) -> &'own Species<'library> {
if let Some(v) = self.display_species {
v
} else {
self.species
}
}
pub fn display_form(&self) -> &'a Form<'a> {
pub fn display_form(&self) -> &'own Form<'library> {
if let Some(v) = self.display_form {
v
} else {
@@ -201,7 +204,7 @@ impl<'a> Pokemon<'a> {
pub fn coloring(&self) -> u8 {
self.coloring
}
pub fn held_item(&self) -> Option<&'a Item> {
pub fn held_item(&self) -> Option<&'own Item> {
self.held_item
}
pub fn has_held_item(&self, name: &StringKey) -> bool {
@@ -211,7 +214,7 @@ impl<'a> Pokemon<'a> {
}
false
}
pub fn set_held_item(&mut self, item: &'a Item) {
pub fn set_held_item(&mut self, item: &'own Item) {
self.held_item = Some(item);
}
pub fn remove_held_item(&mut self) {
@@ -221,10 +224,7 @@ impl<'a> Pokemon<'a> {
if self.held_item.is_none() {
return false;
}
let script = self
.library
.load_item_script(self.held_item.unwrap())
.unwrap();
let script = self.library.load_item_script(self.held_item.unwrap()).unwrap();
if script.is_none() {
return false;
}
@@ -276,7 +276,7 @@ impl<'a> Pokemon<'a> {
&self.effort_values
}
pub fn get_battle(&self) -> Option<&Weak<RwLock<Battle<'a>>>> {
pub fn get_battle(&self) -> Option<&Weak<RwLock<Battle<'own, 'library>>>> {
if let Some(data) = &self.battle_data {
Some(&data.battle)
} else {
@@ -305,7 +305,7 @@ impl<'a> Pokemon<'a> {
&self.ability_script
}
pub fn seen_opponents(&self) -> Option<&Vec<Weak<RwLock<Pokemon<'a>>>>> {
pub fn seen_opponents(&self) -> Option<&Vec<Weak<RwLock<Pokemon<'own, 'library>>>>> {
if let Some(data) = &self.battle_data {
Some(&data.seen_opponents)
} else {
@@ -316,7 +316,7 @@ impl<'a> Pokemon<'a> {
self.allowed_experience
}
pub fn nature(&self) -> &'a Nature {
pub fn nature(&self) -> &'own Nature {
self.nature
}
@@ -328,7 +328,7 @@ impl<'a> Pokemon<'a> {
self.boosted_stats = self.library.stat_calculator().calculate_boosted_stats(self);
}
pub fn change_species(&mut self, species: &'a Species, form: &'a Form) {
pub fn change_species(&mut self, species: &'own Species, form: &'own Form) {
self.species = species;
self.form = form;
@@ -368,7 +368,7 @@ impl<'a> Pokemon<'a> {
}
}
pub fn change_form(&mut self, form: &'a Form) {
pub fn change_form(&mut self, form: &'own Form) {
if std::ptr::eq(self.form, form) {
return;
}
@@ -409,10 +409,10 @@ impl<'a> Pokemon<'a> {
if let Some(battle_data) = &self.battle_data {
if let Some(battle) = battle_data.battle.upgrade() {
battle.read().event_hook().trigger(Event::FormChange {
pokemon: self,
form,
})
battle
.read()
.event_hook()
.trigger(Event::FormChange { pokemon: self, form })
}
}
}
@@ -425,7 +425,7 @@ impl<'a> Pokemon<'a> {
self.current_health == 0
}
pub fn set_battle_data(&mut self, battle: Weak<RwLock<Battle<'a>>>, battle_side_index: u8) {
pub fn set_battle_data(&mut self, battle: Weak<RwLock<Battle<'own, 'library>>>, battle_side_index: u8) {
if let Some(battle_data) = &mut self.battle_data {
battle_data.battle = battle;
battle_data.battle_side_index = battle_side_index;
@@ -465,7 +465,7 @@ impl<'a> Pokemon<'a> {
}
}
pub fn mark_opponent_as_seen(&mut self, pokemon: Weak<RwLock<Pokemon<'a>>>) {
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 {
if seen_opponent.ptr_eq(&pokemon) {
@@ -493,14 +493,7 @@ impl<'a> Pokemon<'a> {
new_health,
});
// TODO: register history
script_hook!(
on_damage,
self,
self,
source,
self.current_health,
new_health
);
script_hook!(on_damage, self, self, source, self.current_health, new_health);
}
}
self.current_health = new_health;
@@ -512,10 +505,7 @@ impl<'a> Pokemon<'a> {
pub fn on_faint(&self, source: DamageSource) {
if let Some(battle_data) = &self.battle_data {
if let Some(battle) = battle_data.battle.upgrade() {
battle
.read()
.event_hook()
.trigger(Event::Faint { pokemon: self });
battle.read().event_hook().trigger(Event::Faint { pokemon: self });
script_hook!(on_faint, self, self, source);
script_hook!(on_remove, self,);
}
@@ -536,13 +526,12 @@ impl<'a> Pokemon<'a> {
}
}
impl<'a> ScriptSource<'a> for Pokemon<'a> {
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) = battle_data.battle.upgrade() {
c += battle.read().sides()[battle_data.battle_side_index as usize]
.get_script_count();
c += battle.read().sides()[battle_data.battle_side_index as usize].get_script_count();
}
}
c
@@ -563,14 +552,13 @@ impl<'a> ScriptSource<'a> for Pokemon<'a> {
self.get_own_scripts(scripts);
if let Some(battle_data) = &self.battle_data {
if let Some(battle) = battle_data.battle.upgrade() {
battle.read().sides()[battle_data.battle_side_index as usize]
.collect_scripts(scripts);
battle.read().sides()[battle_data.battle_side_index as usize].collect_scripts(scripts);
}
}
}
}
impl<'a> VolatileScripts<'a> for Pokemon<'a> {
impl<'own, 'library> VolatileScripts<'own> for Pokemon<'own, 'library> {
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>> {
&self.volatile
}

View File

@@ -0,0 +1,40 @@
use crate::defines::LevelInt;
use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::static_data::{AbilityIndex, DataLibrary, Gender};
use crate::StringKey;
pub struct PokemonBuilder<'own> {
library: &'own DynamicLibrary,
species: StringKey,
level: LevelInt,
}
impl<'own> PokemonBuilder<'own> {
pub fn new(library: &'own DynamicLibrary, species: StringKey, level: LevelInt) -> Self {
Self {
library,
species,
level,
}
}
pub fn build<'func>(self) -> Pokemon<'own, 'own> {
let species = self.library.static_data().species().get(&self.species).unwrap();
let form = species.get_default_form();
Pokemon::new(
self.library,
species,
form,
AbilityIndex {
hidden: false,
index: 0,
},
self.level,
0,
Gender::Male,
0,
&"test_nature".into(),
)
}
}

View File

@@ -2,11 +2,11 @@ use crate::dynamic_data::models::pokemon::Pokemon;
use std::sync::{Arc, RwLock};
#[derive(Debug)]
pub struct PokemonParty<'a> {
pokemon: Vec<Option<Arc<RwLock<Pokemon<'a>>>>>,
pub struct PokemonParty<'pokemon, 'library> {
pokemon: Vec<Option<Arc<RwLock<Pokemon<'pokemon, 'library>>>>>,
}
impl<'a> PokemonParty<'a> {
impl<'own, 'library> PokemonParty<'own, 'library> {
pub fn new(size: usize) -> Self {
let mut pokemon = Vec::with_capacity(size);
for _i in 0..size {
@@ -15,7 +15,7 @@ impl<'a> PokemonParty<'a> {
Self { pokemon }
}
pub fn at(&self, index: usize) -> &Option<Arc<RwLock<Pokemon<'a>>>> {
pub fn at(&self, index: usize) -> &Option<Arc<RwLock<Pokemon<'own, 'library>>>> {
let opt = self.pokemon.get(index);
if let Some(v) = opt {
v
@@ -31,8 +31,8 @@ impl<'a> PokemonParty<'a> {
pub fn swap_into(
&mut self,
index: usize,
pokemon: Option<Arc<RwLock<Pokemon<'a>>>>,
) -> Option<Arc<RwLock<Pokemon<'a>>>> {
pokemon: Option<Arc<RwLock<Pokemon<'own, 'library>>>>,
) -> Option<Arc<RwLock<Pokemon<'own, 'library>>>> {
if index >= self.pokemon.len() {
return pokemon;
}
@@ -54,7 +54,7 @@ impl<'a> PokemonParty<'a> {
self.pokemon.len()
}
pub fn pokemon(&self) -> &Vec<Option<Arc<RwLock<Pokemon<'a>>>>> {
pub fn pokemon(&self) -> &Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>> {
&self.pokemon
}