Crusader_Decomp/rebuild/remorse/tests/collision_tests.cpp

265 lines
No EOL
14 KiB
C++

#include "remorse/collision.h"
#include <cassert>
int main()
{
const auto centeredArea = remorse::calculateFastAreaRange({512, 512, 0});
assert(centeredArea.minSum == -3 && centeredArea.maxSum == 7 &&
centeredArea.minDifference == -3 && centeredArea.maxDifference == 3);
assert(remorse::isWorldPointInFastArea(512, 512, centeredArea));
assert(!remorse::isWorldPointInFastArea(4096, 512, centeredArea));
const auto beforeBlockCrossing = remorse::calculateFastAreaRange({511, 512, 0});
assert(beforeBlockCrossing.minSum == centeredArea.minSum - 1 &&
beforeBlockCrossing.minDifference == centeredArea.minDifference - 1);
const remorse::FastAreaRange fastArea{2, 4, 0, 2};
assert(remorse::isWorldPointInFastArea(512, 512, fastArea));
assert(!remorse::isWorldPointInFastArea(-1, 512, fastArea));
assert(!remorse::isWorldPointInFastArea(0x8000, 512, fastArea));
assert(remorse::isSweepFootprintInFastArea({1000, 1000, 0}, {2000, 1000, 0},
32, 32, fastArea));
assert(!remorse::isSweepFootprintInFastArea({1000, 1000, 0}, {2000, 1000, 0},
32, 32, {2, 3, 0, 2}));
assert(!remorse::isSweepFootprintInFastArea({1000, 1000, 0}, {2000, 1000, 0},
32, 32, {2, 4, 1, 2}));
const auto sweepArea = remorse::areaSweepCandidateBounds({511, 512, 0}, {1024, 1536, 0}, 32, 64);
assert(sweepArea.minX == 479 && sweepArea.minY == 448 &&
sweepArea.maxX == 1280 && sweepArea.maxY == 1792);
const auto reverseArea = remorse::areaSweepCandidateBounds({1024, 1536, 0}, {511, 512, 0}, 32, 64);
assert(reverseArea.minX == sweepArea.minX && reverseArea.maxY == sweepArea.maxY);
const auto edgeSweepArea = remorse::areaSweepCandidateBounds({10, 10, 0}, {0x7fff, 0x7fff, 0}, 32, 64);
assert(edgeSweepArea.minX == 0 && edgeSweepArea.minY == 0 &&
edgeSweepArea.maxX == 0x7fff && edgeSweepArea.maxY == 0x7fff);
const auto outsideBounds = remorse::clampAreaSweepBounds({10, 20, 10}, {-3, 0x8000, 252});
assert(outsideBounds.destination.x == 0 && outsideBounds.destination.y == 0x7fff &&
outsideBounds.destination.z == 250 && outsideBounds.failedDirections == 7 &&
outsideBounds.failed);
const auto belowFloor = remorse::clampAreaSweepBounds({10, 20, 10}, {10, 20, -3});
assert(belowFloor.destination.z == 0 && belowFloor.failedDirections == 4 && !belowFloor.failed);
const auto withinBounds = remorse::clampAreaSweepBounds({10, 20, 10}, {12, 23, 11});
assert(withinBounds.failedDirections == 0 && !withinBounds.failed);
const auto gravityStep = remorse::planGravitySweep({100, 200, 250}, 3, -2, 4, 2, 3);
assert(gravityStep.destination.x == 109 && gravityStep.destination.y == 194);
assert(gravityStep.destination.z == 6 && gravityStep.nextZSpeed == -2);
const auto slowStep = remorse::planGravitySweep({100, 200, 0}, 3, -2, -4, 2, 0);
assert(slowStep.destination.x == 103 && slowStep.destination.y == 198);
assert(slowStep.destination.z == 252 && slowStep.nextZSpeed == -6);
const auto cappedStep = remorse::planGravitySweep({100, 200, 0}, 3, -2, 4, 2, 5);
assert(cappedStep.destination.x == 112 && cappedStep.destination.y == 192);
assert(cappedStep.destination.z == 16 && cappedStep.nextZSpeed == -4);
const remorse::CollisionBody actor{{100, 100, 0}, 96, 96, 80};
const remorse::CollisionBody wall{{300, 100, 0}, 64, 96, 128};
assert(!remorse::collisionBodiesOverlap(actor, wall));
assert(remorse::canMoveCollisionBody(actor, {200, 100, 0}, {wall}));
assert(!remorse::canMoveCollisionBody(actor, {260, 100, 0}, {wall}));
assert(!remorse::canMoveCollisionBody(actor, {400, 100, 0}, {wall}));
const remorse::CollisionBody floorBelow{{300, 100, -20}, 64, 96, 20};
const remorse::CollisionBody overhead{{300, 100, 80}, 64, 96, 32};
assert(!remorse::collisionBodiesOverlap(actor, floorBelow));
assert(!remorse::collisionBodiesOverlap(actor, overhead));
const remorse::CollisionBody initiallyOverlapping{{250, 100, 0}, 96, 96, 80};
assert(remorse::canMoveCollisionBody(initiallyOverlapping, {200, 100, 0}, {wall}));
assert(!remorse::canMoveCollisionBody(initiallyOverlapping, {400, 100, 0}, {wall}));
remorse::ShapeTypeInfo actorInfo;
actorInfo.x = 3;
actorInfo.y = 3;
actorInfo.z = 10;
actorInfo.flags = 0x2;
const remorse::CollisionBody authoredActor = remorse::makeCollisionBody({100, 100, 0}, actorInfo);
assert(authoredActor.extentX == 96);
assert(authoredActor.extentY == 96);
assert(authoredActor.extentZ == 80);
remorse::ShapeTypeInfo obstacleInfo;
obstacleInfo.x = 2;
obstacleInfo.y = 4;
obstacleInfo.z = 8;
const remorse::CollisionBody flipped = remorse::makeCollisionBody({300, 300, 0}, obstacleInfo, true);
assert(flipped.extentX == 128);
assert(flipped.extentY == 64);
const auto sweepItem = remorse::makeSweepItemBody({300, 300, 0}, obstacleInfo, true);
assert(sweepItem.extentX == 64 && sweepItem.extentY == 32 && sweepItem.extentZ == 64);
obstacleInfo.z = 0;
const auto zeroHeightItem = remorse::makeSweepItemBody({300, 300, 0}, obstacleInfo);
assert(zeroHeightItem.extentZ == 1);
obstacleInfo.flags = 1;
assert(remorse::makeSweepItemBody({300, 300, 0}, obstacleInfo).extentZ == 0);
obstacleInfo.z = 8;
obstacleInfo.flags = 0;
const remorse::CollisionBody sweepActor{{100, 100, 0}, 32, 32, 16};
const remorse::CollisionBody sweepWall{{200, 100, 0}, 32, 32, 16};
const auto rightHit = remorse::sweepSolidItemImpact(sweepActor, {250, 100, 0}, sweepWall);
assert(rightHit && rightHit->failedDirections == 1 && rightHit->resolved.x == 168);
const auto leftHit = remorse::sweepSolidItemImpact({{300, 100, 0}, 32, 32, 16},
{150, 100, 0}, sweepWall);
assert(leftHit && leftHit->failedDirections == 1 && leftHit->resolved.x == 232);
const auto upHit = remorse::sweepSolidItemImpact(sweepActor, {100, 250, 0},
{{100, 200, 0}, 32, 32, 16});
assert(upHit && upHit->failedDirections == 2 && upHit->resolved.y == 168);
const auto downHit = remorse::sweepSolidItemImpact({{100, 300, 0}, 32, 32, 16},
{100, 150, 0}, {{100, 200, 0}, 32, 32, 16});
assert(downHit && downHit->failedDirections == 2 && downHit->resolved.y == 232);
const auto ascendingHit = remorse::sweepSolidItemImpact(sweepActor, {100, 100, 30},
{{100, 100, 20}, 32, 32, 16});
assert(ascendingHit && ascendingHit->failedDirections == 4 && ascendingHit->resolved.z == 4);
const auto descendingHit = remorse::sweepSolidItemImpact({{100, 100, 40}, 32, 32, 16},
{100, 100, 0}, {{100, 100, 20}, 32, 32, 16});
assert(descendingHit && descendingHit->failedDirections == 4 && descendingHit->resolved.z == 36);
assert(!remorse::sweepSolidItemImpact(sweepActor, {250, 100, 0},
{{200, 200, 0}, 32, 32, 16}));
remorse::StagedFixedMapItem movingItem;
movingItem.itemNumber = 0x100;
movingItem.data.x = 512;
movingItem.data.y = 512;
movingItem.data.shape = 1;
auto wallItem = movingItem;
wallItem.itemNumber = 0x101;
wallItem.data.x = 640;
wallItem.data.shape = 2;
auto harmlessItem = wallItem;
harmlessItem.itemNumber = 0x102;
harmlessItem.data.x = 600;
harmlessItem.data.shape = 3;
remorse::LiveFixedMapItems liveSweepItems({movingItem, wallItem, harmlessItem});
std::vector<remorse::ShapeTypeInfo> sweepShapes(4);
sweepShapes[1].x = sweepShapes[1].y = sweepShapes[1].z = 2;
sweepShapes[2] = sweepShapes[1];
sweepShapes[2].flags = 2;
sweepShapes[3] = sweepShapes[1];
const remorse::FastAreaRange allBlocks{-128, 128, -128, 128};
const auto liveImpacts = remorse::collectSweepItemImpacts(liveSweepItems, 0x100,
{700, 512, 0}, sweepShapes, allBlocks);
assert(liveImpacts && liveImpacts->size() == 1);
assert((*liveImpacts)[0].itemNumber == 0x101 &&
(*liveImpacts)[0].impact.failedDirections == 1 &&
(*liveImpacts)[0].impact.resolved.x == 608);
auto nextWall = wallItem;
nextWall.itemNumber = 0x103;
nextWall.data.x = 670;
sweepShapes[2].x = 1;
sweepShapes[2].y = 4;
wallItem.data.flags = 0x20;
remorse::LiveFixedMapItems flippedSweepItems({movingItem, wallItem, nextWall, harmlessItem});
const auto flippedImpacts = remorse::collectSweepItemImpacts(flippedSweepItems, 0x100,
{700, 512, 0}, sweepShapes, allBlocks);
assert(flippedImpacts && flippedImpacts->size() == 2);
assert((*flippedImpacts)[0].itemNumber == 0x103 && (*flippedImpacts)[0].impact.resolved.x == 654);
assert((*flippedImpacts)[1].itemNumber == 0x101 && (*flippedImpacts)[1].impact.resolved.x == 576);
const auto nearestImpact = remorse::selectSolidSweepImpact({512, 512, 0},
{700, 512, 0}, *flippedImpacts);
assert(nearestImpact.firstCollision == 0x101 && nearestImpact.destination.x == 576 &&
nearestImpact.failedDirections == 1);
const auto resolvedLive = remorse::resolveSolidLiveSweep(flippedSweepItems, 0x100,
{700, 512, 0}, sweepShapes, allBlocks);
assert(resolvedLive && resolvedLive->firstCollision == 0x101 &&
resolvedLive->destination.x == 576 && resolvedLive->failedDirections == 1);
const auto outsideFastArea = remorse::resolveSolidLiveSweep(flippedSweepItems, 0x100,
{700, 512, 0}, sweepShapes, {2, 2, 0, 0});
assert(outsideFastArea && outsideFastArea->failed &&
outsideFastArea->destination.x == 512 && outsideFastArea->failedDirections == 3);
const auto outsideWorld = remorse::resolveSolidLiveSweep(flippedSweepItems, 0x100,
{-1, 512, 0}, sweepShapes, allBlocks);
assert(outsideWorld && outsideWorld->failed && outsideWorld->failedDirections == 3 &&
outsideWorld->destination.x == 512);
const auto aboveCeiling = remorse::resolveSolidLiveSweep(flippedSweepItems, 0x100,
{512, 512, 251}, sweepShapes, allBlocks);
assert(aboveCeiling && !aboveCeiling->failed && aboveCeiling->failedDirections == 4 &&
aboveCeiling->destination.z == 250);
const auto noImpact = remorse::selectSolidSweepImpact({512, 512, 0}, {700, 512, 0}, {});
assert(noImpact.firstCollision == 0 && noImpact.destination.x == 700 &&
noImpact.failedDirections == 0);
const std::vector<remorse::SweepCandidateImpact> tiedImpacts{
{0x101, {{576, 512, 0}, 1}}, {0x103, {{576, 512, 0}, 2}}};
const auto tiedImpact = remorse::selectSolidSweepImpact({512, 512, 0},
{700, 512, 0}, tiedImpacts);
assert(tiedImpact.firstCollision == 0x101 && tiedImpact.failedDirections == 3);
const std::vector<remorse::SweepCandidateImpact> mixedAxisImpacts{
{0x101, {{540, 590, 0}, 1}}, {0x103, {{550, 550, 0}, 2}}};
const auto mixedAxis = remorse::selectSolidSweepImpact({512, 512, 0},
{700, 700, 0}, mixedAxisImpacts);
assert(mixedAxis.firstCollision == 0x103 && mixedAxis.destination.x == 550 &&
mixedAxis.destination.y == 550 && mixedAxis.failedDirections == 3);
sweepShapes[3].flags = 0x40;
remorse::LiveFixedMapItems damagingItems({movingItem, harmlessItem});
const auto damageOnly = remorse::resolveSolidLiveSweep(damagingItems, 0x100,
{700, 512, 0}, sweepShapes, allBlocks);
assert(damageOnly && damageOnly->firstCollision == 0 &&
damageOnly->destination.x == 700 && damageOnly->failedDirections == 0 &&
(damageOnly->damagingContacts == std::vector<remorse::u16>{0x102}));
movingItem.data.z = 32;
wallItem.data.x = 512;
wallItem.data.z = 16;
remorse::LiveFixedMapItems floorItems({movingItem, wallItem});
const auto floorDisabled = remorse::resolveSolidLiveSweep(floorItems, 0x100,
{512, 512, 32}, sweepShapes, allBlocks);
assert(floorDisabled && floorDisabled->supportItem == 0);
const auto floorTouch = remorse::resolveSolidLiveSweep(floorItems, 0x100,
{512, 512, 32}, sweepShapes, allBlocks, remorse::u8{0});
assert(floorTouch && floorTouch->supportItem == 0x101 && floorTouch->supportGap == 0 &&
floorTouch->destination.z == 32 && floorTouch->firstCollision == 0);
const auto floorStep = remorse::resolveSolidLiveSweep(floorItems, 0x100,
{512, 512, 41}, sweepShapes, allBlocks, remorse::u8{9});
assert(floorStep && floorStep->supportGap == 9 && floorStep->destination.z == 41);
const auto floorTooFar = remorse::resolveSolidLiveSweep(floorItems, 0x100,
{512, 512, 42}, sweepShapes, allBlocks, remorse::u8{9});
assert(floorTooFar && !floorTooFar->supportGap);
auto lowerFloor = wallItem;
lowerFloor.itemNumber = 0x104;
lowerFloor.data.z = 17;
remorse::LiveFixedMapItems layeredFloorItems({movingItem, wallItem, lowerFloor});
const auto closestFloor = remorse::resolveSolidLiveSweep(layeredFloorItems, 0x100,
{512, 512, 41}, sweepShapes, allBlocks, remorse::u8{9});
assert(closestFloor && closestFloor->supportItem == 0x104 && closestFloor->supportGap == 8);
lowerFloor.data.x = 650;
remorse::LiveFixedMapItems distantFloorItems({movingItem, lowerFloor});
const auto outsideFootprint = remorse::resolveSolidLiveSweep(distantFloorItems, 0x100,
{512, 512, 37}, sweepShapes, allBlocks, remorse::u8{9});
assert(outsideFootprint && !outsideFootprint->supportGap);
sweepShapes[2].flags = 0x40;
const auto damagingFloor = remorse::resolveSolidLiveSweep(floorItems, 0x100,
{512, 512, 32}, sweepShapes, allBlocks, remorse::u8{0});
assert(damagingFloor && !damagingFloor->supportGap);
assert(!remorse::collectSweepItemImpacts(liveSweepItems, 0x100,
{700, 512, 0}, sweepShapes, {2, 2, 0, 0}));
assert(!remorse::collectSweepItemImpacts(liveSweepItems, 0x105,
{700, 512, 0}, sweepShapes, allBlocks));
remorse::FixedMap map;
remorse::FixedMapEntry solid;
solid.x = 500;
solid.y = 600;
solid.shape = 1;
map.entries.push_back(solid);
remorse::FixedMapEntry globParent;
globParent.x = 0x480;
globParent.y = 0x640;
globParent.shape = 2;
globParent.quality = 7;
map.entries.push_back(globParent);
std::vector<remorse::ShapeTypeInfo> shapeTypes(4);
shapeTypes[1] = obstacleInfo;
shapeTypes[1].flags = 0x2;
shapeTypes[2].family = 3;
shapeTypes[2].flags = 0x2;
shapeTypes[3] = obstacleInfo;
shapeTypes[3].flags = 0x2;
remorse::GlobObject glob;
glob.index = 7;
glob.entries.push_back(remorse::GlobObjectEntry{2, 3, 4, 3, 0});
const auto bodies = remorse::collectMapCollisionBodies(map, shapeTypes, {glob});
assert(bodies.size() == 2);
assert(bodies[0].location.x == 500 && bodies[0].location.y == 600);
assert(bodies[1].location.x == (0x480 & ~0x3ff) + 2 * 4 + 2);
assert(bodies[1].location.y == (0x640 & ~0x3ff) + 3 * 4 + 2);
assert(bodies[1].location.z == 4);
return 0;
}