From ecabde3389b088782388e1e44ceb5285e2ba37c3 Mon Sep 17 00:00:00 2001 From: ntr Date: Wed, 14 Nov 2018 17:50:28 +1100 Subject: [PATCH] move healing and spell damage calculations into cryp" git push " --- server/src/cryp.rs | 44 +++++++++++++++++++++++++++++++-- server/src/game.rs | 2 +- server/src/skill.rs | 59 ++++++++++++++------------------------------- 3 files changed, 61 insertions(+), 44 deletions(-) diff --git a/server/src/cryp.rs b/server/src/cryp.rs index 6d1b0a0c..815be7fc 100755 --- a/server/src/cryp.rs +++ b/server/src/cryp.rs @@ -49,7 +49,7 @@ pub enum Stat { #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] pub struct CrypStat { - pub value: u64, + value: u64, pub stat: Stat, } @@ -119,7 +119,6 @@ impl Cryp { self } - pub fn level(mut self, lvl: u8) -> Cryp { self.lvl = check_lvl(lvl); self @@ -236,6 +235,47 @@ impl Cryp { self.hp.set(self.stamina.value); 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 { diff --git a/server/src/game.rs b/server/src/game.rs index d96d0b0a..877d942b 100755 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -618,7 +618,7 @@ pub fn game_pve(params: GamePveParams, tx: &mut Transaction, account: &Account) let plr: Cryp = from_slice::(&cryp_bytes)?; // TEMP - if plr.hp.value == 0 { + if plr.is_ko() { return Err(err_msg("cryp is ko")); // plr.rez(); } diff --git a/server/src/skill.rs b/server/src/skill.rs index c2285456..8f2963c5 100755 --- a/server/src/skill.rs +++ b/server/src/skill.rs @@ -470,9 +470,9 @@ impl Skill { // println!("{:?}'s stats", self.name); // 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!(""); @@ -589,8 +589,8 @@ impl Skill { } 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)); - target.hp.reduce(cryp.phys_dmg.value); } 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) { - let new_hp = *[ - 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; + let (healing, overhealing) = target.heal(cryp.phys_dmg()); 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) { - let new_hp = *[ - 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; + let amount = cryp.spell_dmg().wrapping_div(2); + let (healing, overhealing) = target.heal(amount); 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) { - let amount = cryp.spell_dmg.value; + let amount = cryp.spell_dmg(); 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) { @@ -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) { - let amount = cryp.spell_dmg.value; + let amount = cryp.spell_dmg(); 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) { @@ -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) { // damage part - let damage = cryp.spell_dmg.value; - target.hp.reduce(damage); + let (damage, _) = target.deal_spell_dmg(cryp.spell_dmg().wrapping_div(2)); log.push(format!("{:?} | Drain damage {:?}", target.name, damage)); - // healing part - 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; + let (healing, overhealing) = target.heal(damage); log.push(format!("{:?} | Drain healing {:?} ({:?} OH)", cryp.name, healing, overhealing)); } @@ -763,7 +740,7 @@ mod tests { .learn(Skill::Heal) .create(); - x.hp.reduce(5); + x.deal_phys_dmg(5); let mut log = vec![]; heal(&mut y, &mut x, &mut log); @@ -788,7 +765,7 @@ mod tests { y.reduce_effect_durations(&mut log); 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] @@ -806,17 +783,17 @@ mod tests { let mut log = vec![]; // ensure it doesn't have 0 sd - x.spell_dmg.value = 50; - y.hp.reduce(5); + x.spell_dmg.set(50); + y.deal_phys_dmg(5); - let prev_hp = y.hp.value; + let prev_hp = y.hp(); triage(&mut x, &mut y, &mut log); assert!(y.effects.iter().any(|e| e.effect == Effect::Triage)); // y.reduce_effect_durations(&mut log); - // assert!(y.hp.value > prev_hp); + // assert!(y.hp() > prev_hp); } #[test]