Files
nd-wfc/examples/basic_wfc_2d.cpp
cdemeyer-teachx 2996ffedfc initial commit
2025-08-21 12:17:21 +09:00

89 lines
3.8 KiB
C++

#include <iostream>
#include <vector>
#include "nd-wfc/wfc.hpp"
#include "nd-wfc/types.hpp"
int main() {
std::cout << "Running 2D WFC Example" << std::endl;
// Create a 10x10 grid
nd_wfc::Size<2> size = {10, 10};
// Define 4 tile types (like a simple terrain system)
std::vector<nd_wfc::TileId> tiles = {0, 1, 2, 3}; // Empty, Grass, Water, Mountain
// Define adjacency constraints
std::vector<nd_wfc::Constraint> constraints = {
// Empty can connect to anything
{0, 0, nd_wfc::Direction::North}, {0, 0, nd_wfc::Direction::South},
{0, 0, nd_wfc::Direction::East}, {0, 0, nd_wfc::Direction::West},
{0, 1, nd_wfc::Direction::North}, {0, 1, nd_wfc::Direction::South},
{0, 1, nd_wfc::Direction::East}, {0, 1, nd_wfc::Direction::West},
{0, 2, nd_wfc::Direction::North}, {0, 2, nd_wfc::Direction::South},
{0, 2, nd_wfc::Direction::East}, {0, 2, nd_wfc::Direction::West},
{0, 3, nd_wfc::Direction::North}, {0, 3, nd_wfc::Direction::South},
{0, 3, nd_wfc::Direction::East}, {0, 3, nd_wfc::Direction::West},
// Grass can connect to Empty, Grass, and Mountain
{1, 0, nd_wfc::Direction::North}, {1, 0, nd_wfc::Direction::South},
{1, 0, nd_wfc::Direction::East}, {1, 0, nd_wfc::Direction::West},
{1, 1, nd_wfc::Direction::North}, {1, 1, nd_wfc::Direction::South},
{1, 1, nd_wfc::Direction::East}, {1, 1, nd_wfc::Direction::West},
{1, 3, nd_wfc::Direction::North}, {1, 3, nd_wfc::Direction::South},
{1, 3, nd_wfc::Direction::East}, {1, 3, nd_wfc::Direction::West},
// Water can only connect to Empty and Water
{2, 0, nd_wfc::Direction::North}, {2, 0, nd_wfc::Direction::South},
{2, 0, nd_wfc::Direction::East}, {2, 0, nd_wfc::Direction::West},
{2, 2, nd_wfc::Direction::North}, {2, 2, nd_wfc::Direction::South},
{2, 2, nd_wfc::Direction::East}, {2, 2, nd_wfc::Direction::West},
// Mountain can connect to Empty, Grass, and Mountain
{3, 0, nd_wfc::Direction::North}, {3, 0, nd_wfc::Direction::South},
{3, 0, nd_wfc::Direction::East}, {3, 0, nd_wfc::Direction::West},
{3, 1, nd_wfc::Direction::North}, {3, 1, nd_wfc::Direction::South},
{3, 1, nd_wfc::Direction::East}, {3, 1, nd_wfc::Direction::West},
{3, 3, nd_wfc::Direction::North}, {3, 3, nd_wfc::Direction::South},
{3, 3, nd_wfc::Direction::East}, {3, 3, nd_wfc::Direction::West},
};
// Configure the WFC algorithm
nd_wfc::WfcConfig config;
config.seed = 12345; // For reproducible results
config.max_iterations = 10000;
// Create and run WFC
nd_wfc::Wfc2D wfc(size, tiles, constraints, config);
std::cout << "Running WFC algorithm..." << std::endl;
nd_wfc::WfcResult result = wfc.run();
if (result == nd_wfc::WfcResult::Success) {
std::cout << "WFC completed successfully!" << std::endl;
std::cout << "Iterations: " << wfc.getIterationCount() << std::endl;
// Print the result (simple ASCII representation)
const auto& grid = wfc.getGrid();
for (nd_wfc::Index y = 0; y < size[1]; ++y) {
for (nd_wfc::Index x = 0; x < size[0]; ++x) {
nd_wfc::Coord<2> coord = {x, y};
nd_wfc::TileId tile = grid[coord];
char symbol;
switch (tile) {
case 0: symbol = '.'; break; // Empty
case 1: symbol = 'G'; break; // Grass
case 2: symbol = 'W'; break; // Water
case 3: symbol = 'M'; break; // Mountain
default: symbol = '?'; break;
}
std::cout << symbol << ' ';
}
std::cout << std::endl;
}
} else {
std::cout << "WFC failed with result: " << static_cast<int>(result) << std::endl;
}
return 0;
}