# 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 "news_func.h"`
`#include "company_func.h"`
`#include "pathfinder/npf/npf_func.h"`
`#include "depot_base.h"`
`#include "station_base.h"`
`#include "vehicle_gui.h"`
`#include "newgrf_engine.h"`
`#include "pathfinder/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 "autoreplace_gui.h"`
`#include "gfx_func.h"`
`#include "effectvehicle_func.h"`
`#include "ai/ai.hpp"`
`#include "pathfinder/opf/opf_ship.h"`
`#include "landscape_type.h"`
`#include "infrastructure_func.h"`
`#include "table/strings.h"`
`#include "table/sprites.h"`

Go to the source code of this file.

## 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 left, int right, int preferred_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, uint max_distance)
static void | CheckIfShipNeedsService (Vehicle *v)
static void | HandleBrokenShip (Vehicle *v)
static void | PlayShipSound (const Vehicle *v)
void | RecalcShipStuff (Vehicle *v)
static void | CheckShipLeaveDepot (Ship *v)
static bool | ShipAccelerate (Vehicle *v)
static void | ShipArrivesAt (const Vehicle *v, Station *st)
static Track | ChooseShipTrack (const Ship *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, DiagDirection dir)
static void | ShipController (Ship *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 TileIndexDiffC | _ship_leave_depot_offs []
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 | ( | const Ship * | 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 379 of file ship_cmd.cpp.

References _settings_game, IsValidDiagDirection(), NPFShipChooseTrack(), PathfinderSettings::pathfinder_for_ships, GameSettings::pf, VPF_NPF, VPF_OPF, VPF_YAPF, and YapfShipChooseTrack().

Referenced by YapfShipChooseTrack().

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
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 610 of file ship_cmd.cpp.

References _cur_year, _date, _settings_game, CheckInfraUsageAllowed(), CMD_ERROR, CT_INVALID, DC_AUTOREPLACE, DC_EXEC, DC_QUERY_COST, ENGINE_EXCLUSIVE_PREVIEW, GetTileOwner(), GetVehicleCapacity(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, InvalidateAutoreplaceWindow(), InvalidateWindowClassesData(), InvalidateWindowData(), IsEngineBuildable(), VehicleSettings::max_ships, return_cmd_error, SetBit(), SetWindowDirty(), Ship::Ship(), Ship::state, TILE_SIZE, TileX(), TileY(), TRACK_BIT_DEPOT, Ship::UpdateDeltaXY(), VEH_SHIP, 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)

  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 779 of file ship_cmd.cpp.

References CMD_ERROR, DC_EXEC, GB(), SpecializedVehicle< Ship, VEH_SHIP >::GetIfValid(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, InvalidateWindowClassesData(), NUM_CARGO, RefitVehicle(), return_cmd_error, and SetWindowDirty().

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
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 711 of file ship_cmd.cpp.

References CMD_ERROR, DC_EXEC, SpecializedVehicle< Ship, VEH_SHIP >::GetIfValid(), 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)

  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 753 of file ship_cmd.cpp.

References CMD_ERROR, DEPOT_MASS_SEND, DEPOT_SERVICE, SpecializedVehicle< Ship, VEH_SHIP >::GetIfValid(), SendAllVehiclesToDepot(), and VEH_SHIP.

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

References Sprite::height, ST_NORMAL, and Sprite::width.

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

const TileIndexDiffC _ship_leave_depot_offs[]`[static]`

Initial value:

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

Definition at line 299 of file ship_cmd.cpp.