First chunk of battling is now fully working, along with integration tests! 🎉
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-06-17 19:53:33 +02:00
parent 59cc150643
commit b6ddd1ee13
34 changed files with 1934 additions and 1293 deletions

View File

@@ -1,6 +1,6 @@
use super::item_category::{BattleItemCategory, ItemCategory};
use crate::StringKey;
use std::collections::HashSet;
use hashbrown::HashSet;
#[derive(Debug)]
pub struct Item {

View File

@@ -1,4 +1,8 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(u8)]
pub enum ItemCategory {
MiscItem,
@@ -12,8 +16,10 @@ pub enum ItemCategory {
}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(u8)]
pub enum BattleItemCategory {
None,
Healing,
StatusHealing,
Pokeball,

View File

@@ -20,7 +20,7 @@ pub trait DataLibrary<'a, T: 'a> {
modifies.1.remove(index);
}
fn get(&self, key: &StringKey) -> Option<&T> {
fn get(&'a self, key: &StringKey) -> Option<&'a T> {
self.map().get(key)
}

View File

@@ -38,7 +38,7 @@ pub mod tests {
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;
use hashbrown::HashSet;
fn build_item() -> Item {
Item::new(

View File

@@ -4,13 +4,13 @@ use crate::StringKey;
use hashbrown::HashMap;
#[derive(Debug)]
pub struct SpeciesLibrary<'a> {
map: HashMap<StringKey, Box<Species<'a>>>,
pub struct SpeciesLibrary {
map: HashMap<StringKey, Box<Species>>,
list: Vec<StringKey>,
}
impl<'a> SpeciesLibrary<'a> {
pub fn new(capacity: usize) -> SpeciesLibrary<'a> {
impl SpeciesLibrary {
pub fn new(capacity: usize) -> SpeciesLibrary {
SpeciesLibrary {
map: HashMap::with_capacity(capacity),
list: Vec::with_capacity(capacity),
@@ -18,8 +18,8 @@ impl<'a> SpeciesLibrary<'a> {
}
}
impl<'a> DataLibrary<'a, Box<Species<'a>>> for SpeciesLibrary<'a> {
fn map(&self) -> &HashMap<StringKey, Box<Species<'a>>> {
impl<'a> DataLibrary<'a, Box<Species>> for SpeciesLibrary {
fn map(&self) -> &HashMap<StringKey, Box<Species>> {
&self.map
}
@@ -27,7 +27,7 @@ impl<'a> DataLibrary<'a, Box<Species<'a>>> for SpeciesLibrary<'a> {
&self.list
}
fn get_modify(&mut self) -> (&mut HashMap<StringKey, Box<Species<'a>>>, &mut Vec<StringKey>) {
fn get_modify(&mut self) -> (&mut HashMap<StringKey, Box<Species>>, &mut Vec<StringKey>) {
(&mut self.map, &mut self.list)
}
}
@@ -42,7 +42,7 @@ pub mod tests {
use crate::static_data::statistic_set::StatisticSet;
use hashbrown::HashSet;
fn build_species<'a>() -> Species<'a> {
fn build_species<'a>() -> Species {
Species::new(
0,
&"foo".into(),
@@ -65,7 +65,7 @@ pub mod tests {
)
}
pub fn build<'a>() -> SpeciesLibrary<'a> {
pub fn build<'a>() -> SpeciesLibrary {
let mut lib = SpeciesLibrary::new(1);
let species = build_species();
// Borrow as mut so we can insert

View File

@@ -8,9 +8,9 @@ use crate::static_data::SpeciesLibrary;
use crate::static_data::TypeLibrary;
#[derive(Debug)]
pub struct StaticData<'a> {
pub struct StaticData {
settings: LibrarySettings,
species: SpeciesLibrary<'a>,
species: SpeciesLibrary,
moves: MoveLibrary,
items: ItemLibrary,
growth_rates: GrowthRateLibrary,
@@ -19,53 +19,66 @@ pub struct StaticData<'a> {
abilities: AbilityLibrary,
}
impl<'a> StaticData<'a> {
pub fn new(
settings: LibrarySettings,
species: SpeciesLibrary<'a>,
moves: MoveLibrary,
items: ItemLibrary,
growth_rates: GrowthRateLibrary,
types: TypeLibrary,
natures: NatureLibrary,
abilities: AbilityLibrary,
) -> Self {
impl StaticData {
pub fn new(settings: LibrarySettings) -> Self {
Self {
settings,
species,
moves,
items,
growth_rates,
types,
natures,
abilities,
species: SpeciesLibrary::new(0),
moves: MoveLibrary::new(0),
items: ItemLibrary::new(0),
growth_rates: GrowthRateLibrary::new(0),
types: TypeLibrary::new(0),
natures: NatureLibrary::new(0),
abilities: AbilityLibrary::new(0),
}
}
pub fn settings(&self) -> &LibrarySettings {
&self.settings
}
pub fn species(&self) -> &SpeciesLibrary<'a> {
pub fn species(&self) -> &SpeciesLibrary {
&self.species
}
pub fn species_mut(&mut self) -> &mut SpeciesLibrary {
&mut self.species
}
pub fn moves(&self) -> &MoveLibrary {
&self.moves
}
pub fn moves_mut(&mut self) -> &mut MoveLibrary {
&mut self.moves
}
pub fn items(&self) -> &ItemLibrary {
&self.items
}
pub fn items_mut(&mut self) -> &mut ItemLibrary {
&mut self.items
}
pub fn growth_rates(&self) -> &GrowthRateLibrary {
&self.growth_rates
}
pub fn growth_rates_mut(&mut self) -> &mut GrowthRateLibrary {
&mut self.growth_rates
}
pub fn types(&self) -> &TypeLibrary {
&self.types
}
pub fn types_mut(&mut self) -> &mut TypeLibrary {
&mut self.types
}
pub fn natures(&self) -> &NatureLibrary {
&self.natures
}
pub fn natures_mut(&mut self) -> &mut NatureLibrary {
&mut self.natures
}
pub fn abilities(&self) -> &AbilityLibrary {
&self.abilities
}
pub fn abilities_mut<'a>(&'a mut self) -> &'a mut AbilityLibrary {
&mut self.abilities
}
}
#[cfg(test)]
@@ -77,7 +90,7 @@ pub mod test {
};
use crate::static_data::natures;
pub fn build<'a>() -> StaticData<'a> {
pub fn build<'a>() -> StaticData {
StaticData {
settings: LibrarySettings::new(100),
species: species_library::tests::build(),

View File

@@ -6,23 +6,22 @@ use crate::static_data::StatisticSet;
use crate::Random;
use crate::StringKey;
use hashbrown::HashSet;
use std::ops::Deref;
#[derive(Debug)]
pub struct Form<'a> {
pub struct Form {
name: StringKey,
height: f32,
weight: f32,
base_experience: u32,
types: Vec<u8>,
base_stats: StatisticSet<u16>,
abilities: Vec<&'a Ability>,
hidden_abilities: Vec<&'a Ability>,
moves: LearnableMoves<'a>,
abilities: Vec<StringKey>,
hidden_abilities: Vec<StringKey>,
moves: LearnableMoves,
flags: HashSet<StringKey>,
}
impl<'a> Form<'a> {
impl Form {
pub fn new(
name: &StringKey,
height: f32,
@@ -30,11 +29,11 @@ impl<'a> Form<'a> {
base_experience: u32,
types: Vec<u8>,
base_stats: StatisticSet<u16>,
abilities: Vec<&'a Ability>,
hidden_abilities: Vec<&'a Ability>,
moves: LearnableMoves<'a>,
abilities: Vec<StringKey>,
hidden_abilities: Vec<StringKey>,
moves: LearnableMoves,
flags: HashSet<StringKey>,
) -> Form<'a> {
) -> Form {
Form {
name: name.clone(),
height,
@@ -67,13 +66,13 @@ impl<'a> Form<'a> {
pub fn base_stats(&self) -> StatisticSet<u16> {
self.base_stats
}
pub fn abilities(&self) -> &Vec<&'a Ability> {
pub fn abilities(&self) -> &Vec<StringKey> {
&self.abilities
}
pub fn hidden_abilities(&self) -> &Vec<&'a Ability> {
pub fn hidden_abilities(&self) -> &Vec<StringKey> {
&self.hidden_abilities
}
pub fn moves(&self) -> &LearnableMoves<'a> {
pub fn moves(&self) -> &LearnableMoves {
&self.moves
}
pub fn flags(&self) -> &HashSet<StringKey> {
@@ -90,7 +89,7 @@ impl<'a> Form<'a> {
pub fn find_ability_index(&self, ability: &Ability) -> Option<AbilityIndex> {
for (index, a) in self.abilities.iter().enumerate() {
if std::ptr::eq(a.deref(), ability as *const Ability) {
if a == ability.name() {
return Some(AbilityIndex {
hidden: false,
index: index as u8,
@@ -98,7 +97,7 @@ impl<'a> Form<'a> {
}
}
for (index, a) in self.hidden_abilities.iter().enumerate() {
if std::ptr::eq(a.deref(), ability as *const Ability) {
if a == ability.name() {
return Some(AbilityIndex {
hidden: true,
index: index as u8,
@@ -108,19 +107,19 @@ impl<'a> Form<'a> {
None
}
pub fn get_ability(&self, index: AbilityIndex) -> &Ability {
pub fn get_ability(&self, index: AbilityIndex) -> &StringKey {
if index.hidden {
self.hidden_abilities[index.index as usize]
&self.hidden_abilities[index.index as usize]
} else {
self.abilities[index.index as usize]
&self.abilities[index.index as usize]
}
}
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_ability(&self, rand: &mut Random) -> &StringKey {
&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 get_random_hidden_ability(&self, rand: &mut Random) -> &StringKey {
&self.hidden_abilities[rand.get_between_unsigned(0, self.hidden_abilities.len() as u32) as usize]
}
pub fn has_flag(&self, key: &StringKey) -> bool {

View File

@@ -1,112 +1,73 @@
use crate::defines::LevelInt;
use crate::static_data::MoveData;
use crate::StringKey;
use hashbrown::hash_map::Entry::{Occupied, Vacant};
use hashbrown::HashMap;
#[derive(Default, PartialEq, Debug)]
pub struct LearnableMoves<'a> {
learned_by_level: HashMap<LevelInt, Vec<&'a MoveData>>,
distinct_level_moves: Vec<&'a MoveData>,
pub struct LearnableMoves {
learned_by_level: HashMap<LevelInt, Vec<StringKey>>,
distinct_level_moves: Vec<StringKey>,
}
impl<'a> LearnableMoves<'a> {
pub fn new() -> LearnableMoves<'a> {
impl LearnableMoves {
pub fn new() -> LearnableMoves {
LearnableMoves::default()
}
pub fn add_level_move(&mut self, level: LevelInt, m: &'a MoveData) {
pub fn add_level_move(&mut self, level: LevelInt, m: &StringKey) {
match self.learned_by_level.entry(level) {
Occupied(x) => {
x.into_mut().push(m);
x.into_mut().push(m.clone());
}
Vacant(_) => {
self.learned_by_level.insert(level, vec![m]);
self.learned_by_level.insert(level, vec![m.clone()]);
}
}
if !self.distinct_level_moves.contains(&m) {
self.distinct_level_moves.push(m);
self.distinct_level_moves.push(m.clone());
}
}
pub fn get_learned_by_level(&self, level: LevelInt) -> Option<&Vec<&'a MoveData>> {
pub fn get_learned_by_level(&self, level: LevelInt) -> Option<&Vec<StringKey>> {
self.learned_by_level.get(&level)
}
pub fn get_distinct_level_moves(&self) -> &Vec<&'a MoveData> {
pub fn get_distinct_level_moves(&self) -> &Vec<StringKey> {
&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 move1 = MoveData::new(
&"foo".into(),
0,
MoveCategory::Physical,
0,
0,
0,
MoveTarget::Adjacent,
0,
SecondaryEffect::empty(),
Default::default(),
);
let move2 = MoveData::new(
&"bar".into(),
0,
MoveCategory::Physical,
0,
0,
0,
MoveTarget::Adjacent,
0,
SecondaryEffect::empty(),
Default::default(),
);
let mut moves = LearnableMoves::new();
moves.add_level_move(1, &move1);
moves.add_level_move(1, &move2);
moves.add_level_move(1, &"foo".into());
moves.add_level_move(1, &"bar".into());
let m = moves.get_learned_by_level(1u8).unwrap();
assert_eq!(m.len(), 2);
assert_eq!(m[0], &move1);
assert_eq!(m[1], &move2);
assert_eq!(m[0], "foo".into());
assert_eq!(m[1], "bar".into());
}
#[test]
fn adds_two_same_moves_at_different_level() {
let move1 = MoveData::new(
&"foo".into(),
0,
MoveCategory::Physical,
0,
0,
0,
MoveTarget::Adjacent,
0,
SecondaryEffect::empty(),
Default::default(),
);
let mut moves = LearnableMoves::new();
moves.add_level_move(1, &move1);
moves.add_level_move(5, &move1);
moves.add_level_move(1, &"foo".into());
moves.add_level_move(5, &"foo".into());
let m = moves.get_learned_by_level(1u8).unwrap();
assert_eq!(m.len(), 1);
assert_eq!(m[0], &move1);
assert_eq!(m[0], "foo".into());
let m2 = moves.get_learned_by_level(5u8).unwrap();
assert_eq!(m2.len(), 1);
assert_eq!(m2[0], &move1);
assert_eq!(m2[0], "foo".into());
let distinct = moves.get_distinct_level_moves();
assert_eq!(distinct.len(), 1);
assert_eq!(distinct[0], &move1);
assert_eq!(distinct[0], "foo".into());
}
}

View File

@@ -6,28 +6,28 @@ use hashbrown::{HashMap, HashSet};
use std::lazy::SyncLazy;
#[derive(Debug)]
pub struct Species<'a> {
pub struct Species {
id: u16,
name: StringKey,
gender_rate: f32,
growth_rate: StringKey,
capture_rate: u8,
forms: HashMap<StringKey, Form<'a>>,
forms: HashMap<StringKey, Form>,
flags: HashSet<StringKey>,
}
static DEFAULT_KEY: SyncLazy<StringKey> = SyncLazy::new(|| StringKey::new("default"));
impl<'a> Species<'a> {
impl Species {
pub fn new(
id: u16,
name: &StringKey,
gender_rate: f32,
growth_rate: &StringKey,
capture_rate: u8,
default_form: Form<'a>,
default_form: Form,
flags: HashSet<StringKey>,
) -> Species<'a> {
) -> Species {
let mut forms = HashMap::with_capacity(1);
forms.insert_unique_unchecked(DEFAULT_KEY.clone(), default_form);
Species {
@@ -55,14 +55,14 @@ impl<'a> Species<'a> {
pub fn capture_rate(&self) -> u8 {
self.capture_rate
}
pub fn forms(&self) -> &HashMap<StringKey, Form<'a>> {
pub fn forms(&self) -> &HashMap<StringKey, Form> {
&self.forms
}
pub fn flags(&self) -> &HashSet<StringKey> {
&self.flags
}
pub fn add_form(&mut self, id: StringKey, form: Form<'a>) {
pub fn add_form(&mut self, id: StringKey, form: Form) {
self.forms.insert(id, form);
}

View File

@@ -1,4 +1,8 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Statistic {
HP,
Attack,