A lot more work on handling scripts properly.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-06-12 17:57:39 +02:00
parent 56e1237c22
commit 2aadab5a42
11 changed files with 788 additions and 175 deletions

View File

@@ -9,8 +9,10 @@ use crate::dynamic_data::models::battle_side::BattleSide;
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 std::sync::{Arc, RwLock};
use parking_lot::RwLock;
use std::sync::Arc;
#[derive(Debug)]
pub struct Battle<'a> {
@@ -27,7 +29,7 @@ pub struct Battle<'a> {
event_hook: EventHook,
history_holder: Box<HistoryHolder>,
current_turn: u32,
volatile: Arc<RwLock<ScriptSet>>,
volatile_scripts: Arc<RwLock<ScriptSet>>,
last_turn_time: i64,
}
@@ -60,12 +62,12 @@ impl<'a> Battle<'a> {
event_hook: Default::default(),
history_holder: Box::new(HistoryHolder {}),
current_turn: 0,
volatile: Default::default(),
volatile_scripts: Default::default(),
last_turn_time: 0,
}));
for i in 0..number_of_sides {
battle.write().unwrap().sides[i as usize] =
battle.write().sides[i as usize] =
BattleSide::new(i, Arc::downgrade(&battle), pokemon_per_side);
}
battle
@@ -164,10 +166,28 @@ impl<'a> Battle<'a> {
impl<'a> VolatileScripts<'a> for Battle<'a> {
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>> {
&self.volatile
&self.volatile_scripts
}
fn load_volatile_script(&self, key: &StringKey) -> PkmnResult<Option<Box<dyn Script>>> {
self.library.load_script(ScriptCategory::Battle, key)
}
}
impl<'a> ScriptSource<'a> for Battle<'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.volatile_scripts).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
}
}

View File

