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
briefing.cpp
Go to the documentation of this file.
1 #include "briefing.h"
2 #include "unit_generic.h"
3 #include "unit_factory.h"
4 #include "gfx/mesh.h"
5 #include "script/mission.h"
6 #include "gfx/ani_texture.h"
7 #include "gfx/matrix.h"
8 Briefing::Ship::Ship( const char *filename, int faction, const Vector &position )
9 {
10  VSCONSTRUCT2( 's' )
11  Unit*tmp = UnitFactory::createUnit( filename, true, faction );
12  meshdata = tmp->StealMeshes();
13  tmp->Kill();
14  cloak = 1;
15  SetPosition( position );
16 }
17 void Briefing::SetCloak( int w, float c ) //FIXME This could use better names than "w" and "c" --chuck starchaser
18 {
19  if ( w >= 0 && w < static_cast<int>(starships.size()) )
20  starships[w]->cloak = c;
21 }
22 bool UpdatePosition( Vector &res, Vector cur, Vector fin, float speed )
23 {
24  Vector direction( fin-cur );
25  float dirmag = direction.Magnitude();
26  bool ret = true;
27  if (dirmag > 0 && dirmag > speed*SIMULATION_ATOM) {
28  direction = direction*(speed*SIMULATION_ATOM/dirmag);
29  ret = false;
30  }
31  res = direction+cur;
32  return ret;
33 }
34 void SetDirection( Matrix &mat, Vector start, Vector end, const Matrix cam, bool updatepos )
35 {
36  end = end-start;
37  if (end.MagnitudeSquared() > .000001) {
38  Vector p;
39  Vector q( -cam.getR() );
40  Vector r( end );
41  Normalize( r );
42  q = q-r*( r.Dot( q )/r.MagnitudeSquared() );
43  if (q.MagnitudeSquared() < .000001) {
44  q = r;
45  q.Yaw( M_PI/2 );
46  q.Pitch( M_PI/2 );
47  q = q-r*( r.Dot( q )/r.MagnitudeSquared() );
48  }
49  Normalize( q );
50  ScaledCrossProduct( q, r, p );
51  VectorAndPositionToMatrix( mat, p, q, r, QVector( 0, 0, 0 ) );
52  }
53 }
54 extern double interpolation_blend_factor;
56 {
58 
59  cam.SetPosition( cam.GetPosition(), Vector( 0, 0, 0 ), Vector( 0, 0, 0 ), Vector( 0, 0, 0 ) );
61  for (unsigned int i = 0; i < starships.size(); i++)
65  GFXBlendMode( ONE, ZERO );
67  //
68  GFXClear( GFXTRUE );
71 }
72 
73 void Briefing::Ship::Render( const Matrix &cam, double interpol )
74 {
75  Matrix final;
76  Identity( final );
77  Vector pos( Position() );
78  Vector dir = Position()+Vector( 1, 0, 0 );
79  if ( !orders.empty() ) {
80  UpdatePosition( pos, Position(), orders.front().vec, orders.front().speed*interpol );
81  dir = orders.front().vec;
82  }
83  SetDirection( final, pos, dir, cam, !orders.empty() );
84  final.p = pos.Cast();
85 
86  Matrix camfinal;
87  MultMatrix( camfinal, cam, final );
88  for (unsigned int i = 0; i < meshdata.size(); i++) {
89  int scloak = int( cloak*( (-1) > 1 ) ); //FIXME short fix?
90  if ( (scloak&0x1) == 0 )
91  scloak += 1;
92  meshdata[i]->Draw( 1, camfinal, 1, cloak > .99 ? -1 : scloak );
93  }
94 }
95 
97 {
98  Destroy();
100 }
101 
103 {
104  for (unsigned int i = 0; i < meshdata.size(); i++)
105  delete meshdata[i];
106  meshdata.clear();
107 }
108 
110 {
111  VSCONSTRUCT2( 'b' )
112  cam.SetPosition( QVector( 0, 0, 0 ), Vector( 0, 0, 0 ), Vector( 0, 0, 0 ), Vector( 0, 0, 0 ) );
113  cam.SetOrientation( Vector( 1, 0, 0 ), Vector( 0, 1, 0 ), Vector( 0, 0, 1 ) );
114  tp.SetPos( -1, 1 );
115  tp.SetSize( 1, -.5 );
116 }
118 {
120  for (unsigned int i = 0; i < starships.size(); i++)
121  delete starships[i];
122  starships.clear();
123 }
124 
125 void Briefing::RemoveStarship( int which )
126 {
127  if (which < (int) starships.size() && which >= 0)
128  starships[which]->Destroy();
129 }
130 void Briefing::EnqueueOrder( int which, const Vector &dest, float time )
131 {
132  if (which < (int) starships.size() && which >= 0)
133  starships[which]->EnqueueOrder( dest, time );
134 }
135 void Briefing::OverrideOrder( int which, const Vector &dest, float time )
136 {
137  if (which < (int) starships.size() && which >= 0)
138  starships[which]->OverrideOrder( dest, time );
139 }
140 void Briefing::SetPosition( int which, const Vector &Position )
141 {
142  if (which < (int) starships.size() && which >= 0)
143  starships[which]->SetPosition( Position );
144 }
146 {
147  if (which < (int) starships.size() && which >= 0)
148  return starships[which]->Position();
149  return Vector( 0, 0, 0 );
150 }
151 int Briefing::AddStarship( const char *fn, int faction, const Vector &pos )
152 {
153  Ship *tmp = new Ship( fn, faction, pos );
154  if ( tmp->LoadFailed() ) {
155  delete tmp;
156  return -1;
157  }
158  starships.push_back( tmp );
159  return starships.size()-1;
160 }
162 {
163  for (unsigned int i = 0; i < starships.size(); i++)
164  starships[i]->Update();
165 }
167 {
168  if ( !orders.empty() ) {
169  Vector finpos;
170  if ( UpdatePosition( finpos, Position(), orders.front().vec, orders.front().speed ) ) {
171  orders.pop_front();
172  }
173  SetPosition( finpos );
174  }
175 }
176 void Briefing::Ship::OverrideOrder( const Vector &destination, float time )
177 {
178  orders.clear();
179  EnqueueOrder( destination, time );
180 }
181 
182 void Briefing::Ship::EnqueueOrder( const Vector &destination, float time )
183 {
184  if (time < .00001)
185  time = SIMULATION_ATOM;
186  orders.push_back( BriefingOrder( destination, ( destination-Position() ).Magnitude()/time ) );
187 }
188