tbftss/src/game/load.c

188 lines
4.8 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 "../battle/fighters.h"
2022-07-31 11:43:20 +02:00
#include "../challenges/challenges.h"
#include "../galaxy/mission.h"
2022-07-31 11:43:20 +02:00
#include "../galaxy/starSystems.h"
#include "../game/trophies.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 "load.h"
extern Game game;
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
static void loadStarSystems(cJSON *starSystemsJSON);
static void loadMissions(cJSON *missionsCJSON);
2016-02-27 17:16:21 +01:00
static void loadChallenges(cJSON *challengesCJSON);
2016-05-15 09:19:26 +02:00
static void loadTrophies(cJSON *trophiesJSON);
2016-05-17 20:02:58 +02:00
static void loadFighterStats(cJSON *fighterStatsJSON);
2015-10-20 13:51:49 +02:00
void loadGame(void)
{
cJSON *root, *gameJSON;
2022-07-31 11:43:20 +02:00
char *text;
2016-03-03 19:03:07 +01:00
text = readFile(getSaveFilePath(SAVE_FILENAME));
2015-10-20 13:51:49 +02:00
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"));
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
loadTrophies(cJSON_GetObjectItem(gameJSON, "trophies"));
2019-11-07 09:13:57 +01:00
2016-05-17 20:02:58 +02:00
loadFighterStats(cJSON_GetObjectItem(gameJSON, "fighterStats"));
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;
2022-07-31 11:43:20 +02:00
cJSON *starSystemJSON;
2016-03-03 19:03:07 +01:00
2022-07-31 11:43:20 +02:00
for (starSystemJSON = starSystemsJSON->child; starSystemJSON != NULL; starSystemJSON = starSystemJSON->next)
2015-10-20 13:51:49 +02:00
{
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;
2022-07-31 11:43:20 +02:00
cJSON *missionJSON;
2016-03-03 19:03:07 +01:00
2022-07-31 11:43:20 +02: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);
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
if (mission)
{
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
{
2022-07-31 11:43:20 +02:00
Mission *mission;
2015-10-20 13:51:49 +02:00
Challenge *challenge;
2022-07-31 11:43:20 +02: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
{
2022-07-31 11:43:20 +02: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
2022-07-31 11:43:20 +02:00
for (challengeJSON = cJSON_GetObjectItem(missionJSON, "challenges")->child; challengeJSON != NULL; challengeJSON = challengeJSON->next)
2016-02-27 17:16:21 +01:00
{
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
{
2022-07-31 11:43:20 +02:00
int i;
2015-12-23 20:22:31 +01:00
char *statName;
2016-03-03 19:03:07 +01:00
2022-07-31 11:43:20 +02: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
}
2016-05-15 09:19:26 +02:00
static void loadTrophies(cJSON *trophiesJSON)
{
Trophy *t;
2022-07-31 11:43:20 +02:00
cJSON *trophyJSON;
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
if (trophiesJSON)
{
2022-07-31 11:43:20 +02:00
for (trophyJSON = trophiesJSON->child; trophyJSON != NULL; trophyJSON = trophyJSON->next)
2016-05-15 09:19:26 +02:00
{
t = getTrophy(cJSON_GetObjectItem(trophyJSON, "id")->valuestring);
2019-11-07 09:13:57 +01:00
2016-05-15 09:19:26 +02:00
if (t)
{
t->awardDate = cJSON_GetObjectItem(trophyJSON, "awardDate")->valueint;
t->awarded = 1;
}
}
}
}
2016-05-17 20:02:58 +02:00
static void loadFighterStats(cJSON *fighterStatsJSON)
{
Tuple *t, *tail;
cJSON *fighterStatJSON;
2019-11-07 09:13:57 +01:00
2018-12-06 17:52:13 +01:00
destroyFighterStats();
2019-11-07 09:13:57 +01:00
2016-05-17 20:02:58 +02:00
tail = &game.fighterStatHead;
2019-11-07 09:13:57 +01:00
2016-05-17 20:02:58 +02:00
if (fighterStatsJSON)
{
2022-07-31 11:43:20 +02:00
for (fighterStatJSON = fighterStatsJSON->child; fighterStatJSON != NULL; fighterStatJSON = fighterStatJSON->next)
2016-05-17 20:02:58 +02:00
{
t = malloc(sizeof(Tuple));
memset(t, 0, sizeof(Tuple));
2019-11-07 09:13:57 +01:00
2016-05-17 20:02:58 +02:00
STRNCPY(t->key, cJSON_GetObjectItem(fighterStatJSON, "key")->valuestring, MAX_NAME_LENGTH);
t->value = cJSON_GetObjectItem(fighterStatJSON, "value")->valueint;
2019-11-07 09:13:57 +01:00
2016-05-17 20:02:58 +02:00
tail->next = t;
tail = t;
}
}
}