battle module

This commit is contained in:
ntr 2018-08-20 15:19:43 +10:00
parent 0a11d55ef7
commit 8a946acc11
4 changed files with 122 additions and 113 deletions

57
src/battle.rs Executable file
View File

@ -0,0 +1,57 @@
// use uuid::Uuid;
use rand::prelude::*;
use cryp::Cryp;
// impl Attribute {
// pub fn as_str(&self) -> &'static str {
// match self {
// Dmg => "dmg",
// Def => "def",
// }
// }
// }
#[derive(Debug)]
pub struct Battle {
a: Cryp,
b: Cryp,
}
impl Battle {
pub fn new(a: &Cryp, b: &Cryp) -> Battle {
return Battle {
a: a.clone(),
b: b.clone(),
};
}
pub fn cryps(&self) -> Vec<&Cryp> {
vec![&self.a, &self.b]
}
pub fn next(&mut self) -> &mut Battle {
let a_turn = self.a.turn();
let b_turn = self.b.turn();
self.a.assign_dmg(&self.b, &a_turn, &b_turn);
self.b.assign_dmg(&self.a, &b_turn, &a_turn);
self
}
pub fn finished(&self) -> bool {
self.cryps().iter().any(|c| c.hp == 0)
}
pub fn winner(&self) -> Option<&Cryp> {
if self.cryps().iter().all(|c| c.hp == 0) {
return None
}
match self.cryps().iter().find(|c| c.hp > 0) {
Some(w) => Some(w),
None => panic!("no winner found {:?}", self),
}
}
}

View File

@ -1,32 +1,6 @@
use rand::prelude::*; use rand::prelude::*;
use cryp::Cryp; use cryp::Cryp;
use battle::Battle;
pub struct Turn {
pub dmg: Roll,
pub def: Roll,
}
#[derive(Debug)]
pub enum Attribute {
Dmg,
Def,
}
// impl Attribute {
// pub fn as_str(&self) -> &'static str {
// match self {
// Dmg => "dmg",
// Def => "def",
// }
// }
// }
pub struct Roll {
pub value: u64,
pub roll: u64,
pub result: u64,
pub att: Attribute
}
struct Encounter { struct Encounter {
mob: Cryp, mob: Cryp,
@ -34,61 +8,13 @@ struct Encounter {
player: Cryp, player: Cryp,
} }
#[derive(Debug)]
pub struct Battle {
pub a: Cryp,
pub b: Cryp,
// winner: Option<Cryp>,
}
fn att_roll(value: u64, att: Attribute) -> Roll {
let mut rng = thread_rng();
let roll: u64 = rng.gen();
return Roll { att, value, roll, result: value & roll };
}
fn print_roll(r: &Roll) {
println!("{:?}", r.att);
println!("{:064b} &", r.roll);
println!("{:064b}", r.value);
println!("{:064b} - {:?}", r.result, r.result);
println!("");
}
fn cryp_turn(c: &Cryp) -> Turn {
// println!("{:?}'s turn:", c.name);
let dmg = att_roll(c.dmg, Attribute::Dmg);
let def = att_roll(c.def, Attribute::Def);
print_roll(&dmg);
print_roll(&def);
return Turn { dmg, def }
}
pub fn battle(mut a: Cryp, mut b: Cryp) -> Battle {
// println!("battle:",);
// println!("{:?}", cryps[0]);
// println!("{:?}", cryps[1]);
pub fn battle(a: &Cryp, b: &Cryp) -> Battle {
let mut battle = Battle::new(a, b);
loop { loop {
let a_turn = cryp_turn(&a); battle.next();
let b_turn = cryp_turn(&b); if battle.finished() {
break battle
a = a.assign_dmg(&b, &a_turn, &b_turn);
b = b.assign_dmg(&a, &b_turn, &a_turn);
// println!("{:?}", combat);
let mut finished = false;
{
if vec![&a, &b].iter().any(|c| c.hp == 0) {
finished = true
}
}
if finished {
break Battle { a, b };
} }
} }
} }
@ -102,22 +28,16 @@ fn pve(plr: Cryp) -> Encounter {
.level(mob_lvl) .level(mob_lvl)
.create(); .create();
// have to reassign here because battle takes ownership let outcome = battle(&plr, &mob);
let Battle { a: plr, b: mob } = battle(plr, mob);
let win = vec![&plr, &mob].iter().any(|c| c.id == plr.id && c.hp > 0); let success = match outcome.winner() {
Some(c) => c.id == plr.id,
if win { None => false,
return Encounter { };
mob: mob,
success: true,
player: plr,
};
}
return Encounter { return Encounter {
mob: mob, mob: mob,
success: false, success,
player: plr, player: plr,
}; };
} }

