Added getJSONValue to aid with optional JSON data.

This commit is contained in:
Steve 2016-03-04 14:14:04 +00:00
parent b6012a2e21
commit 95cb35d5a1
9 changed files with 501 additions and 572 deletions

View File

@ -453,11 +453,7 @@ static void loadGuns(Entity *parent, cJSON *guns)
e->offsetY = cJSON_GetObjectItem(gun, "y")->valueint;
e->texture = getTexture(cJSON_GetObjectItem(gun, "texture")->valuestring);
e->guns[0].type = lookup(cJSON_GetObjectItem(gun, "type")->valuestring);
if (cJSON_GetObjectItem(gun, "missiles"))
{
e->missiles = cJSON_GetObjectItem(gun, "missiles")->valueint;
}
e->missiles = getJSONValue(gun, "missiles", 0);
if (cJSON_GetObjectItem(gun, "flags"))
{

View File

@ -47,6 +47,7 @@ extern void addDebris(int x, int y, int amount);
extern void runScriptFunction(char *format, ...);
extern void updateObjective(char *name, int type);
extern char **getFileList(char *dir, int *count);
extern int getJSONValue(cJSON *node, char *name, int defValue);
extern Battle battle;
extern Entity *self;

View File

@ -689,18 +689,12 @@ static void loadFighterDef(char *filename)
}
}
if (cJSON_GetObjectItem(root, "combinedGuns"))
{
e->combinedGuns = cJSON_GetObjectItem(root, "combinedGuns")->valueint;
}
e->combinedGuns = getJSONValue(root, "combinedGuns", 0);
}
e->selectedGunType = e->guns[0].type;
if (cJSON_GetObjectItem(root, "missiles"))
{
e->missiles = cJSON_GetObjectItem(root, "missiles")->valueint;
}
e->missiles = getJSONValue(root, "missiles", 0);
if (cJSON_GetObjectItem(root, "flags"))
{

View File

@ -47,6 +47,7 @@ extern char *getFileLocation(char *filename);
extern void addDebris(int x, int y, int amount);
extern char **getFileList(char *dir, int *count);
extern char *getTranslatedString(char *string);
extern int getJSONValue(cJSON *node, char *name, int defValue);
extern App app;
extern Battle battle;

View File

@ -55,10 +55,7 @@ Mission *loadMissionMeta(char *filename)
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;
}
mission->requires = getJSONValue(root, "requires", 0);
if (cJSON_GetObjectItem(root, "epic"))
{
@ -79,12 +76,14 @@ Mission *loadMissionMeta(char *filename)
if (node)
{
mission->challengeData.isChallenge = 1;
mission->challengeData.timeLimit = cJSON_GetObjectItem(node, "timeLimit")->valueint * FPS;
mission->challengeData.killLimit = cJSON_GetObjectItem(node, "killLimit")->valueint;
mission->challengeData.noMissiles = cJSON_GetObjectItem(node, "noMissiles") ? 1 : 0;
mission->challengeData.noECM = cJSON_GetObjectItem(node, "noECM") ? 1 : 0;
mission->challengeData.noBoost = cJSON_GetObjectItem(node, "noBoost") ? 1 : 0;
mission->challengeData.noGuns = cJSON_GetObjectItem(node, "noGuns") ? 1 : 0;
mission->challengeData.timeLimit = getJSONValue(node, "timeLimit", 0);
mission->challengeData.killLimit = getJSONValue(node, "killLimit", 0);
mission->challengeData.waypointLimit = getJSONValue(node, "waypointLimit", 0);
mission->challengeData.noMissiles = getJSONValue(node, "noMissiles", 0);
mission->challengeData.noECM = getJSONValue(node, "noECM", 0);
mission->challengeData.noBoost = getJSONValue(node, "noBoost", 0);
mission->challengeData.noGuns = getJSONValue(node, "noGuns", 0);
node = cJSON_GetObjectItem(node, "challenges");
@ -152,15 +151,8 @@ void loadMission(char *filename)
loadEpicData(cJSON_GetObjectItem(root, "epic"));
}
if (cJSON_GetObjectItem(root, "manualComplete"))
{
battle.manualComplete = cJSON_GetObjectItem(root, "manualComplete")->valueint;
}
if (cJSON_GetObjectItem(root, "unwinnable"))
{
battle.unwinnable = cJSON_GetObjectItem(root, "unwinnable")->valueint;
}
battle.manualComplete = getJSONValue(root, "manualComplete", 0);
battle.unwinnable = getJSONValue(root, "unwinnable", 0);
initScript(cJSON_GetObjectItem(root, "script"));
@ -324,20 +316,12 @@ static void loadObjectives(cJSON *node)
STRNCPY(o->targetName, cJSON_GetObjectItem(node, "targetName")->valuestring, MAX_NAME_LENGTH);
o->targetValue = cJSON_GetObjectItem(node, "targetValue")->valueint;
o->targetType = lookup(cJSON_GetObjectItem(node, "targetType")->valuestring);
o->active = getJSONValue(node, "active", 0);
o->isCondition = getJSONValue(node, "isCondition", 0);
if (cJSON_GetObjectItem(node, "active"))
o->isEliminateAll = getJSONValue(node, "isEliminateAll", 0);
if (isEliminateAll)
{
o->active = cJSON_GetObjectItem(node, "active")->valueint;
}
if (cJSON_GetObjectItem(node, "isCondition"))
{
o->isCondition = cJSON_GetObjectItem(node, "isCondition")->valueint;
}
if (cJSON_GetObjectItem(node, "isEliminateAll"))
{
o->isEliminateAll = cJSON_GetObjectItem(node, "isEliminateAll")->valueint;
o->targetValue = 1;
}
@ -402,31 +386,11 @@ static void loadFighters(cJSON *node)
side = lookup(cJSON_GetObjectItem(node, "side")->valuestring);
x = (cJSON_GetObjectItem(node, "x")->valuedouble / BATTLE_AREA_CELLS) * BATTLE_AREA_WIDTH;
y = (cJSON_GetObjectItem(node, "y")->valuedouble / BATTLE_AREA_CELLS) * BATTLE_AREA_HEIGHT;
if (cJSON_GetObjectItem(node, "name"))
{
name = cJSON_GetObjectItem(node, "name")->valuestring;
}
if (cJSON_GetObjectItem(node, "groupName"))
{
groupName = cJSON_GetObjectItem(node, "groupName")->valuestring;
}
if (cJSON_GetObjectItem(node, "number"))
{
number = cJSON_GetObjectItem(node, "number")->valueint;
}
if (cJSON_GetObjectItem(node, "scatter"))
{
scatter = cJSON_GetObjectItem(node, "scatter")->valueint;
}
if (cJSON_GetObjectItem(node, "active"))
{
active = cJSON_GetObjectItem(node, "active")->valueint;
}
name = getJSONValueStr(node, "name", NULL);
groupName = getJSONValueStr(node, "groupName", NULL);
number = getJSONValue(node, "number", 0);
scatter = getJSONValue(node, "scatter", 0);
active = getJSONValue(node, "active", 0);
if (cJSON_GetObjectItem(node, "flags"))
{
@ -529,31 +493,11 @@ static void loadCapitalShips(cJSON *node)
side = lookup(cJSON_GetObjectItem(node, "side")->valuestring);
x = (cJSON_GetObjectItem(node, "x")->valuedouble / BATTLE_AREA_CELLS) * BATTLE_AREA_WIDTH;
y = (cJSON_GetObjectItem(node, "y")->valuedouble / BATTLE_AREA_CELLS) * BATTLE_AREA_HEIGHT;
if (cJSON_GetObjectItem(node, "name"))
{
name = cJSON_GetObjectItem(node, "name")->valuestring;
}
if (cJSON_GetObjectItem(node, "groupName"))
{
groupName = cJSON_GetObjectItem(node, "groupName")->valuestring;
}
if (cJSON_GetObjectItem(node, "number"))
{
number = cJSON_GetObjectItem(node, "number")->valueint;
}
if (cJSON_GetObjectItem(node, "scatter"))
{
scatter = cJSON_GetObjectItem(node, "scatter")->valueint;
}
if (cJSON_GetObjectItem(node, "active"))
{
active = cJSON_GetObjectItem(node, "active")->valueint;
}
name = cJSON_GetObjectItem(node, "name", NULL);
groupName = getJSONValueStr(node, "groupName", NULL);
number = getJSONValueStr(node, "number", 0);
scatter = getJSONValueStr(node, "scatter", 0);
active = getJSONValue(node, "active", 0);
if (cJSON_GetObjectItem(node, "flags"))
{
@ -636,30 +580,15 @@ static void loadEntities(cJSON *node)
active = 1;
scatter = 1;
if (cJSON_GetObjectItem(node, "name"))
{
name = cJSON_GetObjectItem(node, "name")->valuestring;
}
name = getJSONValueStr(node, "name", NULL);
if (cJSON_GetObjectItem(node, "groupName"))
{
groupName = cJSON_GetObjectItem(node, "groupName")->valuestring;
}
groupName = getJSONValueStr(node, "groupName", NULL);
if (cJSON_GetObjectItem(node, "number"))
{
number = cJSON_GetObjectItem(node, "number")->valueint;
}
number = getJSONValue(node, "number", 0);
if (cJSON_GetObjectItem(node, "active"))
{
active = cJSON_GetObjectItem(node, "active")->valueint;
}
active = getJSONValue(node, "active", 0);
if (cJSON_GetObjectItem(node, "scatter"))
{
scatter = cJSON_GetObjectItem(node, "scatter")->valueint;
}
scatter = getJSONValue(node, "scatter", 0);
for (i = 0 ; i < number ; i++)
{
@ -733,35 +662,20 @@ static void loadItems(cJSON *node)
number = 1;
active = 1;
if (cJSON_GetObjectItem(node, "name"))
{
name = cJSON_GetObjectItem(node, "name")->valuestring;
}
name = getJSONValueStr(node, "name", NULL);
if (cJSON_GetObjectItem(node, "groupName"))
{
groupName = cJSON_GetObjectItem(node, "groupName")->valuestring;
}
groupName = getJSONValueStr(node, "groupName", NULL);
if (cJSON_GetObjectItem(node, "number"))
{
number = cJSON_GetObjectItem(node, "number")->valueint;
}
number = getJSONValue(node, "number", 0);
if (cJSON_GetObjectItem(node, "scatter"))
{
scatter = cJSON_GetObjectItem(node, "scatter")->valueint;
}
scatter = getJSONValue(node, "scatter", 0);
if (cJSON_GetObjectItem(node, "flags"))
{
flags = flagsToLong(cJSON_GetObjectItem(node, "flags")->valuestring, &addFlags);
}
if (cJSON_GetObjectItem(node, "active"))
{
active = cJSON_GetObjectItem(node, "active")->valueint;
}
active = getJSONValue(node, "active", 0);
for (i = 0 ; i < number ; i++)
{
@ -833,10 +747,7 @@ static void loadLocations(cJSON *node)
l->size = cJSON_GetObjectItem(node, "size")->valueint;
if (cJSON_GetObjectItem(node, "active"))
{
active = cJSON_GetObjectItem(node, "active")->valueint;
}
active = getJSONValue(node, "active", 0);
l->x += (SCREEN_WIDTH / 2);
l->y += (SCREEN_HEIGHT / 2);

View File

@ -53,6 +53,8 @@ extern void updateChallengeMissions(void);
extern char *getBackgroundTextureName(int n);
extern char *getPlanetTextureName(int n);
extern char *getMusicFilename(int n);
extern int getJSONValue(cJSON *node, char *name, int defValue);
extern char *getJSONValueStr(cJSON *node, char *name, char *defValue);
extern Battle battle;
extern Entity *player;

View File

@ -56,11 +56,7 @@ static StarSystem *loadStarSystem(cJSON *starSystemJSON)
starSystem->side = lookup(cJSON_GetObjectItem(starSystemJSON, "side")->valuestring);
starSystem->x = cJSON_GetObjectItem(starSystemJSON, "x")->valueint;
starSystem->y = cJSON_GetObjectItem(starSystemJSON, "y")->valueint;
if (cJSON_GetObjectItem(starSystemJSON, "fallsToPandorans"))
{
starSystem->fallsToPandorans = cJSON_GetObjectItem(starSystemJSON, "fallsToPandorans")->valueint;
}
starSystem->fallsToPandorans = getJSONValue(starSystemJSON, "fallsToPandorans", 0);
if (strcmp(starSystem->name, "Sol") == 0)
{

View File

@ -28,5 +28,6 @@ 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 int getJSONValue(cJSON *node, char *name, int defValue);
extern Game game;

View File

@ -95,3 +95,30 @@ char *timeToString(long millis, int showHours)
return TIME;
}
char *getJSONValueStr(cJSON *node, char *name, char *defValue)
{
cJSON *child;
if (node)
{
child = cJSON_GetObjectItem(node, name);
if (child)
{
return child->valuestring;
}
}
return defValue;
}
int getJSONValue(cJSON *node, char *name, int defValue)
{
char *result = getJSONValueStr(node, name, defValue);
if (result)
{
return aoti(result);
}
}