game2006/server/gameserver/android_agent.cc
aozhiwei f4a4f60553 1
2023-04-07 19:09:51 +08:00

323 lines
11 KiB
C++

#include "precompile.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "android_agent.h"
#include "android.h"
#include "room.h"
#include "movement.h"
#include "trigger.h"
#include "glmhelper.h"
#include "mt/Robot.h"
AndroidAgent::AndroidAgent():BaseAgent()
{
}
AndroidAgent::~AndroidAgent()
{
}
State_e AndroidAgent::GetState()
{
#ifdef DEBUG1
a8::XPrintf("GetState\n", {});
#endif
return kPreBattle;
}
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;
}
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
(
CreatureWeakPtr owner;
CreatureWeakPtr last_attacker;
long long last_attacked_frameno = 0;
std::weak_ptr<EventHandlerPtr> handler;
);
context->owner = GetOwner()->GetWeakPtrRef();
context->handler = GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->last_attacker = c->GetWeakPtrRef();
context->last_attacked_frameno = c->room->GetFrameNo();
}
});
context->_destory_cb =
(
[context = context.get()] ()
{
if (context->owner.Get()) {
context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler);
}
});
auto co = std::make_shared<BtCoroutine>(context, "CoRandomWalk");
co->runing_cb =
[this, context] ()
{
if (GetOwner()->GetMovement()->GetPathSize() <= 0) {
return behaviac::BT_SUCCESS;
} else {
return behaviac::BT_RUNNING;
}
};
co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{
has_event = context->last_attacker.Get() && !context->last_attacker.Get()->dead ? true : false;
if (!is_test && has_event) {
FireEvent("OnAttacked",
context->last_attacker.Get()->GetUniId(),
context->last_attacked_frameno);
}
};
return StartCoroutine(co);
}
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(AdjustShotDir(shot_dir), shot_ok, 0, 0);
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
(
CreatureWeakPtr owner;
CreatureWeakPtr last_attacker;
long long last_attacked_frameno = 0;
long long last_frameno = 0;
std::weak_ptr<EventHandlerPtr> handler;
);
context->owner = GetOwner()->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo();
context->handler = GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args)
{
if (!context_wp.expired()) {
auto context = context_wp.lock();
Creature* c = args.Get<Creature*>(0);
context->last_attacker = c->GetWeakPtrRef();
context->last_attacked_frameno = c->room->GetFrameNo();
}
});
context->_destory_cb =
(
[context = context.get()] ()
{
if (context->owner.Get()) {
context->owner.Get()->GetTrigger()->RemoveEventHandler(context->handler);
}
});
auto co = std::make_shared<BtCoroutine>(context, "CoRandomShot");
co->runing_cb =
[this, context] ()
{
if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 3) {
GetOwner()->shot_hold = false;
return behaviac::BT_SUCCESS;
} else {
bool shot_ok = false;
glm::vec3 shot_dir = GetOwner()->GetAttackDir();
GetOwner()->shot_hold = true;
GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0);
return behaviac::BT_RUNNING;
}
};
co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{
has_event = context->last_attacker.Get() && !context->last_attacker.Get()->dead ? true : false;
if (!is_test && has_event) {
FireEvent("OnAttacked",
context->last_attacker.Get()->GetUniId(),
context->last_attacked_frameno);
}
};
return StartCoroutine(co);
}
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(AdjustShotDir(shot_dir), shot_ok, 0, 0);
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
(
CreatureWeakPtr owner;
long long last_frameno = 0;
);
context->owner = GetOwner()->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo();
auto co = std::make_shared<BtCoroutine>(context, "CoAttack");
co->runing_cb =
[this, context] ()
{
if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 3) {
status_ = behaviac::BT_SUCCESS;
GetOwner()->shot_hold = false;
return behaviac::BT_SUCCESS;
} else {
bool shot_ok = false;
glm::vec3 shot_dir = GetOwner()->GetAttackDir();
GetOwner()->shot_hold = true;
GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0);
return behaviac::BT_RUNNING;
}
};
co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{
};
return StartCoroutine(co);
}
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;
}
auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
(
CreatureWeakPtr owner;
CreatureWeakPtr target;
long long last_frameno = 0;
long long last_pursuit_frameno = 0;
);
context->owner = GetOwner()->GetWeakPtrRef();
context->target = enemy->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo();
context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
auto co = std::make_shared<BtCoroutine>(context, "CoPursuit");
co->runing_cb =
[this, context] ()
{
if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 10 ||
!context->target.Get() || context->target.Get()->dead) {
status_ = behaviac::BT_SUCCESS;
GetOwner()->shot_hold = false;
return behaviac::BT_SUCCESS;
} else {
glm::vec3 dir = GetOwner()->GetPos().CalcDir(context->target.Get()->GetPos());
if (GlmHelper::Norm(dir) <= 1.0f) {
GetOwner()->GetMovement()->CalcTargetPos(60);
context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
} else {
bool is_shot = false;
float distance = GlmHelper::Norm(dir) - GetAttackRange();
if (distance > 0.001f) {
if (GetOwner()->GetMovement()->GetPathSize() < 1) {
GlmHelper::Normalize(dir);
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
GetOwner()->GetMovement()->CalcTargetPos(distance + 60);
context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
} else {
if (GetOwner()->room->GetFrameNo() - context->last_pursuit_frameno > SERVER_FRAME_RATE * 1) {
GlmHelper::Normalize(dir);
GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir);
GetOwner()->GetMovement()->CalcTargetPos(distance + 60);
context->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()->shot_hold = true;
GetOwner()->SetAttackDir(shot_dir);
GetOwner()->Shot(AdjustShotDir(shot_dir), shot_ok, 0, 0);
} else {
GetOwner()->shot_hold = false;
}
}
return behaviac::BT_RUNNING;
}
};
co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{
};
return StartCoroutine(co);
}
glm::vec3& AndroidAgent::AdjustShotDir(glm::vec3& shot_dir)
{
if (GetOwner()->IsAndroid()) {
float angle_offset = GetOwner()->AsAndroid()->robot_meta->RandBulletAngleOfsset();
GlmHelper::RotateY(shot_dir, glm::radians(angle_offset));
GetOwner()->SetAttackDir(shot_dir);
}
return shot_dir;
}