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
star_system.cpp
Go to the documentation of this file.
1 #include <assert.h>
2 #include "star_system.h"
3 #include "cmd/planet.h"
4 #include "cmd/unit.h"
5 #include "cmd/unit_collide.h"
6 #include "cmd/collection.h"
7 #include "cmd/click_list.h"
8 #include "lin_time.h"
9 #include "cmd/beam.h"
10 #include "gfx/sphere.h"
11 #include "cmd/unit_collide.h"
12 #include "gfx/halo.h"
13 #include "gfx/background.h"
14 #include "gfx/animation.h"
15 #include "gfx/aux_texture.h"
16 #include "gfx/star.h"
17 #include "cmd/bolt.h"
18 #include <expat.h>
19 #include "gfx/cockpit.h"
20 #include "audiolib.h"
21 #include "cmd/music.h"
22 #include "config_xml.h"
23 #include "vs_globals.h"
24 #include "cmd/cont_terrain.h"
25 #include "vegastrike.h"
26 #include "universe.h"
27 #include "cmd/atmosphere.h"
28 #include "hashtable.h"
29 #include "cmd/nebula.h"
30 #include "galaxy_gen.h"
31 #include "cmd/script/mission.h"
32 #include "in_kb.h"
33 #include "cmd/script/flightgroup.h"
34 #include "load_mission.h"
35 #include "gfx/particle.h"
36 #include "gfx/lerp.h"
37 #include "gfx/warptrail.h"
38 #include "gfx/env_map_gent.h"
39 #include "vsfilesystem.h"
40 #include "cmd/unit_find.h"
41 
42 #include "options.h"
43 
45 
46 GameStarSystem::GameStarSystem( const char *filename, const Vector &centr, const float timeofyear )
47 {
48  no_collision_time = 0; //(int)(1+2.000/SIMULATION_ATOM);
50  name = NULL;
53  bolts = new bolt_draw;
54  collidetable = new CollideTable( this );
55 
57 
58  LoadXML( filename, centr, timeofyear );
59  if (!name)
60  name = strdup( filename );
61  AddStarsystemToUniverse( filename );
62  UpdateTime();
63  time = 0;
64 
66 
67  params.radius = 40000;
68 
69  params.low_color[0] = GFXColor( 0, 0.5, 0.0 );
70 
71  params.low_color[1] = GFXColor( 0, 1.0, 0.0 );
72 
73  params.low_ambient_color[0] = GFXColor( 0.0/255.0, 0.0/255.0, 0.0/255.0 );
74 
75  params.low_ambient_color[1] = GFXColor( 0.0/255.0, 0.0/255.0, 0.0/255.0 );
76 
77  params.high_color[0] = GFXColor( 0.5, 0.0, 0.0 );
78 
79  params.high_color[1] = GFXColor( 1.0, 0.0, 0.0 );
80 
81  params.high_ambient_color[0] = GFXColor( 0, 0, 0 );
82 
83  params.high_ambient_color[1] = GFXColor( 0, 0, 0 );
84 
85  params.scattering = 5;
86 
88 }
89 
91 {
92  return LightMap[0];
93 }
94 
96 {
97  GFXActiveTexture( stage );
98 #ifdef NV_CUBE_MAP
99  LightMap[0]->MakeActive( stage );
100 #else
101  LightMap[0]->MakeActive( stage );
102 #endif
103  GFXTextureEnv( stage, GFXADDTEXTURE );
104 #ifdef NV_CUBE_MAP
105  GFXToggleTexture( true, stage, CUBEMAP );
106  GFXTextureCoordGenMode( stage, CUBE_MAP_GEN, NULL, NULL );
107 #else
108  const float tempo[4] = {1, 0, 0, 0};
109  GFXToggleTexture( true, stage, TEXTURE2D );
110  GFXTextureCoordGenMode( stage, SPHERE_MAP_GEN, tempo, tempo );
111 #endif
112  GFXActiveTexture( 0 );
113 }
114 
116 {
118 #ifdef NV_CUBE_MAP
119  delete LightMap[0];
120  delete LightMap[1];
121  delete LightMap[2];
122  delete LightMap[3];
123  delete LightMap[4];
124  delete LightMap[5];
125 #else
126  delete LightMap[0];
127 #endif
128  delete bg;
129  delete stars;
130  //delete [] name;
131  delete bolts;
132  //delete collidetable;//BAD BAD BAD we need this to happen later!
133 
136 }
137 
139 {
140  return new ClickList( this, &drawList );
141 }
142 
143 void ConditionalCursorDraw( bool tf )
144 {
145  if (game_options.hardware_cursor)
146  winsys_show_cursor( tf );
147 }
148 
150 {
152 }
153 
155 {}
156 
158 extern double interpolation_blend_factor;
159 extern bool cam_setup_phase;
160 
161 //Class for use of UnitWithinRangeLocator template
162 //Used to do distance based pre-culling for draw function based on sorted search structure
164 {
165  struct empty {};
166  vsUMap< void*, struct empty >gravunits;
167 public:
171  {
172  parent = NULL;
173  parenttarget = NULL;
174  }
175  bool acquire( Unit *unit, float distance )
176  {
177  if ( gravunits.find( unit ) == gravunits.end() )
178  return draw( unit );
179  else
180  return true;
181  }
182  void drawParents()
183  {
184  if (parent) {
185  if ( parent->isSubUnit() ) {
187  draw( parent );
188  } else {
189  draw( parent );
190  }
191  }
192  if (parenttarget) {
193  if ( parenttarget->isSubUnit() ) {
195  draw( parenttarget );
196  } else {
197  draw( parenttarget );
198  }
199  }
200  }
201 
202  bool draw( Unit *unit )
203  {
204  if ( parent == unit || (parent && parent->isSubUnit() && parent->owner == unit) )
205  parent = NULL;
206  if ( parenttarget == unit || (parenttarget && parenttarget->isSubUnit() && parenttarget->owner == unit) )
207  parenttarget = NULL;
208  float backup = SIMULATION_ATOM;
209  unsigned int cur_sim_frame = _Universe->activeStarSystem()->getCurrentSimFrame();
211  unit->sim_atom_multiplier,
212  unit->cur_sim_queue_slot,
213  cur_sim_frame );
214  SIMULATION_ATOM = backup*unit->sim_atom_multiplier;
215  (/*(GameUnit< Unit >*)*/ unit)->Draw();
217  SIMULATION_ATOM = backup;
218  return true;
219  }
220  bool grav_acquire( Unit *unit )
221  {
222  gravunits[unit] = empty();
223  return draw( unit );
224  }
225 };
226 
227 //#define UPDATEDEBUG //for hard to track down bugs
228 void GameStarSystem::Draw( bool DrawCockpit )
229 {
230  double starttime = queryTime();
231  GFXEnable( DEPTHTEST );
235  GFXColor4f( 1, 1, 1, 1 );
236  if (DrawCockpit)
238  for (unsigned int i = 0; i < contterrains.size(); ++i)
239  contterrains[i]->AdjustTerrain( this );
240  Unit *par;
241  bool alreadysetviewport = false;
242  if ( ( par = _Universe->AccessCockpit()->GetParent() ) == NULL ) {
244  } else if ( !par->isSubUnit() ) {
245  //now we can assume world is topps
247  par->curr_physical_state,
249  Unit *targ = par->Target();
250  if ( targ && !targ->isSubUnit() ) {
252  targ->curr_physical_state,
254  }
256  alreadysetviewport = true;
257  }
258  double setupdrawtime = queryTime();
259  {
260  cam_setup_phase = true;
261 
262  Unit *saveparent = _Universe->AccessCockpit()->GetSaveParent();
263  Unit *targ = NULL;
264  if (saveparent)
265  targ = saveparent->Target();
266  //Array containing the two interesting units, so as not to have to copy-paste code
267  Unit *camunits[2] = {saveparent, targ};
268  float backup = SIMULATION_ATOM;
269  unsigned int cur_sim_frame = _Universe->activeStarSystem()->getCurrentSimFrame();
270  for (int i = 0; i < 2; ++i) {
271  Unit *unit = camunits[i];
272  //Make sure unit is not null;
273  if ( unit && !unit->isSubUnit() ) {
275  unit->sim_atom_multiplier,
276  unit->cur_sim_queue_slot,
277  cur_sim_frame );
278  SIMULATION_ATOM = backup*unit->sim_atom_multiplier;
279  ( (GameUnit< Unit >*)unit )->GameUnit< Unit >::Draw();
280  }
281  }
283  SIMULATION_ATOM = backup;
284 
285 
288 
289  cam_setup_phase = false;
290  }
291  setupdrawtime = queryTime()-setupdrawtime;
292  GFXDisable( LIGHTING );
293  bg->Draw();
294  double drawtime = queryTime();
295 
296  double maxdrawtime = 0;
297 
298  //Ballpark estimate of when an object of configurable size first becomes one pixel
299 
300  QVector drawstartpos = _Universe->AccessCamera()->GetPosition();
301 
302  Collidable key_iterator( 0, 1, drawstartpos );
303  UnitWithinRangeOfPosition< UnitDrawer >drawer( game_options.precull_dist, 0, key_iterator );
304  //Need to draw really big stuff (i.e. planets, deathstars, and other mind-bogglingly big things that shouldn't be culled despited extreme distance
305  Unit *unit;
306  if ( ( drawer.action.parent = _Universe->AccessCockpit()->GetParent() ) != NULL )
307  drawer.action.parenttarget = drawer.action.parent->Target();
308  for (un_iter iter = this->GravitationalUnits.createIterator(); (unit = *iter) != NULL; ++iter) {
309  float distance = ( drawstartpos-unit->Position() ).Magnitude()-unit->rSize();
310  if (distance < game_options.precull_dist)
311  drawer.action.grav_acquire( unit );
312  else
313  drawer.action.draw( unit );
314  }
315  //Need to get iterator to approx camera position
316  CollideMap::iterator parent = collidemap[Unit::UNIT_ONLY]->lower_bound( key_iterator );
317  findObjectsFromPosition( this->collidemap[Unit::UNIT_ONLY], parent, &drawer, drawstartpos, 0, true );
318  drawer.action.drawParents(); //draw units targeted by camera
319  //FIXME maybe we could do bolts & units instead of unit only--and avoid bolt drawing step
320 
321 #if 0
322  for (unsigned int sim_counter = 0; sim_counter <= SIM_QUEUE_SIZE; ++sim_counter) {
323  double tmp = queryTime();
324  Unit *unit;
326  float backup = SIMULATION_ATOM;
327  unsigned int cur_sim_frame = _Universe->activeStarSystem()->getCurrentSimFrame();
328  while ( ( unit = iter.current() ) != NULL ) {
330  unit->sim_atom_multiplier,
331  unit->cur_sim_queue_slot,
332  cur_sim_frame );
333  //if (par&&par->Target()==unit) {
334  //printf ("i:%f s:%f m:%d c:%d l:%d\n",interpolation_blend_factor,saved_interpolation_blend_factor,unit->sim_atom_multiplier,sim_counter,current_sim_location);
335  //}
336  SIMULATION_ATOM = backup*unit->sim_atom_multiplier;
337  ( (GameUnit< Unit >*)unit )->Draw();
338  iter.advance();
339  }
341  SIMULATION_ATOM = backup;
342  tmp = queryTime()-tmp;
343  if (tmp > maxdrawtime) maxdrawtime = tmp;
344  }
345 #endif
346  drawtime = queryTime()-drawtime;
347  WarpTrailDraw();
348 
349  GFXFogMode( FOG_OFF );
350 
351  GFXColor tmpcol( 0, 0, 0, 1 );
352  GFXGetLightContextAmbient( tmpcol );
353  double processmesh = queryTime();
354  if (!game_options.draw_near_stars_in_front_of_planets) stars->Draw();
356  if (game_options.draw_near_stars_in_front_of_planets) stars->Draw();
357  GFXEnable( DEPTHTEST );
359  //need to wait for lights to finish
363  processmesh = queryTime()-processmesh;
364  Nebula *neb;
365 
366  Matrix ident;
367  Identity( ident );
368 
370  GFXLightContextAmbient( tmpcol );
371  if ( ( neb = _Universe->AccessCamera()->GetNebula() ) )
372  neb->SetFogState();
374  Bolt::Draw();
375 
376  GFXFogMode( FOG_OFF );
381  ConditionalCursorDraw( false );
382  if (DrawCockpit)
384  double fintime = queryTime()-starttime;
386 }
387 
388 extern void update_ani_cache();
390 {
393 }
394 
396 {
398 }
399 
401 {
402  Terrain::UpdateAll( 64 );
403 }
404 
406 {
408 }
409 
411 {
412  if (_Universe->AccessCockpit()->activeStarSystem == ss) {
413  Nebula *neb;
414  if ( ( neb = _Universe->AccessCamera()->GetNebula() ) ) {
415  if (neb->getFade() <= 0)
416  //Update physics should set this
417  _Universe->AccessCamera()->SetNebula( NULL );
418  }
419  }
420 }
421 
423 {
424 #ifdef NV_CUBE_MAP
425  printf( "using NV_CUBE_MAP\n" );
426  static int max_cube_size = XMLSupport::parse_int( vs_config->getVariable( "graphics", "max_cubemap_size", "1024" ) );
427  LightMap[0] = new Texture( (xml->backgroundname+"_light.cube").c_str(), 1, TRILINEAR, CUBEMAP, CUBEMAP_POSITIVE_X,
428  GFXFALSE, max_cube_size );
429  if ( LightMap[0]->LoadSuccess() && LightMap[0]->isCube() ) {
430  LightMap[1] = LightMap[2] = LightMap[3] = LightMap[4] = LightMap[5] = 0;
431  } else {
432  delete LightMap[0];
433  LightMap[0] = new Texture( (xml->backgroundname+"_right.image").c_str(), 1, TRILINEAR, CUBEMAP, CUBEMAP_POSITIVE_X,
434  GFXFALSE, max_cube_size );
435  LightMap[1] = new Texture( (xml->backgroundname+"_left.image").c_str(), 1, TRILINEAR, CUBEMAP, CUBEMAP_NEGATIVE_X,
437  LightMap[0] );
438  LightMap[2] = new Texture( (xml->backgroundname+"_up.image").c_str(), 1, TRILINEAR, CUBEMAP, CUBEMAP_POSITIVE_Y,
440  LightMap[0] );
441  LightMap[3] = new Texture( (xml->backgroundname+"_down.image").c_str(), 1, TRILINEAR, CUBEMAP, CUBEMAP_NEGATIVE_Y,
443  LightMap[0] );
444  LightMap[4] = new Texture( (xml->backgroundname+"_front.image").c_str(), 1, TRILINEAR, CUBEMAP, CUBEMAP_POSITIVE_Z,
446  LightMap[0] );
447  LightMap[5] = new Texture( (xml->backgroundname+"_back.image").c_str(), 1, TRILINEAR, CUBEMAP, CUBEMAP_NEGATIVE_Z,
449  LightMap[0] );
450  }
451 #else
452  string bglight = xml->backgroundname+"_light.image";
453  string bgfile = xml->backgroundname+"_light.image";
454  VSFile f;
456  if (err > Ok)
457  EnvironmentMapGeneratorMain( xml->backgroundname.c_str(), bglight.c_str(), 0, xml->reflectivity, 1 );
458  else
459  f.Close();
460  LightMap[0] = new Texture( bgfile.c_str(), 1, MIPMAP, TEXTURE2D, TEXTURE_2D, GFXTRUE );
461 #endif
462 
463  bg = new Background(
464  xml->backgroundname.c_str(),
465  xml->numstars,
466  g_game.zfar*.9,
467  filename,
468  xml->backgroundColor,
469  xml->backgroundDegamma );
470  stars = new Stars( xml->numnearstars, xml->starsp );
471  stars->SetBlend( game_options.starblend, game_options.starblend );
472 }
473