2021-01-31 16:31:22 +00:00
|
|
|
use crate::defines::{LevelInt, MAX_MOVES};
|
2022-06-11 15:22:46 +00:00
|
|
|
use crate::dynamic_data::event_hooks::event_hook::Event;
|
2021-01-31 16:31:22 +00:00
|
|
|
use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary;
|
|
|
|
use crate::dynamic_data::models::battle::Battle;
|
2022-06-11 15:22:46 +00:00
|
|
|
use crate::dynamic_data::models::damage_source::DamageSource;
|
2021-01-31 16:31:22 +00:00
|
|
|
use crate::dynamic_data::models::learned_move::LearnedMove;
|
2022-06-12 15:57:39 +00:00
|
|
|
use crate::dynamic_data::script_handling::script::{Script, ScriptContainer};
|
2021-01-31 16:31:22 +00:00
|
|
|
use crate::dynamic_data::script_handling::script_set::ScriptSet;
|
2022-06-06 11:54:59 +00:00
|
|
|
use crate::dynamic_data::script_handling::volatile_scripts::VolatileScripts;
|
2022-06-12 15:57:39 +00:00
|
|
|
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
|
2021-01-31 16:31:22 +00:00
|
|
|
use crate::static_data::items::item::Item;
|
2022-06-06 11:54:59 +00:00
|
|
|
use crate::static_data::natures::Nature;
|
2022-06-11 15:22:46 +00:00
|
|
|
use crate::static_data::species_data::ability::Ability;
|
2021-01-31 16:31:22 +00:00
|
|
|
use crate::static_data::species_data::ability_index::AbilityIndex;
|
|
|
|
use crate::static_data::species_data::form::Form;
|
|
|
|
use crate::static_data::species_data::gender::Gender;
|
|
|
|
use crate::static_data::species_data::species::Species;
|
2022-06-06 11:54:59 +00:00
|
|
|
use crate::static_data::statistic_set::{ClampedStatisticSet, StatisticSet};
|
2021-01-31 16:31:22 +00:00
|
|
|
use crate::static_data::statistics::Statistic;
|
2022-06-03 14:35:18 +00:00
|
|
|
use crate::utils::random::Random;
|
2022-06-11 15:22:46 +00:00
|
|
|
use crate::{script_hook, PkmnResult, ScriptCategory, StringKey};
|
2022-06-12 15:57:39 +00:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
use std::sync::{Arc, Weak};
|
2021-01-31 16:31:22 +00:00
|
|
|
|
2022-06-03 14:35:18 +00:00
|
|
|
#[derive(Debug)]
|
2021-01-31 16:31:22 +00:00
|
|
|
pub struct PokemonBattleData<'a> {
|
2022-06-04 10:40:32 +00:00
|
|
|
battle: Weak<RwLock<Battle<'a>>>,
|
2022-06-03 14:35:18 +00:00
|
|
|
battle_side_index: u8,
|
|
|
|
index: u8,
|
2021-01-31 16:31:22 +00:00
|
|
|
on_battle_field: bool,
|
2022-06-06 14:24:13 +00:00
|
|
|
seen_opponents: Vec<Weak<RwLock<Pokemon<'a>>>>,
|
2021-01-31 16:31:22 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 14:35:18 +00:00
|
|
|
impl<'a> PokemonBattleData<'a> {
|
2022-06-04 10:40:32 +00:00
|
|
|
pub fn battle(&'a mut self) -> &'a mut Weak<RwLock<Battle<'a>>> {
|
2022-06-03 14:35:18 +00:00
|
|
|
&mut self.battle
|
|
|
|
}
|
|
|
|
pub fn battle_side_index(&self) -> u8 {
|
|
|
|
self.battle_side_index
|
|
|
|
}
|
|
|
|
pub fn on_battle_field(&'a mut self) -> &mut bool {
|
|
|
|
&mut self.on_battle_field
|
|
|
|
}
|
2022-06-06 14:24:13 +00:00
|
|
|
pub fn seen_opponents(&'a mut self) -> &'a mut Vec<Weak<RwLock<Pokemon<'a>>>> {
|
2022-06-03 14:35:18 +00:00
|
|
|
&mut self.seen_opponents
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 12:43:41 +00:00
|
|
|
#[derive(Debug)]
|
2021-01-31 16:31:22 +00:00
|
|
|
pub struct Pokemon<'a> {
|
|
|
|
library: &'a DynamicLibrary<'a>,
|
|
|
|
species: &'a Species<'a>,
|
|
|
|
form: &'a Form<'a>,
|
|
|
|
|
|
|
|
display_species: Option<&'a Species<'a>>,
|
|
|
|
display_form: Option<&'a Form<'a>>,
|
|
|
|
|
|
|
|
level: LevelInt,
|
|
|
|
experience: u32,
|
|
|
|
unique_identifier: u32,
|
|
|
|
gender: Gender,
|
|
|
|
coloring: u8,
|
|
|
|
held_item: Option<&'a Item>,
|
2022-06-06 11:54:59 +00:00
|
|
|
current_health: u32,
|
2021-01-31 16:31:22 +00:00
|
|
|
|
2022-06-03 14:35:18 +00:00
|
|
|
weight: f32,
|
|
|
|
height: f32,
|
|
|
|
|
2022-06-06 11:54:59 +00:00
|
|
|
stat_boost: ClampedStatisticSet<i8, -6, 6>,
|
|
|
|
flat_stats: StatisticSet<u32>,
|
|
|
|
boosted_stats: StatisticSet<u32>,
|
|
|
|
individual_values: ClampedStatisticSet<u8, 0, 31>,
|
|
|
|
effort_values: ClampedStatisticSet<u8, 0, 252>,
|
|
|
|
nature: &'a Nature,
|
2021-01-31 16:31:22 +00:00
|
|
|
|
|
|
|
nickname: Option<String>,
|
|
|
|
|
|
|
|
ability_index: AbilityIndex,
|
|
|
|
is_ability_overridden: bool,
|
2022-06-11 15:22:46 +00:00
|
|
|
override_ability: Option<Ability>,
|
2021-01-31 16:31:22 +00:00
|
|
|
|
|
|
|
battle_data: Option<PokemonBattleData<'a>>,
|
|
|
|
|
2022-06-14 17:11:24 +00:00
|
|
|
moves: [Option<LearnedMove<'a>>; MAX_MOVES],
|
2021-01-31 16:31:22 +00:00
|
|
|
allowed_experience: bool,
|
|
|
|
|
|
|
|
types: Vec<u8>,
|
2022-06-11 15:22:46 +00:00
|
|
|
is_egg: bool,
|
|
|
|
is_caught: bool,
|
2021-01-31 16:31:22 +00:00
|
|
|
|
2022-06-12 15:57:39 +00:00
|
|
|
held_item_trigger_script: ScriptContainer,
|
|
|
|
ability_script: ScriptContainer,
|
|
|
|
status_script: ScriptContainer,
|
2022-06-06 11:54:59 +00:00
|
|
|
volatile: Arc<RwLock<ScriptSet>>,
|
2022-06-12 16:41:49 +00:00
|
|
|
|
|
|
|
script_source_data: RwLock<ScriptSourceData>,
|
2021-01-31 16:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Pokemon<'a> {
|
|
|
|
pub fn new(
|
|
|
|
library: &'a DynamicLibrary,
|
|
|
|
species: &'a Species,
|
|
|
|
form: &'a Form,
|
|
|
|
ability: AbilityIndex,
|
|
|
|
level: LevelInt,
|
|
|
|
unique_identifier: u32,
|
|
|
|
gender: Gender,
|
|
|
|
coloring: u8,
|
2022-06-11 15:22:46 +00:00
|
|
|
nature: &StringKey,
|
2021-01-31 16:31:22 +00:00
|
|
|
) -> Pokemon<'a> {
|
|
|
|
// Calculate experience from the level for the specified growth rate.
|
|
|
|
let experience = library
|
|
|
|
.static_data()
|
|
|
|
.growth_rates()
|
|
|
|
.calculate_experience(species.growth_rate(), level);
|
|
|
|
let health = form.get_base_stat(Statistic::HP) as u32;
|
2022-06-06 12:43:41 +00:00
|
|
|
let weight = form.weight();
|
|
|
|
let height = form.height();
|
2022-06-06 11:54:59 +00:00
|
|
|
let nature = library
|
|
|
|
.static_data()
|
|
|
|
.natures()
|
|
|
|
.get_nature(&nature)
|
|
|
|
.expect("Unknown nature name was given.");
|
|
|
|
let mut pokemon = Pokemon {
|
2021-01-31 16:31:22 +00:00
|
|
|
library,
|
|
|
|
species,
|
|
|
|
form,
|
|
|
|
display_species: None,
|
|
|
|
display_form: None,
|
|
|
|
level,
|
|
|
|
experience,
|
|
|
|
unique_identifier,
|
|
|
|
gender,
|
|
|
|
coloring,
|
|
|
|
held_item: None,
|
2022-06-06 11:54:59 +00:00
|
|
|
current_health: health,
|
2022-06-03 14:35:18 +00:00
|
|
|
weight,
|
|
|
|
height,
|
2021-01-31 16:31:22 +00:00
|
|
|
stat_boost: Default::default(),
|
2022-06-06 11:54:59 +00:00
|
|
|
flat_stats: Default::default(),
|
|
|
|
boosted_stats: Default::default(),
|
|
|
|
individual_values: Default::default(),
|
|
|
|
effort_values: Default::default(),
|
|
|
|
nature,
|
2021-01-31 16:31:22 +00:00
|
|
|
nickname: None,
|
|
|
|
ability_index: ability,
|
|
|
|
is_ability_overridden: false,
|
2022-06-11 15:22:46 +00:00
|
|
|
override_ability: None,
|
2021-01-31 16:31:22 +00:00
|
|
|
battle_data: None,
|
|
|
|
moves: [None, None, None, None],
|
|
|
|
allowed_experience: false,
|
|
|
|
types: form.types().to_vec(),
|
2022-06-11 15:22:46 +00:00
|
|
|
is_egg: false,
|
|
|
|
is_caught: false,
|
2022-06-12 15:57:39 +00:00
|
|
|
held_item_trigger_script: ScriptContainer::default(),
|
|
|
|
ability_script: ScriptContainer::default(),
|
|
|
|
status_script: ScriptContainer::default(),
|
2022-06-06 11:54:59 +00:00
|
|
|
volatile: Default::default(),
|
2022-06-12 16:41:49 +00:00
|
|
|
script_source_data: Default::default(),
|
2022-06-06 11:54:59 +00:00
|
|
|
};
|
|
|
|
pokemon.recalculate_flat_stats();
|
|
|
|
pokemon
|
|
|
|
}
|
|
|
|
|
2022-06-06 12:43:41 +00:00
|
|
|
pub fn library(&self) -> &'a DynamicLibrary<'a> {
|
|
|
|
self.library
|
|
|
|
}
|
|
|
|
pub fn species(&self) -> &'a Species<'a> {
|
|
|
|
self.species
|
|
|
|
}
|
2022-06-06 11:54:59 +00:00
|
|
|
pub fn form(&self) -> &'a Form<'a> {
|
|
|
|
self.form
|
|
|
|
}
|
2022-06-06 12:43:41 +00:00
|
|
|
pub fn display_species(&self) -> &'a Species<'a> {
|
|
|
|
if let Some(v) = self.display_species {
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
self.species
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn display_form(&self) -> &'a Form<'a> {
|
|
|
|
if let Some(v) = self.display_form {
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
self.form
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn level(&self) -> LevelInt {
|
|
|
|
self.level
|
|
|
|
}
|
|
|
|
pub fn experience(&self) -> u32 {
|
|
|
|
self.experience
|
|
|
|
}
|
|
|
|
pub fn unique_identifier(&self) -> u32 {
|
|
|
|
self.unique_identifier
|
|
|
|
}
|
|
|
|
pub fn gender(&self) -> Gender {
|
|
|
|
self.gender
|
|
|
|
}
|
|
|
|
pub fn coloring(&self) -> u8 {
|
|
|
|
self.coloring
|
|
|
|
}
|
|
|
|
pub fn held_item(&self) -> Option<&'a Item> {
|
|
|
|
self.held_item
|
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
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 {
|
|
|
|
return v.name() == name;
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
pub fn set_held_item(&mut self, item: &'a Item) {
|
|
|
|
self.held_item = Some(item);
|
|
|
|
}
|
|
|
|
pub fn remove_held_item(&mut self) {
|
|
|
|
self.held_item = None;
|
|
|
|
}
|
|
|
|
pub fn consume_held_item(&mut self) -> bool {
|
|
|
|
if self.held_item.is_none() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let script = self
|
|
|
|
.library
|
|
|
|
.load_item_script(self.held_item.unwrap())
|
|
|
|
.unwrap();
|
|
|
|
if script.is_none() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: the entire item use part.
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
|
2022-06-06 12:43:41 +00:00
|
|
|
pub fn current_health(&self) -> u32 {
|
|
|
|
self.current_health
|
|
|
|
}
|
|
|
|
pub fn max_health(&self) -> u32 {
|
|
|
|
self.boosted_stats.hp()
|
|
|
|
}
|
|
|
|
pub fn weight(&self) -> f32 {
|
|
|
|
self.weight
|
|
|
|
}
|
|
|
|
pub fn height(&self) -> f32 {
|
|
|
|
self.height
|
|
|
|
}
|
|
|
|
pub fn nickname(&self) -> &Option<String> {
|
|
|
|
&self.nickname
|
|
|
|
}
|
|
|
|
pub fn real_ability(&self) -> &AbilityIndex {
|
|
|
|
&self.ability_index
|
|
|
|
}
|
|
|
|
pub fn types(&self) -> &Vec<u8> {
|
|
|
|
&self.types
|
|
|
|
}
|
|
|
|
pub fn learned_moves(&self) -> &[Option<LearnedMove>; MAX_MOVES] {
|
|
|
|
&self.moves
|
|
|
|
}
|
2022-06-12 15:57:39 +00:00
|
|
|
pub fn status(&self) -> &ScriptContainer {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.status_script
|
|
|
|
}
|
|
|
|
pub fn flat_stats(&self) -> &StatisticSet<u32> {
|
|
|
|
&self.flat_stats
|
|
|
|
}
|
|
|
|
pub fn boosted_stats(&self) -> &StatisticSet<u32> {
|
|
|
|
&self.boosted_stats
|
|
|
|
}
|
|
|
|
pub fn stat_boost(&self) -> &ClampedStatisticSet<i8, -6, 6> {
|
|
|
|
&self.stat_boost
|
|
|
|
}
|
|
|
|
pub fn individual_values(&self) -> &ClampedStatisticSet<u8, 0, 31> {
|
|
|
|
&self.individual_values
|
|
|
|
}
|
|
|
|
pub fn effort_values(&self) -> &ClampedStatisticSet<u8, 0, 252> {
|
|
|
|
&self.effort_values
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_battle(&self) -> Option<&Weak<RwLock<Battle<'a>>>> {
|
|
|
|
if let Some(data) = &self.battle_data {
|
|
|
|
Some(&data.battle)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn get_battle_side_index(&self) -> Option<u8> {
|
|
|
|
self.battle_data.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)
|
|
|
|
}
|
|
|
|
pub fn is_ability_overriden(&self) -> bool {
|
|
|
|
self.is_ability_overridden
|
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn active_ability(&self) -> &Ability {
|
|
|
|
if self.is_ability_overridden {
|
|
|
|
if let Some(v) = &self.override_ability {
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.form.get_ability(self.ability_index)
|
|
|
|
}
|
|
|
|
|
2022-06-12 15:57:39 +00:00
|
|
|
pub fn ability_script(&self) -> &ScriptContainer {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.ability_script
|
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
|
2022-06-06 14:24:13 +00:00
|
|
|
pub fn seen_opponents(&self) -> Option<&Vec<Weak<RwLock<Pokemon<'a>>>>> {
|
2022-06-06 12:43:41 +00:00
|
|
|
if let Some(data) = &self.battle_data {
|
|
|
|
Some(&data.seen_opponents)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn allowed_experience_gain(&self) -> bool {
|
|
|
|
self.allowed_experience
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:54:59 +00:00
|
|
|
pub fn nature(&self) -> &'a Nature {
|
|
|
|
self.nature
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn recalculate_flat_stats(&mut self) {
|
|
|
|
self.flat_stats = self.library.stat_calculator().calculate_flat_stats(self);
|
|
|
|
self.recalculate_boosted_stats();
|
|
|
|
}
|
|
|
|
pub fn recalculate_boosted_stats(&mut self) {
|
|
|
|
self.boosted_stats = self.library.stat_calculator().calculate_boosted_stats(self);
|
2021-01-31 16:31:22 +00:00
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
|
|
|
|
pub fn change_species(&mut self, species: &'a Species, form: &'a Form) {
|
|
|
|
self.species = species;
|
|
|
|
self.form = form;
|
|
|
|
|
|
|
|
// If the pokemon is genderless, but it's new species is not, we want to set its gender
|
2022-06-06 12:43:41 +00:00
|
|
|
if self.gender != Gender::Genderless && species.gender_rate() < 0.0 {
|
2022-06-11 15:22:46 +00:00
|
|
|
// If we're in battle, use the battle random for predictability
|
2022-06-03 14:35:18 +00:00
|
|
|
if self.battle_data.is_some() {
|
2022-06-04 10:40:32 +00:00
|
|
|
let battle_data = self.battle_data.as_mut().unwrap();
|
2022-06-03 14:35:18 +00:00
|
|
|
self.gender = species.get_random_gender(
|
2022-06-04 10:40:32 +00:00
|
|
|
&mut battle_data
|
2022-06-03 14:35:18 +00:00
|
|
|
.battle
|
|
|
|
.upgrade()
|
|
|
|
.unwrap()
|
2022-06-04 10:40:32 +00:00
|
|
|
.read()
|
2022-06-03 14:35:18 +00:00
|
|
|
.random()
|
2022-06-04 10:40:32 +00:00
|
|
|
.get_rng()
|
|
|
|
.lock()
|
|
|
|
.unwrap(),
|
2022-06-03 14:35:18 +00:00
|
|
|
);
|
|
|
|
} else {
|
2022-06-11 15:22:46 +00:00
|
|
|
// If we're not in battle, just use a new random.
|
2022-06-03 14:35:18 +00:00
|
|
|
self.gender = species.get_random_gender(&mut Random::default());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Else if the new species is genderless, but the pokemon has a gender, make the creature genderless.
|
2022-06-06 12:43:41 +00:00
|
|
|
else if species.gender_rate() < 0.0 && self.gender != Gender::Genderless {
|
2022-06-03 14:35:18 +00:00
|
|
|
self.gender = Gender::Genderless;
|
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
if let Some(battle_data) = &self.battle_data {
|
|
|
|
if let Some(battle) = battle_data.battle.upgrade() {
|
2022-06-12 15:57:39 +00:00
|
|
|
battle.read().event_hook().trigger(Event::SpeciesChange {
|
|
|
|
pokemon: self,
|
|
|
|
species,
|
|
|
|
form,
|
|
|
|
})
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change_form(&mut self, form: &'a Form) {
|
|
|
|
if std::ptr::eq(self.form, form) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.form = form;
|
|
|
|
|
|
|
|
self.types.clear();
|
|
|
|
for t in form.types() {
|
|
|
|
self.types.push(*t);
|
|
|
|
}
|
|
|
|
self.weight = form.weight();
|
|
|
|
self.height = form.height();
|
|
|
|
|
|
|
|
let ability_script = self
|
|
|
|
.library
|
|
|
|
.load_script(ScriptCategory::Ability, self.active_ability().name())
|
|
|
|
.unwrap();
|
|
|
|
if let Some(ability_script) = ability_script {
|
2022-06-12 15:57:39 +00:00
|
|
|
self.ability_script.set(ability_script);
|
2022-06-11 15:22:46 +00:00
|
|
|
// Ensure the ability script gets initialized with the parameters for the ability.
|
|
|
|
self.ability_script()
|
2022-06-12 15:57:39 +00:00
|
|
|
.get()
|
|
|
|
.as_mut()
|
2022-06-11 15:22:46 +00:00
|
|
|
.unwrap()
|
2022-06-12 15:57:39 +00:00
|
|
|
.as_mut()
|
2022-06-11 15:22:46 +00:00
|
|
|
.on_initialize(self.active_ability().parameters())
|
|
|
|
} else {
|
2022-06-12 15:57:39 +00:00
|
|
|
self.ability_script.clear();
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
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 = 0;
|
|
|
|
} else {
|
|
|
|
self.current_health = self.current_health() + diff_health as u32;
|
|
|
|
}
|
|
|
|
// TODO: consider form specific attacks?
|
|
|
|
|
|
|
|
if let Some(battle_data) = &self.battle_data {
|
|
|
|
if let Some(battle) = battle_data.battle.upgrade() {
|
2022-06-12 15:57:39 +00:00
|
|
|
battle.read().event_hook().trigger(Event::FormChange {
|
|
|
|
pokemon: self,
|
|
|
|
form,
|
|
|
|
})
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_usable(&self) -> bool {
|
2022-06-11 15:22:46 +00:00
|
|
|
!self.is_caught && !self.is_egg && !self.is_fainted()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_fainted(&self) -> bool {
|
|
|
|
self.current_health == 0
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 10:40:32 +00:00
|
|
|
pub fn set_battle_data(&mut self, battle: Weak<RwLock<Battle<'a>>>, battle_side_index: u8) {
|
2022-06-03 14:35:18 +00:00
|
|
|
if let Some(battle_data) = &mut self.battle_data {
|
|
|
|
battle_data.battle = battle;
|
|
|
|
battle_data.battle_side_index = battle_side_index;
|
|
|
|
} else {
|
|
|
|
self.battle_data = Some(PokemonBattleData {
|
|
|
|
battle,
|
|
|
|
battle_side_index,
|
|
|
|
index: 0,
|
|
|
|
on_battle_field: 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;
|
|
|
|
if !value {
|
2022-06-12 15:57:39 +00:00
|
|
|
self.volatile.write().clear();
|
2022-06-06 12:43:41 +00:00
|
|
|
self.weight = self.form.weight();
|
|
|
|
self.height = self.form.height();
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_battle_index(&mut self, index: u8) {
|
|
|
|
if let Some(data) = &mut self.battle_data {
|
|
|
|
data.index = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 11:54:59 +00:00
|
|
|
pub fn is_on_battlefield(&self) -> bool {
|
|
|
|
if let Some(data) = &self.battle_data {
|
|
|
|
data.on_battle_field
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 14:24:13 +00:00
|
|
|
pub fn mark_opponent_as_seen(&mut self, pokemon: Weak<RwLock<Pokemon<'a>>>) {
|
2022-06-03 14:35:18 +00:00
|
|
|
if let Some(battle_data) = &mut self.battle_data {
|
2022-06-06 14:24:13 +00:00
|
|
|
for seen_opponent in &battle_data.seen_opponents {
|
|
|
|
if seen_opponent.ptr_eq(&pokemon) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
battle_data.seen_opponents.push(pokemon);
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
|
|
|
|
pub fn damage(&mut self, mut damage: u32, source: DamageSource) {
|
|
|
|
if damage > self.current_health {
|
|
|
|
damage = self.current_health;
|
|
|
|
}
|
|
|
|
if damage == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let new_health = self.current_health() - damage;
|
|
|
|
if let Some(battle_data) = &self.battle_data {
|
|
|
|
if let Some(battle) = battle_data.battle.upgrade() {
|
2022-06-12 15:57:39 +00:00
|
|
|
battle.read().event_hook().trigger(Event::Damage {
|
2022-06-11 15:22:46 +00:00
|
|
|
pokemon: self,
|
|
|
|
source,
|
|
|
|
original_health: self.current_health(),
|
|
|
|
new_health,
|
|
|
|
});
|
|
|
|
// TODO: register history
|
|
|
|
script_hook!(
|
|
|
|
on_damage,
|
|
|
|
self,
|
|
|
|
self,
|
|
|
|
source,
|
|
|
|
self.current_health,
|
|
|
|
new_health
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.current_health = new_health;
|
|
|
|
if self.is_fainted() && damage > 0 {
|
|
|
|
self.on_faint(source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
|
|
|
script_hook!(on_faint, self, self, source);
|
|
|
|
script_hook!(on_remove, self,);
|
|
|
|
}
|
|
|
|
// TODO: Experience gain
|
|
|
|
|
|
|
|
if let Some(battle) = battle_data.battle.upgrade() {
|
|
|
|
if !battle
|
|
|
|
.read()
|
|
|
|
.can_slot_be_filled(battle_data.battle_side_index, battle_data.index)
|
|
|
|
{
|
2022-06-12 15:57:39 +00:00
|
|
|
let mut battle = battle.write();
|
2022-06-11 15:22:46 +00:00
|
|
|
let side = &mut battle.sides_mut()[battle_data.battle_side_index as usize];
|
|
|
|
side.mark_slot_as_unfillable(self);
|
|
|
|
}
|
2022-06-12 15:57:39 +00:00
|
|
|
battle.write().validate_battle_state();
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 15:57:39 +00:00
|
|
|
impl<'a> ScriptSource<'a> for Pokemon<'a> {
|
|
|
|
fn get_script_count(&self) -> usize {
|
2022-06-12 16:41:49 +00:00
|
|
|
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
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
|
2022-06-12 16:41:49 +00:00
|
|
|
&self.script_source_data
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
2022-06-12 15:57:39 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
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) = battle_data.battle.upgrade() {
|
|
|
|
battle.read().sides()[battle_data.battle_side_index as usize]
|
|
|
|
.collect_scripts(scripts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 16:31:22 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 11:54:59 +00:00
|
|
|
impl<'a> VolatileScripts<'a> for Pokemon<'a> {
|
|
|
|
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>> {
|
|
|
|
&self.volatile
|
|
|
|
}
|
|
|
|
|
2022-06-11 15:22:46 +00:00
|
|
|
fn load_volatile_script(&self, key: &StringKey) -> PkmnResult<Option<Box<dyn Script>>> {
|
2022-06-06 11:54:59 +00:00
|
|
|
self.library.load_script(ScriptCategory::Pokemon, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 16:31:22 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
pub mod test {
|
|
|
|
use crate::dynamic_data::libraries::dynamic_library;
|
|
|
|
use crate::dynamic_data::models::pokemon::Pokemon;
|
|
|
|
use crate::static_data::libraries::data_library::DataLibrary;
|
|
|
|
use crate::static_data::species_data::ability_index::AbilityIndex;
|
|
|
|
use crate::static_data::species_data::gender::Gender;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn construct_pokemon() {
|
|
|
|
let lib = dynamic_library::test::build();
|
2022-06-11 15:22:46 +00:00
|
|
|
let species = lib.static_data().species().get(&"foo".into()).unwrap();
|
|
|
|
let form = species.get_form(&"default".into()).unwrap();
|
2021-01-31 16:31:22 +00:00
|
|
|
|
|
|
|
let pokemon = Pokemon::new(
|
|
|
|
&lib,
|
|
|
|
species,
|
|
|
|
form,
|
|
|
|
AbilityIndex {
|
|
|
|
hidden: false,
|
|
|
|
index: 0,
|
|
|
|
},
|
|
|
|
10,
|
|
|
|
0,
|
|
|
|
Gender::Male,
|
|
|
|
0,
|
2022-06-11 15:22:46 +00:00
|
|
|
&"test_nature".into(),
|
2021-01-31 16:31:22 +00:00
|
|
|
);
|
2022-06-11 15:22:46 +00:00
|
|
|
assert_eq!(pokemon.species.name(), &"foo".into());
|
|
|
|
assert_eq!(pokemon.form.name(), &"default".into());
|
2021-01-31 16:31:22 +00:00
|
|
|
}
|
|
|
|
}
|