| 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 |
|
| 27 |
|
| 28 |
#include "ammo.h" |
| 29 |
|
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#include "json_utils.h" |
| 33 |
#include "log.h" |
| 34 |
#include "pic_manager.h" |
| 35 |
#include "utils.h" |
| 36 |
|
| 37 |
AmmoClasses gAmmo; |
| 38 |
|
| 39 |
|
| 40 |
Ammo *StrAmmo(const char *s) |
| 41 |
{ |
| 42 |
return AmmoGetById(&gAmmo, StrAmmoId(s)); |
| 43 |
} |
| 44 |
int StrAmmoId(const char *s) |
| 45 |
{ |
| 46 |
if (s == NULL || strlen(s) == 0) |
| 47 |
{ |
| 48 |
return 0; |
| 49 |
} |
| 50 |
CA_FOREACH(Ammo, a, gAmmo.CustomAmmo) |
| 51 |
if (strcmp(s, a->Name) == 0) |
| 52 |
{ |
| 53 |
return _ca_index + (int)gAmmo.Ammo.size; |
| 54 |
} |
| 55 |
CA_FOREACH_END() |
| 56 |
CA_FOREACH(Ammo, a, gAmmo.Ammo) |
| 57 |
if (strcmp(s, a->Name) == 0) |
| 58 |
{ |
| 59 |
return _ca_index; |
| 60 |
} |
| 61 |
CA_FOREACH_END() |
| 62 |
CASSERT(false, "cannot parse ammo name"); |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
#define VERSION 2 |
| 67 |
static void LoadAmmo(Ammo *a, json_t *node, const int version); |
| 68 |
void AmmoInitialize(AmmoClasses *ammo, const char *path) |
| 69 |
{ |
| 70 |
memset(ammo, 0, sizeof *ammo); |
| 71 |
CArrayInit(&ammo->Ammo, sizeof(Ammo)); |
| 72 |
CArrayInit(&ammo->CustomAmmo, sizeof(Ammo)); |
| 73 |
|
| 74 |
json_t *root = NULL; |
| 75 |
enum json_error e; |
| 76 |
|
| 77 |
char buf[CDOGS_PATH_MAX]; |
| 78 |
GetDataFilePath(buf, path); |
| 79 |
FILE *f = fopen(buf, "r"); |
| 80 |
if (f == NULL) |
| 81 |
{ |
| 82 |
LOG(LM_MAIN, LL_ERROR, "Error: cannot load ammo file %s", buf); |
| 83 |
goto bail; |
| 84 |
} |
| 85 |
e = json_stream_parse(f, &root); |
| 86 |
if (e != JSON_OK) |
| 87 |
{ |
| 88 |
LOG(LM_MAIN, LL_ERROR, "Error parsing ammo file %s [error %d]", buf, |
| 89 |
(int)e); |
| 90 |
goto bail; |
| 91 |
} |
| 92 |
AmmoLoadJSON(&ammo->Ammo, root); |
| 93 |
|
| 94 |
bail: |
| 95 |
if (f) |
| 96 |
{ |
| 97 |
fclose(f); |
| 98 |
} |
| 99 |
json_free_value(&root); |
| 100 |
} |
| 101 |
void AmmoLoadJSON(CArray *ammo, json_t *node) |
| 102 |
{ |
| 103 |
int version; |
| 104 |
LoadInt(&version, node, "Version"); |
| 105 |
if (version > VERSION || version <= 0) |
| 106 |
{ |
| 107 |
CASSERT(false, "cannot read ammo file version"); |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
json_t *ammoNode = json_find_first_label(node, "Ammo")->child; |
| 112 |
for (json_t *child = ammoNode->child; child; child = child->next) |
| 113 |
{ |
| 114 |
Ammo a; |
| 115 |
LoadAmmo(&a, child, version); |
| 116 |
CArrayPushBack(ammo, &a); |
| 117 |
} |
| 118 |
} |
| 119 |
static void LoadAmmo(Ammo *a, json_t *node, const int version) |
| 120 |
{ |
| 121 |
memset(a, 0, sizeof *a); |
| 122 |
a->Name = GetString(node, "Name"); |
| 123 |
json_t *picNode = json_find_first_label(node, "Pic")->child; |
| 124 |
if (version < 2) |
| 125 |
{ |
| 126 |
CPicLoadNormal(&a->Pic, picNode); |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
CPicLoadJSON(&a->Pic, picNode); |
| 131 |
} |
| 132 |
a->Sound = GetString(node, "Sound"); |
| 133 |
LoadInt(&a->Amount, node, "Amount"); |
| 134 |
LoadInt(&a->Max, node, "Max"); |
| 135 |
LoadInt(&a->Price, node, "Price"); |
| 136 |
} |
| 137 |
void AmmoClassesClear(CArray *ammo) |
| 138 |
{ |
| 139 |
CA_FOREACH(Ammo, a, *ammo) |
| 140 |
CFREE(a->Name); |
| 141 |
CFREE(a->Sound); |
| 142 |
CFREE(a->DefaultGun); |
| 143 |
CA_FOREACH_END() |
| 144 |
CArrayClear(ammo); |
| 145 |
} |
| 146 |
void AmmoTerminate(AmmoClasses *ammo) |
| 147 |
{ |
| 148 |
AmmoClassesClear(&ammo->Ammo); |
| 149 |
CArrayTerminate(&ammo->Ammo); |
| 150 |
AmmoClassesClear(&ammo->CustomAmmo); |
| 151 |
CArrayTerminate(&ammo->CustomAmmo); |
| 152 |
} |
| 153 |
|
| 154 |
Ammo *AmmoGetById(AmmoClasses *ammo, const int id) |
| 155 |
{ |
| 156 |
if (id < (int)ammo->Ammo.size) |
| 157 |
{ |
| 158 |
return CArrayGet(&ammo->Ammo, id); |
| 159 |
} |
| 160 |
const int id2 = id - (int)ammo->Ammo.size; |
| 161 |
if (id2 < (int)ammo->CustomAmmo.size) |
| 162 |
{ |
| 163 |
return CArrayGet(&ammo->CustomAmmo, id2); |
| 164 |
} |
| 165 |
return NULL; |
| 166 |
} |
| 167 |
|
| 168 |
int AmmoGetNumClasses(const AmmoClasses *ammo) |
| 169 |
{ |
| 170 |
return (int)ammo->Ammo.size + (int)ammo->CustomAmmo.size; |
| 171 |
} |
| 172 |
|
| 173 |
bool AmmoIsLow(const Ammo *a, const int amount) |
| 174 |
{ |
| 175 |
|
| 176 |
return amount <= (a->Amount / 2); |
| 177 |
} |