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

@@ -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
}