forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
behavior.cpp
211 lines (184 loc) · 5.93 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "behavior.h"
#include <list>
#include <set>
#include <unordered_map>
#include <utility>
#include "behavior_oracle.h"
#include "behavior_strategy.h"
#include "cata_assert.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::add_predicate( const
std::function<status_t ( const oracle_t *, const std::string & )> &
new_predicate, const std::string &argument, const bool &invert_result )
{
conditions.emplace_back( new_predicate, argument, invert_result );
}
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 );
}
status_t node_t::process_predicates( const oracle_t *subject ) const
{
status_t result = status_t::running;
for( const std::tuple< predicate_type, std::string, bool > &predicate_tuple : conditions ) {
predicate_type pt = std::get<0>( predicate_tuple );
std::string pargs = std::get<1>( predicate_tuple );
bool pinvert = std::get<2>( predicate_tuple );
result = pt( subject, pargs );
if( pinvert ) {
if( result == status_t::running ) {
result = status_t::failure;
} else if( result == status_t::failure ) {
result = status_t::running;
}
}
if( result != status_t::running ) {
break;
}
}
return result;
}
behavior_return node_t::tick( const oracle_t *subject ) const
{
if( children.empty() ) {
status_t result = process_predicates( subject );
return { result, this };
} else {
cata_assert( strategy != nullptr );
status_t result = process_predicates( subject );
if( result == status_t::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 == status_t::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
/** @relates string_id */
template<>
bool string_id<node_t>::is_valid() const
{
return behavior_factory.is_valid( *this );
}
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 );
}
void node_t::load( const JsonObject &jo, const std::string_view )
{
// 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." );
}
}
for( const JsonObject predicate_object : jo.get_array( "conditions" ) ) {
const std::string predicate_id = predicate_object.get_string( "predicate" );
auto new_predicate = predicate_map.find( predicate_id );
if( new_predicate == predicate_map.end() ) {
debugmsg( "While loading %s, failed to find predicate %s.",
id.str(), predicate_id );
jo.throw_error( "Invalid predicate in behavior." );
}
const std::string predicate_argument = predicate_object.get_string( "argument", "" );
const bool invert_result = predicate_object.get_bool( "invert_result", false );
conditions.emplace_back( new_predicate->second, predicate_argument,
invert_result );
}
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();
}
}