2022-08-06 03:01:59 +00:00
|
|
|
#include "AreaOfEffectBehavior.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "EntityManager.h"
|
|
|
|
#include "Game.h"
|
|
|
|
#include "dLogger.h"
|
|
|
|
#include "BehaviorBranchContext.h"
|
|
|
|
#include "BehaviorContext.h"
|
2023-06-09 22:12:57 +00:00
|
|
|
#include "QuickBuildComponent.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "DestroyableComponent.h"
|
2022-12-16 21:23:02 +00:00
|
|
|
#include "Game.h"
|
|
|
|
#include "dLogger.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void AreaOfEffectBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) {
|
2022-12-16 21:23:02 +00:00
|
|
|
uint32_t targetCount{};
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-16 21:23:02 +00:00
|
|
|
if (!bitStream->Read(targetCount)) {
|
|
|
|
Game::logger->Log("AreaOfEffectBehavior", "Unable to read targetCount from bitStream, aborting Handle! %i", bitStream->GetNumberOfUnreadBits());
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (targetCount > this->m_maxTargets) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<LWOOBJID> targets;
|
|
|
|
|
|
|
|
targets.reserve(targetCount);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto i = 0u; i < targetCount; ++i) {
|
2022-12-16 21:23:02 +00:00
|
|
|
LWOOBJID target{};
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-16 21:23:02 +00:00
|
|
|
if (!bitStream->Read(target)) {
|
|
|
|
Game::logger->Log("AreaOfEffectBehavior", "failed to read in target %i from bitStream, aborting target Handle!", i);
|
|
|
|
return;
|
|
|
|
};
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
targets.push_back(target);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto target : targets) {
|
2021-12-05 17:54:36 +00:00
|
|
|
branch.target = target;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_action->Handle(context, bitStream, branch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void AreaOfEffectBehavior::Calculate(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* self = EntityManager::Instance()->GetEntity(context->caster);
|
2022-07-28 13:39:57 +00:00
|
|
|
if (self == nullptr) {
|
2022-12-24 02:05:30 +00:00
|
|
|
Game::logger->Log("AreaOfEffectBehavior", "Invalid self for (%llu)!", context->originator);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto reference = branch.isProjectile ? branch.referencePosition : self->GetPosition();
|
|
|
|
|
|
|
|
std::vector<Entity*> targets;
|
|
|
|
|
|
|
|
auto* presetTarget = EntityManager::Instance()->GetEntity(branch.target);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (presetTarget != nullptr) {
|
|
|
|
if (this->m_radius * this->m_radius >= Vector3::DistanceSquared(reference, presetTarget->GetPosition())) {
|
2021-12-05 17:54:36 +00:00
|
|
|
targets.push_back(presetTarget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t includeFaction = m_includeFaction;
|
|
|
|
|
|
|
|
if (self->GetLOT() == 14466) // TODO: Fix edge case
|
|
|
|
{
|
|
|
|
includeFaction = 1;
|
|
|
|
}
|
|
|
|
|
2022-01-02 21:00:01 +00:00
|
|
|
// Gets all of the valid targets, passing in if should target enemies and friends
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto validTarget : context->GetValidTargets(m_ignoreFaction, includeFaction, m_TargetSelf == 1, m_targetEnemy == 1, m_targetFriend == 1)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* entity = EntityManager::Instance()->GetEntity(validTarget);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (entity == nullptr) {
|
2022-12-24 02:05:30 +00:00
|
|
|
Game::logger->Log("AreaOfEffectBehavior", "Invalid target (%llu) for (%llu)!", validTarget, context->originator);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (std::find(targets.begin(), targets.end(), entity) != targets.end()) {
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2023-06-09 08:27:05 +00:00
|
|
|
auto* destroyableComponent = entity->GetComponent<DestroyableComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (destroyableComponent == nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (destroyableComponent->HasFaction(m_ignoreFaction)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto distance = Vector3::DistanceSquared(reference, entity->GetPosition());
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (this->m_radius * this->m_radius >= distance && (this->m_maxTargets == 0 || targets.size() < this->m_maxTargets)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
targets.push_back(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
std::sort(targets.begin(), targets.end(), [reference](Entity* a, Entity* b) {
|
2021-12-05 17:54:36 +00:00
|
|
|
const auto aDistance = Vector3::DistanceSquared(a->GetPosition(), reference);
|
|
|
|
const auto bDistance = Vector3::DistanceSquared(b->GetPosition(), reference);
|
|
|
|
|
|
|
|
return aDistance > bDistance;
|
2022-07-28 13:39:57 +00:00
|
|
|
});
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
const uint32_t size = targets.size();
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream->Write(size);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (size == 0) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->foundTarget = true;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* target : targets) {
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream->Write(target->GetObjectID());
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
PlayFx(u"cast", context->originator, target->GetObjectID());
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* target : targets) {
|
2021-12-05 17:54:36 +00:00
|
|
|
branch.target = target->GetObjectID();
|
|
|
|
|
|
|
|
this->m_action->Calculate(context, bitStream, branch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void AreaOfEffectBehavior::Load() {
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_action = GetAction("action");
|
|
|
|
|
|
|
|
this->m_radius = GetFloat("radius");
|
|
|
|
|
|
|
|
this->m_maxTargets = GetInt("max targets");
|
|
|
|
|
|
|
|
this->m_ignoreFaction = GetInt("ignore_faction");
|
|
|
|
|
|
|
|
this->m_includeFaction = GetInt("include_faction");
|
2021-12-11 10:59:29 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
this->m_TargetSelf = GetInt("target_self");
|
2022-01-02 20:37:03 +00:00
|
|
|
|
|
|
|
this->m_targetEnemy = GetInt("target_enemy");
|
|
|
|
|
|
|
|
this->m_targetFriend = GetInt("target_friend");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|