1pub mod predicate;
2
3use predicate::Predicate;
4
5use crate::{
6 RtState,
7 data::{
8 ReportId, Sentiments,
9 npc::{Controller, Npc, NpcId},
10 },
11};
12use common::{
13 comp,
14 resources::{Time, TimeOfDay},
15 rtsim::NpcInput,
16 shared_server_config::ServerConstants,
17 uid::IdMaps,
18};
19use hashbrown::HashSet;
20use itertools::Either;
21use rand_chacha::ChaChaRng;
22use specs::{Read, ReadExpect, ReadStorage, SystemData, shred};
23use std::{any::Any, collections::VecDeque, marker::PhantomData, ops::ControlFlow};
24use world::{IndexRef, World};
25
26pub trait State: Clone + Send + Sync + 'static {}
27
28impl<T: Clone + Send + Sync + 'static> State for T {}
29
30#[derive(Clone, Copy)]
31struct Resettable<T> {
32 original: T,
33 current: T,
34}
35
36impl<T: Clone> From<T> for Resettable<T> {
37 fn from(value: T) -> Self {
38 Self {
39 original: value.clone(),
40 current: value,
41 }
42 }
43}
44
45impl<T: Clone> Resettable<T> {
46 fn reset(&mut self) { self.current = self.original.clone(); }
47}
48
49impl<T> std::ops::Deref for Resettable<T> {
50 type Target = T;
51
52 fn deref(&self) -> &Self::Target { &self.current }
53}
54
55impl<T> std::ops::DerefMut for Resettable<T> {
56 fn deref_mut(&mut self) -> &mut Self::Target { &mut self.current }
57}
58
59pub struct NpcCtx<'a, 'd> {
63 pub state: &'a RtState,
64 pub world: &'a World,
65 pub index: IndexRef<'a>,
66
67 pub time_of_day: TimeOfDay,
68 pub time: Time,
69
70 pub npc_id: NpcId,
71 pub npc: &'a Npc,
72 pub controller: &'a mut Controller,
73 pub inbox: &'a mut VecDeque<NpcInput>, pub sentiments: &'a mut Sentiments,
75 pub known_reports: &'a mut HashSet<ReportId>,
76
77 pub dt: f32,
79 pub rng: ChaChaRng,
80 pub system_data: &'a NpcSystemData<'d>,
81}
82
83#[derive(SystemData)]
84pub struct NpcSystemData<'a> {
85 pub positions: ReadStorage<'a, comp::Pos>,
86 pub id_maps: Read<'a, IdMaps>,
87 pub server_constants: ReadExpect<'a, ServerConstants>,
88}
89
90pub trait Action<S = (), R = ()>: Any + Send + Sync {
110 fn is_same(&self, other: &Self) -> bool
118 where
119 Self: Sized;
120
121 fn dyn_is_same_sized(&self, other: &dyn Action<S, R>) -> bool
123 where
124 Self: Sized,
125 {
126 match (other as &dyn Any).downcast_ref::<Self>() {
127 Some(other) => self.is_same(other),
128 None => false,
129 }
130 }
131
132 fn dyn_is_same(&self, other: &dyn Action<S, R>) -> bool;
134
135 fn backtrace(&self, bt: &mut Vec<String>);
138
139 fn reset(&mut self);
141
142 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<R>;
144
145 #[must_use]
155 fn then<A1: Action<S, R1>, R1>(self, other: A1) -> Then<Self, A1, R>
156 where
157 Self: Sized,
158 {
159 Then {
160 a0: self,
161 a0_finished: false,
162 a1: other,
163 phantom: PhantomData,
164 }
165 }
166
167 #[must_use]
179 fn and_then<F, A1: Action<S, R1>, R1>(self, f: F) -> AndThen<Self, F, A1, R>
180 where
181 Self: Sized,
182 {
183 AndThen {
184 a0: self,
185 f,
186 a1: None,
187 phantom: PhantomData,
188 }
189 }
190
191 #[must_use]
200 fn repeat(self) -> Repeat<Self, R>
201 where
202 Self: Sized,
203 {
204 Repeat(self, PhantomData)
205 }
206
207 #[must_use]
216 fn stop_if<P: Predicate + Clone>(self, p: P) -> StopIf<Self, P>
217 where
218 Self: Sized,
219 {
220 StopIf(self, p.into())
221 }
222
223 #[must_use]
237 fn interrupt_with<
238 A1: Action<S, R1>,
239 R1,
240 F: Fn(&mut NpcCtx, &mut S) -> Option<A1> + Send + Sync + 'static,
241 >(
242 self,
243 f: F,
244 ) -> InterruptWith<Self, F, A1, R1>
245 where
246 Self: Sized,
247 {
248 InterruptWith {
249 a0: self,
250 f,
251 a1: None,
252 phantom: PhantomData,
253 }
254 }
255
256 #[must_use]
258 fn map<F: Fn(R, &mut S) -> R1, R1>(self, f: F) -> Map<Self, F, R>
259 where
260 Self: Sized,
261 {
262 Map(self, f, PhantomData)
263 }
264
265 #[must_use]
290 fn boxed(self) -> Box<dyn Action<S, R>>
291 where
292 Self: Sized,
293 {
294 Box::new(self)
295 }
296
297 #[must_use]
310 fn with_state<S0>(self, s: S) -> WithState<Self, S, S0>
311 where
312 Self: Sized,
313 S: Clone,
314 {
315 WithState(self, s.into(), PhantomData)
316 }
317
318 #[must_use]
330 fn map_state<S0, F>(self, f: F) -> MapState<Self, F, S, S0>
331 where
332 F: Fn(&mut S0) -> &mut S,
333 Self: Sized,
334 {
335 MapState(self, f, PhantomData)
336 }
337
338 #[must_use]
347 fn debug<F, T>(self, mk_info: F) -> Debug<Self, F, T>
348 where
349 Self: Sized,
350 {
351 Debug(self, mk_info, PhantomData)
352 }
353
354 #[must_use]
355 fn l<Rhs>(self) -> Either<Self, Rhs>
356 where
357 Self: Sized,
358 {
359 Either::Left(self)
360 }
361
362 #[must_use]
363 fn r<Lhs>(self) -> Either<Lhs, Self>
364 where
365 Self: Sized,
366 {
367 Either::Right(self)
368 }
369}
370
371impl<S: State, R: 'static> Action<S, R> for Box<dyn Action<S, R>> {
372 fn is_same(&self, other: &Self) -> bool { (**self).dyn_is_same(other) }
373
374 fn dyn_is_same(&self, other: &dyn Action<S, R>) -> bool {
375 match (other as &dyn Any).downcast_ref::<Self>() {
376 Some(other) => self.is_same(other),
377 None => false,
378 }
379 }
380
381 fn backtrace(&self, bt: &mut Vec<String>) { (**self).backtrace(bt) }
382
383 fn reset(&mut self) { (**self).reset(); }
384
385 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<R> {
386 (**self).tick(ctx, state)
387 }
388}
389
390impl<S: State, R: 'static, A: Action<S, R>, B: Action<S, R>> Action<S, R> for Either<A, B> {
391 fn is_same(&self, other: &Self) -> bool {
392 match (self, other) {
393 (Either::Left(x), Either::Left(y)) => x.is_same(y),
394 (Either::Right(x), Either::Right(y)) => x.is_same(y),
395 _ => false,
396 }
397 }
398
399 fn dyn_is_same(&self, other: &dyn Action<S, R>) -> bool { self.dyn_is_same_sized(other) }
400
401 fn backtrace(&self, bt: &mut Vec<String>) {
402 match self {
403 Either::Left(x) => x.backtrace(bt),
404 Either::Right(x) => x.backtrace(bt),
405 }
406 }
407
408 fn reset(&mut self) {
409 match self {
410 Either::Left(x) => x.reset(),
411 Either::Right(x) => x.reset(),
412 }
413 }
414
415 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<R> {
416 match self {
417 Either::Left(x) => x.tick(ctx, state),
418 Either::Right(x) => x.tick(ctx, state),
419 }
420 }
421}
422
423#[derive(Copy, Clone)]
427pub struct Now<F, A>(F, Option<A>);
428
429impl<
430 S: State,
431 R: Send + Sync + 'static,
432 F: Fn(&mut NpcCtx, &mut S) -> A + Send + Sync + 'static,
433 A: Action<S, R>,
434> Action<S, R> for Now<F, A>
435{
436 fn is_same(&self, _other: &Self) -> bool { true }
438
439 fn dyn_is_same(&self, other: &dyn Action<S, R>) -> bool { self.dyn_is_same_sized(other) }
440
441 fn backtrace(&self, bt: &mut Vec<String>) {
442 if let Some(action) = &self.1 {
443 action.backtrace(bt);
444 } else {
445 bt.push("<thinking>".to_string());
446 }
447 }
448
449 fn reset(&mut self) { self.1 = None; }
450
451 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<R> {
453 (self.1.get_or_insert_with(|| (self.0)(ctx, state))).tick(ctx, state)
454 }
455}
456
457pub fn now<S, R, F, A: Action<S, R>>(f: F) -> Now<F, A>
470where
471 F: Fn(&mut NpcCtx, &mut S) -> A + Send + Sync + 'static,
472{
473 Now(f, None)
474}
475
476#[derive(Copy, Clone)]
480pub struct Until<F, A, R, R1>(F, Option<A>, PhantomData<(R, R1)>);
481
482impl<
483 S: State,
484 R: Send + Sync + 'static,
485 F: Fn(&mut NpcCtx, &mut S) -> ControlFlow<R1, A> + Send + Sync + 'static,
486 A: Action<S, R>,
487 R1: Send + Sync + 'static,
488> Action<S, R1> for Until<F, A, R, R1>
489{
490 fn is_same(&self, _other: &Self) -> bool { true }
492
493 fn dyn_is_same(&self, other: &dyn Action<S, R1>) -> bool { self.dyn_is_same_sized(other) }
494
495 fn backtrace(&self, bt: &mut Vec<String>) {
496 if let Some(action) = &self.1 {
497 action.backtrace(bt);
498 } else {
499 bt.push("<thinking>".to_string());
500 }
501 }
502
503 fn reset(&mut self) { self.1 = None; }
504
505 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<R1> {
506 let action = match &mut self.1 {
507 Some(action) => action,
508 None => match (self.0)(ctx, state) {
509 ControlFlow::Continue(action) => self.1.insert(action),
510 ControlFlow::Break(b) => return ControlFlow::Break(b),
511 },
512 };
513
514 match action.tick(ctx, state) {
515 ControlFlow::Continue(()) => ControlFlow::Continue(()),
516 ControlFlow::Break(_) => {
517 self.1 = None;
518 ControlFlow::Continue(())
519 },
520 }
521 }
522}
523
524pub fn until<S, F, A: Action<S, R>, R, R1>(f: F) -> Until<F, A, R, R1>
525where
526 F: Fn(&mut NpcCtx, &mut S) -> ControlFlow<R1, A>,
527{
528 Until(f, None, PhantomData)
529}
530
531#[derive(Copy, Clone)]
535pub struct Just<F, R = ()>(F, PhantomData<R>);
536
537impl<S: State, R: Send + Sync + 'static, F: Fn(&mut NpcCtx, &mut S) -> R + Send + Sync + 'static>
538 Action<S, R> for Just<F, R>
539{
540 fn is_same(&self, _other: &Self) -> bool { true }
541
542 fn dyn_is_same(&self, other: &dyn Action<S, R>) -> bool { self.dyn_is_same_sized(other) }
543
544 fn backtrace(&self, _bt: &mut Vec<String>) {}
545
546 fn reset(&mut self) {}
547
548 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<R> {
549 ControlFlow::Break((self.0)(ctx, state))
550 }
551}
552
553pub fn just<S: State, F, R: Send + Sync + 'static>(f: F) -> Just<F, R>
565where
566 F: Fn(&mut NpcCtx, &mut S) -> R + Send + Sync + 'static,
567{
568 Just(f, PhantomData)
569}
570
571#[derive(Copy, Clone)]
575pub struct Finish;
576
577impl<S: State> Action<S, ()> for Finish {
578 fn is_same(&self, _other: &Self) -> bool { true }
579
580 fn dyn_is_same(&self, other: &dyn Action<S, ()>) -> bool { self.dyn_is_same_sized(other) }
581
582 fn backtrace(&self, _bt: &mut Vec<String>) {}
583
584 fn reset(&mut self) {}
585
586 fn tick(&mut self, _ctx: &mut NpcCtx, _state: &mut S) -> ControlFlow<()> {
587 ControlFlow::Break(())
588 }
589}
590
591#[must_use]
610pub fn finish() -> Finish { Finish }
611
612pub type Priority = usize;
615
616pub const URGENT: Priority = 0;
617pub const IMPORTANT: Priority = 1;
618pub const CASUAL: Priority = 2;
619
620pub struct Node<S, R>(Box<dyn Action<S, R>>, Priority);
621
622#[must_use]
624pub fn urgent<S, A: Action<S, R>, R>(a: A) -> Node<S, R> { Node(Box::new(a), URGENT) }
625
626#[must_use]
628pub fn important<S, A: Action<S, R>, R>(a: A) -> Node<S, R> { Node(Box::new(a), IMPORTANT) }
629
630#[must_use]
632pub fn casual<S, A: Action<S, R>, R>(a: A) -> Node<S, R> { Node(Box::new(a), CASUAL) }
633
634pub struct Tree<S, F, R> {
636 next: F,
637 prev: Option<Node<S, R>>,
638 interrupt: bool,
639}
640
641impl<S: State, F: Fn(&mut NpcCtx, &mut S) -> Node<S, R> + Send + Sync + 'static, R: 'static>
642 Action<S, R> for Tree<S, F, R>
643{
644 fn is_same(&self, _other: &Self) -> bool { true }
645
646 fn dyn_is_same(&self, other: &dyn Action<S, R>) -> bool { self.dyn_is_same_sized(other) }
647
648 fn backtrace(&self, bt: &mut Vec<String>) {
649 if let Some(prev) = &self.prev {
650 prev.0.backtrace(bt);
651 } else {
652 bt.push("<thinking>".to_string());
653 }
654 }
655
656 fn reset(&mut self) { self.prev = None; }
657
658 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<R> {
659 let new = (self.next)(ctx, state);
660
661 let prev = match &mut self.prev {
662 Some(prev) if prev.1 <= new.1 && (prev.0.dyn_is_same(&*new.0) || !self.interrupt) => {
663 prev
664 },
665 _ => self.prev.insert(new),
666 };
667
668 match prev.0.tick(ctx, state) {
669 ControlFlow::Continue(()) => ControlFlow::Continue(()),
670 ControlFlow::Break(r) => {
671 self.prev = None;
672 ControlFlow::Break(r)
673 },
674 }
675 }
676}
677
678#[must_use]
702pub fn choose<S: State, R: 'static, F>(f: F) -> impl Action<S, R>
703where
704 F: Fn(&mut NpcCtx, &mut S) -> Node<S, R> + Send + Sync + 'static,
705{
706 Tree {
707 next: f,
708 prev: None,
709 interrupt: false,
710 }
711}
712
713#[must_use]
737pub fn watch<S: State, R: 'static, F>(f: F) -> impl Action<S, R>
738where
739 F: Fn(&mut NpcCtx, &mut S) -> Node<S, R> + Send + Sync + 'static,
740{
741 Tree {
742 next: f,
743 prev: None,
744 interrupt: true,
745 }
746}
747
748#[derive(Copy, Clone)]
752pub struct Then<A0, A1, R0> {
753 a0: A0,
754 a0_finished: bool,
755 a1: A1,
756 phantom: PhantomData<R0>,
757}
758
759impl<
760 S: State,
761 A0: Action<S, R0>,
762 A1: Action<S, R1>,
763 R0: Send + Sync + 'static,
764 R1: Send + Sync + 'static,
765> Action<S, R1> for Then<A0, A1, R0>
766{
767 fn is_same(&self, other: &Self) -> bool {
768 self.a0.is_same(&other.a0) && self.a1.is_same(&other.a1)
769 }
770
771 fn dyn_is_same(&self, other: &dyn Action<S, R1>) -> bool { self.dyn_is_same_sized(other) }
772
773 fn backtrace(&self, bt: &mut Vec<String>) {
774 if self.a0_finished {
775 self.a1.backtrace(bt);
776 } else {
777 self.a0.backtrace(bt);
778 }
779 }
780
781 fn reset(&mut self) {
782 self.a0.reset();
783 self.a0_finished = false;
784 self.a1.reset();
785 }
786
787 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<R1> {
788 if !self.a0_finished {
789 match self.a0.tick(ctx, state) {
790 ControlFlow::Continue(()) => return ControlFlow::Continue(()),
791 ControlFlow::Break(_) => self.a0_finished = true,
792 }
793 }
794 self.a1.tick(ctx, state)
795 }
796}
797
798#[derive(Copy, Clone)]
802pub struct AndThen<A0, F, A1, R0> {
803 a0: A0,
804 f: F,
805 a1: Option<A1>,
806 phantom: PhantomData<R0>,
807}
808
809impl<
810 S: State,
811 A0: Action<S, R0>,
812 A1: Action<S, R1>,
813 R0: Send + Sync + 'static,
814 R1: Send + Sync + 'static,
815 F: Fn(R0) -> A1 + Send + Sync + 'static,
816> Action<S, R1> for AndThen<A0, F, A1, R0>
817{
818 fn is_same(&self, other: &Self) -> bool {
819 self.a0.is_same(&other.a0)
820 && match (&self.a1, &other.a1) {
821 (Some(a1_0), Some(a1_1)) => a1_0.is_same(a1_1),
822 _ => true,
823 }
824 }
825
826 fn dyn_is_same(&self, other: &dyn Action<S, R1>) -> bool { self.dyn_is_same_sized(other) }
827
828 fn backtrace(&self, bt: &mut Vec<String>) {
829 if let Some(a1) = &self.a1 {
830 a1.backtrace(bt);
831 } else {
832 self.a0.backtrace(bt);
833 }
834 }
835
836 fn reset(&mut self) {
837 self.a0.reset();
838 self.a1 = None;
839 }
840
841 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<R1> {
842 let a1 = match &mut self.a1 {
843 None => match self.a0.tick(ctx, state) {
844 ControlFlow::Continue(()) => return ControlFlow::Continue(()),
845 ControlFlow::Break(r) => self.a1.insert((self.f)(r)),
846 },
847 Some(a1) => a1,
848 };
849 a1.tick(ctx, state)
850 }
851}
852
853#[derive(Copy, Clone)]
857pub struct InterruptWith<A0, F, A1, R1> {
858 a0: A0,
859 f: F,
860 a1: Option<A1>,
861 phantom: PhantomData<R1>,
862}
863
864impl<
865 S: State,
866 A0: Action<S, R0>,
867 A1: Action<S, R1>,
868 F: Fn(&mut NpcCtx, &mut S) -> Option<A1> + Send + Sync + 'static,
869 R0: Send + Sync + 'static,
870 R1: Send + Sync + 'static,
871> Action<S, R0> for InterruptWith<A0, F, A1, R1>
872{
873 fn is_same(&self, other: &Self) -> bool { self.a0.is_same(&other.a0) }
874
875 fn dyn_is_same(&self, other: &dyn Action<S, R0>) -> bool { self.dyn_is_same_sized(other) }
876
877 fn backtrace(&self, bt: &mut Vec<String>) {
878 if let Some(a1) = &self.a1 {
879 bt.push("<interrupted>".to_string());
881 a1.backtrace(bt);
882 } else {
883 self.a0.backtrace(bt);
884 }
885 }
886
887 fn reset(&mut self) {
888 self.a0.reset();
889 self.a1 = None;
890 }
891
892 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<R0> {
893 if let Some(new_a1) = (self.f)(ctx, state) {
894 self.a1 = Some(new_a1);
895 }
896
897 if let Some(a1) = &mut self.a1 {
898 match a1.tick(ctx, state) {
899 ControlFlow::Continue(()) => return ControlFlow::Continue(()),
900 ControlFlow::Break(_) => self.a1 = None,
901 }
902 }
903
904 self.a0.tick(ctx, state)
905 }
906}
907
908#[derive(Copy, Clone)]
912pub struct Repeat<A, R = ()>(A, PhantomData<R>);
913
914impl<S: State, R: Send + Sync + 'static, A: Action<S, R>> Action<S, !> for Repeat<A, R> {
915 fn is_same(&self, other: &Self) -> bool { self.0.is_same(&other.0) }
916
917 fn dyn_is_same(&self, other: &dyn Action<S, !>) -> bool { self.dyn_is_same_sized(other) }
918
919 fn backtrace(&self, bt: &mut Vec<String>) { self.0.backtrace(bt); }
920
921 fn reset(&mut self) { self.0.reset(); }
922
923 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<!> {
924 match self.0.tick(ctx, state) {
925 ControlFlow::Continue(()) => ControlFlow::Continue(()),
926 ControlFlow::Break(_) => {
927 self.0.reset();
928 ControlFlow::Continue(())
929 },
930 }
931 }
932}
933
934#[derive(Copy, Clone)]
938pub struct Sequence<I, A, R = ()>(Resettable<I>, Option<A>, PhantomData<R>);
939
940impl<
941 S: State,
942 R: Send + Sync + 'static,
943 I: Iterator<Item = A> + Clone + Send + Sync + 'static,
944 A: Action<S, R>,
945> Action<S, ()> for Sequence<I, A, R>
946{
947 fn is_same(&self, _other: &Self) -> bool { true }
948
949 fn dyn_is_same(&self, other: &dyn Action<S, ()>) -> bool { self.dyn_is_same_sized(other) }
950
951 fn backtrace(&self, bt: &mut Vec<String>) {
952 if let Some(action) = &self.1 {
953 action.backtrace(bt);
954 } else {
955 bt.push("<thinking>".to_string());
956 }
957 }
958
959 fn reset(&mut self) {
960 self.0.reset();
961 self.1 = None;
962 }
963
964 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<()> {
965 let item = if let Some(prev) = &mut self.1 {
966 prev
967 } else {
968 match self.0.next() {
969 Some(next) => self.1.insert(next),
970 None => return ControlFlow::Break(()),
971 }
972 };
973
974 if let ControlFlow::Break(_) = item.tick(ctx, state) {
975 self.1 = None;
976 }
977
978 ControlFlow::Continue(())
979 }
980}
981
982#[must_use]
1001pub fn seq<S, I, A, R>(iter: I) -> Sequence<I, A, R>
1002where
1003 I: Iterator<Item = A> + Clone,
1004 A: Action<S, R>,
1005{
1006 Sequence(iter.into(), None, PhantomData)
1007}
1008
1009#[derive(Copy, Clone)]
1013pub struct StopIf<A, P>(A, Resettable<P>);
1014
1015impl<S: State, A: Action<S, R>, P: Predicate + Clone + Send + Sync + 'static, R>
1016 Action<S, Option<R>> for StopIf<A, P>
1017{
1018 fn is_same(&self, other: &Self) -> bool { self.0.is_same(&other.0) }
1019
1020 fn dyn_is_same(&self, other: &dyn Action<S, Option<R>>) -> bool {
1021 self.dyn_is_same_sized(other)
1022 }
1023
1024 fn backtrace(&self, bt: &mut Vec<String>) { self.0.backtrace(bt); }
1025
1026 fn reset(&mut self) {
1027 self.0.reset();
1028 self.1.reset();
1029 }
1030
1031 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<Option<R>> {
1032 if self.1.should(ctx) {
1033 ControlFlow::Break(None)
1034 } else {
1035 self.0.tick(ctx, state).map_break(Some)
1036 }
1037 }
1038}
1039
1040#[derive(Copy, Clone)]
1044pub struct Map<A, F, R>(A, F, PhantomData<R>);
1045
1046impl<
1047 S: State,
1048 A: Action<S, R>,
1049 F: Fn(R, &mut S) -> R1 + Send + Sync + 'static,
1050 R: Send + Sync + 'static,
1051 R1,
1052> Action<S, R1> for Map<A, F, R>
1053{
1054 fn is_same(&self, other: &Self) -> bool { self.0.is_same(&other.0) }
1055
1056 fn dyn_is_same(&self, other: &dyn Action<S, R1>) -> bool { self.dyn_is_same_sized(other) }
1057
1058 fn backtrace(&self, bt: &mut Vec<String>) { self.0.backtrace(bt); }
1059
1060 fn reset(&mut self) { self.0.reset(); }
1061
1062 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<R1> {
1063 self.0.tick(ctx, state).map_break(|t| (self.1)(t, state))
1064 }
1065}
1066
1067#[derive(Copy, Clone)]
1071pub struct Debug<A, F, T>(A, F, PhantomData<T>);
1072
1073impl<
1074 S: 'static,
1075 A: Action<S, R>,
1076 F: Fn() -> T + Send + Sync + 'static,
1077 R: Send + Sync + 'static,
1078 T: Send + Sync + std::fmt::Display + 'static,
1079> Action<S, R> for Debug<A, F, T>
1080{
1081 fn is_same(&self, other: &Self) -> bool { self.0.is_same(&other.0) }
1082
1083 fn dyn_is_same(&self, other: &dyn Action<S, R>) -> bool { self.dyn_is_same_sized(other) }
1084
1085 fn backtrace(&self, bt: &mut Vec<String>) {
1086 bt.push((self.1)().to_string());
1087 self.0.backtrace(bt);
1088 }
1089
1090 fn reset(&mut self) { self.0.reset(); }
1091
1092 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S) -> ControlFlow<R> {
1093 self.0.tick(ctx, state)
1094 }
1095}
1096
1097#[derive(Copy, Clone)]
1098pub struct WithState<A, S, S0>(A, Resettable<S>, PhantomData<S0>);
1099
1100impl<S0: State, S: State, R, A: Action<S, R>> Action<S0, R> for WithState<A, S, S0> {
1101 fn is_same(&self, other: &Self) -> bool
1102 where
1103 Self: Sized,
1104 {
1105 self.0.is_same(&other.0)
1106 }
1107
1108 fn dyn_is_same(&self, other: &dyn Action<S0, R>) -> bool { self.dyn_is_same_sized(other) }
1109
1110 fn backtrace(&self, bt: &mut Vec<String>) { self.0.backtrace(bt) }
1111
1112 fn reset(&mut self) {
1113 self.0.reset();
1114 self.1.reset();
1115 }
1116
1117 fn tick(&mut self, ctx: &mut NpcCtx, _state: &mut S0) -> ControlFlow<R> {
1118 self.0.tick(ctx, &mut self.1.current)
1119 }
1120}
1121
1122#[derive(Copy, Clone)]
1123pub struct MapState<A, F, S, S0>(A, F, PhantomData<(S, S0)>);
1124
1125impl<S0: State, S: State, R, A: Action<S, R>, F: Fn(&mut S0) -> &mut S + Send + Sync + 'static>
1126 Action<S0, R> for MapState<A, F, S, S0>
1127{
1128 fn is_same(&self, other: &Self) -> bool
1129 where
1130 Self: Sized,
1131 {
1132 self.0.is_same(&other.0)
1133 }
1134
1135 fn dyn_is_same(&self, other: &dyn Action<S0, R>) -> bool { self.dyn_is_same_sized(other) }
1136
1137 fn backtrace(&self, bt: &mut Vec<String>) { self.0.backtrace(bt) }
1138
1139 fn reset(&mut self) { self.0.reset(); }
1140
1141 fn tick(&mut self, ctx: &mut NpcCtx, state: &mut S0) -> ControlFlow<R> {
1142 self.0.tick(ctx, (self.1)(state))
1143 }
1144}