diff --git a/src/item_builder.h b/src/item_builder.h index a04f82e..bb750e7 100644 --- a/src/item_builder.h +++ b/src/item_builder.h @@ -18,9 +18,9 @@ typedef enum { } Treasure; typedef enum { - TREASURE, FLESH, HEALTH, + TREASURE, ITEM_COUNT } ItemKey; diff --git a/src/map.c b/src/map.c index e568d46..a100cda 100644 --- a/src/map.c +++ b/src/map.c @@ -109,9 +109,7 @@ map_clear_dead_monsters(Map *map) Monster *monster = current->data; // Loot drops - Item *item = monster_drop_loot(monster); - if (item) - linkedlist_append(&map->items, item); + monster_drop_loot(monster, map); monster_destroy(monster); current->data = NULL; diff --git a/src/monster.c b/src/monster.c index 710e35d..9b8ce2a 100644 --- a/src/monster.c +++ b/src/monster.c @@ -9,6 +9,7 @@ #include "gui.h" #include "item.h" #include "item_builder.h" +#include "map.h" static void monster_load_texts(Monster *m, SDL_Renderer *renderer) @@ -238,21 +239,34 @@ monster_hit(Monster *monster, unsigned int dmg) monster->state.current = monster->state.challenge; } -Item * -monster_drop_loot(Monster *monster) +void +monster_drop_loot(Monster *monster, Map *map) { - static unsigned int drop_chance = 3; - unsigned int drop; + static unsigned int item_drop_chance = 3; + static unsigned int treasure_drop_chance = 2; + bool dropped_something = false; + unsigned int item_drop; + Item *item; - if ((rand() % drop_chance) != 0) - return NULL; + // TODO(Linus): + // Perhaps store multidrops in an item containing items? + // A pouch or bag perhaps? + if ((rand() % treasure_drop_chance) == 0) { + item = item_builder_build_item(TREASURE); + item->sprite->pos = monster->sprite->pos; + dropped_something = true; + linkedlist_append(&map->items, item); + } + if ((rand() % item_drop_chance) == 0) { + item_drop = rand() % TREASURE; + item = item_builder_build_item(item_drop); + item->sprite->pos = monster->sprite->pos; + dropped_something = true; + linkedlist_append(&map->items, item); + } - drop = rand() % ITEM_COUNT; - Item *item = item_builder_build_item(drop); - item->sprite->pos = monster->sprite->pos; - gui_log("%s dropped something", monster->label); - - return item; + if (dropped_something) + gui_log("%s dropped something", monster->label); } void diff --git a/src/monster.h b/src/monster.h index 54bdb72..1b403e3 100644 --- a/src/monster.h +++ b/src/monster.h @@ -34,7 +34,7 @@ void monster_render(Monster*, Camera*); void monster_hit(Monster*, unsigned int dmg); -Item *monster_drop_loot(Monster*); +void monster_drop_loot(Monster*, Map*); void monster_destroy(Monster*); diff --git a/src/player.c b/src/player.c index 0160e95..cdb1bc6 100644 --- a/src/player.c +++ b/src/player.c @@ -57,10 +57,16 @@ has_collided(Player *player, RoomMatrix *matrix) gui_log("Ouch! There is something in the way"); } - if (space->item != NULL) { - if (space->item->effect) - space->item->effect(space->item, player); - space->item->collected = true; + if (space->items != NULL) { + LinkedList *items = space->items; + Item *item; + while (items != NULL) { + item = items->data; + items = items->next; + if (item->effect) + item->effect(item, player); + item->collected = true; + } } return collided; diff --git a/src/roommatrix.c b/src/roommatrix.c index 686404e..6141567 100644 --- a/src/roommatrix.c +++ b/src/roommatrix.c @@ -72,7 +72,7 @@ void roommatrix_populate_from_map(RoomMatrix *rm, Map *m) continue; position = position_to_matrix_coords(&item->sprite->pos); - rm->spaces[position.x][position.y].item = item; + linkedlist_push(&rm->spaces[position.x][position.y].items, item); } } @@ -187,7 +187,7 @@ void roommatrix_reset(RoomMatrix *m) m->spaces[i][j].lightsource = false; m->spaces[i][j].light = 0; m->spaces[i][j].monster = NULL; - m->spaces[i][j].item = NULL; + m->spaces[i][j].items = NULL; m->spaces[i][j].player = NULL; } } diff --git a/src/roommatrix.h b/src/roommatrix.h index c1b06d6..b2b61b2 100644 --- a/src/roommatrix.h +++ b/src/roommatrix.h @@ -11,6 +11,7 @@ typedef struct Map_t Map; typedef struct Monster_t Monster; typedef struct Player_t Player; typedef struct Item_t Item; +typedef struct Node LinkedList; typedef struct { bool occupied; @@ -18,7 +19,7 @@ typedef struct { unsigned int light; Monster *monster; Player *player; - Item *item; + LinkedList *items; } RoomSpace; typedef struct {