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_gen.cpp File Reference
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include "macosx_math.h"
#include <math.h>
#include <time.h>
#include <assert.h>
#include "configxml.h"
#include "vs_globals.h"
#include "xml_support.h"
#include "gfxlib.h"
#include "galaxy_xml.h"
#include "galaxy_gen.h"
#include "vs_random.h"
#include <ctype.h>
#include "vsfilesystem.h"

Go to the source code of this file.

Classes

struct  StarSystemGent::Color
 
class  StarSystemGent::Vector
 
struct  StarSystemGent::GradColor
 
struct  StarSystemGent::PlanetInfo
 
struct  StarSystemGent::StarInfo
 

Namespaces

 StarSystemGent
 

Constant Groups

 StarSystemGent
 

Macros

#define M_PI   3.1415926536
 

Functions

static void seedrand (unsigned long seed)
 
static int stringhash (const string &key)
 
static unsigned int ssrand ()
 
static string GetWrapXY (string cname, int &wrapx, int &wrapy)
 
string getStarSystemName (const string &in)
 
float StarSystemGent::mmax (float a, float b)
 
int StarSystemGent::rnd (int lower, int upper)
 
string StarSystemGent::getGenericName (vector< string > &s)
 
string StarSystemGent::getRandName (vector< string > &s)
 
float StarSystemGent::grand ()
 
static void StarSystemGent::Tab ()
 
float StarSystemGent::difffunc (float inputdiffuse)
 
void StarSystemGent::WriteLight (unsigned int i)
 
void StarSystemGent::ResetGlobalVariables ()
 
void StarSystemGent::readColorGrads (vector< string > &entity, const char *file)
 
float StarSystemGent::clamp01 (float a)
 
float StarSystemGent::getcolor (float c, float var)
 
GradColor StarSystemGent::whichGradColor (float r, unsigned int &j)
 
Color StarSystemGent::StarColor (float radius, unsigned int &entityindex)
 
GFXColor StarSystemGent::getStarColorFromRadius (float radius)
 
float StarSystemGent::LengthOfYear (Vector r, Vector s)
 
void StarSystemGent::CreateLight (unsigned int i)
 
Vector StarSystemGent::generateCenter (float minradii, bool jumppoint)
 
float StarSystemGent::makeRS (Vector &r, Vector &s, float minradii, bool jumppoint)
 
void StarSystemGent::Updateradii (float orbitsize, float thisplanetradius)
 
Vector StarSystemGent::generateAndUpdateRS (Vector &r, Vector &s, float thisplanetradius, bool jumppoint)
 
vector< string > StarSystemGent::parseBigUnit (const string &input)
 
void StarSystemGent::WriteUnit (const string &tag, const string &name, const string &filename, const Vector &r, const Vector &s, const Vector &center, const string &nebfile, const string &destination, bool faction, float thisloy=0)
 
string StarSystemGent::getJumpTo (const string &s)
 
string StarSystemGent::starin (const string &input)
 
string StarSystemGent::GetNebFile (string &input)
 
string StarSystemGent::AnalyzeType (string &input, string &nebfile, float &radius)
 
void StarSystemGent::MakeSmallUnit ()
 
void StarSystemGent::MakeJump (float radius, bool forceRS=false, Vector R=Vector(0, 0, 0), Vector S=Vector(0, 0, 0), Vector center=Vector(0, 0, 0), float thisloy=0)
 
void StarSystemGent::MakeBigUnit (int callingentitytype, string name=string(), float orbitalradius=0)
 
void StarSystemGent::MakeMoons (float callingradius, int callingentitytype)
 
void StarSystemGent::MakeJumps (float callingradius, int callingentitytype, int numberofjumps)
 
void StarSystemGent::MakePlanet (float radius, int entitytype, string texturename, string unitname, string technique, int texturenum, int numberofjumps, int numberofstarbases)
 
void StarSystemGent::beginStar ()
 
void StarSystemGent::endStar ()
 
void StarSystemGent::CreateStar ()
 
void StarSystemGent::CreateFirstStar ()
 
void StarSystemGent::CreatePrimaries ()
 
void StarSystemGent::CreateStarSystem ()
 
void StarSystemGent::readentity (vector< string > &entity, const char *filename)
 
const char * StarSystemGent::noslash (const char *in)
 
string getStarSystemFileName (const string &input)
 
string getStarSystemSector (const string &in)
 
