naev 0.11.5
claim.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include "naev.h"
13#include "claim.h"
14
15#include "array.h"
16#include "event.h"
17#include "log.h"
18#include "mission.h"
19#include "space.h"
20
24struct Claim_s {
25 int active;
26 int *ids;
27 char **strs;
32};
33
34static char **claimed_strs = NULL;
42Claim_t *claim_create( int exclusive )
43{
44 Claim_t *claim= malloc( sizeof(Claim_t) );
45 claim->active = 0;
46 claim->ids = NULL;
47 claim->strs = NULL;
48 claim->exclusive = exclusive;
49
50 return claim;
51}
52
59int claim_addStr( Claim_t *claim, const char *str )
60{
61 assert( !claim->active );
62 /* Allocate if necessary. */
63 if (claim->strs == NULL)
64 claim->strs = array_create( char* );
65
66 /* New ID. */
67 array_push_back( &claim->strs, strdup( str ) );
68 return 0;
69}
70
77int claim_addSys( Claim_t *claim, int ss_id )
78{
79 assert( !claim->active );
80 /* Allocate if necessary. */
81 if (claim->ids == NULL)
82 claim->ids = array_create( int );
83
84 /* New ID. */
85 array_push_back( &claim->ids, ss_id );
86 return 0;
87}
88
95int claim_isNull( const Claim_t *claim )
96{
97 if (claim == NULL)
98 return 1;
99
100 if (array_size(claim->ids) == 0)
101 return 1;
102
103 return 0;
104}
105
112int claim_test( const Claim_t *claim )
113{
114 int exc;
115
116 /* Must actually have a claim. */
117 if (claim == NULL)
118 return 0;
119
120 exc = claim->exclusive;
121
122 /* See if the system is claimed. */
123 for (int i=0; i<array_size(claim->ids); i++) {
124 StarSystem *sys = system_getIndex( claim->ids[i] );
125 int claimed = sys_isFlag( sys, SYSTEM_CLAIMED );
126 if (claimed || (exc && (sys->claims_soft>0)))
127 return 1;
128 }
129
130 /* Check strings. */
131 for (int i=0; i<array_size(claim->strs); i++) {
132 for (int j=0; j<array_size(claimed_strs); j++) {
133 if (strcmp( claim->strs[i], claimed_strs[j] )==0)
134 return 1;
135 }
136 }
137
138 return 0;
139}
140
148int claim_testStr( const Claim_t *claim, const char *str )
149{
150 /* Must actually have a claim. */
151 if (claim == NULL)
152 return 0;
153
154 /* Check strings. */
155 for (int i=0; i<array_size(claim->strs); i++) {
156 if (strcmp( claim->strs[i], str )==0)
157 return 1;
158 }
159
160 return 0;
161}
162
170int claim_testSys( const Claim_t *claim, int sys )
171{
172 /* Must actually have a claim. */
173 if (claim == NULL)
174 return 0;
175
176 /* See if the system is claimed. */
177 for (int i=0; i<array_size(claim->ids); i++)
178 if (claim->ids[i] == sys)
179 return 1;
180
181 return 0;
182}
183
189void claim_destroy( Claim_t *claim )
190{
191 if (claim->active) {
192 for (int i=0; i<array_size(claim->ids); i++) {
193 StarSystem *sys = system_getIndex(claim->ids[i]);
194 if (claim->exclusive)
195 sys_rmFlag( sys, SYSTEM_CLAIMED );
196 else
197 sys->claims_soft--;
198 }
199 }
200 array_free( claim->ids );
201
202 for (int i=0; i<array_size(claim->strs); i++) {
203 if (claim->active) {
204 for (int j=0; j<array_size(claimed_strs); j++) {
205 if (strcmp(claim->strs[i], claimed_strs[j])==0) {
206 free( claimed_strs[j] );
208 break;
209 }
210 }
211 }
212 free( claim->strs[i] );
213 }
214 array_free( claim->strs );
215 free(claim);
216}
217
221void claim_clear (void)
222{
223 /* Clears all the flags. */
224 StarSystem *sys = system_getAll();
225 for (int i=0; i<array_size(sys); i++) {
226 sys_rmFlag( &sys[i], SYSTEM_CLAIMED );
227 sys[i].claims_soft = 0;
228 }
229
230 for (int i=0; i<array_size(claimed_strs); i++)
231 free(claimed_strs[i]);
233 claimed_strs = NULL;
234}
235
245
251void claim_activate( Claim_t *claim )
252{
253 /* Add flags. */
254 for (int i=0; i<array_size(claim->ids); i++) {
255 StarSystem *sys = system_getIndex( claim->ids[i] );
256 if (claim->exclusive)
257 sys_setFlag( sys, SYSTEM_CLAIMED );
258 else
259 sys->claims_soft++;
260 }
261
262 /* Add strings. */
263 if ((claimed_strs == NULL) && (array_size(claim->strs) > 0))
264 claimed_strs = array_create( char* );
265 for (int i=0; i<array_size(claim->strs); i++)
266 array_push_back( &claimed_strs, strdup( claim->strs[i] ) );
267 claim->active = 1;
268}
269
278int claim_xmlSave( xmlTextWriterPtr writer, const Claim_t *claim )
279{
280 if (claim == NULL)
281 return 0;
282
283 xmlw_attr( writer, "exclusive", "%d", claim->exclusive );
284
285 for (int i=0; i<array_size(claim->ids); i++) {
286 StarSystem *sys = system_getIndex( claim->ids[i] );
287 if (sys != NULL)
288 xmlw_elem( writer, "sys", "%s", sys->name );
289 else
290 WARN(_("System Claim has inexistent system"));
291 }
292
293 for (int i=0; i<array_size(claim->strs); i++)
294 xmlw_elem( writer, "str", "%s", claim->strs[i] );
295
296 return 0;
297}
298
305Claim_t *claim_xmlLoad( xmlNodePtr parent )
306{
307 Claim_t *claim;
308 xmlNodePtr node;
309 int exclusive;
310
311 /* Exclusiveness defaults to true due to older versions. */
312 xmlr_attr_int_def( parent, "exclusive", exclusive, 1 );
313
314 /* Create the claim. */
315 claim = claim_create( exclusive );
316
317 /* Load the nodes. */
318 node = parent->xmlChildrenNode;
319 do {
320 if (xml_isNode(node,"sys")) {
321 StarSystem *sys = system_get( xml_get(node) );
322 if (sys != NULL)
323 claim_addSys( claim, system_index(sys) );
324 else
325 WARN(_("System Claim trying to load system '%s' which doesn't exist"), xml_get(node));
326 }
327 else if (xml_isNode(node,"str")) {
328 char *str = xml_get(node);
329 claim_addStr( claim, str );
330 }
331 } while (xml_nextNode(node));
332
333 /* Activate the claim. */
334 claim_activate( claim );
335
336 return claim;
337}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition array.h:158
#define array_erase(ptr_array, first, last)
Erases elements in interval [first, last).
Definition array.h:140
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:168
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
Definition array.h:129
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
Definition array.h:93
int claim_test(const Claim_t *claim)
Tests to see if a system claim would have collisions.
Definition claim.c:112
void claim_destroy(Claim_t *claim)
Destroys a system claim.
Definition claim.c:189
void claim_clear(void)
Clears the claims on all systems.
Definition claim.c:221
int claim_addStr(Claim_t *claim, const char *str)
Adds a string claim to a claim.
Definition claim.c:59
int claim_addSys(Claim_t *claim, int ss_id)
Adds a claim to a system claim.
Definition claim.c:77
void claim_activate(Claim_t *claim)
Activates a claim on a system.
Definition claim.c:251
int claim_testSys(const Claim_t *claim, int sys)
Tests to see if a system is claimed by a system claim.
Definition claim.c:170
int claim_isNull(const Claim_t *claim)
See if a claim actually contains data.
Definition claim.c:95
Claim_t * claim_xmlLoad(xmlNodePtr parent)
Loads a claim.
Definition claim.c:305
static char ** claimed_strs
Definition claim.c:34
int claim_xmlSave(xmlTextWriterPtr writer, const Claim_t *claim)
Saves all the systems in a claim in XML.
Definition claim.c:278
Claim_t * claim_create(int exclusive)
Creates a system claim.
Definition claim.c:42
int claim_testStr(const Claim_t *claim, const char *str)
Tests to see if a system is claimed by a system claim.
Definition claim.c:148
void claim_activateAll(void)
Activates all the claims.
Definition claim.c:239
void event_activateClaims(void)
Activates all the active event claims.
Definition event.c:768
void missions_activateClaims(void)
Activates mission claims.
Definition mission.c:867
Header file with generic functions and naev-specifics.
StarSystem * system_getIndex(int id)
Get the system by its index.
Definition space.c:989
StarSystem * system_getAll(void)
Gets an array (array.h) of all star systems.
Definition space.c:879
StarSystem * system_get(const char *sysname)
Get the system from its name.
Definition space.c:960
int system_index(const StarSystem *sys)
Gets the index of a star system.
Definition space.c:1000
The claim structure.
Definition claim.c:24
char ** strs
Definition claim.c:27
int active
Definition claim.c:25
int exclusive
Definition claim.c:28
int * ids
Definition claim.c:26