Bel LaPointe 2025-03-20 21:38:08 -06:00
parent f493a5e98c
commit fa3c80105f
1 changed files with 69 additions and 32 deletions

View File

@ -158,18 +158,41 @@ pub mod battle {
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(r_idx, w_idx) => { Move::Attack(r_team_idx, w_team_idx) => {
let r = self.teams[r_idx.team].mons[r_idx.mon].clone(); let r_team = self.teams[r_team_idx].clone();
if r.can_attack() { let w_team = &mut self.teams[w_team_idx];
let w = self.teams[w_idx.team].mons[w_idx.mon].clone();
if w.can_be_attacked() { for r_mon_idx in 0..r_team.mons.len() {
self.teams[w_idx.team].mons[w_idx.mon].mon = let r_mon = &r_team.mons[r_mon_idx];
Engine::attack(&w.mon, &r.mon); if !r_mon.can_attack() {
continue;
}
for w_mon_idx in 0..w_team.mons.len() {
let w_mon = &w_team.mons[w_mon_idx];
if !w_mon.can_be_attacked() {
continue;
}
let r_mon = &r_mon.mon;
let w_mon = &w_mon.mon;
w_team.mons[w_mon_idx].mon = Engine::attack(w_mon, r_mon);
} }
} }
self.teams[w_team_idx] = w_team.to_owned();
} }
Move::Swap(to_idx) => { Move::Swap(to_idx) => {
todo!("not impl"); let mut team = self.teams[to_idx.team].clone();
team.mons = team
.mons
.iter()
.map(|m| {
let mut m = m.clone();
m.out = false;
m
})
.collect();
team.mons[to_idx.mon].out = true;
self.teams[to_idx.team] = team;
} }
}; };
}); });
@ -192,7 +215,15 @@ pub mod battle {
match &self.q[i] { match &self.q[i] {
Move::Pass(_) => 0, Move::Pass(_) => 0,
Move::Swap(_) => -10, Move::Swap(_) => -10,
Move::Attack(w, _) => self.teams[w.team].mons[w.mon].mon.dex.new().spd, Move::Attack(r_team, _) => self.teams[r_team.to_owned()]
.mons
.iter()
.filter(|m| m.can_attack())
.map(|m| m.mon.dex.new().spd)
.collect::<Vec<_>>()
.first()
.expect("team had no pokemon to attack")
.to_owned(),
}, },
)); ));
} }
@ -250,12 +281,9 @@ 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( engine.enqueue(Move::Attack(0, 1));
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(1));
// player 1 mon 1 not out // player 1 mon 1 not out
engine.exec(); engine.exec();
@ -294,6 +322,10 @@ pub mod battle {
mons[0].out = true; mons[0].out = true;
Self { mons: mons } Self { mons: mons }
} }
pub fn mons(&self) -> Vec<Instance> {
self.mons.iter().map(|i| i.clone()).collect()
}
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -304,7 +336,7 @@ pub mod battle {
impl Instance { impl Instance {
fn alive(&self) -> bool { fn alive(&self) -> bool {
self.mon.damage < self.mon.dex.new().hp self.damage() < self.mon.dex.new().hp
} }
fn can_attack(&self) -> bool { fn can_attack(&self) -> bool {
@ -314,6 +346,10 @@ pub mod battle {
fn can_be_attacked(&self) -> bool { fn can_be_attacked(&self) -> bool {
self.out && self.alive() self.out && self.alive()
} }
pub fn damage(&self) -> i32 {
self.mon.damage
}
} }
#[cfg(test)] #[cfg(test)]
@ -344,8 +380,8 @@ pub mod battle {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Move { pub enum Move {
Pass(Idx), Pass(usize),
Attack(Idx, Idx), Attack(usize, usize),
Swap(Idx), Swap(Idx),
} }
} }
@ -369,24 +405,25 @@ mod mon_tests {
let mut engine = battle::Engine::new(my_mons, they_mons); let mut engine = battle::Engine::new(my_mons, they_mons);
engine.enqueue(battle::Move::Attack( // team0mon0 trades attacks with team1mon0
battle::Idx { team: 1, mon: 0 }, engine.enqueue(battle::Move::Attack(1, 0));
battle::Idx { team: 0, mon: 0 }, engine.enqueue(battle::Move::Attack(0, 1));
));
engine.enqueue(battle::Move::Attack(
battle::Idx { team: 0, mon: 0 },
battle::Idx { team: 1, mon: 0 },
));
engine.exec(); engine.exec();
assert_eq!(6, engine.teams()[0].mons()[0].damage());
assert_eq!(0, engine.teams()[0].mons()[1].damage());
assert_eq!(1, engine.teams()[1].mons()[0].damage());
assert_eq!(0, engine.teams()[1].mons()[1].damage());
assert_eq!(0, engine.teams()[1].mons()[2].damage());
engine.enqueue(battle::Move::Attack( // team0mon0 swaps with team0mon0
battle::Idx { team: 1, mon: 0 }, // team1mon0 attacks team0
battle::Idx { team: 0, mon: 0 }, engine.enqueue(battle::Move::Attack(1, 0));
));
engine.enqueue(battle::Move::Swap(battle::Idx { team: 0, mon: 1 })); engine.enqueue(battle::Move::Swap(battle::Idx { team: 0, mon: 1 }));
engine.exec(); engine.exec();
assert_eq!(6, engine.teams()[0].mons()[0].damage());
eprintln!("{:?}", engine.teams()[0]); assert_eq!(1, engine.teams()[0].mons()[1].damage());
eprintln!("{:?}", engine.teams()[1]); assert_eq!(1, engine.teams()[1].mons()[0].damage());
assert_eq!(0, engine.teams()[1].mons()[1].damage());
assert_eq!(0, engine.teams()[1].mons()[2].damage());
} }
} }