mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-02 10:53:44 +00:00
Converts PlayAnimation, PlayNDAudioEmitter, PlayEmbeddedEffectOnAllClientsNearObject,
PlayFXEffect, StopFXEffect, BroadcastTextToChatbox, Play2DAmbientSound,
Stop2DAmbientSound, UIMessageServerToSingleClient, UIMessageServerToAllClients,
StartCelebrationEffect, DisplayMessageBox, DisplayChatBubble, ChangeIdleFlags,
SetNameBillboardState, ShowBillboardInteractIcon, PlayCinematic, EndCinematic,
SlashCommandTextFeedback, PlayEmote and SetEmoteLockState (21 old Send
functions, 23 with overloads) and the received MessageBoxRespond,
ChoiceBoxRespond, CinematicUpdate and PlayEmote to NetGameMsgs in
EffectsMessages.{h,cpp}. The high traffic messages get a constructor for their
required fields. UI messages own their AMF arguments. Every caller is switched,
the received messages are registered in GameMessageHandler's map, and the old
functions and switch cases are deleted. Handlers are copied verbatim.
No wire change and no change in recipients: functions that always broadcast
whatever address they were given are sent with Send(UNASSIGNED_SYSTEM_ADDRESS),
functions that only did SEND_PACKET use SendToClient. Quirks are kept and
documented on the structs (PlayAnimation's UTF-8 sized name, the always written
null terminator in BroadcastTextToChatbox, StartCelebrationEffect always
writing celebrationID, SetNameBillboardState having no payload).
Verified byte for byte against a frozen verbatim copy of the old functions over
an input grid, to one client and broadcast; received messages compared with the
old handlers' read sequences and every truncated payload rejected; hand
computed golden bytes; round trips; a deliberate width mutation made the tests
fail. The byte-equality helper gains a Broadcast mode for functions that
ignored their address.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
193 lines
6.3 KiB
C++
193 lines
6.3 KiB
C++
#include "RenderComponent.h"
|
|
|
|
#include <algorithm>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <iomanip>
|
|
|
|
#include "Entity.h"
|
|
|
|
#include "CDClientManager.h"
|
|
#include "GameMessages.h"
|
|
#include "EffectsMessages.h"
|
|
#include "Game.h"
|
|
#include "Logger.h"
|
|
#include "CDAnimationsTable.h"
|
|
|
|
std::unordered_map<int32_t, float> RenderComponent::m_DurationCache{};
|
|
|
|
RenderComponent::RenderComponent(Entity* const parentEntity, const int32_t componentID) : Component{ parentEntity, componentID } {
|
|
m_LastAnimationName = "";
|
|
if (componentID == -1) return;
|
|
|
|
auto query = CDClientDatabase::CreatePreppedStmt("SELECT * FROM RenderComponent WHERE id = ?;");
|
|
query.bind(1, componentID);
|
|
auto result = query.execQuery();
|
|
|
|
if (!result.eof()) {
|
|
auto animationGroupIDs = std::string(result.getStringField("animationGroupIDs", ""));
|
|
if (!animationGroupIDs.empty()) {
|
|
auto* animationsTable = CDClientManager::GetTable<CDAnimationsTable>();
|
|
auto groupIdsSplit = GeneralUtils::SplitString(animationGroupIDs, ',');
|
|
for (auto& groupId : groupIdsSplit) {
|
|
const auto groupIdInt = GeneralUtils::TryParse<int32_t>(groupId);
|
|
|
|
if (!groupIdInt) {
|
|
LOG("bad animation group Id %s", groupId.c_str());
|
|
continue;
|
|
}
|
|
|
|
m_animationGroupIds.push_back(groupIdInt.value());
|
|
animationsTable->CacheAnimationGroup(groupIdInt.value());
|
|
}
|
|
}
|
|
}
|
|
result.finalize();
|
|
}
|
|
|
|
void RenderComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
|
|
if (!bIsInitialUpdate) return;
|
|
|
|
outBitStream.Write<uint32_t>(m_Effects.size());
|
|
|
|
for (auto& eff : m_Effects) {
|
|
outBitStream.Write<uint8_t>(eff.name.size());
|
|
// if there is no name, then we don't write anything else
|
|
if (eff.name.empty()) continue;
|
|
|
|
for (const auto& value : eff.name) outBitStream.Write<uint8_t>(value);
|
|
|
|
outBitStream.Write(eff.effectID);
|
|
|
|
outBitStream.Write<uint8_t>(eff.type.size());
|
|
for (const auto& value : eff.type) outBitStream.Write<uint16_t>(value);
|
|
|
|
outBitStream.Write<float_t>(eff.priority);
|
|
outBitStream.Write<int64_t>(eff.secondary);
|
|
}
|
|
}
|
|
|
|
Effect& RenderComponent::AddEffect(const int32_t effectId, const std::string& name, const std::u16string& type, const float priority) {
|
|
return m_Effects.emplace_back(effectId, name, type, priority);
|
|
}
|
|
|
|
void RenderComponent::RemoveEffect(const std::string& name) {
|
|
if (m_Effects.empty()) return;
|
|
|
|
const auto effectToRemove = std::ranges::find_if(m_Effects, [&name](auto&& effect) { return effect.name == name; });
|
|
if (effectToRemove == m_Effects.end()) return; // Return early if effect is not present
|
|
|
|
const auto lastEffect = m_Effects.rbegin();
|
|
*effectToRemove = std::move(*lastEffect); // Move-overwrite
|
|
m_Effects.pop_back();
|
|
}
|
|
|
|
void RenderComponent::Update(const float deltaTime) {
|
|
for (auto& effect : m_Effects) {
|
|
if (effect.time == 0) continue; // Skip persistent effects
|
|
|
|
const auto result = effect.time - deltaTime;
|
|
if (result <= 0) continue;
|
|
|
|
effect.time = result;
|
|
}
|
|
}
|
|
|
|
void RenderComponent::PlayEffect(const int32_t effectId, const std::u16string& effectType, const std::string& name, const LWOOBJID secondary, const float priority, const float scale, const bool serialize) {
|
|
RemoveEffect(name);
|
|
|
|
GameMessages::PlayFXEffect fx(m_Parent->GetObjectID(), effectId, effectType, name);
|
|
fx.secondary = secondary;
|
|
fx.priority = priority;
|
|
fx.scale = scale;
|
|
fx.serialize = serialize;
|
|
fx.Send(UNASSIGNED_SYSTEM_ADDRESS);
|
|
|
|
auto& effect = AddEffect(effectId, name, effectType, priority);
|
|
|
|
const auto& pair = m_DurationCache.find(effectId);
|
|
|
|
if (pair != m_DurationCache.end()) {
|
|
effect.time = pair->second;
|
|
|
|
return;
|
|
}
|
|
|
|
const std::string effectType_str = GeneralUtils::UTF16ToWTF8(effectType);
|
|
|
|
auto query = CDClientDatabase::CreatePreppedStmt(
|
|
"SELECT animation_length FROM Animations WHERE animation_type IN (SELECT animationName FROM BehaviorEffect WHERE effectID = ? AND effectType = ?);");
|
|
query.bind(1, effectId);
|
|
query.bind(2, effectType_str.c_str());
|
|
|
|
auto result = query.execQuery();
|
|
|
|
if (result.eof() || result.fieldIsNull("animation_length")) {
|
|
result.finalize();
|
|
|
|
m_DurationCache[effectId] = 0;
|
|
|
|
effect.time = 0; // Persistent effect
|
|
|
|
return;
|
|
}
|
|
|
|
effect.time = static_cast<float>(result.getFloatField("animation_length"));
|
|
|
|
result.finalize();
|
|
|
|
m_DurationCache[effectId] = effect.time;
|
|
}
|
|
|
|
void RenderComponent::StopEffect(const std::string& name, const bool killImmediate) {
|
|
GameMessages::StopFXEffect(m_Parent->GetObjectID(), killImmediate, name).Send(UNASSIGNED_SYSTEM_ADDRESS);
|
|
|
|
RemoveEffect(name);
|
|
}
|
|
|
|
float RenderComponent::PlayAnimation(Entity* self, const std::u16string& animation, float priority, float scale) {
|
|
if (!self) return 0.0f;
|
|
return RenderComponent::PlayAnimation(self, GeneralUtils::UTF16ToWTF8(animation), priority, scale);
|
|
}
|
|
|
|
float RenderComponent::PlayAnimation(Entity* self, const std::string& animation, float priority, float scale) {
|
|
if (!self) return 0.0f;
|
|
return RenderComponent::DoAnimation(self, animation, true, priority, scale);
|
|
}
|
|
|
|
float RenderComponent::GetAnimationTime(Entity* self, const std::u16string& animation) {
|
|
if (!self) return 0.0f;
|
|
return RenderComponent::GetAnimationTime(self, GeneralUtils::UTF16ToWTF8(animation));
|
|
}
|
|
|
|
float RenderComponent::GetAnimationTime(Entity* self, const std::string& animation) {
|
|
if (!self) return 0.0f;
|
|
return RenderComponent::DoAnimation(self, animation, false);
|
|
}
|
|
|
|
|
|
float RenderComponent::DoAnimation(Entity* self, const std::string& animation, bool sendAnimation, float priority, float scale) {
|
|
float returnlength = 0.0f;
|
|
if (!self) return returnlength;
|
|
auto* renderComponent = self->GetComponent<RenderComponent>();
|
|
if (!renderComponent) return returnlength;
|
|
|
|
auto* animationsTable = CDClientManager::GetTable<CDAnimationsTable>();
|
|
for (auto& groupId : renderComponent->m_animationGroupIds) {
|
|
auto animationGroup = animationsTable->GetAnimation(animation, renderComponent->GetLastAnimationName(), groupId);
|
|
if (animationGroup) {
|
|
renderComponent->SetLastAnimationName(animationGroup->animation_name);
|
|
returnlength = animationGroup->animation_length;
|
|
}
|
|
}
|
|
if (sendAnimation) {
|
|
GameMessages::PlayAnimation playAnimation(self->GetObjectID(), GeneralUtils::ASCIIToUTF16(animation));
|
|
playAnimation.fPriority = priority;
|
|
playAnimation.fScale = scale;
|
|
playAnimation.Send(UNASSIGNED_SYSTEM_ADDRESS);
|
|
}
|
|
if (returnlength == 0.0f) LOG("WARNING: Unable to find animation %s for lot %i in any group.", animation.c_str(), self->GetLOT());
|
|
return returnlength;
|
|
}
|