View File

@ -1,7 +1,37 @@
use uuid::Uuid; use uuid::Uuid;
use rand::prelude::*; use rand::prelude::*;
use combat::{Turn}; #[derive(Debug)]
pub enum Attribute {
Dmg,
Def,
}
pub struct Roll {
pub value: u64,
pub roll: u64,
pub result: u64,
pub att: Attribute
}
fn att_roll(value: u64, att: Attribute) -> Roll {
let mut rng = thread_rng();
let roll: u64 = rng.gen();
return Roll { att, value, roll, result: value & roll };
}
fn print_roll(r: &Roll) {
println!("{:?}", r.att);
println!("{:064b} &", r.roll);
println!("{:064b}", r.value);
println!("{:064b} - {:?}", r.result, r.result);
println!("");
}
pub struct Turn {
pub dmg: Roll,
pub def: Roll,
}
#[derive(Debug,Clone)] #[derive(Debug,Clone)]
pub struct Cryp { pub struct Cryp {
@ -60,6 +90,18 @@ impl Cryp {
self.create() self.create()
} }
pub fn turn(&self) -> Turn {
// println!("{:?}'s turn:", c.name);
let dmg = att_roll(self.dmg, Attribute::Dmg);
let def = att_roll(self.def, Attribute::Def);
print_roll(&dmg);
print_roll(&def);
return Turn { dmg, def }
}
pub fn create(mut self) -> Cryp { pub fn create(mut self) -> Cryp {
let mut rng = thread_rng(); let mut rng = thread_rng();
let lvl_as_two_pwr = 2_u64.pow(self.lvl.into()); let lvl_as_two_pwr = 2_u64.pow(self.lvl.into());
@ -77,7 +119,7 @@ impl Cryp {
self self
} }
pub fn assign_dmg(mut self, opp: &Cryp, plr_t: &Turn, opp_t: &Turn) -> Cryp { pub fn assign_dmg(&mut self, opp: &Cryp, plr_t: &Turn, opp_t: &Turn) -> &mut Cryp {
let final_dmg = opp_t.dmg.result.saturating_sub(plr_t.def.result); let final_dmg = opp_t.dmg.result.saturating_sub(plr_t.def.result);
let blocked = opp_t.dmg.result.saturating_sub(final_dmg); let blocked = opp_t.dmg.result.saturating_sub(final_dmg);
let hp = self.hp.saturating_sub(final_dmg); let hp = self.hp.saturating_sub(final_dmg);

View File

@ -3,40 +3,30 @@ extern crate uuid;
mod cryp; mod cryp;
mod combat; mod combat;
mod battle;
use combat::{Battle, battle, levelling}; use combat::{battle, levelling};
use battle::{Battle};
use cryp::Cryp; use cryp::Cryp;
pub fn main() { pub fn main() {
let mut a = Cryp::new() let a = Cryp::new()
.named("pronounced \"creeep\"".to_string()) .named("pronounced \"creeep\"".to_string())
.level(8) .level(8)
.create(); .create();
let mut b = Cryp::new() let b = Cryp::new()
.named("lemongrass tea".to_string()) .named("lemongrass tea".to_string())
.level(8) .level(8)
.create(); .create();
let battle = battle(a, b); let outcome = battle(&a, &b);
let r_a = &battle.a; match outcome.winner() {
let r_b = &battle.b; Some(w) => println!("{:?} is the winner with {:?} hp remaining", w.name, w.hp),
None => println!("{:?} was a draw", outcome),
{
let draw = vec!(r_a, r_b).iter().all(|c| c.hp == 0);
if draw {
println!("{:?} was a draw", battle);
return;
}
}
let winner = match vec![r_a, r_b].iter().find(|c| c.hp > 0) {
Some(w) => *w,
None => panic!("no winner found {:?}", battle),
}; };
println!("{:?} is the winner with {:?} hp remaining", winner.name, winner.hp);
return return
} }