1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use core::{
fmt::{Display, Formatter, Result as FmtResult},
ops::Range,
};
use rand::Rng;
use serde::{Deserialize, Serialize};
use crate::{
moves::{MoveCategory, MoveId, MoveSet, OwnedIdMove},
types::{Effective, PokemonType},
Dex, Identifiable, IdRef,
};
mod owned;
pub use owned::*;
mod data;
pub use data::*;
pub mod stat;
use self::stat::{BaseStat, Stat, StatType, Stats};
pub type PokemonId = u16;
pub type Level = u8;
pub type Experience = u32;
pub type Friendship = u8;
pub type Health = stat::BaseStat;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pokemon {
pub id: PokemonId,
pub name: String,
pub primary_type: PokemonType,
pub secondary_type: Option<PokemonType>,
pub moves: Vec<LearnableMove>,
pub base: Stats,
pub species: String,
pub height: u8,
pub weight: u16,
pub training: Training,
pub breeding: Breeding,
}
pub const PARTY_LENGTH: usize = 6;
pub type Party<P> = arrayvec::ArrayVec<[P; PARTY_LENGTH]>;
pub type PokemonRef<'d> = IdRef<'d, Pokemon>;
pub type Pokedex = Dex<Pokemon>;
impl Pokemon {
pub fn generate_moves(&self, level: Level) -> MoveSet<OwnedIdMove> {
let mut learnable = self
.moves
.iter()
.filter(|learnable_move| learnable_move.0 <= level)
.map(|learnable_move| learnable_move.1)
.rev();
let mut moves = MoveSet::<OwnedIdMove>::new();
while !moves.is_full() {
match learnable.next() {
Some(m) => {
if !moves.iter().any(|i| i.m == m) {
moves.push(m.into());
}
}
None => break,
}
}
moves
}
pub fn generate_gender(&self, random: &mut impl Rng) -> Option<Gender> {
self.breeding.gender.map(
|percentage| match random.gen_range(Gender::RANGE) > percentage {
true => Gender::Male,
false => Gender::Female,
},
)
}
pub fn effective(&self, user: PokemonType, category: MoveCategory) -> Effective {
let primary = user.effective(self.primary_type, category);
if let Some(secondary) = self.secondary_type {
primary * user.effective(secondary, category)
} else {
primary
}
}
pub fn exp_from(&self, level: Level) -> Experience {
((self.training.base_exp * level as u16) / 7) as Experience
}
pub fn moves_at_level(&self, level: Level) -> impl Iterator<Item = MoveId> + '_ {
self.moves.iter().filter(move |m| m.0 == level).map(|l| l.1)
}
pub fn moves_at(&self, levels: Range<Level>) -> impl Iterator<Item = MoveId> + '_ {
let levels = Range {
start: levels.start + 1,
end: levels.end + 1,
};
levels
.into_iter()
.flat_map(move |level| self.moves_at_level(level))
}
pub fn stat(&self, ivs: &Stats, evs: &Stats, level: Level, stat: StatType) -> BaseStat {
match stat {
StatType::Health => Self::base_hp(self.base.hp, ivs.hp, evs.hp, level),
stat => Self::base_stat(self.base.get(stat), ivs.get(stat), evs.get(stat), level),
}
}
pub fn base_stat(base: Stat, iv: Stat, ev: Stat, level: Level) -> BaseStat {
let nature = 1.0;
(((2.0 * base as f32 + iv as f32 + ev as f32) * level as f32 / 100.0 + 5.0).floor()
* nature)
.floor() as BaseStat
}
pub fn base_hp(base: Stat, iv: Stat, ev: Stat, level: Level) -> Health {
((2.0 * base as f32 + iv as f32 + ev as f32) * level as f32 / 100.0 + level as f32 + 10.0)
.floor() as Health
}
pub const fn default_friendship() -> Friendship {
70
}
}
impl Identifiable for Pokemon {
type Id = PokemonId;
const UNKNOWN: Self::Id = 0;
fn id(&self) -> &Self::Id {
&self.id
}
}
impl Display for Pokemon {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "#{} {}", self.id, self.name)
}
}