diff --git a/src/pilot.c b/src/pilot.c index 2a91022..d7f3e46 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -950,30 +950,17 @@ unsigned int pilot_create(Ship* ship, char* name, int faction, } pilot_init(dyn, ship, name, faction, ai, dir, pos, vel, flags); - if(flags & PILOT_PLAYER) { - /* Player. */ - if(!pilot_stack) { - pilot_stack = MALLOC_L(Pilot*); - pilot_nstack = 1; - pilot_mstack = 1; - } - pilot_stack[0] = dyn; - } else { - /* Add to the stack. */ - pilot_nstack++; /* There is a new pilot. */ - - if(pilot_nstack >= pilot_mstack) { - /* Need to grow. About 20 at a time. */ - pilot_mstack += PILOT_CHUNK; - tp = pilot_stack; - pilot_stack = realloc(pilot_stack, pilot_mstack*sizeof(Pilot*)); - if((pilot_stack != tp) && player) - /* Take into account possible mem move. */ - player = pilot_stack[0]; - } - - pilot_stack[pilot_nstack-1] = dyn; + /* See if memory needs to grow. */ + if(pilot_nstack+1 > pilot_mstack) { /* Needs to grow. */ + pilot_mstack += PILOT_CHUNK; + tp = pilot_stack; + pilot_stack = realloc(pilot_stack, pilot_mstack*sizeof(Pilot*)); } + + /* Set the pilot in the stack. */ + pilot_stack[pilot_nstack] = dyn; + pilot_nstack++; /* There's a new pilot. */ + return dyn->id; } @@ -1049,20 +1036,26 @@ void pilot_destroy(Pilot* p) { /* Free the prisoned pilot! */ void pilots_free(void) { int i; - if(player) pilot_free(player); - for(i = 1; i < pilot_nstack; i++) + for(i = 0; i < pilot_nstack; i++) pilot_free(pilot_stack[i]); free(pilot_stack); pilot_stack = NULL; + player = NULL; pilot_nstack = 0; } /* Clean up the pilots - Leaves the player. */ void pilots_clean(void) { int i; - for(i = 1; i < pilot_nstack; i++) - pilot_free(pilot_stack[i]); - pilot_nstack = 1; + for(i = 0; i < pilot_nstack; i++) + /* We'll set player at priveleged position. */ + if((player != NULL) && (pilot_stack[i] == player)) + pilot_stack[0] = player; + else /* Rest get killed. */ + pilot_free(pilot_stack[i]); + + if(player != NULL) /* Set stack to 1 if pilot exists. */ + pilot_nstack = 1; } void pilots_cleanAll(void) { @@ -1097,10 +1090,11 @@ void pilots_update(double dt) { /* Render all the pilots. */ void pilots_render(void) { int i; - for(i = 1; i < pilot_nstack; i++) - /* Skip the player. */ - if(pilot_stack[i]->render != NULL) + for(i = 0; i < pilot_nstack; i++) { + if(player == pilot_stack[i]) continue; /* Skip the player. */ + if(pilot_stack[i]->render != NULL) /* Render. */ pilot_stack[i]->render(pilot_stack[i]); + } } /* Parse the fleet node. */ diff --git a/src/player.c b/src/player.c index 854a970..6fc7cf5 100644 --- a/src/player.c +++ b/src/player.c @@ -373,7 +373,11 @@ void player_swapShip(char* shipname) { /* Now swap the players. */ player_stack[i] = player; - pilot_stack[0] = player = ship; + for(j = 0; j < pilot_nstack; j++) /* Find pilot in stack to swap. */ + if(pilot_stack[j] == player) { + pilot_stack[j] = player = ship; + break; + } gl_bindCamera(&player->solid->pos); /* Let's not forget the camera. */ return; }