/
ship.c
928 lines (795 loc) · 26.2 KB
/
ship.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/*
* See Licensing and Copyright notice in naev.h
*/
/**
* @file ship.c
*
* @brief Handles the ship details.
*/
#include "ship.h"
#include "naev.h"
#include "nstring.h"
#include <limits.h>
#include "nxml.h"
#include "log.h"
#include "ndata.h"
#include "toolkit.h"
#include "array.h"
#include "conf.h"
#include "npng.h"
#include "colour.h"
#include "shipstats.h"
#include "nfile.h"
#define XML_SHIP "ship" /**< XML individual ship identifier. */
#define SHIP_EXT ".png" /**< Ship graphics extension format. */
#define SHIP_ENGINE "_engine" /**< Engine graphic extension. */
#define SHIP_TARGET "_target" /**< Target graphic extension. */
#define SHIP_COMM "_comm" /**< Communication graphic extension. */
#define VIEW_WIDTH 300 /**< Ship view window width. */
#define VIEW_HEIGHT 300 /**< Ship view window height. */
#define BUTTON_WIDTH 80 /**< Button width in ship view window. */
#define BUTTON_HEIGHT 30 /**< Button height in ship view window. */
#define CHUNK_SIZE 32 /**< Rate at which to allocate memory. */
#define STATS_DESC_MAX 128 /**< Maximum length for statistics description. */
static Ship* ship_stack = NULL; /**< Stack of ships available in the game. */
/*
* Prototypes
*/
static int ship_loadGFX( Ship *temp, char *buf, int sx, int sy, int engine );
static int ship_parse( Ship *temp, xmlNodePtr parent );
/**
* @brief Gets a ship based on its name.
*
* @param name Name to match.
* @return Ship matching name or NULL if not found.
*/
Ship* ship_get( const char* name )
{
Ship *temp;
int i;
temp = ship_stack;
for (i=0; i < array_size(ship_stack); i++)
if (strcmp(temp[i].name, name)==0)
return &temp[i];
WARN("Ship %s does not exist", name);
return NULL;
}
/**
* @brief Gets a ship based on its name without warning.
*
* @param name Name to match.
* @return Ship matching name or NULL if not found.
*/
Ship* ship_getW( const char* name )
{
Ship *temp;
int i;
temp = ship_stack;
for (i=0; i < array_size(ship_stack); i++)
if (strcmp(temp[i].name, name)==0)
return &temp[i];
return NULL;
}
/**
* @brief Checks to see if an ship exists matching name (case insensitive).
*/
const char *ship_existsCase( const char* name )
{
int i;
for (i=0; i<array_size(ship_stack); i++)
if (strcasecmp(name,ship_stack[i].name)==0)
return ship_stack[i].name;
return NULL;
}
/**
* @brief Gets all the ships.
*/
Ship* ship_getAll( int *n )
{
*n = array_size(ship_stack);
return ship_stack;
}
/**
* @brief Comparison function for qsort().
*/
int ship_compareTech( const void *arg1, const void *arg2 )
{
const Ship *s1, *s2;
/* Get ships. */
s1 = * (const Ship**) arg1;
s2 = * (const Ship**) arg2;
/* Compare class. */
if (s1->class < s2->class)
return +1;
else if (s1->class > s2->class)
return -1;
/* Compare price. */
if (s1->price < s2->price)
return +1;
else if (s1->price > s2->price)
return -1;
/* Same. */
return strcmp( s1->name, s2->name );
}
/**
* @brief Gets the ship's class name in human readable form.
*
* @param s Ship to get the class name from.
* @return The human readable class name.
*/
char* ship_class( Ship* s )
{
switch (s->class) {
case SHIP_CLASS_NULL:
return "NULL";
/* Civilian. */
case SHIP_CLASS_YACHT:
return "Yacht";
case SHIP_CLASS_LUXURY_YACHT:
return "Luxury Yacht";
case SHIP_CLASS_CRUISE_SHIP:
return "Cruise Ship";
/* Merchant. */
case SHIP_CLASS_COURIER:
return "Courier";
case SHIP_CLASS_ARMOURED_TRANSPORT:
return "Armoured Transport";
case SHIP_CLASS_FREIGHTER:
return "Freighter";
case SHIP_CLASS_BULK_CARRIER:
return "Bulk Carrier";
/* Military. */
case SHIP_CLASS_SCOUT:
return "Scout";
case SHIP_CLASS_FIGHTER:
return "Fighter";
case SHIP_CLASS_BOMBER:
return "Bomber";
case SHIP_CLASS_CORVETTE:
return "Corvette";
case SHIP_CLASS_DESTROYER:
return "Destroyer";
case SHIP_CLASS_CRUISER:
return "Cruiser";
case SHIP_CLASS_CARRIER:
return "Carrier";
/* Robotic. */
case SHIP_CLASS_DRONE:
return "Drone";
case SHIP_CLASS_HEAVY_DRONE:
return "Heavy Drone";
case SHIP_CLASS_MOTHERSHIP:
return "Mothership";
/* Unknown. */
default:
return "Unknown";
}
}
/**
* @brief Gets the machine ship class identifier from a human readable string.
*
* @param str String to extract ship class identifier from.
*/
ShipClass ship_classFromString( char* str )
{
/* Civilian */
if (strcmp(str,"Yacht")==0)
return SHIP_CLASS_YACHT;
else if (strcmp(str,"Luxury Yacht")==0)
return SHIP_CLASS_LUXURY_YACHT;
else if (strcmp(str,"Cruise Ship")==0)
return SHIP_CLASS_CRUISE_SHIP;
/* Merchant. */
else if (strcmp(str,"Courier")==0)
return SHIP_CLASS_COURIER;
else if (strcmp(str,"Freighter")==0)
return SHIP_CLASS_FREIGHTER;
else if (strcmp(str,"Armoured Transport")==0)
return SHIP_CLASS_ARMOURED_TRANSPORT;
else if (strcmp(str,"Bulk Carrier")==0)
return SHIP_CLASS_BULK_CARRIER;
/* Military */
else if (strcmp(str,"Scout")==0)
return SHIP_CLASS_SCOUT;
else if (strcmp(str,"Fighter")==0)
return SHIP_CLASS_FIGHTER;
else if (strcmp(str,"Bomber")==0)
return SHIP_CLASS_BOMBER;
else if (strcmp(str,"Corvette")==0)
return SHIP_CLASS_CORVETTE;
else if (strcmp(str,"Destroyer")==0)
return SHIP_CLASS_DESTROYER;
else if (strcmp(str,"Cruiser")==0)
return SHIP_CLASS_CRUISER;
else if (strcmp(str,"Carrier")==0)
return SHIP_CLASS_CARRIER;
/* Robotic */
else if (strcmp(str,"Drone")==0)
return SHIP_CLASS_DRONE;
else if (strcmp(str,"Heavy Drone")==0)
return SHIP_CLASS_HEAVY_DRONE;
else if (strcmp(str,"Mothership")==0)
return SHIP_CLASS_MOTHERSHIP;
/* Unknown */
return SHIP_CLASS_NULL;
}
/**
* @brief Gets the ship's base price (no outfits).
*/
credits_t ship_basePrice( Ship* s )
{
credits_t price;
/* Get ship base price. */
price = s->price;
if (price < 0) {
WARN("Negative ship base price!");
price = 0;
}
return price;
}
/**
* @brief Loads the ship's comm graphic.
*
* Must be freed afterwards.
*/
glTexture* ship_loadCommGFX( Ship* s )
{
if (s->gfx_comm != NULL)
return gl_newImage( s->gfx_comm, 0 );
return NULL;
}
/**
* @brief Generates a target graphic for a ship.
*/
static int ship_genTargetGFX( Ship *temp, SDL_Surface *surface, int sx, int sy )
{
SDL_Surface *gfx, *gfx_store;
int potw, poth, potw_store, poth_store;
int x, y, sw, sh;
SDL_Rect rtemp, dstrect;
#if 0 /* Required for scanlines. */
int i, j;
uint32_t *pix;
double r, g, b, a;
double h, s, v;
#endif
char buf[PATH_MAX];
#if ! SDL_VERSION_ATLEAST(1,3,0)
Uint32 saved_flags;
#endif /* ! SDL_VERSION_ATLEAST(1,3,0) */
/* Get sprite size. */
sw = temp->gfx_space->w / sx;
sh = temp->gfx_space->h / sy;
/* POT size. */
if (gl_needPOT()) {
potw = gl_pot( sw );
poth = gl_pot( sh );
potw_store = gl_pot( SHIP_TARGET_W );
poth_store = gl_pot( SHIP_TARGET_H );
}
else {
potw = sw;
poth = sh;
potw_store = SHIP_TARGET_W;
poth_store = SHIP_TARGET_H;
}
/* Create the surface. */
#if SDL_VERSION_ATLEAST(1,3,0)
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
/* create the temp POT surface */
gfx = SDL_CreateRGBSurface( 0, potw, poth,
surface->format->BytesPerPixel*8, RGBAMASK );
gfx_store = SDL_CreateRGBSurface( 0, potw_store, poth_store,
surface->format->BytesPerPixel*8, RGBAMASK );
#else /* SDL_VERSION_ATLEAST(1,3,0) */
saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
SDL_SetAlpha( surface, 0, SDL_ALPHA_OPAQUE );
SDL_SetColorKey( surface, 0, surface->format->colorkey );
}
/* create the temp POT surface */
gfx = SDL_CreateRGBSurface( SDL_SRCCOLORKEY,
potw, poth, surface->format->BytesPerPixel*8, RGBAMASK );
gfx_store = SDL_CreateRGBSurface( SDL_SRCCOLORKEY,
potw_store, poth_store, surface->format->BytesPerPixel*8, RGBAMASK );
#endif /* SDL_VERSION_ATLEAST(1,3,0) */
if (gfx == NULL) {
WARN( "Unable to create ship '%s' targeting surface.", temp->name );
return -1;
}
/* Copy over for target. */
gl_getSpriteFromDir( &x, &y, temp->gfx_space, M_PI* 5./4. );
rtemp.x = sw * x;
rtemp.y = sh * (temp->gfx_space->sy-y-1);
rtemp.w = sw;
rtemp.h = sh;
dstrect.x = 0;
dstrect.y = 0;
dstrect.w = rtemp.w;
dstrect.h = rtemp.h;
SDL_BlitSurface( surface, &rtemp, gfx, &dstrect );
/* Copy over for store. */
dstrect.x = (SHIP_TARGET_W - sw) / 2;
dstrect.y = (SHIP_TARGET_H - sh) / 2;
dstrect.w = rtemp.w;
dstrect.h = rtemp.h;
SDL_BlitSurface( surface, &rtemp, gfx_store, &dstrect );
#if ! SDL_VERSION_ATLEAST(1,3,0)
/* set saved alpha */
if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
SDL_SetAlpha( surface, 0, 0 );
#endif /* ! SDL_VERSION_ATLEAST(1,3,0) */
/* Load the store surface. */
nsnprintf( buf, sizeof(buf), "%s_gfx_store.png", temp->name );
temp->gfx_store = gl_loadImagePad( buf, gfx_store, 0, SHIP_TARGET_W, SHIP_TARGET_H, 1, 1, 1 );
#if 0 /* Disabled for now due to issues with larger sprites. */
/* Some filtering. */
for (j=0; j<sh; j++) {
for (i=0; i<sw; i++) {
pix = (uint32_t*) ((uint8_t*) gfx->pixels + j*gfx->pitch + i*gfx->format->BytesPerPixel);
r = ((double) (*pix & RMASK)) / (double) RMASK;
g = ((double) (*pix & GMASK)) / (double) GMASK;
b = ((double) (*pix & BMASK)) / (double) BMASK;
a = ((double) (*pix & AMASK)) / (double) AMASK;
if (j%2) /* Add scanlines. */
a *= 0.5;
/* Convert to HSV. */
col_rgb2hsv( &h, &s, &v, r, g, b );
h = 0.0;
s = 1.0;
v *= 1.5;
/* Convert back to RGB. */
col_hsv2rgb( &r, &g, &b, h, s, v );
/* Convert to pixel. */
*pix = ((uint32_t) (r*RMASK) & RMASK) |
((uint32_t) (g*GMASK) & GMASK) |
((uint32_t) (b*BMASK) & BMASK) |
((uint32_t) (a*AMASK) & AMASK);
}
}
#endif
/* Load the surface. */
nsnprintf( buf, sizeof(buf), "%s_gfx_target.png", temp->name );
temp->gfx_target = gl_loadImagePad( buf, gfx, 0, sw, sh, 1, 1, 1 );
return 0;
}
/**
* @brief Loads the graphics for a ship.
*
* @param temp Ship to load into.
* @param buf Name of the texture to work with.
*/
static int ship_loadGFX( Ship *temp, char *buf, int sx, int sy, int engine )
{
char base[PATH_MAX], str[PATH_MAX];
int i;
png_uint_32 w, h;
SDL_RWops *rw;
npng_t *npng;
SDL_Surface *surface;
/* Get base path. */
for (i=0; i<PATH_MAX; i++) {
if ((buf[i] == '\0') || (buf[i] == '_')) {
base[i] = '\0';
break;
}
base[i] = buf[i];
}
if (i>=PATH_MAX) {
WARN("Failed to get base path of '%s'.", buf);
return -1;
}
/* Load the space sprite. */
nsnprintf( str, PATH_MAX, SHIP_GFX_PATH"%s/%s"SHIP_EXT, base, buf );
rw = ndata_rwops( str );
npng = npng_open( rw );
npng_dim( npng, &w, &h );
surface = npng_readSurface( npng, gl_needPOT(), 1 );
/* Load the texture. */
temp->gfx_space = gl_loadImagePad( str, surface,
OPENGL_TEX_MAPTRANS | OPENGL_TEX_MIPMAPS,
w, h, sx, sy, 0 );
/* Create the target graphic. */
ship_genTargetGFX( temp, surface, sx, sy );
/* Free stuff. */
npng_close( npng );
SDL_RWclose( rw );
SDL_FreeSurface( surface );
/* Load the engine sprite .*/
if (engine && conf.engineglow && conf.interpolate) {
nsnprintf( str, PATH_MAX, SHIP_GFX_PATH"%s/%s"SHIP_ENGINE SHIP_EXT, base, buf );
temp->gfx_engine = gl_newSprite( str, sx, sy, OPENGL_TEX_MIPMAPS );
if (temp->gfx_engine == NULL)
WARN("Ship '%s' does not have an engine sprite (%s).", temp->name, str );
}
/* Calculate mount angle. */
temp->mangle = 2.*M_PI;
temp->mangle /= temp->gfx_space->sx * temp->gfx_space->sy;
/* Get the comm graphic for future loading. */
nsnprintf( str, PATH_MAX, SHIP_GFX_PATH"%s/%s"SHIP_COMM SHIP_EXT, base, buf );
temp->gfx_comm = strdup(str);
return 0;
}
/**
* @brief Extracts the ingame ship from an XML node.
*
* @param temp Ship to load data into.
* @param parent Node to get ship from.
* @return 0 on success.
*/
static int ship_parse( Ship *temp, xmlNodePtr parent )
{
int i;
xmlNodePtr cur, node;
int sx, sy;
char *stmp, *buf;
int l, m, h, engine;
OutfitSlotSize base_size;
ShipStatList *ll;
/* Clear memory. */
memset( temp, 0, sizeof(Ship) );
/* Defaults. */
temp->thrust = -1;
temp->speed = -1;
ss_statsInit( &temp->stats_array );
/* Get name. */
xmlr_attr(parent,"name",temp->name);
if (temp->name == NULL)
WARN("Ship in "SHIP_DATA_PATH" has invalid or no name");
/* Load data. */
node = parent->xmlChildrenNode;
do { /* load all the data */
/* Only handle nodes. */
xml_onlyNodes(node);
if (xml_isNode(node,"GFX")) {
/* Get base graphic name. */
buf = xml_get(node);
if (buf==NULL) {
WARN("Ship '%s': GFX element is NULL", temp->name);
continue;
}
/* Get sprite size. */
xmlr_attr(node, "sx", stmp );
if (stmp != NULL) {
sx = atoi(stmp);
free(stmp);
}
else
sx = 8;
xmlr_attr(node, "sy", stmp );
if (stmp != NULL) {
sy = atoi(stmp);
free(stmp);
}
else
sy = 8;
xmlr_attr(node, "noengine", stmp );
if (stmp != NULL) {
engine = 0;
free(stmp);
}
else
engine = 1;
/* Load the graphics. */
ship_loadGFX( temp, buf, sx, sy, engine );
continue;
}
xmlr_strd(node,"GUI",temp->gui);
if (xml_isNode(node,"sound")) {
temp->sound = sound_get( xml_get(node) );
continue;
}
xmlr_strd(node,"base_type",temp->base_type);
if (xml_isNode(node,"class")) {
temp->class = ship_classFromString( xml_get(node) );
continue;
}
xmlr_long(node,"price",temp->price);
xmlr_strd(node,"license",temp->license);
xmlr_strd(node,"fabricator",temp->fabricator);
xmlr_strd(node,"description",temp->description);
if (xml_isNode(node,"movement")) {
cur = node->children;
do {
xml_onlyNodes(cur);
xmlr_float(cur,"thrust",temp->thrust);
xmlr_float(cur,"turn",temp->turn);
xmlr_float(cur,"speed",temp->speed);
/* All the xmlr_ stuff have continue cases. */
WARN("Ship '%s' has unknown movement node '%s'.", temp->name, cur->name);
} while (xml_nextNode(cur));
continue;
}
if (xml_isNode(node,"health")) {
cur = node->children;
do {
xml_onlyNodes(cur);
xmlr_float(cur,"absorb",temp->dmg_absorb);
xmlr_float(cur,"armour",temp->armour);
xmlr_float(cur,"armour_regen",temp->armour_regen);
xmlr_float(cur,"shield",temp->shield);
xmlr_float(cur,"shield_regen",temp->shield_regen);
xmlr_float(cur,"energy",temp->energy);
xmlr_float(cur,"energy_regen",temp->energy_regen);
/* All the xmlr_ stuff have continue cases. */
WARN("Ship '%s' has unknown health node '%s'.", temp->name, cur->name);
} while (xml_nextNode(cur));
continue;
}
if (xml_isNode(node,"characteristics")) {
cur = node->children;
do {
xml_onlyNodes(cur);
xmlr_int(cur,"crew",temp->crew);
xmlr_float(cur,"mass",temp->mass);
xmlr_float(cur,"cpu",temp->cpu);
xmlr_int(cur,"fuel",temp->fuel);
xmlr_float(cur,"cargo",temp->cap_cargo);
/* All the xmlr_ stuff have continue cases. */
WARN("Ship '%s' has unknown characteristic node '%s'.", temp->name, cur->name);
} while (xml_nextNode(cur));
continue;
}
if (xml_isNode(node,"slots")) {
/* First pass, get number of mounts. */
cur = node->children;
do {
xml_onlyNodes(cur);
if (xml_isNode(cur,"structure"))
temp->outfit_nstructure++;
else if (xml_isNode(cur,"utility"))
temp->outfit_nutility++;
else if (xml_isNode(cur,"weapon"))
temp->outfit_nweapon++;
else
WARN("Ship '%s' has unknown slot node '%s'.", temp->name, cur->name);
} while (xml_nextNode(cur));
/* Allocate the space. */
temp->outfit_structure = calloc( temp->outfit_nstructure, sizeof(ShipOutfitSlot) );
temp->outfit_utility = calloc( temp->outfit_nutility, sizeof(ShipOutfitSlot) );
temp->outfit_weapon = calloc( temp->outfit_nweapon, sizeof(ShipOutfitSlot) );
/* Second pass, initialize the mounts. */
l = m = h = 0;
cur = node->children;
do {
xml_onlyNodes(cur);
if (xml_isNode(cur,"structure")) {
temp->outfit_structure[l].slot.type = OUTFIT_SLOT_STRUCTURE;
temp->outfit_structure[l].slot.size = outfit_toSlotSize( xml_get(cur) );
/*if (temp->outfit_structure[l].size == OUTFIT_SLOT_SIZE_NA)
WARN("Ship '%s' has invalid slot size '%s'", temp->name, xml_get(cur) );*/
l++;
}
if (xml_isNode(cur,"utility")) {
temp->outfit_utility[m].slot.type = OUTFIT_SLOT_UTILITY;
temp->outfit_utility[m].slot.size = outfit_toSlotSize( xml_get(cur) );
/*if (temp->outfit_utility[m].size == OUTFIT_SLOT_SIZE_NA)
WARN("Ship '%s' has invalid slot size '%s'", temp->name, xml_get(cur) );*/
m++;
}
if (xml_isNode(cur,"weapon")) {
temp->outfit_weapon[h].slot.type = OUTFIT_SLOT_WEAPON;
temp->outfit_weapon[h].slot.size = outfit_toSlotSize( xml_get(cur) );
/*if (temp->outfit_weapon[h].size == OUTFIT_SLOT_SIZE_NA)
WARN("Ship '%s' has invalid slot size '%s'", temp->name, xml_get(cur) );*/
/* Get mount point. */
xmlr_attr(cur,"x",stmp);
if (stmp!=NULL) {
temp->outfit_weapon[h].mount.x = atof(stmp);
free(stmp);
}
else
WARN("Ship '%s' missing 'x' element of 'weapon' slot.",temp->name);
xmlr_attr(cur,"y",stmp);
if (stmp!=NULL) {
temp->outfit_weapon[h].mount.y = atof(stmp);
/* Since we measure in pixels, we have to modify it so it
* doesn't get corrected by the ortho correction. */
temp->outfit_weapon[h].mount.y *= M_SQRT2;
free(stmp);
}
else
WARN("Ship '%s' missing 'y' element of 'weapon' slot.",temp->name);
xmlr_attr(cur,"h",stmp);
if (stmp!=NULL) {
temp->outfit_weapon[h].mount.h = atof(stmp);
free(stmp);
}
else
WARN("Ship '%s' missing 'h' element of 'weapon' slot.",temp->name);
/* Increment h. */
h++;
}
} while (xml_nextNode(cur));
continue;
}
/* Parse ship stats. */
if (xml_isNode(node,"stats")) {
cur = node->children;
do {
xml_onlyNodes(cur);
ll = ss_listFromXML( cur );
if (ll != NULL) {
ll->next = temp->stats;
temp->stats = ll;
continue;
}
WARN("Ship '%s' has unknown stat '%s'.", temp->name, cur->name);
} while (xml_nextNode(cur));
/* Load array. */
ss_statsInit( &temp->stats_array );
ss_statsModFromList( &temp->stats_array, temp->stats, NULL );
/* Create description. */
if (temp->stats != NULL) {
temp->desc_stats = malloc( STATS_DESC_MAX );
i = ss_statsListDesc( temp->stats, temp->desc_stats, STATS_DESC_MAX, 0 );
if (i <= 0) {
free( temp->desc_stats );
temp->desc_stats = NULL;
}
}
continue;
}
/* Used by in-sanity and NSH utils, no in-game meaning. */
if (xml_isNode(node,"mission"))
continue;
DEBUG("Ship '%s' has unknown node '%s'.", temp->name, node->name);
} while (xml_nextNode(node));
/* Post processing. */
temp->dmg_absorb /= 100.;
temp->turn *= M_PI / 180.; /* Convert to rad. */
temp->thrust *= temp->mass;
/* Second pass default values for slot size. */
if ((temp->class == SHIP_CLASS_BULK_CARRIER) ||
(temp->class == SHIP_CLASS_CRUISER) ||
(temp->class == SHIP_CLASS_CARRIER) ||
(temp->class == SHIP_CLASS_MOTHERSHIP))
base_size = OUTFIT_SLOT_SIZE_HEAVY;
else if ((temp->class == SHIP_CLASS_CRUISE_SHIP) ||
(temp->class == SHIP_CLASS_FREIGHTER) ||
(temp->class == SHIP_CLASS_DESTROYER) ||
(temp->class == SHIP_CLASS_CORVETTE) ||
(temp->class == SHIP_CLASS_HEAVY_DRONE) ||
(temp->class == SHIP_CLASS_ARMOURED_TRANSPORT))
base_size = OUTFIT_SLOT_SIZE_MEDIUM;
else
base_size = OUTFIT_SLOT_SIZE_LIGHT;
for (i=0; i<temp->outfit_nweapon; i++)
if (temp->outfit_weapon[i].slot.size == OUTFIT_SLOT_SIZE_NA)
temp->outfit_weapon[i].slot.size = base_size;
for (i=0; i<temp->outfit_nutility; i++)
if (temp->outfit_utility[i].slot.size == OUTFIT_SLOT_SIZE_NA)
temp->outfit_utility[i].slot.size = base_size;
for (i=0; i<temp->outfit_nstructure; i++)
if (temp->outfit_structure[i].slot.size == OUTFIT_SLOT_SIZE_NA)
temp->outfit_structure[i].slot.size = base_size;
/* ship validator */
#define MELEMENT(o,s) if (o) WARN("Ship '%s' missing '"s"' element", temp->name)
MELEMENT(temp->name==NULL,"name");
MELEMENT(temp->base_type==NULL,"base_type");
MELEMENT(temp->gfx_space==NULL,"GFX");
MELEMENT(temp->gui==NULL,"GUI");
MELEMENT(temp->class==SHIP_CLASS_NULL,"class");
MELEMENT(temp->price==0,"price");
MELEMENT(temp->fabricator==NULL,"fabricator");
MELEMENT(temp->description==NULL,"description");
MELEMENT(temp->thrust==-1,"thrust");
MELEMENT(temp->turn==0,"turn");
MELEMENT(temp->speed==-1,"speed");
MELEMENT(temp->armour==0,"armour");
MELEMENT(temp->shield==0,"shield");
MELEMENT(temp->shield_regen==0,"shield_regen");
MELEMENT(temp->energy==0,"energy");
MELEMENT(temp->energy_regen==0,"energy_regen");
MELEMENT(temp->fuel==0,"fuel");
MELEMENT(temp->crew==0,"crew");
MELEMENT(temp->mass==0.,"mass");
MELEMENT(temp->cpu==0.,"cpu");
MELEMENT(temp->cap_cargo==0,"cap_cargo");
MELEMENT(temp->armour==0.,"armour");
/*MELEMENT(temp->thrust==0.,"thrust");
MELEMENT(temp->turn==0.,"turn");
MELEMENT(temp->speed==0.,"speed");
MELEMENT(temp->shield==0.,"shield");
MELEMENT(temp->shield_regen==0.,"shield_regen");
MELEMENT(temp->energy==0.,"energy");
MELEMENT(temp->energy_regen==0.,"energy_regen");
MELEMENT(temp->fuel==0.,"fuel");*/
MELEMENT(temp->crew==0,"crew");
MELEMENT(temp->mass==0.,"mass");
MELEMENT(temp->cap_cargo==0,"cargo");
#undef MELEMENT
return 0;
}
/**
* @brief Loads all the ships in the data files.
*
* @return 0 on success.
*/
int ships_load (void)
{
uint32_t bufsize, nfiles;
char *buf, **ship_files, *file;
int i, sl;
xmlNodePtr node;
xmlDocPtr doc;
/* Sanity. */
ss_check();
/* Initialize stack if needed. */
if (ship_stack == NULL) {
ship_stack = array_create(Ship);
}
ship_files = ndata_list( SHIP_DATA_PATH, &nfiles );
for (i=0; i<(int)nfiles; i++) {
/* Get the file name .*/
sl = strlen(SHIP_DATA_PATH)+strlen(ship_files[i])+1;
file = malloc( sl*sizeof(char) );
nsnprintf( file, sl, "%s%s", SHIP_DATA_PATH, ship_files[i] );
/* Load the XML. */
buf = ndata_read( file, &bufsize );
doc = xmlParseMemory( buf, bufsize );
free(file);
if (doc == NULL) {
free(buf);
WARN("%s file is invalid xml!",file);
continue;
}
node = doc->xmlChildrenNode; /* First ship node */
if (node == NULL) {
xmlFreeDoc(doc);
free(buf);
WARN("Malformed %s file: does not contain elements",file);
continue;
}
if (xml_isNode(node, XML_SHIP))
/* Load the ship. */
ship_parse( &array_grow(&ship_stack), node );
/* Clean up. */
xmlFreeDoc(doc);
free(buf);
}
/* Shrink stack. */
array_shrink(&ship_stack);
DEBUG("Loaded %d Ship%s", array_size(ship_stack), (array_size(ship_stack)==1) ? "" : "s" );
return 0;
}
/**
* @brief Frees all the ships.
*/
void ships_free (void)
{
Ship *s;
int i;
for (i = 0; i < array_size(ship_stack); i++) {
s = &ship_stack[i];
/* Free stored strings. */
free(s->name);
free(s->description);
free(s->gui);
free(s->base_type);
free(s->fabricator);
free(s->license);
free(s->desc_stats);
/* Free outfits. */
if (s->outfit_structure != NULL)
free(s->outfit_structure);
if (s->outfit_utility != NULL)
free(s->outfit_utility);
if (s->outfit_weapon != NULL)
free(s->outfit_weapon);
/* Free stats. */
if (s->stats != NULL)
ss_free( s->stats );
/* Free graphics. */
gl_freeTexture(s->gfx_space);
if (s->gfx_engine != NULL)
gl_freeTexture(s->gfx_engine);
if (s->gfx_target != NULL)
gl_freeTexture(s->gfx_target);
if (s->gfx_store != NULL)
gl_freeTexture(s->gfx_store);
free(s->gfx_comm);
}
array_free(ship_stack);
ship_stack = NULL;
}