[Change] A ton of code cleanup and optimizations.
This commit is contained in:
parent
6769b024b3
commit
f74603ab35
@ -479,6 +479,7 @@ static void faction_parseSocial(xmlNodePtr parent) {
|
||||
char* buf;
|
||||
Faction* base;
|
||||
int mod;
|
||||
int mem;
|
||||
|
||||
buf = xml_nodeProp(parent, "name");
|
||||
base = &faction_stack[faction_get(buf)];
|
||||
@ -488,27 +489,41 @@ static void faction_parseSocial(xmlNodePtr parent) {
|
||||
do {
|
||||
if(xml_isNode(node, "allies")) {
|
||||
cur = node->xmlChildrenNode;
|
||||
|
||||
mem = 0;
|
||||
do {
|
||||
if(xml_isNode(cur, "ally")) {
|
||||
mod = faction_get(xml_get(cur));
|
||||
base->nallies++;
|
||||
base->allies = realloc(base->allies, sizeof(int)*base->nallies);
|
||||
if(base->nallies > mem) {
|
||||
mem += CHUNK_SIZE;
|
||||
base->allies = realloc(base->allies, sizeof(int)*mem);
|
||||
}
|
||||
base->allies[base->nallies-1] = mod;
|
||||
}
|
||||
} while(xml_nextNode(cur));
|
||||
if(base->nallies > 0)
|
||||
base->allies = realloc(base->allies, sizeof(int)*base->nallies);
|
||||
}
|
||||
|
||||
/* Grab the enemies. */
|
||||
if(xml_isNode(node, "enemies")) {
|
||||
cur = node->xmlChildrenNode;
|
||||
|
||||
mem = 0;
|
||||
do {
|
||||
if(xml_isNode(cur, "enemy")) {
|
||||
mod = faction_get(xml_get(cur));
|
||||
base->nenemies++;
|
||||
base->enemies = realloc(base->enemies, sizeof(int)*base->nenemies);
|
||||
if(base->nenemies > mem) {
|
||||
mem += CHUNK_SIZE;
|
||||
base->enemies = realloc(base->enemies, sizeof(int)*mem);
|
||||
}
|
||||
base->enemies[base->nenemies-1] = mod;
|
||||
}
|
||||
} while(xml_nextNode(cur));
|
||||
if(base->nenemies > 0)
|
||||
base->enemies = realloc(base->enemies, sizeof(int)*base->nenemies);
|
||||
}
|
||||
} while(xml_nextNode(node));
|
||||
}
|
||||
|
15
src/music.c
15
src/music.c
@ -21,6 +21,8 @@
|
||||
|
||||
#define MUSIC_LUA_PATH "../snd/music.lua" /**< Lua music control file. */
|
||||
|
||||
#define CHUNK_SIZE 32 /**< Size of chunk to allocate. */
|
||||
|
||||
int music_disabled = 0; /**< Whether or not music is disabled. */
|
||||
double music_defVolume = 0.8l; /**< Music default volume. */
|
||||
|
||||
@ -166,6 +168,7 @@ static int music_find(void) {
|
||||
uint32_t nfiles, i;
|
||||
char tmp[64];
|
||||
int len;
|
||||
int mem;
|
||||
|
||||
if(music_disabled) return 0;
|
||||
|
||||
@ -173,13 +176,18 @@ static int music_find(void) {
|
||||
files = pack_listfiles(data, &nfiles);
|
||||
|
||||
/* Load the profiles. */
|
||||
for(i = 0; i < nfiles; i++)
|
||||
mem = 0;
|
||||
for(i = 0; i < nfiles; i++) {
|
||||
if((strncmp(files[i], MUSIC_PREFIX, strlen(MUSIC_PREFIX))==0) &&
|
||||
(strncmp(files[i] + strlen(files[i]) - strlen(MUSIC_SUFFIX),
|
||||
MUSIC_SUFFIX, strlen(MUSIC_SUFFIX))==0)) {
|
||||
|
||||
/* Grow the selection size. */
|
||||
music_selection = realloc(music_selection,++nmusic_selection*sizeof(char*));
|
||||
nmusic_selection++;
|
||||
if(nmusic_selection > mem) {
|
||||
mem += CHUNK_SIZE;
|
||||
music_selection = realloc(music_selection, sizeof(char*)*mem);
|
||||
}
|
||||
|
||||
/* Remove the prefix and suffix. */
|
||||
len = strlen(files[i]) - strlen(MUSIC_SUFFIX MUSIC_PREFIX);
|
||||
@ -188,6 +196,9 @@ static int music_find(void) {
|
||||
|
||||
music_selection[nmusic_selection-1] = strdup(tmp);
|
||||
}
|
||||
}
|
||||
music_selection = realloc(music_selection, sizeof(char*)*nmusic_selection);
|
||||
|
||||
/* Free the char* allocated by pack. */
|
||||
for(i = 0; i < nfiles; i++)
|
||||
free(files[i]);
|
||||
|
@ -1043,6 +1043,8 @@ void outfit_free(void) {
|
||||
free(o->description);
|
||||
if(o->gfx_store)
|
||||
gl_freeTexture(o->gfx_store);
|
||||
if(o->license)
|
||||
free(o->license);
|
||||
free(o->name);
|
||||
}
|
||||
free(outfit_stack);
|
||||
|
54
src/pilot.c
54
src/pilot.c
@ -28,6 +28,8 @@
|
||||
|
||||
#define PILOT_CHUNK 32 /**< Chunks to increment pilot_stack by. */
|
||||
|
||||
#define CHUNK_SIZE 32 /**< Size to allocate memory by. */
|
||||
|
||||
/* ID generators. */
|
||||
static unsigned int pilot_id = PLAYER_ID; /**< Stack of pilod ids to assure uniqueness. */
|
||||
static unsigned int mission_cargo_id = 0; /**< ID generator for special mission cargo.
|
||||
@ -68,7 +70,7 @@ static void pilot_hyperspace(Pilot* pilot);
|
||||
void pilot_render(Pilot* pilot);
|
||||
static void pilot_calcCargo(Pilot* pilot);
|
||||
void pilot_free(Pilot* p);
|
||||
static Fleet* fleet_parse(const xmlNodePtr parent);
|
||||
static int fleet_parse(Fleet* tmp, const xmlNodePtr parent);
|
||||
static void pilot_dead(Pilot* p);
|
||||
|
||||
/**
|
||||
@ -1696,13 +1698,15 @@ void pilots_render(void) {
|
||||
}
|
||||
|
||||
/* Parse the fleet node. */
|
||||
static Fleet* fleet_parse(const xmlNodePtr parent) {
|
||||
static int fleet_parse(Fleet* tmp, const xmlNodePtr parent) {
|
||||
xmlNodePtr cur, node;
|
||||
FleetPilot* pilot;
|
||||
char* c;
|
||||
int mem;
|
||||
node = parent->xmlChildrenNode;
|
||||
|
||||
Fleet* tmp = CALLOC_L(Fleet);
|
||||
/* Sane defaults and clean up. */
|
||||
memset(tmp, 0, sizeof(Fleet));
|
||||
tmp->faction = -1;
|
||||
|
||||
tmp->name = (char*)xmlGetProp(parent, (xmlChar*)"name"); /* Already mallocs. */
|
||||
@ -1716,10 +1720,19 @@ static Fleet* fleet_parse(const xmlNodePtr parent) {
|
||||
tmp->ai = xml_getStrd(node);
|
||||
else if(xml_isNode(node, "pilots")) {
|
||||
cur = node->children;
|
||||
mem = 0;
|
||||
do {
|
||||
if(xml_isNode(cur, "pilot")) {
|
||||
tmp->npilots++; /* Pilot count. */
|
||||
pilot = MALLOC_L(FleetPilot);
|
||||
/* See if we must grow. */
|
||||
tmp->npilots++;
|
||||
if(tmp->npilots > mem) {
|
||||
mem += CHUNK_SIZE;
|
||||
tmp->pilots = realloc(tmp->pilots, mem * sizeof(FleetPilot));
|
||||
}
|
||||
pilot = &tmp->pilots[tmp->npilots-1];
|
||||
|
||||
/* Clear memory. */
|
||||
memset(pilot, 0, sizeof(FleetPilot));
|
||||
|
||||
/* Check for name override. */
|
||||
xmlr_attr(cur, "name", c);
|
||||
@ -1739,13 +1752,14 @@ static Fleet* fleet_parse(const xmlNodePtr parent) {
|
||||
if(pilot->chance == 0)
|
||||
WARN("Pilot %s in Fleet %s has 0%% chance of appearing",
|
||||
pilot->name, tmp->name);
|
||||
if(c != NULL) free(c); /* Free the external malloc. */
|
||||
|
||||
tmp->pilots = realloc(tmp->pilots, sizeof(FleetPilot)*tmp->npilots);
|
||||
memcpy(tmp->pilots+(tmp->npilots-1), pilot, sizeof(FleetPilot));
|
||||
free(pilot);
|
||||
if(c != NULL)
|
||||
free(c); /* Free the external malloc. */
|
||||
}
|
||||
} while(xml_nextNode(cur));
|
||||
|
||||
/* Resize to minimum. */
|
||||
tmp->pilots = realloc(tmp->pilots, sizeof(FleetPilot)*tmp->npilots);
|
||||
}
|
||||
} while(xml_nextNode(node));
|
||||
#define MELEMENT(o,s) if(o) WARN("Fleet '%s' missing '"s"' element", tmp->name)
|
||||
@ -1754,19 +1768,18 @@ static Fleet* fleet_parse(const xmlNodePtr parent) {
|
||||
MELEMENT(tmp->pilots==NULL, "pilots");
|
||||
#undef MELEMENT
|
||||
|
||||
return tmp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Load the fleets. */
|
||||
int fleet_load(void) {
|
||||
int mem;
|
||||
uint32_t bufsize;
|
||||
char* buf = pack_readfile(DATA, FLEET_DATA, &bufsize);
|
||||
|
||||
xmlNodePtr node;
|
||||
xmlDocPtr doc = xmlParseMemory(buf, bufsize);
|
||||
|
||||
Fleet* tmp = NULL;
|
||||
|
||||
node = doc->xmlChildrenNode; /* Ships node. */
|
||||
if(strcmp((char*)node->name, XML_ID)) {
|
||||
ERR("Malformed "FLEET_DATA" file: missing root element '"XML_ID"'");
|
||||
@ -1778,15 +1791,24 @@ int fleet_load(void) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
mem = 0;
|
||||
do {
|
||||
if(xml_isNode(node, XML_FLEET)) {
|
||||
tmp = fleet_parse(node);
|
||||
fleet_stack = realloc(fleet_stack, sizeof(Fleet)*(++nfleets));
|
||||
memcpy(fleet_stack+nfleets-1, tmp, sizeof(Fleet));
|
||||
free(tmp);
|
||||
/* See if memory must grow. */
|
||||
nfleets++;
|
||||
if(nfleets > mem) {
|
||||
mem += CHUNK_SIZE;
|
||||
fleet_stack = realloc(fleet_stack, sizeof(Fleet)*mem);
|
||||
}
|
||||
|
||||
/* Load the fleet. */
|
||||
fleet_parse(&fleet_stack[nfleets-1], node);
|
||||
}
|
||||
} while(xml_nextNode(node));
|
||||
|
||||
/* Shrink to minimum. */
|
||||
fleet_stack = realloc(fleet_stack, sizeof(Fleet)*nfleets);
|
||||
|
||||
xmlFreeDoc(doc);
|
||||
free(buf);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user