Refactor the light calculations to be part of map instead of spread out all over. Still need to bring back lighting on walls and also pathing for enemies is currently busted.

This commit is contained in:
Zed A. Shaw 2024-11-28 02:41:01 -05:00
parent 0e8a2e520a
commit 54fa1a23ce
7 changed files with 168 additions and 112 deletions

67
map.hpp
View file

@ -7,12 +7,15 @@
#include <fmt/core.h>
#include "point.hpp"
#include "tser.hpp"
#include "lights.hpp"
#define INV_WALL 0
#define INV_SPACE 1
#define WALL_VALUE 1
#define SPACE_VALUE 0
using lighting::LightSource;
struct Room {
size_t x = 0;
size_t y = 0;
@ -33,10 +36,12 @@ void add_neighbors(Matrix &closed, size_t j, size_t i);
class Map {
public:
int $limit;
size_t $width;
size_t $height;
Matrix $input_map;
Matrix $walls;
Matrix $paths;
Matrix $lighting; // BUG: this is not the place
Matrix $lightmap;
std::vector<Room> $rooms;
Map(Matrix input_map, Matrix walls_map, int limit);
@ -48,58 +53,42 @@ public:
Map(Map &map) = delete;
Matrix& paths() { return $paths; }
Matrix& lighting() { return $lighting; }
Matrix& lighting() { return $lightmap; }
Matrix& input_map() { return $input_map; }
Matrix& walls() { return $walls; }
int limit() { return $limit; }
size_t width() { return $walls[0].size(); }
size_t height() { return $walls.size(); }
size_t width() { return $width; }
size_t height() { return $height; }
int distance(Point to) { return $paths[to.y][to.x]; }
Room &room(size_t at) {
return $rooms[at];
}
size_t room_count() {
return $rooms.size();
}
Room &room(size_t at) { return $rooms[at]; }
size_t room_count() { return $rooms.size(); }
void partition_map(Room &cur, int depth);
void make_room(size_t origin_y, size_t origin_x, size_t width, size_t height);
void add_door(Room &room);
bool inmap(size_t x, size_t y);
bool iswall(size_t x, size_t y);
bool can_move(Point move_to) {
return inmap(move_to.x, move_to.y) &&
!iswall(move_to.x, move_to.y);
}
bool neighbors(Point &out, bool up);
bool can_move(Point move_to);
void generate();
void place_rooms(Room &root);
Point place_entity(size_t room_index);
bool neighbors(Point &out, bool up);
bool inmap(size_t x, size_t y);
bool iswall(size_t x, size_t y);
void make_paths();
void partition_map(Room &cur, int depth);
void set_target(const Point &at, int value=0);
void clear_target(const Point &at);
bool walk(Point &src, Point &target);
void set_door(Room &room, int value);
Point map_to_camera(const Point &loc, const Point &cam_orig);
Point center_camera(const Point &around, size_t view_x, size_t view_y);
void reset_light();
void set_light_target(const Point &at, int value=0);
void path_light();
void light_box(LightSource source, Point from, Point &min_out, Point &max_out);
int light_level(int level, size_t x, size_t y);
void render_light(LightSource source, Point at);
void dump();
Point place_entity(size_t room_index);
Point map_to_camera(const Point &loc, const Point &cam_orig) {
return {loc.x - cam_orig.x, loc.y - cam_orig.y};
}
Point 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};
}
};