void readnames (vector< string > &entity, const char *filename)
 
void readplanetentity (vector< StarInfo > &starinfos, string planetlist, unsigned int numstars)
 
static int pushDown (int val)
 
static int pushDownTowardsMean (int mean, int val)
 
static int pushTowardsMean (int mean, int val)
 
void generateStarSystem (SystemInfo &si)
 

Variables

static VSRandom starsysrandom (time(NULL))
 
const char StarSystemGent::nada [1] = ""
 
vector< ColorStarSystemGent::lights
 
VSFile StarSystemGent::f
 
int StarSystemGent::xmllevel
 
const int StarSystemGent::STAR = 0
 
const int StarSystemGent::PLANET = 1
 
const int StarSystemGent::MOON = 2
 
const int StarSystemGent::JUMP = 3
 
vector< string > StarSystemGent::starentities
 
vector< string > StarSystemGent::jumps
 
vector< string > StarSystemGent::gradtex
 
vector< string > StarSystemGent::naturalphenomena
 
vector< string > StarSystemGent::starbases
 
unsigned int StarSystemGent::numstarbases
 
unsigned int StarSystemGent::numnaturalphenomena
 
unsigned int StarSystemGent::numstarentities
 
vector< string > StarSystemGent::background
 
vector< string > StarSystemGent::names
 
vector< string > StarSystemGent::rings
 
const float StarSystemGent::moonofmoonprob = .01
 
string StarSystemGent::systemname
 
vector< floatStarSystemGent::radii
 
const float StarSystemGent::minspeed = .001
 
const float StarSystemGent::maxspeed = 8
 
vector< floatStarSystemGent::starradius
 
string StarSystemGent::faction
 
vector< GradColorStarSystemGent::colorGradiant
 
float StarSystemGent::compactness = 2
 
float StarSystemGent::jumpcompactness = 2
 
vector< StarInfoStarSystemGent::stars
 
unsigned int StarSystemGent::planetoffset
 
unsigned int StarSystemGent::staroffset
 
unsigned int StarSystemGent::moonlevel
 

Macro Definition Documentation

Function Documentation

void generateStarSystem ( SystemInfo si)

Definition at line 1344 of file galaxy_gen.cpp.

References SystemInfo::asteroids, SystemInfo::asteroidslist, StarSystemGent::background, SystemInfo::backgrounds, SystemInfo::compactness, StarSystemGent::compactness, VSFileSystem::CreateDirectoryHome(), StarSystemGent::CreateStarSystem(), f, SystemInfo::faction, StarSystemGent::faction, SystemInfo::filename, VegaConfig::getVariable(), StarSystemGent::gradtex, i, int, StarSystemGent::jumpcompactness, SystemInfo::jumps, StarSystemGent::jumps, SystemInfo::name, SystemInfo::names, StarSystemGent::names, StarSystemGent::naturalphenomena, SystemInfo::nebulae, SystemInfo::nebulaelist, StarSystemGent::numnaturalphenomena, StarSystemGent::numstarbases, StarSystemGent::numstarentities, SystemInfo::numstars, SystemInfo::numun1, SystemInfo::numun2, VSFileSystem::Ok, XMLSupport::parse_float(), XMLSupport::parse_int(), SystemInfo::planetlist, pushTowardsMean(), StarSystemGent::readColorGrads(), StarSystemGent::readentity(), readnames(), readplanetentity(), StarSystemGent::ResetGlobalVariables(), SystemInfo::ringlist, StarSystemGent::rings, SystemInfo::sector, SystemInfo::seed, seedrand(), VSFileSystem::sharedsectors, SystemInfo::smallun, StarSystemGent::starbases, StarSystemGent::starradius, SystemInfo::stars, StarSystemGent::stars, stringhash(), SystemInfo::sunradius, VSFileSystem::SystemFile, StarSystemGent::systemname, VSFileSystem::universe_name, vs_config, and VSFileSystem::vs_fprintf().

Referenced by MakeStarSystem().

