A lot more work on a bunch of different parts of the system.
This commit is contained in:
56
src/static_data/libraries/ability_library.rs
Normal file
56
src/static_data/libraries/ability_library.rs
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod ability_library;
|
||||
pub mod data_library;
|
||||
pub mod growth_rate_library;
|
||||
pub mod item_library;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user