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