tbftss/src/game/save.c

208 lines
5.2 KiB
C
Raw Normal View History

2015-10-20 13:51:49 +02:00
/*
Copyright (C) 2015-2019,2022 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 "../common.h"
2022-07-31 11:43:20 +02:00
#include "../json/cJSON.h"
#include "../system/io.h"
2022-07-31 11:43:20 +02:00
#include "../system/lookup.h"
#include "save.h"
extern Game game;
2015-10-20 13:51:49 +02:00
2022-07-31 11:43:20 +02:00
static void saveStarSystems(cJSON *gameJSON);
static void saveChallenges(cJSON *gameJSON);
2015-10-20 13:51:49 +02:00
static cJSON *getMissionsJSON(StarSystem *starSystem);
2022-07-31 11:43:20 +02:00
static void saveStats(cJSON *gameJSON);
static void saveTrophies(cJSON *gameJSON);
static void saveFighterStats(cJSON *gameJSON);
2015-10-20 13:51:49 +02:00
void saveGame(void)
{
2022-07-31 11:43:20 +02:00
char *out;
2015-10-20 13:51:49 +02:00
cJSON *root, *gameJSON;
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving Game ...");
root = cJSON_CreateObject();
gameJSON = cJSON_CreateObject();
cJSON_AddItemToObject(root, "game", gameJSON);
2016-03-03 19:03:07 +01:00
cJSON_AddStringToObject(gameJSON, "selectedStarSystem", game.selectedStarSystem);
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
saveStarSystems(gameJSON);
2016-03-03 19:03:07 +01:00
2016-02-27 17:16:21 +01:00
saveChallenges(gameJSON);
2015-10-20 13:51:49 +02:00
saveStats(gameJSON);
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
saveTrophies(gameJSON);
2019-11-07 09:13:57 +01:00
2016-05-17 20:02:58 +02:00
saveFighterStats(gameJSON);
2015-10-20 13:51:49 +02:00
out = cJSON_Print(root);
writeFile(getSaveFilePath(SAVE_FILENAME), out);
2015-10-20 13:51:49 +02:00
cJSON_Delete(root);
free(out);
}
static void saveStarSystems(cJSON *gameJSON)
{
2022-07-31 11:43:20 +02:00
cJSON *starSystemJSON, *starSystemsJSON;
2015-10-20 13:51:49 +02:00
StarSystem *starSystem;
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
starSystemsJSON = cJSON_CreateArray();
2022-07-31 11:43:20 +02:00
for (starSystem = game.starSystemHead.next; starSystem != NULL; starSystem = starSystem->next)
2015-10-20 13:51:49 +02:00
{
2016-05-15 09:19:26 +02:00
if (starSystem->totalMissions > 0)
{
starSystemJSON = cJSON_CreateObject();
2016-03-03 19:03:07 +01:00
2016-05-15 09:19:26 +02:00
cJSON_AddStringToObject(starSystemJSON, "name", starSystem->name);
cJSON_AddStringToObject(starSystemJSON, "side", getLookupName("SIDE_", starSystem->side));
cJSON_AddItemToObject(starSystemJSON, "missions", getMissionsJSON(starSystem));
2016-03-03 19:03:07 +01:00
2016-05-15 09:19:26 +02:00
cJSON_AddItemToArray(starSystemsJSON, starSystemJSON);
}
2015-10-20 13:51:49 +02:00
}
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
cJSON_AddItemToObject(gameJSON, "starSystems", starSystemsJSON);
}
static cJSON *getMissionsJSON(StarSystem *starSystem)
{
2022-07-31 11:43:20 +02:00
cJSON *missionJSON, *missionsJSON;
2015-10-20 13:51:49 +02:00
Mission *mission;
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
missionsJSON = cJSON_CreateArray();
2016-03-03 19:03:07 +01:00
2022-07-31 11:43:20 +02:00
for (mission = starSystem->missionHead.next; mission != NULL; mission = mission->next)
2015-10-20 13:51:49 +02:00
{
missionJSON = cJSON_CreateObject();
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
cJSON_AddStringToObject(missionJSON, "filename", mission->filename);
cJSON_AddNumberToObject(missionJSON, "completed", mission->completed);
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
cJSON_AddItemToArray(missionsJSON, missionJSON);
}
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
return missionsJSON;
}
2016-02-27 17:16:21 +01:00
static void saveChallenges(cJSON *gameJSON)
2015-10-20 13:51:49 +02:00
{
2022-07-31 11:43:20 +02:00
int i;
Mission *mission;
2016-02-27 17:16:21 +01:00
Challenge *c;
2022-07-31 11:43:20 +02:00
cJSON *missionsJSON, *missionJSON, *challengesJSON, *challengeJSON;
2016-03-03 19:03:07 +01:00
2016-02-27 17:16:21 +01:00
missionsJSON = cJSON_CreateArray();
2016-03-03 19:03:07 +01:00
2022-07-31 11:43:20 +02:00
for (mission = game.challengeMissionHead.next; mission != NULL; mission = mission->next)
2015-10-20 13:51:49 +02:00
{
2016-02-27 17:16:21 +01:00
missionJSON = cJSON_CreateObject();
2016-03-03 19:03:07 +01:00
2016-02-27 17:16:21 +01:00
cJSON_AddStringToObject(missionJSON, "filename", mission->filename);
2016-03-03 19:03:07 +01:00
2016-02-27 17:16:21 +01:00
challengesJSON = cJSON_CreateArray();
2019-11-07 09:13:57 +01:00
2022-07-31 11:43:20 +02:00
for (i = 0; i < MAX_CHALLENGES; i++)
2016-02-27 17:16:21 +01:00
{
c = mission->challengeData.challenges[i];
2019-11-07 09:13:57 +01:00
2016-03-03 08:44:04 +01:00
if (c)
{
challengeJSON = cJSON_CreateObject();
cJSON_AddStringToObject(challengeJSON, "type", getLookupName("CHALLENGE_", c->type));
cJSON_AddNumberToObject(challengeJSON, "value", c->value);
cJSON_AddNumberToObject(challengeJSON, "passed", c->passed);
2019-11-07 09:13:57 +01:00
2016-03-03 08:44:04 +01:00
cJSON_AddItemToArray(challengesJSON, challengeJSON);
}
2016-02-27 17:16:21 +01:00
}
2016-03-03 19:03:07 +01:00
2016-02-27 17:16:21 +01:00
cJSON_AddItemToObject(missionJSON, "challenges", challengesJSON);
2016-03-03 19:03:07 +01:00
2016-02-27 17:16:21 +01:00
cJSON_AddItemToArray(missionsJSON, missionJSON);
2015-10-20 13:51:49 +02:00
}
2016-03-03 19:03:07 +01:00
2016-02-27 17:16:21 +01:00
cJSON_AddItemToObject(gameJSON, "challenges", missionsJSON);
2015-10-20 13:51:49 +02:00
}
static void saveStats(cJSON *gameJSON)
{
int i;
2016-03-03 19:03:07 +01:00
2015-10-20 13:51:49 +02:00
cJSON *stats = cJSON_CreateObject();
2016-03-03 19:03:07 +01:00
2022-07-31 11:43:20 +02:00
for (i = 0; i < STAT_MAX; i++)
{
cJSON_AddNumberToObject(stats, getLookupName("STAT_", i), game.stats[i]);
}
2015-10-20 13:51:49 +02:00
cJSON_AddItemToObject(gameJSON, "stats", stats);
}
2016-05-15 09:19:26 +02:00
static void saveTrophies(cJSON *gameJSON)
{
Trophy *t;
2022-07-31 11:43:20 +02:00
cJSON *trophiesJSON, *trophyJSON;
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
trophiesJSON = cJSON_CreateArray();
2022-07-31 11:43:20 +02:00
for (t = game.trophyHead.next; t != NULL; t = t->next)
2016-05-15 09:19:26 +02:00
{
if (t->awarded)
{
trophyJSON = cJSON_CreateObject();
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
cJSON_AddStringToObject(trophyJSON, "id", t->id);
cJSON_AddNumberToObject(trophyJSON, "awardDate", t->awardDate);
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
cJSON_AddItemToArray(trophiesJSON, trophyJSON);
}
}
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
cJSON_AddItemToObject(gameJSON, "trophies", trophiesJSON);
}
2016-05-17 20:02:58 +02:00
static void saveFighterStats(cJSON *gameJSON)
{
Tuple *t;
cJSON *fighterStatsJSON, *fighterStatJSON;
2019-11-07 09:13:57 +01:00
2016-05-17 20:02:58 +02:00
fighterStatsJSON = cJSON_CreateArray();
2022-07-31 11:43:20 +02:00
for (t = game.fighterStatHead.next; t != NULL; t = t->next)
2016-05-17 20:02:58 +02:00
{
fighterStatJSON = cJSON_CreateObject();
2019-11-07 09:13:57 +01:00
2016-05-17 20:02:58 +02:00
cJSON_AddStringToObject(fighterStatJSON, "key", t->key);
cJSON_AddNumberToObject(fighterStatJSON, "value", t->value);
2019-11-07 09:13:57 +01:00
2016-05-17 20:02:58 +02:00
cJSON_AddItemToArray(fighterStatsJSON, fighterStatJSON);
}
2019-11-07 09:13:57 +01:00
2016-05-17 20:02:58 +02:00
cJSON_AddItemToObject(gameJSON, "fighterStats", fighterStatsJSON);
}