Back to rankings

zkat/big-brain

Rust

Utility AI library for the Bevy game engine

bevy
Star Growth
Stars
1.3k
Forks
81
Weekly Growth
Issues
15
5001k
Jul 2020Jul 2022Jul 2024Jul 2026
Artifactscrates.iocargo add big-brain
README

big-brain

crates.io docs.rs Apache
2.0

big-brain is a Utility AI library for games, built for the Bevy Game Engine

It lets you define complex, intricate AI behaviors for your entities based on their perception of the world. Definitions are heavily data-driven, using plain Rust, and you only need to program Scorers (entities that look at your game world and come up with a Score), and Actions (entities that perform actual behaviors upon the world). No other code is needed for actual AI behavior.

See the documentation for more details.

Features

  • Highly concurrent/parallelizable evaluation.
  • Integrates smoothly with Bevy.
  • Proven game AI model.
  • Highly composable and reusable.
  • State machine-style continuous actions/behaviors.
  • Action cancellation.

Example

As a developer, you write application-dependent code to define Scorers and Actions, and then put it all together like building blocks, using Thinkers that will define the actual behavior.

Scorers

Scorers are entities that look at the world and evaluate into Score values. You can think of them as the "eyes" of the AI system. They're a highly-parallel way of being able to look at the World and use it to make some decisions later.

use bevy::prelude::*;
use big_brain::prelude::*;

#[derive(Debug, Clone, Component, ScorerBuilder)]
pub struct Thirsty;

pub fn thirsty_scorer_system(
    thirsts: Query<&Thirst>,
    mut query: Query<(&Actor, &mut Score), With<Thirsty>>,
) {
    for (Actor(actor), mut score) in query.iter_mut() {
        if let Ok(thirst) = thirsts.get(*actor) {
            score.set(thirst.thirst);
        }
    }
}
Actions

Actions are the actual things your entities will do. They are connected to ActionStates that represent the current execution state of the state machine.

use bevy::prelude::*;
use big_brain::prelude::*;

#[derive(Debug, Clone, Component, ActionBuilder)]
pub struct Drink;

fn drink_action_system(
    mut thirsts: Query<&mut Thirst>,
    mut query: Query<(&Actor, &mut ActionState), With<Drink>>,
) {
    for (Actor(actor), mut state) in query.iter_mut() {
        if let Ok(mut thirst) = thirsts.get_mut(*actor) {
            match *state {
                ActionState::Requested => {
                    thirst.thirst = 10.0;
                    *state = ActionState::Success;
                }
                ActionState::Cancelled => {
                    *state = ActionState::Failure;
                }
                _ => {}
            }
        }
    }
}
Thinkers

Finally, you can use it when define the Thinker, which you can attach as a regular Component:

fn spawn_entity(cmd: &mut Commands) {
    cmd.spawn((
        Thirst(70.0, 2.0),
        Thinker::build()
            .picker(FirstToScore { threshold: 0.8 })
            .when(Thirsty, Drink),
    ));
}
App

Once all that's done, we just add our systems and off we go!

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(BigBrainPlugin::new(PreUpdate))
        .add_systems(Startup, init_entities)
        .add_systems(Update, thirst_system)
        .add_systems(PreUpdate, drink_action_system.in_set(BigBrainSet::Actions))
        .add_systems(PreUpdate, thirsty_scorer_system.in_set(BigBrainSet::Scorers))
        .run();
}

bevy version and MSRV

The current version of big-brain is compatible with bevy@0.16.0.

The Minimum Supported Rust Version for big-brain should be considered to be the same as bevy's, which as of the time of this writing was "the latest stable release".

Reflection

All relevant big-brain types implement the bevy Reflect trait, so you should be able to get some useful display info while using things like bevy_inspector_egui.

This implementation should not be considered stable, and individual fields made visible may change at any time and not be considered towards semver. Please use this feature only for debugging.

Contributing

  1. Install the latest Rust toolchain (stable supported).
  2. cargo run --example thirst
  3. Happy hacking!

License

This project is licensed under the Apache-2.0 License.

Related repositories
bevyengine/bevy

A refreshingly simple data-driven game engine built in Rust

Rustcrates.ioApache License 2.0bevygamedev
bevy.org
47.3k4.7k
valence-rs/valence

A Rust framework for building Minecraft servers.

Rustcrates.ioMIT Licensegamedevminecraft
valence.rs
3.3k165
rivet-gg/rivet

🔩 Serverless for stateful backends

Rustcrates.ioApache License 2.0game-developmentmultiplayer
rivet.gg
3.2k80
avianphysics/avian

ECS-driven 2D and 3D physics engine for the Bevy game engine.

Rustcrates.ioApache License 2.0bevyphysics
crates.io/crates/avian3d
3.1k265
bevy-cheatbook/bevy-cheatbook

Unofficial Reference Book for the Bevy Game Engine

Rustcrates.iobevydocumentation
bevy-cheatbook.github.io
2.4k136
Jondolf/avian

ECS-driven 2D and 3D physics engine for the Bevy game engine.

Rustcrates.ioApache License 2.0bevyphysics
crates.io/crates/avian3d
1.9k133
fishfolk/jumpy

Tactical 2D shooter in fishy pixels style. Made with Rust-lang 🦀 and Bevy 🪶

Rustcrates.ioOthergamemodding
fishfolk.org/games/jumpy/
1.9k130
vladbat00/bevy_egui

This crate provides an Egui integration for the Bevy game engine. 🇺🇦 Please support the Ukrainian army: https://savelife.in.ua/en/

Rustcrates.ioMIT Licensebevyegui
1.4k338
djeedai/bevy_hanabi

🎆 Hanabi — a GPU particle system plugin for the Bevy game engine.

Rustcrates.ioApache License 2.0rustparticle
1.4k119
NiklasEi/bevy_game_template

Template for a Bevy game including CI/CD for web, Windows, Linux, macOS, iOS and Android

Rustcrates.ioCreative Commons Zero v1.0 Universalbevygame-development
niklasei.github.io/bevy_game_template/
1.1k107
localgpt-app/localgpt

Local AI assistant, dreaming explorable worlds.

Rustcrates.ioApache License 2.0agentai
localgpt.app
1.1k101
cBournhonesque/lightyear

A networking library to make multiplayer games for the Bevy game engine

Rustcrates.ioApache License 2.0bevygamedev
cbournhonesque.github.io/lightyear/book
1.1k148