Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new behavior, RepeatSequence #24

Merged
merged 7 commits into from
Nov 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix pre-commit errors.
  • Loading branch information
kaphula committed Nov 25, 2023
commit bacbb30e496950b957e76e079adfc2db08dd82ec
6 changes: 5 additions & 1 deletion bonsai/src/state.rs
Original file line number Diff line number Diff line change
@@ -96,7 +96,11 @@ impl<A: Clone> State<A> {
Behavior::WhenAny(any) => State::WhenAnyState(any.into_iter().map(|ev| Some(State::new(ev))).collect()),
Behavior::After(after_all) => State::AfterState(0, after_all.into_iter().map(State::new).collect()),
Behavior::RepeatSequence(ev, rep) => {
let state = State::new(rep.get(0).expect("RepeatSequence's sequence of behaviors to run cannot be empty!").clone());
let state = State::new(
rep.get(0)
.expect("RepeatSequence's sequence of behaviors to run cannot be empty!")
.clone(),
);
State::RepeatSequenceState(Box::new(State::new(*ev)), rep, 0, Box::new(state))
}
}
34 changes: 14 additions & 20 deletions bonsai/tests/behavior_tests.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::behavior_tests::TestActions::{Dec, Inc, LessThan, LessThanRunningSuccess};
use bonsai_bt::Behavior::RepeatSequence;
use bonsai_bt::{
Action,
Behavior::{After, AlwaysSucceed, If, Invert, Select},
Event, Failure, Sequence, State,
Status::Running,
Success, UpdateArgs, Wait, WaitForever, WhenAll, While,
};
use bonsai_bt::Behavior::RepeatSequence;

/// Some test actions.
#[derive(Clone, Debug)]
@@ -18,7 +18,7 @@ enum TestActions {
///, Check if less than
LessThan(i32),
/// Check if less than and return [Running]. If more or equal return [Success].
LessThanRunningSuccess(i32)
LessThanRunningSuccess(i32),
}

// A test state machine that can increment and decrement.
@@ -53,7 +53,6 @@ fn tick(mut acc: i32, dt: f64, state: &mut State<TestActions>) -> (i32, bonsai_b
println!("failure {}>={}", acc, v);
(Success, args.dt)
}

}
});
println!("status: {:?} dt: {}", s, t);
@@ -73,8 +72,7 @@ fn tick_with_ref(acc: &mut i32, dt: f64, state: &mut State<TestActions>) {
*acc -= 1;
(Success, args.dt)
}
TestActions::LessThanRunningSuccess(_) |
LessThan(_) => todo!(),
TestActions::LessThanRunningSuccess(_) | LessThan(_) => todo!(),
});
}

@@ -426,13 +424,11 @@ fn test_after_all_succeed_out_of_order() {
assert_eq!(dt, 0.0);
}


