Files
DarkflameServer/dGame/dComponents/Physics/ControllablePhysicsComponent.cpp
Aaron Kimbrell 9265ab0e10 fix: run speed modifiers stack as the client's do
LWOControllablePhysComponent::msgAddRunspeedModifier (0x00be0af0) and
msgRemoveRunSpeedModifier (0x00be0720): a modifier over 1000 or of exactly
500 counts for nothing, any slow (under 500) wins with the lowest one, and
otherwise the highest boost is the speed. Before, the last one applied won,
so a small boost cast after a big one, or a boost cast while slowed,
replaced it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 14:50:33 -06:00

506 lines
18 KiB
C++

#include "ControllablePhysicsComponent.h"
#include "CombatMessages.h"
#include "Entity.h"
#include "BitStream.h"
#include "Logger.h"
#include "Game.h"
#include "dpWorld.h"
#include "dpEntity.h"
#include "CDPhysicsComponentTable.h"
#include "CDComponentsRegistryTable.h"
#include "CDClientManager.h"
#include "EntityManager.h"
#include "Character.h"
#include "dZoneManager.h"
#include "dServer.h"
#include "Zone.h"
#include "LevelProgressionComponent.h"
#include "eStateChangeType.h"
#include "StringifiedEnum.h"
#include "Amf3.h"
#include "ObjectLoadState.h"
ControllablePhysicsComponent::ControllablePhysicsComponent(Entity* entity, const int32_t componentID) : PhysicsComponent(entity, componentID) {
RegisterMsg(&ControllablePhysicsComponent::OnGetObjectReportInfo);
m_Velocity = {};
m_AngularVelocity = {};
m_InJetpackMode = false;
m_IsOnGround = true;
m_IsOnRail = false;
m_dpEntity = nullptr;
m_Static = false;
m_SpeedMultiplier = 1;
m_GravityScale = 1;
m_DirtyCheats = false;
m_DirtyEquippedItemInfo = true;
m_PickupRadius = 0.0f;
m_DirtyBubble = false;
m_IsInBubble = false;
m_SpecialAnims = false;
m_BubbleType = eBubbleType::DEFAULT;
m_IsTeleporting = false;
m_ImmuneToStunAttackCount = 0;
m_ImmuneToStunEquipCount = 0;
m_ImmuneToStunInteractCount = 0;
m_ImmuneToStunJumpCount = 0;
m_ImmuneToStunMoveCount = 0;
m_ImmuneToStunTurnCount = 0;
m_ImmuneToStunUseItemCount = 0;
// An object that isn't a character starts where the level put it (from the object's spawn info, as the client's
// physics does; a character's place comes with its save)
if (!m_Parent->GetCharacter()) {
SetPosition(m_Parent->GetDefaultPosition());
SetRotation(m_Parent->GetDefaultRotation());
}
}
// After the physics config: a character's body, in the physics component's group
void ControllablePhysicsComponent::LoadConfigData() {
PhysicsComponent::LoadConfigData();
if (m_Parent->GetLOT() != 1) // Other physics entities we care about will be added by BaseCombatAI
return;
if (m_Parent->GetLOT() == 1) {
LOG("Using patch to load minifig physics");
float radius = 1.5f;
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), radius, false);
// The player's group from its physics component (10), which trigger volumes filter on
m_dpEntity->SetCollisionGroup(static_cast<uint32_t>(m_CollisionGroup));
dpWorld::AddEntity(m_dpEntity);
}
}
ControllablePhysicsComponent::~ControllablePhysicsComponent() {
if (m_dpEntity) {
dpWorld::RemoveEntity(m_dpEntity);
}
}
void ControllablePhysicsComponent::Run(float deltaTime) {
}
void ControllablePhysicsComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
//If this is a creation, then we assume the position is dirty, even when it isn't.
//This is because new clients will still need to receive the position.
//if (bIsInitialUpdate) m_DirtyPosition = true;
if (bIsInitialUpdate) {
outBitStream.Write(m_InJetpackMode);
if (m_InJetpackMode) {
outBitStream.Write(m_JetpackEffectID);
outBitStream.Write(m_JetpackFlying);
outBitStream.Write(m_JetpackBypassChecks);
}
outBitStream.Write1(); // always write these on construction
outBitStream.Write(m_ImmuneToStunMoveCount);
outBitStream.Write(m_ImmuneToStunJumpCount);
outBitStream.Write(m_ImmuneToStunTurnCount);
outBitStream.Write(m_ImmuneToStunAttackCount);
outBitStream.Write(m_ImmuneToStunUseItemCount);
outBitStream.Write(m_ImmuneToStunEquipCount);
outBitStream.Write(m_ImmuneToStunInteractCount);
}
// Live wrote the cheat block on construction only when the gravity or speed was changed from 1.
const bool writeCheats = bIsInitialUpdate ? (m_GravityScale != 1.0f || m_SpeedMultiplier != 1.0f) : m_DirtyCheats;
outBitStream.Write(writeCheats);
if (writeCheats) {
outBitStream.Write(m_GravityScale);
outBitStream.Write(m_SpeedMultiplier);
if (!bIsInitialUpdate) m_DirtyCheats = false;
}
outBitStream.Write(m_DirtyEquippedItemInfo || bIsInitialUpdate);
if (m_DirtyEquippedItemInfo || bIsInitialUpdate) {
outBitStream.Write(m_PickupRadius);
// LWOControllablePhysComponent::Deserialize (0x00c45770) reads this into the same flag as the jetpack block's
// flying bit; live wrote 0, also for players in jetpack mode (10,069 player constructions)
outBitStream.Write(m_JetpackFlying);
if (!bIsInitialUpdate) m_DirtyEquippedItemInfo = false;
}
outBitStream.Write(m_DirtyBubble || bIsInitialUpdate);
if (m_DirtyBubble || bIsInitialUpdate) {
outBitStream.Write(m_IsInBubble);
if (m_IsInBubble) {
outBitStream.Write(m_BubbleType);
outBitStream.Write(m_SpecialAnims);
}
if (!bIsInitialUpdate) m_DirtyBubble = false;
}
outBitStream.Write(m_DirtyPosition || bIsInitialUpdate);
if (m_DirtyPosition || bIsInitialUpdate) {
outBitStream.Write(m_Position.x);
outBitStream.Write(m_Position.y);
outBitStream.Write(m_Position.z);
outBitStream.Write(m_Rotation.x);
outBitStream.Write(m_Rotation.y);
outBitStream.Write(m_Rotation.z);
outBitStream.Write(m_Rotation.w);
outBitStream.Write(m_IsOnGround);
outBitStream.Write(m_IsOnRail);
bool isNotZero = m_Velocity != NiPoint3Constant::ZERO;
outBitStream.Write(isNotZero);
if (isNotZero) {
outBitStream.Write(m_Velocity.x);
outBitStream.Write(m_Velocity.y);
outBitStream.Write(m_Velocity.z);
}
isNotZero = m_AngularVelocity != NiPoint3Constant::ZERO;
outBitStream.Write(isNotZero);
if (isNotZero) {
outBitStream.Write(m_AngularVelocity.x);
outBitStream.Write(m_AngularVelocity.y);
outBitStream.Write(m_AngularVelocity.z);
}
outBitStream.Write0(); // local_space_info, always zero for now.
if (!bIsInitialUpdate) {
m_DirtyPosition = false;
outBitStream.Write(m_IsTeleporting);
m_IsTeleporting = false;
}
}
}
void ControllablePhysicsComponent::SetJetPackMode(const bool use, const int32_t effectID, const bool bypassChecks) {
m_InJetpackMode = use;
m_JetpackEffectID = effectID;
m_JetpackBypassChecks = bypassChecks;
}
// A character's save, then where it comes into this world
void ControllablePhysicsComponent::LoadFromSaveData(const tinyxml2::XMLDocument& doc) {
ReadSaveData(doc);
PlaceCharacter();
}
// If it came from another zone it starts at the target scene or the zone's spawn (Moon Base always: the saved place
// tends to be on the roof; a target scene too: only possible in a test map). A world's saved respawn point is where a
// death there respawns, not where arriving starts.
void ControllablePhysicsComponent::PlaceCharacter() {
auto* const character = m_Parent->GetCharacter();
if (!character) return;
const auto mapID = Game::server->GetZoneID();
const auto& targetSceneName = character->GetTargetScene();
if (character->GetZoneID() == Game::server->GetZoneID() && mapID != 1603 && targetSceneName.empty()) return;
NiPoint3 pos;
NiQuaternion rot = QuatUtils::IDENTITY;
auto* targetScene = Game::entityManager->GetSpawnPointEntity(targetSceneName);
if (targetScene != nullptr) {
pos = targetScene->GetPosition();
rot = targetScene->GetRotation();
} else {
pos = Game::zoneManager->GetZone()->GetSpawnPos();
rot = Game::zoneManager->GetZone()->GetSpawnRot();
}
SetPosition(pos);
SetRotation(rot);
}
void ControllablePhysicsComponent::ReadSaveData(const tinyxml2::XMLDocument& doc) {
auto* character = doc.FirstChildElement("obj")->FirstChildElement("char");
if (!character) {
LOG("Failed to find char tag!");
return;
}
m_Parent->GetCharacter()->LoadXmlRespawnCheckpoints();
character->QueryAttribute("lzx", &m_Position.x);
character->QueryAttribute("lzy", &m_Position.y);
character->QueryAttribute("lzz", &m_Position.z);
character->QueryAttribute("lzrx", &m_Rotation.x);
character->QueryAttribute("lzry", &m_Rotation.y);
character->QueryAttribute("lzrz", &m_Rotation.z);
character->QueryAttribute("lzrw", &m_Rotation.w);
m_DirtyPosition = true;
}
void ControllablePhysicsComponent::WriteSaveData(tinyxml2::XMLDocument& doc) {
tinyxml2::XMLElement* character = doc.FirstChildElement("obj")->FirstChildElement("char");
if (!character) {
LOG("Failed to find char tag while updating XML!");
return;
}
if (Character::SavesLocationInThisZone()) {
character->SetAttribute("lzx", m_Position.x);
character->SetAttribute("lzy", m_Position.y);
character->SetAttribute("lzz", m_Position.z);
character->SetAttribute("lzrx", m_Rotation.x);
character->SetAttribute("lzry", m_Rotation.y);
character->SetAttribute("lzrz", m_Rotation.z);
character->SetAttribute("lzrw", m_Rotation.w);
}
}
void ControllablePhysicsComponent::SetPosition(const NiPoint3& pos) {
if (m_Static) return;
PhysicsComponent::SetPosition(pos);
if (m_dpEntity) m_dpEntity->SetPosition(pos);
}
void ControllablePhysicsComponent::SetRotation(const NiQuaternion& rot) {
if (m_Static) return;
PhysicsComponent::SetRotation(rot);
if (m_dpEntity) m_dpEntity->SetRotation(rot);
}
void ControllablePhysicsComponent::SetVelocity(const NiPoint3& vel) {
if (m_Static || m_Velocity == vel) return;
m_Velocity = vel;
m_DirtyPosition = true;
if (m_dpEntity) m_dpEntity->SetVelocity(vel);
}
void ControllablePhysicsComponent::SetAngularVelocity(const NiPoint3& vel) {
if (m_Static || m_AngularVelocity == vel) return;
m_AngularVelocity = vel;
m_DirtyPosition = true;
}
void ControllablePhysicsComponent::SetIsOnGround(bool val) {
if (m_IsOnGround == val) return;
m_DirtyPosition = true;
m_IsOnGround = val;
}
void ControllablePhysicsComponent::SetIsOnRail(bool val) {
if (m_IsOnRail == val) return;
m_DirtyPosition = true;
m_IsOnRail = val;
}
void ControllablePhysicsComponent::SetDirtyPosition(bool val) {
m_DirtyPosition = val;
}
void ControllablePhysicsComponent::AddPickupRadiusScale(float value) {
m_ActivePickupRadiusScales.push_back(value);
if (value > m_PickupRadius) {
m_PickupRadius = value;
m_DirtyEquippedItemInfo = true;
}
}
void ControllablePhysicsComponent::RemovePickupRadiusScale(float value) {
// Attempt to remove pickup radius from active radii
const auto pos = std::find(m_ActivePickupRadiusScales.begin(), m_ActivePickupRadiusScales.end(), value);
if (pos != m_ActivePickupRadiusScales.end()) {
m_ActivePickupRadiusScales.erase(pos);
} else {
LOG_DEBUG("Warning: Could not find pickup radius %f in list of active radii. List has %i active radii.", value, m_ActivePickupRadiusScales.size());
return;
}
// Recalculate pickup radius since we removed one by now
m_PickupRadius = 0.0f;
m_DirtyEquippedItemInfo = true;
for (uint32_t i = 0; i < m_ActivePickupRadiusScales.size(); i++) {
auto candidateRadius = m_ActivePickupRadiusScales[i];
if (m_PickupRadius < candidateRadius) m_PickupRadius = candidateRadius;
}
Game::entityManager->SerializeEntity(m_Parent);
}
void ControllablePhysicsComponent::AddSpeedboost(float value) {
m_ActiveSpeedBoosts.push_back(value);
UpdateSpeedBoost();
}
void ControllablePhysicsComponent::RemoveSpeedboost(float value) {
const auto pos = std::find(m_ActiveSpeedBoosts.begin(), m_ActiveSpeedBoosts.end(), value);
if (pos != m_ActiveSpeedBoosts.end()) {
m_ActiveSpeedBoosts.erase(pos);
} else {
LOG_DEBUG("Warning: Could not find speedboost %f in list of active speedboosts. List has %i active speedboosts.", value, m_ActiveSpeedBoosts.size());
return;
}
UpdateSpeedBoost();
Game::entityManager->SerializeEntity(m_Parent);
}
void ControllablePhysicsComponent::UpdateSpeedBoost() {
// The client's rule (LWOControllablePhysComponent::msgAddRunspeedModifier 0x00be0af0, msgRemoveRunSpeedModifier
// 0x00be0720): a modifier over 1000 or of exactly the base 500 counts for nothing; with any slow (under 500) the
// lowest one is the speed, otherwise the highest boost is
constexpr float baseSpeed = 500.0f;
constexpr float maxModifier = 1000.0f;
float lowestSlow = baseSpeed;
float highestBoost = baseSpeed;
for (const auto boost : m_ActiveSpeedBoosts) {
if (boost > maxModifier) continue;
if (boost < lowestSlow) lowestSlow = boost;
if (boost > highestBoost) highestBoost = boost;
}
if (lowestSlow < baseSpeed) {
m_SpeedBoost = lowestSlow;
} else if (highestBoost > baseSpeed) {
m_SpeedBoost = highestBoost;
} else {
// No modifier counts: entities without level progression (enemies, pets) go back to the normal multiplier of 1
m_SpeedBoost = baseSpeed;
auto* levelProgressionComponent = m_Parent->GetComponent<LevelProgressionComponent>();
if (levelProgressionComponent) m_SpeedBoost = levelProgressionComponent->GetSpeedBase();
}
SetSpeedMultiplier(m_SpeedBoost / baseSpeed);
}
void ControllablePhysicsComponent::ActivateBubbleBuff(eBubbleType bubbleType, bool specialAnims) {
if (m_IsInBubble) {
LOG("Already in bubble");
return;
}
m_BubbleType = bubbleType;
m_IsInBubble = true;
m_DirtyBubble = true;
m_SpecialAnims = specialAnims;
Game::entityManager->SerializeEntity(m_Parent);
}
void ControllablePhysicsComponent::DeactivateBubbleBuff() {
m_DirtyBubble = true;
m_IsInBubble = false;
Game::entityManager->SerializeEntity(m_Parent);
};
void ControllablePhysicsComponent::ApplyStun(const GameMessages::SetStunned& stun) {
// Only characters are stunned from using items
if (!stun.bCantUseItem || !m_Parent->IsPlayer()) return;
if (!stun.bIgnoreImmunity && m_ImmuneToStunUseItemCount > 0) return;
m_StunnedUseItemCount = std::max(0, m_StunnedUseItemCount + (stun.StateChangeType == eStateChangeType::PUSH ? 1 : -1));
}
void ControllablePhysicsComponent::SetStunImmunity(
const eStateChangeType state,
const LWOOBJID originator,
const bool bImmuneToStunAttack,
const bool bImmuneToStunEquip,
const bool bImmuneToStunInteract,
const bool bImmuneToStunJump,
const bool bImmuneToStunMove,
const bool bImmuneToStunTurn,
const bool bImmuneToStunUseItem) {
if (state == eStateChangeType::POP) {
if (bImmuneToStunAttack && m_ImmuneToStunAttackCount > 0) m_ImmuneToStunAttackCount -= 1;
if (bImmuneToStunEquip && m_ImmuneToStunEquipCount > 0) m_ImmuneToStunEquipCount -= 1;
if (bImmuneToStunInteract && m_ImmuneToStunInteractCount > 0) m_ImmuneToStunInteractCount -= 1;
if (bImmuneToStunJump && m_ImmuneToStunJumpCount > 0) m_ImmuneToStunJumpCount -= 1;
if (bImmuneToStunMove && m_ImmuneToStunMoveCount > 0) m_ImmuneToStunMoveCount -= 1;
if (bImmuneToStunTurn && m_ImmuneToStunTurnCount > 0) m_ImmuneToStunTurnCount -= 1;
if (bImmuneToStunUseItem && m_ImmuneToStunUseItemCount > 0) m_ImmuneToStunUseItemCount -= 1;
} else if (state == eStateChangeType::PUSH) {
if (bImmuneToStunAttack) m_ImmuneToStunAttackCount += 1;
if (bImmuneToStunEquip) m_ImmuneToStunEquipCount += 1;
if (bImmuneToStunInteract) m_ImmuneToStunInteractCount += 1;
if (bImmuneToStunJump) m_ImmuneToStunJumpCount += 1;
if (bImmuneToStunMove) m_ImmuneToStunMoveCount += 1;
if (bImmuneToStunTurn) m_ImmuneToStunTurnCount += 1;
if (bImmuneToStunUseItem) m_ImmuneToStunUseItemCount += 1;
}
GameMessages::SetStunImmunity stunImmunity;
stunImmunity.target = m_Parent->GetObjectID();
stunImmunity.Caster = originator;
stunImmunity.StateChangeType = state;
stunImmunity.bImmuneToStunAttack = bImmuneToStunAttack;
stunImmunity.bImmuneToStunEquip = bImmuneToStunEquip;
stunImmunity.bImmuneToStunInteract = bImmuneToStunInteract;
stunImmunity.bImmuneToStunJump = bImmuneToStunJump;
stunImmunity.bImmuneToStunMove = bImmuneToStunMove;
stunImmunity.bImmuneToStunTurn = bImmuneToStunTurn;
stunImmunity.bImmuneToStunUseItem = bImmuneToStunUseItem;
stunImmunity.Send(m_Parent->GetSystemAddress());
}
bool ControllablePhysicsComponent::OnGetObjectReportInfo(GameMessages::GetObjectReportInfo& reportInfo) {
PhysicsComponent::OnGetObjectReportInfo(reportInfo);
auto& info = reportInfo.subCategory->PushDebug("Controllable Physics");
info.PushDebug("Velocity").PushDebug(m_Velocity);
info.PushDebug("Angular Velocity").PushDebug(m_AngularVelocity);
info.PushDebug<AMFBoolValue>("Is On Ground") = m_IsOnGround;
info.PushDebug<AMFBoolValue>("Is On Rail") = m_IsOnRail;
info.PushDebug<AMFBoolValue>("Is In Jetpack Mode") = m_InJetpackMode;
info.PushDebug<AMFBoolValue>("Is Jetpack Flying") = m_JetpackFlying;
info.PushDebug<AMFBoolValue>("Is Bypassing Jetpack Checks") = m_JetpackBypassChecks;
info.PushDebug<AMFIntValue>("Jetpack Effect ID") = m_JetpackEffectID;
info.PushDebug<AMFDoubleValue>("Speed Multiplier") = m_SpeedMultiplier;
info.PushDebug<AMFDoubleValue>("Gravity Scale") = m_GravityScale;
info.PushDebug<AMFBoolValue>("Is Static") = m_Static;
auto& pickupRadii = info.PushDebug("Active Pickup Radius Scales");
size_t i = 0;
for (const auto& scale : m_ActivePickupRadiusScales) {
pickupRadii.PushDebug<AMFStringValue>(std::to_string(i++) + " " + std::to_string(scale)) = "";
}
info.PushDebug<AMFDoubleValue>("Largest Pickup Radius") = m_PickupRadius;
info.PushDebug<AMFBoolValue>("Is Teleporting") = m_IsTeleporting;
auto& activeSpeedBoosts = info.PushDebug("Active Speed Boosts");
i = 0;
for (const auto& boost : m_ActiveSpeedBoosts) {
activeSpeedBoosts.PushDebug<AMFStringValue>(std::to_string(i++) + " " + std::to_string(boost)) = "";
}
info.PushDebug<AMFDoubleValue>("Speed Boost") = m_SpeedBoost;
info.PushDebug<AMFBoolValue>("Is In Bubble") = m_IsInBubble;
info.PushDebug<AMFStringValue>("Bubble Type") = StringifiedEnum::ToString(m_BubbleType).data();
info.PushDebug<AMFBoolValue>("Special Anims") = m_SpecialAnims;
auto& immunity = info.PushDebug("Immunity Effects");
immunity.PushDebug<AMFBoolValue>("Immune to Stun Move") = m_ImmuneToStunMoveCount != 0;
immunity.PushDebug<AMFBoolValue>("Immune to Stun Turn") = m_ImmuneToStunTurnCount != 0;
immunity.PushDebug<AMFBoolValue>("Immune to Stun Attack") = m_ImmuneToStunAttackCount != 0;
immunity.PushDebug<AMFBoolValue>("Immune to Stun Use Item") = m_ImmuneToStunUseItemCount != 0;
immunity.PushDebug<AMFBoolValue>("Immune to Stun Equip") = m_ImmuneToStunEquipCount != 0;
immunity.PushDebug<AMFBoolValue>("Immune to Stun Interact") = m_ImmuneToStunInteractCount != 0;
immunity.PushDebug<AMFBoolValue>("Immune to Stun Jump") = m_ImmuneToStunJumpCount != 0;
return true;
}
void ControllablePhysicsComponent::Allocator(Entity& entity, ObjectLoadState& state) {
const auto controllablePhysicsComponentID = state.GetComponentID(eReplicaComponentType::CONTROLLABLE_PHYSICS);
if (state.hasModelBehaviors) {
// LWOControllablePhysComponent::Allocator (0x0104e070): left to the model case
if (controllablePhysicsComponentID > 0) state.modelPhysicsComponentID = controllablePhysicsComponentID;
} else if (controllablePhysicsComponentID > 0) {
// The component places it: where the level put an object, where a character's save and the zone put it
entity.AddComponent<ControllablePhysicsComponent>(controllablePhysicsComponentID);
}
}