You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
2.8 KiB
129 lines
2.8 KiB
#include "map.hpp"
|
|
#include "dbc.hpp"
|
|
#include <vector>
|
|
#include <array>
|
|
#include <fmt/core.h>
|
|
#include <utility>
|
|
|
|
using std::vector, std::pair;
|
|
using namespace fmt;
|
|
|
|
void dump_map(const std::string &msg, Matrix &map) {
|
|
println("----------------- {}", msg);
|
|
for(auto row : map) {
|
|
for(auto col : row) {
|
|
print("{} ", col);
|
|
}
|
|
print("\n");
|
|
}
|
|
}
|
|
|
|
Map::Map(size_t width, size_t height) :
|
|
$limit(1000),
|
|
$width(width),
|
|
$height(height),
|
|
$walls(height, MatrixRow(width, INV_WALL)),
|
|
$paths(height, width, 1000)
|
|
{}
|
|
|
|
void Map::make_paths() {
|
|
$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);
|
|
}
|
|
|
|
Point Map::place_entity(size_t room_index) {
|
|
dbc::check(room_index < $rooms.size(), "room_index is out of bounds, not enough rooms");
|
|
|
|
Room &start = $rooms[room_index];
|
|
return {start.x+1, start.y+1};
|
|
}
|
|
|
|
bool Map::iswall(size_t x, size_t y) {
|
|
return $walls[y][x] == WALL_VALUE;
|
|
}
|
|
|
|
void Map::dump() {
|
|
dump_map("WALLS", $walls);
|
|
}
|
|
|
|
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);
|
|
|
|
// BUG: is clamp really the best thing here? this seems wrong.
|
|
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::neighbors(Point &out, bool greater) {
|
|
Matrix &paths = $paths.$paths;
|
|
|
|
std::array<Point, 4> dirs{{
|
|
{out.x,out.y-1},
|
|
{out.x+1,out.y},
|
|
{out.x,out.y+1},
|
|
{out.x-1,out.y}
|
|
}};
|
|
|
|
int zero_i = -1;
|
|
int cur = paths[out.y][out.x];
|
|
|
|
// BUG: sometimes cur is in a wall so finding neighbors fails
|
|
for(size_t i = 0; i < dirs.size(); ++i) {
|
|
Point dir = dirs[i];
|
|
int target = inmap(dir.x, dir.y) ? paths[dir.y][dir.x] : $limit;
|
|
|
|
if(target == $limit) continue; // skip unpathable stuff
|
|
|
|
int diff = cur - target;
|
|
|
|
if(diff == 1) {
|
|
out = {.x=dir.x, .y=dir.y};
|
|
return true;
|
|
} else if(diff == 0) {
|
|
zero_i = i;
|
|
}
|
|
}
|
|
|
|
if(zero_i != -1) {
|
|
out = {.x=dirs[zero_i].x, .y=dirs[zero_i].y};
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
bool Map::INVARIANT() {
|
|
using dbc::check;
|
|
|
|
check($walls.size() == height(), "walls wrong height");
|
|
check($walls[0].size() == width(), "walls wrong width");
|
|
|
|
return true;
|
|
}
|
|
|