This commit is contained in:
aozhiwei 2023-10-31 14:53:08 +08:00
parent 3f0357a46c
commit 0df5dc6b4c
3 changed files with 54 additions and 19 deletions

View File

@ -76,14 +76,10 @@ bool BaseAgent::PreEnterCoroutine(int co_id, behaviac::EBTStatus& status)
return false; return false;
} }
auto& co = itr->second; auto& co = itr->second;
if (co->IsAbort()) {
status = behaviac::BT_FAILURE;
return true;
}
if (co->status != behaviac::BT_RUNNING) { if (co->status != behaviac::BT_RUNNING) {
abort(); abort();
} }
co->status = co->runing_cb(); co->status = co->runing_cb(co.get());
status = co->status; status = co->status;
if (co->status != behaviac::BT_RUNNING) { if (co->status != behaviac::BT_RUNNING) {
list_del_init(&co->entry); list_del_init(&co->entry);
@ -137,8 +133,11 @@ behaviac::EBTStatus BaseAgent::CoTest1(int time)
context->time = time; context->time = time;
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoTest1"); auto co = std::make_shared<BtCoroutine>(context, co_id, "CoTest1");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if ((GetRoom()->GetFrameNo() - context->frameno) * FRAME_RATE_MS < if ((GetRoom()->GetFrameNo() - context->frameno) * FRAME_RATE_MS <
context->time) { context->time) {
return behaviac::BT_RUNNING; return behaviac::BT_RUNNING;
@ -159,8 +158,11 @@ behaviac::EBTStatus BaseAgent::CoTest(int time)
context->time = time; context->time = time;
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoTest"); auto co = std::make_shared<BtCoroutine>(context, co_id, "CoTest");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if ((GetRoom()->GetFrameNo() - context->frameno) * FRAME_RATE_MS < if ((GetRoom()->GetFrameNo() - context->frameno) * FRAME_RATE_MS <
context->time) { context->time) {
return behaviac::BT_RUNNING; return behaviac::BT_RUNNING;

View File

@ -9,7 +9,7 @@ class BtCoroutine
{ {
public: public:
std::function<behaviac::EBTStatus()> runing_cb; std::function<behaviac::EBTStatus(BtCoroutine*)> runing_cb;
long long sleep_end_frameno = 0; long long sleep_end_frameno = 0;
int sleep_time = 0; int sleep_time = 0;
list_head entry; list_head entry;

View File

@ -255,8 +255,11 @@ behaviac::EBTStatus HeroAgent::RandomSafeZonePoint(int try_count, int step_len)
); );
auto co = std::make_shared<BtCoroutine>(context, co_id, "RandomSafeZonePoint"); auto co = std::make_shared<BtCoroutine>(context, co_id, "RandomSafeZonePoint");
co->runing_cb = co->runing_cb =
[this, context, try_count, step_len] () [this, context, try_count, step_len] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if (owner_->dead) { if (owner_->dead) {
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
@ -406,8 +409,11 @@ behaviac::EBTStatus HeroAgent::CoIdle(int min_val, int max_val)
context->time = a8::RandEx(min_val, max_val); context->time = a8::RandEx(min_val, max_val);
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoIdle"); auto co = std::make_shared<BtCoroutine>(context, co_id, "CoIdle");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if ((GetRoom()->GetFrameNo() - context->frameno) * FRAME_RATE_MS < if ((GetRoom()->GetFrameNo() - context->frameno) * FRAME_RATE_MS <
context->time) { context->time) {
return behaviac::BT_RUNNING; return behaviac::BT_RUNNING;
@ -426,8 +432,11 @@ behaviac::EBTStatus HeroAgent::CoMoveCurrentTargetRaycast()
); );
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoMoveCurrentTargetRaycast"); auto co = std::make_shared<BtCoroutine>(context, co_id, "CoMoveCurrentTargetRaycast");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if (!current_target_agent->IsValid()) { if (!current_target_agent->IsValid()) {
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
@ -450,8 +459,11 @@ behaviac::EBTStatus HeroAgent::CoShotCurrentTargetRaycast()
); );
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoShotCurrentTargetRaycast"); auto co = std::make_shared<BtCoroutine>(context, co_id, "CoShotCurrentTargetRaycast");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if (!current_target_agent->IsValid()) { if (!current_target_agent->IsValid()) {
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
@ -482,8 +494,11 @@ behaviac::EBTStatus HeroAgent::CoMoveMasterRaycast()
&owner_->xtimer_attacher); &owner_->xtimer_attacher);
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoMoveMasterRaycast"); auto co = std::make_shared<BtCoroutine>(context, co_id, "CoMoveMasterRaycast");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if (!context->timer_ptr.expired()) { if (!context->timer_ptr.expired()) {
return behaviac::BT_RUNNING; return behaviac::BT_RUNNING;
} else { } else {
@ -504,8 +519,11 @@ behaviac::EBTStatus HeroAgent::CoFindPath(const glm::vec3& pos)
context->target_pos = pos; context->target_pos = pos;
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoFindPath"); auto co = std::make_shared<BtCoroutine>(context, co_id, "CoFindPath");
co->runing_cb = co->runing_cb =
[this, context, pos] () [this, context, pos] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if (owner_->dead) { if (owner_->dead) {
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
@ -538,8 +556,11 @@ behaviac::EBTStatus HeroAgent::CoFindPathEx(const glm::vec3& pos, float distance
context->target_pos = pos; context->target_pos = pos;
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoFindPathEx"); auto co = std::make_shared<BtCoroutine>(context, co_id, "CoFindPathEx");
co->runing_cb = co->runing_cb =
[this, context, distance] () [this, context, distance] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if (owner_->dead) { if (owner_->dead) {
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
@ -570,8 +591,11 @@ behaviac::EBTStatus HeroAgent::CoStartMove(int min_val, int max_val)
); );
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoStartMove"); auto co = std::make_shared<BtCoroutine>(context, co_id, "CoStartMove");
co->runing_cb = co->runing_cb =
[this, context, distance] () [this, context, distance] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if (!owner_->dead) { if (!owner_->dead) {
owner_->GetMovement()->CalcTargetPos(distance); owner_->GetMovement()->CalcTargetPos(distance);
} }
@ -596,8 +620,11 @@ behaviac::EBTStatus HeroAgent::CoSleep(int time)
&owner_->xtimer_attacher); &owner_->xtimer_attacher);
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoSleep"); auto co = std::make_shared<BtCoroutine>(context, co_id, "CoSleep");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if (!context->timer_ptr.expired()) { if (!context->timer_ptr.expired()) {
return behaviac::BT_RUNNING; return behaviac::BT_RUNNING;
} else { } else {
@ -652,8 +679,11 @@ behaviac::EBTStatus HeroAgent::CoMoveForward(int min_val, int max_val)
owner_->GetMovement()->CalcTargetPos(distance); owner_->GetMovement()->CalcTargetPos(distance);
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoMoveForward"); auto co = std::make_shared<BtCoroutine>(context, co_id, "CoMoveForward");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if (owner_->GetMovement()->GetPathSize() <= 0) { if (owner_->GetMovement()->GetPathSize() <= 0) {
return behaviac::BT_SUCCESS; return behaviac::BT_SUCCESS;
} else { } else {
@ -945,8 +975,11 @@ behaviac::EBTStatus HeroAgent::CoGetRunAwayPoint()
); );
auto co = std::make_shared<BtCoroutine>(context, co_id, "CoGetRunAwayPoint"); auto co = std::make_shared<BtCoroutine>(context, co_id, "CoGetRunAwayPoint");
co->runing_cb = co->runing_cb =
[this, context] () [this, context] (BtCoroutine* co)
{ {
if (co->IsAbort()) {
return behaviac::BT_FAILURE;
}
if (owner_->dead) { if (owner_->dead) {
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }