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

#include <autodocking.h>

Inheritance diagram for Orders::AutoDocking:
Order

Public Types

typedef std::deque< size_t > DockingPath
 
- 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 }
 

Public Member Functions

 AutoDocking (Unit *destination)
 
void Execute ()
 The function that gets called and executes all queued suborders. More...
 
- 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 ()
 
virtual void SetParent (Unit *parent1)
 Sets the parent of this Unit. Any virtual functions must call this one. More...
 
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)
 
virtual std::string getOrderDescription ()
 
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 ()
 

Static Public Member Functions

static bool CanDock (Unit *player, Unit *station)
 

Protected Member Functions

void InitialState (Unit *player, Unit *station)
 
void SelectionState (Unit *, Unit *)
 
void ApproachState (Unit *, Unit *)
 
void DockingState (Unit *, Unit *)
 
void DockedState (Unit *, Unit *)
 
void UndockingState (Unit *, Unit *)
 
void DepartureState (Unit *, Unit *)
 
void AbortState (Unit *, Unit *)
 
void EndState (Unit *, Unit *)
 
void EnqueuePort (Unit *, Unit *, size_t)
 
void EraseOrders ()
 
- 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...
 

Additional Inherited Members

- 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 55 of file autodocking.h.

Member Typedef Documentation

typedef std::deque<size_t> Orders::AutoDocking::DockingPath

Definition at line 60 of file autodocking.h.

Constructor & Destructor Documentation

Orders::AutoDocking::AutoDocking ( Unit destination)

Definition at line 108 of file autodocking.cpp.

111  target(destination)
112 {
113 }

Member Function Documentation

void Orders::AutoDocking::AbortState ( Unit player,
Unit station 
)
protected

Definition at line 163 of file autodocking.cpp.

References EndState(), EraseOrders(), PRESS, and FlyByKeyboard::StopKey().

Referenced by InitialState(), and SelectionState().

164 {
165  EraseOrders();
166  state = &AutoDocking::EndState;
167 
168  // Safety: full stop on abort
169  KBData kbdata;
170  FlyByKeyboard::StopKey(kbdata, PRESS);
171 }
void Orders::AutoDocking::ApproachState ( Unit player,
Unit station 
)
protected

Definition at line 205 of file autodocking.cpp.

References Unit::CanDockWithMe(), Unit::DockingPortLocations(), DockingState(), Order::Execute(), and SelectionState().

Referenced by SelectionState().

206 {
207  assert(!dockingPath.empty());
208 
209  // Move to docking port
210  if (station->DockingPortLocations()[dockingPath.back()].IsOccupied())
211  {
212  // Another ship has docked at our port. Find a new port.
214  }
215  else if (station->CanDockWithMe(player) == dockingPath.back())
216  {
217  state = &AutoDocking::DockingState;
218  }
219  else
220  {
221  // FIXME: Request clearance X times with fixed interval to keep capital ship immobile
222  Order::Execute();
223  }
224 }
bool Orders::AutoDocking::CanDock ( Unit player,
Unit station 
)
static

Definition at line 130 of file autodocking.cpp.

References anonymous_namespace{autodocking.cpp}::FindDockingPort(), UnitUtil::isCapitalShip(), Unit::IsCleared(), UnitUtil::isCloseEnoughToDock(), and UnitUtil::isDockableUnit().

Referenced by FlyByKeyboard::Execute(), InitialState(), and GameCockpit::LookupUnitStat().

131 {
132  if (!station->IsCleared(player))
133  {
134  return false;
135  }
136  else if (UnitUtil::isCapitalShip(player))
137  {
138  // A capital ship must align one of its docking ports with one of the
139  // the station's docking ports. This docking script cannot do that.
140  return false;
141  }
142  else if (!UnitUtil::isDockableUnit(station))
143  {
144  return false;
145  }
146  else if (!UnitUtil::isCloseEnoughToDock(player, station))
147  {
148  return false;
149  }
150  else if (FindDockingPort(player, station).empty())
151  {
152  return false;
153  }
154  return true;
155 }
void Orders::AutoDocking::DepartureState ( Unit player,
Unit station 
)
protected

Definition at line 259 of file autodocking.cpp.

References Order::done, Order::Done(), EndState(), EraseOrders(), and Order::Execute().

Referenced by UndockingState().

260 {
261  Order::Execute();
262  if (Done())
263  {
264  EraseOrders();
265  done = false;
266  state = &AutoDocking::EndState;
267  }
268 }
void Orders::AutoDocking::DockedState ( Unit player,
Unit station 
)
protected

Definition at line 234 of file autodocking.cpp.

References EraseOrders(), and UndockingState().

Referenced by DockingState().

235 {
236  EraseOrders();
238 }
void Orders::AutoDocking::DockingState ( Unit player,
Unit station 
)
protected

Definition at line 226 of file autodocking.cpp.

References DockedState(), and Unit::ForceDock().

Referenced by ApproachState().

227 {
228  assert(!dockingPath.empty());
229 
230  player->ForceDock(station, dockingPath.back());
231  state = &AutoDocking::DockedState;
232 }
void Orders::AutoDocking::EndState ( Unit player,
Unit station 
)
protected