1345 {
1347  static float radiusscale = XMLSupport::parse_float( vs_config->getVariable( "galaxy", "StarRadiusScale", "1000" ) );
1348  si.sunradius *= radiusscale;
1349  systemname = si.name;
1350  static float compactness_scale = XMLSupport::parse_float( vs_config->getVariable( "galaxy", "CompactnessScale", "1.5" ) );
1351  static float jump_compactness_scale =
1352  XMLSupport::parse_float( vs_config->getVariable( "galaxy", "JumpCompactnessScale", "1.5" ) );
1353  static int meannaturalphenomena = XMLSupport::parse_int( vs_config->getVariable( "galaxy", "MeanNaturalPhenomena", "1" ) );
1354  static int meanbases = XMLSupport::parse_int( vs_config->getVariable( "galaxy", "MeanStarBases", "2" ) );
1355  compactness = si.compactness*compactness_scale;
1356  jumpcompactness = si.compactness*jump_compactness_scale;
1357  if (si.seed)
1358  seedrand( si.seed );
1359  else
1360  seedrand( stringhash( si.sector+'/'+si.name ) );
1361  VSFileSystem::vs_fprintf( stderr, "star %d, natural %d, bases %d", si.numstars, si.numun1, si.numun2 );
1362  int nat = pushTowardsMean( meannaturalphenomena, si.numun1 );
1363  numnaturalphenomena = nat > si.numun1 ? si.numun1 : nat;
1364  numstarbases = pushTowardsMean( meanbases, si.numun2 );
1365  static float smallUnitsMultiplier = XMLSupport::parse_float( vs_config->getVariable( "galaxy", "SmallUnitsMultiplier", "0" ) );
1366  numstarbases = (int) (si.numun2*smallUnitsMultiplier);
1368  VSFileSystem::vs_fprintf( stderr, "star %d, natural %d, bases %d", numstarentities, numnaturalphenomena, numstarbases );
1369  starradius.push_back( si.sunradius );
1370  readColorGrads( gradtex, (si.stars).c_str() );
1371 
1372  readentity( starbases, (si.smallun).c_str() );
1373  readentity( background, (si.backgrounds).c_str() );
1374  if ( background.empty() )
1375  background.push_back( si.backgrounds );
1376  if (si.nebulae)
1377  readentity( naturalphenomena, (si.nebulaelist).c_str() );
1378  if (si.asteroids)
1379  readentity( naturalphenomena, (si.asteroidslist).c_str() );
1380  for (unsigned int i = 0; i < si.jumps.size(); i++)
1381  jumps.push_back( si.jumps[i] );
1382  faction = si.faction;
1383 
1385 
1386  readentity( rings, (si.ringlist).c_str() );
1387  readnames( names, (si.names).c_str() );
1388 
1390 
1391  VSError err = f.OpenCreateWrite( si.filename, SystemFile );
1392  if (err <= Ok) {
1393  CreateStarSystem();
1394  f.Close();
1395  }
1396  ResetGlobalVariables(); //deallocate any unused memory in vectors.
1397 }
string getStarSystemFileName ( const string &  input)

Definition at line 1165 of file galaxy_gen.cpp.

1166 {
1167  return input+string( ".system" );
1168 }
string getStarSystemName ( const string &  in)
string getStarSystemSector ( const string &  in)

Definition at line 1173 of file galaxy_gen.cpp.

1174 {
1175  string::size_type sep = in.find( '/' );
1176  if (sep == string::npos)
1177  return string( "." );
1178 
1179  else
1180  return in.substr( 0, sep );
1181 }
static string GetWrapXY ( string  cname,
int wrapx,
int wrapy 
)
static

Definition at line 54 of file galaxy_gen.cpp.

Referenced by StarSystemGent::MakePlanet().

55 {
56  string wrap = cname;
57  wrapx = wrapy = 1;
58  string::size_type pos = wrap.find( "wrapx" );
59  if (pos != string::npos) {
60  string Wrapx = wrap.substr( pos+5, wrap.length() );
61  cname = cname.substr( 0, pos );
62  sscanf( Wrapx.c_str(), "%d", &wrapx );
63  pos = Wrapx.find( "wrapy" );
64  if (pos != string::npos) {
65  string Wrapy = Wrapx.substr( pos+5, Wrapx.length() );
66  sscanf( Wrapy.c_str(), "%d", &wrapy );
67  }
68  }
69  return cname;
70 }
static int pushDown ( int  val)
static

Definition at line 1323 of file galaxy_gen.cpp.

References StarSystemGent::grand().

Referenced by pushDownTowardsMean().

1324 {
1325  while ( grand() > (1/val) )
1326  val--;
1327  return val;
1328 }
static int pushDownTowardsMean ( int  mean,
int  val 
)
static

Definition at line 1329 of file galaxy_gen.cpp.

References pushDown().

