20 #include "weapon.h" |
20 #include "weapon.h" |
21 |
21 |
22 #include "../util/inihelper.h" |
22 #include "../util/inihelper.h" |
23 #include "../util/logging.h" |
23 #include "../util/logging.h" |
24 #include "../util/util.h" |
24 #include "../util/util.h" |
25 #include "../util/refcounter.h" |
|
26 #include "../util/list.h" |
25 #include "../util/list.h" |
27 |
26 |
28 #include <stdlib.h> |
27 #include <stdlib.h> |
29 #include <ctype.h> |
28 #include <ctype.h> |
30 #include <string.h> |
29 #include <string.h> |
31 |
|
32 static void flib_weaponset_destroy(flib_weaponset *cfg) { |
|
33 if(cfg) { |
|
34 free(cfg->name); |
|
35 free(cfg); |
|
36 } |
|
37 } |
|
38 |
30 |
39 static void setField(char field[WEAPONS_COUNT+1], const char *line, int lineLen, bool no9) { |
31 static void setField(char field[WEAPONS_COUNT+1], const char *line, int lineLen, bool no9) { |
40 if(lineLen>WEAPONS_COUNT) { |
32 if(lineLen>WEAPONS_COUNT) { |
41 lineLen = WEAPONS_COUNT; |
33 lineLen = WEAPONS_COUNT; |
42 } |
34 } |
58 } |
50 } |
59 |
51 |
60 flib_weaponset *flib_weaponset_create(const char *name) { |
52 flib_weaponset *flib_weaponset_create(const char *name) { |
61 flib_weaponset *result = NULL; |
53 flib_weaponset *result = NULL; |
62 if(!log_badargs_if(name==NULL)) { |
54 if(!log_badargs_if(name==NULL)) { |
63 flib_weaponset *newSet = flib_weaponset_retain(flib_calloc(1, sizeof(flib_weaponset))); |
55 flib_weaponset *newSet = flib_calloc(1, sizeof(flib_weaponset)); |
64 if(newSet) { |
56 if(newSet) { |
65 newSet->name = flib_strdupnull(name); |
57 newSet->name = flib_strdupnull(name); |
66 if(newSet->name) { |
58 if(newSet->name) { |
67 setField(newSet->loadout, "", 0, false); |
59 setField(newSet->loadout, "", 0, false); |
68 setField(newSet->crateprob, "", 0, false); |
60 setField(newSet->crateprob, "", 0, false); |
69 setField(newSet->crateammo, "", 0, false); |
61 setField(newSet->crateammo, "", 0, false); |
70 setField(newSet->delay, "", 0, false); |
62 setField(newSet->delay, "", 0, false); |
71 result = flib_weaponset_retain(newSet); |
63 result = newSet; |
72 } |
64 newSet = NULL; |
73 } |
65 } |
74 flib_weaponset_release(newSet); |
66 } |
75 } |
67 flib_weaponset_destroy(newSet); |
76 return result; |
68 } |
77 } |
69 return result; |
78 |
70 } |
79 flib_weaponset *flib_weaponset_retain(flib_weaponset *weaponset) { |
71 |
80 if(weaponset) { |
72 void flib_weaponset_destroy(flib_weaponset *cfg) { |
81 flib_retain(&weaponset->_referenceCount, "flib_weaponset"); |
73 if(cfg) { |
82 } |
74 free(cfg->name); |
83 return weaponset; |
75 free(cfg); |
84 } |
|
85 |
|
86 void flib_weaponset_release(flib_weaponset *weaponset) { |
|
87 if(weaponset && flib_release(&weaponset->_referenceCount, "flib_weaponset")) { |
|
88 flib_weaponset_destroy(weaponset); |
|
89 } |
76 } |
90 } |
77 } |
91 |
78 |
92 flib_weaponset *flib_weaponset_copy(const flib_weaponset *weaponset) { |
79 flib_weaponset *flib_weaponset_copy(const flib_weaponset *weaponset) { |
93 if(!weaponset) { |
80 if(!weaponset) { |
137 char *ammostring = NULL; |
124 char *ammostring = NULL; |
138 if(decodedKeyname && !flib_ini_get_str(ini, &ammostring, keyname)) { |
125 if(decodedKeyname && !flib_ini_get_str(ini, &ammostring, keyname)) { |
139 flib_weaponset *set = flib_weaponset_from_ammostring(decodedKeyname, ammostring); |
126 flib_weaponset *set = flib_weaponset_from_ammostring(decodedKeyname, ammostring); |
140 if(set) { |
127 if(set) { |
141 result = flib_weaponsetlist_insert(list, set, list->weaponsetCount); |
128 result = flib_weaponsetlist_insert(list, set, list->weaponsetCount); |
142 } |
129 if(result) { |
143 flib_weaponset_release(set); |
130 flib_weaponset_destroy(set); |
|
131 } |
|
132 } |
144 } |
133 } |
145 free(ammostring); |
134 free(ammostring); |
146 free(decodedKeyname); |
135 free(decodedKeyname); |
147 free(keyname); |
136 free(keyname); |
148 return result; |
137 return result; |
226 GENERATE_STATIC_LIST_DELETE(deleteWeaponset, flib_weaponset*) |
215 GENERATE_STATIC_LIST_DELETE(deleteWeaponset, flib_weaponset*) |
227 |
216 |
228 int flib_weaponsetlist_insert(flib_weaponsetlist *list, flib_weaponset *set, int pos) { |
217 int flib_weaponsetlist_insert(flib_weaponsetlist *list, flib_weaponset *set, int pos) { |
229 if(!log_badargs_if2(list==NULL, set==NULL) |
218 if(!log_badargs_if2(list==NULL, set==NULL) |
230 && !insertWeaponset(&list->weaponsets, &list->weaponsetCount, set, pos)) { |
219 && !insertWeaponset(&list->weaponsets, &list->weaponsetCount, set, pos)) { |
231 flib_weaponset_retain(set); |
|
232 return 0; |
220 return 0; |
233 } |
221 } |
234 return -1; |
222 return -1; |
235 } |
223 } |
236 |
224 |
237 int flib_weaponsetlist_delete(flib_weaponsetlist *list, int pos) { |
225 int flib_weaponsetlist_delete(flib_weaponsetlist *list, int pos) { |
238 if(!log_badargs_if(list==NULL)) { |
226 if(!log_badargs_if(list==NULL)) { |
239 flib_weaponset *elem = list->weaponsets[pos]; |
227 flib_weaponset *elem = list->weaponsets[pos]; |
240 if(!deleteWeaponset(&list->weaponsets, &list->weaponsetCount, pos)) { |
228 if(!deleteWeaponset(&list->weaponsets, &list->weaponsetCount, pos)) { |
241 flib_weaponset_release(elem); |
229 flib_weaponset_destroy(elem); |
242 return 0; |
230 return 0; |
243 } |
231 } |
244 } |
232 } |
245 return -1; |
233 return -1; |
246 } |
234 } |