# ship_cmd.cpp File Reference

Handling of ships. More...

`#include "stdafx.h"`
`#include "ship.h"`
`#include "landscape.h"`
`#include "timetable.h"`
`#include "command_func.h"`
`#include "station_map.h"`
`#include "news_func.h"`
`#include "company_func.h"`
`#include "npf.h"`
`#include "depot_base.h"`
`#include "vehicle_gui.h"`
`#include "newgrf_engine.h"`
`#include "yapf/yapf.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 "effectvehicle_func.h"`
`#include "settings_type.h"`
`#include "ai/ai.hpp"`
`#include "pathfind.h"`
`#include "table/strings.h"`
`#include "table/sprites.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)
static SpriteID | GetShipIcon (EngineID engine)
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)
void | RecalcShipStuff (Vehicle *v)
static void | CheckShipLeaveDepot (Vehicle *v)
static bool | ShipAccelerate (Vehicle *v)
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)
CommandCost | CmdBuildShip (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build a ship.
CommandCost | CmdSellShip (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Sell a ship.
CommandCost | CmdSendShipToDepot (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Send a ship to the depot.
CommandCost | CmdRefitShip (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| 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

Handling of ships.

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 450 of file ship_cmd.cpp.

References _settings_game, GetVehicleTrackdir(), INVALID_RAILTYPES, INVALID_TRACK, INVALID_TRACKDIR, IsValidDiagDirection(), PathfinderSettings::pathfinder_for_ships, GameSettings::pf, ReverseDiagDir(), TILE_ADD, TileOffsByDiagDir(), TrackdirToTrack(), TRANSPORT_WATER, VPF_NPF, VPF_OPF, VPF_YAPF, and YapfChooseShipTrack().

Referenced by YapfChooseShipTrack().

CommandCost CmdBuildShip | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

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 727 of file ship_cmd.cpp.

References _cur_year, _date, _settings_game, CMD_ERROR, DAYS_IN_LEAP_YEAR, DC_AUTOREPLACE, DC_EXEC, DC_QUERY_COST, ENGINE_EXCLUSIVE_PREVIEW, InvalidateAutoreplaceWindow(), InvalidateWindow(), InvalidateWindowClassesData(), InvalidateWindowData(), IsEngineBuildable(), IsTileOwner(), VehicleSettings::max_ships, return_cmd_error, VehicleSettings::servint_ships, SetBit(), Ship::Ship(), TILE_SIZE, TileX(), TileY(), TRACK_BIT_DEPOT, GameSettings::vehicle, VehicleMove(), and VehicleRandomBits().

CommandCost CmdRefitShip | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

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 895 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(), InvalidateWindow(), InvalidateWindowClassesData(), IsValidVehicleID(), and return_cmd_error.

CommandCost CmdSellShip | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Sell a ship.

Parameters:

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

Definition at line 822 of file ship_cmd.cpp.

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

CommandCost CmdSendShipToDepot | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

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 867 of file ship_cmd.cpp.

References CMD_ERROR, DEPOT_MASS_SEND, DEPOT_SERVICE, IsValidVehicleID(), and SendAllVehiclesToDepot().

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 75 of file ship_cmd.cpp.

References ST_NORMAL.

## 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 500 of file ship_cmd.cpp.

const TileIndexDiffC _ship_leave_depot_offs[]`[static]`

Initial value:

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

Definition at line 274 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 375 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 39 of file ship_cmd.cpp.