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
Faction Class Reference

#include <faction_generic.h>

Classes

struct  comm_face_t
 
struct  faction_stuff
 

Public Member Functions

void ParseAllies (unsigned int whichfaction)
 
 Faction ()
 
 ~Faction ()
 

Static Public Member Functions

static void ParseAllAllies ()
 Figures out the relationships of each faction with each other. More...
 
static void LoadXML (const char *factionfile, char *xmlbuffer=NULL, int buflength=0)
 
static void beginElement (void *userData, const XML_Char *name, const XML_Char **atts)
 
static void endElement (void *userData, const XML_Char *name)
 

Public Attributes

bool citizen
 
int playlist
 
float sparkcolor [4]
 
std::vector< faction_stufffaction
 
std::string logoName
 If logo==0, load from this one. More...
 
std::string logoAlphaName
 
std::string secondaryLogoName
 If secondaryLogo==0, load from this one. More...
 
std::string secondaryLogoAlphaName
 
Texturelogo
 Logos used by the ships of that faction. More...
 
TexturesecondaryLogo
 
char * factionname
 char * of the name More...
 
std::vector< comm_face_tcomm_faces
 
std::vector< boost::shared_ptr
< Animation > > 
explosion
 
std::vector< std::string > explosion_name
 
std::vector< unsigned char > comm_face_sex
 
MapStringFloat ship_relation_modifier
 
boost::shared_ptr< Unitcontraband
 

Detailed Description

Definition at line 16 of file faction_generic.h.

Constructor & Destructor Documentation

Faction::Faction ( )
inline

Definition at line 83 of file faction_generic.h.

References citizen, factionname, logo, playlist, secondaryLogo, and sparkcolor.

84  {
85  playlist = -1;
86  citizen = false;
87  logo = secondaryLogo = NULL;
88  factionname = NULL;
89  sparkcolor[0] = .5;
90  sparkcolor[1] = .5;
91  sparkcolor[2] = 1;
92  sparkcolor[3] = 1;
93  }
Faction::~Faction ( )

Definition at line 9 of file faction_util.cpp.

10 {
11  delete[] factionname;
12  if (contraband.get())
13  contraband->Kill();
14  delete logo;
15 }

Member Function Documentation

void Faction::beginElement ( void *  userData,
const XML_Char *  name,
const XML_Char **  atts 
)
static

Definition at line 93 of file faction_xml.cpp.

References CommXML::attribute_map, FactionXML::BASE_ONLY, FactionXML::COMM_ANIMATION, FactionXML::CONTRABAND, contrabandlists, FactionXML::CONVERSATION, FactionUtil::createAnimation(), FactionXML::DOCKABLE_ONLY, CommXML::element_map, FactionXML::ENEMY, FactionXML::EXPLOSION, FactionXML::FACTION, StarSystemGent::faction, factions, FactionXML::FACTIONS, FactionXML::FRIEND, FactionXML::ISCITIZEN, FactionXML::LOGOA, FactionXML::LOGORGB, XMLSupport::EnumMap::lookup(), FactionXML::MOOD_ANIMATION, CommXML::NAME, StarSystemGent::names, FactionUtil::neutralfac, XMLSupport::parse_bool(), XMLSupport::parse_float(), XMLSupport::parse_int(), FactionUtil::planetfac, FactionXML::RELATION, FactionXML::SECLOGOA, FactionXML::SECLOGORGB, FactionXML::SEX, FactionXML::SHIPMODIFIER, FactionXML::SPARKALPHA, FactionXML::SPARKBLUE, FactionXML::SPARKGREEN, FactionXML::SPARKRED, FactionXML::STATS, unitlevel, CommXML::UNKNOWN, and FactionUtil::upgradefac.

Referenced by LoadXML().

