-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpw_behavior_tree.cpp
More file actions
145 lines (121 loc) · 2.91 KB
/
Copy pathpw_behavior_tree.cpp
File metadata and controls
145 lines (121 loc) · 2.91 KB
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
#include "pw_behavior_tree.h"
#include <stdarg.h>
namespace pwngs
{
BehaviorNode::BehaviorNode()
: m_pParent(NULL)
, m_pPreCondition(NULL)
, m_nStatus(BEHAVIOR_STATUS_READY)
{
}
BehaviorNode::~BehaviorNode()
{
for(CollectionNodesT::iterator iter = m_vtNodes.begin(); iter != m_vtNodes.end(); ++iter)
delete (*iter);
m_vtNodes.clear();
if(m_pPreCondition != NULL)
{
delete m_pPreCondition;
m_pPreCondition = NULL;
}
}
BehaviorNode* BehaviorNode::Insert( BehaviorNode* node,size_t pos /*= std::string::npos*/ )
{
if(pos == std::string::npos)
m_vtNodes.push_back(node);
else
m_vtNodes.insert(m_vtNodes.begin() + pos,node);
node->m_pParent = this;
return this;
}
BehaviorNode* BehaviorNode::Remove( BehaviorNode* node )
{
CollectionNodesT::iterator iter = std::remove(m_vtNodes.begin(),m_vtNodes.end(),node);
if(iter == m_vtNodes.end())
return this;
delete (*iter);
m_vtNodes.erase(iter);
return this;
}
BehaviorNode* BehaviorNode::GetPreCondition()
{
return m_pPreCondition;
}
int BehaviorNode::SetPreCondition( BehaviorNode* node )
{
if(m_pPreCondition != NULL && m_pPreCondition != node)
delete m_pPreCondition;
m_pPreCondition = node;
return 0;
}
EBehaviorResult BehaviorNode::EvaluationPreCondition( BehaviorEnvriment& env )
{
if(m_pPreCondition != NULL)
return m_pPreCondition->Evaluation(env);
return BEHAVIOR_RESULT_SUCCESSFUL;
}
EBehaviorResult BehaviorNode::Evaluation( BehaviorEnvriment& env )
{
EBehaviorResult result = BEHAVIOR_RESULT_FAILURE;
switch(m_nStatus)
{
case BEHAVIOR_STATUS_READY:
{
if(EvaluationPreCondition(env) == BEHAVIOR_RESULT_SUCCESSFUL)
{
result = ExecutionBegan(env);
}
}
break;
case BEHAVIOR_STATUS_RUNNING:
{
result = Execute(env);
if(result == BEHAVIOR_RESULT_FINISHED)
{
ExecutionEnded(env);
}
}
break;
}
return result;
}
BehaviorNode* BehaviorNode::InsertNodes( BehaviorNode* node,... )
{
va_list vl;
va_start(vl,node);
while(node != NULL)
{
Insert(node);
node = va_arg(vl,BehaviorNode*);
}
va_end(vl);
return this;
}
EBehaviorResult BehaviorNode::ExecutionBegan( BehaviorEnvriment& env )
{
m_nStatus = BEHAVIOR_STATUS_RUNNING;
return BEHAVIOR_RESULT_RUNNING;
}
EBehaviorResult BehaviorNode::Execute(BehaviorEnvriment& env)
{
return BEHAVIOR_RESULT_FINISHED;
}
EBehaviorResult BehaviorNode::ExecutionEnded( BehaviorEnvriment& env )
{
m_nStatus = BEHAVIOR_STATUS_READY;
return BEHAVIOR_RESULT_SUCCESSFUL;
}
// ----------------------------------------------------------------------------
BehaviorTree::BehaviorTree(size_t numberOfStates)
: m_nCurrentState(0)
{
m_vtNodes.resize(numberOfStates);
}
EBehaviorResult BehaviorTree::Evaluation( BehaviorEnvriment& env )
{
BehaviorNode* node = m_vtNodes[m_nCurrentState];
if( node != NULL)
return node->Evaluation(env);
return BEHAVIOR_RESULT_FAILURE;
}
}