Automatically load missions from star system misson dir (data/mission/xxx)

This commit is contained in:
Steve 2015-12-20 11:58:21 +00:00
parent 7855cd59ea
commit af04c5a7ef
6 changed files with 122 additions and 112 deletions

View File

@ -4,299 +4,230 @@
"name": "Sol",
"side" : "SIDE_CSN",
"x": 840,
"y": 425,
"missions" : [
"data/missions/sol/01 - free flight.json",
"data/missions/sol/02 - weapons.json",
"data/missions/sol/03 - moving target.json",
"data/missions/sol/04 - armed target.json",
"data/missions/sol/05 - ecm.json",
"data/missions/sol/06 - wingmates.json",
"data/missions/sol/07 - final.json"
]
"y": 425
},
{
"name": "Alpha Centauri",
"side" : "SIDE_CSN",
"x": 860,
"y": 404,
"missions" : []
"y": 404
},
{
"name": "Ecuador Minor",
"side" : "SIDE_CSN",
"x": 831,
"y": 333,
"missions" : []
"y": 333
},
{
"name": "Angel",
"side" : "SIDE_CSN",
"x": 690,
"y": 305,
"missions" : []
"y": 305
},
{
"name": "Indigo",
"side" : "SIDE_CSN",
"x": 760,
"y": 270,
"missions" : []
"y": 270
},
{
"name": "Rex",
"side" : "SIDE_CSN",
"x": 834,
"y": 276,
"missions" : []
"y": 276
},
{
"name": "Gabriel",
"side" : "SIDE_CSN",
"x": 860,
"y": 170,
"missions" : []
"y": 170
},
{
"name": "Darlon",
"side" : "SIDE_CSN",
"x": 808,
"y": 190,
"missions" : []
"y": 190
},
{
"name": "Granada",
"side" : "SIDE_CSN",
"x": 741,
"y": 221,
"missions" : [
"data/missions/granada/01 - suspect packages #1.json"
]
"y": 221
},
{
"name": "Parish",
"side" : "SIDE_CSN",
"x": 666,
"y": 261,
"missions" : []
"y": 261
},
{
"name": "Temper",
"side" : "SIDE_CSN",
"x": 595,
"y": 260,
"missions" : [
"data/missions/temper/01 - pirate uprising #1.json",
"data/missions/temper/02 - pirate uprising #2.json",
"data/missions/temper/03 - pirate uprising #3.json",
"data/missions/temper/04 - pirate uprising #4.json"
]
"y": 260
},
{
"name": "Lenon",
"side" : "SIDE_CSN",
"x": 655,
"y": 180,
"missions" : []
"y": 180
},
{
"name": "HD 21563010",
"side" : "SIDE_CSN",
"x": 813,
"y": 54,
"missions" : []
"y": 54
},
{
"name": "Trilliack",
"side" : "SIDE_UNF",
"x": 501,
"y": 312,
"missions" : []
"y": 312
},
{
"name": "Alba",
"side" : "SIDE_UNF",
"x": 465,
"y": 235,
"missions" : [
"data/missions/alba/01 - patrol #1.json",
"data/missions/alba/02 - patrol #2.json"
]
"y": 235
},
{
"name": "Aster",
"side" : "SIDE_UNF",
"x": 445,
"y": 145,
"missions" : []
"y": 145
},
{
"name": "Iliad",
"side" : "SIDE_UNF",
"x": 335,
"y": 135,
"missions" : [
"data/missions/iliad/01 - iliad defence #1.json",
"data/missions/iliad/02 - iliad defence #2.json"
]
"y": 135
},
{
"name": "Warro",
"side" : "SIDE_UNF",
"x": 289,
"y": 216,
"missions" : [
"data/missions/warro/01 - evacuation #1.json",
"data/missions/warro/02 - evacuation #2.json",
"data/missions/warro/03 - evacuation #3.json"
]
"y": 216
},
{
"name": "Coyote",
"side" : "SIDE_UNF",
"x": 325,
"y": 385,
"fallsToPandorans" : 1,
"missions" : [
"data/missions/coyote/01 - coyote assault #1.json",
"data/missions/coyote/02 - coyote assault #2.json",
"data/missions/coyote/03 - coyote assault #3.json"
]
"fallsToPandorans" : 1
},
{
"name": "Rothan",
"side" : "SIDE_UNF",
"x": 366,
"y": 298,
"missions" : []
"y": 298
},
{
"name": "Donesta",
"side" : "SIDE_UNF",
"x": 527,
"y": 161,
"missions" : []
"y": 161
},
{
"name": "India",
"side" : "SIDE_UNF",
"x": 539,
"y": 427,
"missions" : []
"y": 427
},
{
"name": "Antomis",
"side" : "SIDE_UNF",
"x": 582,
"y": 115,
"missions" : []
"y": 115
},
{
"name": "Mace",
"side" : "SIDE_UNF",
"x": 444,
"y": 359,
"missions" : []
"y": 359
},
{
"name": "Clarke",
"side" : "SIDE_UNF",
"x": 398,
"y": 84,
"missions" : [
"data/missions/clarke/01 - clarke defence #1.json"
]
"y": 84
},
{
"name": "Adetton",
"side" : "SIDE_INF",
"x": 228,
"y": 228,
"missions" : []
"y": 228
},
{
"name": "Atlante",
"side" : "SIDE_INF",
"x": 212,
"y": 85,
"missions" : []
"y": 85
},
{
"name": "Carthege",
"side" : "SIDE_INF",
"x": 163,
"y": 122,
"missions" : []
"y": 122
},
{
"name": "Codexa",
"side" : "SIDE_INF",
"x": 195,
"y": 101,
"missions" : []
"y": 101
},
{
"name": "Haylahe",
"side" : "SIDE_INF",
"x": 204,
"y": 132,
"missions" : []
"y": 132
},
{
"name": "Kethlan",
"side" : "SIDE_INF",
"x": 95,
"y": 95,
"missions" : []
"y": 95
},
{
"name": "Krasst",
"side" : "SIDE_INF",
"x": 135,
"y": 305,
"missions" : []
"y": 305
},
{
"name": "Mekel",
"side" : "SIDE_INF",
"x": 228,
"y": 113,
"missions" : []
"y": 113
},
{
"name": "Phylent",
"side" : "SIDE_INF",
"x": 275,
"y": 165,
"missions" : []
"y": 165
},
{
"name": "Sampi-Persei VII",
"side" : "SIDE_INF",
"x": 342,
"y": 182,
"missions" : []
"y": 182
},
{
"name": "Tigris",
"side" : "SIDE_INF",
"x": 269,
"y": 274,
"missions" : []
"y": 274
},
{
"name": "Troy",
"side" : "SIDE_INF",
"x": 107,
"y": 269,
"missions" : []
"y": 269
},
{
"name": "Pearl",
"side" : "SIDE_INF",
"x": 133,
"y": 215,
"missions" : []
"y": 215
}
]
}

