mods
This commit is contained in:
parent
4806a087e3
commit
b3423ad439
103
src/cryp.rs
103
src/cryp.rs
@ -1,22 +1,66 @@
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Debug,Clone,Copy,PartialEq)]
|
||||||
|
pub enum StatKind {
|
||||||
|
Dmg,
|
||||||
|
Def,
|
||||||
|
Hp,
|
||||||
|
Stam,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug,Clone)]
|
||||||
|
pub struct Mod {
|
||||||
|
pub name: String,
|
||||||
|
pub value: u64,
|
||||||
|
pub affects: Vec<StatKind>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug,Clone)]
|
||||||
pub struct Roll {
|
pub struct Roll {
|
||||||
pub roll: u64,
|
pub roll: u64,
|
||||||
pub result: u64,
|
pub result: u64,
|
||||||
// pub stat: Stat,
|
pub kind: StatKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug,Clone)]
|
#[derive(Debug,Clone)]
|
||||||
pub struct Stat {
|
pub struct Stat {
|
||||||
value: u64,
|
pub value: u64,
|
||||||
|
pub kind: StatKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Stat {
|
impl Stat {
|
||||||
fn roll(&self) -> Roll {
|
fn set(&mut self, v: u64) -> &Stat {
|
||||||
|
self.value = v;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn roll(&self, c: &Cryp) -> Roll {
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
let roll: u64 = rng.gen();
|
let roll: u64 = rng.gen();
|
||||||
return Roll { roll, result: self.value & roll };
|
|
||||||
|
println!("{:?}", self.kind);
|
||||||
|
println!("{:064b} <- base roll", roll);
|
||||||
|
|
||||||
|
let active_mods = c.mods.iter().filter(|m|
|
||||||
|
m.affects.iter().any(|a| *a == self.kind)
|
||||||
|
).collect::<Vec<&Mod>>();
|
||||||
|
|
||||||
|
// println!("{:?}", active_mods);
|
||||||
|
let mod_roll = active_mods.iter().fold(roll, |roll, m| {
|
||||||
|
let roll_with_mod = roll | m.value;
|
||||||
|
println!("{:064b} | <- {:?}", m.value, m.name);
|
||||||
|
roll_with_mod
|
||||||
|
});
|
||||||
|
|
||||||
|
println!("{:064b} <- modified roll", mod_roll);
|
||||||
|
|
||||||
|
let result = self.value & mod_roll;
|
||||||
|
println!("{:064b} & <- attribute roll", self.value);
|
||||||
|
println!("{:064b} = {:?}", result, result);
|
||||||
|
println!("");
|
||||||
|
|
||||||
|
return Roll { kind: self.kind, roll, result };
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reduce(&mut self, dmg: u64) -> &mut Stat {
|
fn reduce(&mut self, dmg: u64) -> &mut Stat {
|
||||||
@ -25,14 +69,6 @@ impl Stat {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 struct Turn {
|
||||||
pub dmg: Roll,
|
pub dmg: Roll,
|
||||||
pub def: Roll,
|
pub def: Roll,
|
||||||
@ -49,6 +85,7 @@ pub struct Cryp {
|
|||||||
pub hp: Stat,
|
pub hp: Stat,
|
||||||
pub xp: u64,
|
pub xp: u64,
|
||||||
pub lvl: u8,
|
pub lvl: u8,
|
||||||
|
pub mods: Vec<Mod>,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,12 +99,13 @@ impl Cryp {
|
|||||||
let id = Uuid::new_v4();
|
let id = Uuid::new_v4();
|
||||||
return Cryp {
|
return Cryp {
|
||||||
id,
|
id,
|
||||||
dmg: Stat { value: 0},
|
dmg: Stat { value: 0, kind: StatKind::Dmg },
|
||||||
def: Stat { value: 0},
|
def: Stat { value: 0, kind: StatKind::Def },
|
||||||
stam: Stat { value: 0},
|
stam: Stat { value: 0, kind: StatKind::Stam },
|
||||||
hp: Stat { value: 0},
|
hp: Stat { value: 0, kind: StatKind::Hp },
|
||||||
lvl: 0,
|
lvl: 0,
|
||||||
xp: 0,
|
xp: 0,
|
||||||
|
mods: vec![],
|
||||||
name: String::new()
|
name: String::new()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -82,6 +120,11 @@ impl Cryp {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_mod(&mut self, m: Mod) -> &mut Cryp {
|
||||||
|
self.mods.push(m);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_xp(mut self) -> Cryp {
|
pub fn add_xp(mut self) -> Cryp {
|
||||||
self.xp = self.xp.saturating_add(1);
|
self.xp = self.xp.saturating_add(1);
|
||||||
if self.xp.is_power_of_two() {
|
if self.xp.is_power_of_two() {
|
||||||
@ -97,11 +140,8 @@ impl Cryp {
|
|||||||
|
|
||||||
pub fn turn(&self) -> Turn {
|
pub fn turn(&self) -> Turn {
|
||||||
// println!("{:?}'s turn:", c.name);
|
// println!("{:?}'s turn:", c.name);
|
||||||
let dmg = self.dmg.roll();
|
let dmg = self.dmg.roll(self);
|
||||||
let def = self.def.roll();
|
let def = self.def.roll(self);
|
||||||
|
|
||||||
print_roll(&dmg);
|
|
||||||
print_roll(&def);
|
|
||||||
|
|
||||||
return Turn { dmg, def }
|
return Turn { dmg, def }
|
||||||
}
|
}
|
||||||
@ -116,10 +156,10 @@ impl Cryp {
|
|||||||
|
|
||||||
self.xp = lvl_as_two_pwr;
|
self.xp = lvl_as_two_pwr;
|
||||||
|
|
||||||
self.dmg = Stat { value: rng.gen_range(1, max) };
|
self.dmg.set(rng.gen_range(1, max));
|
||||||
self.def = Stat { value: rng.gen_range(1, max) };
|
self.def.set(rng.gen_range(1, max));
|
||||||
self.stam = Stat { value: rng.gen_range(1, max) };
|
self.stam.set(rng.gen_range(1, max));
|
||||||
self.hp = Stat { value: self.stam.value };
|
self.hp.set(self.stam.value);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,17 +188,26 @@ impl Cryp {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use cryp::{StatKind,Mod};
|
||||||
use *;
|
use *;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_cryp_test() {
|
fn create_cryp_test() {
|
||||||
let level_two = Cryp::new()
|
let newborn = Mod {
|
||||||
|
name: "newborn".to_string(),
|
||||||
|
value: 0,
|
||||||
|
affects: vec![StatKind::Def],
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut level_two = Cryp::new()
|
||||||
.named("hatchling".to_string())
|
.named("hatchling".to_string())
|
||||||
.level(2)
|
.level(2)
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
|
level_two.add_mod(newborn);
|
||||||
// assert!(level_two.dmg <= 2_u64.pow(2));
|
// assert!(level_two.dmg <= 2_u64.pow(2));
|
||||||
// assert!(level_two.def <= 2_u64.pow(2));
|
// assert!(level_two.def <= 2_u64.pow(2));
|
||||||
// assert!(level_two.stam < 2_u64.pow(2));
|
assert_eq!(level_two.mods.len(), 1);
|
||||||
println!("{:?}", level_two);
|
println!("{:?}", level_two);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/lib.rs
12
src/lib.rs
@ -7,14 +7,22 @@ mod battle;
|
|||||||
|
|
||||||
use combat::{battle, levelling};
|
use combat::{battle, levelling};
|
||||||
use battle::{Battle};
|
use battle::{Battle};
|
||||||
use cryp::Cryp;
|
use cryp::{Cryp, Mod, StatKind};
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let a = Cryp::new()
|
let mut a = Cryp::new()
|
||||||
.named("pronounced \"creeep\"".to_string())
|
.named("pronounced \"creeep\"".to_string())
|
||||||
.level(8)
|
.level(8)
|
||||||
.create();
|
.create();
|
||||||
|
|
||||||
|
let stoney = Mod {
|
||||||
|
name: "stoney".to_string(),
|
||||||
|
value: 127,
|
||||||
|
affects: vec![StatKind::Def],
|
||||||
|
};
|
||||||
|
|
||||||
|
a.add_mod(stoney);
|
||||||
|
|
||||||
let b = Cryp::new()
|
let b = Cryp::new()
|
||||||
.named("lemongrass tea".to_string())
|
.named("lemongrass tea".to_string())
|
||||||
.level(8)
|
.level(8)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user