Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
big-brain
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Louis
big-brain
Commits
a5bc5b74
Unverified
Commit
a5bc5b74
authored
4 years ago
by
Kat Marchán
Browse files
Options
Downloads
Patches
Plain Diff
added Steps action for action sequences
parent
f86664a1
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
big-brain-derive/src/action.rs
+3
-3
3 additions, 3 deletions
big-brain-derive/src/action.rs
src/actions.rs
+138
-4
138 additions, 4 deletions
src/actions.rs
src/lib.rs
+1
-0
1 addition, 0 deletions
src/lib.rs
src/thinker.rs
+7
-7
7 additions, 7 deletions
src/thinker.rs
with
149 additions
and
14 deletions
big-brain-derive/src/action.rs
+
3
−
3
View file @
a5bc5b74
...
...
@@ -72,7 +72,7 @@ impl ToTokens for Action {
mod
big_brain_action_builder
{
use
super
::
#
ident
as
Comp
;
use
big_brain
::{
typetag
,
serde
::
Deserialize
,
Action
,
Action
Manag
er
,
bevy
::
prelude
::{
Entity
,
Commands
},
ActionEnt
};
use
big_brain
::{
typetag
,
serde
::
Deserialize
,
Action
,
Action
Runn
er
,
bevy
::
prelude
::{
Entity
,
Commands
},
ActionEnt
};
#[derive(Debug,
Deserialize)]
struct
#
ident
{
...
...
@@ -81,12 +81,12 @@ impl ToTokens for Action {
#[typetag::deserialize]
impl
Action
for
#
ident
{
fn
build
(
self
:
Box
<
Self
>
,
actor
:
Entity
,
action_ent
:
ActionEnt
,
cmd
:
&
mut
Commands
)
->
Box
<
dyn
Action
Manag
er
>
{
fn
build
(
self
:
Box
<
Self
>
,
actor
:
Entity
,
action_ent
:
ActionEnt
,
cmd
:
&
mut
Commands
)
->
Box
<
dyn
Action
Runn
er
>
{
self
}
}
impl
Action
Manag
er
for
#
ident
{
impl
Action
Runn
er
for
#
ident
{
fn
activate
(
&
self
,
actor
:
Entity
,
action_ent
:
ActionEnt
,
cmd
:
&
mut
Commands
)
{
cmd
.entity
(
action_ent
.0
)
.insert
(
Comp
{
#
(
#
field_assignments
),
*
...
...
This diff is collapsed.
Click to expand it.
src/actions.rs
+
138
−
4
View file @
a5bc5b74
...
...
@@ -3,7 +3,7 @@ use bevy::prelude::*;
use
crate
::
ActionEnt
;
#[derive(Debug)]
pub
struct
Action
Manag
erWrapper
(
pub
(
crate
)
Box
<
dyn
Action
Manag
er
>
);
pub
struct
Action
Runn
erWrapper
(
pub
(
crate
)
Box
<
dyn
Action
Runn
er
>
);
#[derive(Debug,
Clone,
Eq,
PartialEq)]
pub
enum
ActionState
{
...
...
@@ -22,7 +22,7 @@ impl ActionState {
pub
(
crate
)
fn
build
(
builder
:
Box
<
dyn
Action
>
,
actor
:
Entity
,
cmd
:
&
mut
Commands
)
->
ActionEnt
{
let
action_ent
=
ActionEnt
(
cmd
.spawn
()
.id
());
let
manager_wrapper
=
Action
Manag
erWrapper
(
builder
.build
(
actor
,
action_ent
,
cmd
));
let
manager_wrapper
=
Action
Runn
erWrapper
(
builder
.build
(
actor
,
action_ent
,
cmd
));
cmd
.entity
(
action_ent
.0
)
.insert
(
ActionState
::
default
())
.insert
(
manager_wrapper
);
...
...
@@ -47,10 +47,144 @@ pub trait Action: std::fmt::Debug + Send + Sync {
actor
:
Entity
,
action_ent
:
ActionEnt
,
cmd
:
&
mut
Commands
,
)
->
Box
<
dyn
Action
Manag
er
>
;
)
->
Box
<
dyn
Action
Runn
er
>
;
}
pub
trait
Action
Manag
er
:
std
::
fmt
::
Debug
+
Send
+
Sync
{
pub
trait
Action
Runn
er
:
std
::
fmt
::
Debug
+
Send
+
Sync
{
fn
activate
(
&
self
,
actor
:
Entity
,
action
:
ActionEnt
,
cmd
:
&
mut
Commands
);
fn
deactivate
(
&
self
,
action
:
ActionEnt
,
cmd
:
&
mut
Commands
);
}
#[derive(Debug)]
pub
struct
Steps
{
steps
:
Vec
<
ActionEnt
>
,
active_step
:
usize
,
}
pub
fn
steps_system
(
mut
cmd
:
Commands
,
mut
steps_q
:
Query
<
(
Entity
,
&
Parent
,
&
mut
Steps
)
>
,
mut
states
:
Query
<&
mut
ActionState
>
,
runners
:
Query
<&
ActionRunnerWrapper
>
,
)
{
use
ActionState
::
*
;
for
(
seq_ent
,
Parent
(
actor
),
mut
steps_action
)
in
steps_q
.iter_mut
()
{
let
current_state
=
states
.get_mut
(
seq_ent
)
.expect
(
"uh oh"
)
.clone
();
match
current_state
{
Requested
=>
{
// Begin at the beginning
let
step_ent
=
steps_action
.steps
[
steps_action
.active_step
];
let
step_runner
=
runners
.get
(
step_ent
.0
)
.expect
(
"oops"
);
let
mut
step_state
=
states
.get_mut
(
step_ent
.0
)
.expect
(
"oops"
);
step_runner
.0
.activate
(
*
actor
,
step_ent
,
&
mut
cmd
);
*
step_state
=
Requested
;
let
mut
current_state
=
states
.get_mut
(
seq_ent
)
.expect
(
"uh oh"
);
*
current_state
=
Executing
;
}
Executing
=>
{
let
mut
step_state
=
states
.get_mut
(
steps_action
.steps
[
steps_action
.active_step
]
.0
)
.expect
(
"bug"
);
match
*
step_state
{
Init
=>
{
// Request it! This... should not really happen? But just in case I'm missing something... :)
*
step_state
=
Requested
;
}
Executing
|
Requested
=>
{
// do nothing. Everything's running as it should.
}
Cancelled
|
Failure
=>
{
// Cancel ourselves
let
step_ent
=
steps_action
.steps
[
steps_action
.active_step
];
let
step_state
=
step_state
.clone
();
let
step_runner
=
runners
.get
(
step_ent
.0
)
.expect
(
"oops"
);
step_runner
.0
.deactivate
(
step_ent
,
&
mut
cmd
);
let
mut
seq_state
=
states
.get_mut
(
seq_ent
)
.expect
(
"idk"
);
*
seq_state
=
step_state
;
}
Success
if
steps_action
.active_step
==
steps_action
.steps
.len
()
-
1
=>
{
// We're done! Let's just be successful
let
step_ent
=
steps_action
.steps
[
steps_action
.active_step
];
let
step_state
=
step_state
.clone
();
let
step_runner
=
runners
.get
(
step_ent
.0
)
.expect
(
"oops"
);
step_runner
.0
.deactivate
(
step_ent
,
&
mut
cmd
);
let
mut
seq_state
=
states
.get_mut
(
seq_ent
)
.expect
(
"idk"
);
*
seq_state
=
step_state
;
}
Success
=>
{
// Deactivate current step and go to the next step
let
step_ent
=
steps_action
.steps
[
steps_action
.active_step
];
let
step_runner
=
runners
.get
(
step_ent
.0
)
.expect
(
"oops"
);
step_runner
.0
.deactivate
(
step_ent
,
&
mut
cmd
);
steps_action
.active_step
+=
1
;
let
step_ent
=
steps_action
.steps
[
steps_action
.active_step
];
let
step_runner
=
runners
.get
(
step_ent
.0
)
.expect
(
"oops"
);
let
mut
step_state
=
states
.get_mut
(
step_ent
.0
)
.expect
(
"oops"
);
step_runner
.0
.activate
(
*
actor
,
step_ent
,
&
mut
cmd
);
*
step_state
=
ActionState
::
Requested
;
}
}
}
Cancelled
=>
{
// Cancel current action
let
step_ent
=
steps_action
.steps
[
steps_action
.active_step
];
let
step_runner
=
runners
.get
(
step_ent
.0
)
.expect
(
"oops"
);
let
mut
step_state
=
states
.get_mut
(
step_ent
.0
)
.expect
(
"oops"
);
step_runner
.0
.activate
(
*
actor
,
step_ent
,
&
mut
cmd
);
*
step_state
=
ActionState
::
Cancelled
;
}
Init
|
Success
|
Failure
=>
{
// Do nothing.
}
}
}
}
mod
seq_action
{
use
super
::
*
;
use
serde
::
Deserialize
;
#[derive(Debug,
Deserialize)]
struct
Steps
{
steps
:
Vec
<
Box
<
dyn
Action
>>
,
}
#[typetag::deserialize]
impl
Action
for
Steps
{
fn
build
(
self
:
Box
<
Self
>
,
actor
:
Entity
,
_action_ent
:
ActionEnt
,
cmd
:
&
mut
Commands
,
)
->
Box
<
dyn
ActionRunner
>
{
let
runner
=
StepsRunner
{
steps
:
self
.steps
.into_iter
()
.map
(|
builder
|
ActionState
::
build
(
builder
,
actor
,
cmd
))
.collect
(),
};
let
children
:
Vec
<
_
>
=
runner
.steps
.iter
()
.map
(|
x
|
x
.0
)
.collect
();
cmd
.entity
(
actor
)
.push_children
(
&
children
[
..
]);
Box
::
new
(
runner
)
}
}
#[derive(Debug)]
struct
StepsRunner
{
steps
:
Vec
<
ActionEnt
>
,
}
impl
ActionRunner
for
StepsRunner
{
fn
activate
(
&
self
,
_actor
:
Entity
,
action_ent
:
ActionEnt
,
cmd
:
&
mut
Commands
)
{
cmd
.entity
(
action_ent
.0
)
.insert
(
super
::
Steps
{
active_step
:
0
,
steps
:
self
.steps
.clone
(),
});
}
fn
deactivate
(
&
self
,
action_ent
:
ActionEnt
,
cmd
:
&
mut
Commands
)
{
cmd
.entity
(
action_ent
.0
)
.remove
::
<
super
::
Steps
>
();
}
}
}
This diff is collapsed.
Click to expand it.
src/lib.rs
+
1
−
0
View file @
a5bc5b74
...
...
@@ -23,6 +23,7 @@ pub struct BigBrainPlugin;
impl
Plugin
for
BigBrainPlugin
{
fn
build
(
&
self
,
app
:
&
mut
AppBuilder
)
{
app
.add_system
(
thinker_system
.system
());
app
.add_system
(
steps_system
.system
());
app
.add_system
(
fixed_score_system
.system
());
app
.add_system
(
all_or_nothing_system
.system
());
app
.add_system
(
sum_of_scorers_system
.system
());
...
...
This diff is collapsed.
Click to expand it.
src/thinker.rs
+
7
−
7
View file @
a5bc5b74
...
...
@@ -6,7 +6,7 @@ use bevy::prelude::*;
use
serde
::
Deserialize
;
use
crate
::{
actions
::{
self
,
Action
,
Action
Manag
er
,
Action
Manag
erWrapper
,
ActionState
},
actions
::{
self
,
Action
,
Action
Runn
er
,
Action
Runn
erWrapper
,
ActionState
},
choices
::{
Choice
,
ChoiceBuilder
},
scorers
::
Score
,
pickers
::
Picker
,
...
...
@@ -64,7 +64,7 @@ impl Action for builder::Thinker {
actor
:
Entity
,
action_ent
:
ActionEnt
,
cmd
:
&
mut
Commands
,
)
->
Box
<
dyn
Action
Manag
er
>
{
)
->
Box
<
dyn
Action
Runn
er
>
{
let
choices
=
self
.choices
.into_iter
()
...
...
@@ -80,7 +80,7 @@ impl Action for builder::Thinker {
current_action
:
None
,
});
cmd
.entity
(
actor
)
.push_children
(
&
[
action_ent
.0
]);
Box
::
new
(
Thinker
Manag
er
)
Box
::
new
(
Thinker
Runn
er
)
}
}
...
...
@@ -88,9 +88,9 @@ impl Action for builder::Thinker {
pub
struct
ActiveThinker
(
bool
);
#[derive(Debug)]
pub
struct
Thinker
Manag
er
;
pub
struct
Thinker
Runn
er
;
impl
Action
Manag
er
for
Thinker
Manag
er
{
impl
Action
Runn
er
for
Thinker
Runn
er
{
fn
activate
(
&
self
,
_
:
Entity
,
action_ent
:
ActionEnt
,
cmd
:
&
mut
Commands
)
{
cmd
.entity
(
action_ent
.0
)
.insert
(
ActiveThinker
(
false
))
...
...
@@ -125,7 +125,7 @@ pub fn thinker_system(
mut
thinker_q
:
Query
<
(
Entity
,
&
Parent
,
&
mut
Thinker
,
&
ActiveThinker
)
>
,
utilities
:
Query
<&
Score
>
,
mut
action_states
:
Query
<&
mut
actions
::
ActionState
>
,
builder_wrappers
:
Query
<&
Action
Manag
erWrapper
>
,
builder_wrappers
:
Query
<&
Action
Runn
erWrapper
>
,
)
{
let
start
=
Instant
::
now
();
for
(
thinker_ent
,
Parent
(
actor
),
mut
thinker
,
active_thinker
)
in
thinker_q
.iter_mut
()
.skip
(
iterations
.index
)
{
...
...
@@ -222,7 +222,7 @@ fn exec_picked_action(
thinker
:
&
mut
Mut
<
Thinker
>
,
picked_action_ent
:
&
ActionEnt
,
states
:
&
mut
Query
<&
mut
ActionState
>
,
builder_wrappers
:
&
Query
<&
Action
Manag
erWrapper
>
,
builder_wrappers
:
&
Query
<&
Action
Runn
erWrapper
>
,
)
{
// If we do find one, then we need to grab the corresponding
// component for it. The "action" that `picker.pick()` returns
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment