Start of restoring challenges.

This commit is contained in:
Steve 2016-02-27 12:14:29 +00:00
parent 57d8c867e1
commit 266088f764
15 changed files with 258 additions and 167 deletions

48
data/challenges/01.json Normal file
View File

@ -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
}
]
}

View File

@ -1,19 +1,19 @@
[ [
{ {
"name" : "newGame", "name" : "campaign",
"group" : "title", "group" : "title",
"type" : "WT_BUTTON", "type" : "WT_BUTTON",
"text" : "New Game", "text" : "Campaign",
"x" : -1, "x" : -1,
"y" : 250, "y" : 250,
"w" : 200, "w" : 200,
"h": 34 "h": 34
}, },
{ {
"name" : "continue", "name" : "challenges",
"group" : "title", "group" : "title",
"type" : "WT_BUTTON", "type" : "WT_BUTTON",
"text" : "Continue", "text" : "Challenges",
"x" : -1, "x" : -1,
"y" : 350, "y" : 350,
"w" : 200, "w" : 200,

View File

@ -160,6 +160,13 @@ static void doBattle(void)
if (battle.status == MS_IN_PROGRESS) if (battle.status == MS_IN_PROGRESS)
{ {
doScript(); 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--; 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) if (battle.unwinnable && battle.missionFinishedTimer <= -FPS * 6)
{ {
postBattle(); postBattle();

View File

@ -43,6 +43,31 @@ static char *challengeDescription[] = {
"Finish mission in %d minutes or less" "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) void updateChallenges(void)
{ {
Challenge *c; Challenge *c;

View File

@ -20,6 +20,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../common.h" #include "../common.h"
extern Mission *loadMissionMeta(char *filename);
extern char **getFileList(char *dir, int *count);
extern Battle battle; extern Battle battle;
extern Entity *player; extern Entity *player;
extern Game game; extern Game game;

View File

@ -394,7 +394,14 @@ static void drawNumFighters(void)
static void drawObjectives(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) static float distanceToKM(int x1, int y1, int x2, int y2)

View File

@ -85,54 +85,57 @@ static void drawMissionSummary(SDL_Texture *header)
y = 215; y = 215;
drawText(SCREEN_WIDTH / 2, 215, 28, TA_CENTER, colors.white, "OBJECTIVES"); if (!battle.isChallenge)
y += 10;
for (o = battle.objectiveHead.next ; o != NULL ; o = o->next)
{ {
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; y += 50;
switch (o->status) drawText(SCREEN_WIDTH / 2, y, 22, TA_CENTER, colors.white, "(none)");
{
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]);
} }
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)"); y += 25;
}
if (game.currentMission && game.currentMission->challengeHead.next)
{
y += 100;
drawText(SCREEN_WIDTH / 2, y, 28, TA_CENTER, colors.white, "CHALLENGES");
y += 10;
for (c = game.currentMission->challengeHead.next ; c != NULL ; c = c->next) for (c = game.currentMission->challengeHead.next ; c != NULL ; c = c->next)
{ {
@ -155,8 +158,8 @@ static void drawMissionSummary(SDL_Texture *header)
challengeStatus = "Failed"; challengeStatus = "Failed";
} }
drawText(SCREEN_WIDTH / 2 - 25, y, 22, TA_RIGHT, colors.white, "%s", getChallengeDescription(c)); drawText(SCREEN_WIDTH / 2 - 50, 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_LEFT, color, challengeStatus);
} }
} }
} }

View File

@ -31,6 +31,74 @@ static unsigned long hashcode(const char *str);
static char **toTypeArray(char *types, int *numTypes); static char **toTypeArray(char *types, int *numTypes);
static void loadEpicData(cJSON *node); 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) void loadMission(char *filename)
{ {
cJSON *root; cJSON *root;
@ -113,6 +181,8 @@ void loadMission(char *filename)
initPlayer(); initPlayer();
battle.isChallenge = game.currentMission->challengeHead.next != NULL;
playMusic(music); playMusic(music);
} }

View File

@ -21,34 +21,36 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "starSystems.h" #include "starSystems.h"
static void loadMissions(StarSystem *starSystem); static void loadMissions(StarSystem *starSystem);
static void loadStarSystem(cJSON *starSystemJSON); static StarSystem *loadStarSystem(cJSON *starSystemJSON);
static void loadMissionMeta(char *filename, StarSystem *starSystem);
void initStarSystems(void) void initStarSystems(void)
{ {
cJSON *root, *node; cJSON *root, *node;
char *text; char *text;
StarSystem *starSystem, *tail;
tail = &game.starSystemHead;
text = readFile(getFileLocation("data/galaxy/starSystems.json")); text = readFile(getFileLocation("data/galaxy/starSystems.json"));
root = cJSON_Parse(text); root = cJSON_Parse(text);
for (node = cJSON_GetObjectItem(root, "starSystems")->child ; node != NULL ; node = node->next) 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); cJSON_Delete(root);
free(text); free(text);
} }
static void loadStarSystem(cJSON *starSystemJSON) static StarSystem *loadStarSystem(cJSON *starSystemJSON)
{ {
StarSystem *starSystem; StarSystem *starSystem;
starSystem = malloc(sizeof(StarSystem)); starSystem = malloc(sizeof(StarSystem));
memset(starSystem, 0, 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); STRNCPY(starSystem->name, cJSON_GetObjectItem(starSystemJSON, "name")->valuestring, MAX_NAME_LENGTH);
starSystem->side = lookup(cJSON_GetObjectItem(starSystemJSON, "side")->valuestring); starSystem->side = lookup(cJSON_GetObjectItem(starSystemJSON, "side")->valuestring);
@ -72,6 +74,8 @@ static void loadStarSystem(cJSON *starSystemJSON)
starSystem->x *= 3; starSystem->x *= 3;
starSystem->y *= 3; starSystem->y *= 3;
return starSystem;
} }
static void loadMissions(StarSystem *starSystem) static void loadMissions(StarSystem *starSystem)
@ -96,7 +100,7 @@ static void loadMissions(StarSystem *starSystem)
{ {
sprintf(path, "data/missions/%s/%s", name, filenames[i]); sprintf(path, "data/missions/%s/%s", name, filenames[i]);
loadMissionMeta(path, starSystem); loadMissionMeta(path);
free(filenames[i]); free(filenames[i]);
} }
@ -104,74 +108,6 @@ static void loadMissions(StarSystem *starSystem)
free(filenames); 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 *getStarSystem(char *name)
{ {
StarSystem *starSystem; StarSystem *starSystem;

View File

@ -27,5 +27,6 @@ extern long lookup(char *name);
extern int isMissionAvailable(Mission *mission, Mission *prev); extern int isMissionAvailable(Mission *mission, Mission *prev);
extern char *getFileLocation(char *filename); extern char *getFileLocation(char *filename);
extern char **getFileList(char *dir, int *count); extern char **getFileList(char *dir, int *count);
extern Mission *loadMissionMeta(char *filename);
extern Game game; extern Game game;

View File

@ -24,8 +24,6 @@ void initGame(void)
{ {
memset(&game, 0, sizeof(Game)); memset(&game, 0, sizeof(Game));
game.starSystemTail = &game.starSystemHead;
STRNCPY(game.selectedStarSystem, "Sol", MAX_NAME_LENGTH); STRNCPY(game.selectedStarSystem, "Sol", MAX_NAME_LENGTH);
} }

View File

@ -26,8 +26,8 @@ static void handleKeyboard(void);
static void initFighters(void); static void initFighters(void);
static void doFighters(void); static void doFighters(void);
static void drawFighters(void); static void drawFighters(void);
static void newGame(void); static void campaign(void);
static void continueGame(void); static void challenges(void);
static void options(void); static void options(void);
static void quit(void); static void quit(void);
static void returnFromOptions(void); static void returnFromOptions(void);
@ -72,19 +72,8 @@ void initTitle(void)
initFighters(); initFighters();
if (fileExists(getSaveFilePath("game.save"))) getWidget("campaign", "title")->action = campaign;
{ getWidget("challenges", "title")->action = challenges;
selectWidget("continue", "title");
}
else
{
getWidget("continue", "title")->enabled = 0;
selectWidget("newGame", "title");
}
getWidget("newGame", "title")->action = newGame;
getWidget("continue", "title")->action = continueGame;
getWidget("options", "title")->action = options; getWidget("options", "title")->action = options;
getWidget("quit", "title")->action = quit; 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(); initGalacticMap();
} }
static void continueGame(void) static void challenges(void)
{ {
loadGame();
initGalacticMap();
} }
static void options(void) static void options(void)

