/
waitstate.cpp
222 lines (168 loc) · 6.58 KB
/
waitstate.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
212
213
214
215
216
217
218
219
220
221
222
#include "behaviac/fsm/waitstate.h"
#include "behaviac/behaviortree/attachments/effector.h"
#include "behaviac/fsm/startcondition.h"
#include "behaviac/fsm/transitioncondition.h"
#include "behaviac/agent/agent.h"
#include "behaviac/behaviortree/nodes/conditions/condition.h"
#include "behaviac/common/meta.h"
namespace behaviac {
WaitState::WaitState() : m_time(0) {
}
WaitState::~WaitState() {
BEHAVIAC_DELETE(this->m_time);
}
void WaitState::load(int version, const char* agentType, const properties_t& properties) {
super::load(version, agentType, properties);
for (propertie_const_iterator_t it = properties.begin(); it != properties.end(); ++it) {
const property_t& p = (*it);
if (StringUtils::StringEqual(p.name, "Time")) {
if (StringUtils::IsValidString(p.value)) {
const char* pParenthesis = StringUtils::StringFind(p.value, '(');
if (pParenthesis == 0) {
behaviac::string typeName;
this->m_time = AgentMeta::ParseProperty(p.value);
} else {
this->m_time = AgentMeta::ParseMethod(p.value);
}
}
}
}
}
double WaitState::GetTime(Agent* pAgent) const {
double time = 0;
if (this->m_time != NULL) {
int typeNumberId = this->m_time->GetClassTypeNumberId();
if (typeNumberId == GetClassTypeNumberId<int>()) {
time = *(int*)this->m_time->GetValue(pAgent);
} else if (typeNumberId == GetClassTypeNumberId<double>()) {
time = *(double*)this->m_time->GetValue(pAgent);
} else if (typeNumberId == GetClassTypeNumberId<float>()) {
time = *(float*)this->m_time->GetValue(pAgent);
} else {
BEHAVIAC_ASSERT(false);
}
}
return time;
}
int WaitState::GetIntTime(Agent* pAgent) const {
int time = 0;
if (this->m_time) {
int typeNumberId = this->m_time->GetClassTypeNumberId();
if (typeNumberId == GetClassTypeNumberId<int>()) {
time = *(int*)this->m_time->GetValue(pAgent);
}
else {
BEHAVIAC_ASSERT(false);
}
}
return time;
}
bool WaitState::IsValid(Agent* pAgent, BehaviorTask* pTask) const {
if (WaitState::DynamicCast(pTask->GetNode()) == 0) {
return false;
}
return super::IsValid(pAgent, pTask);
}
BehaviorTask* WaitState::createTask() const {
WaitStateTask* pTask = BEHAVIAC_NEW WaitStateTask();
return pTask;
}
WaitStateTask::WaitStateTask() : StateTask(), m_start(0), m_time(0), m_intStart(0), m_intTime(0) {
}
WaitStateTask::~WaitStateTask() {
}
void WaitStateTask::copyto(BehaviorTask* target) const {
super::copyto(target);
BEHAVIAC_ASSERT(WaitStateTask::DynamicCast(target));
WaitStateTask* ttask = (WaitStateTask*)target;
ttask->m_start = this->m_start;
ttask->m_time = this->m_time;
ttask->m_intStart = this->m_intStart;
ttask->m_intTime = this->m_intTime;
}
void WaitStateTask::save(IIONode* node) const {
super::save(node);
if (this->m_status != BT_INVALID) {
CIOID startId("start");
node->setAttr(startId, this->m_start);
CIOID timeId("time");
node->setAttr(timeId, this->m_time);
CIOID intStartId("intstart");
node->setAttr(intStartId, this->m_intStart);
CIOID intTimeId("inttime");
node->setAttr(intTimeId, this->m_intTime);
}
}
void WaitStateTask::load(IIONode* node) {
super::load(node);
if (this->m_status != BT_INVALID) {
behaviac::string attrStr;
CIOID startId("start");
node->getAttr(startId, attrStr);
StringUtils::ParseString(attrStr.c_str(), this->m_start);
CIOID timeId("time");
node->getAttr(timeId, attrStr);
StringUtils::ParseString(attrStr.c_str(), this->m_time);
CIOID intStartId("intstart");
node->getAttr(intStartId, attrStr);
StringUtils::ParseString(attrStr.c_str(), this->m_intStart);
CIOID intTimeId("inttime");
node->getAttr(intTimeId, attrStr);
StringUtils::ParseString(attrStr.c_str(), this->m_intTime);
}
}
double WaitStateTask::GetTime(Agent* pAgent) const {
const WaitState* pWaitNode = WaitState::DynamicCast(this->GetNode());
return pWaitNode ? pWaitNode->GetTime(pAgent) : 0;
}
int WaitStateTask::GetIntTime(Agent* pAgent) const {
const WaitState* pWaitNode = WaitState::DynamicCast(this->GetNode());
return pWaitNode ? pWaitNode->GetIntTime(pAgent) : 0;
}
bool WaitStateTask::onenter(Agent* pAgent) {
BEHAVIAC_UNUSED_VAR(pAgent);
this->m_nextStateId = -1;
bool bUseIntValue = Workspace::GetInstance()->GetUseIntValue();
if (bUseIntValue) {
this->m_intStart = Workspace::GetInstance()->GetIntValueSinceStartup();
this->m_intTime = this->GetIntTime(pAgent);
if (this->m_intTime <= 0) {
return false;
}
} else {
this->m_start = Workspace::GetInstance()->GetDoubleValueSinceStartup();
this->m_time = this->GetTime(pAgent);
if (this->m_time <= 0) {
return false;
}
}
return true;
}
void WaitStateTask::onexit(Agent* pAgent, EBTStatus s) {
BEHAVIAC_UNUSED_VAR(pAgent);
BEHAVIAC_UNUSED_VAR(s);
}
EBTStatus WaitStateTask::update(Agent* pAgent, EBTStatus childStatus) {
BEHAVIAC_ASSERT(childStatus == BT_RUNNING);
BEHAVIAC_UNUSED_VAR(childStatus);
BEHAVIAC_UNUSED_VAR(pAgent);
BEHAVIAC_ASSERT(WaitState::DynamicCast(this->GetNode()) != 0, "node is not an WaitState");
WaitState* pStateNode = (WaitState*)(this->GetNode());
bool bUseIntValue = Workspace::GetInstance()->GetUseIntValue();
if (bUseIntValue) {
long long time = Workspace::GetInstance()->GetIntValueSinceStartup();
if (time - this->m_intStart >= this->m_intTime) {
pStateNode->Update(pAgent, this->m_nextStateId);
return BT_SUCCESS;
}
}
else {
double time = Workspace::GetInstance()->GetDoubleValueSinceStartup();
if (time - this->m_start >= this->m_time) {
pStateNode->Update(pAgent, this->m_nextStateId);
return BT_SUCCESS;
}
}
return BT_RUNNING;
}
}