Conver to using \ for member variables in classes. In structs just use the name.

This commit is contained in:
Zed A. Shaw 2024-10-03 17:05:23 -04:00
parent 187edb898e
commit 5cf66aad02
8 changed files with 143 additions and 144 deletions

96
map.cpp
View file

@ -5,8 +5,8 @@
#include <random>
#include <utility>
std::random_device g_rng;
std::mt19937 g_generator(g_rng());
std::random_device $RNG;
std::mt19937 $GENERATOR($RNG());
using std::vector, std::pair;
using namespace fmt;
@ -45,20 +45,20 @@ inline void add_neighbors(PointList &neighbors, Matrix &closed, size_t y, size_t
* can run make_rooms and generate on. It will
* NOT be valid until you actually run generate.
*/
Map::Map(size_t width, size_t height) : limit_(1000) {
walls_ = Matrix(height, MatrixRow(width, INV_WALL));
input_map_ = Matrix(height, MatrixRow(width, 1));
Map::Map(size_t width, size_t height) : $limit(1000) {
$walls = Matrix(height, MatrixRow(width, INV_WALL));
$input_map = Matrix(height, MatrixRow(width, 1));
}
void Map::make_paths() {
size_t h = input_map_.size();
size_t w = input_map_[0].size();
size_t h = $input_map.size();
size_t w = $input_map[0].size();
// Initialize the new array with every pixel at limit distance
// NOTE: this is normally ones() * limit
int limit = limit_ == 0 ? h * w : limit_;
int limit = $limit == 0 ? h * w : $limit;
Matrix new_arr = Matrix(h, MatrixRow(w, limit));
Matrix closed = walls_;
Matrix closed = $walls;
PointList starting_pixels;
PointList open_pixels;
@ -66,7 +66,7 @@ void Map::make_paths() {
for(size_t counter = 0; counter < h * w; counter++) {
size_t x = counter % w;
size_t y = counter / w;
if(input_map_[y][x] == 0) {
if($input_map[y][x] == 0) {
new_arr[y][x] = 0;
closed[y][x] = 1;
starting_pixels.push_back({.x=x,.y=y});
@ -94,7 +94,7 @@ void Map::make_paths() {
new_arr[sp.y][sp.x] = counter;
}
paths_ = new_arr;
$paths = new_arr;
}
void Map::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {
@ -104,10 +104,10 @@ void Map::make_room(size_t origin_x, size_t origin_y, size_t w, size_t h) {
dbc::pre("h out of bounds", h <= height());
for(size_t y = origin_y; y < origin_y + h; ++y) {
dbc::check(y < walls_.size(), "y is out of bounds");
dbc::check(y < $walls.size(), "y is out of bounds");
for(size_t x = origin_x; x < origin_x + w; ++x) {
dbc::check(x < walls_[y].size(), "x is out of bounds");
walls_[y][x] = INV_SPACE;
dbc::check(x < $walls[y].size(), "x is out of bounds");
$walls[y][x] = INV_SPACE;
}
}
}
@ -117,13 +117,13 @@ inline int make_split(Room &cur, bool horiz) {
int min = dimension / 4;
int max = dimension - min;
std::uniform_int_distribution<int> rand_dim(min, max);
return rand_dim(g_generator);
return rand_dim($GENERATOR);
}
void Map::partition_map(Room &cur, int depth) {
if(cur.width >= 5 && cur.width <= 10 &&
cur.height >= 5 && cur.height <= 10) {
rooms_.push_back(cur);
$rooms.push_back(cur);
return;
}
@ -158,7 +158,7 @@ void Map::partition_map(Room &cur, int depth) {
}
void Map::place_rooms(Room &cur) {
for(auto &cur : rooms_) {
for(auto &cur : $rooms) {
cur.x += 2;
cur.y += 2;
cur.width -= 4;
@ -178,7 +178,7 @@ bool Map::neighbors(Point &out, bool greater) {
}};
int zero_i = -1;
int cur = paths_[out.y][out.x];
int cur = $paths[out.y][out.x];
if(cur == 1000) {
// BUG: sometimes the generation clips a door and we
@ -188,7 +188,7 @@ bool Map::neighbors(Point &out, bool greater) {
for(int i = 0; i < 4; ++i) {
Point dir = dirs[i];
int diff = inmap(dir.x, dir.y) ? cur - paths_[dir.y][dir.x] : -1000;
int diff = inmap(dir.x, dir.y) ? cur - $paths[dir.y][dir.x] : -1000;
if(diff == 1) {
out = {.x=dir.x, .y=dir.y};
@ -211,8 +211,8 @@ bool Map::inmap(size_t x, size_t y) {
}
void Map::set_door(Room &room, int value) {
walls_[room.entry.y][room.entry.x] = value;
walls_[room.exit.y][room.exit.x] = value;
$walls[room.entry.y][room.entry.x] = value;
$walls[room.exit.y][room.exit.x] = value;
}
void rand_side(Room &room, Point &door) {
@ -220,22 +220,22 @@ void rand_side(Room &room, Point &door) {
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)) {
switch(rand_side($GENERATOR)) {
case 0: // north
door.x = room.x + rand_x(g_generator);
door.x = room.x + rand_x($GENERATOR);
door.y = room.y-1;
break;
case 1: // south
door.x = room.x + rand_x(g_generator);
door.x = room.x + rand_x($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);
door.y = room.y + rand_y($GENERATOR);
break;
case 3: // west
door.x = room.x - 1;
door.y = room.y + rand_y(g_generator);
door.y = room.y + rand_y($GENERATOR);
break;
default:
dbc::sentinel("impossible side");
@ -249,15 +249,15 @@ void Map::add_door(Room &room) {
bool Map::walk(Point &src, Point &target) {
// this sets the target for the path
dbc::check(input_map_[target.y][target.x] == 0, "target point not set to 0");
dbc::check($input_map[target.y][target.x] == 0, "target point not set to 0");
walls_[src.y][src.x] = INV_WALL;
walls_[target.y][target.x] = INV_WALL;
$walls[src.y][src.x] = INV_WALL;
$walls[target.y][target.x] = INV_WALL;
// for the walk this needs to be walls since it's inverted?
dbc::check(walls_[src.y][src.x] == INV_WALL,
dbc::check($walls[src.y][src.x] == INV_WALL,
"src room has a wall at exit door");
dbc::check(walls_[target.y][target.x] == INV_WALL,
dbc::check($walls[target.y][target.x] == INV_WALL,
"target room has a wall at entry door");
make_paths();
@ -266,11 +266,11 @@ bool Map::walk(Point &src, Point &target) {
int count = 0;
do {
walls_[out.y][out.x] = INV_SPACE;
$walls[out.y][out.x] = INV_SPACE;
found = neighbors(out, true);
if(paths_[out.y][out.x] == 0) {
walls_[out.y][out.x] = INV_SPACE;
if($paths[out.y][out.x] == 0) {
$walls[out.y][out.x] = INV_SPACE;
return true;
}
} while(found && out.x > 0 && out.y > 0 && ++count < 100);
@ -279,17 +279,17 @@ bool Map::walk(Point &src, Point &target) {
}
void Map::set_target(Point &at, int value) {
input_map_[at.y][at.x] = 0;
$input_map[at.y][at.x] = 0;
}
void Map::clear_target(Point &at) {
input_map_[at.y][at.x] = 1;
$input_map[at.y][at.x] = 1;
}
Point Map::place_entity(size_t room_index) {
dbc::check(room_index < rooms_.size(), "room_index is out of bounds, not enough rooms");
dbc::check(room_index < $rooms.size(), "room_index is out of bounds, not enough rooms");
Room &start = rooms_[room_index];
Room &start = $rooms[room_index];
return {start.x+1, start.y+1};
}
@ -304,9 +304,9 @@ void Map::generate() {
partition_map(root, 10);
place_rooms(root);
for(size_t i = 0; i < rooms_.size() - 1; i++) {
Room &src = rooms_[i];
Room &target = rooms_[i+1];
for(size_t i = 0; i < $rooms.size() - 1; i++) {
Room &src = $rooms[i];
Room &target = $rooms[i+1];
set_target(target.entry);
bool found = walk(src.exit, target.entry);
if(!found) {
@ -315,8 +315,8 @@ void Map::generate() {
clear_target(target.entry);
}
Room &src = rooms_.back();
Room &target = rooms_.front();
Room &src = $rooms.back();
Room &target = $rooms.front();
set_target(target.entry);
walk(src.exit, target.entry);
@ -324,17 +324,17 @@ void Map::generate() {
for(size_t y = 0; y < height(); ++y) {
for(size_t x = 0; x < width(); ++x) {
walls_[y][x] = !walls_[y][x];
$walls[y][x] = !$walls[y][x];
}
}
}
bool Map::iswall(size_t x, size_t y) {
return walls_[y][x] == WALL_VALUE;
return $walls[y][x] == WALL_VALUE;
}
void Map::dump() {
dump_map("PATHS", paths_);
dump_map("WALLS", walls_);
dump_map("INPUT", input_map_);
dump_map("PATHS", $paths);
dump_map("WALLS", $walls);
dump_map("INPUT", $input_map);
}