diff --git a/src/pilot.c b/src/pilot.c index d87c569..e704fc5 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -994,7 +994,7 @@ int pilot_rmOutfit(Pilot* pilot, Outfit* outfit, int quantity) { osec = (pilot->secondary) ? pilot->secondary->outfit->name : NULL; /* Remove the outfit. */ - memmove(pilot->outfits+i, pilot->outfits+i+1, + memmove(&pilot->outfits[i], &pilot->outfits[i+1], sizeof(PilotOutfit)*(pilot->noutfits-i-1)); pilot->noutfits--; pilot->outfits = realloc(pilot->outfits, @@ -1128,6 +1128,41 @@ int pilot_cargoFree(Pilot* p) { return p->cargo_free; } +/** + * + */ +int pilot_moveCargo(Pilot* dest, Pilot* src) { + int i; + + /* Nothing to copy, success! */ + if(src->ncommodities == 0) + return 0; + + /* Check if it fits. */ + if(pilot_cargoUsed(src) > pilot_cargoFree(dest)) { + WARN("Unable to copy cargo over from pilot '%s' to '%s'", src->name, dest->name); + return -1; + } + + /* Alloate new space. */ + i = dest->ncommodities; + dest->ncommodities += src->ncommodities; + dest->commodities = realloc(dest->commodities, + sizeof(PilotCommodity)*dest->ncommodities); + + /* Copy over. */ + memmove(&dest->commodities[i], &src->commodities[0], + sizeof(PilotCommodity) * src->ncommodities); + + /* Clean src. */ + src->ncommodities = 0; + if(src->commodities != NULL) + free(src->commodities); + src->commodities = NULL; + + return 0; +} + /* Try to add quantity of cargo to pilot, return quantity actually added. */ int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity) { int i, q; diff --git a/src/pilot.h b/src/pilot.h index c04a4b6..cd5ec99 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -260,6 +260,7 @@ int pilot_cargoUsed(Pilot* pilot); /* Get amount of cargo onboard. */ int pilot_cargoFree(Pilot* p); /* Cargo space. */ int pilot_addCargo(Pilot* pilot, Commodity* cargo, int quantity); int pilot_rmCargo(Pilot* pilot, Commodity* cargo, int quantity); +int pilot_moveCargo(Pilot* dest, Pilot* src); /* Mission cargo - Not to be confused with normal cargo. */ unsigned int pilot_addMissionCargo(Pilot* pilot, Commodity* cargo, int quantity); int pilot_rmMissionCargo(Pilot* pilot, unsigned int cargo_id); diff --git a/src/player.c b/src/player.c index 45728e3..2d9240a 100644 --- a/src/player.c +++ b/src/player.c @@ -462,6 +462,15 @@ static void player_newShipMake(char* name) { gl_bindCamera(&player->solid->pos); /* Set opengl camera. */ + /* Copy cargo over. */ + if(player_nstack > 0) { /* Not during creation though. */ + pilot_moveCargo(player, player_stack[player_nstack-1]); + + /* Recalculate stats after cargo movement. */ + pilot_calcStats(player); + pilot_calcStats(player_stack[player_nstack-1]); + } + /* Moniez!! */ player->credits = player_credits; player_credits = 0; @@ -486,12 +495,7 @@ void player_swapShip(char* shipname) { ship->credits = player->credits; /* Move cargo over. */ - for(j = 0; j < player->ncommodities; j++) { - pilot_addCargo(ship, player->commodities[j].commodity, - player->commodities[j].quantity); - pilot_rmCargo(player, player->commodities[j].commodity, - player->commodities[j].quantity); - } + pilot_moveCargo(ship, player); /* Extra pass to calculate stats. */ pilot_calcStats(ship);