Initial work on outlining the dynamic side of the library.

This commit is contained in:
Deukhoofd 2021-01-31 17:31:22 +01:00
parent 2a08fb2645
commit c194c5d209
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
24 changed files with 321 additions and 39 deletions

View File

@ -1 +1,2 @@
pub type LevelInt = u8;
pub const MAX_MOVES: usize = 4;

View File

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

View File

@ -0,0 +1,19 @@
use crate::static_data::libraries::static_data::StaticData;
use derive_getters::Getters;
#[derive(Getters, Debug)]
pub struct DynamicLibrary<'a> {
static_data: StaticData<'a>,
}
#[cfg(test)]
pub mod test {
use crate::dynamic_data::libraries::dynamic_library::DynamicLibrary;
use crate::static_data::libraries::static_data;
pub fn build<'a>() -> DynamicLibrary<'a> {
DynamicLibrary {
static_data: static_data::test::build(),
}
}
}

View File

@ -0,0 +1,2 @@
pub mod battle_stat_calculator;
pub mod dynamic_library;

3
src/dynamic_data/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod libraries;
pub mod models;
pub mod script_handling;

View File

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

View File

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

View File

@ -0,0 +1 @@
pub struct LearnedMove {}

View File

@ -0,0 +1,4 @@
pub mod battle;
pub mod battle_side;
pub mod learned_move;
pub mod pokemon;

View File

@ -0,0 +1,143 @@
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::static_data::items::item::Item;
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::statistics::Statistic;
use derive_getters::Getters;
use std::collections::HashSet;
#[derive(Getters)]
pub struct PokemonBattleData<'a> {
battle: &'a Battle,
battle_side: &'a BattleSide,
on_battle_field: bool,
seen_opponents: HashSet<Pokemon<'a>>,
}
#[derive(Getters)]
pub struct Pokemon<'a> {
library: &'a DynamicLibrary<'a>,
species: &'a Species<'a>,
form: &'a Form<'a>,
display_species: Option<&'a Species<'a>>,
display_form: Option<&'a Form<'a>>,
level: LevelInt,
experience: u32,
unique_identifier: u32,
gender: Gender,
coloring: u8,
held_item: Option<&'a Item>,
health: u32,
stat_boost: StatisticSet<i8>,
flat_stats: StatisticSet<u16>,
boosted_stats: StatisticSet<u16>,
nickname: Option<String>,
ability_index: AbilityIndex,
is_ability_overridden: bool,
overridden_ability_name: String,
battle_data: Option<PokemonBattleData<'a>>,
moves: [Option<LearnedMove>; MAX_MOVES],
allowed_experience: bool,
types: Vec<u8>,
ability_script: Option<Box<dyn Script>>,
status_script: Option<Box<dyn Script>>,
volatile: ScriptSet,
}
impl<'a> Pokemon<'a> {
pub fn new(
library: &'a DynamicLibrary,
species: &'a Species,
form: &'a Form,
ability: AbilityIndex,
level: LevelInt,
unique_identifier: u32,
gender: Gender,
coloring: u8,
) -> Pokemon<'a> {
// Calculate experience from the level for the specified growth rate.
let experience = library
.static_data()
.growth_rates()
.calculate_experience(species.growth_rate(), level);
let health = form.get_base_stat(Statistic::HP) as u32;
Pokemon {
library,
species,
form,
display_species: None,
display_form: None,
level,
experience,
unique_identifier,
gender,
coloring,
held_item: None,
health,
stat_boost: Default::default(),
flat_stats: *form.base_stats(),
boosted_stats: *form.base_stats(),
nickname: None,
ability_index: ability,
is_ability_overridden: false,
overridden_ability_name: "".to_string(),
battle_data: None,
moves: [None, None, None, None],
allowed_experience: false,
types: form.types().to_vec(),
ability_script: None,
status_script: None,
volatile: ScriptSet {},
}
}
}
#[cfg(test)]
pub mod test {
use crate::dynamic_data::libraries::dynamic_library;
use crate::dynamic_data::models::pokemon::Pokemon;
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::species_data::ability_index::AbilityIndex;
use crate::static_data::species_data::gender::Gender;
#[test]
fn construct_pokemon() {
let lib = dynamic_library::test::build();
let species = lib.static_data().species().get("foo").unwrap();
let form = species.get_form("default").unwrap();
let pokemon = Pokemon::new(
&lib,
species,
form,
AbilityIndex {
hidden: false,
index: 0,
},
10,
0,
Gender::Male,
0,
);
assert_eq!(pokemon.species.name(), "foo");
assert_eq!(pokemon.form.name(), "default");
}
}

