move healing and spell damage calculations into cryp"

git push
"
This commit is contained in:
ntr 2018-11-14 17:50:28 +11:00
parent 971c0dd03d
commit ecabde3389
3 changed files with 61 additions and 44 deletions

View File

@ -49,7 +49,7 @@ pub enum Stat {
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub struct CrypStat { pub struct CrypStat {
pub value: u64, value: u64,
pub stat: Stat, pub stat: Stat,
} }
@ -119,7 +119,6 @@ impl Cryp {
self self
} }
pub fn level(mut self, lvl: u8) -> Cryp { pub fn level(mut self, lvl: u8) -> Cryp {
self.lvl = check_lvl(lvl); self.lvl = check_lvl(lvl);
self self
@ -236,6 +235,47 @@ impl Cryp {
self.hp.set(self.stamina.value); self.hp.set(self.stamina.value);
self self
} }
// Stats
pub fn phys_dmg(&self) -> u64 {
self.phys_dmg.value
}
pub fn spell_dmg(&self) -> u64 {
self.spell_dmg.value
}
pub fn hp(&self) -> u64 {
self.hp.value
}
pub fn stamina(&self) -> u64 {
self.stamina.value
}
// Stat modifications
pub fn heal(&mut self, amount: u64) -> (u64, u64) {
let current_hp = self.hp();
let new_hp = *[
self.hp().saturating_add(amount),
self.stamina()
].iter().min().unwrap();
let healing = new_hp - current_hp;
let overhealing = amount - healing;
return (healing, overhealing);
}
pub fn deal_phys_dmg(&mut self, amount: u64) -> (u64, u64) {
self.hp.reduce(amount);
return (amount, 0);
}
pub fn deal_spell_dmg(&mut self, amount: u64) -> (u64, u64) {
self.hp.reduce(amount);
return (amount, 0);
}
} }
pub fn cryp_get(tx: &mut Transaction, id: Uuid, account_id: Uuid) -> Result<Cryp, Error> { pub fn cryp_get(tx: &mut Transaction, id: Uuid, account_id: Uuid) -> Result<Cryp, Error> {

View File

@ -618,7 +618,7 @@ pub fn game_pve(params: GamePveParams, tx: &mut Transaction, account: &Account)
let plr: Cryp = from_slice::<Cryp>(&cryp_bytes)?; let plr: Cryp = from_slice::<Cryp>(&cryp_bytes)?;
// TEMP // TEMP
if plr.hp.value == 0 { if plr.is_ko() {
return Err(err_msg("cryp is ko")); return Err(err_msg("cryp is ko"));
// plr.rez(); // plr.rez();
} }

View File

@ -470,9 +470,9 @@ impl Skill {
// println!("{:?}'s stats", self.name); // println!("{:?}'s stats", self.name);
// println!("{:064b} <- finalised", roll.result); // println!("{:064b} <- finalised", roll.result);
// roll.result = roll.result & stat.value; // roll.result = roll.result & stat();
// println!("{:064b} & <- attribute roll", stat.value); // println!("{:064b} & <- attribute roll", stat());
// println!("{:064b} = {:?}", roll.result, roll.result); // println!("{:064b} = {:?}", roll.result, roll.result);
// println!(""); // println!("");
@ -589,8 +589,8 @@ impl Skill {
} }
fn attack(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) { fn attack(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
target.deal_phys_dmg(cryp.phys_dmg());
log.push(format!("{:?} -> {:?} | Attack for {:?}", cryp.name, target.name, cryp.phys_dmg)); log.push(format!("{:?} -> {:?} | Attack for {:?}", cryp.name, target.name, cryp.phys_dmg));
target.hp.reduce(cryp.phys_dmg.value);
} }
fn stun(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) { fn stun(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
@ -630,14 +630,7 @@ fn snare(_cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
} }
fn heal(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) { fn heal(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
let new_hp = *[ let (healing, overhealing) = target.heal(cryp.phys_dmg());
target.hp.value.saturating_add(cryp.spell_dmg.value),
target.stamina.value
].iter().min().unwrap();
let healing = new_hp.saturating_sub(target.hp.value);
let overhealing = target.hp.value.saturating_add(cryp.phys_dmg.value).saturating_sub(target.stamina.value);
target.hp.value = new_hp;
log.push(format!("{:?} -> {:?} | Heal for {:?} ({:?} OH)", cryp.name, target.name, healing, overhealing)); log.push(format!("{:?} -> {:?} | Heal for {:?} ({:?} OH)", cryp.name, target.name, healing, overhealing));
} }
@ -652,22 +645,15 @@ fn triage(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
} }
fn triage_tick(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) { fn triage_tick(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
let new_hp = *[ let amount = cryp.spell_dmg().wrapping_div(2);
target.hp.value.saturating_add(cryp.spell_dmg.value), let (healing, overhealing) = target.heal(amount);
target.stamina.value
].iter().min().unwrap();
let healing = new_hp.saturating_sub(target.hp.value);
let overhealing = target.hp.value.saturating_add(cryp.phys_dmg.value).saturating_sub(target.stamina.value);
target.hp.value = new_hp;
log.push(format!("{:?} -> {:?} | Triage for {:?} ({:?} OH)", cryp.name, target.name, healing, overhealing)); log.push(format!("{:?} -> {:?} | Triage for {:?} ({:?} OH)", cryp.name, target.name, healing, overhealing));
target.hp.value = new_hp;
} }
fn blast(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) { fn blast(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
let amount = cryp.spell_dmg.value; let amount = cryp.spell_dmg();
log.push(format!("{:?} -> {:?} | Blast for {:?}", cryp.name, target.name, amount)); log.push(format!("{:?} -> {:?} | Blast for {:?}", cryp.name, target.name, amount));
target.hp.reduce(amount); target.deal_spell_dmg(amount);
} }
fn amplify(_cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) { fn amplify(_cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
@ -687,9 +673,9 @@ fn decay(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
} }
fn decay_tick(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) { fn decay_tick(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
let amount = cryp.spell_dmg.value; let amount = cryp.spell_dmg();
log.push(format!("{:?} -> {:?} | Decay for {:?}", cryp.name, target.name, amount)); log.push(format!("{:?} -> {:?} | Decay for {:?}", cryp.name, target.name, amount));
target.hp.reduce(amount); target.deal_spell_dmg(amount);
} }
fn hex(_cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) { fn hex(_cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
@ -716,19 +702,10 @@ fn drain(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
fn drain_tick(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) { fn drain_tick(cryp: &mut Cryp, target: &mut Cryp, log: &mut Log) {
// damage part // damage part
let damage = cryp.spell_dmg.value; let (damage, _) = target.deal_spell_dmg(cryp.spell_dmg().wrapping_div(2));
target.hp.reduce(damage);
log.push(format!("{:?} | Drain damage {:?}", target.name, damage)); log.push(format!("{:?} | Drain damage {:?}", target.name, damage));
// healing part let (healing, overhealing) = target.heal(damage);
let new_hp = *[
cryp.hp.value.saturating_add(damage),
cryp.stamina.value
].iter().min().unwrap();
cryp.hp.value = new_hp;
let healing = new_hp.saturating_sub(cryp.hp.value);
let overhealing = cryp.hp.value + damage - cryp.stamina.value;
log.push(format!("{:?} | Drain healing {:?} ({:?} OH)", cryp.name, healing, overhealing)); log.push(format!("{:?} | Drain healing {:?} ({:?} OH)", cryp.name, healing, overhealing));
} }
@ -763,7 +740,7 @@ mod tests {
.learn(Skill::Heal) .learn(Skill::Heal)
.create(); .create();
x.hp.reduce(5); x.deal_phys_dmg(5);
let mut log = vec![]; let mut log = vec![];
heal(&mut y, &mut x, &mut log); heal(&mut y, &mut x, &mut log);
@ -788,7 +765,7 @@ mod tests {
y.reduce_effect_durations(&mut log); y.reduce_effect_durations(&mut log);
let decay = y.effects.iter().find(|e| e.effect == Effect::Decay); let decay = y.effects.iter().find(|e| e.effect == Effect::Decay);
// assert!(y.hp.value == y.stamina.value.saturating_sub(decay.unwrap().tick.unwrap().amount)); // assert!(y.hp() == y.stamina().saturating_sub(decay.unwrap().tick.unwrap().amount));
} }
#[test] #[test]
@ -806,17 +783,17 @@ mod tests {
let mut log = vec![]; let mut log = vec![];
// ensure it doesn't have 0 sd // ensure it doesn't have 0 sd
x.spell_dmg.value = 50; x.spell_dmg.set(50);
y.hp.reduce(5); y.deal_phys_dmg(5);
let prev_hp = y.hp.value; let prev_hp = y.hp();
triage(&mut x, &mut y, &mut log); triage(&mut x, &mut y, &mut log);
assert!(y.effects.iter().any(|e| e.effect == Effect::Triage)); assert!(y.effects.iter().any(|e| e.effect == Effect::Triage));
// y.reduce_effect_durations(&mut log); // y.reduce_effect_durations(&mut log);
// assert!(y.hp.value > prev_hp); // assert!(y.hp() > prev_hp);
} }
#[test] #[test]