2017-12-21 11:57:12 +01:00
|
|
|
#include <stdlib.h>
|
2017-12-01 16:03:19 +01:00
|
|
|
#include "map.h"
|
2017-12-02 23:32:40 +01:00
|
|
|
#include "map_lua.h"
|
2017-12-01 16:03:19 +01:00
|
|
|
#include "util.h"
|
2018-01-24 21:14:34 +01:00
|
|
|
#include "item.h"
|
|
|
|
#include "item_builder.h"
|
|
|
|
#include "gui.h"
|
2017-12-01 16:03:19 +01:00
|
|
|
|
|
|
|
static
|
2017-12-19 21:00:02 +01:00
|
|
|
Room* create_room(void)
|
2017-12-01 16:03:19 +01:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
Room *room;
|
|
|
|
|
|
|
|
room = ec_malloc(sizeof(Room));
|
2017-12-05 08:13:28 +01:00
|
|
|
for (i=0; i < MAP_ROOM_WIDTH; ++i) {
|
|
|
|
for (j=0; j < MAP_ROOM_HEIGHT; ++j) {
|
2017-12-01 16:03:19 +01:00
|
|
|
room->tiles[i][j] = NULL;
|
2017-12-08 09:45:57 +01:00
|
|
|
room->decorations[i][j] = NULL;
|
2017-12-01 16:03:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
2017-12-02 23:32:40 +01:00
|
|
|
Map* map_create()
|
2017-12-01 16:03:19 +01:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
Map *map = ec_malloc(sizeof(Map));
|
2017-12-02 16:24:31 +01:00
|
|
|
map->textures = linkedlist_create();
|
2017-12-22 15:15:40 +01:00
|
|
|
map->monsterTextures = ht_create(30);
|
2017-12-13 14:26:30 +01:00
|
|
|
map->monsters = linkedlist_create();
|
2018-01-24 21:14:34 +01:00
|
|
|
map->items = linkedlist_create();
|
2017-12-01 16:03:19 +01:00
|
|
|
map->currentRoom = (Position) { 0, 0 };
|
2017-12-08 14:40:33 +01:00
|
|
|
map->renderTimer = timer_create();
|
2017-12-01 16:03:19 +01:00
|
|
|
map->level = 1;
|
|
|
|
|
2017-12-05 08:13:28 +01:00
|
|
|
for (i=0; i < MAP_H_ROOM_COUNT; ++i) {
|
|
|
|
for (j=0; j < MAP_V_ROOM_COUNT; ++j) {
|
2017-12-01 16:03:19 +01:00
|
|
|
map->rooms[i][j] = create_room();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2017-12-05 08:13:28 +01:00
|
|
|
void map_add_tile(Map *map, Position *tile_pos, MapTile *tile)
|
2017-12-02 23:32:40 +01:00
|
|
|
{
|
2017-12-05 08:13:28 +01:00
|
|
|
const Position *cr = &map->currentRoom;
|
2017-12-22 06:27:58 +01:00
|
|
|
Room *room = map->rooms[cr->x][cr->y];
|
|
|
|
MapTile **oldTile = &room->tiles[tile_pos->x][tile_pos->y];
|
|
|
|
|
|
|
|
// Clear possible decoration
|
|
|
|
if (tile->levelExit && room->tiles[tile_pos->x][tile_pos->y]) {
|
|
|
|
MapTile **decoration = &room->tiles[tile_pos->x][tile_pos->y];
|
|
|
|
free(*decoration);
|
|
|
|
*decoration = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear possible tile
|
2017-12-02 23:32:40 +01:00
|
|
|
if (*oldTile != NULL) {
|
|
|
|
free(*oldTile);
|
|
|
|
*oldTile = NULL;
|
|
|
|
}
|
|
|
|
*oldTile = tile;
|
|
|
|
}
|
|
|
|
|
2017-12-08 09:45:57 +01:00
|
|
|
void map_add_decoration(Map *map, Position *tile_pos, MapTile *tile)
|
|
|
|
{
|
|
|
|
const Position *cr = &map->currentRoom;
|
|
|
|
MapTile **oldTile = &map->rooms[cr->x][cr->y]->decorations[tile_pos->x][tile_pos->y];
|
|
|
|
if (*oldTile != NULL) {
|
|
|
|
free(*oldTile);
|
|
|
|
*oldTile = NULL;
|
|
|
|
}
|
|
|
|
*oldTile = tile;
|
|
|
|
}
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
Texture*
|
2017-12-13 23:20:54 +01:00
|
|
|
map_add_monster_texture(Map *map, const char *path, SDL_Renderer *renderer)
|
2017-12-13 20:31:04 +01:00
|
|
|
{
|
|
|
|
Texture *t;
|
|
|
|
|
|
|
|
t = ht_get(map->monsterTextures, path);
|
|
|
|
if (!t) {
|
2017-12-15 15:03:29 +01:00
|
|
|
t = texture_create();
|
|
|
|
texture_load_from_file(t, path, renderer);
|
2017-12-13 20:31:04 +01:00
|
|
|
ht_set(map->monsterTextures, path, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2017-12-15 08:08:45 +01:00
|
|
|
void
|
|
|
|
map_clear_dead_monsters(Map *map)
|
|
|
|
{
|
|
|
|
LinkedList *last, *current, *next;
|
|
|
|
|
|
|
|
last = NULL;
|
|
|
|
current = map->monsters;
|
|
|
|
|
|
|
|
while (current != NULL) {
|
|
|
|
if (((Monster*) current->data)->stats.hp <= 0) {
|
|
|
|
if (last == NULL)
|
|
|
|
map->monsters = current->next;
|
|
|
|
else
|
|
|
|
last->next = current->next;
|
|
|
|
|
2018-01-24 21:14:34 +01:00
|
|
|
Monster *monster = current->data;
|
2018-01-25 10:45:05 +01:00
|
|
|
|
|
|
|
// Loot drops
|
|
|
|
Item *item = monster_drop_loot(monster);
|
|
|
|
if (item)
|
|
|
|
linkedlist_append(&map->items, item);
|
2018-01-24 21:14:34 +01:00
|
|
|
|
|
|
|
monster_destroy(monster);
|
|
|
|
current->data = NULL;
|
|
|
|
next = current->next;
|
|
|
|
current->next = NULL;
|
|
|
|
linkedlist_destroy(¤t);
|
|
|
|
current = next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
last = current;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
map_clear_collected_items(Map *map)
|
|
|
|
{
|
|
|
|
LinkedList *last, *current, *next;
|
|
|
|
|
|
|
|
last = NULL;
|
|
|
|
current = map->items;
|
|
|
|
|
|
|
|
while (current != NULL) {
|
|
|
|
if (((Item*) current->data)->collected) {
|
|
|
|
if (last == NULL)
|
|
|
|
map->items = current->next;
|
|
|
|
else
|
|
|
|
last->next = current->next;
|
|
|
|
|
|
|
|
item_destroy(current->data);
|
2017-12-15 08:08:45 +01:00
|
|
|
current->data = NULL;
|
|
|
|
next = current->next;
|
|
|
|
current->next = NULL;
|
|
|
|
linkedlist_destroy(¤t);
|
|
|
|
current = next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
last = current;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
void
|
|
|
|
map_add_monster(Map *map, Monster *m)
|
|
|
|
{
|
|
|
|
linkedlist_append(&map->monsters, m);
|
|
|
|
}
|
|
|
|
|
2017-12-17 13:43:41 +01:00
|
|
|
void
|
|
|
|
map_move_monsters(Map *map, RoomMatrix *rm)
|
|
|
|
{
|
|
|
|
LinkedList *m = map->monsters;
|
|
|
|
while (m) {
|
|
|
|
Monster *monster = m->data;
|
|
|
|
m = m->next;
|
|
|
|
if (!position_in_room(&monster->sprite->pos, &map->currentRoom))
|
|
|
|
continue;
|
|
|
|
monster_move(monster, rm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 23:32:40 +01:00
|
|
|
int map_add_texture(Map *map, const char *path, SDL_Renderer *renderer)
|
|
|
|
{
|
2017-12-15 15:03:29 +01:00
|
|
|
Texture *t = texture_create();
|
|
|
|
texture_load_from_file(t, path, renderer);
|
2017-12-13 20:31:04 +01:00
|
|
|
linkedlist_append(&map->textures, t);
|
2017-12-02 23:32:40 +01:00
|
|
|
return linkedlist_size(map->textures) - 1;
|
|
|
|
}
|
|
|
|
|
2017-12-02 16:24:31 +01:00
|
|
|
static
|
|
|
|
void map_tile_render(Map *map, MapTile *tile, Position *pos, Camera *cam)
|
|
|
|
{
|
2017-12-08 14:40:33 +01:00
|
|
|
static bool second_texture = false;
|
|
|
|
|
2017-12-02 16:24:31 +01:00
|
|
|
if (tile == NULL)
|
|
|
|
return;
|
2017-12-02 23:32:40 +01:00
|
|
|
|
2017-12-08 14:40:33 +01:00
|
|
|
if (timer_get_ticks(map->renderTimer) > 300) {
|
|
|
|
timer_start(map->renderTimer);
|
|
|
|
second_texture = !second_texture;
|
|
|
|
}
|
|
|
|
|
2017-12-02 23:32:40 +01:00
|
|
|
Position camPos = camera_to_camera_position(cam, pos);
|
|
|
|
SDL_Rect draw_box = (SDL_Rect) {
|
|
|
|
camPos.x,
|
|
|
|
camPos.y,
|
2017-12-03 11:09:57 +01:00
|
|
|
TILE_DIMENSION,
|
|
|
|
TILE_DIMENSION
|
2017-12-02 23:32:40 +01:00
|
|
|
};
|
|
|
|
|
2017-12-08 14:40:33 +01:00
|
|
|
Texture *texture;
|
|
|
|
if (tile->textureIndex1 >= 0 && second_texture) {
|
|
|
|
texture = linkedlist_get(&map->textures, tile->textureIndex1);
|
|
|
|
} else {
|
|
|
|
texture = linkedlist_get(&map->textures, tile->textureIndex0);
|
|
|
|
}
|
2017-12-02 23:32:40 +01:00
|
|
|
|
|
|
|
SDL_RenderCopy(cam->renderer,
|
|
|
|
texture->texture,
|
|
|
|
&tile->clip,
|
|
|
|
&draw_box
|
|
|
|
);
|
|
|
|
|
2017-12-02 16:24:31 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 16:03:19 +01:00
|
|
|
void map_render(Map *map, Camera *cam)
|
|
|
|
{
|
2017-12-17 13:43:41 +01:00
|
|
|
unsigned int i, j;
|
|
|
|
LinkedList *monsterItem;
|
2018-01-24 21:14:34 +01:00
|
|
|
LinkedList *items;
|
2017-12-01 16:03:19 +01:00
|
|
|
Room *room;
|
2017-12-08 14:40:33 +01:00
|
|
|
|
|
|
|
if (!timer_started(map->renderTimer)) {
|
|
|
|
timer_start(map->renderTimer);
|
|
|
|
}
|
|
|
|
|
2017-12-01 16:03:19 +01:00
|
|
|
Position roomPos = { map->currentRoom.x, map->currentRoom.y };
|
|
|
|
Position roomCords = {
|
2017-12-03 11:09:57 +01:00
|
|
|
roomPos.x * MAP_ROOM_WIDTH * TILE_DIMENSION,
|
|
|
|
roomPos.y * MAP_ROOM_HEIGHT * TILE_DIMENSION
|
2017-12-01 16:03:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
room = map->rooms[roomPos.x][roomPos.y];
|
2017-12-05 08:13:28 +01:00
|
|
|
for (i=0; i < MAP_ROOM_WIDTH; ++i) {
|
|
|
|
for (j=0; j < MAP_ROOM_HEIGHT; ++j) {
|
2017-12-02 23:32:40 +01:00
|
|
|
Position tilePos = (Position) {
|
2017-12-05 08:13:28 +01:00
|
|
|
roomCords.x + i*TILE_DIMENSION,
|
|
|
|
roomCords.y + j*TILE_DIMENSION
|
2017-12-02 23:32:40 +01:00
|
|
|
};
|
|
|
|
map_tile_render(map, room->tiles[i][j], &tilePos, cam);
|
2017-12-13 20:31:04 +01:00
|
|
|
map_tile_render(map,
|
|
|
|
room->decorations[i][j],
|
|
|
|
&tilePos,
|
|
|
|
cam);
|
2017-12-01 16:03:19 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-13 20:31:04 +01:00
|
|
|
|
2017-12-17 13:43:41 +01:00
|
|
|
monsterItem = map->monsters;
|
2017-12-19 22:51:00 +01:00
|
|
|
while (monsterItem != NULL) {
|
2017-12-17 13:43:41 +01:00
|
|
|
Monster *monster = monsterItem->data;
|
|
|
|
monsterItem = monsterItem->next;
|
2017-12-13 20:31:04 +01:00
|
|
|
monster_render(monster, cam);
|
|
|
|
}
|
2018-01-24 21:14:34 +01:00
|
|
|
|
|
|
|
items = map->items;
|
|
|
|
while (items != NULL) {
|
|
|
|
Item *item = items->data;
|
|
|
|
items = items->next;
|
|
|
|
item_render(item, cam);
|
|
|
|
}
|
2017-12-01 16:03:19 +01:00
|
|
|
}
|
|
|
|
|
2017-12-03 11:09:57 +01:00
|
|
|
void map_set_current_room(Map *map, Position *pos)
|
|
|
|
{
|
|
|
|
unsigned int room_width, room_height;
|
|
|
|
|
|
|
|
room_width = MAP_ROOM_WIDTH * TILE_DIMENSION;
|
|
|
|
room_height = MAP_ROOM_HEIGHT * TILE_DIMENSION;
|
|
|
|
|
|
|
|
if (pos->x <= 0) {
|
|
|
|
map->currentRoom.x = 0;
|
|
|
|
} else {
|
|
|
|
unsigned int room_cord_x = pos->x - (pos->x % room_width);
|
|
|
|
map->currentRoom.x = room_cord_x / room_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos->y <= 0) {
|
|
|
|
map->currentRoom.y = 0;
|
|
|
|
} else {
|
|
|
|
unsigned int room_cord_y = pos->y - (pos->y % room_height);
|
|
|
|
map->currentRoom.y = room_cord_y / room_height;
|
|
|
|
}
|
2017-12-05 08:13:28 +01:00
|
|
|
|
|
|
|
if (map->currentRoom.x >= MAP_H_ROOM_COUNT)
|
|
|
|
map->currentRoom.x = MAP_H_ROOM_COUNT - 1;
|
|
|
|
if (map->currentRoom.y >= MAP_V_ROOM_COUNT)
|
|
|
|
map->currentRoom.y = MAP_V_ROOM_COUNT - 1;
|
2017-12-03 11:09:57 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 16:03:19 +01:00
|
|
|
static
|
|
|
|
void map_room_destroy(Room *room)
|
|
|
|
{
|
|
|
|
int i, j;
|
2017-12-05 08:13:28 +01:00
|
|
|
for (i=0; i < MAP_ROOM_WIDTH; ++i) {
|
|
|
|
for (j=0; j < MAP_ROOM_HEIGHT; ++j) {
|
2017-12-01 16:03:19 +01:00
|
|
|
if (room->tiles[i][j]) {
|
2017-12-02 16:24:31 +01:00
|
|
|
free(room->tiles[i][j]);
|
2017-12-01 16:03:19 +01:00
|
|
|
}
|
2017-12-08 09:45:57 +01:00
|
|
|
if (room->decorations[i][j]) {
|
|
|
|
free(room->decorations[i][j]);
|
|
|
|
}
|
2017-12-01 16:03:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(room);
|
|
|
|
}
|
|
|
|
|
|
|
|
void map_destroy(Map *map)
|
|
|
|
{
|
|
|
|
int i, j;
|
2017-12-05 08:13:28 +01:00
|
|
|
for (i=0; i < MAP_H_ROOM_COUNT; ++i) {
|
|
|
|
for (j=0; j < MAP_V_ROOM_COUNT; ++j) {
|
2017-12-01 16:03:19 +01:00
|
|
|
map_room_destroy(map->rooms[i][j]);
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 21:14:34 +01:00
|
|
|
|
|
|
|
while (map->textures != NULL)
|
2017-12-13 14:26:30 +01:00
|
|
|
texture_destroy(linkedlist_pop(&map->textures));
|
2018-01-24 21:14:34 +01:00
|
|
|
|
|
|
|
while (map->monsters != NULL)
|
2017-12-13 23:20:54 +01:00
|
|
|
monster_destroy(linkedlist_pop(&map->monsters));
|
2018-01-24 21:14:34 +01:00
|
|
|
|
|
|
|
while (map->items != NULL)
|
|
|
|
item_destroy(linkedlist_pop(&map->items));
|
|
|
|
|
2017-12-13 14:26:30 +01:00
|
|
|
ht_destroy_custom(map->monsterTextures, (void (*)(void*)) texture_destroy);
|
2017-12-08 14:40:33 +01:00
|
|
|
timer_destroy(map->renderTimer);
|
2017-12-01 16:03:19 +01:00
|
|
|
free(map);
|
|
|
|
}
|