Vast improvement in the world generation, with more reliable pathing, cleaner generation code, and an ability to do a random or direct walk to create paths. This also works with enemies if I want.

main
Zed A. Shaw 1 week ago
parent c7607533ce
commit 0b4392dbcc
  1. 116
      map.cpp
  2. 7
      map.hpp
  3. 2
      status.txt
  4. 2
      systems.cpp
  5. 10
      tests/worldbuilder.cpp
  6. 85
      worldbuilder.cpp
  7. 6
      worldbuilder.hpp

@ -1,5 +1,6 @@
#include "map.hpp" #include "map.hpp"
#include "dbc.hpp" #include "dbc.hpp"
#include "rand.hpp"
#include <vector> #include <vector>
#include <array> #include <array>
#include <fmt/core.h> #include <fmt/core.h>
@ -8,11 +9,21 @@
using std::vector, std::pair; using std::vector, std::pair;
using namespace fmt; using namespace fmt;
void dump_map(const std::string &msg, Matrix &map) { void dump_map(const std::string &msg, Matrix &map, int show_x, int show_y) {
println("----------------- {}", msg); println("----------------- {}", msg);
for(auto row : map) { for(size_t y = 0; y < map.size(); y++) {
for(auto col : row) { for(size_t x = 0; x < map[y].size(); x++) {
print("{} ", col); int col = map[y][x];
if(int(x) == show_x && int(y) == show_y) {
print("{:x}<", col);
} else if(col == 1000) {
print("# ");
} else if(col > 15) {
print("* ");
} else {
print("{:x} ", col);
}
} }
print("\n"); print("\n");
} }
@ -62,8 +73,9 @@ bool Map::iswall(size_t x, size_t y) {
return $walls[y][x] == WALL_VALUE; return $walls[y][x] == WALL_VALUE;
} }
void Map::dump() { void Map::dump(int show_x, int show_y) {
dump_map("WALLS", $walls); dump_map("WALLS", walls(), show_x, show_y);
dump_map("PATHS", paths(), show_x, show_y);
} }
bool Map::can_move(Point move_to) { bool Map::can_move(Point move_to) {
@ -81,51 +93,84 @@ Point Map::center_camera(const Point &around, size_t view_x, size_t view_y) {
int center_x = int(around.x - view_x / 2); int center_x = int(around.x - view_x / 2);
int center_y = int(around.y - view_y / 2); int center_y = int(around.y - view_y / 2);
// BUG: is clamp really the best thing here? this seems wrong.
size_t start_x = high_x > 0 ? std::clamp(center_x, 0, high_x) : 0; size_t start_x = high_x > 0 ? std::clamp(center_x, 0, high_x) : 0;
size_t start_y = high_y > 0 ? std::clamp(center_y, 0, high_y) : 0; size_t start_y = high_y > 0 ? std::clamp(center_y, 0, high_y) : 0;
return {start_x, start_y}; return {start_x, start_y};
} }
bool Map::neighbors(Point &out, bool greater) { /*
* Finds the next optimal neighbor in the path
* using either a direct or random method.
*
* Both modes will pick a random direction to start
* looking for the next path, then it goes clock-wise
* from there.
*
* In the direct method it will attempt to find
* a path that goes 1 lower in the dijkstra map
* path, and if it can't find that it will go to
* a 0 path (same number).
*
* In random mode it will pick either the next lower
* or the same level depending on what it finds first.
* Since the starting direction is random this will
* give it a semi-random walk that eventually gets to
* the target.
*
* In map generation this makes random paths and carves
* up the space to make rooms more irregular.
*
* When applied to an enemy they will either go straight
* to the player (random=false) or they'll wander around
* drunkenly gradually reaching the player, and dodging
* in and out.
*/
bool Map::neighbors(Point &out, bool random) {
Matrix &paths = $paths.$paths; Matrix &paths = $paths.$paths;
bool zero_found = false;
// just make a list of the four directions
std::array<Point, 4> dirs{{ std::array<Point, 4> dirs{{
{out.x,out.y-1}, {out.x,out.y-1}, // north
{out.x+1,out.y}, {out.x+1,out.y}, // east
{out.x,out.y+1}, {out.x,out.y+1}, // south
{out.x-1,out.y} {out.x-1,out.y} // west
}}; }};
int zero_i = -1; // get the current dijkstra number
int cur = paths[out.y][out.x]; int cur = paths[out.y][out.x];
// BUG: sometimes cur is in a wall so finding neighbors fails // pick a random start of directions
for(size_t i = 0; i < dirs.size(); ++i) { int rand_start = Random::uniform<int>(0, dirs.size());
Point dir = dirs[i];
int target = inmap(dir.x, dir.y) ? paths[dir.y][dir.x] : $limit;
if(target == $limit) continue; // skip unpathable stuff
int diff = cur - target; // go through all possible directions
for(size_t i = 0; i < dirs.size(); i++) {
// but start at the random start, effectively randomizing
// which valid direction to go
Point dir = dirs[(i + rand_start) % dirs.size()];
if(!inmap(dir.x, dir.y)) continue; //skip unpathable stuff
int weight = cur - paths[dir.y][dir.x];
if(diff == 1) { if(weight == 1) {
out = {.x=dir.x, .y=dir.y}; // no matter what we follow direct paths
out = dir;
return true; return true;
} else if(diff == 0) { } else if(random && weight == 0) {
zero_i = i; // if random is selected and it's a 0 path take it
} out = dir;
}
if(zero_i != -1) {
out = {.x=dirs[zero_i].x, .y=dirs[zero_i].y};
return true; return true;
} else { } else if(weight == 0) {
return false; // otherwise keep the last zero path for after
out = dir;
zero_found = true;
} }
} }
// if we reach this then either zero was found and
// zero_found is set true, or it wasn't and nothing found
return zero_found;
}
bool Map::INVARIANT() { bool Map::INVARIANT() {
using dbc::check; using dbc::check;
@ -133,5 +178,14 @@ bool Map::INVARIANT() {
check($walls.size() == height(), "walls wrong height"); check($walls.size() == height(), "walls wrong height");
check($walls[0].size() == width(), "walls wrong width"); check($walls[0].size() == width(), "walls wrong width");
for(auto room : $rooms) {
check(int(room.x) >= 0 && int(room.y) >= 0,
format("room depth={} has invalid position {},{}",
room.depth, room.x, room.y));
check(int(room.width) > 0 && int(room.height) > 0,
format("room depth={} has invalid dims {},{}",
room.depth, room.width, room.height));
}
return true; return true;
} }

@ -19,6 +19,7 @@
using lighting::LightSource; using lighting::LightSource;
struct Room { struct Room {
int depth;
size_t x = 0; size_t x = 0;
size_t y = 0; size_t y = 0;
size_t width = 0; size_t width = 0;
@ -29,7 +30,7 @@ struct Room {
DEFINE_SERIALIZABLE(Room, x, y, width, height); DEFINE_SERIALIZABLE(Room, x, y, width, height);
}; };
void dump_map(const std::string &msg, Matrix &map); void dump_map(const std::string &msg, Matrix &map, int show_x=-1, int show_y=-1);
class Map { class Map {
public: public:
@ -62,7 +63,7 @@ public:
bool inmap(size_t x, size_t y); bool inmap(size_t x, size_t y);
bool iswall(size_t x, size_t y); bool iswall(size_t x, size_t y);
bool can_move(Point move_to); bool can_move(Point move_to);
bool neighbors(Point &out, bool up); bool neighbors(Point &out, bool random=false);
void make_paths(); void make_paths();
void set_target(const Point &at, int value=0); void set_target(const Point &at, int value=0);
@ -71,6 +72,6 @@ public:
Point map_to_camera(const Point &loc, const Point &cam_orig); Point map_to_camera(const Point &loc, const Point &cam_orig);
Point center_camera(const Point &around, size_t view_x, size_t view_y); Point center_camera(const Point &around, size_t view_x, size_t view_y);
void dump(); void dump(int show_x=-1, int show_y=-1);
bool INVARIANT(); bool INVARIANT();
}; };

@ -2,9 +2,9 @@ TODAY'S GOAL:
* Neighbors needs a rewrite * Neighbors needs a rewrite
* Neighbors algo isn't using greater parameter * Neighbors algo isn't using greater parameter
* Clean up and document as much code as possible. * Clean up and document as much code as possible.
* Doxygen? Other docs tool?
* Add a method to render.cpp that sets terminal true color. * Add a method to render.cpp that sets terminal true color.
* limit as 1000 should be a constant * limit as 1000 should be a constant
* Big Code Review
TODO: TODO:
* I can do headless windows in renderer for testing. * I can do headless windows in renderer for testing.

@ -44,7 +44,7 @@ void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player
if(ent != player.entity) { if(ent != player.entity) {
Point out = position.location; // copy Point out = position.location; // copy
if(game_map.distance(out) < config.HEARING_DISTANCE) { if(game_map.distance(out) < config.HEARING_DISTANCE) {
game_map.neighbors(out, false); game_map.neighbors(out);
motion = { int(out.x - position.location.x), int(out.y - position.location.y)}; motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
} }
} }

@ -9,25 +9,29 @@ using namespace fmt;
using namespace nlohmann; using namespace nlohmann;
using std::string; using std::string;
TEST_CASE("bsp algo test", "[map]") { TEST_CASE("bsp algo test", "[builder]") {
Map map(20, 20); Map map(20, 20);
WorldBuilder builder(map); WorldBuilder builder(map);
builder.generate(); builder.generate();
REQUIRE(map.$rooms.size() >= 2);
} }
TEST_CASE("dumping and debugging", "[map]") { TEST_CASE("dumping and debugging", "[builder]") {
Map map(20, 20); Map map(20, 20);
WorldBuilder builder(map); WorldBuilder builder(map);
builder.generate(); builder.generate();
REQUIRE(map.$rooms.size() >= 2);
dump_map("GENERATED", map.paths()); dump_map("GENERATED", map.paths());
map.dump(); map.dump();
} }
TEST_CASE("pathing", "[map]") { TEST_CASE("pathing", "[builder]") {
Map map(20, 20); Map map(20, 20);
WorldBuilder builder(map); WorldBuilder builder(map);
builder.generate(); builder.generate();
REQUIRE(map.$rooms.size() >= 2);
REQUIRE(map.can_move({0,0}) == false); REQUIRE(map.can_move({0,0}) == false);
REQUIRE(map.iswall(0,0) == true); REQUIRE(map.iswall(0,0) == true);

@ -18,6 +18,7 @@ void WorldBuilder::set_door(Room &room, int value) {
} }
void rand_side(Room &room, Point &door) { void rand_side(Room &room, Point &door) {
dbc::check(int(room.width) > 0 && int(room.height) > 0, "Weird room with 0 for height or width.");
int rand_x = Random::uniform<int>(0, room.width - 1); int rand_x = Random::uniform<int>(0, room.width - 1);
int rand_y = Random::uniform<int>(0, room.height - 1); int rand_y = Random::uniform<int>(0, room.height - 1);
@ -57,33 +58,51 @@ void WorldBuilder::partition_map(Room &cur, int depth) {
bool horiz = cur.width > cur.height ? false : true; bool horiz = cur.width > cur.height ? false : true;
int split = make_split(cur, horiz); int split = make_split(cur, horiz);
if(split <= 0) return; // end recursion
Room left = cur; Room left = cur;
Room right = cur; Room right = cur;
if(horiz) { if(horiz) {
dbc::check(split > 0, "split is not > 0"); if(split >= int(cur.height)) return; // end recursion
dbc::check(split < int(cur.height), "split is too big!");
left.height = size_t(split - 1); left.height = size_t(split - 1);
right.y = cur.y + split; right.y = cur.y + split;
right.height = size_t(cur.height - split); right.height = size_t(cur.height - split);
} else { } else {
dbc::check(split > 0, "split is not > 0"); if(split >= int(cur.width)) return; // end recursion
dbc::check(split < int(cur.width), "split is too big!");
left.width = size_t(split-1); left.width = size_t(split-1);
right.x = cur.x + split, right.x = cur.x + split,
right.width = size_t(cur.width - split); right.width = size_t(cur.width - split);
} }
if(depth > 0 && left.width > 5 && left.height > 5) { if(depth > 0 && left.width > 2 && left.height > 2) {
left.depth = depth - 1;
partition_map(left, depth-1); partition_map(left, depth-1);
} }
if(depth > 0 && right.width > 5 && right.height > 5) { if(depth > 0 && right.width > 2 && right.height > 2) {
right.depth = depth - 1;
partition_map(right, depth-1); partition_map(right, depth-1);
} }
} }
void WorldBuilder::update_door(Point &at, int wall_or_space) {
$map.$walls[at.y][at.x] = wall_or_space;
}
void WorldBuilder::tunnel_doors(PointList &holes, Room &src, Room &target) {
$map.set_target(target.entry);
$map.make_paths();
bool found = dig_tunnel(holes, src.exit, target.entry);
dbc::check(found, "room should always be found");
$map.INVARIANT();
$map.clear_target(target.entry);
}
void WorldBuilder::generate() { void WorldBuilder::generate() {
Room root{ Room root{
.x = 0, .x = 0,
@ -93,28 +112,28 @@ void WorldBuilder::generate() {
}; };
partition_map(root, 10); partition_map(root, 10);
place_rooms(root); $map.INVARIANT();
place_rooms();
PointList holes;
for(size_t i = 0; i < $map.$rooms.size() - 1; i++) { for(size_t i = 0; i < $map.$rooms.size() - 1; i++) {
Room &src = $map.$rooms[i]; tunnel_doors(holes, $map.$rooms[i], $map.$rooms[i+1]);
Room &target = $map.$rooms[i+1];
$map.set_target(target.entry);
bool found = dig_tunnel(src.exit, target.entry);
if(!found) {
println("ROOM NOT FOUND!");
}
$map.clear_target(target.entry);
} }
Room &src = $map.$rooms.back(); // one last connection from first room to last
Room &target = $map.$rooms.front(); tunnel_doors(holes, $map.$rooms.back(), $map.$rooms.front());
$map.set_target(target.entry); // place all the holes
dig_tunnel(src.exit, target.entry); for(auto hole : holes) {
$map.clear_target(target.entry); $map.$walls[hole.y][hole.x] = INV_SPACE;
}
// invert the whole map to finish it
for(size_t y = 0; y < $map.$height; ++y) { for(size_t y = 0; y < $map.$height; ++y) {
for(size_t x = 0; x < $map.$width; ++x) { for(size_t x = 0; x < $map.$width; ++x) {
// invert the map
$map.$walls[y][x] = !$map.$walls[y][x]; $map.$walls[y][x] = !$map.$walls[y][x];
} }
} }
@ -133,45 +152,39 @@ void WorldBuilder::make_room(size_t origin_x, size_t origin_y, size_t w, size_t
} }
void WorldBuilder::place_rooms(Room &cur) { void WorldBuilder::place_rooms() {
for(auto &cur : $map.$rooms) { for(auto &cur : $map.$rooms) {
cur.x += 2; cur.x += 2;
cur.y += 2; cur.y += 2;
cur.width -= 4; cur.width -= 4;
cur.height -= 4; cur.height -= 4;
add_door(cur); add_door(cur);
set_door(cur, INV_SPACE);
make_room(cur.x, cur.y, cur.width, cur.height); make_room(cur.x, cur.y, cur.width, cur.height);
} }
} }
bool WorldBuilder::dig_tunnel(Point &src, Point &target) { bool WorldBuilder::dig_tunnel(PointList &holes, Point &src, Point &target) {
Matrix &paths = $map.paths(); Matrix &paths = $map.paths();
Matrix &walls = $map.walls(); int limit = $map.limit();
walls[src.y][src.x] = INV_WALL;
walls[target.y][target.x] = INV_WALL;
// for the walk this needs to be walls since it's inverted? dbc::check(paths[src.y][src.x] != limit,
dbc::check(walls[src.y][src.x] == INV_WALL, "source room has path as a wall");
"src room has a wall at exit door"); dbc::check(paths[target.y][target.x] != limit,
dbc::check(walls[target.y][target.x] == INV_WALL, "target room has path as a wall");
"target room has a wall at entry door");
$map.make_paths();
bool found = false; bool found = false;
Point out{src.x, src.y}; Point out{src.x, src.y};
int count = 0; int count = 0;
do { do {
walls[out.y][out.x] = INV_SPACE;
found = $map.neighbors(out, true); found = $map.neighbors(out, true);
holes.push_back(out);
if(paths[out.y][out.x] == 0) { if(paths[out.y][out.x] == 0) {
walls[out.y][out.x] = INV_SPACE;
return true; return true;
} }
} while(found && out.x > 0 && out.y > 0 && ++count < 100); } while(found && ++count < 200);
return false; return false;
} }

@ -13,6 +13,8 @@ class WorldBuilder {
void add_door(Room &room); void add_door(Room &room);
void generate(); void generate();
void set_door(Room &room, int value); void set_door(Room &room, int value);
void place_rooms(Room &root); void place_rooms();
bool dig_tunnel(Point &src, Point &target); bool dig_tunnel(PointList &holes, Point &src, Point &target);
void tunnel_doors(PointList &holes, Room &src, Room &target);
void update_door(Point &at, int wall_or_space);
}; };

Loading…
Cancel
Save