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
galaxy.cpp File Reference
#include "star_system_generic.h"
#include "cmd/script/mission.h"
#include "universe_generic.h"
#include "galaxy_xml.h"
#include "galaxy_gen.h"
#include "vsfilesystem.h"
#include "configxml.h"
#include "vs_globals.h"
#include "xml_support.h"
#include "lin_time.h"
#include "options.h"
#include <vector>
#include <string>
#include <map>
#include <deque>
#include <algorithm>

Go to the source code of this file.

Functions

string RemoveDotSystem (const char *input)
 
string getUniversePath ()
 
string getVarEitherSectionOrSub (Galaxy *galaxy, string section, string subsection, string variable, string defaultst)
 
void ClampIt (float &prop, float min, float max)
 
void ClampIt (int &prop, int min, int max)
 
static void clampSystem (SystemInfo &si, const SystemInfo &min, const SystemInfo &max)
 
void GetSystemXProp (Galaxy *galaxy, std::string sector, std::string minmax, SystemInfo &si)
 
SystemInfo GetSystemMin (Galaxy *galaxy)
 
SystemInfo GetSystemMax (Galaxy *galaxy)
 
static float av01 ()
 
static float sqav01 ()
 
static float fsqav (float in1, float in2)
 
static int rnd (int in1, int in2)
 
static int iav (int in1, int in2)
 
static int isqav (int in1, int in2)
 
void AvgSystems (const SystemInfo &a, const SystemInfo &b, SystemInfo &si)
 
const vector< string > & ParseDestinations (const string &value)
 
void MakeStarSystem (string file, Galaxy *galaxy, string origin, int forcerandom)
 

Variables

vs_options game_options
 

Function Documentation

static float av01 ( )
static

Definition at line 134 of file galaxy.cpp.

References float.

Referenced by sqav01().

135 {
136  return ( float( rand() ) )/( ( ( (float) RAND_MAX )+1 ) );
137 }
void AvgSystems ( const SystemInfo a,
const SystemInfo b,
SystemInfo si 
)

Definition at line 165 of file galaxy.cpp.

References a, SystemInfo::asteroids, SystemInfo::compactness, SystemInfo::force, fsqav(), iav(), isqav(), SystemInfo::nebulae, SystemInfo::numstars, SystemInfo::numun1, SystemInfo::numun2, SystemInfo::seed, and SystemInfo::sunradius.

Referenced by MakeStarSystem().

166 {
167  si = a; //copy all stuff that cna't be averaged
168  si.sunradius = fsqav( a.sunradius, b.sunradius );
170  si.numstars = isqav( a.numstars, b.numstars );
171  si.nebulae = a.nebulae || b.nebulae;
172  si.asteroids = a.asteroids || b.asteroids;
173  si.numun1 = isqav( a.numun1, b.numun1 );
174  si.numun2 = isqav( a.numun2, b.numun2 );
175  si.seed = iav( a.seed, b.seed );
176  si.force = a.force || b.force;
177 }
void ClampIt ( float prop,
float  min,
float  max 
)

Definition at line 73 of file galaxy.cpp.

References max(), and min().

Referenced by clampSystem().

74 {
75  if (prop < min)
76  prop = min;
77  if (prop > max)
78  prop = max;
79 }
void ClampIt ( int prop,
int  min,
int  max 
)

Definition at line 81 of file galaxy.cpp.

References max(), and min().

82 {
83  if (prop < min)
84  prop = min;
85  if (prop > max)
86  prop = max;
87 }
static void clampSystem ( SystemInfo si,
const SystemInfo min,
const SystemInfo max 
)
static

Definition at line 89 of file galaxy.cpp.

References ClampIt(), SystemInfo::compactness, SystemInfo::numstars, SystemInfo::numun1, SystemInfo::numun2, and SystemInfo::sunradius.

Referenced by MakeStarSystem().

90 {
91  ClampIt( si.sunradius, min.sunradius, max.sunradius );
93  ClampIt( si.numstars, min.numstars, max.numstars );
94  ClampIt( si.numun1, min.numun1, max.numun1 );
95  ClampIt( si.numun2, min.numun2, max.numun2 );
96 }
static float fsqav ( float  in1,
float  in2 
)
static

Definition at line 145 of file galaxy.cpp.

References sqav01().

Referenced by AvgSystems().

