diff --git a/src/pilot.c b/src/pilot.c index 41bb6fc..8ecce72 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -711,7 +711,10 @@ void pilot_init(Pilot* pilot, Ship* ship, char* name, Faction* faction, unsigned int pilot_create(Ship* ship, char* name, Faction* faction, AI_Profile* ai, const double dir, const Vec2* pos, const Vec2* vel, const int flags) { - Pilot* dyn = MALLOC_L(Pilot); + + Pilot** tp, *dyn; + + dyn = MALLOC_L(Pilot); if(dyn == NULL) { WARN("Unable to allocate memory."); return 0; @@ -729,9 +732,16 @@ unsigned int pilot_create(Ship* ship, char* name, Faction* faction, } else { // Add to the stack. pilots++; // There is a new pilot. - - if(pilots >= mpilots) // Need to grow. - pilot_stack = realloc(pilot_stack, ++mpilots*sizeof(Pilot*)); + + if(pilots >= mpilots) { + // Need to grow. About 20 at a time. + mpilots += 20; + tp = pilot_stack; + pilot_stack = realloc(pilot_stack, mpilots*sizeof(Pilot*)); + if((pilot_stack != tp) && player) + // Take into account possible mem move. + player = pilot_stack[0]; + } pilot_stack[pilots-1] = dyn; }