veloren/common/systems/src/stats.rs

254 lines
9.0 KiB
Rust
Raw Normal View History

use common::{
combat,
comp::{
2021-03-04 20:43:58 +00:00
self,
skills::{GeneralSkill, Skill},
Body, CharacterState, Combo, Energy, Health, Inventory, Poise, Pos, SkillSet, Stats,
StatsModifier,
},
event::{EventBus, ServerEvent},
outcome::Outcome,
2021-05-05 13:54:24 +00:00
resources::{DeltaTime, EntitiesDiedLastTick, Time},
uid::Uid,
};
use common_ecs::{Job, Origin, Phase, System};
2020-12-13 17:21:51 +00:00
use hashbrown::HashSet;
use specs::{
shred::ResourceId, Entities, Join, Read, ReadStorage, SystemData, World, Write, WriteStorage,
};
use tracing::warn;
2020-12-16 23:30:33 +00:00
use vek::Vec3;
const ENERGY_REGEN_ACCEL: f32 = 1.0;
2020-12-16 23:30:33 +00:00
const POISE_REGEN_ACCEL: f32 = 2.0;
#[derive(SystemData)]
pub struct ReadData<'a> {
entities: Entities<'a>,
dt: Read<'a, DeltaTime>,
2021-02-27 19:55:06 +00:00
time: Read<'a, Time>,
server_bus: Read<'a, EventBus<ServerEvent>>,
positions: ReadStorage<'a, Pos>,
uids: ReadStorage<'a, Uid>,
bodies: ReadStorage<'a, Body>,
char_states: ReadStorage<'a, CharacterState>,
2021-05-21 00:52:29 +00:00
inventories: ReadStorage<'a, Inventory>,
}
/// This system kills players, levels them up, and regenerates energy.
#[derive(Default)]
pub struct Sys;
2021-03-08 11:13:59 +00:00
impl<'a> System<'a> for Sys {
#[allow(clippy::type_complexity)]
type SystemData = (
ReadData<'a>,
WriteStorage<'a, Stats>,
WriteStorage<'a, SkillSet>,
WriteStorage<'a, Health>,
2020-12-10 00:32:24 +00:00
WriteStorage<'a, Poise>,
WriteStorage<'a, Energy>,
2021-02-27 19:55:06 +00:00
WriteStorage<'a, Combo>,
2021-05-05 13:54:24 +00:00
Write<'a, EntitiesDiedLastTick>,
Write<'a, Vec<Outcome>>,
);
const NAME: &'static str = "stats";
const ORIGIN: Origin = Origin::Common;
const PHASE: Phase = Phase::Create;
fn run(
2021-03-08 11:13:59 +00:00
_job: &mut Job<Self>,
(
read_data,
stats,
mut skill_sets,
mut healths,
2020-12-10 00:32:24 +00:00
mut poises,
mut energies,
2021-02-27 19:55:06 +00:00
mut combos,
2021-05-05 13:54:24 +00:00
mut entities_died_last_tick,
mut outcomes,
): Self::SystemData,
) {
2021-05-05 13:54:24 +00:00
entities_died_last_tick.0.clear();
let mut server_event_emitter = read_data.server_bus.emitter();
let dt = read_data.dt.0;
2020-07-05 12:39:28 +00:00
// Update stats
2021-05-21 00:52:29 +00:00
for (entity, uid, stats, mut skill_set, mut health, pos, mut energy, inventory) in (
&read_data.entities,
&read_data.uids,
&stats,
&mut skill_sets,
&mut healths,
&read_data.positions,
&mut energies,
2021-05-21 00:52:29 +00:00
read_data.inventories.maybe(),
)
.join()
{
let set_dead = { health.should_die() && !health.is_dead };
if set_dead {
2021-05-05 13:54:24 +00:00
let cloned_entity = (entity, *pos);
entities_died_last_tick.0.push(cloned_entity);
server_event_emitter.emit(ServerEvent::Destroy {
entity,
cause: health.last_change,
});
health.is_dead = true;
}
let stat = stats;
let update_max_hp = {
stat.max_health_modifiers.update_maximum()
2021-09-10 19:20:14 +00:00
|| (health.base_max() - health.maximum()).abs() > Health::HEALTH_EPSILON
};
if update_max_hp {
health.update_maximum(stat.max_health_modifiers);
}
let (change_energy, energy_mods) = {
// Calculates energy scaling from stats and inventory
let energy_mods = StatsModifier {
add_mod: stat.max_energy_modifiers.add_mod
+ combat::compute_max_energy_mod(inventory),
mult_mod: stat.max_energy_modifiers.mult_mod,
};
(
energy_mods.update_maximum()
|| (energy.base_max() - energy.maximum()).abs() > Energy::ENERGY_EPSILON,
energy_mods,
)
};
// If modifier sufficiently different, mutably access energy
if change_energy {
energy.update_maximum(energy_mods);
}
2021-05-21 00:52:29 +00:00
let skills_to_level = skill_set
.skill_groups()
.filter_map(|s_g| {
(s_g.available_exp >= skill_set.skill_point_cost(s_g.skill_group_kind))
.then(|| s_g.skill_group_kind)
})
.collect::<HashSet<_>>();
if !skills_to_level.is_empty() {
for skill_group in skills_to_level {
2021-12-01 04:12:02 +00:00
match skill_set.earn_skill_point(skill_group) {
Ok(_) => outcomes.push(Outcome::SkillPointGain {
uid: *uid,
skill_tree: skill_group,
total_points: skill_set.earned_sp(skill_group),
pos: pos.0,
}),
Err(_) => warn!(
"Attempted to add skill point to group which is inelgible to earn one"
2021-12-01 04:12:02 +00:00
),
}
}
}
2020-07-05 12:39:28 +00:00
}
// Apply effects from leveling skills
for (mut skill_set, mut health, mut energy, body) in (
&mut skill_sets,
&mut healths,
&mut energies,
&read_data.bodies,
)
.join()
{
if skill_set.modify_health {
let health_level = skill_set
.skill_level(Skill::General(GeneralSkill::HealthIncrease))
.unwrap_or(0);
health.update_max_hp(*body, health_level);
skill_set.modify_health = false;
}
if skill_set.modify_energy {
let energy_level = skill_set
.skill_level(Skill::General(GeneralSkill::EnergyIncrease))
.unwrap_or(0);
energy.update_max_energy(*body, energy_level);
skill_set.modify_energy = false;
}
}
2020-12-10 00:32:24 +00:00
// Update energies and poises
for (character_state, mut energy, mut poise) in
(&read_data.char_states, &mut energies, &mut poises).join()
2020-07-05 12:39:28 +00:00
{
match character_state {
2020-06-13 11:35:31 +00:00
// Accelerate recharging energy.
CharacterState::Idle { .. }
| CharacterState::Talk { .. }
| CharacterState::Sit { .. }
| CharacterState::Dance { .. }
2021-04-27 14:41:48 +00:00
| CharacterState::Glide { .. }
| CharacterState::GlideWield { .. }
| CharacterState::Wielding { .. }
| CharacterState::Equipping { .. }
| CharacterState::Boost { .. } => {
let res = { energy.current() < energy.maximum() };
if res {
let energy = &mut *energy;
energy.change_by(energy.regen_rate * dt);
energy.regen_rate = (energy.regen_rate + ENERGY_REGEN_ACCEL * dt).min(10.0);
2020-01-19 19:19:44 +00:00
}
2020-12-10 00:32:24 +00:00
let res_poise = { poise.current() < poise.maximum() };
2020-12-10 00:32:24 +00:00
2020-12-16 23:30:33 +00:00
if res_poise {
let poise = &mut *poise;
poise.change_by(poise.regen_rate * dt, Vec3::zero());
poise.regen_rate = (poise.regen_rate + POISE_REGEN_ACCEL * dt).min(10.0);
2020-12-16 23:30:33 +00:00
}
},
2021-04-27 14:41:48 +00:00
// Ability use does not regen and sets the rate back to zero.
CharacterState::BasicMelee { .. }
| CharacterState::DashMelee { .. }
| CharacterState::LeapMelee { .. }
2020-07-08 19:58:41 +00:00
| CharacterState::SpinMelee { .. }
| CharacterState::ComboMelee { .. }
2020-07-26 03:06:53 +00:00
| CharacterState::BasicRanged { .. }
| CharacterState::ChargedMelee { .. }
2020-08-08 20:53:55 +00:00
| CharacterState::ChargedRanged { .. }
| CharacterState::RepeaterRanged { .. }
| CharacterState::Shockwave { .. }
2021-03-01 20:44:29 +00:00
| CharacterState::BasicBeam { .. }
| CharacterState::BasicAura { .. }
| CharacterState::Blink { .. }
| CharacterState::BasicSummon { .. }
2021-06-19 18:53:23 +00:00
| CharacterState::SelfBuff { .. }
| CharacterState::SpriteSummon { .. } => {
if energy.regen_rate != 0.0 {
energy.regen_rate = 0.0
2020-01-19 19:19:44 +00:00
}
},
2021-04-10 03:40:20 +00:00
// Abilities that temporarily stall energy gain, but preserve regen_rate.
CharacterState::Roll { .. }
| CharacterState::Climb { .. }
2021-04-10 03:40:20 +00:00
| CharacterState::Stunned { .. }
2021-06-25 01:38:11 +00:00
| CharacterState::BasicBlock { .. }
| CharacterState::UseItem { .. }
| CharacterState::SpriteInteract { .. } => {},
}
}
2021-01-15 03:32:12 +00:00
2021-02-27 19:55:06 +00:00
// Decay combo
for (_, mut combo) in (&read_data.entities, &mut combos).join() {
2021-03-04 20:43:58 +00:00
if combo.counter() > 0
&& read_data.time.0 - combo.last_increase() > comp::combo::COMBO_DECAY_START
{
2021-02-27 19:55:06 +00:00
combo.reset();
}
}
}
}