From 4f0420d82f853254509da615697f6635e7d29fbc Mon Sep 17 00:00:00 2001 From: Allanis Date: Sat, 16 Mar 2013 20:29:18 +0000 Subject: [PATCH] [Add] Basic commodity support. (Not in use yet). --- .clang_complete | 1 + dat/commodity.xml | 14 +++++++ src/economy.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++ src/economy.h | 11 +++++ src/lephisto.c | 6 ++- 5 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 dat/commodity.xml diff --git a/.clang_complete b/.clang_complete index 733c14f..0100428 100644 --- a/.clang_complete +++ b/.clang_complete @@ -1,4 +1,5 @@ -I /usr/include/SDL/ -I /usr/include/freetype2/ +-I /usr/include/libxml2/ -I lib/lua/ diff --git a/dat/commodity.xml b/dat/commodity.xml new file mode 100644 index 0000000..ff91d1d --- /dev/null +++ b/dat/commodity.xml @@ -0,0 +1,14 @@ + + + + 160 + 140 + 120 + + + 240 + 210 + 180 + + + diff --git a/src/economy.c b/src/economy.c index ee3f7d0..7f86768 100644 --- a/src/economy.c +++ b/src/economy.c @@ -1,7 +1,24 @@ #include +#include +#include + #include "lephisto.h" +#include "xml.h" +#include "pack.h" +#include "log.h" #include "economy.h" +#define XML_COMMODITY_ID "Commodities" // XML section identifier. +#define XML_COMMODITY_TAG "commodity" +#define COMMODITY_DATA "../dat/commodity.xml" + +// Commodity stack. +static Commodity* commodity_stack = NULL; +static int commodity_nstack = 0; + +static void commodity_freeOne(Commodity* com); +static Commodity* commodity_parse(xmlNodePtr parent); + // Convert credits to a usable string for displaying. // str must have 10 characters allocated. void credits2str(char* str, unsigned int credits, int decimals) { @@ -16,3 +33,89 @@ void credits2str(char* str, unsigned int credits, int decimals) { else snprintf(str, 16, "%d", credits); } +// Free a commodity. +static void commodity_freeOne(Commodity* com) { + if(com->name) free(com->name); +} + +static Commodity* commodity_parse(xmlNodePtr parent) { + xmlNodePtr node; + Commodity* tmp = CALLOC_L(Commodity); + + tmp->name = (char*)xmlGetProp(parent, (xmlChar*)"name"); + if(tmp->name == NULL) + WARN("Commodity from "COMMODITY_DATA" has invalid or noname"); + + node = parent->xmlChildrenNode; + + do { + if(xml_isNode(node, "high")) + tmp->high = xml_getInt(node); + else if(xml_isNode(node, "medium")) + tmp->medium = xml_getInt(node); + else if(xml_isNode(node, "low")) + tmp->low = xml_getInt(node); + } while((node = node->next)); + +#define MELEMENT(o,s)if(o)WARN("Commodity '%s' missing '"s"' element",tmp->name) + MELEMENT(tmp->high==0, "high"); + MELEMENT(tmp->medium==0, "medium"); + MELEMENT(tmp->low==0, "low"); +#undef MELEMENT + + return tmp; +} + +int commodity_load(void) { + uint32_t bufsize; + char* buf = pack_readfile(DATA, COMMODITY_DATA, &bufsize); + + xmlNodePtr node; + xmlDocPtr doc = xmlParseMemory(buf, bufsize); + + Commodity* tmp = NULL; + + node = doc->xmlChildrenNode; // Commoditys node. + if(strcmp((char*)node->name, XML_COMMODITY_ID)) { + ERR("Malformed "COMMODITY_DATA + " file: Missing root element '"XML_COMMODITY_ID"'"); + return -1; + } + + node = node->xmlChildrenNode; // First faction node. + if(node == NULL) { + ERR("Malformed "COMMODITY_DATA" file: does not contain elements"); + return -1; + } + + do { + if(node->type == XML_NODE_START) { + if(strcmp((char*)node->name, XML_COMMODITY_TAG)==0) { + tmp = commodity_parse(node); + commodity_stack = realloc(commodity_stack, + sizeof(Commodity)*(++commodity_nstack)); + memcpy(commodity_stack+commodity_nstack-1, tmp, sizeof(Commodity)); + free(tmp); + } + } + } while((node = node->next)); + + xmlFreeDoc(doc); + free(buf); + xmlCleanupParser(); + + DEBUG("Loaded %d commodit%s", + commodity_nstack, (commodity_nstack==1) ? "y" : "ies"); + + return 0; +} + +void commodity_free(void) { + int i; + for(i = 0; i < commodity_nstack; i++) + commodity_freeOne(&commodity_stack[i]); + free(commodity_stack); + commodity_stack = NULL; + commodity_nstack = 0; +} + diff --git a/src/economy.h b/src/economy.h index 79d2f29..4018f9e 100644 --- a/src/economy.h +++ b/src/economy.h @@ -1,4 +1,15 @@ #pragma once +typedef struct Commodity_ { + char* name; + int low, medium, high; // Prices. +} Commodity; + +// Commidity stuff. +Commodity* commodity_get(const char* name); +int commodity_load(void); +void commodity_free(void); + +// Misc. void credits2str(char* str, unsigned int credits, int decimals); diff --git a/src/lephisto.c b/src/lephisto.c index d33df41..3038d79 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -25,6 +25,7 @@ #include "pilot.h" #include "sound.h" #include "spfx.h" +#include "economy.h" #include "music.h" #define XML_START_ID "Start" @@ -151,6 +152,7 @@ int main(int argc, char** argv) { toolkit_init(); // Init the toolkit. // Data loading. + commodity_load(); factions_load(); spfx_load(); outfit_load(); @@ -209,9 +211,9 @@ int main(int argc, char** argv) { outfit_free(); spfx_free(); // Remove the special effects. factions_free(); - gl_freeFont(&gl_smallFont); - + commodity_free(); gl_freeFont(NULL); + gl_freeFont(&gl_smallFont); // Exit subsystems. toolkit_exit(); // Kill the toolkit.