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
turretai.cpp
Go to the documentation of this file.
1 #include "config_xml.h"
2 #include "vs_globals.h"
3 #include "turretai.h"
4 #include "cmd/unit_generic.h"
5 #include "cmd/role_bitmask.h"
6 
7 using namespace Orders;
9 {
10  type |= WEAPON;
11  range = -1;
12  speed = 1;
13  mrange = 1;
14 }
15 void TurretAI::getAverageGunSpeed( float &speed, float &range, float &mrange ) const
16 {
17  speed = this->speed;
18  range = this->range;
19  mrange = this->mrange;
20 }
21 extern unsigned int FireBitmask( Unit *parent, bool shouldfire, bool firemissile );
23 {
24  Unit *targ = parent->Target();
25  if (range == -1) {
26  range = mrange = speed = 0;
27  parent->getAverageGunSpeed( speed, range, mrange );
28  float tspeed, trange, tmrange;
29  Unit *gun;
30  if (parent->GetNumMounts() == 0) {
31  speed = 1;
32  range = 1;
33  mrange = 1; //not much
34  }
35  for (un_iter i = parent->getSubUnits(); (gun = *i) != NULL; ++i) {
36  (*i)->getAverageGunSpeed( tspeed, trange, tmrange );
37  if (trange > range) {
38  speed = tspeed;
39  range = trange;
40  mrange = tmrange;
41  }
42  }
43  if (range == 0) {
44  range = mrange;
45  speed = FLT_MAX;
46  }
47  }
48  if (targ) {
49  static float dot_cutoff = XMLSupport::parse_float( vs_config->getVariable( "AI", "Firing", "TurretDotCutoff", ".4" ) );
50  static float missile_prob =
51  XMLSupport::parse_float( vs_config->getVariable( "AI", "Firing", "TurretMissileProbability", ".05" ) );
53  if (parent->GetNumMounts() > 0) {
55  QVector Pos( targ->Position()-parent->Position() );
56  double mag = Pos.Magnitude();
57  Pos = Pos/mag;
58  float dot = R.Dot( Pos.Cast() );
61  bool isplayerstarship = _Universe->isPlayerStarshipVoid( parent->owner ) != NULL;
62 
63  bool shouldfire =
64  ( (mag-targ->rSize()-parent->rSize() < range
65  && dot > dot_cutoff)
66  && ( isplayerstarship == false || targ->faction == upg
67  || ( isplayerstarship
68  && (targ->getRelation( (Unit*) parent->owner /*now that it is a player, we know it's dereferencable*/ )
69  < 0
70  || targ->Target() == (Unit*) parent->owner) ) ) && targ->faction != neu );
71 
72  parent->Fire( FireBitmask( parent, shouldfire, rand() < missile_prob*RAND_MAX*SIMULATION_ATOM ), true );
73  if (!shouldfire)
74  parent->UnFire();
75  }
76  if (targ->hull < 0)
77  parent->Target( NULL );
78  }
79 }
80