dmg
parent
f493a5e98c
commit
fa3c80105f
|
|
@ -158,18 +158,41 @@ pub mod battle {
|
|||
self.turn_order().iter().for_each(|idx| {
|
||||
match self.q[*idx].clone() {
|
||||
Move::Pass(_) => {}
|
||||
Move::Attack(r_idx, w_idx) => {
|
||||
let r = self.teams[r_idx.team].mons[r_idx.mon].clone();
|
||||
if r.can_attack() {
|
||||
let w = self.teams[w_idx.team].mons[w_idx.mon].clone();
|
||||
if w.can_be_attacked() {
|
||||
self.teams[w_idx.team].mons[w_idx.mon].mon =
|
||||
Engine::attack(&w.mon, &r.mon);
|
||||
Move::Attack(r_team_idx, w_team_idx) => {
|
||||
let r_team = self.teams[r_team_idx].clone();
|
||||
let w_team = &mut self.teams[w_team_idx];
|
||||
|
||||
for r_mon_idx in 0..r_team.mons.len() {
|
||||
let r_mon = &r_team.mons[r_mon_idx];
|
||||
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) => {
|
||||
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] {
|
||||
Move::Pass(_) => 0,
|
||||
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());
|
||||
|
||||
// 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(0, 1));
|
||||
// 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
|
||||
engine.exec();
|
||||
|
||||
|
|
@ -294,6 +322,10 @@ pub mod battle {
|
|||
mons[0].out = true;
|
||||
Self { mons: mons }
|
||||
}
|
||||
|
||||
pub fn mons(&self) -> Vec<Instance> {
|
||||
self.mons.iter().map(|i| i.clone()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
|
@ -304,7 +336,7 @@ pub mod battle {
|
|||
|
||||
impl Instance {
|
||||
fn alive(&self) -> bool {
|
||||
self.mon.damage < self.mon.dex.new().hp
|
||||
self.damage() < self.mon.dex.new().hp
|
||||
}
|
||||
|
||||
fn can_attack(&self) -> bool {
|
||||
|
|
@ -314,6 +346,10 @@ pub mod battle {
|
|||
fn can_be_attacked(&self) -> bool {
|
||||
self.out && self.alive()
|
||||
}
|
||||
|
||||
pub fn damage(&self) -> i32 {
|
||||
self.mon.damage
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -344,8 +380,8 @@ pub mod battle {
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Move {
|
||||
Pass(Idx),
|
||||
Attack(Idx, Idx),
|
||||
Pass(usize),
|
||||
Attack(usize, usize),
|
||||
Swap(Idx),
|
||||
}
|
||||
}
|
||||
|
|
@ -369,24 +405,25 @@ mod mon_tests {
|
|||
|
||||
let mut engine = battle::Engine::new(my_mons, they_mons);
|
||||
|
||||
engine.enqueue(battle::Move::Attack(
|
||||
battle::Idx { team: 1, mon: 0 },
|
||||
battle::Idx { team: 0, mon: 0 },
|
||||
));
|
||||
engine.enqueue(battle::Move::Attack(
|
||||
battle::Idx { team: 0, mon: 0 },
|
||||
battle::Idx { team: 1, mon: 0 },
|
||||
));
|
||||
// team0mon0 trades attacks with team1mon0
|
||||
engine.enqueue(battle::Move::Attack(1, 0));
|
||||
engine.enqueue(battle::Move::Attack(0, 1));
|
||||
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(
|
||||
battle::Idx { team: 1, mon: 0 },
|
||||
battle::Idx { team: 0, mon: 0 },
|
||||
));
|
||||
// team0mon0 swaps with team0mon0
|
||||
// team1mon0 attacks team0
|
||||
engine.enqueue(battle::Move::Attack(1, 0));
|
||||
engine.enqueue(battle::Move::Swap(battle::Idx { team: 0, mon: 1 }));
|
||||
engine.exec();
|
||||
|
||||
eprintln!("{:?}", engine.teams()[0]);
|
||||
eprintln!("{:?}", engine.teams()[1]);
|
||||
assert_eq!(6, engine.teams()[0].mons()[0].damage());
|
||||
assert_eq!(1, 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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue