Debian Codegraph @

src/cdogs/map_archive.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2014-2023 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "map_archive.h"
29
30 #include <tinydir/tinydir.h>
31
32 #include "ammo.h"
33 #include "character_class.h"
34 #include "files.h"
35 #include "json_utils.h"
36 #include "log.h"
37 #include "map_new.h"
38 #include "pickup.h"
39 #include "player_template.h"
40
41 static json_t *ReadArchiveJSON(const char *archive, const char *filename);
42 int MapNewScanArchive(const char *filename, char **title, int *numMissions)
43 {
44 int err = 0;
45 json_t *root = ReadArchiveJSON(filename, "campaign.json");
46 if (root == NULL)
47 {
48 err = -1;
49 goto bail;
50 }
51 err = MapNewScanJSON(root, title, numMissions);
52 if (err < 0)
53 {
54 goto bail;
55 }
56
57 bail:
58 json_free_value(&root);
59 return err;
60 }
61
62 int MapLoadCampaignJSON(const char *filename, CampaignSetting *c, int *version)
63 {
64 int err = 0;
65 json_t *root = ReadArchiveJSON(filename, "campaign.json");
66 if (root == NULL)
67 {
68 err = -1;
69 goto bail;
70 }
71 if (version)
72 {
73 LoadInt(version, root, "Version");
74 if (*version > MAP_VERSION || *version <= 2)
75 {
76 err = -1;
77 goto bail;
78 }
79 }
80 MapNewLoadCampaignJSON(root, c);
81
82 bail:
83 json_free_value(&root);
84 return err;
85 }
86
87 static void LoadArchiveSounds(
88 SoundDevice *device, const char *archive, const char *dirname);
89 static void LoadArchivePics(PicManager *pm, map_t cc, const char *archive);
90 int MapNewLoadArchive(const char *filename, CampaignSetting *c)
91 {
92 LOG(LM_MAP, LL_DEBUG, "Loading archive map %s", filename);
93 int err = 0;
94 json_t *root = NULL;
95 int version = 0;
96 err = MapLoadCampaignJSON(filename, c, &version);
97 if (err != 0)
98 {
99 goto bail;
100 }
101
102 // Load any custom data
103 LoadArchiveSounds(&gSoundDevice, filename, "sounds");
104
105 LoadArchivePics(&gPicManager, gCharSpriteClasses.customClasses, filename);
106
107 root = ReadArchiveJSON(filename, "particles.json");
108 if (root != NULL)
109 {
110 ParticleClassesLoadJSON(&gParticleClasses.CustomClasses, root);
111 }
112
113 root = ReadArchiveJSON(filename, "character_classes.json");
114 if (root != NULL)
115 {
116 CharacterClassesLoadJSON(&gCharacterClasses.CustomClasses, root);
117 }
118
119 root = ReadArchiveJSON(filename, "bullets.json");
120 if (root != NULL)
121 {
122 BulletLoadJSON(&gBulletClasses, &gBulletClasses.CustomClasses, root);
123 }
124
125 bool hasCustomAmmo = false;
126 root = ReadArchiveJSON(filename, "ammo.json");
127 if (root != NULL)
128 {
129 AmmoLoadJSON(&gAmmo.CustomAmmo, root);
130 json_free_value(&root);
131 hasCustomAmmo = true;
132 }
133
134 bool hasCustomGuns = false;
135 root = ReadArchiveJSON(filename, "guns.json");
136 if (root != NULL)
137 {
138 WeaponClassesLoadJSON(
139 &gWeaponClasses, &gWeaponClasses.CustomGuns, root);
140 json_free_value(&root);
141 hasCustomGuns = true;
142 }
143
144 BulletLoadWeapons(&gBulletClasses);
145
146 root = ReadArchiveJSON(filename, "pickups.json");
147 if (root != NULL)
148 {
149 PickupClassesLoadJSON(&gPickupClasses.CustomClasses, root);
150 }
151 if (hasCustomAmmo)
152 {
153 PickupClassesLoadAmmo(
154 &gPickupClasses.CustomClasses, &gAmmo.CustomAmmo);
155 }
156 if (hasCustomGuns)
157 {
158 PickupClassesLoadGuns(
159 &gPickupClasses.CustomClasses, &gWeaponClasses.CustomGuns);
160 }
161 PickupClassesLoadKeys(&gPickupClasses.KeyClasses);
162
163 root = ReadArchiveJSON(filename, "map_objects.json");
164 if (root != NULL)
165 {
166 MapObjectsLoadJSON(&gMapObjects.CustomClasses, root);
167 }
168 MapObjectsLoadAmmoAndGunSpawners(
169 &gMapObjects, &gAmmo, &gWeaponClasses, true);
170
171 root = ReadArchiveJSON(filename, "missions.json");
172 if (root == NULL)
173 {
174 err = -1;
175 goto bail;
176 }
177 LoadMissions(
178 &c->Missions, json_find_first_label(root, "Missions")->child, version);
179 json_free_value(&root);
180
181 // Note: some campaigns don't have characters (e.g. dogfights)
182 root = ReadArchiveJSON(filename, "characters.json");
183 if (root != NULL)
184 {
185 CharacterLoadJSON(
186 &c->characters, &gPlayerTemplates.CustomClasses, root, version);
187 }
188
189 bail:
190 json_free_value(&root);
191 return err;
192 }
193
194 static json_t *ReadArchiveJSON(const char *archive, const char *filename)
195 {
196 json_t *root = NULL;
197 char path[CDOGS_PATH_MAX];
198 sprintf(path, "%s/%s", archive, filename);
199 char *buf = ReadFileIntoBuf(path, "rb");
200 if (buf == NULL)
201 goto bail;
202 const enum json_error e = json_parse_document(&root, buf);
203 if (e != JSON_OK)
204 {
205 LOG(LM_MAP, LL_ERROR, "Invalid syntax in JSON file (%s) error(%d)",
206 filename, (int)e);
207 root = NULL;
208 goto bail;
209 }
210
211 bail:
212 CFREE(buf);
213 return root;
214 }
215
216 static void LoadArchiveSounds(
217 SoundDevice *device, const char *archive, const char *dirname)
218 {
219 char path[CDOGS_PATH_MAX];
220 sprintf(path, "%s/%s", archive, dirname);
221 SoundLoadDir(device->customSounds, path, NULL);
222 }
223 static void LoadArchivePics(PicManager *pm, map_t cc, const char *archive)
224 {
225 char path[CDOGS_PATH_MAX];
226 sprintf(path, "%s/graphics", archive);
227 PicManagerLoadDir(
228 pm, path, NULL, pm->customPics, pm->customSprites, false);
229 sprintf(path, "%s/graphics_hd", archive);
230 PicManagerLoadDir(pm, path, NULL, pm->customPics, pm->customSprites, true);
231 CharSpriteClassesLoadDir(cc, archive);
232 }
233
234 static json_t *SaveMissions(CArray *a);
235 int MapArchiveSave(const char *filename, CampaignSetting *c)
236 {
237 int res = 1;
238 json_t *root = NULL;
239
240 char relbuf[CDOGS_PATH_MAX];
241 if (strcmp(StrGetFileExt(filename), "cdogscpn") == 0 ||
242 strcmp(StrGetFileExt(filename), "CDOGSCPN") == 0)
243 {
244 strcpy(relbuf, filename);
245 }
246 else
247 {
248 sprintf(relbuf, "%s.cdogscpn", filename);
249 }
250 char buf[CDOGS_PATH_MAX];
251 RealPath(relbuf, buf);
252 // Make dir but ignore error, as we may be saving over an existing dir
253 mkdir_deep(buf);
254
255 // Campaign
256 root = json_new_object();
257 AddIntPair(root, "Version", MAP_VERSION);
258 AddStringPair(root, "Title", c->Title);
259 AddStringPair(root, "Author", c->Author);
260 AddStringPair(root, "Description", c->Description);
261 AddBoolPair(root, "Ammo", c->Ammo);
262 AddBoolPair(root, "SkipWeaponMenu", c->SkipWeaponMenu);
263 AddBoolPair(root, "BuyAndSell", c->BuyAndSell);
264 AddBoolPair(root, "RandomPickups", c->RandomPickups);
265 AddIntPair(root, "DoorOpenTicks", c->DoorOpenTicks);
266 AddIntPair(root, "Lives", c->Lives);
267 AddIntPair(root, "MaxLives", c->MaxLives);
268 AddIntPair(root, "PlayerHP", c->PlayerHP);
269 AddIntPair(root, "PlayerMaxHP", c->PlayerMaxHP);
270 AddIntPair(root, "PlayerExcessHP", c->PlayerExcessHP);
271 AddIntPair(root, "Missions", (int)c->Missions.size);
272 char buf2[CDOGS_PATH_MAX];
273 sprintf(buf2, "%s/campaign.json", buf);
274 if (!TrySaveJSONFile(root, buf2))
275 {
276 res = 0;
277 goto bail;
278 }
279
280 json_free_value(&root);
281 root = json_new_object();
282 json_insert_pair_into_object(root, "Missions", SaveMissions(&c->Missions));
283 sprintf(buf2, "%s/missions.json", buf);
284 if (!TrySaveJSONFile(root, buf2))
285 {
286 res = 0;
287 goto bail;
288 }
289
290 if (!CharacterStoreSave(&c->characters, buf))
291 {
292 res = 0;
293 goto bail;
294 }
295
296 bail:
297 json_free_value(&root);
298 return res;
299 }
300
301 static json_t *SaveObjectives(CArray *a);
302 static json_t *SaveWeapons(const CArray *weapons);
303 static json_t *SaveMissionTileClasses(const MissionTileClasses *mtc);
304 static json_t *SaveRooms(const RoomParams r);
305 static json_t *SaveDoors(const DoorParams d);
306 static json_t *SavePillars(const PillarParams p);
307 static json_t *SaveMissions(CArray *a)
308 {
309 json_t *missionsNode = json_new_array();
310 for (int i = 0; i < (int)a->size; i++)
311 {
312 json_t *node = json_new_object();
313 Mission *mission = CArrayGet(a, i);
314 AddStringPair(node, "Title", mission->Title);
315 AddStringPair(node, "Description", mission->Description);
316 AddStringPair(node, "Type", MapTypeStr(mission->Type));
317 AddIntPair(node, "Width", mission->Size.x);
318 AddIntPair(node, "Height", mission->Size.y);
319
320 AddStringPair(node, "ExitStyle", mission->ExitStyle);
321 AddStringPair(node, "KeyStyle", mission->KeyStyle);
322
323 json_insert_pair_into_object(
324 node, "Objectives", SaveObjectives(&mission->Objectives));
325 AddIntArray(node, "Enemies", &mission->Enemies);
326 AddIntArray(node, "SpecialChars", &mission->SpecialChars);
327 json_t *modsNode = json_new_array();
328 for (int j = 0; j < (int)mission->MapObjectDensities.size; j++)
329 {
330 const MapObjectDensity *mod =
331 CArrayGet(&mission->MapObjectDensities, j);
332 json_t *modNode = json_new_object();
333 AddStringPair(modNode, "MapObject", mod->M->Name);
334 AddIntPair(modNode, "Density", mod->Density);
335 json_insert_child(modsNode, modNode);
336 }
337 json_insert_pair_into_object(node, "MapObjectDensities", modsNode);
338 json_t *pcsNode = json_new_array();
339 for (int j = 0; j < (int)mission->PickupCounts.size; j++)
340 {
341 const PickupCount *pc = CArrayGet(&mission->PickupCounts, j);
342 json_t *pcNode = json_new_object();
343 AddStringPair(pcNode, "Pickup", pc->P->Name);
344 AddIntPair(pcNode, "Count", pc->Count);
345 json_insert_child(pcsNode, pcNode);
346 }
347 json_insert_pair_into_object(node, "PickupCounts", pcsNode);
348
349 AddIntPair(node, "EnemyDensity", mission->EnemyDensity);
350 json_insert_pair_into_object(
351 node, "Weapons", SaveWeapons(&mission->Weapons));
352 AddBoolPair(node, "WeaponPersist", mission->WeaponPersist);
353 AddBoolPair(node, "SkipDebrief", mission->SkipDebrief);
354
355 if (mission->Music.Type == MUSIC_SRC_DYNAMIC &&
356 mission->Music.Data.Filename &&
357 strlen(mission->Music.Data.Filename) > 0)
358 {
359 json_insert_pair_into_object(
360 node, "Song", json_new_string(mission->Music.Data.Filename));
361 }
362
363 switch (mission->Type)
364 {
365 case MAPTYPE_CLASSIC:
366 json_insert_pair_into_object(
367 node, "TileClasses",
368 SaveMissionTileClasses(&mission->u.Classic.TileClasses));
369 AddIntPair(node, "Walls", mission->u.Classic.Walls);
370 AddIntPair(node, "WallLength", mission->u.Classic.WallLength);
371 AddIntPair(
372 node, "CorridorWidth", mission->u.Classic.CorridorWidth);
373 json_insert_pair_into_object(
374 node, "Rooms", SaveRooms(mission->u.Classic.Rooms));
375 AddIntPair(node, "Squares", mission->u.Classic.Squares);
376 AddBoolPair(node, "ExitEnabled", mission->u.Classic.ExitEnabled);
377 json_insert_pair_into_object(
378 node, "Doors", SaveDoors(mission->u.Classic.Doors));
379 json_insert_pair_into_object(
380 node, "Pillars", SavePillars(mission->u.Classic.Pillars));
381 break;
382 case MAPTYPE_STATIC:
383 MissionStaticSaveJSON(&mission->u.Static, mission->Size, node);
384 break;
385 case MAPTYPE_CAVE:
386 json_insert_pair_into_object(
387 node, "TileClasses",
388 SaveMissionTileClasses(&mission->u.Cave.TileClasses));
389 AddIntPair(node, "FillPercent", mission->u.Cave.FillPercent);
390 AddIntPair(node, "Repeat", mission->u.Cave.Repeat);
391 AddIntPair(node, "R1", mission->u.Cave.R1);
392 AddIntPair(node, "R2", mission->u.Cave.R2);
393 json_insert_pair_into_object(
394 node, "Rooms", SaveRooms(mission->u.Cave.Rooms));
395 AddIntPair(node, "Squares", mission->u.Cave.Squares);
396 AddBoolPair(node, "ExitEnabled", mission->u.Cave.ExitEnabled);
397 AddBoolPair(node, "DoorsEnabled", mission->u.Cave.DoorsEnabled);
398 break;
399 case MAPTYPE_INTERIOR:
400 json_insert_pair_into_object(
401 node, "TileClasses",
402 SaveMissionTileClasses(&mission->u.Interior.TileClasses));
403 AddIntPair(
404 node, "CorridorWidth", mission->u.Interior.CorridorWidth);
405 json_insert_pair_into_object(
406 node, "Rooms", SaveRooms(mission->u.Interior.Rooms));
407 AddBoolPair(node, "ExitEnabled", mission->u.Interior.ExitEnabled);
408 json_insert_pair_into_object(
409 node, "Doors", SaveDoors(mission->u.Interior.Doors));
410 json_insert_pair_into_object(
411 node, "Pillars", SavePillars(mission->u.Interior.Pillars));
412 break;
413 default:
414 CASSERT(false, "unknown map type");
415 break;
416 }
417
418 json_insert_child(missionsNode, node);
419 }
420 return missionsNode;
421 }
422 static json_t *SaveRooms(const RoomParams r)
423 {
424 json_t *node = json_new_object();
425 AddIntPair(node, "Count", r.Count);
426 AddIntPair(node, "Min", r.Min);
427 AddIntPair(node, "Max", r.Max);
428 AddBoolPair(node, "Edge", r.Edge);
429 AddBoolPair(node, "Overlap", r.Overlap);
430 AddIntPair(node, "Walls", r.Walls);
431 AddIntPair(node, "WallLength", r.WallLength);
432 AddIntPair(node, "WallPad", r.WallPad);
433 return node;
434 }
435 static json_t *SaveWeapons(const CArray *weapons)
436 {
437 json_t *node = json_new_array();
438 for (int i = 0; i < (int)weapons->size; i++)
439 {
440 const WeaponClass **wc = CArrayGet(weapons, i);
441 json_insert_child(node, json_new_string((*wc)->name));
442 }
443 return node;
444 }
445 static json_t *SaveMissionTileClasses(const MissionTileClasses *mtc)
446 {
447 json_t *node = json_new_object();
448 json_insert_pair_into_object(node, "Wall", TileClassSaveJSON(&mtc->Wall));
449 json_insert_pair_into_object(
450 node, "Floor", TileClassSaveJSON(&mtc->Floor));
451 json_insert_pair_into_object(node, "Room", TileClassSaveJSON(&mtc->Room));
452 json_insert_pair_into_object(node, "Door", TileClassSaveJSON(&mtc->Door));
453 return node;
454 }
455 static json_t *SavePillars(const PillarParams p)
456 {
457 json_t *node = json_new_object();
458 AddIntPair(node, "Count", p.Count);
459 AddIntPair(node, "Min", p.Min);
460 AddIntPair(node, "Max", p.Max);
461 return node;
462 }
463 static json_t *SaveDoors(const DoorParams d)
464 {
465 json_t *node = json_new_object();
466 AddBoolPair(node, "Enabled", d.Enabled);
467 AddIntPair(node, "Min", d.Min);
468 AddIntPair(node, "Max", d.Max);
469 AddBoolPair(node, "RandomPos", d.RandomPos);
470 return node;
471 }
472
473 static json_t *SavePickupObjectives(const Objective *o)
474 {
475 json_t *node = json_new_array();
476 CA_FOREACH(const PickupClass *, p, o->u.Pickups)
477 json_insert_child(node, json_new_string((*p)->Name));
478 CA_FOREACH_END()
479 return node;
480 }
481 static json_t *SaveDestroyObjectives(const Objective *o)
482 {
483 json_t *node = json_new_array();
484 CA_FOREACH(const MapObject *, mo, o->u.MapObjects)
485 json_insert_child(node, json_new_string((*mo)->Name));
486 CA_FOREACH_END()
487 return node;
488 }
489 static json_t *SaveObjectives(CArray *a)
490 {
491 json_t *objectivesNode = json_new_array();
492 CA_FOREACH(const Objective, o, *a)
493 json_t *objNode = json_new_object();
494 AddStringPair(objNode, "Description", o->Description);
495 AddStringPair(objNode, "Type", ObjectiveTypeStr(o->Type));
496 switch (o->Type)
497 {
498 case OBJECTIVE_COLLECT:
499 json_insert_pair_into_object(
500 objNode, "Pickups", SavePickupObjectives(o));
501 break;
502 case OBJECTIVE_DESTROY:
503 json_insert_pair_into_object(
504 objNode, "MapObjects", SaveDestroyObjectives(o));
505 break;
506 default:
507 AddIntPair(objNode, "Index", o->u.Index);
508 break;
509 }
510 AddIntPair(objNode, "Count", o->Count);
511 AddIntPair(objNode, "Required", o->Required);
512 AddIntPair(objNode, "Flags", o->Flags);
513 json_insert_child(objectivesNode, objNode);
514 CA_FOREACH_END()
515 return objectivesNode;
516 }