Debian Codegraph @

src/cdogs/character.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2013-2014, 2016, 2019-2021, 2023-2025 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "character.h"
30
31 #include <assert.h>
32
33 #include "actors.h"
34 #include "files.h"
35 #include "json_utils.h"
36 #include "log.h"
37 #include "player_template.h"
38 #include "yajl_utils.h"
39
40 #define CHARACTER_VERSION 14
41
42 #define YAJL_CHECK(func) \
43 if (func != yajl_gen_status_ok) \
44 { \
45 LOG(LM_MAIN, LL_ERROR, "JSON generator error for character\n"); \
46 res = false; \
47 goto bail; \
48 }
49
50 static void CharacterInit(Character *c)
51 {
52 memset(c, 0, sizeof *c);
53 CCALLOC(c->bot, sizeof *c->bot);
54 }
55 static void CharacterTerminate(Character *c)
56 {
57 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
58 {
59 CFREE(c->HeadParts[hp]);
60 }
61 CFREE(c->bot);
62 }
63
64 void CharacterStoreInit(CharacterStore *store)
65 {
66 memset(store, 0, sizeof *store);
67 CArrayInit(&store->OtherChars, sizeof(Character));
68 CArrayInit(&store->prisonerIds, sizeof(int));
69 CArrayInit(&store->baddieIds, sizeof(int));
70 CArrayInit(&store->specialIds, sizeof(int));
71 }
72
73 void CharacterStoreTerminate(CharacterStore *store)
74 {
75 CA_FOREACH(Character, c, store->OtherChars)
76 CharacterTerminate(c);
77 CA_FOREACH_END()
78 CArrayTerminate(&store->OtherChars);
79 CArrayTerminate(&store->prisonerIds);
80 CArrayTerminate(&store->baddieIds);
81 CArrayTerminate(&store->specialIds);
82 memset(store, 0, sizeof *store);
83 }
84
85 void CharacterStoreCopy(
86 CharacterStore *dst, const CharacterStore *src, CArray *playerTemplates)
87 {
88 CharacterStoreTerminate(dst);
89 PlayerTemplatesClear(playerTemplates);
90 CharacterStoreInit(dst);
91 CArrayCopy(&dst->OtherChars, &src->OtherChars);
92 CA_FOREACH(Character, c, dst->OtherChars)
93 CharacterCopy(c, CArrayGet(&src->OtherChars, _ca_index), playerTemplates);
94 CA_FOREACH_END()
95 CArrayCopy(&dst->prisonerIds, &src->prisonerIds);
96 CArrayCopy(&dst->baddieIds, &src->baddieIds);
97 CArrayCopy(&dst->specialIds, &src->specialIds);
98 }
99
100 void CharacterStoreResetOthers(CharacterStore *store)
101 {
102 CArrayClear(&store->prisonerIds);
103 CArrayClear(&store->baddieIds);
104 CArrayClear(&store->specialIds);
105 }
106
107 void CharacterLoadJSON(
108 CharacterStore *c, CArray *playerTemplates, json_t *root, int version)
109 {
110 LoadInt(&version, root, "Version");
111 json_t *child = json_find_first_label(root, "Characters")->child->child;
112 CharacterStoreTerminate(c);
113 PlayerTemplatesClear(playerTemplates);
114 CharacterStoreInit(c);
115 while (child)
116 {
117 Character *ch = CharacterStoreAddOther(c);
118 char *tmp;
119
120 // Face
121 if (version < 13)
122 {
123 // Old face names, before face + hair split
124 if (version < 7)
125 {
126 int face;
127 LoadInt(&face, child, "face");
128 CSTRDUP(tmp, IntCharacterFace(face));
129 }
130 else
131 {
132 tmp = GetString(child, "Class");
133 }
134 char *face = NULL;
135 CharacterOldFaceToHeadParts(tmp, &face, ch->HeadParts);
136 CFREE(tmp);
137 ch->Class = StrCharacterClass(face);
138 CFREE(face);
139 }
140 else
141 {
142 tmp = GetString(child, "Class");
143 ch->Class = StrCharacterClass(tmp);
144 CFREE(tmp);
145 tmp = NULL;
146 LoadStr(&ch->HeadParts[HEAD_PART_HAIR], child, "HairType");
147 if (version < 14)
148 {
149 CharacterOldHairToHeadParts(ch->HeadParts);
150 }
151 else
152 {
153 LoadStr(
154 &ch->HeadParts[HEAD_PART_FACEHAIR], child, "FacehairType");
155 LoadStr(&ch->HeadParts[HEAD_PART_HAT], child, "HatType");
156 LoadStr(
157 &ch->HeadParts[HEAD_PART_GLASSES], child, "GlassesType");
158 }
159 }
160 CASSERT(ch->Class != NULL, "Cannot load character class");
161
162 // Colours
163 if (version < 7)
164 {
165 // Old version stored character looks as palette indices
166 int skin, arm, body, leg, hair;
167 LoadInt(&skin, child, "skin");
168 LoadInt(&arm, child, "arm");
169 LoadInt(&body, child, "body");
170 LoadInt(&leg, child, "leg");
171 LoadInt(&hair, child, "hair");
172 ConvertCharacterColors(skin, arm, body, leg, hair, &ch->Colors);
173 }
174 else
175 {
176 LoadColor(&ch->Colors.Skin, child, "Skin");
177 LoadColor(&ch->Colors.Arms, child, "Arms");
178 LoadColor(&ch->Colors.Body, child, "Body");
179 LoadColor(&ch->Colors.Legs, child, "Legs");
180 LoadColor(&ch->Colors.Hair, child, "Hair");
181 }
182 ch->Colors.Feet = ch->Colors.Legs;
183 if (version < 13)
184 {
185 ConvertHairColors(ch, ch->Class->Name);
186 }
187 else
188 {
189 LoadColor(&ch->Colors.Feet, child, "Feet");
190 }
191 if (version < 14)
192 {
193 ch->Colors.Facehair = ch->Colors.Hair;
194 ch->Colors.Hat = ch->Colors.Hair;
195 ch->Colors.Glasses = ch->Colors.Hair;
196 }
197 else
198 {
199 LoadColor(&ch->Colors.Facehair, child, "Facehair");
200 LoadColor(&ch->Colors.Hat, child, "Hat");
201 LoadColor(&ch->Colors.Glasses, child, "Glasses");
202 }
203
204 LoadStr(&ch->PlayerTemplateName, child, "PlayerTemplateName");
205 LoadFullInt(&ch->speed, child, "speed");
206 tmp = GetString(child, "Gun");
207 ch->Gun = StrWeaponClass(tmp);
208 CFREE(tmp);
209 tmp = NULL;
210 LoadStr(&tmp, child, "Melee");
211 if (tmp != NULL)
212 {
213 ch->Melee = StrWeaponClass(tmp);
214 CFREE(tmp);
215 }
216 LoadInt(&ch->maxHealth, child, "maxHealth");
217 ch->excessHealth = ch->maxHealth * 2;
218 LoadInt(&ch->excessHealth, child, "excessHealth");
219 int flags;
220 LoadInt(&flags, child, "flags");
221 ch->flags = flags;
222
223 tmp = NULL;
224 LoadStr(&tmp, child, "Drop");
225 if (tmp != NULL)
226 {
227 ch->Drop = StrPickupClass(tmp);
228 CFREE(tmp);
229 }
230
231 LoadInt(&ch->bot->probabilityToMove, child, "probabilityToMove");
232 LoadInt(&ch->bot->probabilityToTrack, child, "probabilityToTrack");
233 LoadInt(&ch->bot->probabilityToShoot, child, "probabilityToShoot");
234 LoadInt(&ch->bot->actionDelay, child, "actionDelay");
235
236 if (ch->PlayerTemplateName != NULL)
237 {
238 PlayerTemplateAddCharacter(playerTemplates, ch);
239 }
240
241 child = child->next;
242 }
243 }
244
245 bool CharacterStoreSave(CharacterStore *s, const char *path)
246 {
247 bool res = true;
248 yajl_gen g = yajl_gen_alloc(NULL);
249 if (g == NULL)
250 {
251 LOG(LM_MAIN, LL_ERROR,
252 "Unable to alloc JSON generator for saving character\n");
253 res = false;
254 goto bail;
255 }
256
257 YAJL_CHECK(yajl_gen_map_open(g));
258 YAJL_CHECK(YAJLAddIntPair(g, "Version", CHARACTER_VERSION));
259
260 YAJL_CHECK(yajl_gen_string(
261 g, (const unsigned char *)"Characters", strlen("Characters")));
262 YAJL_CHECK(yajl_gen_array_open(g));
263 CA_FOREACH(Character, c, s->OtherChars)
264 if (!CharacterSave(g, c))
265 {
266 res = false;
267 goto bail;
268 }
269 CA_FOREACH_END()
270 YAJL_CHECK(yajl_gen_array_close(g));
271
272 YAJL_CHECK(yajl_gen_map_close(g));
273 char buf[CDOGS_PATH_MAX];
274 sprintf(buf, "%s/characters.json", path);
275 if (!YAJLTrySaveJSONFile(g, buf))
276 {
277 res = false;
278 goto bail;
279 }
280
281 bail:
282 if (g)
283 {
284 yajl_gen_clear(g);
285 yajl_gen_free(g);
286 }
287 return res;
288 }
289
290 Character *CharacterStoreAddOther(CharacterStore *store)
291 {
292 return CharacterStoreInsertOther(store, store->OtherChars.size);
293 }
294 Character *CharacterStoreInsertOther(CharacterStore *store, const size_t idx)
295 {
296 Character newChar;
297 CharacterInit(&newChar);
298 CArrayInsert(&store->OtherChars, idx, &newChar);
299 return CArrayGet(&store->OtherChars, idx);
300 }
301 void CharacterStoreDeleteOther(CharacterStore *store, int idx)
302 {
303 CArrayDelete(&store->OtherChars, idx);
304 }
305
306 void CharacterStoreAddPrisoner(CharacterStore *store, int character)
307 {
308 CArrayPushBack(&store->prisonerIds, &character);
309 }
310
311 void CharacterStoreAddBaddie(CharacterStore *store, int character)
312 {
313 CArrayPushBack(&store->baddieIds, &character);
314 }
315 void CharacterStoreAddSpecial(CharacterStore *store, int character)
316 {
317 CArrayPushBack(&store->specialIds, &character);
318 }
319 void CharacterStoreDeleteBaddie(CharacterStore *store, int idx)
320 {
321 CArrayDelete(&store->baddieIds, idx);
322 }
323 void CharacterStoreDeleteSpecial(CharacterStore *store, int idx)
324 {
325 CArrayDelete(&store->specialIds, idx);
326 }
327
328 int CharacterStoreGetPrisonerId(const CharacterStore *store, const int i)
329 {
330 return *(int *)CArrayGet(&store->prisonerIds, i);
331 }
332
333 int CharacterStoreGetSpecialId(const CharacterStore *store, const int i)
334 {
335 return *(int *)CArrayGet(&store->specialIds, i);
336 }
337 int CharacterStoreGetRandomBaddieId(const CharacterStore *store)
338 {
339 const int idx = RAND_INT(0, store->baddieIds.size);
340 return *(int *)CArrayGet(&store->baddieIds, idx);
341 }
342 int CharacterStoreGetRandomSpecialId(const CharacterStore *store)
343 {
344 const int idx = RAND_INT(0, store->specialIds.size);
345 return *(int *)CArrayGet(&store->specialIds, idx);
346 }
347
348 void CharacterCopy(
349 Character *dst, const Character *src, CArray *playerTemplates)
350 {
351 memcpy(dst, src, sizeof *dst);
352 const CharBot *cb = dst->bot;
353 if (cb)
354 {
355 CMALLOC(dst->bot, sizeof *dst->bot);
356 memcpy(dst->bot, cb, sizeof *cb);
357 }
358 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
359 {
360 if (dst->HeadParts[hp] != NULL)
361 {
362 char *hpCopy = NULL;
363 CSTRDUP(hpCopy, dst->HeadParts[hp]);
364 dst->HeadParts[hp] = hpCopy;
365 }
366 }
367 if (dst->PlayerTemplateName != NULL && playerTemplates)
368 {
369 PlayerTemplateAddCharacter(playerTemplates, dst);
370 }
371 }
372
373 bool CharacterSave(yajl_gen g, const Character *c)
374 {
375 bool res = true;
376
377 YAJL_CHECK(yajl_gen_map_open(g));
378 YAJL_CHECK(YAJLAddStringPair(g, "Class", c->Class->Name));
379 if (c->PlayerTemplateName)
380 {
381 YAJL_CHECK(
382 YAJLAddStringPair(g, "PlayerTemplateName", c->PlayerTemplateName));
383 }
384 if (c->HeadParts[HEAD_PART_HAIR])
385 {
386 YAJL_CHECK(
387 YAJLAddStringPair(g, "HairType", c->HeadParts[HEAD_PART_HAIR]));
388 }
389 if (c->HeadParts[HEAD_PART_FACEHAIR])
390 {
391 YAJL_CHECK(YAJLAddStringPair(
392 g, "FacehairType", c->HeadParts[HEAD_PART_FACEHAIR]));
393 }
394 if (c->HeadParts[HEAD_PART_HAT])
395 {
396 YAJL_CHECK(
397 YAJLAddStringPair(g, "HatType", c->HeadParts[HEAD_PART_HAT]));
398 }
399 if (c->HeadParts[HEAD_PART_GLASSES])
400 {
401 YAJL_CHECK(YAJLAddStringPair(
402 g, "GlassesType", c->HeadParts[HEAD_PART_GLASSES]));
403 }
404 YAJL_CHECK(YAJLAddColorPair(g, "Skin", c->Colors.Skin));
405 YAJL_CHECK(YAJLAddColorPair(g, "Arms", c->Colors.Arms));
406 YAJL_CHECK(YAJLAddColorPair(g, "Body", c->Colors.Body));
407 YAJL_CHECK(YAJLAddColorPair(g, "Legs", c->Colors.Legs));
408 YAJL_CHECK(YAJLAddColorPair(g, "Hair", c->Colors.Hair));
409 YAJL_CHECK(YAJLAddColorPair(g, "Feet", c->Colors.Feet));
410 YAJL_CHECK(YAJLAddColorPair(g, "Facehair", c->Colors.Facehair));
411 YAJL_CHECK(YAJLAddColorPair(g, "Hat", c->Colors.Hat));
412 YAJL_CHECK(YAJLAddColorPair(g, "Glasses", c->Colors.Glasses));
413 YAJL_CHECK(YAJLAddIntPair(g, "speed", (int)(c->speed * 256)));
414 if (c->Gun)
415 {
416 YAJL_CHECK(YAJLAddStringPair(g, "Gun", c->Gun->name));
417 }
418 if (c->Melee != NULL)
419 {
420 YAJL_CHECK(YAJLAddStringPair(g, "Melee", c->Melee->name));
421 }
422 YAJL_CHECK(YAJLAddIntPair(g, "maxHealth", c->maxHealth));
423 YAJL_CHECK(YAJLAddIntPair(g, "excessHealth", c->excessHealth));
424 YAJL_CHECK(YAJLAddIntPair(g, "flags", c->flags));
425 if (c->Drop != NULL)
426 {
427 YAJL_CHECK(YAJLAddStringPair(g, "Drop", c->Drop->Name));
428 }
429 if (c->bot)
430 {
431 YAJL_CHECK(
432 YAJLAddIntPair(g, "probabilityToMove", c->bot->probabilityToMove));
433 YAJL_CHECK(YAJLAddIntPair(
434 g, "probabilityToTrack", c->bot->probabilityToTrack));
435 YAJL_CHECK(YAJLAddIntPair(
436 g, "probabilityToShoot", c->bot->probabilityToShoot));
437 YAJL_CHECK(YAJLAddIntPair(g, "actionDelay", c->bot->actionDelay));
438 }
439 YAJL_CHECK(yajl_gen_map_close(g));
440
441 bail:
442 return res;
443 }
444
445 bool CharacterIsPrisoner(const CharacterStore *store, const Character *c)
446 {
447 for (int i = 0; i < (int)store->prisonerIds.size; i++)
448 {
449 const Character *prisoner = CArrayGet(
450 &store->OtherChars, CharacterStoreGetPrisonerId(store, i));
451 if (prisoner == c)
452 {
453 return true;
454 }
455 }
456 return false;
457 }
458
459 void CharacterSetHeadPart(Character *c, const HeadPart hp, const char *name)
460 {
461 CFREE(c->HeadParts[hp]);
462 c->HeadParts[hp] = NULL;
463 if (name)
464 {
465 CSTRDUP(c->HeadParts[hp], name);
466 }
467 }
468
469 static color_t RandomColor(void);
470 void CharacterShuffleAppearance(Character *c)
471 {
472 // Choose a random character class
473 const int numCharClasses = (int)gCharacterClasses.Classes.size +
474 (int)gCharacterClasses.CustomClasses.size;
475 const int charClass = rand() % numCharClasses;
476 if (charClass < (int)gCharacterClasses.Classes.size)
477 {
478 c->Class = CArrayGet(&gCharacterClasses.Classes, charClass);
479 }
480 else
481 {
482 c->Class = CArrayGet(
483 &gCharacterClasses.CustomClasses,
484 charClass - gCharacterClasses.Classes.size);
485 }
486
487 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
488 {
489 const char *name = NULL;
490 if (RAND_INT(0, 3) == 0)
491 {
492 const CArray *hpNames = &gPicManager.headPartNames[hp];
493 name = *(char **)CArrayGet(hpNames, rand() % hpNames->size);
494 }
495 CharacterSetHeadPart(c, hp, name);
496 }
497
498 c->Colors.Skin = RandomColor();
499 c->Colors.Arms = RandomColor();
500 c->Colors.Body = RandomColor();
501 c->Colors.Legs = RandomColor();
502 c->Colors.Hair = RandomColor();
503 c->Colors.Feet = RandomColor();
504 c->Colors.Facehair = c->Colors.Hair;
505 c->Colors.Hat = RandomColor();
506 c->Colors.Glasses = RandomColor();
507 }
508 static color_t RandomColor(void)
509 {
510 color_t c;
511 c.r = rand() & 255;
512 c.g = rand() & 255;
513 c.b = rand() & 255;
514 c.a = 255;
515 return c;
516 }