# 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 "effectvehicle_func.h"`
`#include "effectvehicle_base.h"`
`#include "ai/ai.hpp"`
`#include "pathfinder/opf/opf_ship.h"`
`#include "landscape_type.h"`
`#include "engine_base.h"`
`#include "engine_func.h"`
`#include "company_base.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, PaletteID 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 381 of file ship_cmd.cpp.

References _settings_game, IsValidDiagDirection(), NPFShipChooseTrack(), OPFShipChooseTrack(), PathfinderSettings::pathfinder_for_ships, GameSettings::pf, VPF_NPF, VPF_OPF, VPF_YAPF, and 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 612 of file ship_cmd.cpp.

References _cur_year, _date, _settings_game, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::CanAllocateItem(), Vehicle::cargo_cap, Vehicle::cargo_type, CMD_ERROR, CT_INVALID, DC_AUTOREPLACE, DC_EXEC, DC_QUERY_COST, ENGINE_EXCLUSIVE_PREVIEW, EXPENSES_NEW_VEHICLES, GB(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), Engine::GetDefaultCargoType(), GetFreeUnitNumber(), Engine::GetLifeLengthInDays(), GetVehicleCapacity(), Vehicle::group_id, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, InvalidateAutoreplaceWindow(), Vehicle::InvalidateNewGRFCacheOfChain(), InvalidateWindowClassesData(), InvalidateWindowData(), IsEngineBuildable(), IsTileOwner(), VehicleSettings::max_ships, Vehicle::max_speed, return_cmd_error, SetBit(), SetWindowDirty(), Vehicle::tile, TILE_SIZE, TileX(), TileY(), TRACK_BIT_DEPOT, Ship::UpdateDeltaXY(), Vehicle::value, VEH_SHIP, GameSettings::vehicle, Vehicle::vehicle_flags, VehicleMove(), VehicleRandomBits(), and Vehicle::vehstatus.

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

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

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

References CheckOwnership(), CMD_ERROR, DC_EXEC, EXPENSES_NEW_VEHICLES, SpecializedVehicle< Ship, VEH_SHIP >::GetIfValid(), Vehicle::IsStoppedInDepot(), return_cmd_error, Vehicle::value, and Vehicle::vehstatus.

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

References CMD_ERROR, DEPOT_MASS_SEND, DEPOT_SERVICE, SpecializedVehicle< Ship, VEH_SHIP >::GetIfValid(), SendAllVehiclesToDepot(), Vehicle::SendToDepot(), 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 81 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 393 of file ship_cmd.cpp.

const TileIndexDiffC _ship_leave_depot_offs[]`[static]`

Initial value:

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

Definition at line 301 of file ship_cmd.cpp.