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::DockingOps Class Reference

#include <docking.h>

Inheritance diagram for Orders::DockingOps:
Orders::MoveTo Order

Public Member Functions

QVector Movement (Unit *utdw)
 
 DockingOps (Unit *unitToDockWith, Order *oldstate, bool physicallyDock, bool keeptrying)
 
virtual void SetParent (Unit *par)
 Sets the parent of this Unit. Any virtual functions must call this one. More...
 
void Execute ()
 The function that gets called and executes all queued suborders. More...
 
bool RequestClearence (Unit *)
 
bool DockToTarget (Unit *)
 
bool PerformDockingOperations (Unit *)
 
bool Undock (Unit *)
 
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...
 
void RestoreOldAI ()
 
- Public Member Functions inherited from Orders::MoveTo
void SetAfterburn (bool tf)
 The last_velocity keeps track of the previous velocity so the script may determine if it has crossed over 0 this frame or not. More...
 
 MoveTo (const QVector &target, bool aft, unsigned char switchbacks, bool terminating=true)
 takes in the destination target, whether afterburners should be applied, and the ammount of accuracy (how many times it shoudl miss destination and come back) should be used More...
 
void SetDest (const QVector &)
 
virtual ~MoveTo ()
 
virtual string getOrderDescription ()
 
- 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...
 
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 Order
virtual ~Order ()
 
virtual void Destructor ()
 changes the local relation of this unit to another...may inform superiors about "good" or bad! behavior depending on the AI More...
 
- 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

Definition at line 4 of file docking.h.

Constructor & Destructor Documentation

Orders::DockingOps::DockingOps ( Unit unitToDockWith,
Order oldstate,
bool  physicallyDock,
bool  keeptrying 
)

Definition at line 23 of file docking.cpp.

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

23  : MoveTo( QVector( 0, 0, 1 ),
24  false,
25  10, false )
26  , docking( unitToDockWith )
27  , state( GETCLEARENCE )
28  , oldstate( ai )
29 {
30  formerOwnerDoNotDereference = NULL;
31  this->keeptrying = keeptrying;
32  facedtarget = false;
33  physicallyDock = true;
34  port = -1;
35  static float temptimer = XMLSupport::parse_float( vs_config->getVariable( "physics", "docking_time", "10" ) );
36  timer = temptimer;
37 }

Member Function Documentation

void Orders::DockingOps::Destroy ( )
virtual

The virutal function that unrefs all memory then calls Destruct () which takes care of unreffing this or calling delete on this.

Reimplemented from Order.

Definition at line 86 of file docking.cpp.

References Order::Destroy(), Order::parent, Unit::SetOwner(), and UnitContainer::SetUnit().

Referenced by Execute().

87 {
88  if (parent) {
89  if (oldstate)
90  oldstate->Destroy();
91  oldstate = NULL;
92  if (formerOwnerDoNotDereference) {
93  parent->SetOwner( (Unit*) formerOwnerDoNotDereference ); //set owner will not deref
94  formerOwnerDoNotDereference = NULL;
95  }
96  }
97  docking.SetUnit( NULL );
98 }
bool Orders::DockingOps::DockToTarget ( Unit utdw)

Definition at line 150 of file docking.cpp.

References Unit::Dock(), DockedScript(), Unit::DockingPortLocations(), VegaConfig::getVariable(), Unit::isUnit(), Movement(), Order::parent, XMLSupport::parse_float(), PLANETPTR, Unit::Position(), QVector, Unit::RefillWarpEnergy(), Unit::rSize(), UnitContainer::SetUnit(), SIMULATION_ATOM, vs_config, and Unit::WarpCapData().

Referenced by Execute().

