Make entities protect owned bodies.

This commit is contained in:
Tormod G. Hellen 2022-02-04 01:59:27 +01:00
parent 82273f0f36
commit a00121bedf
2 changed files with 46 additions and 42 deletions

View File

@ -79,6 +79,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bumped tracing-subscriber to resolve [RUSTSEC-2022-0006](https://rustsec.org/advisories/RUSTSEC-2022-0006) - Bumped tracing-subscriber to resolve [RUSTSEC-2022-0006](https://rustsec.org/advisories/RUSTSEC-2022-0006)
- Made /home command a mod+ exclusive - Made /home command a mod+ exclusive
- Friendly creatures will now defend each other - Friendly creatures will now defend each other
- Creatures will now defend their pets
### Removed ### Removed

View File

@ -1503,53 +1503,56 @@ impl<'a> AgentData<'a> {
}) })
}; };
let guard_other = |e_health: &Health, let guard_other =
e_body: Option<&Body>, |e_health: &Health, e_body: Option<&Body>, e_alignment: Option<&Alignment>| {
e_alignment: Option<&Alignment>| { let i_am_a_guard = read_data
let i_am_a_guard = read_data .stats
.stats .get(*self.entity)
.get(*self.entity) .map_or(false, |stats| stats.name == "Guard");
.map_or(false, |stats| stats.name == "Guard"); let other_is_a_villager = matches!(e_alignment, Some(Alignment::Npc));
let other_is_a_villager = matches!(e_alignment, Some(Alignment::Npc)); let we_are_friendly: bool = self.alignment.map_or(false, |ma| {
let we_are_friendly: bool = self.alignment.map_or(false, |ma| { e_alignment.map_or(false, |ea| !ea.hostile_towards(*ma))
e_alignment.map_or(false, |ea| !ea.hostile_towards(*ma)) });
}); let we_share_species: bool = self.body.map_or(false, |mb| {
let we_share_species: bool = self.body.map_or(false, |mb| { e_body.map_or(false, |eb| {
e_body.map_or(false, |eb| { eb.is_same_species_as(mb) || (eb.is_humanoid() && mb.is_humanoid())
eb.is_same_species_as(mb) || (eb.is_humanoid() && mb.is_humanoid()) })
}) });
}); let i_own_other =
let other_has_taken_damage = read_data.time.0 - e_health.last_change.time.0 < 5.0; matches!(e_alignment, Some(Alignment::Owned(ouid)) if self.uid == ouid);
let attacker_of = |health: &Health| health.last_change.damage_by(); let other_has_taken_damage = read_data.time.0 - e_health.last_change.time.0 < 5.0;
let attacker_of = |health: &Health| health.last_change.damage_by();
let i_should_defend = other_has_taken_damage let i_should_defend = other_has_taken_damage
&& ((we_are_friendly && we_share_species) || (i_am_a_guard && other_is_a_villager)); && ((we_are_friendly && we_share_species)
|| (i_am_a_guard && other_is_a_villager)
|| i_own_other);
i_should_defend i_should_defend
.then(|| { .then(|| {
attacker_of(e_health) attacker_of(e_health)
.and_then(|damage_contributor| { .and_then(|damage_contributor| {
get_entity_by_id(damage_contributor.uid().0, read_data) get_entity_by_id(damage_contributor.uid().0, read_data)
}) })
.and_then(|attacker| { .and_then(|attacker| {
read_data.alignments.get(attacker).and_then(|aa| { read_data.alignments.get(attacker).and_then(|aa| {
self.alignment.and_then({ self.alignment.and_then({
|ma| { |ma| {
if !ma.passive_towards(*aa) { if !ma.passive_towards(*aa) {
read_data read_data
.positions .positions
.get(attacker) .get(attacker)
.map(|a_pos| (attacker, *a_pos)) .map(|a_pos| (attacker, *a_pos))
} else { } else {
None None
}
} }
} })
}) })
}) })
}) })
}) .flatten()
.flatten() };
};
let rtsim_remember = let rtsim_remember =
|target_stats: &Stats, |target_stats: &Stats,