94 {
95  using namespace FactionXML;
96  AttributeList attributes = AttributeList( atts );
97  string name = names;
98  AttributeList::const_iterator iter;
99  Names elem = (Names) element_map.lookup( name );
100  std::string secString;
101  std::string secStringAlph;
102  switch (elem)
103  {
104  case UNKNOWN:
105  unitlevel++;
106  return;
107  case FACTIONS:
108  assert( unitlevel == 0 );
109  unitlevel++;
110  break;
111  case EXPLOSION:
112  assert( unitlevel == 2 );
113  unitlevel++;
114  for (iter = attributes.begin(); iter != attributes.end(); iter++) {
115  switch ( attribute_map.lookup( (*iter).name ) )
116  {
117  case NAME:
118  factions.back()->explosion.push_back( boost::shared_ptr<Animation>(FactionUtil::createAnimation( (*iter).value.c_str() )) );
119  factions.back()->explosion_name.push_back( (*iter).value );
120  break;
121  }
122  }
123  break;
124  case SHIPMODIFIER:
125  assert( unitlevel == 2 );
126  unitlevel++;
127  {
128  string name;
129  float rel = 0;
130  for (iter = attributes.begin(); iter != attributes.end(); iter++) {
131  switch ( attribute_map.lookup( (*iter).name ) )
132  {
133  case NAME:
134  name = (*iter).value;
135  break;
136  case RELATION:
137  rel = XMLSupport::parse_float( (*iter).value );
138  break;
139  }
140  }
141  if ( rel != 0 && name.length() )
142  factions.back()->ship_relation_modifier[name] = rel;
143  }
144  break;
145  case COMM_ANIMATION:
146  assert( unitlevel == 2 );
147  unitlevel++;
148  factions.back()->comm_faces.push_back( Faction::comm_face_t() );
149  factions.back()->comm_face_sex.push_back( 0 );
150  for (iter = attributes.begin(); iter != attributes.end(); iter++) {
151  switch ( attribute_map.lookup( (*iter).name ) )
152  {
153  case SEX:
154  factions.back()->comm_face_sex.back() = parse_int( (*iter).value );
155  break;
156  case DOCKABLE_ONLY:
157  factions.back()->comm_faces.back().dockable =
158  parse_bool( (*iter).value ) ? comm_face_t::CYES : comm_face_t::CNO;
159 
160  break;
161  case BASE_ONLY:
162  factions.back()->comm_faces.back().base = parse_bool( (*iter).value ) ? comm_face_t::CYES : comm_face_t::CNO;
163  break;
164  }
165  }
166  break;
167  case MOOD_ANIMATION:
168  assert( unitlevel == 3 );
169  unitlevel++;
170  for (iter = attributes.begin(); iter != attributes.end(); iter++) {
171  switch ( attribute_map.lookup( (*iter).name ) )
172  {
173  case NAME:
174  factions.back()->comm_faces.back().animations.push_back( FactionUtil::createAnimation( (*iter).value.c_str() ) );
175  break;
176  }
177  }
178  break;
179  case FACTION:
180  assert( unitlevel == 1 );
181  unitlevel++;
182  factions.push_back(boost::shared_ptr<Faction>(new Faction()));
183  assert( factions.size() > 0 );
184  contrabandlists.push_back( "" );
185  //factions[factions.size()-1];
186  for (iter = attributes.begin(); iter != attributes.end(); iter++) {
187  switch ( attribute_map.lookup( (*iter).name ) )
188  {
189  case SPARKRED:
190  factions.back()->sparkcolor[0] = XMLSupport::parse_float( (*iter).value );
191  break;
192  case SPARKGREEN:
193  factions.back()->sparkcolor[1] = XMLSupport::parse_float( (*iter).value );
194  break;
195  case SPARKBLUE:
196  factions.back()->sparkcolor[2] = XMLSupport::parse_float( (*iter).value );
197  break;
198  case SPARKALPHA:
199  factions.back()->sparkcolor[3] = XMLSupport::parse_float( (*iter).value );
200  break;
201  case NAME:
202  factions[factions.size()-1]->factionname = new char[strlen( (*iter).value.c_str() )+1];
203 
204  strcpy( factions[factions.size()-1]->factionname, (*iter).value.c_str() );
205  if (strcmp( factions[factions.size()-1]->factionname, "neutral" ) == 0)
207  if (strcmp( factions[factions.size()-1]->factionname, "planets" ) == 0)
208  FactionUtil::planetfac = factions.size()-1;
209  if (strcmp( factions[factions.size()-1]->factionname, "upgrades" ) == 0)
211  break;
212  case CONTRABAND:
213  contrabandlists.back() = ( (*iter).value );
214  break;
215  case ISCITIZEN:
216  factions.back()->citizen = XMLSupport::parse_bool( (*iter).value );
217  break;
218  case LOGORGB:
219  factions[factions.size()-1]->logoName = (*iter).value;
220  break;
221  case LOGOA:
222  factions[factions.size()-1]->logoAlphaName = (*iter).value;
223  break;
224  case SECLOGORGB:
225  factions[factions.size()-1]->secondaryLogoName = secString;
226  break;
227  case SECLOGOA:
228  factions[factions.size()-1]->secondaryLogoAlphaName = secStringAlph;
229  break;
230  }
231  }
232  break;
233  case STATS:
234  case FRIEND:
235  case ENEMY:
236  assert( unitlevel == 2 );
237  unitlevel++;
238  factions[factions.size()-1]->faction.push_back( faction_stuff() );
239  assert( factions.size() > 0 );
240  for (iter = attributes.begin(); iter != attributes.end(); iter++) {
241  switch ( attribute_map.lookup( (*iter).name ) )
242  {
243  case NAME:
244 
245  factions[factions.size()-1]->
246  faction[factions[factions.size()-1]->faction.size()-1].stats.name =
247  new char[strlen( (*iter).value.c_str() )+1];
248 
249  strcpy( factions[factions.size()-1]->faction[factions[factions.size()-1]->faction.size()-1].stats.name,
250  (*iter).value.c_str() );
251  break;
252  case RELATION:
253  factions[factions.size()
254  -1]->faction[factions[factions.size()-1]->faction.size()-1].relationship = parse_float(
255  (*iter).value );
256  break;
257  case CONVERSATION:
258  factions[factions.size()
259  -1]->faction[factions[factions.size()-1]->faction.size()-1].conversation.reset(new FSM( (*iter).value ));
260  break;
261  }
262  }
263  break;
264  default:
265  break;
266  }
267 }
void Faction::endElement ( void *  userData,
const XML_Char *  name 
)
static

