Major amounts of progress

This commit is contained in:
2022-06-03 16:35:18 +02:00
parent c194c5d209
commit 310bf857d2
37 changed files with 1558 additions and 29 deletions

View File

@@ -1,2 +1,80 @@
#[derive(Debug)]
pub struct Battle {}
use crate::dynamic_data::event_hooks::event_hook::EventHook;
use crate::dynamic_data::flow::choice_queue::ChoiceQueue;
use crate::dynamic_data::history::history_holder::HistoryHolder;
use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary;
use crate::dynamic_data::models::battle_party::BattleParty;
use crate::dynamic_data::models::battle_random::BattleRandom;
use crate::dynamic_data::models::battle_result::BattleResult;
use crate::dynamic_data::models::battle_side::BattleSide;
use crate::dynamic_data::script_handling::script_set::ScriptSet;
use derive_getters::Getters;
use std::sync::{Arc, Mutex};
#[derive(Getters, Debug)]
pub struct Battle<'a> {
library: &'a DynamicLibrary<'a>,
parties: Vec<BattleParty>,
can_flee: bool,
number_of_sides: u8,
pokemon_per_side: u8,
sides: Vec<BattleSide<'a>>,
#[getter(skip)]
random: BattleRandom,
choice_queue: ChoiceQueue,
has_ended: bool,
result: BattleResult,
event_hook: EventHook,
history_holder: Box<HistoryHolder>,
current_turn: u32,
volatile: ScriptSet,
last_turn_time: i64,
}
impl<'a> Battle<'a> {
pub fn new(
library: &'a DynamicLibrary<'a>,
parties: Vec<BattleParty>,
can_flee: bool,
number_of_sides: u8,
pokemon_per_side: u8,
random_seed: Option<u128>,
) -> Arc<Mutex<Self>> {
let random = if let Some(seed) = random_seed {
BattleRandom::new_with_seed(seed)
} else {
BattleRandom::new()
};
let sides = Vec::with_capacity(number_of_sides as usize);
let battle = Arc::new(Mutex::new(Self {
library,
parties,
can_flee,
number_of_sides,
pokemon_per_side,
sides,
random,
choice_queue: ChoiceQueue {},
has_ended: false,
result: BattleResult::Inconclusive,
event_hook: Default::default(),
history_holder: Box::new(HistoryHolder {}),
current_turn: 0,
volatile: ScriptSet {},
last_turn_time: 0,
}));
for i in 0..number_of_sides {
battle.lock().unwrap().sides[i as usize] =
BattleSide::new(i, Arc::downgrade(&battle), pokemon_per_side);
}
battle
}
pub fn random(&mut self) -> &mut BattleRandom {
&mut self.random
}
pub fn can_slot_be_filled(&self) -> bool {
todo!()
}
}

View File

@@ -0,0 +1,2 @@
#[derive(Debug)]
pub struct BattleParty {}

View File

@@ -0,0 +1,30 @@
use crate::utils::random::Random;
use std::fmt::{Debug, Formatter};
pub struct BattleRandom {
random: Random,
}
impl Debug for BattleRandom {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("BattleRandom").finish()
}
}
impl BattleRandom {
pub fn new() -> Self {
return BattleRandom {
random: Default::default(),
};
}
pub fn new_with_seed(seed: u128) -> Self {
return BattleRandom {
random: Random::new(seed),
};
}
pub fn get_rng(&mut self) -> &mut Random {
&mut self.random
}
}

View File

@@ -0,0 +1,5 @@
#[derive(Debug)]
pub enum BattleResult {
Inconclusive,
Conclusive(u8),
}

View File

