diff --git a/src/battle/missionInfo.c b/src/battle/missionInfo.c index c3c76fa..36e4043 100644 --- a/src/battle/missionInfo.c +++ b/src/battle/missionInfo.c @@ -148,6 +148,7 @@ static void drawObjectives(void) static void drawChallenges(void) { + int i; Challenge *c; char *challengeStatus; SDL_Color color; @@ -164,8 +165,10 @@ static void drawChallenges(void) y += 25; - for (c = game.currentMission->challengeData.challengeHead.next ; c != NULL ; c = c->next) + for (i = 0 ; i < MAX_CHALLENGES ; i++) { + c = mission->challengeData.challenges[i]; + y += 50; color = colors.white; diff --git a/src/challenges/challengeHome.c b/src/challenges/challengeHome.c index ca6ad14..18f7b70 100644 --- a/src/challenges/challengeHome.c +++ b/src/challenges/challengeHome.c @@ -45,7 +45,6 @@ static SDL_Texture *planetTexture; static PointF planet; static int totalChallenges; static int show; -static Challenge *challenge[3]; static char timeLimit[MAX_DESCRIPTION_LENGTH]; static char restrictions[MAX_DESCRIPTION_LENGTH]; static int hasRestrictions; @@ -179,20 +178,8 @@ static void doChallenges(void) static void updateChallengeMissionData(void) { - Challenge *c; - int i; - STRNCPY(timeLimit, timeToString(game.currentMission->challengeData.timeLimit, 0), MAX_DESCRIPTION_LENGTH); sprintf(restrictions, "%s", listRestrictions()); - - memset(challenge, 0, sizeof(Challenge*) * 3); - - i = 0; - - for (c = game.currentMission->challengeData.challengeHead.next ; c != NULL ; c = c->next) - { - challenge[i++] = c; - } } static void addRestriction(char *buffer, int restricted, char *description) @@ -262,7 +249,8 @@ static void draw(void) static void drawChallenges(void) { - Mission *c; + Mission *m; + Challenge *c; SDL_Rect r; int i, endIndex; @@ -273,16 +261,16 @@ static void drawChallenges(void) endIndex = startIndex + MAX_ITEMS; i = 0; - for (c = game.challengeMissionHead.next ; c != NULL ; c = c->next) + for (m = game.challengeMissionHead.next ; m != NULL ; m = m->next) { - c->rect = r; + m->rect = r; if (i >= startIndex && i <= endIndex) { SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 0); SDL_RenderFillRect(app.renderer, &r); - if (game.currentMission == c) + if (game.currentMission == m) { SDL_SetRenderDrawColor(app.renderer, 64, 128, 200, SDL_ALPHA_OPAQUE); SDL_RenderFillRect(app.renderer, &r); @@ -297,9 +285,9 @@ static void drawChallenges(void) drawText(r.x + (r.w / 2), r.y + 28, 30, TA_CENTER, colors.white, "%d", i + 1); - if (c->available) + if (m->available) { - 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); + drawText(r.x + (r.w / 2), r.y + r.w + 5, 18, TA_CENTER, (m->completedChallenges < m->totalChallenges) ? colors.white : colors.green, "%d / %d", m->completedChallenges, m->totalChallenges); } else { @@ -337,23 +325,26 @@ static void drawChallenges(void) drawText(SCREEN_WIDTH / 2, SCREEN_HEIGHT - r.y, 24, TA_CENTER, colors.white, game.currentMission->description); r.y -= 50; + c = game.currentMission->challengeData.challenges[0]; drawText((SCREEN_WIDTH / 2) - 25, SCREEN_HEIGHT - r.y, 18, TA_RIGHT, colors.white, _("Craft: %s"), game.currentMission->craft); - drawText((SCREEN_WIDTH / 2) + 25, SCREEN_HEIGHT - r.y, 18, TA_LEFT, (challenge[0]->passed) ? colors.green : colors.white, "1. %s", getChallengeDescription(challenge[0])); + drawText((SCREEN_WIDTH / 2) + 25, SCREEN_HEIGHT - r.y, 18, TA_LEFT, c ? colors.green : colors.white, "1. %s", getChallengeDescription(c)); r.y -= 30; drawText((SCREEN_WIDTH / 2) - 25, SCREEN_HEIGHT - r.y, 18, TA_RIGHT, colors.white, _("Time Limit: %s"), timeLimit); - if (challenge[1]) + c = game.currentMission->challengeData.challenges[1]; + if (c) { - drawText((SCREEN_WIDTH / 2) + 25, SCREEN_HEIGHT - r.y, 18, TA_LEFT, (challenge[1]->passed) ? colors.green : colors.white, "2. %s", getChallengeDescription(challenge[1])); + drawText((SCREEN_WIDTH / 2) + 25, SCREEN_HEIGHT - r.y, 18, TA_LEFT, (c->passed) ? colors.green : colors.white, "2. %s", getChallengeDescription(c)); } r.y -= 30; drawText((SCREEN_WIDTH / 2) - 25, SCREEN_HEIGHT - r.y, 18, TA_RIGHT, hasRestrictions ? colors.red : colors.white, _("Restrictions: %s"), restrictions); - if (challenge[2]) + c = game.currentMission->challengeData.challenges[2]; + if (c) { - drawText((SCREEN_WIDTH / 2) + 25, SCREEN_HEIGHT - r.y, 18, TA_LEFT, (challenge[2]->passed) ? colors.green : colors.white, "3. %s", getChallengeDescription(challenge[2])); + drawText((SCREEN_WIDTH / 2) + 25, SCREEN_HEIGHT - r.y, 18, TA_LEFT, (c) ? colors.green : colors.white, "3. %s", getChallengeDescription(c)); } } } diff --git a/src/challenges/challenges.c b/src/challenges/challenges.c index f8859ff..8f79fdf 100644 --- a/src/challenges/challenges.c +++ b/src/challenges/challenges.c @@ -94,10 +94,13 @@ void doChallenges(void) static void updateChallenges(void) { + int i; Challenge *c; - for (c = game.currentMission->challengeData.challengeHead.next ; c != NULL ; c = c->next) + for (i = 0 ; i < MAX_CHALLENGES ; i++) { + c = mission->challengeData.challenges[i]; + if (!c->passed) { switch (c->type) @@ -237,10 +240,13 @@ char *getChallengeDescription(Challenge *c) Challenge *getChallenge(Mission *mission, int type, int value) { + int i; Challenge *c; - for (c = mission->challengeData.challengeHead.next ; c != NULL ; c = c->next) + for (i = 0 ; i < MAX_CHALLENGES ; i++) { + c = mission->challengeData.challenges[i]; + if (c->type == type && c->value == value) { return c; @@ -265,6 +271,7 @@ static char *getFormattedChallengeDescription(const char *format, ...) void updateChallengeMissions(void) { + int i; Mission *m; Challenge *c; @@ -272,8 +279,10 @@ void updateChallengeMissions(void) { m->totalChallenges = m->completedChallenges = 0; - for (c = m->challengeData.challengeHead.next ; c != NULL ; c = c->next) + for (i = 0 ; i < MAX_CHALLENGES ; i++) { + c = mission->challengeData.challenges[i]; + m->totalChallenges++; if (c->passed) diff --git a/src/defs.h b/src/defs.h index 23f9b0a..685504c 100644 --- a/src/defs.h +++ b/src/defs.h @@ -55,6 +55,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define MAX_STARS 500 +#define MAX_CHALLENGES 3 + #define MAX_FONTS 32 #define NUM_TEXT_BUCKETS 64 diff --git a/src/galaxy/mission.c b/src/galaxy/mission.c index 69d0d61..5e247e1 100644 --- a/src/galaxy/mission.c +++ b/src/galaxy/mission.c @@ -35,8 +35,9 @@ static char *getAutoPlanet(char *filename); Mission *loadMissionMeta(char *filename) { + int i; Mission *mission; - Challenge *challenge, *challengeTail; + Challenge *challenge; cJSON *root, *node; char *text; @@ -72,8 +73,6 @@ Mission *loadMissionMeta(char *filename) STRNCPY(mission->craft, cJSON_GetObjectItem(node, "type")->valuestring, MAX_NAME_LENGTH); } - challengeTail = &mission->challengeData.challengeHead; - node = cJSON_GetObjectItem(root, "challenge"); if (node) @@ -92,7 +91,9 @@ Mission *loadMissionMeta(char *filename) { node = node->child; - while (node) + i = 0; + + while (node && i < MAX_CHALLENGES) { challenge = malloc(sizeof(Challenge)); memset(challenge, 0, sizeof(Challenge)); @@ -100,10 +101,11 @@ Mission *loadMissionMeta(char *filename) challenge->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); challenge->value = cJSON_GetObjectItem(node, "value")->valueint; - challengeTail->next = challenge; - challengeTail = challenge; + mission->challengeData.challenges[i] = challenge; node = node->next; + + i++; } } } diff --git a/src/structs.h b/src/structs.h index 0cbfa6d..cdc59f7 100644 --- a/src/structs.h +++ b/src/structs.h @@ -250,7 +250,7 @@ typedef struct { int noBoost; int noECM; int noGuns; - Challenge challengeHead; + Challenge *challenges[MAX_CHALLENGES]; } ChallengeData; struct Mission { diff --git a/src/system/save.c b/src/system/save.c index bbdb053..46afb55 100644 --- a/src/system/save.c +++ b/src/system/save.c @@ -95,6 +95,7 @@ static cJSON *getMissionsJSON(StarSystem *starSystem) static void saveChallenges(cJSON *gameJSON) { + int i; Mission *mission; Challenge *c; cJSON *missionsJSON, *missionJSON, *challengesJSON, *challengeJSON; @@ -109,8 +110,10 @@ static void saveChallenges(cJSON *gameJSON) challengesJSON = cJSON_CreateArray(); - for (c = mission->challengeData.challengeHead.next ; c != NULL ; c = c->next) + for (i = 0 ; i < MAX_CHALLENGES ; i++) { + c = mission->challengeData.challenges[i]; + challengeJSON = cJSON_CreateObject(); cJSON_AddStringToObject(challengeJSON, "type", getLookupName("CHALLENGE_", c->type)); cJSON_AddNumberToObject(challengeJSON, "value", c->value);