4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file ship_cmd.cpp Handling of ships. */
14 #include "dock_base.h"
15 #include "landscape.h"
16 #include "timetable.h"
17 #include "news_func.h"
18 #include "company_func.h"
19 #include "pathfinder/npf/npf_func.h"
20 #include "depot_base.h"
21 #include "station_base.h"
22 #include "newgrf_engine.h"
23 #include "pathfinder/yapf/yapf.h"
24 #include "newgrf_sound.h"
25 #include "spritecache.h"
26 #include "strings_func.h"
27 #include "window_func.h"
28 #include "date_func.h"
29 #include "vehicle_func.h"
30 #include "sound_func.h"
32 #include "game/game.hpp"
33 #include "pathfinder/opf/opf_ship.h"
34 #include "engine_base.h"
35 #include "company_base.h"
36 #include "tunnelbridge_map.h"
37 #include "zoom_func.h"
39 #include "table/strings.h"
41 #include "safeguards.h"
44 * Determine the effective #WaterClass for a ship travelling on a tile.
45 * @param tile Tile of interest
46 * @return the waterclass to be used by the ship.
48 WaterClass
GetEffectiveWaterClass(TileIndex tile
)
50 if (HasTileWaterClass(tile
)) return GetWaterClass(tile
);
51 if (IsTileType(tile
, MP_TUNNELBRIDGE
)) {
52 assert(GetTunnelBridgeTransportType(tile
) == TRANSPORT_WATER
);
53 return WATER_CLASS_CANAL
;
55 if (IsTileType(tile
, MP_RAILWAY
)) {
56 assert(GetRailGroundType(tile
) == RAIL_GROUND_WATER
);
57 return WATER_CLASS_SEA
;
62 static const uint16 _ship_sprites
[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
65 bool IsValidImageIndex
<VEH_SHIP
>(uint8 image_index
)
67 return image_index
< lengthof(_ship_sprites
);
70 static inline TrackBits
GetTileShipTrackStatus(TileIndex tile
)
72 return TrackStatusToTrackBits(GetTileTrackStatus(tile
, TRANSPORT_WATER
, 0));
75 static void GetShipIcon(EngineID engine
, EngineImageType image_type
, VehicleSpriteSeq
*result
)
77 const Engine
*e
= Engine::Get(engine
);
78 uint8 spritenum
= e
->u
.ship
.image_index
;
80 if (is_custom_sprite(spritenum
)) {
81 GetCustomVehicleIcon(engine
, DIR_W
, image_type
, result
);
82 if (result
->IsValid()) return;
84 spritenum
= e
->original_image_index
;
87 assert(IsValidImageIndex
<VEH_SHIP
>(spritenum
));
88 result
->Set(DIR_W
+ _ship_sprites
[spritenum
]);
91 void DrawShipEngine(int left
, int right
, int preferred_x
, int y
, EngineID engine
, PaletteID pal
, EngineImageType image_type
)
94 GetShipIcon(engine
, image_type
, &seq
);
98 preferred_x
= Clamp(preferred_x
,
99 left
- UnScaleGUI(rect
.left
),
100 right
- UnScaleGUI(rect
.right
));
102 seq
.Draw(preferred_x
, y
, pal
, pal
== PALETTE_CRASH
);
106 * Get the size of the sprite of a ship sprite heading west (used for lists).
107 * @param engine The engine to get the sprite from.
108 * @param[out] width The width of the sprite.
109 * @param[out] height The height of the sprite.
110 * @param[out] xoffs Number of pixels to shift the sprite to the right.
111 * @param[out] yoffs Number of pixels to shift the sprite downwards.
112 * @param image_type Context the sprite is used in.
114 void GetShipSpriteSize(EngineID engine
, uint
&width
, uint
&height
, int &xoffs
, int &yoffs
, EngineImageType image_type
)
116 VehicleSpriteSeq seq
;
117 GetShipIcon(engine
, image_type
, &seq
);
120 seq
.GetBounds(&rect
);
122 width
= UnScaleGUI(rect
.right
- rect
.left
+ 1);
123 height
= UnScaleGUI(rect
.bottom
- rect
.top
+ 1);
124 xoffs
= UnScaleGUI(rect
.left
);
125 yoffs
= UnScaleGUI(rect
.top
);
128 void Ship::GetImage(Direction direction
, EngineImageType image_type
, VehicleSpriteSeq
*result
) const
130 uint8 spritenum
= this->spritenum
;
132 if (is_custom_sprite(spritenum
)) {
133 GetCustomVehicleSprite(this, direction
, image_type
, result
);
134 if (result
->IsValid()) return;
136 spritenum
= this->GetEngine()->original_image_index
;
139 assert(IsValidImageIndex
<VEH_SHIP
>(spritenum
));
140 result
->Set(_ship_sprites
[spritenum
] + direction
);
143 static const Depot
*FindClosestShipDepot(const Vehicle
*v
, uint max_distance
)
145 /* Find the closest depot */
147 const Depot
*best_depot
= NULL
;
148 /* If we don't have a maximum distance, i.e. distance = 0,
149 * we want to find any depot so the best distance of no
150 * depot must be more than any correct distance. On the
151 * other hand if we have set a maximum distance, any depot
152 * further away than max_distance can safely be ignored. */
153 uint best_dist
= max_distance
== 0 ? UINT_MAX
: max_distance
+ 1;
155 FOR_ALL_DEPOTS(depot
) {
156 TileIndex tile
= depot
->xy
;
157 if (IsShipDepotTile(tile
) && IsTileOwner(tile
, v
->owner
)) {
158 uint dist
= DistanceManhattan(tile
, v
->tile
);
159 if (dist
< best_dist
) {
169 static void CheckIfShipNeedsService(Vehicle
*v
)
171 if (Company::Get(v
->owner
)->settings
.vehicle
.servint_ships
== 0 || !v
->NeedsAutomaticServicing()) return;
172 if (v
->IsChainInDepot()) {
173 VehicleServiceInDepot(v
);
178 switch (_settings_game
.pf
.pathfinder_for_ships
) {
179 case VPF_OPF
: max_distance
= 12; break;
180 case VPF_NPF
: max_distance
= _settings_game
.pf
.npf
.maximum_go_to_depot_penalty
/ NPF_TILE_LENGTH
; break;
181 case VPF_YAPF
: max_distance
= _settings_game
.pf
.yapf
.maximum_go_to_depot_penalty
/ YAPF_TILE_LENGTH
; break;
182 default: NOT_REACHED();
185 const Depot
*depot
= FindClosestShipDepot(v
, max_distance
);
188 if (v
->current_order
.IsType(OT_GOTO_DEPOT
)) {
189 v
->current_order
.MakeDummy();
190 SetWindowWidgetDirty(WC_VEHICLE_VIEW
, v
->index
, WID_VV_START_STOP
);
195 v
->current_order
.MakeGoToDepot(depot
->index
, ODTFB_SERVICE
);
196 v
->dest_tile
= depot
->xy
;
197 SetWindowWidgetDirty(WC_VEHICLE_VIEW
, v
->index
, WID_VV_START_STOP
);
201 * Update the caches of this ship.
203 void Ship::UpdateCache()
205 const ShipVehicleInfo
*svi
= ShipVehInfo(this->engine_type
);
207 /* Get speed fraction for the current water type. Aqueducts are always canals. */
208 bool is_ocean
= GetEffectiveWaterClass(this->tile
) == WATER_CLASS_SEA
;
209 uint raw_speed
= GetVehicleProperty(this, PROP_SHIP_SPEED
, svi
->max_speed
);
210 this->vcache
.cached_max_speed
= svi
->ApplyWaterClassSpeedFrac(raw_speed
, is_ocean
);
212 /* Update cargo aging period. */
213 this->vcache
.cached_cargo_age_period
= GetVehicleProperty(this, PROP_SHIP_CARGO_AGE_PERIOD
, EngInfo(this->engine_type
)->cargo_age_period
);
215 this->UpdateVisualEffect();
218 Money
Ship::GetRunningCost() const
220 const Engine
*e
= this->GetEngine();
221 uint cost_factor
= GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR
, e
->u
.ship
.running_cost
);
222 return GetPrice(PR_RUNNING_SHIP
, cost_factor
, e
->GetGRF());
225 void Ship::OnNewDay()
227 if ((++this->day_counter
& 7) == 0) {
228 DecreaseVehicleValue(this);
231 CheckVehicleBreakdown(this);
233 CheckIfShipNeedsService(this);
237 if (this->running_ticks
== 0) return;
239 CommandCost
cost(EXPENSES_SHIP_RUN
, this->GetRunningCost() * this->running_ticks
/ (DAYS_IN_YEAR
* DAY_TICKS
));
241 this->profit_this_year
-= cost
.GetCost();
242 this->running_ticks
= 0;
244 SubtractMoneyFromCompanyFract(this->owner
, cost
);
246 SetWindowDirty(WC_VEHICLE_DETAILS
, this->index
);
247 /* we need this for the profit */
248 SetWindowClassesDirty(WC_SHIPS_LIST
);
251 Trackdir
Ship::GetVehicleTrackdir() const
253 if (this->vehstatus
& VS_CRASHED
) return INVALID_TRACKDIR
;
255 if (this->IsInDepot()) {
256 /* We'll assume the ship is facing outwards */
257 return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile
));
260 if (this->state
== TRACK_BIT_WORMHOLE
) {
261 /* ship on aqueduct, so just use his direction and assume a diagonal track */
262 return DiagDirToDiagTrackdir(DirToDiagDir(this->direction
));
265 return TrackDirectionToTrackdir(FindFirstTrack(this->state
), this->direction
);
268 void Ship::MarkDirty()
270 this->colourmap
= PAL_NONE
;
271 this->UpdateViewport(true, false);
275 static void PlayShipSound(const Vehicle
*v
)
277 if (!PlayVehicleSound(v
, VSE_START
)) {
278 SndPlayVehicleFx(ShipVehInfo(v
->engine_type
)->sfx
, v
);
282 void Ship::PlayLeaveStationSound() const
288 * Of all the docks a station has, return the best destination for a ship.
290 * @param st Station the ship \a v is heading for.
291 * @return The free and closest (if none is free, just closest) dock of station \a st to ship \a v.
293 const Dock
* GetBestDock(const Ship
*v
, const Station
*st
)
295 assert(st
!= NULL
&& st
->HasFacilities(FACIL_DOCK
) && st
->docks
!= NULL
);
296 if (st
->docks
->next
== NULL
) return st
->docks
;
298 Dock
*best_dock
= NULL
;
299 uint best_distance
= UINT_MAX
;
301 for (Dock
*dock
= st
->docks
; dock
!= NULL
; dock
= dock
->next
) {
302 uint new_distance
= DistanceManhattan(v
->tile
, dock
->flat
);
304 if (new_distance
< best_distance
) {
306 best_distance
= new_distance
;
310 assert(best_dock
!= NULL
);
315 TileIndex
Ship::GetOrderStationLocation(StationID station
)
317 if (station
== this->last_station_visited
) this->last_station_visited
= INVALID_STATION
;
319 const Station
*st
= Station::Get(station
);
320 if (st
->HasFacilities(FACIL_DOCK
)) {
321 if (st
->docks
== NULL
) {
322 return st
->xy
; // A buoy
324 const Dock
* dock
= GetBestDock(this, st
);
326 DiagDirection direction
= DiagdirBetweenTiles(dock
->sloped
, dock
->flat
);
327 return dock
->flat
+ TileOffsByDiagDir(direction
);
330 this->IncrementRealOrderIndex();
335 void Ship::UpdateDeltaXY(Direction direction
)
337 static const int8 _delta_xy_table
[8][4] = {
338 /* y_extent, x_extent, y_offs, x_offs */
339 { 6, 6, -3, -3}, // N
340 { 6, 32, -3, -16}, // NE
341 { 6, 6, -3, -3}, // E
342 {32, 6, -16, -3}, // SE
343 { 6, 6, -3, -3}, // S
344 { 6, 32, -3, -16}, // SW
345 { 6, 6, -3, -3}, // W
346 {32, 6, -16, -3}, // NW
349 const int8
*bb
= _delta_xy_table
[direction
];
350 this->x_offs
= bb
[3];
351 this->y_offs
= bb
[2];
352 this->x_extent
= bb
[1];
353 this->y_extent
= bb
[0];
357 static bool CheckShipLeaveDepot(Ship
*v
)
359 if (!v
->IsChainInDepot()) return false;
361 if (v
->current_order
.IsWaitTimetabled())
362 v
->HandleWaiting(false);
363 if (v
->current_order
.IsType(OT_WAITING
))
366 /* We are leaving a depot, but have to go to the exact same one; re-enter */
367 if (v
->current_order
.IsType(OT_GOTO_DEPOT
) &&
368 IsShipDepotTile(v
->tile
) && GetDepotIndex(v
->tile
) == v
->current_order
.GetDestination()) {
369 VehicleEnterDepot(v
);
373 TileIndex tile
= v
->tile
;
374 Axis axis
= GetShipDepotAxis(tile
);
376 DiagDirection north_dir
= ReverseDiagDir(AxisToDiagDir(axis
));
377 TileIndex north_neighbour
= TILE_ADD(tile
, TileOffsByDiagDir(north_dir
));
378 DiagDirection south_dir
= AxisToDiagDir(axis
);
379 TileIndex south_neighbour
= TILE_ADD(tile
, 2 * TileOffsByDiagDir(south_dir
));
381 TrackBits north_tracks
= DiagdirReachesTracks(north_dir
) & GetTileShipTrackStatus(north_neighbour
);
382 TrackBits south_tracks
= DiagdirReachesTracks(south_dir
) & GetTileShipTrackStatus(south_neighbour
);
383 if (north_tracks
&& south_tracks
) {
384 /* Ask pathfinder for best direction */
385 bool reverse
= false;
387 switch (_settings_game
.pf
.pathfinder_for_ships
) {
388 case VPF_OPF
: reverse
= OPFShipChooseTrack(v
, north_neighbour
, north_dir
, north_tracks
, path_found
) == INVALID_TRACK
; break; // OPF always allows reversing
389 case VPF_NPF
: reverse
= NPFShipCheckReverse(v
); break;
390 case VPF_YAPF
: reverse
= YapfShipCheckReverse(v
); break;
391 default: NOT_REACHED();
393 if (reverse
) north_tracks
= TRACK_BIT_NONE
;
397 /* Leave towards north */
398 v
->direction
= DiagDirToDir(north_dir
);
399 } else if (south_tracks
) {
400 /* Leave towards south */
401 v
->direction
= DiagDirToDir(south_dir
);
403 /* Both ways blocked */
407 v
->state
= AxisToTrackBits(axis
);
408 v
->vehstatus
&= ~VS_HIDDEN
;
411 v
->UpdateViewport(true, true);
412 SetWindowDirty(WC_VEHICLE_DEPOT
, v
->tile
);
415 VehicleServiceInDepot(v
);
416 InvalidateWindowData(WC_VEHICLE_DEPOT
, v
->tile
);
417 SetWindowClassesDirty(WC_SHIPS_LIST
);
422 static bool ShipAccelerate(Vehicle
*v
)
427 spd
= min(v
->cur_speed
+ 1, v
->vcache
.cached_max_speed
);
428 spd
= min(spd
, v
->current_order
.GetMaxSpeed() * 2);
430 /* updates statusbar only if speed have changed to save CPU time */
431 if (spd
!= v
->cur_speed
) {
433 SetWindowWidgetDirty(WC_VEHICLE_VIEW
, v
->index
, WID_VV_START_STOP
);
436 /* Convert direction-independent speed into direction-dependent speed. (old movement method) */
437 spd
= v
->GetOldAdvanceSpeed(spd
);
439 if (spd
== 0) return false;
440 if ((byte
)++spd
== 0) return true;
442 v
->progress
= (t
= v
->progress
) - (byte
)spd
;
444 return (t
< v
->progress
);
448 * Ship arrives at a dock. If it is the first time, send out a news item.
449 * @param v Ship that arrived.
450 * @param st Station being visited.
452 static void ShipArrivesAt(const Vehicle
*v
, Station
*st
)
454 /* Check if station was ever visited before */
455 if (!(st
->had_vehicle_of_type
& HVOT_SHIP
)) {
456 st
->had_vehicle_of_type
|= HVOT_SHIP
;
458 SetDParam(0, st
->index
);
460 STR_NEWS_FIRST_SHIP_ARRIVAL
,
461 (v
->owner
== _local_company
) ? NT_ARRIVAL_COMPANY
: NT_ARRIVAL_OTHER
,
465 AI::NewEvent(v
->owner
, new ScriptEventStationFirstVehicle(st
->index
, v
->index
));
466 Game::NewEvent(new ScriptEventStationFirstVehicle(st
->index
, v
->index
));
472 * Runs the pathfinder to choose a track to continue along.
474 * @param v Ship to navigate
475 * @param tile Tile, the ship is about to enter
476 * @param enterdir Direction of entering
477 * @param tracks Available track choices on \a tile
478 * @return Track to choose, or INVALID_TRACK when to reverse.
480 static Track
ChooseShipTrack(Ship
*v
, TileIndex tile
, DiagDirection enterdir
, TrackBits tracks
)
482 assert(IsValidDiagDirection(enterdir
));
484 bool path_found
= true;
486 switch (_settings_game
.pf
.pathfinder_for_ships
) {
487 case VPF_OPF
: track
= OPFShipChooseTrack(v
, tile
, enterdir
, tracks
, path_found
); break;
488 case VPF_NPF
: track
= NPFShipChooseTrack(v
, tile
, enterdir
, tracks
, path_found
); break;
489 case VPF_YAPF
: track
= YapfShipChooseTrack(v
, tile
, enterdir
, tracks
, path_found
); break;
490 default: NOT_REACHED();
493 v
->HandlePathfindingResult(path_found
);
497 static inline TrackBits
GetAvailShipTracks(TileIndex tile
, DiagDirection dir
)
499 return GetTileShipTrackStatus(tile
) & DiagdirReachesTracks(dir
);
502 static const byte _ship_subcoord
[4][6][3] = {
537 /* Used to find DiagDirection from tile to next tile if track is followed */
538 static const DiagDirection _diagdir_to_next_tile
[6][4] = {
539 { DIAGDIR_NE
, DIAGDIR_SE
, DIAGDIR_SW
, DIAGDIR_NW
},
540 { DIAGDIR_NE
, DIAGDIR_SE
, DIAGDIR_SW
, DIAGDIR_NW
},
541 { DIAGDIR_SE
, DIAGDIR_NE
, DIAGDIR_NW
, DIAGDIR_SW
},
542 { DIAGDIR_SE
, DIAGDIR_NW
, DIAGDIR_NE
, DIAGDIR_SW
},
543 { DIAGDIR_NW
, DIAGDIR_SW
, DIAGDIR_SE
, DIAGDIR_NE
},
544 { DIAGDIR_SW
, DIAGDIR_NW
, DIAGDIR_SE
, DIAGDIR_NE
}
547 /** Helper function for collision avoidance. */
548 static Vehicle
*FindShipOnTile(Vehicle
*v
, void *data
)
550 if (v
->type
!= VEH_SHIP
|| v
->vehstatus
& VS_STOPPED
) return NULL
;
555 static void ShipController(Ship
*v
)
564 v
->current_order_time
++;
566 if (v
->HandleBreakdown()) return;
568 if (v
->vehstatus
& VS_STOPPED
) return;
573 if (v
->current_order
.IsType(OT_LOADING
)) return;
575 if (CheckShipLeaveDepot(v
)) return;
577 v
->ShowVisualEffect();
579 if (!ShipAccelerate(v
)) return;
581 GetNewVehiclePosResult gp
= GetNewVehiclePos(v
);
582 if (v
->state
!= TRACK_BIT_WORMHOLE
) {
583 /* Not on a bridge */
584 if (gp
.old_tile
== gp
.new_tile
) {
585 /* Staying in tile */
586 if (v
->IsInDepot()) {
590 /* Not inside depot */
591 r
= VehicleEnterTile(v
, gp
.new_tile
, gp
.x
, gp
.y
);
592 if (HasBit(r
, VETS_CANNOT_ENTER
)) goto reverse_direction
;
594 /* A leave station order only needs one tick to get processed, so we can
595 * always skip ahead. */
596 if (v
->current_order
.IsType(OT_LEAVESTATION
)) {
597 v
->current_order
.Free();
598 SetWindowWidgetDirty(WC_VEHICLE_VIEW
, v
->index
, WID_VV_START_STOP
);
599 } else if (v
->dest_tile
!= 0) {
600 /* We have a target, let's see if we reached it... */
601 if (v
->current_order
.IsType(OT_GOTO_WAYPOINT
) &&
602 DistanceManhattan(v
->dest_tile
, gp
.new_tile
) <= 3) {
603 /* We got within 3 tiles of our target buoy, so let's skip to our
605 UpdateVehicleTimetable(v
, true);
606 v
->IncrementRealOrderIndex();
607 v
->current_order
.MakeDummy();
609 /* Non-buoy orders really need to reach the tile */
610 if (v
->dest_tile
== gp
.new_tile
) {
611 if (v
->current_order
.IsType(OT_GOTO_DEPOT
)) {
612 if ((gp
.x
& 0xF) == 8 && (gp
.y
& 0xF) == 8) {
613 VehicleEnterDepot(v
);
616 } else if (v
->current_order
.IsType(OT_GOTO_STATION
)) {
617 v
->last_station_visited
= v
->current_order
.GetDestination();
619 /* Process station in the orderlist. */
620 Station
*st
= Station::Get(v
->current_order
.GetDestination());
621 if (st
->facilities
& FACIL_DOCK
) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
622 ShipArrivesAt(v
, st
);
624 } else { // leave stations without docks right aways
625 v
->current_order
.MakeLeaveStation();
626 v
->IncrementRealOrderIndex();
635 if (!IsValidTile(gp
.new_tile
)) goto reverse_direction
;
637 DiagDirection diagdir
= DiagdirBetweenTiles(gp
.old_tile
, gp
.new_tile
);
638 assert(diagdir
!= INVALID_DIAGDIR
);
639 tracks
= GetAvailShipTracks(gp
.new_tile
, diagdir
);
640 if (tracks
== TRACK_BIT_NONE
) goto reverse_direction
;
642 /* Choose a direction, and continue if we find one */
643 track
= ChooseShipTrack(v
, gp
.new_tile
, diagdir
, tracks
);
644 if (track
== INVALID_TRACK
) goto reverse_direction
;
646 /* Try to avoid collision and keep distance between each other. */
647 if (_settings_game
.pf
.forbid_90_deg
&& DistanceManhattan(v
->dest_tile
, gp
.new_tile
) > 3) {
648 if (HasVehicleOnPos(gp
.new_tile
, NULL
, &FindShipOnTile
) ||
649 HasVehicleOnPos(TileAddByDiagDir(gp
.new_tile
, _diagdir_to_next_tile
[track
][diagdir
]), NULL
, &FindShipOnTile
)) {
651 v
->cur_speed
/= 4; // Go quarter speed.
656 case TRACK_BIT_3WAY_NE
: track
== TRACK_RIGHT
? track
= TRACK_X
: track
= TRACK_UPPER
; break;
657 case TRACK_BIT_3WAY_SE
: track
== TRACK_LOWER
? track
= TRACK_Y
: track
= TRACK_RIGHT
; break;
658 case TRACK_BIT_3WAY_SW
: track
== TRACK_LEFT
? track
= TRACK_X
: track
= TRACK_LOWER
; break;
659 case TRACK_BIT_3WAY_NW
: track
== TRACK_UPPER
? track
= TRACK_Y
: track
= TRACK_LEFT
; break;
661 if (!IsWaterTile(gp
.new_tile
) || !IsWaterTile(TileAddByDiagDir(gp
.new_tile
, _diagdir_to_next_tile
[track
][diagdir
]))) track
= old
; // Don't bump in coast, don't get stuck.
665 b
= _ship_subcoord
[diagdir
][track
];
667 gp
.x
= (gp
.x
& ~0xF) | b
[0];
668 gp
.y
= (gp
.y
& ~0xF) | b
[1];
670 /* Call the landscape function and tell it that the vehicle entered the tile */
671 r
= VehicleEnterTile(v
, gp
.new_tile
, gp
.x
, gp
.y
);
672 if (HasBit(r
, VETS_CANNOT_ENTER
)) goto reverse_direction
;
674 if (!HasBit(r
, VETS_ENTERED_WORMHOLE
)) {
675 v
->tile
= gp
.new_tile
;
676 v
->state
= TrackToTrackBits(track
);
678 /* Update ship cache when the water class changes. Aqueducts are always canals. */
679 WaterClass old_wc
= GetEffectiveWaterClass(gp
.old_tile
);
680 WaterClass new_wc
= GetEffectiveWaterClass(gp
.new_tile
);
681 if (old_wc
!= new_wc
) v
->UpdateCache();
684 v
->direction
= (Direction
)b
[2];
688 if (!IsTileType(gp
.new_tile
, MP_TUNNELBRIDGE
) || !HasBit(VehicleEnterTile(v
, gp
.new_tile
, gp
.x
, gp
.y
), VETS_ENTERED_WORMHOLE
)) {
692 if ((v
->vehstatus
& VS_HIDDEN
) == 0) v
->Vehicle::UpdateViewport(true);
697 /* update image of ship, as well as delta XY */
700 v
->z_pos
= GetSlopePixelZ(gp
.x
, gp
.y
);
704 v
->UpdateViewport(true, true);
708 dir
= ReverseDir(v
->direction
);
715 if (!((this->vehstatus
& VS_STOPPED
) || this->IsChainInDepot())) this->running_ticks
++;
717 ShipController(this);
724 * @param tile tile of the depot where ship is built.
725 * @param flags type of operation.
726 * @param e the engine to build.
727 * @param data unused.
728 * @param ret[out] the vehicle that has been built.
729 * @return the cost of this operation or an error.
731 CommandCost
CmdBuildShip(TileIndex tile
, DoCommandFlag flags
, const Engine
*e
, uint16 data
, Vehicle
**ret
)
733 tile
= GetShipDepotNorthTile(tile
);
734 if (flags
& DC_EXEC
) {
738 const ShipVehicleInfo
*svi
= &e
->u
.ship
;
740 Ship
*v
= new Ship();
743 v
->owner
= _current_company
;
745 x
= TileX(tile
) * TILE_SIZE
+ TILE_SIZE
/ 2;
746 y
= TileY(tile
) * TILE_SIZE
+ TILE_SIZE
/ 2;
749 v
->z_pos
= GetSlopePixelZ(x
, y
);
751 v
->UpdateDeltaXY(v
->direction
);
752 v
->vehstatus
= VS_HIDDEN
| VS_STOPPED
| VS_DEFPAL
;
754 v
->spritenum
= svi
->image_index
;
755 v
->cargo_type
= e
->GetDefaultCargoType();
756 v
->cargo_cap
= svi
->capacity
;
759 v
->last_station_visited
= INVALID_STATION
;
760 v
->last_loading_station
= INVALID_STATION
;
761 v
->engine_type
= e
->index
;
763 v
->reliability
= e
->reliability
;
764 v
->reliability_spd_dec
= e
->reliability_spd_dec
;
765 v
->max_age
= e
->GetLifeLengthInDays();
766 _new_vehicle_id
= v
->index
;
768 v
->state
= TRACK_BIT_DEPOT
;
770 v
->SetServiceInterval(Company::Get(_current_company
)->settings
.vehicle
.servint_ships
);
771 v
->date_of_last_service
= _date
;
772 v
->build_year
= _cur_year
;
773 v
->sprite_seq
.Set(SPR_IMG_QUERY
);
774 v
->random_bits
= VehicleRandomBits();
778 if (e
->flags
& ENGINE_EXCLUSIVE_PREVIEW
) SetBit(v
->vehicle_flags
, VF_BUILT_AS_PROTOTYPE
);
779 v
->SetServiceIntervalIsPercent(Company::Get(_current_company
)->settings
.vehicle
.servint_ispercent
);
781 v
->InvalidateNewGRFCacheOfChain();
783 v
->cargo_cap
= e
->DetermineCapacity(v
);
785 v
->InvalidateNewGRFCacheOfChain();
790 return CommandCost();
793 bool Ship::FindClosestDepot(TileIndex
*location
, DestinationID
*destination
, bool *reverse
)
795 const Depot
*depot
= FindClosestShipDepot(this, 0);
797 if (depot
== NULL
) return false;
799 if (location
!= NULL
) *location
= depot
->xy
;
800 if (destination
!= NULL
) *destination
= depot
->index
;