146 {
147  return sqav01()*(in2-in1)+in1;
148 }
SystemInfo GetSystemMax ( Galaxy galaxy)

Definition at line 127 of file galaxy.cpp.

References GetSystemXProp().

Referenced by MakeStarSystem().

128 {
129  SystemInfo si;
130  GetSystemXProp( galaxy, "unknown_sector", "max", si );
131  return si;
132 }
SystemInfo GetSystemMin ( Galaxy galaxy)

Definition at line 120 of file galaxy.cpp.

References GetSystemXProp().

Referenced by MakeStarSystem().

121 {
122  SystemInfo si;
123  GetSystemXProp( galaxy, "unknown_sector", "min", si );
124  return si;
125 }
void GetSystemXProp ( Galaxy galaxy,
std::string  sector,
std::string  minmax,
SystemInfo si 
)

Definition at line 98 of file galaxy.cpp.

References SystemInfo::asteroids, SystemInfo::asteroidslist, SystemInfo::backgrounds, SystemInfo::compactness, SystemInfo::faction, SystemInfo::force, getVarEitherSectionOrSub(), SystemInfo::names, SystemInfo::nebulae, SystemInfo::nebulaelist, SystemInfo::numstars, SystemInfo::numun1, SystemInfo::numun2, XMLSupport::parse_bool(), XMLSupport::parse_float(), XMLSupport::parse_int(), SystemInfo::planetlist, SystemInfo::ringlist, SystemInfo::seed, SystemInfo::smallun, SystemInfo::stars, and SystemInfo::sunradius.

Referenced by GetSystemMax(), GetSystemMin(), and MakeStarSystem().

99 {
100  si.sunradius = parse_float( getVarEitherSectionOrSub( galaxy, sector, minmax, "sun_radius", "5000" ) );
101  si.compactness = parse_float( getVarEitherSectionOrSub( galaxy, sector, minmax, "compactness", "1.5" ) );
102  si.numstars = parse_int( getVarEitherSectionOrSub( galaxy, sector, minmax, "num_stars", "1" ) );
103  si.nebulae = parse_bool( getVarEitherSectionOrSub( galaxy, sector, minmax, "nebulae", "true" ) );
104  si.asteroids = parse_bool( getVarEitherSectionOrSub( galaxy, sector, minmax, "asteroids", "true" ) );
105  si.numun1 = parse_int( getVarEitherSectionOrSub( galaxy, sector, minmax, "num_natural_phenomena", "0" ) );
106  si.numun2 = parse_int( getVarEitherSectionOrSub( galaxy, sector, minmax, "num_starbases", "0" ) );
107  si.faction = getVarEitherSectionOrSub( galaxy, sector, minmax, "faction", "unknown" );
108  si.seed = parse_int( getVarEitherSectionOrSub( galaxy, sector, minmax, "data", "0" ) );
109  si.names = getVarEitherSectionOrSub( galaxy, sector, minmax, "namelist", "names.txt" );
110  si.stars = getVarEitherSectionOrSub( galaxy, sector, minmax, "starlist", "stars.txt" );
111  si.planetlist = getVarEitherSectionOrSub( galaxy, sector, minmax, "planets", "" );
112  si.smallun = getVarEitherSectionOrSub( galaxy, sector, minmax, "unitlist", "smallunits.txt" );
113  si.asteroidslist = getVarEitherSectionOrSub( galaxy, sector, minmax, "asteroidlist", "asteroids.txt" );
114  si.ringlist = getVarEitherSectionOrSub( galaxy, sector, minmax, "ringlist", "rings.txt" );
115  si.nebulaelist = getVarEitherSectionOrSub( galaxy, sector, minmax, "nebulalist", "nebulae.txt" );
116  si.backgrounds = getVarEitherSectionOrSub( galaxy, sector, minmax, "backgroundlist", "background.txt" );
117  si.force = parse_bool( getVarEitherSectionOrSub( galaxy, sector, minmax, "force", "false" ) );
118 }
string getUniversePath ( )

Definition at line 56 of file galaxy.cpp.

References vs_options::universe_path.

57 {
58  char del[] = {'/', '\0'};
59  return game_options.universe_path+string( del );
60 }
string getVarEitherSectionOrSub ( Galaxy galaxy,
string  section,
string  subsection,
string  variable,
string  defaultst 
)

Definition at line 62 of file galaxy.cpp.

References GalaxyXML::SGalaxy::getVariable().

