26#include "gatherable.h"
39#define XML_COMMODITY_ID "commodity"
40#define CRED_TEXT_MAX (ECON_CRED_STRLEN-4)
53static void commodity_freeOne(
Commodity* com );
54static int commodity_parse(
Commodity *temp,
const char *filename );
63void credits2str(
char *str, credits_t credits,
int decimals )
67 snprintf( str, CRED_TEXT_MAX, _(
"%.*f ¤"), 0, (
double)credits );
69 else if (credits >= 1000000000000000000LL)
70 snprintf( str, CRED_TEXT_MAX, _(
"%.*f E¤"), decimals, (
double)credits / 1e18 );
71 else if (credits >= 1000000000000000LL)
72 snprintf( str, CRED_TEXT_MAX, _(
"%.*f P¤"), decimals, (
double)credits / 1e15 );
73 else if (credits >= 1000000000000LL)
74 snprintf( str, CRED_TEXT_MAX, _(
"%.*f T¤"), decimals, (
double)credits / 1e12 );
75 else if (credits >= 1000000000L)
76 snprintf( str, CRED_TEXT_MAX, _(
"%.*f G¤"), decimals, (
double)credits / 1e9 );
77 else if (credits >= 1000000)
78 snprintf( str, CRED_TEXT_MAX, _(
"%.*f M¤"), decimals, (
double)credits / 1e6 );
79 else if (credits >= 1000)
80 snprintf( str, CRED_TEXT_MAX, _(
"%.*f k¤"), decimals, (
double)credits / 1e3 );
82 snprintf (str, CRED_TEXT_MAX, _(
"%.*f ¤"), decimals, (
double)credits );
93void price2str(
char *str, credits_t price, credits_t credits,
int decimals )
95 char buf[ CRED_TEXT_MAX ];
97 if (price <= credits) {
98 credits2str( str, price, decimals );
102 credits2str( buf, price, decimals );
103 snprintf( str, ECON_CRED_STRLEN,
"#r%s#0", (
char*)buf );
112void tonnes2str(
char *str,
int tonnes )
114 snprintf( str, ECON_MASS_STRLEN, n_(
"%d tonne",
"%d tonnes", tonnes ), tonnes );
131Commodity* commodity_get(
const char* name )
136 WARN(_(
"Commodity '%s' not found in stack"), name);
146Commodity* commodity_getW(
const char* name )
151 for (
int i=0; i<
array_size(commodity_temp); i++)
152 if (strcmp(commodity_temp[i]->name, name) == 0)
153 return commodity_temp[i];
162int commodity_getN (
void)
173Commodity* commodity_getByIndex(
const int indx )
176 WARN(_(
"Commodity with index %d not found"),indx);
187static void commodity_freeOne(
Commodity* com )
197 while (next != NULL ) {
205 while (next != NULL ) {
223int commodity_compareTech(
const void *commodity1,
const void *commodity2 )
238 return strcmp( c1->
name, c2->
name );
248 for (
int i=0; i<n; i++) {
250 if (commodity_isFlag(
c,COMMODITY_FLAG_STANDARD))
263static int commodity_parse(
Commodity *temp,
const char *filename )
265 xmlNodePtr node, parent;
272 parent = doc->xmlChildrenNode;
273 if (strcmp((
char*)parent->name,XML_COMMODITY_ID)) {
274 ERR(_(
"Malformed %s file: missing root element '%s'"), filename, XML_COMMODITY_ID);
284 node = parent->xmlChildrenNode;
288 xmlr_strd(node,
"name", temp->
name);
290 xmlr_int(node,
"price", temp->
price);
291 xmlr_float(node,
"price_mod", temp->
price_mod);
292 xmlr_strd(node,
"price_ref", temp->
price_ref);
294 if (xml_isNode(node,
"gfx_space")) {
296 COMMODITY_GFX_PATH
"space/%s", 1, 1, OPENGL_TEX_MIPMAPS );
299 if (xml_isNode(node,
"gfx_store")) {
301 COMMODITY_GFX_PATH
"%s", 1, 1, OPENGL_TEX_MIPMAPS );
304 if (xml_isNode(node,
"standard")) {
305 commodity_setFlag( temp, COMMODITY_FLAG_STANDARD );
308 if (xml_isNode(node,
"always_can_sell")) {
309 commodity_setFlag( temp, COMMODITY_FLAG_ALWAYS_CAN_SELL );
312 if (xml_isNode(node,
"price_constant")) {
313 commodity_setFlag( temp, COMMODITY_FLAG_PRICE_CONSTANT );
316 if (xml_isNode(node,
"illegalto")) {
317 xmlNodePtr cur = node->xmlChildrenNode;
321 if (xml_isNode(cur,
"faction")) {
325 }
while (xml_nextNode(node));
329 xmlr_float(node,
"period", temp->
period);
330 if (xml_isNode(node,
"spob_modifier")) {
333 xmlr_attr_strd(node,
"type", newdict->name);
334 newdict->value = xml_getFloat(node);
338 if (xml_isNode(node,
"faction_modifier")) {
341 xmlr_attr_strd(node,
"type", newdict->name);
342 newdict->value = xml_getFloat(node);
347 WARN(_(
"Commodity '%s' has unknown node '%s'"),temp->
name, node->name);
348 }
while (xml_nextNode(node));
350 if (temp->
name == NULL)
351 WARN( _(
"Commodity from %s has invalid or no name"), COMMODITY_DATA_PATH);
355 WARN(_(
"No <gfx_store> node found, using default texture for commodity \"%s\""), temp->
name);
363 if (temp->
price > 0.)
364 WARN(_(
"Commodity '%s' is setting both 'price' and 'price_ref'."),temp->
name);
368#define MELEMENT(o,s) if (o) WARN( _("Commodity '%s' missing '"s"' element"), temp->name)
370 MELEMENT(temp->high==0,
"high");
371 MELEMENT(temp->medium==0,
"medium");
372 MELEMENT(temp->low==0,
"low");
388int commodity_checkIllegal(
const Commodity *com,
int faction )
403int commodity_isTemp(
const char* name )
405 for (
int i=0; i<
array_size(commodity_temp); i++)
406 if (strcmp(commodity_temp[i]->name, name) == 0)
412 WARN(_(
"Commodity '%s' not found in stack"), name);
423Commodity* commodity_newTemp(
const char* name,
const char* desc )
426 if (commodity_temp == NULL)
432 (*c)->name = strdup(name);
433 (*c)->description = strdup(desc);
440int commodity_tempIllegalto(
Commodity *com,
int faction )
445 WARN(_(
"Trying to modify temporary commodity '%s'!"), com->
name);
469int commodity_load (
void)
472 Uint32 time = SDL_GetTicks();
479 for (
int i=0; i<
array_size(commodities); i++) {
481 int ret = commodity_parse( &
c, commodities[i] );
494 free( commodities[i] );
499 time = SDL_GetTicks() - time;
511void commodity_free (
void)
518 for (
int i=0; i<
array_size(commodity_temp); i++) {
519 commodity_freeOne( commodity_temp[i] );
520 free( commodity_temp[i] );
523 commodity_temp = NULL;
529 gatherable_cleanup();
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
#define array_create_size(basic_type, capacity)
Creates a new dynamic array of ‘basic_type’ with an initial capacity.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
Commodity * commodity_stack
int faction_get(const char *name)
Gets a faction ID by name.
void naev_renderLoadscreen(void)
Renders the loadscreen if necessary.
Header file with generic functions and naev-specifics.
char ** ndata_listRecursive(const char *path)
Lists all the visible files in a directory, at any depth.
glTexture * xml_parseTexture(xmlNodePtr node, const char *path, int defsx, int defsy, const unsigned int flags)
Parses a texture handling the sx and sy elements.
xmlDocPtr xml_parsePhysFS(const char *filename)
Analogous to xmlParseMemory/xmlParseFile.
glTexture * gl_newImage(const char *path, const unsigned int flags)
Loads an image as a texture.
void gl_freeTexture(glTexture *texture)
Frees a texture.
Represents a dictionary of values used to modify a commodity.
CommodityModifier * spob_modifier
double population_modifier
CommodityModifier * faction_modifier