[Change] Renamed ships to ship_nstack to follow current convention.

This commit is contained in:
Allanis 2013-06-26 21:21:46 +01:00
parent a041751ec1
commit d53caec803

View File

@ -1,4 +1,5 @@
#include <string.h> #include <string.h>
#include <limits.h>
#include "lephisto.h" #include "lephisto.h"
#include "log.h" #include "log.h"
@ -22,7 +23,7 @@
#define BUTTON_HEIGHT 30 #define BUTTON_HEIGHT 30
static Ship* ship_stack = NULL; static Ship* ship_stack = NULL;
static int ships = 0; static int ship_nstack = 0;
static Ship* ship_parse(xmlNodePtr parent); static Ship* ship_parse(xmlNodePtr parent);
static void ship_view_close(char* btn); 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* ship_get(const char* name) {
Ship* tmp = ship_stack; Ship* tmp = ship_stack;
int i; int i;
for(i = 0; i < ships; i++)
for(i = 0; i < ship_nstack; i++)
if(strcmp((tmp+i)->name, name)==0) break; 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); WARN("Ship %s does not exist", name);
return tmp+i; return tmp+i;
@ -52,23 +54,53 @@ static char* ship_classes[] = {
/* Return all the ships in text form. */ /* Return all the ships in text form. */
char** ship_getTech(int* n, const int* tech, const int techmax) { char** ship_getTech(int* n, const int* tech, const int techmax) {
int i, j; int i, j, k, num, price;
char** shipnames; char** shipnames;
Ship** ships;
shipnames = malloc(sizeof(Ship*) * ships); ships = malloc(sizeof(Ship*) * ship_nstack);
num = 0;
*n = 0; for(i = 0; i < ship_nstack; i++) {
for(i = 0; i < ships; i++)
if(ship_stack[i].tech <= tech[0]) { if(ship_stack[i].tech <= tech[0]) {
shipnames[*n] = strdup(ship_stack[i].name); ships[num] = &ship_stack[i];
(*n)++; num++;
} else { } else {
for(j = 0; j < techmax; j++) for(j = 0; j < techmax; j++)
if(tech[j] == ship_stack[i].tech) { if(tech[j] == ship_stack[i].tech) {
shipnames[*n] = strdup(ship_stack[i].name); ships[num] = &ship_stack[i];
(*n)++; 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; return shipnames;
} }
@ -222,8 +254,8 @@ int ships_load(void) {
do { do {
if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_SHIP)==0) { if(node->type == XML_NODE_START && strcmp((char*)node->name, XML_SHIP)==0) {
tmp = ship_parse(node); tmp = ship_parse(node);
ship_stack = realloc(ship_stack, sizeof(Ship)*(++ships)); ship_stack = realloc(ship_stack, sizeof(Ship)*(++ship_nstack));
memcpy(ship_stack+ships-1, tmp, sizeof(Ship)); memcpy(ship_stack+ship_nstack-1, tmp, sizeof(Ship));
free(tmp); free(tmp);
} }
} while((node = node->next)); } while((node = node->next));
@ -232,7 +264,7 @@ int ships_load(void) {
free(buf); free(buf);
xmlCleanupParser(); xmlCleanupParser();
DEBUG("Loaded %d ship%s", ships, (ships==1) ? "" : "s"); DEBUG("Loaded %d ship%s", ship_nstack, (ship_nstack==1) ? "" : "s");
return 0; return 0;
} }
@ -240,7 +272,7 @@ int ships_load(void) {
void ships_free(void) { void ships_free(void) {
ShipOutfit* so, *sot; ShipOutfit* so, *sot;
int i; int i;
for(i = 0; i < ships; i++) { for(i = 0; i < ship_nstack; i++) {
/* Free stored strings. */ /* Free stored strings. */
if((ship_stack+i)->name) free(ship_stack[i].name); if((ship_stack+i)->name) free(ship_stack[i].name);
if((ship_stack+i)->description) free(ship_stack[i].description); if((ship_stack+i)->description) free(ship_stack[i].description);