Skip to content

Commit

Permalink
Fix various bugs in Tiled3D and collections
Browse files Browse the repository at this point in the history
  • Loading branch information
heyx3 committed Aug 5, 2022
1 parent 7d4ef32 commit 4d02bfc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions WFC++/Dictionary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace WFC

TValue& operator[](const TKey& key) { return dict[key]; }
const TValue& operator[](const TKey& key) const { return dict.at(key); }
TValue& At(const TKey& key) { return dict.at(key); } //Needed for the debugger, it can't recognize subscript operators

const TValue* TryGet(const TKey& key) const
{
Expand Down
1 change: 1 addition & 0 deletions WFC++/List.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace WFC

T& operator[](size_t i) { return vec[i]; }
const T& operator[](size_t i) const { return vec[i]; }
T& At(size_t i) { return vec[i]; } //Needed for the debugger, it can't recognize subscript operators

T& GetBack() { return vec[vec.size() - 1]; }
const T& GetBack() const { return vec[vec.size() - 1]; }
Expand Down
21 changes: 17 additions & 4 deletions WFC++/Tiled3D/StandardRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ bool StandardRunner::Tick()
else if (cell.NPossibilities < Grid.NPermutedTiles)
nextCells.Add(cellPos);
}

//If every cell was set, then the algorithm is done.
if (nSetCells == Grid.Cells.GetNumbElements())
{
Expand All @@ -183,8 +183,16 @@ bool StandardRunner::Tick()
Vector3i cellPos = PickNextCellToSet();
TileIdx tileIdx;
Transform3D tilePermutation;
std::tie(tileIdx, tilePermutation) = RandomTile(&Grid.PossiblePermutations[{ 0, cellPos }]);
Set(cellPos, tileIdx, tilePermutation);
auto tryRandomTile = RandomTile(&Grid.PossiblePermutations[{ 0, cellPos }]);
if (tryRandomTile.HasValue)
{
std::tie(tileIdx, tilePermutation) = tryRandomTile.Value;
Set(cellPos, tileIdx, tilePermutation);
}
else
{
unsolvableCells.Add(cellPos);
}

return false;
}
Expand All @@ -196,20 +204,25 @@ bool StandardRunner::TickN(int n)
return false;
}

std::tuple<TileIdx, Transform3D> StandardRunner::RandomTile(const TransformSet* allowedPerTile)
WFC::Nullable<std::tuple<TileIdx, Transform3D>> StandardRunner::RandomTile(const TransformSet* allowedPerTile)
{
auto& distributionBuffer = buffer_randomTile_distribution;

//Pick a tile.
distributionBuffer.Clear();
for (int tileI = 0; tileI < Grid.InputTiles.GetSize(); ++tileI)
distributionBuffer.PushBack(allowedPerTile[tileI].Size() * Grid.InputTiles[tileI].Weight);
//If all tiles have weight 0, then none are possible.
if (std::all_of(distributionBuffer.begin(), distributionBuffer.end(),
[](int i) { return i == 0; }))
return { };
std::discrete_distribution<int> tileDistribution(distributionBuffer.begin(),
distributionBuffer.end());
int chosenTileI = tileDistribution(Rand);

//Pick a permutation for the tile.
const auto& permutations = allowedPerTile[chosenTileI];
assert(permutations.Size() > 0);
distributionBuffer.Resize((size_t)N_ROTATIONS_3D * 2);
std::fill(distributionBuffer.begin(), distributionBuffer.end(), 0);
for (Transform3D tr : permutations)
Expand Down
6 changes: 5 additions & 1 deletion WFC++/Tiled3D/StandardRunner.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <tuple>

#include "Grid.h"


namespace WFC
{
namespace Tiled3D
Expand Down Expand Up @@ -134,7 +136,9 @@ namespace Tiled3D

Vector3i PickNextCellToSet();

std::tuple<TileIdx, Transform3D> RandomTile(const TransformSet* allowedPerTile);
//Attempts to pick a random tile, given the allowed permutations of each tile.
//Returns the random selection, or nothing if there were no eligible tiles.
WFC::Nullable<std::tuple<TileIdx, Transform3D>> RandomTile(const TransformSet* allowedPerTile);
};
}
}
2 changes: 2 additions & 0 deletions WFCtests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,8 @@ SUITE(WFC_Tiled3D)
}
}
}

//TODO: Test with more complex tilesets, look for a stack corruption bug
}


Expand Down

0 comments on commit 4d02bfc

Please sign in to comment.