Map generation is working well, and some cleanup.

main
Zed A. Shaw 3 months ago
parent 3a43324fa2
commit bcc524861e
  1. 77
      map.cpp
  2. 11
      map.hpp

@ -21,20 +21,20 @@ void dump_map(const std::string &msg, Matrix &map) {
}
}
inline void add_neighbors(PairList &neighbors, Matrix &closed, size_t j, size_t i) {
inline void add_neighbors(PointList &neighbors, Matrix &closed, size_t y, size_t x) {
size_t h = closed.size();
size_t w = closed[0].size();
vector<size_t> rows{j - 1, j, j + 1};
vector<size_t> cols{i - 1, i, i + 1};
vector<size_t> rows{y - 1, y, y + 1};
vector<size_t> cols{x - 1, x, x + 1};
for(auto row : rows) {
for(auto col : cols) {
for(size_t row : rows) {
for(size_t col : cols) {
if((0 <= row && row < h) &&
(0 <= col && col < w) &&
closed[row][col] == 0)
{
closed[row][col] = 1;
neighbors.push_back({.j=row, .i=col});
neighbors.push_back({.x=col, .y=row});
}
}
}
@ -59,39 +59,39 @@ void Map::make_paths() {
int limit = m_limit == 0 ? h * w : m_limit;
Matrix new_arr = Matrix(h, MatrixRow(w, limit));
Matrix closed = m_walls;
PairList starting_pixels;
PairList open_pixels;
PointList starting_pixels;
PointList open_pixels;
// First pass: Add starting pixels and put them in closed
for(size_t counter = 0; counter < h * w; counter++) {
size_t i = counter % w;
size_t j = counter / w;
if(m_input_map[j][i] == 0) {
new_arr[j][i] = 0;
closed[j][i] = 1;
starting_pixels.push_back({.j=j,.i=i});
size_t x = counter % w;
size_t y = counter / w;
if(m_input_map[y][x] == 0) {
new_arr[y][x] = 0;
closed[y][x] = 1;
starting_pixels.push_back({.x=x,.y=y});
}
}
// Second pass: Add border to open
for(auto sp : starting_pixels) {
add_neighbors(open_pixels, closed, sp.j, sp.i);
add_neighbors(open_pixels, closed, sp.y, sp.x);
}
// Third pass: Iterate filling in the open list
int counter = 1; // leave this here so it's available below
for(; counter < limit && !open_pixels.empty(); ++counter) {
PairList next_open;
PointList next_open;
for(auto sp : open_pixels) {
new_arr[sp.j][sp.i] = counter;
add_neighbors(next_open, closed, sp.j, sp.i);
new_arr[sp.y][sp.x] = counter;
add_neighbors(next_open, closed, sp.y, sp.x);
}
open_pixels = next_open;
}
// Last pass: flood last pixels
for(auto sp : open_pixels) {
new_arr[sp.j][sp.i] = counter;
new_arr[sp.y][sp.x] = counter;
}
m_paths = new_arr;
@ -217,24 +217,23 @@ void Map::add_door(Room &room) {
room.exit.y = room.y + room.height;
}
bool Map::walk(Room &src, Room &target) {
bool Map::walk(Point &src, Point &target) {
// this sets the target for the path
dbc::check(m_input_map[target.entry.y][target.entry.x] == 0, "target point not set to 0");
dbc::check(m_input_map[target.y][target.x] == 0, "target point not set to 0");
m_walls[src.exit.y][src.exit.x] = INV_WALL;
m_walls[target.entry.y][target.entry.x] = INV_WALL;
m_walls[src.y][src.x] = INV_WALL;
m_walls[target.y][target.x] = INV_WALL;
// for the walk this needs to be walls since it's inverted?
dbc::check(m_walls[src.exit.y][src.exit.x] == INV_WALL,
dbc::check(m_walls[src.y][src.x] == INV_WALL,
"src room has a wall at exit door");
dbc::check(m_walls[target.entry.y][target.entry.x] == INV_WALL,
dbc::check(m_walls[target.y][target.x] == INV_WALL,
"target room has a wall at entry door");
make_paths();
bool found = false;
int count = 0;
Point out{src.exit.x, src.exit.y};
Point out{src.x, src.y};
do {
m_walls[out.y][out.x] = INV_SPACE;
@ -249,6 +248,14 @@ bool Map::walk(Room &src, Room &target) {
return false;
}
void Map::set_target(Point &at, int value) {
m_input_map[at.y][at.x] = 0;
}
void Map::clear_target(Point &at) {
m_input_map[at.y][at.x] = 1;
}
void Map::generate() {
Room root{
.x = 0,
@ -263,20 +270,20 @@ void Map::generate() {
for(size_t i = 0; i < m_rooms.size() - 1; i++) {
Room &src = m_rooms[i];
Room &target = m_rooms[i+1];
m_input_map[target.entry.y][target.entry.x] = 0;
bool found = walk(src, target);
set_target(target.entry);
bool found = walk(src.exit, target.entry);
if(!found) {
println("ROOM NOT FOUND!");
}
m_input_map[target.entry.y][target.entry.x] = 1;
clear_target(target.entry);
}
Room &src = m_rooms[m_rooms.size()-1];
Room &target = m_rooms[0];
m_input_map[target.entry.y][target.entry.x] = 0;
walk(src, target);
Room &src = m_rooms.back();
Room &target = m_rooms.front();
dump_map("WALLS", m_walls);
set_target(target.entry);
walk(src.exit, target.entry);
clear_target(target.entry);
for(size_t y = 0; y < height(); ++y) {
for(size_t x = 0; x < width(); ++x) {

@ -15,11 +15,6 @@ struct Point {
size_t y = 0;
};
struct Pair {
size_t j = 0;
size_t i = 0;
};
struct Room;
struct Room {
@ -31,7 +26,7 @@ struct Room {
Point exit;
};
typedef std::vector<Pair> PairList;
typedef std::vector<Point> PointList;
typedef std::vector<int> MatrixRow;
typedef std::vector<MatrixRow> Matrix;
@ -74,7 +69,9 @@ public:
void place_rooms(Room &root);
void make_paths();
void partition_map(Room &cur, int depth);
bool walk(Room &src, Room &target);
void set_target(Point &at, int value=0);
void clear_target(Point &at);
bool walk(Point &src, Point &target);
void set_door(Room &room, int value);
void dump();
};

Loading…
Cancel
Save