[Add] Basic commodity support. (Not in use yet).
This commit is contained in:
parent
49ab58fdda
commit
4f0420d82f
@ -1,4 +1,5 @@
|
||||
-I /usr/include/SDL/
|
||||
-I /usr/include/freetype2/
|
||||
-I /usr/include/libxml2/
|
||||
-I lib/lua/
|
||||
|
||||
|
14
dat/commodity.xml
Normal file
14
dat/commodity.xml
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Commodities>
|
||||
<commodity name="Food">
|
||||
<high>160</high>
|
||||
<medium>140</medium>
|
||||
<low>120</low>
|
||||
</commodity>
|
||||
<commodity name="Ore">
|
||||
<high>240</high>
|
||||
<medium>210</medium>
|
||||
<low>180</low>
|
||||
</commodity>
|
||||
</Commodities>
|
||||
|
103
src/economy.c
103
src/economy.c
@ -1,7 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user