Further massive amounts of work

This commit is contained in:
2022-06-06 13:54:59 +02:00
parent df662ce6b5
commit ce33ec0649
33 changed files with 848 additions and 80 deletions

View File

@@ -1,9 +1,13 @@
use crate::dynamic_data::choices::TurnChoice;
use crate::dynamic_data::event_hooks::event_hook::Event;
use crate::dynamic_data::models::battle::Battle;
use crate::dynamic_data::models::battle_party::BattleParty;
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::script_hook;
use crate::{script_hook, PkmnResult};
use derive_getters::Getters;
use std::sync::{Arc, RwLock, Weak};
@@ -16,7 +20,8 @@ pub struct BattleSide<'a> {
fillable_slots: Vec<bool>,
choices_set: u8,
battle: Weak<RwLock<Battle<'a>>>,
has_fled: bool,
has_fled_battle: bool,
volatile_scripts: Arc<RwLock<ScriptSet>>,
}
impl<'a> BattleSide<'a> {
@@ -39,7 +44,8 @@ impl<'a> BattleSide<'a> {
fillable_slots,
choices_set: 0,
battle,
has_fled: false,
has_fled_battle: false,
volatile_scripts: Default::default(),
}
}
@@ -136,6 +142,113 @@ impl<'a> BattleSide<'a> {
}
false
}
pub fn mark_slot_as_unfillable(&mut self, pokemon: Arc<Pokemon<'a>>) {
for (i, slot) in self.pokemon.iter().enumerate() {
if let Some(p) = slot {
if p.read().unwrap().unique_identifier() == pokemon.unique_identifier() {
self.fillable_slots[i] = false;
return;
}
}
}
}
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() {
return self.fillable_slots[i];
}
}
}
false
}
pub fn is_defeated(&self) -> bool {
for fillable_slot in &self.fillable_slots {
if *fillable_slot {
return false;
}
}
true
}
pub fn has_fled(&self) -> bool {
self.has_fled_battle
}
pub fn mark_as_fled(&mut self) {
self.has_fled_battle = true;
}
pub fn get_random_creature_index(&self) -> u8 {
// TODO: Consider adding parameter to only get index for available creatures.
self.battle
.upgrade()
.unwrap()
.read()
.unwrap()
.random()
.get_max(self.pokemon_per_side as i32) as u8
}
pub fn swap_positions(&mut self, a: u8, b: u8) -> bool {
// If out of range, don't allow swapping.
if a >= self.pokemon_per_side || b >= self.pokemon_per_side {
return false;
}
// If the two indices are the same, don't allow swapping.
if a == b {
return false;
}
let battle = self.battle.upgrade().unwrap();
let battle = battle.read().unwrap();
// Fetch parties for the two indices.
let mut party_a = None;
let mut party_b = None;
for party in battle.parties() {
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.
if party_a.is_none() || party_b.is_none() {
return false;
}
// Don't allow swapping if different parties are responsible for the indices.
if party_a.unwrap() as *const BattleParty != party_b.unwrap() as *const BattleParty {
return false;
}
self.pokemon.swap(a as usize, b as usize);
battle.event_hook().trigger(Event::Swap {
side_index: self.index,
index_a: a,
index_b: b,
});
true
}
}
impl<'a> VolatileScripts<'a> for BattleSide<'a> {
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>> {
&self.volatile_scripts
}
fn load_volatile_script(&self, key: &str) -> PkmnResult<Box<dyn Script>> {
self.battle
.upgrade()
.unwrap()
.read()
.unwrap()
.library()
.load_script(crate::ScriptCategory::Side, key)
}
}
impl<'a> ScriptSource for BattleSide<'a> {