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
fire.cpp File Reference
#include "fire.h"
#include "flybywire.h"
#include "navigation.h"
#include "cmd/planet_generic.h"
#include "config_xml.h"
#include "vs_globals.h"
#include "cmd/unit_util.h"
#include "cmd/script/flightgroup.h"
#include "cmd/role_bitmask.h"
#include "cmd/ai/communication.h"
#include "universe_util.h"
#include <algorithm>
#include "cmd/unit_find.h"
#include "vs_random.h"
#include "lin_time.h"
#include "cmd/pilot.h"

Go to the source code of this file.

Classes

struct  TargetAndRange
 
struct  RangeSortedTurrets
 
struct  TurretBin
 
class  StaticTuple< T, n >
 
class  ChooseTargetClass< numTuple >
 

Functions

static bool NoDockWithClear ()
 
UnitgetAtmospheric (Unit *targ)
 
bool RequestClearence (Unit *parent, Unit *targ, unsigned char sex)
 
bool CanFaceTarget (Unit *su, Unit *targ, const Matrix &matrix)
 
void AssignTBin (Unit *su, vector< TurretBin > &tbin)
 
float Priority (Unit *me, Unit *targ, float gunrange, float rangetotarget, float relationship, char *rolepriority)
 
float Priority (Unit *me, Unit *targ, float gunrange, float rangetotarget, float relationship)
 
unsigned int FireBitmask (Unit *parent, bool shouldfire, bool firemissile)
 

Variables

int numprocessed
 
double targetpick
 
VSRandom targrand (time(NULL))
 
int numpolled [2] = {0, 0}
 
int prevpollindex [2] = {10000, 10000}
 
int pollindex [2] = {1, 1}
 

Function Documentation

void AssignTBin ( Unit su,
vector< TurretBin > &  tbin 
)

Definition at line 207 of file fire.cpp.

References Unit::attackPreference(), Unit::getAverageGunSpeed(), Unit::getSubUnits(), and i.

Referenced by Orders::FireAt::ChooseTargets().

208 {
209  unsigned int bnum = 0;
210  for (; bnum < tbin.size(); bnum++)
211  if ( su->attackPreference() == tbin[bnum].turret[0].tur->attackPreference() )
212  break;
213  if ( bnum >= tbin.size() )
214  tbin.push_back( TurretBin() );
215  float gspeed, grange, mrange;
216  grange = FLT_MAX;
217  su->getAverageGunSpeed( gspeed, grange, mrange );
218  {
219  float ggspeed, ggrange, mmrange;
220  Unit *ssu;
221  for (un_iter i = su->getSubUnits(); (ssu = *i) != NULL; ++i) {
222  ssu->getAverageGunSpeed( ggspeed, ggrange, mmrange );
223  if (ggspeed > gspeed) gspeed = ggspeed;
224  if (ggrange > grange) grange = ggrange;
225  if (mmrange > mrange) mrange = mmrange;
226  }
227  }
228  if (tbin[bnum].maxrange < grange)
229  tbin[bnum].maxrange = grange;
230  tbin[bnum].turret.push_back( RangeSortedTurrets( su, grange ) );
231 }
bool CanFaceTarget ( Unit su,
Unit targ,
const Matrix matrix 
)

Definition at line 77 of file fire.cpp.

References Unit::cumulative_transformation_matrix, Unit::getSubUnits(), i, Unit::Limits::limitmin, Unit::Limits(), Unit::Position(), QVector, Unit::Limits::structurelimits, TransformNormal(), and Vector.

Referenced by TurretBin::AssignTargets().

78 {
79  return true;
80 
81  float limitmin = su->Limits().limitmin;
82  if (limitmin > -.99) {
83  QVector pos = ( targ->Position()-su->Position() ).Normalize();
84  QVector pnorm = pos.Cast();
85  Vector structurelimits = su->Limits().structurelimits;
86  Vector worldlimit = TransformNormal( matrix, structurelimits );
87  if (pnorm.Dot( worldlimit ) < limitmin)
88  return false;
89  }
90  Unit *ssu;
91  for (un_iter i = su->getSubUnits();
92  (ssu = *i) != NULL;
93  ++i)
94  if ( !CanFaceTarget( ssu, targ, su->cumulative_transformation_matrix ) )
95  return false;
96  return true;
97 }
unsigned int FireBitmask ( Unit parent,
bool  shouldfire,
bool  firemissile 
)

Definition at line 639 of file fire.cpp.

References ROLES::EVERYTHING_ELSE, ROLES::FIRE_GUNS, ROLES::FIRE_MISSILES, ROLES::FIRE_ONLY_AUTOTRACKERS, VegaConfig::getVariable(), XMLSupport::parse_bool(), Unit::Target(), Unit::unitRole(), and vs_config.

Referenced by Orders::TurretAI::Execute(), and Orders::FireAt::FireWeapons().

640 {
641  unsigned int firebitm = ROLES::EVERYTHING_ELSE;
642  Unit *un = parent->Target();
643  if (un) {
644  firebitm = ( 1<<un->unitRole() );
645 
646  static bool AlwaysFireAutotrackers =
647  XMLSupport::parse_bool( vs_config->getVariable( "AI", "AlwaysFireAutotrackers", "true" ) );
648  if (shouldfire)
649  firebitm |= ROLES::FIRE_GUNS;
650  if (AlwaysFireAutotrackers && !shouldfire) {
651  firebitm |= ROLES::FIRE_GUNS;
652  firebitm |= ROLES::FIRE_ONLY_AUTOTRACKERS;
653  }
654  if (firemissile)
655  firebitm = ROLES::FIRE_MISSILES; //stops guns
656  }
657  return firebitm;
658 }
Unit* getAtmospheric ( Unit targ)

Definition at line 29 of file fire.cpp.

References _Universe, Universe::activeStarSystem(), UnitCollection::createIterator(), StarSystem::getUnitList(), i, Unit::isUnit(), Magnitude(), PLANETPTR, Unit::Position(), and Unit::rSize().

Referenced by RequestClearence().

30 {
31  if (targ) {
32  Unit *un;
34  (un = *i) != NULL;
35  ++i)
36  if (un->isUnit() == PLANETPTR) {
37  if ( ( targ->Position()-un->Position() ).Magnitude() < targ->rSize()*.5 )
38  if ( !( ( (Planet*) un )->isAtmospheric() ) )
39  return un;
40  }
41  }
42  return NULL;
43 }
static bool NoDockWithClear ( )
static

Definition at line 21 of file fire.cpp.

References VegaConfig::getVariable(), XMLSupport::parse_bool(), and vs_config.

Referenced by RequestClearence().

22 {
23  static bool nodockwithclear = XMLSupport::parse_bool( vs_config->getVariable( "physics", "dock_with_clear_planets", "true" ) );
24  return nodockwithclear;
25 }
float Priority ( Unit me,
Unit targ,
float  gunrange,
float  rangetotarget,
float  relationship,
char *  rolepriority 
)

Definition at line 233 of file fire.cpp.

References Unit::attackPreference(), float, Unit::GetHull(), Unit::GetMass(), ROLES::getPriority(), VegaConfig::getVariable(), Unit::GetVelocity(), XMLSupport::parse_float(), Unit::Position(), Unit::Target(), Unit::Threat(), Unit::unitRole(), Vector, and vs_config.

Referenced by Priority(), and ChooseTargetClass< numTuple >::ShouldTargetUnit().

234 {
235  if (relationship >= 0)
236  return -1;
237  if (targ->GetHull() < 0)
238  return -1;
239  *rolepriority = ROLES::getPriority( me->attackPreference() )[targ->unitRole()]; //number btw 0 and 31 higher better
240  char invrolepriority = 31-*rolepriority;
241  if (invrolepriority <= 0)
242  return -1;
243  if (rangetotarget < 1 && rangetotarget > -1000)
244  rangetotarget = 1;
245  else
246  rangetotarget = fabs( rangetotarget );
247  if (rangetotarget < .5*gunrange)
248  rangetotarget = .5*gunrange;
249  if (gunrange <= 0) {
250  static float mountless_gunrange =
251  XMLSupport::parse_float( vs_config->getVariable( "AI", "Targetting", "MountlessGunRange", "300000000" ) );
252  gunrange = mountless_gunrange;
253  } //probably a mountless capship. 50000 is chosen arbitrarily
254  float inertial_priority = 0;
255  {
256  static float mass_inertial_priority_cutoff =
257  XMLSupport::parse_float( vs_config->getVariable( "AI", "Targetting", "MassInertialPriorityCutoff", "5000" ) );
258  if (me->GetMass() > mass_inertial_priority_cutoff) {
259  static float mass_inertial_priority_scale =
260  XMLSupport::parse_float( vs_config->getVariable( "AI", "Targetting", "MassInertialPriorityScale", ".0000001" ) );
261  Vector normv( me->GetVelocity() );
262  float Speed = me->GetVelocity().Magnitude();
263  normv *= Speed ? 1.0f/Speed : 1.0f;
264  Vector ourToThem = targ->Position()-me->Position();
265  ourToThem.Normalize();
266  inertial_priority = mass_inertial_priority_scale*( .5+.5*( normv.Dot( ourToThem ) ) )*me->GetMass()*Speed;
267  }
268  }
269  static float threat_weight = XMLSupport::parse_float( vs_config->getVariable( "AI", "Targetting", "ThreatWeight", ".5" ) );
270  float threat_priority = (me->Threat() == targ) ? threat_weight : 0;
271  threat_priority += (targ->Target() == me) ? threat_weight : 0;
272  float role_priority01 = ( (float) *rolepriority )/31.;
273  float range_priority01 = .5*gunrange/rangetotarget; //number between 0 and 1 for most ships 1 is best
274  return range_priority01*role_priority01+inertial_priority+threat_priority;
275 }
float Priority ( Unit me,
Unit targ,
float  gunrange,
float  rangetotarget,
float  relationship 
)

Definition at line 277 of file fire.cpp.

References Priority().

278 {
279  char rolepriority = 0;
280  return Priority( me, targ, gunrange, rangetotarget, relationship, &rolepriority );
281 }
bool RequestClearence ( Unit parent,
Unit targ,
unsigned char  sex 
)

Definition at line 45 of file fire.cpp.

References c, Order::Communicate(), Unit::DockingPortLocations(), CommunicationMessage::fsm, Unit::getAIState(), getAtmospheric(), FSM::GetRequestLandNode(), Unit::isUnit(), NoDockWithClear(), PLANETPTR, CommunicationMessage::SetCurrentState(), and Unit::Target().

Referenced by Cockpit::Update().

46 {
47  if ( !targ->DockingPortLocations().size() )
48  return false;
49  if (targ->isUnit() == PLANETPTR) {
50  if ( ( (Planet*) targ )->isAtmospheric() && NoDockWithClear() ) {
51  targ = getAtmospheric( targ );
52  if (!targ)
53  return false;
54  parent->Target( targ );
55  }
56  }
57  CommunicationMessage c( parent, targ, NULL, sex );
58  c.SetCurrentState( c.fsm->GetRequestLandNode(), NULL, sex );
59  Order *o = targ->getAIState();
60  if (o)
61  o->Communicate( c );
62  return true;
63 }

Variable Documentation

int numpolled[2] = {0, 0}

Definition at line 418 of file fire.cpp.

Referenced by Orders::FireAt::ChooseTargets().

int numprocessed
int pollindex[2] = {1, 1}

Definition at line 422 of file fire.cpp.

Referenced by Orders::FireAt::ChooseTargets().

int prevpollindex[2] = {10000, 10000}

Definition at line 420 of file fire.cpp.

Referenced by Orders::FireAt::ChooseTargets().

double targetpick
VSRandom targrand(time(NULL))