Fix bugs in animation queue handling, and add animation timeouts

This commit is contained in:
2025-07-02 16:01:18 -04:00
parent c0307b970b
commit fd453e6b30
9 changed files with 251 additions and 106 deletions

View File

@@ -2,6 +2,7 @@
#include "AnimQueue.h"
#include "UtilityLibrary.h"
#include "GameFramework/Actor.h"
#include <iostream>
FlxAnimationStep::FlxAnimationStep(uint64 hash, std::string_view body) {
Finished = false;
@@ -140,7 +141,7 @@ void UlxAnimationStepLibrary::UnpackAnimationStep(bool &bChanged, FString &Actio
FString UlxAnimationStepLibrary::AnimationStepDebugString(const FlxAnimationStep& step) {
std::string_view body((const char*)(step.Body.GetData()), step.Body.Num());
return FlxAnimationStepDecoder::DebugString(step.Finished, step.Finished, step.Hash, body);
return FlxAnimationStepDecoder::DebugString(step.Finished, step.Hash, body);
}
static FlxAnimationField FindAnimationFieldLL(const FlxAnimationStep& step, std::string_view name) {
@@ -264,16 +265,15 @@ FlxAnimationField FlxAnimationStepDecoder::ReadField() {
return result;
}
FString FlxAnimationStepDecoder::DebugString(bool injectidle, bool persistentonly, uint64 hash, std::string_view body) {
FString FlxAnimationStepDecoder::DebugString(bool finished, uint64 hash, std::string_view body) {
FString result;
FlxAnimationStepDecoder decoder(body);
result.Appendf(TEXT("Hash=%016llx"), hash);
if (injectidle) {
result.Appendf(TEXT(" action=idle"));
if (finished) {
result.Appendf(TEXT(" finished"));
}
while (!decoder.AtEOF()) {
FlxAnimationField field = decoder.ReadField();
if (persistentonly && !field.Persistent) continue;
result.Append(TEXT(" "));
result.Append(FString(field.Name.size(), (const UTF8CHAR*)field.Name.data()));
result.Append(field.Persistent ? TEXT("=") : TEXT(":"));
@@ -317,16 +317,16 @@ FlxAnimQueueDecoder::FlxAnimQueueDecoder(std::string_view queue) : Decoder(queue
ActualSize = Decoder.read_uint8();
}
FString FlxAnimQueueDecoder::DebugString(std::string_view queue) {
FString result;
FlxAnimQueueDecoder decoder(queue);
while (!decoder.AtEOF()) {
FlxAnimationStepView step = decoder.ReadStep();
FString stepdebug = FlxAnimationStepDecoder::DebugString(false, false, step.Hash, step.Body);
result.Appendf(TEXT("%s\n"), *stepdebug);
}
return result;
}
// FString FlxAnimQueueDecoder::DebugString(std::string_view queue) {
// FString result;
// FlxAnimQueueDecoder decoder(queue);
// while (!decoder.AtEOF()) {
// FlxAnimationStepView step = decoder.ReadStep();
// FString stepdebug = FlxAnimationStepDecoder::DebugString(false, step.Hash, step.Body);
// result.Appendf(TEXT("%s\n"), *stepdebug);
// }
// return result;
// }
FlxAnimTracker::FlxAnimTracker() {
Clear();
@@ -358,6 +358,30 @@ void FlxAnimTracker::SkipToEnd() {
}
}
const FlxAnimationStep *FlxAnimTracker::FirstUnfinished() const
{
for (int i = 0; i < AQ.Num(); i++) {
if (!AQ[i].Finished) return &AQ[i];
}
return nullptr;
}
const FlxAnimationStep *FlxAnimTracker::LastFinished() const
{
for (int i = AQ.Num() - 1; i >= 0; i--) {
if (AQ[i].Finished) return &AQ[i];
}
return nullptr;
}
const FlxAnimationStep *FlxAnimTracker::FindAnimation(uint64 hash) const
{
for (int i = 0; i < AQ.Num(); i++) {
if (AQ[i].Hash == hash) return &AQ[i];
}
return nullptr;
}
void FlxAnimTracker::Update(std::string_view encqueue) {
check(!encqueue.empty());
@@ -409,14 +433,14 @@ void FlxAnimTracker::Update(std::string_view encqueue) {
int32 nremove = (AQ.Num() - (matchingindex + 1));
check((nremove >= 0) && (nremove <= AQ.Num()));
for (int32 i = 0; i < nremove; i++) {
AQ.PopLast();
AQ.Pop();
}
// Transfer the new animations onto the queue.
//
while (!newsteps.IsEmpty()) {
FlxAnimationStepView step = newsteps.Pop();
AQ.EmplaceLast(step.Hash, step.Body);
AQ.Emplace(step.Hash, step.Body);
}
// If there are too many animations in AQ, discard
@@ -425,12 +449,15 @@ void FlxAnimTracker::Update(std::string_view encqueue) {
// TODO: this is hardwired to keep 10. Instead, we
// should keep the number specified in the queue.
//
int limit = 10;
int ndiscard = AQ.Num() - limit;
if (ndiscard > 0) {
for (int i = 0; i < ndiscard; i++) {
AQ.PopFirst();
const int limit = 10;
if (AQ.Num() > limit)
{
int offset = AQ.Num() - limit;
for (int i = 0; i < limit; i++)
{
AQ[i] = AQ[i + offset];
}
AQ.SetNum(limit);
}
// Autofinish up to one animation.
@@ -450,6 +477,34 @@ void FlxAnimTracker::Update(std::string_view encqueue) {
}
}
FString FlxAnimTracker::DebugString() const
{
FString Result;
if (Changed)
{
Result += TEXT("changed=true ");
}
else
{
Result += TEXT("changed=false ");
}
if (AutoFinish)
{
Result += TEXT("autofinish=true");
}
else
{
Result += TEXT("autofinish=false");
}
Result += TEXT("\n");
for (int i = 0; i < AQ.Num(); i++)
{
Result += UlxAnimationStepLibrary::AnimationStepDebugString(AQ[i]);
Result += TEXT("\n");
}
return Result;
}
void FlxAnimTracker::SetAutoFinish(const FString &action, const FVector &xyz) {
AutoFinish = true;
AutoFinishAction = action;
@@ -486,3 +541,4 @@ FlxAnimationStep FlxAnimTracker::GetCurrentAnimation() {
result.Hash += 1;
return result;
}