feat: server side knockback for AI moved objects

The client only simulates knockbacks on the character it controls
(LWOControllablePhysComponent::msgKnockback); enemies and NPCs are drawn
where the server puts them, so a knockback on them did nothing and
scripts stunned them instead.

MovementAIComponent::Knockback now flies the object the way the client
flies its character: a vector longer than 5 lifts it 0.5, throws it with
that velocity under WorldConfig gravity (times its gravity scale) until
it lands on the navmesh, no sooner than 250ms later; a shorter one moves
it by the vector. Walls taller than a step stop sideways motion, the
landing is put back onto the navmesh, and pathing and the combat AI wait
until it lands (destinations set mid air are walked to afterwards).
Position and velocity go out in the normal serialization every tick.

KnockbackBehavior builds the vector like the client's Cast (strength
capped at 300, angle as elevation, relative, caster and ignore_self) and
knocks back server moved targets that aren't immune, both when the
server casts and when a client's skill hits. The blocked bit it writes
now also answers for the target's knockback immunity, so a player whose
client hasn't caught up with a Personal Fortress isn't knocked out of it.

The AM shield generators knock enemies back like live instead of
stunning them.

Fixes #257
Refs #185

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-09-26 19:11:41 -05:00
parent e36f894f1f
commit 2d76c81bba
13 changed files with 540 additions and 22 deletions

View File

@@ -8,6 +8,8 @@
#include "DestroyableComponent.h"
#include "Game.h"
#include "Logger.h"
#include "MovementAIComponent.h"
#include "dpKnockback.h"
void KnockbackBehavior::Handle(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) {
bool unknown{};
@@ -16,6 +18,9 @@ void KnockbackBehavior::Handle(BehaviorContext* context, RakNet::BitStream& bitS
LOG("Unable to read unknown from bitStream, aborting Handle! %i", bitStream.GetNumberOfUnreadBits());
return;
};
// The bit is whether the target blocked it
if (!unknown) KnockbackServerObject(context, branch);
}
void KnockbackBehavior::Calculate(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) {
@@ -27,16 +32,53 @@ void KnockbackBehavior::Calculate(BehaviorContext* context, RakNet::BitStream& b
auto* destroyableComponent = target->GetComponent<DestroyableComponent>();
if (destroyableComponent != nullptr) {
blocked = destroyableComponent->IsKnockbackImmune();
// The client knocks back its own character unless this bit is set, then checks its status immunity.
// Answer for the immunity here too, so a client that hasn't caught up (e.g. the Personal Fortress
// that was just raised) can't knock itself back anyway.
blocked = destroyableComponent->IsKnockbackImmune() || destroyableComponent->GetImmuneToKnockback();
}
}
bitStream.Write(blocked);
if (!blocked) KnockbackServerObject(context, branch);
}
void KnockbackBehavior::KnockbackServerObject(BehaviorContext* context, const BehaviorBranchContext& branch) const {
// Mirrors KnockbackBehavior::Cast in the client (1.10.64 0x004efc10)
if (m_ignoreSelf && context->caster == branch.target) return;
// `caster` knocks back the caster, away from the target
const auto knockedID = m_caster ? context->caster : branch.target;
const auto sourceID = m_caster ? branch.target : context->caster;
auto* const knocked = Game::entityManager->GetEntity(knockedID);
if (!knocked || knocked->IsPlayer()) return;
auto* const movementAI = knocked->GetComponent<MovementAIComponent>();
if (!movementAI) return;
const auto* const destroyableComponent = knocked->GetComponent<DestroyableComponent>();
if (destroyableComponent && destroyableComponent->GetImmuneToKnockback()) return;
NiPoint3 away;
if (m_relative) {
// Backwards from where the knocked object faces
away = QuatUtils::Forward(knocked->GetRotation()) * -1.0f;
} else {
const auto* const source = Game::entityManager->GetEntity(sourceID);
if (!source) return;
away = knocked->GetPosition() - source->GetPosition();
}
movementAI->Knockback(dpKnockback::ComputeVector(away, m_angle, m_strength));
}
void KnockbackBehavior::Load() {
this->m_strength = GetInt("strength");
this->m_angle = GetInt("angle");
this->m_strength = GetFloat("strength");
this->m_angle = GetFloat("angle");
this->m_relative = GetBoolean("relative");
this->m_time = GetInt("time_ms");
this->m_ignoreSelf = GetBoolean("ignore_self", true);
this->m_caster = GetBoolean("caster");
}