game2006/server/gameserver/android_agent.cc
aozhiwei 96ed1ff9ef 1
2022-12-09 11:47:21 +08:00

279 lines
8.8 KiB
C++

#include "precompile.h"
#include "android_agent.h"
#include "android.h"
#include "room.h"
#include "movehelper.h"
#include "trigger.h"
AndroidAgent::AndroidAgent():BaseAgent()
{
}
AndroidAgent::~AndroidAgent()
{
}
State_e AndroidAgent::GetState()
{
return kPreBattle;
}
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
(
idle_time / FRAME_RATE_MS,
a8::XParams(),
[] (const a8::XParams& param)
{
},
&GetOwner()->xtimer_attacher.timer_list_);
std::weak_ptr<a8::XTimerPtr> timer_ptr = GetOwner()->room->xtimer.GetTimerPtr(timer);
std::shared_ptr<CreatureWeakPtr> last_attacker = std::make_shared<CreatureWeakPtr>();
std::shared_ptr<long long> last_attacked_frameno = std::make_shared<long long>(0);
std::weak_ptr<EventHandlerPtr> handler = GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[this, last_attacker, last_attacked_frameno] (const std::vector<std::any>& args)
{
Creature* c = std::any_cast<Creature*>(args.at(0));
*last_attacker = c->GetWeakPtrRef();
*last_attacked_frameno = 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;
}
},
[this, last_attacker, last_attacked_frameno] (bool is_test, bool& has_event)
{
has_event = last_attacker->Get() ? true : false;
if (!is_test && has_event) {
FireEvent("OnAttacked", last_attacker->Get()->GetUniId(), *last_attacked_frameno);
}
},
"CoIdle"
);
}
behaviac::EBTStatus AndroidAgent::DoRandomWalk()
{
if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb();
}
a8::Vec2 dir = GetOwner()->GetMoveDir();
dir.Rotate((10 + rand() % 360)/ 180.0f);
dir.Normalize();
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
GetOwner()->GetMoveHelper()->CalcTargetPos(200);
if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) {
return behaviac::BT_FAILURE;
}
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, handler] ()
{
if (GetOwner()->GetMoveHelper()->GetPathSize() <= 0) {
if (!handler.expired()) {
GetOwner()->GetTrigger()->RemoveEventHandler(handler);
}
return behaviac::BT_SUCCESS;
} else {
return behaviac::BT_RUNNING;
}
},
[this] (bool is_test, bool& has_event)
{
},
"CoRandomWalk"
);
}
behaviac::EBTStatus AndroidAgent::DoRandomShot()
{
if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb();
}
a8::Vec2 dir = GetOwner()->GetMoveDir();
dir.Rotate((10 + rand() % 360)/ 180.0f);
dir.Normalize();
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
bool shot_ok = false;
a8::Vec2 shot_dir = dir;
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
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());
});
long long last_frameno = GetOwner()->room->GetFrameNo();
return StartCoroutine
(
[this, last_frameno, handler] ()
{
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) {
if (!handler.expired()) {
GetOwner()->GetTrigger()->RemoveEventHandler(handler);
}
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;
}
},
[this] (bool is_test, bool& has_event)
{
},
"CoRandomShot"
);
}
behaviac::EBTStatus AndroidAgent::DoAttack()
{
if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb();
}
Human* enemy = GetOwner()->room->FindEnemy(GetOwner()->AsHuman(), 300);
if (!enemy) {
return behaviac::BT_FAILURE;
}
a8::Vec2 dir = enemy->GetPos() - GetOwner()->GetPos();
dir.Normalize();
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
bool shot_ok = false;
a8::Vec2 shot_dir = dir;
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
long long last_frameno = GetOwner()->room->GetFrameNo();
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;
}
},
[this] (bool is_test, bool& has_event)
{
},
"CoAttack"
);
}
behaviac::EBTStatus AndroidAgent::DoPursuit()
{
if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb();
}
Human* enemy = GetOwner()->room->FindEnemy(GetOwner()->AsHuman(), 500);
if (!enemy) {
return behaviac::BT_FAILURE;
}
CreatureWeakPtr target = enemy->GetWeakPtrRef();
long long last_frameno = GetOwner()->room->GetFrameNo();
long long last_pursuit_frameno = GetOwner()->room->GetFrameNo();
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;
}
},
[this] (bool is_test, bool& has_event)
{
},
"CoPursuit"
);
}