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
unit_csv.cpp File Reference
#include "unit_generic.h"
#include "csv.h"
#include "savegame.h"
#include "xml_serializer.h"
#include "gfx/sphere.h"
#include "unit_collide.h"
#include "collide2/Stdafx.h"
#include "collide2/CSopcodecollider.h"
#include "unit_factory.h"
#include "audiolib.h"
#include "unit_xml.h"
#include "gfx/quaternion.h"
#include "role_bitmask.h"
#include "unit_csv.h"
#include <algorithm>
#include "lin_time.h"
#include "unit_const_cache.h"

Go to the source code of this file.

Classes

struct  SubUnitStruct
 

Macros

#define VS_PI   3.1415926535897931
 
#define LOADROW_OPTIMIZER   ((0x348299ab))
 
#define FORCE_OPTIMIZER   (1)
 
#define OPTIMIZER_INDEX(Variable)   OPTIDX_##Variable
 
#define INIT_OPTIMIZER(keys, Variable)   do{OPTIMIZER_INDEX(Variable)=(keys.push_back(#Variable),(keys.size()-1));}while(0)
 
#define DEF_OPTIMIZER(Variable)   static unsigned int OPTIMIZER_INDEX( Variable ) = CSVTable::optimizer_undefined;
 
#define OPTIM_GET(row, table, variable)
 

Functions

CSVRow LookupUnitRow (const string &unitname, const string &faction)
 
int GetModeFromName (const char *input_buffer)
 
void pushMesh (std::vector< Mesh * > &mesh, float &randomstartframe, float &randomstartseconds, const char *filename, const float scale, int faction, class Flightgroup *fg, int startframe, double texturestarttime)
 
void addShieldMesh (Unit::XML *xml, const char *filename, const float scale, int faction, class Flightgroup *fg)
 
void addRapidMesh (Unit::XML *xml, const char *filename, const float scale, int faction, class Flightgroup *fg)
 
static void UpgradeUnit (Unit *un, const std::string &upgrades)
 
void AddMeshes (std::vector< Mesh * > &xmeshes, float &randomstartframe, float &randomstartseconds, float unitscale, const std::string &meshes, int faction, Flightgroup *fg, vector< unsigned int > *counts)
 
static std::pair
< string::size_type,
string::size_type > 
nextElementRange (const string &inp, string::size_type &start, string::size_type end)
 
static string nextElementString (const string &inp, string::size_type &start, string::size_type end)
 
static int nextElementInt (const string &inp, string::size_type &start, string::size_type end, int def=0)
 
static double nextElementFloat (const string &inp, string::size_type &start, string::size_type end, double def=0)
 
static double nextElementBool (const string &inp, string::size_type &start, string::size_type end, bool def=false)
 
static string nextElement (string &inp)
 
static bool stob (const string &inp, bool defaul)
 
static double stof (const string &inp, double def=0)
 
static int stoi (const string &inp, int def=0)
 
bool CheckAccessory (Unit *)
 
int parseMountSizes (const char *str)
 
static void AddMounts (Unit *thus, Unit::XML &xml, const std::string &mounts)
 
static vector< SubUnitStructGetSubUnits (const std::string &subunits)
 
static void AddSubUnits (Unit *thus, Unit::XML &xml, const std::string &subunits, int faction, const std::string &modification)
 
void AddDocks (Unit *thus, Unit::XML &xml, const string &docks)
 
void AddLights (Unit *thus, Unit::XML &xml, const string &lights)
 
static void ImportCargo (Unit *thus, const string &imports)
 
static void AddCarg (Unit *thus, const string &cargos)
 
void HudDamage (float *dam, const string &damages)
 
string WriteHudDamage (Unit *un)
 
string WriteHudDamageFunc (Unit *un)
 
void AddSounds (Unit *thus, string sounds)
 
void LoadCockpit (Unit *thus, const string &cockpit)
 
static int AssignIf (const string &inp, float &val, float &val1, float &val2)
 
static int AssignIfDeg (const string &inp, float &val)
 
float getFuelConversion ()
 
const std::string EMPTY_STRING ("")
 
CSVRow GetUnitRow (string filename, bool subu, int faction, bool readlast, bool &rread)
 
static void mapToStringVec (vsUMap< string, string >a, vector< string > &key, vector< string > &value)
 
static string tos (float val)
 
static string tos (double val)
 
static string tos (unsigned int val)
 
static string tos (bool val)
 
static string tos (int val)
 
void UpdateMasterPartList (Unit *ret)
 

Macro Definition Documentation

#define DEF_OPTIMIZER (   Variable)    static unsigned int OPTIMIZER_INDEX( Variable ) = CSVTable::optimizer_undefined;

Definition at line 672 of file unit_csv.cpp.

Referenced by Unit::LoadRow().

#define FORCE_OPTIMIZER   (1)

Definition at line 666 of file unit_csv.cpp.

