move apply damage onto cryp struct

This commit is contained in:
ntr 2018-08-19 22:22:23 +10:00
parent 9ea8361ba9
commit c42bc386dd
3 changed files with 71 additions and 76 deletions

85
src/combat.rs Normal file → Executable file
View File

@ -1,13 +1,13 @@
use rand::prelude::*;
use cryp::Cryp;
struct Turn {
dmg: Roll,
def: Roll,
pub struct Turn {
pub dmg: Roll,
pub def: Roll,
}
#[derive(Debug)]
enum Attribute {
pub enum Attribute {
Dmg,
Def,
}
@ -21,22 +21,23 @@ enum Attribute {
// }
// }
struct Roll {
value: u64,
roll: u64,
result: u64,
att: Attribute
pub struct Roll {
pub value: u64,
pub roll: u64,
pub result: u64,
pub att: Attribute
}
struct Encounter {
mob: Cryp,
success: bool,
player: 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 };
return Roll { att, value, roll, result: value & roll };
}
fn print_roll(r: &Roll) {
@ -58,58 +59,26 @@ fn cryp_turn(c: &Cryp) -> Turn {
return Turn { dmg, def }
}
fn assign_dmg(plr: &Cryp, opp: &Cryp, plr_t: &Turn, opp_t: &Turn) -> 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 new_stam = plr.stam.saturating_sub(final_dmg);
println!("{:?} deals {:?} dmg to {:?} ({:?} blocked / {:?} hp remaining)"
,opp.name
,final_dmg
,plr.name
,blocked
,new_stam);
return Cryp {
dmg: plr.dmg,
def: plr.def,
stam: new_stam,
xp: plr.xp,
lvl: plr.lvl,
id: plr.id,
name: plr.name.clone()
};
}
fn combat_phase(a: &Cryp, b: &Cryp) -> Vec<Cryp> {
let a_turn = cryp_turn(&a);
let b_turn = cryp_turn(&b);
return vec![
assign_dmg(&a, &b, &a_turn, &b_turn),
assign_dmg(&b, &a, &b_turn, &a_turn),
];
}
pub fn battle(a: &Cryp, b: &Cryp) -> Vec<Cryp> {
let mut cryps = vec![
a.clone(),
b.clone()
];
pub fn battle(mut a: Cryp, mut b: Cryp) -> (Cryp, Cryp) {
// println!("battle:",);
// println!("{:?}", cryps[0]);
// println!("{:?}", cryps[1]);
loop {
let combat = combat_phase(&cryps[0], &cryps[1]);
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);
if combat.iter().any(|c| c.stam == 0) {
return combat;
if vec![&a, &b].iter().any(|c| c.hp == 0) {
break (a, b);
}
cryps = combat;
}
}
fn pve(plr: &Cryp) -> Encounter {
fn pve(plr: Cryp) -> Encounter {
let mut rng = thread_rng();
let mob_lvl: u8 = rng.gen_range(1, plr.lvl);
@ -118,25 +87,29 @@ fn pve(plr: &Cryp) -> Encounter {
.level(mob_lvl)
.create();
let battle = battle(plr, &mob);
let win = battle.iter().any(|c| c.id == plr.id && c.stam > 0);
// have to reassign here because battle takes ownership
let (plr, mob) = 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,
};
}
return Encounter {
mob: mob,
success: false,
player: plr,
};
}
pub fn levelling(mut c: Cryp) -> Cryp {
loop {
let enc = pve(&c);
let enc = pve(c);
c = enc.player;
if !enc.success {
println!("{:?} has been KO'd", c.name);

22
src/cryp.rs Normal file → Executable file
View File

@ -1,6 +1,8 @@
use uuid::Uuid;
use rand::prelude::*;
use combat::{Turn};
#[derive(Debug,Clone)]
pub struct Cryp {
pub id: Uuid,
@ -9,6 +11,7 @@ pub struct Cryp {
pub dmg: u64,
pub def: u64,
pub stam: u64,
pub hp: u64,
pub xp: u64,
pub lvl: u8,
pub name: String,
@ -27,6 +30,7 @@ impl Cryp {
dmg: 0,
def: 0,
stam: 0,
hp: 0,
lvl: 0,
xp: 0,
name: String::new()
@ -69,8 +73,26 @@ impl Cryp {
self.dmg = rng.gen_range(1, max);
self.def = rng.gen_range(1, max);
self.stam = rng.gen_range(1, max);
self.hp = self.stam;
self
}
pub fn assign_dmg(mut self, opp: &Cryp, plr_t: &Turn, opp_t: &Turn) -> 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);
println!("{:?} deals {:?} dmg to {:?} ({:?} blocked / {:?} hp remaining)"
,opp.name
,final_dmg
,self.name
,blocked
,hp);
self.hp = hp;
self
}
}

40
src/lib.rs Normal file → Executable file
View File

@ -8,30 +8,30 @@ use combat::{battle, levelling};
use cryp::Cryp;
pub fn main() {
let a = Cryp::new()
.named("pronounced \"creeep\"".to_string())
.level(8)
.create();
// let a = Cryp::new()
// .named("pronounced \"creeep\"".to_string())
// .level(8)
// .create();
let b = Cryp::new()
.named("lemongrass tea".to_string())
.level(8)
.create();
// let b = Cryp::new()
// .named("lemongrass tea".to_string())
// .level(8)
// .create();
let battle = battle(&a, &b);
// let battle = battle(a, b);
// let b_vec = vec!(battle.0, battle.1);
// let draw = b_vec.iter().all(|c| c.hp == 0);
// if draw {
// println!("{:?} was a draw", battle);
// return;
// }
let draw = battle.iter().all(|c| c.stam == 0);
if draw {
println!("{:?} was a draw", battle);
return;
}
// let winner = match b_vec.iter().find(|c| c.hp > 0) {
// Some(w) => w,
// None => panic!("no winner found {:?}", battle),
// };
let winner = match battle.iter().find(|c| c.stam > 0) {
Some(w) => w,
None => panic!("no winner found {:?}", battle),
};
println!("{:?} is the winner with {:?} hp remaining", winner.name, winner.stam);
// println!("{:?} is the winner with {:?} hp remaining", winner.name, winner.hp);
return;
}