View File

@ -0,0 +1,2 @@
pub mod script;
pub mod script_set;

View File

@ -0,0 +1 @@
pub trait Script {}

View File

@ -0,0 +1 @@
pub struct ScriptSet {}

View File

@ -1,10 +1,12 @@
// The too many arguments is annoying, especially for when we create constructors, disable.
#![allow(clippy::too_many_arguments, clippy::needless_range_loop)]
#![feature(nll)]
#![feature(in_band_lifetimes)]
#[macro_use]
extern crate maplit;
pub mod defines;
pub mod dynamic_data;
pub mod static_data;
pub mod utils;

View File

@ -33,12 +33,11 @@ impl Debug for GrowthRateLibrary {
}
#[cfg(test)]
mod tests {
pub mod tests {
use crate::static_data::growth_rates::lookup_growth_rate::LookupGrowthRate;
use crate::static_data::libraries::growth_rate_library::GrowthRateLibrary;
#[test]
fn add_growth_rate_to_library_and_calculate_level() {
pub fn build() -> GrowthRateLibrary {
let mut lib = GrowthRateLibrary::new(1);
// Borrow as mut so we can insert
@ -46,24 +45,20 @@ mod tests {
w.add_growth_rate("foo", Box::new(LookupGrowthRate::new(vec![0, 5, 10, 100])));
// Drops borrow as mut
// Borrow as read so we can read
let r = &lib;
assert_eq!(r.calculate_level("foo", 3), 1);
assert_eq!(r.calculate_level("foo", 50), 3);
lib
}
#[test]
fn add_growth_rate_to_library_and_calculate_level() {
let lib = build();
assert_eq!(lib.calculate_level("foo", 3), 1);
assert_eq!(lib.calculate_level("foo", 50), 3);
}
#[test]
fn add_growth_rate_to_library_and_calculate_experience() {
let mut lib = GrowthRateLibrary::new(1);
// Borrow as mut so we can insert
let w = &mut lib;
w.add_growth_rate("foo", Box::new(LookupGrowthRate::new(vec![0, 5, 10, 100])));
// Drops borrow as mut
// Borrow as read so we can read
let r = &lib;
assert_eq!(r.calculate_experience("foo", 1), 0);
assert_eq!(r.calculate_experience("foo", 3), 10);
let lib = build();
assert_eq!(lib.calculate_experience("foo", 1), 0);
assert_eq!(lib.calculate_experience("foo", 3), 10);
}
}

View File

@ -30,3 +30,33 @@ impl DataLibrary<'_, Item> for ItemLibrary {
(&mut self.map, &mut self.list)
}
}
#[cfg(test)]
pub mod tests {
use crate::static_data::items::item::Item;
use crate::static_data::items::item_category::{BattleItemCategory, ItemCategory};
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::item_library::ItemLibrary;
use std::collections::HashSet;
fn build_item() -> Item {
Item::new(
"foo",
ItemCategory::MiscItem,
BattleItemCategory::MiscBattleItem,
100,
HashSet::new(),
)
}
pub fn build() -> ItemLibrary {
let mut lib = ItemLibrary::new(1);
let m = build_item();
// Borrow as mut so we can insert
let w = &mut lib;
w.add("foo", m);
// Drops borrow as mut
lib
}
}

View File

@ -3,6 +3,5 @@ use derive_getters::Getters;
#[derive(Getters, Debug)]
pub struct LibrarySettings {
maximum_level: LevelInt,
maximum_move_count: u8,
pub(crate) maximum_level: LevelInt,
}

View File

@ -30,3 +30,38 @@ impl DataLibrary<'_, MoveData> for MoveLibrary {
(&mut self.map, &mut self.list)
}
}
#[cfg(test)]
pub mod tests {
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::move_library::MoveLibrary;
use crate::static_data::moves::move_data::{MoveCategory, MoveData, MoveTarget};
use crate::static_data::moves::secondary_effect::SecondaryEffect;
use std::collections::HashSet;
fn build_move() -> MoveData {
MoveData::new(
"foo",
0,
MoveCategory::Physical,
100,
100,
30,
MoveTarget::Any,
0,
SecondaryEffect::empty(),
HashSet::new(),
)
}
pub fn build() -> MoveLibrary {
let mut lib = MoveLibrary::new(1);
let m = build_move();
// Borrow as mut so we can insert
let w = &mut lib;
w.add("foo", m);
// Drops borrow as mut
lib
}
}

