Working line iterator, and mostly working flood iterator that should be good enough for world gen.

This commit is contained in:
Zed A. Shaw 2024-12-18 19:22:22 -05:00
parent 1295e9631d
commit d4b6c35120
5 changed files with 91 additions and 39 deletions

View file

@ -163,7 +163,7 @@ TEST_CASE("thrash compass iterators", "[matrix:compass]") {
}
TEST_CASE("prototype flood algorithm", "[matrix:flood]") {
for(int count = 0; count < 1; count++) {
for(int count = 0; count < 1000; count++) {
size_t width = Random::uniform<size_t>(10, 25);
size_t height = Random::uniform<size_t>(10, 33);
@ -171,32 +171,56 @@ TEST_CASE("prototype flood algorithm", "[matrix:flood]") {
WorldBuilder builder(map);
builder.generate();
REQUIRE(map.room_count() > 0);
if(map.room_count() < 2) continue;
Point start = map.place_entity(map.room_count() / 2);
map.set_target(start);
map.make_paths();
Matrix result = map.paths();
// BUG: place_entity should not put things in walls
map.$walls[start.y][start.x] = 0;
// matrix::dump("WALLS BEFORE FLOOD", result, start.x, start.y);
matrix::dump("WALLS BEFORE FLOOD", map.walls(), start.x, start.y);
/*
for(matrix::flood it{map.$walls, start, 0, 10}; it.next_working(); tick++) {
println("TEST WORKING");
}
*/
for(matrix::flood it{map.$walls, start, 0, 15}; it.next();) {
REQUIRE(matrix::inbounds(map.$walls, it.x, it.y));
map.$walls[it.y][it.x] = 15;
for(matrix::flood it{result, start, 3, 15}; it.next();) {
REQUIRE(matrix::inbounds(result, it.x, it.y));
result[it.y][it.x] = 15;
}
matrix::dump("WALLS AFTER FLOOD", map.walls(), start.x, start.y);
// confirm that everything is 1 or 2 which confirms
// every cell possible is visited and nothing is visited twice
for(matrix::each_cell it{map.$walls}; it.next();) {
REQUIRE(map.$walls[it.y][it.x] <= 15);
}
// matrix::dump("WALLS AFTER FLOOD", result, start.x, start.y);
}
}
TEST_CASE("prototype line algorithm", "[matrix:line]") {
size_t width = Random::uniform<size_t>(10, 12);
size_t height = Random::uniform<size_t>(10, 15);
Map map(width,height);
// create a target for the paths
Point start{.x=map.width() / 2, .y=map.height()/2};
for(matrix::in_box box{map.walls(), start.x, start.y, 3};
box.next();)
{
Matrix result = map.walls();
result[start.y][start.x] = 1;
Point end{.x=box.x, .y=box.y};
for(matrix::line it{start, end}; it.next();)
{
REQUIRE(map.inmap(it.x, it.y));
result[it.y][it.x] = 15;
}
result[start.y][start.x] = 15;
// matrix::dump("RESULT AFTER LINE", result, end.x, end.y);
bool f_found = false;
for(matrix::each_cell it{result}; it.next();) {
if(result[it.y][it.x] == 15) {
f_found = true;
break;
}
}
REQUIRE(f_found);
}
}