@@ -1,2 +1,145 @@
#[derive(Debug)]
pub struct BattleSide {}
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::pokemon::Pokemon;
use crate::dynamic_data::script_handling::ScriptSource;
use crate::script_hook;
use derive_getters::Getters;
use std::sync::{Arc, Mutex, Weak};
#[derive(Getters, Debug)]
pub struct BattleSide<'a> {
index: u8,
pokemon_per_side: u8,
pokemon: Vec<Option<Arc<Mutex<Pokemon<'a>>>>>,
choices: Vec<Option<Arc<TurnChoice<'a>>>>,
fillable_slots: Vec<bool>,
choices_set: u8,
battle: Weak<Mutex<Battle<'a>>>,
has_fled: bool,
}
impl<'a> BattleSide<'a> {
pub fn new(index: u8, battle: Weak<Mutex<Battle<'a>>>, 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;
}
Self {
index,
pokemon_per_side,
pokemon,
choices,
fillable_slots,
choices_set: 0,
battle,
has_fled: false,
}
}
pub fn all_choices_set(&self) -> bool {
self.choices_set == self.pokemon_per_side
}
/// 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.
pub fn all_slots_filled(&self) -> bool {
for pokemon in &self.pokemon {
if (!pokemon.is_none() || !pokemon.as_ref().unwrap().lock().unwrap().is_usable())
&& self
.battle
.upgrade()
.unwrap()
.lock()
.unwrap()
.can_slot_be_filled()
{
return false;
}
}
true
}
pub fn set_choice(&mut self, choice: TurnChoice<'a>) {
for (index, pokemon_slot) in self.pokemon.iter().enumerate() {
if let Some(pokemon) = pokemon_slot {
if pokemon.lock().unwrap().unique_identifier() == choice.user().unique_identifier()
{
self.choices[index] = Some(Arc::new(choice));
self.choices_set += 1;
return;
}
}
}
}
pub fn force_clear_pokemon(&mut self, index: u8) {
self.pokemon[index as usize] = None;
}
pub fn set_pokemon(&mut self, index: u8, pokemon: Option<Arc<Mutex<Pokemon<'a>>>>) {
let old = &mut self.pokemon[index as usize];
if let Some(old_pokemon) = old {
let mut p = old_pokemon.lock().unwrap();
script_hook!(on_remove, p,);
p.set_on_battlefield(false);
}
self.pokemon[index as usize] = pokemon;
let pokemon = &self.pokemon[index as usize];
if let Some(pokemon_mutex) = pokemon {
let mut pokemon = pokemon_mutex.lock().unwrap();
pokemon.set_battle_data(self.battle.clone(), self.index);
pokemon.set_on_battlefield(true);
pokemon.set_battle_index(index);
let battle = self.battle.upgrade().unwrap();
let battle = battle.lock().unwrap();
for side in battle.sides() {
if *side.index() == self.index {
continue;
}
for opponent in side.pokemon().iter().flatten() {
let mut opponent = opponent.lock().unwrap();
opponent.mark_opponent_as_seen(*pokemon.unique_identifier());
pokemon.mark_opponent_as_seen(*opponent.unique_identifier());
}
}
battle.event_hook().trigger(Event::Switch {
side_index: self.index,
index,
pokemon: Some(pokemon_mutex.clone()),
});
script_hook!(on_switch_in, pokemon, &pokemon);
} else {
let battle = self.battle.upgrade().unwrap();
let battle = battle.lock().unwrap();
battle.event_hook().trigger(Event::Switch {
side_index: self.index,
index,
pokemon: None,
});
}
}
pub fn is_pokemon_on_side(&self, pokemon: Arc<Pokemon<'a>>) -> bool {
for p in self.pokemon.iter().flatten() {
if p.lock().unwrap().unique_identifier() == pokemon.unique_identifier() {
return true;
}
}
false
}
}
impl<'a> ScriptSource for BattleSide<'a> {
fn get_script_count(&self) {
todo!()
}
}

View File

@@ -1 +1,2 @@
#[derive(Debug)]
pub struct LearnedMove {}

View File

@@ -1,4 +1,7 @@
pub mod battle;
pub mod battle_party;
pub mod battle_random;
pub mod battle_result;
pub mod battle_side;
pub mod learned_move;
pub mod pokemon;

View File