Definition at line 157 of file autodocking.cpp.

References Unit::autopilotactive, and Order::done.

Referenced by AbortState(), DepartureState(), and UndockingState().

158 {
159  player->autopilotactive = false;
160  done = true;
161 }
void Orders::AutoDocking::EnqueuePort ( Unit player,
Unit station,
size_t  port 
)
protected

Definition at line 276 of file autodocking.cpp.

References Unit::DockingPortLocations(), Order::EnqueueOrder(), Order::FACING, DockingPorts::GetPosition(), Unit::GetTransformation(), DockingPorts::IsInside(), Order::MOVEMENT, MoveTo, QVector, Transform(), and useAfterburner().

Referenced by SelectionState(), and UndockingState().

277 {
278  // Set the coordinates for the docking port
279  const float turningSpeed = 1;
280  // If accuracy is too low then the ship does not always turn precisely.
281  // An incorrect heading may cause it to fly into corridor walls.
282  // If accuracy is too high then the ship turns precisely, but it does
283  // not terminate because it is trying to reach a higher accuracy than
284  // it can. This causes the ship to freeze.
285  const unsigned char accuracy = 7; // Higher means more accurate
286  const bool useAfterburner = false;
287  const bool terminateAfterUse = true;
288 
289  const DockingPorts& currentPort = station->DockingPortLocations()[port];
290  QVector position = Transform(station->GetTransformation(),
291  currentPort.GetPosition().Cast());
292 
293  Order *facing = new ChangeHeading(position, accuracy, turningSpeed, terminateAfterUse);
294  Order *movement = new MoveTo(position, useAfterburner, accuracy, terminateAfterUse);
295  if (currentPort.IsInside())
296  {
297  // Perform facing and movement sequentially when navigating inside the station
298  const unsigned int blockedDuringFacing = MOVEMENT;
299  EnqueueOrder(new Sequence(player, facing, blockedDuringFacing));
300  const unsigned int blockedDuringMovement = FACING;
301  EnqueueOrder(new Sequence(player, movement, blockedDuringMovement));
302  }
303  else
304  {
305  // Perform facing and movement simultaneously outside the station
306  EnqueueOrder(new Join(player, facing, movement));
307  }
308 }
void Orders::AutoDocking::EraseOrders ( )
protected

Definition at line 270 of file autodocking.cpp.

References Order::eraseType(), Order::FACING, and Order::MOVEMENT.

Referenced by AbortState(), DepartureState(), DockedState(), and SelectionState().

271 {
272  eraseType(FACING);
274 }
void Orders::AutoDocking::Execute ( )
virtual

The function that gets called and executes all queued suborders.

Reimplemented from Order.

Definition at line 115 of file autodocking.cpp.

References Order::done, Order::GetParent(), and UnitContainer::GetUnit().

116 {
117  Unit *player = GetParent();
118  Unit *station = target.GetUnit();
119  // Exit if either the ship or the station has been destroyed
120  if (player == NULL || station == NULL)
121  {
122  done = true;
123  }
124  else
125  {
126  (this->*state)(player, station);
127  }
128 }
void Orders::AutoDocking::InitialState ( Unit player,
Unit station 
)
protected

Definition at line 173 of file autodocking.cpp.

References AbortState(), CanDock(), and SelectionState().

174 {
175  if (CanDock(player, station))
176  {
178  }
179  else
180  {
181  state = &AutoDocking::AbortState;
182  }
183 }
void Orders::AutoDocking::SelectionState ( Unit player,
Unit station 
)
protected

Definition at line 185 of file autodocking.cpp.

References AbortState(), ApproachState(), EnqueuePort(), EraseOrders(), and anonymous_namespace{autodocking.cpp}::FindDockingPort().

Referenced by ApproachState(), and InitialState().

186 {
187  EraseOrders();
188 
189  dockingPath = FindDockingPort(player, station);
190  if (dockingPath.empty())
191  {
192  state = &AutoDocking::AbortState;
193  return;
194  }
195 
196  // Enqueue the waypoints from the farthest to the closest.
197  for (DockingPath::const_iterator it = dockingPath.begin(); it != dockingPath.end(); ++it)
198  {
199  EnqueuePort(player, station, *it);
200  }
201 
203 }
void Orders::AutoDocking::UndockingState ( Unit player,
Unit station 
)
protected

Definition at line 240 of file autodocking.cpp.

References DepartureState(), Unit::DockingPortLocations(), EndState(), and EnqueuePort().

Referenced by DockedState().

241 {
242  assert(!dockingPath.empty());
243 
244  state = &AutoDocking::EndState;
245 
246  // Enqueue undocking path if docked at inner port
247  if (station->DockingPortLocations()[dockingPath.back()].IsInside())
248  {
249  DockingPath::reverse_iterator it = dockingPath.rbegin();
250  ++it; // Skip the docking port itself
251  for (; it != dockingPath.rend(); ++it)
252  {
253  EnqueuePort(player, station, *it);
255  }
256  }
257 }

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