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
unit_physics.h
Go to the documentation of this file.
1 #ifndef __UNIT_PHYSICS_CPP__
2 #define __UNIT_PHYSICS_CPP__
3 
4 /*
5  * Vega Strike
6  * Copyright (C) 2001-2002 Daniel Horn
7  *
8  * http://vegastrike.sourceforge.net/
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  */
24 
25 #include "gfx/mesh.h"
26 #include "unit.h"
27 #include "lin_time.h"
28 #include "beam.h"
29 #include "planet.h"
30 #include "audiolib.h"
31 #include "configxml.h"
32 #include "vs_globals.h"
33 
34 #ifdef FIX_TERRAIN
35 #include "gfx/planetary_transform.h"
36 #endif
37 #include "gfx/cockpit.h"
38 #include "unit_util.h"
39 #include "universe_util.h"
40 #include "cmd/script/mission.h"
42 #include "networking/netclient.h"
43 
44 extern float copysign( float x, float y );
45 
46 extern unsigned int apply_float_to_unsigned_int( float tmp ); //short fix
47 
48 template < class UnitType >
50  const Transformation &old_physical_state,
51  const Vector &accel,
52  float difficulty,
53  const Matrix &transmat,
54  const Vector &cum_vel,
55  bool lastframe,
56  UnitCollection *uc )
57 {
58  int player = -1;
59 
60  UnitType::UpdatePhysics2( trans, old_physical_state, accel, difficulty, transmat, cum_vel, lastframe, uc );
61  //Here send (new position + direction = curr_physical_state.position and .orientation)
62  //+ speed to server (which velocity is to consider ?)
63  //+ maybe Angular velocity to anticipate rotations in the other network clients
64  if (Network != NULL) {
65  //Check if this is a player, because in network mode we should only send updates of our moves
66  player = _Universe->whichPlayerStarship( this );
67  if (player >= 0 /* && this->networked */) {
68  if ( Network[0].isTime() ) {
69  /* If you're going to send an alive message, you might as well send your position while you're at it. */
70  ClientState cstmp( this->serial, this->curr_physical_state, this->Velocity, accel, this->AngularVelocity, 0 );
71  Network[player].sendPosition( &cstmp );
72  }
73  this->AddVelocity( difficulty );
74  } else {
75  //Not a player so update the unit's position and stuff with the last received snapshot from the server
76  //This may be be a bot or a unit controlled by the server
77  if (!this->networked) {
78  //Case it is a local unit
79  this->AddVelocity( difficulty );
80  } else {
81  //Networked unit so interpolate its position
82  this->AddVelocity( difficulty );
83 
84  this->curr_physical_state = Network[0].Interpolate( this, SIMULATION_ATOM );
85  }
86  }
87  } else {
88  this->AddVelocity( difficulty );
89  }
90 #ifdef DEPRECATEDPLANETSTUFF
91  if (planet) {
92  Matrix basis;
93  curr_physical_state.to_matrix( this->cumulative_transformation_matrix );
94  Vector p, q, r, c;
95  MatrixToVectors( this->cumulative_transformation_matrix, p, q, r, c );
96  planet->trans->InvTransformBasis( this->cumulative_transformation_matrix, p, q, r, c );
97  planet->cps = Transformation::from_matrix( this->cumulative_transformation_matrix );
98  }
99 #endif
100  this->cumulative_transformation = this->curr_physical_state;
101  this->cumulative_transformation.Compose( trans, transmat );
102  this->cumulative_transformation.to_matrix( this->cumulative_transformation_matrix );
103  this->cumulative_velocity = TransformNormal( transmat, this->Velocity )+cum_vel;
104  unsigned int i;
105  if (lastframe) {
106  char tmp = 0;
107  double blah = queryTime();
108  for (i = 0; i < this->meshdata.size(); i++) {
109  if (!this->meshdata[i])
110  continue;
111  tmp |= this->meshdata[i]->HasBeenDrawn();
112  if ( !this->meshdata[i]->HasBeenDrawn() )
113  this->meshdata[i]->UpdateFX( SIMULATION_ATOM );
114  this->meshdata[i]->UnDraw();
115  }
116  double blah1 = queryTime();
117  if (!tmp && this->hull < 0)
118  Explode( false, SIMULATION_ATOM );
119  double blah2 = queryTime();
120  }
121 }
122 
123 /****************************** ONLY SOUND/GFX STUFF LEFT IN THOSE FUNCTIONS *********************************/
124 
125 template < class UnitType >
126 void GameUnit< UnitType >::Thrust( const Vector &amt1, bool afterburn )
127 {
128  if (this->afterburntype == 0)
129  afterburn = afterburn && this->energy > this->afterburnenergy*SIMULATION_ATOM;
130  if (this->afterburntype == 1)
131  afterburn = afterburn && this->fuel > 0;
132  if (this->afterburntype == 2)
133  afterburn = afterburn && this->warpenergy > 0;
134  Unit::Thrust( amt1, afterburn );
135 
136  static bool must_afterburn_to_buzz =
137  XMLSupport::parse_bool( vs_config->getVariable( "audio", "buzzing_needs_afterburner", "false" ) );
138  if (_Universe->isPlayerStarship( this ) != NULL) {
139  static int playerengine = AUDCreateSound( vs_config->getVariable( "unitaudio",
140  "player_afterburner",
141  "sfx10.wav" ), true );
142  static float enginegain = XMLSupport::parse_float( vs_config->getVariable( "audio", "afterburner_gain", ".5" ) );
143  if ( afterburn != AUDIsPlaying( playerengine ) ) {
144  if (afterburn)
145  AUDPlay( playerengine, QVector( 0, 0, 0 ), Vector( 0, 0, 0 ), enginegain );
146  else
147  AUDStopPlaying( playerengine );
148  }
149  } else if (afterburn || !must_afterburn_to_buzz) {
150  static float buzzingtime = XMLSupport::parse_float( vs_config->getVariable( "audio", "buzzing_time", "5" ) );
151  static float buzzingdistance = XMLSupport::parse_float( vs_config->getVariable( "audio", "buzzing_distance", "5" ) );
152  static float lastbuzz = getNewTime();
153  Unit *playa = _Universe->AccessCockpit()->GetParent();
154  if (playa) {
155  Vector diff = this->Position()-playa->Position();
156  if (UnitUtil::getDistance( this,
157  playa ) < buzzingdistance && playa->owner != this && this->owner != playa
158  && this->owner != playa->owner) {
159  float ttime = getNewTime();
160  if (ttime-lastbuzz > buzzingtime) {
161  Vector pvel = playa->GetVelocity();
162  Vector vel = this->GetVelocity();
163  pvel.Normalize();
164  vel.Normalize();
165  float dotprod = vel.Dot( pvel );
166  if (dotprod < .86) {
167  lastbuzz = ttime;
168  AUDPlay( this->sound->engine, this->Position(), this->GetVelocity(), 1 );
169  } else {}
170  }
171  }
172  }
173  }
174 }
175 
176 template < class UnitType >
178 {
179 #ifndef PERFRAMESOUND
180  AUDAdjustSound( this->sound->engine, this->cumulative_transformation.position, this->cumulative_velocity );
181 #endif
182  return Unit::ResolveForces( trans, transmat );
183 }
184 
185 #endif
186