Debian Codegraph @

src/cdogs/map_new.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2014-2017, 2019-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_new.h"
29
30 #include <assert.h>
31 #include <locale.h>
32 #include <stdio.h>
33
34 #include "cwolfmap/cwolfmap.h"
35 #include "door.h"
36 #include "files.h"
37 #include "json_utils.h"
38 #include "log.h"
39 #include "map_archive.h"
40 #include "map_wolf.h"
41 #include "mission.h"
42 #include "player_template.h"
43
44 int MapNewScanJSON(json_t *root, char **title, int *numMissions)
45 {
46 int err = 0;
47 int version;
48 LoadInt(&version, root, "Version");
49 if (version > MAP_VERSION || version <= 0)
50 {
51 err = -1;
52 goto bail;
53 }
54 if (title)
55 {
56 *title = GetString(root, "Title");
57 }
58 if (numMissions != NULL)
59 {
60 *numMissions = 0;
61 if (version < 3)
62 {
63 for (json_t *missionNode =
64 json_find_first_label(root, "Missions")->child->child;
65 missionNode; missionNode = missionNode->next)
66 {
67 (*numMissions)++;
68 }
69 }
70 else
71 {
72 LoadInt(numMissions, root, "Missions");
73 }
74 }
75
76 bail:
77 return err;
78 }
79
80 int MapNewLoad(const char *filename, CampaignSetting *c)
81 {
82 int err = 0;
83
84 if (IsCampaignOldFile(filename))
85 {
86 CampaignSettingOld cOld;
87 memset(&cOld, 0, sizeof cOld);
88 err = LoadCampaignOld(filename, &cOld);
89 if (!err)
90 {
91 ConvertCampaignSetting(c, &cOld);
92 }
93 CFREE(cOld.missions);
94 CFREE(cOld.characters);
95 return err;
96 }
97
98 if (strcmp(StrGetFileExt(filename), "cdogscpn") == 0 ||
99 strcmp(StrGetFileExt(filename), "CDOGSCPN") == 0)
100 {
101 return MapNewLoadArchive(filename, c);
102 }
103
104 int spearMission = 1;
105 char buf[CDOGS_PATH_MAX];
106 if (filename[strlen(filename) - 2] == '?')
107 {
108 spearMission = atoi(&filename[strlen(filename) - 1]);
109 strcpy(buf, filename);
110 buf[strlen(buf) - 2] = '\0';
111 filename = buf;
112 }
113 if (CWGetType(filename, NULL, NULL, spearMission) != CWMAPTYPE_UNKNOWN)
114 {
115 return MapWolfLoad(filename, spearMission, c);
116 }
117
118 // try to load the new map format
119 json_t *root = NULL;
120 int version;
121 FILE *f = fopen(filename, "r");
122 if (f == NULL)
123 {
124 err = -1;
125 goto bail;
126 }
127 if (json_stream_parse(f, &root) != JSON_OK)
128 {
129 printf("Error parsing campaign '%s'\n", filename);
130 err = -1;
131 goto bail;
132 }
133 LoadInt(&version, root, "Version");
134 if (version > 2 || version <= 0)
135 {
136 assert(0 && "not implemented or unknown campaign");
137 err = -1;
138 goto bail;
139 }
140 MapNewLoadCampaignJSON(root, c);
141 LoadMissions(
142 &c->Missions, json_find_first_label(root, "Missions")->child, version);
143 CharacterLoadJSON(
144 &c->characters, &gPlayerTemplates.CustomClasses, root, version);
145
146 bail:
147 json_free_value(&root);
148 if (f != NULL)
149 {
150 fclose(f);
151 }
152 return err;
153 }
154
155 void MapNewLoadCampaignJSON(json_t *root, CampaignSetting *c)
156 {
157 CFREE(c->Title);
158 c->Title = GetString(root, "Title");
159 CFREE(c->Author);
160 c->Author = GetString(root, "Author");
161 CFREE(c->Description);
162 c->Description = GetString(root, "Description");
163 LoadBool(&c->Ammo, root, "Ammo");
164 LoadBool(&c->SkipWeaponMenu, root, "SkipWeaponMenu");
165 LoadBool(&c->BuyAndSell, root, "BuyAndSell");
166 // Default enable random pickups
167 c->RandomPickups = true;
168 LoadBool(&c->RandomPickups, root, "RandomPickups");
169 // Default one second door close
170 c->DoorOpenTicks = FPS_FRAMELIMIT;
171 LoadInt(&c->DoorOpenTicks, root, "DoorOpenTicks");
172 LoadInt(&c->Lives, root, "Lives");
173 LoadInt(&c->MaxLives, root, "MaxLives");
174 LoadInt(&c->PlayerHP, root, "PlayerHP");
175 LoadInt(&c->PlayerMaxHP, root, "PlayerMaxHP");
176 LoadInt(&c->PlayerExcessHP, root, "PlayerExcessHP");
177 }
178
179 static void LoadMissionObjectives(
180 CArray *objectives, json_t *objectivesNode, const int version);
181 static void LoadWeapons(CArray *weapons, json_t *weaponsNode);
182 static void LoadRooms(RoomParams *r, json_t *roomsNode);
183 static void LoadDoors(DoorParams *d, json_t *doorsNode);
184 static void LoadPillars(PillarParams *p, json_t *pillarsNode);
185 void LoadMissions(CArray *missions, json_t *missionsNode, int version)
186 {
187 json_t *child;
188 int mission = 0;
189 for (child = missionsNode->child; child; child = child->next, mission++)
190 {
191 Mission m;
192 MissionInit(&m);
193 m.Title = GetString(child, "Title");
194 m.Description = GetString(child, "Description");
195 JSON_UTILS_LOAD_ENUM(m.Type, child, "Type", StrMapType);
196 LoadInt(&m.Size.x, child, "Width");
197 LoadInt(&m.Size.y, child, "Height");
198 if (version <= 9)
199 {
200 int style;
201 LoadInt(&style, child, "ExitStyle");
202 strcpy(m.ExitStyle, IntExitStyle(style));
203 }
204 else
205 {
206 char *tmp = GetString(child, "ExitStyle");
207 strcpy(m.ExitStyle, tmp);
208 CFREE(tmp);
209 }
210 if (version <= 8)
211 {
212 int keyStyle;
213 LoadInt(&keyStyle, child, "KeyStyle");
214 strcpy(m.KeyStyle, IntKeyStyle(keyStyle));
215 }
216 else
217 {
218 char *tmp = GetString(child, "KeyStyle");
219 strcpy(m.KeyStyle, tmp);
220 CFREE(tmp);
221 }
222 LoadMissionObjectives(
223 &m.Objectives, json_find_first_label(child, "Objectives")->child,
224 version);
225 LoadIntArray(&m.Enemies, child, "Enemies");
226 LoadIntArray(&m.SpecialChars, child, "SpecialChars");
227 if (version <= 3)
228 {
229 CArray items;
230 CArrayInit(&items, sizeof(int));
231 LoadIntArray(&items, child, "Items");
232 CArray densities;
233 CArrayInit(&densities, sizeof(int));
234 LoadIntArray(&densities, child, "ItemDensities");
235 for (int i = 0; i < (int)items.size; i++)
236 {
237 MapObjectDensity mod;
238 mod.M = IntMapObject(*(int *)CArrayGet(&items, i));
239 mod.Density = *(int *)CArrayGet(&densities, i);
240 CArrayPushBack(&m.MapObjectDensities, &mod);
241 }
242 }
243 else
244 {
245 json_t *modsNode =
246 json_find_first_label(child, "MapObjectDensities");
247 if (modsNode && modsNode->child)
248 {
249 modsNode = modsNode->child;
250 for (json_t *modNode = modsNode->child; modNode;
251 modNode = modNode->next)
252 {
253 MapObjectDensity mod;
254 mod.M = StrMapObject(
255 json_find_first_label(modNode, "MapObject")
256 ->child->text);
257 LoadInt(&mod.Density, modNode, "Density");
258 CArrayPushBack(&m.MapObjectDensities, &mod);
259 }
260 }
261 }
262 json_t *pcsNode =
263 json_find_first_label(child, "PickupCounts");
264 if (pcsNode && pcsNode->child)
265 {
266 pcsNode = pcsNode->child;
267 for (json_t *pcNode = pcsNode->child; pcNode;
268 pcNode = pcNode->next)
269 {
270 PickupCount pc;
271 pc.P = StrPickupClass(
272 json_find_first_label(pcNode, "Pickup")
273 ->child->text);
274 LoadInt(&pc.Count, pcNode, "Count");
275 CArrayPushBack(&m.PickupCounts, &pc);
276 }
277 }
278 LoadInt(&m.EnemyDensity, child, "EnemyDensity");
279 LoadWeapons(
280 &m.Weapons, json_find_first_label(child, "Weapons")->child);
281 LoadBool(&m.WeaponPersist, child, "WeaponPersist");
282 LoadBool(&m.SkipDebrief, child, "SkipDebrief");
283 m.Music.Type = MUSIC_SRC_DYNAMIC;
284 LoadStr(&m.Music.Data.Filename, child, "Song");
285 switch (m.Type)
286 {
287 case MAPTYPE_CLASSIC:
288 LoadMissionTileClasses(&m.u.Classic.TileClasses, child, version);
289 LoadInt(&m.u.Classic.Walls, child, "Walls");
290 LoadInt(&m.u.Classic.WallLength, child, "WallLength");
291 LoadInt(&m.u.Classic.CorridorWidth, child, "CorridorWidth");
292 LoadRooms(
293 &m.u.Classic.Rooms,
294 json_find_first_label(child, "Rooms")->child);
295 LoadInt(&m.u.Classic.Squares, child, "Squares");
296 m.u.Classic.ExitEnabled = true;
297 LoadBool(&m.u.Classic.ExitEnabled, child, "ExitEnabled");
298 LoadDoors(
299 &m.u.Classic.Doors,
300 json_find_first_label(child, "Doors")->child);
301 LoadPillars(
302 &m.u.Classic.Pillars,
303 json_find_first_label(child, "Pillars")->child);
304 break;
305 case MAPTYPE_STATIC:
306 if (!MissionStaticTryLoadJSON(
307 &m.u.Static, child, m.Size, version, mission))
308 {
309 continue;
310 }
311 break;
312 case MAPTYPE_CAVE: {
313 LoadMissionTileClasses(&m.u.Cave.TileClasses, child, version);
314 LoadInt(&m.u.Cave.FillPercent, child, "FillPercent");
315 LoadInt(&m.u.Cave.Repeat, child, "Repeat");
316 LoadInt(&m.u.Cave.R1, child, "R1");
317 LoadInt(&m.u.Cave.R2, child, "R2");
318 json_t *roomsNode = json_find_first_label(child, "Rooms");
319 if (roomsNode != NULL && roomsNode->child != NULL)
320 {
321 LoadRooms(&m.u.Cave.Rooms, roomsNode->child);
322 }
323 LoadInt(&m.u.Cave.Squares, child, "Squares");
324 m.u.Cave.ExitEnabled = true;
325 LoadBool(&m.u.Cave.ExitEnabled, child, "ExitEnabled");
326 if (version < 14)
327 {
328 m.u.Cave.DoorsEnabled = true;
329 }
330 else
331 {
332 LoadBool(&m.u.Cave.DoorsEnabled, child, "DoorsEnabled");
333 }
334 }
335 break;
336 case MAPTYPE_INTERIOR:
337 LoadMissionTileClasses(&m.u.Interior.TileClasses, child, version);
338 LoadInt(&m.u.Interior.CorridorWidth, child, "CorridorWidth");
339 LoadRooms(
340 &m.u.Interior.Rooms,
341 json_find_first_label(child, "Rooms")->child);
342 m.u.Interior.ExitEnabled = true;
343 LoadBool(&m.u.Interior.ExitEnabled, child, "ExitEnabled");
344 LoadDoors(
345 &m.u.Interior.Doors,
346 json_find_first_label(child, "Doors")->child);
347 LoadPillars(
348 &m.u.Interior.Pillars,
349 json_find_first_label(child, "Pillars")->child);
350 break;
351 default:
352 assert(0 && "unknown map type");
353 continue;
354 }
355 CArrayPushBack(missions, &m);
356 }
357 }
358
359 void LoadMissionTileClasses(
360 MissionTileClasses *mtc, json_t *node, const int version)
361 {
362 if (version <= 14)
363 {
364 char wallStyle[CDOGS_FILENAME_MAX];
365 char floorStyle[CDOGS_FILENAME_MAX];
366 char roomStyle[CDOGS_FILENAME_MAX];
367 if (version <= 10)
368 {
369 int style;
370 LoadInt(&style, node, "WallStyle");
371 strcpy(wallStyle, IntWallStyle(style));
372 LoadInt(&style, node, "FloorStyle");
373 strcpy(floorStyle, IntFloorStyle(style));
374 LoadInt(&style, node, "RoomStyle");
375 strcpy(roomStyle, IntRoomStyle(style));
376 }
377 else
378 {
379 char *tmp = GetString(node, "WallStyle");
380 strcpy(wallStyle, tmp);
381 CFREE(tmp);
382 tmp = GetString(node, "FloorStyle");
383 strcpy(floorStyle, tmp);
384 CFREE(tmp);
385 tmp = GetString(node, "RoomStyle");
386 strcpy(roomStyle, tmp);
387 CFREE(tmp);
388 }
389 char doorStyle[CDOGS_FILENAME_MAX];
390 if (version <= 5)
391 {
392 int doorStyleInt;
393 LoadInt(&doorStyleInt, node, "DoorStyle");
394 strcpy(doorStyle, IntDoorStyle(doorStyleInt));
395 }
396 else
397 {
398 char *tmp = GetString(node, "DoorStyle");
399 strcpy(doorStyle, tmp);
400 CFREE(tmp);
401 }
402 color_t wallMask;
403 color_t floorMask;
404 color_t roomMask;
405 color_t altMask;
406 if (version <= 4)
407 {
408 // Load colour indices
409 int wc, fc, rc, ac;
410 LoadInt(&wc, node, "WallColor");
411 LoadInt(&fc, node, "FloorColor");
412 LoadInt(&rc, node, "RoomColor");
413 LoadInt(&ac, node, "AltColor");
414 wallMask = RangeToColor(wc);
415 floorMask = RangeToColor(fc);
416 roomMask = RangeToColor(rc);
417 altMask = RangeToColor(ac);
418 }
419 else
420 {
421 LoadColor(&wallMask, node, "WallMask");
422 LoadColor(&floorMask, node, "FloorMask");
423 LoadColor(&roomMask, node, "RoomMask");
424 LoadColor(&altMask, node, "AltMask");
425 }
426 TileClassInit(
427 &mtc->Wall, &gPicManager, &gTileWall, wallStyle,
428 TileClassBaseStyleType(TILE_CLASS_WALL), wallMask, altMask);
429 TileClassInit(
430 &mtc->Floor, &gPicManager, &gTileFloor, floorStyle,
431 TileClassBaseStyleType(TILE_CLASS_FLOOR), floorMask, altMask);
432 TileClassInit(
433 &mtc->Room, &gPicManager, &gTileRoom, roomStyle,
434 TileClassBaseStyleType(TILE_CLASS_FLOOR), roomMask, altMask);
435 TileClassInit(
436 &mtc->Door, &gPicManager, &gTileDoor, doorStyle,
437 TileClassBaseStyleType(TILE_CLASS_DOOR), colorWhite, colorWhite);
438 }
439 else
440 {
441 const json_t *class =
442 json_find_first_label(node, "TileClasses")->child;
443 TileClassLoadJSON(
444 &mtc->Wall, json_find_first_label(class, "Wall")->child);
445 TileClassLoadJSON(
446 &mtc->Floor, json_find_first_label(class, "Floor")->child);
447 TileClassLoadJSON(
448 &mtc->Room, json_find_first_label(class, "Room")->child);
449 TileClassLoadJSON(
450 &mtc->Door, json_find_first_label(class, "Door")->child);
451 }
452 }
453 static void LoadMissionObjectives(
454 CArray *objectives, json_t *objectivesNode, const int version)
455 {
456 json_t *child;
457 for (child = objectivesNode->child; child; child = child->next)
458 {
459 Objective o;
460 ObjectiveLoadJSON(&o, child, version);
461 CArrayPushBack(objectives, &o);
462 }
463 }
464 static void AddWeapon(CArray *weapons, const CArray *guns);
465 static void LoadWeapons(CArray *weapons, json_t *weaponsNode)
466 {
467 if (!weaponsNode->child)
468 {
469 // enable all weapons
470 AddWeapon(weapons, &gWeaponClasses.Guns);
471 AddWeapon(weapons, &gWeaponClasses.CustomGuns);
472 }
473 else
474 {
475 for (json_t *child = weaponsNode->child; child; child = child->next)
476 {
477 const WeaponClass *wc = StrWeaponClass(child->text);
478 if (wc == NULL)
479 {
480 continue;
481 }
482 CArrayPushBack(weapons, &wc);
483 }
484 }
485 }
486 static void AddWeapon(CArray *weapons, const CArray *guns)
487 {
488 for (int i = 0; i < (int)guns->size; i++)
489 {
490 const WeaponClass *wc = CArrayGet(guns, i);
491 if (wc->IsRealGun)
492 {
493 CArrayPushBack(weapons, &wc);
494 }
495 }
496 }
497 static void LoadRooms(RoomParams *r, json_t *roomsNode)
498 {
499 LoadInt(&r->Count, roomsNode, "Count");
500 LoadInt(&r->Min, roomsNode, "Min");
501 LoadInt(&r->Max, roomsNode, "Max");
502 LoadBool(&r->Edge, roomsNode, "Edge");
503 LoadBool(&r->Overlap, roomsNode, "Overlap");
504 LoadInt(&r->Walls, roomsNode, "Walls");
505 LoadInt(&r->WallLength, roomsNode, "WallLength");
506 LoadInt(&r->WallPad, roomsNode, "WallPad");
507 }
508 static void LoadDoors(DoorParams *d, json_t *doorsNode)
509 {
510 LoadBool(&d->Enabled, doorsNode, "Enabled");
511 LoadInt(&d->Min, doorsNode, "Min");
512 LoadInt(&d->Max, doorsNode, "Max");
513 LoadBool(&d->RandomPos, doorsNode, "RandomPos");
514 }
515 static void LoadPillars(PillarParams *p, json_t *pillarsNode)
516 {
517 LoadInt(&p->Count, pillarsNode, "Count");
518 LoadInt(&p->Min, pillarsNode, "Min");
519 LoadInt(&p->Max, pillarsNode, "Max");
520 }