2017-12-10 19:51:24 +01:00
|
|
|
#include <stdlib.h>
|
2017-12-05 15:03:20 +01:00
|
|
|
#include "roommatrix.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "map.h"
|
|
|
|
|
|
|
|
RoomMatrix* roommatrix_create()
|
|
|
|
{
|
|
|
|
RoomMatrix *m = ec_malloc(sizeof(RoomMatrix));
|
|
|
|
roommatrix_reset(m);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
void roommatrix_populate_from_map(RoomMatrix *rm, Map *m)
|
|
|
|
{
|
2017-12-13 23:20:54 +01:00
|
|
|
int i, j, monster_count;
|
|
|
|
Position monster_matrix_pos;
|
2017-12-05 15:03:20 +01:00
|
|
|
Room *r;
|
2017-12-13 23:20:54 +01:00
|
|
|
Monster *monster;
|
2017-12-05 15:03:20 +01:00
|
|
|
|
2017-12-07 23:58:29 +01:00
|
|
|
roommatrix_reset(rm);
|
|
|
|
|
2017-12-06 11:44:17 +01:00
|
|
|
rm->roomPos = m->currentRoom;
|
|
|
|
r = m->rooms[rm->roomPos.x][rm->roomPos.y];
|
2017-12-05 15:03:20 +01:00
|
|
|
|
|
|
|
for (i = 0; i < MAP_ROOM_WIDTH; ++i) {
|
|
|
|
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
|
2017-12-10 19:51:24 +01:00
|
|
|
if (r->tiles[i][j]) {
|
2017-12-08 09:45:57 +01:00
|
|
|
rm->spaces[i][j].occupied = r->tiles[i][j]->collider;
|
2017-12-10 19:51:24 +01:00
|
|
|
rm->spaces[i][j].lightsource = r->tiles[i][j]->lightsource;
|
|
|
|
}
|
|
|
|
if (r->decorations[i][j]) {
|
|
|
|
rm->spaces[i][j].occupied |= r->decorations[i][j]->collider;
|
|
|
|
rm->spaces[i][j].lightsource |= r->decorations[i][j]->lightsource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 23:20:54 +01:00
|
|
|
|
|
|
|
monster_count = linkedlist_size(m->monsters);
|
|
|
|
for (i = 0; i < monster_count; ++i) {
|
|
|
|
monster = linkedlist_get(&m->monsters, i);
|
|
|
|
if (!position_in_room(&monster->sprite->pos, &m->currentRoom))
|
|
|
|
continue;
|
|
|
|
monster_matrix_pos = position_to_matrix_coords(&monster->sprite->pos);
|
|
|
|
rm->spaces[monster_matrix_pos.x][monster_matrix_pos.y].occupied = true;
|
|
|
|
}
|
2017-12-10 19:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
min(int a, int b)
|
|
|
|
{
|
|
|
|
return a > b ? b : a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
max(int a, int b)
|
|
|
|
{
|
|
|
|
return a > b ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
roommatrix_add_lightsource(RoomMatrix *matrix, Position *pos)
|
|
|
|
{
|
|
|
|
Position mpos = position_to_matrix_coords(pos);
|
|
|
|
matrix->spaces[mpos.x][mpos.y].lightsource = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_light_for_tile(RoomMatrix *matrix, int x, int y)
|
|
|
|
{
|
|
|
|
int x_max, x_min, y_max, y_min, i, j;
|
2017-12-10 23:49:15 +01:00
|
|
|
int lightval, distance_modifier;
|
2017-12-10 19:51:24 +01:00
|
|
|
RoomSpace *space;
|
|
|
|
|
|
|
|
space = &matrix->spaces[x][y];
|
|
|
|
if (!space->lightsource)
|
|
|
|
return;
|
|
|
|
|
|
|
|
space->light = 255;
|
|
|
|
|
|
|
|
x_max = min(x + 5, MAP_ROOM_WIDTH - 1);
|
|
|
|
x_min = max(x - 5, 0);
|
|
|
|
y_max = min(y + 5, MAP_ROOM_HEIGHT - 1);
|
|
|
|
y_min = max(y - 5, 0);
|
|
|
|
|
|
|
|
for (i = x_min; i <= x_max; ++i) {
|
|
|
|
for (j = y_min; j <= y_max; ++j) {
|
|
|
|
lightval = matrix->spaces[i][j].light;
|
2017-12-10 23:49:15 +01:00
|
|
|
distance_modifier = abs(x-i) == abs(y-j) ? abs(x-i) + 1 : max(abs(x-i), abs(y-j));
|
|
|
|
lightval += 255 - (distance_modifier * 50);
|
2017-12-10 19:51:24 +01:00
|
|
|
lightval = min(255, lightval);
|
|
|
|
lightval = max(0, lightval);
|
|
|
|
matrix->spaces[i][j].light = lightval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
roommatrix_build_lightmap(RoomMatrix *matrix)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < MAP_ROOM_WIDTH; ++i) {
|
|
|
|
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
|
|
|
|
set_light_for_tile(matrix, i, j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
roommatrix_render_lightmap(RoomMatrix *matrix, Camera *cam)
|
|
|
|
{
|
|
|
|
int i, j, light;
|
|
|
|
|
|
|
|
for (i = 0; i < MAP_ROOM_WIDTH; ++i) {
|
|
|
|
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
|
|
|
|
light = max(0, 230 - matrix->spaces[i][j].light);
|
|
|
|
SDL_SetRenderDrawColor(cam->renderer,
|
|
|
|
0, 0, 0, light);
|
2017-12-10 23:49:15 +01:00
|
|
|
SDL_Rect box = (SDL_Rect) {
|
|
|
|
i*TILE_DIMENSION,
|
|
|
|
j*TILE_DIMENSION,
|
|
|
|
TILE_DIMENSION,
|
|
|
|
TILE_DIMENSION
|
|
|
|
};
|
2017-12-10 19:51:24 +01:00
|
|
|
SDL_RenderFillRect(cam->renderer, &box);
|
2017-12-05 15:03:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void roommatrix_reset(RoomMatrix *m)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < MAP_ROOM_WIDTH; ++i) {
|
|
|
|
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
|
|
|
|
m->spaces[i][j].occupied = false;
|
2017-12-10 19:51:24 +01:00
|
|
|
m->spaces[i][j].lightsource = false;
|
|
|
|
m->spaces[i][j].light = 0;
|
2017-12-05 15:03:20 +01:00
|
|
|
m->spaces[i][j].character = NULL;
|
|
|
|
m->spaces[i][j].player = NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 11:44:17 +01:00
|
|
|
m->roomPos = (Position) { 0, 0 };
|
2017-12-05 15:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void roommatrix_destroy(RoomMatrix *m)
|
|
|
|
{
|
|
|
|
free(m);
|
|
|
|
}
|