Definition at line 269 of file faction_xml.cpp.

References CommXML::element_map, XMLSupport::EnumMap::lookup(), unitlevel, and CommXML::UNKNOWN.

Referenced by LoadXML().

270 {
271  using namespace FactionXML;
272  Names elem = (Names) element_map.lookup( name );
273  switch (elem)
274  {
275  case UNKNOWN:
276  unitlevel--;
277  break;
278  default:
279  unitlevel--;
280  break;
281  }
282 }
void Faction::LoadXML ( const char *  factionfile,
char *  xmlbuffer = NULL,
int  buflength = 0 
)
static

Definition at line 284 of file faction_xml.cpp.

References beginElement(), VSFileSystem::CommFile, endElement(), f, factions, i, j, FactionUtil::LoadSerializedFaction(), VSFileSystem::LookForFile(), VSFileSystem::Ok, unitlevel, and VSFileSystem::UnknownFile.

Referenced by Universe::LoadFactionXML().

285 {
286  using namespace FactionXML;
287  using namespace VSFileSystem;
288  using std::cout;
289  using std::endl;
290  using std::pair;
291  unitlevel = 0;
292  VSFile f;
293  VSError err;
294  if (buflength == 0 || xmlbuffer == NULL) {
295  cout<<"FactionXML:LoadXML "<<filename<<endl;
296  err = f.OpenReadOnly( filename, UnknownFile );
297  if (err > Ok) {
298  cout<<"Failed to open '"<<filename<<"' this probably means it can't find the data directory"<<endl;
299  assert( 0 );
300  }
301  }
302  XML_Parser parser = XML_ParserCreate( NULL );
303  XML_SetUserData( parser, NULL );
304  XML_SetElementHandler( parser, &Faction::beginElement, &Faction::endElement );
305  if (buflength != 0 && xmlbuffer != NULL)
306  XML_Parse( parser, xmlbuffer, buflength, 1 );
307  else
308  XML_Parse( parser, ( f.ReadFull() ).c_str(), f.Size(), 1 );
309  XML_ParserFree( parser );
310  ParseAllAllies();
311 
312  // Results are cached to avoid looking for too many files
313  typedef vsUMap< string, boost::shared_ptr<FSM> > Cache;
314  Cache cache;
315 
316  std::string fileSuffix(".xml");
317  std::string neutralName("neutral");
318  boost::shared_ptr<FSM> neutralComm;
319  neutralComm.reset(new FSM(neutralName + fileSuffix));
320  cache.insert(Cache::value_type(neutralName, neutralComm));
321 
322  for (unsigned int i = 0; i < factions.size(); i++)
323  {
324  boost::shared_ptr<Faction> fact = factions[i];
325  std::string myCommFile = fact->factionname + fileSuffix;
326  boost::shared_ptr<FSM> myComm;
327  Cache::iterator it = cache.find(myCommFile);
328  if ( it != cache.end() )
329  {
330  myComm = it->second;
331  }
332  else
333  {
334  if (VSFileSystem::LookForFile(myCommFile, CommFile) <= Ok)
335  {
336  myComm.reset(new FSM(myCommFile));
337  }
338  else
339  {
340  myComm.reset();
341  }
342  cache.insert(Cache::value_type(myCommFile, myComm));
343  }
344 
345  for (unsigned int j = 0; j < factions[i]->faction.size(); j++)
346  {
347  std::string jointCommFile = fact->factionname
348  + std::string("to")
349  + factions[j]->factionname
350  + fileSuffix;
351  boost::shared_ptr<FSM> jointComm;
352  if (!fact->faction[j].conversation)
353  {
354  it = cache.find(jointCommFile);
355  if ( it != cache.end() )
356  {
357  jointComm = it->second;
358  }
359  else
360  {
361  if (VSFileSystem::LookForFile(jointCommFile, CommFile) <= Ok)
362  {
363  jointComm.reset(new FSM(jointCommFile));
364  }
365  else
366  {
367  jointComm.reset();
368  }
369  cache.insert(Cache::value_type(jointCommFile, jointComm));
370  }
371  factions[i]->faction[j].conversation = jointComm
372  ? jointComm
373  : (myComm ? myComm : neutralComm);
374  }
375  }
376  }
377  char *munull = NULL;
378  cache.clear();
380 }
void Faction::ParseAllAllies ( )
static