#define INIT_OPTIMIZER (   keys,
  Variable 
)    do{OPTIMIZER_INDEX(Variable)=(keys.push_back(#Variable),(keys.size()-1));}while(0)

Definition at line 670 of file unit_csv.cpp.

Referenced by Unit::LoadRow().

#define LOADROW_OPTIMIZER   ((0x348299ab))

Definition at line 663 of file unit_csv.cpp.

Referenced by Unit::LoadRow().

#define OPTIM_GET (   row,
  table,
  variable 
)
Value:
(( \
(table->optimizer_indexes[OPTIMIZER_INDEX(variable)] == CSVTable::optimizer_undefined) \
: row[ table->optimizer_indexes[OPTIMIZER_INDEX(variable)] ] \
))

Definition at line 676 of file unit_csv.cpp.

Referenced by Unit::LoadRow().

#define OPTIMIZER_INDEX (   Variable)    OPTIDX_##Variable

Definition at line 668 of file unit_csv.cpp.

#define VS_PI   3.1415926535897931

Definition at line 20 of file unit_csv.cpp.

Referenced by AssignIfDeg(), GetSubUnits(), Unit::LoadRow(), and Unit::WriteUnitString().

Function Documentation

static void AddCarg ( Unit thus,
const string &  cargos 
)
static

Definition at line 499 of file unit_csv.cpp.

References Unit::AddCargo(), UnitImages< BOGUS >::cargo, Cargo::category, Cargo::content, Cargo::description, Cargo::functionality, Cargo::installed, Cargo::mass, Cargo::maxfunctionality, Cargo::mission, nextElementBool(), nextElementFloat(), nextElementInt(), nextElementString(), Unit::pImage, Cargo::price, Cargo::quantity, and Cargo::volume.

Referenced by Unit::LoadRow().

500 {
501  string::size_type where, when, ofs = 0;
502  {
503  int nelem = 0;
504  while ( ( ofs = cargos.find( '{', ofs ) ) != string::npos )
505  nelem++, ofs++;
506  thus->pImage->cargo.reserve( nelem+thus->pImage->cargo.size() );
507  ofs = 0;
508  }
509  while ( ( where = cargos.find( '{', ofs ) ) != string::npos ) {
510  if ( ( when = cargos.find( '}', where+1 ) ) != string::npos ) {
511  Cargo carg;
512  string::size_type elemstart = where+1, elemend = when;
513  ofs = when+1;
514 
515  carg.content = nextElementString( cargos, elemstart, elemend );
516  carg.category = nextElementString( cargos, elemstart, elemend );
517  carg.price = nextElementFloat( cargos, elemstart, elemend );
518  carg.quantity = nextElementInt( cargos, elemstart, elemend );
519  carg.mass = nextElementFloat( cargos, elemstart, elemend );
520  carg.volume = nextElementFloat( cargos, elemstart, elemend );
521  carg.functionality = nextElementFloat( cargos, elemstart, elemend );
522  carg.maxfunctionality = nextElementFloat( cargos, elemstart, elemend );
523  carg.description = nextElementString( cargos, elemstart, elemend );
524  carg.mission = nextElementBool( cargos, elemstart, elemend, false );
525  carg.installed = nextElementBool( cargos, elemstart, elemend,
526  carg.category.get().find("upgrades/") == 0 );
527 
528  thus->AddCargo( carg, false );
529  } else {
530  ofs = string::npos;
531  }
532  }
533 }
void AddDocks ( Unit thus,
Unit::XML xml,
const string &  docks 
)

Definition at line 400 of file unit_csv.cpp.

References UnitImages< BOGUS >::dockingports, VegaConfig::getVariable(), i, nextElementFloat(), nextElementInt(), XMLSupport::parse_int(), Unit::pImage, QVector, SERVER, size, Unit::XML::unitscale, and vs_config.

Referenced by Unit::LoadRow().

401 {
402  string::size_type where, when;
403  string::size_type ofs = 0;
404  int overlap = 1;
405  if (SERVER)
406  //NETFIXME: Shouldn't need to know this option on client side, but server references docking port by absolute number.
407  overlap = XMLSupport::parse_int( vs_config->getVariable( "server", "players_per_docking_port", "10" ) );
408  {
409  int nelem = 0;
410  while ( ( ofs = docks.find( '{', ofs ) ) != string::npos )
411  nelem++, ofs++;
412  thus->pImage->dockingports.reserve( nelem*overlap+thus->pImage->dockingports.size() );
413  ofs = 0;
414  }
415  while ( ( where = docks.find( '{', ofs ) ) != string::npos ) {
416  if ( ( when = docks.find( '}', where+1 ) ) != string::npos ) {
417  string::size_type elemstart = where+1, elemend = when;
418  ofs = when+1;
419 
420  QVector pos = QVector( 0, 0, 0 );
421  int type = nextElementInt( docks, elemstart, elemend );
422  pos.i = nextElementFloat( docks, elemstart, elemend );
423  pos.j = nextElementFloat( docks, elemstart, elemend );
424  pos.k = nextElementFloat( docks, elemstart, elemend );
425  double size = nextElementFloat( docks, elemstart, elemend );
426  double minsize = nextElementFloat( docks, elemstart, elemend );
427  for (int i = 0; i < overlap; i++)
428  thus->pImage->dockingports.push_back( DockingPorts( pos.Cast()*xml.unitscale, size*xml.unitscale, minsize
429  *xml.unitscale, DockingPorts::Type::Value(type) ) );
430  } else {
431  ofs = string::npos;
432  }
433  }
434 }
void AddLights ( Unit thus,
Unit::XML xml,
const string &  lights 
)

Definition at line 436 of file unit_csv.cpp.

References GFXColor::a, Unit::addHalo(), GFXColor::b, GFXColor::g, VegaConfig::getVariable(), nextElementFloat(), nextElementString(), XMLSupport::parse_float(), QVector, GFXColor::r, Unit::XML::unitscale, and vs_config.

Referenced by Unit::LoadRow().

437 {
438  static float default_halo_activation =
439  XMLSupport::parse_float( vs_config->getVariable( "graphics", "default_engine_activation", ".00048828125" ) );
440  string::size_type where, when;
441  string::size_type ofs = 0;
442  while ( ( where = lights.find( '{', ofs ) ) != string::npos ) {
443  if ( ( when = lights.find( '}', where+1 ) ) != string::npos ) {
444  string::size_type elemstart = where+1, elemend = when;
445  ofs = when+1;
446 
447  string filename = nextElementString( lights, elemstart, elemend );
448  QVector pos, scale;
449  GFXColor halocolor;
450  pos.i = nextElementFloat( lights, elemstart, elemend );
451  pos.j = nextElementFloat( lights, elemstart, elemend );
452  pos.k = nextElementFloat( lights, elemstart, elemend );
453  scale.i = xml.unitscale*nextElementFloat( lights, elemstart, elemend, 1 );
454  scale.j = scale.k = scale.i;
455  halocolor.r = nextElementFloat( lights, elemstart, elemend, 1 );
456  halocolor.g = nextElementFloat( lights, elemstart, elemend, 1 );
457  halocolor.b = nextElementFloat( lights, elemstart, elemend, 1 );
458  halocolor.a = nextElementFloat( lights, elemstart, elemend, 1 );
459  double act_speed = nextElementFloat( lights, elemstart, elemend, default_halo_activation );
460 
461  thus->addHalo( filename.c_str(), pos*xml.unitscale, scale.Cast(), halocolor, "", act_speed );
462  } else {
463  ofs = string::npos;
464  }
465  }
466 }
void AddMeshes ( std::vector< Mesh * > &  xmeshes,
float randomstartframe,
float randomstartseconds,
float  unitscale,
const std::string &  meshes,
int  faction,
Flightgroup fg,
vector< unsigned int > *  counts 
)

Definition at line 84 of file unit_csv.cpp.

References StarSystemGent::faction, and pushMesh().

Referenced by Unit::LoadRow(), and GameUnit< UnitType >::Split().

92 {
93  string::size_type where, when, wheresf, wherest, ofs = 0;
94  if (counts) counts->clear();
95  {
96  int nelem = 0;
97  while ( ( ofs = meshes.find( '{', ofs ) ) != string::npos )
98  nelem++, ofs++;
99  if (counts) counts->reserve( nelem );
100  xmeshes.reserve( nelem );
101  ofs = 0;
102  }
103  while ( ( where = meshes.find( '{', ofs ) ) != string::npos ) {
104  when = meshes.find( '}', where+1 ); //matching closing brace
105  string mesh = meshes.substr( where+1, ( (when == string::npos) ? string::npos : when-where-1 ) );
106  ofs = ( (when == string::npos) ? string::npos : when+1 );
107 
108  wheresf = mesh.find( ';' );
109  string startf = "0";
110  string startt = "0";
111  if (wheresf != string::npos) {
112  startf = mesh.substr( wheresf+1 );
113  mesh = mesh.substr( 0, wheresf );
114  wherest = startf.find( ';' );
115  if (wherest != string::npos) {
116  startt = startf.substr( wherest+1 );
117  startf = startf.substr( 0, wherest );
118  }
119  }
120  int startframe = startf == "RANDOM" ? -1 : ( startf == "ASYNC" ? -1 : atoi( startf.c_str() ) );
121  float starttime = startt == "RANDOM" ? -1.0f : atof( startt.c_str() );
122  unsigned int s = xmeshes.size();
123  pushMesh( xmeshes, randomstartframe, randomstartseconds, mesh.c_str(), unitscale, faction, fg, startframe, starttime );
124  if (counts) counts->push_back( xmeshes.size()-s );
125  }
126 }
static void AddMounts ( Unit thus,
Unit::XML xml,
const std::string &  mounts 
)
static

Definition at line 209 of file unit_csv.cpp.

References a, AUDCreateSound(), b, Quaternion::from_vectors(), Unit::GetNumMounts(), VegaConfig::getVariable(), int, Unit::mounts, nextElementBool(), nextElementFloat(), nextElementInt(), nextElementString(), weapon_info::NOWEAP, XMLSupport::parse_bool(), parseMountSizes(), weapon_info::PROJECTILE, QVector, R, Mount::SetMountOrientation(), Mount::SetMountPosition(), weapon_info::size, Mount::size, Mount::type, Unit::XML::unitscale, volume, and vs_config.

Referenced by Unit::LoadRow().

210 {
211  string::size_type where, when, ofs = 0;
212  unsigned int first_new_mount = thus->mounts.size();
213  {
214  int nmountz = 0;
215  while ( ( ofs = mounts.find( '{', ofs ) ) != string::npos )
216  nmountz++, ofs++;
217  thus->mounts.reserve( nmountz+thus->mounts.size() );
218  ofs = 0;
219  }
220  while ( ( where = mounts.find( '{', ofs ) ) != string::npos ) {
221  if ( ( when = mounts.find( '}', where+1 ) ) != string::npos ) {
222  string::size_type elemstart = where+1, elemend = when;
223  ofs = when+1;
224 
225  QVector P;
226  QVector Q = QVector( 0, 1, 0 );
227  QVector R = QVector( 0, 0, 1 );
228  QVector pos = QVector( 0, 0, 0 );
229 
230  string filename = nextElementString( mounts, elemstart, elemend );
231  int ammo = nextElementInt( mounts, elemstart, elemend, -1 );
232  int volume = nextElementInt( mounts, elemstart, elemend );
233  string mountsize = nextElementString( mounts, elemstart, elemend );
234  pos.i = nextElementFloat( mounts, elemstart, elemend );
235  pos.j = nextElementFloat( mounts, elemstart, elemend );
236  pos.k = nextElementFloat( mounts, elemstart, elemend );
237  double xyscale = nextElementFloat( mounts, elemstart, elemend );
238  double zscale = nextElementFloat( mounts, elemstart, elemend );
239  R.i = nextElementFloat( mounts, elemstart, elemend );
240  R.j = nextElementFloat( mounts, elemstart, elemend );
241  R.k = nextElementFloat( mounts, elemstart, elemend, 1 );
242  Q.i = nextElementFloat( mounts, elemstart, elemend );
243  Q.j = nextElementFloat( mounts, elemstart, elemend, 1 );
244  Q.k = nextElementFloat( mounts, elemstart, elemend );
245  float func = nextElementFloat( mounts, elemstart, elemend, 1 );
246  float maxfunc = nextElementFloat( mounts, elemstart, elemend, 1 );
247  bool banked = nextElementBool( mounts, elemstart, elemend, false );
248  Q.Normalize();
249  if ( fabs( Q.i ) == fabs( R.i ) && fabs( Q.j ) == fabs( R.j ) && fabs( Q.k ) == fabs( R.k ) ) {
250  Q.i = -1;
251  Q.j = 0;
252  Q.k = 0;
253  }
254  R.Normalize();
255 
256  CrossProduct( Q, R, P );
257  CrossProduct( R, P, Q );
258  Q.Normalize();
259  Mount mnt( filename, ammo, volume, xml.unitscale*xyscale, xml.unitscale*zscale, func, maxfunc, banked );
260  mnt.SetMountOrientation( Quaternion::from_vectors( P.Cast(), Q.Cast(), R.Cast() ) );
261  mnt.SetMountPosition( xml.unitscale*pos.Cast() );
262  int mntsiz = weapon_info::NOWEAP;
263  if ( mountsize.length() ) {
264  mntsiz = parseMountSizes( mountsize.c_str() );
265  mnt.size = mntsiz;
266  } else {
267  mnt.size = mnt.type->size;
268  }
269  thus->mounts.push_back( mnt );
270  } else {ofs = string::npos; }}
271  unsigned char parity = 0;
272  for (unsigned int a = first_new_mount; a < thus->mounts.size(); ++a) {
273  static bool half_sounds = XMLSupport::parse_bool( vs_config->getVariable( "audio", "every_other_mount", "false" ) );
274  if ( (a&1) == parity ) {
275  int b = a;
276  if ( (a&3) == 2 && (int) a < (thus->GetNumMounts()-1) ) {
277  if (thus->mounts[a].type->type != weapon_info::PROJECTILE
278  && thus->mounts[a+1].type->type != weapon_info::PROJECTILE)
279  {
280  b = a+1;
281  }
282  }
283  thus->mounts[b].sound = AUDCreateSound( thus->mounts[b].type->sound, false );
284  } else if ( (!half_sounds) || thus->mounts[a].type->type == weapon_info::PROJECTILE ) {
285  thus->mounts[a].sound = AUDCreateSound( thus->mounts[a].type->sound, false );
286  }
287  if (a > 0)
288  if (thus->mounts[a].sound == thus->mounts[a-1].sound && thus->mounts[a].sound != -1)
289  printf( "Sound error\n" );
290  }
291 }
void addRapidMesh ( Unit::XML xml,
const char *  filename,
const float  scale,
int  faction,
class Flightgroup fg 
)

Definition at line 464 of file unit_xml.cpp.

Referenced by Unit::LoadRow().

465 {
466  xml->rapidmesh = Mesh::LoadMesh( filename, Vector( scale, scale, scale ), faction, fg );
467 }
void addShieldMesh ( Unit::XML xml,
const char *  filename,
const float  scale,
int  faction,
class Flightgroup fg 
)

Definition at line 453 of file unit_xml.cpp.

Referenced by Unit::LoadRow().

454 {
455  static bool forceit = XMLSupport::parse_bool( vs_config->getVariable( "graphics", "forceOneOneShieldBlend", "true" ) );
456  xml->shieldmesh = Mesh::LoadMesh( filename, Vector( scale, scale, scale ), faction, fg );
457  if (xml->shieldmesh && forceit) {
458  xml->shieldmesh->SetBlendMode( ONE, ONE, true );
459  xml->shieldmesh->setEnvMap( false, true );
460  xml->shieldmesh->setLighting( true, true );
461  }
462 }
void AddSounds ( Unit thus,
string  sounds 
)

Definition at line 571 of file unit_csv.cpp.

References UnitSounds::armor, AUDCreateSound(), AUDCreateSoundWAV(), UnitSounds::cloak, UnitSounds::engine, UnitSounds::explode, VegaConfig::getVariable(), UnitSounds::hull, UnitSounds::jump, nextElement(), UnitSounds::shield, Unit::sound, and vs_config.

Referenced by Unit::LoadRow().

572 {
573  if (sounds.length() != 0) {
574  string tmp = nextElement( sounds );
575  if ( tmp.length() )
576  thus->sound->shield = AUDCreateSoundWAV( tmp, false );
577  tmp = nextElement( sounds );
578  if ( tmp.length() )
579  thus->sound->armor = AUDCreateSoundWAV( tmp, false );
580  tmp = nextElement( sounds );
581  if ( tmp.length() )
582  thus->sound->hull = AUDCreateSoundWAV( tmp, false );
583  tmp = nextElement( sounds );
584  if ( tmp.length() )
585  thus->sound->jump = AUDCreateSoundWAV( tmp, false );
586  tmp = nextElement( sounds );
587  if ( tmp.length() )
588  thus->sound->explode = AUDCreateSoundWAV( tmp, false );
589  tmp = nextElement( sounds );
590  if ( tmp.length() )
591  thus->sound->cloak = AUDCreateSoundWAV( tmp, false );
592  tmp = nextElement( sounds );
593  if ( tmp.length() )
594  thus->sound->engine = AUDCreateSoundWAV( tmp, true );
595  }
596  if (thus->sound->cloak == -1) {
597  static std::string ssound = vs_config->getVariable( "unitaudio", "cloak", "sfx43.wav" );
598  thus->sound->cloak = AUDCreateSound( ssound, false );
599  }
600  if (thus->sound->engine == -1) {
601  static std::string ssound = vs_config->getVariable( "unitaudio", "afterburner", "sfx10.wav" );
602  thus->sound->engine = AUDCreateSound( ssound, false );
603  }
604  if (thus->sound->shield == -1) {
605  static std::string ssound = vs_config->getVariable( "unitaudio", "shield", "sfx09.wav" );
606  thus->sound->shield = AUDCreateSound( ssound, false );
607  }
608  if (thus->sound->armor == -1) {
609  static std::string ssound = vs_config->getVariable( "unitaudio", "armor", "sfx08.wav" );
610  thus->sound->armor = AUDCreateSound( ssound, false );
611  }
612  if (thus->sound->hull == -1) {
613  static std::string ssound = vs_config->getVariable( "unitaudio", "armor", "sfx08.wav" );
614  thus->sound->hull = AUDCreateSound( ssound, false );
615  }
616  if (thus->sound->explode == -1) {
617  static std::string ssound = vs_config->getVariable( "unitaudio", "explode", "explosion.wav" );
618  thus->sound->explode = AUDCreateSound( ssound, false );
619  }
620  if (thus->sound->jump == -1) {
621  static std::string ssound = vs_config->getVariable( "unitaudio", "explode", "sfx43.wav" );
622  thus->sound->jump = AUDCreateSound( ssound, false );
623  }
624 }
static void AddSubUnits ( Unit thus,
Unit::XML xml,
const std::string &  subunits,
int  faction,
const std::string &  modification 
)
static

Definition at line 348 of file unit_csv.cpp.

References a, CheckAccessory(), UnitFactory::createUnit(), StarSystemGent::faction, float_to_int(), GetSubUnits(), Unit::graphicOptions, i, Unit::isSubUnit(), Network, UnitCollection::prepend(), QVector, R, SERVER, Unit::graphic_options::SubUnit, Unit::SubUnits, Unit::XML::units, and Unit::XML::unitscale.

Referenced by Unit::LoadRow().

349 {
350  if (SERVER || Network) {
351  //Semihack: Keep loading if thus is already a subunit...
352  //A planet can have a wormhole subunit, which itself has more subunits.
353  if (!thus->graphicOptions.SubUnit)
354  return; //subvert all subunits in MP
355  }
356  vector< SubUnitStruct >su = GetSubUnits( subunits );
357  xml.units.reserve( subunits.size()+xml.units.size() );
358  for (vector< SubUnitStruct >::iterator i = su.begin(); i != su.end(); ++i) {
359  string filename = (*i).filename;
360  QVector pos = (*i).pos;
361  QVector Q = (*i).Q;
362  QVector R = (*i).R;
363  double restricted = (*i).restricted;
364  xml.units.push_back( UnitFactory::createUnit( filename.c_str(), true, faction, modification, NULL ) ); //I set here the fg arg to NULL
365  if (xml.units.back()->name == "LOAD_FAILED") {
366  xml.units.back()->limits.yaw = 0;
367  xml.units.back()->limits.pitch = 0;
368  xml.units.back()->limits.roll = 0;
369  xml.units.back()->limits.lateral = xml.units.back()->limits.retro = xml.units.back()->limits.forward =
370  xml.units.back()->limits.afterburn = 0.0;
371  }
372  if ( !thus->isSubUnit() ) //Useless to set recursive owner in subunits - as parent will do the same
373  xml.units.back()->SetRecursiveOwner( thus );
374  xml.units.back()->SetOrientation( Q, R );
375  R.Normalize();
376  xml.units.back()->prev_physical_state = xml.units.back()->curr_physical_state;
377  xml.units.back()->SetPosition( pos*xml.unitscale );
378  xml.units.back()->limits.structurelimits = R.Cast();
379  xml.units.back()->limits.limitmin = restricted;
380  xml.units.back()->name = filename;
381  if (xml.units.back()->pImage->unitwriter != NULL)
382  xml.units.back()->pImage->unitwriter->setName( filename );
383  CheckAccessory( xml.units.back() ); //turns on the ceerazy rotation for the turr
384  }
385  for (int a = xml.units.size()-1; a >= 0; a--) {
386  bool randomspawn = xml.units[a]->name.get().find( "randomspawn" ) != string::npos;
387  if (randomspawn) {
388  int chancetospawn = float_to_int( xml.units[a]->WarpCapData() );
389  if (chancetospawn > rand()%100)
390  thus->SubUnits.prepend( xml.units[a] );
391 
392  else
393  xml.units[a]->Kill();
394  } else {
395  thus->SubUnits.prepend( xml.units[a] );
396  }
397  }
398 }
static int AssignIf ( const string &  inp,
float val,
float val1,
float val2 
)
static

Definition at line 635 of file unit_csv.cpp.

References stof().

Referenced by Unit::LoadRow().

636 {
637  if ( inp.length() ) {
638  val = ::stof( inp );
639  val1 = ::stof( inp );
640  val2 = ::stof( inp );
641  return 1;
642  }
643  return 0;
644 }
static int AssignIfDeg ( const string &  inp,
float val 
)
static

Definition at line 646 of file unit_csv.cpp.

References stof(), and VS_PI.

Referenced by Unit::LoadRow().

647 {
648  if ( inp.length() ) {
649  val = ::stof( inp )*VS_PI/180;
650  return 1;
651  }
652  return 0;
653 }
bool CheckAccessory ( Unit )

Definition at line 1348 of file unit_generic.cpp.

1349 {
1350  bool accessory = tur->name.get().find( "accessory" ) != string::npos;
1351  if (accessory) {
1352  tur->SetAngularVelocity( tur->DownCoordinateLevel( Vector( tur->GetComputerData().max_pitch_up,
1353  tur->GetComputerData().max_yaw_right,
1354  tur->GetComputerData().max_roll_right ) ) );
1355  }
1356  return accessory;
1357 }
const std::string EMPTY_STRING ( ""  )
float getFuelConversion ( )

Definition at line 655 of file unit_csv.cpp.

References VegaConfig::getVariable(), XMLSupport::parse_float(), and vs_config.

656 {
657  static float fuel_conversion = XMLSupport::parse_float( vs_config->getVariable( "physics", "FuelConversion", ".00144" ) );
658  return fuel_conversion;
659 }
int GetModeFromName ( const char *  input_buffer)

Definition at line 87 of file unit_xml.cpp.

88 {
89  if (strlen( input_buffer ) > 3) {
90  if (input_buffer[0] == 'a'
91  && input_buffer[1] == 'd'
92  && input_buffer[2] == 'd')
93  return 1;
94  if (input_buffer[0] == 'm'
95  && input_buffer[1] == 'u'
96  && input_buffer[2] == 'l')
97  return 2;
98  }
99  return 0;
100 }
static vector< SubUnitStruct > GetSubUnits ( const std::string &  subunits)
static

Definition at line 310 of file unit_csv.cpp.

References UniverseUtil::cos(), nextElementFloat(), nextElementString(), QVector, R, and VS_PI.

Referenced by AddSubUnits(), and Unit::WriteUnitString().

311 {
312  string::size_type where, when, ofs = 0;
313  vector< SubUnitStruct >ret;
314  {
315  int nelem = 0;
316  while ( ( ofs = subunits.find( '{', ofs ) ) != string::npos )
317  nelem++, ofs++;
318  ret.reserve( nelem );
319  ofs = 0;
320  }
321  while ( ( where = subunits.find( '{', ofs ) ) != string::npos ) {
322  if ( ( when = subunits.find( '}', ofs ) ) != string::npos ) {
323  string::size_type elemstart = where+1, elemend = when;
324  ofs = when+1;
325 
326  QVector pos, Q, R;
327 
328  string filename = nextElementString( subunits, elemstart, elemend );
329  pos.i = nextElementFloat( subunits, elemstart, elemend );
330  pos.j = nextElementFloat( subunits, elemstart, elemend );
331  pos.k = nextElementFloat( subunits, elemstart, elemend );
332  R.i = nextElementFloat( subunits, elemstart, elemend );
333  R.j = nextElementFloat( subunits, elemstart, elemend );
334  R.k = nextElementFloat( subunits, elemstart, elemend );
335  Q.i = nextElementFloat( subunits, elemstart, elemend );
336  Q.j = nextElementFloat( subunits, elemstart, elemend );
337  Q.k = nextElementFloat( subunits, elemstart, elemend );
338  double restricted = cos( nextElementFloat( subunits, elemstart, elemend, 180 )*VS_PI/180.0 );
339 
340  ret.push_back( SubUnitStruct( filename, pos, Q, R, restricted ) );
341  } else {
342  ofs = string::npos;
343  }
344  }
345  return ret;
346 }
CSVRow GetUnitRow ( string  filename,
bool  subu,
int  faction,
bool  readlast,
bool rread 
)

Definition at line 1429 of file unit_csv.cpp.

References FactionUtil::GetFactionName(), i, and unitTables.

1430 {
1431  std::string hashname = filename+"__"+FactionUtil::GetFactionName( faction );
1432  for (int i = ( (int) unitTables.size() )-(readlast ? 1 : 2); i >= 0; --i) {
1433  unsigned int where;
1434  if ( unitTables[i]->RowExists( hashname, where ) ) {
1435  rread = true;
1436  return CSVRow( unitTables[i], where );
1437  } else if ( unitTables[i]->RowExists( filename, where ) ) {
1438  rread = true;
1439  return CSVRow( unitTables[i], where );
1440  }
1441  }
1442  rread = false;
1443  return CSVRow();
1444 }
void HudDamage ( float dam,
const string &  damages 
)

Definition at line 535 of file unit_csv.cpp.

References i, and nextElementFloat().

Referenced by Unit::LoadRow().

536 {
537  if (dam) {
538  string::size_type elemstart = 0, elemend = string::npos;
539  for (int i = 0; i < 1+MAXVDUS+UnitImages< void >::NUMGAUGES; ++i)
540  dam[i] = nextElementFloat( damages, elemstart, elemend, 1 );
541  }
542 }
static void ImportCargo ( Unit thus,
const string &  imports 
)
static

Definition at line 468 of file unit_csv.cpp.

References UnitImages< BOGUS >::cargo, Unit::ImportPartList(), Network, nextElementFloat(), nextElementString(), and Unit::pImage.

Referenced by Unit::LoadRow().

469 {
470  if (Network != NULL)
471  return; //Server takes care of this.
472 
473  string::size_type where, when, ofs = 0;
474  {
475  int nelem = 0;
476  while ( ( ofs = imports.find( '{', ofs ) ) != string::npos )
477  nelem++, ofs++;
478  thus->pImage->cargo.reserve( nelem+thus->pImage->cargo.size() );
479  ofs = 0;
480  }
481  while ( ( where = imports.find( '{', ofs ) ) != string::npos ) {
482  if ( ( when = imports.find( '}', where+1 ) ) != string::npos ) {
483  string::size_type elemstart = where+1, elemend = when;
484  ofs = when+1;
485 
486  string filename = nextElementString( imports, elemstart, elemend );
487  double price = nextElementFloat( imports, elemstart, elemend, 1 );
488  double pricestddev = nextElementFloat( imports, elemstart, elemend );
489  double quant = nextElementFloat( imports, elemstart, elemend, 1 );
490  double quantstddev = nextElementFloat( imports, elemstart, elemend );
491 
492  thus->ImportPartList( filename, price, pricestddev, quant, quantstddev );
493  } else {
494  ofs = string::npos;
495  }
496  }
497 }
void LoadCockpit ( Unit thus,
const string &  cockpit 
)

Definition at line 626 of file unit_csv.cpp.

References UnitImages< BOGUS >::CockpitCenter, UnitImages< BOGUS >::cockpitImage, nextElementFloat(), nextElementString(), and Unit::pImage.

Referenced by Unit::LoadRow().

627 {
628  string::size_type elemstart = 0, elemend = string::npos;
629  thus->pImage->cockpitImage = nextElementString( cockpit, elemstart, elemend );
630  thus->pImage->CockpitCenter.i = nextElementFloat( cockpit, elemstart, elemend );
631  thus->pImage->CockpitCenter.j = nextElementFloat( cockpit, elemstart, elemend );
632  thus->pImage->CockpitCenter.k = nextElementFloat( cockpit, elemstart, elemend );
633 }
CSVRow LookupUnitRow ( const string &  unitname,
const string &  faction 
)

Definition at line 22 of file unit_csv.cpp.

References StarSystemGent::faction, i, and unitTables.

Referenced by UniverseUtil::LookupUnitStat(), and GameUnit< UnitType >::Split().

23 {
24  string hashname = unitname+"__"+faction;
25  for (vector< CSVTable* >::reverse_iterator i = unitTables.rbegin(); i != unitTables.rend(); ++i) {
26  unsigned int where;
27  if ( (*i)->RowExists( hashname, where ) )
28  return CSVRow( (*i), where );
29  else if ( (*i)->RowExists( unitname, where ) )
30  return CSVRow( (*i), where );
31  }
32  return CSVRow();
33 }
static void mapToStringVec ( vsUMap< string, string >  a,
vector< string > &  key,
vector< string > &  value 
)
static

Definition at line 1480 of file unit_csv.cpp.

References i.

Referenced by Unit::WriteUnitString().

1481 {
1482  for (vsUMap< string, string >::iterator i = a.begin(); i != a.end(); ++i) {
1483  key.push_back( i->first );
1484  value.push_back( i->second );
1485  }
1486 }
static string nextElement ( string &  inp)
static

Definition at line 175 of file unit_csv.cpp.

References nextElementRange(), and start.

Referenced by AddSounds().

176 {
177  string::size_type start = 0;
178  std::pair< string::size_type, string::size_type >rng = nextElementRange( inp, start, string::npos );
179  string ret = inp.substr( rng.first, ( (rng.second == string::npos) ? string::npos : (rng.second-rng.first) ) );
180  inp.erase( 0, ( (rng.second == string::npos) ? string::npos : (rng.second+1) ) );
181  return ret;
182 }
static double nextElementBool ( const string &  inp,
string::size_type &  start,
string::size_type  end,
bool  def = false 
)
static

Definition at line 165 of file unit_csv.cpp.

References nextElementRange(), and XMLSupport::parse_bool().

Referenced by AddCarg(), and AddMounts().

166 {
167  std::pair< string::size_type, string::size_type >rng = nextElementRange( inp, start, end );
168  return (rng.first
169  == rng.second) ? def : XMLSupport::parse_bool( inp.substr( rng.first,
170  ( (rng.second
171  == string::npos) ? string::npos : (rng.second
172  -rng.first) ) ) );
173 }
static double nextElementFloat ( const string &  inp,
string::size_type &  start,
string::size_type  end,
double  def = 0 
)
static

Definition at line 159 of file unit_csv.cpp.

References nextElementRange().

Referenced by AddCarg(), AddDocks(), AddLights(), AddMounts(), GetSubUnits(), HudDamage(), ImportCargo(), and LoadCockpit().

160 {
161  std::pair< string::size_type, string::size_type >rng = nextElementRange( inp, start, end );
162  return (rng.first == rng.second) ? def : atof( inp.c_str()+rng.first );
163 }
static int nextElementInt ( const string &  inp,
string::size_type &  start,
string::size_type  end,
int  def = 0 
)
static

Definition at line 153 of file unit_csv.cpp.

References nextElementRange().

Referenced by AddCarg(), AddDocks(), and AddMounts().

154 {
155  std::pair< string::size_type, string::size_type >rng = nextElementRange( inp, start, end );
156  return (rng.first == rng.second) ? def : atoi( inp.c_str()+rng.first );
157 }
static std::pair< string::size_type, string::size_type > nextElementRange ( const string &  inp,
string::size_type &  start,
string::size_type  end 
)
static

Definition at line 128 of file unit_csv.cpp.

References start.

Referenced by nextElement(), nextElementBool(), nextElementFloat(), nextElementInt(), and nextElementString().

131 {
132  string::size_type ostart = start;
133  start = inp.find( ';', start );
134  if ( start != string::npos && ( start != end && (end == string::npos || start < end) ) ) {
135  ++start;
136  return std::pair< string::size_type, string::size_type > ( ostart, start-1 );
137  } else {
138  start = end;
139  return std::pair< string::size_type, string::size_type > ( ostart, end );
140  }
141 }
static string nextElementString ( const string &  inp,
string::size_type &  start,
string::size_type  end 
)
static

Definition at line 143 of file unit_csv.cpp.

References nextElementRange().

Referenced by AddCarg(), AddLights(), AddMounts(), GetSubUnits(), ImportCargo(), and LoadCockpit().

144 {
145  std::pair< string::size_type, string::size_type >rng = nextElementRange( inp, start, end );
146  if (rng.second == string::npos)
147  return inp.substr( rng.first );
148 
149  else
150  return inp.substr( rng.first, rng.second-rng.first );
151 }
int parseMountSizes ( const char *  str)

Definition at line 273 of file unit_functions_generic.cpp.

Referenced by AddMounts().

274 {
275  char tmp[13][50];
276  int ans = weapon_info::NOWEAP;
277  int num = sscanf( str,
278  "%s %s %s %s %s %s %s %s %s %s %s %s %s",
279  tmp[0],
280  tmp[1],
281  tmp[2],
282  tmp[3],
283  tmp[4],
284  tmp[5],
285  tmp[6],
286  tmp[7],
287  tmp[8],
288  tmp[9],
289  tmp[10],
290  tmp[11],
291  tmp[12] );
292  for (int i = 0; i < num; i++)
293  ans |= lookupMountSize( tmp[i] );
294  return ans;
295 }
void pushMesh ( std::vector< Mesh * > &  mesh,
float randomstartframe,
float randomstartseconds,
const char *  filename,
const float  scale,
int  faction,
class Flightgroup fg,
int  startframe,
double  texturestarttime 
)

Definition at line 469 of file unit_xml.cpp.

References float, i, Mesh::LoadMeshes(), and Vector.

Referenced by AddMeshes().

478 {
479  vector< Mesh* >m = Mesh::LoadMeshes( filename, Vector( scale, scale, scale ), faction, fg );
480  for (unsigned int i = 0; i < m.size(); ++i) {
481  meshes.push_back( m[i] );
482  if (startframe >= 0) {
483  meshes.back()->setCurrentFrame( startframe );
484  } else if (startframe == -2) {
485  float r = ( (float) rand() )/RAND_MAX;
486  meshes.back()->setCurrentFrame( r*meshes.back()->getFramesPerSecond() );
487  } else if (startframe == -1) {
488  if (randomstartseconds == 0)
489  randomstartseconds = randomstartframe*meshes.back()->getNumLOD()/meshes.back()->getFramesPerSecond();
490  meshes.back()->setCurrentFrame( randomstartseconds*meshes.back()->getFramesPerSecond() );
491  }
492  if (texturestarttime > 0) {
493  meshes.back()->setTextureCumulativeTime( texturestarttime );
494  } else {
495  float fps = meshes.back()->getTextureFramesPerSecond();
496  int frames = meshes.back()->getNumTextureFrames();
497  double ran = randomstartframe;
498  if (fps > 0 && frames > 1) {
499  ran *= frames/fps;
500  } else {
501  ran *= 1000;
502  }
503  meshes.back()->setTextureCumulativeTime( ran );
504  }
505  }
506 }
static bool stob ( const string &  inp,
bool  defaul 
)
static

Definition at line 184 of file unit_csv.cpp.

References XMLSupport::parse_bool().

Referenced by Unit::LoadRow().

185 {
186  if (inp.length() != 0)
187  return XMLSupport::parse_bool( inp );
188  return defaul;
189 }
static double stof ( const string &  inp,
double  def = 0 
)
static

Definition at line 191 of file unit_csv.cpp.

References XMLSupport::parse_float().

Referenced by AssignIf(), AssignIfDeg(), Unit::LoadRow(), Unit::makeMasterPartList(), and Unit::WriteUnitString().

192 {
193  if (inp.length() != 0)
194  return XMLSupport::parse_float( inp );
195  return def;
196 }
static int stoi ( const string &  inp,
int  def = 0 
)
static

Definition at line 198 of file unit_csv.cpp.

References XMLSupport::parse_int().

Referenced by Unit::LoadRow(), and Unit::makeMasterPartList().

199 {
200  if (inp.length() != 0)
201  return XMLSupport::parse_int( inp );
202  return def;
203 }
static string tos ( float  val)
static

Definition at line 1487 of file unit_csv.cpp.

References XMLSupport::tostring().

Referenced by Unit::WriteUnitString().

1488 {
1489  return XMLSupport::tostring( val );
1490 }
static string tos ( double  val)
static

Definition at line 1492 of file unit_csv.cpp.

References XMLSupport::tostring().

1493 {
1494  return XMLSupport::tostring( (float) val );
1495 }
static string tos ( unsigned int  val)
static

Definition at line 1497 of file unit_csv.cpp.

References XMLSupport::tostring().

1498 {
1499  return XMLSupport::tostring( val );
1500 }
static string tos ( bool  val)
static

Definition at line 1502 of file unit_csv.cpp.

References XMLSupport::tostring().

1503 {
1504  return XMLSupport::tostring( (int) val );
1505 }
static string tos ( int  val)
static

Definition at line 1507 of file unit_csv.cpp.

References XMLSupport::tostring().

1508 {
1509  return XMLSupport::tostring( val );
1510 }
void UpdateMasterPartList ( Unit ret)

Definition at line 1758 of file unit_csv.cpp.

References _Universe, Universe::AccessCockpit(), UnitImages< BOGUS >::cargo, Cargo::category, Cargo::content, Cargo::description, Unit::GetCargo(), Unit::GetImageInformation(), SaveGame::getMissionStringData(), i, j, Cargo::mass, Unit::numCargo(), Universe::numPlayers(), XMLSupport::parse_float(), Cargo::price, Cargo::quantity, Unit::RemoveCargo(), Cockpit::savegame, and Cargo::volume.

Referenced by Unit::ForceDock(), and Unit::makeMasterPartList().

1759 {
1760  for (unsigned int i = 0; i < _Universe->numPlayers(); ++i) {
1761  Cockpit *cp = _Universe->AccessCockpit( i );
1762  std::vector< std::string > *addedcargoname = &cp->savegame->getMissionStringData( "master_part_list_content" );
1763  std::vector< std::string > *addedcargocat = &cp->savegame->getMissionStringData( "master_part_list_category" );
1764  std::vector< std::string > *addedcargovol = &cp->savegame->getMissionStringData( "master_part_list_volume" );
1765  std::vector< std::string > *addedcargoprice = &cp->savegame->getMissionStringData( "master_part_list_price" );
1766  std::vector< std::string > *addedcargomass = &cp->savegame->getMissionStringData( "master_part_list_mass" );
1767  std::vector< std::string > *addedcargodesc = &cp->savegame->getMissionStringData( "master_part_list_description" );
1768  for (unsigned int j = 0; j < addedcargoname->size(); ++j) {
1769  Cargo carg;
1770  carg.content = (*addedcargoname)[j];
1771  carg.category = ( j < addedcargocat->size() ? (*addedcargocat)[j] : std::string( "Uncategorized" ) );
1772  carg.volume = (j < addedcargovol->size() ? XMLSupport::parse_float( (*addedcargovol)[j] ) : 1.0);
1773  carg.price = (j < addedcargoprice->size() ? XMLSupport::parse_float( (*addedcargoprice)[j] ) : 0.0);
1774  carg.mass = (j < addedcargomass->size() ? XMLSupport::parse_float( (*addedcargomass)[j] ) : .01);
1775  carg.description = ( j < addedcargodesc->size() ? (*addedcargodesc)[j] : std::string( "No Description Added" ) );
1776  carg.quantity = 1;
1777  ret->GetImageInformation().cargo.push_back( carg );
1778  }
1779  }
1780  std::sort( ret->GetImageInformation().cargo.begin(), ret->GetImageInformation().cargo.end() );
1781  {
1782  Cargo last_cargo;
1783  for (int i = ret->numCargo()-1; i >= 0; --i) {
1784  if (ret->GetCargo( i ).content == last_cargo.content
1785  && ret->GetCargo( i ).category == last_cargo.category)
1786  ret->RemoveCargo( i, ret->GetCargo( i ).quantity, true );
1787  else
1788  last_cargo = ret->GetCargo( i );
1789  }
1790  }
1791 }
static void UpgradeUnit ( Unit un,
const std::string &  upgrades 
)
static

Definition at line 48 of file unit_csv.cpp.

References UnitFactory::createUnit(), ClassCache< Typ, Key >::getCachedConst(), GetModeFromName(), FactionUtil::GetUpgradeFaction(), XMLSupport::parse_int(), ClassCache< Typ, Key >::setCachedConst(), and UnitUtil::upgrade().

Referenced by Unit::LoadRow().

49 {
50  string::size_type when;
51  string::size_type ofs = 0;
52  while ( ( when = upgrades.find( '{', ofs ) ) != string::npos ) {
53  string::size_type where = upgrades.find( '}', when+1 );
54  string upgrade = upgrades.substr( when+1, ( (where == string::npos) ? string::npos : where-when-1 ) );
55  ofs = ( (where == string::npos) ? string::npos : where+1 );
56 
57  unsigned int mountoffset = 0;
58  unsigned int subunitoffset = 0;
59  string::size_type where1 = upgrade.find( ';' );
60  string::size_type where2 = upgrade.rfind( ';' );
61  if (where1 != string::npos) {
62  mountoffset = XMLSupport::parse_int( upgrade.substr( where1+1, where2 != where1 ? where2 : upgrade.length() ) );
63  if (where2 != where1 && where2 != string::npos)
64  subunitoffset = XMLSupport::parse_int( upgrade.substr( where2+1 ) );
65  }
66  upgrade = upgrade.substr( 0, where1 );
67  if (upgrade.length() == 0)
68  continue;
70  if (!upgradee) {
72  UnitFactory::createUnit( upgrade.c_str(),
73  true,
75  }
76  double percent = 1.0;
77  un->Unit::Upgrade( upgradee,
78  mountoffset,
79  subunitoffset,
80  GetModeFromName( upgrade.c_str() ), true, percent, NULL );
81  }
82 }
string WriteHudDamage ( Unit un)

Definition at line 544 of file unit_csv.cpp.

References UnitImages< BOGUS >::cockpit_damage, i, Unit::pImage, and XMLSupport::tostring().

Referenced by Unit::WriteUnitString().

545 {
546  string ret;
547  const string semi = ";";
548  if (un->pImage->cockpit_damage) {
549  for (int i = 0; i < 1+MAXVDUS+UnitImages< void >::NUMGAUGES; ++i) {
550  ret += XMLSupport::tostring( un->pImage->cockpit_damage[i] );
551  ret += semi;
552  }
553  }
554  return ret;
555 }
string WriteHudDamageFunc ( Unit un)

Definition at line 557 of file unit_csv.cpp.

References UnitImages< BOGUS >::cockpit_damage, i, MAXVDUS, Unit::pImage, and XMLSupport::tostring().

Referenced by Unit::WriteUnitString().

558 {
559  string ret;
560  const string semi = ";";
561  if (un->pImage->cockpit_damage) {
563  for (int i = numg; i < 2*numg; ++i) {
564  ret += XMLSupport::tostring( un->pImage->cockpit_damage[i] );
565  ret += semi;
566  }
567  }
568  return ret;
569 }