tbftss/src/battle/items.c

251 lines
5.0 KiB
C
Raw Normal View History

2015-11-16 12:27:03 +01:00
/*
2019-01-01 17:14:11 +01:00
Copyright (C) 2015-2019 Parallel Realities
2015-11-16 12:27:03 +01:00
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 2
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "items.h"
static void action(void);
static Entity *getItemDef(char *name);
static Entity defHead, *defTail;
void loadItemDefs(void)
{
cJSON *root, *node;
char *text;
Entity *e;
text = readFile("data/battle/items.json");
2015-11-16 12:27:03 +01:00
root = cJSON_Parse(text);
2015-11-16 12:27:03 +01:00
memset(&defHead, 0, sizeof(Entity));
defTail = &defHead;
2015-11-16 12:27:03 +01:00
for (node = root->child ; node != NULL ; node = node->next)
{
e = malloc(sizeof(Entity));
memset(e, 0, sizeof(Entity));
e->type = ET_ITEM;
e->active = 1;
2015-11-16 12:27:03 +01:00
STRNCPY(e->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH);
2016-05-15 09:19:26 +02:00
STRNCPY(e->defName, cJSON_GetObjectItem(node, "defName")->valuestring, MAX_NAME_LENGTH);
2018-12-06 09:37:19 +01:00
e->texture = getAtlasImage(cJSON_GetObjectItem(node, "texture")->valuestring);
e->health = e->maxHealth = FPS;
2016-05-15 09:19:26 +02:00
e->flags = EF_NO_HEALTH_BAR;
2018-12-06 09:37:19 +01:00
e->w = e->texture->rect.w;
e->h = e->texture->rect.h;
2015-11-16 12:27:03 +01:00
defTail->next = e;
2016-03-12 19:22:48 +01:00
defTail = e;
2015-11-16 12:27:03 +01:00
}
2015-11-16 12:27:03 +01:00
cJSON_Delete(root);
free(text);
}
Entity *spawnItem(char *name)
{
2016-06-05 09:35:59 +02:00
Entity *e, *def, *item;
def = NULL;
2015-11-16 12:27:03 +01:00
item = spawnEntity();
2016-06-05 09:35:59 +02:00
if (strcmp(name, "RANDOM"))
{
def = getItemDef(name);
}
else
{
for (e = defHead.next ; e != NULL ; e = e->next)
{
if (!def || rand() % 2)
{
def = e;
}
}
}
2015-11-16 12:27:03 +01:00
memcpy(item, def, sizeof(Entity));
2016-05-15 09:19:26 +02:00
item->next = NULL;
item->action = action;
2016-05-15 09:19:26 +02:00
return item;
}
void addRandomItem(int x, int y)
{
2016-06-05 09:35:59 +02:00
Entity *item;
2016-05-15 09:19:26 +02:00
2016-06-05 09:35:59 +02:00
item = spawnItem("RANDOM");
2016-05-15 09:19:26 +02:00
item->x = x;
item->y = y;
item->speed = 1;
item->dx = rand() % 200 - rand() % 200;
item->dy = rand() % 200 - rand() % 200;
item->dx *= 0.01;
item->dy *= 0.01;
2015-11-16 12:27:03 +01:00
item->action = action;
}
2016-05-15 09:19:26 +02:00
static Entity *getItemDef(char *defName)
2015-11-16 12:27:03 +01:00
{
Entity *e;
2015-11-16 12:27:03 +01:00
for (e = defHead.next ; e != NULL ; e = e->next)
{
2016-05-15 09:19:26 +02:00
if (strcmp(e->defName, defName) == 0)
2015-11-16 12:27:03 +01:00
{
return e;
}
}
2016-05-15 09:19:26 +02:00
printf("Error: no such item '%s'\n", defName);
2015-11-16 12:27:03 +01:00
exit(1);
}
static void action(void)
{
Entity *e, **candidates;
int i;
2016-05-15 09:19:26 +02:00
candidates = getAllEntsInRadius(self->x, self->y, MAX(self->w, self->h), self);
2015-11-16 12:27:03 +01:00
for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i])
{
2016-05-15 09:19:26 +02:00
if (e->alive == ALIVE_ALIVE && (e->flags & EF_COLLECTS_ITEMS) && collision(self->x - (self->w / 2), self->y - (self->h / 2), self->w, self->h, e->x - (e->w / 2), e->y - (e->h / 2), e->w, e->h))
2015-11-16 12:27:03 +01:00
{
self->health = 0;
2015-11-17 08:23:50 +01:00
playBattleSound(SND_GET_ITEM, self->x, self->y);
updateObjective(self->name, TT_ITEM);
2015-11-17 08:23:50 +01:00
if (e == player)
{
2016-02-28 14:45:17 +01:00
addHudMessage(colors.white, _("Picked up %s"), self->name);
2016-03-07 13:29:23 +01:00
battle.stats[STAT_ITEMS_COLLECTED_PLAYER]++;
}
else
{
2015-11-17 08:23:50 +01:00
battle.stats[STAT_ITEMS_COLLECTED]++;
}
self->action = NULL;
2015-11-16 12:27:03 +01:00
}
}
}
2016-03-27 12:21:23 +02:00
void loadItems(cJSON *node)
{
Entity *e;
char *name, *groupName, *type;
int i, scatter, number, active, addFlags;
long flags;
float x, y;
flags = -1;
scatter = 1;
if (node)
{
node = node->child;
while (node)
{
type = cJSON_GetObjectItem(node, "type")->valuestring;
x = (cJSON_GetObjectItem(node, "x")->valuedouble / BATTLE_AREA_CELLS) * BATTLE_AREA_WIDTH;
y = (cJSON_GetObjectItem(node, "y")->valuedouble / BATTLE_AREA_CELLS) * BATTLE_AREA_HEIGHT;
name = NULL;
groupName = NULL;
name = getJSONValueStr(node, "name", NULL);
groupName = getJSONValueStr(node, "groupName", NULL);
number = getJSONValue(node, "number", 1);
scatter = getJSONValue(node, "scatter", 1);
active = getJSONValue(node, "active", 1);
if (cJSON_GetObjectItem(node, "flags"))
{
flags = flagsToLong(cJSON_GetObjectItem(node, "flags")->valuestring, &addFlags);
}
for (i = 0 ; i < number ; i++)
{
e = spawnItem(type);
if (name)
{
STRNCPY(e->name, name, MAX_NAME_LENGTH);
}
if (groupName)
{
STRNCPY(e->groupName, groupName, MAX_NAME_LENGTH);
}
if (flags != -1)
{
if (addFlags)
{
e->flags |= flags;
}
else
{
e->flags = flags;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Flags for '%s' (%s) replaced", e->name, e->defName);
}
}
e->x = x;
e->y = y;
e->active = active;
if (scatter > 1)
{
e->x += (rand() % scatter) - (rand() % scatter);
e->y += (rand() % scatter) - (rand() % scatter);
}
2018-12-06 09:37:19 +01:00
e->w = e->texture->rect.w;
e->h = e->texture->rect.h;
2016-03-27 12:21:23 +02:00
}
node = node->next;
}
}
}
2015-11-16 12:27:03 +01:00
void destroyItemDefs(void)
{
2015-12-14 09:15:41 +01:00
Entity *e;
2015-11-16 12:27:03 +01:00
while (defHead.next)
{
2015-12-14 09:15:41 +01:00
e = defHead.next;
defHead.next = e->next;
free(e);
2015-11-16 12:27:03 +01:00
}
}