Referenced by pushTowardsMean().

1330 {
1331  int delta = mean-1;
1332  return delta+pushDown( val-delta );
1333 }
static int pushTowardsMean ( int  mean,
int  val 
)
static

Definition at line 1334 of file galaxy_gen.cpp.

References VegaConfig::getVariable(), XMLSupport::parse_bool(), pushDownTowardsMean(), and vs_config.

Referenced by generateStarSystem().

1335 {
1336  static bool dopushingtomean = XMLSupport::parse_bool( vs_config->getVariable( "galaxy", "PushValuesToMean", "true" ) );
1337  if (!dopushingtomean)
1338  return val;
1339  if (val < mean)
1340  return -pushDownTowardsMean( -mean, -val );
1341  return pushDownTowardsMean( mean, val );
1342 }
void readnames ( vector< string > &  entity,
const char *  filename 
)

warning... obvious vulnerability

Definition at line 1182 of file galaxy_gen.cpp.

References VSFileSystem::VSFile::Close(), VSFileSystem::VSFile::Eof(), f, i, input_buffer, VSFileSystem::Ok, VSFileSystem::VSFile::OpenReadOnly(), VSFileSystem::VSFile::ReadLine(), and VSFileSystem::UniverseFile.

Referenced by generateStarSystem().

1183 {
1184  VSFile f;
1185  VSError err = f.OpenReadOnly( filename, UniverseFile );
1186  if (err > Ok)
1187  return;
1189  char input_buffer[1000];
1190  while ( !f.Eof() ) {
1191  f.ReadLine( input_buffer, 999 );
1192  if (input_buffer[0] == '\0' || input_buffer[0] == '\n' || input_buffer[0] == '\r')
1193  continue;
1194  for (unsigned int i = 0; input_buffer[i] != '\0' && i < 999; i++) {
1195  if (input_buffer[i] == '\r')
1196  input_buffer[i] = '\0';
1197  if (input_buffer[i] == '\n') {
1198  input_buffer[i] = '\0';
1199  break;
1200  }
1201  }
1202  entity.push_back( input_buffer );
1203  }
1204  f.Close();
1205 }
void readplanetentity ( vector< StarInfo > &  starinfos,
string  planetlist,
unsigned int  numstars 
)

Definition at line 1207 of file galaxy_gen.cpp.

References _Universe, Universe::getGalaxy(), GalaxyXML::Galaxy::getPlanetNameFromInitial(), GalaxyXML::Galaxy::getPlanetVariable(), i, j, StarSystemGent::jumps, k, StarSystemGent::numstarbases, XMLSupport::parse_int(), rnd(), size, and VSFileSystem::vs_fprintf().

Referenced by generateStarSystem().

