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

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