| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 113 |
|
| 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 |
} |