diff --git a/bonsai/src/behavior.rs b/bonsai/src/behavior.rs index 2b7882a..c4763fd 100644 --- a/bonsai/src/behavior.rs +++ b/bonsai/src/behavior.rs @@ -87,8 +87,8 @@ pub enum Behavior { ///let mut bt = BT::new(rs, ()); /// ///let mut i = 0; - ///let status = bt.tick(&Event::zero_dt_args(), &mut |args: ActionArgs, _| { - /// match args.action { + ///let status = bt.tick(&Event::zero_dt_args(), &mut |action, _, _| { + /// match action { /// Ex::A => { /// i += 1; /// if i == 4 { diff --git a/bonsai/src/bt.rs b/bonsai/src/bt.rs index 8b03709..7ad9118 100644 --- a/bonsai/src/bt.rs +++ b/bonsai/src/bt.rs @@ -51,7 +51,7 @@ impl BT { pub fn tick(&mut self, e: &E, f: &mut F) -> (Status, f64) where E: UpdateEvent, - F: FnMut(ActionArgs, &mut B) -> (Status, f64), + F: FnMut(&A, &mut B, ActionArgs) -> (Status, f64), { self.state.tick(e, &mut self.bb, f) } diff --git a/bonsai/src/lib.rs b/bonsai/src/lib.rs index 88747e5..38b02ac 100644 --- a/bonsai/src/lib.rs +++ b/bonsai/src/lib.rs @@ -51,7 +51,7 @@ //! fn tick(mut acc: i32, dt: f64, bt: &mut BT>) -> i32 { //! let e: Event = UpdateArgs { dt }.into(); //! -//! let (_status, _dt) = bt.tick(&e, &mut |args, blackboard| match *args.action { +//! let (_status, _dt) = bt.tick(&e, &mut |action, blackboard, args| match *action { //! Actions::Inc => { //! acc += 1; //! (Success, args.dt) diff --git a/bonsai/src/sequence.rs b/bonsai/src/sequence.rs index e51f697..feda7b3 100644 --- a/bonsai/src/sequence.rs +++ b/bonsai/src/sequence.rs @@ -20,7 +20,7 @@ pub fn sequence(args: SequenceArgs) -> (Status, f64) where A: Clone, E: UpdateEvent, - F: FnMut(ActionArgs, &mut B) -> (Status, f64), + F: FnMut(&A, &mut B, ActionArgs) -> (Status, f64), { let SequenceArgs { select, diff --git a/bonsai/src/state.rs b/bonsai/src/state.rs index 0df0037..6100c93 100644 --- a/bonsai/src/state.rs +++ b/bonsai/src/state.rs @@ -14,15 +14,13 @@ use serde::{Deserialize, Serialize}; pub const RUNNING: (Status, f64) = (Running, 0.0); /// The arguments in the action callback. -pub struct ActionArgs<'a, E: 'a, A: 'a> { +pub struct ActionArgs<'a, E: 'a> { /// The event. pub event: &'a E, /// The remaining delta time. When one action terminates, /// it can consume some of dt and the remaining is passed /// onto the next action. pub dt: f64, - /// The action running. - pub action: &'a A, } /// Keeps track of a behavior. @@ -203,7 +201,7 @@ impl State { pub fn tick(&mut self, e: &E, blackboard: &mut B, f: &mut F) -> (Status, f64) where E: UpdateEvent, - F: FnMut(ActionArgs, &mut B) -> (Status, f64), + F: FnMut(&A, &mut B, ActionArgs) -> (Status, f64), { let upd = e.update(|args| Some(args.dt)).unwrap_or(None); @@ -212,12 +210,12 @@ impl State { (_, &mut ActionState(ref action)) => { // println!("In ActionState: {:?}", action); f( + action, + blackboard, ActionArgs { event: e, dt: upd.unwrap_or(0.0), - action, }, - blackboard, ) } (_, &mut InvertState(ref mut cur)) => { diff --git a/bonsai/src/visualizer.rs b/bonsai/src/visualizer.rs index 41534b8..171fd0b 100644 --- a/bonsai/src/visualizer.rs +++ b/bonsai/src/visualizer.rs @@ -154,7 +154,7 @@ mod tests { // A test state machine that can increment and decrement. fn tick(mut acc: i32, dt: f64, bt: &mut BT>) -> (i32, Status, f64) { let e: Event = UpdateArgs { dt }.into(); - let (s, t) = bt.tick(&e, &mut |args, blackboard| match args.action { + let (s, t) = bt.tick(&e, &mut |action, blackboard, args| match action { TestActions::Inc => { acc += 1; (Success, args.dt) diff --git a/bonsai/src/when_all.rs b/bonsai/src/when_all.rs index 3e3954f..d52ed14 100644 --- a/bonsai/src/when_all.rs +++ b/bonsai/src/when_all.rs @@ -17,7 +17,7 @@ pub fn when_all( where A: Clone, E: UpdateEvent, - F: FnMut(ActionArgs, &mut B) -> (Status, f64), + F: FnMut(&A, &mut B, ActionArgs) -> (Status, f64), { let (status, inv_status) = if any { // `WhenAny` diff --git a/bonsai/tests/behavior_tests.rs b/bonsai/tests/behavior_tests.rs index 543e14e..95aca3f 100644 --- a/bonsai/tests/behavior_tests.rs +++ b/bonsai/tests/behavior_tests.rs @@ -21,40 +21,36 @@ enum TestActions { fn tick(mut acc: i32, dt: f64, state: &mut State) -> (i32, bonsai_bt::Status, f64) { let e: Event = UpdateArgs { dt }.into(); println!("acc {}", acc); - let (s, t) = state.tick( - &e, - &mut (), - &mut |args: ActionArgs, _| match *args.action { - Inc => { - acc += 1; + let (s, t) = state.tick(&e, &mut (), &mut |action, _, args: ActionArgs| match *action { + Inc => { + acc += 1; + (Success, args.dt) + } + Dec => { + acc -= 1; + (Success, args.dt) + } + LessThan(v) => { + println!("inside less than with acc: {}", acc); + if acc < v { + println!("success {}<{}", acc, v); (Success, args.dt) + } else { + println!("failure {}>={}", acc, v); + (Failure, args.dt) } - Dec => { - acc -= 1; + } + TestActions::LessThanRunningSuccess(v) => { + println!("inside LessThanRunningSuccess with acc: {}", acc); + if acc < v { + println!("success {}<{}", acc, v); + (Running, args.dt) + } else { + println!("failure {}>={}", acc, v); (Success, args.dt) } - LessThan(v) => { - println!("inside less than with acc: {}", acc); - if acc < v { - println!("success {}<{}", acc, v); - (Success, args.dt) - } else { - println!("failure {}>={}", acc, v); - (Failure, args.dt) - } - } - TestActions::LessThanRunningSuccess(v) => { - println!("inside LessThanRunningSuccess with acc: {}", acc); - if acc < v { - println!("success {}<{}", acc, v); - (Running, args.dt) - } else { - println!("failure {}>={}", acc, v); - (Success, args.dt) - } - } - }, - ); + } + }); println!("status: {:?} dt: {}", s, t); (acc, s, t) @@ -64,21 +60,17 @@ fn tick(mut acc: i32, dt: f64, state: &mut State) -> (i32, bonsai_b fn tick_with_ref(acc: &mut i32, dt: f64, state: &mut State) { let e: Event = UpdateArgs { dt }.into(); - state.tick( - &e, - &mut (), - &mut |args: ActionArgs, _| match *args.action { - Inc => { - *acc += 1; - (Success, args.dt) - } - Dec => { - *acc -= 1; - (Success, args.dt) - } - TestActions::LessThanRunningSuccess(_) | LessThan(_) => todo!(), - }, - ); + state.tick(&e, &mut (), &mut |action, _, args: ActionArgs| match *action { + Inc => { + *acc += 1; + (Success, args.dt) + } + Dec => { + *acc -= 1; + (Success, args.dt) + } + TestActions::LessThanRunningSuccess(_) | LessThan(_) => todo!(), + }); } // Each action that terminates immediately diff --git a/bonsai/tests/blackboard_tests.rs b/bonsai/tests/blackboard_tests.rs index 7b4e5f0..37b5043 100644 --- a/bonsai/tests/blackboard_tests.rs +++ b/bonsai/tests/blackboard_tests.rs @@ -17,7 +17,7 @@ pub enum TestActions { fn tick(mut acc: i32, dt: f64, bt: &mut BT>) -> i32 { let e: Event = UpdateArgs { dt }.into(); - let (_s, _t) = bt.tick(&e, &mut |args, _| match *args.action { + let (_s, _t) = bt.tick(&e, &mut |action, _, args| match *action { Inc => { acc += 1; (Success, args.dt) diff --git a/bonsai/tests/bt_tests.rs b/bonsai/tests/bt_tests.rs index 2d404c7..586f653 100644 --- a/bonsai/tests/bt_tests.rs +++ b/bonsai/tests/bt_tests.rs @@ -18,7 +18,7 @@ enum TestActions { fn tick(mut acc: i32, dt: f64, bt: &mut BT>) -> (i32, bonsai_bt::Status, f64) { let e: Event = UpdateArgs { dt }.into(); println!("acc {}", acc); - let (s, t) = bt.tick(&e, &mut |args, _| match *args.action { + let (s, t) = bt.tick(&e, &mut |action, _, args| match *action { Inc => { acc += 1; (Success, args.dt) diff --git a/bonsai/tests/dynamic_behavior_tests.rs b/bonsai/tests/dynamic_behavior_tests.rs index 4c77bee..796b264 100644 --- a/bonsai/tests/dynamic_behavior_tests.rs +++ b/bonsai/tests/dynamic_behavior_tests.rs @@ -15,34 +15,30 @@ enum TestActions { fn tick(mut acc: usize, dt: f64, t: &mut f64, counter: &mut usize, state: &mut State) -> usize { let e: Event = UpdateArgs { dt }.into(); - let (_s, _t) = state.tick( - &e, - &mut (), - &mut |args: ActionArgs, _| match args.action { - Inc => { - acc += 1; - (Success, args.dt) + let (_s, _t) = state.tick(&e, &mut (), &mut |action, _, args: ActionArgs| match action { + Inc => { + acc += 1; + (Success, args.dt) + } + DynamicWait(times) => { + // reset dynamic timer + if *counter >= times.len() { + *counter = 0 } - DynamicWait(times) => { - // reset dynamic timer - if *counter >= times.len() { - *counter = 0 - } - let wait_t = times[counter.to_owned()]; + let wait_t = times[counter.to_owned()]; - if *t + dt >= wait_t { - let time_overdue = *t + dt - wait_t; - *counter += 1; - *t = -dt; - (Success, time_overdue) - } else { - *t += dt; - RUNNING - } + if *t + dt >= wait_t { + let time_overdue = *t + dt - wait_t; + *counter += 1; + *t = -dt; + (Success, time_overdue) + } else { + *t += dt; + RUNNING } - }, - ); + } + }); acc } diff --git a/examples/src/3d/main.rs b/examples/src/3d/main.rs index 4612ea0..578ccf3 100644 --- a/examples/src/3d/main.rs +++ b/examples/src/3d/main.rs @@ -96,8 +96,8 @@ fn game_tick( let mut last_pos = mouse_pos(0.0, 0.0); // update state of behaviosuccessr tree #[rustfmt::skip] - bt.tick(&e,&mut |args: bonsai_bt::ActionArgs, _| - match *args.action { + bt.tick(&e,&mut |action, _, args: bonsai_bt::ActionArgs| + match *action { Animation::LongerThan(dur) => { if t > dur { (Status::Success, args.dt) diff --git a/examples/src/async_drone/main.rs b/examples/src/async_drone/main.rs index 306649b..7504141 100644 --- a/examples/src/async_drone/main.rs +++ b/examples/src/async_drone/main.rs @@ -50,8 +50,8 @@ async fn drone_tick( // update state of behaviosuccessr tree #[rustfmt::skip] - bt.tick(&e,&mut |args: bonsai_bt::ActionArgs, _| - match *args.action { + bt.tick(&e,&mut |action, _, args: bonsai_bt::ActionArgs| + match *action { DroneAction::AvoidOthers => { let avoid_state = &drone_state.avoid_others; if let Some(avoid_status) = avoid_state { diff --git a/examples/src/boids/boid.rs b/examples/src/boids/boid.rs index 4bf09c7..54e2e40 100644 --- a/examples/src/boids/boid.rs +++ b/examples/src/boids/boid.rs @@ -65,8 +65,8 @@ pub fn game_tick(dt: f32, cursor: mint::Point2, boid: &mut Boid, other_boid let win_height: f32 = *db.get("win_height").unwrap(); #[rustfmt::skip] - bt.tick(&e,&mut |args: bonsai_bt::ActionArgs, _| { - match args.action { + bt.tick(&e,&mut |action, _, args: bonsai_bt::ActionArgs| { + match action { Action::AvoidOthers => { let avoid_factor = 0.5; let mut move_x = 0.0; diff --git a/examples/src/simple_npc_ai/main.rs b/examples/src/simple_npc_ai/main.rs index e891da9..fddbe65 100644 --- a/examples/src/simple_npc_ai/main.rs +++ b/examples/src/simple_npc_ai/main.rs @@ -15,8 +15,8 @@ fn game_tick(bt: &mut BT, state: &mut EnemyNPCState) - let e: Event = UpdateArgs { dt: 0.0 }.into(); #[rustfmt::skip] - let status = bt.tick(&e, &mut |args: bonsai_bt::ActionArgs, blackboard| { - match *args.action { + let status = bt.tick(&e, &mut |action, blackboard, _| { + match *action { EnemyNPC::Run => { state.perform_action("run"); (Success, 0.0)