15#include "difficulty.h"
22#define DIFFICULTY_XML_ID "difficulty"
25static const Difficulty *difficulty_default = NULL;
26static const Difficulty *difficulty_global = NULL;
27static const Difficulty *difficulty_local = NULL;
28static const Difficulty *difficulty_current = NULL;
33int difficulty_load(
void )
37 for (
int i = 0; i <
array_size( difficulty_files ); i++ ) {
48 node = doc->xmlChildrenNode;
49 if ( !xml_isNode( node, DIFFICULTY_XML_ID ) ) {
50 WARN( _(
"Malformed '%s' file: missing root element '%s'" ),
51 difficulty_files[i], DIFFICULTY_XML_ID );
60 xmlr_attr_strd( node,
"name",
d.name );
61 xmlr_attr_int_opt( node,
"default",
d.def );
64 cur = node->xmlChildrenNode;
69 xmlr_strd( cur,
"description",
d.description );
78 WARN( _(
"Difficulty '%s' has unknown node '%s'" ),
d.name,
80 }
while ( xml_nextNode( cur ) );
88 for (
int i = 0; i <
array_size( difficulty_stack ); i++ ) {
91 if ( difficulty_default )
92 WARN( _(
"More than one difficulty with default flag set!" ) );
93 difficulty_default = dd;
96 if ( difficulty_default == NULL ) {
97 WARN( _(
"No default difficulty set!" ) );
98 difficulty_default = difficulty_stack;
100 difficulty_current = difficulty_default;
103 if ( conf.difficulty != NULL )
104 difficulty_setGlobal( difficulty_get( conf.difficulty ) );
106 for (
int i = 0; i <
array_size( difficulty_files ); i++ )
107 free( difficulty_files[i] );
115void difficulty_free(
void )
117 for (
int i = 0; i <
array_size( difficulty_stack ); i++ ) {
120 free(
d->description );
131 return difficulty_current;
139 return difficulty_stack;
142static const Difficulty *difficulty_getDefault(
void )
144 if ( difficulty_global != NULL )
145 return difficulty_global;
147 return difficulty_default;
153const Difficulty *difficulty_get(
const char *name )
156 return difficulty_getDefault();
157 for (
int i = 0; i <
array_size( difficulty_stack ); i++ ) {
159 if ( strcmp( name,
d->name ) == 0 )
162 WARN( _(
"Unknown difficulty setting '%s'" ), name );
163 return difficulty_default;
166static void difficulty_update(
void )
168 if ( difficulty_local != NULL )
169 difficulty_current = difficulty_local;
170 else if ( difficulty_global != NULL )
171 difficulty_current = difficulty_global;
173 difficulty_current = difficulty_default;
181 difficulty_global =
d;
190 difficulty_local =
d;
202 char *display = malloc( STRMAX );
203 l =
scnprintf( display, STRMAX, _(
"Difficulty %s" ), _(
d->name ) );
204 l +=
scnprintf( &display[l], STRMAX - l,
"\n" );
205 if (
d->description != NULL )
206 l +=
scnprintf( &display[l], STRMAX - l,
"%s\n", _(
d->description ) );
208 _(
"This difficulty applies the following effect to the "
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#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’.
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.
int scnprintf(char *text, size_t maxlen, const char *fmt,...)
Like snprintf(), but returns the number of characters ACTUALLY "printed" into the buffer....
xmlDocPtr xml_parsePhysFS(const char *filename)
Analogous to xmlParseMemory/xmlParseFile.
int ss_statsListDesc(const ShipStatList *ll, char *buf, int len, int newline)
Writes the ship statistics description.
void ss_free(ShipStatList *ll)
Frees a list of ship stats.
int ss_statsMergeFromList(ShipStats *stats, const ShipStatList *list)
Updates a stat structure from a stat list.
ShipStatList * ss_listFromXML(xmlNodePtr node)
Creates a shipstat list element from an xml node.
Represents relative ship statistics as a linked list.
struct ShipStatList_ * next
Represents ship statistics, properties ship can use.