View File

@ -95,7 +95,8 @@ void initSDL(void)
void initGameSystem(void) void initGameSystem(void)
{ {
int STEPS = 13; int step = 0;
int STEPS = 14;
initColor(&colors.red, 255, 0, 0); initColor(&colors.red, 255, 0, 0);
initColor(&colors.orange, 255, 128, 0); initColor(&colors.orange, 255, 128, 0);
@ -109,59 +110,63 @@ void initGameSystem(void)
initColor(&colors.lightGrey, 192, 192, 192); initColor(&colors.lightGrey, 192, 192, 192);
initColor(&colors.darkGrey, 128, 128, 128); initColor(&colors.darkGrey, 128, 128, 128);
showLoadingStep(0, STEPS); showLoadingStep(step++, STEPS);
initFonts(); initFonts();
showLoadingStep(1, STEPS); showLoadingStep(step++, STEPS);
initInput(); initInput();
showLoadingStep(2, STEPS); showLoadingStep(step++, STEPS);
initLookups(); initLookups();
showLoadingStep(3, STEPS); showLoadingStep(step++, STEPS);
initSounds(); initSounds();
showLoadingStep(4, STEPS); showLoadingStep(step++, STEPS);
initWidgets(); initWidgets();
showLoadingStep(5, STEPS); showLoadingStep(step++, STEPS);
initGame(); initGame();
showLoadingStep(6, STEPS); showLoadingStep(step++, STEPS);
loadFighterDefs(); loadFighterDefs();
showLoadingStep(7, STEPS); showLoadingStep(step++, STEPS);
loadCapitalShipDefs(); loadCapitalShipDefs();
showLoadingStep(8, STEPS); showLoadingStep(step++, STEPS);
loadItemDefs(); loadItemDefs();
showLoadingStep(9, STEPS); showLoadingStep(step++, STEPS);
initBulletDefs(); initBulletDefs();
showLoadingStep(10, STEPS); showLoadingStep(step++, STEPS);
initStarSystems(); initStarSystems();
showLoadingStep(11, STEPS); showLoadingStep(step++, STEPS);
initChallenges();
showLoadingStep(step++, STEPS);
initBattle(); initBattle();
showLoadingStep(12, STEPS); showLoadingStep(step++, STEPS);
initModalDialog(); initModalDialog();
showLoadingStep(13, STEPS); showLoadingStep(step++, STEPS);
} }
/* /*

View File

@ -42,6 +42,7 @@ extern void initLookups(void);
extern void initBattle(void); extern void initBattle(void);
extern void initGame(void); extern void initGame(void);
extern void initStarSystems(void); extern void initStarSystems(void);
extern void initChallenges(void);
extern void initWidgets(void); extern void initWidgets(void);
extern void destroyLookups(void); extern void destroyLookups(void);
extern void destroyFonts(void); extern void destroyFonts(void);

View File

@ -34,8 +34,9 @@ void loadTestMission(char *filename)
initBattle(); initBattle();
loadMission(filename);
loadChallenges(filename); loadChallenges(filename);
loadMission(filename);
} }
static void loadChallenges(char *filename) static void loadChallenges(char *filename)