OpenTTD Source 20260206-master-g4d4e37dbf1
ship_cmd.cpp File Reference

Handling of ships. More...

#include "stdafx.h"
#include "ship.h"
#include "landscape.h"
#include "timetable.h"
#include "news_func.h"
#include "company_func.h"
#include "depot_base.h"
#include "station_base.h"
#include "newgrf_engine.h"
#include "pathfinder/yapf/yapf.h"
#include "pathfinder/yapf/yapf_ship_regions.h"
#include "newgrf_sound.h"
#include "strings_func.h"
#include "window_func.h"
#include "timer/timer_game_calendar.h"
#include "timer/timer_game_economy.h"
#include "vehicle_func.h"
#include "sound_func.h"
#include "ai/ai.hpp"
#include "game/game.hpp"
#include "engine_base.h"
#include "company_base.h"
#include "tunnelbridge_map.h"
#include "zoom_func.h"
#include "framerate_type.h"
#include "industry.h"
#include "industry_map.h"
#include "ship_cmd.h"
#include "table/strings.h"
#include <unordered_set>
#include "safeguards.h"

Go to the source code of this file.

Data Structures

struct  ShipSubcoordData
 Structure for ship sub-coordinate data for moving into a new tile via a Diagdir onto a Track. More...

Functions

WaterClass GetEffectiveWaterClass (TileIndex tile)
 Determine the effective WaterClass for a ship travelling on a tile.
