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/>.
|
|
|
|
*/
|
|
|
|
|
2018-02-21 00:29:21 +01:00
|
|
|
#include <SDL.h>
|
2018-01-24 21:20:24 +01:00
|
|
|
#include <stdlib.h>
|
2019-03-18 11:39:26 +01:00
|
|
|
#include <assert.h>
|
2018-01-24 21:14:34 +01:00
|
|
|
|
|
|
|
#include "item_builder.h"
|
|
|
|
#include "texture.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "gui.h"
|
2018-02-15 14:00:59 +01:00
|
|
|
#include "mixer.h"
|
2018-02-15 14:45:20 +01:00
|
|
|
#include "random.h"
|
2018-08-10 14:29:45 +02:00
|
|
|
#include "texturecache.h"
|
|
|
|
#include "sprite.h"
|
2019-03-04 20:32:09 +01:00
|
|
|
#include "sprite_util.h"
|
2019-03-18 11:39:26 +01:00
|
|
|
#include "map.h"
|
2018-01-24 21:14:34 +01:00
|
|
|
|
|
|
|
static ItemBuilder *builder = NULL;
|
|
|
|
|
|
|
|
void
|
|
|
|
item_builder_init(SDL_Renderer *renderer)
|
|
|
|
{
|
|
|
|
builder = ec_malloc(sizeof(ItemBuilder));
|
|
|
|
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 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-08-11 15:15:53 +02:00
|
|
|
player->stats.hp += (int) item->value;
|
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-02-15 14:00:59 +01:00
|
|
|
mixer_play_effect(EAT);
|
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 16:41:55 +01:00
|
|
|
player->potion_sips += (int) item->value;
|
2018-01-24 21:14:34 +01:00
|
|
|
|
2018-02-15 14:00:59 +01:00
|
|
|
mixer_play_effect(BOTTLE);
|
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
|
|
|
}
|
|
|
|
|
2018-03-13 16:13:54 +01:00
|
|
|
static void
|
|
|
|
pickup_dagger(Item *item, Player *player)
|
|
|
|
{
|
|
|
|
player->daggers += (Uint32) item->value;
|
|
|
|
|
2018-03-15 16:30:41 +01:00
|
|
|
mixer_play_effect(DAGGER_PICKUP);
|
2018-09-13 10:48:35 +02:00
|
|
|
double count = player->class == ROGUE ? item->value * 2 : item->value;
|
|
|
|
if (count > 1)
|
|
|
|
gui_log("You collect %u daggers", (Uint32) count);
|
2018-03-13 16:13:54 +01:00
|
|
|
else
|
|
|
|
gui_log("You collect a dagger");
|
|
|
|
}
|
|
|
|
|
2019-03-04 16:06:13 +01:00
|
|
|
static Item *
|
|
|
|
create_item(const char *path0, const char *path1, SDL_Rect clip, void (*cb)(Item*, Player*))
|
|
|
|
{
|
|
|
|
Item *item;
|
|
|
|
|
|
|
|
item = item_create();
|
|
|
|
Texture *t0 = texturecache_add(path0);
|
|
|
|
Texture *t1 = NULL;
|
|
|
|
if (path1)
|
|
|
|
t1 = texturecache_add(path1);
|
|
|
|
|
|
|
|
item->sprite = sprite_create();
|
|
|
|
sprite_set_texture(item->sprite, t0, 0);
|
|
|
|
sprite_set_texture(item->sprite, t1, 1);
|
|
|
|
item->sprite->dim = GAME_DIMENSION;
|
|
|
|
item->sprite->clip = clip;
|
|
|
|
item->effect = cb;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2018-01-25 10:45:05 +01:00
|
|
|
static void
|
|
|
|
pickup_gold(Item *item, Player *player)
|
|
|
|
{
|
|
|
|
player->gold += item->value;
|
2018-02-15 14:00:59 +01:00
|
|
|
mixer_play_effect(COIN);
|
2018-01-25 10:45:05 +01:00
|
|
|
gui_log("You pick up %s", &item->label);
|
|
|
|
}
|
|
|
|
|
2019-03-08 10:16:15 +01:00
|
|
|
Item *
|
|
|
|
item_builder_build_treasure(Treasure type, double goldAmt)
|
2018-01-25 10:45:05 +01:00
|
|
|
{
|
2019-03-08 10:16:15 +01:00
|
|
|
double amt = goldAmt;
|
2018-02-28 22:31:38 +01:00
|
|
|
char label[50] = "";
|
2018-02-24 21:47:09 +01:00
|
|
|
SDL_Rect clip = CLIP16(0, 0);
|
2019-03-08 10:16:15 +01:00
|
|
|
switch (type) {
|
2018-01-25 10:45:05 +01:00
|
|
|
case COPPER:
|
2018-02-28 22:31:38 +01:00
|
|
|
m_sprintf(label, 50, "%.0f copper", amt);
|
2018-01-25 10:45:05 +01:00
|
|
|
amt /= 100;
|
|
|
|
break;
|
|
|
|
case SILVER:
|
2018-02-28 22:31:38 +01:00
|
|
|
m_sprintf(label, 50, "%.0f silver", amt);
|
2018-01-25 10:45:05 +01:00
|
|
|
clip.x = 48;
|
|
|
|
amt /= 10;
|
|
|
|
break;
|
|
|
|
case GOLD:
|
2018-02-28 22:31:38 +01:00
|
|
|
m_sprintf(label, 50, "%.0f gold", amt);
|
2018-01-25 10:45:05 +01:00
|
|
|
clip.y = 16;
|
|
|
|
break;
|
|
|
|
case PLATINUM:
|
2018-02-28 22:31:38 +01:00
|
|
|
m_sprintf(label, 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;
|
|
|
|
|
2018-08-10 14:29:45 +02:00
|
|
|
Item *item = create_item("Items/Money.png", NULL, clip, &pickup_gold);
|
2018-01-25 10:45:05 +01:00
|
|
|
m_strcpy(item->label, 50, label);
|
|
|
|
item->value = amt;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2019-03-18 11:39:26 +01:00
|
|
|
static void
|
|
|
|
pickup_silver_key(Item *item, Player *player)
|
|
|
|
{
|
2019-03-20 09:53:35 +01:00
|
|
|
gui_log("You pickup %s", item->label);
|
2019-03-24 12:20:45 +01:00
|
|
|
mixer_play_effect(KEY_PICKUP);
|
2019-03-20 09:53:35 +01:00
|
|
|
player->equipment.keys |= LOCK_SILVER;
|
2019-03-18 11:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pickup_gold_key(Item *item, Player *player)
|
|
|
|
{
|
2019-03-20 09:53:35 +01:00
|
|
|
gui_log("You pickup %s", item->label);
|
2019-03-24 12:20:45 +01:00
|
|
|
mixer_play_effect(KEY_PICKUP);
|
2019-03-20 09:53:35 +01:00
|
|
|
player->equipment.keys |= LOCK_GOLD;
|
2019-03-18 11:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Item *
|
|
|
|
item_builder_build_key(unsigned int type)
|
|
|
|
{
|
|
|
|
char label[20];
|
|
|
|
SDL_Rect clip = CLIP16(0, 0);
|
|
|
|
Item *item;
|
|
|
|
switch (type) {
|
|
|
|
case 1:
|
|
|
|
m_sprintf(label, 20, "a silver key");
|
|
|
|
item = create_item("Items/Key.png", NULL, clip, &pickup_silver_key);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
m_sprintf(label, 20, "a gold key");
|
|
|
|
item = create_item("Items/Key.png", NULL, clip, &pickup_gold_key);
|
|
|
|
clip.x = 16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("Bad keytype provided");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_strcpy(item->label, 20, label);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2019-03-08 10:16:15 +01:00
|
|
|
static Item *
|
|
|
|
create_treasure(int current_level)
|
|
|
|
{
|
|
|
|
double amt;
|
|
|
|
unsigned int highest_treasure;
|
|
|
|
unsigned int value;
|
|
|
|
|
|
|
|
amt = (unsigned int) 1 + get_random(5*current_level) % 40;
|
|
|
|
amt = amt == 0 ? 1 : amt;
|
|
|
|
|
|
|
|
if (current_level > 9) {
|
|
|
|
highest_treasure = PLATINUM;
|
|
|
|
} else if (current_level > 3) {
|
|
|
|
highest_treasure = GOLD;
|
|
|
|
} else {
|
|
|
|
highest_treasure = SILVER;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = get_random(highest_treasure);
|
|
|
|
return item_builder_build_treasure((Treasure) value, amt);
|
|
|
|
}
|
|
|
|
|
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-03-13 16:13:54 +01:00
|
|
|
static const char *path_flesh = "Items/Flesh.png";
|
|
|
|
static const char *path_potion = "Items/Potion.png";
|
|
|
|
static const char *path_short_wep = "Items/ShortWep.png";
|
2018-01-25 10:45:05 +01:00
|
|
|
|
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-02-24 21:15:13 +01:00
|
|
|
item = create_treasure(level*2);
|
2018-01-25 10:45:05 +01:00
|
|
|
break;
|
|
|
|
case FLESH:
|
2018-01-30 13:44:21 +01:00
|
|
|
item = create_item(path_flesh,
|
2018-08-10 14:29:45 +02:00
|
|
|
NULL,
|
2018-02-24 21:47:09 +01:00
|
|
|
CLIP16(get_random(7) * 16, get_random(1) * 16),
|
2018-01-30 13:44:21 +01:00
|
|
|
&eat_flesh);
|
2018-03-01 19:37:57 +01:00
|
|
|
item->value = 1 + get_random(level);
|
2018-01-25 10:45:05 +01:00
|
|
|
break;
|
|
|
|
case HEALTH:
|
2018-01-30 13:44:21 +01:00
|
|
|
item = create_item(path_potion,
|
2018-08-10 14:29:45 +02:00
|
|
|
NULL,
|
2018-02-24 21:47:09 +01:00
|
|
|
CLIP16(0, 0),
|
2018-01-30 13:44:21 +01:00
|
|
|
&drink_health);
|
2018-03-01 19:37:57 +01:00
|
|
|
item->value = 1 + get_random(level);
|
2018-01-25 10:45:05 +01:00
|
|
|
break;
|
2018-03-13 16:13:54 +01:00
|
|
|
case DAGGER:
|
|
|
|
item = create_item(path_short_wep,
|
2018-08-10 14:29:45 +02:00
|
|
|
NULL,
|
2018-03-13 16:13:54 +01:00
|
|
|
CLIP16(0, 0),
|
|
|
|
&pickup_dagger);
|
|
|
|
item->value = 1;
|
|
|
|
break;
|
2018-01-25 10:45:05 +01:00
|
|
|
default:
|
|
|
|
fatal("in item_builder_build() : Unhandled item key %d", key);
|
|
|
|
break;
|
2018-01-24 21:14:34 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 16:06:13 +01:00
|
|
|
if (item->value != 1) {
|
2019-03-04 20:32:09 +01:00
|
|
|
Sprite *valueSprite = sprite_util_create_text_sprite("GUI/SDS_8x8.ttf",
|
|
|
|
8,
|
|
|
|
C_BLUE,
|
|
|
|
C_BLACK,
|
|
|
|
"%g",
|
|
|
|
item->value);
|
2019-03-04 16:06:13 +01:00
|
|
|
valueSprite->offset.x = item->sprite->dim.width - valueSprite->dim.width;
|
|
|
|
valueSprite->offset.y = item->sprite->dim.height - valueSprite->dim.height;
|
|
|
|
linkedlist_append(&item->subsprites, valueSprite);
|
|
|
|
}
|
|
|
|
|
2018-01-24 21:14:34 +01:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2018-01-30 13:44:21 +01:00
|
|
|
Item *
|
|
|
|
item_builder_build_sack(void)
|
|
|
|
{
|
2018-02-22 12:36:24 +01:00
|
|
|
return create_item("Items/Chest0.png",
|
2018-08-10 14:29:45 +02:00
|
|
|
NULL,
|
2018-02-24 21:47:09 +01:00
|
|
|
CLIP16(0, 32),
|
2018-01-30 13:44:21 +01:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2018-08-10 14:29:45 +02:00
|
|
|
Item *
|
|
|
|
item_builder_build_container(const char *path0, const char *path1, SDL_Rect clip)
|
|
|
|
{
|
|
|
|
Item *chest = create_item(path0,
|
|
|
|
path1,
|
|
|
|
clip,
|
|
|
|
NULL);
|
|
|
|
chest->openable = true;
|
|
|
|
chest->sprite->animate = false;
|
|
|
|
return chest;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
free(builder);
|
2018-01-25 10:45:05 +01:00
|
|
|
}
|