Make doors randomly on the rooms.

main
Zed A. Shaw 3 months ago
parent dd6d29ed7d
commit 69fa7d9e4e
  1. 10
      main.cpp
  2. 42
      map.cpp

@ -23,7 +23,6 @@ using std::string;
using namespace fmt;
using namespace std::chrono_literals;
int main() {
using namespace ftxui;
std::string reset_position;
@ -43,6 +42,7 @@ int main() {
string dead_yet = "NOT DEAD";
bool show_paths = false;
bool bomb = false;
auto map = Renderer([&] {
game_map.set_target(me);
@ -63,6 +63,12 @@ int main() {
}
}
if(bomb) {
/// draw
bomb = false;
c.DrawPointCircle(me.x*2, me.y*4, 4, Color::Red);
}
c.DrawText(enemy.x*2, enemy.y*4, "!", Color::Red);
c.DrawText(me.x*2, me.y*4, "@", Color::Blue);
c.DrawText(goal.x*2, goal.y*4, "$", Color::Yellow);
@ -95,6 +101,8 @@ int main() {
y += 1;
} else if(e == Event::ArrowUp) {
y -= 1;
} else if(e == Event::Backspace) {
bomb = true;
} else {
return false;
}

@ -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…
Cancel
Save