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_jump.h
Go to the documentation of this file.
1 #ifndef __UNIT_JUMP_CPP__
2 #define __UNIT_JUMP_CPP__
3 
4 #include "unit.h"
5 #include "audiolib.h"
6 #include "star_system_generic.h"
7 #include "cmd/images.h"
8 //From star_system_jump.cpp
10 extern std::vector< unorigdest* >pendingjump;
11 
12 //From star_system_jump.cpp
13 inline bool CompareDest( Unit *un, StarSystem *origin )
14 {
15  for (unsigned int i = 0; i < un->GetDestinations().size(); i++)
16  if ( std::string( origin->getFileName() ) == std::string( un->GetDestinations()[i] ) )
17  return true;
18  return false;
19 }
20 
21 inline std::vector< Unit* >ComparePrimaries( Unit *primary, StarSystem *origin )
22 {
23  std::vector< Unit* >myvec;
24  if ( CompareDest( primary, origin ) )
25  myvec.push_back( primary );
26  return myvec;
27 }
28 
29 extern void DealPossibleJumpDamage( Unit *un );
30 extern void ActivateAnimation( Unit* );
31 void WarpPursuit( Unit *un, StarSystem *sourcess, std::string destination );
32 
33 template < class UnitType >
34 bool GameUnit< UnitType >::TransferUnitToSystem( unsigned int kk, StarSystem* &savedStarSystem, bool dosightandsound )
35 {
36  bool ret = false;
37  if (pendingjump[kk]->orig == this->activeStarSystem || this->activeStarSystem == NULL) {
38  if ( Unit::TransferUnitToSystem( pendingjump[kk]->dest ) ) {
40  ret = true;
41 
42  Unit *unit;
43  for (un_iter iter = pendingjump[kk]->orig->getUnitList().createIterator(); (unit = *iter); ++iter) {
44  if (unit->Threat() == this)
45  unit->Threaten( NULL, 0 );
46  if (unit->VelocityReference() == this)
47  unit->VelocityReference( NULL );
48  if (unit->Target() == this) {
49  if ( pendingjump[kk]->jumppoint.GetUnit() ) {
50  unit->Target( pendingjump[kk]->jumppoint.GetUnit() );
51  unit->ActivateJumpDrive( 0 );
52  } else {
53  WarpPursuit( unit, pendingjump[kk]->orig, pendingjump[kk]->dest->getFileName() );
54  }
55  } else {
56  Flightgroup *ff = unit->getFlightgroup();
57  if (ff)
58  if ( this == ff->leader.GetUnit() && (ff->directive == "f" || ff->directive == "F") ) {
59  unit->Target( pendingjump[kk]->jumppoint.GetUnit() );
60  unit->getFlightgroup()->directive = "F";
61  unit->ActivateJumpDrive( 0 );
62  }
63  }
64  }
65  if ( this == _Universe->AccessCockpit()->GetParent() ) {
66  VSFileSystem::vs_fprintf( stderr, "Unit is the active player character...changing scene graph\n" );
67  savedStarSystem->SwapOut();
69  savedStarSystem = pendingjump[kk]->dest;
70  pendingjump[kk]->dest->SwapIn();
71  }
73  vector< Unit* >possibilities;
74  Unit *primary;
75  if (pendingjump[kk]->final_location.i == 0
76  && pendingjump[kk]->final_location.j == 0
77  && pendingjump[kk]->final_location.k == 0)
78  for (un_iter iter = pendingjump[kk]->dest->getUnitList().createIterator(); (primary = *iter); ++iter) {
79  vector< Unit* >tmp;
80  tmp = ComparePrimaries( primary, pendingjump[kk]->orig );
81  if ( !tmp.empty() )
82  possibilities.insert( possibilities.end(), tmp.begin(), tmp.end() );
83  }
84  else
85  this->SetCurPosition( pendingjump[kk]->final_location );
86  if ( !possibilities.empty() ) {
87  static int jumpdest = 235034;
88  Unit *jumpnode = possibilities[jumpdest%possibilities.size()];
89  QVector pos = jumpnode->Position();
90 
91  this->SetCurPosition( pos );
92  ActivateAnimation( jumpnode );
93  if (jumpnode->isUnit() == UNITPTR) {
94  QVector Offset( pos.i < 0 ? 1 : -1,
95  pos.j < 0 ? 1 : -1,
96  pos.k < 0 ? 1 : -1 );
97  Offset *= jumpnode->rSize()*2+this->rSize()*2;
98  this->SetPosAndCumPos( pos+Offset );
99  if (is_null( jumpnode->location[Unit::UNIT_ONLY] ) == false
100  && is_null( jumpnode->location[Unit::UNIT_BOLT] ) == false)
101  this->UpdateCollideQueue( pendingjump[kk]->dest, jumpnode->location );
102  }
103  jumpdest += 23231;
104  }
105  Unit *tester;
106  for (unsigned int jjj = 0; jjj < 2; ++jjj)
108  (tester = *i) != NULL; ++i)
109  if (tester->isUnit() == UNITPTR && tester != this)
110  if ( ( this->LocalPosition()-tester->LocalPosition() ).Magnitude() < this->rSize()+tester->rSize() )
111  SetCurPosition( this->LocalPosition()+this->cumulative_transformation_matrix.getR()
112  *( 4*( this->rSize()+tester->rSize() ) ) );
113  DealPossibleJumpDamage( this );
114  static int jumparrive = AUDCreateSound( vs_config->getVariable( "unitaudio", "jumparrive", "sfx43.wav" ), false );
115  if (dosightandsound)
116  AUDPlay( jumparrive, this->LocalPosition(), this->GetVelocity(), 1 );
117  } else {
118 #ifdef JUMP_DEBUG
119  VSFileSystem::vs_fprintf( stderr, "Unit FAILED remove from star system\n" );
120 #endif
121  }
122  if (this->docked&UnitType::DOCKING_UNITS)
123  for (unsigned int i = 0; i < this->pImage->dockedunits.size(); i++) {
124  Unit *unut;
125  if ( NULL != ( unut = this->pImage->dockedunits[i]->uc.GetUnit() ) )
126  unut->TransferUnitToSystem( kk, savedStarSystem, dosightandsound );
127  }
128  if ( this->docked&(UnitType::DOCKED|UnitType::DOCKED_INSIDE) ) {
129  Unit *un = this->pImage->DockedTo.GetUnit();
130  if (!un) {
131  this->docked &= ( ~(UnitType::DOCKED|UnitType::DOCKED_INSIDE) );
132  } else {
133  Unit *targ = NULL;
134  for (un_iter i = pendingjump[kk]->dest->getUnitList().createIterator();
135  ( targ = (*i) );
136  ++i)
137  if (targ == un)
138  break;
139  if (targ != un)
140  this->UnDock( un );
141  }
142  }
143  } else {
144  VSFileSystem::vs_fprintf( stderr, "Already jumped\n" );
145  }
146  return ret;
147 }
148 
149 #endif
150