1
This commit is contained in:
parent
0eca84937a
commit
ab8db13131
@ -10,6 +10,9 @@
|
|||||||
#include "trigger.h"
|
#include "trigger.h"
|
||||||
#include "glmhelper.h"
|
#include "glmhelper.h"
|
||||||
#include "skill.h"
|
#include "skill.h"
|
||||||
|
#include "btcontext.h"
|
||||||
|
#include "btevent.h"
|
||||||
|
#include "btcoroutine.h"
|
||||||
|
|
||||||
#include "mt/Robot.h"
|
#include "mt/Robot.h"
|
||||||
|
|
||||||
|
@ -8,6 +8,9 @@
|
|||||||
#include "glmhelper.h"
|
#include "glmhelper.h"
|
||||||
#include "trigger.h"
|
#include "trigger.h"
|
||||||
#include "skill.h"
|
#include "skill.h"
|
||||||
|
#include "btcontext.h"
|
||||||
|
#include "btevent.h"
|
||||||
|
#include "btcoroutine.h"
|
||||||
|
|
||||||
#include "f8/btmgr.h"
|
#include "f8/btmgr.h"
|
||||||
|
|
||||||
@ -22,74 +25,6 @@ static auto SpToWp(std::shared_ptr<T> sp)
|
|||||||
return std::weak_ptr<T>(sp);
|
return std::weak_ptr<T>(sp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtContext::AddHandler(CreatureWeakPtr target, std::weak_ptr<EventHandlerPtr> handler)
|
|
||||||
{
|
|
||||||
handlers.push_back(std::make_tuple(target, handler));
|
|
||||||
}
|
|
||||||
|
|
||||||
BtContext::~BtContext()
|
|
||||||
{
|
|
||||||
for (auto& tuple : handlers) {
|
|
||||||
if (std::get<0>(tuple).Get() && !std::get<1>(tuple).expired()) {
|
|
||||||
std::get<0>(tuple).Get()->GetTrigger()->RemoveEventHandler(std::get<1>(tuple));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<BtEvent> BtEvent::Create(int event_id,
|
|
||||||
a8::Args event_params,
|
|
||||||
std::function<bool()> has_event_cb
|
|
||||||
)
|
|
||||||
{
|
|
||||||
return std::make_shared<BtEvent>(
|
|
||||||
event_id,
|
|
||||||
event_params,
|
|
||||||
has_event_cb
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BtEvent::FireEvent(BaseAgent* agent)
|
|
||||||
{
|
|
||||||
switch (event_id_) {
|
|
||||||
case kBetOnAttack:
|
|
||||||
{
|
|
||||||
agent->FireEvent("OnAttacked",
|
|
||||||
event_params_.Get<int>(0),
|
|
||||||
event_params_.Get<long long>(1));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case kBetOnCrazeMode:
|
|
||||||
{
|
|
||||||
agent->FireEvent("OnCrazeMode");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BtCoroutine::HasEvent()
|
|
||||||
{
|
|
||||||
if (!context->events.empty()) {
|
|
||||||
auto event = context->events.at(context->events.size() - 1);
|
|
||||||
return event->HasEvent();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BtCoroutine::FireEvent(BaseAgent* agent)
|
|
||||||
{
|
|
||||||
if (!context->events.empty()) {
|
|
||||||
auto event = context->events.at(context->events.size() - 1);
|
|
||||||
if (event->HasEvent()) {
|
|
||||||
event->FireEvent(agent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DumpBt(BaseAgent* agent)
|
void DumpBt(BaseAgent* agent)
|
||||||
{
|
{
|
||||||
static std::string last_bt_name;
|
static std::string last_bt_name;
|
||||||
|
@ -1,28 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "precompile.h"
|
#include "precompile.h"
|
||||||
|
|
||||||
#include "behaviac/behaviac.h"
|
#include "behaviac/behaviac.h"
|
||||||
#include "behaviac_customized_types.h"
|
#include "behaviac_customized_types.h"
|
||||||
|
|
||||||
class BaseAgent;
|
|
||||||
class BtEvent;
|
|
||||||
struct EventHandlerPtr;
|
|
||||||
struct BtContext : public std::enable_shared_from_this<BtContext>
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
CreatureWeakPtr owner;
|
|
||||||
std::vector<std::shared_ptr<BtEvent>> events;
|
|
||||||
std::vector<std::tuple<CreatureWeakPtr, std::weak_ptr<EventHandlerPtr>>> handlers;
|
|
||||||
|
|
||||||
virtual ~BtContext();
|
|
||||||
void AddHandler(CreatureWeakPtr target, std::weak_ptr<EventHandlerPtr> handler);
|
|
||||||
std::weak_ptr<BtContext> GetWp() { return shared_from_this();};
|
|
||||||
private:
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#define MAKE_BTCONTEXT(...) \
|
#define MAKE_BTCONTEXT(...) \
|
||||||
[] (CreatureWeakPtr owner, BaseAgent* agent) \
|
[] (CreatureWeakPtr owner, BaseAgent* agent) \
|
||||||
{ \
|
{ \
|
||||||
@ -37,56 +18,7 @@ context->owner = owner; \
|
|||||||
return context; \
|
return context; \
|
||||||
}(GetOwner()->GetWeakPtrRef(), this)
|
}(GetOwner()->GetWeakPtrRef(), this)
|
||||||
|
|
||||||
struct BtCoroutine
|
struct BtCoroutine;
|
||||||
{
|
|
||||||
std::shared_ptr<BtContext> context;
|
|
||||||
const char* name = nullptr;
|
|
||||||
std::function<behaviac::EBTStatus()> runing_cb;
|
|
||||||
long long sleep_end_frameno = 0;
|
|
||||||
int sleep_time = 0;
|
|
||||||
|
|
||||||
BtCoroutine(std::shared_ptr<BtContext> context, const char* name)
|
|
||||||
{
|
|
||||||
this->context = context;
|
|
||||||
this->name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HasEvent();
|
|
||||||
void FireEvent(BaseAgent* agent);
|
|
||||||
};
|
|
||||||
|
|
||||||
enum BtEventType_e
|
|
||||||
{
|
|
||||||
kBetOnAttack = 1,
|
|
||||||
kBetOnCrazeMode,
|
|
||||||
};
|
|
||||||
|
|
||||||
class BaseAgent;
|
|
||||||
class BtEvent
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
int GetEventId() const { return event_id_; }
|
|
||||||
const a8::Args& GetParams() const { return event_params_; }
|
|
||||||
bool HasEvent() const { return has_event_cb_(); }
|
|
||||||
|
|
||||||
void FireEvent(BaseAgent* agent);
|
|
||||||
|
|
||||||
static std::shared_ptr<BtEvent> Create(int event_id,
|
|
||||||
a8::Args event_params,
|
|
||||||
std::function<bool()> has_event_cb
|
|
||||||
);
|
|
||||||
|
|
||||||
BtEvent(int event_id,
|
|
||||||
a8::Args event_params,
|
|
||||||
std::function<bool()> has_event_cb):
|
|
||||||
event_id_(event_id), event_params_(event_params), has_event_cb_(has_event_cb) {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
int event_id_ = 0;
|
|
||||||
a8::Args event_params_;
|
|
||||||
std::function<bool()> has_event_cb_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Creature;
|
class Creature;
|
||||||
class BaseAgent : public behaviac::Agent
|
class BaseAgent : public behaviac::Agent
|
||||||
{
|
{
|
||||||
|
19
server/gameserver/btcontext.cc
Normal file
19
server/gameserver/btcontext.cc
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include "precompile.h"
|
||||||
|
|
||||||
|
#include "btcontext.h"
|
||||||
|
#include "creature.h"
|
||||||
|
#include "trigger.h"
|
||||||
|
|
||||||
|
void BtContext::AddHandler(CreatureWeakPtr target, std::weak_ptr<EventHandlerPtr> handler)
|
||||||
|
{
|
||||||
|
handlers.push_back(std::make_tuple(target, handler));
|
||||||
|
}
|
||||||
|
|
||||||
|
BtContext::~BtContext()
|
||||||
|
{
|
||||||
|
for (auto& tuple : handlers) {
|
||||||
|
if (std::get<0>(tuple).Get() && !std::get<1>(tuple).expired()) {
|
||||||
|
std::get<0>(tuple).Get()->GetTrigger()->RemoveEventHandler(std::get<1>(tuple));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
server/gameserver/btcontext.h
Normal file
19
server/gameserver/btcontext.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
class BaseAgent;
|
||||||
|
class BtEvent;
|
||||||
|
struct EventHandlerPtr;
|
||||||
|
struct BtContext : public std::enable_shared_from_this<BtContext>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
CreatureWeakPtr owner;
|
||||||
|
std::vector<std::shared_ptr<BtEvent>> events;
|
||||||
|
std::vector<std::tuple<CreatureWeakPtr, std::weak_ptr<EventHandlerPtr>>> handlers;
|
||||||
|
|
||||||
|
virtual ~BtContext();
|
||||||
|
void AddHandler(CreatureWeakPtr target, std::weak_ptr<EventHandlerPtr> handler);
|
||||||
|
std::weak_ptr<BtContext> GetWp() { return shared_from_this();};
|
||||||
|
private:
|
||||||
|
|
||||||
|
};
|
27
server/gameserver/btcoroutine.cc
Normal file
27
server/gameserver/btcoroutine.cc
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include "precompile.h"
|
||||||
|
|
||||||
|
#include "btcoroutine.h"
|
||||||
|
#include "creature.h"
|
||||||
|
#include "trigger.h"
|
||||||
|
#include "base_agent.h"
|
||||||
|
#include "btcontext.h"
|
||||||
|
#include "btevent.h"
|
||||||
|
|
||||||
|
bool BtCoroutine::HasEvent()
|
||||||
|
{
|
||||||
|
if (!context->events.empty()) {
|
||||||
|
auto event = context->events.at(context->events.size() - 1);
|
||||||
|
return event->HasEvent();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BtCoroutine::FireEvent(BaseAgent* agent)
|
||||||
|
{
|
||||||
|
if (!context->events.empty()) {
|
||||||
|
auto event = context->events.at(context->events.size() - 1);
|
||||||
|
if (event->HasEvent()) {
|
||||||
|
event->FireEvent(agent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
server/gameserver/btcoroutine.h
Normal file
24
server/gameserver/btcoroutine.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "behaviac/behaviac.h"
|
||||||
|
#include "behaviac_customized_types.h"
|
||||||
|
|
||||||
|
struct BtContext;
|
||||||
|
class BaseAgent;
|
||||||
|
struct BtCoroutine
|
||||||
|
{
|
||||||
|
std::shared_ptr<BtContext> context;
|
||||||
|
const char* name = nullptr;
|
||||||
|
std::function<behaviac::EBTStatus()> runing_cb;
|
||||||
|
long long sleep_end_frameno = 0;
|
||||||
|
int sleep_time = 0;
|
||||||
|
|
||||||
|
BtCoroutine(std::shared_ptr<BtContext> context, const char* name)
|
||||||
|
{
|
||||||
|
this->context = context;
|
||||||
|
this->name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HasEvent();
|
||||||
|
void FireEvent(BaseAgent* agent);
|
||||||
|
};
|
41
server/gameserver/btevent.cc
Normal file
41
server/gameserver/btevent.cc
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#include "precompile.h"
|
||||||
|
|
||||||
|
#include "btevent.h"
|
||||||
|
#include "creature.h"
|
||||||
|
#include "trigger.h"
|
||||||
|
#include "base_agent.h"
|
||||||
|
|
||||||
|
std::shared_ptr<BtEvent> BtEvent::Create(int event_id,
|
||||||
|
a8::Args event_params,
|
||||||
|
std::function<bool()> has_event_cb
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return std::make_shared<BtEvent>(
|
||||||
|
event_id,
|
||||||
|
event_params,
|
||||||
|
has_event_cb
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BtEvent::FireEvent(BaseAgent* agent)
|
||||||
|
{
|
||||||
|
switch (event_id_) {
|
||||||
|
case kBetOnAttack:
|
||||||
|
{
|
||||||
|
agent->FireEvent("OnAttacked",
|
||||||
|
event_params_.Get<int>(0),
|
||||||
|
event_params_.Get<long long>(1));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case kBetOnCrazeMode:
|
||||||
|
{
|
||||||
|
agent->FireEvent("OnCrazeMode");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
33
server/gameserver/btevent.h
Normal file
33
server/gameserver/btevent.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
enum BtEventType_e
|
||||||
|
{
|
||||||
|
kBetOnAttack = 1,
|
||||||
|
kBetOnCrazeMode,
|
||||||
|
};
|
||||||
|
|
||||||
|
class BaseAgent;
|
||||||
|
class BtEvent
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int GetEventId() const { return event_id_; }
|
||||||
|
const a8::Args& GetParams() const { return event_params_; }
|
||||||
|
bool HasEvent() const { return has_event_cb_(); }
|
||||||
|
|
||||||
|
void FireEvent(BaseAgent* agent);
|
||||||
|
|
||||||
|
static std::shared_ptr<BtEvent> Create(int event_id,
|
||||||
|
a8::Args event_params,
|
||||||
|
std::function<bool()> has_event_cb
|
||||||
|
);
|
||||||
|
|
||||||
|
BtEvent(int event_id,
|
||||||
|
a8::Args event_params,
|
||||||
|
std::function<bool()> has_event_cb):
|
||||||
|
event_id_(event_id), event_params_(event_params), has_event_cb_(has_event_cb) {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
int event_id_ = 0;
|
||||||
|
a8::Args event_params_;
|
||||||
|
std::function<bool()> has_event_cb_;
|
||||||
|
};
|
@ -10,6 +10,10 @@
|
|||||||
#include "glmhelper.h"
|
#include "glmhelper.h"
|
||||||
#include "mapinstance.h"
|
#include "mapinstance.h"
|
||||||
#include "human.h"
|
#include "human.h"
|
||||||
|
#include "btcontext.h"
|
||||||
|
#include "btevent.h"
|
||||||
|
#include "btcoroutine.h"
|
||||||
|
|
||||||
|
|
||||||
#include "mt/Map.h"
|
#include "mt/Map.h"
|
||||||
#include "mt/Hero.h"
|
#include "mt/Hero.h"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user