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
warpto.cpp
Go to the documentation of this file.
1 #include "cmd/unit_generic.h"
2 #include "cmd/unit_util.h"
3 #include "universe_util.h"
4 #include "config_xml.h"
5 
7 {
8  static float mat = XMLSupport::parse_float( vs_config->getVariable( "AI", "max_allowable_travel_time", "15" ) );
9  return mat;
10 }
11 
12 bool DistanceWarrantsWarpTo( Unit *parent, float dist, bool following )
13 {
14  //first let us decide whether the target is far enough to warrant using warp
15  //double dist =UnitUtil::getSignificantDistance(parent,target);
16  static float tooclose = XMLSupport::parse_float( vs_config->getVariable( "AI", "too_close_for_warp_tactic", "13000" ) );
17  static float tooclosefollowing =
18  XMLSupport::parse_float( vs_config->getVariable( "AI", "too_close_for_warp_in_formation", "1500" ) );
19  float toodamnclose = following ? tooclosefollowing : tooclose;
20  float diff = 1;
21  parent->GetVelocityDifficultyMult( diff );
22  float timetolive = dist/(diff*parent->GetComputerData().max_combat_speed);
23  if ( timetolive > ( 5*max_allowable_travel_time() ) ) {
24  return true;
25  } else if ( timetolive > ( max_allowable_travel_time() ) ) {
26  float mytime = SIMULATION_ATOM*1.5;
27  static bool rampdown = XMLSupport::parse_bool( vs_config->getVariable( "physics", "autopilot_ramp_warp_down", "true" ) );
28  if (rampdown == false) {
29  static float warprampdowntime =
30  XMLSupport::parse_float( vs_config->getVariable( "physics", "warprampdowntime", "0.5" ) );
31  mytime = warprampdowntime;
32  }
33  if (dist-parent->GetWarpVelocity().Magnitude()*mytime < toodamnclose)
34  return false; //avoid nasty jitter-jumping behavior should eventually have "running away check"
35 
36  return true;
37  }
38  return false;
39 }
40 
41 bool DistanceWarrantsTravelTo( Unit *parent, float dist, bool following )
42 {
43  //first let us decide whether the target is far enough to warrant using warp
44  float diff = 1;
45  parent->GetVelocityDifficultyMult( diff );
46  float timetolive = dist/(diff*parent->GetComputerData().max_combat_speed);
47  if ( timetolive > max_allowable_travel_time() )
48  return true;
49  return false;
50 }
51 
52 bool TargetWorthPursuing( Unit *parent, Unit *target )
53 {
54  return true;
55 }
56 
57 static void ActuallyWarpTo( Unit *parent, const QVector &tarpos, Vector tarvel, Unit *MatchSpeed = NULL )
58 {
59  Vector vel = parent->GetVelocity();
60  static float mindirveldot = XMLSupport::parse_float( vs_config->getVariable( "AI", "warp_cone", ".8" ) );
61  static float mintarveldot = XMLSupport::parse_float( vs_config->getVariable( "AI", "match_velocity_cone", "-.8" ) );
62  tarvel.Normalize();
63  vel.Normalize();
64  Vector dir = tarpos-parent->Position();
65  dir.Normalize();
66  float dirveldot = dir.Dot( parent->cumulative_transformation_matrix.getR() );
67  dir *= -1;
68  float chasedot = dir.Dot( tarvel );
69  if (dirveldot > mindirveldot) {
70  static float min_energy_to_enter_warp =
71  XMLSupport::parse_float( vs_config->getVariable( "AI", "min_energy_to_enter_warp", ".33" ) );
72  if (parent->WarpEnergyData() > min_energy_to_enter_warp) {
73  if (parent->graphicOptions.InWarp == 0) {
74  parent->graphicOptions.InWarp = 1; //don't want the AI thrashing
75  parent->graphicOptions.WarpRamping = 1; //don't want the AI thrashing
76  }
77  }
78  } else {
79  parent->graphicOptions.InWarp = 0;
80  }
81  static bool domatch = XMLSupport::parse_bool( vs_config->getVariable( "AI", "match_velocity_of_pursuant", "false" ) );
82  if (chasedot > mintarveldot || !domatch)
83  parent->computer.velocity_ref.SetUnit( NULL );
84  else
85  parent->computer.velocity_ref.SetUnit( MatchSpeed );
86 }
87 
88 void WarpToP( Unit *parent, Unit *target, bool following )
89 {
90  float dist = UnitUtil::getSignificantDistance( parent, target );
91  if ( DistanceWarrantsWarpTo( parent, dist, following ) ) {
92  if ( TargetWorthPursuing( parent, target ) ) {
93  static bool auto_valid =
94  XMLSupport::parse_bool( vs_config->getVariable( "physics", "insystem_jump_or_timeless_auto-pilot", "false" ) );
95  if (auto_valid) {
96  std::string tmp;
97  parent->AutoPilotTo( target, false );
98  } else {
99  ActuallyWarpTo( parent, target->Position(), target->GetVelocity(), target );
100  }
101  }
102  } else {
103  parent->graphicOptions.InWarp = 0;
104  }
105 }
106 
107 void WarpToP( Unit *parent, const QVector &target, float radius, bool following )
108 {
109  float dist = (parent->Position()-target).Magnitude()-radius-parent->rSize();
110  if ( DistanceWarrantsWarpTo( parent, dist, following ) ) {
111  static bool auto_valid =
112  XMLSupport::parse_bool( vs_config->getVariable( "physics", "insystem_jump_or_timeless_auto-pilot", "false" ) );
113  if (!auto_valid)
114  ActuallyWarpTo( parent, target, QVector( 0, 0, .00001 ) );
115  } else {
116  parent->graphicOptions.InWarp = 0;
117  }
118 }
119