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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user