2022-06-19 19:34:08 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-04-15 17:33:29 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
2022-06-19 19:34:08 +00:00
|
|
|
use parking_lot::lock_api::RwLockReadGuard;
|
|
|
|
use parking_lot::{RawRwLock, RwLock};
|
|
|
|
|
2022-06-03 14:35:18 +00:00
|
|
|
use crate::dynamic_data::choices::TurnChoice;
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::dynamic_data::event_hooks::Event;
|
2022-06-03 14:35:18 +00:00
|
|
|
use crate::dynamic_data::models::battle::Battle;
|
|
|
|
use crate::dynamic_data::models::pokemon::Pokemon;
|
2022-06-12 15:57:39 +00:00
|
|
|
use crate::dynamic_data::script_handling::{ScriptSource, ScriptSourceData, ScriptWrapper};
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::dynamic_data::Script;
|
|
|
|
use crate::dynamic_data::ScriptSet;
|
2022-07-01 15:07:22 +00:00
|
|
|
use crate::dynamic_data::VolatileScriptsOwner;
|
2023-04-15 17:33:29 +00:00
|
|
|
use crate::{script_hook, PkmnError, StringKey, ValueIdentifiable, ValueIdentifier};
|
2022-06-03 14:35:18 +00:00
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// A side on a battle.
|
2022-06-06 12:43:41 +00:00
|
|
|
#[derive(Debug)]
|
2022-08-20 11:17:20 +00:00
|
|
|
pub struct BattleSide {
|
2022-10-15 15:21:24 +00:00
|
|
|
/// A unique identifier so we know what value this is.
|
|
|
|
identifier: ValueIdentifier,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The index of the side on the battle.
|
2022-06-03 14:35:18 +00:00
|
|
|
index: u8,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The number of Pokemon that can be on the side.
|
2022-06-03 14:35:18 +00:00
|
|
|
pokemon_per_side: u8,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// A list of pokemon currently on the battlefield.
|
2022-08-20 11:17:20 +00:00
|
|
|
pokemon: RwLock<Vec<Option<Arc<Pokemon>>>>,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The currently set choices for all Pokemon on the battlefield. Cleared when the turn starts.
|
2022-08-20 11:17:20 +00:00
|
|
|
choices: RwLock<Vec<Option<TurnChoice>>>,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The slots on the side that can still be filled. Once all slots are set to false, this side
|
|
|
|
/// has lost the battle.
|
2022-06-18 13:52:39 +00:00
|
|
|
fillable_slots: Vec<AtomicBool>,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The number of choices that are set.
|
2022-06-19 10:07:54 +00:00
|
|
|
choices_set: AtomicU8,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// A reference to the battle we're part of.
|
2022-08-20 11:17:20 +00:00
|
|
|
battle: *mut Battle,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Whether or not this side has fled.
|
2022-06-06 11:54:59 +00:00
|
|
|
has_fled_battle: bool,
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The volatile scripts that are attached to the side.
|
2022-06-19 10:07:54 +00:00
|
|
|
volatile_scripts: Arc<ScriptSet>,
|
2022-06-12 16:41:49 +00:00
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Data required for this to be a script source.
|
2022-06-12 16:41:49 +00:00
|
|
|
script_source_data: RwLock<ScriptSourceData>,
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 16:23:35 +00:00
|
|
|
impl BattleSide {
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Instantiates a battle side.
|
2022-06-17 17:53:33 +00:00
|
|
|
pub fn new(index: u8, pokemon_per_side: u8) -> Self {
|
2022-06-03 14:35:18 +00:00
|
|
|
let mut pokemon = Vec::with_capacity(pokemon_per_side as usize);
|
|
|
|
let mut choices = Vec::with_capacity(pokemon_per_side as usize);
|
|
|
|
let mut fillable_slots = Vec::with_capacity(pokemon_per_side as usize);
|
|
|
|
|
2022-06-17 17:53:33 +00:00
|
|
|
for _i in 0..pokemon_per_side {
|
|
|
|
pokemon.push(None);
|
|
|
|
choices.push(None);
|
2022-06-18 13:52:39 +00:00
|
|
|
fillable_slots.push(AtomicBool::new(false));
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
2022-06-18 13:52:39 +00:00
|
|
|
let choices = RwLock::new(choices);
|
2022-06-19 10:07:54 +00:00
|
|
|
let pokemon = RwLock::new(pokemon);
|
2022-06-03 14:35:18 +00:00
|
|
|
|
|
|
|
Self {
|
2022-10-15 15:21:24 +00:00
|
|
|
identifier: Default::default(),
|
2022-06-03 14:35:18 +00:00
|
|
|
index,
|
|
|
|
pokemon_per_side,
|
|
|
|
pokemon,
|
|
|
|
choices,
|
|
|
|
fillable_slots,
|
2022-06-19 10:07:54 +00:00
|
|
|
choices_set: AtomicU8::new(0),
|
2022-06-18 13:52:39 +00:00
|
|
|
battle: std::ptr::null_mut::<Battle>(),
|
2022-06-06 11:54:59 +00:00
|
|
|
has_fled_battle: false,
|
|
|
|
volatile_scripts: Default::default(),
|
2022-06-12 16:41:49 +00:00
|
|
|
script_source_data: Default::default(),
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-17 17:53:33 +00:00
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Set the battle this side belongs to.
|
2022-08-20 11:17:20 +00:00
|
|
|
pub(crate) fn set_battle(&mut self, battle: *mut Battle) {
|
2022-06-17 17:53:33 +00:00
|
|
|
self.battle = battle;
|
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The index of the side on the battle.
|
2022-06-06 12:43:41 +00:00
|
|
|
pub fn index(&self) -> u8 {
|
|
|
|
self.index
|
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The number of Pokemon that can be on the side.
|
2022-06-06 12:43:41 +00:00
|
|
|
pub fn pokemon_per_side(&self) -> u8 {
|
|
|
|
self.pokemon_per_side
|
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// A list of pokemon currently on the battlefield.
|
2022-08-20 11:17:20 +00:00
|
|
|
pub fn pokemon(&self) -> RwLockReadGuard<'_, RawRwLock, Vec<Option<Arc<Pokemon>>>> {
|
2022-06-19 10:07:54 +00:00
|
|
|
self.pokemon.read()
|
2022-06-06 12:43:41 +00:00
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The currently set choices for all Pokemon on the battlefield. Cleared when the turn starts.
|
2022-08-20 11:17:20 +00:00
|
|
|
pub fn choices(&self) -> &RwLock<Vec<Option<TurnChoice>>> {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.choices
|
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The slots on the side that can still be filled. Once all slots are set to false, this side
|
|
|
|
/// has lost the battle.
|
2022-06-18 13:52:39 +00:00
|
|
|
pub fn fillable_slots(&self) -> &Vec<AtomicBool> {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.fillable_slots
|
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The number of choices that are set.
|
2022-06-06 12:43:41 +00:00
|
|
|
pub fn choices_set(&self) -> u8 {
|
2022-06-19 10:07:54 +00:00
|
|
|
self.choices_set.load(Ordering::SeqCst)
|
2022-06-06 12:43:41 +00:00
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// A reference to the battle we're part of.
|
2023-04-15 17:33:29 +00:00
|
|
|
pub fn battle(&self) -> Result<&Battle> {
|
|
|
|
unsafe { self.battle.as_ref().ok_or(anyhow!("Battle was not set, but requested")) }
|
2022-06-06 12:43:41 +00:00
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Whether or not this side has fled.
|
2022-06-06 12:43:41 +00:00
|
|
|
pub fn has_fled_battle(&self) -> bool {
|
|
|
|
self.has_fled_battle
|
|
|
|
}
|
2022-06-30 15:34:57 +00:00
|
|
|
/// The volatile scripts that are attached to the side.
|
2022-06-19 10:07:54 +00:00
|
|
|
pub fn volatile_scripts(&self) -> &Arc<ScriptSet> {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.volatile_scripts
|
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Whether every Pokemon on this side has its choices
|
2022-06-03 14:35:18 +00:00
|
|
|
pub fn all_choices_set(&self) -> bool {
|
2022-06-19 10:07:54 +00:00
|
|
|
self.choices_set() == self.pokemon_per_side
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if there are slots that need to be filled with a new pokemon, that have parties
|
|
|
|
/// responsible for them. Returns false if all slots are filled with usable pokemon, or slots are
|
|
|
|
/// empty, but can't be filled by any party anymore.
|
2023-04-15 17:33:29 +00:00
|
|
|
pub fn all_slots_filled(&self) -> Result<bool> {
|
2022-06-19 10:07:54 +00:00
|
|
|
for (i, pokemon) in self.pokemon.read().iter().enumerate() {
|
2023-04-15 17:33:29 +00:00
|
|
|
match pokemon {
|
|
|
|
Some(pokemon) => {
|
|
|
|
if !pokemon.is_usable() && self.battle()?.can_slot_be_filled(self.index, i as u8) {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
if self.battle()?.can_slot_be_filled(self.index, i as u8) {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-15 17:33:29 +00:00
|
|
|
Ok(true)
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Sets a choice for a Pokemon on this side.
|
2023-04-15 17:33:29 +00:00
|
|
|
pub(crate) fn set_choice(&self, choice: TurnChoice) -> Result<()> {
|
2022-06-19 10:07:54 +00:00
|
|
|
for (index, pokemon_slot) in self.pokemon.read().iter().enumerate() {
|
2022-06-03 14:35:18 +00:00
|
|
|
if let Some(pokemon) = pokemon_slot {
|
2022-06-18 13:52:39 +00:00
|
|
|
if std::ptr::eq(pokemon.deref(), choice.user().deref()) {
|
2023-04-15 17:33:29 +00:00
|
|
|
let len = self.choices.read().len();
|
|
|
|
self.choices
|
|
|
|
.write()
|
|
|
|
.get_mut(index)
|
|
|
|
.ok_or(PkmnError::IndexOutOfBounds { index, len })?
|
|
|
|
.replace(choice);
|
2022-06-19 10:07:54 +00:00
|
|
|
self.choices_set.fetch_add(1, Ordering::SeqCst);
|
2023-04-15 17:33:29 +00:00
|
|
|
return Ok(());
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-15 17:33:29 +00:00
|
|
|
Ok(())
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Resets all choices on this side.
|
2022-06-18 13:52:39 +00:00
|
|
|
pub fn reset_choices(&self) {
|
|
|
|
let len = self.choices.read().len();
|
|
|
|
for i in 0..len {
|
2023-04-15 17:33:29 +00:00
|
|
|
self.choices.write().get_mut(i).take();
|
2022-06-16 15:59:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Forcibly removes a Pokemon from the field.
|
2022-06-03 14:35:18 +00:00
|
|
|
pub fn force_clear_pokemon(&mut self, index: u8) {
|
2023-04-15 17:33:29 +00:00
|
|
|
self.pokemon.write().get_mut(index as usize).take();
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Switches out a spot on the field for a different Pokemon.
|
2023-04-15 17:33:29 +00:00
|
|
|
pub fn set_pokemon(&self, index: u8, pokemon: Option<Arc<Pokemon>>) -> Result<()> {
|
2022-06-19 10:07:54 +00:00
|
|
|
{
|
2023-04-15 17:33:29 +00:00
|
|
|
let mut write_lock = self.pokemon.write();
|
|
|
|
let old = write_lock.get_mut(index as usize).ok_or(PkmnError::IndexOutOfBounds {
|
|
|
|
index: index as usize,
|
|
|
|
len: self.pokemon_per_side as usize,
|
|
|
|
})?;
|
|
|
|
let old = match pokemon {
|
|
|
|
Some(pokemon) => old.replace(pokemon),
|
|
|
|
None => old.take(),
|
|
|
|
};
|
|
|
|
if let Some(old_pokemon) = old.clone() {
|
|
|
|
// We need to drop the lock before calling the hook, as it might try to access the
|
|
|
|
// side again.
|
|
|
|
drop(write_lock);
|
2022-06-19 10:07:54 +00:00
|
|
|
script_hook!(on_remove, old_pokemon,);
|
|
|
|
old_pokemon.set_on_battlefield(false);
|
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
2023-04-15 17:33:29 +00:00
|
|
|
|
|
|
|
let pokemon = {
|
|
|
|
let read_lock = self.pokemon.read();
|
|
|
|
&read_lock
|
|
|
|
.get(index as usize)
|
|
|
|
.ok_or(PkmnError::IndexOutOfBounds {
|
|
|
|
index: index as usize,
|
|
|
|
len: read_lock.len(),
|
|
|
|
})?
|
|
|
|
.clone()
|
|
|
|
};
|
2022-06-18 13:52:39 +00:00
|
|
|
if let Some(pokemon) = pokemon {
|
2022-06-17 17:53:33 +00:00
|
|
|
pokemon.set_battle_data(self.battle, self.index);
|
2022-06-03 14:35:18 +00:00
|
|
|
pokemon.set_on_battlefield(true);
|
|
|
|
pokemon.set_battle_index(index);
|
|
|
|
|
2023-04-15 17:33:29 +00:00
|
|
|
let battle = self.battle()?;
|
2022-06-03 14:35:18 +00:00
|
|
|
for side in battle.sides() {
|
2022-06-06 12:43:41 +00:00
|
|
|
if side.index() == self.index {
|
2022-06-03 14:35:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-18 13:52:39 +00:00
|
|
|
for opponent in side.pokemon().iter().flatten() {
|
|
|
|
opponent.mark_opponent_as_seen(Arc::downgrade(pokemon));
|
|
|
|
pokemon.mark_opponent_as_seen(Arc::downgrade(opponent));
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
battle.event_hook().trigger(Event::Switch {
|
|
|
|
side_index: self.index,
|
|
|
|
index,
|
2022-11-26 10:25:56 +00:00
|
|
|
pokemon: Some(pokemon.clone()),
|
2022-06-03 14:35:18 +00:00
|
|
|
});
|
2022-06-18 14:06:54 +00:00
|
|
|
script_hook!(on_switch_in, pokemon, pokemon);
|
2022-06-03 14:35:18 +00:00
|
|
|
} else {
|
2023-04-15 17:33:29 +00:00
|
|
|
self.battle()?.event_hook().trigger(Event::Switch {
|
2022-06-03 14:35:18 +00:00
|
|
|
side_index: self.index,
|
|
|
|
index,
|
|
|
|
pokemon: None,
|
|
|
|
});
|
|
|
|
}
|
2023-04-15 17:33:29 +00:00
|
|
|
Ok(())
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Checks whether a Pokemon is on the field in this side.
|
2022-08-20 11:17:20 +00:00
|
|
|
pub fn is_pokemon_on_side(&self, pokemon: Arc<Pokemon>) -> bool {
|
2022-06-19 10:07:54 +00:00
|
|
|
for p in self.pokemon.read().iter().flatten() {
|
2022-06-18 13:52:39 +00:00
|
|
|
if std::ptr::eq(p.deref().deref(), pokemon.deref()) {
|
2022-06-03 14:35:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2022-06-06 11:54:59 +00:00
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Marks a slot as unfillable. This happens when no parties are able to fill the slot anymore.
|
|
|
|
/// If this happens, the slot can not be used again.
|
2023-04-15 17:33:29 +00:00
|
|
|
pub(crate) fn mark_slot_as_unfillable(&self, index: u8) -> Result<()> {
|
|
|
|
self.fillable_slots
|
|
|
|
.get(index as usize)
|
|
|
|
.ok_or(PkmnError::IndexOutOfBounds {
|
|
|
|
index: index as usize,
|
|
|
|
len: self.fillable_slots.len(),
|
|
|
|
})?
|
|
|
|
.store(false, Ordering::SeqCst);
|
|
|
|
Ok(())
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Checks whether a slot is unfillable or not.
|
2023-04-15 17:33:29 +00:00
|
|
|
pub fn is_slot_unfillable(&self, pokemon: Arc<Pokemon>) -> Result<bool> {
|
2022-06-19 10:07:54 +00:00
|
|
|
for (i, slot) in self.pokemon.read().iter().enumerate() {
|
2022-06-06 11:54:59 +00:00
|
|
|
if let Some(p) = slot {
|
2022-06-18 13:52:39 +00:00
|
|
|
if std::ptr::eq(p.deref().deref(), pokemon.deref()) {
|
2023-04-15 17:33:29 +00:00
|
|
|
return Ok(self
|
|
|
|
.fillable_slots
|
|
|
|
.get(i)
|
|
|
|
.ok_or(PkmnError::IndexOutOfBounds {
|
|
|
|
index: i,
|
|
|
|
len: self.fillable_slots.len(),
|
|
|
|
})?
|
|
|
|
.load(Ordering::Relaxed));
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-15 17:33:29 +00:00
|
|
|
Ok(false)
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Checks whether the side has been defeated.
|
2022-06-06 11:54:59 +00:00
|
|
|
pub fn is_defeated(&self) -> bool {
|
|
|
|
for fillable_slot in &self.fillable_slots {
|
2022-06-18 13:52:39 +00:00
|
|
|
if fillable_slot.load(Ordering::Relaxed) {
|
2022-06-06 11:54:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Mark the side as fled.
|
2022-06-06 11:54:59 +00:00
|
|
|
pub fn mark_as_fled(&mut self) {
|
|
|
|
self.has_fled_battle = true;
|
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Gets a random Pokemon on the given side.
|
2023-04-15 12:34:42 +00:00
|
|
|
pub fn get_random_creature_index(&self) -> Result<u8> {
|
2022-06-06 11:54:59 +00:00
|
|
|
// TODO: Consider adding parameter to only get index for available creatures.
|
2023-04-15 17:33:29 +00:00
|
|
|
Ok(self.battle()?.random().get_max(self.pokemon_per_side as i32)? as u8)
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 15:34:57 +00:00
|
|
|
/// Swap two Pokemon on a single side around.
|
2023-04-15 17:33:29 +00:00
|
|
|
pub fn swap_positions(&mut self, a: u8, b: u8) -> Result<bool> {
|
2022-06-06 11:54:59 +00:00
|
|
|
// If out of range, don't allow swapping.
|
|
|
|
if a >= self.pokemon_per_side || b >= self.pokemon_per_side {
|
2023-04-15 17:33:29 +00:00
|
|
|
return Ok(false);
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
// If the two indices are the same, don't allow swapping.
|
|
|
|
if a == b {
|
2023-04-15 17:33:29 +00:00
|
|
|
return Ok(false);
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch parties for the two indices.
|
|
|
|
let mut party_a = None;
|
|
|
|
let mut party_b = None;
|
2023-04-15 17:33:29 +00:00
|
|
|
for party in self.battle()?.parties() {
|
2022-06-06 11:54:59 +00:00
|
|
|
if party.is_responsible_for_index(self.index, a) {
|
|
|
|
party_a = Some(party);
|
|
|
|
}
|
|
|
|
if party.is_responsible_for_index(self.index, b) {
|
|
|
|
party_b = Some(party);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If either of the parties does not exist, fail.
|
2023-04-15 17:33:29 +00:00
|
|
|
let party_a = match party_a {
|
|
|
|
Some(party) => party,
|
|
|
|
None => return Ok(false),
|
|
|
|
};
|
|
|
|
let party_b = match party_b {
|
|
|
|
Some(party) => party,
|
|
|
|
None => return Ok(false),
|
|
|
|
};
|
2022-06-06 11:54:59 +00:00
|
|
|
// Don't allow swapping if different parties are responsible for the indices.
|
2023-04-15 17:33:29 +00:00
|
|
|
if party_a.value_identifier() != party_b.value_identifier() {
|
|
|
|
return Ok(false);
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 10:07:54 +00:00
|
|
|
self.pokemon.write().swap(a as usize, b as usize);
|
2023-04-15 17:33:29 +00:00
|
|
|
self.battle()?.event_hook().trigger(Event::Swap {
|
2022-06-06 11:54:59 +00:00
|
|
|
side_index: self.index,
|
|
|
|
index_a: a,
|
|
|
|
index_b: b,
|
|
|
|
});
|
2023-04-15 17:33:29 +00:00
|
|
|
Ok(true)
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-20 11:17:20 +00:00
|
|
|
impl VolatileScriptsOwner for BattleSide {
|
2022-06-19 10:07:54 +00:00
|
|
|
fn volatile_scripts(&self) -> &Arc<ScriptSet> {
|
2022-06-06 11:54:59 +00:00
|
|
|
&self.volatile_scripts
|
|
|
|
}
|
|
|
|
|
2023-04-15 12:34:42 +00:00
|
|
|
fn load_volatile_script(&self, key: &StringKey) -> Result<Option<Arc<dyn Script>>> {
|
2023-04-15 17:33:29 +00:00
|
|
|
self.battle()?
|
2022-06-30 15:34:57 +00:00
|
|
|
.library()
|
2022-09-07 16:01:26 +00:00
|
|
|
.load_script(self.into(), crate::ScriptCategory::Side, key)
|
2022-06-06 11:54:59 +00:00
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 11:17:20 +00:00
|
|
|
impl ScriptSource for BattleSide {
|
2023-04-15 17:33:29 +00:00
|
|
|
fn get_script_count(&self) -> Result<usize> {
|
|
|
|
Ok(self.battle()?.get_script_count()? + 1)
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
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-12 15:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_own_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
|
|
|
|
scripts.push((&self.volatile_scripts).into());
|
|
|
|
}
|
|
|
|
|
2023-04-15 17:33:29 +00:00
|
|
|
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
|
2022-06-12 15:57:39 +00:00
|
|
|
self.get_own_scripts(scripts);
|
2023-04-15 17:33:29 +00:00
|
|
|
self.battle()?.collect_scripts(scripts)
|
2022-06-12 15:57:39 +00:00
|
|
|
}
|
2022-06-03 14:35:18 +00:00
|
|
|
}
|
2022-10-15 15:21:24 +00:00
|
|
|
|
|
|
|
impl ValueIdentifiable for BattleSide {
|
|
|
|
fn value_identifier(&self) -> ValueIdentifier {
|
|
|
|
self.identifier
|
|
|
|
}
|
|
|
|
}
|