Function veloren_rtsim::ai::choose

source ·
pub fn choose<S: State, R: 'static, F>(f: F) -> impl Action<S, R>
where F: Fn(&mut NpcCtx<'_>, &mut S) -> Node<S, R> + Send + Sync + 'static,
Expand description

An action that allows implementing a decision tree, with action prioritisation.

The inner function will be run every tick to decide on an action. When an action is chosen, it will be performed until completed UNLESS an action with a more urgent priority is chosen in a subsequent tick. choose tries to commit to actions when it can: only more urgent actions will interrupt an action that’s currently being performed. If you want something that’s more eager to switch actions, see watch.

§Example

choose(|ctx| {
    if ctx.npc.is_being_attacked() {
        urgent(combat()) // If we're in danger, do something!
    } else if ctx.npc.is_hungry() {
        important(eat()) // If we're hungry, eat
    } else {
        casual(idle()) // Otherwise, do nothing
    }
})