tbftss/src/galaxy/starSystems.c

214 lines
4.9 KiB
C
Raw Normal View History

2015-10-20 13:51:49 +02:00
/*
2019-01-01 17:14:11 +01:00
Copyright (C) 2015-2019 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);
starSystem->type = (starSystem->side != SIDE_PANDORAN) ? SS_NORMAL : SS_PANDORAN;
if (strcmp(starSystem->name, "Sol") == 0)
{
starSystem->type = SS_SOL;
}
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]);
}
2016-03-06 20:07:43 +01:00
sprintf(path, "data/missions/%s", name);
2016-03-06 20:07:43 +01:00
filenames = getFileList(path, &count);
2016-03-06 20:07:43 +01:00
for (i = 0 ; i < count ; i++)
{
sprintf(path, "data/missions/%s/%s", name, filenames[i]);
2016-03-06 20:07:43 +01:00
2016-02-27 17:16:21 +01:00
mission = loadMissionMeta(path);
if (mission)
{
tail->next = mission;
tail = mission;
}
2016-03-06 20:07:43 +01:00
free(filenames[i]);
}
2016-03-06 20:07:43 +01:00
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-08-11 19:06:42 +02: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++;
2016-06-07 10:24:09 +02:00
2016-08-11 19:06:42 +02:00
if (starSystem->type == SS_NORMAL && !mission->isOptional)
2016-06-07 10:24:09 +02:00
{
2016-08-11 19:06:42 +02:00
game.totalMissions++;
}
if (mission->completed)
{
starSystem->completedMissions++;
if (starSystem->type == SS_NORMAL && !mission->isOptional)
{
game.completedMissions++;
}
2016-06-07 10:24:09 +02:00
}
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 = starSystem->type == SS_SOL || isMissionAvailable(mission, prev);
if (mission->available)
{
2016-02-21 17:55:11 +01:00
starSystem->availableMissions++;
2016-06-07 10:24:09 +02:00
if (starSystem->type == SS_NORMAL && !mission->isOptional)
{
game.availableMissions++;
}
2016-08-11 19:06:42 +02:00
if (!mission->completed)
2016-08-11 19:06:42 +02:00
{
starSystem->activeMission = mission;
2016-08-11 19:06:42 +02:00
}
}
prev = mission;
}
2018-05-06 12:57:52 +02: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);
}
}