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
navigation.cpp File Reference
#include "navigation.h"
#include "macosx_math.h"
#include <math.h>
#include <assert.h>
#include "cmd/unit_generic.h"
#include "lin_time.h"
#include "cmd/script/flightgroup.h"
#include "config_xml.h"
#include "vs_globals.h"
#include "warpto.h"
#include "flybywire.h"
#include "cmd/unit_util.h"

Go to the source code of this file.

Functions

static float CalculateBalancedDecelTime (float l, float v, float &F, float mass)
 
static float CalculateDecelTime (float l, float v, float &F, float D, float mass)
 
float CalculateNearestWarpUnit (const Unit *thus, float minmultiplier, Unit **nearest_unit, bool negative_spec_units)
 
bool DistanceWarrantsWarpTo (Unit *parent, float dist, bool following)
 
static float mymax (float a, float b)
 
static float mymin (float a, float b)
 
bool useJitteryAutopilot (Unit *parent, Unit *target, float minaccel)
 

Variables

float MOVETHRESHOLD = SIMULATION_ATOM/1.9
 
float TURNTHRESHOLD = SIMULATION_ATOM/1.9
 

Function Documentation

static float CalculateBalancedDecelTime ( float  l,
float  v,
float F,
float  mass 
)
static

the time we need to start slowing down from now calculation (if it's in this frame we'll only accelerate for partial vslowdown - decel * t = 0 t = vslowdown/decel finalx = -.5 decel ( v/decel)^2 + v^2 / decel + slowdownx = .5 * v^2 / decel + slowdownx slowdownx = .5 accel * t^2 + v0 * t + initx finalx = (.5*(accel * t + v0)^2)/decel + .5 accel * t^2 + v0*t + initx ; Length = finalx-initx Length = (.5*accel^2*t^2+accel*t*v0+ .5 *v0^2)/decel + .5 accel * t^2 + v0*t

balanced thrust equation Length = accel * t^2 + 2*t*v0 + .5*v0^2/accel t = ( -2v0 (+/-) sqrtf (4*v0^2 - 4*(.5*v0^2 - accel*Length) ) / (2*accel)) t = -v0/accel (+/-) sqrtf (.5*v0^2 + Length*accel)/accel;

8/15/05 Patched Calulate BalancedDecel time: our previous quantization factor ignored the quantization during ACCEL phase and also ignored the fact that we overestimated the integral rather than underestimated new quantization factor is .5*accel*SIMULATION_ATOM*SIMULATION_ATOM-.5*initialVelocity*SIMULATION_ATOM also this threshold idea is silly–accelerate if t>SIM_ATOM decel if t<0 still havent fixed t between 0 and SIM_ATOM...have decent approx for now. 3/2/02 Patched CalculateBalancedDecel time with the fact that length should be more by a quantity of .5*initialVelocity*SIMULATION_ATOM

Definition at line 37 of file navigation.cpp.

References SIMULATION_ATOM, and v.

Referenced by Orders::MoveToParent::Execute().

38 {
39  float accel = F/mass;
40  if (accel <= 0)
41  return 0;
42  if (l < 0) {
43  l = -l;
44  v = -v;
45  F = -F;
46  }
47  double temp = .5*v*v+(l-v*SIMULATION_ATOM*(.5)+.5*SIMULATION_ATOM*SIMULATION_ATOM*accel)*accel;
48  if (temp < 0)
49  temp = 0;
50  return ( -v+sqrtf( temp ) )/accel;
51 }
static float CalculateDecelTime ( float  l,
float  v,
float F,
float  D,
float  mass 
)
static

the time we need to start slowing down from now calculation (if it's in this frame we'll only accelerate for partial vslowdown - decel * t = 0 t = vslowdown/decel finalx = -.5 decel ( v/decel)^2 + v^2 / decel + slowdownx = .5 * v^2 / decel + slowdownx slowdownx = .5 accel * t^2 + v0 * t + initx finalx = (.5*(accel * t + v0)^2)/decel + .5 accel * t^2 + v0*t + initx ; Length = finalx-initx Length = (.5*accel^2*t^2+accel*t*v0+ .5 *v0^2)/decel + .5 accel * t^2 + v0*t

imbalanced thrust equation Length = .5*(accel+accel*accel/decel) * t^2 + t*v0(1+accel/decel) + .5*v0^2/decel t = ( -v0*(1+accel/decel) (+/-) sqrtf (v0^2*(1+accel/decel)^2 - 2*(accel+accel*accel/decel)*(.5*v0^2/decel-Length)))/2*.5*(accel+accel*accel/decel); t = (-v0 (+/-) sqrtf (v0^2 - 2*(accel/(1+accel/decel))*(.5*v0^2/decel-Length)))/accel

Definition at line 66 of file navigation.cpp.

References SIMULATION_ATOM, and v.

Referenced by Orders::MoveToParent::Execute().

67 {
68  float accel = F/mass;
69  float decel = D/mass;
70  if (l < 0) {
71  l = -l;
72  v = -v;
73  accel = decel;
74  decel = F/mass;
75  F = -D;
76  }
77  float vsqr = v*v;
78  float fourac = 2*accel*( (.5*v*v/decel)-v*SIMULATION_ATOM*.5-l )/(1+accel/decel);
79  if (fourac > vsqr) return FLT_MAX; //FIXME avoid sqrt negative not sure if this is right
80 
81  return ( -v+sqrtf( vsqr-fourac ) )/accel;
82 }
float CalculateNearestWarpUnit ( const Unit thus,
float  minmultiplier,
Unit **  nearest_unit,
bool  negative_spec_units 
)

Definition at line 2595 of file unit_generic.cpp.

References _Universe, Universe::activeStarSystem(), StarSystem::collidemap, UnitCollection::fastIterator(), findObjects(), UniverseUtil::getPlanetRadiusPercent(), UnitUtil::getSignificantDistance(), VegaConfig::getVariable(), Unit::graphicOptions, StarSystem::gravitationalUnits(), Unit::location, XMLSupport::parse_float(), PLANETPTR, Unit::Position(), QVector, NearestUnitLocator::retval, Unit::rSize(), Unit::specInterdiction, Unit::graphic_options::specInterdictionOnline, Collidable::CollideRef::unit, Unit::UNIT_ONLY, and vs_config.

Referenced by Unit::AddVelocity(), and Orders::AutoLongHaul::Execute().

2596 {
2597  static float smallwarphack = XMLSupport::parse_float( vs_config->getVariable( "physics", "minwarpeffectsize", "100" ) );
2598  static float bigwarphack =
2599  XMLSupport::parse_float( vs_config->getVariable( "physics", "maxwarpeffectsize", "10000000" ) );
2600  //Boundary between multiplier regions 1&2. 2 is "high" mult
2601  static double warpregion1 = XMLSupport::parse_float( vs_config->getVariable( "physics", "warpregion1", "5000000" ) );
2602  //Boundary between multiplier regions 0&1 0 is mult=1
2603  static double warpregion0 = XMLSupport::parse_float( vs_config->getVariable( "physics", "warpregion0", "5000" ) );
2604  //Mult at 1-2 boundary
2605  static double warpcruisemult = XMLSupport::parse_float( vs_config->getVariable( "physics", "warpcruisemult", "5000" ) );
2606  //degree of curve
2607  static double curvedegree = XMLSupport::parse_float( vs_config->getVariable( "physics", "warpcurvedegree", "1.5" ) );
2608  //coefficient so as to agree with above
2609  static double upcurvek = warpcruisemult/pow( (warpregion1-warpregion0), curvedegree );
2610  //inverse fractional effect of ship vs real big object
2611  static float def_inv_interdiction = 1.
2612  /XMLSupport::parse_float( vs_config->getVariable( "physics", "default_interdiction",
2613  ".125" ) );
2614  Unit *planet;
2615  Unit *testthis = NULL;
2616  {
2617  NearestUnitLocator locatespec;
2619  testthis = locatespec.retval.unit;
2620  }
2622  (planet = *iter) || testthis;
2623  ++iter) {
2624  if ( !planet || !planet->Killed() ) {
2625  if (planet == NULL) {
2626  planet = testthis;
2627  testthis = NULL;
2628  }
2629  if (planet == thus)
2630  continue;
2631  float shiphack = 1;
2632  if (planet->isUnit() != PLANETPTR) {
2633  shiphack = def_inv_interdiction;
2634  if ( planet->specInterdiction != 0 && planet->graphicOptions.specInterdictionOnline != 0
2635  && (planet->specInterdiction > 0 || count_negative_warp_units) ) {
2636  shiphack = 1/fabs( planet->specInterdiction );
2637  if (thus->specInterdiction != 0 && thus->graphicOptions.specInterdictionOnline != 0)
2638  //only counters artificial interdiction ... or maybe it cheap ones shouldn't counter expensive ones!? or
2639  // expensive ones should counter planets...this is safe now, for gameplay
2640  shiphack *= fabs( thus->specInterdiction );
2641  }
2642  }
2643  float multipliertemp = 1;
2644  float minsizeeffect = (planet->rSize() > smallwarphack) ? planet->rSize() : smallwarphack;
2645  float effectiverad = minsizeeffect*( 1.0f+UniverseUtil::getPlanetRadiusPercent() )+thus->rSize();
2646  if (effectiverad > bigwarphack)
2647  effectiverad = bigwarphack;
2648  QVector dir = thus->Position()-planet->Position();
2649  double udist = dir.Magnitude();
2650  float sigdist = UnitUtil::getSignificantDistance( thus, planet );
2651  if ( planet->isPlanet() && udist < (1<<28) ) //If distance is viable as a float approximation and it's an actual celestial body
2652  udist = sigdist;
2653  do {
2654  double dist = udist;
2655  if (dist < 0) dist = 0;
2656  dist *= shiphack;
2657  if ( dist > (effectiverad+warpregion0) )
2658  multipliertemp = pow( (dist-effectiverad-warpregion0), curvedegree )*upcurvek;
2659  else
2660  multipliertemp = 1;
2661  if (multipliertemp < minmultiplier) {
2662  minmultiplier = multipliertemp;
2663  *nearest_unit = planet;
2664  //eventually use new multiplier to compute
2665  } else {break; }
2666  } while (0);
2667  if (!testthis)
2668  break; //don't want the ++
2669  }
2670  }
2671  return minmultiplier;
2672 }
bool DistanceWarrantsWarpTo ( Unit parent,
float  dist,
bool  following 
)

Definition at line 12 of file warpto.cpp.

Referenced by Orders::AutoLongHaul::Execute(), and WarpToP().

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 }
static float mymax ( float  a,
float  b 
)
static

