project_files/frontlib/model/weapon.c
changeset 7275 15f722e0b96f
parent 7271 5608ac657362
child 7314 6171f0bad318
equal deleted inserted replaced
7273:8eed495fd8da 7275:15f722e0b96f
    70 	if(weaponset && flib_release(&weaponset->_referenceCount, "flib_weaponset")) {
    70 	if(weaponset && flib_release(&weaponset->_referenceCount, "flib_weaponset")) {
    71 		flib_weaponset_destroy(weaponset);
    71 		flib_weaponset_destroy(weaponset);
    72 	}
    72 	}
    73 }
    73 }
    74 
    74 
       
    75 flib_weaponset *flib_weaponset_copy(const flib_weaponset *weaponset) {
       
    76 	if(!weaponset) {
       
    77 		return NULL;
       
    78 	}
       
    79 
       
    80 	flib_weaponset *result = flib_weaponset_create(weaponset->name);
       
    81 	if(result) {
       
    82 		memcpy(result->loadout, weaponset->loadout, WEAPONS_COUNT+1);
       
    83 		memcpy(result->crateprob, weaponset->crateprob, WEAPONS_COUNT+1);
       
    84 		memcpy(result->delay, weaponset->delay, WEAPONS_COUNT+1);
       
    85 		memcpy(result->crateammo, weaponset->crateammo, WEAPONS_COUNT+1);
       
    86 	}
       
    87 
       
    88 	return result;
       
    89 }
       
    90 
    75 static void flib_weaponsetlist_destroy(flib_weaponsetlist *list) {
    91 static void flib_weaponsetlist_destroy(flib_weaponsetlist *list) {
    76 	if(list) {
    92 	if(list) {
    77 		for(int i=0; i<list->weaponsetCount; i++) {
    93 		for(int i=0; i<list->weaponsetCount; i++) {
    78 			flib_weaponset_release(list->weaponsets[i]);
    94 			flib_weaponset_release(list->weaponsets[i]);
    79 		}
    95 		}
       
    96 		free(list->weaponsets);
    80 		free(list);
    97 		free(list);
    81 	}
    98 	}
    82 }
    99 }
    83 
   100 
    84 flib_weaponset *flib_weaponset_from_ammostring(const char *name, const char *ammostring) {
   101 flib_weaponset *flib_weaponset_from_ammostring(const char *name, const char *ammostring) {
   194 
   211 
   195 flib_weaponsetlist *flib_weaponsetlist_create() {
   212 flib_weaponsetlist *flib_weaponsetlist_create() {
   196 	return flib_weaponsetlist_retain(flib_calloc(1, sizeof(flib_weaponsetlist)));
   213 	return flib_weaponsetlist_retain(flib_calloc(1, sizeof(flib_weaponsetlist)));
   197 }
   214 }
   198 
   215 
       
   216 GENERATE_STATIC_LIST_INSERT(insertWeaponset, flib_weaponset*)
       
   217 GENERATE_STATIC_LIST_DELETE(deleteWeaponset, flib_weaponset*)
       
   218 
   199 int flib_weaponsetlist_insert(flib_weaponsetlist *list, flib_weaponset *set, int pos) {
   219 int flib_weaponsetlist_insert(flib_weaponsetlist *list, flib_weaponset *set, int pos) {
   200 	flib_weaponset **changedList = flib_list_insert(list->weaponsets, &list->weaponsetCount, sizeof(*list->weaponsets), &set, pos);
   220 	if(!list) {
   201 	if(changedList) {
   221 		flib_log_e("Invalid parameter in flib_weaponsetlist_insert");
   202 		list->weaponsets = changedList;
   222 	} else if(!insertWeaponset(&list->weaponsets, &list->weaponsetCount, set, pos)) {
   203 		flib_weaponset_retain(set);
   223 		flib_weaponset_retain(set);
   204 		return 0;
   224 		return 0;
   205 	} else {
   225 	}
   206 		return -1;
   226 	return -1;
   207 	}
       
   208 }
   227 }
   209 
   228 
   210 int flib_weaponsetlist_delete(flib_weaponsetlist *list, int pos) {
   229 int flib_weaponsetlist_delete(flib_weaponsetlist *list, int pos) {
   211 	int result = -1;
   230 	if(!list) {
   212 	if(!list || pos<0 || pos>=list->weaponsetCount) {
       
   213 		flib_log_e("Invalid parameter in flib_weaponsetlist_delete");
   231 		flib_log_e("Invalid parameter in flib_weaponsetlist_delete");
   214 	} else {
   232 	} else {
   215 		flib_weaponset *elem = list->weaponsets[pos];
   233 		flib_weaponset *elem = list->weaponsets[pos];
   216 		flib_weaponset **changedList = flib_list_delete(list->weaponsets, &list->weaponsetCount, sizeof(*list->weaponsets), pos);
   234 		if(!deleteWeaponset(&list->weaponsets, &list->weaponsetCount, pos)) {
   217 		if(changedList || list->weaponsetCount==0) {
       
   218 			list->weaponsets = changedList;
       
   219 			flib_weaponset_release(elem);
   235 			flib_weaponset_release(elem);
   220 			result = 0;
   236 			return 0;
   221 		}
   237 		}
   222 	}
   238 	}
   223 	return result;
   239 	return -1;
   224 }
   240 }
   225 
   241 
   226 flib_weaponsetlist *flib_weaponsetlist_retain(flib_weaponsetlist *list) {
   242 flib_weaponsetlist *flib_weaponsetlist_retain(flib_weaponsetlist *list) {
   227 	if(list) {
   243 	if(list) {
   228 		flib_retain(&list->_referenceCount, "flib_weaponsetlist");
   244 		flib_retain(&list->_referenceCount, "flib_weaponsetlist");