151 {
152  if (utdw->DockingPortLocations()[port].IsOccupied()) {
153  if (keeptrying) {
154  state = GETCLEARENCE;
155  return false;
156  } else {
157  docking.SetUnit( NULL );
158  state = GETCLEARENCE;
159  return false;
160  }
161  }
162  QVector loc = Movement( utdw );
163  float rad = utdw->DockingPortLocations()[port].GetRadius() + parent->rSize();
164  float diss = (parent->Position()-loc).MagnitudeSquared()-.1;
165  bool isplanet = utdw->isUnit() == PLANETPTR;
166  static float MinimumCapacityToRefuelOnLand =
167  XMLSupport::parse_float( vs_config->getVariable( "physics", "MinimumWarpCapToRefuelDockeesAutomatically", "0" ) );
168  if ( diss <= ( isplanet ? rad*rad : parent->rSize()*parent->rSize() ) ) {
169  DockedScript( parent, utdw );
170  if (physicallyDock) {
171  return parent->Dock( utdw );
172  } else {
173  float maxWillingToRefill = utdw->WarpCapData();
174  if (maxWillingToRefill >= MinimumCapacityToRefuelOnLand)
175  parent->RefillWarpEnergy(); //BUCO! This needs its own units.csv column to see how much we refill!
176  return true;
177  }
178  } else if (diss <= 1.2*rad*rad) {
179  timer += SIMULATION_ATOM;
180  static float tmp = XMLSupport::parse_float( vs_config->getVariable( "physics", "docking_time", "10" ) );
181  if (timer >= 1.5*tmp) {
182  if (physicallyDock) {
183  return parent->Dock( utdw );
184  } else {
185  float maxWillingToRefill = utdw->WarpCapData();
186  if (maxWillingToRefill >= MinimumCapacityToRefuelOnLand)
187  parent->RefillWarpEnergy(); //BUCO! This needs its own units.csv column to see how much we refill!
188  return true;
189  }
190  }
191  }
192  return false;
193 }
void Orders::DockingOps::Execute ( )
virtual

The function that gets called and executes all queued suborders.

Reimplemented from Orders::MoveTo.

Definition at line 46 of file docking.cpp.

References Destroy(), DockToTarget(), Order::done, UnitContainer::GetUnit(), Order::parent, PerformDockingOperations(), RequestClearence(), RestoreOldAI(), Unit::SetAngularVelocity(), Undock(), and Vector.

47 {
48  Unit *utdw = docking.GetUnit();
49  if (parent == utdw || utdw == NULL) {
50  RestoreOldAI();
51  Destroy();
52  return;
53  }
54  switch (state)
55  {
56  case GETCLEARENCE:
57  if ( !RequestClearence( utdw ) ) {
58  if (!keeptrying) {
59  RestoreOldAI();
60  Destroy();
61  return;
62  }
63  } else {
64  state = DOCKING;
65  //no break
66  }
67  case DOCKING:
68  if ( DockToTarget( utdw ) )
69  state = DOCKED;
70  break;
71  case DOCKED:
72  if ( PerformDockingOperations( utdw ) )
73  state = UNDOCKING;
74  break;
75  case UNDOCKING:
76  if ( Undock( utdw ) ) {
77  RestoreOldAI();
78  Destroy();
79  return;
80  }
81  break;
82  }
83  parent->SetAngularVelocity( Vector( 0, 0, 0 ) ); //FIXME if you want it to turn to dock point
84  done = false;
85 }
QVector Orders::DockingOps::Movement ( Unit utdw)

Definition at line 135 of file docking.cpp.

References DistanceWarrantsTravelTo(), Unit::DockingPortLocations(), Order::EnqueueOrder(), Orders::MoveTo::Execute(), Unit::GetTransformation(), Magnitude(), Order::parent, Unit::Position(), QVector, Orders::MoveTo::SetAfterburn(), Orders::MoveTo::SetDest(), Transform(), and WarpToP().

Referenced by DockToTarget(), and PerformDockingOperations().

136 {
137  const QVector loc( Transform( utdw->GetTransformation(), utdw->DockingPortLocations()[port].GetPosition().Cast() ) );
138  SetDest( loc );
139 
141  if (!facedtarget) {
142  facedtarget = true;
143  EnqueueOrder( new ChangeHeading( loc, 4, 1, true ) );
144  }
145  MoveTo::Execute();
146  if (rand()%256 == 0)
147  WarpToP( parent, utdw, true );
148  return loc;
149 }
bool Orders::DockingOps::PerformDockingOperations ( Unit utdw)

