Skip to content

Commit

Permalink
Moved in stuff from piston-event
Browse files Browse the repository at this point in the history
  • Loading branch information
bvssvni committed Oct 28, 2014
1 parent c92362a commit 2c53fc0
Show file tree
Hide file tree
Showing 6 changed files with 575 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .gitignore
@@ -0,0 +1,31 @@
.DS_Store
*~
*#
*.o
*.so
*.swp
*.old
*.bak
*.kate-swp
*.dylib
*.dSYM
*.dll
*.rlib
*.dummy
*.exe
*-test
/bin/main
/bin/test-internal
/bin/test-external
/doc/
/target/
/build/
/.rust/
rusti.sh
watch.sh
/examples/**
!/examples/*.rs
!/examples/assets/
!/bin/assets/

Cargo.lock
19 changes: 19 additions & 0 deletions Cargo.toml
@@ -0,0 +1,19 @@
[package]

name = "ai_behavior"
version = "0.0.0"
authors = [
"bvssvni <bvssvni@gmail.com>",
"Coeuvre <coeuvre@gmail.com>"
]
tags = []

[lib]

name = "ai_behavior"
path = "src/lib.rs"

[dependencies.event]

git = "https://github.com/PistonDevelopers/event"

61 changes: 61 additions & 0 deletions src/behavior.rs
@@ -0,0 +1,61 @@

use input;

/// Describes a behavior.
///
/// This is used for more complex event logic.
/// Can also be used for game AI.
#[deriving(Clone, Decodable, Encodable, PartialEq)]
pub enum Behavior<A> {
/// A button was pressed.
Pressed(input::Button),
/// A button was released.
Released(input::Button),
/// A high level description of an action.
Action(A),
/// Converts `Success` into `Failure` and vice versa.
Fail(Box<Behavior<A>>),
/// Ignores failures and returns `Success`.
AlwaysSucceed(Box<Behavior<A>>),
/// Runs behaviors one by one until a behavior succeeds.
///
/// If a behavior fails it will try the next one.
/// Fails if the last behavior fails.
/// Can be thought of as a short-circuited logical OR gate.
Select(Vec<Behavior<A>>),
/// Waits an amount of time before continuing.
///
/// f64: Time in seconds
Wait(f64),
/// Wait forever.
WaitForever,
/// `If(condition, success, failure)`
If(Box<Behavior<A>>, Box<Behavior<A>>, Box<Behavior<A>>),
/// Runs behaviors one by one until all succeeded.
///
/// The sequence fails if a behavior fails.
/// The sequence succeeds if all the behavior succeeds.
/// Can be thought of as a short-circuited logical AND gate.
Sequence(Vec<Behavior<A>>),
/// Loops while conditional behavior is running.
///
/// Succeeds if the conditional behavior succeeds.
/// Fails if the conditional behavior fails,
/// or if any behavior in the loop body fails.
While(Box<Behavior<A>>, Vec<Behavior<A>>),
/// Runs all behaviors in parallel until all succeeded.
///
/// Succeeds if all behaviors succeed.
/// Fails is any behavior fails.
WhenAll(Vec<Behavior<A>>),
/// Runs all behaviors in parallel until one succeeds.
///
/// Succeeds if one behavior succeeds.
/// Fails if all behaviors failed.
WhenAny(Vec<Behavior<A>>),
/// Runs all behaviors in parallel until all succeeds in sequence.
///
/// Succeeds if all behaviors succeed, but only if succeeding in sequence.
/// Fails if one behavior fails.
After(Vec<Behavior<A>>),
}
37 changes: 37 additions & 0 deletions src/lib.rs
@@ -0,0 +1,37 @@
#![deny(missing_doc)]

//! AI behavior tree

extern crate event;
extern crate input;
extern crate serialize;

pub use behavior::{
Action,
After,
AlwaysSucceed,
Behavior,
If,
Fail,
Pressed,
Released,
Select,
Sequence,
Wait,
WaitForever,
WhenAll,
WhenAny,
While,
};
pub use state::State;
pub use status::{
Failure,
Status,
Success,
Running,
};

mod behavior;
mod state;
mod status;

0 comments on commit 2c53fc0

Please sign in to comment.