A lot more work on a bunch of different parts of the system.

This commit is contained in:
2022-06-11 17:22:46 +02:00
parent 10e93949e4
commit 6e8f4dd4a5
35 changed files with 735 additions and 197 deletions

View File

@@ -0,0 +1,29 @@
use crate::static_data::moves::secondary_effect::EffectParameter;
use crate::StringKey;
#[derive(Debug)]
pub struct Ability {
name: StringKey,
effect: StringKey,
parameters: Vec<EffectParameter>,
}
impl Ability {
pub fn new(name: &StringKey, effect: &StringKey, parameters: Vec<EffectParameter>) -> Self {
Self {
name: name.clone(),
effect: effect.clone(),
parameters,
}
}
pub fn name(&self) -> &StringKey {
&self.name
}
pub fn effect(&self) -> &StringKey {
&self.effect
}
pub fn parameters(&self) -> &Vec<EffectParameter> {
&self.parameters
}
}

View File

@@ -1,39 +1,42 @@
use self::super::learnable_moves::LearnableMoves;
use crate::static_data::species_data::ability::Ability;
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 crate::StringKey;
use hashbrown::HashSet;
use std::ops::Deref;
#[derive(Debug)]
pub struct Form<'a> {
name: String,
name: StringKey,
height: f32,
weight: f32,
base_experience: u32,
types: Vec<u8>,
base_stats: StatisticSet<u16>,
abilities: Vec<String>,
hidden_abilities: Vec<String>,
abilities: Vec<&'a Ability>,
hidden_abilities: Vec<&'a Ability>,
moves: LearnableMoves<'a>,
flags: HashSet<String>,
flags: HashSet<StringKey>,
}
impl<'a> Form<'a> {
pub fn new(
name: &str,
name: &StringKey,
height: f32,
weight: f32,
base_experience: u32,
types: Vec<u8>,
base_stats: StatisticSet<u16>,
abilities: Vec<String>,
hidden_abilities: Vec<String>,
abilities: Vec<&'a Ability>,
hidden_abilities: Vec<&'a Ability>,
moves: LearnableMoves<'a>,
flags: HashSet<String>,
flags: HashSet<StringKey>,
) -> Form<'a> {
Form {
name: name.to_string(),
name: name.clone(),
height,
weight,
base_experience,
@@ -46,7 +49,7 @@ impl<'a> Form<'a> {
}
}
pub fn name(&self) -> &str {
pub fn name(&self) -> &StringKey {
&self.name
}
pub fn height(&self) -> f32 {
@@ -64,16 +67,16 @@ impl<'a> Form<'a> {
pub fn base_stats(&self) -> StatisticSet<u16> {
self.base_stats
}
pub fn abilities(&self) -> &Vec<String> {
pub fn abilities(&self) -> &Vec<&'a Ability> {
&self.abilities
}
pub fn hidden_abilities(&self) -> &Vec<String> {
pub fn hidden_abilities(&self) -> &Vec<&'a Ability> {
&self.hidden_abilities
}
pub fn moves(&self) -> &LearnableMoves<'a> {
&self.moves
}
pub fn flags(&self) -> &HashSet<String> {
pub fn flags(&self) -> &HashSet<StringKey> {
&self.flags
}
@@ -85,9 +88,9 @@ impl<'a> Form<'a> {
self.base_stats.get_stat(stat)
}
pub fn find_ability_index(&self, ability: &str) -> Option<AbilityIndex> {
pub fn find_ability_index(&self, ability: &Ability) -> Option<AbilityIndex> {
for (index, a) in self.abilities.iter().enumerate() {
if a == ability {
if std::ptr::eq(a.deref(), ability as *const Ability) {
return Some(AbilityIndex {
hidden: false,
index: index as u8,
@@ -95,7 +98,7 @@ impl<'a> Form<'a> {
}
}
for (index, a) in self.hidden_abilities.iter().enumerate() {
if a == ability {
if std::ptr::eq(a.deref(), ability as *const Ability) {
return Some(AbilityIndex {
hidden: true,
index: index as u8,
@@ -105,15 +108,23 @@ impl<'a> Form<'a> {
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_ability(&self, index: AbilityIndex) -> &Ability {
if index.hidden {
self.hidden_abilities[index.index as usize]
} else {
self.abilities[index.index as usize]
}
}
pub fn get_random_hidden_ability(&self, rand: &mut Random) -> &String {
&self.hidden_abilities
pub fn get_random_ability(&self, rand: &mut Random) -> &Ability {
self.abilities[rand.get_between_unsigned(0, self.abilities.len() as u32) as usize]
}
pub fn get_random_hidden_ability(&self, rand: &mut Random) -> &Ability {
self.hidden_abilities
[rand.get_between_unsigned(0, self.hidden_abilities.len() as u32) as usize]
}
pub fn has_flag(&self, key: &str) -> bool {
pub fn has_flag(&self, key: &StringKey) -> bool {
self.flags.contains(key)
}
}

View File

@@ -46,7 +46,7 @@ mod tests {
#[test]
fn adds_level_moves() {
let move1 = MoveData::new(
"foo",
&"foo".into(),
0,
MoveCategory::Physical,
0,
@@ -58,7 +58,7 @@ mod tests {
Default::default(),
);
let move2 = MoveData::new(
"bar",
&"bar".into(),
0,
MoveCategory::Physical,
0,
@@ -83,7 +83,7 @@ mod tests {
#[test]
fn adds_two_same_moves_at_different_level() {
let move1 = MoveData::new(
"foo",
&"foo".into(),
0,
MoveCategory::Physical,
0,

View File

@@ -3,3 +3,4 @@ pub mod form;
pub mod gender;
pub mod learnable_moves;
pub mod species;
pub mod ability;

View File

@@ -1,36 +1,40 @@
use self::super::form::Form;
use crate::static_data::species_data::gender::Gender;
use crate::utils::random::Random;
use crate::StringKey;
use hashbrown::{HashMap, HashSet};
#[derive(Debug)]
pub struct Species<'a> {
id: u16,
name: String,
name: StringKey,
gender_rate: f32,
growth_rate: String,
growth_rate: StringKey,
capture_rate: u8,
forms: HashMap<String, Form<'a>>,
flags: HashSet<String>,
forms: HashMap<StringKey, Form<'a>>,
flags: HashSet<StringKey>,
}
lazy_static::lazy_static! {
static ref DEFAULT_KEY: StringKey = StringKey::new("default");
}
impl<'a> Species<'a> {
pub fn new(
id: u16,
name: &str,
name: &StringKey,
gender_rate: f32,
growth_rate: &str,
growth_rate: &StringKey,
capture_rate: u8,
default_form: Form<'a>,
flags: HashSet<String>,
flags: HashSet<StringKey>,
) -> Species<'a> {
let mut forms = HashMap::with_capacity(1);
forms.insert("default".to_string(), default_form);
forms.insert_unique_unchecked(DEFAULT_KEY.clone(), default_form);
Species {
id,
name: name.to_string(),
name: name.clone(),
gender_rate,
growth_rate: growth_rate.to_string(),
growth_rate: growth_rate.clone(),
capture_rate,
forms,
flags,
@@ -39,33 +43,37 @@ impl<'a> Species<'a> {
pub fn id(&self) -> u16 {
self.id
}
pub fn name(&self) -> &str {
pub fn name(&self) -> &StringKey {
&self.name
}
pub fn gender_rate(&self) -> f32 {
self.gender_rate
}
pub fn growth_rate(&self) -> &str {
pub fn growth_rate(&self) -> &StringKey {
&self.growth_rate
}
pub fn capture_rate(&self) -> u8 {
self.capture_rate
}
pub fn forms(&self) -> &HashMap<String, Form<'a>> {
pub fn forms(&self) -> &HashMap<StringKey, Form<'a>> {
&self.forms
}
pub fn flags(&self) -> &HashSet<String> {
pub fn flags(&self) -> &HashSet<StringKey> {
&self.flags
}
pub fn add_form(&mut self, id: String, form: Form<'a>) {
pub fn add_form(&mut self, id: StringKey, form: Form<'a>) {
self.forms.insert(id, form);
}
pub fn get_form(&self, id: &str) -> Option<&Form> {
pub fn get_form(&self, id: &StringKey) -> Option<&Form> {
self.forms.get(id)
}
pub fn get_default_form(&self) -> &Form {
self.forms.get(&DEFAULT_KEY).unwrap()
}
pub fn get_random_gender(&self, rand: &mut Random) -> Gender {
if self.gender_rate < 0.0 {
Gender::Genderless
@@ -76,7 +84,7 @@ impl<'a> Species<'a> {
}
}
pub fn has_flag(&self, key: &str) -> bool {
pub fn has_flag(&self, key: &StringKey) -> bool {
self.flags.contains(key)
}
}