tbftss/src/galaxy/starSystems.c

199 lines
4.7 KiB
C
Raw Normal View History

2015-10-20 13:51:49 +02:00
/*
2016-02-21 16:50:27 +01:00
Copyright (C) 2015-2016 Parallel Realities
2015-10-20 13:51:49 +02: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 "starSystems.h"
static void loadMissions(StarSystem *starSystem);
2016-02-27 13:14:29 +01:00
static StarSystem *loadStarSystem(cJSON *starSystemJSON);
2015-10-20 13:51:49 +02:00
void initStarSystems(void)
{
cJSON *root, *node;
char *text;
2016-02-27 13:14:29 +01:00
StarSystem *starSystem, *tail;
2016-02-27 13:14:29 +01:00
tail = &game.starSystemHead;
text = readFile("data/galaxy/starSystems.json");
2015-10-20 13:51:49 +02:00
root = cJSON_Parse(text);
2015-10-20 13:51:49 +02:00
for (node = cJSON_GetObjectItem(root, "starSystems")->child ; node != NULL ; node = node->next)
{
2016-02-27 13:14:29 +01:00
starSystem = loadStarSystem(node);
tail->next = starSystem;
tail = starSystem;
2015-10-20 13:51:49 +02:00
}
2015-10-20 13:51:49 +02:00
cJSON_Delete(root);
free(text);
}
2016-02-27 13:14:29 +01:00
static StarSystem *loadStarSystem(cJSON *starSystemJSON)
2015-10-20 13:51:49 +02:00
{
StarSystem *starSystem;
2015-10-20 13:51:49 +02:00
starSystem = malloc(sizeof(StarSystem));
memset(starSystem, 0, sizeof(StarSystem));
2015-10-20 13:51:49 +02:00
STRNCPY(starSystem->name, cJSON_GetObjectItem(starSystemJSON, "name")->valuestring, MAX_NAME_LENGTH);
starSystem->side = lookup(cJSON_GetObjectItem(starSystemJSON, "side")->valuestring);
starSystem->x = cJSON_GetObjectItem(starSystemJSON, "x")->valueint;
starSystem->y = cJSON_GetObjectItem(starSystemJSON, "y")->valueint;
starSystem->fallsToPandorans = getJSONValue(starSystemJSON, "fallsToPandorans", 0);
if (strcmp(starSystem->name, "Sol") == 0)
{
starSystem->isSol = 1;
}
2015-10-20 13:51:49 +02:00
starSystem->missionHead.completed = 1;
loadMissions(starSystem);
2015-10-20 13:51:49 +02:00
starSystem->x *= 3;
starSystem->y *= 3;
2016-02-27 13:14:29 +01:00
return starSystem;
2015-10-20 13:51:49 +02:00
}
static void loadMissions(StarSystem *starSystem)
{
int i, count;
char name[MAX_NAME_LENGTH];
char path[MAX_FILENAME_LENGTH];
char **filenames;
2016-02-27 17:16:21 +01:00
Mission *mission, *tail;
2016-02-27 17:16:21 +01:00
tail = &starSystem->missionHead;
STRNCPY(name, starSystem->name, MAX_NAME_LENGTH);
for (i = 0 ; name[i] ; i++)
{
name[i] = tolower(name[i]);
}
sprintf(path, "data/missions/%s", name);
filenames = getFileList(path, &count);
for (i = 0 ; i < count ; i++)
{
sprintf(path, "data/missions/%s/%s", name, filenames[i]);
2016-02-27 17:16:21 +01:00
mission = loadMissionMeta(path);
tail->next = mission;
tail = mission;
free(filenames[i]);
}
free(filenames);
}
2015-10-20 13:51:49 +02:00
StarSystem *getStarSystem(char *name)
{
StarSystem *starSystem;
2015-10-20 13:51:49 +02:00
for (starSystem = game.starSystemHead.next ; starSystem != NULL ; starSystem = starSystem->next)
{
if (strcmp(starSystem->name, name) == 0)
{
return starSystem;
}
}
2015-10-20 13:51:49 +02:00
return NULL;
}
void updateStarSystemMissions(void)
2015-10-20 13:51:49 +02:00
{
StarSystem *starSystem;
Mission *mission, *prev;
2016-02-21 17:55:11 +01:00
game.completedMissions = game.totalMissions = game.availableMissions = 0;
2015-10-20 13:51:49 +02:00
for (starSystem = game.starSystemHead.next ; starSystem != NULL ; starSystem = starSystem->next)
{
2016-02-21 17:55:11 +01:00
starSystem->completedMissions = starSystem->availableMissions = starSystem->totalMissions = 0;
2015-10-20 13:51:49 +02:00
for (mission = starSystem->missionHead.next ; mission != NULL ; mission = mission->next)
{
2016-02-21 17:55:11 +01:00
starSystem->totalMissions++;
2015-10-20 13:51:49 +02:00
if (mission->completed)
{
starSystem->completedMissions++;
}
}
if (strcmp(starSystem->name, "Sol") != 0)
{
2016-02-21 17:55:11 +01:00
game.totalMissions += starSystem->totalMissions;
game.completedMissions += starSystem->completedMissions;
}
2015-10-20 13:51:49 +02:00
}
for (starSystem = game.starSystemHead.next ; starSystem != NULL ; starSystem = starSystem->next)
{
prev = &starSystem->missionHead;
for (mission = starSystem->missionHead.next ; mission != NULL ; mission = mission->next)
{
mission->available = strcmp(starSystem->name, "Sol") == 0 || isMissionAvailable(mission, prev);
if (mission->available)
{
2016-02-21 17:55:11 +01:00
starSystem->availableMissions++;
}
prev = mission;
}
if (strcmp(starSystem->name, "Sol") != 0)
{
2016-02-21 17:55:11 +01:00
game.availableMissions += starSystem->availableMissions;
}
2016-02-21 17:55:11 +01:00
sprintf(starSystem->description, "[ %s ] [ Missions %d / %d ]", starSystem->name, starSystem->completedMissions, starSystem->availableMissions);
}
}
2015-10-20 13:51:49 +02:00
void destroyStarSystems(void)
{
StarSystem *starSystem;
Mission *mission;
2015-10-20 13:51:49 +02:00
while (game.starSystemHead.next)
{
starSystem = game.starSystemHead.next;
2015-10-20 13:51:49 +02:00
while (starSystem->missionHead.next)
{
mission = starSystem->missionHead.next;
starSystem->missionHead.next = mission->next;
free(mission);
}
2015-10-20 13:51:49 +02:00
game.starSystemHead.next = starSystem->next;
free(starSystem);
}
}