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 cryp::Cryp;
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
}
use battle::Battle;
struct Encounter {
mob: Cryp,
@ -34,61 +8,13 @@ struct Encounter {
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 {
let a_turn = cryp_turn(&a);
let b_turn = cryp_turn(&b);
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 };
battle.next();
if battle.finished() {
break battle
}
}
}
@ -102,22 +28,16 @@ fn pve(plr: Cryp) -> Encounter {
.level(mob_lvl)
.create();
// have to reassign here because battle takes ownership
let Battle { a: plr, b: mob } = battle(plr, mob);
let outcome = battle(&plr, &mob);
let win = vec![&plr, &mob].iter().any(|c| c.id == plr.id && c.hp > 0);
if win {
return Encounter {
mob: mob,
success: true,
player: plr,
};
}
let success = match outcome.winner() {
Some(c) => c.id == plr.id,
None => false,
};
return Encounter {
mob: mob,
success: false,
success,
player: plr,
};
}

View File

@ -1,7 +1,37 @@
use uuid::Uuid;
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)]
pub struct Cryp {
@ -60,6 +90,18 @@ impl Cryp {
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 {
let mut rng = thread_rng();
let lvl_as_two_pwr = 2_u64.pow(self.lvl.into());
@ -77,7 +119,7 @@ impl Cryp {
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 blocked = opp_t.dmg.result.saturating_sub(final_dmg);
let hp = self.hp.saturating_sub(final_dmg);

View File

@ -3,40 +3,30 @@ extern crate uuid;
mod cryp;
mod combat;
mod battle;
use combat::{Battle, battle, levelling};
use combat::{battle, levelling};
use battle::{Battle};
use cryp::Cryp;
pub fn main() {
let mut a = Cryp::new()
let a = Cryp::new()
.named("pronounced \"creeep\"".to_string())
.level(8)
.create();
let mut b = Cryp::new()
let b = Cryp::new()
.named("lemongrass tea".to_string())
.level(8)
.create();
let battle = battle(a, b);
let outcome = battle(&a, &b);
let r_a = &battle.a;
let r_b = &battle.b;
{
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),
match outcome.winner() {
Some(w) => println!("{:?} is the winner with {:?} hp remaining", w.name, w.hp),
None => println!("{:?} was a draw", outcome),
};
println!("{:?} is the winner with {:?} hp remaining", winner.name, winner.hp);
return
}