2021-12-05 17:54:36 +00:00
|
|
|
#include "SwitchBehavior.h"
|
|
|
|
#include "BehaviorBranchContext.h"
|
|
|
|
#include "EntityManager.h"
|
2023-10-21 23:31:55 +00:00
|
|
|
#include "Logger.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "DestroyableComponent.h"
|
|
|
|
#include "BehaviorContext.h"
|
|
|
|
#include "BuffComponent.h"
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
void SwitchBehavior::Handle(BehaviorContext* context, RakNet::BitStream& bitStream, const BehaviorBranchContext branch) {
|
2021-12-05 17:54:36 +00:00
|
|
|
auto state = true;
|
|
|
|
|
|
|
|
if (this->m_imagination > 0 || !this->m_isEnemyFaction) {
|
2024-02-27 07:25:44 +00:00
|
|
|
if (!bitStream.Read(state)) {
|
2024-03-13 02:10:01 +00:00
|
|
|
Log::Warn("Unable to read state from bitStream, aborting Handle! {}", bitStream.GetNumberOfUnreadBits());
|
2022-12-16 21:23:02 +00:00
|
|
|
return;
|
|
|
|
};
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 20:56:33 +00:00
|
|
|
auto* entity = Game::entityManager->GetEntity(context->originator);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
if (entity == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* destroyableComponent = entity->GetComponent<DestroyableComponent>();
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (destroyableComponent == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-13 02:10:01 +00:00
|
|
|
Log::Debug("[{}] State: ({}), imagination: ({}) / ({})", entity->GetLOT(), state, destroyableComponent->GetImagination(), destroyableComponent->GetMaxImagination());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-05-11 11:37:02 +00:00
|
|
|
if (state) {
|
2021-12-05 17:54:36 +00:00
|
|
|
this->m_actionTrue->Handle(context, bitStream, branch);
|
|
|
|
} else {
|
|
|
|
this->m_actionFalse->Handle(context, bitStream, branch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 07:25:44 +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
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
if (this->m_imagination > 0 || !this->m_isEnemyFaction) {
|
2023-07-15 20:56:33 +00:00
|
|
|
auto* entity = Game::entityManager->GetEntity(branch.target);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
state = entity != nullptr;
|
|
|
|
|
|
|
|
if (state && m_targetHasBuff != 0) {
|
|
|
|
auto* buffComponent = entity->GetComponent<BuffComponent>();
|
|
|
|
|
|
|
|
if (buffComponent != nullptr && !buffComponent->HasBuff(m_targetHasBuff)) {
|
|
|
|
state = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
bitStream.Write(state);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
this->m_actionTrue->Calculate(context, bitStream, branch);
|
|
|
|
} else {
|
|
|
|
this->m_actionFalse->Calculate(context, bitStream, branch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchBehavior::Load() {
|
|
|
|
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");
|
|
|
|
}
|