First chunk of battling is now fully working, along with integration tests! 🎉
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-06-17 19:53:33 +02:00
parent 59cc150643
commit b6ddd1ee13
34 changed files with 1934 additions and 1293 deletions

View File

@@ -9,8 +9,7 @@ use crate::dynamic_data::script_handling::volatile_scripts::VolatileScripts;
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, Weak};
use std::sync::Arc;
#[derive(Debug)]
pub struct BattleSide<'own, 'library> {
@@ -20,7 +19,7 @@ pub struct BattleSide<'own, 'library> {
choices: Vec<Option<Arc<RwLock<TurnChoice<'own, 'library>>>>>,
fillable_slots: Vec<bool>,
choices_set: u8,
battle: Weak<RwLock<Battle<'own, 'library>>>,
battle: *mut Battle<'own, 'library>,
has_fled_battle: bool,
volatile_scripts: Arc<RwLock<ScriptSet>>,
@@ -28,15 +27,15 @@ pub struct BattleSide<'own, 'library> {
}
impl<'own, 'library> BattleSide<'own, 'library> {
pub fn new(index: u8, battle: Weak<RwLock<Battle<'own, 'library>>>, pokemon_per_side: u8) -> Self {
pub fn new(index: u8, pokemon_per_side: u8) -> Self {
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);
for i in 0..pokemon_per_side {
pokemon[i as usize] = None;
choices[i as usize] = None;
fillable_slots[i as usize] = true;
for _i in 0..pokemon_per_side {
pokemon.push(None);
choices.push(None);
fillable_slots.push(true);
}
Self {
@@ -46,12 +45,17 @@ impl<'own, 'library> BattleSide<'own, 'library> {
choices,
fillable_slots,
choices_set: 0,
battle,
battle: 0 as *mut Battle,
has_fled_battle: false,
volatile_scripts: Default::default(),
script_source_data: Default::default(),
}
}
pub(crate) fn set_battle(&mut self, battle: *mut Battle<'own, 'library>) {
self.battle = battle;
}
pub fn index(&self) -> u8 {
self.index
}
@@ -74,8 +78,8 @@ impl<'own, 'library> BattleSide<'own, 'library> {
pub fn choices_set(&self) -> u8 {
self.choices_set
}
pub fn battle(&self) -> &Weak<RwLock<Battle<'own, 'library>>> {
&self.battle
pub fn battle(&self) -> &Battle<'own, 'library> {
unsafe { self.battle.as_ref().unwrap() }
}
pub fn has_fled_battle(&self) -> bool {
self.has_fled_battle
@@ -94,12 +98,7 @@ impl<'own, 'library> BattleSide<'own, 'library> {
pub fn all_slots_filled(&self) -> bool {
for (i, pokemon) in self.pokemon.iter().enumerate() {
if (!pokemon.is_none() || !pokemon.as_ref().unwrap().read().is_usable())
&& self
.battle
.upgrade()
.unwrap()
.read()
.can_slot_be_filled(self.index, i as u8)
&& self.battle().can_slot_be_filled(self.index, i as u8)
{
return false;
}
@@ -140,12 +139,11 @@ impl<'own, 'library> BattleSide<'own, 'library> {
let pokemon = &self.pokemon[index as usize];
if let Some(pokemon_mutex) = pokemon {
let mut pokemon = pokemon_mutex.write();
pokemon.set_battle_data(self.battle.clone(), self.index);
pokemon.set_battle_data(self.battle, self.index);
pokemon.set_on_battlefield(true);
pokemon.set_battle_index(index);
let battle = self.battle.upgrade().unwrap();
let battle = battle.read();
let battle = self.battle();
for side in battle.sides() {
if side.index() == self.index {
continue;
@@ -163,9 +161,7 @@ impl<'own, 'library> BattleSide<'own, 'library> {
});
script_hook!(on_switch_in, pokemon, &pokemon);
} else {
let battle = self.battle.upgrade().unwrap();
let battle = battle.read();
battle.event_hook().trigger(Event::Switch {
self.battle().event_hook().trigger(Event::Switch {
side_index: self.index,
index,
pokemon: None,
@@ -182,15 +178,8 @@ impl<'own, 'library> BattleSide<'own, 'library> {
false
}
pub fn mark_slot_as_unfillable(&mut self, pokemon: &Pokemon<'own, 'library>) {
for (i, slot) in self.pokemon.iter().enumerate() {
if let Some(p) = slot {
if p.read().deref() as *const Pokemon == pokemon as *const Pokemon {
self.fillable_slots[i] = false;
return;
}
}
}
pub fn mark_slot_as_unfillable(&mut self, index: u8) {
self.fillable_slots[index as usize] = false;
}
pub fn is_slot_unfillable(&self, pokemon: Arc<Pokemon<'own, 'library>>) -> bool {
@@ -223,12 +212,7 @@ impl<'own, 'library> BattleSide<'own, 'library> {
pub fn get_random_creature_index(&self) -> u8 {
// TODO: Consider adding parameter to only get index for available creatures.
self.battle
.upgrade()
.unwrap()
.read()
.random()
.get_max(self.pokemon_per_side as i32) as u8
self.battle().random().get_max(self.pokemon_per_side as i32) as u8
}
pub fn swap_positions(&mut self, a: u8, b: u8) -> bool {
@@ -241,12 +225,10 @@ impl<'own, 'library> BattleSide<'own, 'library> {
return false;
}
let battle = self.battle.upgrade().unwrap();
let battle = battle.read();
// Fetch parties for the two indices.
let mut party_a = None;
let mut party_b = None;
for party in battle.parties() {
for party in self.battle().parties() {
if party.is_responsible_for_index(self.index, a) {
party_a = Some(party);
}
@@ -264,7 +246,7 @@ impl<'own, 'library> BattleSide<'own, 'library> {
}
self.pokemon.swap(a as usize, b as usize);
battle.event_hook().trigger(Event::Swap {
self.battle().event_hook().trigger(Event::Swap {
side_index: self.index,
index_a: a,
index_b: b,
@@ -279,18 +261,13 @@ impl<'own, 'library> VolatileScripts<'own> for BattleSide<'own, 'library> {
}
fn load_volatile_script(&self, key: &StringKey) -> PkmnResult<Option<Box<dyn Script>>> {
self.battle
.upgrade()
.unwrap()
.read()
.library()
.load_script(crate::ScriptCategory::Side, key)
self.battle().library().load_script(crate::ScriptCategory::Side, key)
}
}
impl<'own, 'library> ScriptSource<'own> for BattleSide<'own, 'library> {
fn get_script_count(&self) -> usize {
self.battle.upgrade().unwrap().read().get_script_count() + 1
self.battle().get_script_count() + 1
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
@@ -303,6 +280,6 @@ impl<'own, 'library> ScriptSource<'own> for BattleSide<'own, 'library> {
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.get_own_scripts(scripts);
self.battle.upgrade().unwrap().read().collect_scripts(scripts);
self.battle().collect_scripts(scripts);
}
}