2022-01-14 12:19:46 -05:00
|
|
|
use ::rltk::RandomNumberGenerator;
|
2021-12-24 10:38:44 -05:00
|
|
|
use ::specs::prelude::*;
|
2021-12-10 20:16:48 -05:00
|
|
|
|
2022-01-20 11:48:58 -05:00
|
|
|
use crate::colors;
|
2021-11-15 09:45:12 -05:00
|
|
|
use crate::components::{
|
2022-01-20 11:48:58 -05:00
|
|
|
Attributes, EquipmentSlot, Equipped, HungerClock, HungerState, MeleeWeapon, Name,
|
|
|
|
NaturalAttackDefense, Pools, Skill, Skills, WantsToMelee, WeaponAttribute, Wearable,
|
2021-11-15 09:45:12 -05:00
|
|
|
};
|
2022-01-20 11:48:58 -05:00
|
|
|
use crate::effects::{add_effect, EffectType, Targets};
|
2021-12-10 20:16:48 -05:00
|
|
|
use crate::game_log::GameLog;
|
2022-01-03 16:30:14 -05:00
|
|
|
use crate::gamesystem::skill_bonus;
|
2021-10-29 15:15:22 -04:00
|
|
|
|
|
|
|
pub struct MeleeCombatSystem {}
|
|
|
|
|
|
|
|
impl<'a> System<'a> for MeleeCombatSystem {
|
2021-11-15 13:55:31 -05:00
|
|
|
#[allow(clippy::type_complexity)]
|
2021-10-29 15:15:22 -04:00
|
|
|
type SystemData = (
|
|
|
|
Entities<'a>,
|
2021-11-01 14:46:45 -04:00
|
|
|
WriteExpect<'a, GameLog>,
|
2021-10-29 15:15:22 -04:00
|
|
|
WriteStorage<'a, WantsToMelee>,
|
|
|
|
ReadStorage<'a, Name>,
|
2022-01-03 16:30:14 -05:00
|
|
|
ReadStorage<'a, Attributes>,
|
|
|
|
ReadStorage<'a, Skills>,
|
2021-11-19 11:31:27 -05:00
|
|
|
ReadStorage<'a, HungerClock>,
|
2022-01-03 16:30:14 -05:00
|
|
|
ReadStorage<'a, Pools>,
|
|
|
|
WriteExpect<'a, RandomNumberGenerator>,
|
2022-01-04 11:11:38 -05:00
|
|
|
ReadStorage<'a, Equipped>,
|
|
|
|
ReadStorage<'a, MeleeWeapon>,
|
|
|
|
ReadStorage<'a, Wearable>,
|
2022-01-04 11:29:23 -05:00
|
|
|
ReadStorage<'a, NaturalAttackDefense>,
|
2021-10-29 15:15:22 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(&mut self, data: Self::SystemData) {
|
2021-11-15 09:45:12 -05:00
|
|
|
let (
|
|
|
|
entities,
|
|
|
|
mut log,
|
|
|
|
mut wants_melee,
|
|
|
|
names,
|
2022-01-03 16:30:14 -05:00
|
|
|
attributes,
|
|
|
|
skills,
|
2021-11-19 11:31:27 -05:00
|
|
|
hunger_clock,
|
2022-01-03 16:30:14 -05:00
|
|
|
pools,
|
|
|
|
mut rng,
|
2022-01-04 11:11:38 -05:00
|
|
|
equipped_items,
|
|
|
|
meleeweapons,
|
|
|
|
wearables,
|
2022-01-04 11:29:23 -05:00
|
|
|
natural,
|
2021-11-15 09:45:12 -05:00
|
|
|
) = data;
|
2021-10-29 15:15:22 -04:00
|
|
|
|
2022-01-03 16:30:14 -05:00
|
|
|
for (entity, wants_melee, name, attacker_attributes, attacker_skills, attacker_pools) in (
|
|
|
|
&entities,
|
|
|
|
&wants_melee,
|
|
|
|
&names,
|
|
|
|
&attributes,
|
|
|
|
&skills,
|
|
|
|
&pools,
|
|
|
|
)
|
|
|
|
.join()
|
2021-10-29 15:15:22 -04:00
|
|
|
{
|
2022-01-03 16:30:14 -05:00
|
|
|
// Are the attacker and defender alive? Only attack if they are
|
|
|
|
let target_pools = pools.get(wants_melee.target).unwrap();
|
|
|
|
let target_attributes = attributes.get(wants_melee.target).unwrap();
|
|
|
|
let target_skills = skills.get(wants_melee.target).unwrap();
|
|
|
|
if attacker_pools.hit_points.current > 0 && target_pools.hit_points.current > 0 {
|
|
|
|
let target_name = names.get(wants_melee.target).unwrap();
|
2021-11-15 09:45:12 -05:00
|
|
|
|
2022-01-25 14:25:11 -05:00
|
|
|
// Define the basic unarmed attack -- overridden by wielding check below if a weapon is equipped
|
2022-01-04 11:11:38 -05:00
|
|
|
let mut weapon_info = MeleeWeapon {
|
|
|
|
attribute: WeaponAttribute::Might,
|
|
|
|
hit_bonus: 0,
|
|
|
|
damage_n_dice: 1,
|
|
|
|
damage_die_type: 4,
|
|
|
|
damage_bonus: 0,
|
2022-01-25 14:25:11 -05:00
|
|
|
proc_chance: None,
|
|
|
|
proc_target: None,
|
2022-01-04 11:11:38 -05:00
|
|
|
};
|
|
|
|
|
2022-01-04 11:29:23 -05:00
|
|
|
if let Some(nat) = natural.get(entity) {
|
|
|
|
if !nat.attacks.is_empty() {
|
|
|
|
let attack_index = if nat.attacks.len() == 1 {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
rng.roll_dice(1, nat.attacks.len() as i32) as usize - 1
|
|
|
|
};
|
|
|
|
|
|
|
|
let attk = &nat.attacks[attack_index];
|
|
|
|
|
|
|
|
weapon_info.hit_bonus = attk.hit_bonus;
|
|
|
|
weapon_info.damage_n_dice = attk.damage_n_dice;
|
|
|
|
weapon_info.damage_die_type = attk.damage_die_type;
|
|
|
|
weapon_info.damage_bonus = attk.damage_bonus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 14:25:11 -05:00
|
|
|
let mut weapon_entity: Option<Entity> = None;
|
|
|
|
for (weaponentity, wielded, melee) in
|
|
|
|
(&entities, &equipped_items, &meleeweapons).join()
|
|
|
|
{
|
2022-01-04 11:11:38 -05:00
|
|
|
if wielded.owner == entity && wielded.slot == EquipmentSlot::Melee {
|
|
|
|
weapon_info = melee.clone();
|
2022-01-25 14:25:11 -05:00
|
|
|
weapon_entity = Some(weaponentity);
|
2022-01-04 11:11:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 16:30:14 -05:00
|
|
|
let natural_roll = rng.roll_dice(1, 20);
|
2022-01-04 11:11:38 -05:00
|
|
|
let attribute_hit_bonus = if weapon_info.attribute == WeaponAttribute::Might {
|
|
|
|
attacker_attributes.might.bonus
|
|
|
|
} else {
|
|
|
|
attacker_attributes.quickness.bonus
|
|
|
|
};
|
2022-01-03 16:30:14 -05:00
|
|
|
let skill_hit_bonus = skill_bonus(Skill::Melee, &*attacker_skills);
|
2022-01-04 11:11:38 -05:00
|
|
|
let weapon_hit_bonus = weapon_info.hit_bonus;
|
2022-01-03 16:30:14 -05:00
|
|
|
let mut status_hit_bonus = 0;
|
2021-11-19 11:31:27 -05:00
|
|
|
if let Some(hc) = hunger_clock.get(entity) {
|
2022-01-03 16:30:14 -05:00
|
|
|
// Well-Fed grants +1
|
2021-11-19 11:31:27 -05:00
|
|
|
if hc.state == HungerState::WellFed {
|
2022-01-03 16:30:14 -05:00
|
|
|
status_hit_bonus += 1;
|
2021-11-19 11:31:27 -05:00
|
|
|
}
|
|
|
|
}
|
2022-01-03 16:30:14 -05:00
|
|
|
let modified_hit_roll = natural_roll
|
|
|
|
+ attribute_hit_bonus
|
|
|
|
+ skill_hit_bonus
|
|
|
|
+ weapon_hit_bonus
|
|
|
|
+ status_hit_bonus;
|
2021-11-19 11:31:27 -05:00
|
|
|
|
2022-01-04 11:11:38 -05:00
|
|
|
let mut armor_item_bonus_f = 0.0;
|
|
|
|
for (wielded, armor) in (&equipped_items, &wearables).join() {
|
|
|
|
if wielded.owner == wants_melee.target {
|
|
|
|
armor_item_bonus_f += armor.armor_class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 11:29:23 -05:00
|
|
|
let base_armor_class = match natural.get(wants_melee.target) {
|
|
|
|
None => 10,
|
|
|
|
Some(nat) => nat.armor_class.unwrap_or(10),
|
|
|
|
};
|
2022-01-03 16:30:14 -05:00
|
|
|
let armor_quickness_bonus = target_attributes.quickness.bonus;
|
|
|
|
let armor_skill_bonus = skill_bonus(Skill::Defense, &*target_skills);
|
2022-01-04 11:11:38 -05:00
|
|
|
let armor_item_bonus = armor_item_bonus_f as i32;
|
2022-01-03 16:30:14 -05:00
|
|
|
let armor_class =
|
|
|
|
base_armor_class + armor_quickness_bonus + armor_skill_bonus + armor_item_bonus;
|
2021-11-15 09:45:12 -05:00
|
|
|
|
2022-01-03 16:30:14 -05:00
|
|
|
if natural_roll != 1 && (natural_roll == 20 || modified_hit_roll > armor_class) {
|
|
|
|
// Target hit! Until we support weapons, we're going with 1d4
|
|
|
|
let base_damage = rng.roll_dice(1, 4);
|
|
|
|
let attr_damage_bonus = attacker_attributes.might.bonus;
|
|
|
|
let skill_damage_bonus = skill_bonus(Skill::Melee, &*attacker_skills);
|
2022-01-04 11:11:38 -05:00
|
|
|
let weapon_damage_bonus = weapon_info.damage_bonus;
|
2022-01-03 16:30:14 -05:00
|
|
|
|
|
|
|
let damage = i32::max(
|
|
|
|
0,
|
|
|
|
base_damage
|
|
|
|
+ attr_damage_bonus
|
|
|
|
+ skill_hit_bonus
|
|
|
|
+ skill_damage_bonus
|
|
|
|
+ weapon_damage_bonus,
|
|
|
|
);
|
2022-01-20 11:48:58 -05:00
|
|
|
add_effect(
|
|
|
|
Some(entity),
|
|
|
|
EffectType::Damage { amount: damage },
|
|
|
|
Targets::Single {
|
|
|
|
target: wants_melee.target,
|
|
|
|
},
|
2022-01-05 14:59:45 -05:00
|
|
|
);
|
2022-01-03 16:30:14 -05:00
|
|
|
log.append(format!(
|
|
|
|
"{} hits {} for {} hp.",
|
|
|
|
&name.name, &target_name.name, damage
|
|
|
|
));
|
2022-01-25 14:25:11 -05:00
|
|
|
|
|
|
|
// Proc effects
|
|
|
|
if let Some(chance) = &weapon_info.proc_chance {
|
|
|
|
if rng.roll_dice(1, 100) <= (chance * 100.0) as i32 {
|
|
|
|
let effect_target = if weapon_info.proc_target.unwrap() == "Self" {
|
|
|
|
Targets::Single { target: entity }
|
|
|
|
} else {
|
|
|
|
Targets::Single {
|
|
|
|
target: wants_melee.target,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
add_effect(
|
|
|
|
Some(entity),
|
|
|
|
EffectType::ItemUse {
|
|
|
|
item: weapon_entity.unwrap(),
|
|
|
|
},
|
|
|
|
effect_target,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-01-03 16:30:14 -05:00
|
|
|
} else if natural_roll == 1 {
|
|
|
|
// Natural 1 miss
|
|
|
|
log.append(format!(
|
|
|
|
"{} considers attacking {}, but misjudges the timing.",
|
|
|
|
name.name, target_name.name
|
|
|
|
));
|
2022-01-20 11:48:58 -05:00
|
|
|
add_effect(
|
|
|
|
None,
|
|
|
|
EffectType::Particle {
|
|
|
|
glyph: rltk::to_cp437('‼'),
|
|
|
|
fg: colors::BLUE,
|
|
|
|
bg: colors::BLACK,
|
|
|
|
lifespan: 200.0,
|
|
|
|
},
|
|
|
|
Targets::Single {
|
|
|
|
target: wants_melee.target,
|
|
|
|
},
|
|
|
|
);
|
2022-01-03 16:30:14 -05:00
|
|
|
} else {
|
|
|
|
// Miss
|
|
|
|
log.append(format!(
|
|
|
|
"{} attacks {}, but can't connect",
|
|
|
|
name.name, target_name.name
|
|
|
|
));
|
2022-01-20 11:48:58 -05:00
|
|
|
add_effect(
|
|
|
|
None,
|
|
|
|
EffectType::Particle {
|
|
|
|
glyph: rltk::to_cp437('‼'),
|
|
|
|
fg: colors::CYAN,
|
|
|
|
bg: colors::BLACK,
|
|
|
|
lifespan: 200.0,
|
|
|
|
},
|
|
|
|
Targets::Single {
|
|
|
|
target: wants_melee.target,
|
|
|
|
},
|
|
|
|
);
|
2021-10-29 15:15:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wants_melee.clear();
|
|
|
|
}
|
|
|
|
}
|