From bfc3582d1e875da3413a0f83bf7fd9b8ddf952c8 Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 19 Feb 2013 22:17:31 +0000 Subject: [PATCH] [Change] pilot and star stack won't lower memory allocation now. --- src/pilot.c | 22 +++++++++++++++++++--- src/pilot.h | 3 ++- src/player.c | 7 +++++++ src/player.h | 1 + src/space.c | 42 +++++++++++++++++++++++++++--------------- 5 files changed, 56 insertions(+), 19 deletions(-) diff --git a/src/pilot.c b/src/pilot.c index 2a93a43..493494c 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -21,6 +21,7 @@ static unsigned int pilot_id = PLAYER_ID; // Stack of pilots - yes, they come in stacks now. Pilot** pilot_stack = NULL; // Not static, it is used in player.c and weapon.c and ai.c int pilots = 0; +static int mpilots = 0; extern Pilot* player; // Stack of fleets. @@ -126,8 +127,7 @@ void pilot_shoot(Pilot* p, const unsigned int target, const int secondary) { static void pilot_shootWeapon(Pilot* p, PilotOutfit* w, const unsigned int t) { // WElll... Trying to shoot when you have no ammo?? FUUU - int quantity = (outfit_isAmmo(w->outfit) && p->secondary) ? - p->secondary->quantity : w->quantity; + int quantity = outfit_isAmmo(w->outfit) ? p->secondary->quantity : w->quantity; // Check to see if weapon is ready. if((SDL_GetTicks() - w->timer) < (w->outfit->delay / quantity)) return; @@ -322,11 +322,16 @@ unsigned int pilot_create(Ship* ship, char* name, Faction* faction, AI_Profile* if(!pilot_stack) { pilot_stack = MALLOC_L(Pilot*); pilots = 1; + mpilots = 1; } pilot_stack[0] = dyn; } else { // Add to the stack. - pilot_stack = realloc(pilot_stack, ++pilots*sizeof(Pilot*)); + pilots++; // There is a new pilot. + + if(pilots >= mpilots) // Need to grow. + pilot_stack = realloc(pilot_stack, ++mpilots*sizeof(Pilot*)); + pilot_stack[pilots-1] = dyn; } return dyn->id; @@ -360,6 +365,16 @@ void pilots_free(void) { for(i = 0; i < pilots; i++) pilot_free(pilot_stack[i]); free(pilot_stack); + pilot_stack = NULL; + pilots = 0; +} + +// Clean up the pilots - Leaves the player. +void pilots_clean(void) { + int i; + for(i = 1; i < pilots; i++) + pilot_free(pilot_stack[i]); + pilots = 1; } // Update all pilots. @@ -500,6 +515,7 @@ void fleet_free(void) { free(fleet_stack[i].pilots); } free(fleet_stack); + fleet_stack = NULL; } nfleets = 0; } diff --git a/src/pilot.h b/src/pilot.h index b80517c..1d2a50f 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -108,7 +108,8 @@ unsigned int pilot_create(Ship* ship, char* name, Faction* faction, AI_Profile* // Init/Cleanup. void pilot_destroy(Pilot* p); void pilots_free(void); -int fleet_load(void); +void pilots_clean(void); +int fleet_load(void); // TODO void fleet_free(void); // Update. diff --git a/src/player.c b/src/player.c index 360f409..8ccf3c6 100644 --- a/src/player.c +++ b/src/player.c @@ -191,6 +191,13 @@ void player_message(const char* fmt, ...) { msg_stack[0].t = SDL_GetTicks() + msg_timeout; } +// Clear the targets. +void player_clear(void) { + player_target = PLAYER_ID; + planet_target = -1; +} + +// Render the background player stuff, namely planet target void player_renderBG(void) { double x, y; glColour* c; diff --git a/src/player.h b/src/player.h index 80a5d91..5278627 100644 --- a/src/player.h +++ b/src/player.h @@ -32,6 +32,7 @@ void player_renderBG(void); // Render BG layer. // Misc. void player_message(const char* fmt, ...); +void player_clear(void); // Keybind actions. void player_setRadarRel(int mod); diff --git a/src/space.c b/src/space.c index 62c4af3..53ed8b1 100644 --- a/src/space.c +++ b/src/space.c @@ -9,6 +9,7 @@ #include "faction.h" #include "xml.h" #include "pause.h" +#include "weapon.h" #include "player.h" #define XML_PLANET_ID "Planets" @@ -49,6 +50,7 @@ typedef struct { static Star* stars = NULL; // Star array. static int nstars = 0; // Total stars. +static int mstars = 0; // Intern static Planet* planet_get(const char* name); @@ -174,22 +176,32 @@ int space_hyperspace(Pilot* p) { void space_init(const char* sysname) { int i, j; Vec2 v, vn; + + // Cleanup some stuff. + player_clear(); // Clears targets. + pilots_clean(); // Destroy all the current pilots, exept player. + weapon_clear(); // Get rid of all the weapons. - for(i = 0; i < nsystems; i++) - if(strcmp(sysname, systems[i].name)==0) - break; - if(i == nsystems) ERR("System %s not found in stack", sysname); - cur_system = systems+i; + if((sysname == NULL) && (cur_system == NULL)) + ERR("Cannot reinit system if there is no system previously loaded"); + else if(sysname != NULL) { + for(i = 0; i < nsystems; i++) + if(strcmp(sysname, systems[i].name)==0) + break; + if(i == nsystems) ERR("System %s not found in stack", sysname); + cur_system = systems+i; - player_message("Entering System %s on %s", sysname, stardate); + player_message("Entering System %s on %s", sysname, stardate); - // Set up stars. - nstars = (cur_system->stars*gl_screen.w*gl_screen.h+STAR_BUF*STAR_BUF)/(800*640); - stars = realloc(stars, sizeof(Star)*nstars); // Should realloc this, not malloc. - for(i = 0; i < nstars; i++) { - stars[i].brightness = (double)RNG(50, 200)/256.; - stars[i].x = (double)RNG(-STAR_BUF, gl_screen.w + STAR_BUF); - stars[i].y = (double)RNG(-STAR_BUF, gl_screen.h + STAR_BUF); + // Set up stars. + nstars = (cur_system->stars*gl_screen.w*gl_screen.h+STAR_BUF*STAR_BUF)/(800*640); + if(mstars < nstars) + stars = realloc(stars, sizeof(Star)*nstars); // should realloc not malloc. + for(i = 0; i < nstars; i++) { + stars[i].brightness = (double)RNG(50, 200)/256.; + stars[i].x = (double)RNG(-STAR_BUF, gl_screen.w + STAR_BUF); + stars[i].y = (double)RNG(-STAR_BUF, gl_screen.h + STAR_BUF); + } } // Set up fleets -> pilots. vectnull(&vn); @@ -201,8 +213,8 @@ void space_init(const char* sysname) { for(j = 0; j < cur_system->fleets[i].fleet->npilots; j++) if(RNG(0,100) <= cur_system->fleets[i].fleet->pilots[j].chance) { // Other ships in the fleet should start split up. - vect_cadd(&v, RNG(50, 150) * (RNG(0,1) ? 1 : -1), - RNG(50, 150) * (RNG(0,1) ? 1 : -1)); + vect_cadd(&v, RNG(75, 150) * (RNG(0,1) ? 1 : -1), + RNG(75, 150) * (RNG(0,1) ? 1 : -1)); pilot_create(cur_system->fleets[i].fleet->pilots[j].ship, cur_system->fleets[i].fleet->pilots[j].name, cur_system->fleets[i].fleet->faction,