game2006/server/gameserver/android_agent.cc
aozhiwei d1772dd4b7 1
2022-12-07 16:01:16 +08:00

213 lines
6.7 KiB
C++

#include "precompile.h"
#include "android_agent.h"
#include "android.h"
#include "room.h"
#include "movehelper.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);
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_;
}
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;
}
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_;
}
behaviac::EBTStatus AndroidAgent::DoRandomShot()
{
if (status_ == behaviac::BT_RUNNING) {
return status_runing_cb_();
}
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);
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 behaviac::BT_RUNNING;
}
};
status_ = behaviac::BT_RUNNING;
return status_;
}
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();
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 behaviac::BT_RUNNING;
}
};
status_ = behaviac::BT_RUNNING;
return status_;
}
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();
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_;
}