Crusader_Decomp/rebuild/remorse/tests/animation_archive_tests.cpp

183 lines
8.4 KiB
C++
Raw Normal View History

#include "remorse/animation_archive.h"
#include "remorse/file_system.h"
#include "remorse/isometric_controls.h"
#include "remorse/shape_archive.h"
#include <cassert>
#include <array>
#include <filesystem>
namespace {
void writeU32Le(std::vector<remorse::u8>& bytes, std::size_t offset, remorse::u32 value)
{
for (std::size_t index = 0; index < 4; ++index) {
bytes[offset + index] = static_cast<remorse::u8>(value >> (index * 8));
}
}
remorse::AnimationArchive makeSyntheticArchive()
{
constexpr remorse::u32 shapeTable = 0x20;
constexpr remorse::u32 clipOffset = 0x440;
std::vector<remorse::u8> bytes(clipOffset + 4 + 2 * 16 * 8);
writeU32Le(bytes, 4, shapeTable);
writeU32Le(bytes, shapeTable + 4, clipOffset);
bytes[clipOffset] = 2;
bytes[clipOffset + 1] = 5;
bytes[clipOffset + 2] = 0x02;
bytes[clipOffset + 3] = 0x40;
const std::size_t frameOffset = clipOffset + 4 + (5 * 2 + 1) * 8;
bytes[frameOffset] = 0x34;
bytes[frameOffset + 1] = 0x81;
bytes[frameOffset + 2] = 0xfe;
bytes[frameOffset + 4] = 0xff;
bytes[frameOffset + 5] = 0x01;
return remorse::AnimationArchive{std::move(bytes)};
}
} // namespace
int main()
{
const auto synthetic = makeSyntheticArchive();
const auto sixteenDirectionClip = remorse::loadAnimationClip(synthetic, 1, 1);
assert(sixteenDirectionClip.has_value());
assert(sixteenDirectionClip->frameCount == 2);
assert(sixteenDirectionClip->directionCount == 16);
const auto selectedFrame = remorse::getAnimationFrame(*sixteenDirectionClip, 5, 1);
assert(selectedFrame.has_value());
assert(remorse::getAnimationShapeFrame(*selectedFrame) == 0x134);
assert(remorse::isAnimationFrameFlipped(*selectedFrame));
const auto rotatedStep = remorse::decodeAnimationDisplacement(*selectedFrame, 5, true);
assert(rotatedStep.has_value());
assert(rotatedStep->x == 2);
assert(rotatedStep->y == -4);
assert(rotatedStep->z == -2);
assert(!rotatedStep->onGround);
assert(!remorse::getAnimationFrame(*sixteenDirectionClip, 16, 0));
assert(!remorse::getAnimationFrame(*sixteenDirectionClip, 0, 2));
remorse::AnimationFrameMetadata negativeStride;
negativeStride.bytes[4] = 0xff;
negativeStride.bytes[5] = 0x01;
const auto northStep = remorse::decodeAnimationDisplacement(negativeStride, 0, false);
assert(northStep.has_value());
assert(northStep->x == 0);
assert(northStep->y == 2);
negativeStride.bytes[5] = 0x03;
const auto groundedStep = remorse::decodeAnimationDisplacement(negativeStride, 0, false);
assert(groundedStep.has_value() && groundedStep->onGround);
remorse::AnimationFrameMetadata positiveStride;
positiveStride.bytes[4] = 1;
const remorse::s32 expectedX[16] = {
0, 2, 2, 4, 2, 4, 2, 2, 0, -2, -2, -4, -2, -4, -2, -2};
const remorse::s32 expectedY[16] = {
-2, -4, -2, -2, 0, 2, 2, 4, 2, 4, 2, 2, 0, -2, -2, -4};
for (remorse::u8 direction = 0; direction < 16; ++direction) {
const auto step = remorse::decodeAnimationDisplacement(positiveStride, direction, false);
assert(step.has_value());
assert(step->x == expectedX[direction]);
assert(step->y == expectedY[direction]);
}
remorse::AnimationArchive malformed;
malformed.bytes.resize(8);
writeU32Le(malformed.bytes, 4, 0xfffffff0);
assert(!remorse::loadAnimationClip(malformed, 1, 0));
const auto root = remorse::findGameDataRoot(std::filesystem::current_path());
assert(root.has_value());
const auto animationPath = remorse::resolveGameDataPath(
*root, std::filesystem::path("STATIC") / "ANIM.DAT");
assert(animationPath.has_value());
const auto animationArchive = remorse::loadAnimationArchive(*animationPath);
assert(animationArchive.has_value());
const auto stand = remorse::loadAnimationClip(*animationArchive, 1, 0);
const auto walk = remorse::loadAnimationClip(*animationArchive, 1, 1);
const auto run = remorse::loadAnimationClip(*animationArchive, 1, 3);
assert(stand.has_value() && stand->frameCount > 0);
assert(walk.has_value() && walk->frameCount == 17);
assert(run.has_value() && run->frameCount == 12);
std::array<std::optional<remorse::AnimationClip>, 0x40> playerAnimations;
for (const remorse::u8 index : remorse::controllerAnimationIndices) {
playerAnimations[index] = remorse::loadAnimationClip(
*animationArchive, 1, index);
}
for (const remorse::u8 animation : remorse::controllerAnimationIndices) {
assert(playerAnimations[animation] && playerAnimations[animation]->frameCount != 0);
}
for (const remorse::AnimationClip* clip : {&*walk, &*run}) {
assert(clip->header[1] == 5);
for (remorse::u8 direction = 0; direction < 16; ++direction) {
for (remorse::u8 frame = 1; frame < clip->frameCount; ++frame) {
const auto metadata = remorse::getAnimationFrame(*clip, direction, frame);
assert(metadata && (metadata->bytes[5] & 2) != 0);
}
}
}
auto startClip = *walk;
startClip.header[1] = 1;
assert(remorse::selectAnimationStartFrame(startClip, false, false, true, true, 0) == 8);
assert(remorse::selectAnimationStartFrame(startClip, true, false, true, true, 0) == 0);
startClip.header[1] = 5;
assert(remorse::selectAnimationStartFrame(startClip, true, false, true, true, 0) == 16);
startClip.header[1] = 4;
assert(remorse::selectAnimationStartFrame(startClip, false, false, true, true, 0) == 0);
assert(remorse::selectAnimationStartFrame(startClip, false, true, true, true, 0) == 1);
assert(remorse::selectAnimationStartFrame(startClip, false, true, true, true, 3) == 2);
assert(remorse::selectAnimationStartFrame(startClip, false, true, false, false, 0) == 2);
assert(remorse::selectAnimationStartFrame(startClip, false, true, false, true, 2) == 2);
startClip.header[1] = 0;
assert(remorse::selectAnimationStartFrame(startClip, false, true, true, true, 0) == 0);
assert(remorse::getAnimationFrame(*walk, 0, 0).has_value());
assert(remorse::getAnimationFrame(*run, 15, 11).has_value());
for (remorse::u8 direction : {remorse::u8{6}, remorse::u8{7}, remorse::u8{10}, remorse::u8{11}}) {
for (remorse::u8 frame : {remorse::u8{4}, remorse::u8{12}}) {
const auto metadata = remorse::getAnimationFrame(*walk, direction, frame);
assert(metadata.has_value() && (metadata->bytes[5] & 1) != 0);
const auto authored = remorse::decodeAnimationDisplacement(*metadata, direction, false);
const auto host = remorse::hostMovementStep(*metadata, direction, false);
assert(authored && (authored->x < 0 || authored->y < 0));
assert(host && host->x == 0 && host->y == 0);
}
}
const auto runFrame = remorse::getAnimationFrame(*run, 14, 6);
assert(runFrame.has_value());
const auto hostRun = remorse::hostMovementStep(*runFrame, 14, false);
assert(hostRun && hostRun->x < 0 && hostRun->y < 0);
const auto shapesPath = remorse::resolveGameDataPath(
*root, std::filesystem::path("STATIC") / "SHAPES.FLX");
assert(shapesPath.has_value());
const auto shapeArchive = remorse::loadShapeArchive(*shapesPath);
assert(shapeArchive.has_value());
const auto playerFrames = remorse::loadShapeFrames(*shapeArchive, 1);
assert(playerFrames.has_value() && playerFrames->size() >= walk->frameCount);
std::vector<bool> referencedFrames(playerFrames->size(), false);
for (const auto& clip : playerAnimations) {
if (!clip) {
continue;
}
for (remorse::u8 direction = 0; direction < 16; ++direction) {
for (remorse::u8 frame = 0; frame < clip->frameCount; ++frame) {
const auto metadata = remorse::getAnimationFrame(*clip, direction, frame);
assert(metadata.has_value());
const remorse::u16 shapeFrame = remorse::getAnimationShapeFrame(*metadata);
assert(shapeFrame < referencedFrames.size());
referencedFrames[shapeFrame] = true;
}
}
}
for (std::size_t frame = 0; frame < referencedFrames.size(); ++frame) {
if (referencedFrames[frame]) {
assert(remorse::decodeShapeFrame((*playerFrames)[frame]).has_value());
}
}
assert(std::any_of(referencedFrames.begin(), referencedFrames.end(), [](bool referenced) {
return referenced;
}));
return 0;
}