1208 {
1209  if (numstars < 1) {
1210  numstars = 1;
1211  vs_fprintf( stderr, "No stars exist in this system!\n" );
1212  }
1213  string::size_type i, j;
1214  unsigned int u;
1215  starinfos.reserve( numstars );
1216  for (u = 0; u < numstars; ++u)
1217  starinfos.push_back( StarInfo() );
1218  u--;
1219  while (i = planetlist.find( ' ' ), 1) {
1220  if (i == 0) {
1221  planetlist = planetlist.substr( 1 );
1222  continue;
1223  }
1224  int nummoon = 0;
1225  for (j = 0; j < i && j < planetlist.size() && planetlist[j] == '*'; ++j)
1226  nummoon++;
1227  if (nummoon == 0)
1228  u++;
1229  if ( j == string::npos || j >= planetlist.size() ) break;
1230  starinfos[u%numstars].planets.push_back( PlanetInfo() );
1231  starinfos[u%numstars].planets.back().moonlevel = nummoon;
1232  {
1233  GalaxyXML::Galaxy *galaxy = _Universe->getGalaxy();
1234 
1235  static const string numtag("#num#");
1236  static const string empty;
1237  static const string::size_type numlen = numtag.length();
1238  string::size_type numpos;
1239 
1240  // Get planet name and texture
1241  string planetname = galaxy->getPlanetNameFromInitial( planetlist.substr( j, i == string::npos ? string::npos : i-j ) );
1242  string texturename = galaxy->getPlanetVariable( planetname, "texture", "No texture supplied in <planets>!" );
1243 
1244  // Get unit name, deriving a default name from its texture
1245  string defunitname = texturename.substr(0, texturename.find_first_of('|'));
1246  if (defunitname.find_last_of('/') != string::npos)
1247  defunitname = defunitname.substr(defunitname.find_last_of('/')+1);
1248  defunitname = defunitname.substr(0, defunitname.find_last_of('.'));
1249 
1250  numpos=0;
1251  while ((numpos = defunitname.find(numtag, numpos)) != string::npos)
1252  defunitname.replace(numpos, numlen, empty);
1253 
1254  string unitname = galaxy->getPlanetVariable( planetname, "unit", defunitname );
1255 
1256  // Get planet rendering technique
1257  string techniquename=galaxy->getPlanetVariable( planetname, "technique", "" );
1258 
1259  // Replace randomized number placeholder tags
1260  starinfos[u%numstars].planets.back().num =
1261  rnd( XMLSupport::parse_int( galaxy->getPlanetVariable( planetname, "texture_min", "0" ) ),
1262  XMLSupport::parse_int( galaxy->getPlanetVariable( planetname, "texture_max", "0" ) ) );
1263 
1264  char num[32];
1265  if (starinfos[u%numstars].planets.back().num == 0)
1266  num[0] = 0;
1267  else
1268  snprintf(num, sizeof(num), "%d", starinfos[u%numstars].planets.back().num);
1269 
1270  numpos=0;
1271  while ((numpos = texturename.find(numtag, numpos)) != string::npos)
1272  texturename.replace(numpos, numlen, num);
1273 
1274  // Store info
1275  starinfos[u%numstars].planets.back().name = texturename;
1276  starinfos[u%numstars].planets.back().unitname = unitname;
1277  starinfos[u%numstars].planets.back().technique = techniquename;
1278  }
1279  starinfos[u%numstars].planets.back().numstarbases = 0;
1280  starinfos[u%numstars].planets.back().numjumps = 0;
1281  if (i == string::npos) break;
1282  planetlist = planetlist.substr( i+1 );
1283  }
1284  unsigned int k;
1285  if ( starinfos.size() ) {
1286  bool size = 0;
1287  for (k = 0; k < starinfos.size(); ++k)
1288  if ( starinfos[k].planets.size() ) {
1289  size = true;
1290  break;
1291  }
1292  if (!size) {
1293  int oldjumps = jumps.size();
1294  int oldstarbases = numstarbases;
1295  int newstuff;
1296  for (k = starinfos.size(); k > 0; --k) {
1297  newstuff = oldjumps/k;
1298  starinfos[k-1].numjumps = newstuff;
1299  oldjumps -= newstuff;
1300  newstuff = oldstarbases/k;
1301  starinfos[k-1].numstarbases = newstuff;
1302  oldstarbases -= newstuff;
1303  }
1304  } else {
1305  for (k = 0; k < jumps.size(); ++k) {
1306  vector< PlanetInfo > *temp; //& doesn't like me so I use *.
1307  do
1308  temp = &starinfos[rnd( 0, starinfos.size() )].planets;
1309  while ( !temp->size() );
1310  (*temp)[rnd( 0, temp->size() )].numjumps++;
1311  }
1312  for (k = 0; k < numstarbases; ++k) {
1313  vector< PlanetInfo > *temp; //& appears to still have dislike for me.
1314  do
1315  temp = &starinfos[rnd( 0, starinfos.size() )].planets;
1316  while ( !temp->size() );
1317  (*temp)[rnd( 0, temp->size() )].numstarbases++;
1318  }
1319  }
1320  }
1321 }
static void seedrand ( unsigned long  seed)
static

Definition at line 36 of file galaxy_gen.cpp.

References starsysrandom.

Referenced by generateStarSystem().

37 {
38  starsysrandom = VSRandom( seed );
39 }
static unsigned int ssrand ( )
static

Definition at line 49 of file galaxy_gen.cpp.

References VSRandom::rand(), and starsysrandom.

Referenced by StarSystemGent::grand(), and StarSystemGent::rnd().

50 {
51  return starsysrandom.rand();
52 }
static int stringhash ( const string &  key)
static

Definition at line 40 of file galaxy_gen.cpp.

References k, and start.

Referenced by generateStarSystem().

41 {
42  unsigned int k = 0;
43  string::const_iterator start = key.begin();
44  for (; start != key.end(); start++)
45  k += (k*128)+*start;
46  return k;
47 }

Variable Documentation

VSRandom starsysrandom(time(NULL))
static

Referenced by seedrand(), and ssrand().