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
Orders::AutoLongHaul Class Reference

#include <navigation.h>

Inheritance diagram for Orders::AutoLongHaul:
Orders::ChangeHeading Order

Public Member Functions

 AutoLongHaul (bool fini=false, int accuracy=1)
 
virtual void Execute ()
 The function that gets called and executes all queued suborders. More...
 
virtual void SetParent (Unit *parent1)
 Sets the parent of this Unit. Any virtual functions must call this one. More...
 
virtual string getOrderDescription ()
 
virtual ~AutoLongHaul ()
 
- Public Member Functions inherited from Orders::ChangeHeading
 ChangeHeading (const QVector &final_heading, int switchback, float turning_speed=1, bool term=false)
 takes in the destination target, and the ammount of accuracy (how many times it should miss destination and come back) should be used More...
 
void SetDest (const QVector &)
 
virtual ~ChangeHeading ()
 
- Public Member Functions inherited from Order
virtual void ChooseTarget ()
 this function calls the destructor (needs to be overridden for python; More...
 
virtual bool PursueTarget (Unit *, bool isleader)
 
void ClearMessages ()
 clears the messasges of this order More...
 
 Order ()
 The default constructor setting everything to NULL and no dependency on order. More...
 
 Order (int type, int subtype)
 The constructor that specifies what order dependencies this order has. More...
 
virtual void Destroy ()
 The virutal function that unrefs all memory then calls Destruct () which takes care of unreffing this or calling delete on this. More...
 
OrderqueryType (unsigned int type)
 returns a pointer to the first order that may be bitwised ored with that type More...
 
OrderqueryAny (unsigned int type)
 returns a pointer to the first order that may be bitwise ored with any type More...
 
void eraseType (unsigned int type)
 Erases all orders that bitwise OR with that type. More...
 
bool AttachOrder (Unit *targets)
 Attaches a group of targets to this order (used for strategery-type games) More...
 
bool AttachOrder (QVector target)
 Attaches a navigation point to this order. More...
 
bool AttachSelfOrder (Unit *targets)
 Attaches a group (form up) to this order. More...
 
OrderEnqueueOrder (Order *ord)
 Enqueues another order that will be executed (in parallel perhaps) when next void Execute() is called. More...
 
OrderReplaceOrder (Order *ord)
 Replaces the first order of that type in the order queue. More...
 
bool Done ()
 
int getType ()
 
int getSubType ()
 
UnitGetParent () const
 
virtual void Communicate (const class CommunicationMessage &c)
 Sends a communication message from the Unit (encapulated in c) to this unit. More...
 
virtual void ProcessCommMessage (class CommunicationMessage &c)
 processes a single message...generally called by the Messages() func More...
 
virtual void ProcessCommunicationMessages (float CommRepsonseTime, bool RemoveMessageProcessed)
 responds (or does not) to certain messages in the message queue More...
 
OrderfindOrder (Order *ord)
 return pointer to order or NULL if not found More...
 
void eraseOrder (Order *ord)
 erase that order from the list More...
 
OrderEnqueueOrderFirst (Order *ord)
 enqueue order as first order More...
 
virtual olist_tgetOrderList ()
 returns the orderlist (NULL for orders that haven't got any) More...
 
virtual void AdjustRelationTo (Unit *un, float factor)
 
OrderfindOrderList ()
 searches the suborders recursively for the first order that has an orderlist More...
 
std::string createFullOrderDescription (int level=0)
 
void setActionString (std::string astring)
 
std::string getActionString ()
 
virtual float getMood ()
 

Additional Inherited Members

- Public Types inherited from Order
enum  ORDERTYPES {
  MOVEMENT =1, FACING =2, WEAPON =4, CLOAKING =8,
  ALLTYPES =(1|2|4|8)
}
 The varieties of order types MOVEMENT,FACING, and WEAPON orders may not be mutually executed (lest one engine goes left, the other right) More...
 
enum  SUBORDERTYPES { SLOCATION =1, STARGET =2, SSELF =4 }
 
- Protected Member Functions inherited from Orders::ChangeHeading
void ResetDone ()
 
- Protected Attributes inherited from Order
Unitparent
 The unit this order is attached to. More...
 
unsigned int type
 The bit code (from ORDERTYPES) that this order is (for parallel execution) More...
 
unsigned int subtype
 
bool done
 Whether or not this order is done. More...
 
UnitContainer group
 If this order applies to a group of units (as in form up with this group) More...
 
QVector targetlocation
 If this order applies to a physical location in world space. More...
 
std::vector< Order * > suborders
 The queue of suborders that will be executed in parallel according to bit code. More...
 
std::list< class
CommunicationMessage * > 
messagequeue
 a bunch of communications that have not been answered CommunicationMessages are actually containing reference to a nice Finite State Machine that can allow a player to have a reasonable conversation with an AI More...
 
std::string actionstring
 

Detailed Description

This class analyzes a Unit's position and adjusts ChangeHeading to SPEC towards target

Definition at line 137 of file navigation.h.

Constructor & Destructor Documentation

AutoLongHaul::AutoLongHaul ( bool  fini = false,
int  accuracy = 1 
)

Definition at line 438 of file navigation.cpp.

References Order::FACING, Order::MOVEMENT, Order::STARGET, Order::subtype, and Order::type.

438  : ChangeHeading( QVector( 0, 0, 1 ), accuracy )
439  , finish( fini )
440 {
441  type = FACING|MOVEMENT;
442  subtype = STARGET;
443  deactivatewarp = false;
444  StraightToTarget = true;
445  inside_landing_zone = false;
446 }
AutoLongHaul::~AutoLongHaul ( )
virtual

Definition at line 646 of file navigation.cpp.

References VSFileSystem::vs_fprintf().

647 {
648 #ifdef ORDERDEBUG
649  VSFileSystem::vs_fprintf( stderr, "ft%x", this );
650  fflush( stderr );
651 #endif
652 }

Member Function Documentation

void AutoLongHaul::Execute ( )
virtual

The function that gets called and executes all queued suborders.

Reimplemented from Orders::ChangeHeading.

Definition at line 511 of file navigation.cpp.

References Unit::autopilotactive, CalculateNearestWarpUnit(), Unit::Computer::combat_mode, UniverseUtil::cos(), Unit::cumulative_transformation_matrix, DistanceWarrantsWarpTo(), Order::done, Orders::ChangeHeading::Execute(), Unit::Limits::forward, Unit::GetComputerData(), Unit::GetMass(), Matrix::getR(), UnitUtil::getSignificantDistance(), UnitContainer::GetUnit(), VegaConfig::getVariable(), Unit::graphicOptions, Order::group, Unit::graphic_options::InWarp, Unit::isSubUnit(), Unit::Limits::lateral, Unit::limits, Unit::LocalPosition(), Unit::Computer::max_combat_ab_speed, mymax(), mymin, XMLSupport::parse_bool(), XMLSupport::parse_float(), Unit::Position(), QVector, Unit::graphic_options::RampCounter, Orders::ChangeHeading::ResetDone(), Unit::Limits::retro, Unit::rSize(), Orders::ChangeHeading::SetDest(), UnitContainer::SetUnit(), speed, Unit::Target(), useJitteryAutopilot(), Unit::Limits::vertical, vs_config, Unit::graphic_options::WarpFieldStrength, and Unit::graphic_options::WarpRamping.

512 {
513  Unit *target = group.GetUnit();
514  if (target == NULL) {
515  group.SetUnit( parent->Target() );
516  done = finish;
517  parent->autopilotactive = false;
518  return;
519  }
520  static bool compensate_for_interdiction =
521  XMLSupport::parse_bool( vs_config->getVariable( "physics", "autopilot_compensate_for_interdiction", "false" ) );
522  static float enough_warp_for_cruise =
523  XMLSupport::parse_float( vs_config->getVariable( "physics", "enough_warp_for_cruise", "1000" ) );
524  static float go_perpendicular_speed =
525  XMLSupport::parse_float( vs_config->getVariable( "physics", "warp_perpendicular", "80" ) );
526  static float min_warp_orbit_radius =
527  XMLSupport::parse_float( vs_config->getVariable( "physics", "min_warp_orbit_radius", "100000000" ) );
528  static float warp_orbit_multiplier =
529  XMLSupport::parse_float( vs_config->getVariable( "physics", "warp_orbit_multiplier", "4" ) );
530  static float warp_behind_angle =
531  cos( 3.1415926536*XMLSupport::parse_float( vs_config->getVariable( "physics", "warp_behind_angle", "150" ) )/180. );
532  QVector myposition = parent->isSubUnit() ? parent->Position() : parent->LocalPosition(); //get unit pos
533  QVector destination = target->isSubUnit() ? target->Position() : target->LocalPosition(); //get destination
534  QVector destinationdirection = (destination-myposition); //find vector from us to destination
535  double destinationdistance = destinationdirection.Magnitude();
536  destinationdirection = destinationdirection*(1./destinationdistance); //this is a direction, so it is normalize
537  if (parent->graphicOptions.WarpFieldStrength < enough_warp_for_cruise && parent->graphicOptions.InWarp) {
538  //face target unless warp ramping is done and warp is less than some intolerable ammt
539  Unit *obstacle = NULL;
540  float maxmultiplier = CalculateNearestWarpUnit( parent, FLT_MAX, &obstacle, compensate_for_interdiction ); //find the unit affecting our spec
541  bool currently_inside_landing_zone = false;
542  if (obstacle)
543  currently_inside_landing_zone = InsideLandingPort( obstacle );
544  if (currently_inside_landing_zone != inside_landing_zone) {
545  inside_landing_zone = currently_inside_landing_zone;
546  MakeLinearVelocityOrder();
547  }
548  if (maxmultiplier < enough_warp_for_cruise && obstacle != NULL && obstacle != target) {
549  //if it exists and is not our destination
550  QVector obstacledirection = (obstacle->LocalPosition()-myposition); //find vector from us to obstacle
551  double obstacledistance = obstacledirection.Magnitude();
552 
553  obstacledirection = obstacledirection*(1./obstacledistance); //normalize the obstacle direction as well
554  float angle = obstacledirection.Dot( destinationdirection );
555  if (obstacledistance-obstacle->rSize() < destinationdistance-target->rSize() && angle > warp_behind_angle) {
556  //if our obstacle is closer than obj and the obstacle is not behind us
557  QVector planetdest = destination-obstacle->LocalPosition(); //find the vector from planet to dest
558  QVector planetme = -obstacledirection; //obstacle to me
559  QVector planetperp = planetme.Cross( planetdest ); //find vector out of that plane
560  QVector detourvector = destinationdirection.Cross( planetperp ); //find vector perpendicular to our desired course emerging from planet
561  double renormalizedetour = detourvector.Magnitude();
562  if (renormalizedetour > .01) detourvector = detourvector*(1./renormalizedetour); //normalize it
563  double finaldetourdistance = mymax( obstacle->rSize()*warp_orbit_multiplier, min_warp_orbit_radius ); //scale that direction by some multiplier of obstacle size and a constant
564  detourvector = detourvector*finaldetourdistance; //we want to go perpendicular to our transit direction by that ammt
565  QVector newdestination = NewDestination( obstacle->LocalPosition()+detourvector, finaldetourdistance ); //add to our position
566  float weight = (maxmultiplier-go_perpendicular_speed)/(enough_warp_for_cruise-go_perpendicular_speed); //find out how close we are to our desired warp multiplier and weight our direction by that
567  weight *= weight; //
568  if (maxmultiplier < go_perpendicular_speed) {
569  QVector perpendicular = myposition+planetme*( finaldetourdistance/planetme.Magnitude() );
570  weight = (go_perpendicular_speed-maxmultiplier)/go_perpendicular_speed;
571  destination = weight*perpendicular+(1-weight)*newdestination;
572  } else {
573  QVector olddestination = myposition+destinationdirection*finaldetourdistance; //destination direction in the same magnitude as the newdestination from the ship
574  destination = newdestination*(1-weight)+olddestination*weight; //use the weight to combine our direction and the dest
575  }
576  StraightToTarget = false;
577  } else {
578  StraightToTarget = true;
579  }
580  } else {StraightToTarget = true; }} else if (parent->graphicOptions.WarpFieldStrength >= enough_warp_for_cruise) {
581  StraightToTarget = true;
582  }
584  deactivatewarp = false;
585  float mass = parent->GetMass();
586  float minaccel =
588  if (mass) minaccel /= mass;
589  if ( StraightToTarget && useJitteryAutopilot( parent, target, minaccel ) ) {
590  QVector cfacing = parent->cumulative_transformation_matrix.getR(); //velocity.Cast();
591  float speed = cfacing.Magnitude();
592  if (speed > .01)
593  cfacing = cfacing*(1./speed);
594  static float dotLimit =
595  cos( 3.1415926536*XMLSupport::parse_float( vs_config->getVariable( "physics",
596  "autopilot_spec_lining_up_angle",
597  "3" ) )/180. );
598  if (cfacing.Dot( destinationdirection ) < dotLimit) //if wanting to face target but overshooting.
599  deactivatewarp = true; //turn off drive
600  }
601  static bool rampdown = XMLSupport::parse_bool( vs_config->getVariable( "physics", "autopilot_ramp_warp_down", "true" ) );
603  UnitUtil::getSignificantDistance( parent, target ), false ) && deactivatewarp == false) { \
604  if (parent->graphicOptions.InWarp == 0) {
607  }
608  } else if (parent->graphicOptions.InWarp == 1) {
610  if (rampdown)
612  }
613  SetDest( destination );
614  bool combat_mode = parent->GetComputerData().combat_mode;
615  parent->GetComputerData().combat_mode = !inside_landing_zone; //turn off limits in landing zone
617  parent->GetComputerData().combat_mode = combat_mode;
618  if (!finish)
619  ResetDone();
620  static float distance_to_stop =
621  XMLSupport::parse_float( vs_config->getVariable( "physics", "auto_pilot_termination_distance", "6000" ) );
622  static float enemy_distance_to_stop =
623  XMLSupport::parse_float( vs_config->getVariable( "physics", "auto_pilot_termination_distance_enemy", "24000" ) );
624  static bool do_auto_finish = XMLSupport::parse_bool( vs_config->getVariable( "physics", "autopilot_terminate", "true" ) );
625  double dis = UnitUtil::getSignificantDistance( parent, target );
626  bool stopnow = false;
627  float speed = parent->GetComputerData().max_combat_ab_speed;
628  if (speed && parent->limits.retro) {
629  float time_to_destination = dis/speed; //conservative
630  float time_to_stop = speed*mass/parent->limits.retro;
631  if (time_to_destination <= time_to_stop)
632  stopnow = true;
633  }
634  if ( do_auto_finish
635  && ( stopnow || dis < distance_to_stop || (target->Target() == parent && dis < enemy_distance_to_stop) ) ) {
636  parent->autopilotactive = false;
637  if (parent->graphicOptions.InWarp == 1) {
639  if (rampdown)
641  }
642  done = true;
643  }
644 }
virtual string Orders::AutoLongHaul::getOrderDescription ( )
inlinevirtual

Reimplemented from Orders::ChangeHeading.

Definition at line 150 of file navigation.h.

151  {
152  return "ASAP";
153  }
void AutoLongHaul::SetParent ( Unit parent1)
virtual

Sets the parent of this Unit. Any virtual functions must call this one.

Reimplemented from Order.

Definition at line 462 of file navigation.cpp.

References Order::group, Order::SetParent(), UnitContainer::SetUnit(), and Unit::Target().

463 {
464  ChangeHeading::SetParent( parent1 );
465  group.SetUnit( parent1->Target() );
466  inside_landing_zone = false;
467  MakeLinearVelocityOrder();
468 }

The documentation for this class was generated from the following files: