[Change] Outfit_getTech now sorts by type and price.

This commit is contained in:
Allanis 2013-07-02 13:11:25 +01:00
parent 9dc797a8a3
commit 8e6e690388
2 changed files with 61 additions and 18 deletions

View File

@ -22,7 +22,7 @@ extern SDL_mutex* sound_lock; /* Sound.c */
/* The Stack. */ /* The Stack. */
static Outfit* outfit_stack = NULL; static Outfit* outfit_stack = NULL;
static int outfits = 0; static int outfit_nstack = 0;
/* Misc. */ /* Misc. */
static DamageType outfit_strToDamageType(char* buf); static DamageType outfit_strToDamageType(char* buf);
@ -40,7 +40,7 @@ static void outfit_parseSMap(Outfit* tmp, const xmlNodePtr parent);
/* Return an outfit. */ /* Return an outfit. */
Outfit* outfit_get(const char* name) { Outfit* outfit_get(const char* name) {
int i; int i;
for(i = 0; i < outfits; i++) for(i = 0; i < outfit_nstack; i++)
if(strcmp(name, outfit_stack[i].name)==0) if(strcmp(name, outfit_stack[i].name)==0)
return &outfit_stack[i]; return &outfit_stack[i];
return NULL; return NULL;
@ -48,23 +48,65 @@ Outfit* outfit_get(const char* name) {
/* Return all the outfits. */ /* Return all the outfits. */
char** outfit_getTech(int* n, const int* tech, const int techmax) { char** outfit_getTech(int* n, const int* tech, const int techmax) {
int i, j; int i, j, k, num, price;
char** outfitnames = malloc(sizeof(Outfit*) * outfits); Outfit** outfits;
char** outfitnames;
OutfitType type;
*n = 0; outfits = malloc(sizeof(Outfit*)*outfit_nstack);
for(i = 0; i < outfits; i++)
if(outfit_stack[i].tech <= tech[0]) { /* Get the available techs. */
outfitnames[*n] = strdup(outfit_stack[i].name); num = 0;
(*n)++; for(i = 0; i < outfit_nstack; i++)
if(outfit_stack[i].tech <= tech[0]) { /* Check vs base tech. */
outfits[num] = &outfit_stack[i];
num++;
} else { } else {
for(j = 0; j < techmax; j++) for(j = 0; j < techmax; j++) /* Check vs special techs. */
if(tech[j] == outfit_stack[i].tech) { if(tech[j] == outfit_stack[i].tech) {
outfitnames[*n] = strdup(outfit_stack[i].name); outfits[num] = &outfit_stack[i];
(*n)++; num++;
} }
} }
/* Actual size is bigger, but it'll just get freed ;). */ /* Now sort by type and price. */
*n = 0;
price = -1;
type = OUTFIT_TYPE_NULL+1; /* First type. */
outfitnames = malloc(sizeof(char*)*num);
/* Sort by type */
while(type < OUTFIT_TYPE_SENTINEL) {
/* Check for cheapest. */
for(j = 0; j < num; j++) {
/* Must be of the current type. */
if(outfits[j]->type != type) continue;
/* Is this the cheapest? */
if((price == -1) || (outfits[price]->price > outfits[j]->price)) {
/* Check if already in stack. */
for(k = 0; k < (*n); k++)
if(strcmp(outfitnames[k], outfits[j]->name)==0)
break;
/* Not in stack and therefore is cheapest. */
if(k == (*n))
price = j;
}
}
if(price == -1)
type++;
else {
/* Add current cheapest to stack. */
outfitnames[*n] = strdup(outfits[price]->name);
(*n)++;
price = -1;
}
}
/* Cleanup. */
free(outfits);
return outfitnames; return outfitnames;
} }
@ -544,8 +586,8 @@ int outfit_load(void) {
do { do {
if(xml_isNode(node, XML_OUTFIT_TAG)) { if(xml_isNode(node, XML_OUTFIT_TAG)) {
tmp = outfit_parse(node); tmp = outfit_parse(node);
outfit_stack = realloc(outfit_stack, sizeof(Outfit)*(++outfits)); outfit_stack = realloc(outfit_stack, sizeof(Outfit)*(++outfit_nstack));
memcpy(outfit_stack+outfits-1, tmp, sizeof(Outfit)); memcpy(outfit_stack+outfit_nstack-1, tmp, sizeof(Outfit));
free(tmp); free(tmp);
} }
} while((node = node->next)); } while((node = node->next));
@ -554,7 +596,7 @@ int outfit_load(void) {
free(buf); free(buf);
xmlCleanupParser(); xmlCleanupParser();
DEBUG("Loaded %d outfit%s", outfits, (outfits==1) ? "" : "s"); DEBUG("Loaded %d outfit%s", outfit_nstack, (outfit_nstack==1) ? "" : "s");
return 0; return 0;
} }
@ -562,7 +604,7 @@ int outfit_load(void) {
/* Frees the outfit stack. */ /* Frees the outfit stack. */
void outfit_free(void) { void outfit_free(void) {
int i; int i;
for(i = 0; i < outfits; i++) { for(i = 0; i < outfit_nstack; i++) {
/* Free graphics. */ /* Free graphics. */
if(outfit_gfx(&outfit_stack[i])) if(outfit_gfx(&outfit_stack[i]))
gl_freeTexture(outfit_gfx(&outfit_stack[i])); gl_freeTexture(outfit_gfx(&outfit_stack[i]));

View File

@ -25,7 +25,8 @@ typedef enum OutfitType_ {
OUTFIT_TYPE_TURRET_BEAM, OUTFIT_TYPE_TURRET_BEAM,
OUTFIT_TYPE_MODIFICATION, OUTFIT_TYPE_MODIFICATION,
OUTFIT_TYPE_AFTERBURNER, OUTFIT_TYPE_AFTERBURNER,
OUTFIT_TYPE_MAP OUTFIT_TYPE_MAP,
OUTFIT_TYPE_SENTINEL /* Indicates last type. */
} OutfitType; } OutfitType;
typedef enum DamageType_ { typedef enum DamageType_ {