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
ROLES Namespace Reference

Functions

int discreteLog (int bitmask)
 
vector< vector< char > > buildroles ()
 
vector< vector< char > > & getAllRolePriorities ()
 
vector< char > & getPriority (unsigned char rolerow)
 
unsigned char InternalGetRole (const std::string &s)
 
const std::string & InternalGetStrRole (unsigned char c)
 
vector< vector< string > > buildscripts ()
 
const std::string & getRoleEvents (unsigned char ourrole, unsigned char theirs)
 
unsigned char getRole (const std::string &s)
 
const std::string & getRole (unsigned char c)
 
unsigned int readBitmask (const std::string &ss)
 
unsigned int getCapitalRoles ()
 
unsigned int maxRoleValue ()
 

Variables

static vsUMap< string, introlemap
 
static vsUMap< int, string > irolemap
 
const unsigned int FIRE_ONLY_AUTOTRACKERS = (1<<31)
 
const unsigned int FIRE_MISSILES = (1<<30)
 
const unsigned int FIRE_GUNS = (1<<29)
 
const unsigned int EVERYTHING_ELSE = ( ~(FIRE_MISSILES|FIRE_GUNS|FIRE_ONLY_AUTOTRACKERS) )
 

Function Documentation

vector< vector< char > > ROLES::buildroles ( )

Definition at line 129 of file role_bitmask.cpp.

References a, VSFileSystem::AiFile, b, VSFileSystem::VSFile::Close(), f, i, int, irolemap, j, k, VSFileSystem::Ok, VSFileSystem::VSFile::OpenReadOnly(), XMLSupport::parse_int(), readCSV(), VSFileSystem::VSFile::ReadLine(), rolemap, size, VSFileSystem::VSFile::Size(), and strtoupper().

Referenced by getAllRolePriorities().

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 }
vector< vector< string > > ROLES::buildscripts ( )

Definition at line 59 of file role_bitmask.cpp.

References VSFileSystem::AiFile, VSFileSystem::VSFile::Close(), f, fprintf, getAllRolePriorities(), getRole(), i, index, j, VSFileSystem::Ok, VSFileSystem::VSFile::OpenReadOnly(), readCSV(), VSFileSystem::VSFile::ReadLine(), size, and VSFileSystem::VSFile::Size().

Referenced by getRoleEvents().

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 }
int ROLES::discreteLog ( int  bitmask)

Definition at line 15 of file role_bitmask.cpp.

References i, int, and VSFileSystem::vs_fprintf().

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 }
std::vector< std::vector< char > > & ROLES::getAllRolePriorities ( )

Definition at line 25 of file role_bitmask.cpp.

References buildroles().

Referenced by buildscripts(), getPriority(), Universe::Init(), main(), and NetServer::start().

26 {
27  static vector< vector< char > >allrolepriority = buildroles();
28  return allrolepriority;
29 }
unsigned int ROLES::getCapitalRoles ( )

Definition at line 219 of file role_bitmask.cpp.

References getRole(), VegaConfig::getVariable(), and vs_config.

Referenced by UnitUtil::isCapitalShip().

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 }
std::vector< char > & ROLES::getPriority ( unsigned char  rolerow)

Definition at line 30 of file role_bitmask.cpp.

References getAllRolePriorities(), size, and VSFileSystem::vs_fprintf().

Referenced by TurretBin::AssignTargets(), Priority(), and ChooseTargetClass< numTuple >::ShouldTargetUnit().

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 }
const std::string & ROLES::getRole ( unsigned char  c)

Definition at line 202 of file role_bitmask.cpp.

References InternalGetStrRole().

203 {
204  return InternalGetStrRole( c );
205 }
const std::string & ROLES::getRoleEvents ( unsigned char  ourrole,
unsigned char  theirs 
)

Definition at line 115 of file role_bitmask.cpp.

References buildscripts(), size, and VSFileSystem::vs_fprintf().

Referenced by getProperScript().

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 }
unsigned char ROLES::InternalGetRole ( const std::string &  s)

Definition at line 42 of file role_bitmask.cpp.

References i, rolemap, and strtoupper().

Referenced by getRole().

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 }
const std::string& ROLES::InternalGetStrRole ( unsigned char  c)

Definition at line 49 of file role_bitmask.cpp.

References i, irolemap, and rolemap.

Referenced by getRole().

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 }
unsigned int ROLES::maxRoleValue ( )
unsigned int ROLES::readBitmask ( const std::string &  ss)

Definition at line 206 of file role_bitmask.cpp.

References getRole().

Referenced by BeamXML::beginElement().

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 }

Variable Documentation

const unsigned int ROLES::EVERYTHING_ELSE = ( ~(FIRE_MISSILES|FIRE_GUNS|FIRE_ONLY_AUTOTRACKERS) )
const unsigned int ROLES::FIRE_GUNS = (1<<29)
const unsigned int ROLES::FIRE_MISSILES = (1<<30)
const unsigned int ROLES::FIRE_ONLY_AUTOTRACKERS = (1<<31)

Definition at line 7 of file role_bitmask.h.

Referenced by Unit::Fire(), and FireBitmask().

vsUMap< int, string > ROLES::irolemap
static

Definition at line 40 of file role_bitmask.cpp.

Referenced by buildroles(), and InternalGetStrRole().

vsUMap< string, int > ROLES::rolemap
static

Definition at line 39 of file role_bitmask.cpp.

Referenced by buildroles(), InternalGetRole(), and InternalGetStrRole().