1use crate::{
2 consts::{
3 AVG_FOLLOW_DIST, DEFAULT_ATTACK_RANGE, IDLE_HEALING_ITEM_THRESHOLD, MAX_PATROL_DIST,
4 SEPARATION_BIAS, SEPARATION_DIST, STD_AWARENESS_DECAY_RATE,
5 },
6 data::{AgentData, AgentEmitters, AttackData, Path, ReadData, Tactic, TargetData},
7 util::{
8 are_our_owners_hostile, entities_have_line_of_sight, get_attacker, get_entity_by_id,
9 is_dead_or_invulnerable, is_dressed_as_cultist, is_dressed_as_pirate, is_dressed_as_witch,
10 is_invulnerable, is_steering, is_village_guard, is_villager,
11 },
12};
13use common::{
14 combat::perception_dist_multiplier_from_stealth,
15 comp::{
16 self, Agent, Alignment, Body, CharacterState, Content, ControlAction, ControlEvent,
17 Controller, HealthChange, InputKind, InventoryAction, Pos, PresenceKind, Scale,
18 UnresolvedChatMsg, UtteranceKind,
19 ability::BASE_ABILITY_LIMIT,
20 agent::{FlightMode, PidControllers, Sound, SoundKind, Target},
21 biped_large, body,
22 inventory::slot::EquipSlot,
23 item::{
24 ConsumableKind, Effects, Item, ItemDesc, ItemKind,
25 tool::{AbilitySpec, ToolKind},
26 },
27 projectile::{ProjectileConstructorKind, aim_projectile},
28 },
29 consts::MAX_MOUNT_RANGE,
30 effect::{BuffEffect, Effect},
31 event::{ChatEvent, EmitExt, SoundEvent},
32 interaction::InteractionKind,
33 match_some,
34 mounting::VolumePos,
35 path::TraversalConfig,
36 rtsim::NpcActivity,
37 states::basic_beam,
38 terrain::Block,
39 time::DayPeriod,
40 util::Dir,
41 vol::ReadVol,
42};
43use itertools::Itertools;
44use rand::{RngExt, rng};
45use specs::Entity as EcsEntity;
46use vek::*;
47
48#[cfg(feature = "use-dyn-lib")]
49use {crate::LIB, std::ffi::CStr};
50
51impl AgentData<'_> {
52 pub fn glider_equip(&self, controller: &mut Controller, read_data: &ReadData) {
56 self.dismount(controller, read_data);
57 controller.push_action(ControlAction::GlideWield);
58 }
59
60 pub fn glider_flight(&self, controller: &mut Controller, _read_data: &ReadData) {
62 let Some(fluid) = self.physics_state.in_fluid else {
63 return;
64 };
65
66 let vel = self.vel;
67
68 let comp::Vel(rel_flow) = fluid.relative_flow(vel);
69
70 let is_wind_downwards = rel_flow.z.is_sign_negative();
71
72 let look_dir = if is_wind_downwards {
73 Vec3::from(-rel_flow.xy())
74 } else {
75 -rel_flow
76 };
77
78 controller.inputs.look_dir = Dir::from_unnormalized(look_dir).unwrap_or_else(Dir::forward);
79 }
80
81 pub fn fly_upward(&self, controller: &mut Controller, read_data: &ReadData) {
82 self.dismount(controller, read_data);
83
84 controller.push_basic_input(InputKind::Fly);
85 controller.inputs.move_z = 1.0;
86 }
87
88 pub fn path_toward_target(
95 &self,
96 agent: &mut Agent,
97 controller: &mut Controller,
98 tgt_pos: Vec3<f32>,
99 read_data: &ReadData,
100 path: Path,
101 speed_multiplier: Option<f32>,
102 ) -> Option<Vec3<f32>> {
103 self.dismount_uncontrollable(controller, read_data);
104
105 let pos_difference = tgt_pos - self.pos.0;
106 let pathing_pos = match path {
107 Path::Separate => {
108 let mut sep_vec: Vec3<f32> = Vec3::zero();
109
110 for entity in read_data
111 .cached_spatial_grid
112 .0
113 .in_circle_aabr(self.pos.0.xy(), SEPARATION_DIST)
114 {
115 if let (Some(alignment), Some(other_alignment)) =
116 (self.alignment, read_data.alignments.get(entity))
117 && Alignment::passive_towards(*alignment, *other_alignment)
118 && let (Some(pos), Some(body), Some(other_body)) = (
119 read_data.positions.get(entity),
120 self.body,
121 read_data.bodies.get(entity),
122 )
123 {
124 let dist_xy = self.pos.0.xy().distance(pos.0.xy());
125 let spacing = body.spacing_radius() + other_body.spacing_radius();
126 if dist_xy < spacing {
127 let pos_diff = self.pos.0.xy() - pos.0.xy();
128 sep_vec += pos_diff.try_normalized().unwrap_or_else(Vec2::zero)
129 * ((spacing - dist_xy) / spacing);
130 }
131 }
132 }
133
134 tgt_pos + sep_vec * SEPARATION_BIAS + pos_difference * (1.0 - SEPARATION_BIAS)
135 },
136 Path::AtTarget => tgt_pos,
137 };
138 let speed_multiplier = speed_multiplier.unwrap_or(1.0).min(1.0);
139
140 let in_loaded_chunk = |pos: Vec3<f32>| {
141 read_data
142 .terrain
143 .contains_key(read_data.terrain.pos_key(pos.map(|e| e.floor() as i32)))
144 };
145
146 let is_target_loaded = in_loaded_chunk(pathing_pos);
151
152 if let Some((bearing, speed, stuck)) = agent.chaser.chase(
153 &*read_data.terrain,
154 self.pos.0,
155 self.vel.0,
156 pathing_pos,
157 TraversalConfig {
158 min_tgt_dist: 0.25,
159 is_target_loaded,
160 ..self.traversal_config
161 },
162 &read_data.time,
163 ) {
164 self.unstuck_if(stuck, controller);
165 self.traverse(controller, bearing, speed * speed_multiplier);
166 Some(bearing)
167 } else {
168 None
169 }
170 }
171
172 fn traverse(&self, controller: &mut Controller, bearing: Vec3<f32>, speed: f32) {
173 controller.inputs.move_dir =
174 bearing.xy().try_normalized().unwrap_or_else(Vec2::zero) * speed;
175
176 self.jump_if(
178 (self.physics_state.on_ground.is_some() && bearing.z > 1.5)
179 || self.traversal_config.can_fly,
180 controller,
181 );
182 controller.inputs.move_z = bearing.z;
183 }
184
185 pub fn unstuck_if(&self, condition: bool, controller: &mut Controller) {
186 if condition && rng().random_bool(0.05) {
187 if matches!(self.char_state, CharacterState::Climb(_)) || rng().random_bool(0.5) {
188 controller.push_basic_input(InputKind::Jump);
189 } else {
190 controller.push_basic_input(InputKind::Roll);
191 }
192 } else {
193 if controller.queued_inputs.contains_key(&InputKind::Jump) {
194 controller.push_cancel_input(InputKind::Jump);
195 }
196 if controller.queued_inputs.contains_key(&InputKind::Roll) {
197 controller.push_cancel_input(InputKind::Roll);
198 }
199 }
200 }
201
202 pub fn jump_if(&self, condition: bool, controller: &mut Controller) {
203 if condition {
204 controller.push_basic_input(InputKind::Jump);
205 } else if controller.queued_inputs.contains_key(&InputKind::Jump) {
206 controller.push_cancel_input(InputKind::Jump)
207 }
208 }
209
210 pub fn idle(
211 &self,
212 agent: &mut Agent,
213 controller: &mut Controller,
214 read_data: &ReadData,
215 _emitters: &mut AgentEmitters,
216 rng: &mut impl RngExt,
217 ) {
218 enum ActionTimers {
219 TimerIdle = 0,
220 }
221
222 agent
223 .awareness
224 .change_by(STD_AWARENESS_DECAY_RATE * read_data.dt.0);
225
226 let lantern_equipped = self
229 .inventory
230 .equipped(EquipSlot::Lantern)
231 .as_ref()
232 .is_some_and(|item| matches!(&*item.kind(), comp::item::ItemKind::Lantern(_)));
233 let lantern_turned_on = self.light_emitter.is_some();
234 let day_period = DayPeriod::from(read_data.time_of_day.0);
235 if lantern_equipped && rng.random_bool(0.001) {
237 if day_period.is_dark() && !lantern_turned_on {
238 controller.push_event(ControlEvent::EnableLantern)
243 } else if lantern_turned_on && day_period.is_light() {
244 controller.push_event(ControlEvent::DisableLantern)
247 }
248 };
249
250 if let Some(body) = self.body {
251 let attempt_heal = if matches!(body, Body::Humanoid(_)) {
252 self.damage < IDLE_HEALING_ITEM_THRESHOLD
253 } else {
254 true
255 };
256 if attempt_heal && self.heal_self(agent, controller, true) {
257 agent.behavior_state.timers[ActionTimers::TimerIdle as usize] = 0.01;
258 return;
259 }
260 } else {
261 agent.behavior_state.timers[ActionTimers::TimerIdle as usize] = 0.01;
262 return;
263 }
264
265 agent.behavior_state.timers[ActionTimers::TimerIdle as usize] = 0.0;
266
267 'activity: {
268 match agent.rtsim_controller.activity {
269 Some(NpcActivity::Goto(travel_to, speed_factor)) => {
270 self.dismount_uncontrollable(controller, read_data);
271
272 agent.bearing = Vec2::zero();
273
274 if self.traversal_config.can_fly
277 && !read_data
278 .terrain
279 .ray(self.pos.0, self.pos.0 + (Vec3::unit_z() * 3.0))
280 .until(Block::is_solid)
281 .cast()
282 .1
283 .map_or(true, |b| b.is_some())
284 {
285 controller.push_basic_input(InputKind::Fly);
286 } else {
287 controller.push_cancel_input(InputKind::Fly)
288 }
289
290 if let Some(bearing) = self.path_toward_target(
291 agent,
292 controller,
293 travel_to,
294 read_data,
295 Path::AtTarget,
296 Some(speed_factor),
297 ) {
298 let height_offset = bearing.z
299 + if self.traversal_config.can_fly {
300 let obstacle_ahead = read_data
302 .terrain
303 .ray(
304 self.pos.0 + Vec3::unit_z(),
305 self.pos.0
306 + bearing.try_normalized().unwrap_or_else(Vec3::unit_y)
307 * 80.0
308 + Vec3::unit_z(),
309 )
310 .until(Block::is_solid)
311 .cast()
312 .1
313 .map_or(true, |b| b.is_some());
314
315 let mut ground_too_close = self
316 .body
317 .map(|body| {
318 #[cfg(feature = "worldgen")]
319 let height_approx = self.pos.0.z
320 - read_data
321 .world
322 .sim()
323 .get_alt_approx(
324 self.pos.0.xy().map(|x: f32| x as i32),
325 )
326 .unwrap_or(0.0);
327 #[cfg(not(feature = "worldgen"))]
328 let height_approx = self.pos.0.z;
329
330 height_approx < body.flying_height()
331 })
332 .unwrap_or(false);
333
334 const NUM_RAYS: usize = 5;
335
336 for i in 0..=NUM_RAYS {
338 let magnitude = self.body.map_or(20.0, |b| b.flying_height());
339 if let Some(dir) = Lerp::lerp(
344 -Vec3::unit_z(),
345 Vec3::new(bearing.x, bearing.y, 0.0),
346 i as f32 / NUM_RAYS as f32,
347 )
348 .try_normalized()
349 {
350 ground_too_close |= read_data
351 .terrain
352 .ray(self.pos.0, self.pos.0 + magnitude * dir)
353 .until(|b: &Block| b.is_solid() || b.is_liquid())
354 .cast()
355 .1
356 .is_ok_and(|b| b.is_some())
357 }
358 }
359
360 if obstacle_ahead || ground_too_close {
361 5.0 } else {
363 -2.0
364 } } else {
366 0.05 };
368
369 if let Some(mpid) = agent.multi_pid_controllers.as_mut() {
370 if let Some(z_controller) = mpid.z_controller.as_mut() {
371 z_controller.sp = self.pos.0.z + height_offset;
372 controller.inputs.move_z = z_controller.calc_err();
373 z_controller.limit_integral_windup(|z| *z = z.clamp(-10.0, 10.0));
375 } else {
376 controller.inputs.move_z = 0.0;
377 }
378 } else {
379 controller.inputs.move_z = height_offset;
380 }
381 }
382
383 if rng.random_bool(0.1)
385 && matches!(
386 read_data.char_states.get(*self.entity),
387 Some(CharacterState::Wielding(_))
388 )
389 {
390 controller.push_action(ControlAction::Unwield);
391 }
392 break 'activity; },
394
395 Some(NpcActivity::GotoFlying(
396 travel_to,
397 speed_factor,
398 height_offset,
399 direction_override,
400 flight_mode,
401 )) => {
402 self.dismount_uncontrollable(controller, read_data);
403
404 if self.traversal_config.vectored_propulsion {
405 controller.push_basic_input(InputKind::Fly);
418
419 if let Some(direction) = direction_override {
434 controller.inputs.look_dir = direction;
435 } else {
436 controller.inputs.look_dir =
438 Dir::from_unnormalized((travel_to - self.pos.0).xy().with_z(0.0))
439 .unwrap_or_default();
440 }
441
442 if agent
456 .multi_pid_controllers
457 .as_ref()
458 .is_some_and(|mpid| mpid.mode != flight_mode)
459 {
460 agent.multi_pid_controllers = None;
461 }
462 let mpid = agent.multi_pid_controllers.get_or_insert_with(|| {
463 PidControllers::<16>::new_multi_pid_controllers(flight_mode, travel_to)
464 });
465 let sample_time = read_data.time.0;
466
467 #[allow(unused_variables)]
468 let terrain_alt_with_lookahead = |dist: f32| -> f32 {
469 #[cfg(feature = "worldgen")]
471 let terrain_alt = read_data
472 .world
473 .sim()
474 .get_alt_approx(
475 (self.pos.0.xy()
476 + controller.inputs.look_dir.to_vec().xy() * dist)
477 .map(|x: f32| x as i32),
478 )
479 .unwrap_or(0.0);
480 #[cfg(not(feature = "worldgen"))]
481 let terrain_alt = 0.0;
482 terrain_alt
483 };
484
485 if flight_mode == FlightMode::FlyThrough {
486 let travel_vec = travel_to - self.pos.0;
487 let bearing =
488 travel_vec.xy().try_normalized().unwrap_or_else(Vec2::zero);
489 controller.inputs.move_dir = bearing * speed_factor;
490 let terrain_alt = terrain_alt_with_lookahead(32.0);
491 let height = height_offset.unwrap_or(100.0);
492 if let Some(z_controller) = mpid.z_controller.as_mut() {
493 z_controller.sp = terrain_alt + height;
494 }
495 mpid.add_measurement(sample_time, self.pos.0);
496 if terrain_alt >= self.pos.0.z - 32.0 {
498 controller.inputs.move_z = 1.0 * speed_factor;
501 controller.inputs.move_dir =
503 self.vel.0.xy().try_normalized().unwrap_or_else(Vec2::zero)
504 * -1.0
505 * speed_factor;
506 } else {
507 controller.inputs.move_z =
508 mpid.calc_err_z().unwrap_or(0.0).min(1.0) * speed_factor;
509 }
510 mpid.limit_windup_z(|z| *z = z.clamp(-20.0, 20.0));
515 } else {
516 if let Some(x_controller) = mpid.x_controller.as_mut() {
520 x_controller.sp = travel_to.x;
521 }
522 if let Some(y_controller) = mpid.y_controller.as_mut() {
523 y_controller.sp = travel_to.y;
524 }
525
526 let z_setpoint = if let Some(height) = height_offset {
531 let clearance_alt = terrain_alt_with_lookahead(16.0) + height;
532 clearance_alt.max(travel_to.z)
533 } else {
534 travel_to.z
535 };
536 if let Some(z_controller) = mpid.z_controller.as_mut() {
537 z_controller.sp = z_setpoint;
538 }
539
540 mpid.add_measurement(sample_time, self.pos.0);
541 controller.inputs.move_dir.x =
542 mpid.calc_err_x().unwrap_or(0.0).min(1.0) * speed_factor;
543 controller.inputs.move_dir.y =
544 mpid.calc_err_y().unwrap_or(0.0).min(1.0) * speed_factor;
545 controller.inputs.move_z =
546 mpid.calc_err_z().unwrap_or(0.0).min(1.0) * speed_factor;
547
548 mpid.limit_windup_x(|x| *x = x.clamp(-1.0, 1.0));
550 mpid.limit_windup_y(|y| *y = y.clamp(-1.0, 1.0));
551 mpid.limit_windup_z(|z| *z = z.clamp(-1.0, 1.0));
552 }
553 }
554 break 'activity; },
556 Some(NpcActivity::Gather(_resources)) => {
557 controller.push_action(ControlAction::Dance);
559 break 'activity; },
561 Some(NpcActivity::Dance(dir)) => {
562 if let Some(look_dir) = dir {
564 controller.inputs.look_dir = look_dir;
565 if self.ori.look_dir().dot(look_dir.to_vec()) < 0.95 {
566 controller.inputs.move_dir = look_dir.to_vec().xy() * 0.01;
567 break 'activity;
568 } else {
569 controller.inputs.move_dir = Vec2::zero();
570 }
571 }
572 controller.push_action(ControlAction::Dance);
573 break 'activity; },
575 Some(NpcActivity::Cheer(dir)) => {
576 if let Some(look_dir) = dir {
577 controller.inputs.look_dir = look_dir;
578 if self.ori.look_dir().dot(look_dir.to_vec()) < 0.95 {
579 controller.inputs.move_dir = look_dir.to_vec().xy() * 0.01;
580 break 'activity;
581 } else {
582 controller.inputs.move_dir = Vec2::zero();
583 }
584 }
585 controller.push_action(ControlAction::Talk(None));
586 break 'activity; },
588 Some(NpcActivity::Sit(dir, pos)) => {
589 if let Some(pos) =
590 pos.filter(|p| read_data.terrain.get(*p).is_ok_and(|b| b.is_mountable()))
591 {
592 if !read_data.is_volume_riders.contains(*self.entity) {
593 controller
594 .push_event(ControlEvent::MountVolume(VolumePos::terrain(pos)));
595 }
596 } else {
597 if let Some(look_dir) = dir {
598 controller.inputs.look_dir = look_dir;
599 if self.ori.look_dir().dot(look_dir.to_vec()) < 0.95 {
600 controller.inputs.move_dir = look_dir.to_vec().xy() * 0.01;
601 break 'activity;
602 } else {
603 controller.inputs.move_dir = Vec2::zero();
604 }
605 }
606 controller.push_action(ControlAction::Sit);
607 }
608 break 'activity; },
610 Some(NpcActivity::HuntAnimals) => {
611 if rng.random::<f32>() < 0.1 {
612 self.choose_target(
613 agent,
614 controller,
615 read_data,
616 AgentData::is_hunting_animal,
617 );
618 }
619 },
620 Some(NpcActivity::Talk(target)) => {
621 if agent.target.is_none()
622 && let Some(target) = read_data.id_maps.actor_entity(target)
623 && let Some(target_uid) = read_data.uids.get(target)
624 {
625 controller.push_action(ControlAction::Stand);
627 self.look_toward(controller, read_data, target);
628 controller.push_action(ControlAction::Talk(Some(*target_uid)));
629 break 'activity;
630 }
631 },
632 None => {},
633 }
634
635 let owner_uid = self
636 .alignment
637 .and_then(|alignment| match_some!(alignment, Alignment::Owned(uid) => uid));
638
639 let owner = owner_uid.and_then(|owner_uid| get_entity_by_id(*owner_uid, read_data));
640
641 let is_being_pet = read_data
642 .interactors
643 .get(*self.entity)
644 .and_then(|interactors| interactors.get(*owner_uid?))
645 .is_some_and(|interaction| matches!(interaction.kind, InteractionKind::Pet));
646
647 let is_in_range = owner
648 .and_then(|owner| read_data.positions.get(owner))
649 .is_some_and(|pos| pos.0.distance_squared(self.pos.0) < MAX_MOUNT_RANGE.powi(2));
650
651 if read_data.is_riders.contains(*self.entity) {
653 if rng.random_bool(0.0001) {
654 self.dismount_uncontrollable(controller, read_data);
655 } else {
656 break 'activity;
657 }
658 } else if let Some(owner_uid) = owner_uid
659 && is_in_range
660 && !is_being_pet
661 && rng.random_bool(0.01)
662 {
663 controller.push_event(ControlEvent::Mount(*owner_uid));
664 break 'activity;
665 }
666
667 if self.traversal_config.can_fly
670 && self
671 .inventory
672 .equipped(EquipSlot::ActiveMainhand)
673 .as_ref()
674 .is_some_and(|item| {
675 item.ability_spec().is_some_and(|a_s| match &*a_s {
676 AbilitySpec::Custom(spec) => {
677 matches!(
678 spec.as_str(),
679 "Simple Flying Melee"
680 | "Bloodmoon Bat"
681 | "Vampire Bat"
682 | "Flame Wyvern"
683 | "Frost Wyvern"
684 | "Cloud Wyvern"
685 | "Sea Wyvern"
686 | "Weald Wyvern"
687 )
688 },
689 _ => false,
690 })
691 })
692 {
693 controller.push_basic_input(InputKind::Fly);
695 let alt = read_data
698 .terrain
699 .ray(self.pos.0, self.pos.0 - (Vec3::unit_z() * 7.0))
700 .until(Block::is_solid)
701 .cast()
702 .0;
703 let set_point = 5.0;
704 let error = set_point - alt;
705 controller.inputs.move_z = error;
706 if self.physics_state.on_ground.is_some() {
708 controller.push_basic_input(InputKind::Jump);
709 }
710 }
711
712 let diff = Vec2::new(rng.random::<f32>() - 0.5, rng.random::<f32>() - 0.5);
713 agent.bearing += (diff * 0.1 - agent.bearing * 0.01)
714 * agent.psyche.idle_wander_factor.max(0.0).sqrt()
715 * agent.psyche.aggro_range_multiplier.max(0.0).sqrt();
716 if let Some(patrol_origin) = agent.patrol_origin
717 .or_else(|| if let Some(Alignment::Owned(owner_uid)) = self.alignment
719 && let Some(owner) = get_entity_by_id(*owner_uid, read_data)
720 && let Some(pos) = read_data.positions.get(owner)
721 {
722 Some(pos.0)
723 } else {
724 None
725 })
726 {
727 agent.bearing += ((patrol_origin.xy() - self.pos.0.xy())
728 / (0.01 + MAX_PATROL_DIST * agent.psyche.idle_wander_factor))
729 * 0.015
730 * agent.psyche.idle_wander_factor;
731 }
732
733 agent.bearing *= 0.1
737 + if read_data
738 .terrain
739 .ray(
740 self.pos.0 + Vec3::unit_z(),
741 self.pos.0
742 + Vec3::from(agent.bearing)
743 .try_normalized()
744 .unwrap_or_else(Vec3::unit_y)
745 * 5.0
746 + Vec3::unit_z(),
747 )
748 .until(Block::is_solid)
749 .cast()
750 .1
751 .map_or(true, |b| b.is_none())
752 && read_data
753 .terrain
754 .ray(
755 self.pos.0
756 + Vec3::from(agent.bearing)
757 .try_normalized()
758 .unwrap_or_else(Vec3::unit_y),
759 self.pos.0
760 + Vec3::from(agent.bearing)
761 .try_normalized()
762 .unwrap_or_else(Vec3::unit_y)
763 - Vec3::unit_z() * 4.0,
764 )
765 .until(Block::is_solid)
766 .cast()
767 .0
768 < 3.0
769 {
770 0.9
771 } else {
772 0.0
773 };
774
775 if agent.bearing.magnitude_squared() > 0.5f32.powi(2) {
776 controller.inputs.move_dir = agent.bearing;
777 }
778
779 if rng.random_bool(0.1)
781 && matches!(
782 read_data.char_states.get(*self.entity),
783 Some(CharacterState::Wielding(_))
784 )
785 {
786 controller.push_action(ControlAction::Unwield);
787 }
788
789 if rng.random::<f32>() < 0.0015 {
790 controller.push_utterance(UtteranceKind::Calm);
791 }
792
793 if rng.random::<f32>() < 0.0035 {
795 controller.push_action(ControlAction::Sit);
796 }
797 }
798 }
799
800 pub fn follow(
801 &self,
802 agent: &mut Agent,
803 controller: &mut Controller,
804 read_data: &ReadData,
805 tgt_pos: &Pos,
806 ) {
807 self.dismount_uncontrollable(controller, read_data);
808
809 if let Some((bearing, speed, stuck)) = agent.chaser.chase(
810 &*read_data.terrain,
811 self.pos.0,
812 self.vel.0,
813 tgt_pos.0,
814 TraversalConfig {
815 min_tgt_dist: AVG_FOLLOW_DIST,
816 ..self.traversal_config
817 },
818 &read_data.time,
819 ) {
820 self.unstuck_if(stuck, controller);
821 let dist_sqrd = self.pos.0.distance_squared(tgt_pos.0);
822 self.traverse(
823 controller,
824 bearing,
825 speed.min(0.2 + (dist_sqrd - AVG_FOLLOW_DIST.powi(2)) / 8.0),
826 );
827 }
828 }
829
830 pub fn look_toward(
831 &self,
832 controller: &mut Controller,
833 read_data: &ReadData,
834 target: EcsEntity,
835 ) -> bool {
836 if let Some(tgt_pos) = read_data.positions.get(target)
837 && !is_steering(*self.entity, read_data)
838 && let Some(dir) = Dir::look_toward(
839 self.pos,
840 self.body,
841 Some(&comp::Scale(self.scale)),
842 tgt_pos,
843 read_data.bodies.get(target),
844 read_data.scales.get(target),
845 )
846 {
847 controller.inputs.look_dir = dir;
848 true
849 } else {
850 false
851 }
852 }
853
854 pub fn flee(
855 &self,
856 agent: &mut Agent,
857 controller: &mut Controller,
858 read_data: &ReadData,
859 tgt_pos: &Pos,
860 ) {
861 const MAX_FLEE_SPEED: f32 = 0.65;
863
864 self.dismount_uncontrollable(controller, read_data);
865
866 if let Some(body) = self.body
867 && body.can_strafe()
868 && !self.is_gliding
869 {
870 controller.push_action(ControlAction::Unwield);
871 }
872
873 if let Some((bearing, speed, stuck)) = agent.chaser.chase(
874 &*read_data.terrain,
875 self.pos.0,
876 self.vel.0,
877 self.pos.0
879 + (self.pos.0 - tgt_pos.0)
880 .try_normalized()
881 .unwrap_or_else(Vec3::unit_y)
882 * 50.0,
883 TraversalConfig {
884 min_tgt_dist: 1.25,
885 ..self.traversal_config
886 },
887 &read_data.time,
888 ) {
889 self.unstuck_if(stuck, controller);
890 self.traverse(controller, bearing, speed.min(MAX_FLEE_SPEED));
891 }
892 }
893
894 pub fn heal_self(
899 &self,
900 _agent: &mut Agent,
901 controller: &mut Controller,
902 relaxed: bool,
903 ) -> bool {
904 if self.buffs.is_some_and(|buffs| {
906 buffs.iter_active().flatten().any(|buff| {
907 buff.kind.effects(&buff.data, None).iter().any(|effect| {
910 if let comp::BuffEffect::HealthChangeOverTime { rate, .. } = effect
911 && *rate > 0.0
912 {
913 true
914 } else {
915 false
916 }
917 })
918 })
919 }) {
920 return false;
921 }
922
923 let heal_multiplier = self.stats.map_or(1.0, |s| s.item_effect_reduction);
925 if heal_multiplier < 0.5 {
926 return false;
927 }
928 let effect_healing_value = |effect: &Effect| -> (f32, f32) {
930 let mut value = 0.0;
931 let mut heal_reduction = 0.0;
932 match effect {
933 Effect::Health(HealthChange { amount, .. }) => {
934 value += *amount;
935 },
936 Effect::Buff(BuffEffect { kind, data, .. }) => {
937 if let Some(duration) = data.duration {
938 for effect in kind.effects(data, None) {
941 match effect {
942 comp::BuffEffect::HealthChangeOverTime { rate, kind, .. } => {
943 let amount = match kind {
944 comp::ModifierKind::Additive => rate * duration.0 as f32,
945 comp::ModifierKind::Multiplicative => {
946 (1.0 + rate).powf(duration.0 as f32)
947 },
948 };
949
950 value += amount;
951 },
952 comp::BuffEffect::ItemEffectReduction(amount) => {
953 heal_reduction =
954 heal_reduction + amount - heal_reduction * amount;
955 },
956 _ => {},
957 }
958 }
959 value += data.strength * data.duration.map_or(0.0, |d| d.0 as f32);
960 }
961 },
962
963 _ => {},
964 }
965
966 (value, heal_reduction)
967 };
968 let healing_value = |item: &Item| {
969 let mut value = 0.0;
970 let mut heal_multiplier_value = 1.0;
971
972 if let ItemKind::Consumable { kind, effects, .. } = &*item.kind()
973 && (matches!(kind, ConsumableKind::Drink)
974 || (relaxed && matches!(kind, ConsumableKind::Food)))
975 {
976 match effects {
977 Effects::Any(effects) => {
978 for effect in effects.iter() {
980 let (add, red) = effect_healing_value(effect);
981 value += add / effects.len() as f32;
982 heal_multiplier_value *= 1.0 - red / effects.len() as f32;
983 }
984 },
985 Effects::All(_) | Effects::One(_) => {
986 for effect in effects.effects() {
987 let (add, red) = effect_healing_value(effect);
988 value += add;
989 heal_multiplier_value *= 1.0 - red;
990 }
991 },
992 }
993 }
994 if heal_multiplier_value < 1.0 && (heal_multiplier < 1.0 || relaxed) {
997 value *= 0.1;
998 }
999 value as i32
1000 };
1001
1002 let item = self
1003 .inventory
1004 .slots_with_id()
1005 .filter_map(|(id, slot)| match slot {
1006 Some(item) if healing_value(item) > 0 => Some((id, item)),
1007 _ => None,
1008 })
1009 .max_by_key(|(_, item)| {
1010 if relaxed {
1011 -healing_value(item)
1012 } else {
1013 healing_value(item)
1014 }
1015 });
1016
1017 if let Some((id, _)) = item {
1018 use comp::inventory::slot::Slot;
1019 controller.push_action(ControlAction::InventoryAction(InventoryAction::Use(
1020 Slot::Inventory(id),
1021 )));
1022 true
1023 } else {
1024 false
1025 }
1026 }
1027
1028 pub fn choose_target(
1029 &self,
1030 agent: &mut Agent,
1031 controller: &mut Controller,
1032 read_data: &ReadData,
1033 is_enemy: fn(&Self, EcsEntity, &ReadData) -> bool,
1034 ) {
1035 enum ActionStateTimers {
1036 TimerChooseTarget = 0,
1037 }
1038 agent.behavior_state.timers[ActionStateTimers::TimerChooseTarget as usize] = 0.0;
1039 let mut aggro_on = false;
1040
1041 let common::CachedSpatialGrid(grid) = self.cached_spatial_grid;
1044
1045 let entities_nearby = grid
1046 .in_circle_aabr(self.pos.0.xy(), agent.psyche.search_dist())
1047 .collect_vec();
1048
1049 let get_pos = |entity| read_data.positions.get(entity);
1050 let get_enemy = |(entity, attack_target): (EcsEntity, bool)| {
1051 if attack_target {
1052 if is_enemy(self, entity, read_data) {
1053 Some((entity, true))
1054 } else if self.should_defend(entity, read_data) {
1055 if let Some(attacker) = get_attacker(entity, read_data) {
1056 if !self.passive_towards(attacker, read_data) {
1057 aggro_on = true;
1059 Some((attacker, true))
1060 } else {
1061 None
1062 }
1063 } else {
1064 None
1065 }
1066 } else {
1067 None
1068 }
1069 } else {
1070 Some((entity, false))
1071 }
1072 };
1073 let is_valid_target = |entity: EcsEntity| match read_data.bodies.get(entity) {
1074 Some(Body::Item(item)) => {
1075 if !matches!(item, body::item::Body::Thrown(_)) {
1076 let is_humanoid = matches!(self.body, Some(Body::Humanoid(_)));
1077 let avoids_item_drops = matches!(
1078 self.body,
1079 Some(Body::BipedLarge(biped_large::Body {
1080 species: biped_large::Species::Gigasfrost
1081 | biped_large::Species::Gigasfire,
1082 ..
1083 }))
1084 );
1085 let wants_pickup = !avoids_item_drops
1088 && (is_humanoid || matches!(item, body::item::Body::Consumable));
1089
1090 let attempt_pickup = wants_pickup
1093 && read_data
1094 .loot_owners
1095 .get(entity).is_none_or(|loot_owner| {
1096 !(is_humanoid
1097 && loot_owner.can_pickup(
1098 *self.uid,
1099 read_data.groups.get(entity),
1100 self.alignment,
1101 self.body,
1102 None,
1103 )
1104 && (
1105 !loot_owner.is_soft() ||
1106 loot_owner
1108 .uid()
1109 .and_then(|uid| read_data.id_maps.uid_entity(uid)).is_none_or(|entity| !is_enemy(self, entity, read_data)))
1110 )
1111 });
1112
1113 if attempt_pickup {
1114 Some((entity, false))
1115 } else {
1116 None
1117 }
1118 } else {
1119 None
1120 }
1121 },
1122 _ => {
1123 if read_data
1124 .healths
1125 .get(entity)
1126 .is_some_and(|health| !health.is_dead && !is_invulnerable(entity, read_data))
1127 {
1128 let needs_saving = comp::is_downed(
1129 read_data.healths.get(entity),
1130 read_data.char_states.get(entity),
1131 );
1132
1133 let wants_to_save = match (self.alignment, read_data.alignments.get(entity)) {
1134 (Some(Alignment::Npc), _) if read_data.presences.get(entity).is_some_and(|presence| matches!(presence.kind, PresenceKind::Character(_))) => true,
1137 (Some(Alignment::Npc), Some(Alignment::Npc)) => true,
1138 (Some(Alignment::Enemy), Some(Alignment::Enemy)) => true,
1139 _ => false,
1140 } && agent.allowed_to_speak()
1141 && read_data
1143 .interactors
1144 .get(entity).is_none_or(|interactors| {
1145 !interactors.has_interaction(InteractionKind::HelpDowned)
1146 }) && self.char_state.can_interact();
1147
1148 Some((entity, !(needs_saving && wants_to_save)))
1150 } else {
1151 None
1152 }
1153 },
1154 };
1155
1156 let is_detected = |entity: &EcsEntity, e_pos: &Pos, e_scale: Option<&Scale>| {
1157 self.detects_other(agent, controller, entity, e_pos, e_scale, read_data)
1158 };
1159
1160 let target = entities_nearby
1161 .iter()
1162 .filter_map(|e| is_valid_target(*e))
1163 .filter_map(get_enemy)
1164 .filter_map(|(entity, attack_target)| {
1165 get_pos(entity).map(|pos| (entity, pos, attack_target))
1166 })
1167 .filter(|(entity, e_pos, _)| is_detected(entity, e_pos, read_data.scales.get(*entity)))
1168 .min_by_key(|(_, e_pos, attack_target)| {
1169 (
1170 *attack_target,
1171 (e_pos.0.distance_squared(self.pos.0) * 100.0) as i32,
1172 )
1173 })
1174 .map(|(entity, _, attack_target)| (entity, attack_target));
1175
1176 if agent.target.is_none() && target.is_some() {
1177 if aggro_on {
1178 controller.push_utterance(UtteranceKind::Angry);
1179 } else {
1180 controller.push_utterance(UtteranceKind::Surprised);
1181 }
1182 }
1183 if agent.psyche.should_stop_pursuing || target.is_some() {
1184 agent.target = target.map(|(entity, attack_target)| Target {
1185 target: entity,
1186 hostile: attack_target,
1187 selected_at: read_data.time.0,
1188 aggro_on,
1189 last_known_pos: get_pos(entity).map(|pos| pos.0),
1190 })
1191 }
1192 }
1193
1194 pub fn attack(
1195 &self,
1196 agent: &mut Agent,
1197 controller: &mut Controller,
1198 tgt_data: &TargetData,
1199 read_data: &ReadData,
1200 rng: &mut impl RngExt,
1201 ) {
1202 #[cfg(any(feature = "be-dyn-lib", feature = "use-dyn-lib"))]
1203 let _rng = rng;
1204
1205 #[cfg(not(feature = "use-dyn-lib"))]
1206 {
1207 #[cfg(not(feature = "be-dyn-lib"))]
1208 self.attack_inner(agent, controller, tgt_data, read_data, rng);
1209 #[cfg(feature = "be-dyn-lib")]
1210 self.attack_inner(agent, controller, tgt_data, read_data);
1211 }
1212 #[cfg(feature = "use-dyn-lib")]
1213 {
1214 let lock = LIB.lock().unwrap();
1215 let lib = &lock.as_ref().unwrap().lib;
1216 const ATTACK_FN: &[u8] = b"attack_inner\0";
1217
1218 let attack_fn: common_dynlib::Symbol<
1219 fn(&Self, &mut Agent, &mut Controller, &TargetData, &ReadData),
1220 > = unsafe { lib.get(ATTACK_FN) }.unwrap_or_else(|e| {
1221 panic!(
1222 "Trying to use: {} but had error: {:?}",
1223 CStr::from_bytes_with_nul(ATTACK_FN)
1224 .map(CStr::to_str)
1225 .unwrap()
1226 .unwrap(),
1227 e
1228 )
1229 });
1230 attack_fn(self, agent, controller, tgt_data, read_data);
1231 }
1232 }
1233
1234 #[cfg_attr(feature = "be-dyn-lib", unsafe(export_name = "attack_inner"))]
1235 pub fn attack_inner(
1236 &self,
1237 agent: &mut Agent,
1238 controller: &mut Controller,
1239 tgt_data: &TargetData,
1240 read_data: &ReadData,
1241 #[cfg(not(feature = "be-dyn-lib"))] rng: &mut impl RngExt,
1242 ) {
1243 #[cfg(feature = "be-dyn-lib")]
1244 let rng = &mut rng();
1245
1246 self.dismount_uncontrollable(controller, read_data);
1247
1248 let tool_tactic = |tool_kind| match tool_kind {
1249 ToolKind::Bow => Tactic::Bow,
1250 ToolKind::Staff => Tactic::Staff,
1251 ToolKind::Sceptre => Tactic::Sceptre,
1252 ToolKind::Hammer => Tactic::Hammer,
1253 ToolKind::Sword | ToolKind::Blowgun => Tactic::Sword,
1254 ToolKind::Axe => Tactic::Axe,
1255 _ => Tactic::SimpleMelee,
1256 };
1257
1258 let tactic = self
1259 .inventory
1260 .equipped(EquipSlot::ActiveMainhand)
1261 .as_ref()
1262 .map(|item| {
1263 if let Some(ability_spec) = item.ability_spec() {
1264 match &*ability_spec {
1265 AbilitySpec::Custom(spec) => match spec.as_str() {
1266 "Oni" | "Sword Simple" | "BipedLargeCultistSword" => {
1267 Tactic::SwordSimple
1268 },
1269 "Staff Simple" | "BipedLargeCultistStaff" | "Ogre Staff" => {
1270 Tactic::Staff
1271 },
1272 "BipedLargeCultistHammer" => Tactic::Hammer,
1273 "Simple Flying Melee" => Tactic::SimpleFlyingMelee,
1274 "Bow Simple" | "BipedLargeCultistBow" => Tactic::Bow,
1275 "Stone Golem" | "Coral Golem" => Tactic::StoneGolem,
1276 "Iron Golem" => Tactic::IronGolem,
1277 "Quad Med Quick" => Tactic::CircleCharge {
1278 radius: 5,
1279 circle_time: 2,
1280 },
1281 "Quad Med Jump" | "Darkhound" => Tactic::QuadMedJump,
1282 "Quad Med Charge" => Tactic::CircleCharge {
1283 radius: 6,
1284 circle_time: 1,
1285 },
1286 "Quad Med Basic" => Tactic::QuadMedBasic,
1287 "Quad Med Hoof" => Tactic::QuadMedHoof,
1288 "ClaySteed" => Tactic::ClaySteed,
1289 "Elephant" => Tactic::Elephant,
1290 "Rocksnapper" => Tactic::Rocksnapper,
1291 "Roshwalr" => Tactic::Roshwalr,
1292 "Asp" | "Maneater" => Tactic::QuadLowRanged,
1293 "Quad Low Breathe" | "Quad Low Beam" | "Basilisk" => {
1294 Tactic::QuadLowBeam
1295 },
1296 "Organ" => Tactic::OrganAura,
1297 "Quad Low Tail" | "Husk Brute" => Tactic::TailSlap,
1298 "Quad Low Quick" => Tactic::QuadLowQuick,
1299 "Quad Low Basic" => Tactic::QuadLowBasic,
1300 "Theropod Basic" | "Theropod Bird" | "Theropod Small" => {
1301 Tactic::Theropod
1302 },
1303 "Antlion" => Tactic::ArthropodMelee,
1305 "Tarantula" | "Horn Beetle" => Tactic::ArthropodAmbush,
1306 "Weevil" | "Black Widow" | "Crawler" => Tactic::ArthropodRanged,
1307 "Theropod Charge" => Tactic::CircleCharge {
1308 radius: 6,
1309 circle_time: 1,
1310 },
1311 "Turret" => Tactic::RadialTurret,
1312 "Flamethrower" => Tactic::RadialTurret,
1313 "Haniwa Sentry" => Tactic::RotatingTurret,
1314 "Bird Large Breathe" => Tactic::BirdLargeBreathe,
1315 "Bird Large Fire" => Tactic::BirdLargeFire,
1316 "Bird Large Basic" => Tactic::BirdLargeBasic,
1317 "Flame Wyvern" | "Frost Wyvern" | "Cloud Wyvern" | "Sea Wyvern"
1318 | "Weald Wyvern" => Tactic::Wyvern,
1319 "Bird Medium Basic" => Tactic::BirdMediumBasic,
1320 "Bushly" | "Cactid" | "Irrwurz" | "Driggle" | "Mossy Snail"
1321 | "Strigoi Claws" | "Harlequin" => Tactic::SimpleDouble,
1322 "Clay Golem" => Tactic::ClayGolem,
1323 "Ancient Effigy" => Tactic::AncientEffigy,
1324 "TerracottaStatue" | "Mogwai" => Tactic::TerracottaStatue,
1325 "TerracottaBesieger" => Tactic::Bow,
1326 "TerracottaDemolisher" => Tactic::SimpleDouble,
1327 "TerracottaPunisher" => Tactic::SimpleMelee,
1328 "TerracottaPursuer" => Tactic::SwordSimple,
1329 "Cursekeeper" => Tactic::Cursekeeper,
1330 "CursekeeperFake" => Tactic::CursekeeperFake,
1331 "ShamanicSpirit" => Tactic::ShamanicSpirit,
1332 "Jiangshi" => Tactic::Jiangshi,
1333 "Mindflayer" => Tactic::Mindflayer,
1334 "Flamekeeper" => Tactic::Flamekeeper,
1335 "Forgemaster" => Tactic::Forgemaster,
1336 "Minotaur" => Tactic::Minotaur,
1337 "Cyclops" => Tactic::Cyclops,
1338 "Dullahan" => Tactic::Dullahan,
1339 "Grave Warden" => Tactic::GraveWarden,
1340 "Tidal Warrior" => Tactic::TidalWarrior,
1341 "Karkatha" => Tactic::Karkatha,
1342 "Tidal Totem"
1343 | "Tornado"
1344 | "Gnarling Totem Red"
1345 | "Gnarling Totem Green"
1346 | "Gnarling Totem White" => Tactic::RadialTurret,
1347 "FieryTornado" => Tactic::FieryTornado,
1348 "Yeti" => Tactic::Yeti,
1349 "Harvester" => Tactic::Harvester,
1350 "Cardinal" => Tactic::Cardinal,
1351 "Sea Bishop" => Tactic::SeaBishop,
1352 "Dagon" => Tactic::Dagon,
1353 "Snaretongue" => Tactic::Snaretongue,
1354 "Dagonite" => Tactic::ArthropodAmbush,
1355 "Gnarling Dagger" => Tactic::SimpleBackstab,
1356 "Gnarling Blowgun" => Tactic::ElevatedRanged,
1357 "Deadwood" => Tactic::Deadwood,
1358 "Mandragora" => Tactic::Mandragora,
1359 "Wood Golem" => Tactic::WoodGolem,
1360 "Gnarling Chieftain" => Tactic::GnarlingChieftain,
1361 "Frost Gigas" => Tactic::FrostGigas,
1362 "Boreal Hammer" => Tactic::BorealHammer,
1363 "Boreal Bow" => Tactic::BorealBow,
1364 "Fire Gigas" => Tactic::FireGigas,
1365 "Ashen Axe" => Tactic::AshenAxe,
1366 "Ashen Staff" => Tactic::AshenStaff,
1367 "Adlet Hunter" => Tactic::AdletHunter,
1368 "Adlet Icepicker" => Tactic::AdletIcepicker,
1369 "Adlet Tracker" => Tactic::AdletTracker,
1370 "Hydra" => Tactic::Hydra,
1371 "Ice Drake" => Tactic::IceDrake,
1372 "Frostfang" => Tactic::RandomAbilities {
1373 primary: 1,
1374 secondary: 3,
1375 abilities: [0; BASE_ABILITY_LIMIT],
1376 },
1377 "Tursus Claws" => Tactic::RandomAbilities {
1378 primary: 2,
1379 secondary: 1,
1380 abilities: [4, 0, 0, 0, 0],
1381 },
1382 "Adlet Elder" => Tactic::AdletElder,
1383 "Haniwa Soldier" => Tactic::HaniwaSoldier,
1384 "Haniwa Guard" => Tactic::HaniwaGuard,
1385 "Haniwa Archer" => Tactic::HaniwaArcher,
1386 "Bloodmoon Bat" => Tactic::BloodmoonBat,
1387 "Vampire Bat" => Tactic::VampireBat,
1388 "Bloodmoon Heiress" => Tactic::BloodmoonHeiress,
1389
1390 _ => Tactic::SimpleMelee,
1391 },
1392 AbilitySpec::Tool(tool_kind) => tool_tactic(*tool_kind),
1393 }
1394 } else if let ItemKind::Tool(tool) = &*item.kind() {
1395 tool_tactic(tool.kind)
1396 } else {
1397 Tactic::SimpleMelee
1398 }
1399 })
1400 .unwrap_or(Tactic::SimpleMelee);
1401
1402 controller.push_action(ControlAction::Wield);
1404
1405 let self_radius = self.body.map_or(0.5, |b| b.max_radius()) * self.scale;
1408 let self_attack_range =
1409 (self.body.map_or(0.5, |b| b.front_radius()) + DEFAULT_ATTACK_RANGE) * self.scale;
1410 let tgt_radius =
1411 tgt_data.body.map_or(0.5, |b| b.max_radius()) * tgt_data.scale.map_or(1.0, |s| s.0);
1412 let min_attack_dist = self_attack_range + tgt_radius;
1413 let body_dist = self_radius + tgt_radius;
1414 let dist_sqrd = self.pos.0.distance_squared(tgt_data.pos.0);
1415 let angle = self
1416 .ori
1417 .look_vec()
1418 .angle_between(tgt_data.pos.0 - self.pos.0)
1419 .to_degrees();
1420 let angle_xy = self
1421 .ori
1422 .look_vec()
1423 .xy()
1424 .angle_between((tgt_data.pos.0 - self.pos.0).xy())
1425 .to_degrees();
1426
1427 let eye_offset = self.body.map_or(0.0, |b| b.eye_height(self.scale));
1428
1429 let tgt_eye_height = tgt_data
1430 .body
1431 .map_or(0.0, |b| b.eye_height(tgt_data.scale.map_or(1.0, |s| s.0)));
1432 let tgt_eye_offset = tgt_eye_height +
1433 if tactic == Tactic::QuadMedJump {
1438 1.0
1439 } else if matches!(tactic, Tactic::QuadLowRanged) {
1440 -1.0
1441 } else {
1442 0.0
1443 };
1444
1445 if let Some(dir) = match self.char_state {
1460 CharacterState::ChargedRanged(c) if dist_sqrd > 0.0 => {
1461 let charge_factor =
1462 c.timer.as_secs_f32() / c.static_data.charge_duration.as_secs_f32();
1463 let projectile_speed = c.static_data.initial_projectile_speed
1464 + charge_factor * c.static_data.scaled_projectile_speed;
1465 aim_projectile(
1466 projectile_speed,
1467 self.pos.0
1468 + self.body.map_or(Vec3::zero(), |body| {
1469 body.projectile_offsets(self.ori.look_vec(), self.scale)
1470 }),
1471 Vec3::new(
1472 tgt_data.pos.0.x,
1473 tgt_data.pos.0.y,
1474 tgt_data.pos.0.z + tgt_eye_offset,
1475 ),
1476 false,
1477 )
1478 },
1479 CharacterState::BasicRanged(c) => {
1480 let offset_z = match c.static_data.projectile.kind {
1481 ProjectileConstructorKind::Explosive { .. }
1483 | ProjectileConstructorKind::ExplosiveHazard { .. }
1484 | ProjectileConstructorKind::Hazard { .. } => 0.0,
1485 _ => tgt_eye_offset,
1486 };
1487 let projectile_speed = c.static_data.projectile_speed;
1488 aim_projectile(
1489 projectile_speed,
1490 self.pos.0
1491 + self.body.map_or(Vec3::zero(), |body| {
1492 body.projectile_offsets(self.ori.look_vec(), self.scale)
1493 }),
1494 Vec3::new(
1495 tgt_data.pos.0.x,
1496 tgt_data.pos.0.y,
1497 tgt_data.pos.0.z + offset_z,
1498 ),
1499 false,
1500 )
1501 .map(|dir| {
1505 if c.static_data.vertical_angle_offset != 0.0 {
1506 let cross_z = vek::Vec3::unit_z().cross(*dir).normalized();
1507 Dir::from_unnormalized(
1508 vek::Quaternion::rotation_3d(c.static_data.vertical_angle_offset, cross_z)
1509 * *dir,
1510 )
1511 .unwrap_or(dir)
1512 } else {
1513 dir
1514 }
1515 })
1516 },
1517 CharacterState::RapidRanged(c) => {
1518 let offset_z = match c.static_data.projectile.kind {
1519 ProjectileConstructorKind::Explosive { .. }
1521 | ProjectileConstructorKind::ExplosiveHazard { .. }
1522 | ProjectileConstructorKind::Hazard { .. } => 0.0,
1523 _ => tgt_eye_offset,
1524 };
1525 let projectile_speed = c.static_data.projectile_speed;
1526 aim_projectile(
1527 projectile_speed,
1528 self.pos.0
1529 + self.body.map_or(Vec3::zero(), |body| {
1530 body.projectile_offsets(self.ori.look_vec(), self.scale)
1531 }),
1532 Vec3::new(
1533 tgt_data.pos.0.x,
1534 tgt_data.pos.0.y,
1535 tgt_data.pos.0.z + offset_z,
1536 ),
1537 false,
1538 )
1539 },
1540 CharacterState::LeapMelee(_)
1541 if matches!(tactic, Tactic::Hammer | Tactic::BorealHammer | Tactic::Axe) =>
1542 {
1543 let direction_weight = match tactic {
1544 Tactic::Hammer | Tactic::BorealHammer => 0.1,
1545 Tactic::Axe => 0.3,
1546 _ => unreachable!("Direction weight called on incorrect tactic."),
1547 };
1548
1549 let tgt_pos = tgt_data.pos.0;
1550 let self_pos = self.pos.0;
1551
1552 let delta_x = (tgt_pos.x - self_pos.x) * direction_weight;
1553 let delta_y = (tgt_pos.y - self_pos.y) * direction_weight;
1554
1555 Dir::from_unnormalized(Vec3::new(delta_x, delta_y, -1.0))
1556 },
1557 CharacterState::BasicBeam(_) => {
1558 let aim_from = self.body.map_or(self.pos.0, |body| {
1559 self.pos.0
1560 + basic_beam::beam_offsets(
1561 body,
1562 controller.inputs.look_dir,
1563 self.ori.look_vec(),
1564 self.vel.0 - self.physics_state.ground_vel,
1566 self.physics_state.on_ground,
1567 )
1568 });
1569 let aim_to = Vec3::new(
1570 tgt_data.pos.0.x,
1571 tgt_data.pos.0.y,
1572 tgt_data.pos.0.z + tgt_eye_offset,
1573 );
1574 Dir::from_unnormalized(aim_to - aim_from)
1575 },
1576 _ => {
1577 let aim_from = Vec3::new(self.pos.0.x, self.pos.0.y, self.pos.0.z + eye_offset);
1578 let aim_to = Vec3::new(
1579 tgt_data.pos.0.x,
1580 tgt_data.pos.0.y,
1581 tgt_data.pos.0.z + tgt_eye_offset,
1582 );
1583 Dir::from_unnormalized(aim_to - aim_from)
1584 },
1585 } {
1586 controller.inputs.look_dir = dir;
1587 }
1588
1589 let attack_data = AttackData {
1590 body_dist,
1591 min_attack_dist,
1592 dist_sqrd,
1593 angle,
1594 angle_xy,
1595 };
1596
1597 match tactic {
1600 Tactic::SimpleFlyingMelee => self.handle_simple_flying_melee(
1601 agent,
1602 controller,
1603 &attack_data,
1604 tgt_data,
1605 read_data,
1606 rng,
1607 ),
1608 Tactic::SimpleMelee => {
1609 self.handle_simple_melee(agent, controller, &attack_data, tgt_data, read_data, rng)
1610 },
1611 Tactic::Axe => {
1612 self.handle_axe_attack(agent, controller, &attack_data, tgt_data, read_data, rng)
1613 },
1614 Tactic::Hammer => {
1615 self.handle_hammer_attack(agent, controller, &attack_data, tgt_data, read_data, rng)
1616 },
1617 Tactic::Sword => {
1618 self.handle_sword_attack(agent, controller, &attack_data, tgt_data, read_data, rng)
1619 },
1620 Tactic::Bow => {
1621 self.handle_bow_attack(agent, controller, &attack_data, tgt_data, read_data, rng)
1622 },
1623 Tactic::Staff => {
1624 self.handle_staff_attack(agent, controller, &attack_data, tgt_data, read_data, rng)
1625 },
1626 Tactic::Sceptre => self.handle_sceptre_attack(
1627 agent,
1628 controller,
1629 &attack_data,
1630 tgt_data,
1631 read_data,
1632 rng,
1633 ),
1634 Tactic::StoneGolem => {
1635 self.handle_stone_golem_attack(agent, controller, &attack_data, tgt_data, read_data)
1636 },
1637 Tactic::IronGolem => {
1638 self.handle_iron_golem_attack(agent, controller, &attack_data, tgt_data, read_data)
1639 },
1640 Tactic::CircleCharge {
1641 radius,
1642 circle_time,
1643 } => self.handle_circle_charge_attack(
1644 agent,
1645 controller,
1646 &attack_data,
1647 tgt_data,
1648 read_data,
1649 radius,
1650 circle_time,
1651 rng,
1652 ),
1653 Tactic::QuadLowRanged => self.handle_quadlow_ranged_attack(
1654 agent,
1655 controller,
1656 &attack_data,
1657 tgt_data,
1658 read_data,
1659 ),
1660 Tactic::TailSlap => {
1661 self.handle_tail_slap_attack(agent, controller, &attack_data, tgt_data, read_data)
1662 },
1663 Tactic::QuadLowQuick => self.handle_quadlow_quick_attack(
1664 agent,
1665 controller,
1666 &attack_data,
1667 tgt_data,
1668 read_data,
1669 ),
1670 Tactic::QuadLowBasic => self.handle_quadlow_basic_attack(
1671 agent,
1672 controller,
1673 &attack_data,
1674 tgt_data,
1675 read_data,
1676 ),
1677 Tactic::QuadMedJump => self.handle_quadmed_jump_attack(
1678 agent,
1679 controller,
1680 &attack_data,
1681 tgt_data,
1682 read_data,
1683 ),
1684 Tactic::QuadMedBasic => self.handle_quadmed_basic_attack(
1685 agent,
1686 controller,
1687 &attack_data,
1688 tgt_data,
1689 read_data,
1690 ),
1691 Tactic::QuadMedHoof => self.handle_quadmed_hoof_attack(
1692 agent,
1693 controller,
1694 &attack_data,
1695 tgt_data,
1696 read_data,
1697 ),
1698 Tactic::QuadLowBeam => self.handle_quadlow_beam_attack(
1699 agent,
1700 controller,
1701 &attack_data,
1702 tgt_data,
1703 read_data,
1704 ),
1705 Tactic::Elephant => self.handle_elephant_attack(
1706 agent,
1707 controller,
1708 &attack_data,
1709 tgt_data,
1710 read_data,
1711 rng,
1712 ),
1713 Tactic::Rocksnapper => {
1714 self.handle_rocksnapper_attack(agent, controller, &attack_data, tgt_data, read_data)
1715 },
1716 Tactic::Roshwalr => {
1717 self.handle_roshwalr_attack(agent, controller, &attack_data, tgt_data, read_data)
1718 },
1719 Tactic::OrganAura => {
1720 self.handle_organ_aura_attack(agent, controller, &attack_data, tgt_data, read_data)
1721 },
1722 Tactic::Theropod => {
1723 self.handle_theropod_attack(agent, controller, &attack_data, tgt_data, read_data)
1724 },
1725 Tactic::ArthropodMelee => self.handle_arthropod_melee_attack(
1726 agent,
1727 controller,
1728 &attack_data,
1729 tgt_data,
1730 read_data,
1731 ),
1732 Tactic::ArthropodAmbush => self.handle_arthropod_ambush_attack(
1733 agent,
1734 controller,
1735 &attack_data,
1736 tgt_data,
1737 read_data,
1738 rng,
1739 ),
1740 Tactic::ArthropodRanged => self.handle_arthropod_ranged_attack(
1741 agent,
1742 controller,
1743 &attack_data,
1744 tgt_data,
1745 read_data,
1746 ),
1747 Tactic::Turret => {
1748 self.handle_turret_attack(agent, controller, &attack_data, tgt_data, read_data)
1749 },
1750 Tactic::FixedTurret => self.handle_fixed_turret_attack(
1751 agent,
1752 controller,
1753 &attack_data,
1754 tgt_data,
1755 read_data,
1756 ),
1757 Tactic::RotatingTurret => {
1758 self.handle_rotating_turret_attack(agent, controller, tgt_data, read_data)
1759 },
1760 Tactic::Mindflayer => self.handle_mindflayer_attack(
1761 agent,
1762 controller,
1763 &attack_data,
1764 tgt_data,
1765 read_data,
1766 rng,
1767 ),
1768 Tactic::Flamekeeper => {
1769 self.handle_flamekeeper_attack(agent, controller, &attack_data, tgt_data, read_data)
1770 },
1771 Tactic::Forgemaster => {
1772 self.handle_forgemaster_attack(agent, controller, &attack_data, tgt_data, read_data)
1773 },
1774 Tactic::BirdLargeFire => self.handle_birdlarge_fire_attack(
1775 agent,
1776 controller,
1777 &attack_data,
1778 tgt_data,
1779 read_data,
1780 rng,
1781 ),
1782 Tactic::BirdLargeBreathe => self.handle_birdlarge_breathe_attack(
1784 agent,
1785 controller,
1786 &attack_data,
1787 tgt_data,
1788 read_data,
1789 rng,
1790 ),
1791 Tactic::BirdLargeBasic => self.handle_birdlarge_basic_attack(
1792 agent,
1793 controller,
1794 &attack_data,
1795 tgt_data,
1796 read_data,
1797 ),
1798 Tactic::Wyvern => {
1799 self.handle_wyvern_attack(agent, controller, &attack_data, tgt_data, read_data, rng)
1800 },
1801 Tactic::BirdMediumBasic => {
1802 self.handle_simple_melee(agent, controller, &attack_data, tgt_data, read_data, rng)
1803 },
1804 Tactic::SimpleDouble => self.handle_simple_double_attack(
1805 agent,
1806 controller,
1807 &attack_data,
1808 tgt_data,
1809 read_data,
1810 ),
1811 Tactic::Jiangshi => {
1812 self.handle_jiangshi_attack(agent, controller, &attack_data, tgt_data, read_data)
1813 },
1814 Tactic::ClayGolem => {
1815 self.handle_clay_golem_attack(agent, controller, &attack_data, tgt_data, read_data)
1816 },
1817 Tactic::ClaySteed => {
1818 self.handle_clay_steed_attack(agent, controller, &attack_data, tgt_data, read_data)
1819 },
1820 Tactic::AncientEffigy => self.handle_ancient_effigy_attack(
1821 agent,
1822 controller,
1823 &attack_data,
1824 tgt_data,
1825 read_data,
1826 ),
1827 Tactic::TerracottaStatue => {
1828 self.handle_terracotta_statue_attack(agent, controller, &attack_data, read_data)
1829 },
1830 Tactic::Minotaur => {
1831 self.handle_minotaur_attack(agent, controller, &attack_data, tgt_data, read_data)
1832 },
1833 Tactic::Cyclops => {
1834 self.handle_cyclops_attack(agent, controller, &attack_data, tgt_data, read_data)
1835 },
1836 Tactic::Dullahan => {
1837 self.handle_dullahan_attack(agent, controller, &attack_data, tgt_data, read_data)
1838 },
1839 Tactic::GraveWarden => self.handle_grave_warden_attack(
1840 agent,
1841 controller,
1842 &attack_data,
1843 tgt_data,
1844 read_data,
1845 ),
1846 Tactic::TidalWarrior => self.handle_tidal_warrior_attack(
1847 agent,
1848 controller,
1849 &attack_data,
1850 tgt_data,
1851 read_data,
1852 ),
1853 Tactic::Karkatha => self.handle_karkatha_attack(
1854 agent,
1855 controller,
1856 &attack_data,
1857 tgt_data,
1858 read_data,
1859 rng,
1860 ),
1861 Tactic::RadialTurret => self.handle_radial_turret_attack(controller),
1862 Tactic::FieryTornado => self.handle_fiery_tornado_attack(agent, controller),
1863 Tactic::Yeti => {
1864 self.handle_yeti_attack(agent, controller, &attack_data, tgt_data, read_data)
1865 },
1866 Tactic::Harvester => self.handle_harvester_attack(
1867 agent,
1868 controller,
1869 &attack_data,
1870 tgt_data,
1871 read_data,
1872 rng,
1873 ),
1874 Tactic::Cardinal => self.handle_cardinal_attack(
1875 agent,
1876 controller,
1877 &attack_data,
1878 tgt_data,
1879 read_data,
1880 rng,
1881 ),
1882 Tactic::SeaBishop => self.handle_sea_bishop_attack(
1883 agent,
1884 controller,
1885 &attack_data,
1886 tgt_data,
1887 read_data,
1888 rng,
1889 ),
1890 Tactic::Cursekeeper => self.handle_cursekeeper_attack(
1891 agent,
1892 controller,
1893 &attack_data,
1894 tgt_data,
1895 read_data,
1896 rng,
1897 ),
1898 Tactic::CursekeeperFake => {
1899 self.handle_cursekeeper_fake_attack(controller, &attack_data)
1900 },
1901 Tactic::ShamanicSpirit => self.handle_shamanic_spirit_attack(
1902 agent,
1903 controller,
1904 &attack_data,
1905 tgt_data,
1906 read_data,
1907 ),
1908 Tactic::Dagon => {
1909 self.handle_dagon_attack(agent, controller, &attack_data, tgt_data, read_data)
1910 },
1911 Tactic::Snaretongue => {
1912 self.handle_snaretongue_attack(agent, controller, &attack_data, read_data)
1913 },
1914 Tactic::SimpleBackstab => {
1915 self.handle_simple_backstab(agent, controller, &attack_data, tgt_data, read_data)
1916 },
1917 Tactic::ElevatedRanged => {
1918 self.handle_elevated_ranged(agent, controller, &attack_data, tgt_data, read_data)
1919 },
1920 Tactic::Deadwood => {
1921 self.handle_deadwood(agent, controller, &attack_data, tgt_data, read_data)
1922 },
1923 Tactic::Mandragora => {
1924 self.handle_mandragora(agent, controller, &attack_data, tgt_data, read_data)
1925 },
1926 Tactic::WoodGolem => {
1927 self.handle_wood_golem(agent, controller, &attack_data, tgt_data, read_data, rng)
1928 },
1929 Tactic::GnarlingChieftain => self.handle_gnarling_chieftain(
1930 agent,
1931 controller,
1932 &attack_data,
1933 tgt_data,
1934 read_data,
1935 rng,
1936 ),
1937 Tactic::FrostGigas => self.handle_frostgigas_attack(
1938 agent,
1939 controller,
1940 &attack_data,
1941 tgt_data,
1942 read_data,
1943 rng,
1944 ),
1945 Tactic::BorealHammer => self.handle_boreal_hammer_attack(
1946 agent,
1947 controller,
1948 &attack_data,
1949 tgt_data,
1950 read_data,
1951 rng,
1952 ),
1953 Tactic::BorealBow => self.handle_boreal_bow_attack(
1954 agent,
1955 controller,
1956 &attack_data,
1957 tgt_data,
1958 read_data,
1959 rng,
1960 ),
1961 Tactic::FireGigas => self.handle_firegigas_attack(
1962 agent,
1963 controller,
1964 &attack_data,
1965 tgt_data,
1966 read_data,
1967 rng,
1968 ),
1969 Tactic::AshenAxe => self.handle_ashen_axe_attack(
1970 agent,
1971 controller,
1972 &attack_data,
1973 tgt_data,
1974 read_data,
1975 rng,
1976 ),
1977 Tactic::AshenStaff => self.handle_ashen_staff_attack(
1978 agent,
1979 controller,
1980 &attack_data,
1981 tgt_data,
1982 read_data,
1983 rng,
1984 ),
1985 Tactic::SwordSimple => self.handle_sword_simple_attack(
1986 agent,
1987 controller,
1988 &attack_data,
1989 tgt_data,
1990 read_data,
1991 ),
1992 Tactic::AdletHunter => {
1993 self.handle_adlet_hunter(agent, controller, &attack_data, tgt_data, read_data, rng)
1994 },
1995 Tactic::AdletIcepicker => {
1996 self.handle_adlet_icepicker(agent, controller, &attack_data, tgt_data, read_data)
1997 },
1998 Tactic::AdletTracker => {
1999 self.handle_adlet_tracker(agent, controller, &attack_data, tgt_data, read_data)
2000 },
2001 Tactic::IceDrake => {
2002 self.handle_icedrake(agent, controller, &attack_data, tgt_data, read_data, rng)
2003 },
2004 Tactic::Hydra => {
2005 self.handle_hydra(agent, controller, &attack_data, tgt_data, read_data, rng)
2006 },
2007 Tactic::BloodmoonBat => self.handle_bloodmoon_bat_attack(
2008 agent,
2009 controller,
2010 &attack_data,
2011 tgt_data,
2012 read_data,
2013 rng,
2014 ),
2015 Tactic::VampireBat => self.handle_vampire_bat_attack(
2016 agent,
2017 controller,
2018 &attack_data,
2019 tgt_data,
2020 read_data,
2021 rng,
2022 ),
2023 Tactic::BloodmoonHeiress => self.handle_bloodmoon_heiress_attack(
2024 agent,
2025 controller,
2026 &attack_data,
2027 tgt_data,
2028 read_data,
2029 rng,
2030 ),
2031 Tactic::RandomAbilities {
2032 primary,
2033 secondary,
2034 abilities,
2035 } => self.handle_random_abilities(
2036 agent,
2037 controller,
2038 &attack_data,
2039 tgt_data,
2040 read_data,
2041 rng,
2042 primary,
2043 secondary,
2044 abilities,
2045 ),
2046 Tactic::AdletElder => {
2047 self.handle_adlet_elder(agent, controller, &attack_data, tgt_data, read_data, rng)
2048 },
2049 Tactic::HaniwaSoldier => {
2050 self.handle_haniwa_soldier(agent, controller, &attack_data, tgt_data, read_data)
2051 },
2052 Tactic::HaniwaGuard => {
2053 self.handle_haniwa_guard(agent, controller, &attack_data, tgt_data, read_data, rng)
2054 },
2055 Tactic::HaniwaArcher => {
2056 self.handle_haniwa_archer(agent, controller, &attack_data, tgt_data, read_data)
2057 },
2058 }
2059 }
2060
2061 pub fn handle_sounds_heard(
2062 &self,
2063 agent: &mut Agent,
2064 controller: &mut Controller,
2065 read_data: &ReadData,
2066 emitters: &mut AgentEmitters,
2067 rng: &mut impl RngExt,
2068 ) {
2069 agent.forget_old_sounds(read_data.time.0);
2070
2071 if is_invulnerable(*self.entity, read_data) || is_steering(*self.entity, read_data) {
2072 self.idle(agent, controller, read_data, emitters, rng);
2073 return;
2074 }
2075
2076 if let Some(sound) = agent.sounds_heard.last() {
2077 let sound_pos = Pos(sound.pos);
2078 let dist_sqrd = self.pos.0.distance_squared(sound_pos.0);
2079 let is_close = dist_sqrd < 35.0_f32.powi(2);
2084
2085 let sound_was_loud = sound.vol >= 10.0;
2086 let sound_was_threatening = sound_was_loud
2087 || matches!(sound.kind, SoundKind::Utterance(UtteranceKind::Scream, _));
2088
2089 let has_enemy_alignment = matches!(self.alignment, Some(Alignment::Enemy));
2090 let follows_threatening_sounds =
2091 has_enemy_alignment || is_village_guard(*self.entity, read_data);
2092
2093 if sound_was_threatening && is_close {
2094 if !self.below_flee_health(agent) && follows_threatening_sounds {
2095 self.follow(agent, controller, read_data, &sound_pos);
2096 } else if self.below_flee_health(agent) || !follows_threatening_sounds {
2097 self.flee(agent, controller, read_data, &sound_pos);
2098 } else {
2099 self.idle(agent, controller, read_data, emitters, rng);
2100 }
2101 } else {
2102 self.idle(agent, controller, read_data, emitters, rng);
2103 }
2104 } else {
2105 self.idle(agent, controller, read_data, emitters, rng);
2106 }
2107 }
2108
2109 pub fn attack_target_attacker(
2110 &self,
2111 agent: &mut Agent,
2112 read_data: &ReadData,
2113 controller: &mut Controller,
2114 emitters: &mut AgentEmitters,
2115 rng: &mut impl RngExt,
2116 ) {
2117 if let Some(Target { target, .. }) = agent.target
2118 && let Some(tgt_health) = read_data.healths.get(target)
2119 && let Some(by) = tgt_health.last_change.damage_by()
2120 && let Some(attacker) = get_entity_by_id(by.uid(), read_data)
2121 {
2122 if agent.target.is_none() {
2123 controller.push_utterance(UtteranceKind::Angry);
2124 }
2125
2126 let attacker_pos = read_data.positions.get(attacker).map(|pos| pos.0);
2127 agent.target = Some(Target::new(
2128 attacker,
2129 true,
2130 read_data.time.0,
2131 true,
2132 attacker_pos,
2133 ));
2134
2135 if let Some(tgt_pos) = read_data.positions.get(attacker) {
2136 if is_dead_or_invulnerable(attacker, read_data) {
2137 agent.target = Some(Target::new(
2138 target,
2139 false,
2140 read_data.time.0,
2141 false,
2142 Some(tgt_pos.0),
2143 ));
2144
2145 self.idle(agent, controller, read_data, emitters, rng);
2146 } else {
2147 let target_data = TargetData::new(tgt_pos, target, read_data);
2148 self.attack(agent, controller, &target_data, read_data, rng);
2155 }
2156 }
2157 }
2158 }
2159
2160 pub fn chat_npc_if_allowed_to_speak(
2163 &self,
2164 msg: Content,
2165 agent: &Agent,
2166 emitters: &mut AgentEmitters,
2167 ) -> bool {
2168 if agent.allowed_to_speak() {
2169 self.chat_npc(msg, emitters);
2170 true
2171 } else {
2172 false
2173 }
2174 }
2175
2176 pub fn chat_npc(&self, content: Content, emitters: &mut AgentEmitters) {
2177 emitters.emit(ChatEvent {
2178 msg: UnresolvedChatMsg::npc(*self.uid, content),
2179 from_client: false,
2180 });
2181 }
2182
2183 fn emit_scream(&self, time: f64, emitters: &mut AgentEmitters) {
2184 if let Some(body) = self.body {
2185 emitters.emit(SoundEvent {
2186 sound: Sound::new(
2187 SoundKind::Utterance(UtteranceKind::Scream, *body),
2188 self.pos.0,
2189 13.0,
2190 time,
2191 ),
2192 });
2193 }
2194 }
2195
2196 pub fn cry_out(&self, agent: &Agent, emitters: &mut AgentEmitters, read_data: &ReadData) {
2197 let has_enemy_alignment = matches!(self.alignment, Some(Alignment::Enemy));
2198 let is_below_flee_health = self.below_flee_health(agent);
2199
2200 if has_enemy_alignment && is_below_flee_health {
2201 self.chat_npc_if_allowed_to_speak(
2202 Content::localized("npc-speech-cultist_low_health_fleeing"),
2203 agent,
2204 emitters,
2205 );
2206 } else if is_villager(self.alignment) {
2207 self.chat_npc_if_allowed_to_speak(
2208 Content::localized("npc-speech-villager_under_attack"),
2209 agent,
2210 emitters,
2211 );
2212 self.emit_scream(read_data.time.0, emitters);
2213 }
2214 }
2215
2216 pub fn exclaim_relief_about_enemy_dead(&self, agent: &Agent, emitters: &mut AgentEmitters) {
2217 if is_villager(self.alignment) {
2218 self.chat_npc_if_allowed_to_speak(
2219 Content::localized("npc-speech-villager_enemy_killed"),
2220 agent,
2221 emitters,
2222 );
2223 }
2224 }
2225
2226 pub fn below_flee_health(&self, agent: &Agent) -> bool {
2227 self.damage.min(1.0) < agent.psyche.flee_health
2228 }
2229
2230 pub fn is_more_dangerous_than_target(
2231 &self,
2232 entity: EcsEntity,
2233 target: Target,
2234 read_data: &ReadData,
2235 ) -> bool {
2236 let entity_pos = read_data.positions.get(entity);
2237 let target_pos = read_data.positions.get(target.target);
2238
2239 entity_pos.is_some_and(|entity_pos| {
2240 target_pos.is_none_or(|target_pos| {
2241 const FUZZY_DIST_COMPARISON: f32 = 0.8;
2246
2247 let is_target_further = target_pos.0.distance(entity_pos.0)
2248 < target_pos.0.distance(entity_pos.0) * FUZZY_DIST_COMPARISON;
2249 let is_entity_hostile = read_data
2250 .alignments
2251 .get(entity)
2252 .zip(self.alignment)
2253 .is_some_and(|(entity, me)| me.hostile_towards(*entity));
2254
2255 !target.aggro_on || (is_target_further && is_entity_hostile)
2258 })
2259 })
2260 }
2261
2262 pub fn is_enemy(&self, entity: EcsEntity, read_data: &ReadData) -> bool {
2263 let other_alignment = read_data.alignments.get(entity);
2264
2265 (entity != *self.entity)
2266 && !self.passive_towards(entity, read_data)
2267 && (are_our_owners_hostile(self.alignment, other_alignment, read_data)
2268 || (is_villager(self.alignment) && is_dressed_as_cultist(entity, read_data)
2269 || (is_villager(self.alignment) && is_dressed_as_witch(entity, read_data))
2270 || (is_villager(self.alignment) && is_dressed_as_pirate(entity, read_data))))
2271 }
2272
2273 pub fn is_hunting_animal(&self, entity: EcsEntity, read_data: &ReadData) -> bool {
2274 (entity != *self.entity)
2275 && !self.friendly_towards(entity, read_data)
2276 && matches!(read_data.bodies.get(entity), Some(Body::QuadrupedSmall(_)))
2277 }
2278
2279 fn should_defend(&self, entity: EcsEntity, read_data: &ReadData) -> bool {
2280 let entity_alignment = read_data.alignments.get(entity);
2281
2282 let we_are_friendly = entity_alignment.is_some_and(|entity_alignment| {
2283 self.alignment
2284 .is_some_and(|alignment| !alignment.hostile_towards(*entity_alignment))
2285 });
2286 let we_share_species = read_data.bodies.get(entity).is_some_and(|entity_body| {
2287 self.body.is_some_and(|body| {
2288 entity_body.is_same_species_as(body)
2289 || (entity_body.is_humanoid() && body.is_humanoid())
2290 })
2291 });
2292 let self_owns_entity =
2293 matches!(entity_alignment, Some(Alignment::Owned(ouid)) if *self.uid == *ouid);
2294
2295 (we_are_friendly && we_share_species)
2296 || (is_village_guard(*self.entity, read_data) && is_villager(entity_alignment))
2297 || self_owns_entity
2298 }
2299
2300 fn passive_towards(&self, entity: EcsEntity, read_data: &ReadData) -> bool {
2301 if let (Some(self_alignment), Some(other_alignment)) =
2302 (self.alignment, read_data.alignments.get(entity))
2303 {
2304 self_alignment.passive_towards(*other_alignment)
2305 } else {
2306 false
2307 }
2308 }
2309
2310 fn friendly_towards(&self, entity: EcsEntity, read_data: &ReadData) -> bool {
2311 if let (Some(self_alignment), Some(other_alignment)) =
2312 (self.alignment, read_data.alignments.get(entity))
2313 {
2314 self_alignment.friendly_towards(*other_alignment)
2315 } else {
2316 false
2317 }
2318 }
2319
2320 pub fn can_see_entity(
2321 &self,
2322 agent: &Agent,
2323 controller: &Controller,
2324 other: EcsEntity,
2325 other_pos: &Pos,
2326 other_scale: Option<&Scale>,
2327 read_data: &ReadData,
2328 ) -> bool {
2329 let other_stealth_multiplier = {
2330 let other_inventory = read_data.inventories.get(other);
2331 let other_char_state = read_data.char_states.get(other);
2332
2333 perception_dist_multiplier_from_stealth(other_inventory, other_char_state, self.msm)
2334 };
2335
2336 let within_sight_dist = {
2337 let sight_dist = agent.psyche.sight_dist * other_stealth_multiplier;
2338 let dist_sqrd = other_pos.0.distance_squared(self.pos.0);
2339
2340 dist_sqrd < sight_dist.powi(2)
2341 };
2342
2343 let within_fov = (other_pos.0 - self.pos.0)
2344 .try_normalized()
2345 .is_some_and(|v| v.dot(*controller.inputs.look_dir) > 0.15);
2346
2347 let other_body = read_data.bodies.get(other);
2348
2349 (within_sight_dist)
2350 && within_fov
2351 && entities_have_line_of_sight(
2352 self.pos,
2353 self.body,
2354 self.scale,
2355 other_pos,
2356 other_body,
2357 other_scale,
2358 read_data,
2359 )
2360 }
2361
2362 pub fn detects_other(
2363 &self,
2364 agent: &Agent,
2365 controller: &Controller,
2366 other: &EcsEntity,
2367 other_pos: &Pos,
2368 other_scale: Option<&Scale>,
2369 read_data: &ReadData,
2370 ) -> bool {
2371 self.can_sense_directly_near(other_pos)
2372 || self.can_see_entity(agent, controller, *other, other_pos, other_scale, read_data)
2373 }
2374
2375 pub fn can_sense_directly_near(&self, e_pos: &Pos) -> bool {
2376 let chance = rng().random_bool(0.3);
2377 e_pos.0.distance_squared(self.pos.0) < 5_f32.powi(2) && chance
2378 }
2379
2380 pub fn menacing(
2381 &self,
2382 agent: &mut Agent,
2383 controller: &mut Controller,
2384 target: EcsEntity,
2385 tgt_data: &TargetData,
2386 read_data: &ReadData,
2387 emitters: &mut AgentEmitters,
2388 remembers_fight_with_target: bool,
2389 ) {
2390 let max_move = 0.5;
2391 let move_dir = controller.inputs.move_dir;
2392 let move_dir_mag = move_dir.magnitude();
2393 let mut chat = |agent: &mut Agent, content: Content| {
2394 self.chat_npc_if_allowed_to_speak(content, agent, emitters);
2395 };
2396 let mut chat_villager_remembers_fighting = |agent: &mut Agent| {
2397 let tgt_name = read_data.stats.get(target).map(|stats| stats.name.clone());
2398
2399 if let Some(tgt_name) = tgt_name.as_ref().and_then(|name| name.as_plain()) {
2402 chat(
2403 agent,
2404 Content::localized_with_args("npc-speech-remembers-fight", [(
2405 "name", tgt_name,
2406 )]),
2407 )
2408 } else {
2409 chat(
2410 agent,
2411 Content::localized("npc-speech-remembers-fight-no-name"),
2412 );
2413 }
2414 };
2415
2416 self.look_toward(controller, read_data, target);
2417 controller.push_action(ControlAction::Wield);
2418
2419 if move_dir_mag > max_move {
2420 controller.inputs.move_dir = max_move * move_dir / move_dir_mag;
2421 }
2422
2423 match agent
2424 .timer
2425 .timeout_elapsed(read_data.time.0, comp::agent::TimerAction::Warn, 5.0)
2426 {
2427 Some(true) | None => {
2428 self.path_toward_target(
2429 agent,
2430 controller,
2431 tgt_data.pos.0,
2432 read_data,
2433 Path::AtTarget,
2434 Some(0.4),
2435 );
2436 },
2437 Some(false) => {
2438 agent
2439 .timer
2440 .start(read_data.time.0, comp::agent::TimerAction::Warn);
2441 controller.push_utterance(UtteranceKind::Angry);
2442 if is_villager(self.alignment) {
2443 if remembers_fight_with_target {
2444 chat_villager_remembers_fighting(agent);
2445 } else if is_dressed_as_cultist(target, read_data) {
2446 chat(
2447 agent,
2448 Content::localized("npc-speech-villager_cultist_alarm"),
2449 );
2450 } else if is_dressed_as_witch(target, read_data) {
2451 chat(agent, Content::localized("npc-speech-villager_witch_alarm"));
2452 } else if is_dressed_as_pirate(target, read_data) {
2453 chat(
2454 agent,
2455 Content::localized("npc-speech-villager_pirate_alarm"),
2456 );
2457 } else {
2458 chat(agent, Content::localized("npc-speech-menacing"));
2459 }
2460 } else {
2461 chat(agent, Content::localized("npc-speech-menacing"));
2462 }
2463 },
2464 }
2465 }
2466
2467 pub fn dismount_uncontrollable(&self, controller: &mut Controller, read_data: &ReadData) {
2469 if read_data.is_riders.get(*self.entity).is_some_and(|mount| {
2470 read_data
2471 .id_maps
2472 .uid_entity(mount.mount)
2473 .and_then(|e| read_data.bodies.get(e))
2474 .is_none_or(|b| b.has_free_will())
2475 }) || read_data
2476 .is_volume_riders
2477 .get(*self.entity)
2478 .is_some_and(|r| !r.is_steering_entity())
2479 {
2480 controller.push_event(ControlEvent::Unmount);
2481 }
2482 }
2483
2484 pub fn dismount(&self, controller: &mut Controller, read_data: &ReadData) {
2489 if read_data.is_riders.contains(*self.entity)
2490 || read_data
2491 .is_volume_riders
2492 .get(*self.entity)
2493 .is_some_and(|r| !r.is_steering_entity())
2494 {
2495 controller.push_event(ControlEvent::Unmount);
2496 }
2497 }
2498}