317 lines
11 KiB
C++
317 lines
11 KiB
C++
#include "testHarness.h"
|
|
#include "vf/Brush.h"
|
|
#include "vf/Codec.h"
|
|
#include "vf/EditHistory.h"
|
|
#include "vf/Selection.h"
|
|
|
|
#include <algorithm>
|
|
#include <vector>
|
|
|
|
using namespace vf;
|
|
|
|
namespace {
|
|
|
|
uint64_t worldFingerprint(const World& world) {
|
|
std::vector<Vec3i> coords;
|
|
for (const auto& entry : world.allChunks())
|
|
if (!entry.second->empty()) coords.push_back(entry.first);
|
|
std::sort(coords.begin(), coords.end(), [](const Vec3i& a, const Vec3i& b) {
|
|
if (a.x != b.x) return a.x < b.x;
|
|
if (a.y != b.y) return a.y < b.y;
|
|
return a.z < b.z;
|
|
});
|
|
|
|
uint64_t digest = 1469598103934665603ull;
|
|
for (const Vec3i& coord : coords) {
|
|
int32_t header[3] = {coord.x, coord.y, coord.z};
|
|
digest ^= fnv1a64(reinterpret_cast<const uint8_t*>(header), sizeof(header));
|
|
digest *= 1099511628211ull;
|
|
const auto& blocks = world.chunkAt(coord)->raw();
|
|
digest ^= fnv1a64(reinterpret_cast<const uint8_t*>(blocks.data()), blocks.size());
|
|
digest *= 1099511628211ull;
|
|
}
|
|
return digest;
|
|
}
|
|
|
|
// Bumpy stone terrain with a grass skin and one free-standing pillar, so every
|
|
// brush has something real to bite into.
|
|
World bumpyWorld() {
|
|
World world;
|
|
for (int z = 0; z < 16; ++z)
|
|
for (int x = 0; x < 16; ++x) {
|
|
int height = 4 + ((x + z) % 3);
|
|
for (int y = 0; y <= height; ++y)
|
|
world.setBlock(x, y, z, y == height ? Block::Grass : Block::Stone);
|
|
}
|
|
for (int y = 5; y <= 9; ++y) world.setBlock(8, y, 8, Block::Stone);
|
|
return world;
|
|
}
|
|
|
|
World flatWorld(int span, int surfaceY) {
|
|
World world;
|
|
for (int z = 0; z < span; ++z)
|
|
for (int x = 0; x < span; ++x)
|
|
for (int y = 0; y <= surfaceY; ++y) world.setBlock(x, y, z, Block::Stone);
|
|
return world;
|
|
}
|
|
|
|
World stoneCube(const Vec3i& lo, int span) {
|
|
World world;
|
|
for (int y = 0; y < span; ++y)
|
|
for (int z = 0; z < span; ++z)
|
|
for (int x = 0; x < span; ++x)
|
|
world.setBlock(lo.x + x, lo.y + y, lo.z + z, Block::Stone);
|
|
return world;
|
|
}
|
|
|
|
int surfaceHeight(const World& world, int x, int z, int from) {
|
|
for (int y = from; y >= 0; --y)
|
|
if (!isAir(world.blockAt(x, y, z))) return y;
|
|
return -1;
|
|
}
|
|
|
|
BrushParams baseParams(BrushKind kind) {
|
|
BrushParams params;
|
|
params.kind = kind;
|
|
params.center = {8, 7, 8};
|
|
params.radius = 3;
|
|
params.height = 4;
|
|
params.block = Block::Stone;
|
|
params.secondaryBlock = Block::Dirt;
|
|
params.freeNeighbourThreshold = 2;
|
|
params.seed = 7u;
|
|
return params;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
VF_TEST(everyBrushKindWritesExactlyOneUndoableBatch) {
|
|
const BrushKind kinds[8] = {BrushKind::Sphere, BrushKind::Cylinder, BrushKind::Replace,
|
|
BrushKind::Smooth, BrushKind::Erode, BrushKind::Overlay,
|
|
BrushKind::Naturalize, BrushKind::Gradient};
|
|
|
|
for (BrushKind kind : kinds) {
|
|
World world = bumpyWorld();
|
|
uint64_t before = worldFingerprint(world);
|
|
EditHistory history(1u << 22);
|
|
BrushStroke stroke(world, history);
|
|
|
|
BrushResult outcome = stroke.apply(baseParams(kind), 1u);
|
|
CHECK(outcome.recorded);
|
|
CHECK(outcome.changed > 0u);
|
|
CHECK_EQ(history.stats().batchCount, 1u);
|
|
CHECK(worldFingerprint(world) != before);
|
|
CHECK(history.peekUndo()->label == std::string(BrushStroke::kindName(kind)));
|
|
|
|
CHECK(history.undo(world));
|
|
CHECK_EQ(worldFingerprint(world), before);
|
|
CHECK_EQ(history.undoDepth(), 0u);
|
|
CHECK(stroke.lastResult().changed == outcome.changed);
|
|
}
|
|
}
|
|
|
|
VF_TEST(theSphereBrushFillsHollowsAndShellsTheSameVolume) {
|
|
Selection ball = Selection::sphere({16, 16, 16}, 4);
|
|
Selection core = Selection::sphere({16, 16, 16}, 3);
|
|
|
|
World world;
|
|
EditHistory history(1u << 22);
|
|
BrushStroke stroke(world, history);
|
|
|
|
BrushParams fill = baseParams(BrushKind::Sphere);
|
|
fill.center = {16, 16, 16};
|
|
fill.radius = 4;
|
|
BrushResult filled = stroke.apply(fill, 1u);
|
|
CHECK_EQ(filled.changed, ball.count());
|
|
CHECK(world.blockAt(16, 16, 16) == Block::Stone);
|
|
CHECK(world.blockAt(16, 21, 16) == Block::Air);
|
|
|
|
BrushParams hollow = fill;
|
|
hollow.mode = BrushParams::VolumeMode::Hollow;
|
|
hollow.radius = 3;
|
|
BrushResult emptied = stroke.apply(hollow, 2u);
|
|
CHECK_EQ(emptied.changed, core.count());
|
|
CHECK(world.blockAt(16, 16, 16) == Block::Air);
|
|
CHECK(world.blockAt(20, 16, 16) == Block::Stone);
|
|
|
|
World shellWorld;
|
|
EditHistory shellHistory(1u << 22);
|
|
BrushStroke shellStroke(shellWorld, shellHistory);
|
|
BrushParams shell = fill;
|
|
shell.mode = BrushParams::VolumeMode::Shell;
|
|
shell.shellThickness = 1;
|
|
BrushResult carved = shellStroke.apply(shell, 3u);
|
|
CHECK_EQ(carved.changed, ball.count() - core.count());
|
|
CHECK(shellWorld.blockAt(16, 16, 16) == Block::Air);
|
|
CHECK(shellWorld.blockAt(20, 16, 16) == Block::Stone);
|
|
}
|
|
|
|
VF_TEST(theCylinderBrushFillsATubeOfTheRequestedHeight) {
|
|
World world;
|
|
EditHistory history(1u << 22);
|
|
BrushStroke stroke(world, history);
|
|
|
|
BrushParams params = baseParams(BrushKind::Cylinder);
|
|
params.center = {20, 10, 20};
|
|
params.radius = 3;
|
|
params.height = 5;
|
|
params.block = Block::Planks;
|
|
|
|
BrushResult outcome = stroke.apply(params, 1u);
|
|
CHECK_EQ(outcome.changed, Selection::cylinder({20, 10, 20}, 3, 5).count());
|
|
CHECK(world.blockAt(20, 10, 20) == Block::Planks);
|
|
CHECK(world.blockAt(20, 14, 20) == Block::Planks);
|
|
CHECK(world.blockAt(20, 15, 20) == Block::Air);
|
|
CHECK(world.blockAt(20, 9, 20) == Block::Air);
|
|
CHECK(world.blockAt(24, 12, 20) == Block::Air);
|
|
|
|
BrushParams shell = params;
|
|
shell.mode = BrushParams::VolumeMode::Shell;
|
|
shell.shellThickness = 1;
|
|
World tubeWorld;
|
|
EditHistory tubeHistory(1u << 22);
|
|
BrushStroke tubeStroke(tubeWorld, tubeHistory);
|
|
BrushResult carved = tubeStroke.apply(shell, 2u);
|
|
CHECK(carved.changed < outcome.changed);
|
|
CHECK(tubeWorld.blockAt(20, 10, 20) == Block::Planks);
|
|
CHECK(tubeWorld.blockAt(20, 11, 20) == Block::Air);
|
|
CHECK(tubeWorld.blockAt(20, 14, 20) == Block::Planks);
|
|
CHECK(tubeWorld.blockAt(23, 12, 20) == Block::Planks);
|
|
}
|
|
|
|
VF_TEST(theReplaceBrushOnlyTouchesTheListedBlockTypes) {
|
|
World world = bumpyWorld();
|
|
EditHistory history(1u << 22);
|
|
BrushStroke stroke(world, history);
|
|
|
|
BrushParams params = baseParams(BrushKind::Replace);
|
|
params.center = {8, 4, 8};
|
|
params.radius = 4;
|
|
params.block = Block::Sand;
|
|
params.onlyReplace = {Block::Grass};
|
|
|
|
Selection region = BrushStroke::regionOf(params);
|
|
std::size_t grassBefore = region.stats(world).countOf(Block::Grass);
|
|
CHECK(grassBefore > 0u);
|
|
|
|
BrushResult outcome = stroke.apply(params, 1u);
|
|
CHECK_EQ(outcome.changed, grassBefore);
|
|
CHECK_EQ(region.stats(world).countOf(Block::Grass), 0u);
|
|
CHECK_EQ(region.stats(world).countOf(Block::Sand), grassBefore);
|
|
CHECK(region.stats(world).countOf(Block::Stone) > 0u);
|
|
|
|
BrushParams everything = params;
|
|
everything.onlyReplace.clear();
|
|
everything.block = Block::Glass;
|
|
BrushResult sweep = stroke.apply(everything, 2u);
|
|
SelectionStats after = region.stats(world);
|
|
CHECK_EQ(after.countOf(Block::Stone), 0u);
|
|
CHECK_EQ(after.countOf(Block::Sand), 0u);
|
|
CHECK(sweep.changed > 0u);
|
|
CHECK(after.countOf(Block::Air) > 0u);
|
|
}
|
|
|
|
VF_TEST(theSmoothBrushCutsDownASpikeAndFillsAPit) {
|
|
World world = flatWorld(20, 4);
|
|
for (int y = 5; y <= 10; ++y) world.setBlock(8, y, 8, Block::Stone);
|
|
CHECK_EQ(surfaceHeight(world, 8, 8, 12), 10);
|
|
|
|
EditHistory history(1u << 22);
|
|
BrushStroke stroke(world, history);
|
|
BrushParams params = baseParams(BrushKind::Smooth);
|
|
params.center = {8, 6, 8};
|
|
params.radius = 4;
|
|
|
|
Selection region = Selection::box({4, 0, 4}, {12, 12, 12});
|
|
BrushResult outcome = stroke.applyWithin(params, region, 1u);
|
|
CHECK_EQ(outcome.changed, 6u);
|
|
CHECK_EQ(surfaceHeight(world, 8, 8, 12), 4);
|
|
CHECK_EQ(surfaceHeight(world, 7, 8, 12), 4);
|
|
CHECK(world.blockAt(8, 4, 8) == Block::Stone);
|
|
|
|
World pitWorld = flatWorld(20, 4);
|
|
pitWorld.setBlock(8, 4, 8, Block::Air);
|
|
CHECK_EQ(surfaceHeight(pitWorld, 8, 8, 12), 3);
|
|
EditHistory pitHistory(1u << 22);
|
|
BrushStroke pitStroke(pitWorld, pitHistory);
|
|
BrushResult filled = pitStroke.applyWithin(params, region, 2u);
|
|
CHECK_EQ(filled.changed, 1u);
|
|
CHECK(pitWorld.blockAt(8, 4, 8) == Block::Stone);
|
|
CHECK_EQ(surfaceHeight(pitWorld, 8, 8, 12), 4);
|
|
}
|
|
|
|
VF_TEST(theErodeBrushRemovesOnlyBlocksWithEnoughFreeNeighbours) {
|
|
World world = stoneCube({10, 10, 10}, 3);
|
|
EditHistory history(1u << 22);
|
|
BrushStroke stroke(world, history);
|
|
|
|
BrushParams params = baseParams(BrushKind::Erode);
|
|
params.center = {11, 11, 11};
|
|
params.radius = 2;
|
|
params.freeNeighbourThreshold = 3;
|
|
|
|
BrushResult outcome = stroke.apply(params, 1u);
|
|
// Only the eight corners of a 3x3x3 cube see three empty faces.
|
|
CHECK_EQ(outcome.changed, 8u);
|
|
CHECK(world.blockAt(10, 10, 10) == Block::Air);
|
|
CHECK(world.blockAt(12, 12, 12) == Block::Air);
|
|
CHECK(world.blockAt(11, 10, 10) == Block::Stone);
|
|
CHECK(world.blockAt(11, 11, 11) == Block::Stone);
|
|
|
|
CHECK(history.undo(world));
|
|
CHECK(world.blockAt(10, 10, 10) == Block::Stone);
|
|
|
|
BrushParams gentler = params;
|
|
gentler.freeNeighbourThreshold = 4;
|
|
BrushResult none = stroke.apply(gentler, 2u);
|
|
CHECK_EQ(none.changed, 0u);
|
|
CHECK(!none.recorded);
|
|
}
|
|
|
|
VF_TEST(theOverlayBrushDecidesFromTheStateBeforeTheStroke) {
|
|
World world = flatWorld(5, 4);
|
|
EditHistory history(1u << 22);
|
|
BrushStroke stroke(world, history);
|
|
|
|
BrushParams params = baseParams(BrushKind::Overlay);
|
|
params.block = Block::Snow;
|
|
params.freeNeighbourThreshold = 1;
|
|
Selection layer = Selection::box({0, 5, 0}, {4, 5, 4});
|
|
|
|
BrushResult outcome = stroke.applyWithin(params, layer, 1u);
|
|
CHECK_EQ(outcome.changed, 25u);
|
|
CHECK(world.blockAt(0, 5, 0) == Block::Snow);
|
|
CHECK(world.blockAt(2, 5, 2) == Block::Snow);
|
|
CHECK(world.blockAt(2, 6, 2) == Block::Air);
|
|
|
|
// A wall along x = 0 gives the column next to it two solid neighbours, and
|
|
// nothing else in the layer has more than one. A brush that wrote as it
|
|
// walked would let that first column qualify its neighbour, and the whole
|
|
// row would fall over like dominoes.
|
|
World walled = flatWorld(5, 4);
|
|
for (int z = 0; z < 5; ++z) walled.setBlock(0, 5, z, Block::Stone);
|
|
EditHistory walledHistory(1u << 22);
|
|
BrushStroke walledStroke(walled, walledHistory);
|
|
BrushParams strict = params;
|
|
strict.freeNeighbourThreshold = 2;
|
|
Selection openLayer = Selection::box({1, 5, 0}, {4, 5, 4});
|
|
|
|
BrushResult edged = walledStroke.applyWithin(strict, openLayer, 1u);
|
|
CHECK_EQ(edged.changed, 5u);
|
|
for (int z = 0; z < 5; ++z) {
|
|
CHECK(walled.blockAt(1, 5, z) == Block::Snow);
|
|
CHECK(walled.blockAt(2, 5, z) == Block::Air);
|
|
CHECK(walled.blockAt(3, 5, z) == Block::Air);
|
|
CHECK(walled.blockAt(4, 5, z) == Block::Air);
|
|
}
|
|
|
|
// The same layer with no wall to lean on stays untouched at that threshold.
|
|
World bare = flatWorld(5, 4);
|
|
EditHistory bareHistory(1u << 22);
|
|
BrushStroke bareStroke(bare, bareHistory);
|
|
BrushResult quiet = bareStroke.applyWithin(strict, layer, 1u);
|
|
CHECK_EQ(quiet.changed, 0u);
|
|
CHECK_EQ(bareHistory.stats().batchCount, 0u);
|
|
}
|
|
|