2022-08-06 03:01:59 +00:00
|
|
|
#include "BasicAttackBehavior.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "BehaviorBranchContext.h"
|
|
|
|
#include "Game.h"
|
|
|
|
#include "dLogger.h"
|
|
|
|
#include "EntityManager.h"
|
|
|
|
#include "DestroyableComponent.h"
|
|
|
|
#include "BehaviorContext.h"
|
|
|
|
|
|
|
|
|
|
|
|
void BasicAttackBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) {
|
|
|
|
if (context->unmanaged) {
|
|
|
|
auto* entity = EntityManager::Instance()->GetEntity(branch.target);
|
|
|
|
|
|
|
|
auto* destroyableComponent = entity->GetComponent<DestroyableComponent>();
|
|
|
|
if (destroyableComponent != nullptr) {
|
|
|
|
PlayFx(u"onhit", entity->GetObjectID());
|
2022-04-25 10:25:07 +00:00
|
|
|
destroyableComponent->Damage(this->m_maxDamage, context->originator, context->skillID);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->m_onSuccess->Handle(context, bitStream, branch);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream->AlignReadToByteBoundary();
|
|
|
|
|
|
|
|
uint16_t allocatedBits;
|
|
|
|
bitStream->Read(allocatedBits);
|
|
|
|
|
|
|
|
const auto baseAddress = bitStream->GetReadOffset();
|
|
|
|
if (bitStream->ReadBit()) { // Blocked
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bitStream->ReadBit()) { // Immune
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bitStream->ReadBit()) { // Success
|
|
|
|
uint32_t unknown;
|
|
|
|
bitStream->Read(unknown);
|
|
|
|
|
|
|
|
uint32_t damageDealt;
|
|
|
|
bitStream->Read(damageDealt);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
// A value that's too large may be a cheating attempt, so we set it to MIN too
|
2021-12-05 17:54:36 +00:00
|
|
|
if (damageDealt > this->m_maxDamage || damageDealt < this->m_minDamage) {
|
|
|
|
damageDealt = this->m_minDamage;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* entity = EntityManager::Instance()->GetEntity(branch.target);
|
|
|
|
bool died;
|
|
|
|
bitStream->Read(died);
|
|
|
|
|
|
|
|
if (entity != nullptr) {
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* destroyableComponent = entity->GetComponent<DestroyableComponent>();
|
2021-12-05 17:54:36 +00:00
|
|
|
if (destroyableComponent != nullptr) {
|
|
|
|
PlayFx(u"onhit", entity->GetObjectID());
|
2022-04-25 10:25:07 +00:00
|
|
|
destroyableComponent->Damage(damageDealt, context->originator, context->skillID);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t successState;
|
|
|
|
bitStream->Read(successState);
|
|
|
|
|
|
|
|
switch (successState) {
|
|
|
|
case 1:
|
|
|
|
this->m_onSuccess->Handle(context, bitStream, branch);
|
|
|
|
break;
|
|
|
|
default:
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("BasicAttackBehavior", "Unknown success state (%i)!", successState);
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bitStream->SetReadOffset(baseAddress + allocatedBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicAttackBehavior::Calculate(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) {
|
|
|
|
auto* self = EntityManager::Instance()->GetEntity(context->originator);
|
|
|
|
if (self == nullptr) {
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("BasicAttackBehavior", "Invalid self entity (%llu)!", context->originator);
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
bitStream->AlignWriteToByteBoundary();
|
|
|
|
|
|
|
|
const auto allocatedAddress = bitStream->GetWriteOffset();
|
|
|
|
|
|
|
|
bitStream->Write(uint16_t(0));
|
|
|
|
|
|
|
|
const auto startAddress = bitStream->GetWriteOffset();
|
|
|
|
|
|
|
|
bitStream->Write0(); // Blocked
|
|
|
|
bitStream->Write0(); // Immune
|
|
|
|
bitStream->Write1(); // Success
|
|
|
|
|
|
|
|
if (true) {
|
|
|
|
uint32_t unknown3 = 0;
|
|
|
|
bitStream->Write(unknown3);
|
|
|
|
|
|
|
|
auto damage = this->m_minDamage;
|
|
|
|
auto* entity = EntityManager::Instance()->GetEntity(branch.target);
|
|
|
|
|
|
|
|
if (entity == nullptr) {
|
|
|
|
damage = 0;
|
|
|
|
bitStream->Write(damage);
|
|
|
|
bitStream->Write(false);
|
|
|
|
} else {
|
|
|
|
bitStream->Write(damage);
|
|
|
|
bitStream->Write(true);
|
|
|
|
|
|
|
|
auto* destroyableComponent = entity->GetComponent<DestroyableComponent>();
|
|
|
|
if (damage != 0 && destroyableComponent != nullptr) {
|
|
|
|
PlayFx(u"onhit", entity->GetObjectID(), 1);
|
2022-07-28 13:39:57 +00:00
|
|
|
destroyableComponent->Damage(damage, context->originator, context->skillID, false);
|
2021-12-05 17:54:36 +00:00
|
|
|
context->ScheduleUpdate(branch.target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t successState = 1;
|
|
|
|
bitStream->Write(successState);
|
|
|
|
|
|
|
|
switch (successState) {
|
|
|
|
case 1:
|
|
|
|
this->m_onSuccess->Calculate(context, bitStream, branch);
|
|
|
|
break;
|
|
|
|
default:
|
2022-07-25 02:26:51 +00:00
|
|
|
Game::logger->Log("BasicAttackBehavior", "Unknown success state (%i)!", successState);
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto endAddress = bitStream->GetWriteOffset();
|
|
|
|
const uint16_t allocate = endAddress - startAddress + 1;
|
|
|
|
|
|
|
|
bitStream->SetWriteOffset(allocatedAddress);
|
|
|
|
bitStream->Write(allocate);
|
|
|
|
bitStream->SetWriteOffset(startAddress + allocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicAttackBehavior::Load() {
|
|
|
|
this->m_minDamage = GetInt("min damage");
|
|
|
|
if (this->m_minDamage == 0) this->m_minDamage = 1;
|
|
|
|
|
|
|
|
this->m_maxDamage = GetInt("max damage");
|
|
|
|
if (this->m_maxDamage == 0) this->m_maxDamage = 1;
|
|
|
|
|
|
|
|
this->m_onSuccess = GetAction("on_success");
|
|
|
|
}
|