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

@@ -204,6 +204,20 @@ pub struct ItemChoice<'user, 'library> {
choice_data: Box<CommonChoiceData<'user, 'library>>,
}
impl<'user, 'library> ItemChoice<'user, 'library> {
pub fn new(user: Arc<RwLock<Pokemon<'user, 'library>>>) -> Self {
Self {
choice_data: Box::new(CommonChoiceData {
user,
speed: 0,
random_value: 0,
has_failed: false,
script_source_data: Default::default(),
}),
}
}
}
impl<'user, 'library> ScriptSource<'user> for ItemChoice<'user, 'library> {
fn get_script_count(&self) -> usize {
0
@@ -225,6 +239,20 @@ pub struct SwitchChoice<'user, 'library> {
choice_data: Box<CommonChoiceData<'user, 'library>>,
}
impl<'user, 'library> SwitchChoice<'user, 'library> {
pub fn new(user: Arc<RwLock<Pokemon<'user, 'library>>>) -> Self {
Self {
choice_data: Box::new(CommonChoiceData {
user,
speed: 0,
random_value: 0,
has_failed: false,
script_source_data: Default::default(),
}),
}
}
}
impl<'user, 'library> ScriptSource<'user> for SwitchChoice<'user, 'library> {
fn get_script_count(&self) -> usize {
0
@@ -246,6 +274,20 @@ pub struct FleeChoice<'user, 'library> {
choice_data: Box<CommonChoiceData<'user, 'library>>,
}
impl<'user, 'library> FleeChoice<'user, 'library> {
pub fn new(user: Arc<RwLock<Pokemon<'user, 'library>>>) -> Self {
Self {
choice_data: Box::new(CommonChoiceData {
user,
speed: 0,
random_value: 0,
has_failed: false,
script_source_data: Default::default(),
}),
}
}
}
impl<'user, 'library> ScriptSource<'user> for FleeChoice<'user, 'library> {
fn get_script_count(&self) -> usize {
0
@@ -267,6 +309,20 @@ pub struct PassChoice<'user, 'library> {
choice_data: Box<CommonChoiceData<'user, 'library>>,
}
impl<'user, 'library> PassChoice<'user, 'library> {
pub fn new(user: Arc<RwLock<Pokemon<'user, 'library>>>) -> Self {
Self {
choice_data: Box::new(CommonChoiceData {
user,
speed: 0,
random_value: 0,
has_failed: false,
script_source_data: Default::default(),
}),
}
}
}
impl<'user, 'library> ScriptSource<'user> for PassChoice<'user, 'library> {
fn get_script_count(&self) -> usize {
0

View File

@@ -43,12 +43,12 @@ pub enum Event<'own, 'battle, 'library> {
},
SpeciesChange {
pokemon: &'own Pokemon<'battle, 'library>,
species: &'own Species<'library>,
form: &'own Form<'library>,
species: &'own Species,
form: &'own Form,
},
FormChange {
pokemon: &'own Pokemon<'battle, 'library>,
form: &'own Form<'library>,
form: &'own Form,
},
Damage {
pokemon: &'own Pokemon<'battle, 'library>,

View File

@@ -83,7 +83,11 @@ impl<'own, 'library> Battle<'own, 'library> {
return Ok(());
}
match choice_guard.deref() {
TurnChoice::Move(..) => self.execute_move_choice(&choice)?,
TurnChoice::Move(..) => {
drop(user);
drop(choice_guard);
self.execute_move_choice(&choice)?
}
TurnChoice::Item(_) => {}
TurnChoice::Switch(_) => {}
TurnChoice::Flee(_) => {}
@@ -100,13 +104,12 @@ impl<'own, 'library> Battle<'own, 'library> {
let choice = write_guard.get_move_turn_data();
let used_move = choice.used_move();
let move_data_lock = used_move.read();
let mut move_data = move_data_lock.move_data();
let move_data = move_data_lock.move_data();
let mut move_name = move_data.name().clone();
script_hook!(change_move, choice, choice, &mut move_name);
if move_name != *move_data.name() {
move_data = self.library().static_data().moves().get(&move_name).unwrap();
// FIXME: also change the script on the choice.
}
let move_data = self.library().static_data().moves().get(&move_name).unwrap();
drop(move_data_lock);
// FIXME: also change the script on the choice if changed;
let target_type = move_data.target();
let targets = resolve_targets(choice.target_side(), choice.target_index(), target_type, self);
@@ -154,8 +157,8 @@ impl<'own, 'library> Battle<'own, 'library> {
fn handle_move_for_target(
&self,
executing_move: &mut ExecutingMove<'_, 'own, '_>,
target: &Arc<RwLock<Pokemon<'own, '_>>>,
executing_move: &mut ExecutingMove<'_, 'own, 'library>,
target: &Arc<RwLock<Pokemon<'own, 'library>>>,
) -> PkmnResult<()> {
{
let mut fail = false;

View File

@@ -40,6 +40,7 @@ pub trait DamageLibrary: std::fmt::Debug {
hit_data: &HitData,
) -> f32;
}
#[derive(Debug)]
pub struct Gen7DamageLibrary {
has_randomness: bool,
@@ -93,8 +94,14 @@ impl DamageLibrary for Gen7DamageLibrary {
}
if self.has_randomness {
let battle = executing_move.user().read().get_battle().unwrap().upgrade().unwrap();
let random_percentage = 85 + battle.read().random().get_between(0, 16);
let random_percentage = 85
+ executing_move
.user()
.read()
.get_battle()
.unwrap()
.random()
.get_between(0, 16);
float_damage = (float_damage * (random_percentage as f32 / 100.0)).floor();
}

View File

@@ -10,14 +10,32 @@ use crate::{PkmnResult, StringKey};
#[derive(Debug)]
pub struct DynamicLibrary {
static_data: StaticData<'static>,
static_data: StaticData,
stat_calculator: BattleStatCalculator,
damage_calculator: Box<dyn DamageLibrary>,
misc_library: Box<dyn MiscLibrary<'static>>,
}
impl<'library> DynamicLibrary {
pub fn static_data(&self) -> &StaticData<'library> {
unsafe impl Sync for DynamicLibrary {}
unsafe impl Send for DynamicLibrary {}
impl DynamicLibrary {
pub fn new(
static_data: StaticData,
stat_calculator: BattleStatCalculator,
damage_calculator: Box<dyn DamageLibrary>,
misc_library: Box<dyn MiscLibrary<'static>>,
) -> Self {
Self {
static_data,
stat_calculator,
damage_calculator,
misc_library,
}
}
pub fn static_data(&self) -> &StaticData {
&self.static_data
}
pub fn stat_calculator(&self) -> &BattleStatCalculator {

View File

@@ -47,14 +47,18 @@ impl<'own, 'library> Battle<'own, 'library> {
number_of_sides: u8,
pokemon_per_side: u8,
random_seed: Option<u128>,
) -> Arc<RwLock<Self>> {
) -> Self {
let random = if let Some(seed) = random_seed {
BattleRandom::new_with_seed(seed)
} else {
BattleRandom::default()
};
let sides = Vec::with_capacity(number_of_sides as usize);
let battle = Arc::new(RwLock::new(Self {
let mut sides = Vec::with_capacity(number_of_sides as usize);
for i in 0..number_of_sides {
sides.push(BattleSide::new(i, pokemon_per_side));
}
let mut battle = Self {
library,
parties,
can_flee,
@@ -71,11 +75,13 @@ impl<'own, 'library> Battle<'own, 'library> {
volatile_scripts: Default::default(),
last_turn_time: chrono::Duration::zero(),
script_source_data: Default::default(),
}));
};
for i in 0..number_of_sides {
battle.write().sides[i as usize] = BattleSide::new(i, Arc::downgrade(&battle), pokemon_per_side);
let ptr: *mut Battle = &mut battle;
for side in &mut battle.sides {
side.set_battle(ptr);
}
battle
}

View File

@@ -1,12 +1,21 @@
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::models::pokemon_party::PokemonParty;
use parking_lot::RwLock;
use std::sync::Arc;
#[derive(Debug)]
pub struct BattleParty<'own, 'library> {
party: &'own PokemonParty<'own, 'library>,
party: Arc<PokemonParty<'own, 'library>>,
responsible_indices: Vec<(u8, u8)>,
}
impl<'own, 'library> BattleParty<'own, 'library> {
pub fn new(party: Arc<PokemonParty<'own, 'library>>, responsible_indices: Vec<(u8, u8)>) -> Self {
Self {
party,
responsible_indices,
}
}
pub fn is_responsible_for_index(&self, side: u8, index: u8) -> bool {
for responsible_index in &self.responsible_indices {
if responsible_index.0 == side && responsible_index.1 == index {
@@ -18,11 +27,15 @@ impl<'own, 'library> BattleParty<'own, 'library> {
pub fn has_pokemon_not_in_field(&self) -> bool {
for pokemon in self.party.pokemon().iter().flatten() {
let pokemon = pokemon.read().unwrap();
let pokemon = pokemon.read();
if pokemon.is_usable() && !pokemon.is_on_battlefield() {
return true;
}
}
false
}
pub fn get_pokemon(&self, index: usize) -> &Option<Arc<RwLock<Pokemon<'own, 'library>>>> {
self.party.at(index)
}
}

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);
}
}

View File

@@ -3,7 +3,7 @@ use crate::dynamic_data::event_hooks::event_hook::Event;
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::models::learned_move::{LearnedMove, MoveLearnMethod};
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;
@@ -16,7 +16,7 @@ use crate::static_data::species_data::form::Form;
use crate::static_data::species_data::gender::Gender;
use crate::static_data::species_data::species::Species;
use crate::static_data::statistic_set::{ClampedStatisticSet, StatisticSet};
use crate::static_data::statistics::Statistic;
use crate::static_data::DataLibrary;
use crate::utils::random::Random;
use crate::{script_hook, PkmnResult, ScriptCategory, StringKey};
use parking_lot::RwLock;
@@ -24,7 +24,7 @@ use std::sync::{Arc, Weak};
#[derive(Debug)]
pub struct PokemonBattleData<'pokemon, 'library> {
battle: Weak<RwLock<Battle<'pokemon, 'library>>>,
battle: *mut Battle<'pokemon, 'library>,
battle_side_index: u8,
index: u8,
on_battle_field: bool,
@@ -32,9 +32,13 @@ pub struct PokemonBattleData<'pokemon, 'library> {
}
impl<'pokemon, 'library> PokemonBattleData<'pokemon, 'library> {
pub fn battle(&mut self) -> &mut Weak<RwLock<Battle<'pokemon, 'library>>> {
&mut self.battle
pub fn battle_mut(&mut self) -> Option<&mut Battle<'pokemon, 'library>> {
unsafe { self.battle.as_mut() }
}
pub fn battle(&self) -> Option<&Battle<'pokemon, 'library>> {
unsafe { self.battle.as_ref() }
}
pub fn battle_side_index(&self) -> u8 {
self.battle_side_index
}
@@ -52,11 +56,11 @@ where
'own: 'library,
{
library: &'own DynamicLibrary,
species: &'own Species<'library>,
form: &'own Form<'library>,
species: &'own Species,
form: &'own Form,
display_species: Option<&'own Species<'library>>,
display_form: Option<&'own Form<'library>>,
display_species: Option<&'own Species>,
display_form: Option<&'own Form>,
level: LevelInt,
experience: u32,
@@ -84,7 +88,7 @@ where
battle_data: Option<PokemonBattleData<'own, 'library>>,
moves: [Option<LearnedMove<'library>>; MAX_MOVES],
moves: [Option<Arc<RwLock<LearnedMove<'library>>>>; MAX_MOVES],
allowed_experience: bool,
types: Vec<u8>,
@@ -116,14 +120,13 @@ impl<'own, 'library> Pokemon<'own, 'library> {
.static_data()
.growth_rates()
.calculate_experience(species.growth_rate(), level);
let health = form.get_base_stat(Statistic::HP) as u32;
let weight = form.weight();
let height = form.height();
let nature = library
.static_data()
.natures()
.get_nature(&nature)
.expect("Unknown nature name was given.");
.unwrap_or_else(|| panic!("Unknown nature name was given: {}.", &nature));
let mut pokemon = Self {
library,
species,
@@ -136,7 +139,7 @@ impl<'own, 'library> Pokemon<'own, 'library> {
gender,
coloring,
held_item: None,
current_health: health,
current_health: 1,
weight,
height,
stat_boost: Default::default(),
@@ -162,26 +165,29 @@ impl<'own, 'library> Pokemon<'own, 'library> {
script_source_data: Default::default(),
};
pokemon.recalculate_flat_stats();
let health = pokemon.flat_stats().hp();
pokemon.current_health = health;
pokemon
}
pub fn library(&self) -> &'own DynamicLibrary {
self.library
}
pub fn species(&self) -> &'own Species<'library> {
pub fn species(&self) -> &'own Species {
self.species
}
pub fn form(&self) -> &'own Form<'library> {
pub fn form(&self) -> &'own Form {
self.form
}
pub fn display_species(&self) -> &'own Species<'library> {
pub fn display_species(&self) -> &'own Species {
if let Some(v) = self.display_species {
v
} else {
self.species
}
}
pub fn display_form(&self) -> &'own Form<'library> {
pub fn display_form(&self) -> &'own Form {
if let Some(v) = self.display_form {
v
} else {
@@ -254,7 +260,7 @@ impl<'own, 'library> Pokemon<'own, 'library> {
pub fn types(&self) -> &Vec<u8> {
&self.types
}
pub fn learned_moves(&self) -> &[Option<LearnedMove>; MAX_MOVES] {
pub fn learned_moves(&self) -> &[Option<Arc<RwLock<LearnedMove<'library>>>>; MAX_MOVES] {
&self.moves
}
pub fn status(&self) -> &ScriptContainer {
@@ -276,9 +282,9 @@ impl<'own, 'library> Pokemon<'own, 'library> {
&self.effort_values
}
pub fn get_battle(&self) -> Option<&Weak<RwLock<Battle<'own, 'library>>>> {
pub fn get_battle(&self) -> Option<&Battle<'own, 'library>> {
if let Some(data) = &self.battle_data {
Some(&data.battle)
Some(&data.battle().unwrap())
} else {
None
}
@@ -298,7 +304,11 @@ impl<'own, 'library> Pokemon<'own, 'library> {
return v;
}
}
self.form.get_ability(self.ability_index)
self.library
.static_data()
.abilities()
.get(self.form.get_ability(self.ability_index))
.unwrap()
}
pub fn ability_script(&self) -> &ScriptContainer {
@@ -337,17 +347,8 @@ impl<'own, 'library> Pokemon<'own, 'library> {
// If we're in battle, use the battle random for predictability
if self.battle_data.is_some() {
let battle_data = self.battle_data.as_mut().unwrap();
self.gender = species.get_random_gender(
&mut battle_data
.battle
.upgrade()
.unwrap()
.read()
.random()
.get_rng()
.lock()
.unwrap(),
);
self.gender =
species.get_random_gender(&mut battle_data.battle().unwrap().random().get_rng().lock().unwrap());
} else {
// If we're not in battle, just use a new random.
self.gender = species.get_random_gender(&mut Random::default());
@@ -358,8 +359,8 @@ impl<'own, 'library> Pokemon<'own, 'library> {
self.gender = Gender::Genderless;
}
if let Some(battle_data) = &self.battle_data {
if let Some(battle) = battle_data.battle.upgrade() {
battle.read().event_hook().trigger(Event::SpeciesChange {
if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::SpeciesChange {
pokemon: self,
species,
form,
@@ -408,11 +409,8 @@ impl<'own, 'library> Pokemon<'own, 'library> {
// TODO: consider form specific attacks?
if let Some(battle_data) = &self.battle_data {
if let Some(battle) = battle_data.battle.upgrade() {
battle
.read()
.event_hook()
.trigger(Event::FormChange { pokemon: self, form })
if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::FormChange { pokemon: self, form })
}
}
}
@@ -425,7 +423,7 @@ impl<'own, 'library> Pokemon<'own, 'library> {
self.current_health == 0
}
pub fn set_battle_data(&mut self, battle: Weak<RwLock<Battle<'own, 'library>>>, battle_side_index: u8) {
pub fn set_battle_data(&mut self, battle: *mut Battle<'own, 'library>, battle_side_index: u8) {
if let Some(battle_data) = &mut self.battle_data {
battle_data.battle = battle;
battle_data.battle_side_index = battle_side_index;
@@ -485,8 +483,8 @@ impl<'own, 'library> Pokemon<'own, 'library> {
}
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().event_hook().trigger(Event::Damage {
if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::Damage {
pokemon: self,
source,
original_health: self.current_health(),
@@ -502,36 +500,56 @@ impl<'own, 'library> Pokemon<'own, 'library> {
}
}
pub fn on_faint(&self, source: DamageSource) {
if let Some(battle_data) = &self.battle_data {
if let Some(battle) = battle_data.battle.upgrade() {
battle.read().event_hook().trigger(Event::Faint { pokemon: self });
script_hook!(on_faint, self, self, source);
script_hook!(on_remove, self,);
}
// TODO: Experience gain
pub fn on_faint(&mut self, source: DamageSource) {
if self.battle_data.is_some() && self.battle_data.as_ref().unwrap().battle().is_some() {
self.battle_data
.as_ref()
.unwrap()
.battle()
.unwrap()
.event_hook()
.trigger(Event::Faint { pokemon: self });
script_hook!(on_faint, self, self, source);
script_hook!(on_remove, self,);
if let Some(battle) = battle_data.battle.upgrade() {
if !battle
.read()
.can_slot_be_filled(battle_data.battle_side_index, battle_data.index)
{
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().validate_battle_state();
let side_index = self.battle_data.as_ref().unwrap().battle_side_index;
let index = self.battle_data.as_ref().unwrap().index;
if !self
.battle_data
.as_ref()
.unwrap()
.battle()
.unwrap()
.can_slot_be_filled(side_index, index)
{
self.battle_data.as_mut().unwrap().battle_mut().unwrap().sides_mut()[side_index as usize]
.mark_slot_as_unfillable(index);
}
self.battle_data
.as_mut()
.unwrap()
.battle_mut()
.unwrap()
.validate_battle_state();
}
}
pub fn learn_move(&mut self, move_name: &StringKey, learn_method: MoveLearnMethod) {
let move_pos = self.learned_moves().iter().position(|a| a.is_none());
if move_pos.is_none() {
panic!("No more moves with an empty space found.");
}
let move_data = self.library.static_data().moves().get(move_name).unwrap();
self.moves[move_pos.unwrap()] = Some(Arc::new(RwLock::new(LearnedMove::new(move_data, learn_method))));
}
}
impl<'own, 'library> ScriptSource<'own> for Pokemon<'own, 'library> {
fn get_script_count(&self) -> usize {
let mut c = 3;
if let Some(battle_data) = &self.battle_data {
if let Some(battle) = battle_data.battle.upgrade() {
c += battle.read().sides()[battle_data.battle_side_index as usize].get_script_count();
if let Some(battle) = battle_data.battle() {
c += battle.sides()[battle_data.battle_side_index as usize].get_script_count();
}
}
c
@@ -551,8 +569,8 @@ impl<'own, 'library> ScriptSource<'own> for Pokemon<'own, 'library> {
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);
if let Some(battle) = battle_data.battle() {
battle.sides()[battle_data.battle_side_index as usize].collect_scripts(scripts);
}
}
}

View File

@@ -1,5 +1,6 @@
use crate::defines::LevelInt;
use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary;
use crate::dynamic_data::models::learned_move::MoveLearnMethod;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::static_data::{AbilityIndex, DataLibrary, Gender};
use crate::StringKey;
@@ -8,6 +9,7 @@ pub struct PokemonBuilder<'own> {
library: &'own DynamicLibrary,
species: StringKey,
level: LevelInt,
learned_moves: Vec<StringKey>,
}
impl<'own> PokemonBuilder<'own> {
@@ -16,13 +18,18 @@ impl<'own> PokemonBuilder<'own> {
library,
species,
level,
learned_moves: vec![],
}
}
pub fn learn_move(mut self, learned_move: StringKey) -> Self {
self.learned_moves.push(learned_move);
self
}
pub fn build<'func>(self) -> Pokemon<'own, 'own> {
pub fn build(self) -> Pokemon<'own, 'own> {
let species = self.library.static_data().species().get(&self.species).unwrap();
let form = species.get_default_form();
Pokemon::new(
let mut p = Pokemon::new(
self.library,
species,
form,
@@ -34,7 +41,12 @@ impl<'own> PokemonBuilder<'own> {
0,
Gender::Male,
0,
&"test_nature".into(),
)
&"hardy".into(),
);
for learned_move in self.learned_moves {
p.learn_move(&learned_move, MoveLearnMethod::Unknown);
}
p
}
}

View File

@@ -1,5 +1,6 @@
use crate::dynamic_data::models::pokemon::Pokemon;
use std::sync::{Arc, RwLock};
use parking_lot::RwLock;
use std::sync::Arc;
#[derive(Debug)]
pub struct PokemonParty<'pokemon, 'library> {
@@ -15,6 +16,10 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
Self { pokemon }
}
pub fn new_from_vec(pokemon: Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>>) -> Self {
Self { pokemon }
}
pub fn at(&self, index: usize) -> &Option<Arc<RwLock<Pokemon<'own, 'library>>>> {
let opt = self.pokemon.get(index);
if let Some(v) = opt {
@@ -43,7 +48,7 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
pub fn has_usable_pokemon(&self) -> bool {
for pokemon in self.pokemon.iter().flatten() {
if pokemon.read().unwrap().is_usable() {
if pokemon.read().is_usable() {
return true;
}
}

View File

@@ -149,9 +149,11 @@ impl ScriptAggregator {
self.index = index;
let wrapper = unsafe { &self.scripts.as_ref().unwrap()[self.index as usize] };
if let ScriptWrapper::Set(s) = wrapper {
if let Some(..) = s.upgrade() {
self.set_index = 0;
return true;
if let Some(set) = s.upgrade() {
if set.read().count() > 0 {
self.set_index = 0;
return true;
}
}
} else if let ScriptWrapper::Script(script) = wrapper {
if let Some(v) = script.upgrade() {