game2006/server/gameserver/android_agent.cc
aozhiwei d130f99cfd 1
2022-12-28 20:10:02 +08:00

310 lines
10 KiB
C++

#include "precompile.h"
#if 0
#include "android_agent.h"
#include "android.h"
#include "room.h"
#include "movement.h"
#include "trigger.h"
#include "glmhelper.h"
AndroidAgent::AndroidAgent():BaseAgent()
{
}
AndroidAgent::~AndroidAgent()
{
}
State_e AndroidAgent::GetState()
{
#ifdef DEBUG1
a8::XPrintf("GetState\n", {});
#endif
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);
a8::XTimerWp timer_ptr = GetOwner()->room->xtimer.SetTimeoutWpEx
(
idle_time / FRAME_RATE_MS,
[] (int event, const a8::Args* args)
{
},
&GetOwner()->xtimer_attacher);
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 a8::Args& args)
{
Creature* c = args.Get<Creature*>(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, timer_ptr, handler, last_attacker, last_attacked_frameno]
(bool is_test, bool& has_event) mutable
{
has_event = last_attacker->Get() && !last_attacker->Get()->dead ? true : false;
if (!is_test && has_event) {
if (!timer_ptr.expired()) {
GetOwner()->room->xtimer.Delete(timer_ptr);
}
if (!handler.expired()) {
GetOwner()->GetTrigger()->RemoveEventHandler(handler);
}
FireEvent("OnAttacked", last_attacker->Get()->GetUniId(), *last_attacked_frameno);
}
},
"CoIdle"
);
}
behaviac::EBTStatus AndroidAgent::DoRandomWalk()
{
if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb();
}
glm::vec3 dir = GetOwner()->GetMoveDir();
GlmHelper::RotateY(dir, (10 + rand() % 360)/ 180.0f);
GlmHelper::Normalize(dir);
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
GetOwner()->GetMovement()->CalcTargetPos(200);
if (GetOwner()->GetMovement()->GetPathSize() <= 0) {
return behaviac::BT_FAILURE;
}
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 a8::Args& args)
{
Creature* c = args.Get<Creature*>(0);
*last_attacker = c->GetWeakPtrRef();
*last_attacked_frameno = c->room->GetFrameNo();
});
return StartCoroutine
(
[this, handler] ()
{
if (GetOwner()->GetMovement()->GetPathSize() <= 0) {
if (!handler.expired()) {
GetOwner()->GetTrigger()->RemoveEventHandler(handler);
}
return behaviac::BT_SUCCESS;
} else {
return behaviac::BT_RUNNING;
}
},
[this, handler, last_attacker, last_attacked_frameno]
(bool is_test, bool& has_event) mutable
{
has_event = last_attacker->Get() && !last_attacker->Get()->dead ? true : false;
if (!is_test && has_event) {
if (!handler.expired()) {
GetOwner()->GetTrigger()->RemoveEventHandler(handler);
}
FireEvent("OnAttacked", last_attacker->Get()->GetUniId(), *last_attacked_frameno);
}
},
"CoRandomWalk"
);
}
behaviac::EBTStatus AndroidAgent::DoRandomShot()
{
if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb();
}
glm::vec3 dir = GetOwner()->GetMoveDir();
GlmHelper::RotateY(dir, (10 + rand() % 360)/ 180.0f);
GlmHelper::Normalize(dir);
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
bool shot_ok = false;
glm::vec3 shot_dir = dir;
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
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 a8::Args& args)
{
Creature* c = args.Get<Creature*>(0);
*last_attacker = c->GetWeakPtrRef();
*last_attacked_frameno = 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;
glm::vec3 shot_dir = GetOwner()->GetAttackDir();
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
return behaviac::BT_RUNNING;
}
},
[this, handler, last_attacker, last_attacked_frameno]
(bool is_test, bool& has_event) mutable
{
has_event = last_attacker->Get() && !last_attacker->Get()->dead ? true : false;
if (!is_test && has_event) {
if (!handler.expired()) {
GetOwner()->GetTrigger()->RemoveEventHandler(handler);
}
FireEvent("OnAttacked", last_attacker->Get()->GetUniId(), *last_attacked_frameno);
}
},
"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;
}
glm::vec3 dir = GetOwner()->GetPos().CalcDir(enemy->GetPos());
GlmHelper::Normalize(dir);
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
bool shot_ok = false;
glm::vec3 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;
glm::vec3 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 {
glm::vec3 dir = GetOwner()->GetPos().CalcDir(target.Get()->GetPos());
if (GlmHelper::Norm(dir) <= 1.0f) {
GetOwner()->GetMovement()->CalcTargetPos(60);
last_pursuit_frameno = GetOwner()->room->GetFrameNo();
} else {
bool is_shot = false;
if (GlmHelper::Norm(dir) > 300) {
if (GetOwner()->GetMovement()->GetPathSize() < 1) {
GlmHelper::Normalize(dir);
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
GetOwner()->GetMovement()->CalcTargetPos(200);
last_pursuit_frameno = GetOwner()->room->GetFrameNo();
} else {
if (GetOwner()->room->GetFrameNo() - last_pursuit_frameno > SERVER_FRAME_RATE * 1) {
GlmHelper::Normalize(dir);
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
GetOwner()->GetMovement()->CalcTargetPos(200);
last_pursuit_frameno = GetOwner()->room->GetFrameNo();
}
}
} else {
GlmHelper::Normalize(dir);
is_shot = true;
}
if (is_shot) {
bool shot_ok = false;
glm::vec3 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"
);
}
#endif