2015-10-20 13:51:49 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2015 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 "load.h"
|
|
|
|
|
|
|
|
static void loadStats(cJSON *stats);
|
|
|
|
static void loadStarSystems(cJSON *starSystemsJSON);
|
2015-10-25 10:29:41 +01:00
|
|
|
static void loadMissions(cJSON *missionsCJSON);
|
2015-10-20 13:51:49 +02:00
|
|
|
static void loadChallenges(Mission *mission, cJSON *challengesCJSON);
|
|
|
|
|
|
|
|
void loadGame(void)
|
|
|
|
{
|
2015-10-30 08:22:49 +01:00
|
|
|
cJSON *root, *gameJSON;
|
2015-10-20 13:51:49 +02:00
|
|
|
char *text;
|
|
|
|
|
|
|
|
text = readFile(getSaveFilePath("game.save"));
|
|
|
|
root = cJSON_Parse(text);
|
|
|
|
|
2015-10-30 08:22:49 +01:00
|
|
|
gameJSON = cJSON_GetObjectItem(root, "game");
|
2015-10-20 13:51:49 +02:00
|
|
|
|
2015-10-30 08:22:49 +01:00
|
|
|
STRNCPY(game.selectedStarSystem, cJSON_GetObjectItem(gameJSON, "selectedStarSystem")->valuestring, MAX_NAME_LENGTH);
|
2015-10-20 13:51:49 +02:00
|
|
|
|
2015-10-30 08:22:49 +01:00
|
|
|
loadStarSystems(cJSON_GetObjectItem(gameJSON, "starSystems"));
|
|
|
|
|
|
|
|
loadStats(cJSON_GetObjectItem(gameJSON, "stats"));
|
2015-10-20 13:51:49 +02:00
|
|
|
|
|
|
|
cJSON_Delete(root);
|
|
|
|
free(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void loadStarSystems(cJSON *starSystemsJSON)
|
|
|
|
{
|
|
|
|
cJSON *starSystemJSON;
|
|
|
|
|
|
|
|
for (starSystemJSON = starSystemsJSON->child ; starSystemJSON != NULL ; starSystemJSON = starSystemJSON->next)
|
|
|
|
{
|
2015-10-25 10:29:41 +01:00
|
|
|
loadMissions(cJSON_GetObjectItem(starSystemJSON, "missions"));
|
2015-10-20 13:51:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 10:29:41 +01:00
|
|
|
static void loadMissions(cJSON *missionsCJSON)
|
2015-10-20 13:51:49 +02:00
|
|
|
{
|
|
|
|
Mission *mission;
|
|
|
|
cJSON *missionCJSON;
|
|
|
|
|
|
|
|
for (missionCJSON = missionsCJSON->child ; missionCJSON != NULL ; missionCJSON = missionCJSON->next)
|
|
|
|
{
|
2015-10-25 10:29:41 +01:00
|
|
|
mission = getMission(cJSON_GetObjectItem(missionCJSON, "filename")->valuestring);
|
2015-10-20 13:51:49 +02:00
|
|
|
|
|
|
|
mission->completed = cJSON_GetObjectItem(missionCJSON, "completed")->valueint;
|
|
|
|
|
|
|
|
if (cJSON_GetObjectItem(missionCJSON, "challenges"))
|
|
|
|
{
|
|
|
|
loadChallenges(mission, cJSON_GetObjectItem(missionCJSON, "challenges"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void loadChallenges(Mission *mission, cJSON *challengesCJSON)
|
|
|
|
{
|
|
|
|
Challenge *challenge;
|
|
|
|
cJSON *challengeCJSON;
|
|
|
|
|
|
|
|
for (challengeCJSON = challengesCJSON->child ; challengeCJSON != NULL ; challengeCJSON = challengeCJSON->next)
|
|
|
|
{
|
|
|
|
challenge = getChallenge(mission, lookup(cJSON_GetObjectItem(challengeCJSON, "type")->valuestring));
|
|
|
|
|
|
|
|
if (!challenge)
|
|
|
|
{
|
|
|
|
printf("Couldn't find challenge to update\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
challenge->passed = cJSON_GetObjectItem(challengeCJSON, "passed")->valueint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void loadStats(cJSON *stats)
|
|
|
|
{
|
2015-10-25 01:01:46 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0 ; i < STAT_MAX ; i++)
|
2015-10-24 17:00:06 +02:00
|
|
|
{
|
2015-10-25 01:01:46 +02:00
|
|
|
game.stats[i] = cJSON_GetObjectItem(stats, getLookupName("STAT_", i))->valueint;
|
2015-10-24 17:00:06 +02:00
|
|
|
}
|
2015-10-20 13:51:49 +02:00
|
|
|
}
|