2022-08-06 03:01:59 +00:00
|
|
|
#include "SwitchBehavior.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "BehaviorBranchContext.h"
|
|
|
|
#include "EntityManager.h"
|
|
|
|
#include "dLogger.h"
|
|
|
|
#include "DestroyableComponent.h"
|
|
|
|
#include "BehaviorContext.h"
|
|
|
|
#include "BuffComponent.h"
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void SwitchBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bitStream, const BehaviorBranchContext branch) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto state = true;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (this->m_imagination > 0 || !this->m_isEnemyFaction) {
|
2022-12-16 21:23:02 +00:00
|
|
|
if (!bitStream->Read(state)) {
|
|
|
|
Game::logger->Log("SwitchBehavior", "Unable to read state from bitStream, aborting Handle! %i", bitStream->GetNumberOfUnreadBits());
|
|
|
|
return;
|
|
|
|
};
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* entity = EntityManager::Instance()->GetEntity(context->originator);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (entity == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* destroyableComponent = entity->GetComponent<DestroyableComponent>();
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (destroyableComponent == nullptr) {
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-16 21:24:13 +00:00
|
|
|
Game::logger->LogDebug("SwitchBehavior", "[%i] State: (%d), imagination: (%i) / (%f)", entity->GetLOT(), state, destroyableComponent->GetImagination(), destroyableComponent->GetMaxImagination());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (state || (entity->GetLOT() == 8092 && destroyableComponent->GetImagination() >= m_imagination)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_actionTrue->Handle(context, bitStream, branch);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_actionFalse->Handle(context, bitStream, branch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void SwitchBehavior::Calculate(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto state = true;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (this->m_imagination > 0 || !this->m_isEnemyFaction) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto* entity = EntityManager::Instance()->GetEntity(branch.target);
|
|
|
|
|
|
|
|
state = entity != nullptr;
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (state && m_targetHasBuff != 0) {
|
|
|
|
auto* buffComponent = entity->GetComponent<BuffComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (buffComponent != nullptr && !buffComponent->HasBuff(m_targetHasBuff)) {
|
2021-12-05 17:54:36 +00:00
|
|
|
state = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bitStream->Write(state);
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (state) {
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_actionTrue->Calculate(context, bitStream, branch);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_actionFalse->Calculate(context, bitStream, branch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void SwitchBehavior::Load() {
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_actionTrue = GetAction("action_true");
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_actionFalse = GetAction("action_false");
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_imagination = GetInt("imagination");
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_isEnemyFaction = GetBoolean("isEnemyFaction");
|
|
|
|
|
|
|
|
this->m_targetHasBuff = GetInt("target_has_buff");
|
|
|
|
}
|