Further massive amounts of work
This commit is contained in:
@@ -12,8 +12,9 @@ use std::sync::{Arc, RwLock};
|
||||
|
||||
#[derive(Getters, Debug)]
|
||||
pub struct Battle<'a> {
|
||||
#[getter(skip)]
|
||||
library: &'a DynamicLibrary<'a>,
|
||||
parties: Vec<BattleParty>,
|
||||
parties: Vec<BattleParty<'a>>,
|
||||
can_flee: bool,
|
||||
number_of_sides: u8,
|
||||
pokemon_per_side: u8,
|
||||
@@ -33,7 +34,7 @@ pub struct Battle<'a> {
|
||||
impl<'a> Battle<'a> {
|
||||
pub fn new(
|
||||
library: &'a DynamicLibrary<'a>,
|
||||
parties: Vec<BattleParty>,
|
||||
parties: Vec<BattleParty<'a>>,
|
||||
can_flee: bool,
|
||||
number_of_sides: u8,
|
||||
pokemon_per_side: u8,
|
||||
@@ -59,7 +60,7 @@ impl<'a> Battle<'a> {
|
||||
event_hook: Default::default(),
|
||||
history_holder: Box::new(HistoryHolder {}),
|
||||
current_turn: 0,
|
||||
volatile: ScriptSet {},
|
||||
volatile: ScriptSet::default(),
|
||||
last_turn_time: 0,
|
||||
}));
|
||||
|
||||
@@ -77,4 +78,8 @@ impl<'a> Battle<'a> {
|
||||
pub fn can_slot_be_filled(&self) -> bool {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn library(&self) -> &'a DynamicLibrary<'a> {
|
||||
self.library
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,28 @@
|
||||
use crate::dynamic_data::models::pokemon_party::PokemonParty;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BattleParty {}
|
||||
pub struct BattleParty<'a> {
|
||||
party: &'a PokemonParty<'a>,
|
||||
responsible_indices: Vec<(u8, u8)>,
|
||||
}
|
||||
|
||||
impl<'a> BattleParty<'a> {
|
||||
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 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn has_pokemon_not_in_field(&self) -> bool {
|
||||
for pokemon in self.party.pokemon().iter().flatten() {
|
||||
let pokemon = pokemon.read().unwrap();
|
||||
if pokemon.is_usable() && !pokemon.is_on_battlefield() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,16 @@ impl BattleRandom {
|
||||
pub fn get_rng(&self) -> &Mutex<Random> {
|
||||
&self.random
|
||||
}
|
||||
|
||||
pub fn get(&self) -> i32 {
|
||||
return self.get_rng().lock().unwrap().get();
|
||||
}
|
||||
pub fn get_max(&self, max: i32) -> i32 {
|
||||
return self.get_rng().lock().unwrap().get_max(max);
|
||||
}
|
||||
pub fn get_between(&self, min: i32, max: i32) -> i32 {
|
||||
return self.get_rng().lock().unwrap().get_between(min, max);
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for BattleRandom {
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -5,3 +5,4 @@ pub mod battle_result;
|
||||
pub mod battle_side;
|
||||
pub mod learned_move;
|
||||
pub mod pokemon;
|
||||
pub mod pokemon_party;
|
||||
|
||||
@@ -4,18 +4,21 @@ use crate::dynamic_data::models::battle::Battle;
|
||||
use crate::dynamic_data::models::learned_move::LearnedMove;
|
||||
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::static_data::items::item::Item;
|
||||
use crate::static_data::natures::Nature;
|
||||
use crate::static_data::species_data::ability_index::AbilityIndex;
|
||||
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::StatisticSet;
|
||||
use crate::static_data::statistic_set::{ClampedStatisticSet, StatisticSet};
|
||||
use crate::static_data::statistics::Statistic;
|
||||
use crate::utils::random::Random;
|
||||
use crate::{PkmnResult, ScriptCategory};
|
||||
use derive_getters::Getters;
|
||||
use std::collections::HashSet;
|
||||
use std::sync::{RwLock, Weak};
|
||||
use std::sync::{Arc, RwLock, Weak};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PokemonBattleData<'a> {
|
||||
@@ -45,6 +48,7 @@ impl<'a> PokemonBattleData<'a> {
|
||||
pub struct Pokemon<'a> {
|
||||
library: &'a DynamicLibrary<'a>,
|
||||
species: &'a Species<'a>,
|
||||
#[getter(skip)]
|
||||
form: &'a Form<'a>,
|
||||
|
||||
display_species: Option<&'a Species<'a>>,
|
||||
@@ -56,14 +60,18 @@ pub struct Pokemon<'a> {
|
||||
gender: Gender,
|
||||
coloring: u8,
|
||||
held_item: Option<&'a Item>,
|
||||
health: u32,
|
||||
current_health: u32,
|
||||
|
||||
weight: f32,
|
||||
height: f32,
|
||||
|
||||
stat_boost: StatisticSet<i8>,
|
||||
flat_stats: StatisticSet<u16>,
|
||||
boosted_stats: StatisticSet<u16>,
|
||||
stat_boost: ClampedStatisticSet<i8, -6, 6>,
|
||||
flat_stats: StatisticSet<u32>,
|
||||
boosted_stats: StatisticSet<u32>,
|
||||
individual_values: ClampedStatisticSet<u8, 0, 31>,
|
||||
effort_values: ClampedStatisticSet<u8, 0, 252>,
|
||||
#[getter(skip)]
|
||||
nature: &'a Nature,
|
||||
|
||||
nickname: Option<String>,
|
||||
|
||||
@@ -80,7 +88,7 @@ pub struct Pokemon<'a> {
|
||||
|
||||
ability_script: Option<Box<dyn Script>>,
|
||||
status_script: Option<Box<dyn Script>>,
|
||||
volatile: ScriptSet,
|
||||
volatile: Arc<RwLock<ScriptSet>>,
|
||||
}
|
||||
|
||||
impl<'a> Pokemon<'a> {
|
||||
@@ -93,6 +101,7 @@ impl<'a> Pokemon<'a> {
|
||||
unique_identifier: u32,
|
||||
gender: Gender,
|
||||
coloring: u8,
|
||||
nature: String,
|
||||
) -> Pokemon<'a> {
|
||||
// Calculate experience from the level for the specified growth rate.
|
||||
let experience = library
|
||||
@@ -102,7 +111,12 @@ impl<'a> Pokemon<'a> {
|
||||
let health = form.get_base_stat(Statistic::HP) as u32;
|
||||
let weight = *form.weight();
|
||||
let height = *form.height();
|
||||
Pokemon {
|
||||
let nature = library
|
||||
.static_data()
|
||||
.natures()
|
||||
.get_nature(&nature)
|
||||
.expect("Unknown nature name was given.");
|
||||
let mut pokemon = Pokemon {
|
||||
library,
|
||||
species,
|
||||
form,
|
||||
@@ -114,12 +128,15 @@ impl<'a> Pokemon<'a> {
|
||||
gender,
|
||||
coloring,
|
||||
held_item: None,
|
||||
health,
|
||||
current_health: health,
|
||||
weight,
|
||||
height,
|
||||
stat_boost: Default::default(),
|
||||
flat_stats: *form.base_stats(),
|
||||
boosted_stats: *form.base_stats(),
|
||||
flat_stats: Default::default(),
|
||||
boosted_stats: Default::default(),
|
||||
individual_values: Default::default(),
|
||||
effort_values: Default::default(),
|
||||
nature,
|
||||
nickname: None,
|
||||
ability_index: ability,
|
||||
is_ability_overridden: false,
|
||||
@@ -130,8 +147,26 @@ impl<'a> Pokemon<'a> {
|
||||
types: form.types().to_vec(),
|
||||
ability_script: None,
|
||||
status_script: None,
|
||||
volatile: ScriptSet {},
|
||||
}
|
||||
volatile: Default::default(),
|
||||
};
|
||||
pokemon.recalculate_flat_stats();
|
||||
|
||||
pokemon
|
||||
}
|
||||
|
||||
pub fn form(&self) -> &'a Form<'a> {
|
||||
self.form
|
||||
}
|
||||
pub fn nature(&self) -> &'a Nature {
|
||||
self.nature
|
||||
}
|
||||
|
||||
pub fn recalculate_flat_stats(&mut self) {
|
||||
self.flat_stats = self.library.stat_calculator().calculate_flat_stats(self);
|
||||
self.recalculate_boosted_stats();
|
||||
}
|
||||
pub fn recalculate_boosted_stats(&mut self) {
|
||||
self.boosted_stats = self.library.stat_calculator().calculate_boosted_stats(self);
|
||||
}
|
||||
|
||||
pub fn change_species(&mut self, species: &'a Species, form: &'a Form) {
|
||||
@@ -188,7 +223,7 @@ impl<'a> Pokemon<'a> {
|
||||
if let Some(data) = &mut self.battle_data {
|
||||
data.on_battle_field = value;
|
||||
if !value {
|
||||
self.reset_active_scripts();
|
||||
self.volatile.write().unwrap().clear();
|
||||
self.weight = *self.form.weight();
|
||||
self.height = *self.form.height();
|
||||
}
|
||||
@@ -201,15 +236,19 @@ impl<'a> Pokemon<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_on_battlefield(&self) -> bool {
|
||||
if let Some(data) = &self.battle_data {
|
||||
data.on_battle_field
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mark_opponent_as_seen(&mut self, unique_identifier: u32) {
|
||||
if let Some(battle_data) = &mut self.battle_data {
|
||||
battle_data.seen_opponents.insert(unique_identifier);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reset_active_scripts(&mut self) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ScriptSource for Pokemon<'a> {
|
||||
@@ -218,6 +257,16 @@ impl<'a> ScriptSource for Pokemon<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> VolatileScripts<'a> for Pokemon<'a> {
|
||||
fn volatile_scripts(&self) -> &Arc<RwLock<ScriptSet>> {
|
||||
&self.volatile
|
||||
}
|
||||
|
||||
fn load_volatile_script(&self, key: &str) -> PkmnResult<Box<dyn Script>> {
|
||||
self.library.load_script(ScriptCategory::Pokemon, key)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test {
|
||||
use crate::dynamic_data::libraries::dynamic_library;
|
||||
@@ -244,6 +293,7 @@ pub mod test {
|
||||
0,
|
||||
Gender::Male,
|
||||
0,
|
||||
"test_nature".to_string(),
|
||||
);
|
||||
assert_eq!(pokemon.species.name(), "foo");
|
||||
assert_eq!(pokemon.form.name(), "default");
|
||||
|
||||
80
src/dynamic_data/models/pokemon_party.rs
Normal file
80
src/dynamic_data/models/pokemon_party.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
use crate::dynamic_data::models::pokemon::Pokemon;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PokemonParty<'a> {
|
||||
pokemon: Vec<Option<Arc<RwLock<Pokemon<'a>>>>>,
|
||||
}
|
||||
|
||||
impl<'a> PokemonParty<'a> {
|
||||
pub fn new(size: usize) -> Self {
|
||||
let mut pokemon = Vec::with_capacity(size);
|
||||
for _i in 0..size {
|
||||
pokemon.push(None);
|
||||
}
|
||||
Self { pokemon }
|
||||
}
|
||||
|
||||
pub fn at(&self, index: usize) -> &Option<Arc<RwLock<Pokemon<'a>>>> {
|
||||
let opt = self.pokemon.get(index);
|
||||
if let Some(v) = opt {
|
||||
v
|
||||
} else {
|
||||
&None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn switch(&mut self, a: usize, b: usize) {
|
||||
self.pokemon.swap(a, b);
|
||||
}
|
||||
|
||||
pub fn swap_into(
|
||||
&mut self,
|
||||
index: usize,
|
||||
pokemon: Option<Arc<RwLock<Pokemon<'a>>>>,
|
||||
) -> Option<Arc<RwLock<Pokemon<'a>>>> {
|
||||
if index >= self.pokemon.len() {
|
||||
return pokemon;
|
||||
}
|
||||
let old = self.pokemon[index].as_ref().cloned();
|
||||
self.pokemon[index] = pokemon;
|
||||
old
|
||||
}
|
||||
|
||||
pub fn has_usable_pokemon(&self) -> bool {
|
||||
for pokemon in self.pokemon.iter().flatten() {
|
||||
if pokemon.read().unwrap().is_usable() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn length(&self) -> usize {
|
||||
self.pokemon.len()
|
||||
}
|
||||
|
||||
pub fn pokemon(&self) -> &Vec<Option<Arc<RwLock<Pokemon<'a>>>>> {
|
||||
&self.pokemon
|
||||
}
|
||||
|
||||
pub fn pack_party(&mut self) {
|
||||
let mut first_empty = None;
|
||||
let mut i = 0;
|
||||
loop {
|
||||
if self.pokemon[i].is_none() {
|
||||
if first_empty.is_none() {
|
||||
first_empty = Some(i)
|
||||
}
|
||||
} else if first_empty.is_some() {
|
||||
self.pokemon.swap(first_empty.unwrap(), i);
|
||||
i = first_empty.unwrap();
|
||||
first_empty = None;
|
||||
}
|
||||
i += 1;
|
||||
if i >= self.pokemon.len() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user