From d53caec8036561c3bc5b70b03e1731e3ecae2bd0 Mon Sep 17 00:00:00 2001 From: Allanis Date: Wed, 26 Jun 2013 21:21:46 +0100 Subject: [PATCH] [Change] Renamed ships to ship_nstack to follow current convention. --- src/ship.c | 64 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/src/ship.c b/src/ship.c index 80107fe..337bc5b 100644 --- a/src/ship.c +++ b/src/ship.c @@ -1,4 +1,5 @@ #include +#include #include "lephisto.h" #include "log.h" @@ -22,7 +23,7 @@ #define BUTTON_HEIGHT 30 static Ship* ship_stack = NULL; -static int ships = 0; +static int ship_nstack = 0; static Ship* ship_parse(xmlNodePtr parent); static void ship_view_close(char* btn); @@ -31,10 +32,11 @@ static void ship_view_close(char* btn); Ship* ship_get(const char* name) { Ship* tmp = ship_stack; int i; - for(i = 0; i < ships; i++) + + for(i = 0; i < ship_nstack; i++) if(strcmp((tmp+i)->name, name)==0) break; - if(i == ships) /* Ship doesn't exist, game will probably crash now. */ + if(i == ship_nstack) /* Ship doesn't exist, game will probably crash now. */ WARN("Ship %s does not exist", name); return tmp+i; @@ -52,23 +54,53 @@ static char* ship_classes[] = { /* Return all the ships in text form. */ char** ship_getTech(int* n, const int* tech, const int techmax) { - int i, j; + int i, j, k, num, price; char** shipnames; + Ship** ships; - shipnames = malloc(sizeof(Ship*) * ships); - - *n = 0; - for(i = 0; i < ships; i++) + ships = malloc(sizeof(Ship*) * ship_nstack); + num = 0; + for(i = 0; i < ship_nstack; i++) { if(ship_stack[i].tech <= tech[0]) { - shipnames[*n] = strdup(ship_stack[i].name); - (*n)++; + ships[num] = &ship_stack[i]; + num++; } else { for(j = 0; j < techmax; j++) if(tech[j] == ship_stack[i].tech) { - shipnames[*n] = strdup(ship_stack[i].name); - (*n)++; + ships[num] = &ship_stack[i]; + num++; } } + } + + /* Now sort by price. */ + *n = 0; + price = -1; + shipnames = malloc(sizeof(char*) * num); + /* Until we fill the new stack. */ + for(i = 0; i < num; i++) { + /* Check for cheapest. */ + for(j = 0; j < num; j++) { + /* Is cheapest? */ + if((price == -1) || (ships[price]->price > ships[j]->price)) { + /* Check if already in stack. */ + for(k = 0; k < (*n); k++) + if(strcmp(shipnames[k], ships[j]->name)==0) + break; + + /* Not in stack and therefore is cheapest. */ + if(k == (*n)) + price = j; + } + } + /* Add the current cheapest to stack. */ + shipnames[i] = strdup(ships[price]->name); + (*n)++; + price = -1; + } + + /* Cleanup */ + free(ships); return shipnames; } @@ -222,8 +254,8 @@ int ships_load(void) { do { if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_SHIP)==0) { tmp = ship_parse(node); - ship_stack = realloc(ship_stack, sizeof(Ship)*(++ships)); - memcpy(ship_stack+ships-1, tmp, sizeof(Ship)); + ship_stack = realloc(ship_stack, sizeof(Ship)*(++ship_nstack)); + memcpy(ship_stack+ship_nstack-1, tmp, sizeof(Ship)); free(tmp); } } while((node = node->next)); @@ -232,7 +264,7 @@ int ships_load(void) { free(buf); xmlCleanupParser(); - DEBUG("Loaded %d ship%s", ships, (ships==1) ? "" : "s"); + DEBUG("Loaded %d ship%s", ship_nstack, (ship_nstack==1) ? "" : "s"); return 0; } @@ -240,7 +272,7 @@ int ships_load(void) { void ships_free(void) { ShipOutfit* so, *sot; int i; - for(i = 0; i < ships; i++) { + for(i = 0; i < ship_nstack; i++) { /* Free stored strings. */ if((ship_stack+i)->name) free(ship_stack[i].name); if((ship_stack+i)->description) free(ship_stack[i].description);