@@ -6,10 +6,11 @@ use crate::dynamic_data::models::pokemon::Pokemon;
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;
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
use crate::{script_hook, PkmnResult, StringKey};
use parking_lot::RwLock;
use std::ops::Deref;
use std::sync::{Arc, RwLock, Weak};
use std::sync::{Arc, Weak};
#[derive(Debug)]
pub struct BattleSide<'a> {
@@ -85,13 +86,12 @@ impl<'a> BattleSide<'a> {
/// empty, but can't be filled by any party anymore.
pub fn all_slots_filled(&self) -> bool {
for (i, pokemon) in self.pokemon.iter().enumerate() {
if (!pokemon.is_none() || !pokemon.as_ref().unwrap().read().unwrap().is_usable())
if (!pokemon.is_none() || !pokemon.as_ref().unwrap().read().is_usable())
&& self
.battle
.upgrade()
.unwrap()
.read()
.unwrap()
.can_slot_be_filled(self.index, i as u8)
{
return false;
@@ -103,8 +103,7 @@ impl<'a> BattleSide<'a> {
pub fn set_choice(&mut self, choice: TurnChoice<'a>) {
for (index, pokemon_slot) in self.pokemon.iter().enumerate() {
if let Some(pokemon) = pokemon_slot {
if pokemon.read().unwrap().unique_identifier() == choice.user().unique_identifier()
{
if pokemon.read().unique_identifier() == choice.user().unique_identifier() {
self.choices[index] = Some(Arc::new(choice));
self.choices_set += 1;
return;
@@ -120,26 +119,26 @@ impl<'a> BattleSide<'a> {
pub fn set_pokemon(&mut self, index: u8, pokemon: Option<Arc<RwLock<Pokemon<'a>>>>) {
let old = &mut self.pokemon[index as usize];
if let Some(old_pokemon) = old {
let mut p = old_pokemon.write().unwrap();
let mut p = old_pokemon.write();
script_hook!(on_remove, p,);
p.set_on_battlefield(false);
}
self.pokemon[index as usize] = pokemon;
let pokemon = &self.pokemon[index as usize];
if let Some(pokemon_mutex) = pokemon {
let mut pokemon = pokemon_mutex.write().unwrap();
let mut pokemon = pokemon_mutex.write();
pokemon.set_battle_data(self.battle.clone(), self.index);
pokemon.set_on_battlefield(true);
pokemon.set_battle_index(index);
let battle = self.battle.upgrade().unwrap();
let battle = battle.read().unwrap();
let battle = battle.read();
for side in battle.sides() {
if side.index() == self.index {
continue;
}
for opponent_mutex in side.pokemon().iter().flatten() {
let mut opponent = opponent_mutex.write().unwrap();
let mut opponent = opponent_mutex.write();
opponent.mark_opponent_as_seen(Arc::downgrade(pokemon_mutex));
pokemon.mark_opponent_as_seen(Arc::downgrade(opponent_mutex));
}
@@ -152,7 +151,7 @@ impl<'a> BattleSide<'a> {
script_hook!(on_switch_in, pokemon, &pokemon);
} else {
let battle = self.battle.upgrade().unwrap();
let battle = battle.read().unwrap();
let battle = battle.read();
battle.event_hook().trigger(Event::Switch {
side_index: self.index,
index,
@@ -163,7 +162,7 @@ impl<'a> BattleSide<'a> {
pub fn is_pokemon_on_side(&self, pokemon: Arc<Pokemon<'a>>) -> bool {
for p in self.pokemon.iter().flatten() {
if p.read().unwrap().unique_identifier() == pokemon.unique_identifier() {
if p.read().unique_identifier() == pokemon.unique_identifier() {
return true;
}
}
@@ -173,7 +172,7 @@ impl<'a> BattleSide<'a> {
pub fn mark_slot_as_unfillable(&mut self, pokemon: &Pokemon<'a>) {
for (i, slot) in self.pokemon.iter().enumerate() {
if let Some(p) = slot {
if p.read().unwrap().deref() as *const Pokemon == pokemon as *const Pokemon {
if p.read().deref() as *const Pokemon == pokemon as *const Pokemon {
self.fillable_slots[i] = false;
return;
}
@@ -184,7 +183,7 @@ impl<'a> BattleSide<'a> {
pub fn is_slot_unfillable(&self, pokemon: Arc<Pokemon<'a>>) -> bool {
for (i, slot) in self.pokemon.iter().enumerate() {
if let Some(p) = slot {
if p.read().unwrap().unique_identifier() == pokemon.unique_identifier() {
if p.read().unique_identifier() == pokemon.unique_identifier() {
return self.fillable_slots[i];
}
}
@@ -215,7 +214,6 @@ impl<'a> BattleSide<'a> {
.upgrade()
.unwrap()
.read()
.unwrap()
.random()
.get_max(self.pokemon_per_side as i32) as u8
}
@@ -231,7 +229,7 @@ impl<'a> BattleSide<'a> {
}
let battle = self.battle.upgrade().unwrap();
let battle = battle.read().unwrap();
let battle = battle.read();
// Fetch parties for the two indices.
let mut party_a = None;
let mut party_b = None;
@@ -272,14 +270,30 @@ impl<'a> VolatileScripts<'a> for BattleSide<'a> {
.upgrade()
.unwrap()
.read()
.unwrap()
.library()
.load_script(crate::ScriptCategory::Side, key)
}
}
impl<'a> ScriptSource for BattleSide<'a> {
fn get_script_count(&self) {
impl<'a> ScriptSource<'a> for BattleSide<'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.volatile_scripts).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
self.battle
.upgrade()
.unwrap()
.read()
.collect_scripts(scripts);
}
}

View File

@@ -0,0 +1,164 @@
use crate::dynamic_data::models::learned_move::LearnedMove;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::script_handling::script::ScriptContainer;
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
use crate::static_data::MoveData;
use crate::{PkmnResult, PokemonError};
use parking_lot::RwLock;
use std::ops::Deref;
#[derive(Default)]
pub struct HitData {
critical: bool,
base_power: u8,
effectiveness: f32,
damage: u32,
move_type: u8,
has_failed: bool,
}
impl HitData {
pub fn is_critical(&self) -> bool {
self.critical
}
pub fn base_power(&self) -> u8 {
self.base_power
}
pub fn effectiveness(&self) -> f32 {
self.effectiveness
}
pub fn damage(&self) -> u32 {
self.damage
}
pub fn move_type(&self) -> u8 {
self.move_type
}
pub fn has_failed(&self) -> bool {
self.has_failed
}
pub fn set_critical(&mut self, value: bool) {
self.critical = value;
}
pub fn set_base_power(&mut self, value: u8) {
self.base_power = value;
}
pub fn set_effectiveness(&mut self, value: f32) {
self.effectiveness = value;
}
pub fn set_damage(&mut self, value: u32) {
self.damage = value;
}
pub fn set_move_type(&mut self, value: u8) {
self.move_type = value;
}
pub fn fail(&mut self) {
self.has_failed = true;
}
}
pub struct ExecutingMove<'a, 'b> {
number_of_hits: u8,
hits: Vec<HitData>,
user: &'a Pokemon<'b>,
chosen_move: &'a LearnedMove,
use_move: &'a MoveData,
script: ScriptContainer,
targets: Vec<Option<&'a Pokemon<'b>>>,
}
impl<'a, 'b> ExecutingMove<'a, 'b> {
pub fn new(
targets: Vec<Option<&'a Pokemon<'b>>>,
number_of_hits: u8,
user: &'a Pokemon<'b>,
chosen_move: &'a LearnedMove,
use_move: &'a MoveData,
script: ScriptContainer,
) -> Self {
let total_hits = number_of_hits as usize * targets.len();
let mut hits = Vec::with_capacity(total_hits);
for _i in 0..total_hits {
hits.push(HitData::default())
}
Self {
number_of_hits,
hits,
user,
chosen_move,
use_move,
script,
targets,
}
}
pub fn target_count(&self) -> usize {
self.targets.len()
}
pub fn number_of_hits(&self) -> u8 {
self.number_of_hits
}
pub fn user(&self) -> &'a Pokemon<'b> {
self.user
}
pub fn chosen_move(&self) -> &'a LearnedMove {
self.chosen_move
}
pub fn use_move(&self) -> &'a 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> {
for (index, target) in self.targets.iter().enumerate() {
if let Some(target) = target {
if std::ptr::eq(target.deref(), for_target) {
let i = index * self.number_of_hits as usize + hit as usize;
return Ok(&self.hits[i]);
}
}
}
Err(PokemonError::InvalidTargetRequested)
}
pub fn get_target_slice(&self, for_target: &Pokemon<'b>) -> PkmnResult<&[HitData]> {
for (index, target) in self.targets.iter().enumerate() {
if let Some(target) = target {
if std::ptr::eq(target.deref(), for_target) {
let i = index * self.number_of_hits as usize;
return Ok(&self.hits[i..i + self.number_of_hits as usize]);
}
}
}
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
}
}
impl<'a, 'b> ScriptSource<'a> for ExecutingMove<'a, 'b> {
fn get_script_count(&self) -> usize {
1
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
todo!()
}
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
scripts.push((&self.script).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
self.user.get_own_scripts(scripts);
}
}

View File

@@ -4,6 +4,7 @@ pub mod battle_random;
pub mod battle_result;
pub mod battle_side;
pub mod damage_source;
pub mod executing_move;
pub mod learned_move;
pub mod pokemon;
pub mod pokemon_party;

View File

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