76 lines
1.8 KiB
C++
76 lines
1.8 KiB
C++
#include "precompile.h"
|
|
|
|
#include "base_agent.h"
|
|
#include "room.h"
|
|
#include "creature.h"
|
|
#include "human.h"
|
|
|
|
BaseAgent::BaseAgent():behaviac::Agent()
|
|
{
|
|
|
|
}
|
|
|
|
BaseAgent::~BaseAgent()
|
|
{
|
|
}
|
|
|
|
bool BaseAgent::IsGameOver()
|
|
{
|
|
return GetOwner()->room->IsGameOver();
|
|
}
|
|
|
|
bool BaseAgent::HasTarget(float range)
|
|
{
|
|
Human* enemy = GetOwner()->room->FindEnemy(GetOwner()->AsHuman(), range);
|
|
return enemy != nullptr;
|
|
}
|
|
|
|
behaviac::EBTStatus BaseAgent::DoRunningCb()
|
|
{
|
|
if (status_ != behaviac::BT_RUNNING) {
|
|
abort();
|
|
}
|
|
status_ = status_runing_cb_();
|
|
if (status_ != behaviac::BT_RUNNING) {
|
|
status_runing_cb_ = nullptr;
|
|
}
|
|
return status_;
|
|
}
|
|
|
|
behaviac::EBTStatus BaseAgent::StartCoroutine(std::function<behaviac::EBTStatus()> cb)
|
|
{
|
|
status_runing_cb_ = std::move(cb);
|
|
status_ = behaviac::BT_RUNNING;
|
|
return status_;
|
|
}
|
|
|
|
behaviac::EBTStatus BaseAgent::CoAttackTarget(int target_id)
|
|
{
|
|
if (status_ == behaviac::BT_RUNNING) {
|
|
return DoRunningCb();
|
|
}
|
|
Entity* entity = GetOwner()->room->GetEntityByUniId(target_id);
|
|
if (!entity || !entity->IsCreature(GetOwner()->room)) {
|
|
return behaviac::BT_FAILURE;
|
|
}
|
|
CreatureWeakPtr target = ((Creature*)entity)->GetWeakPtrRef();
|
|
if (target.Get()->dead) {
|
|
return behaviac::BT_FAILURE;
|
|
}
|
|
|
|
long long frameno = GetOwner()->room->GetFrameNo();
|
|
long long last_pursuit_frameno = GetOwner()->room->GetFrameNo();
|
|
return StartCoroutine
|
|
(
|
|
[this, target, frameno, last_pursuit_frameno] () mutable
|
|
{
|
|
if (GetOwner()->room->GetFrameNo() - frameno > SERVER_FRAME_RATE * 10 ||
|
|
!target.Get() || target.Get()->dead) {
|
|
return behaviac::BT_SUCCESS;
|
|
} else {
|
|
}
|
|
return behaviac::BT_RUNNING;
|
|
}
|
|
);
|
|
}
|