@@ -1,10 +1,10 @@
use crate::defines::{LevelInt, MAX_MOVES};
use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary;
use crate::dynamic_data::models::battle::Battle;
use crate::dynamic_data::models::battle_side::BattleSide;
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::ScriptSource;
use crate::static_data::items::item::Item;
use crate::static_data::species_data::ability_index::AbilityIndex;
use crate::static_data::species_data::form::Form;
@@ -12,18 +12,36 @@ 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::statistics::Statistic;
use crate::utils::random::Random;
use derive_getters::Getters;
use std::collections::HashSet;
use std::sync::{Mutex, Weak};
#[derive(Getters)]
#[derive(Debug)]
pub struct PokemonBattleData<'a> {
battle: &'a Battle,
battle_side: &'a BattleSide,
battle: Weak<Mutex<Battle<'a>>>,
battle_side_index: u8,
index: u8,
on_battle_field: bool,
seen_opponents: HashSet<Pokemon<'a>>,
seen_opponents: HashSet<u32>,
}
#[derive(Getters)]
impl<'a> PokemonBattleData<'a> {
pub fn battle(&'a mut self) -> &'a mut Weak<Mutex<Battle<'a>>> {
&mut self.battle
}
pub fn battle_side_index(&self) -> u8 {
self.battle_side_index
}
pub fn on_battle_field(&'a mut self) -> &mut bool {
&mut self.on_battle_field
}
pub fn seen_opponents(&'a mut self) -> &'a mut HashSet<u32> {
&mut self.seen_opponents
}
}
#[derive(Getters, Debug)]
pub struct Pokemon<'a> {
library: &'a DynamicLibrary<'a>,
species: &'a Species<'a>,
@@ -40,6 +58,9 @@ pub struct Pokemon<'a> {
held_item: Option<&'a Item>,
health: u32,
weight: f32,
height: f32,
stat_boost: StatisticSet<i8>,
flat_stats: StatisticSet<u16>,
boosted_stats: StatisticSet<u16>,
@@ -79,6 +100,8 @@ impl<'a> Pokemon<'a> {
.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();
Pokemon {
library,
species,
@@ -92,6 +115,8 @@ impl<'a> Pokemon<'a> {
coloring,
held_item: None,
health,
weight,
height,
stat_boost: Default::default(),
flat_stats: *form.base_stats(),
boosted_stats: *form.base_stats(),
@@ -108,6 +133,87 @@ impl<'a> Pokemon<'a> {
volatile: ScriptSet {},
}
}
pub fn change_species(&mut self, species: &'a Species, form: &'a Form) {
self.species = species;
self.form = form;
// If the pokemon is genderless, but it's new species is not, we want to set its gender
if self.gender != Gender::Genderless && *species.gender_rate() < 0.0 {
if self.battle_data.is_some() {
let battle = self.battle_data.as_mut().unwrap();
self.gender = species.get_random_gender(
battle
.battle
.upgrade()
.unwrap()
.lock()
.unwrap()
.random()
.get_rng(),
);
} else {
self.gender = species.get_random_gender(&mut Random::default());
}
}
// Else if the new species is genderless, but the pokemon has a gender, make the creature genderless.
else if *species.gender_rate() < 0.0 && self.gender != Gender::Genderless {
self.gender = Gender::Genderless;
}
// TODO: Battle Event trigger
}
pub fn is_usable(&self) -> bool {
todo!()
}
pub fn set_battle_data(&mut self, battle: Weak<Mutex<Battle<'a>>>, 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;
} else {
self.battle_data = Some(PokemonBattleData {
battle,
battle_side_index,
index: 0,
on_battle_field: false,
seen_opponents: Default::default(),
})
}
}
pub fn set_on_battlefield(&mut self, value: bool) {
if let Some(data) = &mut self.battle_data {
data.on_battle_field = value;
if !value {
self.reset_active_scripts();
self.weight = *self.form.weight();
self.height = *self.form.height();
}
}
}
pub fn set_battle_index(&mut self, index: u8) {
if let Some(data) = &mut self.battle_data {
data.index = index;
}
}
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> {
fn get_script_count(&self) {
todo!()
}
}
#[cfg(test)]