Debian Codegraph @

src/cdogs/weapon_usage.c

raw symbols SCIP
1 /*
2 Copyright (c) 2025 Cong Xu
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 POSSIBILITY OF SUCH DAMAGE.
25 */
26 #include "weapon_usage.h"
27
28 WeaponUsages WeaponUsagesNew(void)
29 {
30 return hashmap_new();
31 }
32
33 void WeaponUsagesTerminate(WeaponUsages wu)
34 {
35 hashmap_free(wu);
36 }
37
38 void WeaponUsagesUpdate(
39 WeaponUsages wu, const WeaponClass *wc, const int dShot, const int dHit)
40 {
41 if (!wc || !wc->IsRealGun)
42 {
43 return;
44 }
45 NWeaponUsage *w = NULL;
46 if (hashmap_get(wu, wc->name, (any_t *)&w) == MAP_MISSING)
47 {
48 CCALLOC(w, sizeof *w);
49 strcpy(w->Weapon, wc->name);
50 hashmap_put(wu, wc->name, w);
51 }
52 w->Shots += dShot;
53 w->Hits += dHit;
54 }
55
56 typedef struct
57 {
58 float accuracy;
59 int weight;
60 } GetAccuracyData;
61 static int GetAccuracyItem(any_t data, any_t item);
62 float WeaponUsagesGetAccuracy(const WeaponUsages wu)
63 {
64 CArray accuracies;
65 CArrayInit(&accuracies, sizeof(GetAccuracyData));
66 hashmap_iterate(wu, GetAccuracyItem, &accuracies);
67 float cumSum = 0;
68 int totalWeight = 0;
69 CA_FOREACH(const GetAccuracyData, gData, accuracies)
70 cumSum += gData->accuracy * gData->weight;
71 totalWeight += gData->weight;
72 CA_FOREACH_END()
73 return totalWeight > 0 ? cumSum / totalWeight : 0;
74 }
75 static int GetAccuracyItem(any_t data, any_t item)
76 {
77 NWeaponUsage *w = item;
78 // Don't include explosives in accuracy
79 if (w->Shots > 0)
80 {
81 const WeaponClass *wc = StrWeaponClass(w->Weapon);
82 if (!WeaponClassIsHighDPS(wc))
83 {
84 GetAccuracyData gData;
85 gData.accuracy = (float)w->Hits / w->Shots;
86 gData.weight = MAX(wc->Lock, 1);
87 CArray *accuracies = data;
88 CArrayPushBack(accuracies, &gData);
89 }
90 }
91 return MAP_OK;
92 }
93
94 typedef struct
95 {
96 const WeaponClass *wc;
97 int ticks;
98 } GetFavoriteData;
99 static int GetFavoriteItem(any_t data, any_t item);
100 const WeaponClass *WeaponUsagesGetFavorite(const WeaponUsages wu)
101 {
102 // Find the weapon that spent the most time shooting
103 GetFavoriteData gData = {NULL, 0};
104 hashmap_iterate(wu, GetFavoriteItem, &gData);
105 return gData.wc;
106 }
107 static int GetFavoriteItem(any_t data, any_t item)
108 {
109 GetFavoriteData *gData = data;
110 NWeaponUsage *w = item;
111 const WeaponClass *wc = StrWeaponClass(w->Weapon);
112 // TODO: this overcounts guns with multiple bullets per shot
113 // Need to divide by this multiple
114 const int ticks = w->Shots * MAX(wc->Lock, 1);
115 if (ticks > gData->ticks)
116 {
117 gData->wc = wc;
118 gData->ticks = ticks;
119 }
120 return MAP_OK;
121 }