2018-02-16 18:11:26 +01:00
|
|
|
/*
|
|
|
|
* BreakHack - A dungeone crawler RPG
|
|
|
|
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-12-10 19:51:24 +01:00
|
|
|
#include <stdlib.h>
|
2017-12-19 19:42:05 +01:00
|
|
|
#include "defines.h"
|
2017-12-05 15:03:20 +01:00
|
|
|
#include "roommatrix.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "map.h"
|
2017-12-18 15:26:56 +01:00
|
|
|
#include "player.h"
|
2018-01-24 21:14:34 +01:00
|
|
|
#include "item.h"
|
2017-12-05 15:03:20 +01:00
|
|
|
|
|
|
|
RoomMatrix* roommatrix_create()
|
|
|
|
{
|
2018-01-31 16:55:48 +01:00
|
|
|
int i, j;
|
2017-12-05 15:03:20 +01:00
|
|
|
RoomMatrix *m = ec_malloc(sizeof(RoomMatrix));
|
2018-01-31 16:55:48 +01:00
|
|
|
for (i = 0; i < MAP_ROOM_WIDTH; ++i) {
|
|
|
|
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
|
|
|
|
m->spaces[i][j].items = NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-12-05 15:03:20 +01:00
|
|
|
roommatrix_reset(m);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2018-02-06 16:47:45 +01:00
|
|
|
void
|
|
|
|
roommatrix_handle_event(RoomMatrix *matrix, SDL_Event *event)
|
|
|
|
{
|
|
|
|
if (event->type != SDL_MOUSEMOTION)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (event->motion.x < GAME_VIEW_WIDTH
|
|
|
|
&& event->motion.y < GAME_VIEW_HEIGHT)
|
|
|
|
{
|
|
|
|
matrix->mousePos.x = event->motion.x;
|
|
|
|
matrix->mousePos.y = event->motion.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 15:03:20 +01:00
|
|
|
void roommatrix_populate_from_map(RoomMatrix *rm, Map *m)
|
|
|
|
{
|
2017-12-17 13:43:41 +01:00
|
|
|
int i, j;
|
2018-01-24 21:14:34 +01:00
|
|
|
Position position;
|
2017-12-05 15:03:20 +01:00
|
|
|
Room *r;
|
2017-12-13 23:20:54 +01:00
|
|
|
Monster *monster;
|
2017-12-17 13:43:41 +01:00
|
|
|
LinkedList *monsterItem;
|
2018-01-24 21:14:34 +01:00
|
|
|
Item *item;
|
|
|
|
LinkedList *items;
|
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-22 06:27:58 +01:00
|
|
|
RoomSpace *space = &rm->spaces[i][j];
|
2017-12-10 19:51:24 +01:00
|
|
|
if (r->tiles[i][j]) {
|
2017-12-22 06:27:58 +01:00
|
|
|
space->occupied =
|
2017-12-15 08:08:45 +01:00
|
|
|
r->tiles[i][j]->collider;
|
2017-12-22 06:27:58 +01:00
|
|
|
space->lightsource =
|
2017-12-15 08:08:45 +01:00
|
|
|
r->tiles[i][j]->lightsource;
|
2017-12-10 19:51:24 +01:00
|
|
|
}
|
|
|
|
if (r->decorations[i][j]) {
|
2017-12-22 06:27:58 +01:00
|
|
|
space->occupied |=
|
2017-12-15 08:08:45 +01:00
|
|
|
r->decorations[i][j]->collider;
|
2017-12-22 06:27:58 +01:00
|
|
|
space->lightsource |=
|
2017-12-15 08:08:45 +01:00
|
|
|
r->decorations[i][j]->lightsource;
|
2017-12-10 19:51:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-13 23:20:54 +01:00
|
|
|
|
2017-12-17 13:43:41 +01:00
|
|
|
monsterItem = m->monsters;
|
|
|
|
while (monsterItem) {
|
|
|
|
monster = monsterItem->data;
|
|
|
|
monsterItem = monsterItem->next;
|
|
|
|
|
2017-12-13 23:20:54 +01:00
|
|
|
if (!position_in_room(&monster->sprite->pos, &m->currentRoom))
|
|
|
|
continue;
|
2017-12-17 13:43:41 +01:00
|
|
|
|
2018-01-24 21:14:34 +01:00
|
|
|
position =
|
2017-12-15 08:08:45 +01:00
|
|
|
position_to_matrix_coords(&monster->sprite->pos);
|
|
|
|
|
2018-01-24 21:14:34 +01:00
|
|
|
rm->spaces[position.x][position.y]
|
2017-12-15 08:08:45 +01:00
|
|
|
.occupied = true;
|
2018-01-24 21:14:34 +01:00
|
|
|
rm->spaces[position.x][position.y]
|
2017-12-15 08:08:45 +01:00
|
|
|
.monster = monster;
|
2017-12-13 23:20:54 +01:00
|
|
|
}
|
2018-01-24 21:14:34 +01:00
|
|
|
|
|
|
|
items = m->items;
|
|
|
|
while (items) {
|
|
|
|
item = items->data;
|
|
|
|
items = items->next;
|
|
|
|
|
|
|
|
if (!position_in_room(&item->sprite->pos, &m->currentRoom))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
position = position_to_matrix_coords(&item->sprite->pos);
|
2018-01-29 13:48:44 +01:00
|
|
|
linkedlist_push(&rm->spaces[position.x][position.y].items, item);
|
2018-01-24 21:14:34 +01:00
|
|
|
}
|
2017-12-10 19:51:24 +01:00
|
|
|
}
|
|
|
|
|
2017-12-21 12:35:52 +01:00
|
|
|
// TODO(Linus): These should probably be macros
|
|
|
|
#undef min
|
2017-12-21 12:26:57 +01:00
|
|
|
#ifndef min
|
2017-12-10 19:51:24 +01:00
|
|
|
static int
|
|
|
|
min(int a, int b)
|
|
|
|
{
|
|
|
|
return a > b ? b : a;
|
|
|
|
}
|
2017-12-21 12:26:57 +01:00
|
|
|
#endif // min
|
2017-12-10 19:51:24 +01:00
|
|
|
|
2017-12-21 12:35:52 +01:00
|
|
|
#undef max
|
2017-12-21 12:26:57 +01:00
|
|
|
#ifndef max
|
2017-12-10 19:51:24 +01:00
|
|
|
static int
|
|
|
|
max(int a, int b)
|
|
|
|
{
|
|
|
|
return a > b ? a : b;
|
|
|
|
}
|
2017-12-21 12:26:57 +01:00
|
|
|
#endif // max
|
2017-12-10 19:51:24 +01:00
|
|
|
|
2017-12-18 15:26:56 +01:00
|
|
|
void
|
|
|
|
roommatrix_update_with_player(RoomMatrix *rm, Player *p)
|
|
|
|
{
|
|
|
|
Position rp = position_to_matrix_coords(&p->sprite->pos);
|
|
|
|
rm->spaces[rp.x][rp.y].occupied = true;
|
|
|
|
rm->spaces[rp.x][rp.y].player = p;
|
|
|
|
rm->playerRoomPos = rp;
|
|
|
|
}
|
|
|
|
|
2017-12-10 19:51:24 +01:00
|
|
|
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-15 08:08:45 +01:00
|
|
|
distance_modifier = abs(x-i) == abs(y-j) ?
|
2018-02-12 11:32:43 +01:00
|
|
|
min(abs(x-i) + 1, 5) : 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 16:47:45 +01:00
|
|
|
void
|
|
|
|
roommatrix_render_mouse_square(RoomMatrix *matrix, Camera *cam)
|
|
|
|
{
|
|
|
|
Position mmc = position_to_matrix_coords(&matrix->mousePos);
|
|
|
|
SDL_Rect box = (SDL_Rect) {
|
|
|
|
mmc.x*TILE_DIMENSION,
|
|
|
|
mmc.y*TILE_DIMENSION,
|
|
|
|
TILE_DIMENSION,
|
|
|
|
TILE_DIMENSION
|
|
|
|
};
|
|
|
|
|
|
|
|
SDL_SetRenderDrawColor(cam->renderer, 255, 255, 0, 90);
|
|
|
|
SDL_RenderFillRect(cam->renderer, &box);
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:32:43 +01:00
|
|
|
#ifdef LIGHTMAPDEBUG
|
|
|
|
static Texture *
|
|
|
|
create_light_texture(int light, Camera *cam)
|
|
|
|
{
|
|
|
|
static SDL_Color color = { 255, 255, 0, 0 };
|
|
|
|
|
|
|
|
char buffer[4];
|
|
|
|
Texture *t = texture_create();
|
|
|
|
texture_load_font(t, "assets/GUI/SDS_8x8.ttf", 8);
|
|
|
|
m_sprintf(buffer, 4, "%d", light);
|
|
|
|
texture_load_from_text(t, buffer, color, cam->renderer);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
#endif // LIGHTMAPDEBUG
|
|
|
|
|
2017-12-10 19:51:24 +01:00
|
|
|
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) {
|
2018-02-12 11:32:43 +01:00
|
|
|
light = max(245 - matrix->spaces[i][j].light, 0);
|
2018-02-06 16:47:45 +01:00
|
|
|
|
2017-12-10 23:49:15 +01:00
|
|
|
SDL_Rect box = (SDL_Rect) {
|
|
|
|
i*TILE_DIMENSION,
|
|
|
|
j*TILE_DIMENSION,
|
|
|
|
TILE_DIMENSION,
|
|
|
|
TILE_DIMENSION
|
|
|
|
};
|
2018-02-06 16:47:45 +01:00
|
|
|
|
|
|
|
SDL_SetRenderDrawColor(cam->renderer,
|
2018-02-22 09:44:27 +01:00
|
|
|
0, 0, 0, (Uint8) light);
|
2017-12-10 19:51:24 +01:00
|
|
|
SDL_RenderFillRect(cam->renderer, &box);
|
2018-02-12 11:32:43 +01:00
|
|
|
|
|
|
|
#ifdef LIGHTMAPDEBUG
|
|
|
|
Texture *t = create_light_texture(light, cam);
|
|
|
|
Position p = { box.x+3, box.y+3 };
|
|
|
|
texture_render(t, &p, cam);
|
|
|
|
texture_destroy(t);
|
|
|
|
#endif // LIGHTMAPDEBUG
|
2017-12-05 15:03:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void roommatrix_reset(RoomMatrix *m)
|
|
|
|
{
|
2018-01-31 16:55:48 +01:00
|
|
|
RoomSpace *space;
|
2017-12-05 15:03:20 +01:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < MAP_ROOM_WIDTH; ++i) {
|
|
|
|
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
|
2018-01-31 16:55:48 +01:00
|
|
|
space = &m->spaces[i][j];
|
|
|
|
space->occupied = false;
|
|
|
|
space->lightsource = false;
|
|
|
|
space->light = 0;
|
|
|
|
space->monster = NULL;
|
|
|
|
space->player = NULL;
|
|
|
|
while (space->items != NULL)
|
|
|
|
linkedlist_pop(&space->items);
|
2017-12-05 15:03:20 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-06 11:44:17 +01:00
|
|
|
m->roomPos = (Position) { 0, 0 };
|
2017-12-18 09:59:01 +01:00
|
|
|
m->playerRoomPos = (Position) { 1, 1 };
|
2017-12-05 15:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void roommatrix_destroy(RoomMatrix *m)
|
|
|
|
{
|
|
|
|
free(m);
|
|
|
|
}
|