From 266088f764e6ed17b95d544d6672654316916c65 Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 27 Feb 2016 12:14:29 +0000 Subject: [PATCH] Start of restoring challenges. --- data/challenges/01.json | 48 ++++++++++++++++++++++ data/widgets/title.json | 8 ++-- src/battle/battle.c | 14 +++---- src/battle/challenges.c | 25 ++++++++++++ src/battle/challenges.h | 3 ++ src/battle/hud.c | 9 ++++- src/battle/missionInfo.c | 85 ++++++++++++++++++++------------------- src/galaxy/mission.c | 70 ++++++++++++++++++++++++++++++++ src/galaxy/starSystems.c | 86 +++++----------------------------------- src/galaxy/starSystems.h | 1 + src/game/game.c | 2 - src/game/title.c | 35 +++++++--------- src/system/init.c | 35 +++++++++------- src/system/init.h | 1 + src/test/testMission.c | 3 +- 15 files changed, 258 insertions(+), 167 deletions(-) create mode 100644 data/challenges/01.json diff --git a/data/challenges/01.json b/data/challenges/01.json new file mode 100644 index 0000000..16d9d88 --- /dev/null +++ b/data/challenges/01.json @@ -0,0 +1,48 @@ +{ + "name" : "Destroy all the Darts", + "description" : "Destroy all the Darts", + "background" : "gfx/backgrounds/background03.jpg", + "planet" : "gfx/planets/spirit.png", + "music" : "", + "player" : { + "type" : "Nymph", + "side" : "SIDE_ALLIES", + "pilot" : "-", + "squadron" : "-", + "x" : 25, + "y" : 25 + }, + "objectives" : [ + { + "description" : "Destroy Dart", + "targetName" : "Dart", + "targetValue" : 3, + "targetType" : "TT_DESTROY" + } + ], + "challenges" : [ + { + "type" : "CHALLENGE_TIME", + "targetValue" : 10 + }, + { + "type" : "CHALLENGE_TIME", + "targetValue" : 15 + }, + { + "type" : "CHALLENGE_TIME", + "targetValue" : 20 + } + ], + "fighters" : [ + { + "name" : "Dart", + "types" : "Dart", + "side" : "SIDE_PIRATE", + "x" : 25, + "y" : 22, + "number" : 3, + "scatter" : 1000 + } + ] +} diff --git a/data/widgets/title.json b/data/widgets/title.json index 1712fd3..54668d7 100644 --- a/data/widgets/title.json +++ b/data/widgets/title.json @@ -1,19 +1,19 @@ [ { - "name" : "newGame", + "name" : "campaign", "group" : "title", "type" : "WT_BUTTON", - "text" : "New Game", + "text" : "Campaign", "x" : -1, "y" : 250, "w" : 200, "h": 34 }, { - "name" : "continue", + "name" : "challenges", "group" : "title", "type" : "WT_BUTTON", - "text" : "Continue", + "text" : "Challenges", "x" : -1, "y" : 350, "w" : 200, diff --git a/src/battle/battle.c b/src/battle/battle.c index e31740f..8a80743 100644 --- a/src/battle/battle.c +++ b/src/battle/battle.c @@ -160,6 +160,13 @@ static void doBattle(void) if (battle.status == MS_IN_PROGRESS) { doScript(); + + battle.stats[STAT_TIME]++; + + if (battle.stats[STAT_TIME] % FPS == 0) + { + runScriptFunction("TIME %d", battle.stats[STAT_TIME] / 60); + } } } @@ -168,13 +175,6 @@ static void doBattle(void) battle.missionFinishedTimer--; } - if (battle.stats[STAT_TIME] % FPS == 0) - { - runScriptFunction("TIME %d", battle.stats[STAT_TIME] / 60); - } - - battle.stats[STAT_TIME]++; - if (battle.unwinnable && battle.missionFinishedTimer <= -FPS * 6) { postBattle(); diff --git a/src/battle/challenges.c b/src/battle/challenges.c index 212182b..77b97eb 100644 --- a/src/battle/challenges.c +++ b/src/battle/challenges.c @@ -43,6 +43,31 @@ static char *challengeDescription[] = { "Finish mission in %d minutes or less" }; +void initChallenges(void) +{ + Mission *mission, *tail; + char **filenames; + char path[MAX_FILENAME_LENGTH]; + int count, i; + + tail = &game.challengeMissionHead; + + filenames = getFileList("data/challenges", &count); + + for (i = 0 ; i < count ; i++) + { + sprintf(path, "data/challenges/%s", filenames[i]); + + mission = loadMissionMeta(path); + tail->next = mission; + tail = mission; + + free(filenames[i]); + } + + free(filenames); +} + void updateChallenges(void) { Challenge *c; diff --git a/src/battle/challenges.h b/src/battle/challenges.h index a5c230f..a078c38 100644 --- a/src/battle/challenges.h +++ b/src/battle/challenges.h @@ -20,6 +20,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../common.h" +extern Mission *loadMissionMeta(char *filename); +extern char **getFileList(char *dir, int *count); + extern Battle battle; extern Entity *player; extern Game game; diff --git a/src/battle/hud.c b/src/battle/hud.c index d5c2921..e728b0e 100644 --- a/src/battle/hud.c +++ b/src/battle/hud.c @@ -394,7 +394,14 @@ static void drawNumFighters(void) static void drawObjectives(void) { - drawText(SCREEN_WIDTH / 2, 10, 16, TA_CENTER, colors.white, "%d / %d", battle.numObjectivesComplete, battle.numObjectivesTotal); + if (!battle.isChallenge) + { + drawText(SCREEN_WIDTH / 2, 10, 16, TA_CENTER, colors.white, "%d / %d", battle.numObjectivesComplete, battle.numObjectivesTotal); + } + else + { + drawText(SCREEN_WIDTH / 2, 10, 16, TA_CENTER, colors.white, "%d", battle.stats[STAT_TIME] / FPS); + } } static float distanceToKM(int x1, int y1, int x2, int y2) diff --git a/src/battle/missionInfo.c b/src/battle/missionInfo.c index 38f8a9e..5a6d898 100644 --- a/src/battle/missionInfo.c +++ b/src/battle/missionInfo.c @@ -85,54 +85,57 @@ static void drawMissionSummary(SDL_Texture *header) y = 215; - drawText(SCREEN_WIDTH / 2, 215, 28, TA_CENTER, colors.white, "OBJECTIVES"); - - y += 10; - - for (o = battle.objectiveHead.next ; o != NULL ; o = o->next) + if (!battle.isChallenge) { - if (o->active) + drawText(SCREEN_WIDTH / 2, y, 28, TA_CENTER, colors.white, "OBJECTIVES"); + + y += 10; + + for (o = battle.objectiveHead.next ; o != NULL ; o = o->next) + { + if (o->active) + { + y += 50; + + switch (o->status) + { + case OS_INCOMPLETE: + color = colors.white; + break; + + case OS_COMPLETE: + color = colors.green; + break; + + case OS_FAILED: + color = colors.red; + break; + } + + drawText(SCREEN_WIDTH / 2 - 100, y, 22, TA_RIGHT, colors.white, o->description); + if (o->targetValue > 1 && !o->isCondition) + { + drawText(SCREEN_WIDTH / 2, y, 22, TA_CENTER, colors.white, "%d / %d", o->currentValue, o->targetValue); + } + drawText(SCREEN_WIDTH / 2 + 100, y, 22, TA_LEFT, color, objectiveStatus[o->status]); + } + } + + if (!battle.objectiveHead.next) { y += 50; - switch (o->status) - { - case OS_INCOMPLETE: - color = colors.white; - break; - - case OS_COMPLETE: - color = colors.green; - break; - - case OS_FAILED: - color = colors.red; - break; - } - - drawText(SCREEN_WIDTH / 2 - 100, y, 22, TA_RIGHT, colors.white, o->description); - if (o->targetValue > 1 && !o->isCondition) - { - drawText(SCREEN_WIDTH / 2, y, 22, TA_CENTER, colors.white, "%d / %d", o->currentValue, o->targetValue); - } - drawText(SCREEN_WIDTH / 2 + 100, y, 22, TA_LEFT, color, objectiveStatus[o->status]); + drawText(SCREEN_WIDTH / 2, y, 22, TA_CENTER, colors.white, "(none)"); } + + y += 75; } - if (!battle.objectiveHead.next) + if (battle.isChallenge) { - y += 50; + drawText(SCREEN_WIDTH / 2, y, 24, TA_CENTER, colors.white, game.currentMission->description); - drawText(SCREEN_WIDTH / 2, y, 22, TA_CENTER, colors.white, "(none)"); - } - - if (game.currentMission && game.currentMission->challengeHead.next) - { - y += 100; - - drawText(SCREEN_WIDTH / 2, y, 28, TA_CENTER, colors.white, "CHALLENGES"); - - y += 10; + y += 25; for (c = game.currentMission->challengeHead.next ; c != NULL ; c = c->next) { @@ -155,8 +158,8 @@ static void drawMissionSummary(SDL_Texture *header) challengeStatus = "Failed"; } - drawText(SCREEN_WIDTH / 2 - 25, y, 22, TA_RIGHT, colors.white, "%s", getChallengeDescription(c)); - drawText(SCREEN_WIDTH / 2 + 25, y, 22, TA_LEFT, color, challengeStatus); + drawText(SCREEN_WIDTH / 2 - 50, y, 22, TA_RIGHT, colors.white, "%s", getChallengeDescription(c)); + drawText(SCREEN_WIDTH / 2 + 50, y, 22, TA_LEFT, color, challengeStatus); } } } diff --git a/src/galaxy/mission.c b/src/galaxy/mission.c index 4205f0f..784ca1f 100644 --- a/src/galaxy/mission.c +++ b/src/galaxy/mission.c @@ -31,6 +31,74 @@ static unsigned long hashcode(const char *str); static char **toTypeArray(char *types, int *numTypes); static void loadEpicData(cJSON *node); +Mission *loadMissionMeta(char *filename) +{ + Mission *mission; + Challenge *challenge, *challengeTail; + cJSON *root, *node; + char *text; + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename); + + text = readFile(getFileLocation(filename)); + + root = cJSON_Parse(text); + + mission = malloc(sizeof(Mission)); + memset(mission, 0, sizeof(Mission)); + + STRNCPY(mission->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); + STRNCPY(mission->description, cJSON_GetObjectItem(root, "description")->valuestring, MAX_DESCRIPTION_LENGTH); + STRNCPY(mission->filename, filename, MAX_DESCRIPTION_LENGTH); + + if (cJSON_GetObjectItem(root, "requires")) + { + mission->requires = cJSON_GetObjectItem(root, "requires")->valueint; + } + + if (cJSON_GetObjectItem(root, "epic")) + { + mission->epic = 1; + } + + node = cJSON_GetObjectItem(root, "player"); + + if (node) + { + STRNCPY(mission->pilot, cJSON_GetObjectItem(node, "pilot")->valuestring, MAX_NAME_LENGTH); + STRNCPY(mission->squadron, cJSON_GetObjectItem(node, "squadron")->valuestring, MAX_NAME_LENGTH); + STRNCPY(mission->craft, cJSON_GetObjectItem(node, "type")->valuestring, MAX_NAME_LENGTH); + } + + challengeTail = &mission->challengeHead; + + node = cJSON_GetObjectItem(root, "challenges"); + + if (node) + { + node = node->child; + + while (node) + { + challenge = malloc(sizeof(Challenge)); + memset(challenge, 0, sizeof(Challenge)); + + challenge->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); + challenge->targetValue = cJSON_GetObjectItem(node, "targetValue")->valueint; + + challengeTail->next = challenge; + challengeTail = challenge; + + node = node->next; + } + } + + cJSON_Delete(root); + free(text); + + return mission; +} + void loadMission(char *filename) { cJSON *root; @@ -113,6 +181,8 @@ void loadMission(char *filename) initPlayer(); + battle.isChallenge = game.currentMission->challengeHead.next != NULL; + playMusic(music); } diff --git a/src/galaxy/starSystems.c b/src/galaxy/starSystems.c index 03ae06b..3576208 100644 --- a/src/galaxy/starSystems.c +++ b/src/galaxy/starSystems.c @@ -21,34 +21,36 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "starSystems.h" static void loadMissions(StarSystem *starSystem); -static void loadStarSystem(cJSON *starSystemJSON); -static void loadMissionMeta(char *filename, StarSystem *starSystem); +static StarSystem *loadStarSystem(cJSON *starSystemJSON); void initStarSystems(void) { cJSON *root, *node; char *text; + StarSystem *starSystem, *tail; + + tail = &game.starSystemHead; text = readFile(getFileLocation("data/galaxy/starSystems.json")); root = cJSON_Parse(text); for (node = cJSON_GetObjectItem(root, "starSystems")->child ; node != NULL ; node = node->next) { - loadStarSystem(node); + starSystem = loadStarSystem(node); + tail->next = starSystem; + tail = starSystem; } cJSON_Delete(root); free(text); } -static void loadStarSystem(cJSON *starSystemJSON) +static StarSystem *loadStarSystem(cJSON *starSystemJSON) { StarSystem *starSystem; starSystem = malloc(sizeof(StarSystem)); memset(starSystem, 0, sizeof(StarSystem)); - game.starSystemTail->next = starSystem; - game.starSystemTail = starSystem; STRNCPY(starSystem->name, cJSON_GetObjectItem(starSystemJSON, "name")->valuestring, MAX_NAME_LENGTH); starSystem->side = lookup(cJSON_GetObjectItem(starSystemJSON, "side")->valuestring); @@ -72,6 +74,8 @@ static void loadStarSystem(cJSON *starSystemJSON) starSystem->x *= 3; starSystem->y *= 3; + + return starSystem; } static void loadMissions(StarSystem *starSystem) @@ -96,7 +100,7 @@ static void loadMissions(StarSystem *starSystem) { sprintf(path, "data/missions/%s/%s", name, filenames[i]); - loadMissionMeta(path, starSystem); + loadMissionMeta(path); free(filenames[i]); } @@ -104,74 +108,6 @@ static void loadMissions(StarSystem *starSystem) free(filenames); } -static void loadMissionMeta(char *filename, StarSystem *starSystem) -{ - Mission *mission; - Challenge *challenge, *challengeTail; - cJSON *root, *node; - char *text; - - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename); - - text = readFile(getFileLocation(filename)); - - root = cJSON_Parse(text); - - mission = malloc(sizeof(Mission)); - memset(mission, 0, sizeof(Mission)); - starSystem->missionTail->next = mission; - starSystem->missionTail = mission; - - STRNCPY(mission->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); - STRNCPY(mission->description, cJSON_GetObjectItem(root, "description")->valuestring, MAX_DESCRIPTION_LENGTH); - STRNCPY(mission->filename, filename, MAX_DESCRIPTION_LENGTH); - - if (cJSON_GetObjectItem(root, "requires")) - { - mission->requires = cJSON_GetObjectItem(root, "requires")->valueint; - } - - if (cJSON_GetObjectItem(root, "epic")) - { - mission->epic = 1; - } - - node = cJSON_GetObjectItem(root, "player"); - - if (node) - { - STRNCPY(mission->pilot, cJSON_GetObjectItem(node, "pilot")->valuestring, MAX_NAME_LENGTH); - STRNCPY(mission->squadron, cJSON_GetObjectItem(node, "squadron")->valuestring, MAX_NAME_LENGTH); - STRNCPY(mission->craft, cJSON_GetObjectItem(node, "type")->valuestring, MAX_NAME_LENGTH); - } - - challengeTail = &mission->challengeHead; - - node = cJSON_GetObjectItem(root, "challenges"); - - if (node) - { - node = node->child; - - while (node) - { - challenge = malloc(sizeof(Challenge)); - memset(challenge, 0, sizeof(Challenge)); - - challenge->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); - challenge->targetValue = cJSON_GetObjectItem(node, "targetValue")->valueint; - - challengeTail->next = challenge; - challengeTail = challenge; - - node = node->next; - } - } - - cJSON_Delete(root); - free(text); -} - StarSystem *getStarSystem(char *name) { StarSystem *starSystem; diff --git a/src/galaxy/starSystems.h b/src/galaxy/starSystems.h index 37574ba..1bd7f51 100644 --- a/src/galaxy/starSystems.h +++ b/src/galaxy/starSystems.h @@ -27,5 +27,6 @@ extern long lookup(char *name); extern int isMissionAvailable(Mission *mission, Mission *prev); extern char *getFileLocation(char *filename); extern char **getFileList(char *dir, int *count); +extern Mission *loadMissionMeta(char *filename); extern Game game; diff --git a/src/game/game.c b/src/game/game.c index c00f69a..f9dddd4 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -24,8 +24,6 @@ void initGame(void) { memset(&game, 0, sizeof(Game)); - game.starSystemTail = &game.starSystemHead; - STRNCPY(game.selectedStarSystem, "Sol", MAX_NAME_LENGTH); } diff --git a/src/game/title.c b/src/game/title.c index ec7aac5..1425f72 100644 --- a/src/game/title.c +++ b/src/game/title.c @@ -26,8 +26,8 @@ static void handleKeyboard(void); static void initFighters(void); static void doFighters(void); static void drawFighters(void); -static void newGame(void); -static void continueGame(void); +static void campaign(void); +static void challenges(void); static void options(void); static void quit(void); static void returnFromOptions(void); @@ -72,19 +72,8 @@ void initTitle(void) initFighters(); - if (fileExists(getSaveFilePath("game.save"))) - { - selectWidget("continue", "title"); - } - else - { - getWidget("continue", "title")->enabled = 0; - - selectWidget("newGame", "title"); - } - - getWidget("newGame", "title")->action = newGame; - getWidget("continue", "title")->action = continueGame; + getWidget("campaign", "title")->action = campaign; + getWidget("challenges", "title")->action = challenges; getWidget("options", "title")->action = options; getWidget("quit", "title")->action = quit; @@ -208,18 +197,22 @@ static void handleKeyboard(void) } } -static void newGame(void) +static void campaign(void) { - resetGame(); + if (fileExists(getSaveFilePath("game.save"))) + { + loadGame(); + } + else + { + resetGame(); + } initGalacticMap(); } -static void continueGame(void) +static void challenges(void) { - loadGame(); - - initGalacticMap(); } static void options(void) diff --git a/src/system/init.c b/src/system/init.c index f49f3ce..578f5a5 100644 --- a/src/system/init.c +++ b/src/system/init.c @@ -95,7 +95,8 @@ void initSDL(void) void initGameSystem(void) { - int STEPS = 13; + int step = 0; + int STEPS = 14; initColor(&colors.red, 255, 0, 0); initColor(&colors.orange, 255, 128, 0); @@ -109,59 +110,63 @@ void initGameSystem(void) initColor(&colors.lightGrey, 192, 192, 192); initColor(&colors.darkGrey, 128, 128, 128); - showLoadingStep(0, STEPS); + showLoadingStep(step++, STEPS); initFonts(); - showLoadingStep(1, STEPS); + showLoadingStep(step++, STEPS); initInput(); - showLoadingStep(2, STEPS); + showLoadingStep(step++, STEPS); initLookups(); - showLoadingStep(3, STEPS); + showLoadingStep(step++, STEPS); initSounds(); - showLoadingStep(4, STEPS); + showLoadingStep(step++, STEPS); initWidgets(); - showLoadingStep(5, STEPS); + showLoadingStep(step++, STEPS); initGame(); - showLoadingStep(6, STEPS); + showLoadingStep(step++, STEPS); loadFighterDefs(); - showLoadingStep(7, STEPS); + showLoadingStep(step++, STEPS); loadCapitalShipDefs(); - showLoadingStep(8, STEPS); + showLoadingStep(step++, STEPS); loadItemDefs(); - showLoadingStep(9, STEPS); + showLoadingStep(step++, STEPS); initBulletDefs(); - showLoadingStep(10, STEPS); + showLoadingStep(step++, STEPS); initStarSystems(); - showLoadingStep(11, STEPS); + showLoadingStep(step++, STEPS); + + initChallenges(); + + showLoadingStep(step++, STEPS); initBattle(); - showLoadingStep(12, STEPS); + showLoadingStep(step++, STEPS); initModalDialog(); - showLoadingStep(13, STEPS); + showLoadingStep(step++, STEPS); } /* diff --git a/src/system/init.h b/src/system/init.h index ac3c517..0d762f8 100644 --- a/src/system/init.h +++ b/src/system/init.h @@ -42,6 +42,7 @@ extern void initLookups(void); extern void initBattle(void); extern void initGame(void); extern void initStarSystems(void); +extern void initChallenges(void); extern void initWidgets(void); extern void destroyLookups(void); extern void destroyFonts(void); diff --git a/src/test/testMission.c b/src/test/testMission.c index 4554615..d2968a4 100644 --- a/src/test/testMission.c +++ b/src/test/testMission.c @@ -34,8 +34,9 @@ void loadTestMission(char *filename) initBattle(); - loadMission(filename); loadChallenges(filename); + + loadMission(filename); } static void loadChallenges(char *filename)