2018-01-24 21:14:34 +01:00
|
|
|
#include <SDL2/SDL.h>
|
2018-01-24 21:20:24 +01:00
|
|
|
#include <stdlib.h>
|
2018-01-24 21:14:34 +01:00
|
|
|
|
|
|
|
#include "item_builder.h"
|
|
|
|
#include "texture.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "gui.h"
|
|
|
|
|
|
|
|
static ItemBuilder *builder = NULL;
|
|
|
|
|
|
|
|
void
|
|
|
|
item_builder_init(SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
builder = ec_malloc(sizeof(ItemBuilder));
|
|
|
|
builder->textures = ht_create(20);
|
|
|
|
builder->renderer = renderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-01-24 21:20:24 +01:00
|
|
|
check_builder(void)
|
2018-01-24 21:14:34 +01:00
|
|
|
{
|
|
|
|
if (!builder)
|
|
|
|
fatal("item_builder_init() not run");
|
|
|
|
}
|
|
|
|
|
|
|
|
static Texture *
|
|
|
|
load_texture(const char *path)
|
|
|
|
{
|
|
|
|
Texture *t = ht_get(builder->textures, path);
|
|
|
|
if (!t) {
|
|
|
|
t = texture_create();
|
|
|
|
texture_load_from_file(t, path, builder->renderer);
|
|
|
|
t->dim = (Dimension) { 32, 32 };
|
2018-01-31 16:55:48 +01:00
|
|
|
ht_set(builder->textures, path, t);
|
2018-01-24 21:14:34 +01:00
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-01-25 10:45:05 +01:00
|
|
|
eat_flesh(Item *item, Player *player)
|
2018-01-24 21:14:34 +01:00
|
|
|
{
|
2018-01-25 10:45:05 +01:00
|
|
|
int original_hp = player->stats.hp;
|
2018-01-30 15:16:14 +01:00
|
|
|
player->stats.hp += (int) item->value * player->stats.lvl;
|
2018-01-25 10:45:05 +01:00
|
|
|
if (player->stats.hp > player->stats.maxhp)
|
|
|
|
player->stats.hp = player->stats.maxhp;
|
2018-01-24 21:24:42 +01:00
|
|
|
|
2018-01-31 09:15:33 +01:00
|
|
|
gui_log("You eat some foul meat and gain %d health",
|
|
|
|
player->stats.hp - original_hp);
|
2018-01-25 10:45:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
drink_health(Item *item, Player *player)
|
|
|
|
{
|
2018-02-03 13:02:39 +01:00
|
|
|
player->potion_sips += item->value;
|
2018-01-24 21:14:34 +01:00
|
|
|
|
2018-02-03 13:02:39 +01:00
|
|
|
gui_log("You collect %u sips of health", (unsigned int) item->value);
|
2018-01-24 21:14:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Item *
|
2018-01-25 10:45:05 +01:00
|
|
|
create_item(const char *path, SDL_Rect clip, void (*cb)(Item*, Player*))
|
2018-01-24 21:14:34 +01:00
|
|
|
{
|
|
|
|
Texture *t;
|
|
|
|
Item *item;
|
|
|
|
|
|
|
|
item = item_create();
|
2018-01-25 10:45:05 +01:00
|
|
|
t = load_texture(path);
|
2018-01-24 21:14:34 +01:00
|
|
|
|
|
|
|
item->sprite = sprite_create();
|
|
|
|
sprite_set_texture(item->sprite, t, 0);
|
2018-01-25 10:45:05 +01:00
|
|
|
item->sprite->clip = clip;
|
|
|
|
item->effect = cb;
|
2018-01-24 21:14:34 +01:00
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2018-01-25 10:45:05 +01:00
|
|
|
static void
|
|
|
|
pickup_gold(Item *item, Player *player)
|
|
|
|
{
|
|
|
|
player->gold += item->value;
|
|
|
|
gui_log("You pick up %s", &item->label);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Item *
|
2018-01-31 09:15:33 +01:00
|
|
|
create_treasure(int current_level)
|
2018-01-25 10:45:05 +01:00
|
|
|
{
|
2018-02-03 13:02:39 +01:00
|
|
|
double amt;
|
2018-01-25 10:45:05 +01:00
|
|
|
char label[50];
|
2018-01-31 09:15:33 +01:00
|
|
|
unsigned int highest_treasure;
|
|
|
|
unsigned int value;
|
|
|
|
|
2018-02-03 13:02:39 +01:00
|
|
|
amt = (unsigned int) (rand() + (5*current_level)) % 40;
|
|
|
|
|
|
|
|
if (current_level > 9) {
|
2018-01-31 09:15:33 +01:00
|
|
|
highest_treasure = TREASURE_COUNT;
|
2018-02-03 13:02:39 +01:00
|
|
|
} else if (current_level > 3) {
|
2018-01-31 09:15:33 +01:00
|
|
|
highest_treasure = PLATINUM;
|
|
|
|
} else {
|
|
|
|
highest_treasure = GOLD;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = rand() % highest_treasure;
|
2018-01-25 10:45:05 +01:00
|
|
|
|
|
|
|
SDL_Rect clip = { 0, 0, 16, 16 };
|
|
|
|
switch (value) {
|
|
|
|
case COPPER:
|
2018-01-27 23:14:39 +01:00
|
|
|
m_sprintf(&label[0], 50, "%.0f copper", amt);
|
2018-01-25 10:45:05 +01:00
|
|
|
amt /= 100;
|
|
|
|
break;
|
|
|
|
case SILVER:
|
2018-01-27 23:14:39 +01:00
|
|
|
m_sprintf(&label[0], 50, "%.0f silver", amt);
|
2018-01-25 10:45:05 +01:00
|
|
|
clip.x = 48;
|
|
|
|
amt /= 10;
|
|
|
|
break;
|
|
|
|
case GOLD:
|
2018-01-27 23:14:39 +01:00
|
|
|
m_sprintf(&label[0], 50, "%.0f gold", amt);
|
2018-01-25 10:45:05 +01:00
|
|
|
clip.y = 16;
|
|
|
|
break;
|
|
|
|
case PLATINUM:
|
2018-01-27 23:14:39 +01:00
|
|
|
m_sprintf(&label[0], 50, "%.0f platinum", amt);
|
2018-01-25 10:45:05 +01:00
|
|
|
clip.x = 48;
|
|
|
|
clip.y = 16;
|
|
|
|
amt *= 10;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (amt > 15 && amt < 30)
|
|
|
|
clip.x += 16;
|
|
|
|
else if (amt <= 15)
|
|
|
|
clip.x += 32;
|
|
|
|
|
|
|
|
Item *item = create_item("assets/Items/Money.png", clip, &pickup_gold);
|
|
|
|
m_strcpy(item->label, 50, label);
|
|
|
|
item->value = amt;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2018-01-24 21:14:34 +01:00
|
|
|
Item *
|
2018-01-31 09:15:33 +01:00
|
|
|
item_builder_build_item(ItemKey key, int level)
|
2018-01-24 21:14:34 +01:00
|
|
|
{
|
2018-01-25 10:45:05 +01:00
|
|
|
static const char *path_flesh = "assets/Items/Flesh.png";
|
|
|
|
static const char *path_potion = "assets/Items/Potion.png";
|
|
|
|
|
2018-01-24 21:14:34 +01:00
|
|
|
check_builder();
|
|
|
|
|
|
|
|
Item *item = NULL;
|
|
|
|
switch (key) {
|
2018-01-25 10:45:05 +01:00
|
|
|
case TREASURE:
|
2018-01-31 09:15:33 +01:00
|
|
|
item = create_treasure(level);
|
2018-01-25 10:45:05 +01:00
|
|
|
break;
|
|
|
|
case FLESH:
|
2018-01-30 13:44:21 +01:00
|
|
|
item = create_item(path_flesh,
|
|
|
|
(SDL_Rect) { 0, 0, 16, 16 },
|
|
|
|
&eat_flesh);
|
2018-01-25 10:45:05 +01:00
|
|
|
item->value = 1;
|
|
|
|
break;
|
|
|
|
case HEALTH:
|
2018-01-30 13:44:21 +01:00
|
|
|
item = create_item(path_potion,
|
|
|
|
(SDL_Rect) { 0, 0, 16, 16 },
|
|
|
|
&drink_health);
|
2018-01-25 10:45:05 +01:00
|
|
|
item->value = 1 + (rand() % 2);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("in item_builder_build() : Unhandled item key %d", key);
|
|
|
|
break;
|
2018-01-24 21:14:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2018-01-30 13:44:21 +01:00
|
|
|
Item *
|
|
|
|
item_builder_build_sack(void)
|
|
|
|
{
|
|
|
|
return create_item("assets/Items/Chest0.png",
|
|
|
|
(SDL_Rect) { 0, 32, 16, 16 },
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2018-01-24 21:14:34 +01:00
|
|
|
void
|
2018-01-24 21:20:24 +01:00
|
|
|
item_builder_close(void)
|
2018-01-24 21:14:34 +01:00
|
|
|
{
|
2018-01-31 16:55:48 +01:00
|
|
|
ht_destroy_custom(builder->textures, (void (*)(void*)) texture_destroy);
|
2018-01-24 21:14:34 +01:00
|
|
|
free(builder);
|
2018-01-25 10:45:05 +01:00
|
|
|
}
|