This commit is contained in:
aozhiwei 2023-10-21 18:43:27 +08:00
parent bb70c6cffe
commit 720a3725e2
4 changed files with 100 additions and 96 deletions

View File

@ -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<BtCoroutine> 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);
}
}

View File

@ -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<void()> _destory_cb; \
long long frameno = 0; \
~Context() { if (_destory_cb) { _destory_cb(); };}; \
}; \
auto context = std::make_shared<Context>(); \
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<BtCoroutine> co);
void CheckCoroutineEvent();
protected:
std::map<int, std::shared_ptr<BtCoroutine>> coroutines_hash_;
list_head coroutines_list_;
};

View File

@ -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<TargetAgent>();
@ -648,13 +637,6 @@ void HeroAgent::UnSetFlag(int tag)
a8::UnSetBitFlag(flags_, tag);
}
behaviac::EBTStatus HeroAgent::StartCoroutine(std::shared_ptr<BtCoroutine> co)
{
coroutines_hash_[co->GetId()] = co;
list_add_tail(&coroutines_list_, &co->entry);
return behaviac::BT_RUNNING;
}
behaviac::EBTStatus HeroAgent::RegisterEvents(behaviac::vector<BtEvent_e> 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);
}
}

View File

@ -2,21 +2,6 @@
#include "base_agent.h"
#define MAKE_BTCONTEXT(...) \
[] (CreatureWeakPtr owner, BaseAgent* agent) \
{ \
class Context : public BtContext \
{public: \
__VA_ARGS__; \
std::function<void()> _destory_cb; \
long long frameno = 0; \
~Context() { if (_destory_cb) { _destory_cb(); };}; \
}; \
auto context = std::make_shared<Context>(); \
context->frameno = owner.Get()->room->GetFrameNo(); \
return context; \
}(owner_->GetWeakPtrRef(), this)
namespace a8
{
template<typename T>
@ -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<BtCoroutine> 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<int, int> dyn_hash_;
std::map<int, std::shared_ptr<BtCoroutine>> coroutines_hash_;
list_head coroutines_list_;
};