[Change] pilot and star stack won't lower memory allocation now.

This commit is contained in:
Allanis 2013-02-19 22:17:31 +00:00
parent f5834d33f9
commit bfc3582d1e
5 changed files with 56 additions and 19 deletions

View File

@ -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;
}

View File

@ -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.

View File

@ -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;

View File

@ -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);

View File

@ -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,