Definition at line 194 of file docking.cpp.

References Order::EnqueueOrder(), Orders::MoveTo::Execute(), VegaConfig::getVariable(), Unit::isUnit(), Movement(), Order::parent, XMLSupport::parse_float(), PLANETPTR, Unit::Position(), QVector, Orders::MoveTo::SetDest(), SIMULATION_ATOM, Unit::UnDock(), up, and vs_config.

Referenced by Execute().

195 {
196  timer -= SIMULATION_ATOM;
197  bool isplanet = utdw->isUnit() == PLANETPTR;
198  if (timer < 0) {
199  static float tmp = XMLSupport::parse_float( vs_config->getVariable( "physics", "un_docking_time", "180" ) );
200  timer = tmp;
201  EnqueueOrder( new ChangeHeading( parent->Position()*2-utdw->Position(), 4, 1, true ) );
202  if (physicallyDock)
203  return parent->UnDock( utdw );
204  else
205  return true;
206  } else if (!physicallyDock) {
207  if (isplanet) {
208  //orbit;
209  QVector cur = utdw->Position()-parent->Position();
210  QVector up = QVector( 0, 1, 0 );
211  if (up.i == cur.i && up.j == cur.j && up.k == cur.k)
212  up = QVector( 0, 0, 1 );
213  SetDest( cur.Cross( up )*10000 );
214  MoveTo::Execute();
215  } else {
216  Movement( utdw );
217  }
218  }
219  return false;
220 }
bool Orders::DockingOps::RequestClearence ( Unit utdw)

Definition at line 126 of file docking.cpp.

References Order::parent, Unit::RequestClearance(), and Orders::SelectDockPort().

Referenced by Execute().

127 {
128  if ( physicallyDock && !utdw->RequestClearance( parent ) )
129  return false;
130  port = SelectDockPort( utdw, parent );
131  if (port == -1)
132  return false;
133  return true;
134 }
void Orders::DockingOps::RestoreOldAI ( )

Definition at line 99 of file docking.cpp.

References Unit::aistate, Order::parent, and Unit::SetOwner().

Referenced by Execute().

100 {
101  if (parent) {
102  parent->aistate = oldstate; //that's me!
103  if (formerOwnerDoNotDereference) {
104  parent->SetOwner( (Unit*) formerOwnerDoNotDereference );
105  formerOwnerDoNotDereference = NULL;
106  }
107  oldstate = NULL;
108  }
109 }
void Orders::DockingOps::SetParent ( Unit parent1)
virtual

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

Reimplemented from Order.

Definition at line 38 of file docking.cpp.

References UnitContainer::GetUnit(), Unit::owner, Order::parent, Unit::SetOwner(), and Order::SetParent().

39 {
40  MoveTo::SetParent( par );
41  if (parent) {
42  formerOwnerDoNotDereference = parent->owner;
43  parent->SetOwner( docking.GetUnit() );
44  }
45 }
bool Orders::DockingOps::Undock ( Unit utdw)

Definition at line 221 of file docking.cpp.

References Order::done, Orders::MoveTo::Execute(), Order::parent, Unit::Position(), QVector, Unit::rSize(), Orders::MoveTo::SetDest(), and SIMULATION_ATOM.

Referenced by Execute().

222 {
223  //this is a good heuristic... find the location where you are.compare with center...then fly the fuck away
224  QVector awaydir = parent->Position()-utdw->Position();
225  float len = ( (utdw->rSize()+parent->rSize()*2)/awaydir.Magnitude() );
226  awaydir *= len;
227  SetDest( awaydir+utdw->Position() );
228  MoveTo::Execute();
229  timer -= SIMULATION_ATOM;
230  return (len < 1) || done || timer < 0;
231 }

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