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
drawsystem.cpp
Go to the documentation of this file.
1 #include <set>
2 #include "vsfilesystem.h"
3 #include "vs_globals.h"
4 #include "vegastrike.h"
5 #include "gfx/gauge.h"
6 #include "gfx/cockpit.h"
7 #include "universe.h"
8 #include "star_system.h"
9 #include "cmd/unit_generic.h"
10 #include "cmd/unit_factory.h"
11 #include "cmd/collection.h"
12 #include "gfx/hud.h"
13 #include "gfx/vdu.h"
14 #include "lin_time.h" //for fps
15 #include "config_xml.h"
16 #include "lin_time.h"
17 #include "cmd/images.h"
18 #include "cmd/script/mission.h"
19 #include "cmd/script/msgcenter.h"
20 #include "cmd/ai/flyjoystick.h"
21 #include "cmd/ai/firekeyboard.h"
22 #include "cmd/ai/aggressive.h"
23 #include "main_loop.h"
24 #include <assert.h> //needed for assert() calls
25 #include "savegame.h"
26 #include "gfx/animation.h"
27 #include "gfx/mesh.h"
28 #include "universe_util.h"
29 #include "in_mouse.h"
30 #include "gui/glut_support.h"
31 #include "networking/netclient.h"
32 #include "cmd/unit_util.h"
33 #include "math.h"
34 #include "save_util.h"
35 #include "navscreen.h"
36 #include "gfx/masks.h"
37 #include "galaxy_gen.h"
38 
39 //**********************************
40 //Main function for drawing a CURRENT system
41 //works :
42 //scans all items, records min + max coords of the system, for relevant items
43 //rescans, and enlists the found items that it wants drawn
44 //- items with mouse over them will go into a mouselist.
45 //draws the draw lists, with the mouse lists cycled 'n' times (according to kliks)
46 //**********************************
47 
49 {
51  if ( !(*bleh) )
52  return;
53 //string mystr ("3d "+XMLSupport::tostring (system_view));
54 //UniverseUtil::IOmessage (0,"game","all",mystr);
55 
56  //what's my name
57  //***************************
58  TextPlane systemname; //will be used to display shits names
60  //GFXColor factioncolor = factioncolours[faction];
61  string systemnamestring = "#ff0000Sector: #ffff00"+getStarSystemSector( _Universe->activeStarSystem()->getFileName() )
62  +" #ff0000Current System: #ffff00"+_Universe->activeStarSystem()->getName()+" ("
63  +FactionUtil::GetFactionName( faction )
64  +"#ffff00)";
65  //int length = systemnamestring.size();
66  //float offset = (float(length)*0.001);
67  //systemname.SetPos( (((screenskipby4[0]+screenskipby4[1])/2)-offset) , screenskipby4[3]); // middle position
68  systemname.SetPos( screenskipby4[0]+0.03, screenskipby4[3]+0.02 ); //left position
69  systemname.col = GFXColor( 1, 1, .7, 1 );
70  systemname.SetText( systemnamestring );
71 //systemname.SetCharSize(1, 1);
72  static float background_alpha =
73  XMLSupport::parse_float( vs_config->getVariable( "graphics", "hud", "text_background_alpha", "0.0625" ) );
74  GFXColor tpbg = systemname.bgcol;
75  bool automatte = (0 == tpbg.a);
76  if (automatte) systemname.bgcol = GFXColor( 0, 0, 0, background_alpha );
77  systemname.Draw( systemnamestring, 0, true, false, automatte );
78  systemname.bgcol = tpbg;
79  //***************************
80 
81 //navdrawlist mainlist(0, screenoccupation, factioncolours); // lists of items to draw
82 //mainlist.unselectedalpha = unselectedalpha;
83  navdrawlist mouselist( 1, screenoccupation, factioncolours ); //lists of items to draw that are in mouse range
84 
85  QVector pos; //item position
86  QVector pos_flat; //item position flat on plane
87 
88  float zdistance = 0.0;
89  float zscale = 0.0;
90 
91  Adjust3dTransformation( system_view == VIEW_3D, 1 );
92  //Set up first item to compare to + centres
93  //**********************************
94  while ( (*bleh) && ( _Universe->AccessCockpit()->GetParent() != (*bleh) )
95  && ( UnitUtil::isSun( *bleh ) || !UnitUtil::isSignificant( *bleh ) ) ) //no sun's in initial setup
96  ++bleh;
97  if ( !(*bleh) ) //nothing there that's significant, just do it all
99  //GET THE POSITION
100  //*************************
101  pos = (*bleh)->Position();
102  ReplaceAxes( pos );
103  //*************************
104 
105  //Modify by old rotation amount
106  //*************************
107 //if(system_view==VIEW_3D)
108 //{
109 //pos = dxyz(pos, 0, ry_s, 0);
110 //pos = dxyz(pos, rx_s, 0, 0);
111 //}
112  //*************************
113 
114  float max_x = (float) pos.i;
115  float min_x = (float) pos.i;
116  float max_y = (float) pos.j;
117  float min_y = (float) pos.j;
118  float max_z = (float) pos.k;
119  float min_z = (float) pos.k;
120 
121 //float themaxvalue = fabs(pos.i);
122  themaxvalue = 0.0;
123 
124  float center_nav_x = ( (screenskipby4[0]+screenskipby4[1])/2 );
125  float center_nav_y = ( (screenskipby4[2]+screenskipby4[3])/2 );
126  //**********************************
127  //Retrieve unit data min/max
128  //**********************************
129  while (*bleh) {
130  //this goes through one time to get the major components locations, and scales its output appropriately
131  if ( UnitUtil::isSun( *bleh ) ) {
132  ++bleh;
133  continue;
134  }
135  string temp = (*bleh)->name;
136  pos = (*bleh)->Position();
137  ReplaceAxes( pos );
138  //Modify by old rotation amount
139  //*************************
140 //if(system_view==VIEW_3D)
141 //{
142 //pos = dxyz(pos, 0, ry_s, 0);
143 //pos = dxyz(pos, rx_s, 0, 0);
144 //}
145  //*************************
146  //*************************
147  if ( ( UnitUtil::isSignificant( *bleh ) ) || ( _Universe->AccessCockpit()->GetParent() == (*bleh) ) )
148  RecordMinAndMax( pos, min_x, max_x, min_y, max_y, min_z, max_z, themaxvalue );
149  ++bleh;
150  }
151  //**********************************
152 
153  //Find Centers
154  //**********************************
155  center_x = (min_x+max_x)/2;
156  center_y = (min_y+max_y)/2;
157  center_z = (min_z+max_z)/2;
158  //**********************************
159 
160  max_x = 2*max_x-center_x;
161  max_y = 2*max_y-center_y;
162  max_z = 2*max_z-center_z;
163  min_x = 2*min_x-center_x;
164  min_y = 2*min_y-center_y;
165  min_z = 2*min_z-center_z;
166 
167  themaxvalue *= 2;
168 
169 //#define SQRT3 1.7320508
170 //themaxvalue = sqrt(themaxvalue*themaxvalue + themaxvalue*themaxvalue + themaxvalue*themaxvalue);
171 //themaxvalue = SQRT3*themaxvalue;
172 
173  //Set Camera Distance
174  //**********************************
175 //{
176  float half_x = (max_x-min_x);
177  float half_y = (max_y-min_y);
178  float half_z = (max_z-min_z);
179 
180  camera_z = sqrt( (half_x*half_x)+(half_y*half_y)+(half_z*half_z) );
181 
182 //float halfmax = 0.5*themaxvalue;
183 //camera_z = sqrt( (halfmax*halfmax) + (halfmax*halfmax) + (halfmax*halfmax) );
184 //camera_z = 4.0*themaxvalue;
185 //}
186 
187  //**********************************
188 
189  DrawOriginOrientationTri( center_nav_x, center_nav_y, 1 );
190 
191 /*
192  * string mystr ("max x "+XMLSupport::tostring (max_x));
193  * UniverseUtil::IOmessage (0,"game","all",mystr);
194  *
195  * string mystr2 ("min x "+XMLSupport::tostring (min_x));
196  * UniverseUtil::IOmessage (0,"game","all",mystr2);
197  *
198  * string mystr3 ("max y "+XMLSupport::tostring (max_y));
199  * UniverseUtil::IOmessage (0,"game","all",mystr3);
200  *
201  * string mystr4 ("min y "+XMLSupport::tostring (min_y));
202  * UniverseUtil::IOmessage (0,"game","all",mystr4);
203  *
204  * string mystrcx ("center x "+XMLSupport::tostring (center_x));
205  * UniverseUtil::IOmessage (0,"game","all",mystrcx);
206  *
207  * string mystrcy ("center y "+XMLSupport::tostring (center_y));
208  * UniverseUtil::IOmessage (0,"game","all",mystrcy);
209  */
210 
212 
213  //Enlist the items and attributes
214  //**********************************
216  while (*blah) {
217  //this draws the points
218 
219  //Retrieve unit data
220  //**********************************
221  string temp = (*blah)->name;
222 
223  pos = (*blah)->Position();
224  ReplaceAxes( pos );
225 
226  float the_x, the_y, the_x_flat, the_y_flat, system_item_scale_temp;
227  TranslateCoordinates( pos,
228  pos_flat,
229  center_nav_x,
230  center_nav_y,
231  themaxvalue,
232  zscale,
233  zdistance,
234  the_x,
235  the_y,
236  the_x_flat,
237  the_y_flat,
238  system_item_scale_temp,
239  1 );
240  //IGNORE OFF SCREEN
241  //**********************************
242  if ( !TestIfInRange( screenskipby4[0], screenskipby4[1], screenskipby4[2], screenskipby4[3], the_x, the_y ) ) {
243  ++blah;
244  continue;
245  }
246  //**********************************
247 
248  //Now starts the test that determines the type of things and inserts
249  //|
250  //|
251  //\/
252 
253  float insert_size = 0.0;
254  int insert_type = navambiguous;
255  if ( (*blah)->isUnit() == UNITPTR ) {
256  //unit
257  /*if(UnitUtil::isPlayerStarship(*blah) > -1) // is a PLAYER SHIP
258  * {
259  * if (UnitUtil::isPlayerStarship (*blah)==UniverseUtil::getCurrentPlayer()) // is THE PLAYER
260  * {
261  * insert_type = navcurrentplayer;
262  * insert_size = navcurrentplayersize;
263  * }
264  * else // is A PLAYER
265  * {
266  * insert_type = navplayer;
267  * insert_size = navplayersize;
268  * }
269  * }
270  * else // is a non player ship
271  * {*/
272  if ( UnitUtil::isSignificant( *blah ) ) {
273  //capship or station
274  if ( (*blah)->GetComputerData().max_speed() == 0 ) {
275  //is this item STATIONARY?
276  insert_type = navstation;
277  insert_size = navstationsize;
278  } else {
279  //it moves = capship
280  if ( ThePlayer->InRange( (*blah), false, false ) ) {
281  //only insert if in range
282  insert_type = navcapship;
283  insert_size = navcapshipsize;
284  } else {
285  //skip unit completely if not in range
286  ++blah;
287  continue;
288  }
289  }
290  } else {
291  //fighter
292  /*if(ThePlayer->InRange((*blah),false,false)) // only insert if in range
293  * {
294  * insert_type = navfighter;
295  * insert_size = navfightersize;
296  * }
297  * else // skip unit completely if not in range
298  * {
299  * ++blah;
300  * continue;
301  * }*/
302  if (UnitUtil::isPlayerStarship( *blah ) > -1) {
303  //is THE PLAYER
304  insert_type = navfighter;
305  insert_size = navfightersize;
306  } else {
307  //skip unit completely if not in range
308  ++blah;
309  continue;
310  }
311  }
312  //}
313  } else if ( (*blah)->isUnit() == PLANETPTR ) {
314  //is it a PLANET?
315  if ( UnitUtil::isSun( *blah ) ) {
316  //is this a SUN?
317  insert_type = navsun;
318  insert_size = navsunsize;
319  } else if ( !( (*blah)->GetDestinations().empty() ) ) {
320  //is a jump point (has destinations)
321  insert_type = navjump;
322  insert_size = navjumpsize;
323  } else {
324  //its a planet
325  insert_type = navplanet;
326  insert_size = navplanetsize;
327  }
328  } else if ( (*blah)->isUnit() == MISSILEPTR ) {
329  //a missile
330  insert_type = navmissile;
331  insert_size = navmissilesize;
332  } else if ( (*blah)->isUnit() == ASTEROIDPTR ) {
333  //an asteroid
334  insert_type = navasteroid;
335  insert_size = navasteroidsize;
336  } else if ( (*blah)->isUnit() == NEBULAPTR ) {
337  //a nebula
338  insert_type = navnebula;
339  insert_size = navnebulasize;
340  } else {
341  //undefined non unit
342  insert_type = navambiguous;
343  insert_size = navambiguoussize;
344  }
345  if ( system_item_scale_temp > (system_item_scale*3) )
346  system_item_scale_temp = (system_item_scale*3);
347  insert_size *= system_item_scale_temp;
348  if ( _Universe->AccessCockpit()->GetParent()->Target() == (*blah) ) {
349  //Get a color from the config
350  static float col[4] = {1, 0.3, 0.3, 0.8};
351  static bool init = false;
352  if (!init) {
353  vs_config->getColor( "nav", "targeted_unit", col, true );
354  init = true;
355  }
356  DrawTargetCorners( the_x, the_y, insert_size, GFXColor( col[0], col[1], col[2], col[3] ) );
357  }
358  bool tests_in_range = 0;
359  if (insert_type == navstation)
360  tests_in_range = TestIfInRangeBlk( the_x, the_y, insert_size, mouse_x_current, mouse_y_current );
361  else
362  tests_in_range = TestIfInRangeRad( the_x, the_y, insert_size, mouse_x_current, mouse_y_current );
363  Unit *myunit = (*blah);
364 
365  ++blah;
366  DisplayOrientationLines( the_x, the_y, the_x_flat, the_y_flat, 1 );
367  if (tests_in_range) {
368  mouselist.insert( insert_type, insert_size, the_x, the_y, myunit );
369  } else {
370  drawlistitem( insert_type,
371  insert_size,
372  the_x,
373  the_y,
374  myunit,
375  screenoccupation,
376  false,
377  (*blah) ? true : false,
378  unselectedalpha,
379  factioncolours );
380  }
381  }
382  //********************************** // done enlisting items and attributes
383  //Adjust mouse list for 'n' kliks
384  //**********************************
385  //STANDARD : (1 3 2) ~ [0] [2] [1]
386  //VS : (1 2 3) ~ [0] [1] [2] <-- use this
387  if (mouselist.get_n_contents() > 0) {
388  //mouse is over a target when this is > 0
389  if (mouse_wentdown[2] == 1) //mouse button went down for mouse button 2(standard)
390  rotations += 1;
391  }
392  if ( rotations >= mouselist.get_n_contents() ) //dont rotate more than there is
393  rotations = 0;
394  int r = 0;
395  while (r < rotations) {
396  //rotate whatver rotations, leaving n rotated items, tail on top
397  mouselist.rotate();
398  r += 1;
399  }
400  //**********************************
401  //Draw the damn shit
402  //**********************************
403 //mainlist.draw(); // draw the items
404 //mainlist.wipe(); // whipe the list
405  //**********************************
406  //Check for selection query
407  //give back the selected tail IF there is one
408  //IF given back, undo the selection state
409  //**********************************
410  if ( 1 || checkbit( buttonstates, 1 ) ) {
411  //button #2 is down, wanting a (selection)
412  if (mouselist.get_n_contents() > 0) {
413  //mouse is over a target when this is > 0
414  if (mouse_wentdown[0] == 1) {
415  //mouse button went down for mouse button 1
416  currentselection = mouselist.gettailunit();
417  unsetbit( buttonstates, 1 );
418  //JUST FOR NOW, target == current selection. later it'll be used for other shit, that will then set target.
419  if ( currentselection.GetUnit() ) {
420  ( UniverseUtil::getPlayerX( UniverseUtil::getCurrentPlayer() ) )->Target( currentselection.GetUnit() );
421  ( UniverseUtil::getPlayerX( UniverseUtil::getCurrentPlayer() ) )->LockTarget( currentselection.GetUnit() );
422  }
423  }
424  }
425  }
426  //**********************************
427 
428  //Clear the lists
429  //**********************************
430  mouselist.draw(); //draw mouse over'd items
431  mouselist.wipe(); //whipe mouse over'd list
432  //**********************************
433 }
434 //**********************************
435