From 2e5b45064a08387544084d3851d83f7172afa6d2 Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 27 Feb 2016 16:16:21 +0000 Subject: [PATCH] Challenges integration. --- common.mk | 4 +- data/challenges/01.json | 6 +- data/widgets/challenges.json | 12 ++ src/battle/battle.c | 28 +++- src/battle/battle.h | 1 + src/challenges/challengeHome.c | 192 ++++++++++++++++++++++++ src/challenges/challengeHome.h | 47 ++++++ src/{battle => challenges}/challenges.c | 26 ++-- src/{battle => challenges}/challenges.h | 0 src/galaxy/galacticMap.c | 29 ++-- src/galaxy/mission.c | 14 +- src/galaxy/starSystems.c | 8 +- src/game/game.c | 24 --- src/game/title.c | 12 +- src/game/title.h | 7 +- src/main.c | 5 + src/main.h | 3 + src/structs.h | 6 +- src/system/load.c | 47 +++--- src/system/load.h | 2 +- src/system/save.c | 44 +++--- src/test/testMission.c | 2 +- 22 files changed, 395 insertions(+), 124 deletions(-) create mode 100644 data/widgets/challenges.json create mode 100644 src/challenges/challengeHome.c create mode 100644 src/challenges/challengeHome.h rename src/{battle => challenges}/challenges.c (86%) rename src/{battle => challenges}/challenges.h (100%) diff --git a/common.mk b/common.mk index 3defbaa..02d4a4e 100644 --- a/common.mk +++ b/common.mk @@ -1,7 +1,7 @@ VERSION = 0.6 REVISION = $(shell date +"%y%m%d") -SEARCHPATH += src/ src/battle src/draw src/game src/galaxy src/json src/system src/test +SEARCHPATH += src/ src/battle src/challenges src/draw src/game src/galaxy src/json src/system src/test vpath %.c $(SEARCHPATH) vpath %.h $(SEARCHPATH) @@ -9,7 +9,7 @@ DEPS += defs.h structs.h OBJS += ai.o OBJS += battle.o bullets.o -OBJS += capitalShips.o challenges.o cJSON.o +OBJS += capitalShips.o challengeHome.o challenges.o cJSON.o OBJS += debris.o dev.o draw.o OBJS += effects.o entities.o extractionPoint.o OBJS += fighters.o diff --git a/data/challenges/01.json b/data/challenges/01.json index 16d9d88..97e816a 100644 --- a/data/challenges/01.json +++ b/data/challenges/01.json @@ -23,15 +23,15 @@ "challenges" : [ { "type" : "CHALLENGE_TIME", - "targetValue" : 10 + "value" : 20 }, { "type" : "CHALLENGE_TIME", - "targetValue" : 15 + "value" : 15 }, { "type" : "CHALLENGE_TIME", - "targetValue" : 20 + "value" : 10 } ], "fighters" : [ diff --git a/data/widgets/challenges.json b/data/widgets/challenges.json new file mode 100644 index 0000000..f0ca632 --- /dev/null +++ b/data/widgets/challenges.json @@ -0,0 +1,12 @@ +[ + { + "name" : "start", + "group" : "challenges", + "type" : "WT_BUTTON", + "text" : "Start Challenge Mission", + "x" : -1, + "y" : 680, + "w" : 250, + "h": 34 + } +] diff --git a/src/battle/battle.c b/src/battle/battle.c index 8a80743..cd4048e 100644 --- a/src/battle/battle.c +++ b/src/battle/battle.c @@ -249,7 +249,7 @@ static void drawMenu(void) SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 0); SDL_RenderFillRect(app.renderer, &r); - SDL_SetRenderDrawColor(app.renderer, 200, 200, 200, 255); + SDL_SetRenderDrawColor(app.renderer, 200, 200, 200, SDL_ALPHA_OPAQUE); SDL_RenderDrawRect(app.renderer, &r); drawWidgets("inBattle"); @@ -305,7 +305,14 @@ static void continueGame(void) destroyBattle(); - initGalacticMap(); + if (!battle.isChallenge) + { + initGalacticMap(); + } + else + { + initChallengeHome(); + } } static void options(void) @@ -339,7 +346,14 @@ static void quitBattle(void) destroyBattle(); - initGalacticMap(); + if (!battle.isChallenge) + { + initGalacticMap(); + } + else + { + initChallengeHome(); + } } static void postBattle(void) @@ -354,11 +368,13 @@ static void postBattle(void) } } - if (game.currentMission && !game.currentMission->completed) + if (!battle.isChallenge) { - game.currentMission->completed = (battle.status == MS_COMPLETE || !battle.numObjectivesTotal); + if (game.currentMission && !game.currentMission->completed) + { + game.currentMission->completed = (battle.status == MS_COMPLETE || !battle.numObjectivesTotal); + } } - } void destroyBattle(void) diff --git a/src/battle/battle.h b/src/battle/battle.h index b680472..e18d870 100644 --- a/src/battle/battle.h +++ b/src/battle/battle.h @@ -81,6 +81,7 @@ extern void drawLocations(void); extern void destroyDebris(void); extern void destroyBullets(void); extern void destroyEffects(void); +extern void initChallengeHome(void); extern App app; extern Battle battle; diff --git a/src/challenges/challengeHome.c b/src/challenges/challengeHome.c new file mode 100644 index 0000000..b03aa11 --- /dev/null +++ b/src/challenges/challengeHome.c @@ -0,0 +1,192 @@ +/* +Copyright (C) 2015-2016 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 "challengeHome.h" + +static void logic(void); +static void draw(void); +static void handleKeyboard(void); +static void drawChallenges(void); +static void updateChallengeMissions(void); +static void doChallenges(void); +static void startChallengeMission(void); + +static SDL_Texture *background; +static int startIndex; +static Widget *start; + +void initChallengeHome(void) +{ + startSectionTransition(); + + stopMusic(); + + updateChallengeMissions(); + + saveGame(); + + app.delegate.logic = &logic; + app.delegate.draw = &draw; + memset(&app.keyboard, 0, sizeof(int) * MAX_KEYBOARD_KEYS); + + background = getTexture("gfx/backgrounds/background04.jpg"); + + battle.camera.x = battle.camera.y = 0; + + game.currentMission = NULL; + + startIndex = 0; + + initBackground(); + + start = getWidget("start", "challenges"); + start->enabled = 0; + start->action = startChallengeMission; + + setMouse(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); + + endSectionTransition(); +} + +static void updateChallengeMissions(void) +{ + Mission *m; + Challenge *c; + + for (m = game.challengeMissionHead.next ; m != NULL ; m = m->next) + { + m->totalChallenges = m->completedChallenges = 0; + + for (c = m->challengeHead.next ; c != NULL ; c = c->next) + { + m->totalChallenges++; + + if (c->passed) + { + m->completedChallenges++; + } + } + } +} + +static void logic(void) +{ + handleKeyboard(); + + scrollBackground(-0.25, 0); + + doStars(0.5, 0); + + doChallenges(); + + doWidgets(); +} + +static void doChallenges(void) +{ + Mission *c; + + for (c = game.challengeMissionHead.next ; c != NULL ; c = c->next) + { + if (app.mouse.button[SDL_BUTTON_LEFT] && collision(app.mouse.x, app.mouse.y, 3, 3, c->rect.x, c->rect.y, c->rect.w, c->rect.h)) + { + game.currentMission = c; + + start->enabled = 1; + + app.mouse.button[SDL_BUTTON_LEFT] = 0; + } + } +} + +static void draw(void) +{ + drawBackground(background); + + drawStars(); + + drawChallenges(); + + drawWidgets("challenges"); +} + +static void drawChallenges(void) +{ + Mission *c; + SDL_Rect r; + int i, endIndex; + + r.x = 135; + r.y = 165; + r.w = r.h = 96; + + endIndex = startIndex + MAX_ITEMS; + i = 0; + + for (c = game.challengeMissionHead.next ; c != NULL ; c = c->next) + { + c->rect = r; + + if (i >= startIndex && i <= endIndex) + { + SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 0); + SDL_RenderFillRect(app.renderer, &r); + + if (game.currentMission == c) + { + SDL_SetRenderDrawColor(app.renderer, 64, 128, 200, SDL_ALPHA_OPAQUE); + SDL_RenderFillRect(app.renderer, &r); + SDL_SetRenderDrawColor(app.renderer, 128, 192, 255, SDL_ALPHA_OPAQUE); + SDL_RenderDrawRect(app.renderer, &r); + } + else + { + SDL_SetRenderDrawColor(app.renderer, 64, 64, 64, SDL_ALPHA_OPAQUE); + SDL_RenderDrawRect(app.renderer, &r); + } + + drawText(r.x + (r.w / 2), r.y + 28, 30, TA_CENTER, colors.white, "%d", i + 1); + + drawText(r.x + (r.w / 2), r.y + r.w + 5, 18, TA_CENTER, (c->completedChallenges < c->totalChallenges) ? colors.white : colors.green, "%d / %d", c->completedChallenges, c->totalChallenges); + + r.x += 150; + + if (r.x > SCREEN_WIDTH - 200) + { + r.y += 165; + r.x = 135; + } + } + + i++; + } +} + +static void handleKeyboard(void) +{ + +} + +static void startChallengeMission(void) +{ + initBattle(); + + loadMission(game.currentMission->filename); +} diff --git a/src/challenges/challengeHome.h b/src/challenges/challengeHome.h new file mode 100644 index 0000000..b727334 --- /dev/null +++ b/src/challenges/challengeHome.h @@ -0,0 +1,47 @@ +/* +Copyright (C) 2015-2016 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 "../common.h" + +#define MAX_ITEMS 21 + +extern void startSectionTransition(void); +extern void endSectionTransition(void); +extern void stopMusic(void); +extern void initBattle(void); +extern void loadMission(char *filename); +extern void setMouse(int x, int y); +extern void doStars(float dx, float dy); +extern void drawStars(void); +extern void doWidgets(void); +extern SDL_Texture *getTexture(const char *filename); +extern void drawBackground(SDL_Texture *texture); +extern void initBackground(void); +extern void scrollBackground(float x, float y); +extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); +extern void drawWidgets(char *groupName); +extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2); +extern Widget *getWidget(const char *name, const char *group); +extern void saveGame(void); + +extern App app; +extern Battle battle; +extern Colors colors; +extern Game game; diff --git a/src/battle/challenges.c b/src/challenges/challenges.c similarity index 86% rename from src/battle/challenges.c rename to src/challenges/challenges.c index 77b97eb..cc02b56 100644 --- a/src/battle/challenges.c +++ b/src/challenges/challenges.c @@ -114,14 +114,14 @@ static void updateTimeChallenge(Challenge *c) switch (c->type) { case CHALLENGE_TIME: - if (battle.stats[STAT_TIME] / FPS <= c->targetValue) + if (battle.stats[STAT_TIME] / FPS < c->value) { c->passed = 1; } break; case CHALLENGE_TIME_MINS: - if ((battle.stats[STAT_TIME] / FPS) / 60 <= c->targetValue) + if ((battle.stats[STAT_TIME] / FPS) / 60 < c->value) { c->passed = 1; } @@ -137,7 +137,7 @@ static void updateAccuracyChallenge(Challenge *c) percent /= battle.stats[STAT_SHOTS_FIRED]; percent *= 100; - if (percent >= c->targetValue) + if (percent >= c->value) { c->passed = 1; } @@ -151,7 +151,7 @@ static void updateArmourChallenge(Challenge *c) percent /= player->maxHealth; percent *= 100; - if (percent >= c->targetValue) + if (percent >= c->value) { c->passed = 1; } @@ -161,7 +161,7 @@ static void updateLossesChallenge(Challenge *c) { if (!c->passed) { - c->passed = battle.stats[STAT_ALLIES_KILLED] <= c->targetValue; + c->passed = battle.stats[STAT_ALLIES_KILLED] <= c->value; } } @@ -169,7 +169,7 @@ static void updatePlayerKillsChallenge(Challenge *c) { if (!c->passed) { - c->passed = battle.stats[STAT_ENEMIES_KILLED_PLAYER] >= c->targetValue; + c->passed = battle.stats[STAT_ENEMIES_KILLED_PLAYER] >= c->value; } } @@ -177,24 +177,24 @@ static void updateDisabledChallenge(Challenge *c) { if (!c->passed) { - c->passed = battle.stats[STAT_ENEMIES_DISABLED] >= c->targetValue; + c->passed = battle.stats[STAT_ENEMIES_DISABLED] >= c->value; } } char *getChallengeDescription(Challenge *c) { - return getFormattedChallengeDescription(challengeDescription[c->type], c->targetValue); + return getFormattedChallengeDescription(challengeDescription[c->type], c->value); } -Challenge *getChallenge(Mission *mission, int type) +Challenge *getChallenge(Mission *mission, int type, int value) { - Challenge *challenge; + Challenge *c; - for (challenge = mission->challengeHead.next ; challenge != NULL ; challenge = challenge->next) + for (c = mission->challengeHead.next ; c != NULL ; c = c->next) { - if (challenge->type == type) + if (c->type == type && c->value == value) { - return challenge; + return c; } } diff --git a/src/battle/challenges.h b/src/challenges/challenges.h similarity index 100% rename from src/battle/challenges.h rename to src/challenges/challenges.h diff --git a/src/galaxy/galacticMap.c b/src/galaxy/galacticMap.c index dbf530b..8021fd9 100644 --- a/src/galaxy/galacticMap.c +++ b/src/galaxy/galacticMap.c @@ -48,7 +48,6 @@ static void updatePandoranAdvance(void); static void fallenOK(void); static StarSystem *selectedStarSystem; -static Mission *selectedMission = {0}; static SDL_Texture *background; static SDL_Texture *starSystemTexture; static SDL_Texture *arrowTexture; @@ -263,12 +262,12 @@ static void doStarSystemView(void) { if (mission->available && app.mouse.button[SDL_BUTTON_LEFT] && collision(app.mouse.x - app.mouse.w / 2, app.mouse.y - app.mouse.h / 2, app.mouse.w, app.mouse.h, mission->rect.x, mission->rect.y, mission->rect.w, mission->rect.h)) { - if (selectedMission != mission) + if (game.currentMission != mission) { playSound(SND_GUI_CLICK); } - selectedMission = mission; + game.currentMission = mission; return; } } @@ -500,7 +499,7 @@ static void selectStarSystem(void) { show = SHOW_STAR_SYSTEM; STRNCPY(game.selectedStarSystem, selectedStarSystem->name, MAX_NAME_LENGTH); - selectedMission = selectedStarSystem->missionHead.next; + game.currentMission = selectedStarSystem->missionHead.next; playSound(SND_GUI_SELECT); } } @@ -542,7 +541,7 @@ static void drawStarSystemDetail(void) mission->rect.w = 300; mission->rect.h = 40; - if (mission == selectedMission) + if (mission == game.currentMission) { SDL_SetRenderDrawColor(app.renderer, 32, 64, 128, 255); SDL_RenderFillRect(app.renderer, &mission->rect); @@ -559,27 +558,27 @@ static void drawStarSystemDetail(void) y += 50; } - if (selectedMission->available) + if (game.currentMission->available) { - drawText(525, 135, 18, TA_LEFT, colors.lightGrey, "Pilot: %s", selectedMission->pilot); - drawText(525, 160, 18, TA_LEFT, colors.lightGrey, "Craft: %s", selectedMission->craft); - drawText(525, 185, 18, TA_LEFT, colors.lightGrey, "Squadron: %s", selectedMission->squadron); + drawText(525, 135, 18, TA_LEFT, colors.lightGrey, "Pilot: %s", game.currentMission->pilot); + drawText(525, 160, 18, TA_LEFT, colors.lightGrey, "Craft: %s", game.currentMission->craft); + drawText(525, 185, 18, TA_LEFT, colors.lightGrey, "Squadron: %s", game.currentMission->squadron); limitTextWidth(500); - drawText(525, 230, 22, TA_LEFT, colors.white, selectedMission->description); + drawText(525, 230, 22, TA_LEFT, colors.white, game.currentMission->description); limitTextWidth(0); } - if (selectedMission->completed) + if (game.currentMission->completed) { drawText(525, SCREEN_HEIGHT - 95, 18, TA_LEFT, colors.green, "This mission has been completed."); } - else if (selectedMission->epic) + else if (game.currentMission->epic) { drawText(525, SCREEN_HEIGHT - 95, 18, TA_LEFT, colors.yellow, "Note: this is an Epic Mission."); } - startMissionButton->enabled = (!selectedMission->completed || selectedStarSystem->isSol); + startMissionButton->enabled = (!game.currentMission->completed || selectedStarSystem->isSol); drawWidgets("starSystem"); } @@ -646,8 +645,8 @@ static void handleMouse(void) static void startMission(void) { initBattle(); - game.currentMission = selectedMission; - loadMission(selectedMission->filename); + + loadMission(game.currentMission->filename); } static void drawMenu(void) diff --git a/src/galaxy/mission.c b/src/galaxy/mission.c index 784ca1f..bfbdde2 100644 --- a/src/galaxy/mission.c +++ b/src/galaxy/mission.c @@ -84,7 +84,7 @@ Mission *loadMissionMeta(char *filename) memset(challenge, 0, sizeof(Challenge)); challenge->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); - challenge->targetValue = cJSON_GetObjectItem(node, "targetValue")->valueint; + challenge->value = cJSON_GetObjectItem(node, "value")->valueint; challengeTail->next = challenge; challengeTail = challenge; @@ -817,6 +817,7 @@ Mission *getMission(char *filename) StarSystem *starSystem; Mission *mission; + /* First, search the star systems */ for (starSystem = game.starSystemHead.next ; starSystem != NULL ; starSystem = starSystem->next) { for (mission = starSystem->missionHead.next ; mission != NULL ; mission = mission->next) @@ -828,6 +829,17 @@ Mission *getMission(char *filename) } } + /* now search the challenges */ + for (mission = game.challengeMissionHead.next ; mission != NULL ; mission = mission->next) + { + if (strcmp(mission->filename, filename) == 0) + { + return mission; + } + } + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "No such mission '%s'", filename); + return NULL; } diff --git a/src/galaxy/starSystems.c b/src/galaxy/starSystems.c index 3576208..10a9903 100644 --- a/src/galaxy/starSystems.c +++ b/src/galaxy/starSystems.c @@ -68,7 +68,6 @@ static StarSystem *loadStarSystem(cJSON *starSystemJSON) } starSystem->missionHead.completed = 1; - starSystem->missionTail = &starSystem->missionHead; loadMissions(starSystem); @@ -84,6 +83,9 @@ static void loadMissions(StarSystem *starSystem) char name[MAX_NAME_LENGTH]; char path[MAX_FILENAME_LENGTH]; char **filenames; + Mission *mission, *tail; + + tail = &starSystem->missionHead; STRNCPY(name, starSystem->name, MAX_NAME_LENGTH); @@ -100,7 +102,9 @@ static void loadMissions(StarSystem *starSystem) { sprintf(path, "data/missions/%s/%s", name, filenames[i]); - loadMissionMeta(path); + mission = loadMissionMeta(path); + tail->next = mission; + tail = mission; free(filenames[i]); } diff --git a/src/game/game.c b/src/game/game.c index f9dddd4..168fe97 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -27,30 +27,6 @@ void initGame(void) STRNCPY(game.selectedStarSystem, "Sol", MAX_NAME_LENGTH); } -void resetGame(void) -{ - StarSystem *starSystem; - Mission *mission; - Challenge *challenge; - - memset(&game.stats, 0, sizeof(int) * STAT_MAX); - - for (starSystem = game.starSystemHead.next ; starSystem != NULL ; starSystem = starSystem->next) - { - for (mission = starSystem->missionHead.next ; mission != NULL ; mission = mission->next) - { - mission->completed = 0; - - for (challenge = mission->challengeHead.next ; challenge != NULL ; challenge = challenge->next) - { - challenge->passed = 0; - } - } - } - - STRNCPY(game.selectedStarSystem, "Sol", MAX_NAME_LENGTH); -} - void destroyGame(void) { } diff --git a/src/game/title.c b/src/game/title.c index 1425f72..90cb75f 100644 --- a/src/game/title.c +++ b/src/game/title.c @@ -81,7 +81,7 @@ void initTitle(void) endSectionTransition(); - SDL_WarpMouseInWindow(app.window, SCREEN_WIDTH / 2, SCREEN_HEIGHT - 100); + setMouse(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); playMusic("music/Rise of spirit.ogg"); } @@ -199,20 +199,12 @@ static void handleKeyboard(void) static void campaign(void) { - if (fileExists(getSaveFilePath("game.save"))) - { - loadGame(); - } - else - { - resetGame(); - } - initGalacticMap(); } static void challenges(void) { + initChallengeHome(); } static void options(void) diff --git a/src/game/title.h b/src/game/title.h index 5132c1c..599fa60 100644 --- a/src/game/title.h +++ b/src/game/title.h @@ -40,20 +40,17 @@ extern void drawWidgets(char *groupName); extern void doWidgets(void); extern Widget *getWidget(const char *name, const char *group); extern void selectWidget(const char *name, const char *group); -extern void loadGame(void); -extern int fileExists(char *filename); -extern void resetGame(void); extern void initGalacticMap(void); extern void initOptions(void (*returnFromOptions)(void)); extern void drawOptions(void); -extern char *getSaveFilePath(char *filename); extern void playMusic(char *filename); extern void destroyBattle(void); extern void playSound(int id); extern void initEffects(void); +extern void setMouse(int x, int y); +extern void initChallengeHome(void); extern App app; extern Battle battle; extern Colors colors; -extern Game game; extern Entity *self; diff --git a/src/main.c b/src/main.c index 56a515b..85ec3a3 100644 --- a/src/main.c +++ b/src/main.c @@ -177,6 +177,11 @@ static void handleArguments(int argc, char *argv[]) if (!testingMission) { + if (fileExists(getSaveFilePath("game.save"))) + { + loadGame(); + } + initTitle(); } } diff --git a/src/main.h b/src/main.h index 470ef63..f12c91f 100644 --- a/src/main.h +++ b/src/main.h @@ -41,6 +41,9 @@ extern void prepareScene(void); extern void presentScene(void); extern void doModalDialog(void); extern void drawModalDialog(void); +extern void loadGame(void); +extern int fileExists(char *filename); +extern char *getSaveFilePath(char *filename); App app; Colors colors; diff --git a/src/structs.h b/src/structs.h index b07dde5..b072bb0 100644 --- a/src/structs.h +++ b/src/structs.h @@ -235,7 +235,7 @@ struct Objective { struct Challenge { int type; - int targetValue; + int value; int passed; Challenge *next; }; @@ -251,6 +251,8 @@ struct Mission { int available; int completed; int epic; + int completedChallenges; + int totalChallenges; SDL_Rect rect; Challenge challengeHead; Mission *next; @@ -267,7 +269,7 @@ struct StarSystem { int availableMissions; int fallsToPandorans; int isSol; - Mission missionHead, *missionTail; + Mission missionHead; StarSystem *next; }; diff --git a/src/system/load.c b/src/system/load.c index b7ac93a..9ca5887 100644 --- a/src/system/load.c +++ b/src/system/load.c @@ -23,7 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void loadStats(cJSON *stats); static void loadStarSystems(cJSON *starSystemsJSON); static void loadMissions(cJSON *missionsCJSON); -static void loadChallenges(Mission *mission, cJSON *challengesCJSON); +static void loadChallenges(cJSON *challengesCJSON); void loadGame(void) { @@ -39,6 +39,8 @@ void loadGame(void) loadStarSystems(cJSON_GetObjectItem(gameJSON, "starSystems")); + loadChallenges(cJSON_GetObjectItem(gameJSON, "challenges")); + loadStats(cJSON_GetObjectItem(gameJSON, "stats")); cJSON_Delete(root); @@ -60,40 +62,41 @@ static void loadStarSystems(cJSON *starSystemsJSON) } } -static void loadMissions(cJSON *missionsCJSON) +static void loadMissions(cJSON *missionsJSON) { Mission *mission; - cJSON *missionCJSON; + cJSON *missionJSON; - for (missionCJSON = missionsCJSON->child ; missionCJSON != NULL ; missionCJSON = missionCJSON->next) + for (missionJSON = missionsJSON->child ; missionJSON != NULL ; missionJSON = missionJSON->next) { - mission = getMission(cJSON_GetObjectItem(missionCJSON, "filename")->valuestring); - - mission->completed = cJSON_GetObjectItem(missionCJSON, "completed")->valueint; - - if (cJSON_GetObjectItem(missionCJSON, "challenges")) - { - loadChallenges(mission, cJSON_GetObjectItem(missionCJSON, "challenges")); - } + mission = getMission(cJSON_GetObjectItem(missionJSON, "filename")->valuestring); + mission->completed = cJSON_GetObjectItem(missionJSON, "completed")->valueint; } } -static void loadChallenges(Mission *mission, cJSON *challengesCJSON) +static void loadChallenges(cJSON *missionsJSON) { + Mission *mission; Challenge *challenge; - cJSON *challengeCJSON; + cJSON *missionJSON, *challengeJSON; + int type, value; - for (challengeCJSON = challengesCJSON->child ; challengeCJSON != NULL ; challengeCJSON = challengeCJSON->next) + if (missionsJSON) { - challenge = getChallenge(mission, lookup(cJSON_GetObjectItem(challengeCJSON, "type")->valuestring)); - - if (!challenge) + for (missionJSON = missionsJSON->child ; missionJSON != NULL ; missionJSON = missionJSON->next) { - printf("Couldn't find challenge to update\n"); - continue; + mission = getMission(cJSON_GetObjectItem(missionJSON, "filename")->valuestring); + + 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; + + challenge = getChallenge(mission, type, value); + + challenge->passed = cJSON_GetObjectItem(challengeJSON, "passed")->valueint; + } } - - challenge->passed = cJSON_GetObjectItem(challengeCJSON, "passed")->valueint; } } diff --git a/src/system/load.h b/src/system/load.h index 84ea951..b0543fe 100644 --- a/src/system/load.h +++ b/src/system/load.h @@ -24,7 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern char *readFile(char *filename); extern Mission *getMission(char *filename); -extern Challenge *getChallenge(Mission *mission, int type); +extern Challenge *getChallenge(Mission *mission, int type, int value); extern int lookup(char *lookup); extern char *getSaveFilePath(char *filename); extern char *getLookupName(char *prefix, long num); diff --git a/src/system/save.c b/src/system/save.c index cb4b7e3..e6cc574 100644 --- a/src/system/save.c +++ b/src/system/save.c @@ -21,8 +21,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "save.h" static void saveStarSystems(cJSON *gameJSON); +static void saveChallenges(cJSON *gameJSON); static cJSON *getMissionsJSON(StarSystem *starSystem); -static cJSON *getChallengesJSON(Mission *mission); static void saveStats(cJSON *gameJSON); void saveGame(void) @@ -39,6 +39,8 @@ void saveGame(void) cJSON_AddStringToObject(gameJSON, "selectedStarSystem", game.selectedStarSystem); saveStarSystems(gameJSON); + + saveChallenges(gameJSON); saveStats(gameJSON); @@ -85,36 +87,44 @@ static cJSON *getMissionsJSON(StarSystem *starSystem) cJSON_AddStringToObject(missionJSON, "filename", mission->filename); cJSON_AddNumberToObject(missionJSON, "completed", mission->completed); - if (mission->challengeHead.next) - { - cJSON_AddItemToObject(missionJSON, "challenges", getChallengesJSON(mission)); - } - cJSON_AddItemToArray(missionsJSON, missionJSON); } return missionsJSON; } -static cJSON *getChallengesJSON(Mission *mission) +static void saveChallenges(cJSON *gameJSON) { - cJSON *challengesJSON, *challengeJSON; - Challenge *challenge; + Mission *mission; + Challenge *c; + cJSON *missionsJSON, *missionJSON, *challengesJSON, *challengeJSON; - challengesJSON = cJSON_CreateArray(); + missionsJSON = cJSON_CreateArray(); - for (challenge = mission->challengeHead.next ; challenge != NULL ; challenge = challenge->next) + for (mission = game.challengeMissionHead.next ; mission != NULL ; mission = mission->next) { - challengeJSON = cJSON_CreateObject(); + missionJSON = cJSON_CreateObject(); - cJSON_AddStringToObject(challengeJSON, "type", getLookupName("CHALLENGE_", challenge->type)); - cJSON_AddNumberToObject(challengeJSON, "targetValue", challenge->targetValue); - cJSON_AddNumberToObject(challengeJSON, "passed", challenge->passed); + cJSON_AddStringToObject(missionJSON, "filename", mission->filename); - cJSON_AddItemToArray(challengesJSON, challengeJSON); + challengesJSON = cJSON_CreateArray(); + + for (c = mission->challengeHead.next ; c != NULL ; c = c->next) + { + challengeJSON = cJSON_CreateObject(); + cJSON_AddStringToObject(challengeJSON, "type", getLookupName("CHALLENGE_", c->type)); + cJSON_AddNumberToObject(challengeJSON, "value", c->value); + cJSON_AddNumberToObject(challengeJSON, "passed", c->passed); + + cJSON_AddItemToArray(challengesJSON, challengeJSON); + } + + cJSON_AddItemToObject(missionJSON, "challenges", challengesJSON); + + cJSON_AddItemToArray(missionsJSON, missionJSON); } - return challengesJSON; + cJSON_AddItemToObject(gameJSON, "challenges", missionsJSON); } static void saveStats(cJSON *gameJSON) diff --git a/src/test/testMission.c b/src/test/testMission.c index d2968a4..a8abbe8 100644 --- a/src/test/testMission.c +++ b/src/test/testMission.c @@ -63,7 +63,7 @@ static void loadChallenges(char *filename) memset(challenge, 0, sizeof(Challenge)); challenge->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); - challenge->targetValue = cJSON_GetObjectItem(node, "targetValue")->valueint; + challenge->value = cJSON_GetObjectItem(node, "value")->valueint; challengeTail->next = challenge; challengeTail = challenge;