dont pass species but rather dex around

main
Bel LaPointe 2025-03-20 20:12:32 -06:00
parent 50841b807a
commit 6007c2f2ed
1 changed files with 291 additions and 255 deletions

View File

@ -14,7 +14,7 @@ pub mod mon {
#[test] #[test]
fn test_literal() { fn test_literal() {
let _: Species = Species{ let _: Species = Species {
name: "".to_string(), name: "".to_string(),
hp: 1, hp: 1,
atk: 2, atk: 2,
@ -34,19 +34,39 @@ pub mod mon {
pub fn new(&self) -> Species { pub fn new(&self) -> Species {
self.clone().into() self.clone().into()
} }
pub fn name(&self) -> String {
self.new().name
}
pub fn hp(&self) -> i32 {
self.new().hp
}
pub fn atk(&self) -> i32 {
self.new().atk
}
pub fn def(&self) -> i32 {
self.new().def
}
pub fn spd(&self) -> i32 {
self.new().spd
}
} }
impl Into<Species> for Dex { impl Into<Species> for Dex {
fn into(self) -> Species { fn into(self) -> Species {
match self { match self {
Dex::Pika => Species{ Dex::Pika => Species {
name: "Pika".to_string(), name: "Pika".to_string(),
hp: 10, hp: 10,
atk: 11, atk: 11,
def: 9, def: 9,
spd: 11, spd: 11,
}, },
Dex::Mari => Species{ Dex::Mari => Species {
name: "Mari".to_string(), name: "Mari".to_string(),
hp: 10, hp: 10,
atk: 8, atk: 8,
@ -73,14 +93,14 @@ pub mod mon {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Instance { pub struct Instance {
pub species: Species, pub dex: Dex,
pub damage: i32, pub damage: i32,
} }
impl Instance { impl Instance {
pub fn roll(dex: Dex) -> Instance { pub fn roll(dex: Dex) -> Instance {
Instance{ Instance {
species: dex.into(), dex: dex,
damage: 0, damage: 0,
} }
} }
@ -94,7 +114,7 @@ pub mod mon {
fn test_instance_roll() { fn test_instance_roll() {
let i: Instance = Instance::roll(Dex::Pika); let i: Instance = Instance::roll(Dex::Pika);
assert_eq!(0, i.damage); assert_eq!(0, i.damage);
assert_eq!(Dex::Pika.new(), i.species); assert_eq!(Dex::Pika, i.dex);
} }
} }
} }
@ -109,7 +129,7 @@ pub mod battle {
impl Engine { impl Engine {
pub fn new(first: Vec<mon::Instance>, second: Vec<mon::Instance>) -> Engine { pub fn new(first: Vec<mon::Instance>, second: Vec<mon::Instance>) -> Engine {
let mut result = Self{ let mut result = Self {
teams: vec![], teams: vec![],
q: vec![], q: vec![],
}; };
@ -129,16 +149,17 @@ pub mod battle {
pub fn exec(&mut self) { pub fn exec(&mut self) {
self.turn_order().iter().for_each(|idx| { self.turn_order().iter().for_each(|idx| {
match self.q[*idx].clone() { match self.q[*idx].clone() {
Move::Pass(_) => {}, Move::Pass(_) => {}
Move::Attack(rIdx, wIdx) => { Move::Attack(r_idx, w_idx) => {
let r = self.teams[rIdx.team].mons[rIdx.mon].clone(); let r = self.teams[r_idx.team].mons[r_idx.mon].clone();
if r.can_attack() { if r.can_attack() {
let w = self.teams[wIdx.team].mons[wIdx.mon].clone(); let w = self.teams[w_idx.team].mons[w_idx.mon].clone();
if w.can_be_attacked() { if w.can_be_attacked() {
self.teams[wIdx.team].mons[wIdx.mon].mon = Engine::attack(&w.mon, &r.mon); self.teams[w_idx.team].mons[w_idx.mon].mon =
Engine::attack(&w.mon, &r.mon);
}
} }
} }
},
}; };
}); });
self.new_turn(); self.new_turn();
@ -151,10 +172,13 @@ pub mod battle {
fn turn_order(&self) -> Vec<usize> { fn turn_order(&self) -> Vec<usize> {
let mut order = vec![]; let mut order = vec![];
for i in 0..self.q.len() { for i in 0..self.q.len() {
order.push((i, match &self.q[i] { order.push((
i,
match &self.q[i] {
Move::Pass(_) => 0, Move::Pass(_) => 0,
Move::Attack(w, _) => self.teams[w.team].mons[w.mon].mon.species.spd, Move::Attack(w, _) => self.teams[w.team].mons[w.mon].mon.dex.new().spd,
})); },
));
} }
order.sort_unstable_by(|a, b| a.1.cmp(&b.1)); order.sort_unstable_by(|a, b| a.1.cmp(&b.1));
order.iter().map(|x| x.0).collect() order.iter().map(|x| x.0).collect()
@ -162,8 +186,8 @@ pub mod battle {
fn attack(w: &mon::Instance, r: &mon::Instance) -> mon::Instance { fn attack(w: &mon::Instance, r: &mon::Instance) -> mon::Instance {
let mut w = w.clone(); let mut w = w.clone();
let atk_delta = r.species.atk - 10; let atk_delta = r.dex.new().atk - 10;
let def_delta = w.species.def - 10; let def_delta = w.dex.new().def - 10;
let power = match atk_delta - def_delta { let power = match atk_delta - def_delta {
v if v < 1 => 1, v if v < 1 => 1,
@ -171,8 +195,8 @@ pub mod battle {
}; };
w.damage = match w.damage + power { w.damage = match w.damage + power {
v if v < w.species.hp => v, v if v < w.dex.new().hp => v,
_ => w.species.hp, _ => w.dex.new().hp,
}; };
w w
@ -200,10 +224,10 @@ pub mod battle {
mari = Engine::attack(&mut mari, &mut pika); mari = Engine::attack(&mut mari, &mut pika);
assert_eq!(1, mari.damage); assert_eq!(1, mari.damage);
for _ in 0..pika.species.hp+5 { for _ in 0..pika.dex.new().hp + 5 {
pika = Engine::attack(&mut pika, &mut mari); pika = Engine::attack(&mut pika, &mut mari);
} }
assert_eq!(pika.species.hp, pika.damage); assert_eq!(pika.dex.new().hp, pika.damage);
assert_eq!(1, mari.damage); assert_eq!(1, mari.damage);
} }
@ -213,9 +237,12 @@ pub mod battle {
engine.join(team_b()); engine.join(team_b());
// player 0 mon 0 attacks team 1 mon 0 // player 0 mon 0 attacks team 1 mon 0
engine.enqueue(Move::Attack(Idx{team: 0, mon: 0}, Idx{team: 1, mon: 0})); engine.enqueue(Move::Attack(
Idx { team: 0, mon: 0 },
Idx { team: 1, mon: 0 },
));
// player 1 mon 0 passes // player 1 mon 0 passes
engine.enqueue(Move::Pass(Idx{team: 1, mon: 0})); engine.enqueue(Move::Pass(Idx { team: 1, mon: 0 }));
// player 1 mon 1 not out // player 1 mon 1 not out
engine.exec(); engine.exec();
@ -229,7 +256,10 @@ pub mod battle {
} }
fn team_b() -> Vec<mon::Instance> { fn team_b() -> Vec<mon::Instance> {
vec![mon::Instance::roll(mon::Dex::Mari), mon::Instance::roll(mon::Dex::Pika)] vec![
mon::Instance::roll(mon::Dex::Mari),
mon::Instance::roll(mon::Dex::Pika),
]
} }
} }
@ -241,9 +271,15 @@ pub mod battle {
impl Team { impl Team {
fn new(mons: Vec<mon::Instance>) -> Self { fn new(mons: Vec<mon::Instance>) -> Self {
assert!(mons.len() > 0); assert!(mons.len() > 0);
let mut mons: Vec<_> = mons.iter().map(|m| Instance{mon: m.clone(), out: false}).collect(); let mut mons: Vec<_> = mons
.iter()
.map(|m| Instance {
mon: m.clone(),
out: false,
})
.collect();
mons[0].out = true; mons[0].out = true;
Self{mons: mons} Self { mons: mons }
} }
} }
@ -255,7 +291,7 @@ pub mod battle {
impl Instance { impl Instance {
fn alive(&self) -> bool { fn alive(&self) -> bool {
self.mon.damage < self.mon.species.hp self.mon.damage < self.mon.dex.new().hp
} }
fn can_attack(&self) -> bool { fn can_attack(&self) -> bool {
@ -273,14 +309,14 @@ pub mod battle {
#[test] #[test]
fn test_checks() { fn test_checks() {
let mut i = Instance{ let mut i = Instance {
mon: mon::Instance::roll(mon::Dex::Pika), mon: mon::Instance::roll(mon::Dex::Pika),
out: true, out: true,
}; };
assert_eq!(true, i.alive()); assert_eq!(true, i.alive());
assert_eq!(true, i.can_attack()); assert_eq!(true, i.can_attack());
assert_eq!(true, i.can_be_attacked()); assert_eq!(true, i.can_be_attacked());
i.mon.damage = i.mon.species.hp; i.mon.damage = i.mon.dex.new().hp;
assert_eq!(false, i.alive()); assert_eq!(false, i.alive());
assert_eq!(false, i.can_attack()); assert_eq!(false, i.can_attack());
assert_eq!(false, i.can_be_attacked()); assert_eq!(false, i.can_be_attacked());
@ -288,13 +324,13 @@ pub mod battle {
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct Idx { pub struct Idx {
pub team: usize, pub team: usize,
pub mon: usize, pub mon: usize,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
enum Move { pub enum Move {
Pass(Idx), Pass(Idx),
Attack(Idx, Idx), Attack(Idx, Idx),
} }