View File

@ -32,7 +32,7 @@ impl<'a> DataLibrary<'a, Species<'a>> for SpeciesLibrary<'a> {
}
#[cfg(test)]
mod tests {
pub mod tests {
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::species_library::SpeciesLibrary;
use crate::static_data::species_data::form::Form;
@ -64,16 +64,21 @@ mod tests {
)
}
#[test]
fn add_species_to_library_and_fetch() {
pub fn build<'a>() -> SpeciesLibrary<'a> {
let mut lib = SpeciesLibrary::new(1);
let species = build_species();
// Borrow as mut so we can insert
let w = &mut lib;
w.add("foo", species);
// Drops borrow as mut
lib
}
#[test]
fn add_species_to_library_and_fetch() {
let lib = build();
// Borrow as read so we can read
let r = &lib;
let mon = r.get("foo");
@ -85,14 +90,9 @@ mod tests {
#[test]
fn add_species_to_library_then_remove() {
let mut lib = SpeciesLibrary::new(1);
let species = build_species();
let mut lib = build();
// Borrow as mut so we can insert
let w = &mut lib;
w.add("foo", species);
w.remove("foo");
// Drops borrow as mut
lib.remove("foo");
// Borrow as read so we can read
let r = &lib;

View File

@ -7,7 +7,7 @@ use crate::static_data::libraries::type_library::TypeLibrary;
use derive_getters::Getters;
#[derive(Getters, Debug)]
struct StaticData<'a> {
pub struct StaticData<'a> {
settings: LibrarySettings,
species: SpeciesLibrary<'a>,
moves: MoveLibrary,
@ -15,3 +15,23 @@ struct StaticData<'a> {
growth_rates: GrowthRateLibrary,
types: TypeLibrary,
}
#[cfg(test)]
pub mod test {
use crate::static_data::libraries::library_settings::LibrarySettings;
use crate::static_data::libraries::static_data::StaticData;
use crate::static_data::libraries::{
growth_rate_library, item_library, move_library, species_library, type_library,
};
pub fn build<'a>() -> StaticData<'a> {
StaticData {
settings: LibrarySettings { maximum_level: 100 },
species: species_library::tests::build(),
moves: move_library::tests::build(),
items: item_library::tests::build(),
growth_rates: growth_rate_library::tests::build(),
types: type_library::tests::build(),
}
}
}

View File

@ -46,10 +46,25 @@ impl TypeLibrary {
}
#[cfg(test)]
mod tests {
pub mod tests {
use crate::static_data::libraries::type_library::TypeLibrary;
use assert_approx_eq::assert_approx_eq;
pub fn build() -> TypeLibrary {
let mut lib = TypeLibrary::new(2);
// Borrow as mut so we can insert
let w = &mut lib;
w.register_type("foo");
w.register_type("bar");
// Drops borrow as mut
w.set_effectiveness(0, 1, 0.5);
w.set_effectiveness(1, 0, 2.0);
lib
}
#[test]
fn add_two_types_retrieve_them() {
let mut lib = TypeLibrary::new(2);

View File

@ -44,7 +44,7 @@ pub struct MoveData {
impl MoveData {
pub fn new(
name: String,
name: &str,
move_type: u8,
category: MoveCategory,
base_power: u8,
@ -56,7 +56,7 @@ impl MoveData {
flags: HashSet<String>,
) -> MoveData {
MoveData {
name,
name: name.to_string(),
move_type,
category,
base_power,

View File

@ -47,7 +47,7 @@ mod tests {
fn adds_level_moves() {
let mut moves = LearnableMoves::new();
let move1 = MoveData::new(
"foo".to_string(),
"foo",
0,
MoveCategory::Physical,
0,
@ -59,7 +59,7 @@ mod tests {
Default::default(),
);
let move2 = MoveData::new(
"bar".to_string(),
"bar",
0,
MoveCategory::Physical,
0,
@ -83,7 +83,7 @@ mod tests {
fn adds_two_same_moves_at_different_level() {
let mut moves = LearnableMoves::new();
let move1 = MoveData::new(
"foo".to_string(),
"foo",
0,
MoveCategory::Physical,
0,

View File

@ -2,7 +2,7 @@ use super::statistics::Statistic;
use derive_getters::Getters;
use num_traits::PrimInt;
#[derive(Default, Eq, PartialEq, Debug, Getters)]
#[derive(Default, Eq, PartialEq, Copy, Clone, Debug, Getters)]
pub struct StatisticSet<T>
where
T: PrimInt,