Trait veloren_rtsim::ai::Action
source · pub trait Action<S = (), R = ()>: Any + Send + Sync {
Show 17 methods
// Required methods
fn is_same(&self, other: &Self) -> bool
where Self: Sized;
fn dyn_is_same(&self, other: &dyn Action<S, R>) -> bool;
fn backtrace(&self, bt: &mut Vec<String>);
fn reset(&mut self);
fn tick(&mut self, ctx: &mut NpcCtx<'_>, state: &mut S) -> ControlFlow<R>;
// Provided methods
fn dyn_is_same_sized(&self, other: &dyn Action<S, R>) -> bool
where Self: Sized { ... }
fn then<A1: Action<S, R1>, R1>(self, other: A1) -> Then<Self, A1, R>
where Self: Sized { ... }
fn repeat(self) -> Repeat<Self, R>
where Self: Sized { ... }
fn stop_if<P: Predicate + Clone>(self, p: P) -> StopIf<Self, P>
where Self: Sized { ... }
fn interrupt_with<A1: Action<S, R1>, R1, F: Fn(&mut NpcCtx<'_>, &mut S) -> Option<A1> + Send + Sync + 'static>(
self,
f: F,
) -> InterruptWith<Self, F, A1, R1>
where Self: Sized { ... }
fn map<F: Fn(R, &mut S) -> R1, R1>(self, f: F) -> Map<Self, F, R>
where Self: Sized { ... }
fn boxed(self) -> Box<dyn Action<S, R>>
where Self: Sized { ... }
fn with_state<S0>(self, s: S) -> WithState<Self, S, S0>
where Self: Sized,
S: Clone { ... }
fn map_state<S0, F>(self, f: F) -> MapState<Self, F, S, S0>
where F: Fn(&mut S0) -> &mut S,
Self: Sized { ... }
fn debug<F, T>(self, mk_info: F) -> Debug<Self, F, T>
where Self: Sized { ... }
fn l<Rhs>(self) -> Either<Self, Rhs>
where Self: Sized { ... }
fn r<Lhs>(self) -> Either<Lhs, Self>
where Self: Sized { ... }
}
Expand description
A trait that describes ‘actions’: long-running tasks performed by rtsim NPCs. These can be as simple as walking in a straight line between two locations or as complex as taking part in an adventure with players or performing an entire daily work schedule.
Actions are built up from smaller sub-actions via the combinator methods
defined on this trait, and with the standalone functions in this module.
Using these combinators, in a similar manner to using the Iterator
API,
it is possible to construct arbitrarily complex actions including behaviour
trees (see choose
and watch
) and other forms of moment-by-moment
decision-making.
On completion, actions may produce a value, denoted by the type parameter
R
. For example, an action may communicate whether it was successful or
unsuccessful through this completion value.
You should not need to implement this trait yourself when writing AI code. If you find yourself wanting to implement it, please discuss with the core dev team first.
Required Methods§
sourcefn is_same(&self, other: &Self) -> boolwhere
Self: Sized,
fn is_same(&self, other: &Self) -> boolwhere
Self: Sized,
Returns true
if the action should be considered the ‘same’ (i.e:
achieving the same objective) as another. In general, the AI system
will try to avoid switching (and therefore restarting) tasks when the
new task is the ‘same’ as the old one.
sourcefn dyn_is_same(&self, other: &dyn Action<S, R>) -> bool
fn dyn_is_same(&self, other: &dyn Action<S, R>) -> bool
Like Action::is_same
, but allows for dynamic dispatch.
sourcefn backtrace(&self, bt: &mut Vec<String>)
fn backtrace(&self, bt: &mut Vec<String>)
Generate a backtrace for the action. The action should recursively push all of the tasks it is currently performing.
sourcefn tick(&mut self, ctx: &mut NpcCtx<'_>, state: &mut S) -> ControlFlow<R>
fn tick(&mut self, ctx: &mut NpcCtx<'_>, state: &mut S) -> ControlFlow<R>
Perform the action for the current tick.
Provided Methods§
sourcefn dyn_is_same_sized(&self, other: &dyn Action<S, R>) -> boolwhere
Self: Sized,
fn dyn_is_same_sized(&self, other: &dyn Action<S, R>) -> boolwhere
Self: Sized,
Like Action::is_same
, but allows for dynamic dispatch.
sourcefn interrupt_with<A1: Action<S, R1>, R1, F: Fn(&mut NpcCtx<'_>, &mut S) -> Option<A1> + Send + Sync + 'static>(
self,
f: F,
) -> InterruptWith<Self, F, A1, R1>where
Self: Sized,
fn interrupt_with<A1: Action<S, R1>, R1, F: Fn(&mut NpcCtx<'_>, &mut S) -> Option<A1> + Send + Sync + 'static>(
self,
f: F,
) -> InterruptWith<Self, F, A1, R1>where
Self: Sized,
sourcefn map<F: Fn(R, &mut S) -> R1, R1>(self, f: F) -> Map<Self, F, R>where
Self: Sized,
fn map<F: Fn(R, &mut S) -> R1, R1>(self, f: F) -> Map<Self, F, R>where
Self: Sized,
Map the completion value of this action to something else.
sourcefn boxed(self) -> Box<dyn Action<S, R>>where
Self: Sized,
fn boxed(self) -> Box<dyn Action<S, R>>where
Self: Sized,
Box the action. Often used to perform type erasure, such as when you want to return one of many actions (each with different types) from the same function.
Note that Either
can often be used to unify mismatched types without
the need for boxing.
§Example
// Error! Type mismatch between branches
if npc.is_too_tired() {
goto(npc.home)
} else {
go_on_an_adventure()
}
// All fine
if npc.is_too_tired() {
goto(npc.home).boxed()
} else {
go_on_an_adventure().boxed()
}
sourcefn with_state<S0>(self, s: S) -> WithState<Self, S, S0>
fn with_state<S0>(self, s: S) -> WithState<Self, S, S0>
fn l<Rhs>(self) -> Either<Self, Rhs>where
Self: Sized,
fn r<Lhs>(self) -> Either<Lhs, Self>where
Self: Sized,
Trait Implementations§
source§impl<S: State, R: 'static> Action<S, R> for Box<dyn Action<S, R>>
impl<S: State, R: 'static> Action<S, R> for Box<dyn Action<S, R>>
source§fn is_same(&self, other: &Self) -> bool
fn is_same(&self, other: &Self) -> bool
true
if the action should be considered the ‘same’ (i.e:
achieving the same objective) as another. In general, the AI system
will try to avoid switching (and therefore restarting) tasks when the
new task is the ‘same’ as the old one.source§fn dyn_is_same(&self, other: &dyn Action<S, R>) -> bool
fn dyn_is_same(&self, other: &dyn Action<S, R>) -> bool
Action::is_same
, but allows for dynamic dispatch.source§fn backtrace(&self, bt: &mut Vec<String>)
fn backtrace(&self, bt: &mut Vec<String>)
source§fn tick(&mut self, ctx: &mut NpcCtx<'_>, state: &mut S) -> ControlFlow<R>
fn tick(&mut self, ctx: &mut NpcCtx<'_>, state: &mut S) -> ControlFlow<R>
source§fn dyn_is_same_sized(&self, other: &dyn Action<S, R>) -> boolwhere
Self: Sized,
fn dyn_is_same_sized(&self, other: &dyn Action<S, R>) -> boolwhere
Self: Sized,
Action::is_same
, but allows for dynamic dispatch.source§fn then<A1: Action<S, R1>, R1>(self, other: A1) -> Then<Self, A1, R>where
Self: Sized,
fn then<A1: Action<S, R1>, R1>(self, other: A1) -> Then<Self, A1, R>where
Self: Sized,
source§fn repeat(self) -> Repeat<Self, R>where
Self: Sized,
fn repeat(self) -> Repeat<Self, R>where
Self: Sized,
source§fn stop_if<P: Predicate + Clone>(self, p: P) -> StopIf<Self, P>where
Self: Sized,
fn stop_if<P: Predicate + Clone>(self, p: P) -> StopIf<Self, P>where
Self: Sized,
source§fn interrupt_with<A1: Action<S, R1>, R1, F: Fn(&mut NpcCtx<'_>, &mut S) -> Option<A1> + Send + Sync + 'static>(
self,
f: F,
) -> InterruptWith<Self, F, A1, R1>where
Self: Sized,
fn interrupt_with<A1: Action<S, R1>, R1, F: Fn(&mut NpcCtx<'_>, &mut S) -> Option<A1> + Send + Sync + 'static>(
self,
f: F,
) -> InterruptWith<Self, F, A1, R1>where
Self: Sized,
source§fn map<F: Fn(R, &mut S) -> R1, R1>(self, f: F) -> Map<Self, F, R>where
Self: Sized,
fn map<F: Fn(R, &mut S) -> R1, R1>(self, f: F) -> Map<Self, F, R>where
Self: Sized,
source§fn boxed(self) -> Box<dyn Action<S, R>>where
Self: Sized,
fn boxed(self) -> Box<dyn Action<S, R>>where
Self: Sized,
source§fn with_state<S0>(self, s: S) -> WithState<Self, S, S0>
fn with_state<S0>(self, s: S) -> WithState<Self, S, S0>
source§fn map_state<S0, F>(self, f: F) -> MapState<Self, F, S, S0>
fn map_state<S0, F>(self, f: F) -> MapState<Self, F, S, S0>
source§fn debug<F, T>(self, mk_info: F) -> Debug<Self, F, T>where
Self: Sized,
fn debug<F, T>(self, mk_info: F) -> Debug<Self, F, T>where
Self: Sized,
/npc_info
command. Read more