2021-12-05 17:54:36 +00:00
|
|
|
#include "CDAnimationsTable.h"
|
2023-03-20 13:10:52 +00:00
|
|
|
#include "GeneralUtils.h"
|
|
|
|
#include "Game.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-08-11 04:27:40 +00:00
|
|
|
|
|
|
|
void CDAnimationsTable::LoadValuesFromDatabase() {
|
|
|
|
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM Animations");
|
2024-02-09 13:37:58 +00:00
|
|
|
auto& animations = GetEntriesMutable();
|
2023-08-11 04:27:40 +00:00
|
|
|
while (!tableData.eof()) {
|
|
|
|
std::string animation_type = tableData.getStringField("animation_type", "");
|
|
|
|
DluAssert(!animation_type.empty());
|
|
|
|
AnimationGroupID animationGroupID = tableData.getIntField("animationGroupID", -1);
|
|
|
|
DluAssert(animationGroupID != -1);
|
|
|
|
|
|
|
|
CDAnimation entry;
|
|
|
|
entry.animation_name = tableData.getStringField("animation_name", "");
|
|
|
|
entry.chance_to_play = tableData.getFloatField("chance_to_play", 1.0f);
|
|
|
|
UNUSED_COLUMN(entry.min_loops = tableData.getIntField("min_loops", 0);)
|
|
|
|
UNUSED_COLUMN(entry.max_loops = tableData.getIntField("max_loops", 0);)
|
|
|
|
entry.animation_length = tableData.getFloatField("animation_length", 0.0f);
|
|
|
|
UNUSED_COLUMN(entry.hideEquip = tableData.getIntField("hideEquip", 0) == 1;)
|
|
|
|
UNUSED_COLUMN(entry.ignoreUpperBody = tableData.getIntField("ignoreUpperBody", 0) == 1;)
|
|
|
|
UNUSED_COLUMN(entry.restartable = tableData.getIntField("restartable", 0) == 1;)
|
|
|
|
UNUSED_COLUMN(entry.face_animation_name = tableData.getStringField("face_animation_name", "");)
|
|
|
|
UNUSED_COLUMN(entry.priority = tableData.getFloatField("priority", 0.0f);)
|
|
|
|
UNUSED_COLUMN(entry.blendTime = tableData.getFloatField("blendTime", 0.0f);)
|
|
|
|
|
2024-02-09 13:37:58 +00:00
|
|
|
animations[CDAnimationKey(animation_type, animationGroupID)].push_back(entry);
|
2023-08-11 04:27:40 +00:00
|
|
|
tableData.nextRow();
|
|
|
|
}
|
|
|
|
|
|
|
|
tableData.finalize();
|
|
|
|
}
|
|
|
|
|
2023-03-26 10:09:04 +00:00
|
|
|
bool CDAnimationsTable::CacheData(CppSQLite3Statement& queryToCache) {
|
2023-03-20 13:10:52 +00:00
|
|
|
auto tableData = queryToCache.execQuery();
|
|
|
|
// If we received a bad lookup, cache it anyways so we do not run the query again.
|
|
|
|
if (tableData.eof()) return false;
|
2024-02-09 13:37:58 +00:00
|
|
|
auto& animations = GetEntriesMutable();
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-03-20 13:10:52 +00:00
|
|
|
do {
|
|
|
|
std::string animation_type = tableData.getStringField("animation_type", "");
|
|
|
|
DluAssert(!animation_type.empty());
|
|
|
|
AnimationGroupID animationGroupID = tableData.getIntField("animationGroupID", -1);
|
|
|
|
DluAssert(animationGroupID != -1);
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-03-20 13:10:52 +00:00
|
|
|
CDAnimation entry;
|
2023-01-07 09:48:59 +00:00
|
|
|
entry.animation_name = tableData.getStringField("animation_name", "");
|
2023-03-20 13:10:52 +00:00
|
|
|
entry.chance_to_play = tableData.getFloatField("chance_to_play", 1.0f);
|
2023-03-26 10:09:04 +00:00
|
|
|
UNUSED_COLUMN(entry.min_loops = tableData.getIntField("min_loops", 0);)
|
|
|
|
UNUSED_COLUMN(entry.max_loops = tableData.getIntField("max_loops", 0);)
|
2023-03-20 13:10:52 +00:00
|
|
|
entry.animation_length = tableData.getFloatField("animation_length", 0.0f);
|
2023-03-26 10:09:04 +00:00
|
|
|
UNUSED_COLUMN(entry.hideEquip = tableData.getIntField("hideEquip", 0) == 1;)
|
|
|
|
UNUSED_COLUMN(entry.ignoreUpperBody = tableData.getIntField("ignoreUpperBody", 0) == 1;)
|
|
|
|
UNUSED_COLUMN(entry.restartable = tableData.getIntField("restartable", 0) == 1;)
|
|
|
|
UNUSED_COLUMN(entry.face_animation_name = tableData.getStringField("face_animation_name", "");)
|
|
|
|
UNUSED_COLUMN(entry.priority = tableData.getFloatField("priority", 0.0f);)
|
|
|
|
UNUSED_COLUMN(entry.blendTime = tableData.getFloatField("blendTime", 0.0f);)
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2024-02-09 13:37:58 +00:00
|
|
|
animations[CDAnimationKey(animation_type, animationGroupID)].push_back(entry);
|
2021-12-05 17:54:36 +00:00
|
|
|
tableData.nextRow();
|
2023-03-20 13:10:52 +00:00
|
|
|
} while (!tableData.eof());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
tableData.finalize();
|
2023-03-20 13:10:52 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDAnimationsTable::CacheAnimations(const CDAnimationKey animationKey) {
|
|
|
|
auto query = CDClientDatabase::CreatePreppedStmt("SELECT * FROM Animations WHERE animationGroupID = ? and animation_type = ?");
|
|
|
|
query.bind(1, static_cast<int32_t>(animationKey.second));
|
|
|
|
query.bind(2, animationKey.first.c_str());
|
2024-02-09 13:37:58 +00:00
|
|
|
auto& animations = GetEntriesMutable();
|
2023-03-20 13:10:52 +00:00
|
|
|
// If we received a bad lookup, cache it anyways so we do not run the query again.
|
|
|
|
if (!CacheData(query)) {
|
2024-02-09 13:37:58 +00:00
|
|
|
animations[animationKey];
|
2023-03-20 13:10:52 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 13:10:52 +00:00
|
|
|
void CDAnimationsTable::CacheAnimationGroup(AnimationGroupID animationGroupID) {
|
2024-02-09 13:37:58 +00:00
|
|
|
auto& animations = GetEntriesMutable();
|
|
|
|
auto animationEntryCached = animations.find(CDAnimationKey("", animationGroupID));
|
|
|
|
if (animationEntryCached != animations.end()) {
|
2023-03-20 13:10:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-03-20 13:10:52 +00:00
|
|
|
auto query = CDClientDatabase::CreatePreppedStmt("SELECT * FROM Animations WHERE animationGroupID = ?");
|
|
|
|
query.bind(1, static_cast<int32_t>(animationGroupID));
|
2022-07-28 13:39:57 +00:00
|
|
|
|
2023-03-20 13:10:52 +00:00
|
|
|
// Cache the query so we don't run the query again.
|
|
|
|
CacheData(query);
|
2024-02-09 13:37:58 +00:00
|
|
|
animations[CDAnimationKey("", animationGroupID)];
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 13:37:58 +00:00
|
|
|
std::optional<CDAnimation> CDAnimationsTable::GetAnimation(const AnimationID& animationType, const std::string& previousAnimationName, const AnimationGroupID animationGroupID) {
|
|
|
|
auto& animations = GetEntriesMutable();
|
2023-03-20 13:10:52 +00:00
|
|
|
CDAnimationKey animationKey(animationType, animationGroupID);
|
2024-02-09 13:37:58 +00:00
|
|
|
auto animationEntryCached = animations.find(animationKey);
|
|
|
|
if (animationEntryCached == animations.end()) {
|
2023-03-20 13:10:52 +00:00
|
|
|
this->CacheAnimations(animationKey);
|
|
|
|
}
|
2023-01-07 09:48:59 +00:00
|
|
|
|
2024-02-09 13:37:58 +00:00
|
|
|
auto animationEntry = animations.find(animationKey);
|
2023-03-20 13:10:52 +00:00
|
|
|
// If we have only one animation, return it regardless of the chance to play.
|
|
|
|
if (animationEntry->second.size() == 1) {
|
2024-02-09 13:37:58 +00:00
|
|
|
return animationEntry->second.front();
|
2023-03-20 13:10:52 +00:00
|
|
|
}
|
2023-03-26 10:09:04 +00:00
|
|
|
auto randomAnimation = GeneralUtils::GenerateRandomNumber<float>(0, 1);
|
2023-03-20 13:10:52 +00:00
|
|
|
|
|
|
|
for (auto& animationEntry : animationEntry->second) {
|
|
|
|
randomAnimation -= animationEntry.chance_to_play;
|
|
|
|
// This is how the client gets the random animation.
|
2024-02-09 13:37:58 +00:00
|
|
|
if (animationEntry.animation_name != previousAnimationName && randomAnimation <= 0.0f) return animationEntry;
|
2023-03-20 13:10:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 13:37:58 +00:00
|
|
|
return std::nullopt;
|
2023-03-20 13:10:52 +00:00
|
|
|
}
|