View File

@ -46,6 +46,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define MAX_DESCRIPTION_LENGTH 512
#define MAX_FILENAME_LENGTH 1024
#define MAX_LISTED_FILES 64
#define NUM_TEXTURE_BUCKETS 32
#define MAX_STARS 500

View File

@ -20,6 +20,7 @@ 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);
@ -42,7 +43,6 @@ void initStarSystems(void)
static void loadStarSystem(cJSON *starSystemJSON)
{
cJSON *node;
StarSystem *starSystem;
starSystem = malloc(sizeof(StarSystem));
@ -63,15 +63,42 @@ static void loadStarSystem(cJSON *starSystemJSON)
starSystem->missionHead.completed = 1;
starSystem->missionTail = &starSystem->missionHead;
for (node = cJSON_GetObjectItem(starSystemJSON, "missions")->child ; node != NULL ; node = node->next)
{
loadMissionMeta(node->valuestring, starSystem);
}
loadMissions(starSystem);
starSystem->x *= 3;
starSystem->y *= 3;
}
static void loadMissions(StarSystem *starSystem)
{
int i, count;
char name[MAX_NAME_LENGTH];
char path[MAX_FILENAME_LENGTH];
char **filenames;
STRNCPY(name, starSystem->name, MAX_NAME_LENGTH);
for (i = 0 ; name[i] ; i++)
{
name[i] = tolower(name[i]);
}
sprintf(path, "data/missions/%s", name);
filenames = getFileList(path, &count);
for (i = 0 ; i < count ; i++)
{
sprintf(path, "data/missions/%s/%s", name, filenames[i]);
loadMissionMeta(path, starSystem);
free(filenames[i]);
}
free(filenames);
}
static void loadMissionMeta(char *filename, StarSystem *starSystem)
{
Mission *mission;

View File

@ -26,5 +26,6 @@ extern char *readFile(char *filename);
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 Game game;

View File

@ -20,6 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "io.h"
static int stringComparator(const void *a, const void *b);
int fileExists(char *filename)
{
struct stat buffer;
@ -89,3 +91,49 @@ char *getFileLocation(char *filename)
return path;
}
char **getFileList(char *dir, int *count)
{
DIR *d;
struct dirent *ent;
int i;
char **filenames;
filenames = malloc(sizeof(char*) * MAX_LISTED_FILES);
memset(filenames, 0, sizeof(char*) * MAX_LISTED_FILES);
i = 0;
if ((d = opendir(dir)) != NULL)
{
while ((ent = readdir(d)) != NULL)
{
if (ent->d_name[0] != '.')
{
filenames[i] = malloc(sizeof(char) * MAX_FILENAME_LENGTH);
STRNCPY(filenames[i], ent->d_name, MAX_FILENAME_LENGTH);
if (++i >= MAX_LISTED_FILES)
{
break;
}
}
}
closedir(d);
}
*count = i;
qsort(filenames, i, sizeof(char*), stringComparator);
return filenames;
}
static int stringComparator(const void *a, const void *b)
{
char **s1 = (char **)a;
char **s2 = (char **)b;
return strcmp(*s1, *s2);
}

View File

@ -21,5 +21,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../common.h"
#include "sys/stat.h"
#include "dirent.h"
extern App app;