template<>
bool IsValidImageIndex< VEH_SHIP > (uint8_t image_index)
static TrackBits GetTileShipTrackStatus (TileIndex tile)
static void GetShipIcon (EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
void DrawShipEngine (int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
void GetShipSpriteSize (EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
 Get the size of the sprite of a ship sprite heading west (used for lists).
static const DepotFindClosestShipDepot (const Vehicle *v, uint max_distance)
static void CheckIfShipNeedsService (Vehicle *v)
static bool CheckReverseShip (const Ship *v, Trackdir *trackdir=nullptr)
static bool CheckShipStayInDepot (Ship *v)
 Checks whether a ship should stay in the depot.
static uint ShipAccelerate (Vehicle *v)
 Accelerates the ship towards its target speed.
static void ShipArrivesAt (const Vehicle *v, Station *st)
 Ship arrives at a dock.
static Track ChooseShipTrack (Ship *v, TileIndex tile, TrackBits tracks)
 Runs the pathfinder to choose a track to continue along.
static TrackBits GetAvailShipTracks (TileIndex tile, DiagDirection dir)
 Get the available water tracks on a tile for a ship entering a tile.
static int ShipTestUpDownOnLock (const Ship *v)
 Test if a ship is in the centre of a lock and should move up or down.
static bool ShipMoveUpDownOnLock (Ship *v)
 Test and move a ship up or down in a lock.
bool IsShipDestinationTile (TileIndex tile, StationID station)
 Test if a tile is a docking tile for the given station.
static void ReverseShipIntoTrackdir (Ship *v, Trackdir trackdir)
static void ReverseShip (Ship *v)
static void ShipController (Ship *v)
CommandCost CmdBuildShip (DoCommandFlags flags, TileIndex tile, const Engine *e, Vehicle **ret)
 Build a ship.

Variables

constexpr int MAX_SHIP_DEPOT_SEARCH_DISTANCE = 80
 Max distance in tiles (as the crow flies) to search for depots when user clicks "go to depot".
static const uint16_t _ship_sprites [] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D}
static const ShipSubcoordData _ship_subcoord [DIAGDIR_END][TRACK_END]
 Ship sub-coordinate data for moving into a new tile via a Diagdir onto a Track.

Detailed Description

Handling of ships.

Definition in file ship_cmd.cpp.

Function Documentation

◆ CheckIfShipNeedsService()

void CheckIfShipNeedsService ( Vehicle * v)
static

Definition at line 203 of file ship_cmd.cpp.

◆ CheckReverseShip()

bool CheckReverseShip ( const Ship * v,
Trackdir * trackdir = nullptr )
static

Definition at line 355 of file ship_cmd.cpp.

◆ CheckShipStayInDepot()

◆ ChooseShipTrack()

Track ChooseShipTrack ( Ship * v,
TileIndex tile,
TrackBits tracks )
static

Runs the pathfinder to choose a track to continue along.

Parameters
vShip to navigate
tileTile, the ship is about to enter
tracksAvailable track choices on tile
Returns
Track to choose, or INVALID_TRACK when to reverse.

Definition at line 465 of file ship_cmd.cpp.

References Vehicle::dest_tile, FindFirstTrack(), Vehicle::HandlePathfindingResult(), HasBit(), INVALID_TILE, IsDiagonalTrack(), Ship::path, Ship::state, TrackBitsToTrack(), TrackdirToTrack(), TrackToOppositeTrack(), and YapfShipChooseTrack().

◆ CmdBuildShip()

◆ DrawShipEngine()

void DrawShipEngine ( int left,
int right,
int preferred_x,
int y,
EngineID engine,
PaletteID pal,
EngineImageType image_type )

Definition at line 96 of file ship_cmd.cpp.

◆ FindClosestShipDepot()

const Depot * FindClosestShipDepot ( const Vehicle * v,
uint max_distance )
static

Definition at line 150 of file ship_cmd.cpp.

◆ GetAvailShipTracks()

TrackBits GetAvailShipTracks ( TileIndex tile,
DiagDirection dir )
inlinestatic

Get the available water tracks on a tile for a ship entering a tile.

Parameters
tileThe tile about to enter.
dirThe entry direction.
Returns
The available trackbits on the next tile.

Definition at line 504 of file ship_cmd.cpp.

References DiagdirReachesTracks().

◆ GetEffectiveWaterClass()

WaterClass GetEffectiveWaterClass ( TileIndex tile)

Determine the effective WaterClass for a ship travelling on a tile.

Parameters
tileTile of interest
Returns
the waterclass to be used by the ship.

Definition at line 53 of file ship_cmd.cpp.

References Canal, GetTunnelBridgeTransportType(), GetWaterClass(), HalfTileWater, HasTileWaterClass(), IsTileType(), Railway, Sea, TRANSPORT_WATER, and TunnelBridge.

Referenced by CYapfCostShipT< Types >::PfCalcCost(), and Ship::UpdateCache().

◆ GetShipIcon()

void GetShipIcon ( EngineID engine,
EngineImageType image_type,
VehicleSpriteSeq * result )
static

Definition at line 80 of file ship_cmd.cpp.

◆ GetShipSpriteSize()

void GetShipSpriteSize ( EngineID engine,
uint & width,
uint & height,
int & xoffs,
int & yoffs,
EngineImageType image_type )

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

Parameters
engineThe engine to get the sprite from.
[out]widthThe width of the sprite.
[out]heightThe height of the sprite.
[out]xoffsNumber of pixels to shift the sprite to the right.
[out]yoffsNumber of pixels to shift the sprite downwards.
image_typeContext the sprite is used in.

Definition at line 119 of file ship_cmd.cpp.

References VehicleSpriteSeq::GetBounds(), Rect::Height(), Rect, UnScaleGUI(), and Rect::Width().

Referenced by EnginePreviewWindow::UpdateWidgetSize().

◆ GetTileShipTrackStatus()

TrackBits GetTileShipTrackStatus ( TileIndex tile)
inlinestatic

Definition at line 75 of file ship_cmd.cpp.

◆ IsShipDestinationTile()

bool IsShipDestinationTile ( TileIndex tile,
StationID station )

Test if a tile is a docking tile for the given station.

Parameters
tileDocking tile to test.
stationDestination station.
Returns
true iff docking tile is next to station.

Definition at line 617 of file ship_cmd.cpp.

References DIAGDIR_BEGIN, DIAGDIR_END, Industry::GetByTile(), GetStationIndex(), Industry, IsDockingTile(), IsDockTile(), IsDockWaterPart(), IsOilRig(), IsTileType(), IsValidTile(), Industry::neutral_station, Station, and TileOffsByDiagDir().

Referenced by RemoveDock().

◆ IsValidImageIndex< VEH_SHIP >()

template<>
bool IsValidImageIndex< VEH_SHIP > ( uint8_t image_index)

Definition at line 70 of file ship_cmd.cpp.

◆ ReverseShip()

void ReverseShip ( Ship * v)
static

Definition at line 655 of file ship_cmd.cpp.

◆ ReverseShipIntoTrackdir()

void ReverseShipIntoTrackdir ( Ship * v,
Trackdir trackdir )
static

Definition at line 634 of file ship_cmd.cpp.

◆ ShipAccelerate()

uint ShipAccelerate ( Vehicle * v)
static

Accelerates the ship towards its target speed.

Parameters
vShip to accelerate.
Returns
Number of steps to move the ship.

Definition at line 414 of file ship_cmd.cpp.

References Vehicle::acceleration, VehicleCache::cached_max_speed, Vehicle::cur_speed, Vehicle::current_order, Vehicle::GetAdvanceDistance(), Vehicle::GetAdvanceSpeed(), Order::GetMaxSpeed(), Vehicle::progress, SetWindowWidgetDirty(), Vehicle::vcache, WC_VEHICLE_VIEW, and WID_VV_START_STOP.

◆ ShipArrivesAt()

void ShipArrivesAt ( const Vehicle * v,
Station * st )
static

Ship arrives at a dock.

If it is the first time, send out a news item.

Parameters
vShip that arrived.
stStation being visited.

Definition at line 439 of file ship_cmd.cpp.

References _local_company, AddVehicleNewsItem(), ArrivalCompany, ArrivalOther, GetEncodedString(), HVOT_SHIP, AI::NewEvent(), Game::NewEvent(), and Vehicle::owner.

◆ ShipController()

void ShipController ( Ship * v)
static

Definition at line 669 of file ship_cmd.cpp.

◆ ShipMoveUpDownOnLock()

bool ShipMoveUpDownOnLock ( Ship * v)
static

Test and move a ship up or down in a lock.

Parameters
vShip to move.
Returns
true iff ship is moving up or down in a lock.

Definition at line 591 of file ship_cmd.cpp.

References Vehicle::cur_speed, SetWindowWidgetDirty(), ShipTestUpDownOnLock(), Vehicle::tick_counter, Vehicle::UpdatePosition(), SpecializedVehicle< T, Type >::UpdateViewport(), WC_VEHICLE_VIEW, WID_VV_START_STOP, and Vehicle::z_pos.

◆ ShipTestUpDownOnLock()

int ShipTestUpDownOnLock ( const Ship * v)
static

Test if a ship is in the centre of a lock and should move up or down.

Parameters
vShip being tested.
Returns
0 if ship is not moving in lock, or -1 to move down, 1 to move up.

Definition at line 566 of file ship_cmd.cpp.

References Vehicle::direction, DirToDiagDir(), GetInclinedSlopeDirection(), GetLockPart(), GetTileMaxZ(), GetTileSlope(), GetTileZ(), IsLock(), IsTileType(), IsValidDiagDirection(), Middle, Vehicle::tile, TILE_HEIGHT, Water, Vehicle::x_pos, Vehicle::y_pos, and Vehicle::z_pos.

Referenced by ShipMoveUpDownOnLock().

Variable Documentation

◆ _ship_sprites

const uint16_t _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D}
static

Definition at line 67 of file ship_cmd.cpp.

◆ _ship_subcoord

const ShipSubcoordData _ship_subcoord[DIAGDIR_END][TRACK_END]
static

Ship sub-coordinate data for moving into a new tile via a Diagdir onto a Track.

Array indexes are Diagdir, Track. There will always be three possible tracks going into an adjacent tile via a Diagdir, so each Diagdir sub-array will have three valid and three invalid structures per Track.

Definition at line 522 of file ship_cmd.cpp.

◆ MAX_SHIP_DEPOT_SEARCH_DISTANCE

int MAX_SHIP_DEPOT_SEARCH_DISTANCE = 80
constexpr

Max distance in tiles (as the crow flies) to search for depots when user clicks "go to depot".

Definition at line 46 of file ship_cmd.cpp.

Referenced by Ship::FindClosestDepot().