A lot more work on a bunch of different parts of the system.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use crate::defines::{LevelInt, MAX_MOVES};
|
||||
use crate::dynamic_data::event_hooks::event_hook::Event;
|
||||
use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary;
|
||||
use crate::dynamic_data::models::battle::Battle;
|
||||
use crate::dynamic_data::models::damage_source::DamageSource;
|
||||
use crate::dynamic_data::models::learned_move::LearnedMove;
|
||||
use crate::dynamic_data::script_handling::script::Script;
|
||||
use crate::dynamic_data::script_handling::script_set::ScriptSet;
|
||||
@@ -8,6 +10,7 @@ use crate::dynamic_data::script_handling::volatile_scripts::VolatileScripts;
|
||||
use crate::dynamic_data::script_handling::ScriptSource;
|
||||
use crate::static_data::items::item::Item;
|
||||
use crate::static_data::natures::Nature;
|
||||
use crate::static_data::species_data::ability::Ability;
|
||||
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;
|
||||
@@ -15,7 +18,7 @@ use crate::static_data::species_data::species::Species;
|
||||
use crate::static_data::statistic_set::{ClampedStatisticSet, StatisticSet};
|
||||
use crate::static_data::statistics::Statistic;
|
||||
use crate::utils::random::Random;
|
||||
use crate::{PkmnResult, ScriptCategory};
|
||||
use crate::{script_hook, PkmnResult, ScriptCategory, StringKey};
|
||||
use std::sync::{Arc, RwLock, Weak};
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -73,6 +76,7 @@ pub struct Pokemon<'a> {
|
||||
|
||||
ability_index: AbilityIndex,
|
||||
is_ability_overridden: bool,
|
||||
override_ability: Option<Ability>,
|
||||
|
||||
battle_data: Option<PokemonBattleData<'a>>,
|
||||
|
||||
@@ -80,8 +84,10 @@ pub struct Pokemon<'a> {
|
||||
allowed_experience: bool,
|
||||
|
||||
types: Vec<u8>,
|
||||
is_egg: bool,
|
||||
is_caught: bool,
|
||||
|
||||
ability_script: Option<Box<dyn Script>>,
|
||||
ability_script: Option<Arc<Box<dyn Script>>>,
|
||||
status_script: Option<Box<dyn Script>>,
|
||||
volatile: Arc<RwLock<ScriptSet>>,
|
||||
}
|
||||
@@ -96,7 +102,7 @@ impl<'a> Pokemon<'a> {
|
||||
unique_identifier: u32,
|
||||
gender: Gender,
|
||||
coloring: u8,
|
||||
nature: String,
|
||||
nature: &StringKey,
|
||||
) -> Pokemon<'a> {
|
||||
// Calculate experience from the level for the specified growth rate.
|
||||
let experience = library
|
||||
@@ -135,16 +141,18 @@ impl<'a> Pokemon<'a> {
|
||||
nickname: None,
|
||||
ability_index: ability,
|
||||
is_ability_overridden: false,
|
||||
override_ability: None,
|
||||
battle_data: None,
|
||||
moves: [None, None, None, None],
|
||||
allowed_experience: false,
|
||||
types: form.types().to_vec(),
|
||||
is_egg: false,
|
||||
is_caught: false,
|
||||
ability_script: None,
|
||||
status_script: None,
|
||||
volatile: Default::default(),
|
||||
};
|
||||
pokemon.recalculate_flat_stats();
|
||||
|
||||
pokemon
|
||||
}
|
||||
|
||||
@@ -190,6 +198,35 @@ impl<'a> Pokemon<'a> {
|
||||
pub fn held_item(&self) -> Option<&'a Item> {
|
||||
self.held_item
|
||||
}
|
||||
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!();
|
||||
}
|
||||
|
||||
pub fn current_health(&self) -> u32 {
|
||||
self.current_health
|
||||
}
|
||||
@@ -249,9 +286,19 @@ impl<'a> Pokemon<'a> {
|
||||
pub fn is_ability_overriden(&self) -> bool {
|
||||
self.is_ability_overridden
|
||||
}
|
||||
pub fn active_ability(&self) -> &Option<Box<dyn Script>> {
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn ability_script(&self) -> &Option<Arc<Box<dyn Script>>> {
|
||||
&self.ability_script
|
||||
}
|
||||
|
||||
pub fn seen_opponents(&self) -> Option<&Vec<Weak<RwLock<Pokemon<'a>>>>> {
|
||||
if let Some(data) = &self.battle_data {
|
||||
Some(&data.seen_opponents)
|
||||
@@ -281,6 +328,7 @@ impl<'a> Pokemon<'a> {
|
||||
|
||||
// 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(
|
||||
@@ -296,6 +344,7 @@ impl<'a> Pokemon<'a> {
|
||||
.unwrap(),
|
||||
);
|
||||
} else {
|
||||
// If we're not in battle, just use a new random.
|
||||
self.gender = species.get_random_gender(&mut Random::default());
|
||||
}
|
||||
}
|
||||
@@ -303,11 +352,78 @@ impl<'a> Pokemon<'a> {
|
||||
else if species.gender_rate() < 0.0 && self.gender != Gender::Genderless {
|
||||
self.gender = Gender::Genderless;
|
||||
}
|
||||
// TODO: Battle Event trigger
|
||||
if let Some(battle_data) = &self.battle_data {
|
||||
if let Some(battle) = battle_data.battle.upgrade() {
|
||||
battle
|
||||
.read()
|
||||
.unwrap()
|
||||
.event_hook()
|
||||
.trigger(Event::SpeciesChange {
|
||||
pokemon: self,
|
||||
species,
|
||||
form,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
self.ability_script = Some(Arc::new(ability_script));
|
||||
// Ensure the ability script gets initialized with the parameters for the ability.
|
||||
self.ability_script()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.on_initialize(self.active_ability().parameters())
|
||||
} else {
|
||||
self.ability_script = None;
|
||||
}
|
||||
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() {
|
||||
battle
|
||||
.read()
|
||||
.unwrap()
|
||||
.event_hook()
|
||||
.trigger(Event::FormChange {
|
||||
pokemon: self,
|
||||
form,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_usable(&self) -> bool {
|
||||
todo!()
|
||||
!self.is_caught && !self.is_egg && !self.is_fainted()
|
||||
}
|
||||
|
||||
pub fn is_fainted(&self) -> bool {
|
||||
self.current_health == 0
|
||||
}
|
||||
|
||||
pub fn set_battle_data(&mut self, battle: Weak<RwLock<Battle<'a>>>, battle_side_index: u8) {
|
||||
@@ -360,6 +476,67 @@ impl<'a> Pokemon<'a> {
|
||||
battle_data.seen_opponents.push(pokemon);
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
battle.read().unwrap().event_hook().trigger(Event::Damage {
|
||||
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()
|
||||
.unwrap()
|
||||
.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()
|
||||
.unwrap()
|
||||
.can_slot_be_filled(battle_data.battle_side_index, battle_data.index)
|
||||
{
|
||||
let mut battle = battle.write().unwrap();
|
||||
let side = &mut battle.sides_mut()[battle_data.battle_side_index as usize];
|
||||
side.mark_slot_as_unfillable(self);
|
||||
}
|
||||
battle.write().unwrap().validate_battle_state();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ScriptSource for Pokemon<'a> {
|
||||
@@ -373,7 +550,7 @@ impl<'a> VolatileScripts<'a> for Pokemon<'a> {
|
||||
&self.volatile
|
||||
}
|
||||
|
||||
fn load_volatile_script(&self, key: &str) -> PkmnResult<Box<dyn Script>> {
|
||||
fn load_volatile_script(&self, key: &StringKey) -> PkmnResult<Option<Box<dyn Script>>> {
|
||||
self.library.load_script(ScriptCategory::Pokemon, key)
|
||||
}
|
||||
}
|
||||
@@ -389,8 +566,8 @@ pub mod test {
|
||||
#[test]
|
||||
fn construct_pokemon() {
|
||||
let lib = dynamic_library::test::build();
|
||||
let species = lib.static_data().species().get("foo").unwrap();
|
||||
let form = species.get_form("default").unwrap();
|
||||
let species = lib.static_data().species().get(&"foo".into()).unwrap();
|
||||
let form = species.get_form(&"default".into()).unwrap();
|
||||
|
||||
let pokemon = Pokemon::new(
|
||||
&lib,
|
||||
@@ -404,9 +581,9 @@ pub mod test {
|
||||
0,
|
||||
Gender::Male,
|
||||
0,
|
||||
"test_nature".to_string(),
|
||||
&"test_nature".into(),
|
||||
);
|
||||
assert_eq!(pokemon.species.name(), "foo");
|
||||
assert_eq!(pokemon.form.name(), "default");
|
||||
assert_eq!(pokemon.species.name(), &"foo".into());
|
||||
assert_eq!(pokemon.form.name(), &"default".into());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user