This commit is contained in:
aozhiwei 2022-12-08 19:28:18 +08:00
parent eb291bdc54
commit 5cf7397ddf
3 changed files with 142 additions and 107 deletions

View File

@ -4,6 +4,7 @@
#include "android.h" #include "android.h"
#include "room.h" #include "room.h"
#include "movehelper.h" #include "movehelper.h"
#include "trigger.h"
AndroidAgent::AndroidAgent():BaseAgent() AndroidAgent::AndroidAgent():BaseAgent()
{ {
@ -24,6 +25,7 @@ behaviac::EBTStatus AndroidAgent::DoIdle(int min_time, int max_time)
if (status_ == behaviac::BT_RUNNING) { if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb(); return DoRunningCb();
} }
int idle_time = a8::RandEx(min_time, max_time); int idle_time = a8::RandEx(min_time, max_time);
xtimer_list* timer = GetOwner()->room->xtimer.AddDeadLineTimerAndAttach xtimer_list* timer = GetOwner()->room->xtimer.AddDeadLineTimerAndAttach
( (
@ -34,18 +36,31 @@ behaviac::EBTStatus AndroidAgent::DoIdle(int min_time, int max_time)
}, },
&GetOwner()->xtimer_attacher.timer_list_); &GetOwner()->xtimer_attacher.timer_list_);
std::weak_ptr<a8::XTimerPtr> timer_ptr = GetOwner()->room->xtimer.GetTimerPtr(timer); std::weak_ptr<a8::XTimerPtr> timer_ptr = GetOwner()->room->xtimer.GetTimerPtr(timer);
status_runing_cb_ =
[this, timer_ptr] () std::weak_ptr<EventHandlerPtr> handler = GetOwner()->GetTrigger()->AddListener
{ (
if (timer_ptr.lock()) { kAttacked,
return behaviac::BT_RUNNING; [this] (const std::vector<std::any>& args)
} else { {
status_ = behaviac::BT_SUCCESS; Creature* c = std::any_cast<Creature*>(args.at(0));
return behaviac::BT_SUCCESS; FireEvent("OnAttacked", c->GetUniId(), c->room->GetFrameNo());
} });
};
status_ = behaviac::BT_RUNNING; return StartCoroutine
return status_; (
[this, timer_ptr, handler] ()
{
if (!timer_ptr.expired()) {
return behaviac::BT_RUNNING;
} else {
if (!handler.expired()) {
GetOwner()->GetTrigger()->RemoveEventHandler(handler);
}
return behaviac::BT_SUCCESS;
}
},
"CoIdle"
);
} }
behaviac::EBTStatus AndroidAgent::DoRandomWalk() behaviac::EBTStatus AndroidAgent::DoRandomWalk()
@ -63,25 +78,28 @@ behaviac::EBTStatus AndroidAgent::DoRandomWalk()
if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) { if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) {
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
status_runing_cb_ =
[this] () return StartCoroutine
{ (
if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) { [this] ()
status_ = behaviac::BT_SUCCESS; {
return behaviac::BT_SUCCESS; if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) {
} else { status_ = behaviac::BT_SUCCESS;
return behaviac::BT_RUNNING; return behaviac::BT_SUCCESS;
} } else {
}; return behaviac::BT_RUNNING;
status_ = behaviac::BT_RUNNING; }
return status_; },
"CoRandomWalk"
);
} }
behaviac::EBTStatus AndroidAgent::DoRandomShot() behaviac::EBTStatus AndroidAgent::DoRandomShot()
{ {
if (status_ == behaviac::BT_RUNNING) { if (status_ == behaviac::BT_RUNNING) {
return status_runing_cb_(); return DoRunningCb();
} }
a8::Vec2 dir = GetOwner()->GetMoveDir(); a8::Vec2 dir = GetOwner()->GetMoveDir();
dir.Rotate((10 + rand() % 360)/ 180.0f); dir.Rotate((10 + rand() % 360)/ 180.0f);
dir.Normalize(); dir.Normalize();
@ -91,24 +109,25 @@ behaviac::EBTStatus AndroidAgent::DoRandomShot()
a8::Vec2 shot_dir = dir; a8::Vec2 shot_dir = dir;
GetOwner()->Shot(shot_dir, shot_ok, 0, 0); GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
long long last_frameno = GetOwner()->room->GetFrameNo(); long long last_frameno = GetOwner()->room->GetFrameNo();
status_runing_cb_ = return StartCoroutine
[this, last_frameno] () (
{ [this, last_frameno] ()
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) { {
status_ = behaviac::BT_SUCCESS; if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) {
return behaviac::BT_SUCCESS; status_ = behaviac::BT_SUCCESS;
} else { return behaviac::BT_SUCCESS;
bool shot_ok = false; } else {
a8::Vec2 shot_dir = GetOwner()->GetAttackDir(); bool shot_ok = false;
GetOwner()->Shot(shot_dir, shot_ok, 0, 0); a8::Vec2 shot_dir = GetOwner()->GetAttackDir();
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
return behaviac::BT_RUNNING; return behaviac::BT_RUNNING;
} }
}; },
"CoRandomShot"
status_ = behaviac::BT_RUNNING; );
return status_;
} }
behaviac::EBTStatus AndroidAgent::DoAttack() behaviac::EBTStatus AndroidAgent::DoAttack()
@ -130,23 +149,23 @@ behaviac::EBTStatus AndroidAgent::DoAttack()
GetOwner()->Shot(shot_dir, shot_ok, 0, 0); GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
long long last_frameno = GetOwner()->room->GetFrameNo(); long long last_frameno = GetOwner()->room->GetFrameNo();
status_runing_cb_ = return StartCoroutine
[this, last_frameno] () (
{ [this, last_frameno] ()
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) { {
status_ = behaviac::BT_SUCCESS; if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) {
return behaviac::BT_SUCCESS; status_ = behaviac::BT_SUCCESS;
} else { return behaviac::BT_SUCCESS;
bool shot_ok = false; } else {
a8::Vec2 shot_dir = GetOwner()->GetAttackDir(); bool shot_ok = false;
GetOwner()->Shot(shot_dir, shot_ok, 0, 0); a8::Vec2 shot_dir = GetOwner()->GetAttackDir();
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
return behaviac::BT_RUNNING; return behaviac::BT_RUNNING;
} }
}; },
"CoAttack"
status_ = behaviac::BT_RUNNING; );
return status_;
} }
behaviac::EBTStatus AndroidAgent::DoPursuit() behaviac::EBTStatus AndroidAgent::DoPursuit()
@ -162,51 +181,51 @@ behaviac::EBTStatus AndroidAgent::DoPursuit()
CreatureWeakPtr target = enemy->GetWeakPtrRef(); CreatureWeakPtr target = enemy->GetWeakPtrRef();
long long last_frameno = GetOwner()->room->GetFrameNo(); long long last_frameno = GetOwner()->room->GetFrameNo();
long long last_pursuit_frameno = GetOwner()->room->GetFrameNo(); long long last_pursuit_frameno = GetOwner()->room->GetFrameNo();
status_runing_cb_ = return StartCoroutine
[this, last_frameno, target, last_pursuit_frameno] () mutable (
{ [this, last_frameno, target, last_pursuit_frameno] () mutable
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 10 || {
!target.Get() || target.Get()->dead) { if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 10 ||
status_ = behaviac::BT_SUCCESS; !target.Get() || target.Get()->dead) {
return behaviac::BT_SUCCESS; status_ = behaviac::BT_SUCCESS;
} else { return behaviac::BT_SUCCESS;
a8::Vec2 dir = target.Get()->GetPos() - GetOwner()->GetPos(); } else {
if (dir.Norm() <= 1.0f) { a8::Vec2 dir = target.Get()->GetPos() - GetOwner()->GetPos();
GetOwner()->GetMoveHelper()->CalcTargetPos(60); if (dir.Norm() <= 1.0f) {
last_pursuit_frameno = GetOwner()->room->GetFrameNo(); GetOwner()->GetMoveHelper()->CalcTargetPos(60);
} else { last_pursuit_frameno = GetOwner()->room->GetFrameNo();
bool is_shot = false; } else {
if (dir.Norm() > 300) { bool is_shot = false;
if (GetOwner()->GetMoveHelper()->GetPathSize() < 1) { if (dir.Norm() > 300) {
dir.Normalize(); if (GetOwner()->GetMoveHelper()->GetPathSize() < 1) {
GetOwner()->SetMoveDir(dir); dir.Normalize();
GetOwner()->SetAttackDir(dir); GetOwner()->SetMoveDir(dir);
GetOwner()->GetMoveHelper()->CalcTargetPos(200); GetOwner()->SetAttackDir(dir);
last_pursuit_frameno = GetOwner()->room->GetFrameNo(); GetOwner()->GetMoveHelper()->CalcTargetPos(200);
} else { last_pursuit_frameno = GetOwner()->room->GetFrameNo();
if (GetOwner()->room->GetFrameNo() - last_pursuit_frameno > SERVER_FRAME_RATE * 1) { } else {
dir.Normalize(); if (GetOwner()->room->GetFrameNo() - last_pursuit_frameno > SERVER_FRAME_RATE * 1) {
GetOwner()->SetMoveDir(dir); dir.Normalize();
GetOwner()->SetAttackDir(dir); GetOwner()->SetMoveDir(dir);
GetOwner()->GetMoveHelper()->CalcTargetPos(200); GetOwner()->SetAttackDir(dir);
last_pursuit_frameno = GetOwner()->room->GetFrameNo(); GetOwner()->GetMoveHelper()->CalcTargetPos(200);
} last_pursuit_frameno = GetOwner()->room->GetFrameNo();
} }
} else { }
dir.Normalize(); } else {
is_shot = true; dir.Normalize();
} is_shot = true;
if (is_shot) { }
bool shot_ok = false; if (is_shot) {
a8::Vec2 shot_dir = dir; bool shot_ok = false;
GetOwner()->SetAttackDir(dir); a8::Vec2 shot_dir = dir;
GetOwner()->Shot(shot_dir, shot_ok, 0, 0); GetOwner()->SetAttackDir(dir);
} GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
} }
return behaviac::BT_RUNNING; }
} return behaviac::BT_RUNNING;
}; }
},
status_ = behaviac::BT_RUNNING; "CoPursuit"
return status_; );
} }

