tbftss/src/system/load.c

121 lines
3.3 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 "load.h"
2016-03-03 19:03:07 +01:00
static void loadStats(cJSON *statsJSON);
2015-10-20 13:51:49 +02:00
static void loadStarSystems(cJSON *starSystemsJSON);
static void loadMissions(cJSON *missionsCJSON);
2016-02-27 17:16:21 +01:00
static void loadChallenges(cJSON *challengesCJSON);
2015-10-20 13:51:49 +02:00
void loadGame(void)
{
cJSON *root, *gameJSON;
2015-10-20 13:51:49 +02:00
char *text;
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
text = readFile(getSaveFilePath("game.save"));
root = cJSON_Parse(text);
2016-03-03 19:03:07 +01:00
gameJSON = cJSON_GetObjectItem(root, "game");
2016-03-03 19:03:07 +01:00
STRNCPY(game.selectedStarSystem, cJSON_GetObjectItem(gameJSON, "selectedStarSystem")->valuestring, MAX_NAME_LENGTH);
2016-03-03 19:03:07 +01:00
loadStarSystems(cJSON_GetObjectItem(gameJSON, "starSystems"));
2016-03-03 19:03:07 +01:00
2016-02-27 17:16:21 +01:00
loadChallenges(cJSON_GetObjectItem(gameJSON, "challenges"));
2016-03-03 19:03:07 +01:00
loadStats(cJSON_GetObjectItem(gameJSON, "stats"));
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
cJSON_Delete(root);
free(text);
}
static void loadStarSystems(cJSON *starSystemsJSON)
{
StarSystem *starSystem;
2015-10-20 13:51:49 +02:00
cJSON *starSystemJSON;
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
for (starSystemJSON = starSystemsJSON->child ; starSystemJSON != NULL ; starSystemJSON = starSystemJSON->next)
{
starSystem = getStarSystem(cJSON_GetObjectItem(starSystemJSON, "name")->valuestring);
2016-03-03 19:03:07 +01:00
starSystem->side = lookup(cJSON_GetObjectItem(starSystemJSON, "side")->valuestring);
2016-03-03 19:03:07 +01:00
loadMissions(cJSON_GetObjectItem(starSystemJSON, "missions"));
2015-10-20 13:51:49 +02:00
}
}
2016-02-27 17:16:21 +01:00
static void loadMissions(cJSON *missionsJSON)
2015-10-20 13:51:49 +02:00
{
Mission *mission;
2016-02-27 17:16:21 +01:00
cJSON *missionJSON;
2016-03-03 19:03:07 +01:00
2016-02-27 17:16:21 +01:00
for (missionJSON = missionsJSON->child ; missionJSON != NULL ; missionJSON = missionJSON->next)
2015-10-20 13:51:49 +02:00
{
2016-02-27 17:16:21 +01:00
mission = getMission(cJSON_GetObjectItem(missionJSON, "filename")->valuestring);
mission->completed = cJSON_GetObjectItem(missionJSON, "completed")->valueint;
2015-10-20 13:51:49 +02:00
}
}
2016-02-27 17:16:21 +01:00
static void loadChallenges(cJSON *missionsJSON)
2015-10-20 13:51:49 +02:00
{
2016-02-27 17:16:21 +01:00
Mission *mission;
2015-10-20 13:51:49 +02:00
Challenge *challenge;
2016-02-27 17:16:21 +01:00
cJSON *missionJSON, *challengeJSON;
int type, value;
2016-03-03 19:03:07 +01:00
2016-02-27 17:16:21 +01:00
if (missionsJSON)
2015-10-20 13:51:49 +02:00
{
2016-02-27 17:16:21 +01:00
for (missionJSON = missionsJSON->child ; missionJSON != NULL ; missionJSON = missionJSON->next)
2015-10-20 13:51:49 +02:00
{
2016-02-27 17:16:21 +01:00
mission = getMission(cJSON_GetObjectItem(missionJSON, "filename")->valuestring);
2016-03-03 19:03:07 +01:00
2016-02-27 17:16:21 +01:00
for (challengeJSON = cJSON_GetObjectItem(missionJSON, "challenges")->child ; challengeJSON != NULL ; challengeJSON = challengeJSON->next)
{
type = lookup(cJSON_GetObjectItem(challengeJSON, "type")->valuestring);
value = cJSON_GetObjectItem(challengeJSON, "value")->valueint;
2016-03-03 19:03:07 +01:00
2016-02-27 17:16:21 +01:00
challenge = getChallenge(mission, type, value);
2016-03-03 19:03:07 +01:00
2016-02-28 14:02:57 +01:00
if (challenge)
{
challenge->passed = cJSON_GetObjectItem(challengeJSON, "passed")->valueint;
}
2016-02-27 17:16:21 +01:00
}
2015-10-20 13:51:49 +02:00
}
}
}
2016-03-03 19:03:07 +01:00
static void loadStats(cJSON *statsJSON)
2015-10-20 13:51:49 +02:00
{
int i;
2015-12-23 20:22:31 +01:00
char *statName;
2016-03-03 19:03:07 +01:00
for (i = 0 ; i < STAT_MAX ; i++)
{
2015-12-23 20:22:31 +01:00
statName = getLookupName("STAT_", i);
2016-03-03 19:03:07 +01:00
if (statName && cJSON_GetObjectItem(statsJSON, statName))
2015-12-23 20:22:31 +01:00
{
2016-03-03 19:03:07 +01:00
game.stats[i] = cJSON_GetObjectItem(statsJSON, statName)->valueint;
2015-12-23 20:22:31 +01:00
}
}
2015-10-20 13:51:49 +02:00
}