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

View File

@ -32,14 +32,24 @@ behaviac::EBTStatus BaseAgent::DoRunningCb()
abort();
}
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) {
status_runing_cb_ = nullptr;
}
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_ = behaviac::BT_RUNNING;
return status_;
@ -104,6 +114,7 @@ behaviac::EBTStatus BaseAgent::CoAttackTarget(int target_id)
}
}
return behaviac::BT_RUNNING;
}
},
"CoAttackTarget"
);
}

View File

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