[Change] pilot and star stack won't lower memory allocation now.
This commit is contained in:
parent
f5834d33f9
commit
bfc3582d1e
22
src/pilot.c
22
src/pilot.c
@ -21,6 +21,7 @@ static unsigned int pilot_id = PLAYER_ID;
|
|||||||
// Stack of pilots - yes, they come in stacks now.
|
// 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
|
Pilot** pilot_stack = NULL; // Not static, it is used in player.c and weapon.c and ai.c
|
||||||
int pilots = 0;
|
int pilots = 0;
|
||||||
|
static int mpilots = 0;
|
||||||
extern Pilot* player;
|
extern Pilot* player;
|
||||||
|
|
||||||
// Stack of fleets.
|
// 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) {
|
static void pilot_shootWeapon(Pilot* p, PilotOutfit* w, const unsigned int t) {
|
||||||
// WElll... Trying to shoot when you have no ammo?? FUUU
|
// WElll... Trying to shoot when you have no ammo?? FUUU
|
||||||
int quantity = (outfit_isAmmo(w->outfit) && p->secondary) ?
|
int quantity = outfit_isAmmo(w->outfit) ? p->secondary->quantity : w->quantity;
|
||||||
p->secondary->quantity : w->quantity;
|
|
||||||
// Check to see if weapon is ready.
|
// Check to see if weapon is ready.
|
||||||
if((SDL_GetTicks() - w->timer) < (w->outfit->delay / quantity)) return;
|
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) {
|
if(!pilot_stack) {
|
||||||
pilot_stack = MALLOC_L(Pilot*);
|
pilot_stack = MALLOC_L(Pilot*);
|
||||||
pilots = 1;
|
pilots = 1;
|
||||||
|
mpilots = 1;
|
||||||
}
|
}
|
||||||
pilot_stack[0] = dyn;
|
pilot_stack[0] = dyn;
|
||||||
} else {
|
} else {
|
||||||
// Add to the stack.
|
// 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;
|
pilot_stack[pilots-1] = dyn;
|
||||||
}
|
}
|
||||||
return dyn->id;
|
return dyn->id;
|
||||||
@ -360,6 +365,16 @@ void pilots_free(void) {
|
|||||||
for(i = 0; i < pilots; i++)
|
for(i = 0; i < pilots; i++)
|
||||||
pilot_free(pilot_stack[i]);
|
pilot_free(pilot_stack[i]);
|
||||||
free(pilot_stack);
|
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.
|
// Update all pilots.
|
||||||
@ -500,6 +515,7 @@ void fleet_free(void) {
|
|||||||
free(fleet_stack[i].pilots);
|
free(fleet_stack[i].pilots);
|
||||||
}
|
}
|
||||||
free(fleet_stack);
|
free(fleet_stack);
|
||||||
|
fleet_stack = NULL;
|
||||||
}
|
}
|
||||||
nfleets = 0;
|
nfleets = 0;
|
||||||
}
|
}
|
||||||
|
@ -108,7 +108,8 @@ unsigned int pilot_create(Ship* ship, char* name, Faction* faction, AI_Profile*
|
|||||||
// Init/Cleanup.
|
// Init/Cleanup.
|
||||||
void pilot_destroy(Pilot* p);
|
void pilot_destroy(Pilot* p);
|
||||||
void pilots_free(void);
|
void pilots_free(void);
|
||||||
int fleet_load(void);
|
void pilots_clean(void);
|
||||||
|
int fleet_load(void); // TODO
|
||||||
void fleet_free(void);
|
void fleet_free(void);
|
||||||
|
|
||||||
// Update.
|
// Update.
|
||||||
|
@ -191,6 +191,13 @@ void player_message(const char* fmt, ...) {
|
|||||||
msg_stack[0].t = SDL_GetTicks() + msg_timeout;
|
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) {
|
void player_renderBG(void) {
|
||||||
double x, y;
|
double x, y;
|
||||||
glColour* c;
|
glColour* c;
|
||||||
|
@ -32,6 +32,7 @@ void player_renderBG(void); // Render BG layer.
|
|||||||
|
|
||||||
// Misc.
|
// Misc.
|
||||||
void player_message(const char* fmt, ...);
|
void player_message(const char* fmt, ...);
|
||||||
|
void player_clear(void);
|
||||||
|
|
||||||
// Keybind actions.
|
// Keybind actions.
|
||||||
void player_setRadarRel(int mod);
|
void player_setRadarRel(int mod);
|
||||||
|
18
src/space.c
18
src/space.c
@ -9,6 +9,7 @@
|
|||||||
#include "faction.h"
|
#include "faction.h"
|
||||||
#include "xml.h"
|
#include "xml.h"
|
||||||
#include "pause.h"
|
#include "pause.h"
|
||||||
|
#include "weapon.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
|
||||||
#define XML_PLANET_ID "Planets"
|
#define XML_PLANET_ID "Planets"
|
||||||
@ -49,6 +50,7 @@ typedef struct {
|
|||||||
|
|
||||||
static Star* stars = NULL; // Star array.
|
static Star* stars = NULL; // Star array.
|
||||||
static int nstars = 0; // Total stars.
|
static int nstars = 0; // Total stars.
|
||||||
|
static int mstars = 0;
|
||||||
|
|
||||||
// Intern
|
// Intern
|
||||||
static Planet* planet_get(const char* name);
|
static Planet* planet_get(const char* name);
|
||||||
@ -175,6 +177,14 @@ void space_init(const char* sysname) {
|
|||||||
int i, j;
|
int i, j;
|
||||||
Vec2 v, vn;
|
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.
|
||||||
|
|
||||||
|
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++)
|
for(i = 0; i < nsystems; i++)
|
||||||
if(strcmp(sysname, systems[i].name)==0)
|
if(strcmp(sysname, systems[i].name)==0)
|
||||||
break;
|
break;
|
||||||
@ -185,12 +195,14 @@ void space_init(const char* sysname) {
|
|||||||
|
|
||||||
// Set up stars.
|
// Set up stars.
|
||||||
nstars = (cur_system->stars*gl_screen.w*gl_screen.h+STAR_BUF*STAR_BUF)/(800*640);
|
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.
|
if(mstars < nstars)
|
||||||
|
stars = realloc(stars, sizeof(Star)*nstars); // should realloc not malloc.
|
||||||
for(i = 0; i < nstars; i++) {
|
for(i = 0; i < nstars; i++) {
|
||||||
stars[i].brightness = (double)RNG(50, 200)/256.;
|
stars[i].brightness = (double)RNG(50, 200)/256.;
|
||||||
stars[i].x = (double)RNG(-STAR_BUF, gl_screen.w + STAR_BUF);
|
stars[i].x = (double)RNG(-STAR_BUF, gl_screen.w + STAR_BUF);
|
||||||
stars[i].y = (double)RNG(-STAR_BUF, gl_screen.h + STAR_BUF);
|
stars[i].y = (double)RNG(-STAR_BUF, gl_screen.h + STAR_BUF);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Set up fleets -> pilots.
|
// Set up fleets -> pilots.
|
||||||
vectnull(&vn);
|
vectnull(&vn);
|
||||||
for(i = 0; i < cur_system->nfleets; i++)
|
for(i = 0; i < cur_system->nfleets; i++)
|
||||||
@ -201,8 +213,8 @@ void space_init(const char* sysname) {
|
|||||||
for(j = 0; j < cur_system->fleets[i].fleet->npilots; j++)
|
for(j = 0; j < cur_system->fleets[i].fleet->npilots; j++)
|
||||||
if(RNG(0,100) <= cur_system->fleets[i].fleet->pilots[j].chance) {
|
if(RNG(0,100) <= cur_system->fleets[i].fleet->pilots[j].chance) {
|
||||||
// Other ships in the fleet should start split up.
|
// Other ships in the fleet should start split up.
|
||||||
vect_cadd(&v, RNG(50, 150) * (RNG(0,1) ? 1 : -1),
|
vect_cadd(&v, RNG(75, 150) * (RNG(0,1) ? 1 : -1),
|
||||||
RNG(50, 150) * (RNG(0,1) ? 1 : -1));
|
RNG(75, 150) * (RNG(0,1) ? 1 : -1));
|
||||||
pilot_create(cur_system->fleets[i].fleet->pilots[j].ship,
|
pilot_create(cur_system->fleets[i].fleet->pilots[j].ship,
|
||||||
cur_system->fleets[i].fleet->pilots[j].name,
|
cur_system->fleets[i].fleet->pilots[j].name,
|
||||||
cur_system->fleets[i].fleet->faction,
|
cur_system->fleets[i].fleet->faction,
|
||||||
|
Loading…
Reference in New Issue
Block a user