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"
|
2023-10-21 23:31:55 +00:00
|
|
|
#include "Logger.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "EntityManager.h"
|
2023-11-12 11:53:03 +00:00
|
|
|
#include "dZoneManager.h"
|
|
|
|
#include "WorldConfig.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
#include "DestroyableComponent.h"
|
|
|
|
#include "BehaviorContext.h"
|
2022-12-28 22:04:37 +00:00
|
|
|
#include "eBasicAttackSuccessTypes.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
void BasicAttackBehavior::Handle(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) {
|
2021-12-05 17:54:36 +00:00
|
|
|
if (context->unmanaged) {
|
2023-07-15 20:56:33 +00:00
|
|
|
auto* entity = Game::entityManager->GetEntity(branch.target);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
auto* destroyableComponent = entity->GetComponent<DestroyableComponent>();
|
|
|
|
if (destroyableComponent != nullptr) {
|
2023-11-12 11:53:03 +00:00
|
|
|
PlayFx(u"onhit", entity->GetObjectID()); //This damage animation doesn't seem to play consistently
|
2022-12-16 06:10:58 +00:00
|
|
|
destroyableComponent->Damage(this->m_MaxDamage, context->originator, context->skillID);
|
2023-11-12 11:53:03 +00:00
|
|
|
|
|
|
|
//Handle player damage cooldown
|
|
|
|
if (entity->IsPlayer() && !this->m_DontApplyImmune) {
|
|
|
|
const float immunityTime = Game::zoneManager->GetWorldConfig()->globalImmunityTime;
|
|
|
|
destroyableComponent->SetDamageCooldownTimer(immunityTime);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 06:10:58 +00:00
|
|
|
this->m_OnSuccess->Handle(context, bitStream, branch);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
bitStream.AlignReadToByteBoundary();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-16 06:10:58 +00:00
|
|
|
uint16_t allocatedBits{};
|
2024-02-27 07:25:44 +00:00
|
|
|
if (!bitStream.Read(allocatedBits) || allocatedBits == 0) {
|
2024-03-08 21:44:02 +00:00
|
|
|
Log::Debug("No allocated bits");
|
2022-12-16 06:10:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-03-08 21:44:02 +00:00
|
|
|
Log::Debug("Number of allocated bits {:d}", allocatedBits);
|
2024-02-27 07:25:44 +00:00
|
|
|
const auto baseAddress = bitStream.GetReadOffset();
|
2022-12-28 22:04:37 +00:00
|
|
|
|
|
|
|
DoHandleBehavior(context, bitStream, branch);
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
bitStream.SetReadOffset(baseAddress + allocatedBits);
|
2022-12-28 22:04:37 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
void BasicAttackBehavior::DoHandleBehavior(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) {
|
2023-07-15 20:56:33 +00:00
|
|
|
auto* targetEntity = Game::entityManager->GetEntity(branch.target);
|
2022-12-28 22:04:37 +00:00
|
|
|
if (!targetEntity) {
|
2024-03-08 21:44:02 +00:00
|
|
|
Log::Warn("Target targetEntity {:d} not found.", branch.target);
|
2022-12-28 22:04:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* destroyableComponent = targetEntity->GetComponent<DestroyableComponent>();
|
|
|
|
if (!destroyableComponent) {
|
2024-03-08 21:44:02 +00:00
|
|
|
Log::Warn("No destroyable found on the obj/lot {:d}/{:d}", branch.target, targetEntity->GetLOT());
|
2022-12-28 22:04:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-16 06:10:58 +00:00
|
|
|
bool isBlocked{};
|
|
|
|
bool isImmune{};
|
|
|
|
bool isSuccess{};
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
if (!bitStream.Read(isBlocked)) {
|
2024-03-08 21:44:02 +00:00
|
|
|
Log::Warn("Unable to read isBlocked");
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-28 22:04:37 +00:00
|
|
|
if (isBlocked) {
|
|
|
|
destroyableComponent->SetAttacksToBlock(std::min(destroyableComponent->GetAttacksToBlock() - 1, 0U));
|
2023-07-15 20:56:33 +00:00
|
|
|
Game::entityManager->SerializeEntity(targetEntity);
|
2022-12-28 22:04:37 +00:00
|
|
|
this->m_OnFailBlocked->Handle(context, bitStream, branch);
|
|
|
|
return;
|
|
|
|
}
|
2022-12-16 06:10:58 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
if (!bitStream.Read(isImmune)) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Unable to read isImmune");
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-28 22:04:37 +00:00
|
|
|
if (isImmune) {
|
2023-11-12 11:53:03 +00:00
|
|
|
LOG_DEBUG("Target targetEntity %llu is immune!", branch.target);
|
2022-12-28 22:04:37 +00:00
|
|
|
this->m_OnFailImmune->Handle(context, bitStream, branch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
if (!bitStream.Read(isSuccess)) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("failed to read success from bitstream");
|
2022-12-28 22:04:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-12-16 06:10:58 +00:00
|
|
|
|
2022-12-28 22:04:37 +00:00
|
|
|
if (isSuccess) {
|
|
|
|
uint32_t armorDamageDealt{};
|
2024-02-27 07:25:44 +00:00
|
|
|
if (!bitStream.Read(armorDamageDealt)) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Unable to read armorDamageDealt");
|
2022-12-16 06:10:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-28 22:04:37 +00:00
|
|
|
uint32_t healthDamageDealt{};
|
2024-02-27 07:25:44 +00:00
|
|
|
if (!bitStream.Read(healthDamageDealt)) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Unable to read healthDamageDealt");
|
2022-12-16 06:10:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-28 22:04:37 +00:00
|
|
|
uint32_t totalDamageDealt = armorDamageDealt + healthDamageDealt;
|
|
|
|
|
|
|
|
// A value that's too large may be a cheating attempt, so we set it to MIN
|
|
|
|
if (totalDamageDealt > this->m_MaxDamage) {
|
|
|
|
totalDamageDealt = this->m_MinDamage;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 06:10:58 +00:00
|
|
|
bool died{};
|
2024-02-27 07:25:44 +00:00
|
|
|
if (!bitStream.Read(died)) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Unable to read died");
|
2022-12-16 06:10:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-12-28 22:04:37 +00:00
|
|
|
auto previousArmor = destroyableComponent->GetArmor();
|
|
|
|
auto previousHealth = destroyableComponent->GetHealth();
|
|
|
|
PlayFx(u"onhit", targetEntity->GetObjectID());
|
|
|
|
destroyableComponent->Damage(totalDamageDealt, context->originator, context->skillID);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 06:10:58 +00:00
|
|
|
uint8_t successState{};
|
2024-02-27 07:25:44 +00:00
|
|
|
if (!bitStream.Read(successState)) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Unable to read success state");
|
2022-12-16 06:10:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-28 22:04:37 +00:00
|
|
|
switch (static_cast<eBasicAttackSuccessTypes>(successState)) {
|
|
|
|
case eBasicAttackSuccessTypes::SUCCESS:
|
2022-12-16 06:10:58 +00:00
|
|
|
this->m_OnSuccess->Handle(context, bitStream, branch);
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
2022-12-28 22:04:37 +00:00
|
|
|
case eBasicAttackSuccessTypes::FAILARMOR:
|
|
|
|
this->m_OnFailArmor->Handle(context, bitStream, branch);
|
|
|
|
break;
|
2021-12-05 17:54:36 +00:00
|
|
|
default:
|
2022-12-28 22:04:37 +00:00
|
|
|
if (static_cast<eBasicAttackSuccessTypes>(successState) != eBasicAttackSuccessTypes::FAILIMMUNE) {
|
2024-03-08 21:44:02 +00:00
|
|
|
Log::Warn("Unknown success state ({:d})!", successState);
|
2022-12-28 22:04:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->m_OnFailImmune->Handle(context, bitStream, branch);
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
void BasicAttackBehavior::Calculate(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) {
|
|
|
|
bitStream.AlignWriteToByteBoundary();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
const auto allocatedAddress = bitStream.GetWriteOffset();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
bitStream.Write<uint16_t>(0);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
const auto startAddress = bitStream.GetWriteOffset();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-28 22:04:37 +00:00
|
|
|
DoBehaviorCalculation(context, bitStream, branch);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
const auto endAddress = bitStream.GetWriteOffset();
|
2023-04-01 09:45:29 +00:00
|
|
|
const uint16_t allocate = endAddress - startAddress;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
bitStream.SetWriteOffset(allocatedAddress);
|
|
|
|
bitStream.Write(allocate);
|
|
|
|
bitStream.SetWriteOffset(startAddress + allocate);
|
2022-12-28 22:04:37 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
void BasicAttackBehavior::DoBehaviorCalculation(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) {
|
2023-07-15 20:56:33 +00:00
|
|
|
auto* targetEntity = Game::entityManager->GetEntity(branch.target);
|
2022-12-28 22:04:37 +00:00
|
|
|
if (!targetEntity) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("Target entity %llu is null!", branch.target);
|
2022-12-28 22:04:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* destroyableComponent = targetEntity->GetComponent<DestroyableComponent>();
|
|
|
|
if (!destroyableComponent || !destroyableComponent->GetParent()) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("No destroyable component on %llu", branch.target);
|
2022-12-28 22:04:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool isBlocking = destroyableComponent->GetAttacksToBlock() > 0;
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
bitStream.Write(isBlocking);
|
2022-12-28 22:04:37 +00:00
|
|
|
|
|
|
|
if (isBlocking) {
|
|
|
|
destroyableComponent->SetAttacksToBlock(destroyableComponent->GetAttacksToBlock() - 1);
|
2023-07-15 20:56:33 +00:00
|
|
|
Game::entityManager->SerializeEntity(targetEntity);
|
2022-12-28 22:04:37 +00:00
|
|
|
this->m_OnFailBlocked->Calculate(context, bitStream, branch);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-24 00:11:00 +00:00
|
|
|
const bool isImmune = destroyableComponent->IsImmune() || destroyableComponent->IsCooldownImmune();
|
2024-02-27 07:25:44 +00:00
|
|
|
bitStream.Write(isImmune);
|
2022-12-28 22:04:37 +00:00
|
|
|
|
|
|
|
if (isImmune) {
|
2023-11-12 11:53:03 +00:00
|
|
|
LOG_DEBUG("Target targetEntity %llu is immune!", branch.target);
|
2022-12-28 22:04:37 +00:00
|
|
|
this->m_OnFailImmune->Calculate(context, bitStream, branch);
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-28 22:04:37 +00:00
|
|
|
bool isSuccess = false;
|
|
|
|
const uint32_t previousHealth = destroyableComponent->GetHealth();
|
|
|
|
const uint32_t previousArmor = destroyableComponent->GetArmor();
|
|
|
|
|
|
|
|
const auto damage = this->m_MinDamage;
|
|
|
|
|
|
|
|
PlayFx(u"onhit", targetEntity->GetObjectID(), 1);
|
|
|
|
destroyableComponent->Damage(damage, context->originator, context->skillID, false);
|
|
|
|
context->ScheduleUpdate(branch.target);
|
|
|
|
|
|
|
|
const uint32_t armorDamageDealt = previousArmor - destroyableComponent->GetArmor();
|
|
|
|
const uint32_t healthDamageDealt = previousHealth - destroyableComponent->GetHealth();
|
|
|
|
isSuccess = armorDamageDealt > 0 || healthDamageDealt > 0 || (armorDamageDealt + healthDamageDealt) > 0;
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
bitStream.Write(isSuccess);
|
2022-12-28 22:04:37 +00:00
|
|
|
|
2023-11-12 11:53:03 +00:00
|
|
|
//Handle player damage cooldown
|
|
|
|
if (isSuccess && targetEntity->IsPlayer() && !this->m_DontApplyImmune) {
|
2023-12-24 00:11:00 +00:00
|
|
|
destroyableComponent->SetDamageCooldownTimer(Game::zoneManager->GetWorldConfig()->globalImmunityTime);
|
2023-11-12 11:53:03 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 22:04:37 +00:00
|
|
|
eBasicAttackSuccessTypes successState = eBasicAttackSuccessTypes::FAILIMMUNE;
|
|
|
|
if (isSuccess) {
|
|
|
|
if (healthDamageDealt >= 1) {
|
|
|
|
successState = eBasicAttackSuccessTypes::SUCCESS;
|
|
|
|
} else if (armorDamageDealt >= 1) {
|
|
|
|
successState = this->m_OnFailArmor->m_templateId == BehaviorTemplates::BEHAVIOR_EMPTY ? eBasicAttackSuccessTypes::FAILIMMUNE : eBasicAttackSuccessTypes::FAILARMOR;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-12-28 22:04:37 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
bitStream.Write(armorDamageDealt);
|
|
|
|
bitStream.Write(healthDamageDealt);
|
|
|
|
bitStream.Write(targetEntity->GetIsDead());
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
bitStream.Write(successState);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-28 22:04:37 +00:00
|
|
|
switch (static_cast<eBasicAttackSuccessTypes>(successState)) {
|
|
|
|
case eBasicAttackSuccessTypes::SUCCESS:
|
2022-12-16 06:10:58 +00:00
|
|
|
this->m_OnSuccess->Calculate(context, bitStream, branch);
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
2022-12-28 22:04:37 +00:00
|
|
|
case eBasicAttackSuccessTypes::FAILARMOR:
|
|
|
|
this->m_OnFailArmor->Calculate(context, bitStream, branch);
|
|
|
|
break;
|
2021-12-05 17:54:36 +00:00
|
|
|
default:
|
2022-12-28 22:04:37 +00:00
|
|
|
if (static_cast<eBasicAttackSuccessTypes>(successState) != eBasicAttackSuccessTypes::FAILIMMUNE) {
|
2024-03-08 21:44:02 +00:00
|
|
|
Log::Warn("Unknown success state ({:d})!", GeneralUtils::ToUnderlying(successState));
|
2022-12-28 22:04:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
this->m_OnFailImmune->Calculate(context, bitStream, branch);
|
2021-12-05 17:54:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicAttackBehavior::Load() {
|
2023-11-12 11:53:03 +00:00
|
|
|
this->m_DontApplyImmune = GetBoolean("dont_apply_immune");
|
|
|
|
|
2022-12-16 06:10:58 +00:00
|
|
|
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;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-28 22:04:37 +00:00
|
|
|
// The client sets the minimum damage to maximum, so we'll do the same. These are usually the same value anyways.
|
|
|
|
if (this->m_MinDamage < this->m_MaxDamage) this->m_MinDamage = this->m_MaxDamage;
|
|
|
|
|
2022-12-16 06:10:58 +00:00
|
|
|
this->m_OnSuccess = GetAction("on_success");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-12-16 06:10:58 +00:00
|
|
|
this->m_OnFailArmor = GetAction("on_fail_armor");
|
2022-12-28 22:04:37 +00:00
|
|
|
|
|
|
|
this->m_OnFailImmune = GetAction("on_fail_immune");
|
|
|
|
|
|
|
|
this->m_OnFailBlocked = GetAction("on_fail_blocked");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|