Debian Codegraph @

src/cdogsed/char_editor.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2017-2021, 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 "char_editor.h"
29
30 #include "editor_ui_common.h"
31 #include "nk_window.h"
32 #include <cdogs/draw/draw_actor.h>
33
34 #define ROW_HEIGHT 25
35 const float colRatios[] = {0.25f, 0.75f};
36 #define CHAR_STORE_BOTTOM 490
37 #define PAD 10.0f
38
39 typedef struct
40 {
41 Character *Char;
42 CampaignSetting *Setting;
43 bool *FileChanged;
44 char *CharacterClassNames;
45 char *HeadPartNames[HEAD_PART_COUNT];
46 char *GunNames;
47 char *MeleeNames;
48 char *PickupNames;
49 CArray texidsChars; // of GLuint[BODY_PART_COUNT]
50 GLuint texidsPreview[BODY_PART_COUNT];
51 CArray texIdsCharClasses; // of GLuint
52 CArray texIdsHeadParts[HEAD_PART_COUNT]; // of GLuint
53 CArray texIdsGuns; // of GLuint
54 CArray texIdsMelees; // of GLuint
55 CArray texIdsPickups; // of GLuint
56 Animation anim;
57 direction_e previewDir;
58 gunstate_e gunState;
59 Animation animSelection;
60 } EditorContext;
61
62 static const char *IndexCharacterClassName(const int i);
63 static const char *IndexHeadPartName(const int i, const HeadPart hp);
64 static const char *IndexHairName(const int i)
65 {
66 return IndexHeadPartName(i, HEAD_PART_HAIR);
67 }
68 static const char *IndexFacehairName(const int i)
69 {
70 return IndexHeadPartName(i, HEAD_PART_FACEHAIR);
71 }
72 static const char *IndexHatName(const int i)
73 {
74 return IndexHeadPartName(i, HEAD_PART_HAT);
75 }
76 static const char *IndexGlassesName(const int i)
77 {
78 return IndexHeadPartName(i, HEAD_PART_GLASSES);
79 }
80 static const char *IndexGunName(const int i);
81 static const char *IndexMeleeName(const int i);
82 static const char *IndexPickupName(const int i);
83 static const WeaponClass *IndexWeaponClassReal(const int i);
84 static const WeaponClass *IndexWeaponClassMelee(const int i);
85 static const PickupClass *IndexPickupClass(const int i);
86 static int NumCharacterClasses(void);
87 static int NumGuns(void);
88 static int NumMelees(void);
89 static int NumPickups(void);
90 static void AddCharacterTextures(EditorContext *ec);
91 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data);
92 void CharEditor(
93 GraphicsDevice *g, CampaignSetting *setting, EventHandlers *handlers,
94 bool *fileChanged)
95 {
96 NKWindowConfig cfg;
97 memset(&cfg, 0, sizeof cfg);
98 cfg.Title = "Character Editor";
99 cfg.Size = svec2i(800, 800);
100 cfg.MinSize = svec2i(800, 500);
101 cfg.WindowFlags = SDL_WINDOW_RESIZABLE;
102 color_t bg = {41, 26, 26, 255};
103 cfg.BG = bg;
104 cfg.Icon = g->icon;
105 cfg.Handlers = handlers;
106 cfg.Draw = Draw;
107
108 NKWindowInit(&cfg);
109
110 // Initialise editor context
111 EditorContext ec;
112 ec.Char = NULL;
113 ec.Setting = setting;
114 ec.FileChanged = fileChanged;
115 ec.CharacterClassNames =
116 GetClassNames(NumCharacterClasses(), IndexCharacterClassName);
117 const char *(*indexHeadPartFuncs[HEAD_PART_COUNT])(int) = {
118 IndexHairName, IndexFacehairName, IndexHatName, IndexGlassesName};
119 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
120 {
121 ec.HeadPartNames[hp] = GetClassNames(
122 gPicManager.headPartNames[hp].size, *indexHeadPartFuncs[hp]);
123 }
124 ec.GunNames = GetClassNames(NumGuns(), IndexGunName);
125 ec.MeleeNames = GetClassNames(NumMelees(), IndexMeleeName);
126 ec.PickupNames = GetClassNames(NumPickups(), IndexPickupName);
127
128 CArrayInit(&ec.texidsChars, sizeof(GLuint) * BODY_PART_COUNT);
129 for (int i = 0; i < (int)setting->characters.OtherChars.size; i++)
130 {
131 AddCharacterTextures(&ec);
132 }
133 glGenTextures(BODY_PART_COUNT, ec.texidsPreview);
134
135 CharColors cc;
136 cc.Skin = colorSkin;
137 cc.Hair = colorRed;
138
139 TexArrayInit(&ec.texIdsCharClasses, NumCharacterClasses());
140 CA_FOREACH(const GLuint, texid, ec.texIdsCharClasses)
141 const CharacterClass *c = IndexCharacterClass(_ca_index);
142 LoadTexFromPic(*texid, GetHeadPic(c, DIRECTION_DOWN, false, &cc));
143 CA_FOREACH_END()
144
145 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
146 {
147 TexArrayInit(
148 &ec.texIdsHeadParts[hp], gPicManager.headPartNames[hp].size);
149 CA_FOREACH(const GLuint, texid, ec.texIdsHeadParts[hp])
150 const char *name = IndexHeadPartName(_ca_index, hp);
151 LoadTexFromPic(
152 *texid, GetHeadPartPic(name, hp, DIRECTION_DOWN, false, &cc));
153 CA_FOREACH_END()
154 }
155
156 TexArrayInit(&ec.texIdsGuns, NumGuns());
157 CA_FOREACH(const GLuint, texid, ec.texIdsGuns)
158 const WeaponClass *wc = IndexWeaponClassReal(_ca_index);
159 LoadTexFromPic(*texid, wc->Icon);
160 CA_FOREACH_END()
161
162 TexArrayInit(&ec.texIdsMelees, NumMelees());
163 CA_FOREACH(const GLuint, texid, ec.texIdsMelees)
164 const WeaponClass *wc = IndexWeaponClassMelee(_ca_index);
165 if (wc == NULL)
166 continue;
167 LoadTexFromPic(*texid, wc->Icon);
168 CA_FOREACH_END()
169
170 TexArrayInit(&ec.texIdsPickups, NumPickups());
171 CA_FOREACH(const GLuint, texid, ec.texIdsPickups)
172 const PickupClass *pc = IndexPickupClass(_ca_index);
173 if (pc == NULL)
174 continue;
175 LoadTexFromPic(*texid, CPicGetPic(&pc->Pic, 0));
176 CA_FOREACH_END()
177
178 ec.anim = AnimationGetActorAnimation(ACTORANIMATION_WALKING);
179 ec.previewDir = DIRECTION_DOWN;
180 ec.animSelection = AnimationGetActorAnimation(ACTORANIMATION_IDLE);
181
182 cfg.DrawData = &ec;
183
184 NKWindow(cfg);
185
186 CFREE(ec.CharacterClassNames);
187 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
188 {
189 CFREE(ec.HeadPartNames[hp]);
190 }
191 CFREE(ec.GunNames);
192 CFREE(ec.MeleeNames);
193 CFREE(ec.PickupNames);
194 glDeleteTextures(
195 (GLsizei)(BODY_PART_COUNT * ec.texidsChars.size), ec.texidsChars.data);
196 CArrayTerminate(&ec.texidsChars);
197 glDeleteTextures(BODY_PART_COUNT, ec.texidsPreview);
198 #define DELTEX(_tx) \
199 glDeleteTextures((GLsizei)(_tx).size, (_tx).data); \
200 TexArrayTerminate(&(_tx));
201 DELTEX(ec.texIdsCharClasses);
202 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
203 {
204 DELTEX(ec.texIdsHeadParts[hp]);
205 }
206 DELTEX(ec.texIdsGuns);
207 DELTEX(ec.texIdsMelees);
208 DELTEX(ec.texIdsPickups);
209 }
210
211 static const char *IndexCharacterClassName(const int i)
212 {
213 const CharacterClass *c = IndexCharacterClass(i);
214 return c->Name;
215 }
216 static const char *IndexHeadPartName(const int i, const HeadPart hp)
217 {
218 return *(char **)CArrayGet(&gPicManager.headPartNames[hp], i);
219 }
220 static int NumCharacterClasses(void)
221 {
222 return (int)(gCharacterClasses.Classes.size +
223 gCharacterClasses.CustomClasses.size);
224 }
225 static const char *IndexGunName(const int i)
226 {
227 const WeaponClass *wc = IndexWeaponClassReal(i);
228 return wc ? wc->name : NULL;
229 }
230 static const char *IndexMeleeName(const int i)
231 {
232 const WeaponClass *wc = IndexWeaponClassMelee(i);
233 return wc ? wc->name : "(None)";
234 }
235 static const char *IndexPickupName(const int i)
236 {
237 const PickupClass *pc = IndexPickupClass(i);
238 return pc ? pc->Name : "(None)";
239 }
240 static const WeaponClass *IndexWeaponClassReal(const int i)
241 {
242 int j = 0;
243 CA_FOREACH(WeaponClass, wc, gWeaponClasses.Guns)
244 if (!wc->IsRealGun)
245 {
246 continue;
247 }
248 if (j == i)
249 {
250 return wc;
251 }
252 j++;
253 CA_FOREACH_END()
254 CA_FOREACH(WeaponClass, wc, gWeaponClasses.CustomGuns)
255 if (!wc->IsRealGun)
256 {
257 continue;
258 }
259 if (j == i)
260 {
261 return wc;
262 }
263 j++;
264 CA_FOREACH_END()
265 CASSERT(false, "cannot find gun");
266 return NULL;
267 }
268 static const WeaponClass *IndexWeaponClassMelee(const int i)
269 {
270 if (i == 0)
271 {
272 return NULL;
273 }
274 int j = 1;
275 CA_FOREACH(WeaponClass, wc, gWeaponClasses.Guns)
276 if (!wc->IsRealGun || wc->Type != GUNTYPE_MELEE)
277 {
278 continue;
279 }
280 if (j == i)
281 {
282 return wc;
283 }
284 j++;
285 CA_FOREACH_END()
286 CA_FOREACH(WeaponClass, wc, gWeaponClasses.CustomGuns)
287 if (!wc->IsRealGun || wc->Type != GUNTYPE_MELEE)
288 {
289 continue;
290 }
291 if (j == i)
292 {
293 return wc;
294 }
295 j++;
296 CA_FOREACH_END()
297 CASSERT(false, "cannot find melee");
298 return NULL;
299 }
300 static const PickupClass *IndexPickupClass(const int i)
301 {
302 if (i == 0)
303 {
304 return NULL;
305 }
306 int j = 1;
307 CA_FOREACH(const PickupClass, pc, gPickupClasses.Classes)
308 if (j == i)
309 {
310 return pc;
311 }
312 j++;
313 CA_FOREACH_END()
314 CA_FOREACH(const PickupClass, pc, gPickupClasses.CustomClasses)
315 if (j == i)
316 {
317 return pc;
318 }
319 j++;
320 CA_FOREACH_END()
321 CA_FOREACH(const PickupClass, pc, gPickupClasses.KeyClasses)
322 if (j == i)
323 {
324 return pc;
325 }
326 j++;
327 CA_FOREACH_END()
328 CASSERT(false, "cannot find pickup");
329 return NULL;
330 }
331 static int NumGuns(void)
332 {
333 int total = 0;
334 CA_FOREACH(const WeaponClass, wc, gWeaponClasses.Guns)
335 if (wc->IsRealGun)
336 {
337 total++;
338 }
339 CA_FOREACH_END()
340 CA_FOREACH(const WeaponClass, wc, gWeaponClasses.CustomGuns)
341 if (wc->IsRealGun)
342 {
343 total++;
344 }
345 CA_FOREACH_END()
346 return total;
347 }
348 static int NumMelees(void)
349 {
350 int total = 0;
351 CA_FOREACH(const WeaponClass, wc, gWeaponClasses.Guns)
352 if (wc->IsRealGun && wc->Type == GUNTYPE_MELEE)
353 {
354 total++;
355 }
356 CA_FOREACH_END()
357 CA_FOREACH(const WeaponClass, wc, gWeaponClasses.CustomGuns)
358 if (wc->IsRealGun && wc->Type == GUNTYPE_MELEE)
359 {
360 total++;
361 }
362 CA_FOREACH_END()
363 return total;
364 }
365 static int NumPickups(void)
366 {
367 return (int)(gPickupClasses.Classes.size +
368 gPickupClasses.CustomClasses.size +
369 gPickupClasses.KeyClasses.size + 1);
370 }
371 static int GunIndex(const WeaponClass *wc)
372 {
373 int j = 0;
374 CA_FOREACH(const WeaponClass, wc2, gWeaponClasses.Guns)
375 if (!wc2->IsRealGun)
376 {
377 continue;
378 }
379 if (strcmp(wc->name, wc2->name) == 0)
380 {
381 return j;
382 }
383 j++;
384 CA_FOREACH_END()
385 CA_FOREACH(const WeaponClass, wc2, gWeaponClasses.CustomGuns)
386 if (!wc2->IsRealGun)
387 {
388 continue;
389 }
390 if (strcmp(wc->name, wc2->name) == 0)
391 {
392 return j;
393 }
394 j++;
395 CA_FOREACH_END()
396 CASSERT(false, "cannot find gun");
397 return -1;
398 }
399 static int MeleeIndex(const WeaponClass *wc)
400 {
401 if (wc == NULL)
402 {
403 return 0;
404 }
405 int j = 1;
406 CA_FOREACH(const WeaponClass, wc2, gWeaponClasses.Guns)
407 if (!wc2->IsRealGun || wc2->Type != GUNTYPE_MELEE)
408 {
409 continue;
410 }
411 if (strcmp(wc->name, wc2->name) == 0)
412 {
413 return j;
414 }
415 j++;
416 CA_FOREACH_END()
417 CA_FOREACH(const WeaponClass, wc2, gWeaponClasses.CustomGuns)
418 if (!wc2->IsRealGun || wc2->Type != GUNTYPE_MELEE)
419 {
420 continue;
421 }
422 if (strcmp(wc->name, wc2->name) == 0)
423 {
424 return j;
425 }
426 j++;
427 CA_FOREACH_END()
428 CASSERT(false, "cannot find melee");
429 return -1;
430 }
431 static int PickupIndex(const PickupClass *pc)
432 {
433 if (pc == NULL)
434 {
435 return 0;
436 }
437 int j = 1;
438 CA_FOREACH(const PickupClass, pc2, gPickupClasses.Classes)
439 if (pc == pc2)
440 {
441 return j;
442 }
443 j++;
444 CA_FOREACH_END()
445 CA_FOREACH(const PickupClass, pc2, gPickupClasses.CustomClasses)
446 if (pc == pc2)
447 {
448 return j;
449 }
450 j++;
451 CA_FOREACH_END()
452 CA_FOREACH(const PickupClass, pc2, gPickupClasses.KeyClasses)
453 if (pc == pc2)
454 {
455 return j;
456 }
457 j++;
458 CA_FOREACH_END()
459 CASSERT(false, "cannot find pickup");
460 return -1;
461 }
462
463 static void AddCharacter(EditorContext *ec, const int cloneIdx);
464 static int MoveCharacter(
465 EditorContext *ec, const int selectedIndex, const int d);
466 static void DeleteCharacter(EditorContext *ec, const int selectedIndex);
467 static int DrawClassSelection(
468 struct nk_context *ctx, EditorContext *ec, const char *label,
469 const GLuint *texids, const char *items, const int selected,
470 const size_t len);
471 static int HeadPartIndex(const char *name, const HeadPart hp);
472 static void DrawFlag(
473 struct nk_context *ctx, EditorContext *ec, const char *label,
474 const int flag, const char *tooltip);
475 static void DrawCharacter(
476 struct nk_context *ctx, Character *c, GLuint *texids,
477 const struct vec2i pos, const Animation *anim, const direction_e d,
478 const gunstate_e gunStates[MAX_BARRELS]);
479 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data)
480 {
481 EditorContext *ec = data;
482 // Stretch char store with window
483 struct vec2i windowSize;
484 SDL_GetWindowSize(win, &windowSize.x, &windowSize.y);
485 const struct vec2i charStoreSize = svec2i(
486 MAX(400, windowSize.x - 100),
487 MAX(200, windowSize.y - CHAR_STORE_BOTTOM));
488 if (nk_begin(
489 ctx, "Character Store",
490 nk_rect(
491 PAD, PAD, (float)charStoreSize.x - PAD,
492 (float)charStoreSize.y - PAD),
493 NK_WINDOW_BORDER | NK_WINDOW_TITLE))
494 {
495 int selectedIndex = -1;
496 CA_FOREACH(Character, c, ec->Setting->characters.OtherChars)
497 if (ec->Char == c)
498 {
499 selectedIndex = _ca_index;
500 }
501 CA_FOREACH_END()
502
503 // TODO: keep buttons from scrolling off
504 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 5);
505 if (nk_button_label(ctx, "Add"))
506 {
507 AddCharacter(ec, -1);
508 selectedIndex =
509 MAX((int)ec->Setting->characters.OtherChars.size - 1, 0);
510 }
511 if (nk_button_label(ctx, "Move Up"))
512 {
513 selectedIndex = MoveCharacter(ec, selectedIndex, -1);
514 }
515 if (nk_button_label(ctx, "Move Down"))
516 {
517 selectedIndex = MoveCharacter(ec, selectedIndex, 1);
518 }
519 if (selectedIndex >= 0 && nk_button_label(ctx, "Duplicate"))
520 {
521 AddCharacter(ec, selectedIndex);
522 selectedIndex =
523 MAX((int)ec->Setting->characters.OtherChars.size - 1, 0);
524 }
525 if (selectedIndex >= 0 && nk_button_label(ctx, "Remove"))
526 {
527 DeleteCharacter(ec, selectedIndex);
528 selectedIndex =
529 MIN(selectedIndex,
530 (int)ec->Setting->characters.OtherChars.size - 1);
531 }
532 if (selectedIndex >= 0)
533 {
534 ec->Char =
535 CArrayGet(&ec->Setting->characters.OtherChars, selectedIndex);
536 }
537 else
538 {
539 ec->Char = NULL;
540 }
541
542 // Show existing characters
543 nk_layout_row_dynamic(ctx, 32 * PIC_SCALE, (int)charStoreSize.x / 75);
544 CA_FOREACH(Character, c, ec->Setting->characters.OtherChars)
545 const int selected = ec->Char == c;
546 // show both label and full character
547 char label[256];
548 sprintf(
549 label, "%s%s", c->PlayerTemplateName != NULL ? "*" : "",
550 c->Gun->name);
551 if (nk_select_label(
552 ctx, label, NK_TEXT_ALIGN_BOTTOM | NK_TEXT_ALIGN_CENTERED,
553 selected))
554 {
555 ec->Char = c;
556 }
557 gunstate_e gunStates[MAX_BARRELS];
558 for (int i = 0; i < MAX_BARRELS; i++)
559 {
560 gunStates[i] = GUNSTATE_READY;
561 }
562 DrawCharacter(
563 ctx, c, CArrayGet(&ec->texidsChars, _ca_index), svec2i(-34, 5),
564 &ec->animSelection, DIRECTION_DOWN, gunStates);
565 CA_FOREACH_END()
566 nk_end(ctx);
567 }
568
569 if (ec->Char != NULL)
570 {
571 const float previewWidth = 100;
572 if (nk_begin(
573 ctx, "Preview",
574 nk_rect(
575 charStoreSize.x + PAD, PAD, previewWidth,
576 charStoreSize.y - PAD),
577 NK_WINDOW_BORDER | NK_WINDOW_TITLE))
578 {
579 // Preview direction
580 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 2);
581 if (nk_button_label(ctx, "<"))
582 {
583 ec->previewDir = (direction_e)CLAMP_OPPOSITE(
584 (int)ec->previewDir + 1, 0, DIRECTION_UPLEFT);
585 }
586 if (nk_button_label(ctx, ">"))
587 {
588 ec->previewDir = (direction_e)CLAMP_OPPOSITE(
589 (int)ec->previewDir - 1, 0, DIRECTION_UPLEFT);
590 }
591 // Preview
592 nk_layout_row_dynamic(ctx, 48 * PIC_SCALE, 1);
593 gunstate_e gunStates[MAX_BARRELS];
594 for (int i = 0; i < MAX_BARRELS; i++)
595 {
596 gunStates[i] = ec->gunState;
597 }
598 DrawCharacter(
599 ctx, ec->Char, ec->texidsPreview, svec2i(0, 5), &ec->anim,
600 ec->previewDir, gunStates);
601 // Animation
602 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
603 const int isWalking = ec->anim.Type == ACTORANIMATION_WALKING;
604 if (nk_select_label(ctx, "Run", NK_TEXT_ALIGN_LEFT, isWalking) &&
605 !isWalking)
606 {
607 ec->anim = AnimationGetActorAnimation(ACTORANIMATION_WALKING);
608 }
609 const int isIdle = ec->anim.Type == ACTORANIMATION_IDLE;
610 if (nk_select_label(ctx, "Idle", NK_TEXT_ALIGN_LEFT, isIdle) &&
611 !isIdle)
612 {
613 ec->anim = AnimationGetActorAnimation(ACTORANIMATION_IDLE);
614 }
615 int isFiring = ec->gunState == GUNSTATE_FIRING;
616 nk_checkbox_label(ctx, "Firing", &isFiring);
617 ec->gunState = isFiring ? GUNSTATE_FIRING : GUNSTATE_READY;
618 nk_end(ctx);
619 }
620
621 if (nk_begin(
622 ctx, "Appearance",
623 nk_rect(
624 PAD, (float)charStoreSize.y + PAD, 260,
625 CHAR_STORE_BOTTOM - PAD * 2),
626 NK_WINDOW_BORDER | NK_WINDOW_TITLE))
627 {
628 nk_layout_row(ctx, NK_DYNAMIC, ROW_HEIGHT, 2, colRatios);
629
630 const int selectedClass = DrawClassSelection(
631 ctx, ec, "Class:", ec->texIdsCharClasses.data,
632 ec->CharacterClassNames,
633 (int)CharacterClassIndex(ec->Char->Class),
634 NumCharacterClasses());
635 ec->Char->Class = IndexCharacterClass(selectedClass);
636
637 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
638 {
639 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
640 int hasStyle = ec->Char->HeadParts[hp] != NULL;
641 char buf[256];
642 sprintf(buf, "Has %s", HeadPartStr(hp));
643 nk_checkbox_label(ctx, buf, &hasStyle);
644 if (hasStyle)
645 {
646 nk_layout_row(ctx, NK_DYNAMIC, ROW_HEIGHT, 2, colRatios);
647 const int current =
648 HeadPartIndex(ec->Char->HeadParts[hp], hp);
649 sprintf(buf, "%s:", HeadPartStr(hp));
650 int selected = DrawClassSelection(
651 ctx, ec, buf, ec->texIdsHeadParts[hp].data,
652 ec->HeadPartNames[hp], current,
653 gPicManager.headPartNames[hp].size);
654 if (selected == -1)
655 {
656 selected = 0;
657 }
658 if (current != selected)
659 {
660 CharacterSetHeadPart(
661 ec->Char, hp, IndexHeadPartName(selected, hp));
662 }
663 }
664 else
665 {
666 CharacterSetHeadPart(ec->Char, hp, NULL);
667 }
668 }
669
670 // Character colours
671 nk_layout_row(ctx, NK_DYNAMIC, ROW_HEIGHT, 2, colRatios);
672 if (ColorPicker(ctx, ROW_HEIGHT, "Skin:", &ec->Char->Colors.Skin))
673 {
674 *ec->FileChanged = true;
675 }
676 if (ColorPicker(ctx, ROW_HEIGHT, "Hair:", &ec->Char->Colors.Hair))
677 {
678 *ec->FileChanged = true;
679 }
680 if (ColorPicker(
681 ctx, ROW_HEIGHT,
682 "Facial Hair:", &ec->Char->Colors.Facehair))
683 {
684 *ec->FileChanged = true;
685 }
686 if (ColorPicker(ctx, ROW_HEIGHT, "Hat:", &ec->Char->Colors.Hat))
687 {
688 *ec->FileChanged = true;
689 }
690 if (ColorPicker(
691 ctx, ROW_HEIGHT, "Glasses:", &ec->Char->Colors.Glasses))
692 {
693 *ec->FileChanged = true;
694 }
695 if (ColorPicker(ctx, ROW_HEIGHT, "Arms:", &ec->Char->Colors.Arms))
696 {
697 *ec->FileChanged = true;
698 }
699 if (ColorPicker(ctx, ROW_HEIGHT, "Body:", &ec->Char->Colors.Body))
700 {
701 *ec->FileChanged = true;
702 }
703 if (ColorPicker(ctx, ROW_HEIGHT, "Legs:", &ec->Char->Colors.Legs))
704 {
705 *ec->FileChanged = true;
706 }
707 if (ColorPicker(ctx, ROW_HEIGHT, "Feet:", &ec->Char->Colors.Feet))
708 {
709 *ec->FileChanged = true;
710 }
711 nk_end(ctx);
712 }
713
714 if (nk_begin(
715 ctx, "Attributes",
716 nk_rect(280, (float)charStoreSize.y + PAD, 250, 310),
717 NK_WINDOW_BORDER | NK_WINDOW_TITLE))
718 {
719 // Speed (256 = 100%)
720 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
721 int isPlayerTemplate = ec->Char->PlayerTemplateName != NULL;
722 nk_checkbox_label(ctx, "Player Template", &isPlayerTemplate);
723 if (isPlayerTemplate)
724 {
725 char buf[256];
726 buf[0] = '\0';
727 if (ec->Char->PlayerTemplateName != NULL)
728 {
729 strcpy(buf, ec->Char->PlayerTemplateName);
730 }
731 DrawTextbox(ctx, buf, 256, "Name", NK_EDIT_FIELD);
732 if (ec->Char->PlayerTemplateName == NULL ||
733 strcmp(buf, ec->Char->PlayerTemplateName) != 0)
734 {
735 *ec->FileChanged = true;
736 CFREE(ec->Char->PlayerTemplateName);
737 CSTRDUP(ec->Char->PlayerTemplateName, buf);
738 }
739 }
740 else
741 {
742 CFREE(ec->Char->PlayerTemplateName);
743 ec->Char->PlayerTemplateName = NULL;
744 }
745 nk_property_float(
746 ctx, "Speed:", 0, &ec->Char->speed, 4, 0.05f, 0.01f);
747
748 nk_layout_row(ctx, NK_DYNAMIC, ROW_HEIGHT, 2, colRatios);
749 const int selectedGun = DrawClassSelection(
750 ctx, ec, "Gun:", ec->texIdsGuns.data, ec->GunNames,
751 GunIndex(ec->Char->Gun), NumGuns());
752 ec->Char->Gun = IndexWeaponClassReal(selectedGun);
753 const int selectedMelee = DrawClassSelection(
754 ctx, ec, "Melee:", ec->texIdsMelees.data, ec->MeleeNames,
755 MeleeIndex(ec->Char->Melee), NumMelees());
756 ec->Char->Melee = IndexWeaponClassMelee(selectedMelee);
757
758 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
759 nk_property_int(
760 ctx, "Max Health:", 10, &ec->Char->maxHealth, 2000, 10, 1);
761 nk_property_int(
762 ctx, "Excess Health:", ec->Char->maxHealth,
763 &ec->Char->excessHealth, 2000, 10, 1);
764
765 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 2);
766 DrawFlag(ctx, ec, "Asbestos", FLAGS_ASBESTOS, "Immune to fire");
767 DrawFlag(
768 ctx, ec, "Immunity", FLAGS_IMMUNITY,
769 "Immune to poison and petrify");
770 DrawFlag(ctx, ec, "See-through", FLAGS_SEETHROUGH, NULL);
771 DrawFlag(ctx, ec, "Invulnerable", FLAGS_INVULNERABLE, NULL);
772 DrawFlag(
773 ctx, ec, "Penalty", FLAGS_PENALTY,
774 "Large score penalty when shot");
775 DrawFlag(
776 ctx, ec, "Victim", FLAGS_VICTIM, "Takes damage from everyone");
777
778 const int selectedPickup = DrawClassSelection(
779 ctx, ec, "Drop:", ec->texIdsPickups.data, ec->PickupNames,
780 PickupIndex(ec->Char->Drop), NumPickups());
781 ec->Char->Drop = IndexPickupClass(selectedPickup);
782
783 nk_end(ctx);
784 }
785
786 if (nk_begin(
787 ctx, "AI",
788 nk_rect(
789 540, (float)charStoreSize.y + PAD, 250,
790 CHAR_STORE_BOTTOM - PAD * 2),
791 NK_WINDOW_BORDER | NK_WINDOW_TITLE))
792 {
793 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
794 nk_property_int(
795 ctx, "Move (%):", 0, &ec->Char->bot->probabilityToMove, 100, 5,
796 1);
797 nk_property_int(
798 ctx, "Track (%):", 0, &ec->Char->bot->probabilityToTrack, 100,
799 5, 1);
800 nk_property_int(
801 ctx, "Shoot (%):", 0, &ec->Char->bot->probabilityToShoot, 100,
802 5, 1);
803 nk_property_int(
804 ctx, "Action delay:", 0, &ec->Char->bot->actionDelay, 50, 5,
805 1);
806
807 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 2);
808 DrawFlag(
809 ctx, ec, "Runs away", FLAGS_RUNS_AWAY,
810 "Runs away from player");
811 DrawFlag(
812 ctx, ec, "Sneaky", FLAGS_SNEAKY,
813 "Shoots back when player shoots");
814 DrawFlag(
815 ctx, ec, "Good guy", FLAGS_GOOD_GUY, "Same team as players");
816 DrawFlag(
817 ctx, ec, "Sleeping", FLAGS_SLEEPING,
818 "Doesn't move unless seen");
819 DrawFlag(
820 ctx, ec, "Prisoner", FLAGS_PRISONER,
821 "Doesn't move until touched");
822 DrawFlag(ctx, ec, "Follower", FLAGS_FOLLOWER, "Follows players");
823 DrawFlag(
824 ctx, ec, "Awake", FLAGS_AWAKEALWAYS,
825 "Don't go to sleep after players leave");
826 DrawFlag(
827 ctx, ec, "Deaf", FLAGS_DEAF, "Doesn't wake to guns nearby");
828 DrawFlag(
829 ctx, ec, "All-Seeing", FLAGS_ALL_SEEING,
830 "Has full 360-degree vision");
831 DrawFlag(
832 ctx, ec, "Unlock Doors", FLAGS_UNLOCK_DOORS,
833 "Can open any locked door");
834 DrawFlag(
835 ctx, ec, "Move and shoot", FLAGS_MOVE_AND_SHOOT,
836 "Can move while shooting");
837 nk_end(ctx);
838 }
839 }
840
841 AnimationUpdate(&ec->anim, 1);
842 AnimationUpdate(&ec->animSelection, 1);
843
844 return true;
845 }
846
847 static void AddCharacter(EditorContext *ec, const int cloneIdx)
848 {
849 ec->Char = CharacterStoreAddOther(&ec->Setting->characters);
850 if (cloneIdx >= 0)
851 {
852 const Character *clone =
853 CArrayGet(&ec->Setting->characters.OtherChars, cloneIdx);
854 CFREE(ec->Char->bot);
855 memcpy(ec->Char, clone, sizeof *ec->Char);
856 if (clone->PlayerTemplateName)
857 {
858 CSTRDUP(ec->Char->PlayerTemplateName, clone->PlayerTemplateName);
859 }
860 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
861 {
862 ec->Char->HeadParts[hp] = NULL;
863 CharacterSetHeadPart(ec->Char, hp, clone->HeadParts[hp]);
864 }
865 CMALLOC(ec->Char->bot, sizeof *ec->Char->bot);
866 memcpy(ec->Char->bot, clone->bot, sizeof *ec->Char->bot);
867 }
868 else
869 {
870 // set up character template
871 ec->Char->Class = StrCharacterClass("Ogre");
872 ec->Char->Colors.Skin = colorGreen;
873 ec->Char->Colors.Arms = colorDarkGray;
874 ec->Char->Colors.Body = colorDarkGray;
875 ec->Char->Colors.Legs = colorDarkGray;
876 ec->Char->Colors.Hair = colorBlack;
877 ec->Char->Colors.Feet = colorDarkGray;
878 ec->Char->Colors.Facehair = colorDarkGray;
879 ec->Char->Colors.Hat = colorDarkGray;
880 ec->Char->Colors.Glasses = colorDarkGray;
881 ec->Char->speed = 1;
882 ec->Char->Gun = StrWeaponClass("Machine gun");
883 ec->Char->maxHealth = 40;
884 ec->Char->excessHealth = 80;
885 ec->Char->flags = FLAGS_IMMUNITY;
886 ec->Char->bot->probabilityToMove = 50;
887 ec->Char->bot->probabilityToTrack = 25;
888 ec->Char->bot->probabilityToShoot = 2;
889 ec->Char->bot->actionDelay = 15;
890 }
891
892 AddCharacterTextures(ec);
893
894 *ec->FileChanged = true;
895 }
896
897 static void AddCharacterTextures(EditorContext *ec)
898 {
899 GLuint texids[BODY_PART_COUNT];
900 glGenTextures(BODY_PART_COUNT, texids);
901 CArrayPushBack(&ec->texidsChars, &texids);
902 }
903
904 static int MoveCharacter(
905 EditorContext *ec, const int selectedIndex, const int d)
906 {
907 const int moveIndex = selectedIndex + d;
908 CArray *chars = &ec->Setting->characters.OtherChars;
909 if (moveIndex < 0 || moveIndex >= (int)chars->size)
910 {
911 return selectedIndex;
912 }
913
914 Character tmp;
915 Character *selected = CArrayGet(chars, selectedIndex);
916 Character *move = CArrayGet(chars, moveIndex);
917 memcpy(&tmp, selected, chars->elemSize);
918 memcpy(selected, move, chars->elemSize);
919 memcpy(move, &tmp, chars->elemSize);
920 return moveIndex;
921 }
922
923 static void DeleteCharacter(EditorContext *ec, const int selectedIndex)
924 {
925 CharacterStoreDeleteOther(&ec->Setting->characters, selectedIndex);
926 const int indexClamped =
927 MIN(selectedIndex, (int)ec->Setting->characters.OtherChars.size - 1);
928 if (indexClamped >= 0)
929 {
930 ec->Char =
931 CArrayGet(&ec->Setting->characters.OtherChars, indexClamped);
932 }
933 else
934 {
935 ec->Char = NULL;
936 }
937
938 // Delete character textures
939 GLuint *texids = CArrayGet(&ec->texidsChars, selectedIndex);
940 glDeleteTextures(BODY_PART_COUNT, texids);
941 CArrayDelete(&ec->texidsChars, selectedIndex);
942
943 *ec->FileChanged = true;
944 }
945
946 static int DrawClassSelection(
947 struct nk_context *ctx, EditorContext *ec, const char *label,
948 const GLuint *texids, const char *items, const int selected,
949 const size_t len)
950 {
951 nk_label(ctx, label, NK_TEXT_LEFT);
952 const int selectedNew = nk_combo_separator_image(
953 ctx, texids, items, '\0', selected, (int)len, ROW_HEIGHT,
954 nk_vec2(nk_widget_width(ctx), 8 * ROW_HEIGHT));
955 if (selectedNew != selected)
956 {
957 *ec->FileChanged = true;
958 }
959 return selectedNew;
960 }
961
962 static int HeadPartIndex(const char *name, const HeadPart hp)
963 {
964 if (name == NULL)
965 {
966 return -1;
967 }
968 CA_FOREACH(const char *, hpName, gPicManager.headPartNames[hp])
969 if (strcmp(*hpName, name) == 0)
970 {
971 return _ca_index;
972 }
973 CA_FOREACH_END()
974 return -1;
975 }
976
977 static void DrawFlag(
978 struct nk_context *ctx, EditorContext *ec, const char *label,
979 const int flag, const char *tooltip)
980 {
981 nk_checkbox_flags_label(ctx, label, &ec->Char->flags, flag);
982 if (tooltip && nk_widget_is_hovered(ctx))
983 {
984 nk_tooltip(ctx, tooltip);
985 }
986 }
987
988 static void DrawCharacter(
989 struct nk_context *ctx, Character *c, GLuint *texids,
990 const struct vec2i pos, const Animation *anim, const direction_e d,
991 const gunstate_e gunStates[MAX_BARRELS])
992 {
993 const int frame = AnimationGetFrame(anim);
994 ActorPics pics = GetCharacterPics(
995 c, d, d, anim->Type, frame, c->Gun, gunStates, false, colorTransparent,
996 NULL, NULL, 0);
997 for (int i = 0; i < BODY_PART_COUNT; i++)
998 {
999 const Pic *pic = pics.OrderedPics[i];
1000 if (pic == NULL)
1001 {
1002 continue;
1003 }
1004 const struct vec2i drawPos = svec2i_add(
1005 svec2i_add(pos, pics.OrderedOffsets[i]), svec2i(16, 16));
1006 DrawPic(ctx, pic, texids[i], drawPos, PIC_SCALE);
1007 }
1008 }