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

@@ -1,25 +1,26 @@
use super::item_category::{BattleItemCategory, ItemCategory};
use crate::StringKey;
use std::collections::HashSet;
#[derive(Debug)]
pub struct Item {
name: String,
name: StringKey,
category: ItemCategory,
battle_category: BattleItemCategory,
price: i32,
flags: HashSet<String>,
flags: HashSet<StringKey>,
}
impl Item {
pub fn new(
name: &str,
name: &StringKey,
category: ItemCategory,
battle_category: BattleItemCategory,
price: i32,
flags: HashSet<String>,
flags: HashSet<StringKey>,
) -> Item {
Item {
name: name.to_string(),
name: name.clone(),
category,
battle_category,
price,
@@ -27,7 +28,7 @@ impl Item {
}
}
pub fn name(&self) -> &str {
pub fn name(&self) -> &StringKey {
&self.name
}
pub fn category(&self) -> ItemCategory {
@@ -39,11 +40,11 @@ impl Item {
pub fn price(&self) -> i32 {
self.price
}
pub fn flags(&self) -> &HashSet<String> {
pub fn flags(&self) -> &HashSet<StringKey> {
&self.flags
}
pub fn has_flag(&self, key: &str) -> bool {
pub fn has_flag(&self, key: &StringKey) -> bool {
self.flags.contains(key)
}
}

View File

@@ -6,7 +6,7 @@ pub enum ItemCategory {
Medicine,
Berry,
TMHM,
FormeChanger,
FormChanger,
KeyItem,
Mail,
}

View File

@@ -0,0 +1,56 @@
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::species_data::ability::Ability;
use crate::StringKey;
use hashbrown::HashMap;
#[derive(Debug)]
pub struct AbilityLibrary {
map: HashMap<StringKey, Box<Ability>>,
list: Vec<StringKey>,
}
impl AbilityLibrary {
pub fn new(capacity: usize) -> AbilityLibrary {
AbilityLibrary {
map: HashMap::with_capacity(capacity),
list: Vec::with_capacity(capacity),
}
}
}
impl DataLibrary<'_, Box<Ability>> for AbilityLibrary {
fn map(&self) -> &HashMap<StringKey, Box<Ability>> {
&self.map
}
fn list_values(&self) -> &Vec<StringKey> {
&self.list
}
fn get_modify(&mut self) -> (&mut HashMap<StringKey, Box<Ability>>, &mut Vec<StringKey>) {
(&mut self.map, &mut self.list)
}
}
#[cfg(test)]
pub mod tests {
use crate::static_data::libraries::ability_library::AbilityLibrary;
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::species_data::ability::Ability;
use crate::StringKey;
pub fn build() -> AbilityLibrary {
let mut lib = AbilityLibrary::new(1);
lib.add(
&StringKey::new("test_ability"),
Box::new(Ability::new(
&"test_ability".into(),
&"test_ability".into(),
Vec::new(),
)),
);
// Drops borrow as mut
lib
}
}

View File

@@ -1,29 +1,30 @@
use crate::utils::random::Random;
use crate::StringKey;
use hashbrown::HashMap;
pub trait DataLibrary<'a, T: 'a> {
fn map(&self) -> &HashMap<String, T>;
fn list_values(&self) -> &Vec<String>;
fn get_modify(&mut self) -> (&mut HashMap<String, T>, &mut Vec<String>);
fn map(&self) -> &HashMap<StringKey, T>;
fn list_values(&self) -> &Vec<StringKey>;
fn get_modify(&mut self) -> (&mut HashMap<StringKey, T>, &mut Vec<StringKey>);
fn add(&mut self, key: &str, value: T) {
fn add(&mut self, key: &StringKey, value: T) {
let modifies = self.get_modify();
modifies.0.insert(key.to_string(), value);
modifies.1.push(key.to_string());
modifies.0.insert(key.clone(), value);
modifies.1.push(key.clone());
}
fn remove(&mut self, key: &str) {
fn remove(&mut self, key: &StringKey) {
let modifies = self.get_modify();
let index = modifies.1.iter().position(|r| *r == key).unwrap();
let index = modifies.1.iter().position(|r| r == key).unwrap();
modifies.0.remove(key);
modifies.1.remove(index);
}
fn get(&self, key: &str) -> Option<&T> {
fn get(&self, key: &StringKey) -> Option<&T> {
self.map().get(key)
}
fn get_mut(&mut self, key: &str) -> Option<&mut T> {
fn get_mut(&mut self, key: &StringKey) -> Option<&mut T> {
self.get_modify().0.get_mut(key)
}

View File

@@ -1,11 +1,12 @@
use crate::defines::LevelInt;
use crate::static_data::growth_rates::growth_rate::GrowthRate;
use crate::StringKey;
use hashbrown::HashMap;
use std::fmt;
use std::fmt::{Debug, Formatter};
pub struct GrowthRateLibrary {
growth_rates: HashMap<String, Box<dyn GrowthRate>>,
growth_rates: HashMap<StringKey, Box<dyn GrowthRate>>,
}
impl GrowthRateLibrary {
@@ -15,14 +16,14 @@ impl GrowthRateLibrary {
}
}
pub fn calculate_level(&self, growth_rate: &str, experience: u32) -> LevelInt {
pub fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> LevelInt {
self.growth_rates[growth_rate].calculate_level(experience)
}
pub fn calculate_experience(&self, growth_rate: &str, level: LevelInt) -> u32 {
pub fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> u32 {
self.growth_rates[growth_rate].calculate_experience(level)
}
pub fn add_growth_rate(&mut self, key: &str, value: Box<dyn GrowthRate>) {
self.growth_rates.insert(key.to_string(), value);
pub fn add_growth_rate(&mut self, key: &StringKey, value: Box<dyn GrowthRate>) {
self.growth_rates.insert(key.clone(), value);
}
}
@@ -43,7 +44,7 @@ pub mod tests {
// Borrow as mut so we can insert
let w = &mut lib;
w.add_growth_rate(
"test_growthrate",
&"test_growthrate".into(),
Box::new(LookupGrowthRate::new(vec![0, 5, 10, 100])),
);
// Drops borrow as mut
@@ -54,14 +55,14 @@ pub mod tests {
#[test]
fn add_growth_rate_to_library_and_calculate_level() {
let lib = build();
assert_eq!(lib.calculate_level("test_growthrate", 3), 1);
assert_eq!(lib.calculate_level("test_growthrate", 50), 3);
assert_eq!(lib.calculate_level(&"test_growthrate".into(), 3), 1);
assert_eq!(lib.calculate_level(&"test_growthrate".into(), 50), 3);
}
#[test]
fn add_growth_rate_to_library_and_calculate_experience() {
let lib = build();
assert_eq!(lib.calculate_experience("test_growthrate", 1), 0);
assert_eq!(lib.calculate_experience("test_growthrate", 3), 10);
assert_eq!(lib.calculate_experience(&"test_growthrate".into(), 1), 0);
assert_eq!(lib.calculate_experience(&"test_growthrate".into(), 3), 10);
}
}

View File

@@ -1,11 +1,12 @@
use crate::static_data::items::item::Item;
use crate::static_data::libraries::data_library::DataLibrary;
use crate::StringKey;
use hashbrown::HashMap;
#[derive(Debug)]
pub struct ItemLibrary {
map: HashMap<String, Box<Item>>,
list: Vec<String>,
map: HashMap<StringKey, Box<Item>>,
list: Vec<StringKey>,
}
impl ItemLibrary {
@@ -18,15 +19,15 @@ impl ItemLibrary {
}
impl DataLibrary<'_, Box<Item>> for ItemLibrary {
fn map(&self) -> &HashMap<String, Box<Item>> {
fn map(&self) -> &HashMap<StringKey, Box<Item>> {
&self.map
}
fn list_values(&self) -> &Vec<String> {
fn list_values(&self) -> &Vec<StringKey> {
&self.list
}
fn get_modify(&mut self) -> (&mut HashMap<String, Box<Item>>, &mut Vec<String>) {
fn get_modify(&mut self) -> (&mut HashMap<StringKey, Box<Item>>, &mut Vec<StringKey>) {
(&mut self.map, &mut self.list)
}
}
@@ -41,7 +42,7 @@ pub mod tests {
fn build_item() -> Item {
Item::new(
"foo",
&"foo".into(),
ItemCategory::MiscItem,
BattleItemCategory::MiscBattleItem,
100,
@@ -54,7 +55,7 @@ pub mod tests {
let m = build_item();
// Borrow as mut so we can insert
let w = &mut lib;
w.add("foo", Box::from(m));
w.add(&"foo".into(), Box::from(m));
// Drops borrow as mut
lib

View File

@@ -1,3 +1,4 @@
pub mod ability_library;
pub mod data_library;
pub mod growth_rate_library;
pub mod item_library;

View File

@@ -1,11 +1,12 @@
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::moves::move_data::MoveData;
use crate::StringKey;
use hashbrown::HashMap;
#[derive(Debug)]
pub struct MoveLibrary {
map: HashMap<String, MoveData>,
list: Vec<String>,
map: HashMap<StringKey, MoveData>,
list: Vec<StringKey>,
}
impl MoveLibrary {
@@ -18,15 +19,15 @@ impl MoveLibrary {
}
impl DataLibrary<'_, MoveData> for MoveLibrary {
fn map(&self) -> &HashMap<String, MoveData> {
fn map(&self) -> &HashMap<StringKey, MoveData> {
&self.map
}
fn list_values(&self) -> &Vec<String> {
fn list_values(&self) -> &Vec<StringKey> {
&self.list
}
fn get_modify(&mut self) -> (&mut HashMap<String, MoveData>, &mut Vec<String>) {
fn get_modify(&mut self) -> (&mut HashMap<StringKey, MoveData>, &mut Vec<StringKey>) {
(&mut self.map, &mut self.list)
}
}
@@ -37,11 +38,12 @@ pub mod tests {
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 crate::StringKey;
use std::collections::HashSet;
fn build_move() -> MoveData {
MoveData::new(
"foo",
&"foo".into(),
0,
MoveCategory::Physical,
100,
@@ -59,7 +61,7 @@ pub mod tests {
let m = build_move();
// Borrow as mut so we can insert
let w = &mut lib;
w.add("foo", m);
w.add(&StringKey::new("foo"), m);
// Drops borrow as mut
lib

View File

@@ -1,11 +1,12 @@
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::species_data::species::Species;
use crate::StringKey;
use hashbrown::HashMap;
#[derive(Debug)]
pub struct SpeciesLibrary<'a> {
map: HashMap<String, Box<Species<'a>>>,
list: Vec<String>,
map: HashMap<StringKey, Box<Species<'a>>>,
list: Vec<StringKey>,
}
impl<'a> SpeciesLibrary<'a> {
@@ -18,15 +19,20 @@ impl<'a> SpeciesLibrary<'a> {
}
impl<'a> DataLibrary<'a, Box<Species<'a>>> for SpeciesLibrary<'a> {
fn map(&self) -> &HashMap<String, Box<Species<'a>>> {
fn map(&self) -> &HashMap<StringKey, Box<Species<'a>>> {
&self.map
}
fn list_values(&self) -> &Vec<String> {
fn list_values(&self) -> &Vec<StringKey> {
&self.list
}
fn get_modify(&mut self) -> (&mut HashMap<String, Box<Species<'a>>>, &mut Vec<String>) {
fn get_modify(
&mut self,
) -> (
&mut HashMap<StringKey, Box<Species<'a>>>,
&mut Vec<StringKey>,
) {
(&mut self.map, &mut self.list)
}
}
@@ -44,12 +50,12 @@ pub mod tests {
fn build_species<'a>() -> Species<'a> {
Species::new(
0,
"foo",
&"foo".into(),
0.5,
"test_growthrate",
&"test_growthrate".into(),
0,
Form::new(
"default",
&"default".into(),
0.0,
0.0,
0,
@@ -69,7 +75,7 @@ pub mod tests {
let species = build_species();
// Borrow as mut so we can insert
let w = &mut lib;
w.add("foo", Box::from(species));
w.add(&"foo".into(), Box::from(species));
// Drops borrow as mut
lib
@@ -81,10 +87,10 @@ pub mod tests {
// Borrow as read so we can read
let r = &lib;
let mon = r.get("foo");
let mon = r.get(&"foo".into());
assert!(mon.is_some());
assert_eq!(mon.unwrap().id(), 0_u16);
assert_eq!(mon.unwrap().as_ref().name(), "foo");
assert_eq!(mon.unwrap().as_ref().name(), &"foo".into());
assert_eq!(r.len(), 1);
}
@@ -92,11 +98,11 @@ pub mod tests {
fn add_species_to_library_then_remove() {
let mut lib = build();
lib.remove("foo");
lib.remove(&"foo".into());
// Borrow as read so we can read
let r = &lib;
let mon = r.get("foo");
let mon = r.get(&"foo".into());
assert!(mon.is_none());
assert_eq!(r.len(), 0);
}

View File

@@ -1,3 +1,4 @@
use crate::static_data::libraries::ability_library::AbilityLibrary;
use crate::static_data::libraries::growth_rate_library::GrowthRateLibrary;
use crate::static_data::libraries::item_library::ItemLibrary;
use crate::static_data::libraries::library_settings::LibrarySettings;
@@ -15,6 +16,7 @@ pub struct StaticData<'a> {
growth_rates: GrowthRateLibrary,
types: TypeLibrary,
natures: NatureLibrary,
abilities: AbilityLibrary,
}
impl<'a> StaticData<'a> {
@@ -26,6 +28,7 @@ impl<'a> StaticData<'a> {
growth_rates: GrowthRateLibrary,
types: TypeLibrary,
natures: NatureLibrary,
abilities: AbilityLibrary,
) -> Self {
Self {
settings,
@@ -35,6 +38,7 @@ impl<'a> StaticData<'a> {
growth_rates,
types,
natures,
abilities,
}
}
@@ -59,6 +63,9 @@ impl<'a> StaticData<'a> {
pub fn natures(&self) -> &NatureLibrary {
&self.natures
}
pub fn abilities(&self) -> &AbilityLibrary {
&self.abilities
}
}
#[cfg(test)]
@@ -66,7 +73,8 @@ 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,
ability_library, growth_rate_library, item_library, move_library, species_library,
type_library,
};
use crate::static_data::natures;
@@ -79,6 +87,7 @@ pub mod test {
growth_rates: growth_rate_library::tests::build(),
types: type_library::tests::build(),
natures: natures::tests::build(),
abilities: ability_library::tests::build(),
}
}
}

View File

@@ -1,8 +1,9 @@
use crate::StringKey;
use hashbrown::HashMap;
#[derive(Debug)]
pub struct TypeLibrary {
types: HashMap<String, u8>,
types: HashMap<StringKey, u8>,
effectiveness: Vec<Vec<f32>>,
}
@@ -14,7 +15,7 @@ impl TypeLibrary {
}
}
pub fn get_type_id(&self, key: &str) -> u8 {
pub fn get_type_id(&self, key: &StringKey) -> u8 {
self.types[key]
}
@@ -30,9 +31,9 @@ impl TypeLibrary {
e
}
pub fn register_type(&mut self, name: &str) -> u8 {
pub fn register_type(&mut self, name: &StringKey) -> u8 {
let id = self.types.len() as u8;
self.types.insert(name.to_string(), id);
self.types.insert(name.clone(), id);
self.effectiveness.resize((id + 1) as usize, vec![]);
for effectiveness in &mut self.effectiveness {
effectiveness.resize((id + 1) as usize, 1.0)
@@ -55,8 +56,8 @@ pub mod tests {
// Borrow as mut so we can insert
let w = &mut lib;
w.register_type("foo");
w.register_type("bar");
w.register_type(&"foo".into());
w.register_type(&"bar".into());
// Drops borrow as mut
w.set_effectiveness(0, 1, 0.5);
@@ -71,14 +72,14 @@ pub mod tests {
// Borrow as mut so we can insert
let w = &mut lib;
w.register_type("foo");
w.register_type("bar");
w.register_type(&"foo".into());
w.register_type(&"bar".into());
// Drops borrow as mut
// Borrow as read so we can read
let r = &lib;
assert_eq!(r.get_type_id("foo"), 0);
assert_eq!(r.get_type_id("bar"), 1);
assert_eq!(r.get_type_id(&"foo".into()), 0);
assert_eq!(r.get_type_id(&"bar".into()), 1);
}
#[test]
@@ -87,8 +88,8 @@ pub mod tests {
// Borrow as mut so we can insert
let w = &mut lib;
w.register_type("foo");
w.register_type("bar");
w.register_type(&"foo".into());
w.register_type(&"bar".into());
w.set_effectiveness(0, 1, 0.5);
w.set_effectiveness(1, 0, 2.0);
// Drops borrow as mut
@@ -105,8 +106,8 @@ pub mod tests {
// Borrow as mut so we can insert
let w = &mut lib;
w.register_type("foo");
w.register_type("bar");
w.register_type(&"foo".into());
w.register_type(&"bar".into());
w.set_effectiveness(0, 1, 0.5);
w.set_effectiveness(1, 0, 2.0);
// Drops borrow as mut

View File

@@ -1,4 +1,5 @@
use self::super::secondary_effect::SecondaryEffect;
use crate::StringKey;
use std::collections::HashSet;
#[derive(PartialEq, Debug)]
@@ -30,7 +31,7 @@ pub enum MoveTarget {
#[derive(PartialEq, Debug)]
pub struct MoveData {
name: String,
name: StringKey,
move_type: u8,
category: MoveCategory,
base_power: u8,
@@ -44,7 +45,7 @@ pub struct MoveData {
impl MoveData {
pub fn new(
name: &str,
name: &StringKey,
move_type: u8,
category: MoveCategory,
base_power: u8,
@@ -56,7 +57,7 @@ impl MoveData {
flags: HashSet<String>,
) -> MoveData {
MoveData {
name: name.to_string(),
name: name.clone(),
move_type,
category,
base_power,

View File

@@ -1,4 +1,5 @@
use crate::static_data::statistics::Statistic;
use crate::StringKey;
use hashbrown::HashMap;
#[derive(Debug)]
@@ -45,7 +46,7 @@ impl Nature {
#[derive(Debug)]
pub struct NatureLibrary {
map: HashMap<String, Nature>,
map: HashMap<StringKey, Nature>,
}
impl NatureLibrary {
@@ -55,20 +56,20 @@ impl NatureLibrary {
}
}
pub fn load_nature(&mut self, name: &str, nature: Nature) {
self.map.insert(name.to_string(), nature);
pub fn load_nature(&mut self, name: StringKey, nature: Nature) {
self.map.insert(name, nature);
}
pub fn get_nature(&self, key: &str) -> Option<&Nature> {
pub fn get_nature(&self, key: &StringKey) -> Option<&Nature> {
self.map.get(key)
}
pub fn get_nature_name(&self, nature: &Nature) -> String {
pub fn get_nature_name(&self, nature: &Nature) -> StringKey {
for kv in &self.map {
// As natures can't be copied, and should always be the same reference as the value
// in the map, we just compare by reference.
if (kv.1 as *const Nature) == (nature as *const Nature) {
return kv.0.to_string();
return kv.0.clone();
}
}
panic!("No name was found for the given nature. This should never happen.");
@@ -84,7 +85,7 @@ pub mod tests {
let mut lib = NatureLibrary::new(2);
lib.load_nature(
"test_nature",
"test_nature".into(),
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
);
@@ -95,14 +96,14 @@ pub mod tests {
fn create_nature_library_insert_and_retrieve() {
let mut lib = NatureLibrary::new(2);
lib.load_nature(
"foo",
"foo".into(),
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
);
lib.load_nature(
"bar",
"bar".into(),
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
);
let n1 = lib.get_nature("foo").expect("Nature was not found");
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
assert_eq!(n1.increase_stat, Statistic::HP);
assert_eq!(n1.decrease_stat, Statistic::Attack);
assert_eq!(n1.increase_modifier, 1.1);
@@ -113,19 +114,19 @@ pub mod tests {
fn create_nature_library_insert_and_get_name() {
let mut lib = NatureLibrary::new(2);
lib.load_nature(
"foo",
"foo".into(),
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
);
lib.load_nature(
"bar",
"bar".into(),
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
);
let n1 = lib.get_nature("foo").expect("Nature was not found");
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
let name = lib.get_nature_name(n1);
assert_eq!(name, "foo");
let n2 = lib.get_nature("bar").expect("Nature was not found");
assert_eq!(name, "foo".into());
let n2 = lib.get_nature(&"bar".into()).expect("Nature was not found");
let name2 = lib.get_nature_name(n2);
assert_eq!(name2, "bar");
assert_eq!(name2, "bar".into());
}
}

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