blobwarsAttrition/src/world/worldLoader.c

201 lines
5.7 KiB
C
Raw Normal View History

2018-01-29 09:34:39 +01:00
/*
Copyright (C) 2018 Parallel Realities
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "worldLoader.h"
static void loadEnemyTypes(char *enemyTypes);
static void loadTriggers(cJSON *root);
static void loadBob(cJSON *root);
static void loadEntities(cJSON *root);
static void loadObjectives(cJSON *root);
2018-02-10 19:35:56 +01:00
void loadWorld(char *id)
2018-01-29 09:34:39 +01:00
{
cJSON *root;
2018-02-10 19:35:56 +01:00
char *text, filename[MAX_FILENAME_LENGTH];
2018-01-29 09:34:39 +01:00
memset(&world, 0, sizeof(World));
world.entityTail = &world.entityHead;
world.triggerTail = &world.triggerHead;
world.objectiveTail = &world.objectiveHead;
world.particleTail = &world.particleHead;
2018-01-29 09:34:39 +01:00
2018-02-10 19:35:56 +01:00
sprintf(filename, "%s/%s.json", app.saveDir, id);
if (!game.isComplete && fileExists(filename))
2018-02-10 19:35:56 +01:00
{
text = readFile(filename);
}
else
{
sprintf(filename, "data/maps/%s.json", id);
text = readFile(filename);
}
2018-01-29 09:34:39 +01:00
root = cJSON_Parse(text);
STRNCPY(world.id, cJSON_GetObjectItem(root, "id")->valuestring, MAX_NAME_LENGTH);
STRNCPY(world.name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH);
world.minEnemySpawnTime = cJSON_GetObjectItem(root, "minEnemySpawnTime")->valueint;
world.maxEnemySpawnTime = cJSON_GetObjectItem(root, "maxEnemySpawnTime")->valueint;
STRNCPY(world.music, cJSON_GetObjectItem(root, "music")->valuestring, MAX_FILENAME_LENGTH);
STRNCPY(world.tileset, cJSON_GetObjectItem(root, "tileset")->valuestring, MAX_NAME_LENGTH);
STRNCPY(world.background, cJSON_GetObjectItem(root, "background")->valuestring, MAX_FILENAME_LENGTH);
world.entityCounter = cJSON_GetObjectItem(root, "entityCounter")->valueint;
world.missionType = strcmp(world.id, "beachApproach") == 0 ? MT_TRAINING : MT_NORMAL;
world.missionType = strncmp(world.id, "outpost", 7) == 0 ? MT_OUTPOST : world.missionType;
world.missionType = strncmp(world.id, "boss", 4) == 0 ? MT_BOSS : world.missionType;
2018-01-29 09:34:39 +01:00
loadEnemyTypes(cJSON_GetObjectItem(root, "enemyTypes")->valuestring);
loadTriggers(cJSON_GetObjectItem(root, "triggers"));
loadBob(cJSON_GetObjectItem(root, "bob"));
loadEntities(cJSON_GetObjectItem(root, "entities"));
loadObjectives(cJSON_GetObjectItem(root, "objectives"));
2018-02-03 17:21:53 +01:00
cJSON_Delete(root);
free(text);
2018-01-29 09:34:39 +01:00
}
static void loadEnemyTypes(char *enemyTypes)
{
int i;
char *enemyType;
2018-02-10 08:57:31 +01:00
world.numEnemyTypes = 1;
2018-01-29 09:34:39 +01:00
for (i = 0 ; i < strlen(enemyTypes) ; i++)
{
if (enemyTypes[i] == '|')
{
world.numEnemyTypes++;
}
}
world.enemyTypes = malloc(world.numEnemyTypes * sizeof(char*));
i = 0;
enemyType = strtok(enemyTypes, "|");
while (enemyType)
{
world.enemyTypes[i] = malloc(strlen(enemyType) + 1);
strcpy(world.enemyTypes[i], enemyType);
2018-02-10 08:57:31 +01:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "world.entityTypes[%d] = %s", i, enemyType);
2018-01-29 09:34:39 +01:00
enemyType = strtok(NULL, "|");
i++;
}
}
static void loadTriggers(cJSON *root)
{
cJSON *node;
Trigger *t;
for (node = root->child ; node != NULL ; node = node->next)
{
t = malloc(sizeof(Trigger));
memset(t, 0, sizeof(Trigger));
world.triggerTail->next = t;
world.triggerTail = t;
STRNCPY(t->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH);
STRNCPY(t->targetNames, cJSON_GetObjectItem(node, "targetNames")->valuestring, MAX_DESCRIPTION_LENGTH);
STRNCPY(t->message, cJSON_GetObjectItem(node, "message")->valuestring, MAX_DESCRIPTION_LENGTH);
t->x = cJSON_GetObjectItem(node, "x")->valueint;
t->y = cJSON_GetObjectItem(node, "y")->valueint;
t->w = cJSON_GetObjectItem(node, "w")->valueint;
t->h = cJSON_GetObjectItem(node, "h")->valueint;
}
}
static void loadBob(cJSON *root)
{
2018-01-31 22:50:49 +01:00
world.bob = (Bob*)createEntity("Bob");
2018-01-29 09:34:39 +01:00
2018-02-01 08:50:37 +01:00
self = (Entity*)world.bob;
2018-01-31 22:50:49 +01:00
world.bob->load(root);
2018-02-01 08:50:37 +01:00
world.bob->init();
2018-02-03 00:01:51 +01:00
world.bob->animate();
2018-01-29 09:34:39 +01:00
}
static void loadEntities(cJSON *root)
{
2018-01-29 23:12:18 +01:00
cJSON *node;
for (node = root->child ; node != NULL ; node = node->next)
{
2018-01-30 23:47:40 +01:00
self = createEntity(cJSON_GetObjectItem(node, "type")->valuestring);
if (cJSON_GetObjectItem(node, "name"))
{
STRNCPY(self->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH);
}
self->x = cJSON_GetObjectItem(node, "x")->valueint;
self->y = cJSON_GetObjectItem(node, "y")->valueint;
self->load(node);
2018-02-01 08:50:37 +01:00
self->init();
2018-02-03 00:01:51 +01:00
self->animate();
if (self->type == ET_ENEMY && dev.cheatNoEnemies)
{
self->alive = ALIVE_DEAD;
}
2018-01-29 23:12:18 +01:00
}
2018-01-29 09:34:39 +01:00
}
static void loadObjectives(cJSON *root)
{
2018-01-29 23:12:18 +01:00
Objective *o;
cJSON *node;
for (node = root->child ; node != NULL ; node = node->next)
{
o = malloc(sizeof(Objective));
memset(o, 0, sizeof(Objective));
world.objectiveTail->next = o;
world.objectiveTail = o;
STRNCPY(o->id, cJSON_GetObjectItem(node, "id")->valuestring, MAX_NAME_LENGTH);
STRNCPY(o->targetName, cJSON_GetObjectItem(node, "targetName")->valuestring, MAX_NAME_LENGTH);
STRNCPY(o->description, cJSON_GetObjectItem(node, "description")->valuestring, MAX_DESCRIPTION_LENGTH);
o->totalValue = cJSON_GetObjectItem(node, "totalValue")->valueint;
2018-02-04 10:56:37 +01:00
o->targetValue = cJSON_GetObjectItem(node, "targetValue")->valueint;
2018-01-29 23:12:18 +01:00
o->currentValue = cJSON_GetObjectItem(node, "currentValue")->valueint;
o->required = cJSON_GetObjectItem(node, "required")->valueint;
}
2018-01-29 09:34:39 +01:00
}