Referenced by GetSystemXProp(), and MakeStarSystem().

63 {
64  string d3fault = galaxy->getVariable( section, subsection, variable,
65  galaxy->getVariable( section, variable, defaultst ) );
66  if (d3fault.length() == 0)
67  d3fault = galaxy->getVariable( section, variable, defaultst );
68  if (d3fault.length() == 0)
69  return defaultst;
70  return d3fault; //this code will prevent the empty planet lists from interfering
71 }
static int iav ( int  in1,
int  in2 
)
static

Definition at line 155 of file galaxy.cpp.

References rnd().

Referenced by AvgSystems().

156 {
157  return rnd( in1, in2+1 );
158 }
static int isqav ( int  in1,
int  in2 
)
static

Definition at line 160 of file galaxy.cpp.

References sqav01().

Referenced by AvgSystems().

161 {
162  return (int) ( in1+(in2+1-in1)*sqav01() );
163 }
void MakeStarSystem ( string  file,
Galaxy galaxy,
string  origin,
int  forcerandom 
)

Definition at line 192 of file galaxy.cpp.

References SystemInfo::asteroids, SystemInfo::asteroidslist, AvgSystems(), SystemInfo::backgrounds, clampSystem(), SystemInfo::compactness, SystemInfo::faction, accountXML::file, SystemInfo::filename, SystemInfo::force, generateStarSystem(), getStarSystemName(), getStarSystemSector(), GetSystemMax(), GetSystemMin(), GetSystemXProp(), getVarEitherSectionOrSub(), GalaxyXML::SGalaxy::getVariable(), i, SystemInfo::jumps, SystemInfo::name, SystemInfo::names, SystemInfo::nebulae, SystemInfo::nebulaelist, SystemInfo::numstars, SystemInfo::numun1, SystemInfo::numun2, XMLSupport::parse_bool(), XMLSupport::parse_float(), XMLSupport::parse_int(), ParseDestinations(), SystemInfo::planetlist, vs_options::PushValuesToMean, RemoveDotSystem(), SystemInfo::ringlist, SystemInfo::sector, SystemInfo::seed, SystemInfo::smallun, SystemInfo::stars, SystemInfo::sunradius, and XMLSupport::tostring().

Referenced by Universe::Generate1().

193 {
194  SystemInfo Ave;
195  SystemInfo si;
196  AvgSystems( GetSystemMin( galaxy ), GetSystemMax( galaxy ), Ave );
197  //Do we really need this duplicate code... or can we use GetSystemXProp()
199  si.name = RemoveDotSystem( getStarSystemName( file ).c_str() );
200  si.filename = file;
201  si.sunradius = parse_float( getVarEitherSectionOrSub( galaxy, si.sector, si.name, "sun_radius", tostring( Ave.sunradius ) ) );
202  si.compactness =
203  parse_float( getVarEitherSectionOrSub( galaxy, si.sector, si.name, "compactness", tostring( Ave.compactness ) ) );
204  si.numstars = parse_int( getVarEitherSectionOrSub( galaxy, si.sector, si.name, "num_stars", tostring( Ave.numstars ) ) );
205  si.nebulae = parse_bool( getVarEitherSectionOrSub( galaxy, si.sector, si.name, "nebulae", tostring( Ave.nebulae ) ) );
206  si.asteroids = parse_bool( getVarEitherSectionOrSub( galaxy, si.sector, si.name, "asteroids", tostring( Ave.asteroids ) ) );
207  si.numun1 =
208  parse_int( getVarEitherSectionOrSub( galaxy, si.sector, si.name, "num_natural_phenomena", tostring( Ave.numun1 ) ) );
209  si.numun2 = parse_int( getVarEitherSectionOrSub( galaxy, si.sector, si.name, "num_starbases", tostring( Ave.numun2 ) ) );
210  si.faction = getVarEitherSectionOrSub( galaxy, si.sector, si.name, "faction", Ave.faction );
211  si.seed = parse_int( getVarEitherSectionOrSub( galaxy, si.sector, si.name, "data", tostring( Ave.seed ) ) );
212  si.names = getVarEitherSectionOrSub( galaxy, si.sector, si.name, "namelist", Ave.names );
213  si.stars = getVarEitherSectionOrSub( galaxy, si.sector, si.name, "starlist", Ave.stars );
214  si.planetlist = getVarEitherSectionOrSub( galaxy, si.sector, si.name, "planets", Ave.planetlist );
215  si.smallun = getVarEitherSectionOrSub( galaxy, si.sector, si.name, "unitlist", Ave.smallun );
216  si.asteroidslist = getVarEitherSectionOrSub( galaxy, si.sector, si.name, "asteroidlist", Ave.asteroidslist );
217  si.ringlist = getVarEitherSectionOrSub( galaxy, si.sector, si.ringlist, "ringlist", Ave.ringlist );
218  si.nebulaelist = getVarEitherSectionOrSub( galaxy, si.sector, si.name, "nebulalist", Ave.nebulaelist );
219  si.backgrounds = getVarEitherSectionOrSub( galaxy, si.sector, si.name, "backgroundlist", Ave.backgrounds );
220  si.force = parse_bool( getVarEitherSectionOrSub( galaxy, si.sector, si.name, "force", Ave.force ? "true" : "false" ) );
222  si.force = true;
223  string dest = galaxy->getVariable( si.sector, si.name, "jumps", "" );
224  if ( dest.length() )
225  si.jumps = ParseDestinations( dest );
226  bool canret = origin.length() == 0;
227  for (unsigned int i = 0; i < si.jumps.size(); i++)
228  if (si.jumps[i] == origin) {
229  canret = true;
230  break;
231  }
232  if (!canret)
233  si.jumps.push_back( origin );
234  if (!si.force) {
235  SystemInfo minlimit, maxlimit;
236  GetSystemXProp( galaxy, "unknown_sector", "minlimit", minlimit );
237  GetSystemXProp( galaxy, "unknown_sector", "maxlimit", maxlimit );
238  clampSystem( si, minlimit, maxlimit );
239  }
240  generateStarSystem( si );
241 }
const vector< string >& ParseDestinations ( const string &  value)

