From df7794d882a88f23c7e20901b68a4ab7a6f686e6 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 5 Apr 2023 14:08:18 +0800 Subject: [PATCH] 1 --- server/gameserver/hero_agent.cc | 150 +++++++++++++++++++++----------- 1 file changed, 97 insertions(+), 53 deletions(-) diff --git a/server/gameserver/hero_agent.cc b/server/gameserver/hero_agent.cc index e7f544aa..135bf4bf 100644 --- a/server/gameserver/hero_agent.cc +++ b/server/gameserver/hero_agent.cc @@ -9,6 +9,7 @@ #include "trigger.h" #include "glmhelper.h" #include "mapinstance.h" +#include "human.h" #include "mt/Map.h" @@ -387,27 +388,60 @@ behaviac::EBTStatus HeroAgent::DoPursuit() if (status_ == behaviac::BT_RUNNING) { return DoRunningCb(); } - Creature* enemy = GetOwner()->room->FindEnemy(GetOwner(), 500); + 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 + auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED ( - [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 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 || - !target.Get() || target.Get()->dead) { + if (context_wp.expired()) { + auto context = context_wp.lock(); + Creature* c = args.Get(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(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; return behaviac::BT_SUCCESS; } 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) { GetOwner()->GetMovement()->CalcTargetPos(60); - last_pursuit_frameno = GetOwner()->room->GetFrameNo(); + context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); } else { bool is_shot = false; if (GlmHelper::Norm(dir) > 300) { @@ -416,14 +450,14 @@ behaviac::EBTStatus HeroAgent::DoPursuit() GetOwner()->SetMoveDir(dir); GetOwner()->SetAttackDir(dir); GetOwner()->GetMovement()->CalcTargetPos(200); - last_pursuit_frameno = GetOwner()->room->GetFrameNo(); + context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); } 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); GetOwner()->SetMoveDir(dir); GetOwner()->SetAttackDir(dir); GetOwner()->GetMovement()->CalcTargetPos(200); - last_pursuit_frameno = GetOwner()->room->GetFrameNo(); + context->last_pursuit_frameno = GetOwner()->room->GetFrameNo(); } } } else { @@ -434,22 +468,18 @@ behaviac::EBTStatus HeroAgent::DoPursuit() bool shot_ok = false; glm::vec3 shot_dir = dir; GetOwner()->SetAttackDir(dir); - if (bullet_trace_mode_) { - GetOwner()->Shot(shot_dir, shot_ok, 0, 0); - } else { - GetOwner()->Shot(shot_dir, shot_ok, 0, 0); - } + GetOwner()->Shot(shot_dir, shot_ok, 0, 0); } } return behaviac::BT_RUNNING; } - }, - [this] (bool is_test, bool& has_event) + }; + co->event_cb = + [this, context] + (bool is_test, bool& has_event) mutable { - - }, - "CoPursuit" - ); + }; + return StartCoroutine(co); } 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); } - CreatureWeakPtr target = enemy->GetWeakPtrRef(); - long long last_frameno = GetOwner()->room->GetFrameNo(); - return StartCoroutine + auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED ( - [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(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; return behaviac::BT_SUCCESS; } else { - if (!target.Get()) { + if (!context->target.Get()) { return behaviac::BT_SUCCESS; } - if (target.Get()->dead) { + if (context->target.Get()->dead) { return behaviac::BT_SUCCESS; } if (GlmHelper::IsEqual2D(GetOwner()->GetPos().ToGlmVec3(), - target.Get()->GetPos().ToGlmVec3())) { + context->target.Get()->GetPos().ToGlmVec3())) { return behaviac::BT_FAILURE; } - float distance = target.Get()->GetPos().Distance2D2(GetOwner()->GetPos()); + float distance = context->target.Get()->GetPos().Distance2D2(GetOwner()->GetPos()); if (distance > GetAttackRange()) { return behaviac::BT_FAILURE; } 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); GetOwner()->SetAttackDir(shot_dir); 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 { GetOwner()->Shot(shot_dir, shot_ok, 0, 0); } return behaviac::BT_RUNNING; } - }, - [this] (bool is_test, bool& has_event) + }; + co->event_cb = + [this, context] + (bool is_test, bool& has_event) mutable { - - }, - "CoHelpAttack" - ); + }; + return StartCoroutine(co); } behaviac::EBTStatus HeroAgent::DoFlyToMasterAround() @@ -613,13 +650,20 @@ behaviac::EBTStatus HeroAgent::DoFollowMaster() return behaviac::BT_FAILURE; } - long long last_frameno = GetOwner()->room->GetFrameNo(); - long long last_follow_frameno = GetOwner()->room->GetFrameNo(); - return StartCoroutine + auto context = A8_MAKE_SMART_ANON_STRUCT_SHARED ( - [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(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) { status_ = behaviac::BT_SUCCESS; return behaviac::BT_SUCCESS; @@ -637,17 +681,17 @@ behaviac::EBTStatus HeroAgent::DoFollowMaster() GetOwner()->SetMoveDir(dir); GetOwner()->SetAttackDir(dir); GetOwner()->GetMovement()->CalcTargetPos(60); - last_follow_frameno = GetOwner()->room->GetFrameNo(); + context->last_follow_frameno = GetOwner()->room->GetFrameNo(); } return behaviac::BT_RUNNING; } - }, - [this] (bool is_test, bool& has_event) + }; + co->event_cb = + [this, context] + (bool is_test, bool& has_event) mutable { - - }, - "CoFollowMaster" - ); + }; + return StartCoroutine(co); } float HeroAgent::GetMasterDistance()