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
role_bitmask.cpp
Go to the documentation of this file.
1 #include "role_bitmask.h"
2 #include "xml_support.h"
3 #include <gnuhash.h>
4 
5 #include "vs_globals.h"
6 #include "config_xml.h"
7 #include "vsfilesystem.h"
8 #include "csv.h"
9 using std::string;
10 using std::pair;
11 using namespace VSFileSystem;
12 
13 namespace ROLES
14 {
15 int discreteLog( int bitmask )
16 {
17  for (unsigned char i = 0; i < sizeof (int)*8; i++)
18  if ( bitmask&(1<<i) )
19  return i;
20  VSFileSystem::vs_fprintf( stderr, "undefined discrete log." );
21  return 0;
22 }
23 vector< vector< char > >buildroles();
24 
25 vector< vector< char > >& getAllRolePriorities()
26 {
27  static vector< vector< char > >allrolepriority = buildroles();
28  return allrolepriority;
29 }
30 vector< char >& getPriority( unsigned char rolerow )
31 {
32  if ( rolerow > getAllRolePriorities().size() ) {
33  VSFileSystem::vs_fprintf( stderr, "FATAL ERROR ROLE OUT OF RANGE" );
34  exit( 1 );
35  }
36  return getAllRolePriorities()[rolerow];
37 }
38 
39 static vsUMap< string, int > rolemap;
40 static vsUMap< int, string > irolemap;
41 
42 unsigned char InternalGetRole( const std::string &s )
43 {
44  vsUMap< string, int >::const_iterator i = rolemap.find( strtoupper( s ) );
45  if ( i != rolemap.end() )
46  return (*i).second;
47  return 0;
48 }
49 const std::string& InternalGetStrRole( unsigned char c )
50 {
51  static const std::string empty;
52 
53  vsUMap< int, string >::const_iterator i = irolemap.find( c );
54  if ( i != irolemap.end() )
55  return (*i).second;
56  return rolemap.size() ? rolemap.begin()->first : empty;
57 }
58 
59 vector< vector< string > >buildscripts()
60 {
61  vector< vector< string > >scripts;
63 
64  VSFile f;
65  VSError err = f.OpenReadOnly( "VegaEvents.csv", AiFile );
66  if (err <= Ok) {
67  int len = f.Size();
68  char *temp = (char*) malloc( len+1 );
69  memset( temp, 0, len+1 );
70  f.ReadLine( temp, len );
71  size_t siz = getAllRolePriorities().size();
72 
73  vector< string >vec = readCSV( temp );
74  if ( siz && getAllRolePriorities()[0].size() != vec.size() ) {
75  fprintf(
76  stderr,
77  "FATAL error in hash map... column %u in ai/VegaEvents.csv does not line up with that item in ai/VegaPriorities.csv\n",
78  (unsigned int) vec.size() );
79  }
80  if ( vec.size() ) vec.erase( vec.begin() );
81  for (unsigned int j = 0; j < vec.size(); j++)
82  if (getRole( vec[j] ) != j) {
83  fprintf(
84  stderr,
85  "FATAL error in hash map... column %d in ai/VegaEvents.csv does not line up with that item in ai/VegaPriorities.csv\n",
86  j );
87  }
88  unsigned int i = 0;
89  for (i = 0; i < siz; i++) {
90  scripts.push_back( vector< string > () );
91  for (unsigned int j = 0; j < vec.size(); j++)
92  scripts[i].push_back( "default" );
93  }
94  for (i = 0; i < vec.size(); i++) {
95  f.ReadLine( temp, len );
96  vector< string >strs = readCSV( temp );
97  if ( strs.size() ) {
98  string front = strs.front();
99  unsigned int scriptind = getRole( front );
100  while (scripts.size() <= scriptind)
101  scripts.push_back( vector< string > () );
102  for (unsigned int j = 1; j < strs.size() && j <= vec.size(); j++) {
103  unsigned int index = getRole( vec[j-1] );
104  while (scripts[scriptind].size() <= index)
105  scripts[scriptind].push_back( "default" );
106  scripts[scriptind][index] = strs[j];
107  }
108  }
109  }
110  free( temp );
111  f.Close();
112  }
113  return scripts;
114 }
115 const std::string& getRoleEvents( unsigned char ourrole, unsigned char theirs )
116 {
117  static vector< vector< string > >script = buildscripts();
118  const static string def = "default";
119  if ( ourrole >= script.size() ) {
120  VSFileSystem::vs_fprintf( stderr, "bad error with getRoleEvetnts (no event specified)" );
121  return def;
122  }
123  if ( theirs >= script[ourrole].size() ) {
124  VSFileSystem::vs_fprintf( stderr, "bad error || with getRoleEvetnts (no event specified)" );
125  return def;
126  }
127  return script[ourrole][theirs];
128 }
129 vector< vector< char > >buildroles()
130 {
131  vector< vector< char > >rolePriorities;
132  VSFile f;
133  VSError err = f.OpenReadOnly( "VegaPriorities.csv", AiFile );
134  if (err <= Ok) {
135  int len = f.Size();
136  char *temp = (char*) malloc( len+1 );
137  memset( temp, 0, len+1 );
138  f.ReadLine( temp, len );
139  vector< string >vec = readCSV( temp );
140  unsigned int i;
141  for (i = 1; i < vec.size(); i++) {
142  rolemap.insert( pair< string, int > ( strtoupper( vec[i] ), i-1 ) );
143  irolemap.insert( pair< int, string > ( i-1, strtoupper( vec[i] ) ) );
144  }
145  vector< vector< char > >tmprolepriorities;
146  vector< string >tmpnamelist;
147  while (f.ReadLine( temp, len ) == Ok) {
148  vector< string >priority = readCSV( temp );
149  if (priority.size() > 0) {
150  tmpnamelist.push_back( strtoupper( priority[0] ) );
151  tmprolepriorities.push_back( vector< char > () );
152  for (unsigned int j = 1; j < priority.size(); j++)
153  tmprolepriorities.back().push_back( XMLSupport::parse_int( priority[j] ) );
154  while ( tmprolepriorities.back().size() < vec.size() )
155  tmprolepriorities.back().push_back( 31 );
156  }
157  }
158  for (int k = 0; k < 2; ++k)
159  for (i = 0; i < tmpnamelist.size(); ++i) {
160  vsUMap< string, int >::iterator iter = rolemap.find( tmpnamelist[i] );
161  int j = -1;
162  if ( iter != rolemap.end() ) {
163  if (k == 0)
164  j = iter->second;
165  } else if (k == 1) {
166  for (j = 0; j < (int) rolePriorities.size(); ++j)
167  if (rolePriorities[j].size() == 0)
168  break;
169  rolemap[tmpnamelist[i]] = j;
170  irolemap[j] = tmpnamelist[i];
171  }
172  if (j != -1) {
173  while (rolePriorities.size() <= (unsigned int) j)
174  rolePriorities.push_back( vector< char > () );
175  rolePriorities[j].swap( tmprolepriorities[i] );
176  }
177  }
178  size_t a = rolePriorities.size();
179  if ( rolePriorities.size() ) {
180  size_t b = rolePriorities[0].size();
181  while (b > a) {
182  rolePriorities.push_back( rolePriorities[0] );
183  a++;
184  }
185  if (a > b)
186  for (size_t i = 0; i < rolePriorities.size(); ++i)
187  rolePriorities[i].resize( a );
188  //this is just to square out the table and make it safe to access with *any * input string
189  }
190  free( temp );
191  f.Close();
192  } else {
193  rolePriorities.push_back( vector< char > () );
194  rolePriorities[0].push_back( 0 );
195  }
196  return rolePriorities;
197 }
198 unsigned char getRole( const std::string &s )
199 {
200  return InternalGetRole( s );
201 }
202 const std::string& getRole( unsigned char c )
203 {
204  return InternalGetStrRole( c );
205 }
206 unsigned int readBitmask( const std::string &ss )
207 {
208  string s = ss;
209  std::string::size_type loc = string::npos;
210  int ans = 0;
211  do {
212  loc = s.find( " " );
213  ans |= ( 1<<getRole( s.substr( 0, loc ) ) );
214  if (loc != string::npos)
215  s = s.substr( loc+1 );
216  } while (loc != string::npos);
217  return ans;
218 }
219 unsigned int getCapitalRoles()
220 {
221  static string defaultcapshipvalues = vs_config->getVariable( "data",
222  "capship_roles",
223  "ESCORTCAP CAPITAL CARRIER BASE TROOP" );
224  unsigned int retval = 0;
225  string inp = defaultcapshipvalues;
226  string::size_type where;
227  while ( ( where = inp.find( " " ) ) != string::npos ) {
228  string tmp = inp.substr( 0, where );
229  unsigned char logrole = getRole( tmp );
230  if ( tmp == getRole( logrole ) )
231  retval |= (1<<logrole);
232  inp = inp.substr( where+1 );
233  }
234  if ( inp.length() ) {
235  unsigned char logrole = getRole( inp );
236  string tmp = getRole( logrole );
237  if (tmp == inp)
238  retval |= (1<<logrole);
239  }
240  return retval;
241 }
242 }
243