Debian Codegraph @

src/cdogs/draw/draw_actor.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (C) 1995 Ronny Wester
5 Copyright (C) 2003 Jeremy Chin
6 Copyright (C) 2003-2007 Lucas Martin-King
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 This file incorporates work covered by the following copyright and
23 permission notice:
24
25 Copyright (c) 2013-2021, 2023, 2025 Cong Xu
26 All rights reserved.
27
28 Redistribution and use in source and binary forms, with or without
29 modification, are permitted provided that the following conditions are met:
30
31 Redistributions of source code must retain the above copyright notice, this
32 list of conditions and the following disclaimer.
33 Redistributions in binary form must reproduce the above copyright notice,
34 this list of conditions and the following disclaimer in the documentation
35 and/or other materials provided with the distribution.
36
37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include "draw/draw_actor.h"
50
51 #include <assert.h>
52 #include <math.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56
57 #include "actors.h"
58 #include "algorithms.h"
59 #include "blit.h"
60 #include "config.h"
61 #include "draw/draw.h"
62 #include "draw/drawtools.h"
63 #include "font.h"
64 #include "game_events.h"
65 #include "net_util.h"
66 #include "objs.h"
67 #include "pic_manager.h"
68 #include "pics.h"
69
70 #define TRANSPARENT_ACTOR_ALPHA 64
71
72 static struct vec2i GetActorDrawOffset(
73 const Pic *pic, const BodyPart part, const CharSprites *cs,
74 const ActorAnimation anim, const int frame, const direction_e d,
75 const gunstate_e state)
76 {
77 if (pic == NULL)
78 {
79 return svec2i_zero();
80 }
81 struct vec2i offset = svec2i_scale_divide(pic->size, -2);
82 offset = svec2i_subtract(
83 offset, CharSpritesGetOffset(
84 cs->Offsets.Frame[part],
85 anim == ACTORANIMATION_WALKING ? "run" : "idle", frame));
86 offset = svec2i_add(offset, svec2i_assign_vec2(cs->Offsets.Dir[part][d]));
87 if ((part == BODY_PART_GUN_R || part == BODY_PART_GUN_L) &&
88 state == GUNSTATE_RECOIL)
89 {
90 // Offset the gun pic towards the player
91 const struct vec2i recoilOffsets[DIRECTION_COUNT] = {
92 {0, 1}, {-1, 1}, {-1, 0}, {-1, -1},
93 {0, -1}, {1, -1}, {1, 0}, {1, 1}};
94 offset = svec2i_add(offset, recoilOffsets[d]);
95 }
96 return offset;
97 }
98
99 static void GetEasterDates(
100 const int Y, int *friM, int *friD, int *monM, int *monD)
101 {
102 const int a = Y % 19;
103 const int b = Y / 100;
104 const int c = Y % 100;
105 const int d = b / 4;
106 const int e = b % 4;
107 const int f = (b + 8) / 25;
108 const int g = (b - f + 1) / 3;
109 const int h = (19 * a + b - d - g + 15) % 30;
110 const int i = c / 4;
111 const int k = c % 4;
112 const int L = (32 + 2 * e + 2 * i - h - k) % 7;
113 const int m = (a + 11 * h + 22 * L) / 451;
114 struct tm t;
115 t.tm_year = Y - 1900;
116 t.tm_mon = (h + L - 7 * m + 114) / 31 - 1;
117 t.tm_mday = ((h + L - 7 * m + 114) % 31) + 1;
118 // Derive Good Friday
119 t.tm_mday -= 2;
120 mktime(&t);
121 *friM = t.tm_mon + 1;
122 *friD = t.tm_mday;
123 // Derive Easter Monday
124 t.tm_mday += 3;
125 mktime(&t);
126 *monM = t.tm_mon + 1;
127 *monD = t.tm_mday;
128 }
129 static int GetNthDayOfWeek(int y, const int m, const int n, const int wday)
130 {
131 // E.g. GetNthDayOfWeek(2025, 12, 3, 4) -> 3rd Thursday of Nov 2025 (18th)
132 // Find weekday of first day of month
133 int d = 1;
134 const int weekday = (d += m < 3 ? y-- : y - 2,
135 23 * m / 9 + d + 4 + y / 4 - y / 100 + y / 400) %
136 7;
137 const int firstDayOfWeek =
138 1 + (wday >= weekday ? wday - weekday : wday + 7 - weekday);
139 return firstDayOfWeek + 7 * (n - 1);
140 }
141 static const char *GetFestiveHat(void)
142 {
143 static struct tm *t = NULL;
144 if (t == NULL)
145 {
146 time_t now = time(NULL);
147 t = localtime(&now);
148 }
149 if (ConfigGetBool(&gConfig, "Game.NoFestiveHats"))
150 {
151 return NULL;
152 }
153 if (t->tm_mon + 1 == 1 && t->tm_mday == 1)
154 return "party";
155 if (t->tm_mon + 1 == 3 && t->tm_mday == 17)
156 return "leprechaun";
157 if (t->tm_mon + 1 == 5 && t->tm_mday == 5)
158 return "sombrero";
159 if (t->tm_mon + 1 == 10 && t->tm_mday == 31)
160 return "witch";
161 if (t->tm_mon + 1 == 12 && t->tm_mday == 25)
162 return "santa";
163 int easterFriM, easterFriD, easterMonM, easterMonD;
164 GetEasterDates(
165 t->tm_year + 1900, &easterFriM, &easterFriD, &easterMonM, &easterMonD);
166 const bool isAfterEasterFri =
167 t->tm_mon + 1 > easterFriM ||
168 (t->tm_mon + 1 == easterFriM && t->tm_mday >= easterFriD);
169 const bool isBeforeEasterMon =
170 t->tm_mon + 1 < easterMonM ||
171 (t->tm_mon + 1 == easterMonM && t->tm_mday <= easterMonD);
172 if (isAfterEasterFri && isBeforeEasterMon)
173 return "bunny";
174 if (t->tm_mon + 1 == 11 &&
175 t->tm_mday ==
176 GetNthDayOfWeek(t->tm_year + 1900, 11, 4, 4)) // 4th Thu of Nov
177 return "capotain";
178 return NULL;
179 }
180
181 static direction_e GetLegDirAndFrame(
182 const TActor *a, const direction_e bodyDir, int *frame);
183 static ActorPics GetUnorderedPics(
184 const Character *c, const direction_e dir, const direction_e legDir,
185 const ActorAnimation anim, const int frame, const WeaponClass *gun,
186 const gunstate_e barrelStates[MAX_BARRELS], const bool isGrimacing,
187 const color_t shadowMask, const color_t *mask, const CharColors *colors,
188 const int deadPic);
189 static void UpdatePilotHeadPic(
190 ActorPics *pics, const TActor *a, const direction_e dir);
191 static void ReorderPics(
192 ActorPics *pics, const Character *c, const direction_e dir,
193 const WeaponClass *gun, const gunstate_e barrelStates[MAX_BARRELS]);
194 ActorPics GetCharacterPicsFromActor(const TActor *a)
195 {
196 if (a->vehicleUID != -1)
197 {
198 // Don't draw if piloting a vehicle - this actor will be drawn
199 // when the vehicle is drawn
200 ActorPics p;
201 memset(&p, 0, sizeof p);
202 return p;
203 }
204 const Character *c = ActorGetCharacter(a);
205 const Weapon *gun = ACTOR_GET_WEAPON(a);
206
207 color_t mask;
208 bool hasStatus = false;
209 if (a->flamed)
210 {
211 mask = colorRed;
212 hasStatus = true;
213 }
214 else if (a->poisoned)
215 {
216 mask = colorPoison;
217 hasStatus = true;
218 }
219 else if (a->petrified)
220 {
221 mask = colorGray;
222 hasStatus = true;
223 }
224 else if (a->confused)
225 {
226 mask = colorPurple;
227 hasStatus = true;
228 }
229
230 const CharColors allBlack = CharColorsFromOneColor(colorBlack);
231 const CharColors allWhite = CharColorsFromOneColor(colorWhite);
232 const bool isTransparent = !!(a->flags & FLAGS_SEETHROUGH);
233 const CharColors *colors = NULL;
234 const color_t *maskP = NULL;
235 color_t shadowMask = colorTransparent;
236 if (isTransparent)
237 {
238 colors = &allBlack;
239 maskP = &mask;
240 mask.a = TRANSPARENT_ACTOR_ALPHA;
241 }
242 else
243 {
244 shadowMask = a->PlayerUID >= 0 ? c->Colors.Body : colorBlack;
245 if (hasStatus)
246 {
247 maskP = &mask;
248 colors = &allWhite;
249 }
250 }
251
252 const direction_e dir = RadiansToDirection(a->DrawRadians);
253 int frame;
254 const direction_e legDir = GetLegDirAndFrame(a, dir, &frame);
255 gunstate_e gunStates[MAX_BARRELS];
256 memset(&gunStates, 0, sizeof gunStates);
257 for (int i = 0; gun->Gun != NULL && i < WeaponClassNumBarrels(gun->Gun);
258 i++)
259 {
260 gunStates[i] = gun->barrels[i].state;
261 }
262
263 ActorPics pics = GetUnorderedPics(
264 c, dir, legDir, a->anim.Type, frame, gun->Gun, gunStates,
265 ActorIsGrimacing(a), shadowMask, maskP, colors, a->dead);
266 UpdatePilotHeadPic(&pics, a, dir);
267 ReorderPics(&pics, c, dir, gun->Gun, gunStates);
268 return pics;
269 }
270 static void UpdatePilotHeadPic(
271 ActorPics *pics, const TActor *a, const direction_e dir)
272 {
273 if (a->pilotUID == a->uid)
274 {
275 return;
276 }
277 // If this is a vehicle, take the head/hair pic from the pilot
278 memset(&pics->HeadParts, 0, sizeof pics->HeadParts);
279 const TActor *pilot = ActorGetByUID(a->pilotUID);
280 if (pilot == NULL)
281 {
282 return;
283 }
284 const Character *c = ActorGetCharacter(pilot);
285 const bool grimace = ActorIsGrimacing(a);
286 pics->Head = GetHeadPic(c->Class, dir, grimace, &c->Colors);
287
288 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
289 {
290 if (c->Class->HasHeadParts[hp])
291 {
292 pics->HeadParts[hp] =
293 GetHeadPartPic(c->HeadParts[hp], hp, dir, grimace, &c->Colors);
294 }
295 }
296 }
297 ActorPics GetCharacterPics(
298 const Character *c, const direction_e dir, const direction_e legDir,
299 const ActorAnimation anim, const int frame, const WeaponClass *gun,
300 const gunstate_e barrelStates[MAX_BARRELS], const bool isGrimacing,
301 const color_t shadowMask, const color_t *mask, const CharColors *colors,
302 const int deadPic)
303 {
304 ActorPics pics = GetUnorderedPics(
305 c, dir, legDir, anim, frame, gun, barrelStates, isGrimacing,
306 shadowMask, mask, colors, deadPic);
307
308 ReorderPics(&pics, c, dir, gun, barrelStates);
309
310 return pics;
311 }
312 static const Pic *GetBodyPic(
313 PicManager *pm, const CharSprites *cs, const direction_e dir,
314 const ActorAnimation anim, const int frame, const int numBarrels,
315 const int grips, const gunstate_e barrelState, const CharColors *colors);
316 static const Pic *GetLegsPic(
317 PicManager *pm, const CharSprites *cs, const direction_e dir,
318 const ActorAnimation anim, const int frame, const CharColors *colors);
319 static const Pic *GetGunPic(
320 PicManager *pm, const char *gunSprites, const direction_e dir,
321 const int gunState, const CharColors *colors);
322 static ActorPics GetUnorderedPics(
323 const Character *c, const direction_e dir, const direction_e legDir,
324 const ActorAnimation anim, const int frame, const WeaponClass *gun,
325 const gunstate_e barrelStates[MAX_BARRELS], const bool isGrimacing,
326 const color_t shadowMask, const color_t *mask, const CharColors *colors,
327 const int deadPic)
328 {
329 ActorPics pics;
330 memset(&pics, 0, sizeof pics);
331
332 pics.Sprites = c->Class->Sprites;
333
334 // Dummy return to handle invalid character class
335 if (c->Class == NULL)
336 {
337 pics.IsDead = true;
338 pics.IsDying = true;
339 pics.Body = NULL;
340 pics.OrderedPics[0] = pics.Body;
341 return pics;
342 }
343
344 pics.ShadowMask = shadowMask;
345 if (mask != NULL)
346 {
347 pics.Mask = *mask;
348 }
349 else
350 {
351 pics.Mask = colorWhite;
352 }
353
354 // If the actor is dead, simply draw a dying animation
355 pics.IsDead = deadPic > 0;
356 if (pics.IsDead)
357 {
358 const NamedSprites *deathSprites =
359 CharacterClassGetDeathSprites(c->Class, &gPicManager);
360 if (deadPic - 1 < (int)deathSprites->pics.size)
361 {
362 pics.IsDying = true;
363 pics.Body = CArrayGet(&deathSprites->pics, deadPic - 1);
364 pics.OrderedPics[0] = pics.Body;
365 }
366 return pics;
367 }
368
369 if (colors == NULL)
370 {
371 colors = &c->Colors;
372 }
373
374 // Head
375 direction_e headDir = dir;
376 // If idle, turn head left/right on occasion
377 if (anim == ACTORANIMATION_IDLE)
378 {
379 if (frame == IDLEHEAD_LEFT)
380 headDir = (dir + 7) % 8;
381 else if (frame == IDLEHEAD_RIGHT)
382 headDir = (dir + 1) % 8;
383 }
384 bool grimace = isGrimacing;
385 const int numBarrels = (barrelStates == NULL || gun == NULL ||
386 WC_BARREL_ATTR(*gun, Sprites, 0) == NULL)
387 ? 0
388 : WeaponClassNumBarrels(gun);
389 for (int i = 0; i < MAX_BARRELS; i++)
390 {
391 if (barrelStates[i] == GUNSTATE_FIRING ||
392 barrelStates[i] == GUNSTATE_RECOIL)
393 {
394 grimace = true;
395 break;
396 }
397 }
398 const int grips = gun == NULL ? 0 : WC_BARREL_ATTR(*gun, Grips, 0);
399 pics.Head = GetHeadPic(c->Class, headDir, grimace, colors);
400 pics.HeadOffset = GetActorDrawOffset(
401 pics.Head, BODY_PART_HEAD, c->Class->Sprites, anim, frame, dir,
402 GUNSTATE_READY);
403
404 const char *festiveHat = GetFestiveHat();
405 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
406 {
407 // Holiday override
408 const bool hasHeadPart = c->Class->HasHeadParts[hp] ||
409 (hp == HEAD_PART_HAT && festiveHat != NULL);
410 if (hasHeadPart)
411 {
412 const char *headPart = (hp == HEAD_PART_HAT && festiveHat)
413 ? festiveHat
414 : c->HeadParts[hp];
415 pics.HeadParts[hp] =
416 GetHeadPartPic(headPart, hp, headDir, grimace, colors);
417 pics.HeadPartOffsets[hp] = GetActorDrawOffset(
418 pics.HeadParts[hp], BODY_PART_HEAD, c->Class->Sprites, anim,
419 frame, dir, GUNSTATE_READY);
420 }
421 }
422
423 // Gun
424 for (int i = 0; i < numBarrels; i++)
425 {
426 pics.Guns[i] = GetGunPic(
427 &gPicManager, WC_BARREL_ATTR(*gun, Sprites, i), dir,
428 barrelStates[i], colors);
429 if (pics.Guns[i] != NULL)
430 {
431 pics.GunOffsets[i] = GetActorDrawOffset(
432 pics.Guns[i], i == 0 ? BODY_PART_GUN_R : BODY_PART_GUN_L,
433 c->Class->Sprites, anim, frame,
434 i == 0 ? dir : DirectionMirrorX(dir), barrelStates[i]);
435 if (i == 1)
436 {
437 const int xHalf = pics.Guns[i]->size.x / 2;
438 const int xOffsetOrig = pics.GunOffsets[i].x + xHalf;
439 pics.GunOffsets[i].x = -xOffsetOrig - xHalf;
440 }
441 }
442 }
443
444 // Body
445 pics.Body = GetBodyPic(
446 &gPicManager, c->Class->Sprites, dir, anim, frame, numBarrels, grips,
447 barrelStates[0], colors);
448 pics.BodyOffset = GetActorDrawOffset(
449 pics.Body, BODY_PART_BODY, c->Class->Sprites, anim, frame, dir,
450 GUNSTATE_READY);
451
452 // Legs
453 pics.Legs = GetLegsPic(
454 &gPicManager, c->Class->Sprites, legDir, anim, frame, colors);
455 pics.LegsOffset = GetActorDrawOffset(
456 pics.Legs, BODY_PART_LEGS, c->Class->Sprites, anim, frame, legDir,
457 GUNSTATE_READY);
458 return pics;
459 }
460 static direction_e GetLegDirAndFrame(
461 const TActor *a, const direction_e bodyDir, int *frame)
462 {
463 *frame = AnimationGetFrame(&a->anim);
464 const struct vec2 vel = svec2_add(a->MoveVel, a->thing.Vel);
465 if (svec2_is_zero(vel))
466 {
467 return bodyDir;
468 }
469 const direction_e legDir = RadiansToDirection(svec2_angle(vel) + MPI_2);
470 // Walk backwards if the leg dir is >90 degrees from body dir
471 const int dirDiff = abs((int)bodyDir - (int)legDir);
472 const bool reversed = dirDiff > 2 && dirDiff < 6;
473 if (reversed)
474 {
475 *frame = ANIMATION_MAX_FRAMES - *frame;
476 return DirectionOpposite(legDir);
477 }
478 return legDir;
479 }
480 static void ReorderPics(
481 ActorPics *pics, const Character *c, const direction_e dir,
482 const WeaponClass *gun, const gunstate_e barrelStates[MAX_BARRELS])
483 {
484 // Determine draw order based on the direction the player is facing
485 // Rotate direction left for 2-grip guns, as the gun is held in front
486 // of the actor
487 const int grips = gun == NULL ? 0 : WC_BARREL_ATTR(*gun, Grips, 0);
488 const direction_e drawOrderDir =
489 grips == 2 && barrelStates[0] == GUNSTATE_READY
490 ? DirectionRotate(dir, -1)
491 : dir;
492 for (int bp = 0; bp < BODY_PART_COUNT; bp++)
493 {
494 const BodyPart drawOrder = c->Class->Sprites->Order[drawOrderDir][bp];
495 switch (drawOrder)
496 {
497 case BODY_PART_HEAD:
498 pics->OrderedPics[bp] = pics->Head;
499 pics->OrderedOffsets[bp] = pics->HeadOffset;
500 break;
501 case BODY_PART_HAIR:
502 pics->OrderedPics[bp] = pics->HeadParts[HEAD_PART_HAIR];
503 pics->OrderedOffsets[bp] = pics->HeadPartOffsets[HEAD_PART_HAIR];
504 break;
505 case BODY_PART_FACEHAIR:
506 pics->OrderedPics[bp] = pics->HeadParts[HEAD_PART_FACEHAIR];
507 pics->OrderedOffsets[bp] =
508 pics->HeadPartOffsets[HEAD_PART_FACEHAIR];
509 break;
510 case BODY_PART_HAT:
511 pics->OrderedPics[bp] = pics->HeadParts[HEAD_PART_HAT];
512 pics->OrderedOffsets[bp] = pics->HeadPartOffsets[HEAD_PART_HAT];
513 break;
514 case BODY_PART_GLASSES:
515 pics->OrderedPics[bp] = pics->HeadParts[HEAD_PART_GLASSES];
516 pics->OrderedOffsets[bp] =
517 pics->HeadPartOffsets[HEAD_PART_GLASSES];
518 break;
519 case BODY_PART_BODY:
520 pics->OrderedPics[bp] = pics->Body;
521 pics->OrderedOffsets[bp] = pics->BodyOffset;
522 break;
523 case BODY_PART_LEGS:
524 pics->OrderedPics[bp] = pics->Legs;
525 pics->OrderedOffsets[bp] = pics->LegsOffset;
526 break;
527 case BODY_PART_GUN_R:
528 pics->OrderedPics[bp] = pics->Guns[0];
529 pics->OrderedOffsets[bp] = pics->GunOffsets[0];
530 break;
531 case BODY_PART_GUN_L:
532 pics->OrderedPics[bp] = pics->Guns[1];
533 pics->OrderedOffsets[bp] = pics->GunOffsets[1];
534 break;
535 default:
536 break;
537 }
538 }
539 }
540
541 static void DrawDyingBody(
542 GraphicsDevice *g, const ActorPics *pics, const struct vec2i pos,
543 const Rect2i bounds);
544 void DrawActorPics(
545 const ActorPics *pics, const struct vec2i pos, const Rect2i bounds)
546 {
547 if (pics->IsDead)
548 {
549 if (pics->IsDying)
550 {
551 DrawDyingBody(&gGraphicsDevice, pics, pos, bounds);
552 }
553 }
554 else
555 {
556 // TODO: use bounds
557 DrawShadow(&gGraphicsDevice, pos, svec2(8, 6), pics->ShadowMask);
558 for (int i = 0; i < BODY_PART_COUNT; i++)
559 {
560 const Pic *pic = pics->OrderedPics[i];
561 if (pic == NULL)
562 {
563 continue;
564 }
565 const struct vec2i drawPos =
566 svec2i_add(pos, pics->OrderedOffsets[i]);
567 const Rect2i drawSrc =
568 Rect2iIsZero(bounds)
569 ? bounds
570 : Rect2iNew(
571 svec2i_subtract(bounds.Pos, drawPos),
572 svec2i_subtract(bounds.Size, bounds.Pos));
573 PicRender(
574 pic, gGraphicsDevice.gameWindow.renderer, drawPos, pics->Mask,
575 0, svec2_one(), SDL_FLIP_NONE, drawSrc);
576 }
577 }
578 }
579 static void DrawLaserSightSingle(
580 const struct vec2i from, const float radians, const int range,
581 const color_t color);
582 void DrawLaserSight(
583 const ActorPics *pics, const TActor *a, const struct vec2i picPos)
584 {
585 // Don't draw if dead or transparent
586 if (pics->IsDead || ColorEquals(pics->ShadowMask, colorTransparent))
587 return;
588 // Check config
589 const LaserSight ls = ConfigGetEnum(&gConfig, "Game.LaserSight");
590 if (ls != LASER_SIGHT_ALL &&
591 !(ls == LASER_SIGHT_PLAYERS && a->PlayerUID >= 0))
592 {
593 return;
594 }
595 // Draw weapon indicators
596 const Weapon *w = ACTOR_GET_WEAPON(a);
597 const WeaponClass *wc = w->Gun;
598 if (wc == NULL)
599 {
600 return;
601 }
602 for (int i = 0; i < WeaponClassNumBarrels(wc); i++)
603 {
604 struct vec2i muzzlePos = svec2i_add(
605 picPos, svec2i_assign_vec2(ActorGetMuzzleOffset(a, w, i)));
606 const WeaponClass *wcb = WeaponClassGetBarrel(wc, i);
607 muzzlePos.y -= wcb->u.Normal.MuzzleHeight / Z_FACTOR;
608 const float radians =
609 dir2radians[a->direction] + wcb->u.Normal.AngleOffset;
610 const int range = (int)WeaponClassGetRange(wcb);
611 color_t color = colorCyan;
612 color.a = 64;
613 const float spreadHalf =
614 (wcb->u.Normal.Spread.Count - 1) * wcb->u.Normal.Spread.Width / 2 +
615 wcb->u.Normal.Recoil / 2;
616 if (spreadHalf > 0)
617 {
618 DrawLaserSightSingle(
619 muzzlePos, radians - spreadHalf, range, color);
620 DrawLaserSightSingle(
621 muzzlePos, radians + spreadHalf, range, color);
622 }
623 else
624 {
625 DrawLaserSightSingle(muzzlePos, radians, range, color);
626 }
627 }
628 }
629 static void DrawLaserSightSingle(
630 const struct vec2i from, const float radians, const int range,
631 const color_t color)
632 {
633 const struct vec2 v =
634 svec2_scale(Vec2FromRadiansScaled(radians), (float)range);
635 const struct vec2i to = svec2i_add(from, svec2i_assign_vec2(v));
636 DrawLine(from, to, color);
637 }
638
639 const Pic *GetHeadPic(
640 const CharacterClass *c, const direction_e dir, const bool isGrimacing,
641 const CharColors *colors)
642 {
643 if (strlen(c->HeadSprites) == 0)
644 {
645 return NULL;
646 }
647 // If firing, draw the firing head pic
648 const int row = isGrimacing ? 1 : 0;
649 const int idx = (int)dir + row * 8;
650 // Get or generate masked sprites
651 const NamedSprites *ns =
652 PicManagerGetCharSprites(&gPicManager, c->HeadSprites, colors);
653 return CArrayGet(&ns->pics, idx);
654 }
655 const Pic *GetHeadPartPic(
656 const char *name, const HeadPart hp, const direction_e dir,
657 const bool isGrimacing, const CharColors *colors)
658 {
659 if (name == NULL)
660 {
661 return NULL;
662 }
663 const int row = isGrimacing ? 1 : 0;
664 const int idx = (int)dir + row * 8;
665 // Get or generate masked sprites
666 char buf[CDOGS_PATH_MAX];
667 const char *subpaths[] = {"hairs", "facehairs", "hats", "glasses"};
668 sprintf(buf, "chars/%s/%s", subpaths[hp], name);
669 const NamedSprites *ns =
670 PicManagerGetCharSprites(&gPicManager, buf, colors);
671 if (ns == NULL)
672 {
673 return NULL;
674 }
675 return CArrayGet(&ns->pics, idx);
676 }
677 static const Pic *GetBodyPic(
678 PicManager *pm, const CharSprites *cs, const direction_e dir,
679 const ActorAnimation anim, const int frame, const int numBarrels,
680 const int grips, const gunstate_e barrelState, const CharColors *colors)
681 {
682 const int stride = anim == ACTORANIMATION_WALKING ? 8 : 1;
683 const int col = frame % stride;
684 const int row = (int)dir;
685 const int idx = col + row * stride;
686 char buf[CDOGS_PATH_MAX];
687 CASSERT(numBarrels <= 2, "up to 2 barrels supported");
688 const NamedSprites *ns = NULL;
689 const char *upperPose = "";
690 if (numBarrels == 1)
691 {
692 upperPose = "_handgun";
693 }
694 if (numBarrels == 2)
695 {
696 upperPose = "_dualgun";
697 }
698 if (grips == 2)
699 {
700 upperPose = "_rifle";
701 if (barrelState == GUNSTATE_FIRING || barrelState == GUNSTATE_RECOIL)
702 {
703 upperPose = "_riflefire";
704 }
705 }
706 for (;;)
707 {
708 sprintf(
709 buf, "chars/bodies/%s/upper_%s%s", cs->Name,
710 anim == ACTORANIMATION_WALKING ? "run" : "idle",
711 upperPose); // TODO: other gun holding poses
712 // Get or generate masked sprites
713 ns = PicManagerGetCharSprites(pm, buf, colors);
714 // TODO: provide dualgun sprites for all body types
715 if (ns == NULL && strcmp(upperPose, "_handgun") != 0)
716 {
717 upperPose = "_handgun";
718 continue;
719 }
720 break;
721 }
722 return CArrayGet(&ns->pics, idx);
723 }
724 static const Pic *GetLegsPic(
725 PicManager *pm, const CharSprites *cs, const direction_e dir,
726 const ActorAnimation anim, const int frame, const CharColors *colors)
727 {
728 const int stride = anim == ACTORANIMATION_WALKING ? 8 : 1;
729 const int col = frame % stride;
730 const int row = (int)dir;
731 const int idx = col + row * stride;
732 char buf[CDOGS_PATH_MAX];
733 sprintf(
734 buf, "chars/bodies/%s/legs_%s", cs->Name,
735 anim == ACTORANIMATION_WALKING ? "run" : "idle");
736 // Get or generate masked sprites
737 const NamedSprites *ns = PicManagerGetCharSprites(pm, buf, colors);
738 return CArrayGet(&ns->pics, idx);
739 }
740 static const Pic *GetGunPic(
741 PicManager *pm, const char *gunSprites, const direction_e dir,
742 const int gunState, const CharColors *colors)
743 {
744 const int idx = (gunState == GUNSTATE_READY ? 8 : 0) + dir;
745 // Get or generate masked sprites
746 const NamedSprites *ns = PicManagerGetCharSprites(pm, gunSprites, colors);
747 if (ns == NULL)
748 {
749 return NULL;
750 }
751 return CArrayGet(&ns->pics, idx);
752 }
753
754 void DrawCharacterSimple(
755 const Character *c, const struct vec2i pos, const direction_e d,
756 const bool hilite, const bool showGun, const WeaponClass *gun)
757 {
758 const gunstate_e barrelStates[MAX_BARRELS] = {
759 GUNSTATE_READY, GUNSTATE_READY};
760 ActorPics pics = GetCharacterPics(
761 c, d, d, ACTORANIMATION_IDLE, 0, gun, barrelStates, false, colorBlack,
762 NULL, NULL, 0);
763 DrawActorPics(&pics, pos, Rect2iZero());
764 if (hilite)
765 {
766 FontCh('>', svec2i_add(pos, svec2i(-8, -16)));
767 if (showGun)
768 {
769 FontStr(c->Gun->name, svec2i_add(pos, svec2i(-8, 8)));
770 }
771 }
772 }
773
774 void DrawHead(
775 SDL_Renderer *renderer, const Character *c, const direction_e dir,
776 const struct vec2i pos)
777 {
778 const bool isGrimacing = false;
779 const Pic *head = GetHeadPic(c->Class, dir, isGrimacing, &c->Colors);
780 const struct vec2i headOffset = GetActorDrawOffset(
781 head, BODY_PART_HEAD, c->Class->Sprites, ACTORANIMATION_IDLE, 0,
782 DIRECTION_DOWN, GUNSTATE_READY);
783 const color_t mask = colorWhite;
784 const struct vec2i charOffset = svec2i(0, 12);
785 PicRender(
786 head, renderer, svec2i_add(svec2i_add(pos, headOffset), charOffset),
787 mask, 0, svec2_one(), SDL_FLIP_NONE, Rect2iZero());
788
789 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
790 {
791 if (c->Class->HasHeadParts[hp])
792 {
793 const Pic *pic = GetHeadPartPic(
794 c->HeadParts[hp], hp, dir, isGrimacing, &c->Colors);
795 if (pic)
796 {
797 const struct vec2i headPartOffset = GetActorDrawOffset(
798 pic, BODY_PART_HEAD, c->Class->Sprites,
799 ACTORANIMATION_IDLE, 0, DIRECTION_DOWN, GUNSTATE_READY);
800 PicRender(
801 pic, renderer,
802 svec2i_add(svec2i_add(pos, headPartOffset), charOffset),
803 mask, 0, svec2_one(), SDL_FLIP_NONE, Rect2iZero());
804 }
805 }
806 }
807 }
808 #define DYING_BODY_OFFSET 3
809 static void DrawDyingBody(
810 GraphicsDevice *g, const ActorPics *pics, const struct vec2i pos,
811 const Rect2i bounds)
812 {
813 const Pic *body = pics->Body;
814 const struct vec2i drawPos = svec2i_subtract(
815 pos, svec2i(body->size.x / 2, body->size.y / 2 + DYING_BODY_OFFSET));
816 const Rect2i drawSrc = Rect2iIsZero(bounds)
817 ? bounds
818 : Rect2iNew(
819 svec2i_subtract(bounds.Pos, drawPos),
820 svec2i_subtract(bounds.Size, bounds.Pos));
821 PicRender(
822 body, g->gameWindow.renderer, drawPos, pics->Mask, 0, svec2_one(),
823 SDL_FLIP_NONE, drawSrc);
824 }