# order_cmd.cpp File Reference

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "order.h"`
`#include "airport.h"`
`#include "depot.h"`
`#include "waypoint.h"`
`#include "command_func.h"`
`#include "station.h"`
`#include "player_func.h"`
`#include "news.h"`
`#include "saveload.h"`
`#include "vehicle_gui.h"`
`#include "cargotype.h"`
`#include "aircraft.h"`
`#include "strings_func.h"`
`#include "core/alloc_func.hpp"`
`#include "functions.h"`
`#include "window_func.h"`
`#include "settings_type.h"`
`#include "string_func.h"`
`#include "table/strings.h"`

Go to the source code of this file.

## Functions
Order | UnpackOldOrder (uint16 packed)
| Unpacks a order from savegames made with TTD(Patch).
static Order | UnpackVersion4Order (uint16 packed)
| Unpacks a order from savegames with version 4 and lower.
void | InvalidateVehicleOrder (const Vehicle *v)
| Updates the widgets of a vehicle which contains the order-data.
static void | SwapOrders (Order *order1, Order *order2)
| Swap two orders.
void | AssignOrder (Order *order, Order data)
| Assign data to an order (from an other order) This function makes sure that the index is maintained correctly.
static void | DeleteOrderWarnings (const Vehicle *v)
| Delete all news items regarding defective orders about a vehicle This could kill still valid warnings (for example about void order when just another order gets added), but assume the player will notice the problems, when (s)he's changing the orders.
static TileIndex | GetOrderLocation (const Order &o)
CommandCost | CmdInsertOrder (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Add an order to the orderlist of a vehicle.
static CommandCost | DecloneOrder (Vehicle *dst, uint32 flags)
| Declone an order-list.
static void | RemoveSharedOrderVehicleList (Vehicle *v)
| Remove the VehicleList that shows all the vehicles with the same shared orders.
CommandCost | CmdDeleteOrder (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Delete an order from the orderlist of a vehicle.
CommandCost | CmdSkipToOrder (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Goto order of order-list.
CommandCost | CmdMoveOrder (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Move an order inside the orderlist.
CommandCost | CmdModifyOrder (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Modify an order in the orderlist of a vehicle.
CommandCost | CmdCloneOrder (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Clone/share/copy an order-list of an other vehicle.
CommandCost | CmdOrderRefit (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Add/remove refit orders from an order.
void | BackupVehicleOrders (const Vehicle *v, BackuppedOrders *bak)
| Backup a vehicle order-list, so you can replace a vehicle without loosing the order-list.
void | RestoreVehicleOrders (const Vehicle *v, const BackuppedOrders *bak)
| Restore vehicle orders that are backupped via BackupVehicleOrders.
CommandCost | CmdRestoreOrderIndex (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Restore the current order-index of a vehicle and sets service-interval.
static TileIndex | GetStationTileForVehicle (const Vehicle *v, const Station *st)
void | CheckOrders (const Vehicle *v)
| Check the orders of a vehicle, to see if there are invalid orders and stuff.
void | RemoveOrderFromAllVehicles (OrderType type, DestinationID destination)
| Removes an order from all vehicles.
bool | VehicleHasDepotOrders (const Vehicle *v)
| Checks if a vehicle has a GOTO_DEPOT in his order list.
void | DeleteVehicleOrders (Vehicle *v)
| Delete all orders from a vehicle.
Date | GetServiceIntervalClamped (uint index)
| Get the service interval domain.
bool | CheckForValidOrders (const Vehicle *v)
| Check if a vehicle has any valid orders.
void | InitializeOrders ()
static void | Save_ORDR ()
static void | Load_ORDR ()
## Variables
TileIndex | _backup_orders_tile
BackuppedOrders | _backup_orders_data
static const SaveLoad | _order_desc []
const ChunkHandler | _order_chunk_handlers []

## Detailed Description

Definition in file order_cmd.cpp.

## Function Documentation

void AssignOrder | ( | Order * | order,
|  | Order | data |
| ) |  |  |

Assign data to an order (from an other order) This function makes sure that the index is maintained correctly.

Definition at line 116 of file order_cmd.cpp.

Referenced by CmdCloneOrder(), CmdInsertOrder(), and SwapOrders().

void BackupVehicleOrders | ( | const Vehicle * | v,
|  | BackuppedOrders * | bak |
| ) |  |  |

Backup a vehicle order-list, so you can replace a vehicle without loosing the order-list.

Definition at line 926 of file order_cmd.cpp.

bool CheckForValidOrders | ( | const Vehicle * | v | ) |

Check if a vehicle has any valid orders.

Returns:
  false if there are no valid orders

Definition at line 1282 of file order_cmd.cpp.

void CheckOrders | ( | const Vehicle * | v | ) |

Check the orders of a vehicle, to see if there are invalid orders and stuff.

Definition at line 1075 of file order_cmd.cpp.

References AddNewsItem(), GetLastVehicleOrder(), NEWS_FLAGS, NF_VEHICLE, NF_VIEWPORT, NM_SMALL, and NT_ADVICE.

CommandCost CmdCloneOrder | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Clone/share/copy an order-list of an other vehicle.

Parameters:

  | tile | unused
  | flags | operation to perform
  | p1 | various bitstuffed elements p1 = (bit 0-15) - destination vehicle to clone orders to (p1 & 0xFFFF) p1 = (bit 16-31) - source vehicle to clone orders from, if any (none for CO_UNSHARE)
  | p2 | mode of cloning: CO_SHARE, CO_COPY, or CO_UNSHARE

Definition at line 746 of file order_cmd.cpp.

References AssignOrder(), CC_PASSENGERS, CMD_ERROR, DC_EXEC, DecloneOrder(), DeleteVehicleOrders(), GB(), GetFirstVehicleFromSharedList(), INVALID_TILE, InvalidateVehicleOrder(), IsValidVehicleID(), and return_cmd_error.

CommandCost CmdDeleteOrder | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Delete an order from the orderlist of a vehicle.

Parameters:

  | tile | unused
  | flags | operation to perform
  | p1 | the ID of the vehicle
  | p2 | the order to delete (max 255)

Definition at line 455 of file order_cmd.cpp.

References CMD_ERROR, DC_EXEC, DecloneOrder(), DeleteOrderWarnings(), GetFirstVehicleFromSharedList(), HasBit(), InvalidateVehicleOrder(), IsValidVehicleID(), RemoveSharedOrderVehicleList(), and SwapOrders().

CommandCost CmdInsertOrder | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Add an order to the orderlist of a vehicle.

Parameters:

  | tile | unused
  | flags | operation to perform
  | p1 | various bitstuffed elements p1 = (bit 0 - 15) - ID of the vehicle p1 = (bit 16 - 31) - the selected order (if any). If the last order is given, the order will be inserted before that one only the first 8 bits used currently (bit 16 - 23) (max 255)
  | p2 | packed order to insert

Definition at line 165 of file order_cmd.cpp.

References AssignOrder(), CanAircraftUseStation(), CC_PASSENGERS, CMD_ERROR, DC_EXEC, DeleteOrderWarnings(), DistanceManhattan(), GB(), GetFirstVehicleFromSharedList(), GetLastVehicleOrder(), GetTileOwner(), InvalidateVehicleOrder(), IsTileDepotType(), IsValidVehicleID(), OFB_FULL_LOAD, OFB_HALT_IN_DEPOT, OFB_NON_STOP, OFB_PART_OF_ORDERS, OFB_TRANSFER, OFB_UNLOAD, OWNER_NONE, return_cmd_error, SwapOrders(), and VPF_NPF.

CommandCost CmdModifyOrder | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Modify an order in the orderlist of a vehicle.

Parameters:

  | tile | unused
  | flags | operation to perform
  | p1 | various bitstuffed elements p1 = (bit 0 - 15) - ID of the vehicle p1 = (bit 16 - 31) - the selected order (if any). If the last order is given, the order will be inserted before that one only the first 8 bits used currently (bit 16 - 23) (max 255)
  | p2 | mode to change the order to (always set)

Definition at line 666 of file order_cmd.cpp.

References ClrBit(), CMD_ERROR, DC_EXEC, DeleteOrderWarnings(), GB(), GetFirstVehicleFromSharedList(), HasBit(), InvalidateVehicleOrder(), IsValidVehicleID(), and ToggleBit().

CommandCost CmdMoveOrder | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Move an order inside the orderlist.

Parameters:

  | tile | unused
  | p1 | the ID of the vehicle
  | p2 | order to move and target bit 0-15 : the order to move bit 16-31 : the target order

Note:
  The target order will move one place down in the orderlist if you move the order upwards else it'll move it one place down

Definition at line 580 of file order_cmd.cpp.

References CMD_ERROR, DC_EXEC, DeleteOrderWarnings(), GB(), GetFirstVehicleFromSharedList(), InvalidateVehicleOrder(), IsValidVehicleID(), and SwapOrders().

CommandCost CmdOrderRefit | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Add/remove refit orders from an order.

Parameters:

  | tile | Not used
  | flags | operation to perform
  | p1 | VehicleIndex of the vehicle having the order
  | p2 | bitmask bit 0-7 CargoID bit 8-15 Cargo subtype bit 16-23 number of order to modify

Definition at line 880 of file order_cmd.cpp.

References CMD_ERROR, DC_EXEC, GB(), GetFirstVehicleFromSharedList(), HasBit(), InvalidateVehicleOrder(), and IsValidVehicleID().

CommandCost CmdRestoreOrderIndex | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Restore the current order-index of a vehicle and sets service-interval.

Parameters:

  | tile | unused
  | flags | operation to perform
  | p1 | the ID of the vehicle
  | p2 | various bistuffed elements p2 = (bit 0-15) - current order-index (p2 & 0xFFFF) p2 = (bit 16-31) - service interval (p2 >> 16)

Todo:
  Unfortunately you cannot safely restore the unitnumber or the old vehicle as far as I can see. We can store it in BackuppedOrders, and restore it, but but we have no way of seeing it has been tampered with or not, as we have no legit way of knowing what that ID was.
  If we do want to backup/restore it, just add UnitID uid to BackuppedOrders, and restore it as parameter 'y' (ugly hack I know) for example. "v->unitnumber = y;"

Definition at line 1030 of file order_cmd.cpp.

References CMD_ERROR, DC_EXEC, GB(), GetServiceIntervalClamped(), and IsValidVehicleID().

CommandCost CmdSkipToOrder | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Goto order of order-list.

Parameters:

  | tile | unused
  | flags | operation to perform
  | p1 | The ID of the vehicle which order is skipped
  | p2 | the selected order to which we want to skip

Definition at line 535 of file order_cmd.cpp.

References CMD_ERROR, DC_EXEC, HasBit(), InvalidateVehicleOrder(), and IsValidVehicleID().

static CommandCost DecloneOrder | ( | Vehicle * | dst,
|  | uint32 | flags |
| ) |  |  | `[static]`

Declone an order-list.

Parameters:

  | *dst | delete the orders of this vehicle
  | flags | execution flags

Definition at line 420 of file order_cmd.cpp.

References DC_EXEC, DeleteVehicleOrders(), and InvalidateVehicleOrder().

Referenced by CmdCloneOrder(), and CmdDeleteOrder().

void DeleteVehicleOrders | ( | Vehicle * | v | ) |

Delete all orders from a vehicle.

Definition at line 1220 of file order_cmd.cpp.

References DeleteOrderWarnings(), InvalidateVehicleOrder(), and RemoveSharedOrderVehicleList().

Referenced by CmdCloneOrder(), CmdMoveRailVehicle(), and DecloneOrder().

Date GetServiceIntervalClamped | ( | uint | index | ) |

Get the service interval domain.

Get the new proposed service interval for the vehicle is indeed, clamped within the given bounds.

See also:
  MIN_SERVINT_PERCENT ,etc.

Parameters:

  | index | proposed service interval

Returns:
  service interval

Definition at line 1269 of file order_cmd.cpp.

References Clamp().

Referenced by CmdChangeServiceInt(), CmdRestoreOrderIndex(), and VehicleDetailsWndProc().

void InvalidateVehicleOrder | ( | const Vehicle * | v | ) |

Updates the widgets of a vehicle which contains the order-data.

Definition at line 87 of file order_cmd.cpp.

Referenced by CmdCloneOrder(), CmdDeleteOrder(), CmdInsertOrder(), CmdModifyOrder(), CmdMoveOrder(), CmdOrderRefit(), CmdSkipToOrder(), DecloneOrder(), DeleteVehicleOrders(), ProcessTrainOrder(), and RemoveOrderFromAllVehicles().

void RemoveOrderFromAllVehicles | ( | OrderType | type,
|  | DestinationID | destination |
| ) |  |  |

Removes an order from all vehicles.

Triggers when, say, a station is removed.

Parameters:

  | type | The type of the order (OT_GOTO_[STATION|DEPOT|WAYPOINT]).
  | destination | The destination. Can be a StationID, DepotID or WaypointID.

Definition at line 1150 of file order_cmd.cpp.

References GetFirstVehicleFromSharedList(), and InvalidateVehicleOrder().

void RestoreVehicleOrders | ( | const Vehicle * | v,
|  | const BackuppedOrders * | bak |
| ) |  |  |

Restore vehicle orders that are backupped via BackupVehicleOrders.

Definition at line 977 of file order_cmd.cpp.

References _cmd_text, CMD_ADD_VEHICLE_GROUP, CMD_CHANGE_TIMETABLE, CMD_CLONE_ORDER, CMD_INSERT_ORDER, CMD_NAME_VEHICLE, CMD_NO_TEST_IF_IN_NETWORK, CMD_RESTORE_ORDER_INDEX, and DoCommandP().

Referenced by CcBuildAircraft().

static void SwapOrders | ( | Order * | order1,
|  | Order * | order2 |
| ) |  |  | `[static]`

Swap two orders.

Definition at line 99 of file order_cmd.cpp.

References AssignOrder().

Referenced by CmdDeleteOrder(), CmdInsertOrder(), and CmdMoveOrder().

Order UnpackOldOrder | ( | uint16 | packed | ) |

Unpacks a order from savegames made with TTD(Patch).

Definition at line 38 of file order_cmd.cpp.

References GB().

static Order UnpackVersion4Order | ( | uint16 | packed | ) | `[static]`

Unpacks a order from savegames with version 4 and lower.

Definition at line 67 of file order_cmd.cpp.

References GB().

bool VehicleHasDepotOrders | ( | const Vehicle * | v | ) |

Checks if a vehicle has a GOTO_DEPOT in his order list.

Returns:
  True if this is true (lol ;))

Definition at line 1203 of file order_cmd.cpp.

Referenced by CheckSendAircraftToHangarForReplacement().

## Variable Documentation

const ChunkHandler _order_chunk_handlers[]

Initial value:

     {
      { 'ORDR', Save_ORDR, Load_ORDR, CH_ARRAY | CH_LAST},
    }

const SaveLoad _order_desc[]`[static]`

Initial value:

     {
      SLE_VAR(Order, type,  SLE_UINT8),
      SLE_VAR(Order, flags, SLE_UINT8),
      SLE_VAR(Order, dest,  SLE_UINT16),
      SLE_REF(Order, next,  REF_ORDER),
      SLE_CONDVAR(Order, refit_cargo,    SLE_UINT8,  36, SL_MAX_VERSION),
      SLE_CONDVAR(Order, refit_subtype,  SLE_UINT8,  36, SL_MAX_VERSION),
      SLE_CONDVAR(Order, wait_time,      SLE_UINT16, 67, SL_MAX_VERSION),
      SLE_CONDVAR(Order, travel_time,    SLE_UINT16, 67, SL_MAX_VERSION),

      SLE_CONDNULL(10, 5, 35),

    }

Definition at line 1299 of file order_cmd.cpp.