This commit is contained in:
aozhiwei 2023-04-05 14:08:18 +08:00
parent 8cca25ad24
commit df7794d882

View File

@ -9,6 +9,7 @@
#include "trigger.h" #include "trigger.h"
#include "glmhelper.h" #include "glmhelper.h"
#include "mapinstance.h" #include "mapinstance.h"
#include "human.h"
#include "mt/Map.h" #include "mt/Map.h"
@ -387,27 +388,60 @@ behaviac::EBTStatus HeroAgent::DoPursuit()
if (status_ == behaviac::BT_RUNNING) { if (status_ == behaviac::BT_RUNNING) {
return DoRunningCb(); return DoRunningCb();
} }
Creature* enemy = GetOwner()->room->FindEnemy(GetOwner(), 500); Human* enemy = GetOwner()->room->FindEnemy(GetOwner()->AsHuman(), 500);
if (!enemy) { if (!enemy) {
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
CreatureWeakPtr target = enemy->GetWeakPtrRef(); auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
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 CreatureWeakPtr owner;
CreatureWeakPtr target;
CreatureWeakPtr last_attacker;
long long last_attacked_frameno = 0;
long long last_frameno = 0;
long long last_pursuit_frameno = 0;
std::weak_ptr<EventHandlerPtr> handler;
);
context->owner = GetOwner()->GetWeakPtrRef();
context->target = enemy->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo();
context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
context->handler = GetOwner()->GetTrigger()->AddListener
(
kAttacked,
[context_wp = context->GetWp()] (const a8::Args& args)
{ {
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 10 || if (context_wp.expired()) {
!target.Get() || target.Get()->dead) { 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, "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; status_ = behaviac::BT_SUCCESS;
return behaviac::BT_SUCCESS; return behaviac::BT_SUCCESS;
} else { } else {
glm::vec3 dir = GetOwner()->GetPos().CalcDir(target.Get()->GetPos()); glm::vec3 dir = GetOwner()->GetPos().CalcDir(context->target.Get()->GetPos());
if (GlmHelper::Norm(dir) <= 1.0f) { if (GlmHelper::Norm(dir) <= 1.0f) {
GetOwner()->GetMovement()->CalcTargetPos(60); GetOwner()->GetMovement()->CalcTargetPos(60);
last_pursuit_frameno = GetOwner()->room->GetFrameNo(); context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
} else { } else {
bool is_shot = false; bool is_shot = false;
if (GlmHelper::Norm(dir) > 300) { if (GlmHelper::Norm(dir) > 300) {
@ -416,14 +450,14 @@ behaviac::EBTStatus HeroAgent::DoPursuit()
GetOwner()->SetMoveDir(dir); GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir); GetOwner()->SetAttackDir(dir);
GetOwner()->GetMovement()->CalcTargetPos(200); GetOwner()->GetMovement()->CalcTargetPos(200);
last_pursuit_frameno = GetOwner()->room->GetFrameNo(); context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
} else { } else {
if (GetOwner()->room->GetFrameNo() - last_pursuit_frameno > SERVER_FRAME_RATE * 1) { if (GetOwner()->room->GetFrameNo() - context->last_pursuit_frameno > SERVER_FRAME_RATE * 1) {
GlmHelper::Normalize(dir); GlmHelper::Normalize(dir);
GetOwner()->SetMoveDir(dir); GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir); GetOwner()->SetAttackDir(dir);
GetOwner()->GetMovement()->CalcTargetPos(200); GetOwner()->GetMovement()->CalcTargetPos(200);
last_pursuit_frameno = GetOwner()->room->GetFrameNo(); context->last_pursuit_frameno = GetOwner()->room->GetFrameNo();
} }
} }
} else { } else {
@ -434,22 +468,18 @@ behaviac::EBTStatus HeroAgent::DoPursuit()
bool shot_ok = false; bool shot_ok = false;
glm::vec3 shot_dir = dir; glm::vec3 shot_dir = dir;
GetOwner()->SetAttackDir(dir); GetOwner()->SetAttackDir(dir);
if (bullet_trace_mode_) { GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
} else {
GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
}
} }
} }
return behaviac::BT_RUNNING; return behaviac::BT_RUNNING;
} }
}, };
[this] (bool is_test, bool& has_event) co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{ {
};
}, return StartCoroutine(co);
"CoPursuit"
);
} }
behaviac::EBTStatus HeroAgent::DoHelpAttack(int target_uniid) behaviac::EBTStatus HeroAgent::DoHelpAttack(int target_uniid)
@ -483,49 +513,56 @@ behaviac::EBTStatus HeroAgent::DoHelpAttack(int target_uniid)
GetOwner()->Shot(shot_dir, shot_ok, 0, 0); GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
} }
CreatureWeakPtr target = enemy->GetWeakPtrRef(); auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
long long last_frameno = GetOwner()->room->GetFrameNo();
return StartCoroutine
( (
[this, last_frameno, target] () mutable CreatureWeakPtr target;
long long last_frameno = 0;
);
context->target = enemy->GetWeakPtrRef();
context->last_frameno = GetOwner()->room->GetFrameNo();
auto co = std::make_shared<BtCoroutine>(context, "CoHelpAttack");
co->runing_cb =
[this, context] ()
{ {
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 3) { if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 3) {
status_ = behaviac::BT_SUCCESS; status_ = behaviac::BT_SUCCESS;
return behaviac::BT_SUCCESS; return behaviac::BT_SUCCESS;
} else { } else {
if (!target.Get()) { if (!context->target.Get()) {
return behaviac::BT_SUCCESS; return behaviac::BT_SUCCESS;
} }
if (target.Get()->dead) { if (context->target.Get()->dead) {
return behaviac::BT_SUCCESS; return behaviac::BT_SUCCESS;
} }
if (GlmHelper::IsEqual2D(GetOwner()->GetPos().ToGlmVec3(), if (GlmHelper::IsEqual2D(GetOwner()->GetPos().ToGlmVec3(),
target.Get()->GetPos().ToGlmVec3())) { context->target.Get()->GetPos().ToGlmVec3())) {
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
float distance = target.Get()->GetPos().Distance2D2(GetOwner()->GetPos()); float distance = context->target.Get()->GetPos().Distance2D2(GetOwner()->GetPos());
if (distance > GetAttackRange()) { if (distance > GetAttackRange()) {
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
bool shot_ok = false; bool shot_ok = false;
glm::vec3 shot_dir = GetOwner()->GetPos().CalcDir(target.Get()->GetPos());; glm::vec3 shot_dir = GetOwner()->GetPos().CalcDir(context->target.Get()->GetPos());;
GlmHelper::Normalize(shot_dir); GlmHelper::Normalize(shot_dir);
GetOwner()->SetAttackDir(shot_dir); GetOwner()->SetAttackDir(shot_dir);
if (bullet_trace_mode_) { if (bullet_trace_mode_) {
GetOwner()->Shot(shot_dir, shot_ok, 0, target.Get()->GetUniId()); GetOwner()->Shot(shot_dir, shot_ok, 0, context->target.Get()->GetUniId());
} else { } else {
GetOwner()->Shot(shot_dir, shot_ok, 0, 0); GetOwner()->Shot(shot_dir, shot_ok, 0, 0);
} }
return behaviac::BT_RUNNING; return behaviac::BT_RUNNING;
} }
}, };
[this] (bool is_test, bool& has_event) co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{ {
};
}, return StartCoroutine(co);
"CoHelpAttack"
);
} }
behaviac::EBTStatus HeroAgent::DoFlyToMasterAround() behaviac::EBTStatus HeroAgent::DoFlyToMasterAround()
@ -613,13 +650,20 @@ behaviac::EBTStatus HeroAgent::DoFollowMaster()
return behaviac::BT_FAILURE; return behaviac::BT_FAILURE;
} }
long long last_frameno = GetOwner()->room->GetFrameNo(); auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED
long long last_follow_frameno = GetOwner()->room->GetFrameNo();
return StartCoroutine
( (
[this, last_frameno, last_follow_frameno] () mutable long long last_frameno = 0;
long long last_follow_frameno = 0;
);
context->last_frameno = GetOwner()->room->GetFrameNo();
context->last_follow_frameno = GetOwner()->room->GetFrameNo();
auto co = std::make_shared<BtCoroutine>(context, "CoFollowMaster");
co->runing_cb =
[this, context] ()
{ {
if (GetOwner()->room->GetFrameNo() - last_frameno > SERVER_FRAME_RATE * 10 || if (GetOwner()->room->GetFrameNo() - context->last_frameno > SERVER_FRAME_RATE * 10 ||
!GetOwner()->AsHero()->master.Get() || GetOwner()->AsHero()->master.Get()->dead) { !GetOwner()->AsHero()->master.Get() || GetOwner()->AsHero()->master.Get()->dead) {
status_ = behaviac::BT_SUCCESS; status_ = behaviac::BT_SUCCESS;
return behaviac::BT_SUCCESS; return behaviac::BT_SUCCESS;
@ -637,17 +681,17 @@ behaviac::EBTStatus HeroAgent::DoFollowMaster()
GetOwner()->SetMoveDir(dir); GetOwner()->SetMoveDir(dir);
GetOwner()->SetAttackDir(dir); GetOwner()->SetAttackDir(dir);
GetOwner()->GetMovement()->CalcTargetPos(60); GetOwner()->GetMovement()->CalcTargetPos(60);
last_follow_frameno = GetOwner()->room->GetFrameNo(); context->last_follow_frameno = GetOwner()->room->GetFrameNo();
} }
return behaviac::BT_RUNNING; return behaviac::BT_RUNNING;
} }
}, };
[this] (bool is_test, bool& has_event) co->event_cb =
[this, context]
(bool is_test, bool& has_event) mutable
{ {
};
}, return StartCoroutine(co);
"CoFollowMaster"
);
} }
float HeroAgent::GetMasterDistance() float HeroAgent::GetMasterDistance()