1use crate::{
36 audio::{Cue, Surface},
37 camera::{CameraShake, ShakeImpulse, ShakeSample},
38};
39use ferravex_math::Vec3;
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub enum FeedbackError {
42 Invalid,
43 Capacity,
44}
45#[derive(Clone, Copy, Debug, PartialEq, Eq)]
46pub enum FeedbackKind {
47 Fire,
48 Hit,
49 Hurt,
50 Land,
51 Dash,
52 Pickup,
53 Explosion,
54}
55#[derive(Clone, Copy, Debug)]
56pub struct FeedbackEvent {
57 pub kind: FeedbackKind,
58 pub intensity: f32,
59 pub position: Vec3,
60}
61impl FeedbackEvent {
62 pub fn new(kind: FeedbackKind, position: Vec3, intensity: f32) -> Result<Self, FeedbackError> {
74 let event = Self {
75 kind,
76 intensity,
77 position,
78 };
79 if !event.valid() {
80 return Err(FeedbackError::Invalid);
81 }
82 Ok(event)
83 }
84
85 pub fn fire(position: Vec3, intensity: f32) -> Result<Self, FeedbackError> {
87 Self::new(FeedbackKind::Fire, position, intensity)
88 }
89
90 pub fn hit(position: Vec3, intensity: f32) -> Result<Self, FeedbackError> {
92 Self::new(FeedbackKind::Hit, position, intensity)
93 }
94
95 pub fn hurt(position: Vec3, intensity: f32) -> Result<Self, FeedbackError> {
97 Self::new(FeedbackKind::Hurt, position, intensity)
98 }
99
100 pub fn land(position: Vec3, intensity: f32) -> Result<Self, FeedbackError> {
102 Self::new(FeedbackKind::Land, position, intensity)
103 }
104
105 pub fn dash(position: Vec3, intensity: f32) -> Result<Self, FeedbackError> {
107 Self::new(FeedbackKind::Dash, position, intensity)
108 }
109
110 pub fn pickup(position: Vec3, intensity: f32) -> Result<Self, FeedbackError> {
112 Self::new(FeedbackKind::Pickup, position, intensity)
113 }
114
115 pub fn explosion(position: Vec3, intensity: f32) -> Result<Self, FeedbackError> {
117 Self::new(FeedbackKind::Explosion, position, intensity)
118 }
119
120 fn valid(self) -> bool {
121 self.intensity.is_finite()
122 && (0. ..=4.).contains(&self.intensity)
123 && self.position.is_finite()
124 }
125}
126
127#[derive(Clone, Copy, Debug, PartialEq)]
146pub struct FeedbackPolicy {
147 reduced_motion: bool,
148 reduced_flash: bool,
149 intensity: f32,
150}
151impl Default for FeedbackPolicy {
152 fn default() -> Self {
153 Self {
154 reduced_motion: false,
155 reduced_flash: false,
156 intensity: 1.,
157 }
158 }
159}
160impl FeedbackPolicy {
161 pub fn new(
163 reduced_motion: bool,
164 reduced_flash: bool,
165 intensity: f32,
166 ) -> Result<Self, FeedbackError> {
167 if !intensity.is_finite() || !(0. ..=1.).contains(&intensity) {
168 return Err(FeedbackError::Invalid);
169 }
170 Ok(Self {
171 reduced_motion,
172 reduced_flash,
173 intensity,
174 })
175 }
176
177 pub fn reduced_motion(self) -> bool {
178 self.reduced_motion
179 }
180
181 pub fn reduced_flash(self) -> bool {
182 self.reduced_flash
183 }
184
185 pub fn intensity(self) -> f32 {
186 self.intensity
187 }
188
189 fn motion_scale(self) -> f32 {
190 if self.reduced_motion {
191 0.
192 } else {
193 self.intensity
194 }
195 }
196
197 fn flash_scale(self) -> f32 {
198 if self.reduced_flash {
199 0.
200 } else {
201 self.intensity
202 }
203 }
204}
205
206#[derive(Clone, Copy, Debug)]
207pub struct FeedbackConfig {
208 pub motion: f32,
209 pub flash: f32,
210 pub rumble: f32,
211 pub translation_scale: f32,
212 pub single_player_hitstop: bool,
213 pub networked: bool,
214}
215impl Default for FeedbackConfig {
216 fn default() -> Self {
217 Self {
218 motion: 1.,
219 flash: 1.,
220 rumble: 1.,
221 translation_scale: 0.03,
222 single_player_hitstop: false,
223 networked: false,
224 }
225 }
226}
227impl FeedbackConfig {
228 fn valid(self) -> bool {
229 [self.motion, self.flash, self.rumble]
230 .iter()
231 .all(|v| v.is_finite() && (0. ..=1.).contains(v))
232 && self.translation_scale.is_finite()
233 && (0. ..=10.).contains(&self.translation_scale)
234 }
235}
236#[derive(Clone, Copy, Debug, PartialEq, Eq)]
237pub enum Reaction {
238 Recoil,
239 Impact,
240 Stagger,
241 Land,
242 Dash,
243 Collect,
244}
245#[derive(Clone, Copy, Debug)]
246pub struct FeedbackResponse {
247 pub cue: Cue,
248 pub gain: f32,
249 pub particles: u8,
251 pub reaction: Reaction,
254 pub position: Vec3,
255}
256#[derive(Clone, Copy, Debug, Default)]
257pub struct FeedbackFrame {
258 pub shake: ShakeSample,
259 pub flash: f32,
260 pub rumble: [f32; 2],
261 pub hitstop_seconds: f32,
264}
265
266#[derive(Clone, Copy, Debug, Default, PartialEq)]
278pub struct ImpactSample {
279 pub emphasis: f32,
284 pub flash: f32,
288}
289
290fn decay_impact(level: f32, rate: f32, dt: f32) -> f32 {
291 let next = level * (-rate * dt).exp();
292 if dt > 0. && next < 0.0001 { 0. } else { next }
293}
294
295pub struct FeedbackController<const N: usize> {
300 config: FeedbackConfig,
301 policy: FeedbackPolicy,
302 seen: [u64; N],
303 next: usize,
304 pending: usize,
305 shake: CameraShake,
306 emphasis: f32,
307 flash: f32,
308 rumble: [f32; 2],
309 hitstop: f32,
310 pub dropped: u64,
311}
312impl<const N: usize> FeedbackController<N> {
313 pub fn new(config: FeedbackConfig, seed: u64) -> Result<Self, FeedbackError> {
314 if N == 0 || N > 128 {
315 return Err(FeedbackError::Capacity);
316 }
317 if !config.valid() {
318 return Err(FeedbackError::Invalid);
319 }
320 Ok(Self {
321 config,
322 policy: FeedbackPolicy::default(),
323 seen: [0; N],
324 next: 0,
325 pending: 0,
326 shake: CameraShake::new(seed),
327 emphasis: 0.,
328 flash: 0.,
329 rumble: [0.; 2],
330 hitstop: 0.,
331 dropped: 0,
332 })
333 }
334
335 pub fn policy(&self) -> FeedbackPolicy {
337 self.policy
338 }
339
340 pub fn set_policy(&mut self, policy: FeedbackPolicy) {
352 self.policy = policy;
353 if policy.motion_scale() == 0. {
354 self.shake.clear();
355 self.emphasis = 0.;
356 self.hitstop = 0.;
357 }
358 if policy.flash_scale() == 0. {
359 self.flash = 0.;
360 }
361 }
362
363 pub fn configure(&mut self, config: FeedbackConfig) -> Result<(), FeedbackError> {
368 if !config.valid() {
369 return Err(FeedbackError::Invalid);
370 }
371 self.config = config;
372 if config.motion == 0. {
373 self.shake.clear();
374 self.hitstop = 0.;
375 }
376 if config.networked || !config.single_player_hitstop {
377 self.hitstop = 0.;
378 }
379 self.emphasis = self.emphasis.min(config.motion);
380 self.flash = self.flash.min(config.flash);
381 for r in &mut self.rumble {
382 *r = (*r).min(config.rumble);
383 }
384 Ok(())
385 }
386 pub fn clear(&mut self) {
391 self.seen.fill(0);
392 self.next = 0;
393 self.pending = 0;
394 self.shake.clear();
395 self.emphasis = 0.;
396 self.flash = 0.;
397 self.rumble = [0.; 2];
398 self.hitstop = 0.;
399 }
400
401 pub fn emit_if_accepted(
416 &mut self,
417 id: u64,
418 accepted: bool,
419 event: FeedbackEvent,
420 ) -> Result<Option<FeedbackResponse>, FeedbackError> {
421 if !accepted {
422 return Ok(None);
423 }
424 self.emit(id, event)
425 }
426
427 pub fn emit(
430 &mut self,
431 id: u64,
432 event: FeedbackEvent,
433 ) -> Result<Option<FeedbackResponse>, FeedbackError> {
434 if id == 0 || !event.valid() {
435 return Err(FeedbackError::Invalid);
436 }
437 if self.seen.contains(&id) {
438 return Ok(None);
439 }
440 if self.pending == N {
441 self.dropped = self.dropped.saturating_add(1);
442 return Err(FeedbackError::Capacity);
443 }
444 let (cue, gain, particles, reaction, motion, flash, rumble, stop) = match event.kind {
445 FeedbackKind::Fire => (Cue::Fire, 0.55, 2, Reaction::Recoil, 0.2, 0., 0.2, 0.),
446 FeedbackKind::Hit => (
447 Cue::Impact(Surface::Metal),
448 0.4,
449 8,
450 Reaction::Impact,
451 0.3,
452 0.,
453 0.3,
454 0.02,
455 ),
456 FeedbackKind::Hurt => (
457 Cue::Impact(Surface::Metal),
458 0.4,
459 6,
460 Reaction::Stagger,
461 0.7,
462 0.7,
463 0.7,
464 0.035,
465 ),
466 FeedbackKind::Land => (
467 Cue::Impact(Surface::Stone),
468 0.4,
469 4,
470 Reaction::Land,
471 0.3,
472 0.,
473 0.3,
474 0.,
475 ),
476 FeedbackKind::Dash => (Cue::Jump, 0.3, 5, Reaction::Dash, 0.15, 0., 0.2, 0.),
477 FeedbackKind::Pickup => (Cue::Pickup, 0.5, 6, Reaction::Collect, 0., 0., 0.15, 0.),
478 FeedbackKind::Explosion => (
479 Cue::Impact(Surface::Stone),
480 0.8,
481 24,
482 Reaction::Impact,
483 1.,
484 0.5,
485 1.,
486 0.04,
487 ),
488 };
489 self.seen[self.next] = id;
490 self.next = (self.next + 1) % N;
491 self.pending += 1;
492 let intensity = event.intensity;
493 if self.config.motion > 0.
494 && self.policy.motion_scale() > 0.
495 && intensity > 0.
496 && motion > 0.
497 && self
498 .shake
499 .push(ShakeImpulse {
500 translation: Vec3::splat(
501 (motion * intensity * self.config.translation_scale * self.config.motion)
502 .min(10.),
503 ),
504 rotation: Vec3::ZERO,
505 duration: 0.15,
506 frequency: 24.,
507 })
508 .is_err()
509 {
510 self.dropped = self.dropped.saturating_add(1);
511 }
512 if matches!(
513 event.kind,
514 FeedbackKind::Hit | FeedbackKind::Hurt | FeedbackKind::Explosion
515 ) && self.policy.motion_scale() > 0.
516 {
517 self.emphasis = self
518 .emphasis
519 .max((motion * intensity * self.config.motion).min(1.));
520 }
521 if self.policy.flash_scale() > 0. {
522 self.flash = self
523 .flash
524 .max((flash * intensity * self.config.flash).min(1.));
525 }
526 self.rumble[0] = self.rumble[0].max((rumble * intensity * self.config.rumble).min(1.));
527 self.rumble[1] =
528 self.rumble[1].max((rumble * 0.6 * intensity * self.config.rumble).min(1.));
529 if self.config.single_player_hitstop
530 && !self.config.networked
531 && self.config.motion > 0.
532 && self.policy.motion_scale() > 0.
533 {
534 self.hitstop = self.hitstop.max((stop * intensity).min(0.06));
535 }
536 Ok(Some(FeedbackResponse {
537 cue,
538 gain: (gain * intensity).min(1.),
539 particles: ((particles as f32 * intensity).min(64.) * self.policy.motion_scale()) as u8,
540 reaction,
541 position: event.position,
542 }))
543 }
544
545 pub fn impact(&self) -> ImpactSample {
561 ImpactSample {
562 emphasis: self.emphasis * self.policy.motion_scale(),
563 flash: self.flash * self.policy.flash_scale(),
564 }
565 }
566
567 pub fn advance_with_impact(
591 &mut self,
592 dt: f32,
593 ) -> Result<(FeedbackFrame, ImpactSample), FeedbackError> {
594 let impact = self.impact();
595 self.advance(dt).map(|frame| (frame, impact))
596 }
597
598 pub fn advance(&mut self, dt: f32) -> Result<FeedbackFrame, FeedbackError> {
603 if !dt.is_finite() || !(0. ..=0.25).contains(&dt) {
604 return Err(FeedbackError::Invalid);
605 }
606 let mut shake = self.shake.advance(dt).map_err(|_| FeedbackError::Invalid)?;
607 shake.translation *= self.policy.motion_scale();
608 shake.rotation *= self.policy.motion_scale();
609 let frame = FeedbackFrame {
610 shake,
611 flash: self.flash * self.policy.flash_scale(),
612 rumble: self.rumble,
613 hitstop_seconds: self.hitstop * self.policy.motion_scale(),
614 };
615 self.emphasis = decay_impact(self.emphasis, 24., dt);
616 self.flash = decay_impact(self.flash, 18., dt);
617 for rumble in &mut self.rumble {
618 *rumble *= (-24. * dt).exp();
619 }
620 self.hitstop = (self.hitstop - dt).max(0.);
621 self.pending = 0;
622 Ok(frame)
623 }
624}
625#[cfg(test)]
626mod tests {
627 use super::*;
628 fn event() -> FeedbackEvent {
629 FeedbackEvent {
630 kind: FeedbackKind::Hurt,
631 intensity: 1.,
632 position: Vec3::ZERO,
633 }
634 }
635 #[test]
636 fn confirmed_events_dedup_bounded_feedback_and_decay() {
637 let mut f = FeedbackController::<2>::new(FeedbackConfig::default(), 1).unwrap();
638 assert!(f.emit(1, event()).unwrap().is_some());
639 assert!(f.emit(1, event()).unwrap().is_none());
640 f.emit(2, event()).unwrap();
641 assert!(f.emit(3, event()).is_err());
642 let first = f.advance(0.1).unwrap();
643 assert!(first.flash > 0.5 && first.rumble[0] > 0.5);
644 let mut last = first;
645 for _ in 0..20 {
646 last = f.advance(0.1).unwrap();
647 }
648 assert!(last.flash < 0.001 && last.rumble[0] < 0.001);
649 assert_eq!(last.shake.translation, Vec3::ZERO);
650 }
651 #[test]
652 fn reduced_motion_and_online_mode_never_pause_simulation() {
653 let config = FeedbackConfig {
654 single_player_hitstop: true,
655 networked: true,
656 ..Default::default()
657 };
658 let mut f = FeedbackController::<8>::new(config, 3).unwrap();
659 f.emit(1, event()).unwrap();
660 assert_eq!(f.advance(0.).unwrap().hitstop_seconds, 0.);
661 f.configure(FeedbackConfig {
662 motion: 0.,
663 flash: 0.,
664 rumble: 0.,
665 ..config
666 })
667 .unwrap();
668 f.emit(2, event()).unwrap();
669 let frame = f.advance(0.).unwrap();
670 assert_eq!(frame.shake.translation, Vec3::ZERO);
671 assert_eq!(frame.flash, 0.);
672 assert_eq!(frame.rumble, [0.; 2]);
673 }
674 #[test]
675 fn invalid_config_and_event_are_transactional() {
676 let mut f = FeedbackController::<2>::new(FeedbackConfig::default(), 1).unwrap();
677 let mut e = event();
678 e.intensity = f32::NAN;
679 assert!(f.emit(1, e).is_err());
680 assert!(f.emit(1, event()).unwrap().is_some());
681 let old = f.advance(0.).unwrap();
682 assert!(f.advance(f32::NAN).is_err());
683 assert_eq!(f.advance(0.).unwrap().flash, old.flash);
684 }
685
686 #[test]
687 fn policies_and_event_helpers_reject_invalid_inputs() {
688 for intensity in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, -0.01, 1.01] {
689 assert_eq!(
690 FeedbackPolicy::new(false, false, intensity).unwrap_err(),
691 FeedbackError::Invalid
692 );
693 }
694 for intensity in [0., 0.5, 1.] {
695 let policy = FeedbackPolicy::new(true, false, intensity).unwrap();
696 assert!(policy.reduced_motion());
697 assert!(!policy.reduced_flash());
698 assert_eq!(policy.intensity(), intensity);
699 }
700 let constructors = [
701 (
702 FeedbackKind::Fire,
703 FeedbackEvent::fire as fn(Vec3, f32) -> _,
704 ),
705 (FeedbackKind::Hit, FeedbackEvent::hit),
706 (FeedbackKind::Hurt, FeedbackEvent::hurt),
707 (FeedbackKind::Land, FeedbackEvent::land),
708 (FeedbackKind::Dash, FeedbackEvent::dash),
709 (FeedbackKind::Pickup, FeedbackEvent::pickup),
710 (FeedbackKind::Explosion, FeedbackEvent::explosion),
711 ];
712 for (kind, make) in constructors {
713 for intensity in [0., 0.5, 1., 4.] {
714 let event = make(Vec3::ONE, intensity).unwrap();
715 assert_eq!(event.kind, kind);
716 assert_eq!(event.intensity, intensity);
717 assert_eq!(event.position, Vec3::ONE);
718 }
719 for intensity in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, -0.01, 4.01] {
720 assert_eq!(
721 make(Vec3::ZERO, intensity).unwrap_err(),
722 FeedbackError::Invalid
723 );
724 }
725 for position in [
726 Vec3::new(f32::NAN, 0., 0.),
727 Vec3::new(0., f32::INFINITY, 0.),
728 Vec3::new(0., 0., f32::NEG_INFINITY),
729 ] {
730 assert_eq!(make(position, 1.).unwrap_err(), FeedbackError::Invalid);
731 }
732 }
733 }
734
735 #[test]
736 fn default_policy_preserves_authored_responses_and_shake() {
737 let cases = [
738 (
739 FeedbackKind::Fire,
740 Cue::Fire,
741 0.55,
742 2,
743 Reaction::Recoil,
744 0.2,
745 0.,
746 0.2,
747 0.,
748 ),
749 (
750 FeedbackKind::Hit,
751 Cue::Impact(Surface::Metal),
752 0.4,
753 8,
754 Reaction::Impact,
755 0.3,
756 0.,
757 0.3,
758 0.02,
759 ),
760 (
761 FeedbackKind::Hurt,
762 Cue::Impact(Surface::Metal),
763 0.4,
764 6,
765 Reaction::Stagger,
766 0.7,
767 0.7,
768 0.7,
769 0.035,
770 ),
771 (
772 FeedbackKind::Land,
773 Cue::Impact(Surface::Stone),
774 0.4,
775 4,
776 Reaction::Land,
777 0.3,
778 0.,
779 0.3,
780 0.,
781 ),
782 (
783 FeedbackKind::Dash,
784 Cue::Jump,
785 0.3,
786 5,
787 Reaction::Dash,
788 0.15,
789 0.,
790 0.2,
791 0.,
792 ),
793 (
794 FeedbackKind::Pickup,
795 Cue::Pickup,
796 0.5,
797 6,
798 Reaction::Collect,
799 0.,
800 0.,
801 0.15,
802 0.,
803 ),
804 (
805 FeedbackKind::Explosion,
806 Cue::Impact(Surface::Stone),
807 0.8,
808 24,
809 Reaction::Impact,
810 1.,
811 0.5,
812 1.,
813 0.04,
814 ),
815 ];
816 for (kind, cue, gain, particles, reaction, motion, flash, rumble, stop) in cases {
817 let mut f = FeedbackController::<8>::new(
818 FeedbackConfig {
819 single_player_hitstop: true,
820 ..Default::default()
821 },
822 7,
823 )
824 .unwrap();
825 let response = f
826 .emit(1, FeedbackEvent::new(kind, Vec3::ONE, 1.).unwrap())
827 .unwrap()
828 .unwrap();
829 assert_eq!(response.cue, cue);
830 assert_eq!(response.gain, gain);
831 assert_eq!(response.particles, particles);
832 assert_eq!(response.reaction, reaction);
833 assert_eq!(response.position, Vec3::ONE);
834 let frame = f.advance(0.).unwrap();
835 let mut expected_shake = CameraShake::new(7);
836 if motion > 0. {
837 expected_shake
838 .push(ShakeImpulse {
839 translation: Vec3::splat(motion * 0.03),
840 rotation: Vec3::ZERO,
841 duration: 0.15,
842 frequency: 24.,
843 })
844 .unwrap();
845 }
846 assert_eq!(frame.shake, expected_shake.advance(0.).unwrap());
847 assert_eq!(frame.flash, flash);
848 assert_eq!(frame.rumble, [rumble, rumble * 0.6]);
849 assert_eq!(frame.hitstop_seconds, stop);
850 }
851 }
852
853 #[test]
854 fn visual_policies_preserve_audio_rumble_and_semantic_responses_for_every_kind() {
855 for kind in [
856 FeedbackKind::Fire,
857 FeedbackKind::Hit,
858 FeedbackKind::Hurt,
859 FeedbackKind::Land,
860 FeedbackKind::Dash,
861 FeedbackKind::Pickup,
862 FeedbackKind::Explosion,
863 ] {
864 for intensity in [0., 0.5, 1., 4.] {
865 for (motion, flash, scale) in [
866 (true, false, 1.),
867 (false, true, 1.),
868 (true, true, 1.),
869 (false, false, 0.),
870 (false, false, 0.5),
871 ] {
872 let config = FeedbackConfig {
873 single_player_hitstop: true,
874 ..Default::default()
875 };
876 let mut full = FeedbackController::<8>::new(config, 7).unwrap();
877 let mut reduced = FeedbackController::<8>::new(config, 7).unwrap();
878 reduced.set_policy(FeedbackPolicy::new(motion, flash, scale).unwrap());
879 let event = FeedbackEvent::new(kind, Vec3::ONE, intensity).unwrap();
880 let a = full.emit(1, event).unwrap().unwrap();
881 let b = reduced.emit(1, event).unwrap().unwrap();
882 assert_eq!(
883 (a.cue, a.gain, a.reaction, a.position),
884 (b.cue, b.gain, b.reaction, b.position)
885 );
886 assert!(b.particles <= a.particles);
887 let a = full.advance(0.).unwrap();
888 let b_frame = reduced.advance(0.).unwrap();
889 assert_eq!(a.rumble, b_frame.rumble);
890 if motion || scale == 0. {
891 assert_eq!(b.particles, 0);
892 assert_eq!(b_frame.shake, ShakeSample::default());
893 assert_eq!(b_frame.hitstop_seconds, 0.);
894 } else {
895 assert_eq!(b_frame.shake.translation, a.shake.translation * scale);
896 assert_eq!(b_frame.hitstop_seconds, a.hitstop_seconds * scale);
897 }
898 assert_eq!(b_frame.flash, if flash { 0. } else { a.flash * scale });
899 }
900 }
901 }
902 }
903
904 #[test]
905 fn fractional_policy_scales_after_event_caps_and_updates_pending_output() {
906 let config = FeedbackConfig {
907 single_player_hitstop: true,
908 ..Default::default()
909 };
910 let mut full = FeedbackController::<8>::new(config, 7).unwrap();
911 let mut reduced = FeedbackController::<8>::new(config, 7).unwrap();
912 reduced.set_policy(FeedbackPolicy::new(false, false, 0.5).unwrap());
913 let event = FeedbackEvent::explosion(Vec3::ZERO, 4.).unwrap();
914 let a = full.emit(1, event).unwrap().unwrap();
915 let b = reduced.emit(1, event).unwrap().unwrap();
916 assert_eq!((a.particles, b.particles), (64, 32));
917 assert_eq!((a.gain, b.gain), (1., 1.));
918 let a = full.advance(0.).unwrap();
919 let b = reduced.advance(0.).unwrap();
920 assert_eq!((a.flash, b.flash), (1., 0.5));
921 assert_eq!((a.hitstop_seconds, b.hitstop_seconds), (0.06, 0.03));
922 reduced.set_policy(FeedbackPolicy::new(false, false, 0.25).unwrap());
923 let b = reduced.advance(0.).unwrap();
924 assert_eq!(b.flash, 0.25);
925 assert_eq!(b.shake.translation, a.shake.translation * 0.25);
926 assert_eq!(b.hitstop_seconds, 0.015);
927 }
928
929 #[test]
930 fn turning_visuals_off_clears_pending_effects_without_replay() {
931 for (motion, flash, scale) in [(true, false, 1.), (false, true, 1.), (false, false, 0.)] {
932 let config = FeedbackConfig {
933 single_player_hitstop: true,
934 ..Default::default()
935 };
936 let mut f = FeedbackController::<8>::new(config, 7).unwrap();
937 f.emit(1, event()).unwrap();
938 let before = f.advance(0.).unwrap();
939 f.set_policy(FeedbackPolicy::new(motion, flash, scale).unwrap());
940 f.emit(2, event()).unwrap();
941 let disabled = f.advance(0.).unwrap();
942 f.set_policy(FeedbackPolicy::default());
943 let enabled = f.advance(0.).unwrap();
944 if motion || scale == 0. {
945 assert_eq!(disabled.shake, ShakeSample::default());
946 assert_eq!(enabled.shake, ShakeSample::default());
947 assert_eq!(enabled.hitstop_seconds, 0.);
948 }
949 if flash || scale == 0. {
950 assert_eq!(disabled.flash, 0.);
951 assert_eq!(enabled.flash, 0.);
952 }
953 assert_eq!(enabled.rumble, before.rumble);
954 f.emit(3, event()).unwrap();
955 let fresh = f.advance(0.).unwrap();
956 assert!(fresh.shake.translation.length_squared() > 0.);
957 assert!(fresh.flash > 0.);
958 }
959 }
960
961 #[test]
962 fn rejected_actions_do_not_reserve_ids_or_capacity() {
963 let mut f = FeedbackController::<1>::new(FeedbackConfig::default(), 7).unwrap();
964 let invalid = FeedbackEvent {
965 intensity: f32::NAN,
966 ..event()
967 };
968 assert!(f.emit_if_accepted(0, false, invalid).unwrap().is_none());
969 assert!(f.emit_if_accepted(1, false, event()).unwrap().is_none());
970 assert_eq!(f.pending, 0);
971 assert_eq!(f.dropped, 0);
972 let frame = f.advance(0.).unwrap();
973 assert_eq!(frame.shake, ShakeSample::default());
974 assert_eq!(frame.flash, 0.);
975 assert_eq!(frame.rumble, [0.; 2]);
976 assert!(f.emit_if_accepted(1, true, event()).unwrap().is_some());
977 assert!(f.emit_if_accepted(1, true, event()).unwrap().is_none());
978 assert_eq!(
979 f.emit_if_accepted(2, true, event()).unwrap_err(),
980 FeedbackError::Capacity
981 );
982 assert_eq!(f.dropped, 1);
983 }
984
985 #[test]
986 fn duplicate_and_overflow_events_leave_feedback_unchanged_and_can_be_retried() {
987 let mut f = FeedbackController::<2>::new(FeedbackConfig::default(), 7).unwrap();
988 assert_eq!(f.emit(0, event()).unwrap_err(), FeedbackError::Invalid);
989 f.emit(u64::MAX, event()).unwrap();
990 let shake = f.shake;
991 let intense = FeedbackEvent::explosion(Vec3::ZERO, 4.).unwrap();
992 assert!(f.emit(u64::MAX, intense).unwrap().is_none());
993 assert_eq!(f.shake, shake);
994 assert_eq!(f.pending, 1);
995 assert_eq!(f.flash, 0.7);
996 f.emit(2, event()).unwrap();
997 let shake = f.shake;
998 assert!(f.emit(2, intense).unwrap().is_none());
999 assert_eq!(f.emit(3, intense).unwrap_err(), FeedbackError::Capacity);
1000 assert_eq!(f.shake, shake);
1001 assert_eq!(f.flash, 0.7);
1002 assert_eq!(f.dropped, 1);
1003 f.advance(0.25).unwrap();
1004 assert!(f.emit(3, event()).unwrap().is_some());
1005 assert!(f.emit(2, event()).unwrap().is_none());
1006 assert!(f.emit(u64::MAX, event()).unwrap().is_some());
1008 }
1009
1010 #[test]
1011 fn restart_clear_preserves_preferences_but_releases_all_transient_state() {
1012 let config = FeedbackConfig {
1013 single_player_hitstop: true,
1014 ..Default::default()
1015 };
1016 let mut f = FeedbackController::<1>::new(config, 7).unwrap();
1017 let policy = FeedbackPolicy::new(false, false, 0.5).unwrap();
1018 f.set_policy(policy);
1019 f.emit(1, event()).unwrap();
1020 assert_eq!(f.emit(2, event()).unwrap_err(), FeedbackError::Capacity);
1021 f.clear();
1022 assert_eq!(f.policy(), policy);
1023 assert_eq!(f.dropped, 1);
1024 assert_eq!(f.pending, 0);
1025 let frame = f.advance(0.).unwrap();
1026 assert_eq!(frame.shake, ShakeSample::default());
1027 assert_eq!(frame.flash, 0.);
1028 assert_eq!(frame.rumble, [0.; 2]);
1029 assert_eq!(frame.hitstop_seconds, 0.);
1030 assert!(f.emit(1, event()).unwrap().is_some());
1031 assert!(f.advance(0.).unwrap().hitstop_seconds > 0.);
1032 }
1033
1034 #[test]
1035 fn configuration_is_transactional_and_cannot_override_policy() {
1036 let mut f = FeedbackController::<8>::new(FeedbackConfig::default(), 7).unwrap();
1037 f.emit(1, event()).unwrap();
1038 let before = f.advance(0.).unwrap();
1039 assert_eq!(
1040 f.configure(FeedbackConfig {
1041 motion: f32::NAN,
1042 flash: 0.,
1043 ..Default::default()
1044 }),
1045 Err(FeedbackError::Invalid)
1046 );
1047 let after = f.advance(0.).unwrap();
1048 assert_eq!(after.shake, before.shake);
1049 assert_eq!(after.flash, before.flash);
1050 let policy = FeedbackPolicy::new(true, true, 1.).unwrap();
1051 f.set_policy(policy);
1052 f.configure(FeedbackConfig {
1053 single_player_hitstop: true,
1054 ..Default::default()
1055 })
1056 .unwrap();
1057 let response = f.emit(2, event()).unwrap().unwrap();
1058 let after = f.advance(0.).unwrap();
1059 assert_eq!(f.policy(), policy);
1060 assert_eq!(response.gain, 0.4);
1061 assert_eq!(response.particles, 0);
1062 assert_eq!(after.shake, ShakeSample::default());
1063 assert_eq!(after.flash, 0.);
1064 assert_eq!(after.hitstop_seconds, 0.);
1065 }
1066
1067 #[test]
1068 fn silent_events_do_not_exhaust_shake_pool_and_visual_overflow_preserves_audio() {
1069 let mut f = FeedbackController::<32>::new(FeedbackConfig::default(), 7).unwrap();
1070 let silent = FeedbackEvent::hurt(Vec3::ZERO, 0.).unwrap();
1071 for id in 1..=8 {
1072 let response = f.emit(id, silent).unwrap().unwrap();
1073 assert_eq!(response.gain, 0.);
1074 assert_eq!(response.particles, 0);
1075 }
1076 assert_eq!(f.shake.active(), 0);
1077 for id in 9..=17 {
1078 assert_eq!(f.emit(id, event()).unwrap().unwrap().gain, 0.4);
1079 }
1080 assert_eq!(f.shake.active(), 8);
1081 assert_eq!(f.dropped, 1);
1082 assert!(f.emit(17, event()).unwrap().is_none());
1083 assert_eq!(f.dropped, 1);
1084 }
1085
1086 #[test]
1087 fn capacities_are_bounded_and_invalid_time_does_not_release_pending_events() {
1088 assert_eq!(
1089 FeedbackController::<0>::new(Default::default(), 1).err(),
1090 Some(FeedbackError::Capacity)
1091 );
1092 assert_eq!(
1093 FeedbackController::<129>::new(Default::default(), 1).err(),
1094 Some(FeedbackError::Capacity)
1095 );
1096 let mut f = FeedbackController::<128>::new(Default::default(), 1).unwrap();
1097 f.set_policy(FeedbackPolicy::new(true, true, 1.).unwrap());
1098 for id in 1..=128 {
1099 assert!(f.emit(id, event()).unwrap().is_some());
1100 }
1101 assert!(f.advance(f32::NAN).is_err());
1102 assert_eq!(f.emit(129, event()).unwrap_err(), FeedbackError::Capacity);
1103 assert_eq!(f.seen.len(), 128);
1104 f.advance(0.).unwrap();
1105 assert!(f.emit(129, event()).unwrap().is_some());
1106 }
1107
1108 #[test]
1109 fn impact_samples_pair_with_frames_decay_monotonically_and_settle_exactly() {
1110 let mut f = FeedbackController::<8>::new(Default::default(), 7).unwrap();
1111 assert_eq!(f.impact(), ImpactSample::default());
1112 let event = FeedbackEvent::explosion(Vec3::ZERO, 4.).unwrap();
1113 f.emit(1, event).unwrap();
1114 let peak = ImpactSample {
1115 emphasis: 1.,
1116 flash: 1.,
1117 };
1118 assert_eq!(f.impact(), peak);
1119 assert_eq!(f.advance_with_impact(0.).unwrap().1, peak);
1120 assert_eq!(f.impact(), peak);
1121 let mut previous = peak;
1122 for _ in 0..6 {
1123 let (frame, sample) = f.advance_with_impact(0.1).unwrap();
1124 assert_eq!(sample, previous);
1125 assert_eq!(frame.flash, sample.flash);
1126 let current = f.impact();
1127 assert!((0. ..=previous.emphasis).contains(¤t.emphasis));
1128 assert!((0. ..=previous.flash).contains(¤t.flash));
1129 previous = current;
1130 }
1131 assert_eq!(f.impact(), ImpactSample::default());
1132 assert_eq!(
1133 f.advance_with_impact(0.).unwrap().1,
1134 ImpactSample::default()
1135 );
1136 assert!(f.emit(1, event).unwrap().is_none());
1138 assert_eq!(f.impact(), ImpactSample::default());
1139 }
1140
1141 #[test]
1142 fn only_combat_events_raise_emphasis_and_authored_strength_is_capped() {
1143 for (kind, emphasis, flash) in [
1144 (FeedbackKind::Fire, 0., 0.),
1145 (FeedbackKind::Hit, 0.3, 0.),
1146 (FeedbackKind::Hurt, 0.7, 0.7),
1147 (FeedbackKind::Land, 0., 0.),
1148 (FeedbackKind::Dash, 0., 0.),
1149 (FeedbackKind::Pickup, 0., 0.),
1150 (FeedbackKind::Explosion, 1., 0.5),
1151 ] {
1152 for intensity in [0., 0.5, 1., 4.] {
1153 let config = FeedbackConfig {
1154 motion: 0.5,
1155 flash: 0.75,
1156 ..Default::default()
1157 };
1158 let mut f = FeedbackController::<8>::new(config, 7).unwrap();
1159 f.emit(1, FeedbackEvent::new(kind, Vec3::ZERO, intensity).unwrap())
1160 .unwrap();
1161 assert_eq!(
1162 f.impact(),
1163 ImpactSample {
1164 emphasis: (emphasis * intensity * config.motion).min(1.),
1165 flash: (flash * intensity * config.flash).min(1.),
1166 }
1167 );
1168 }
1169 }
1170 }
1171
1172 #[test]
1173 fn impact_policies_are_independent_and_preserve_sound_and_haptics_online() {
1174 let config = FeedbackConfig {
1175 single_player_hitstop: true,
1176 networked: true,
1177 ..Default::default()
1178 };
1179 for motion in [false, true] {
1180 for flash in [false, true] {
1181 for scale in [0., 0.25, 1.] {
1182 let mut f = FeedbackController::<8>::new(config, 7).unwrap();
1183 f.set_policy(FeedbackPolicy::new(motion, flash, scale).unwrap());
1184 let event = FeedbackEvent::explosion(Vec3::ZERO, 4.).unwrap();
1185 let response = f.emit(1, event).unwrap().unwrap();
1186 assert_eq!(response.cue, Cue::Impact(Surface::Stone));
1187 assert_eq!(response.gain, 1.);
1188 for dt in [0., 0.125, 0.25] {
1189 let (frame, impact) = f.advance_with_impact(dt).unwrap();
1190 assert_eq!(impact.flash, frame.flash);
1191 assert_eq!(frame.hitstop_seconds, 0.);
1192 if motion {
1193 assert_eq!(impact.emphasis, 0.);
1194 }
1195 if flash {
1196 assert_eq!(impact.flash, 0.);
1197 }
1198 assert!((0. ..=scale).contains(&impact.emphasis));
1199 assert!((0. ..=scale).contains(&impact.flash));
1200 }
1201 let mut full = FeedbackController::<8>::new(config, 7).unwrap();
1203 full.emit(1, event).unwrap();
1204 for dt in [0., 0.125, 0.25] {
1205 full.advance(dt).unwrap();
1206 }
1207 assert_eq!(f.rumble, full.rumble);
1208 assert_eq!(
1209 f.impact().emphasis,
1210 if motion {
1211 0.
1212 } else {
1213 full.impact().emphasis * scale
1214 }
1215 );
1216 assert_eq!(
1217 f.impact().flash,
1218 if flash {
1219 0.
1220 } else {
1221 full.impact().flash * scale
1222 }
1223 );
1224 }
1225 }
1226 }
1227 }
1228
1229 #[test]
1230 fn impact_policy_changes_rescale_live_levels_and_disabled_channels_never_revive() {
1231 for (motion, flash, scale) in [
1232 (true, false, 1.),
1233 (false, true, 1.),
1234 (true, true, 1.),
1235 (false, false, 0.),
1236 ] {
1237 let mut f = FeedbackController::<8>::new(Default::default(), 7).unwrap();
1238 let event = FeedbackEvent::explosion(Vec3::ZERO, 4.).unwrap();
1239 f.emit(1, event).unwrap();
1240 f.set_policy(FeedbackPolicy::new(false, false, 0.5).unwrap());
1241 assert_eq!(
1242 f.impact(),
1243 ImpactSample {
1244 emphasis: 0.5,
1245 flash: 0.5
1246 }
1247 );
1248 f.set_policy(FeedbackPolicy::new(false, false, 0.25).unwrap());
1249 assert_eq!(
1250 f.impact(),
1251 ImpactSample {
1252 emphasis: 0.25,
1253 flash: 0.25
1254 }
1255 );
1256 f.set_policy(FeedbackPolicy::new(motion, flash, scale).unwrap());
1257 f.emit(2, event).unwrap();
1258 let disabled = f.impact();
1259 f.set_policy(FeedbackPolicy::default());
1260 let restored = f.impact();
1261 assert_eq!(disabled, restored);
1262 assert_eq!(
1263 restored.emphasis,
1264 if motion || scale == 0. { 0. } else { 1. }
1265 );
1266 assert_eq!(restored.flash, if flash || scale == 0. { 0. } else { 1. });
1267 f.emit(3, event).unwrap();
1268 assert_eq!(
1269 f.impact(),
1270 ImpactSample {
1271 emphasis: 1.,
1272 flash: 1.
1273 }
1274 );
1275 }
1276 }
1277
1278 #[test]
1279 fn impact_sampling_rejections_duplicates_and_overflow_do_not_raise_or_refresh_levels() {
1280 let mut f = FeedbackController::<2>::new(Default::default(), 7).unwrap();
1281 let strong = FeedbackEvent::explosion(Vec3::ZERO, 4.).unwrap();
1282 f.emit(1, event()).unwrap();
1283 f.advance(0.125).unwrap();
1284 let before = f.impact();
1285 assert!(f.emit_if_accepted(2, false, strong).unwrap().is_none());
1286 assert!(f.emit(1, strong).unwrap().is_none());
1287 assert_eq!(f.impact(), before);
1288 f.emit(2, FeedbackEvent::hurt(Vec3::ZERO, 0.001).unwrap())
1290 .unwrap();
1291 f.emit(3, FeedbackEvent::hurt(Vec3::ZERO, 0.).unwrap())
1292 .unwrap();
1293 assert_eq!(f.impact(), before);
1294 assert_eq!(f.pending, 2);
1295 assert_eq!(f.emit(4, strong).unwrap_err(), FeedbackError::Capacity);
1296 assert_eq!(f.impact(), before);
1297 assert_eq!(f.dropped, 1);
1298 f.advance(0.125).unwrap();
1299 assert!(f.impact().emphasis < before.emphasis);
1300 assert!(f.emit(4, strong).unwrap().is_some());
1301 assert_eq!(
1302 f.impact(),
1303 ImpactSample {
1304 emphasis: 1.,
1305 flash: 1.
1306 }
1307 );
1308 }
1309
1310 #[test]
1311 fn impact_errors_are_transactional_and_configuration_and_restart_clear_levels() {
1312 let mut f = FeedbackController::<1>::new(Default::default(), 7).unwrap();
1313 f.emit(1, event()).unwrap();
1314 let before = f.impact();
1315 for dt in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, -0.01, 0.251] {
1316 assert_eq!(
1317 f.advance_with_impact(dt).unwrap_err(),
1318 FeedbackError::Invalid
1319 );
1320 assert_eq!(f.impact(), before);
1321 assert_eq!(f.pending, 1);
1322 }
1323 for invalid in [
1324 FeedbackEvent {
1325 intensity: f32::NAN,
1326 ..event()
1327 },
1328 FeedbackEvent {
1329 position: Vec3::splat(f32::INFINITY),
1330 ..event()
1331 },
1332 ] {
1333 assert_eq!(f.emit(2, invalid).unwrap_err(), FeedbackError::Invalid);
1334 assert_eq!(f.impact(), before);
1335 }
1336 assert_eq!(
1337 f.configure(FeedbackConfig {
1338 flash: f32::NAN,
1339 ..Default::default()
1340 }),
1341 Err(FeedbackError::Invalid)
1342 );
1343 assert_eq!(f.impact(), before);
1344 f.configure(FeedbackConfig {
1345 motion: 0.2,
1346 flash: 0.3,
1347 ..Default::default()
1348 })
1349 .unwrap();
1350 assert_eq!(
1351 f.impact(),
1352 ImpactSample {
1353 emphasis: 0.2,
1354 flash: 0.3
1355 }
1356 );
1357 f.configure(FeedbackConfig {
1358 motion: 0.,
1359 flash: 0.,
1360 ..Default::default()
1361 })
1362 .unwrap();
1363 f.configure(FeedbackConfig::default()).unwrap();
1364 assert_eq!(f.impact(), ImpactSample::default());
1365 assert!(f.emit(1, event()).unwrap().is_none());
1366 f.clear();
1367 assert!(f.emit(1, event()).unwrap().is_some());
1368 f.clear();
1369 assert_eq!(f.impact(), ImpactSample::default());
1370 assert_eq!(f.pending, 0);
1371 }
1372
1373 #[test]
1374 fn existing_and_paired_advance_share_one_decay_and_preserve_frame_outputs() {
1375 let config = FeedbackConfig {
1376 single_player_hitstop: true,
1377 ..Default::default()
1378 };
1379 let mut original = FeedbackController::<8>::new(config, 7).unwrap();
1380 let mut paired = FeedbackController::<8>::new(config, 7).unwrap();
1381 original.emit(1, event()).unwrap();
1382 paired.emit(1, event()).unwrap();
1383 for dt in [0., 0.0625, 0.125, 0.25, 0.25] {
1384 let before = original.impact();
1385 let a = original.advance(dt).unwrap();
1386 let (b, impact) = paired.advance_with_impact(dt).unwrap();
1387 assert_eq!(before, impact);
1388 assert_eq!(a.shake, b.shake);
1389 assert_eq!(a.flash, b.flash);
1390 assert_eq!(a.rumble, b.rumble);
1391 assert_eq!(a.hitstop_seconds, b.hitstop_seconds);
1392 assert_eq!(original.impact(), paired.impact());
1393 }
1394 }
1395
1396 #[test]
1397 fn impact_decay_is_consistent_across_frame_partitions_and_tiny_levels_do_not_linger() {
1398 let mut whole = FeedbackController::<8>::new(Default::default(), 7).unwrap();
1399 let mut split = FeedbackController::<8>::new(Default::default(), 7).unwrap();
1400 whole.emit(1, event()).unwrap();
1401 split.emit(1, event()).unwrap();
1402 whole.advance(0.125).unwrap();
1403 for _ in 0..4 {
1404 split.advance(0.03125).unwrap();
1405 }
1406 assert!((whole.impact().emphasis - split.impact().emphasis).abs() < 0.000001);
1407 assert!((whole.impact().flash - split.impact().flash).abs() < 0.000001);
1408 let mut tiny = FeedbackController::<8>::new(Default::default(), 7).unwrap();
1409 tiny.emit(1, FeedbackEvent::hurt(Vec3::ZERO, 0.00001).unwrap())
1410 .unwrap();
1411 let before = tiny.impact();
1412 assert!(before.emphasis > 0. && before.flash > 0.);
1413 tiny.advance(0.).unwrap();
1414 assert_eq!(tiny.impact(), before);
1415 tiny.advance(0.001).unwrap();
1416 assert_eq!(tiny.impact(), ImpactSample::default());
1417 }
1418}