Debian Codegraph @

src/cdogs/player.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2014-2016, 2018-2020, 2022-2023, 2025 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 "player.h"
29
30 #include "actors.h"
31 #include "events.h"
32 #include "log.h"
33 #include "net_client.h"
34 #include "player_template.h"
35
36 #define STARTING_CASH 10000
37
38 CArray gPlayerDatas;
39
40 void PlayerDataInit(CArray *p)
41 {
42 CArrayInit(p, sizeof(PlayerData));
43 }
44
45 void PlayerDataAddOrUpdate(const NPlayerData pd)
46 {
47 PlayerData *p = PlayerDataGetByUID(pd.UID);
48 if (p == NULL)
49 {
50 PlayerData pNew;
51 memset(&pNew, 0, sizeof pNew);
52 CArrayPushBack(&gPlayerDatas, &pNew);
53 p = CArrayGet(&gPlayerDatas, (int)gPlayerDatas.size - 1);
54
55 // Set defaults
56 p->ActorUID = -1;
57 p->IsLocal =
58 (int)pd.UID >= gNetClient.FirstPlayerUID &&
59 (int)pd.UID < gNetClient.FirstPlayerUID + MAX_LOCAL_PLAYERS;
60 CArrayInitFillZero(&p->ammo, sizeof(int), pd.Ammo_count);
61 p->inputDevice = INPUT_DEVICE_UNSET;
62
63 p->Char.speed = 1;
64
65 p->WeaponUsages = WeaponUsagesNew();
66
67 LOG(LM_MAIN, LL_INFO, "add default player UID(%u) local(%s)", pd.UID,
68 p->IsLocal ? "true" : "false");
69 }
70
71 p->UID = pd.UID;
72
73 strcpy(p->name, pd.Name);
74 p->Char.Class = StrCharacterClass(pd.CharacterClass);
75 if (p->Char.Class == NULL)
76 {
77 p->Char.Class = StrCharacterClass("Jones");
78 }
79 #define ADDHEADPART(_hp, _pdPart) \
80 CFREE(p->Char.HeadParts[_hp]); \
81 p->Char.HeadParts[_hp] = NULL; \
82 if (strlen(_pdPart) > 0) \
83 { \
84 CSTRDUP(p->Char.HeadParts[_hp], _pdPart); \
85 }
86 ADDHEADPART(HEAD_PART_HAIR, pd.Hair);
87 ADDHEADPART(HEAD_PART_FACEHAIR, pd.Facehair);
88 ADDHEADPART(HEAD_PART_HAT, pd.Hat);
89 ADDHEADPART(HEAD_PART_GLASSES, pd.Glasses);
90 p->Char.Colors = Net2CharColors(pd.Colors);
91 for (int i = 0; i < (int)pd.Weapons_count; i++)
92 {
93 p->guns[i] = NULL;
94 if (strlen(pd.Weapons[i]) > 0)
95 {
96 const WeaponClass *wc = StrWeaponClass(pd.Weapons[i]);
97 p->guns[i] = wc;
98 }
99 }
100 CArrayFillZero(&p->ammo);
101 for (int i = 0; i < (int)pd.Ammo_count; i++)
102 {
103 CArraySet(&p->ammo, pd.Ammo[i].Id, &pd.Ammo[i].Amount);
104 }
105 p->Lives = pd.Lives;
106 p->Stats = pd.Stats;
107 p->Totals = pd.Totals;
108 p->Char.maxHealth = pd.MaxHealth;
109 p->Char.excessHealth = pd.ExcessHealth;
110 p->HP = pd.HP;
111 p->lastMission = pd.LastMission;
112
113 // Ready players as well
114 p->Ready = true;
115
116 LOG(LM_MAIN, LL_INFO,
117 "update player UID(%d) maxHealth(%d) excessHealth(%d) HP(%d)", p->UID,
118 p->Char.maxHealth, p->Char.excessHealth, p->HP);
119 }
120
121 static void PlayerTerminate(PlayerData *p);
122 void PlayerRemove(const int uid)
123 {
124 // Find the player so we can remove by index
125 PlayerData *p = NULL;
126 int i;
127 for (i = 0; i < (int)gPlayerDatas.size; i++)
128 {
129 PlayerData *pi = CArrayGet(&gPlayerDatas, i);
130 if (pi->UID == uid)
131 {
132 p = pi;
133 break;
134 }
135 }
136 if (p == NULL)
137 {
138 return;
139 }
140 if (p->ActorUID >= 0)
141 {
142 ActorDestroy(ActorGetByUID(p->ActorUID));
143 }
144 PlayerTerminate(p);
145 CArrayDelete(&gPlayerDatas, i);
146
147 LOG(LM_MAIN, LL_INFO, "remove player UID(%d)", uid);
148 }
149
150 static void PlayerTerminate(PlayerData *p)
151 {
152 CFREE(p->Char.bot);
153 WeaponUsagesTerminate(p->WeaponUsages);
154 }
155
156 NPlayerData PlayerDataDefault(const int idx)
157 {
158 NPlayerData pd = NPlayerData_init_default;
159
160 pd.has_Colors = pd.has_Stats = pd.has_Totals = true;
161
162 // load from template if available
163 const PlayerTemplate *t = PlayerTemplateGetById(&gPlayerTemplates, idx);
164 if (t != NULL)
165 {
166 strcpy(pd.Name, t->name);
167 strcpy(pd.CharacterClass, t->CharClassName);
168 if (t->HeadParts[HEAD_PART_HAIR] != NULL)
169 {
170 strcpy(pd.Hair, t->HeadParts[HEAD_PART_HAIR]);
171 }
172 if (t->HeadParts[HEAD_PART_FACEHAIR] != NULL)
173 {
174 strcpy(pd.Facehair, t->HeadParts[HEAD_PART_FACEHAIR]);
175 }
176 if (t->HeadParts[HEAD_PART_HAT] != NULL)
177 {
178 strcpy(pd.Hat, t->HeadParts[HEAD_PART_HAT]);
179 }
180 if (t->HeadParts[HEAD_PART_GLASSES] != NULL)
181 {
182 strcpy(pd.Glasses, t->HeadParts[HEAD_PART_GLASSES]);
183 }
184 pd.Colors = CharColors2Net(t->Colors);
185 }
186 else
187 {
188 switch (idx)
189 {
190 case 0:
191 strcpy(pd.Name, "Jones");
192 strcpy(pd.CharacterClass, "Jones");
193 pd.Colors.Skin = Color2Net(colorSkin);
194 pd.Colors.Arms = Color2Net(colorLightBlue);
195 pd.Colors.Body = Color2Net(colorLightBlue);
196 pd.Colors.Legs = Color2Net(colorLightBlue);
197 pd.Colors.Feet = Color2Net(colorLightBlue);
198 break;
199 case 1:
200 strcpy(pd.Name, "Ice");
201 strcpy(pd.CharacterClass, "Jones");
202 strcpy(pd.Glasses, "shades");
203 pd.Colors.Skin = Color2Net(colorDarkSkin);
204 pd.Colors.Arms = Color2Net(colorRed);
205 pd.Colors.Body = Color2Net(colorRed);
206 pd.Colors.Legs = Color2Net(colorRed);
207 pd.Colors.Feet = Color2Net(colorRed);
208 pd.Colors.Glasses = Color2Net(colorBlack);
209 break;
210 case 2:
211 strcpy(pd.Name, "Warbaby");
212 strcpy(pd.CharacterClass, "Jones");
213 strcpy(pd.Hat, "beret");
214 pd.Colors.Skin = Color2Net(colorSkin);
215 pd.Colors.Arms = Color2Net(colorGreen);
216 pd.Colors.Body = Color2Net(colorGreen);
217 pd.Colors.Legs = Color2Net(colorGreen);
218 pd.Colors.Feet = Color2Net(colorGreen);
219 pd.Colors.Hat = Color2Net(colorRed);
220 break;
221 case 3:
222 strcpy(pd.Name, "Han");
223 strcpy(pd.CharacterClass, "Jones");
224 strcpy(pd.Hair, "rattail");
225 strcpy(pd.Facehair, "handlebar");
226 pd.Colors.Skin = Color2Net(colorAsianSkin);
227 pd.Colors.Arms = Color2Net(colorYellow);
228 pd.Colors.Body = Color2Net(colorYellow);
229 pd.Colors.Legs = Color2Net(colorYellow);
230 pd.Colors.Hair = Color2Net(colorYellow);
231 pd.Colors.Feet = Color2Net(colorYellow);
232 pd.Colors.Facehair = Color2Net(colorYellow);
233 break;
234 default:
235 // Set up player N template
236 sprintf(pd.Name, "Player %d", idx);
237 strcpy(pd.CharacterClass, "Jones");
238 pd.Colors.Skin = Color2Net(colorSkin);
239 pd.Colors.Arms = Color2Net(colorLightBlue);
240 pd.Colors.Body = Color2Net(colorLightBlue);
241 pd.Colors.Legs = Color2Net(colorLightBlue);
242 pd.Colors.Hair = Color2Net(colorLightBlue);
243 pd.Colors.Feet = Color2Net(colorLightBlue);
244 break;
245 }
246 }
247
248 pd.HP = CampaignGetHP(&gCampaign);
249 pd.MaxHealth = CampaignGetMaxHP(&gCampaign);
250 pd.ExcessHealth = CampaignGetExcessHP(&gCampaign);
251 pd.Lives = CampaignGetLives(&gCampaign);
252 if (gCampaign.Setting.BuyAndSell)
253 {
254 pd.Totals.Score = STARTING_CASH;
255 }
256
257 pd.Ammo_count = (pb_size_t)AmmoGetNumClasses(&gAmmo);
258
259 return pd;
260 }
261
262 NPlayerData PlayerDataMissionReset(const PlayerData *p)
263 {
264 NPlayerData pd = NMakePlayerData(p);
265 if (gCampaign.Setting.PlayerHP == 0)
266 {
267 pd.HP = CampaignGetHP(&gCampaign);
268 }
269
270 memset(&pd.Stats, 0, sizeof pd.Stats);
271
272 pd.LastMission = gCampaign.MissionIndex;
273 pd.MaxHealth = CampaignGetMaxHP(&gCampaign);
274 pd.ExcessHealth = CampaignGetExcessHP(&gCampaign);
275 return pd;
276 }
277
278 void PlayerDataTerminate(CArray *p)
279 {
280 for (int i = 0; i < (int)p->size; i++)
281 {
282 PlayerTerminate(CArrayGet(p, i));
283 }
284 CArrayTerminate(p);
285 }
286
287 PlayerData *PlayerDataGetByUID(const int uid)
288 {
289 if (uid == -1)
290 {
291 return NULL;
292 }
293 CA_FOREACH(PlayerData, p, gPlayerDatas)
294 if (p->UID == uid)
295 return p;
296 CA_FOREACH_END()
297 return NULL;
298 }
299
300 int FindLocalPlayerIndex(const int uid)
301 {
302 const PlayerData *p = PlayerDataGetByUID(uid);
303 if (p == NULL || !p->IsLocal)
304 {
305 // This update was for a non-local player; abort
306 return -1;
307 }
308 // Note: player UIDs divided by MAX_LOCAL_PLAYERS per client
309 return uid % MAX_LOCAL_PLAYERS;
310 }
311
312 int GetNumPlayers(
313 const PlayerAliveOptions alive, const bool human, const bool local)
314 {
315 int numPlayers = 0;
316 CA_FOREACH(const PlayerData, p, gPlayerDatas)
317 bool life = false;
318 switch (alive)
319 {
320 case PLAYER_ANY:
321 life = true;
322 break;
323 case PLAYER_ALIVE:
324 life = IsPlayerAlive(p);
325 break;
326 case PLAYER_ALIVE_OR_DYING:
327 life = IsPlayerAliveOrDying(p);
328 break;
329 }
330 if (life && (!human || p->inputDevice != INPUT_DEVICE_AI) &&
331 (!local || p->IsLocal))
332 {
333 numPlayers++;
334 }
335 CA_FOREACH_END()
336 return numPlayers;
337 }
338
339 bool AreAllPlayersDeadAndNoLives(void)
340 {
341 CA_FOREACH(const PlayerData, p, gPlayerDatas)
342 if (IsPlayerAlive(p) || p->Lives > 0)
343 {
344 return false;
345 }
346 CA_FOREACH_END()
347 return true;
348 }
349
350 const PlayerData *GetFirstPlayer(
351 const bool alive, const bool human, const bool local)
352 {
353 CA_FOREACH(const PlayerData, p, gPlayerDatas)
354 if ((!alive || IsPlayerAliveOrDying(p)) &&
355 (!human || p->inputDevice != INPUT_DEVICE_AI) &&
356 (!local || p->IsLocal))
357 {
358 return p;
359 }
360 CA_FOREACH_END()
361 return NULL;
362 }
363
364 bool IsPlayerAlive(const PlayerData *player)
365 {
366 if (player == NULL || player->ActorUID == -1)
367 {
368 return false;
369 }
370 const TActor *p = ActorGetByUID(player->ActorUID);
371 return !p->dead;
372 }
373 bool IsPlayerHuman(const PlayerData *player)
374 {
375 return player->inputDevice != INPUT_DEVICE_AI;
376 }
377 bool IsPlayerHumanAndAlive(const PlayerData *player)
378 {
379 return IsPlayerAlive(player) && IsPlayerHuman(player);
380 }
381 bool IsPlayerAliveOrDying(const PlayerData *player)
382 {
383 if (player->ActorUID == -1)
384 {
385 return false;
386 }
387 const TActor *p = ActorGetByUID(player->ActorUID);
388 const NamedSprites *deathSprites = CharacterClassGetDeathSprites(
389 ActorGetCharacter(p)->Class, &gPicManager);
390 return p->dead <= (int)deathSprites->pics.size;
391 }
392 bool IsPlayerScreen(const PlayerData *p)
393 {
394 const bool humanOnly = IsPVP(gCampaign.Entry.Mode) ||
395 !ConfigGetBool(&gConfig, "Interface.SplitscreenAI");
396 const bool humanOrScreen = !humanOnly || p->inputDevice != INPUT_DEVICE_AI;
397 return p->IsLocal && humanOrScreen && IsPlayerAliveOrDying(p);
398 }
399
400 struct vec2 PlayersGetMidpoint(void)
401 {
402 // for all surviving players, find bounding rectangle, and get center
403 struct vec2 min, max;
404 PlayersGetBoundingRectangle(&min, &max);
405 return svec2_scale(svec2_add(min, max), 0.5f);
406 }
407
408 void PlayersGetBoundingRectangle(struct vec2 *min, struct vec2 *max)
409 {
410 bool isFirst = true;
411 *min = svec2_zero();
412 *max = svec2_zero();
413 const bool humansOnly =
414 GetNumPlayers(PLAYER_ALIVE_OR_DYING, true, false) > 0;
415 CA_FOREACH(const PlayerData, p, gPlayerDatas)
416 if (!p->IsLocal)
417 {
418 continue;
419 }
420 if (humansOnly ? IsPlayerHumanAndAlive(p) : IsPlayerAlive(p))
421 {
422 const TActor *player = ActorGetByUID(p->ActorUID);
423 const Thing *ti = &player->thing;
424 if (isFirst)
425 {
426 *min = *max = ti->Pos;
427 }
428 else
429 {
430 min->x = MIN(ti->Pos.x, min->x);
431 min->y = MIN(ti->Pos.y, min->y);
432 max->x = MAX(ti->Pos.x, max->x);
433 max->y = MAX(ti->Pos.y, max->y);
434 }
435 isFirst = false;
436 }
437 CA_FOREACH_END()
438 }
439
440 // Get the number of players that use this ammo
441 int PlayersNumUseAmmo(const int ammoId)
442 {
443 int numPlayersWithAmmo = 0;
444 CA_FOREACH(const PlayerData, p, gPlayerDatas)
445 if (!IsPlayerAlive(p))
446 {
447 continue;
448 }
449 const TActor *player = ActorGetByUID(p->ActorUID);
450 for (int j = 0; j < MAX_WEAPONS; j++)
451 {
452 const Weapon *w = &player->guns[j];
453 if (w->Gun == NULL)
454 continue;
455 for (int i = 0; i < WeaponClassNumBarrels(w->Gun); i++)
456 {
457 if (WC_BARREL_ATTR(*(w->Gun), AmmoId, i) == ammoId)
458 {
459 numPlayersWithAmmo++;
460 }
461 }
462 }
463 CA_FOREACH_END()
464 return numPlayersWithAmmo;
465 }
466
467 bool PlayerIsLocal(const int uid)
468 {
469 const PlayerData *p = PlayerDataGetByUID(uid);
470 return p != NULL && p->IsLocal;
471 }
472
473 void PlayerScore(PlayerData *p, const int points)
474 {
475 if (p == NULL)
476 {
477 return;
478 }
479 p->Stats.Score += points;
480 p->Totals.Score += points;
481 }
482
483 bool PlayerTrySetInputDevice(
484 PlayerData *p, const input_device_e d, const int idx)
485 {
486 if (p->inputDevice == d && p->deviceIndex == idx)
487 {
488 return false;
489 }
490 p->inputDevice = d;
491 p->deviceIndex = idx;
492 LOG(LM_MAIN, LL_DEBUG, "playerUID(%d) assigned input device(%d %d)",
493 p->UID, (int)d, idx);
494 return true;
495 }
496
497 bool PlayerTrySetUnusedInputDevice(
498 PlayerData *p, const input_device_e d, const int idx)
499 {
500 // Check that player's input device is unassigned
501 if (p->inputDevice != INPUT_DEVICE_UNSET)
502 return false;
503 // Check that no players use this input device
504 CA_FOREACH(const PlayerData, pOther, gPlayerDatas)
505 if (pOther->inputDevice == d && pOther->deviceIndex == idx)
506 {
507 return false;
508 }
509 CA_FOREACH_END()
510 return PlayerTrySetInputDevice(p, d, idx);
511 }
512
513 int PlayerGetNumWeapons(const PlayerData *p)
514 {
515 int count = 0;
516 for (int i = 0; i < MAX_WEAPONS; i++)
517 {
518 if (p->guns[i] != NULL)
519 {
520 count++;
521 }
522 }
523 return count;
524 }
525
526 bool PlayerHasGrenadeButton(const PlayerData *p)
527 {
528 return p && InputHasGrenadeButton(p->inputDevice, p->deviceIndex);
529 }
530
531 bool PlayerHasWeapon(const PlayerData *p, const WeaponClass *wc)
532 {
533 if (wc == NULL)
534 {
535 return false;
536 }
537 for (int i = 0; i < MAX_WEAPONS; i++)
538 {
539 if (p->guns[i] == wc)
540 {
541 return true;
542 }
543 }
544 return false;
545 }
546
547 bool PlayerHasWeaponUpgrade(const PlayerData *p, const WeaponClass *wc)
548 {
549 if (wc == NULL)
550 {
551 return false;
552 }
553 for (int i = 0; i < MAX_WEAPONS; i++)
554 {
555 const WeaponClass *prereq = WeaponClassGetPrerequisite(p->guns[i]);
556 if (prereq == wc)
557 {
558 return true;
559 }
560 }
561 return false;
562 }
563
564 static void AddWeaponToSlot(
565 PlayerData *p, const WeaponClass *wc, const int slot)
566 {
567 p->guns[slot] = wc;
568 // Add minimal ammo
569 const int numBarrels = WeaponClassNumBarrels(wc);
570 for (int i = 0; i < numBarrels; i++)
571 {
572 const int ammoId = WC_BARREL_ATTR(*wc, AmmoId, i);
573 if (ammoId < 0)
574 {
575 continue;
576 }
577 const Ammo *a = AmmoGetById(&gAmmo, ammoId);
578 const int startingAmount = a->Amount * AMMO_STARTING_MULTIPLE;
579 if (startingAmount > PlayerGetAmmoAmount(p, ammoId))
580 {
581 CArraySet(&p->ammo, ammoId, &startingAmount);
582 }
583 }
584 }
585
586 void PlayerAddWeaponToSlot(
587 PlayerData *p, const WeaponClass *wc, const int slot)
588 {
589 if (wc)
590 {
591 // See if the weapon or its pre-/post-requisite is already
592 // equipped; if so swap it with the slot
593 for (int i = 0; i < MAX_WEAPONS; i++)
594 {
595 if (i == slot)
596 {
597 continue;
598 }
599 if (p->guns[i] && (p->guns[i] == wc ||
600 WeaponClassGetPrerequisite(p->guns[i]) == wc ||
601 p->guns[i] == WeaponClassGetPrerequisite(wc)))
602 {
603 p->guns[i] = p->guns[slot];
604 PlayerAddWeaponToSlot(p, wc, i);
605 return;
606 }
607 }
608 }
609 // Remove old weapon
610 if (p->guns[slot])
611 {
612 PlayerRemoveWeapon(p, slot);
613 }
614 // Subtract gun price
615 if (gCampaign.Setting.BuyAndSell)
616 {
617 PlayerScore(p, -WeaponClassFullPrice(wc));
618 }
619 AddWeaponToSlot(p, wc, slot);
620 }
621
622 void PlayerAddWeapon(PlayerData *p, const WeaponClass *wc)
623 {
624 if (PlayerHasWeapon(p, wc))
625 {
626 return;
627 }
628 // Add the weapon to the next empty slot, based on type
629 int first = 0;
630 int max = MELEE_SLOT;
631 if (wc->Type == GUNTYPE_MELEE)
632 {
633 first = MELEE_SLOT;
634 max = MELEE_SLOT + 1;
635 }
636 else if (wc->Type == GUNTYPE_GRENADE)
637 {
638 first = MAX_GUNS;
639 max = first + MAX_GRENADES;
640 }
641 for (int i = first; i < max; i++)
642 {
643 if (p->guns[i] == NULL)
644 {
645 AddWeaponToSlot(p, wc, i);
646 break;
647 }
648 }
649 }
650
651 void PlayerRemoveWeapon(PlayerData *p, const int slot)
652 {
653 // Refund gun price
654 if (gCampaign.Setting.BuyAndSell)
655 {
656 PlayerScore(p, WeaponClassFullPrice(p->guns[slot]));
657 }
658 // Refund ammo if no guns use this ammo
659 int ammoIds[MAX_BARRELS] = {-1, -1};
660 const int numBarrels = WeaponClassNumBarrels(p->guns[slot]);
661 for (int i = 0; i < numBarrels; i++)
662 {
663 ammoIds[i] = WC_BARREL_ATTR(*p->guns[slot], AmmoId, i);
664 }
665 p->guns[slot] = NULL;
666 PlayerAddMinimalWeapons(p);
667 for (int i = 0; i < MAX_BARRELS; i++)
668 {
669 if (ammoIds[i] >= 0 && !PlayerUsesAmmo(p, ammoIds[i]))
670 {
671 PlayerAddAmmo(
672 p, ammoIds[i], -PlayerGetAmmoAmount(p, ammoIds[i]), false);
673 }
674 }
675 }
676
677 void PlayerAddMinimalWeapons(PlayerData *p)
678 {
679 // Always have fists
680 PlayerAddWeapon(p, StrWeaponClass("Fists"));
681 }
682
683 bool PlayerUsesAmmo(const PlayerData *p, const int ammoId)
684 {
685 for (int i = 0; i < MAX_WEAPONS; i++)
686 {
687 if (p->guns[i] == NULL)
688 {
689 continue;
690 }
691 const int numBarrels = WeaponClassNumBarrels(p->guns[i]);
692 for (int j = 0; j < numBarrels; j++)
693 {
694 const int ammoId2 = WC_BARREL_ATTR(*p->guns[i], AmmoId, j);
695 if (ammoId == ammoId2)
696 {
697 return true;
698 }
699 }
700 }
701 return false;
702 }
703
704 bool PlayerUsesAnyAmmo(const PlayerData *p)
705 {
706 for (int i = 0; i < MAX_WEAPONS; i++)
707 {
708 if (p->guns[i] == NULL)
709 {
710 continue;
711 }
712 const int numBarrels = WeaponClassNumBarrels(p->guns[i]);
713 for (int j = 0; j < numBarrels; j++)
714 {
715 const int ammoId2 = WC_BARREL_ATTR(*p->guns[i], AmmoId, j);
716 if (ammoId2 >= 0)
717 {
718 return true;
719 }
720 }
721 }
722 return false;
723 }
724
725 int PlayerGetAmmoAmount(const PlayerData *p, const int ammoId)
726 {
727 return (ammoId >= 0 && (int)p->ammo.size >= ammoId)
728 ? *(int *)CArrayGet(&p->ammo, ammoId)
729 : 0;
730 }
731
732 void PlayerAddAmmo(
733 PlayerData *p, const int ammoId, const int amount, const bool isFree)
734 {
735 int *ammoAmount = CArrayGet(&p->ammo, ammoId);
736 const int oldAmount = *ammoAmount;
737 const Ammo *a = AmmoGetById(&gAmmo, ammoId);
738 *ammoAmount = CLAMP(*ammoAmount + amount, 0, a->Max);
739 if (!isFree && a->Price && gCampaign.Setting.BuyAndSell)
740 {
741 const int dLots = (oldAmount - *ammoAmount) / a->Amount;
742 PlayerScore(p, dLots * a->Price);
743 }
744 }
745
746 void PlayerSetHP(PlayerData *p, const int hp)
747 {
748 p->HP = CLAMP(hp, 1, CampaignGetMaxHP(&gCampaign));
749 }
750
751 void PlayerSetLives(PlayerData *p, const int lives)
752 {
753 p->Lives = CLAMP(lives, 1, CampaignGetMaxLives(&gCampaign));
754 }
755
756 int CharacterGetStartingHealth(const Character *c, const PlayerData *p)
757 {
758 if (p == NULL)
759 {
760 return MAX(
761 (c->maxHealth * ConfigGetInt(&gConfig, "Game.NonPlayerHP")) / 100,
762 1);
763 }
764 else
765 {
766 return p->HP > 0 ? p->HP : c->maxHealth;
767 }
768 }