forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
behavior.cpp
176 lines (156 loc) · 4.55 KB
/
behavior.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "behavior.h"
#include <cassert>
#include <list>
#include <set>
#include <unordered_map>
#include <utility>
#include "behavior_oracle.h"
#include "behavior_strategy.h"
#include "generic_factory.h"
#include "debug.h"
#include "json.h"
using namespace behavior;
void node_t::set_strategy( const strategy_t *new_strategy )
{
strategy = new_strategy;
}
void node_t::set_predicate( std::function<status_t ( const oracle_t * )> new_predicate )
{
predicate = new_predicate;
}
void node_t::set_goal( const std::string &new_goal )
{
_goal = new_goal;
}
void node_t::add_child( const node_t *new_child )
{
children.push_back( new_child );
}
behavior_return node_t::tick( const oracle_t *subject ) const
{
assert( predicate );
if( children.empty() ) {
return { predicate( subject ), this };
} else {
assert( strategy != nullptr );
status_t result = predicate( subject );
if( result == running ) {
return strategy->evaluate( subject, children );
} else {
return { result, nullptr };
}
}
}
std::string node_t::goal() const
{
return _goal;
}
std::string tree::tick( const oracle_t *subject )
{
behavior_return result = root->tick( subject );
active_node = result.result == running ? result.selection : nullptr;
return goal();
}
std::string tree::goal() const
{
return active_node == nullptr ? "idle" : active_node->goal();
}
void tree::add( const node_t *new_node )
{
root = new_node;
}
// Now for the generic_factory definition
// This struct only exists to hold node data until finalization.
struct node_data {
string_id<node_t> id;
std::vector<std::string> children;
};
namespace
{
generic_factory<behavior::node_t> behavior_factory( "behavior" );
std::list<node_data> temp_node_data;
} // namespace
template<>
const node_t &string_id<node_t>::obj() const
{
return behavior_factory.obj( *this );
}
void behavior::load_behavior( const JsonObject &jo, const std::string &src )
{
behavior_factory.load( jo, src );
}
node_t::node_t()
{
predicate = &return_running;
}
void node_t::load( const JsonObject &jo, const std::string & )
{
// We don't initialize the node unless it has no children (opportunistic optimization).
// Instead we initialize a parallel struct that holds the labels until finalization.
if( jo.has_array( "children" ) ) {
temp_node_data.emplace_back();
node_data &new_data = temp_node_data.back();
new_data.id = id;
optional( jo, was_loaded, "children", new_data.children );
}
if( jo.has_string( "strategy" ) ) {
std::unordered_map<std::string, const strategy_t *>::iterator new_strategy =
behavior::strategy_map.find( jo.get_string( "strategy" ) );
if( new_strategy != strategy_map.end() ) {
strategy = new_strategy->second;
} else {
debugmsg( "While loading %s, failed to find strategy %s.",
id.str(), jo.get_string( "strategy" ) );
jo.throw_error( "Invalid strategy in behavior." );
}
}
if( jo.has_string( "predicate" ) ) {
auto new_predicate = predicate_map.find( jo.get_string( "predicate" ) );
if( new_predicate != predicate_map.end() ) {
predicate = new_predicate->second;
} else {
debugmsg( "While loading %s, failed to find predicate %s.",
id.str(), jo.get_string( "predicate" ) );
jo.throw_error( "Invalid strategy in behavior." );
}
}
optional( jo, was_loaded, "goal", _goal );
}
void node_t::check() const
{
// Invariants
if( children.empty() ) {
if( _goal.empty() ) {
debugmsg( "Behavior %s must have either children or a goal.",
id.str() );
}
} else {
if( strategy == nullptr ) {
debugmsg( "Behavior %s has children but no strategy.", id.str() );
}
if( !_goal.empty() ) {
debugmsg( "Behavior %s has both children and a goal.", id.str() );
}
}
}
void behavior::reset()
{
temp_node_data.clear();
behavior_factory.reset();
}
void behavior::finalize()
{
for( const node_data &new_node : temp_node_data ) {
for( const std::string &child : new_node.children ) {
const_cast<node_t &>( new_node.id.obj() ).
add_child( &string_id<node_t>( child ).obj() );
}
}
temp_node_data.clear();
}
void behavior::check_consistency()
{
for( const node_t &checked_node : behavior_factory.get_all() ) {
checked_node.check();
}
}