[Change] A ton of code cleanup.

This commit is contained in:
Allanis 2014-05-22 22:07:22 +01:00
parent 08a3fe6c9c
commit 3a13801a14

View File

@ -1023,7 +1023,7 @@ int pilot_addOutfit(Pilot* pilot, Outfit* outfit, int quantity) {
/* Does outfit already exist? */
for(i = 0; i < pilot->noutfits; i++)
if(strcmp(outfit->name, pilot->outfits[i].outfit->name)==0) {
if(pilot->outfits[i].outfit == outfit) {
po = &pilot->outfits[i];
o = po->quantity;
po->quantity += q;
@ -1268,6 +1268,8 @@ int pilot_cargoFree(Pilot* p) {
/**
* @brief Move cargo from one pilot to another.
*
* At the end, dest has exactly the same cargo as src and leaves src with none.
* @param dest Destination pilot.
* @param src Source pilot.
* @return 0 on success.
@ -1636,6 +1638,10 @@ Pilot* pilot_createEmpty(Ship* ship, char* name,
Pilot* dyn;
dyn = malloc(sizeof(Pilot));
if(dyn == NULL) {
WARN("Unable to allocate memory");
return 0;
}
pilot_init(dyn, ship, name, faction, ai, 0., NULL, NULL, flags | PILOT_EMPTY);
return dyn;
}
@ -1648,10 +1654,13 @@ Pilot* pilot_createEmpty(Ship* ship, char* name,
Pilot* pilot_copy(Pilot* src) {
int i;
Pilot* dest = malloc(sizeof(Pilot));
memcpy(dest, src, sizeof(Pilot));
if(src->name) dest->name = strdup(src->name);
/* Solid. */
/* Copy data over, we'll have to reset all the pointers though. */
memcpy(dest, src, sizeof(Pilot));
if(src->name) dest->name = strdup(src->name);
if(src->title) dest->title = strdup(src->title);
/* Copy Solid. */
dest->solid = malloc(sizeof(Solid));
memcpy(dest->solid, src->solid, sizeof(Solid));
@ -1661,26 +1670,40 @@ Pilot* pilot_copy(Pilot* src) {
memcpy(dest->mounted, src->mounted, sizeof(int)*src->ship->nmounts);
}
/* Hooks get cleared. */
memset(dest->hook_type, 0, sizeof(int)*PILOT_HOOKS);
memset(dest->hook, 0, sizeof(int)*PILOT_HOOKS);
/* Copy has no escorts. */
dest->escorts = NULL;
dest->nescorts = 0;
/* AI is not copied. */
dest->task = NULL;
/* Set pointers and firneds to NULL. */
/* Outfits. */
dest->outfits = NULL;
dest->noutfits = 0;
dest->secondary = NULL;
dest->ammo = NULL;
dest->afterburner = NULL;
/* Commodities. */
dest->commodities = NULL;
dest->ncommodities = 0;
/* Calculate stats. */
pilot_calcStats(dest);
/* Copy outfits. */
dest->outfits = NULL;
dest->noutfits = 0;
dest->secondary = NULL;
dest->ammo = NULL;
dest->afterburner = NULL;
for(i = 0; i < src->noutfits; i++)
pilot_addOutfit(dest, src->outfits[i].outfit,
src->outfits[i].quantity);
/* Copy commodities. */
dest->commodities = NULL;
dest->ncommodities = 0;
for(i = 0; i < src->ncommodities; i++)
pilot_addCargo(dest, src->commodities[i].commodity,
src->commodities[i].quantity);
/* Ai is not copied. */
dest->task = NULL;
return dest;
}
@ -1701,10 +1724,12 @@ void pilot_free(Pilot* p) {
pilot_rmOutfit(p, p->outfits[0].outfit, p->outfits[0].quantity);
/* Remove commodities. */
if(p->commodities)
free(p->commodities);
if(p->commodities != NULL)
pilot_rmCargo(p, p->commodities[0].commodity, p->commodities[0].quantity);
free(p->name);
/* Free name and title. */
if(p->name != NULL) free(p->name);
if(p->title != NULL) free(p->title);
/* Clean up data. */
if(p->ai != NULL)