Make doors randomly on the rooms.

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

@ -179,7 +179,12 @@ bool Map::neighbors(Point &out, bool greater) {
int zero_i = -1; int zero_i = -1;
int cur = m_paths[out.y][out.x]; 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) { for(int i = 0; i < 4; ++i) {
Point dir = dirs[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; 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) { void Map::add_door(Room &room) {
room.entry.x = room.x; rand_side(room, room.entry);
room.entry.y = room.y-1; rand_side(room, room.exit);
room.exit.x = room.x + room.width - 1;
room.exit.y = room.y + room.height;
} }
bool Map::walk(Point &src, Point &target) { 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"); "target room has a wall at entry door");
make_paths(); make_paths();
bool found = false; bool found = false;
Point out{src.x, src.y}; Point out{src.x, src.y};
do { do {
m_walls[out.y][out.x] = INV_SPACE; m_walls[out.y][out.x] = INV_SPACE;
found = neighbors(out, true); found = neighbors(out, true);

Loading…
Cancel
Save