Function veloren_rtsim::ai::watch

source ·
pub fn watch<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 a different action of the same or higher priority is chosen in a subsequent tick. watch is very unfocused and will happily switch between actions rapidly between ticks if conditions change. If you want something that tends to commit to actions until they are completed, see choose.

§Example

watch(|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
    }
})