tbftss/src/battle/locations.c

147 lines
3.0 KiB
C
Raw Normal View History

/*
Copyright (C) 2015-2019,2022 Parallel Realities
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 "../common.h"
2022-07-31 11:43:20 +02:00
#include "../battle/script.h"
#include "../json/cJSON.h"
#include "../system/draw.h"
#include "../system/util.h"
2022-07-31 11:43:20 +02:00
#include "locations.h"
2022-07-31 11:43:20 +02:00
extern Battle battle;
extern Entity *player;
void doLocations(void)
{
Location *l, *prev;
2019-11-07 09:13:57 +01:00
prev = &battle.locationHead;
2019-11-07 09:13:57 +01:00
2022-07-31 11:43:20 +02:00
for (l = battle.locationHead.next; l != NULL; l = l->next)
{
if (l->active && getDistance(player->x, player->y, l->x, l->y) <= l->size)
{
runScriptFunction(l->name);
2019-11-07 09:13:57 +01:00
prev->next = l->next;
free(l);
l = prev;
}
2019-11-07 09:13:57 +01:00
prev = l;
}
}
void drawLocations(void)
{
Location *l;
2019-11-07 09:13:57 +01:00
2022-07-31 11:43:20 +02:00
for (l = battle.locationHead.next; l != NULL; l = l->next)
{
if (l->active)
{
drawCircle(l->x - battle.camera.x, l->y - battle.camera.y, l->size, 0, 255, 0, 255);
}
}
}
void activateLocations(char *locations)
{
2022-07-31 11:43:20 +02:00
char *token;
Location *l;
2019-11-07 09:13:57 +01:00
token = strtok(locations, ";");
2019-11-07 09:13:57 +01:00
while (token)
{
2022-07-31 11:43:20 +02:00
for (l = battle.locationHead.next; l != NULL; l = l->next)
{
if (strcmp(token, l->name) == 0)
{
l->active = 1;
}
}
2019-11-07 09:13:57 +01:00
token = strtok(NULL, ";");
}
}
2016-03-27 12:21:23 +02:00
2016-05-15 09:19:26 +02:00
/*
* Literally only used when Christabel's shuttle is disabled
*/
void createChristabelLocation(void)
{
Location *l;
2022-07-31 11:43:20 +02:00
Entity *e;
2019-11-07 09:13:57 +01:00
2022-07-31 11:43:20 +02:00
for (e = battle.entityHead.next; e != NULL; e = e->next)
2016-05-15 09:19:26 +02:00
{
if (strcmp(e->name, "Christabel") == 0)
{
l = malloc(sizeof(Location));
memset(l, 0, sizeof(Location));
battle.locationTail->next = l;
battle.locationTail = l;
STRNCPY(l->name, "CristabelLocation", MAX_NAME_LENGTH);
l->x = e->x;
l->y = e->y;
l->size = 500;
l->active = 1;
l->x -= l->size / 2;
l->y -= l->size / 2;
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
return;
}
}
}
2016-03-27 12:21:23 +02:00
void loadLocations(cJSON *node)
{
2022-07-31 11:43:20 +02:00
int active;
2016-03-27 12:21:23 +02:00
Location *l;
if (node)
{
node = node->child;
while (node)
{
l = malloc(sizeof(Location));
memset(l, 0, sizeof(Location));
battle.locationTail->next = l;
battle.locationTail = l;
STRNCPY(l->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH);
l->x = (cJSON_GetObjectItem(node, "x")->valuedouble / BATTLE_AREA_CELLS) * BATTLE_AREA_WIDTH;
l->y = (cJSON_GetObjectItem(node, "y")->valuedouble / BATTLE_AREA_CELLS) * BATTLE_AREA_HEIGHT;
l->size = cJSON_GetObjectItem(node, "size")->valueint;
l->active = active = getJSONValue(node, "active", 1);
l->x += (SCREEN_WIDTH / 2);
l->y += (SCREEN_HEIGHT / 2);
2019-11-07 09:13:57 +01:00
2016-03-27 12:21:23 +02:00
node = node->next;
}
}
}