Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fg_util.cpp
Go to the documentation of this file.
1 #include "savegame.h"
2 #include "fg_util.h"
3 #include "faction_generic.h"
4 #include <algorithm>
5 
6 namespace fg_util
7 {
8 //
9 
10 void itoa( unsigned int dat, char *output )
11 {
12  if (dat == 0) {
13  *output++ = '0';
14  *output = '\0';
15  } else {
16  char *s = output;
17  while (dat) {
18  *s++ = '0'+dat%10;
19  dat /= 10;
20  }
21  *s = '\0';
22  std::reverse( output, s );
23  }
24 }
25 
26 std::string MakeFactionKey( int faction )
27 {
28  char output[16];
29  output[0] = 'F';
30  output[1] = 'F';
31  output[2] = ':';
32  itoa( faction, output+3 );
33  return std::string( output );
34 }
35 
36 bool IsFGKey( const std::string &fgcandidate )
37 {
38  if (fgcandidate.length() > 3 && fgcandidate[0] == 'F' && fgcandidate[1] == 'G' && fgcandidate[2] == ':') return true;
39  return false;
40 }
41 
42 static std::string gFG = "FG:";
43 
44 std::string MakeFGKey( const std::string &fgname, int faction )
45 {
46  char tmp[16];
47  tmp[0] = '|';
48  itoa( faction, tmp+1 );
49  return gFG+fgname+tmp;
50 }
51 
52 static std::string gSS = "SS:";
53 
54 std::string MakeStarSystemFGKey( const std::string &starsystem )
55 {
56  return gSS+starsystem;
57 }
58 
59 unsigned int ShipListOffset()
60 {
61  return 3;
62 }
63 
64 unsigned int PerShipDataSize()
65 {
66  return 3;
67 }
68 
69 bool CheckFG( std::vector< std::string > &data )
70 {
71  bool retval = false;
72  unsigned int leg = data.size();
73  unsigned int inc = PerShipDataSize();
74  for (unsigned int i = ShipListOffset()+1; i+1 < leg; i += inc) {
75  std::string *numlanded = &data[i+1];
76  std::string *numtotal = &data[i];
77  if (*numlanded != *numtotal) {
78  retval = true;
79  *numlanded = *numtotal;
80  }
81  }
82  return retval;
83 }
84 
85 bool CheckFG( SaveGame *sg, const std::string &fgname, unsigned int faction )
86 {
87  std::string key = MakeFGKey( fgname, faction );
88  return CheckFG( sg->getMissionStringData( key ) );
89 }
90 
91 void PurgeZeroShips( SaveGame *sg, unsigned int faction )
92 {
93  std::string key = MakeFactionKey( faction );
94  unsigned int len = sg->getMissionStringDataLength( key );
95  unsigned int i = 0;
96  while (i < len) {
97  CheckFG( sg, sg->getMissionStringData( key )[i] /*flightgroup*/, faction );
98  i += 1;
99  }
100 }
101 
103 {
104  for (unsigned int i = 0; i < factions.size(); ++i)
105  fg_util::PurgeZeroShips( sg, i );
106 }
107 
108 //
109 }
110