mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-06-20 05:34:22 +00:00
fix: add range checks to npc combat skill behavior (#2003)
* fix: add range checks to npc combat skill behavior tested that all enemies now cast skills smartly based on range to targets, and do not cast skills if they are out of range. fixes an issue where the spider queen could attack you outside the normal range fixes an issue where entering happy flower caused you to need to restart the client fixes #965 * feedback
This commit is contained in:
@@ -5,20 +5,40 @@
|
||||
|
||||
void NpcCombatSkillBehavior::Calculate(BehaviorContext* context, RakNet::BitStream& bit_stream, BehaviorBranchContext branch) {
|
||||
context->skillTime = this->m_npcSkillTime;
|
||||
const auto* const targetEntity = Game::entityManager->GetEntity(branch.target);
|
||||
const auto* const sourceEntity = Game::entityManager->GetEntity(context->caster);
|
||||
|
||||
for (auto* behavior : this->m_behaviors) {
|
||||
behavior->Calculate(context, bit_stream, branch);
|
||||
bool cast = true;
|
||||
// Check that the target is within the cast range
|
||||
if (targetEntity && sourceEntity && this->m_maxRange != 0.0f) {
|
||||
const auto targetPos = targetEntity->GetPosition();
|
||||
const auto sourcePos = sourceEntity->GetPosition();
|
||||
const auto distance = NiPoint3::DistanceSquared(targetPos, sourcePos);
|
||||
cast = distance >= this->m_minRange && distance <= this->m_maxRange;
|
||||
}
|
||||
|
||||
if (cast) {
|
||||
for (auto* behavior : this->m_behaviors) {
|
||||
behavior->Calculate(context, bit_stream, branch);
|
||||
}
|
||||
} else {
|
||||
// We failed to find a valid target, do not continue the behavior
|
||||
context->foundTarget = false;
|
||||
}
|
||||
}
|
||||
|
||||
void NpcCombatSkillBehavior::Load() {
|
||||
this->m_npcSkillTime = GetFloat("npc skill time");
|
||||
this->m_minRange = GetFloat("min range") * 0.9f; // Make the min and max 10% smaller to account for server/client position disagreements
|
||||
this->m_minRange *= this->m_minRange;
|
||||
this->m_maxRange = GetFloat("max range") * 0.9f; // Make the min and max 10% smaller to account for server/client position disagreements
|
||||
this->m_maxRange *= this->m_maxRange;
|
||||
|
||||
const auto parameters = GetParameterNames();
|
||||
|
||||
for (const auto& parameter : parameters) {
|
||||
if (parameter.first.rfind("behavior", 0) == 0) {
|
||||
auto* action = GetAction(parameter.second);
|
||||
for (const auto& [parameter, value] : parameters) {
|
||||
if (parameter.rfind("behavior", 0) == 0) {
|
||||
auto* action = GetAction(value);
|
||||
|
||||
this->m_behaviors.push_back(action);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ public:
|
||||
float m_npcSkillTime;
|
||||
|
||||
float m_maxRange{};
|
||||
float m_minRange{};
|
||||
|
||||
/*
|
||||
* Inherited
|
||||
|
||||
@@ -350,7 +350,7 @@ void BaseCombatAIComponent::CalculateCombat(const float deltaTime) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto result = skillComponent->CalculateBehavior(entry.skillId, entry.behavior->m_behaviorId, LWOOBJID_EMPTY);
|
||||
const auto result = skillComponent->CalculateBehavior(entry.skillId, entry.behavior->m_behaviorId, GetTarget());
|
||||
|
||||
if (result.success) {
|
||||
if (m_MovementAI != nullptr) {
|
||||
@@ -759,8 +759,8 @@ void BaseCombatAIComponent::SetTetherSpeed(float value) {
|
||||
m_TetherSpeed = value;
|
||||
}
|
||||
|
||||
void BaseCombatAIComponent::Stun(const float time) {
|
||||
if (m_StunImmune || m_StunTime > time) {
|
||||
void BaseCombatAIComponent::Stun(const float time, const bool force) {
|
||||
if (!force && (m_StunImmune || m_StunTime > time)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -183,8 +183,9 @@ public:
|
||||
/**
|
||||
* Stuns the entity for a certain amount of time, will not work if the entity is stun immune
|
||||
* @param time the time to stun the entity, if stunnable
|
||||
* @param force whether or not to force the stun and ignore checks
|
||||
*/
|
||||
void Stun(float time);
|
||||
void Stun(float time, const bool force = false);
|
||||
|
||||
/**
|
||||
* Gets the radius that will cause this entity to get aggro'd, causing a target chase
|
||||
|
||||
Reference in New Issue
Block a user