View File

@ -32,14 +32,24 @@ behaviac::EBTStatus BaseAgent::DoRunningCb()
abort(); abort();
} }
status_ = status_runing_cb_(); status_ = status_runing_cb_();
#ifdef DEBUG
if ((GetOwner()->room->GetFrameNo() - status_frameno_) % SERVER_FRAME_RATE == 0) {
a8::XPrintf("Running Status:%s %d\n", {status_name_, status_});
}
#endif
if (status_ != behaviac::BT_RUNNING) { if (status_ != behaviac::BT_RUNNING) {
status_runing_cb_ = nullptr; status_runing_cb_ = nullptr;
} }
return status_; return status_;
} }
behaviac::EBTStatus BaseAgent::StartCoroutine(std::function<behaviac::EBTStatus()> cb) behaviac::EBTStatus BaseAgent::StartCoroutine(std::function<behaviac::EBTStatus()> cb,
const char* name)
{ {
#ifdef DEBUG
status_frameno_ = GetOwner()->room->GetFrameNo();
status_name_ = name;
#endif
status_runing_cb_ = std::move(cb); status_runing_cb_ = std::move(cb);
status_ = behaviac::BT_RUNNING; status_ = behaviac::BT_RUNNING;
return status_; return status_;
@ -104,6 +114,7 @@ behaviac::EBTStatus BaseAgent::CoAttackTarget(int target_id)
} }
} }
return behaviac::BT_RUNNING; return behaviac::BT_RUNNING;
} },
"CoAttackTarget"
); );
} }

View File

@ -24,9 +24,14 @@ public:
protected: protected:
behaviac::EBTStatus DoRunningCb(); behaviac::EBTStatus DoRunningCb();
behaviac::EBTStatus StartCoroutine(std::function<behaviac::EBTStatus()> cb); behaviac::EBTStatus StartCoroutine(std::function<behaviac::EBTStatus()> cb,
const char* name);
protected: protected:
#ifdef DEBUG
long long status_frameno_ = 0;
const char* status_name_ = nullptr;
#endif
behaviac::EBTStatus status_= behaviac::BT_SUCCESS; behaviac::EBTStatus status_= behaviac::BT_SUCCESS;
std::function<behaviac::EBTStatus()> status_runing_cb_; std::function<behaviac::EBTStatus()> status_runing_cb_;