#[test]
fn test_repeat_sequence() {
{
let a: i32 = 0;
let after = RepeatSequence(Box::new(Action(LessThanRunningSuccess(5))),
vec![Action(Inc)]);
let after = RepeatSequence(Box::new(Action(LessThanRunningSuccess(5))), vec![Action(Inc)]);

let mut state = State::new(after);

@@ -454,8 +450,10 @@ fn test_repeat_sequence() {
fn test_repeat_sequence_fail() {
{
let a: i32 = 4;
let after = RepeatSequence(Box::new(Action(LessThanRunningSuccess(5))),
vec![Action(Dec), Action(LessThan(0))]);
let after = RepeatSequence(
Box::new(Action(LessThanRunningSuccess(5))),
vec![Action(Dec), Action(LessThan(0))],
);
let mut state = State::new(after);
let (a, s, dt) = tick(a, 0.0, &mut state);

@@ -470,8 +468,10 @@ fn test_repeat_sequence_timed() {
let a: i32 = 0;
let time_step = 0.1;
let steps = 5;
let after = RepeatSequence(Box::new(Action(LessThanRunningSuccess(steps))),
vec![Wait(time_step), Action(Inc)]);
let after = RepeatSequence(
Box::new(Action(LessThanRunningSuccess(steps))),
vec![Wait(time_step), Action(Inc)],
);
let mut state = State::new(after);

// increment 3 times
@@ -480,22 +480,16 @@ fn test_repeat_sequence_timed() {
assert_eq!(a, 3);
assert_eq!(s, Running);


let (a, s, dt) = tick(a, 100.0, &mut state);
assert_eq!(dt, 100.0);
assert_eq!(a, 5);
assert_eq!(s, Success);
}


#[test]
#[should_panic]
fn test_repeat_sequence_empty() {
let a: i32 = 1;
let after = RepeatSequence( Box::new(Action(LessThanRunningSuccess(0))),
vec![]);

let after = RepeatSequence(Box::new(Action(LessThanRunningSuccess(0))), vec![]);
// panics because no behaviors...
let mut state = State::new(after);
let _state = State::new(after);
}

6 changes: 3 additions & 3 deletions examples/src/boids/main.rs
Original file line number Diff line number Diff line change
@@ -84,7 +84,7 @@ impl event::EventHandler for GameState {

for i in 0..(self.boids).len() {
let boids_vec = self.boids.to_vec();
let mut b = &mut self.boids[i];
let b = &mut self.boids[i];
game_tick(self.dt.as_secs_f32(), input::mouse::position(ctx), b, boids_vec);

//Convert new velocity to postion change
@@ -113,8 +113,8 @@ impl event::EventHandler for GameState {
});

let text_pos = glam::vec2(
(WIDTH - menu_text.width(ctx) as f32) / 2.0,
(HEIGHT - menu_text.height(ctx) as f32) / 2.0,
(WIDTH - menu_text.width(ctx)) / 2.0,
(HEIGHT - menu_text.height(ctx)) / 2.0,
);

graphics::draw(ctx, &menu_text, graphics::DrawParam::default().dest(text_pos))?;
25 changes: 11 additions & 14 deletions examples/src/simple_npc_ai/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use bonsai_bt::Behavior::RepeatSequence;
use bonsai_bt::{Behavior::Action, Event, Failure, Running, Status, Success, UpdateArgs, BT, While, Sequence};
use bonsai_bt::{Behavior::Action, Event, Failure, Running, Status, Success, UpdateArgs, BT};

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, PartialEq)]
pub enum EnemyNPC {
@@ -21,38 +21,38 @@ fn game_tick(bt: &mut BT<EnemyNPC, (), ()>, state: &mut EnemyNPCState) -> Status
match *args.action {
EnemyNPC::Run => {
state.perform_action("run");
return (Success, 0.0)
(Success, 0.0)
},
EnemyNPC::HasActionPointsLeft => {
if state.action_points == 0 {
println!("NPC does not have actions points left... ");
return (Success, 0.0);
(Success, 0.0)
}
else {
println!("NPC has action points: {}", state.action_points );
return(Running, 0.0)
(Running, 0.0)
}
}
EnemyNPC::Shoot => {
state.perform_action("shoot");
return(Success, 0.0)
(Success, 0.0)
}
EnemyNPC::Rest => {
if state.fully_rested() {
return (Success, 0.0)
}
state.rest();
return (Running, 0.0)
(Running, 0.0)
}
EnemyNPC::Die => {
state.die();
return (Success, 0.0);
(Success, 0.0)
}
EnemyNPC::IsDead => {
if state.is_alive() {
return (Running, 0.0);
}
return (Success, 0.0);
(Success, 0.0)
}
}
});
@@ -68,7 +68,7 @@ struct EnemyNPCState {
}
impl EnemyNPCState {
fn consume_action_point(&mut self) {
self.action_points = self.action_points.checked_sub(1).unwrap_or(0);
self.action_points = self.action_points.saturating_sub(1);
}
fn rest(&mut self) {
self.action_points = (self.action_points + 1).min(self.max_action_points);
@@ -81,8 +81,7 @@ impl EnemyNPCState {
fn is_alive(&self) -> bool {
if self.alive {
println!("NPC is alive...");
}
else {
} else {
println!("NPC is dead...");
}
self.alive
@@ -161,12 +160,10 @@ fn main() {
alive: true,
};


loop {
println!("reached main loop...");
match game_tick(&mut bt, &mut npc_state) {
Success |
Failure => {
Success | Failure => {
break;
}
Running => {}