2021-12-05 17:54:36 +00:00
|
|
|
#include "RenderComponent.h"
|
|
|
|
|
2024-01-31 14:38:38 +00:00
|
|
|
#include <algorithm>
|
2021-12-05 17:54:36 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2024-01-31 14:38:38 +00:00
|
|
|
#include <utility>
|
2021-12-05 17:54:36 +00:00
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
#include "Entity.h"
|
|
|
|
|
|
|
|
#include "CDClientManager.h"
|
|
|
|
#include "GameMessages.h"
|
|
|
|
#include "Game.h"
|
2023-10-21 23:31:55 +00:00
|
|
|
#include "Logger.h"
|
2023-03-20 13:10:52 +00:00
|
|
|
#include "CDAnimationsTable.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
std::unordered_map<int32_t, float> RenderComponent::m_DurationCache{};
|
|
|
|
|
2024-01-31 14:38:38 +00:00
|
|
|
RenderComponent::RenderComponent(Entity* const parentEntity, const int32_t componentId) : Component{ parentEntity } {
|
2023-03-20 13:10:52 +00:00
|
|
|
m_LastAnimationName = "";
|
2023-03-26 10:09:04 +00:00
|
|
|
if (componentId == -1) return;
|
|
|
|
|
2023-03-20 13:10:52 +00:00
|
|
|
auto query = CDClientDatabase::CreatePreppedStmt("SELECT * FROM RenderComponent WHERE id = ?;");
|
|
|
|
query.bind(1, componentId);
|
|
|
|
auto result = query.execQuery();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-03-20 13:10:52 +00:00
|
|
|
if (!result.eof()) {
|
|
|
|
auto animationGroupIDs = std::string(result.getStringField("animationGroupIDs", ""));
|
|
|
|
if (!animationGroupIDs.empty()) {
|
2024-02-09 05:40:43 +00:00
|
|
|
auto* animationsTable = CDClientManager::GetTable<CDAnimationsTable>();
|
2023-03-20 13:10:52 +00:00
|
|
|
auto groupIdsSplit = GeneralUtils::SplitString(animationGroupIDs, ',');
|
|
|
|
for (auto& groupId : groupIdsSplit) {
|
2024-02-10 11:05:25 +00:00
|
|
|
const auto groupIdInt = GeneralUtils::TryParse<int32_t>(groupId);
|
|
|
|
|
|
|
|
if (!groupIdInt) {
|
2023-10-21 23:31:55 +00:00
|
|
|
LOG("bad animation group Id %s", groupId.c_str());
|
2023-03-20 13:10:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2024-02-10 11:05:25 +00:00
|
|
|
|
|
|
|
m_animationGroupIds.push_back(groupIdInt.value());
|
|
|
|
animationsTable->CacheAnimationGroup(groupIdInt.value());
|
2023-03-20 13:10:52 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result.finalize();
|
|
|
|
}
|
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
void RenderComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
|
2021-12-05 17:54:36 +00:00
|
|
|
if (!bIsInitialUpdate) return;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
outBitStream.Write<uint32_t>(m_Effects.size());
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-01-31 14:38:38 +00:00
|
|
|
for (auto& eff : m_Effects) {
|
2024-02-27 07:25:44 +00:00
|
|
|
outBitStream.Write<uint8_t>(eff.name.size());
|
2023-10-09 20:20:56 +00:00
|
|
|
// if there is no name, then we don't write anything else
|
2024-01-31 14:38:38 +00:00
|
|
|
if (eff.name.empty()) continue;
|
2023-10-09 20:20:56 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
for (const auto& value : eff.name) outBitStream.Write<uint8_t>(value);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
outBitStream.Write(eff.effectID);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
outBitStream.Write<uint8_t>(eff.type.size());
|
|
|
|
for (const auto& value : eff.type) outBitStream.Write<uint16_t>(value);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-27 07:25:44 +00:00
|
|
|
outBitStream.Write<float_t>(eff.priority);
|
|
|
|
outBitStream.Write<int64_t>(eff.secondary);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-31 14:38:38 +00:00
|
|
|
Effect& RenderComponent::AddEffect(const int32_t effectId, const std::string& name, const std::u16string& type, const float priority) {
|
2024-02-01 15:43:28 +00:00
|
|
|
return m_Effects.emplace_back(effectId, name, type, priority);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderComponent::RemoveEffect(const std::string& name) {
|
2024-01-31 14:38:38 +00:00
|
|
|
if (m_Effects.empty()) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-31 14:38:38 +00:00
|
|
|
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
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2024-01-31 14:38:38 +00:00
|
|
|
const auto lastEffect = m_Effects.rbegin();
|
|
|
|
*effectToRemove = std::move(*lastEffect); // Move-overwrite
|
|
|
|
m_Effects.pop_back();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-01-31 14:38:38 +00:00
|
|
|
void RenderComponent::Update(const float deltaTime) {
|
|
|
|
for (auto& effect : m_Effects) {
|
|
|
|
if (effect.time == 0) continue; // Skip persistent effects
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-01-31 14:38:38 +00:00
|
|
|
const auto result = effect.time - deltaTime;
|
|
|
|
if (result <= 0) continue;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-01-31 14:38:38 +00:00
|
|
|
effect.time = result;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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::SendPlayFXEffect(m_Parent, effectId, effectType, name, secondary, priority, scale, serialize);
|
|
|
|
|
2024-01-31 14:38:38 +00:00
|
|
|
auto& effect = AddEffect(effectId, name, effectType, priority);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
const auto& pair = m_DurationCache.find(effectId);
|
|
|
|
|
|
|
|
if (pair != m_DurationCache.end()) {
|
2024-01-31 14:38:38 +00:00
|
|
|
effect.time = pair->second;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-01-06 21:05:03 +00:00
|
|
|
return;
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2022-01-13 03:48:27 +00:00
|
|
|
|
|
|
|
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());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
auto result = query.execQuery();
|
|
|
|
|
2024-04-14 04:41:51 +00:00
|
|
|
if (result.eof() || result.fieldIsNull("animation_length")) {
|
2021-12-05 17:54:36 +00:00
|
|
|
result.finalize();
|
|
|
|
|
|
|
|
m_DurationCache[effectId] = 0;
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-01-31 14:38:38 +00:00
|
|
|
effect.time = 0; // Persistent effect
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-04-14 04:41:51 +00:00
|
|
|
effect.time = static_cast<float>(result.getFloatField("animation_length"));
|
2022-01-06 21:05:03 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
result.finalize();
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-01-31 14:38:38 +00:00
|
|
|
m_DurationCache[effectId] = effect.time;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderComponent::StopEffect(const std::string& name, const bool killImmediate) {
|
|
|
|
GameMessages::SendStopFXEffect(m_Parent, killImmediate, name);
|
|
|
|
|
|
|
|
RemoveEffect(name);
|
|
|
|
}
|
|
|
|
|
2023-03-20 13:10:52 +00:00
|
|
|
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) {
|
2023-03-26 10:09:04 +00:00
|
|
|
float returnlength = 0.0f;
|
|
|
|
if (!self) return returnlength;
|
2023-03-20 13:10:52 +00:00
|
|
|
auto* renderComponent = self->GetComponent<RenderComponent>();
|
2023-03-26 10:09:04 +00:00
|
|
|
if (!renderComponent) return returnlength;
|
2023-03-20 13:10:52 +00:00
|
|
|
|
2024-02-09 05:40:43 +00:00
|
|
|
auto* animationsTable = CDClientManager::GetTable<CDAnimationsTable>();
|
2023-03-20 13:10:52 +00:00
|
|
|
for (auto& groupId : renderComponent->m_animationGroupIds) {
|
|
|
|
auto animationGroup = animationsTable->GetAnimation(animation, renderComponent->GetLastAnimationName(), groupId);
|
2024-02-09 13:37:58 +00:00
|
|
|
if (animationGroup) {
|
|
|
|
renderComponent->SetLastAnimationName(animationGroup->animation_name);
|
|
|
|
returnlength = animationGroup->animation_length;
|
2023-03-20 13:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-26 10:09:04 +00:00
|
|
|
if (sendAnimation) GameMessages::SendPlayAnimation(self, GeneralUtils::ASCIIToUTF16(animation), priority, scale);
|
2023-10-21 23:31:55 +00:00
|
|
|
if (returnlength == 0.0f) LOG("WARNING: Unable to find animation %s for lot %i in any group.", animation.c_str(), self->GetLOT());
|
2023-03-26 10:09:04 +00:00
|
|
|
return returnlength;
|
2023-03-20 13:10:52 +00:00
|
|
|
}
|