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

@@ -0,0 +1,18 @@
use crate::dynamic_data::models::learned_move::LearnedMove;
use crate::dynamic_data::models::pokemon::Pokemon;
#[derive(Debug)]
pub enum TurnChoice<'a> {
Move {
user: &'a Pokemon<'a>,
used_move: Box<LearnedMove>,
},
}
impl<'a> TurnChoice<'a> {
pub fn user(&self) -> &'a Pokemon<'a> {
match self {
TurnChoice::Move { user, .. } => user,
}
}
}

View File

@@ -0,0 +1,43 @@
use crate::dynamic_data::models::pokemon::Pokemon;
use std::fmt::{Debug, Formatter};
use std::sync::{Arc, Mutex};
pub struct EventHook {
evt_hook_function: Vec<fn(&Box<&Event>)>,
}
impl EventHook {
pub fn register_listener(&mut self, func: fn(&Box<&Event>)) {
self.evt_hook_function.push(func);
}
pub fn trigger(&self, evt: Event) {
let b = Box::new(&evt);
for f in &self.evt_hook_function {
f(&b);
}
}
}
impl Default for EventHook {
fn default() -> Self {
EventHook {
evt_hook_function: vec![],
}
}
}
impl Debug for EventHook {
fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result {
Ok(())
}
}
#[derive(Debug)]
pub enum Event<'a> {
Switch {
side_index: u8,
index: u8,
pokemon: Option<Arc<Mutex<Pokemon<'a>>>>,
},
}

View File

@@ -0,0 +1 @@
pub mod event_hook;

View File

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

View File

@@ -0,0 +1 @@
pub mod choice_queue;

View File

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

View File

@@ -0,0 +1 @@
pub mod history_holder;

View File

@@ -1,5 +1,3 @@
use crate::dynamic_data::models::pokemon::Pokemon;
pub trait BattleStatCalculator {
//fn is_critical(attack: &ExecutingMove, target: &Pokemon, hit: u8);
}

View File

@@ -1,3 +1,7 @@
pub mod choices;
pub mod event_hooks;
pub mod flow;
pub mod history;
pub mod libraries;
pub mod models;
pub mod script_handling;

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)]

View File

@@ -1,2 +1,86 @@
use crate::dynamic_data::script_handling::script::Script;
use crate::dynamic_data::script_handling::script_set::ScriptSet;
pub mod script;
pub mod script_set;
#[macro_export]
macro_rules! script_hook {
($hook_name: ident, $source: ident, $($parameters: expr),*) => {
let mut aggregator = $source.get_script_iterator();
while let Some(script) = aggregator.get_next() {
if script.is_suppressed() {
continue;
}
script.$hook_name($($parameters),*);
}
};
}
pub trait ScriptSource {
fn get_script_iterator(&self) -> ScriptAggregator {
todo!()
}
fn get_script_count(&self);
}
pub enum ScriptWrapper<'a> {
Script(&'a Box<dyn Script>),
Set(&'a ScriptSet),
}
pub struct ScriptAggregator<'a> {
scripts: Vec<Option<ScriptWrapper<'a>>>,
size: i32,
index: i32,
set_index: i32,
}
impl<'a> ScriptAggregator<'a> {
pub fn new(scripts: Vec<Option<ScriptWrapper<'a>>>) -> Self {
let len = scripts.len();
Self {
scripts,
size: len as i32,
index: -1,
set_index: -1,
}
}
fn increment_to_next_value(&mut self) -> bool {
if self.index != -1 {
if let Some(wrapper) = &self.scripts[self.index as usize] {
if let ScriptWrapper::Set(set) = wrapper {
self.set_index += 1;
if self.set_index as usize >= set.count() {
self.set_index = -1;
} else {
return true;
}
}
}
}
self.index += 1;
for index in self.index..self.size {
self.index = index;
if let Some(wrapper) = &self.scripts[index as usize] {
if let ScriptWrapper::Set(..) = wrapper {
self.set_index = 0;
}
return true;
}
}
false
}
pub fn get_next(&mut self) -> Option<&Box<dyn Script>> {
if !self.increment_to_next_value() {
return None;
}
return match self.scripts[self.index as usize].as_ref().unwrap() {
ScriptWrapper::Script(script) => Some(script),
ScriptWrapper::Set(set) => Some(set.at(self.set_index as usize)),
};
}
}

View File

@@ -1 +1,63 @@
pub trait Script {}
use crate::dynamic_data::models::pokemon::Pokemon;
use std::fmt::{Debug, Formatter};
pub trait Script {
fn is_suppressed(&self) -> bool {
self.get_suppressed_count() > 0
}
fn get_suppressed_count(&self) -> usize;
fn add_suppression(&self);
fn remove_suppression(&self);
// FIXME: add missing parameters
fn stack(&self);
fn on_remove(&self);
fn on_initialize(&self);
fn on_before_turn(&self);
fn change_speed(&self);
fn change_priority(&self);
fn change_attack(&self);
fn change_number_of_hits(&self);
fn prevent_attack(&self);
fn fail_attack(&self);
fn stop_before_attack(&self);
fn on_before_attack(&self);
fn fail_incoming_attack(&self);
fn is_invulnerable(&self);
fn on_attack_miss(&self);
fn change_attack_type(&self);
fn block_critical(&self);
fn override_base_power(&self);
fn change_damage_stats_user(&self);
fn bypass_defensive_stat(&self);
fn bypass_offensive_stat(&self);
fn change_stat_modifier(&self);
fn change_damage_modifier(&self);
fn change_damage(&self);
fn change_incoming_damage(&self);
fn on_incoming_hit(&self);
fn on_opponent_faints(&self);
fn prevent_stat_boost_change(&self);
fn change_stat_boost_change(&self);
fn on_secondary_effect(&self);
fn on_after_hits(&self);
fn prevent_self_switch(&self);
fn prevent_opponent_switch(&self);
fn modify_effect_chance(&self);
fn modify_incoming_effect_change(&self);
fn on_fail(&self);
fn on_opponent_fail(&self);
fn prevent_self_run_away(&self);
fn prevent_opponent_run_away(&self);
fn on_end_turn(&self);
fn on_damage(&self);
fn on_faint(&self);
fn on_switch_in<'b>(&self, pokemon: &'b Pokemon);
fn on_after_held_item_consume(&self);
}
impl Debug for dyn Script {
fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result {
Ok(())
}
}

View File

@@ -1 +1,14 @@
use crate::dynamic_data::script_handling::script::Script;
#[derive(Debug)]
pub struct ScriptSet {}
impl ScriptSet {
pub fn count(&self) -> usize {
todo!()
}
pub fn at(&self, _index: usize) -> &Box<dyn Script> {
todo!()
}
}