# ship_cmd.cpp File Reference

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "ship.h"`
`#include "tile_cmd.h"`
`#include "landscape.h"`
`#include "timetable.h"`
`#include "command_func.h"`
`#include "pathfind.h"`
`#include "station_map.h"`
`#include "station.h"`
`#include "news.h"`
`#include "engine.h"`
`#include "player_func.h"`
`#include "player_base.h"`
`#include "npf.h"`
`#include "depot.h"`
`#include "vehicle_gui.h"`
`#include "newgrf_engine.h"`
`#include "water_map.h"`
`#include "yapf/yapf.h"`
`#include "debug.h"`
`#include "newgrf_callbacks.h"`
`#include "newgrf_text.h"`
`#include "newgrf_sound.h"`
`#include "spritecache.h"`
`#include "strings_func.h"`
`#include "functions.h"`
`#include "window_func.h"`
`#include "date_func.h"`
`#include "vehicle_func.h"`
`#include "sound_func.h"`
`#include "variables.h"`
`#include "autoreplace_gui.h"`
`#include "gfx_func.h"`
`#include "settings_type.h"`
`#include "table/strings.h"`

Go to the source code of this file.

## Data Structures
struct | PathFindShip
## Defines
#define | MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
## Functions
static TrackBits | GetTileShipTrackStatus (TileIndex tile)
void | DrawShipEngine (int x, int y, EngineID engine, SpriteID pal)
void | GetShipSpriteSize (EngineID engine, uint &width, uint &height)
| Get the size of the sprite of a ship sprite heading west (used for lists).
static const Depot * | FindClosestShipDepot (const Vehicle *v)
static void | CheckIfShipNeedsService (Vehicle *v)
static void | HandleBrokenShip (Vehicle *v)
static void | PlayShipSound (const Vehicle *v)
static void | ProcessShipOrder (Vehicle *v)
void | RecalcShipStuff (Vehicle *v)
static void | CheckShipLeaveDepot (Vehicle *v)
static bool | ShipAccelerate (Vehicle *v)
static CommandCost | EstimateShipCost (EngineID engine_type)
static void | ShipArrivesAt (const Vehicle *v, Station *st)
static bool | ShipTrackFollower (TileIndex tile, PathFindShip *pfs, int track, uint length)
static uint | FindShipTrack (Vehicle *v, TileIndex tile, DiagDirection dir, TrackBits bits, TileIndex skiptile, Track *track)
static NPFFoundTargetData | PerfNPFRouteToStationOrTile (TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData *target, TransportType type, Owner owner, RailTypes railtypes)
static Track | ChooseShipTrack (Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
| returns the track to choose on the next tile, or -1 when it's better to reverse.
static Direction | ShipGetNewDirectionFromTiles (TileIndex new_tile, TileIndex old_tile)
static Direction | ShipGetNewDirection (Vehicle *v, int x, int y)
static TrackBits | GetAvailShipTracks (TileIndex tile, int dir)
static void | ShipController (Vehicle *v)
static void | AgeShipCargo (Vehicle *v)
void | ShipsYearlyLoop ()
CommandCost | CmdBuildShip (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Build a ship.
CommandCost | CmdSellShip (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Sell a ship.
CommandCost | CmdStartStopShip (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Start/Stop a ship.
CommandCost | CmdSendShipToDepot (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Send a ship to the depot.
CommandCost | CmdRefitShip (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Refits a ship to the specified cargo type.
## Variables
static const uint16 | _ship_sprites [] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D}
static const TrackBits | _ship_sometracks [4]
static const TileIndexDiffC | _ship_leave_depot_offs []
static const byte | _ship_search_directions [6][4]
static const byte | _pick_shiptrack_table [6] = {1, 3, 2, 2, 0, 0}
static const Direction | _new_vehicle_direction_table []
static const byte | _ship_subcoord [4][6][3]

## Detailed Description

Definition in file ship_cmd.cpp.

## Function Documentation

static Track ChooseShipTrack | ( | Vehicle * | v,
|  | TileIndex | tile,
|  | DiagDirection | enterdir,
|  | TrackBits | tracks |
| ) |  |  | `[static]`

returns the track to choose on the next tile, or -1 when it's better to reverse.

The tile given is the tile we are about to enter, enterdir is the direction in which we are entering the tile

Definition at line 522 of file ship_cmd.cpp.

References GetVehicleTrackdir(), INVALID_RAILTYPES, INVALID_TRACK, INVALID_TRACKDIR, IsValidDiagDirection(), ReverseDiagDir(), TILE_ADD, TileOffsByDiagDir(), TrackdirToTrack(), VPF_NPF, VPF_OPF, VPF_YAPF, and YapfChooseShipTrack().

Referenced by YapfChooseShipTrack().

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

Build a ship.

Parameters:

  | tile | tile of depot where ship is built
  | flags | type of operation
  | p1 | ship type being built (engine)
  | p2 | unused

Definition at line 807 of file ship_cmd.cpp.

References CMD_ERROR, DC_AUTOREPLACE, DC_EXEC, DC_QUERY_COST, ENGINE_EXCLUSIVE_PREVIEW, CommandCost::GetCost(), InvalidateAutoreplaceWindow(), IsEngineBuildable(), IsTileDepotType(), IsTileOwner(), return_cmd_error, SetBit(), Ship::Ship(), TILE_SIZE, TileX(), TileY(), TRACK_BIT_DEPOT, and VehicleRandomBits().

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

Refits a ship to the specified cargo type.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | vehicle ID of the ship to refit
  | p2 | various bitstuffed elements p2 = (bit 0-7) - the new cargo type to refit to (p2 & 0xFF) p2 = (bit 8-15) - the new cargo subtype to refit to p2 = (bit 16) - refit only this vehicle (ignored)

Returns:
  cost of refit or error

Definition at line 1053 of file ship_cmd.cpp.

References CALLBACK_FAILED, CanRefitTo(), CBID_VEHICLE_REFIT_CAPACITY, CBM_VEHICLE_REFIT_CAPACITY, CMD_ERROR, DC_EXEC, GB(), GetRefitCost(), GetVehicleCallback(), HasBit(), IsValidVehicleID(), and return_cmd_error.

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

Sell a ship.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | vehicle ID to be sold
  | p2 | unused

Definition at line 900 of file ship_cmd.cpp.

References CMD_ERROR, DC_EXEC, DeleteDepotHighlightOfVehicle(), HASBITS, IsValidVehicleID(), and return_cmd_error.

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

Send a ship to the depot.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | vehicle ID to send to the depot
  | p2 | various bitmasked elements p2 bit 0-3 - DEPOT_ flags (see vehicle.h) p2 bit 8-10 - VLW flag (for mass goto depot)

Definition at line 975 of file ship_cmd.cpp.

References ClrBit(), CMD_ERROR, DC_EXEC, HasBit(), IsValidVehicleID(), OFB_NON_STOP, return_cmd_error, SendAllVehiclesToDepot(), SetBit(), and ToggleBit().

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

Start/Stop a ship.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | ship ID to start/stop
  | p2 | unused

Definition at line 936 of file ship_cmd.cpp.

References CALLBACK_FAILED, CBID_VEHICLE_START_STOP_CHECK, CMD_ERROR, DC_EXEC, DeleteVehicleNews(), error, GB(), GetEngineGRFID(), GetGRFStringID(), GetVehicleCallback(), IsValidVehicleID(), and return_cmd_error.

void GetShipSpriteSize | ( | EngineID | engine,
|  | uint & | width,
|  | uint & | height |
| ) |  |  |

Get the size of the sprite of a ship sprite heading west (used for lists).

Parameters:

  | engine | The engine to get the sprite from
  | width | The width of the sprite
  | height | The height of the sprite

Definition at line 78 of file ship_cmd.cpp.

References DIR_W.

## Variable Documentation

const Direction _new_vehicle_direction_table[]`[static]`

Initial value:

     {
      DIR_N , DIR_NW, DIR_W , INVALID_DIR,
      DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
      DIR_E , DIR_SE, DIR_S
    }

Definition at line 572 of file ship_cmd.cpp.

const TileIndexDiffC _ship_leave_depot_offs[]`[static]`

Initial value:

     {
      {-1,  0},
      { 0, -1}
    }

Definition at line 336 of file ship_cmd.cpp.

const byte _ship_search_directions[6][4]`[static]`

Initial value:

     {
      { 0, 9, 2, 9 },
      { 9, 1, 9, 3 },
      { 9, 0, 3, 9 },
      { 1, 9, 9, 2 },
      { 3, 2, 9, 9 },
      { 9, 9, 1, 0 },
    }

Definition at line 447 of file ship_cmd.cpp.

const TrackBits _ship_sometracks[4]`[static]`

Initial value:

     {
      TRACK_BIT_X | TRACK_BIT_LOWER | TRACK_BIT_LEFT,
      TRACK_BIT_Y | TRACK_BIT_UPPER | TRACK_BIT_LEFT,
      TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_RIGHT,
      TRACK_BIT_Y | TRACK_BIT_LOWER | TRACK_BIT_RIGHT,
    }

Definition at line 45 of file ship_cmd.cpp.