Definition at line 475 of file navigation.cpp.

References b.

Referenced by Unit::DamageRandSys(), DrawShield(), Orders::AutoLongHaul::Execute(), HaloAccelSmooth(), Beam::Init(), and Unit::LightShields().

476 {
477  return a > b ? a : b;
478 }
static float mymin ( float  a,
float  b 
)
static

Definition at line 479 of file navigation.cpp.

References b.

480 {
481  return a < b ? a : b;
482 }
bool useJitteryAutopilot ( Unit parent,
Unit target,
float  minaccel 
)

Definition at line 484 of file navigation.cpp.

References Unit::Computer::combat_mode, Unit::computer, Unit::GetComputerData(), VegaConfig::getVariable(), Unit::graphicOptions, Unit::isPlanet(), Unit::Computer::max_combat_ab_speed, StarSystemGent::maxspeed, XMLSupport::parse_float(), Unit::specInterdiction, Unit::graphic_options::specInterdictionOnline, and vs_config.

Referenced by Orders::AutoLongHaul::Execute().

485 {
486  static float specInterdictionLimit =
487  XMLSupport::parse_float( vs_config->getVariable( "physics", "min_spec_interdiction_for_jittery_autopilot", ".05" ) );
488  if ( target->isPlanet() == false
489  && (target->graphicOptions.specInterdictionOnline == 0 || fabs( target->specInterdiction ) < specInterdictionLimit) )
490  return true;
491  if (parent->computer.combat_mode == false)
492  return true;
493  float maxspeed = parent->GetComputerData().max_combat_ab_speed;
494  static float accel_auto_limit =
495  XMLSupport::parse_float( vs_config->getVariable( "physics", "max_accel_for_smooth_autopilot", "10" ) );
496  static float speed_auto_limit =
497  XMLSupport::parse_float( vs_config->getVariable( "physics", "max_over_combat_speed_for_smooth_autopilot", "2" ) );
498  if (minaccel < accel_auto_limit || parent->Velocity.MagnitudeSquared() > maxspeed*maxspeed*speed_auto_limit
499  *speed_auto_limit)
500  return true;
501  return false;
502 }

Variable Documentation

float MOVETHRESHOLD = SIMULATION_ATOM/1.9

Definition at line 114 of file navigation.cpp.

float TURNTHRESHOLD = SIMULATION_ATOM/1.9

Definition at line 270 of file navigation.cpp.