diff --git a/src/outfit.c b/src/outfit.c index c696989..491ae1f 100644 --- a/src/outfit.c +++ b/src/outfit.c @@ -22,7 +22,7 @@ extern SDL_mutex* sound_lock; /* Sound.c */ /* The Stack. */ static Outfit* outfit_stack = NULL; -static int outfits = 0; +static int outfit_nstack = 0; /* Misc. */ static DamageType outfit_strToDamageType(char* buf); @@ -40,7 +40,7 @@ static void outfit_parseSMap(Outfit* tmp, const xmlNodePtr parent); /* Return an outfit. */ Outfit* outfit_get(const char* name) { int i; - for(i = 0; i < outfits; i++) + for(i = 0; i < outfit_nstack; i++) if(strcmp(name, outfit_stack[i].name)==0) return &outfit_stack[i]; return NULL; @@ -48,23 +48,65 @@ Outfit* outfit_get(const char* name) { /* Return all the outfits. */ char** outfit_getTech(int* n, const int* tech, const int techmax) { - int i, j; - char** outfitnames = malloc(sizeof(Outfit*) * outfits); + int i, j, k, num, price; + Outfit** outfits; + char** outfitnames; + OutfitType type; - *n = 0; - for(i = 0; i < outfits; i++) - if(outfit_stack[i].tech <= tech[0]) { - outfitnames[*n] = strdup(outfit_stack[i].name); - (*n)++; + outfits = malloc(sizeof(Outfit*)*outfit_nstack); + + /* Get the available techs. */ + num = 0; + 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 { - for(j = 0; j < techmax; j++) + for(j = 0; j < techmax; j++) /* Check vs special techs. */ if(tech[j] == outfit_stack[i].tech) { - outfitnames[*n] = strdup(outfit_stack[i].name); - (*n)++; + outfits[num] = &outfit_stack[i]; + 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; } @@ -544,8 +586,8 @@ int outfit_load(void) { do { if(xml_isNode(node, XML_OUTFIT_TAG)) { tmp = outfit_parse(node); - outfit_stack = realloc(outfit_stack, sizeof(Outfit)*(++outfits)); - memcpy(outfit_stack+outfits-1, tmp, sizeof(Outfit)); + outfit_stack = realloc(outfit_stack, sizeof(Outfit)*(++outfit_nstack)); + memcpy(outfit_stack+outfit_nstack-1, tmp, sizeof(Outfit)); free(tmp); } } while((node = node->next)); @@ -554,7 +596,7 @@ int outfit_load(void) { free(buf); xmlCleanupParser(); - DEBUG("Loaded %d outfit%s", outfits, (outfits==1) ? "" : "s"); + DEBUG("Loaded %d outfit%s", outfit_nstack, (outfit_nstack==1) ? "" : "s"); return 0; } @@ -562,7 +604,7 @@ int outfit_load(void) { /* Frees the outfit stack. */ void outfit_free(void) { int i; - for(i = 0; i < outfits; i++) { + for(i = 0; i < outfit_nstack; i++) { /* Free graphics. */ if(outfit_gfx(&outfit_stack[i])) gl_freeTexture(outfit_gfx(&outfit_stack[i])); diff --git a/src/outfit.h b/src/outfit.h index 33952cf..f4888fe 100644 --- a/src/outfit.h +++ b/src/outfit.h @@ -25,7 +25,8 @@ typedef enum OutfitType_ { OUTFIT_TYPE_TURRET_BEAM, OUTFIT_TYPE_MODIFICATION, OUTFIT_TYPE_AFTERBURNER, - OUTFIT_TYPE_MAP + OUTFIT_TYPE_MAP, + OUTFIT_TYPE_SENTINEL /* Indicates last type. */ } OutfitType; typedef enum DamageType_ {