Figures out the relationships of each faction with each other.

Definition at line 8 of file faction_generic.cpp.

References StarSystemGent::faction, factions, and i.

9 {
10  //MSVC has a bug where it won't let you reuse variables intitliezed in the paramater list of the for loop
11  //the work around is A.) make the scope of the variable in the function level or not to re-define it in subsequent loops
12  unsigned int i = 0;
13  for (i = 0; i < factions.size(); i++)
14  factions[i]->ParseAllies( i );
15  for (i = 0; i < factions.size(); i++)
16  factions[i]->faction[i].relationship = 1;
17 }
void Faction::ParseAllies ( unsigned int  whichfaction)

Definition at line 18 of file faction_generic.cpp.

References StarSystemGent::faction, factions, i, and j.

19 {
20  unsigned int i, j;
21  vector< faction_stuff >tempvec;
22  for (i = 0; i < faction.size(); i++)
23  for (j = 0; j < factions.size(); j++)
24  if (strcmp( faction[i].stats.name, factions[j]->factionname ) == 0) {
25  delete[] faction[i].stats.name;
26  faction[i].stats.index = j;
27  break;
28  }
29  for (i = 0; i < factions.size(); i++) {
30  tempvec.push_back( faction_stuff() );
31  tempvec[i].stats.index = i;
32  tempvec[i].relationship = ( (i == thisfaction) ? 1 : 0 );
33  }
34  for (i = 0; i < faction.size(); i++) {
35  faction_stuff::faction_name tmp = tempvec[faction[i].stats.index].stats;
36  tempvec[faction[i].stats.index] = faction[i];
37  tempvec[faction[i].stats.index].stats = tmp;
38  }
39  faction.swap( tempvec );
40 
41 }

Member Data Documentation

bool Faction::citizen

holds the relationships to all other factions loaded hold misguided info for self FIXME

Definition at line 45 of file faction_generic.h.

Referenced by Faction().

std::vector< unsigned char > Faction::comm_face_sex

Definition at line 73 of file faction_generic.h.

std::vector< comm_face_t > Faction::comm_faces

Definition at line 70 of file faction_generic.h.

boost::shared_ptr<Unit> Faction::contraband

Definition at line 76 of file faction_generic.h.

std::vector< boost::shared_ptr<Animation> > Faction::explosion

Definition at line 71 of file faction_generic.h.

std::vector< std::string > Faction::explosion_name

Definition at line 72 of file faction_generic.h.

std::vector< faction_stuff > Faction::faction

Definition at line 48 of file faction_generic.h.

char* Faction::factionname

char * of the name

Definition at line 60 of file faction_generic.h.

Referenced by Faction().

Texture* Faction::logo

Logos used by the ships of that faction.

Definition at line 56 of file faction_generic.h.

Referenced by Faction().

std::string Faction::logoAlphaName

Definition at line 51 of file faction_generic.h.

std::string Faction::logoName

If logo==0, load from this one.

Definition at line 50 of file faction_generic.h.

int Faction::playlist

Definition at line 46 of file faction_generic.h.

Referenced by Faction().

Texture* Faction::secondaryLogo

Definition at line 58 of file faction_generic.h.

Referenced by Faction().

std::string Faction::secondaryLogoAlphaName

Definition at line 54 of file faction_generic.h.

std::string Faction::secondaryLogoName

If secondaryLogo==0, load from this one.

Definition at line 53 of file faction_generic.h.

MapStringFloat Faction::ship_relation_modifier

Definition at line 74 of file faction_generic.h.

float Faction::sparkcolor[4]

Definition at line 47 of file faction_generic.h.

Referenced by Faction().


The documentation for this class was generated from the following files: