Initial commit, implements most of the static_data side.

This commit is contained in:
2021-01-30 22:29:59 +01:00
commit 2a08fb2645
33 changed files with 1237 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct AbilityIndex {
pub hidden: bool,
pub index: u8,
}

View File

@@ -0,0 +1,89 @@
use self::super::learnable_moves::LearnableMoves;
use crate::static_data::species_data::ability_index::AbilityIndex;
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;
#[derive(Getters, Debug)]
pub struct Form<'a> {
name: String,
height: f32,
weight: f32,
base_experience: u32,
types: Vec<u8>,
base_stats: StatisticSet<u16>,
abilities: Vec<String>,
hidden_abilities: Vec<String>,
moves: LearnableMoves<'a>,
flags: HashSet<String>,
}
impl<'a> Form<'a> {
pub fn new(
name: &str,
height: f32,
weight: f32,
base_experience: u32,
types: Vec<u8>,
base_stats: StatisticSet<u16>,
abilities: Vec<String>,
hidden_abilities: Vec<String>,
moves: LearnableMoves<'a>,
flags: HashSet<String>,
) -> Form<'a> {
Form {
name: name.to_string(),
height,
weight,
base_experience,
types,
base_stats,
abilities,
hidden_abilities,
moves,
flags,
}
}
pub fn get_type(&self, index: usize) -> u8 {
self.types[index]
}
pub fn get_base_stat(&self, stat: Statistic) -> u16 {
self.base_stats.get_stat(stat)
}
pub fn find_ability_index(&self, ability: &str) -> Option<AbilityIndex> {
for (index, a) in self.abilities.iter().enumerate() {
if a == ability {
return Some(AbilityIndex {
hidden: false,
index: index as u8,
});
}
}
for (index, a) in self.hidden_abilities.iter().enumerate() {
if a == ability {
return Some(AbilityIndex {
hidden: true,
index: index as u8,
});
}
}
None
}
pub fn get_random_ability(&self, rand: &mut Random) -> &String {
&self.abilities[rand.get_between_unsigned(0, self.abilities.len() as u32) as usize]
}
pub fn get_random_hidden_ability(&self, rand: &mut Random) -> &String {
&self.hidden_abilities
[rand.get_between_unsigned(0, self.hidden_abilities.len() as u32) as usize]
}
pub fn has_flag(&self, key: &str) -> bool {
self.flags.contains(key)
}
}

View File

@@ -0,0 +1,8 @@
// Required for standard pokemon functions, but somewhat controversial nowadays. Consider adding a feature
// that allows for a more progressive gender system for those that want it?
#[derive(Debug)]
pub enum Gender {
Male,
Female,
Genderless,
}

View File

@@ -0,0 +1,110 @@
use crate::defines::LevelInt;
use crate::static_data::moves::move_data::MoveData;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::HashMap;
#[derive(Default, PartialEq, Debug)]
pub struct LearnableMoves<'a> {
learned_by_level: HashMap<LevelInt, Vec<&'a MoveData>>,
distinct_level_moves: Vec<&'a MoveData>,
}
impl<'a> LearnableMoves<'a> {
pub fn new() -> LearnableMoves<'a> {
LearnableMoves::default()
}
pub fn add_level_move(&mut self, level: LevelInt, m: &'a MoveData) {
match self.learned_by_level.entry(level) {
Occupied(x) => {
x.into_mut().push(m);
}
Vacant(_) => {
self.learned_by_level.insert(level, vec![m]);
}
}
if !self.distinct_level_moves.contains(&m) {
self.distinct_level_moves.push(m);
}
}
pub fn get_learned_by_level(&self, level: LevelInt) -> Option<&Vec<&'a MoveData>> {
self.learned_by_level.get(&level)
}
pub fn get_distinct_level_moves(&self) -> &Vec<&'a MoveData> {
&self.distinct_level_moves
}
}
#[cfg(test)]
mod tests {
use crate::static_data::moves::move_data::{MoveCategory, MoveData, MoveTarget};
use crate::static_data::moves::secondary_effect::SecondaryEffect;
use crate::static_data::species_data::learnable_moves::LearnableMoves;
#[test]
fn adds_level_moves() {
let mut moves = LearnableMoves::new();
let move1 = MoveData::new(
"foo".to_string(),
0,
MoveCategory::Physical,
0,
0,
0,
MoveTarget::Adjacent,
0,
SecondaryEffect::empty(),
Default::default(),
);
let move2 = MoveData::new(
"bar".to_string(),
0,
MoveCategory::Physical,
0,
0,
0,
MoveTarget::Adjacent,
0,
SecondaryEffect::empty(),
Default::default(),
);
moves.add_level_move(1, &move1);
moves.add_level_move(1, &move2);
let m = moves.get_learned_by_level(1u8).unwrap();
assert_eq!(m.len(), 2);
assert_eq!(m[0], &move1);
assert_eq!(m[1], &move2);
}
#[test]
fn adds_two_same_moves_at_different_level() {
let mut moves = LearnableMoves::new();
let move1 = MoveData::new(
"foo".to_string(),
0,
MoveCategory::Physical,
0,
0,
0,
MoveTarget::Adjacent,
0,
SecondaryEffect::empty(),
Default::default(),
);
moves.add_level_move(1, &move1);
moves.add_level_move(5, &move1);
let m = moves.get_learned_by_level(1u8).unwrap();
assert_eq!(m.len(), 1);
assert_eq!(m[0], &move1);
let m2 = moves.get_learned_by_level(5u8).unwrap();
assert_eq!(m2.len(), 1);
assert_eq!(m2[0], &move1);
let distinct = moves.get_distinct_level_moves();
assert_eq!(distinct.len(), 1);
assert_eq!(distinct[0], &move1);
}
}

View File

@@ -0,0 +1,5 @@
pub mod ability_index;
pub mod form;
pub mod gender;
pub mod learnable_moves;
pub mod species;

View File

@@ -0,0 +1,62 @@
use self::super::form::Form;
use crate::static_data::species_data::gender::Gender;
use crate::utils::random::Random;
use derive_getters::Getters;
use std::collections::{HashMap, HashSet};
#[derive(Debug, Getters)]
pub struct Species<'a> {
id: u16,
name: String,
gender_rate: f32,
growth_rate: String,
capture_rate: u8,
forms: HashMap<String, Form<'a>>,
flags: HashSet<String>,
}
impl<'a> Species<'a> {
pub fn new(
id: u16,
name: &str,
gender_rate: f32,
growth_rate: &str,
capture_rate: u8,
default_form: Form<'a>,
flags: HashSet<String>,
) -> Species<'a> {
Species {
id,
name: name.to_string(),
gender_rate,
growth_rate: growth_rate.to_string(),
capture_rate,
forms: hashmap! {
"default".to_string() => default_form,
},
flags,
}
}
pub fn add_form(&mut self, id: String, form: Form<'a>) {
self.forms.insert(id, form);
}
pub fn get_form(&self, id: &str) -> Option<&Form> {
self.forms.get(id)
}
pub fn get_random_gender(&self, rand: &mut Random) -> Gender {
if self.gender_rate < 0.0 {
Gender::Genderless
} else if rand.get_float() >= self.gender_rate {
Gender::Female
} else {
Gender::Male
}
}
pub fn has_flag(&self, key: &str) -> bool {
self.flags.contains(key)
}
}