From 720a3725e2a84e9877166da2e06f692e0e1538db Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 21 Oct 2023 18:43:27 +0800 Subject: [PATCH] 1 --- server/gameserver/base_agent.cc | 63 +++++++++++++++++++++++++++++ server/gameserver/base_agent.h | 37 +++++++++++++++++ server/gameserver/hero_agent.cc | 72 --------------------------------- server/gameserver/hero_agent.h | 24 ----------- 4 files changed, 100 insertions(+), 96 deletions(-) diff --git a/server/gameserver/base_agent.cc b/server/gameserver/base_agent.cc index 08e4ae2e..ae1ab799 100644 --- a/server/gameserver/base_agent.cc +++ b/server/gameserver/base_agent.cc @@ -47,3 +47,66 @@ float BaseAgent::RandRangeAsFloat(float min_val, float max_val) { return RandRange(min_val, max_val); } + +behaviac::EBTStatus BaseAgent::StartCoroutine(std::shared_ptr co) +{ + coroutines_hash_[co->GetId()] = co; + list_add_tail(&coroutines_list_, &co->entry); + return behaviac::BT_RUNNING; +} + +bool BaseAgent::PreEnterCoroutine(int co_id, behaviac::EBTStatus& status) +{ + status = behaviac::BT_FAILURE; + if (coroutines_hash_.empty()) { + return false; + } + auto itr = coroutines_hash_.find(co_id); + if (itr == coroutines_hash_.end()) { + return false; + } + auto& co = itr->second; + if (co->status != behaviac::BT_RUNNING) { + abort(); + } + #if 0 + if (owner_->room->GetFrameNo() < co->sleep_end_frameno) { + status = behaviac::BT_RUNNING; + return true; + } + #endif + co->status = co->runing_cb(); + status = co->status; + if (co->status != behaviac::BT_RUNNING) { + list_del_init(&co->entry); + coroutines_hash_.erase(itr); + } + return true; +} + +void BaseAgent::CheckCoroutineEvent() +{ + int co_id = -1; + { + list_head* pos = nullptr; + list_head* next = nullptr; + list_head* head = &coroutines_list_; + list_for_each_safe(pos, next, head) { + BtCoroutine* co = list_entry(pos, BtCoroutine, entry); + if (co->GetContext()->HasEvent()) { + co_id = co->GetId(); + break; + } + } + } + if (co_id >= 0) { + auto itr = coroutines_hash_.find(co_id); + if (itr == coroutines_hash_.end()) { + abort(); + } + auto co = itr->second; + coroutines_hash_.clear(); + INIT_LIST_HEAD(&coroutines_list_); + co->GetContext()->FireEvent(this); + } +} diff --git a/server/gameserver/base_agent.h b/server/gameserver/base_agent.h index 33c07d7a..981766aa 100644 --- a/server/gameserver/base_agent.h +++ b/server/gameserver/base_agent.h @@ -5,7 +5,35 @@ #include "behaviac/behaviac.h" #include "behaviac_customized_types.h" +#include "btcoroutine.h" +#include "btcontext.h" + +#define PRE_ENTER_COROUTINE() \ + const int co_id = __LINE__; \ + { \ + behaviac::EBTStatus status; \ + if (PreEnterCoroutine(co_id, status)) { \ + return status; \ + } \ + } + +#define MAKE_BTCONTEXT(...) \ +[] (BaseAgent* agent) \ +{ \ +class Context : public BtContext \ +{public: \ + __VA_ARGS__; \ + std::function _destory_cb; \ + long long frameno = 0; \ + ~Context() { if (_destory_cb) { _destory_cb(); };}; \ +}; \ +auto context = std::make_shared(); \ +context->frameno = agent->GetRoom()->GetFrameNo(); \ +return context; \ +}(this) + class Room; +class BtCoroutine; class BaseAgent : public behaviac::Agent { public: @@ -24,4 +52,13 @@ public: bool IsMobaMode(); virtual Room* GetRoom() = 0; + +protected: + bool PreEnterCoroutine(int co_id, behaviac::EBTStatus& status); + behaviac::EBTStatus StartCoroutine(std::shared_ptr co); + void CheckCoroutineEvent(); + +protected: + std::map> coroutines_hash_; + list_head coroutines_list_; }; diff --git a/server/gameserver/hero_agent.cc b/server/gameserver/hero_agent.cc index c8db3692..cd6ba9ad 100644 --- a/server/gameserver/hero_agent.cc +++ b/server/gameserver/hero_agent.cc @@ -14,8 +14,6 @@ #include "mapinstance.h" #include "collision.h" #include "human.h" -#include "btcoroutine.h" -#include "btcontext.h" #include "team.h" #include "master_agent.h" #include "team_agent.h" @@ -25,15 +23,6 @@ #include "mt/Equip.h" #include "mt/Skill.h" -#define PRE_ENTER_COROUTINE() \ - const int co_id = __LINE__; \ - { \ - behaviac::EBTStatus status; \ - if (PreEnterCoroutine(co_id, status)) { \ - return status; \ - } \ - } - HeroAgent::HeroAgent():BaseAgent() { current_target_agent = behaviac::Agent::Create(); @@ -648,13 +637,6 @@ void HeroAgent::UnSetFlag(int tag) a8::UnSetBitFlag(flags_, tag); } -behaviac::EBTStatus HeroAgent::StartCoroutine(std::shared_ptr co) -{ - coroutines_hash_[co->GetId()] = co; - list_add_tail(&coroutines_list_, &co->entry); - return behaviac::BT_RUNNING; -} - behaviac::EBTStatus HeroAgent::RegisterEvents(behaviac::vector events) { return behaviac::BT_SUCCESS; @@ -914,57 +896,3 @@ bool HeroAgent::InternalUseSkill(int skill_id, int& wait_time) } return false; } - -bool HeroAgent::PreEnterCoroutine(int co_id, behaviac::EBTStatus& status) -{ - status = behaviac::BT_FAILURE; - if (coroutines_hash_.empty()) { - return false; - } - auto itr = coroutines_hash_.find(co_id); - if (itr == coroutines_hash_.end()) { - return false; - } - auto& co = itr->second; - if (co->status != behaviac::BT_RUNNING) { - abort(); - } - if (owner_->room->GetFrameNo() < co->sleep_end_frameno) { - status = behaviac::BT_RUNNING; - return true; - } - co->status = co->runing_cb(); - status = co->status; - if (co->status != behaviac::BT_RUNNING) { - list_del_init(&co->entry); - coroutines_hash_.erase(itr); - } - return true; -} - -void HeroAgent::CheckCoroutineEvent() -{ - int co_id = -1; - { - list_head* pos = nullptr; - list_head* next = nullptr; - list_head* head = &coroutines_list_; - list_for_each_safe(pos, next, head) { - BtCoroutine* co = list_entry(pos, BtCoroutine, entry); - if (co->GetContext()->HasEvent()) { - co_id = co->GetId(); - break; - } - } - } - if (co_id >= 0) { - auto itr = coroutines_hash_.find(co_id); - if (itr == coroutines_hash_.end()) { - abort(); - } - auto co = itr->second; - coroutines_hash_.clear(); - INIT_LIST_HEAD(&coroutines_list_); - co->GetContext()->FireEvent(this); - } -} diff --git a/server/gameserver/hero_agent.h b/server/gameserver/hero_agent.h index 423169d7..5485806b 100644 --- a/server/gameserver/hero_agent.h +++ b/server/gameserver/hero_agent.h @@ -2,21 +2,6 @@ #include "base_agent.h" -#define MAKE_BTCONTEXT(...) \ -[] (CreatureWeakPtr owner, BaseAgent* agent) \ -{ \ -class Context : public BtContext \ -{public: \ - __VA_ARGS__; \ - std::function _destory_cb; \ - long long frameno = 0; \ - ~Context() { if (_destory_cb) { _destory_cb(); };}; \ -}; \ -auto context = std::make_shared(); \ -context->frameno = owner.Get()->room->GetFrameNo(); \ -return context; \ -}(owner_->GetWeakPtrRef(), this) - namespace a8 { template @@ -31,7 +16,6 @@ class RoomAgent; class TeamAgent; class TargetAgent; class MasterAgent; -class BtCoroutine; class HeroAgent : public BaseAgent { public: @@ -139,19 +123,11 @@ public: glm::vec3 out_point2 = glm::vec3(0.0f, 0.0f, 0.0f); protected: - bool PreEnterCoroutine(int co_id, behaviac::EBTStatus& status); - behaviac::EBTStatus StartCoroutine(std::shared_ptr co); bool InternalUseSkill(int skill_id, int& wait_time); - void CheckCoroutineEvent(); - Creature* owner_ = nullptr; - private: bool bullet_trace_mode_ = false; long long flags_ = 0; std::map dyn_hash_; - - std::map> coroutines_hash_; - list_head coroutines_list_; };