Make doors randomly on the rooms.
This commit is contained in:
parent
dd6d29ed7d
commit
69fa7d9e4e
2 changed files with 44 additions and 8 deletions
42
map.cpp
42
map.cpp
|
@ -179,7 +179,12 @@ bool Map::neighbors(Point &out, bool greater) {
|
|||
|
||||
int zero_i = -1;
|
||||
int cur = m_paths[out.y][out.x];
|
||||
dbc::check(cur != 1000, "WRONG! start point is in a wall");
|
||||
|
||||
if(cur == 1000) {
|
||||
// BUG: sometimes the generation clips a door and we
|
||||
// start in a wall
|
||||
return false;
|
||||
}
|
||||
|
||||
for(int i = 0; i < 4; ++i) {
|
||||
Point dir = dirs[i];
|
||||
|
@ -210,11 +215,36 @@ void Map::set_door(Room &room, int value) {
|
|||
m_walls[room.exit.y][room.exit.x] = value;
|
||||
}
|
||||
|
||||
void rand_side(Room &room, Point &door) {
|
||||
std::uniform_int_distribution<int> rand_side(0, 3);
|
||||
std::uniform_int_distribution<int> rand_x(0, room.width - 1);
|
||||
std::uniform_int_distribution<int> rand_y(0, room.height - 1);
|
||||
|
||||
switch(rand_side(g_generator)) {
|
||||
case 0: // north
|
||||
door.x = room.x + rand_x(g_generator);
|
||||
door.y = room.y-1;
|
||||
break;
|
||||
case 1: // south
|
||||
door.x = room.x + rand_x(g_generator);
|
||||
door.y = room.y + room.height;
|
||||
break;
|
||||
case 2: // east
|
||||
door.x = room.x + room.width;
|
||||
door.y = room.y + rand_y(g_generator);
|
||||
break;
|
||||
case 3: // west
|
||||
door.x = room.x - 1;
|
||||
door.y = room.y + rand_y(g_generator);
|
||||
break;
|
||||
default:
|
||||
dbc::sentinel("impossible side");
|
||||
}
|
||||
}
|
||||
|
||||
void Map::add_door(Room &room) {
|
||||
room.entry.x = room.x;
|
||||
room.entry.y = room.y-1;
|
||||
room.exit.x = room.x + room.width - 1;
|
||||
room.exit.y = room.y + room.height;
|
||||
rand_side(room, room.entry);
|
||||
rand_side(room, room.exit);
|
||||
}
|
||||
|
||||
bool Map::walk(Point &src, Point &target) {
|
||||
|
@ -231,10 +261,8 @@ bool Map::walk(Point &src, Point &target) {
|
|||
"target room has a wall at entry door");
|
||||
|
||||
make_paths();
|
||||
|
||||
bool found = false;
|
||||
Point out{src.x, src.y};
|
||||
|
||||
do {
|
||||
m_walls[out.y][out.x] = INV_SPACE;
|
||||
found = neighbors(out, true);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue