Mostly cleaned up world get to handle more rooms and paths, but rando_rect needs to be actually random.

This commit is contained in:
Zed A. Shaw 2025-01-12 14:57:54 -05:00
parent c19c53b15b
commit e9277bf052
11 changed files with 257 additions and 39 deletions

43
map.cpp
View file

@ -44,18 +44,20 @@ void Map::clear_target(const Point &at) {
$paths.clear_target(at);
}
Point Map::place_entity(size_t room_index) {
bool Map::place_entity(size_t room_index, Point &out) {
dbc::check(room_index < $rooms.size(), "room_index is out of bounds, not enough rooms");
Room &start = $rooms[room_index];
size_t size = std::max(start.width, start.height);
for(matrix::box it{$walls, start.x, start.y, size}; it.next();) {
if(!iswall(it.x, it.y)) return {it.x, it.y};
for(matrix::rando_rect it{$walls, start.x, start.y, start.width, start.height}; it.next();) {
if(!iswall(it.x, it.y)) {
out.x = it.x;
out.y = it.y;
return true;
}
}
dbc::sentinel("DIDN'T FIND AN OPEN SPACE!");
return {start.x, start.y};
return false;
}
bool Map::iswall(size_t x, size_t y) {
@ -205,3 +207,32 @@ void Map::expand() {
$paths = Pathing($width, $height);
$tiles = TileMap($width, $height);
}
void Map::add_room(Room &room) {
// println(">>ADDING ROOM x/y={},{}; w/h={},{}; map={},{}",
// room.x, room.y, room.width, room.height, $width, $height);
room.x++;
room.y++;
room.width--;
room.height--;
if(room.x + room.width >= $width) {
// fix the width
room.x--;
}
if(room.y + room.height >= $height) {
// fix the height
room.y--;
}
$rooms.push_back(room);
}
void Map::invert_space() {
for(matrix::each_cell it{$walls}; it.next();) {
int is_wall = !$walls[it.y][it.x];
$walls[it.y][it.x] = is_wall;
}
}