game2006/server/gameserver/android_agent.cc
aozhiwei 9d2738630c 1
2022-12-07 11:35:33 +08:00

190 lines
5.2 KiB
C++

#include "precompile.h"
#include "android_agent.h"
#include "android.h"
#include "room.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());
if (!enemy) {
return behaviac::BT_FAILURE;
}
a8::Vec2 dir = enemy->GetPos() - GetOwner()->GetPos();
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::DoPursuit()
{
if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb();
}
Human* enemy = GetOwner()->room->FindEnemy(GetOwner()->AsHuman());
if (!enemy) {
return behaviac::BT_FAILURE;
}
a8::Vec2 dir = enemy->GetPos() - GetOwner()->GetPos();
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_;
}