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