naev 0.12.5
dev_spob.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <stdlib.h>
12
13#include <libgen.h>
14
15#include "dev_spob.h"
16
17#include "array.h"
18#include "conf.h"
19#include "nxml.h"
20#include "start.h"
21
28int dpl_saveSpob( const Spob *p )
29{
30 xmlDocPtr doc;
31 xmlTextWriterPtr writer;
32 char *file;
33 int ret = 0;
34 const char *lua_default = start_spob_lua_default();
35
36 if ( conf.dev_data_dir == NULL ) {
37 WARN( _( "%s is not set!" ), "conf.dev_data_dir" );
38 return -1;
39 }
40
41 /* Create the writer. */
42 writer = xmlNewTextWriterDoc( &doc, 0 );
43 if ( writer == NULL ) {
44 WARN( _( "testXmlwriterDoc: Error creating the xml writer" ) );
45 return -1;
46 }
47
48 /* Set the writer parameters. */
49 xmlw_setParams( writer );
50
51 /* Start writer. */
52 xmlw_start( writer );
53 xmlw_startElem( writer, "spob" );
54
55 /* Attributes. */
56 xmlw_attr( writer, "name", "%s", p->name );
57
58 /* Some global attributes. */
59 if ( p->display != NULL )
60 xmlw_elem( writer, "display", "%s", p->display );
61 if ( p->feature != NULL )
62 xmlw_elem( writer, "feature", "%s", p->feature );
63 if ( ( p->lua_file != NULL ) &&
64 ( ( lua_default == NULL ) || strcmp( lua_default, p->lua_file ) != 0 ) )
65 xmlw_elem( writer, "lua", "%s", p->lua_file_raw );
66 if ( spob_isFlag( p, SPOB_RADIUS ) )
67 xmlw_elem( writer, "radius", "%f", p->radius );
68 if ( p->marker != NULL )
69 xmlw_elem( writer, "marker", "%s", p->marker->name );
70
71 /* Position. */
72 xmlw_startElem( writer, "pos" );
73 xmlw_attr( writer, "x", "%f", p->pos.x );
74 xmlw_attr( writer, "y", "%f", p->pos.y );
75 xmlw_endElem( writer ); /* "pos" */
76
77 /* GFX. */
78 xmlw_startElem( writer, "GFX" );
79 if ( p->gfx_space3dPath != NULL ) {
80 xmlw_startElem( writer, "space3d" );
81 xmlw_attr( writer, "size", "%f", p->gfx_space3d_size );
82 xmlw_str( writer, "%s", p->gfx_space3dPath );
83 xmlw_endElem( writer ); /* space3d */
84 }
85 if ( p->gfx_spacePath != NULL )
86 xmlw_elem( writer, "space", "%s", p->gfx_spacePath );
87 if ( p->gfx_exteriorPath != NULL )
88 xmlw_elem( writer, "exterior", "%s", p->gfx_exteriorPath );
89 if ( p->gfx_commPath != NULL )
90 xmlw_elem( writer, "comm", "%s", p->gfx_commPath );
91 xmlw_endElem( writer ); /* "GFX" */
92
93 /* Presence. */
94 if ( p->presence.faction >= 0 ) {
95 xmlw_startElem( writer, "presence" );
96 xmlw_elem( writer, "faction", "%s", faction_name( p->presence.faction ) );
97 xmlw_elem( writer, "base", "%f", p->presence.base );
98 xmlw_elem( writer, "bonus", "%f", p->presence.bonus );
99 xmlw_elem( writer, "range", "%d", p->presence.range );
100 xmlw_endElem( writer );
101 }
102
103 /* General. */
104 xmlw_startElem( writer, "general" );
105 xmlw_elem( writer, "class", "%s", p->class );
106 xmlw_elem( writer, "population", "%g", (double)p->population );
107 xmlw_elem( writer, "hide", "%f", p->hide );
108 xmlw_startElem( writer, "services" );
109 if ( spob_hasService( p, SPOB_SERVICE_LAND ) )
110 xmlw_elemEmpty( writer, "land" );
111 if ( spob_hasService( p, SPOB_SERVICE_REFUEL ) )
112 xmlw_elemEmpty( writer, "refuel" );
113 if ( spob_hasService( p, SPOB_SERVICE_BAR ) )
114 xmlw_elemEmpty( writer, "bar" );
115 if ( spob_hasService( p, SPOB_SERVICE_MISSIONS ) )
116 xmlw_elemEmpty( writer, "missions" );
117 if ( spob_hasService( p, SPOB_SERVICE_COMMODITY ) )
118 xmlw_elemEmpty( writer, "commodity" );
119 if ( spob_hasService( p, SPOB_SERVICE_OUTFITS ) )
120 xmlw_elemEmpty( writer, "outfits" );
121 if ( spob_hasService( p, SPOB_SERVICE_SHIPYARD ) )
122 xmlw_elemEmpty( writer, "shipyard" );
123 if ( spob_hasService( p, SPOB_SERVICE_BLACKMARKET ) )
124 xmlw_elemEmpty( writer, "blackmarket" );
125 if ( spob_isFlag( p, SPOB_NOMISNSPAWN ) )
126 xmlw_elemEmpty( writer, "nomissionspawn" );
127 if ( spob_isFlag( p, SPOB_UNINHABITED ) )
128 xmlw_elemEmpty( writer, "uninhabited" );
129 if ( spob_isFlag( p, SPOB_NOLANES ) )
130 xmlw_elemEmpty( writer, "nolanes" );
131 xmlw_endElem( writer ); /* "services" */
132 if ( spob_hasService( p, SPOB_SERVICE_LAND ) ) {
133 if ( p->presence.faction >= 0 ) {
134 xmlw_startElem( writer, "commodities" );
135 for ( int i = 0; i < array_size( p->commodities ); i++ ) {
136 Commodity *c = p->commodities[i];
137 if ( !commodity_isFlag( c, COMMODITY_FLAG_STANDARD ) )
138 xmlw_elem( writer, "commodity", "%s", c->name );
139 }
140 xmlw_endElem( writer ); /* "commodities" */
141 }
142
143 xmlw_elem( writer, "description", "%s", p->description );
144 if ( spob_hasService( p, SPOB_SERVICE_BAR ) )
145 xmlw_elem( writer, "bar", "%s", p->bar_description );
146 }
147 xmlw_endElem( writer ); /* "general" */
148
149 /* Tech. */
150 if ( spob_hasService( p, SPOB_SERVICE_LAND ) )
151 tech_groupWrite( writer, p->tech );
152
153 if ( array_size( p->tags ) > 0 ) {
154 xmlw_startElem( writer, "tags" );
155 for ( int i = 0; i < array_size( p->tags ); i++ )
156 xmlw_elem( writer, "tag", "%s", p->tags[i] );
157 xmlw_endElem( writer ); /* "tags" */
158 }
159
160 xmlw_endElem( writer ); /* "spob" */
161 xmlw_done( writer );
162
163 /* No need for writer anymore. */
164 xmlFreeTextWriter( writer );
165
166 /* Write data. */
167 char path[PATH_MAX];
168 snprintf( path, sizeof( path ), "%s", p->filename );
169 const char *filename = basename( path );
170 SDL_asprintf( &file, "%s/spob/%s", conf.dev_data_dir, filename );
171 if ( xmlSaveFileEnc( file, doc, "UTF-8" ) < 0 ) {
172 WARN( "Failed to write '%s'!", file );
173 ret = -1;
174 }
175
176 /* Clean up. */
177 xmlFreeDoc( doc );
178 free( file );
179
180 return ret;
181}
182
188int dpl_saveAll( void )
189{
190 const Spob *p = spob_getAll();
191 int ret = 0;
192
193 /* Write spobs. */
194 for ( int i = 0; i < array_size( p ); i++ )
195 ret |= dpl_saveSpob( &p[i] );
196
197 return ret;
198}
Provides macros to work with dynamic arrays.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:179
int dpl_saveAll(void)
Saves all the star spobs.
Definition dev_spob.c:188
int dpl_saveSpob(const Spob *p)
Saves a spob.
Definition dev_spob.c:28
const char * faction_name(int f)
Gets a factions "real" (internal) name.
Definition faction.c:331
#define PATH_MAX
Definition naev.h:57
void xmlw_setParams(xmlTextWriterPtr writer)
Sets up the standard xml write parameters.
Definition nxml.c:59
static const double c[]
Definition rng.c:256
Spob * spob_getAll(void)
Gets an array (array.h) of all spobs.
Definition space.c:1166
const char * start_spob_lua_default(void)
Gets the default spob Lua file.
Definition start.c:292
Represents a commodity.
Definition commodity.h:57
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...
Definition space.h:102
int tech_groupWrite(xmlTextWriterPtr writer, tech_group_t *grp)
Writes a group in an xml node.
Definition tech.c:239