2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-10-17 22:42:43 +00:00
|
|
|
use crate::{
|
2021-01-08 19:12:09 +00:00
|
|
|
comp::{
|
2021-04-04 03:04:02 +00:00
|
|
|
buff::{Buff, BuffChange, BuffData, BuffKind, BuffSource},
|
2021-01-08 20:53:52 +00:00
|
|
|
inventory::{
|
2021-01-16 17:01:57 +00:00
|
|
|
item::{
|
|
|
|
armor::Protection,
|
2021-03-30 16:16:20 +00:00
|
|
|
tool::{self, Tool, ToolKind},
|
2021-03-30 20:15:37 +00:00
|
|
|
Item, ItemDesc, ItemKind, MaterialStatManifest,
|
2021-01-16 17:01:57 +00:00
|
|
|
},
|
2021-01-08 20:53:52 +00:00
|
|
|
slot::EquipSlot,
|
|
|
|
},
|
2021-01-30 04:40:03 +00:00
|
|
|
poise::PoiseChange,
|
2021-03-30 16:16:20 +00:00
|
|
|
skills::SkillGroupKind,
|
2021-03-02 22:19:38 +00:00
|
|
|
Body, Combo, Energy, EnergyChange, EnergySource, Health, HealthChange, HealthSource,
|
|
|
|
Inventory, Stats,
|
2021-01-08 19:12:09 +00:00
|
|
|
},
|
2021-01-26 03:53:52 +00:00
|
|
|
event::ServerEvent,
|
2021-04-04 03:04:02 +00:00
|
|
|
outcome::Outcome,
|
2020-12-13 17:11:55 +00:00
|
|
|
uid::Uid,
|
2020-10-18 18:21:58 +00:00
|
|
|
util::Dir,
|
2020-10-17 22:42:43 +00:00
|
|
|
};
|
2021-02-17 13:03:20 +00:00
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-26 03:53:52 +00:00
|
|
|
use rand::{thread_rng, Rng};
|
2021-02-17 13:03:20 +00:00
|
|
|
|
2020-10-17 22:42:43 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-02-17 13:03:20 +00:00
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-26 03:53:52 +00:00
|
|
|
use specs::Entity as EcsEntity;
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-28 01:44:49 +00:00
|
|
|
use std::time::Duration;
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))] use vek::*;
|
2020-10-17 22:42:43 +00:00
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-11-02 00:26:01 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub enum GroupTarget {
|
|
|
|
InGroup,
|
|
|
|
OutOfGroup,
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-02-02 18:02:40 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct AttackerInfo<'a> {
|
|
|
|
pub entity: EcsEntity,
|
|
|
|
pub uid: Uid,
|
|
|
|
pub energy: Option<&'a Energy>,
|
2021-03-02 22:19:38 +00:00
|
|
|
pub combo: Option<&'a Combo>,
|
2021-02-02 18:02:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 20:02:03 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
pub struct TargetInfo<'a> {
|
|
|
|
pub entity: EcsEntity,
|
|
|
|
pub inventory: Option<&'a Inventory>,
|
|
|
|
pub stats: Option<&'a Stats>,
|
2021-03-20 20:26:10 +00:00
|
|
|
pub health: Option<&'a Health>,
|
2021-04-04 03:04:02 +00:00
|
|
|
pub pos: Vec3<f32>,
|
2021-02-28 20:02:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-29 01:37:33 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)] // TODO: Yeet clone derive
|
2021-01-26 00:01:07 +00:00
|
|
|
pub struct Attack {
|
2021-02-02 18:02:40 +00:00
|
|
|
damages: Vec<AttackDamage>,
|
|
|
|
effects: Vec<AttackEffect>,
|
2021-01-26 03:53:52 +00:00
|
|
|
crit_chance: f32,
|
|
|
|
crit_multiplier: f32,
|
2021-01-26 00:01:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-26 00:01:07 +00:00
|
|
|
impl Default for Attack {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
damages: Vec::new(),
|
|
|
|
effects: Vec::new(),
|
|
|
|
crit_chance: 0.0,
|
|
|
|
crit_multiplier: 1.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-26 00:01:07 +00:00
|
|
|
impl Attack {
|
2021-02-02 18:02:40 +00:00
|
|
|
pub fn with_damage(mut self, damage: AttackDamage) -> Self {
|
2021-01-26 00:01:07 +00:00
|
|
|
self.damages.push(damage);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:02:40 +00:00
|
|
|
pub fn with_effect(mut self, effect: AttackEffect) -> Self {
|
2021-01-26 00:01:07 +00:00
|
|
|
self.effects.push(effect);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:02:40 +00:00
|
|
|
pub fn with_crit(mut self, crit_chance: f32, crit_multiplier: f32) -> Self {
|
|
|
|
self.crit_chance = crit_chance;
|
|
|
|
self.crit_multiplier = crit_multiplier;
|
2021-01-26 00:01:07 +00:00
|
|
|
self
|
|
|
|
}
|
2021-01-26 02:50:16 +00:00
|
|
|
|
2021-02-27 19:55:06 +00:00
|
|
|
pub fn with_combo_increment(self) -> Self {
|
|
|
|
self.with_effect(
|
|
|
|
AttackEffect::new(None, CombatEffect::Combo(1))
|
|
|
|
.with_requirement(CombatRequirement::AnyDamage),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:02:40 +00:00
|
|
|
pub fn effects(&self) -> impl Iterator<Item = &AttackEffect> { self.effects.iter() }
|
2021-01-31 17:15:28 +00:00
|
|
|
|
2021-01-28 01:54:45 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2021-01-26 03:53:52 +00:00
|
|
|
pub fn apply_attack(
|
|
|
|
&self,
|
|
|
|
target_group: GroupTarget,
|
2021-02-28 20:02:03 +00:00
|
|
|
attacker: Option<AttackerInfo>,
|
|
|
|
target: TargetInfo,
|
2021-01-26 03:53:52 +00:00
|
|
|
dir: Dir,
|
2021-01-28 01:54:45 +00:00
|
|
|
target_dodging: bool,
|
2021-01-31 16:40:04 +00:00
|
|
|
// Currently just modifies damage, maybe look into modifying strength of other effects?
|
|
|
|
strength_modifier: f32,
|
2021-02-02 18:02:40 +00:00
|
|
|
mut emit: impl FnMut(ServerEvent),
|
2021-04-04 03:04:02 +00:00
|
|
|
mut emit_outcome: impl FnMut(Outcome),
|
2021-02-02 18:02:40 +00:00
|
|
|
) {
|
2021-01-26 03:53:52 +00:00
|
|
|
let is_crit = thread_rng().gen::<f32>() < self.crit_chance;
|
|
|
|
let mut accumulated_damage = 0.0;
|
|
|
|
for damage in self
|
|
|
|
.damages
|
|
|
|
.iter()
|
|
|
|
.filter(|d| d.target.map_or(true, |t| t == target_group))
|
2021-01-28 01:54:45 +00:00
|
|
|
.filter(|d| !(matches!(d.target, Some(GroupTarget::OutOfGroup)) && target_dodging))
|
2021-01-26 03:53:52 +00:00
|
|
|
{
|
2021-02-28 20:02:03 +00:00
|
|
|
let damage_reduction = Damage::compute_damage_reduction(target.inventory, target.stats);
|
2021-02-02 18:02:40 +00:00
|
|
|
let change = damage.damage.calculate_health_change(
|
2021-02-28 20:02:03 +00:00
|
|
|
damage_reduction,
|
|
|
|
attacker.map(|a| a.uid),
|
2021-01-28 01:44:49 +00:00
|
|
|
is_crit,
|
|
|
|
self.crit_multiplier,
|
2021-01-31 16:40:04 +00:00
|
|
|
strength_modifier,
|
2021-01-28 01:44:49 +00:00
|
|
|
);
|
2021-01-31 17:33:22 +00:00
|
|
|
let applied_damage = -change.amount as f32;
|
|
|
|
accumulated_damage += applied_damage;
|
2021-04-04 03:04:02 +00:00
|
|
|
emit_outcome(Outcome::Damage { pos: target.pos });
|
2021-01-26 03:53:52 +00:00
|
|
|
if change.amount != 0 {
|
2021-02-02 18:02:40 +00:00
|
|
|
emit(ServerEvent::Damage {
|
2021-02-28 20:02:03 +00:00
|
|
|
entity: target.entity,
|
2021-01-26 03:53:52 +00:00
|
|
|
change,
|
|
|
|
});
|
|
|
|
for effect in damage.effects.iter() {
|
|
|
|
match effect {
|
2021-02-02 18:02:40 +00:00
|
|
|
CombatEffect::Knockback(kb) => {
|
2021-01-26 03:53:52 +00:00
|
|
|
let impulse = kb.calculate_impulse(dir);
|
|
|
|
if !impulse.is_approx_zero() {
|
2021-02-02 18:02:40 +00:00
|
|
|
emit(ServerEvent::Knockback {
|
2021-02-28 20:02:03 +00:00
|
|
|
entity: target.entity,
|
2021-01-26 03:53:52 +00:00
|
|
|
impulse,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2021-02-02 18:02:40 +00:00
|
|
|
CombatEffect::EnergyReward(ec) => {
|
2021-02-28 20:02:03 +00:00
|
|
|
if let Some(attacker_entity) = attacker.map(|a| a.entity) {
|
2021-02-02 18:02:40 +00:00
|
|
|
emit(ServerEvent::EnergyChange {
|
2021-02-01 02:43:26 +00:00
|
|
|
entity: attacker_entity,
|
|
|
|
change: EnergyChange {
|
|
|
|
amount: *ec as i32,
|
|
|
|
source: EnergySource::HitEnemy,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-01-26 17:58:52 +00:00
|
|
|
},
|
2021-02-02 18:02:40 +00:00
|
|
|
CombatEffect::Buff(b) => {
|
2021-01-28 01:44:49 +00:00
|
|
|
if thread_rng().gen::<f32>() < b.chance {
|
2021-02-02 18:02:40 +00:00
|
|
|
emit(ServerEvent::Buff {
|
2021-02-28 20:02:03 +00:00
|
|
|
entity: target.entity,
|
2021-01-28 01:44:49 +00:00
|
|
|
buff_change: BuffChange::Add(
|
2021-02-28 20:02:03 +00:00
|
|
|
b.to_buff(attacker.map(|a| a.uid), applied_damage),
|
2021-01-28 01:44:49 +00:00
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2021-02-02 18:02:40 +00:00
|
|
|
CombatEffect::Lifesteal(l) => {
|
2021-02-28 20:02:03 +00:00
|
|
|
if let Some(attacker_entity) = attacker.map(|a| a.entity) {
|
2021-02-01 02:43:26 +00:00
|
|
|
let change = HealthChange {
|
|
|
|
amount: (applied_damage * l) as i32,
|
2021-02-02 18:02:40 +00:00
|
|
|
cause: HealthSource::Heal {
|
2021-02-28 20:02:03 +00:00
|
|
|
by: attacker.map(|a| a.uid),
|
2021-02-02 18:02:40 +00:00
|
|
|
},
|
2021-02-01 02:43:26 +00:00
|
|
|
};
|
2021-02-02 18:02:40 +00:00
|
|
|
if change.amount != 0 {
|
|
|
|
emit(ServerEvent::Damage {
|
|
|
|
entity: attacker_entity,
|
|
|
|
change,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CombatEffect::Poise(p) => {
|
2021-02-28 20:02:03 +00:00
|
|
|
let change = PoiseChange::from_value(*p, target.inventory);
|
2021-02-02 18:02:40 +00:00
|
|
|
if change.amount != 0 {
|
|
|
|
emit(ServerEvent::PoiseChange {
|
2021-02-28 20:02:03 +00:00
|
|
|
entity: target.entity,
|
2021-02-01 02:43:26 +00:00
|
|
|
change,
|
2021-02-02 18:02:40 +00:00
|
|
|
kb_dir: *dir,
|
2021-02-01 02:43:26 +00:00
|
|
|
});
|
|
|
|
}
|
2021-01-29 01:55:43 +00:00
|
|
|
},
|
2021-02-02 18:02:40 +00:00
|
|
|
CombatEffect::Heal(h) => {
|
2021-01-30 05:03:23 +00:00
|
|
|
let change = HealthChange {
|
|
|
|
amount: *h as i32,
|
2021-02-02 18:02:40 +00:00
|
|
|
cause: HealthSource::Heal {
|
2021-02-28 20:02:03 +00:00
|
|
|
by: attacker.map(|a| a.uid),
|
2021-02-02 18:02:40 +00:00
|
|
|
},
|
2021-01-30 05:03:23 +00:00
|
|
|
};
|
2021-02-02 18:02:40 +00:00
|
|
|
if change.amount != 0 {
|
|
|
|
emit(ServerEvent::Damage {
|
2021-02-28 20:02:03 +00:00
|
|
|
entity: target.entity,
|
2021-02-02 18:02:40 +00:00
|
|
|
change,
|
|
|
|
});
|
|
|
|
}
|
2021-01-30 05:03:23 +00:00
|
|
|
},
|
2021-02-27 19:55:06 +00:00
|
|
|
CombatEffect::Combo(c) => {
|
2021-02-28 20:02:03 +00:00
|
|
|
if let Some(attacker_entity) = attacker.map(|a| a.entity) {
|
2021-02-27 19:55:06 +00:00
|
|
|
emit(ServerEvent::ComboChange {
|
|
|
|
entity: attacker_entity,
|
|
|
|
change: *c,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2021-01-26 03:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 02:15:25 +00:00
|
|
|
for effect in self
|
|
|
|
.effects
|
|
|
|
.iter()
|
|
|
|
.filter(|e| e.target.map_or(true, |t| t == target_group))
|
|
|
|
.filter(|e| !(matches!(e.target, Some(GroupTarget::OutOfGroup)) && target_dodging))
|
|
|
|
{
|
2021-03-09 00:23:00 +00:00
|
|
|
if effect.requirements.iter().all(|req| match req {
|
2021-03-20 20:26:10 +00:00
|
|
|
CombatRequirement::AnyDamage => accumulated_damage > 0.0 && target.health.is_some(),
|
2021-03-09 00:23:00 +00:00
|
|
|
CombatRequirement::Energy(r) => {
|
2021-02-02 18:02:40 +00:00
|
|
|
if let Some(AttackerInfo {
|
|
|
|
entity,
|
|
|
|
energy: Some(e),
|
|
|
|
..
|
2021-02-28 20:02:03 +00:00
|
|
|
}) = attacker
|
2021-02-02 18:02:40 +00:00
|
|
|
{
|
2021-02-05 01:39:12 +00:00
|
|
|
let sufficient_energy = e.current() as f32 >= *r;
|
2021-02-02 18:02:40 +00:00
|
|
|
if sufficient_energy {
|
|
|
|
emit(ServerEvent::EnergyChange {
|
|
|
|
entity,
|
2021-02-01 02:43:26 +00:00
|
|
|
change: EnergyChange {
|
|
|
|
amount: -(*r as i32),
|
|
|
|
source: EnergySource::Ability,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-02-02 18:02:40 +00:00
|
|
|
|
|
|
|
sufficient_energy
|
2021-01-30 05:17:40 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
},
|
2021-03-09 00:23:00 +00:00
|
|
|
CombatRequirement::Combo(r) => {
|
2021-03-02 22:19:38 +00:00
|
|
|
if let Some(AttackerInfo {
|
|
|
|
entity,
|
|
|
|
combo: Some(c),
|
|
|
|
..
|
|
|
|
}) = attacker
|
|
|
|
{
|
|
|
|
let sufficient_combo = c.counter() >= *r;
|
|
|
|
if sufficient_combo {
|
|
|
|
emit(ServerEvent::ComboChange {
|
|
|
|
entity,
|
|
|
|
change: -(*r as i32),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sufficient_combo
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
},
|
2021-03-09 00:23:00 +00:00
|
|
|
}) {
|
2021-01-28 18:50:56 +00:00
|
|
|
match effect.effect {
|
2021-02-02 18:02:40 +00:00
|
|
|
CombatEffect::Knockback(kb) => {
|
2021-01-28 18:50:56 +00:00
|
|
|
let impulse = kb.calculate_impulse(dir);
|
|
|
|
if !impulse.is_approx_zero() {
|
2021-02-02 18:02:40 +00:00
|
|
|
emit(ServerEvent::Knockback {
|
2021-02-28 20:02:03 +00:00
|
|
|
entity: target.entity,
|
2021-01-28 18:50:56 +00:00
|
|
|
impulse,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2021-02-02 18:02:40 +00:00
|
|
|
CombatEffect::EnergyReward(ec) => {
|
2021-02-28 20:02:03 +00:00
|
|
|
if let Some(attacker_entity) = attacker.map(|a| a.entity) {
|
2021-02-02 18:02:40 +00:00
|
|
|
emit(ServerEvent::EnergyChange {
|
2021-02-01 02:43:26 +00:00
|
|
|
entity: attacker_entity,
|
|
|
|
change: EnergyChange {
|
|
|
|
amount: ec as i32,
|
|
|
|
source: EnergySource::HitEnemy,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2021-01-28 18:50:56 +00:00
|
|
|
},
|
2021-02-02 18:02:40 +00:00
|
|
|
CombatEffect::Buff(b) => {
|
2021-01-28 18:50:56 +00:00
|
|
|
if thread_rng().gen::<f32>() < b.chance {
|
2021-02-02 18:02:40 +00:00
|
|
|
emit(ServerEvent::Buff {
|
2021-02-28 20:02:03 +00:00
|
|
|
entity: target.entity,
|
2021-01-28 18:50:56 +00:00
|
|
|
buff_change: BuffChange::Add(
|
2021-02-28 20:02:03 +00:00
|
|
|
b.to_buff(attacker.map(|a| a.uid), accumulated_damage),
|
2021-01-28 18:50:56 +00:00
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2021-02-02 18:02:40 +00:00
|
|
|
CombatEffect::Lifesteal(l) => {
|
2021-02-28 20:02:03 +00:00
|
|
|
if let Some(attacker_entity) = attacker.map(|a| a.entity) {
|
2021-02-01 02:43:26 +00:00
|
|
|
let change = HealthChange {
|
|
|
|
amount: (accumulated_damage * l) as i32,
|
2021-02-02 18:02:40 +00:00
|
|
|
cause: HealthSource::Heal {
|
2021-02-28 20:02:03 +00:00
|
|
|
by: attacker.map(|a| a.uid),
|
2021-02-02 18:02:40 +00:00
|
|
|
},
|
2021-02-01 02:43:26 +00:00
|
|
|
};
|
2021-02-02 18:02:40 +00:00
|
|
|
if change.amount != 0 {
|
|
|
|
emit(ServerEvent::Damage {
|
|
|
|
entity: attacker_entity,
|
|
|
|
change,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CombatEffect::Poise(p) => {
|
2021-02-28 20:02:03 +00:00
|
|
|
let change = PoiseChange::from_value(p, target.inventory);
|
2021-02-02 18:02:40 +00:00
|
|
|
if change.amount != 0 {
|
|
|
|
emit(ServerEvent::PoiseChange {
|
2021-02-28 20:02:03 +00:00
|
|
|
entity: target.entity,
|
2021-02-01 02:43:26 +00:00
|
|
|
change,
|
2021-02-02 18:02:40 +00:00
|
|
|
kb_dir: *dir,
|
2021-02-01 02:43:26 +00:00
|
|
|
});
|
|
|
|
}
|
2021-01-29 01:55:43 +00:00
|
|
|
},
|
2021-02-02 18:02:40 +00:00
|
|
|
CombatEffect::Heal(h) => {
|
2021-01-30 05:03:23 +00:00
|
|
|
let change = HealthChange {
|
|
|
|
amount: h as i32,
|
2021-02-02 18:02:40 +00:00
|
|
|
cause: HealthSource::Heal {
|
2021-02-28 20:02:03 +00:00
|
|
|
by: attacker.map(|a| a.uid),
|
2021-02-02 18:02:40 +00:00
|
|
|
},
|
2021-01-30 05:03:23 +00:00
|
|
|
};
|
2021-02-02 18:02:40 +00:00
|
|
|
if change.amount != 0 {
|
|
|
|
emit(ServerEvent::Damage {
|
2021-02-28 20:02:03 +00:00
|
|
|
entity: target.entity,
|
2021-02-02 18:02:40 +00:00
|
|
|
change,
|
|
|
|
});
|
|
|
|
}
|
2021-01-30 05:03:23 +00:00
|
|
|
},
|
2021-02-27 19:55:06 +00:00
|
|
|
CombatEffect::Combo(c) => {
|
2021-02-28 20:02:03 +00:00
|
|
|
if let Some(attacker_entity) = attacker.map(|a| a.entity) {
|
2021-02-27 19:55:06 +00:00
|
|
|
emit(ServerEvent::ComboChange {
|
|
|
|
entity: attacker_entity,
|
|
|
|
change: c,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2021-01-28 18:50:56 +00:00
|
|
|
}
|
2021-01-28 02:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-26 02:50:16 +00:00
|
|
|
}
|
2021-01-26 00:01:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-29 01:37:33 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2021-02-02 18:02:40 +00:00
|
|
|
pub struct AttackDamage {
|
2021-01-26 00:01:07 +00:00
|
|
|
damage: Damage,
|
|
|
|
target: Option<GroupTarget>,
|
2021-02-02 18:02:40 +00:00
|
|
|
effects: Vec<CombatEffect>,
|
2021-01-26 00:01:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-02-02 18:02:40 +00:00
|
|
|
impl AttackDamage {
|
2021-01-26 00:01:07 +00:00
|
|
|
pub fn new(damage: Damage, target: Option<GroupTarget>) -> Self {
|
|
|
|
Self {
|
|
|
|
damage,
|
|
|
|
target,
|
|
|
|
effects: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:02:40 +00:00
|
|
|
pub fn with_effect(mut self, effect: CombatEffect) -> Self {
|
2021-01-26 00:01:07 +00:00
|
|
|
self.effects.push(effect);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-29 01:37:33 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2021-02-02 18:02:40 +00:00
|
|
|
pub struct AttackEffect {
|
2021-01-26 00:01:07 +00:00
|
|
|
target: Option<GroupTarget>,
|
2021-02-02 18:02:40 +00:00
|
|
|
effect: CombatEffect,
|
2021-03-09 00:23:00 +00:00
|
|
|
requirements: Vec<CombatRequirement>,
|
2021-01-26 00:01:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-02-02 18:02:40 +00:00
|
|
|
impl AttackEffect {
|
|
|
|
pub fn new(target: Option<GroupTarget>, effect: CombatEffect) -> Self {
|
2021-01-28 18:50:56 +00:00
|
|
|
Self {
|
|
|
|
target,
|
|
|
|
effect,
|
2021-03-09 00:23:00 +00:00
|
|
|
requirements: Vec::new(),
|
2021-01-28 18:50:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_requirement(mut self, requirement: CombatRequirement) -> Self {
|
2021-03-09 00:23:00 +00:00
|
|
|
self.requirements.push(requirement);
|
2021-01-28 18:50:56 +00:00
|
|
|
self
|
2021-01-26 00:01:07 +00:00
|
|
|
}
|
2021-01-31 17:15:28 +00:00
|
|
|
|
2021-02-02 18:02:40 +00:00
|
|
|
pub fn effect(&self) -> &CombatEffect { &self.effect }
|
2021-01-26 00:01:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-03-20 17:29:57 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
2021-02-02 18:02:40 +00:00
|
|
|
pub enum CombatEffect {
|
2021-01-30 05:03:23 +00:00
|
|
|
Heal(f32),
|
2021-01-28 01:44:49 +00:00
|
|
|
Buff(CombatBuff),
|
2021-01-26 00:31:06 +00:00
|
|
|
Knockback(Knockback),
|
2021-02-05 01:39:12 +00:00
|
|
|
EnergyReward(f32),
|
2021-01-29 01:55:43 +00:00
|
|
|
Lifesteal(f32),
|
2021-01-30 04:40:03 +00:00
|
|
|
Poise(f32),
|
2021-02-27 19:55:06 +00:00
|
|
|
Combo(i32),
|
2021-01-26 00:01:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-29 01:37:33 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2021-01-28 18:50:56 +00:00
|
|
|
pub enum CombatRequirement {
|
|
|
|
AnyDamage,
|
2021-03-02 22:19:38 +00:00
|
|
|
Energy(f32),
|
|
|
|
Combo(u32),
|
2021-01-28 18:50:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 01:37:33 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
2020-10-30 20:41:21 +00:00
|
|
|
pub enum DamageSource {
|
2021-01-18 05:46:53 +00:00
|
|
|
Buff(BuffKind),
|
2020-10-30 20:41:21 +00:00
|
|
|
Melee,
|
|
|
|
Projectile,
|
|
|
|
Explosion,
|
|
|
|
Falling,
|
|
|
|
Shockwave,
|
|
|
|
Energy,
|
2020-11-05 01:21:42 +00:00
|
|
|
Other,
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-10-17 22:42:43 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2020-10-30 20:41:21 +00:00
|
|
|
pub struct Damage {
|
|
|
|
pub source: DamageSource,
|
|
|
|
pub value: f32,
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-10-17 22:42:43 +00:00
|
|
|
impl Damage {
|
2021-01-08 19:12:09 +00:00
|
|
|
/// Returns the total damage reduction provided by all equipped items
|
2021-02-28 20:02:03 +00:00
|
|
|
pub fn compute_damage_reduction(inventory: Option<&Inventory>, stats: Option<&Stats>) -> f32 {
|
2021-02-28 23:14:59 +00:00
|
|
|
let inventory_dr = if let Some(inventory) = inventory {
|
2021-02-28 20:02:03 +00:00
|
|
|
let protection = inventory
|
|
|
|
.equipped_items()
|
|
|
|
.filter_map(|item| {
|
|
|
|
if let ItemKind::Armor(armor) = &item.kind() {
|
|
|
|
Some(armor.get_protection())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|protection| match protection {
|
|
|
|
Protection::Normal(protection) => Some(protection),
|
|
|
|
Protection::Invincible => None,
|
|
|
|
})
|
|
|
|
.sum::<Option<f32>>();
|
|
|
|
|
|
|
|
const FIFTY_PERCENT_DR_THRESHOLD: f32 = 60.0;
|
|
|
|
|
|
|
|
match protection {
|
|
|
|
Some(dr) => dr / (FIFTY_PERCENT_DR_THRESHOLD + dr.abs()),
|
|
|
|
None => 1.0,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
let stats_dr = if let Some(stats) = stats {
|
|
|
|
stats.damage_reduction
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
2021-02-28 23:14:59 +00:00
|
|
|
1.0 - (1.0 - inventory_dr) * (1.0 - stats_dr)
|
2021-01-08 19:12:09 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:02:40 +00:00
|
|
|
pub fn calculate_health_change(
|
2021-01-26 03:53:52 +00:00
|
|
|
self,
|
2021-02-28 20:02:03 +00:00
|
|
|
damage_reduction: f32,
|
2021-01-26 03:53:52 +00:00
|
|
|
uid: Option<Uid>,
|
|
|
|
is_crit: bool,
|
|
|
|
crit_mult: f32,
|
2021-01-31 16:40:04 +00:00
|
|
|
damage_modifier: f32,
|
2021-01-26 03:53:52 +00:00
|
|
|
) -> HealthChange {
|
2021-01-31 16:40:04 +00:00
|
|
|
let mut damage = self.value * damage_modifier;
|
2021-03-09 03:53:58 +00:00
|
|
|
// Critical hit damage (to be applied post-armor for melee, and pre-armor for
|
|
|
|
// other damage kinds
|
|
|
|
let critdamage = if is_crit {
|
|
|
|
damage * (crit_mult - 1.0)
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
2020-10-30 20:41:21 +00:00
|
|
|
match self.source {
|
|
|
|
DamageSource::Melee => {
|
2020-10-17 22:42:43 +00:00
|
|
|
// Armor
|
|
|
|
damage *= 1.0 - damage_reduction;
|
|
|
|
|
|
|
|
// Critical damage applies after armor for melee
|
|
|
|
if (damage_reduction - 1.0).abs() > f32::EPSILON {
|
|
|
|
damage += critdamage;
|
|
|
|
}
|
|
|
|
|
2020-12-06 02:29:46 +00:00
|
|
|
HealthChange {
|
|
|
|
amount: -damage as i32,
|
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
2020-11-05 01:21:42 +00:00
|
|
|
},
|
2020-12-06 02:29:46 +00:00
|
|
|
}
|
2020-10-17 22:42:43 +00:00
|
|
|
},
|
2020-10-30 20:41:21 +00:00
|
|
|
DamageSource::Projectile => {
|
2020-10-17 22:42:43 +00:00
|
|
|
// Critical hit
|
2021-03-09 03:53:58 +00:00
|
|
|
damage += critdamage;
|
2020-10-17 22:42:43 +00:00
|
|
|
// Armor
|
|
|
|
damage *= 1.0 - damage_reduction;
|
|
|
|
|
2020-12-06 02:29:46 +00:00
|
|
|
HealthChange {
|
|
|
|
amount: -damage as i32,
|
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
2020-11-05 01:21:42 +00:00
|
|
|
},
|
2020-12-06 02:29:46 +00:00
|
|
|
}
|
2020-10-17 22:42:43 +00:00
|
|
|
},
|
2020-10-30 20:41:21 +00:00
|
|
|
DamageSource::Explosion => {
|
2021-03-09 03:53:58 +00:00
|
|
|
// Critical hit
|
|
|
|
damage += critdamage;
|
2020-10-17 22:42:43 +00:00
|
|
|
// Armor
|
|
|
|
damage *= 1.0 - damage_reduction;
|
|
|
|
|
2020-12-06 02:29:46 +00:00
|
|
|
HealthChange {
|
|
|
|
amount: -damage as i32,
|
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
2020-11-05 01:21:42 +00:00
|
|
|
},
|
2020-12-06 02:29:46 +00:00
|
|
|
}
|
2020-10-17 22:42:43 +00:00
|
|
|
},
|
2020-10-30 20:41:21 +00:00
|
|
|
DamageSource::Shockwave => {
|
2021-03-09 03:53:58 +00:00
|
|
|
// Critical hit
|
|
|
|
damage += critdamage;
|
2020-10-17 22:42:43 +00:00
|
|
|
// Armor
|
|
|
|
damage *= 1.0 - damage_reduction;
|
|
|
|
|
2020-12-06 02:29:46 +00:00
|
|
|
HealthChange {
|
|
|
|
amount: -damage as i32,
|
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
2020-11-05 01:21:42 +00:00
|
|
|
},
|
2020-12-06 02:29:46 +00:00
|
|
|
}
|
2020-10-17 22:42:43 +00:00
|
|
|
},
|
2020-10-30 20:41:21 +00:00
|
|
|
DamageSource::Energy => {
|
2021-03-09 03:53:58 +00:00
|
|
|
// Critical hit
|
|
|
|
damage += critdamage;
|
2020-10-17 22:42:43 +00:00
|
|
|
// Armor
|
|
|
|
damage *= 1.0 - damage_reduction;
|
|
|
|
|
2020-12-06 02:29:46 +00:00
|
|
|
HealthChange {
|
|
|
|
amount: -damage as i32,
|
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
2020-11-05 01:21:42 +00:00
|
|
|
},
|
2020-12-06 02:29:46 +00:00
|
|
|
}
|
|
|
|
},
|
2020-10-30 20:41:21 +00:00
|
|
|
DamageSource::Falling => {
|
2020-10-17 22:42:43 +00:00
|
|
|
// Armor
|
|
|
|
if (damage_reduction - 1.0).abs() < f32::EPSILON {
|
|
|
|
damage = 0.0;
|
|
|
|
}
|
2020-12-06 02:29:46 +00:00
|
|
|
HealthChange {
|
|
|
|
amount: -damage as i32,
|
|
|
|
cause: HealthSource::World,
|
2021-01-15 03:32:12 +00:00
|
|
|
}
|
2020-10-17 22:42:43 +00:00
|
|
|
},
|
2021-01-18 05:46:53 +00:00
|
|
|
DamageSource::Buff(_) => HealthChange {
|
|
|
|
amount: -damage as i32,
|
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
|
|
|
},
|
|
|
|
},
|
2020-11-05 01:21:42 +00:00
|
|
|
DamageSource::Other => HealthChange {
|
|
|
|
amount: -damage as i32,
|
|
|
|
cause: HealthSource::Damage {
|
|
|
|
kind: self.source,
|
|
|
|
by: uid,
|
|
|
|
},
|
2020-12-06 02:29:46 +00:00
|
|
|
},
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-30 20:41:21 +00:00
|
|
|
|
|
|
|
pub fn interpolate_damage(&mut self, frac: f32, min: f32) {
|
|
|
|
let new_damage = min + frac * (self.value - min);
|
|
|
|
self.value = new_damage;
|
|
|
|
}
|
2020-10-17 22:42:43 +00:00
|
|
|
}
|
2020-10-18 18:21:58 +00:00
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-10-18 18:21:58 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
2021-01-25 22:46:43 +00:00
|
|
|
pub struct Knockback {
|
|
|
|
pub direction: KnockbackDir,
|
|
|
|
pub strength: f32,
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-25 22:46:43 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub enum KnockbackDir {
|
|
|
|
Away,
|
|
|
|
Towards,
|
|
|
|
Up,
|
|
|
|
TowardsUp,
|
2020-10-18 18:21:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-10-18 18:21:58 +00:00
|
|
|
impl Knockback {
|
2020-10-27 22:16:17 +00:00
|
|
|
pub fn calculate_impulse(self, dir: Dir) -> Vec3<f32> {
|
2021-01-25 22:46:43 +00:00
|
|
|
match self.direction {
|
|
|
|
KnockbackDir::Away => self.strength * *Dir::slerp(dir, Dir::new(Vec3::unit_z()), 0.5),
|
|
|
|
KnockbackDir::Towards => {
|
|
|
|
self.strength * *Dir::slerp(-dir, Dir::new(Vec3::unit_z()), 0.5)
|
2020-10-18 18:21:58 +00:00
|
|
|
},
|
2021-01-25 22:46:43 +00:00
|
|
|
KnockbackDir::Up => self.strength * Vec3::unit_z(),
|
|
|
|
KnockbackDir::TowardsUp => {
|
|
|
|
self.strength * *Dir::slerp(-dir, Dir::new(Vec3::unit_z()), 0.85)
|
2020-10-18 18:21:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-12-24 17:54:00 +00:00
|
|
|
|
|
|
|
pub fn modify_strength(mut self, power: f32) -> Self {
|
2021-01-25 22:46:43 +00:00
|
|
|
self.strength *= power;
|
2020-12-24 17:54:00 +00:00
|
|
|
self
|
|
|
|
}
|
2020-10-18 18:21:58 +00:00
|
|
|
}
|
2020-11-15 02:05:18 +00:00
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-03-20 17:29:57 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
2021-01-28 01:44:49 +00:00
|
|
|
pub struct CombatBuff {
|
|
|
|
pub kind: BuffKind,
|
|
|
|
pub dur_secs: f32,
|
|
|
|
pub strength: CombatBuffStrength,
|
|
|
|
pub chance: f32,
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-03-20 17:29:57 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
2021-01-28 01:44:49 +00:00
|
|
|
pub enum CombatBuffStrength {
|
|
|
|
DamageFraction(f32),
|
|
|
|
Value(f32),
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-28 01:44:49 +00:00
|
|
|
impl CombatBuffStrength {
|
|
|
|
fn to_strength(self, damage: f32) -> f32 {
|
|
|
|
match self {
|
|
|
|
CombatBuffStrength::DamageFraction(f) => damage * f,
|
|
|
|
CombatBuffStrength::Value(v) => v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-01-28 01:44:49 +00:00
|
|
|
impl CombatBuff {
|
2021-02-01 02:43:26 +00:00
|
|
|
fn to_buff(self, uid: Option<Uid>, damage: f32) -> Buff {
|
2021-01-28 01:44:49 +00:00
|
|
|
// TODO: Generate BufCategoryId vec (probably requires damage overhaul?)
|
2021-02-01 02:43:26 +00:00
|
|
|
let source = if let Some(uid) = uid {
|
|
|
|
BuffSource::Character { by: uid }
|
|
|
|
} else {
|
|
|
|
BuffSource::Unknown
|
|
|
|
};
|
2021-01-28 01:44:49 +00:00
|
|
|
Buff::new(
|
|
|
|
self.kind,
|
|
|
|
BuffData::new(
|
|
|
|
self.strength.to_strength(damage),
|
|
|
|
Some(Duration::from_secs_f32(self.dur_secs)),
|
|
|
|
),
|
|
|
|
Vec::new(),
|
2021-02-01 02:43:26 +00:00
|
|
|
source,
|
2021-01-28 01:44:49 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-29 00:04:44 +00:00
|
|
|
pub fn default_physical() -> Self {
|
2021-01-28 01:44:49 +00:00
|
|
|
Self {
|
|
|
|
kind: BuffKind::Bleeding,
|
|
|
|
dur_secs: 10.0,
|
|
|
|
strength: CombatBuffStrength::DamageFraction(0.1),
|
|
|
|
chance: 0.1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-02-22 20:37:59 +00:00
|
|
|
fn equipped_item_and_tool(inv: &Inventory, slot: EquipSlot) -> Option<(&Item, &Tool)> {
|
2021-01-16 17:01:57 +00:00
|
|
|
inv.equipped(slot).and_then(|i| {
|
|
|
|
if let ItemKind::Tool(tool) = &i.kind() {
|
2021-02-17 04:23:08 +00:00
|
|
|
Some((i, tool))
|
2021-01-16 17:01:57 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2020-11-15 02:05:18 +00:00
|
|
|
pub fn get_weapons(inv: &Inventory) -> (Option<ToolKind>, Option<ToolKind>) {
|
|
|
|
(
|
2021-02-17 04:23:08 +00:00
|
|
|
equipped_item_and_tool(inv, EquipSlot::Mainhand).map(|(_, tool)| tool.kind),
|
|
|
|
equipped_item_and_tool(inv, EquipSlot::Offhand).map(|(_, tool)| tool.kind),
|
2020-11-15 02:05:18 +00:00
|
|
|
)
|
2021-01-07 05:23:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 20:15:37 +00:00
|
|
|
pub fn weapon_rating<T: ItemDesc>(item: &T, msm: &MaterialStatManifest) -> f32 {
|
2021-04-02 21:37:08 +00:00
|
|
|
const DAMAGE_WEIGHT: f32 = 2.0;
|
2021-03-30 16:16:20 +00:00
|
|
|
const POISE_WEIGHT: f32 = 1.0;
|
|
|
|
|
|
|
|
if let ItemKind::Tool(tool) = item.kind() {
|
|
|
|
let stats = tool::Stats::from((msm, item.components(), tool));
|
|
|
|
|
|
|
|
let damage_rating =
|
|
|
|
stats.power * stats.speed * (1.0 + stats.crit_chance * (stats.crit_mult - 1.0));
|
|
|
|
let poise_rating = stats.poise_strength * stats.speed;
|
|
|
|
|
2021-04-02 21:37:08 +00:00
|
|
|
(damage_rating * DAMAGE_WEIGHT + poise_rating * POISE_WEIGHT)
|
|
|
|
/ (DAMAGE_WEIGHT + POISE_WEIGHT)
|
2021-03-30 16:16:20 +00:00
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn weapon_skills(inventory: &Inventory, stats: &Stats) -> f32 {
|
|
|
|
let (mainhand, offhand) = get_weapons(inventory);
|
|
|
|
let mainhand_skills = if let Some(tool) = mainhand {
|
|
|
|
stats.skill_set.earned_sp(SkillGroupKind::Weapon(tool)) as f32
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
let offhand_skills = if let Some(tool) = offhand {
|
|
|
|
stats.skill_set.earned_sp(SkillGroupKind::Weapon(tool)) as f32
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
mainhand_skills.max(offhand_skills)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_weapon_rating(inventory: &Inventory, msm: &MaterialStatManifest) -> f32 {
|
|
|
|
let mainhand_rating =
|
|
|
|
if let Some((item, _)) = equipped_item_and_tool(inventory, EquipSlot::Mainhand) {
|
|
|
|
weapon_rating(item, msm)
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
|
|
|
|
let offhand_rating =
|
|
|
|
if let Some((item, _)) = equipped_item_and_tool(inventory, EquipSlot::Offhand) {
|
|
|
|
weapon_rating(item, msm)
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
|
|
|
|
mainhand_rating.max(offhand_rating)
|
2021-01-07 05:23:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:03:20 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-02-23 20:29:27 +00:00
|
|
|
pub fn combat_rating(
|
|
|
|
inventory: &Inventory,
|
|
|
|
health: &Health,
|
|
|
|
stats: &Stats,
|
|
|
|
body: Body,
|
|
|
|
msm: &MaterialStatManifest,
|
|
|
|
) -> f32 {
|
2021-03-30 16:16:20 +00:00
|
|
|
const WEAPON_WEIGHT: f32 = 1.0;
|
|
|
|
const HEALTH_WEIGHT: f32 = 1.0;
|
|
|
|
const SKILLS_WEIGHT: f32 = 1.0;
|
|
|
|
// Assumes a "standard" max health of 100
|
|
|
|
let health_rating = health.base_max() as f32
|
|
|
|
/ 100.0
|
2021-04-08 00:09:51 +00:00
|
|
|
/ (1.0 - Damage::compute_damage_reduction(Some(inventory), None)).max(0.00001);
|
2021-03-30 16:16:20 +00:00
|
|
|
|
|
|
|
// Assumes a standard person has earned 20 skill points in the general skill
|
2021-04-02 21:37:08 +00:00
|
|
|
// tree and 10 skill points for the weapon skill tree
|
2021-03-30 16:16:20 +00:00
|
|
|
let skills_rating = (stats.skill_set.earned_sp(SkillGroupKind::General) as f32 / 20.0
|
|
|
|
+ weapon_skills(inventory, stats) / 10.0)
|
|
|
|
/ 2.0;
|
|
|
|
|
|
|
|
let weapon_rating = get_weapon_rating(inventory, msm);
|
|
|
|
|
|
|
|
let combined_rating = (health_rating * HEALTH_WEIGHT
|
|
|
|
+ skills_rating * SKILLS_WEIGHT
|
|
|
|
+ weapon_rating * WEAPON_WEIGHT)
|
|
|
|
/ (HEALTH_WEIGHT + SKILLS_WEIGHT + WEAPON_WEIGHT);
|
|
|
|
|
|
|
|
// Body multiplier meant to account for an enemy being harder than equipment and
|
|
|
|
// skills would account for. It should only not be 1.0 for non-humanoids
|
2021-01-22 21:12:16 +00:00
|
|
|
combined_rating * body.combat_multiplier()
|
2021-01-07 05:23:24 +00:00
|
|
|
}
|