Definition at line 178 of file galaxy.cpp.

Referenced by Mission::call_unit_launch(), Universe::getAdjacentStarSystems(), MakeStarSystem(), and UniverseUtil::showSplashScreen().

179 {
180  static vector< string >rv;
181  rv.clear();
182  string::size_type pos = 0, sep;
183  while ( ( sep = value.find( ' ', pos ) ) != string::npos ) {
184  rv.push_back( value.substr( pos, sep-pos ) );
185  pos = sep+1;
186  }
187  if ( pos < value.length() )
188  rv.push_back( value.substr( pos ) );
189  return rv;
190 }
string RemoveDotSystem ( const char *  input)

Definition at line 27 of file galaxy.cpp.

References RemoveDotSystem().

Referenced by Universe::Generate1(), Universe::getAdjacentStarSystems(), StarSystem::getFileName(), Universe::getGalaxyProperty(), Universe::getGalaxyPropertyDefault(), MakeStarSystem(), and RemoveDotSystem().

28 {
29  int sl = strlen( input );
30  if (sl == 0)
31  return string( "" );
32  char *tmp = strdup( input );
33  char *ptr = tmp+sl-1;
34  while (ptr > tmp) {
35  if (*ptr == '.') {
36  if ( 0 == strcmp( ptr, ".system" ) ) {
37  *ptr = '\0';
38 
39  char *ttmp = tmp;
40  tmp = strdup( RemoveDotSystem( tmp ).c_str() );
41  ptr = ( tmp+(ptr-ttmp) );
42  free( ttmp );
43 
44  break;
45  } else {
46  break;
47  }
48  }
49  ptr--;
50  }
51  string retval( tmp );
52  free( tmp );
53  return retval;
54 }
static int rnd ( int  in1,
int  in2 
)
static

Definition at line 150 of file galaxy.cpp.

Referenced by StarSystemGent::generateCenter(), StarSystemGent::getGenericName(), StarSystemGent::getRandName(), iav(), main(), StarSystemGent::MakePlanet(), StarSystemGent::makeRS(), and readplanetentity().

151 {
152  return (int) ( in1+(in2-in1)*( float( rand() ) )/( ( (float) RAND_MAX )+1 ) );
153 }
static float sqav01 ( )
static

Definition at line 139 of file galaxy.cpp.

References av01().

Referenced by fsqav(), and isqav().

140 {
141  float tmp = av01();
142  return tmp*tmp;
143 }

Variable Documentation

vs_options game_options

Definition at line 83 of file main.cpp.