forked from NotAShelf/rogged
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Ie3b66d17f6f660c9b9a719210bd86f9f6a6a6964
126 lines
2.4 KiB
C
126 lines
2.4 KiB
C
#include "common.h"
|
|
#include "map.h"
|
|
#include "rng.h"
|
|
#include <stddef.h>
|
|
|
|
void item_spawn(Item items[], int *count, Map *map, int floor) {
|
|
*count = 0;
|
|
|
|
// Number of items scales with floor
|
|
int num_items = 2 + rng_int(0, 3);
|
|
if (num_items > MAX_ITEMS)
|
|
num_items = MAX_ITEMS;
|
|
|
|
for (int i = 0; i < num_items; i++) {
|
|
// Find random floor position
|
|
int ix, iy;
|
|
get_random_floor_tile(map, &ix, &iy, 50);
|
|
|
|
// Don't spawn on other items
|
|
int occupied = 0;
|
|
for (int j = 0; j < *count; j++) {
|
|
if (items[j].x == ix && items[j].y == iy) {
|
|
occupied = 1;
|
|
break;
|
|
}
|
|
}
|
|
if (occupied)
|
|
continue;
|
|
|
|
// Create item
|
|
Item item;
|
|
item.x = ix;
|
|
item.y = iy;
|
|
item.floor = floor;
|
|
item.picked_up = 0;
|
|
|
|
// Item type distribution
|
|
int type_roll = rng_int(0, 99);
|
|
|
|
if (type_roll < 50) {
|
|
// 50% chance for potion
|
|
item.type = ITEM_POTION;
|
|
item.power = 5 + rng_int(0, floor * 2); // healing: 5 + 0-2*floor
|
|
} else if (type_roll < 80) {
|
|
// 30% chance for weapon
|
|
item.type = ITEM_WEAPON;
|
|
item.power = 1 + rng_int(0, floor); // attack bonus: 1 + 0-floor
|
|
} else {
|
|
// 20% chance for armor
|
|
item.type = ITEM_ARMOR;
|
|
item.power = 1 + rng_int(0, floor / 2); // defense bonus
|
|
}
|
|
|
|
items[i] = item;
|
|
(*count)++;
|
|
}
|
|
}
|
|
|
|
// Get item name for display
|
|
const char *item_get_name(Item *i) {
|
|
if (i == NULL)
|
|
return "";
|
|
|
|
switch (i->type) {
|
|
case ITEM_POTION:
|
|
return "Potion";
|
|
case ITEM_WEAPON:
|
|
return "Weapon";
|
|
case ITEM_ARMOR:
|
|
return "Armor";
|
|
default:
|
|
return "Unknown";
|
|
}
|
|
}
|
|
|
|
// Get item description
|
|
const char *item_get_description(Item *i) {
|
|
if (i == NULL)
|
|
return "";
|
|
|
|
switch (i->type) {
|
|
case ITEM_POTION:
|
|
return "Heals HP";
|
|
case ITEM_WEAPON:
|
|
return "+Attack";
|
|
case ITEM_ARMOR:
|
|
return "+Defense";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
// Get item power value
|
|
int item_get_power(Item *i) {
|
|
if (i == NULL)
|
|
return 0;
|
|
return i->power;
|
|
}
|
|
|
|
void item_use(Player *p, Item *i) {
|
|
if (p == NULL || i == NULL)
|
|
return;
|
|
|
|
switch (i->type) {
|
|
case ITEM_POTION:
|
|
// Heal player
|
|
p->hp += i->power;
|
|
if (p->hp > p->max_hp) {
|
|
p->hp = p->max_hp;
|
|
}
|
|
break;
|
|
|
|
case ITEM_WEAPON:
|
|
// Increase attack
|
|
p->attack += i->power;
|
|
break;
|
|
|
|
case ITEM_ARMOR:
|
|
// Increase defense
|
|
p->defense += i->power;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|