static void AddGameConfigNode(GameConfig *config, json_t *root) { char buf[32]; json_t *subConfig = json_new_object(); json_insert_pair_into_object( subConfig, "FriendlyFire", json_new_bool(config->FriendlyFire)); sprintf(buf, "%u", config->RandomSeed); json_insert_pair_into_object( subConfig, "RandomSeed", json_new_number(buf)); JSON_UTILS_ADD_ENUM_PAIR(subConfig, "Difficulty", config->Difficulty, DifficultyStr); json_insert_pair_into_object( subConfig, "SlowMotion", json_new_bool(config->SlowMotion)); AddIntPair(subConfig, "EnemyDensity", config->EnemyDensity); AddIntPair(subConfig, "NonPlayerHP", config->NonPlayerHP); AddIntPair(subConfig, "PlayerHP", config->PlayerHP); json_insert_pair_into_object( subConfig, "Fog", json_new_bool(config->Fog)); AddIntPair(subConfig, "SightRange", config->SightRange); json_insert_pair_into_object( subConfig, "Shadows", json_new_bool(config->Shadows)); json_insert_pair_into_object( subConfig, "MoveWhenShooting", json_new_bool(config->MoveWhenShooting)); JSON_UTILS_ADD_ENUM_PAIR( subConfig, "SwitchMoveStyle", config->SwitchMoveStyle, SwitchMoveStyleStr); json_insert_pair_into_object( subConfig, "ShotsPushback", json_new_bool(config->ShotsPushback)); JSON_UTILS_ADD_ENUM_PAIR( subConfig, "AllyCollision", config->AllyCollision, AllyCollisionStr); json_insert_pair_into_object( subConfig, "HealthPickups", json_new_bool(config->HealthPickups)); json_insert_pair_into_object(root, "Game", subConfig); }
int MapArchiveSave(const char *filename, CampaignSetting *c) { int res = 1; json_t *root = NULL; char relbuf[CDOGS_PATH_MAX]; if (strcmp(StrGetFileExt(filename), "cdogscpn") == 0 || strcmp(StrGetFileExt(filename), "CDOGSCPN") == 0) { strcpy(relbuf, filename); } else { sprintf(relbuf, "%s.cdogscpn", filename); } char buf[CDOGS_PATH_MAX]; RealPath(relbuf, buf); // Make dir but ignore error, as we may be saving over an existing dir mkdir_deep(buf); // Campaign root = json_new_object(); AddIntPair(root, "Version", MAP_VERSION); AddStringPair(root, "Title", c->Title); AddStringPair(root, "Author", c->Author); AddStringPair(root, "Description", c->Description); AddIntPair(root, "Missions", c->Missions.size); char buf2[CDOGS_PATH_MAX]; sprintf(buf2, "%s/campaign.json", buf); if (!TrySaveJSONFile(root, buf2)) { res = 0; goto bail; } json_free_value(&root); root = json_new_object(); json_insert_pair_into_object(root, "Missions", SaveMissions(&c->Missions)); sprintf(buf2, "%s/missions.json", buf); if (!TrySaveJSONFile(root, buf2)) { res = 0; goto bail; } json_free_value(&root); root = json_new_object(); json_insert_pair_into_object( root, "Characters", SaveCharacters(&c->characters)); sprintf(buf2, "%s/characters.json", buf); if (!TrySaveJSONFile(root, buf2)) { res = 0; goto bail; } bail: json_free_value(&root); return res; }
static json_t *SaveClassicDoors(Mission *m) { json_t *node = json_new_object(); AddBoolPair(node, "Enabled", m->u.Classic.Doors.Enabled); AddIntPair(node, "Min", m->u.Classic.Doors.Min); AddIntPair(node, "Max", m->u.Classic.Doors.Max); return node; }
static json_t *SaveClassicPillars(Mission *m) { json_t *node = json_new_object(); AddIntPair(node, "Count", m->u.Classic.Pillars.Count); AddIntPair(node, "Min", m->u.Classic.Pillars.Min); AddIntPair(node, "Max", m->u.Classic.Pillars.Max); return node; }
static void AddGraphicsConfigNode(GraphicsConfig *config, json_t *root) { json_t *subConfig = json_new_object(); AddIntPair(subConfig, "Brightness", config->Brightness); AddIntPair(subConfig, "ResolutionWidth", config->Res.x); AddIntPair(subConfig, "ResolutionHeight", config->Res.y); json_insert_pair_into_object( subConfig, "Fullscreen", json_new_bool(config->Fullscreen)); AddIntPair(subConfig, "ScaleFactor", config->ScaleFactor); AddIntPair(subConfig, "ShakeMultiplier", config->ShakeMultiplier); JSON_UTILS_ADD_ENUM_PAIR(subConfig, "ScaleMode", config->ScaleMode, ScaleModeStr); json_insert_pair_into_object(root, "Graphics", subConfig); }
static void AddSoundConfigNode(SoundConfig *config, json_t *root) { json_t *subConfig = json_new_object(); AddIntPair(subConfig, "SoundVolume", config->SoundVolume); AddIntPair(subConfig, "MusicVolume", config->MusicVolume); json_insert_pair_into_object( subConfig, "Footsteps", json_new_bool(config->Footsteps)); json_insert_pair_into_object( subConfig, "Hits", json_new_bool(config->Hits)); json_insert_pair_into_object( subConfig, "Reloads", json_new_bool(config->Reloads)); json_insert_pair_into_object(root, "Sound", subConfig); }
static json_t *CreateMissionNode(MissionSave *m) { json_t *subConfig = json_new_object(); AddCampaignNode(&m->Campaign, subConfig); json_insert_pair_into_object(subConfig, "Password", json_new_string(m->Password)); AddIntPair(subConfig, "MissionsCompleted", m->MissionsCompleted); return subConfig; }
static json_t *SaveObjectives(CArray *a) { json_t *objectivesNode = json_new_array(); int i; for (i = 0; i < (int)a->size; i++) { json_t *objNode = json_new_object(); MissionObjective *mo = CArrayGet(a, i); AddStringPair(objNode, "Description", mo->Description); AddStringPair(objNode, "Type", ObjectiveTypeStr(mo->Type)); AddIntPair(objNode, "Index", mo->Index); AddIntPair(objNode, "Count", mo->Count); AddIntPair(objNode, "Required", mo->Required); AddIntPair(objNode, "Flags", mo->Flags); json_insert_child(objectivesNode, objNode); } return objectivesNode; }
static void AddKeysNode(input_keys_t *keys, json_t *parent) { json_t *subConfig = json_new_object(); // TODO: key names (there's no key name to int conversion) AddIntPair(subConfig, "left", keys->left); AddIntPair(subConfig, "right", keys->right); AddIntPair(subConfig, "up", keys->up); AddIntPair(subConfig, "down", keys->down); AddIntPair(subConfig, "button1", keys->button1); AddIntPair(subConfig, "button2", keys->button2); AddIntPair(subConfig, "map", keys->map); json_insert_pair_into_object(parent, "Keys", subConfig); }
static void ConfigSaveVisit(const Config *c, json_t *node) { switch (c->Type) { case CONFIG_TYPE_STRING: CASSERT(false, "not implemented"); break; case CONFIG_TYPE_INT: AddIntPair(node, c->Name, c->u.Int.Value); break; case CONFIG_TYPE_FLOAT: CASSERT(false, "not implemented"); break; case CONFIG_TYPE_BOOL: json_insert_pair_into_object( node, c->Name, json_new_bool(c->u.Bool.Value)); break; case CONFIG_TYPE_ENUM: JSON_UTILS_ADD_ENUM_PAIR( node, c->Name, c->u.Enum.Value, c->u.Enum.EnumToStr); break; case CONFIG_TYPE_GROUP: { json_t *child = node; // If the config has no name, then it is the root element // Add children directly to the node // Otherwise, create a new child if (c->Name != NULL) { child = json_new_object(); } CA_FOREACH(Config, cg, c->u.Group) ConfigSaveVisit(cg, child); CA_FOREACH_END() if (c->Name != NULL) { json_insert_pair_into_object(node, c->Name, child); } } break; default: CASSERT(false, "Unknown config type"); break; } }
static json_t *SaveStaticCharacters(Mission *m) { json_t *chars = json_new_array(); CA_FOREACH(CharacterPositions, cp, m->u.Static.Characters) json_t *charNode = json_new_object(); AddIntPair(charNode, "Index", cp->Index); json_t *positions = json_new_array(); for (int j = 0; j < (int)cp->Positions.size; j++) { Vec2i *pos = CArrayGet(&cp->Positions, j); json_insert_child(positions, SaveVec2i(*pos)); } json_insert_pair_into_object( charNode, "Positions", positions); json_insert_child(chars, charNode); CA_FOREACH_END() return chars; }
static json_t *SaveStaticKeys(Mission *m) { json_t *keys = json_new_array(); CA_FOREACH(KeyPositions, kp, m->u.Static.Keys) json_t *keyNode = json_new_object(); AddIntPair(keyNode, "Index", kp->Index); json_t *positions = json_new_array(); for (int j = 0; j < (int)kp->Positions.size; j++) { Vec2i *pos = CArrayGet(&kp->Positions, j); json_insert_child(positions, SaveVec2i(*pos)); } json_insert_pair_into_object( keyNode, "Positions", positions); json_insert_child(keys, keyNode); CA_FOREACH_END() return keys; }
static json_t *SaveClassicRooms(Mission *m) { json_t *node = json_new_object(); AddIntPair(node, "Count", m->u.Classic.Rooms.Count); AddIntPair(node, "Min", m->u.Classic.Rooms.Min); AddIntPair(node, "Max", m->u.Classic.Rooms.Max); AddBoolPair(node, "Edge", m->u.Classic.Rooms.Edge); AddBoolPair(node, "Overlap", m->u.Classic.Rooms.Overlap); AddIntPair(node, "Walls", m->u.Classic.Rooms.Walls); AddIntPair(node, "WallLength", m->u.Classic.Rooms.WallLength); AddIntPair(node, "WallPad", m->u.Classic.Rooms.WallPad); return node; }
static json_t *SaveStaticObjectives(Mission *m) { json_t *objs = json_new_array(); CA_FOREACH(ObjectivePositions, op, m->u.Static.Objectives) json_t *objNode = json_new_object(); AddIntPair(objNode, "Index", op->Index); json_t *positions = json_new_array(); for (int j = 0; j < (int)op->Positions.size; j++) { Vec2i *pos = CArrayGet(&op->Positions, j); json_insert_child(positions, SaveVec2i(*pos)); } json_insert_pair_into_object( objNode, "Positions", positions); json_insert_pair_into_object( objNode, "Indices", SaveIntArray(&op->Indices)); json_insert_child(objs, objNode); CA_FOREACH_END() return objs; }
static json_t *SaveCharacters(CharacterStore *s) { json_t *charNode = json_new_array(); CA_FOREACH(Character, c, s->OtherChars) json_t *node = json_new_object(); AddStringPair(node, "Class", c->Class->Name); AddColorPair(node, "Skin", c->Colors.Skin); AddColorPair(node, "Arms", c->Colors.Arms); AddColorPair(node, "Body", c->Colors.Body); AddColorPair(node, "Legs", c->Colors.Legs); AddColorPair(node, "Hair", c->Colors.Hair); AddIntPair(node, "speed", c->speed); json_insert_pair_into_object( node, "Gun", json_new_string(c->Gun->name)); AddIntPair(node, "maxHealth", c->maxHealth); AddIntPair(node, "flags", c->flags); AddIntPair(node, "probabilityToMove", c->bot->probabilityToMove); AddIntPair(node, "probabilityToTrack", c->bot->probabilityToTrack); AddIntPair(node, "probabilityToShoot", c->bot->probabilityToShoot); AddIntPair(node, "actionDelay", c->bot->actionDelay); json_insert_child(charNode, node); CA_FOREACH_END() return charNode; }
static json_t *SaveCharacters(CharacterStore *s) { json_t *charNode = json_new_array(); int i; for (i = 0; i < (int)s->OtherChars.size; i++) { json_t *node = json_new_object(); Character *c = CArrayGet(&s->OtherChars, i); AddIntPair(node, "face", c->looks.Face); AddIntPair(node, "skin", c->looks.Skin); AddIntPair(node, "arm", c->looks.Arm); AddIntPair(node, "body", c->looks.Body); AddIntPair(node, "leg", c->looks.Leg); AddIntPair(node, "hair", c->looks.Hair); AddIntPair(node, "speed", c->speed); json_insert_pair_into_object( node, "Gun", json_new_string(c->Gun->name)); AddIntPair(node, "maxHealth", c->maxHealth); AddIntPair(node, "flags", c->flags); AddIntPair(node, "probabilityToMove", c->bot->probabilityToMove); AddIntPair(node, "probabilityToTrack", c->bot->probabilityToTrack); AddIntPair(node, "probabilityToShoot", c->bot->probabilityToShoot); AddIntPair(node, "actionDelay", c->bot->actionDelay); json_insert_child(charNode, node); } return charNode; }
static json_t *SaveMissions(CArray *a) { json_t *missionsNode = json_new_array(); for (int i = 0; i < (int)a->size; i++) { json_t *node = json_new_object(); Mission *mission = CArrayGet(a, i); AddStringPair(node, "Title", mission->Title); AddStringPair(node, "Description", mission->Description); AddStringPair(node, "Type", MapTypeStr(mission->Type)); AddIntPair(node, "Width", mission->Size.x); AddIntPair(node, "Height", mission->Size.y); AddIntPair(node, "WallStyle", mission->WallStyle); AddIntPair(node, "FloorStyle", mission->FloorStyle); AddIntPair(node, "RoomStyle", mission->RoomStyle); AddIntPair(node, "ExitStyle", mission->ExitStyle); AddIntPair(node, "KeyStyle", mission->KeyStyle); AddStringPair(node, "DoorStyle", mission->DoorStyle); json_insert_pair_into_object( node, "Objectives", SaveObjectives(&mission->Objectives)); json_insert_pair_into_object( node, "Enemies", SaveIntArray(&mission->Enemies)); json_insert_pair_into_object( node, "SpecialChars", SaveIntArray(&mission->SpecialChars)); json_t *modsNode = json_new_array(); for (int j = 0; j < (int)mission->MapObjectDensities.size; j++) { const MapObjectDensity *mod = CArrayGet(&mission->MapObjectDensities, j); json_t *modNode = json_new_object(); AddStringPair(modNode, "MapObject", mod->M->Name); AddIntPair(modNode, "Density", mod->Density); json_insert_child(modsNode, modNode); } json_insert_pair_into_object(node, "MapObjectDensities", modsNode); AddIntPair(node, "EnemyDensity", mission->EnemyDensity); json_insert_pair_into_object( node, "Weapons", SaveWeapons(&mission->Weapons)); json_insert_pair_into_object( node, "Song", json_new_string(mission->Song)); AddColorPair(node, "WallMask", mission->WallMask); AddColorPair(node, "FloorMask", mission->FloorMask); AddColorPair(node, "RoomMask", mission->RoomMask); AddColorPair(node, "AltMask", mission->AltMask); switch (mission->Type) { case MAPTYPE_CLASSIC: AddIntPair(node, "Walls", mission->u.Classic.Walls); AddIntPair(node, "WallLength", mission->u.Classic.WallLength); AddIntPair( node, "CorridorWidth", mission->u.Classic.CorridorWidth); json_insert_pair_into_object( node, "Rooms", SaveClassicRooms(mission)); AddIntPair(node, "Squares", mission->u.Classic.Squares); json_insert_pair_into_object( node, "Doors", SaveClassicDoors(mission)); json_insert_pair_into_object( node, "Pillars", SaveClassicPillars(mission)); break; case MAPTYPE_STATIC: { json_insert_pair_into_object( node, "Tiles", SaveStaticTiles(mission)); json_insert_pair_into_object( node, "StaticItems", SaveStaticItems(mission)); json_insert_pair_into_object( node, "StaticWrecks", SaveStaticWrecks(mission)); json_insert_pair_into_object( node, "StaticCharacters", SaveStaticCharacters(mission)); json_insert_pair_into_object( node, "StaticObjectives", SaveStaticObjectives(mission)); json_insert_pair_into_object( node, "StaticKeys", SaveStaticKeys(mission)); json_insert_pair_into_object( node, "Start", SaveVec2i(mission->u.Static.Start)); json_t *exitNode = json_new_object(); json_insert_pair_into_object( exitNode, "Start", SaveVec2i(mission->u.Static.Exit.Start)); json_insert_pair_into_object( exitNode, "End", SaveVec2i(mission->u.Static.Exit.End)); json_insert_pair_into_object(node, "Exit", exitNode); } break; default: assert(0 && "unknown map type"); break; } json_insert_child(missionsNode, node); } return missionsNode; }