naev 0.10.4
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 const char *lua_default = start_spob_lua_default();
37
38 /* Create the writer. */
39 writer = xmlNewTextWriterDoc(&doc, 0);
40 if (writer == NULL) {
41 WARN(_("testXmlwriterDoc: Error creating the xml writer"));
42 return -1;
43 }
44
45 /* Set the writer parameters. */
46 xmlw_setParams( writer );
47
48 /* Start writer. */
49 xmlw_start(writer);
50 xmlw_startElem( writer, "spob" );
51
52 /* Attributes. */
53 xmlw_attr( writer, "name", "%s", p->name );
54
55 /* Some global attributes. */
56 if (p->display!=NULL)
57 xmlw_elem( writer, "display", "%s", p->display );
58 if (p->feature!=NULL)
59 xmlw_elem( writer, "feature", "%s", p->feature );
60 if ((p->lua_file!=NULL) && ((lua_default==NULL) || strcmp(lua_default,p->lua_file)!=0))
61 xmlw_elem( writer, "lua", "%s", p->lua_file );
62 if (spob_isFlag(p,SPOB_RADIUS))
63 xmlw_elem( writer, "radius", "%f", p->radius );
64 if (p->marker != NULL)
65 xmlw_elem( writer, "marker", "%s", p->marker->name );
66
67 /* Position. */
68 xmlw_startElem( writer, "pos" );
69 xmlw_attr( writer, "x", "%f", p->pos.x );
70 xmlw_attr( writer, "y", "%f", p->pos.y );
71 xmlw_endElem( writer ); /* "pos" */
72
73 /* GFX. */
74 xmlw_startElem( writer, "GFX" );
75 if (p->gfx_spacePath != NULL)
76 xmlw_elem( writer, "space", "%s", p->gfx_spacePath );
77 if (p->gfx_exteriorPath != NULL)
78 xmlw_elem( writer, "exterior", "%s", p->gfx_exteriorPath );
79 xmlw_endElem( writer ); /* "GFX" */
80
81 /* Presence. */
82 if (p->presence.faction >= 0) {
83 xmlw_startElem( writer, "presence" );
84 xmlw_elem( writer, "faction", "%s", faction_name( p->presence.faction ) );
85 xmlw_elem( writer, "base", "%f", p->presence.base );
86 xmlw_elem( writer, "bonus", "%f", p->presence.bonus );
87 xmlw_elem( writer, "range", "%d", p->presence.range );
88 xmlw_endElem( writer );
89 }
90
91 /* General. */
92 xmlw_startElem( writer, "general" );
93 xmlw_elem( writer, "class", "%s", p->class );
94 xmlw_elem( writer, "population", "%g", (double)p->population );
95 xmlw_elem( writer, "hide", "%f", p->hide );
96 xmlw_startElem( writer, "services" );
97 if (spob_hasService( p, SPOB_SERVICE_LAND ))
98 xmlw_elemEmpty( writer, "land" );
99 if (spob_hasService( p, SPOB_SERVICE_REFUEL ))
100 xmlw_elemEmpty( writer, "refuel" );
101 if (spob_hasService( p, SPOB_SERVICE_BAR ))
102 xmlw_elemEmpty( writer, "bar" );
103 if (spob_hasService( p, SPOB_SERVICE_MISSIONS ))
104 xmlw_elemEmpty( writer, "missions" );
105 if (spob_hasService( p, SPOB_SERVICE_COMMODITY ))
106 xmlw_elemEmpty( writer, "commodity" );
107 if (spob_hasService( p, SPOB_SERVICE_OUTFITS ))
108 xmlw_elemEmpty( writer, "outfits" );
109 if (spob_hasService( p, SPOB_SERVICE_SHIPYARD ))
110 xmlw_elemEmpty( writer, "shipyard" );
111 if (spob_hasService( p, SPOB_SERVICE_BLACKMARKET ))
112 xmlw_elemEmpty( writer, "blackmarket" );
113 if (spob_isFlag( p, SPOB_NOMISNSPAWN ))
114 xmlw_elemEmpty( writer, "nomissionspawn" );
115 if (spob_isFlag( p, SPOB_UNINHABITED ))
116 xmlw_elemEmpty( writer, "uninhabited" );
117 xmlw_endElem( writer ); /* "services" */
118 if (spob_hasService( p, SPOB_SERVICE_LAND )) {
119 if (p->presence.faction >= 0) {
120 xmlw_startElem( writer, "commodities" );
121 for (int i=0; i<array_size(p->commodities); i++) {
122 Commodity *c = p->commodities[i];
123 if (!commodity_isFlag(c,COMMODITY_FLAG_STANDARD))
124 xmlw_elem( writer, "commodity", "%s", c->name );
125 }
126 xmlw_endElem( writer ); /* "commodities" */
127 }
128
129 xmlw_elem( writer, "description", "%s", p->description );
130 if (spob_hasService( p, SPOB_SERVICE_BAR ))
131 xmlw_elem( writer, "bar", "%s", p->bar_description );
132 }
133 xmlw_endElem( writer ); /* "general" */
134
135 /* Tech. */
136 if (spob_hasService( p, SPOB_SERVICE_LAND ))
137 tech_groupWrite( writer, p->tech );
138
139 if (array_size(p->tags)>0) {
140 xmlw_startElem( writer, "tags" );
141 for (int i=0; i<array_size(p->tags); i++)
142 xmlw_elem( writer, "tag", "%s", p->tags[i] );
143 xmlw_endElem( writer ); /* "tags" */
144 }
145
146 xmlw_endElem( writer ); /* "spob" */
147 xmlw_done( writer );
148
149 /* No need for writer anymore. */
150 xmlFreeTextWriter( writer );
151
152 /* Write data. */
153 cleanName = uniedit_nameFilter( p->name );
154 asprintf( &file, "%s/%s.xml", conf.dev_save_spob, cleanName );
155 if (xmlSaveFileEnc( file, doc, "UTF-8" ) < 0)
156 WARN("Failed writing '%s'!", file);
157
158 /* Clean up. */
159 xmlFreeDoc(doc);
160 free(cleanName);
161 free(file);
162
163 return 0;
164}
165
171int dpl_saveAll (void)
172{
173 const Spob *p = spob_getAll();
174
175 /* Write spobs. */
176 for (int i=0; i<array_size(p); i++)
177 dpl_saveSpob( &p[i] );
178
179 return 0;
180}
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:171
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:304
Header file with generic functions and naev-specifics.
int asprintf(char **strp, const char *fmt,...)
Like sprintf(), but it allocates a large-enough string and returns the pointer in the first argument....
Definition: nstring.c:161
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:1063
const char * start_spob_lua_default(void)
Gets the default spob Lua file.
Definition: start.c:266
Represents a commodity.
Definition: commodity.h:43
char * dev_save_spob
Definition: conf.h:173
Represents a Space Object (SPOB), including and not limited to planets, stations, wormholes,...
Definition: space.h:88
int tech_groupWrite(xmlTextWriterPtr writer, tech_group_t *grp)
Writes a group in an xml node.
Definition: tech.c:216