Skip to content

Commit

Permalink
Merge branch 'refactor-tiled3d' of github.com:heyx3/WFCpp into refact…
Browse files Browse the repository at this point in the history
…or-tiled3d
  • Loading branch information
heyx3 committed Dec 10, 2024
2 parents f6bc489 + b12c9f2 commit aa81b07
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
8 changes: 2 additions & 6 deletions WFC++/MemoryChecks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
#include <array>
#include <inttypes.h>
#include <exception>
#include <cstdio>


//To debug heap memory corruption, `#define WFCPP_CHECK_MEMORY 1` before including this library.
//You may also define a printf-like error handler, `WFCPP_CHECK_MEMORY_ERROR(s, ...)`.
//It defaults to `fprintf(stderr, ...)`.
//Then you can call `DEBUGMEM_Validate()` on an instance to check the padding bits for corruption.

//DEBUG: Turn memory check off explicitly. Associated static_assert is further down.
#define WFCPP_CHECK_MEMORY 0

//For Intellisense, act as if memory debugging is always on.
#if !defined(WFCPP_CHECK_MEMORY) && (defined(__INTELLISENSE__) || defined(__RESHARPER__))
#define WFCPP_CHECK_MEMORY 1
#endif

//By default, disable memory debugging.
//By default, only enable memory debugging in debug builds.
#if !defined(WFCPP_CHECK_MEMORY)
#if defined(NDEBUG)
#define WFCPP_CHECK_MEMORY 0
Expand All @@ -34,8 +32,6 @@
fprintf(stderr, "WFCPP: heap corruption detected! " s, ##__VA_ARGS__)
#endif

static_assert(!WFCPP_CHECK_MEMORY);

namespace WFC::DEBUGMEM
{
//Helper struct representing a set of bytes that should stay at a hard-coded value
Expand Down
6 changes: 3 additions & 3 deletions WFC++/Tiled3D/Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Grid::Grid(const List<Tile>& inputTiles, const Vector3i& outputSize)
Cells(outputSize),
PossiblePermutations({ (int)inputTiles.GetSize(), outputSize })
{
assert(inputTiles.GetSize() < (TileIdx)(-1)); //The last index is reserved for [null]
assert(inputTiles.GetSize() < TileIdx_INVALID); //The last index is reserved for [null]

//Set up FaceIndices.
int32_t nextID = 0;
for (const auto& tile : InputTiles)
Expand Down Expand Up @@ -292,7 +292,7 @@ void Grid::ResetCellPossibilities(const Vector3i& cellPos, CellState& cell, Repo
if (cell.NPossibilities == NPermutedTiles)
return;

cell.ChosenTile = -1;
cell.ChosenTile = TileIdx_INVALID;
cell.NPossibilities = NPermutedTiles;

if (report)
Expand Down
7 changes: 4 additions & 3 deletions WFC++/Tiled3D/Grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <array>
#include <tuple>
#include <algorithm>
#include <limits>

#include "Tile.hpp"

Expand All @@ -13,7 +14,7 @@ namespace WFC
namespace Tiled3D
{
using TileIdx = uint16_t;
const TileIdx TileIdx_INVALID = 0xffff;
constexpr TileIdx TileIdx_INVALID = std::numeric_limits<TileIdx>::max();

//A 3D space which tiles can be placed in.
class WFC_API Grid
Expand All @@ -27,7 +28,7 @@ namespace WFC
{
WFCPP_MEMORY_CHECK_HEADER(16, "CellState struct");

TileIdx ChosenTile = -1;
TileIdx ChosenTile = TileIdx_INVALID;
Transform3D ChosenPermutation = Transform3D{};

//Some tiles are given by the user, and this algorithm has to work around them.
Expand All @@ -54,7 +55,7 @@ namespace WFC
#endif
}

bool IsSet() const { return ChosenTile != (TileIdx)(-1); }
bool IsSet() const { return ChosenTile != TileIdx_INVALID; }

WFCPP_MEMORY_CHECK_FOOTER(16);
};
Expand Down
2 changes: 1 addition & 1 deletion WFC++/Tiled3D/StandardRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Vector3i StandardRunner::PickNextCellToSet()
}
);
cellPriorities.GetUnderlying().erase(newEndIterator, cellPriorities.end());
assert(cellPriorities.GetSize() > 1);
assert(cellPriorities.GetSize() > 0);

//Randomly choose one max-priority cell.
return std::get<0>(cellPriorities[
Expand Down
5 changes: 3 additions & 2 deletions WFC++/Tiled3D/Tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ namespace WFC
namespace Tiled3D
{
//Unique identifiers for each tile permutation.
//TODO: This is way too close to 'TileIdx', rename one of the two and look for bugs from conflation.
using TileID = uint_fast32_t;
const TileID TileID_INVALID = -1,
TileID_FIRST_VALID = 0;
constexpr TileID TileID_INVALID = std::numeric_limits<TileID>::max(),
TileID_FIRST_VALID = 0;

//A single item in a tileset.
struct WFC_API Tile
Expand Down
12 changes: 9 additions & 3 deletions WFCtests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,13 @@ SUITE(WFC_Tiled3D)
usedTransforms.Add(Transform3D{ false, Rotations3D::AxisZ_90 });
usedTransforms.Add(Transform3D{ false, Rotations3D::AxisY_90 }); //The incompatible one.
//Use a really big world grid to do some stress- and performance-testing.
StandardRunner state(OneTileArmy(usedTransforms), { 50, 50, 50 });
StandardRunner state(OneTileArmy(usedTransforms),
#ifdef _DEBUG
{ 10, 10, 10 }
#else
{ 50, 75, 100 }
#endif
);

//Set one tile on each Z-slice.
Dictionary<Vector3i, std::tuple<TileIdx, Transform3D>> constants;
Expand All @@ -889,14 +895,14 @@ SUITE(WFC_Tiled3D)
//Run until the whole grid is solved.
//The number of iterations needed is simple to calculate,
// as the grid can only be solved one way.
std::cout << "Running slow test...\n";
std::cout << " (running slow test..";
int nLeft = state.Grid.Cells.GetNumbElements() - 6;
for (int i = 0; i < nLeft - 1; ++i)
{
auto didEnd = state.Tick();
CHECK(!didEnd);
}
std::cout << "Slow test finished!\n";
std::cout << ". finished!)\n";
//Finish the last tile.
auto didEnd = state.Tick();
CHECK(!didEnd);
Expand Down

0 comments on commit aa81b07

Please sign in to comment.