A lot more work on handling scripts properly.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -4,10 +4,10 @@ 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::{Script, ScriptContainer};
|
||||
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;
|
||||
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
|
||||
use crate::static_data::items::item::Item;
|
||||
use crate::static_data::natures::Nature;
|
||||
use crate::static_data::species_data::ability::Ability;
|
||||
@@ -19,7 +19,8 @@ use crate::static_data::statistic_set::{ClampedStatisticSet, StatisticSet};
|
||||
use crate::static_data::statistics::Statistic;
|
||||
use crate::utils::random::Random;
|
||||
use crate::{script_hook, PkmnResult, ScriptCategory, StringKey};
|
||||
use std::sync::{Arc, RwLock, Weak};
|
||||
use parking_lot::RwLock;
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PokemonBattleData<'a> {
|
||||
@@ -87,8 +88,9 @@ pub struct Pokemon<'a> {
|
||||
is_egg: bool,
|
||||
is_caught: bool,
|
||||
|
||||
ability_script: Option<Arc<Box<dyn Script>>>,
|
||||
status_script: Option<Box<dyn Script>>,
|
||||
held_item_trigger_script: ScriptContainer,
|
||||
ability_script: ScriptContainer,
|
||||
status_script: ScriptContainer,
|
||||
volatile: Arc<RwLock<ScriptSet>>,
|
||||
}
|
||||
|
||||
@@ -148,8 +150,9 @@ impl<'a> Pokemon<'a> {
|
||||
types: form.types().to_vec(),
|
||||
is_egg: false,
|
||||
is_caught: false,
|
||||
ability_script: None,
|
||||
status_script: None,
|
||||
held_item_trigger_script: ScriptContainer::default(),
|
||||
ability_script: ScriptContainer::default(),
|
||||
status_script: ScriptContainer::default(),
|
||||
volatile: Default::default(),
|
||||
};
|
||||
pokemon.recalculate_flat_stats();
|
||||
@@ -251,7 +254,7 @@ impl<'a> Pokemon<'a> {
|
||||
pub fn learned_moves(&self) -> &[Option<LearnedMove>; MAX_MOVES] {
|
||||
&self.moves
|
||||
}
|
||||
pub fn status(&self) -> &Option<Box<dyn Script>> {
|
||||
pub fn status(&self) -> &ScriptContainer {
|
||||
&self.status_script
|
||||
}
|
||||
pub fn flat_stats(&self) -> &StatisticSet<u32> {
|
||||
@@ -295,7 +298,7 @@ impl<'a> Pokemon<'a> {
|
||||
self.form.get_ability(self.ability_index)
|
||||
}
|
||||
|
||||
pub fn ability_script(&self) -> &Option<Arc<Box<dyn Script>>> {
|
||||
pub fn ability_script(&self) -> &ScriptContainer {
|
||||
&self.ability_script
|
||||
}
|
||||
|
||||
@@ -337,7 +340,6 @@ impl<'a> Pokemon<'a> {
|
||||
.upgrade()
|
||||
.unwrap()
|
||||
.read()
|
||||
.unwrap()
|
||||
.random()
|
||||
.get_rng()
|
||||
.lock()
|
||||
@@ -354,15 +356,11 @@ impl<'a> Pokemon<'a> {
|
||||
}
|
||||
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,
|
||||
})
|
||||
battle.read().event_hook().trigger(Event::SpeciesChange {
|
||||
pokemon: self,
|
||||
species,
|
||||
form,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -385,14 +383,16 @@ impl<'a> Pokemon<'a> {
|
||||
.load_script(ScriptCategory::Ability, self.active_ability().name())
|
||||
.unwrap();
|
||||
if let Some(ability_script) = ability_script {
|
||||
self.ability_script = Some(Arc::new(ability_script));
|
||||
self.ability_script.set(ability_script);
|
||||
// Ensure the ability script gets initialized with the parameters for the ability.
|
||||
self.ability_script()
|
||||
.as_ref()
|
||||
.get()
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.as_mut()
|
||||
.on_initialize(self.active_ability().parameters())
|
||||
} else {
|
||||
self.ability_script = None;
|
||||
self.ability_script.clear();
|
||||
}
|
||||
let old_health = self.max_health();
|
||||
self.recalculate_flat_stats();
|
||||
@@ -406,14 +406,10 @@ impl<'a> Pokemon<'a> {
|
||||
|
||||
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,
|
||||
})
|
||||
battle.read().event_hook().trigger(Event::FormChange {
|
||||
pokemon: self,
|
||||
form,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -445,7 +441,7 @@ impl<'a> Pokemon<'a> {
|
||||
if let Some(data) = &mut self.battle_data {
|
||||
data.on_battle_field = value;
|
||||
if !value {
|
||||
self.volatile.write().unwrap().clear();
|
||||
self.volatile.write().clear();
|
||||
self.weight = self.form.weight();
|
||||
self.height = self.form.height();
|
||||
}
|
||||
@@ -487,7 +483,7 @@ impl<'a> Pokemon<'a> {
|
||||
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 {
|
||||
battle.read().event_hook().trigger(Event::Damage {
|
||||
pokemon: self,
|
||||
source,
|
||||
original_health: self.current_health(),
|
||||
@@ -515,7 +511,6 @@ impl<'a> Pokemon<'a> {
|
||||
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);
|
||||
@@ -526,23 +521,43 @@ impl<'a> Pokemon<'a> {
|
||||
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 mut battle = battle.write();
|
||||
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();
|
||||
battle.write().validate_battle_state();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ScriptSource for Pokemon<'a> {
|
||||
fn get_script_count(&self) {
|
||||
impl<'a> ScriptSource<'a> for Pokemon<'a> {
|
||||
fn get_script_count(&self) -> usize {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> VolatileScripts<'a> for Pokemon<'a> {
|
||||
|
||||
Reference in New Issue
Block a user