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
universe.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn & Alan Shieh
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include "gfxlib.h"
24 #include "universe.h"
25 #include "lin_time.h"
26 #include "in.h"
27 #include "gfx/aux_texture.h"
28 #include "profile.h"
29 #include "gfx/cockpit.h"
30 #include "cmd/weapon_xml.h"
31 #include "galaxy_xml.h"
32 #include <algorithm>
33 #include "config_xml.h"
34 #include "vs_globals.h"
35 #include "xml_support.h"
36 #include "audiolib.h"
37 #include "cmd/script/mission.h"
38 #include "cmd/unit.h"
39 #include "in_kb.h"
40 #include "in_kb_data.h"
41 #include "in_main.h"
42 #if defined (__APPLE__)
43 #import <sys/param.h>
44 #endif
45 #include "savegame.h"
46 #include "gfx/screenshot.h"
47 #include "universe_util.h"
48 
49 #include <algorithm>
50 #include <string>
51 #include <vector>
52 
53 #include "options.h"
54 
56 using std::string;
57 using std::vector;
58 using std::find;
60 extern void CacheJumpStar( bool );
61 extern void SortStarSystems( vector< StarSystem* > &ss, StarSystem *drawn );
62 StarSystem* GameUniverse::Init( string systemfile, const Vector &centr, const string planetname )
63 {
64  static bool js = true;
65  if (js) {
66  js = false;
68  CacheJumpStar( false );
69  }
70  return this->Universe::Init( systemfile, centr, planetname );
71 }
72 
73 extern void bootstrap_first_loop();
74 
75 void GameUniverse::Init( int argc, char **argv, const char *galaxy )
76 {
77  current_cockpit = 0;
78  //Select drivers
79 #if defined (__APPLE__)
80  //get the current working directory so when glut trashes it we can restore.
81  char pwd[MAXPATHLEN];
82  getcwd( pwd, MAXPATHLEN );
83 #endif
84  GFXInit( argc, argv );
85 #if defined (__APPLE__)
86  //Restore it
87  chdir( pwd );
88 #endif
89  StartGFX();
90  InitInput();
91 
92  hud_camera = Camera();
93 
94  //Hasten splash screen loading, to cover up lengthy universe initialization
96 
97  this->Universe::Init( galaxy );
98 }
99 
100 GameUniverse::GameUniverse( int argc, char **argv, const char *galaxy )
101 {
102  this->Init( argc, argv, galaxy );
103  is_server = false;
104 }
105 
107 {}
108 
110 {
111  DeInitInput();
112  GFXShutdown();
113 }
114 
115 //sets up all the stuff... in this case the ships to be rendered
116 
117 void GameUniverse::SetupCockpits( vector< string >playerNames )
118 {
119  for (unsigned int i = 0; i < playerNames.size(); ++i) {
120  cockpit.push_back( NULL );
121  int temp = cockpit.size();
122  cockpit.back() = new GameCockpit( "", NULL, playerNames[i] );
123  temp = cockpit.size();
124  }
125 }
126 
128 {
129  getActiveStarSystem( 0 )->activateLightMap( stage );
130 }
131 
133 {
134  return getActiveStarSystem( 0 )->getLightMap();
135 }
136 
138 {
139  GFXBeginScene();
140  GFXMaterial mat;
141  mat.ar = 1.00F;
142  mat.ag = 1.00F;
143  mat.ab = 1.00F;
144  mat.aa = 1.00F;
145 
146  mat.dr = 1.00F;
147  mat.dg = 1.00F;
148  mat.db = 1.00F;
149  mat.da = 1.00F;
150 
151  mat.sr = 1.00F;
152  mat.sg = 1.00F;
153  mat.sb = 1.00F;
154  mat.sa = 1.00F;
155 
156  mat.er = 0.0F;
157  mat.eg = 0.0F;
158  mat.eb = 0.0F;
159  mat.ea = 1.0F;
160  mat.power = 60.0F;
161  unsigned int tmp;
162  GFXSetMaterial( tmp, mat );
163  GFXSelectMaterial( tmp );
164  int ligh;
165  GFXCreateLightContext( ligh );
166  GFXSetLightContext( ligh );
167  GFXLightContextAmbient( GFXColor( 0, 0, 0, 1 ) );
168  GFXEndScene();
169 }
170 
172 {
173  GFXLoop( main_loop );
174 }
175 
176 void CalculateCoords( unsigned int i, unsigned int size, float &x, float &y, float &w, float &h )
177 {
178  if (size <= 1) {
179  x = y = 0;
180  w = h = 1;
181  return;
182  }
183  if ( size <= 3 || i < (size/2) ) {
184  y = 0;
185  h = 1;
186  w = 1./( (float) size );
187  x = ( (float) i )/size;
188  if (size > 3)
189  h = .5;
190  }
191  if (size > 3) {
192  if (i > size/2) {
193  y = .5;
194  h = .5;
195  x = ( (float) i-(size/2) )/(size-size/2);
196  w = 1/(size-size/2);
197  }
198  }
199 }
200 
201 extern bool RefreshGUI();
202 extern float rand01();
203 extern int timecount;
204 
205 inline void loadsounds( const string &str, const int max, soundArray &snds, bool loop = false )
206 {
207  char addstr[2] = {'\0'};
208  snds.allocate( max );
209  for (int i = 0; i < max; ++i) {
210  addstr[0] = '1'+i;
211  string mynewstr = str;
212  while (1) {
213  std::string::size_type found = mynewstr.find( '?' );
214  if (found != std::string::npos) {
215  mynewstr[found] = addstr[0];
216  } else {
217  break;
218  }
219  }
220  snds.ptr[i].loadsound( mynewstr, loop );
221  }
222 }
223 
225 {
226  static int lasttimecompress = 0;
227  if ( (timecount != lasttimecompress) && (game_options.compress_max > 0) ) {
228  static bool inittimecompresssounds = false;
229  static soundArray loop_snds;
230  static soundArray burst_snds;
231  static soundArray end_snds;
232  if (inittimecompresssounds == false) {
233  loadsounds( game_options.compress_loop, game_options.compress_max, loop_snds, true );
234  loadsounds( game_options.compress_stop, game_options.compress_max, end_snds );
235  loadsounds( game_options.compress_change, game_options.compress_max, burst_snds );
236  inittimecompresssounds = true;
237  }
238  int soundfile = (timecount-1)/game_options.compress_interval;
239  int lastsoundfile = (lasttimecompress-1)/game_options.compress_interval;
240  if (timecount > 0 && lasttimecompress >= 0) {
241  if ( (soundfile+1) >= game_options.compress_max ) {
242  burst_snds.ptr[game_options.compress_max-1].playsound();
243  } else {
244  if ( lasttimecompress > 0 && loop_snds.ptr[lastsoundfile].sound >= 0
245  && AUDIsPlaying( loop_snds.ptr[lastsoundfile].sound ) )
246  AUDStopPlaying( loop_snds.ptr[lastsoundfile].sound );
247  loop_snds.ptr[soundfile].playsound();
248  burst_snds.ptr[soundfile].playsound();
249  }
250  } else if (lasttimecompress > 0 && timecount == 0) {
251  for (int i = 0; i < game_options.compress_max; ++i)
252  if ( loop_snds.ptr[i].sound >= 0 && AUDIsPlaying( loop_snds.ptr[i].sound ) )
253  AUDStopPlaying( loop_snds.ptr[i].sound );
254  if (lastsoundfile >= game_options.compress_max)
255  end_snds.ptr[game_options.compress_max-1].playsound();
256  else
257  end_snds.ptr[lastsoundfile].playsound();
258  }
259  lasttimecompress = timecount;
260  }
261 }
262 
263 extern bool screenshotkey;
264 
265 extern int getmicrosleep();
267 {
268 #ifndef WIN32
269  RESETTIME();
270 #endif
271  GFXBeginScene();
272  size_t i;
273  StarSystem *lastStarSystem = NULL;
274  for (i = 0; i < cockpit.size(); ++i) {
275  SetActiveCockpit( i );
276  float x, y, w, h;
277  CalculateCoords( i, cockpit.size(), x, y, w, h );
278  AccessCamera()->SetSubwindow( x, y, w, h );
279  if (cockpit.size() > 1 && AccessCockpit( i )->activeStarSystem != lastStarSystem) {
280  active_star_system[0]->SwapOut();
281  lastStarSystem = AccessCockpit()->activeStarSystem;
282  active_star_system[0] = lastStarSystem;
283  lastStarSystem->SwapIn();
284  }
286  if (cockpit.size() > 0)
287  AccessCamera()->UpdateGFX();
289  activeStarSystem()->Draw();
290  AccessCamera()->SetSubwindow( 0, 0, 1, 1 );
291  }
292  UpdateTime();
294  _Universe->SetActiveCockpit( ( (int) ( rand01()*cockpit.size() ) )%cockpit.size() );
295  for (i = 0; i < star_system.size() && i < game_options.NumRunningSystems; ++i)
296  star_system[i]->Update( (i == 0) ? 1 : game_options.InactiveSystemTime/i, true );
298  for (i = 0; i < cockpit.size(); ++i) {
299  SetActiveCockpit( i );
301  ProcessInput( i ); //input neesd to be taken care of;
303  }
304  if (screenshotkey) {
305  KBData b;
306  Screenshot( b, PRESS );
307  screenshotkey = false;
308  }
309  GFXEndScene();
310  //so we don't starve the audio thread
312 
313  //remove systems not recently visited?
314  static int sorttime = 0;
315  if (game_options.garbagecollectfrequency != 0) {
316  //don't want to delete something when there is something pending to jump therexo
317  if ( PendingJumpsEmpty() ) {
318  if ( (++sorttime)%game_options.garbagecollectfrequency == 1 ) {
320  if (star_system.size() > game_options.numoldsystems && game_options.deleteoldsystems) {
321  if ( std::find( active_star_system.begin(), active_star_system.end(),
322  star_system.back() ) == active_star_system.end() ) {
323  delete star_system.back();
324  star_system.pop_back();
325  } else {
326  VSFileSystem::vs_fprintf( stderr, "error with active star system list\n" );
327  }
328  }
329  }
330  }
331  }
332 }
333 
334 void GameUniverse::WriteSaveGame( bool auto_save )
335 {
336  for (unsigned int i = 0; i < cockpit.size(); ++i) {
337  if ( AccessCockpit( i ) ) {
338  ::WriteSaveGame( AccessCockpit( i ), auto_save );
339 #if 0
340  if ( AccessCockpit( i )->GetParent() )
341  if (AccessCockpit( i )->GetParent()->GetHull() > 0) {
343  i )->activeStarSystem->getFileName().c_str(),
346  AccessCockpit( i )->GetParent()->WriteUnit( AccessCockpit( i )->GetUnitModifications().c_str() );
347  }
348 
349 #endif
350 
351  }
352  }
353 }
354 
355 extern StarSystem * GetLoadedStarSystem( const char *system );
356 
357 StarSystem* GameUniverse::GenerateStarSystem( const char *file, const char *jumpback, Vector center )
358 {
359  StarSystem *tmpcache;
360  if ( ( tmpcache = GetLoadedStarSystem( file ) ) )
361  return tmpcache;
362  this->Generate1( file, jumpback );
363  StarSystem *ss = new GameStarSystem( file, center );
364  this->Generate2( ss );
365  return ss;
366 }