Most files moved, now the stragglers.
This commit is contained in:
parent
f460add0af
commit
ebe84c4d78
20 changed files with 0 additions and 0 deletions
149
src/map.cpp
149
src/map.cpp
|
|
@ -1,149 +0,0 @@
|
|||
#include "map.hpp"
|
||||
#include "dbc.hpp"
|
||||
#include "algos/rand.hpp"
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <fmt/core.h>
|
||||
#include <utility>
|
||||
#include "algos/matrix.hpp"
|
||||
|
||||
using std::vector, std::pair;
|
||||
using namespace fmt;
|
||||
|
||||
Map::Map(size_t width, size_t height) :
|
||||
$width(width),
|
||||
$height(height),
|
||||
$walls(height, matrix::Row(width, SPACE_VALUE)),
|
||||
$paths(width, height)
|
||||
{}
|
||||
|
||||
Map::Map(Matrix &walls, Pathing &paths) :
|
||||
$walls(walls),
|
||||
$paths(paths)
|
||||
{
|
||||
$width = matrix::width(walls);
|
||||
$height = matrix::height(walls);
|
||||
}
|
||||
|
||||
void Map::make_paths() {
|
||||
INVARIANT();
|
||||
$paths.compute_paths($walls);
|
||||
}
|
||||
|
||||
bool Map::inmap(size_t x, size_t y) {
|
||||
return x < $width && y < $height;
|
||||
}
|
||||
|
||||
void Map::set_target(const Point &at, int value) {
|
||||
$paths.set_target(at, value);
|
||||
}
|
||||
|
||||
void Map::clear_target(const Point &at) {
|
||||
$paths.clear_target(at);
|
||||
}
|
||||
|
||||
bool Map::place_entity(size_t room_index, Point &out) {
|
||||
dbc::check($dead_ends.size() != 0, "no dead ends?!");
|
||||
|
||||
if(room_index < $rooms.size()) {
|
||||
Room &start = $rooms.at(room_index);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out = $dead_ends.at(room_index % $dead_ends.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Map::iswall(size_t x, size_t y) {
|
||||
return $walls[y][x] == WALL_VALUE;
|
||||
}
|
||||
|
||||
void Map::dump(int show_x, int show_y) {
|
||||
matrix::dump("WALLS", walls(), show_x, show_y);
|
||||
matrix::dump("PATHS", paths(), show_x, show_y);
|
||||
}
|
||||
|
||||
bool Map::can_move(Point move_to) {
|
||||
return inmap(move_to.x, move_to.y) &&
|
||||
!iswall(move_to.x, move_to.y);
|
||||
}
|
||||
|
||||
Point Map::map_to_camera(const Point &loc, const Point &cam_orig) {
|
||||
return {loc.x - cam_orig.x, loc.y - cam_orig.y};
|
||||
}
|
||||
|
||||
Point Map::center_camera(const Point &around, size_t view_x, size_t view_y) {
|
||||
int high_x = int(width() - view_x);
|
||||
int high_y = int(height() - view_y);
|
||||
int center_x = int(around.x - view_x / 2);
|
||||
int center_y = int(around.y - view_y / 2);
|
||||
|
||||
size_t start_x = high_x > 0 ? std::clamp(center_x, 0, high_x) : 0;
|
||||
size_t start_y = high_y > 0 ? std::clamp(center_y, 0, high_y) : 0;
|
||||
|
||||
return {start_x, start_y};
|
||||
}
|
||||
|
||||
bool Map::random_walk(Point &out, bool random, int direction) {
|
||||
int choice = Random::uniform(0,4);
|
||||
return $paths.find_path(out, direction, random && choice == 0) != PathingResult::FAIL;
|
||||
}
|
||||
|
||||
bool Map::INVARIANT() {
|
||||
using dbc::check;
|
||||
|
||||
check($walls.size() == height(), "walls wrong height");
|
||||
check($walls[0].size() == width(), "walls wrong width");
|
||||
check($paths.$width == width(), "in Map paths width don't match map width");
|
||||
check($paths.$height == height(), "in Map paths height don't match map height");
|
||||
|
||||
for(auto room : $rooms) {
|
||||
check(int(room.x) >= 0 && int(room.y) >= 0,
|
||||
format("room invalid position {},{}",
|
||||
room.x, room.y));
|
||||
check(int(room.width) > 0 && int(room.height) > 0,
|
||||
format("room has invalid dims {},{}",
|
||||
room.width, room.height));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Map::init_tiles() {
|
||||
$tiles = $walls;
|
||||
}
|
||||
|
||||
void Map::enclose() {
|
||||
// wraps the outside edge with solid walls
|
||||
std::array<Point, 4> starts{{
|
||||
{0,0}, {$width-1, 0}, {$width-1, $height-1}, {0, $height-1}
|
||||
}};
|
||||
|
||||
std::array<Point, 4> ends{{
|
||||
{$width-1, 0}, {$width-1, $height-1}, {0, $height-1}, {0,0},
|
||||
}};
|
||||
|
||||
for(size_t i = 0; i < starts.size(); i++) {
|
||||
for(matrix::line it{starts[i], ends[i]}; it.next();) {
|
||||
$walls[it.y][it.x] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Map::add_room(Room &room) {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue