Rearrange conditionals in FreeTrainTrackReservation
[openttd/fttd.git] / src / ship_cmd.cpp
blob16d0c4a8cb3428146e66da47cafc96ddec7aa7a0
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file ship_cmd.cpp Handling of ships. */
12 #include "stdafx.h"
13 #include "ship.h"
14 #include "landscape.h"
15 #include "timetable.h"
16 #include "news_func.h"
17 #include "company_func.h"
18 #include "depot_base.h"
19 #include "station_base.h"
20 #include "newgrf_engine.h"
21 #include "pathfinder/yapf/yapf.h"
22 #include "newgrf_sound.h"
23 #include "spritecache.h"
24 #include "strings_func.h"
25 #include "window_func.h"
26 #include "date_func.h"
27 #include "vehicle_func.h"
28 #include "sound_func.h"
29 #include "ai/ai.hpp"
30 #include "pathfinder/opf/opf_ship.h"
31 #include "engine_base.h"
32 #include "company_base.h"
33 #include "zoom_func.h"
34 #include "map/bridge.h"
36 #include "table/strings.h"
38 /**
39 * Determine the effective #WaterClass for a ship travelling on a tile.
40 * @param tile Tile of interest
41 * @return the waterclass to be used by the ship.
43 WaterClass GetEffectiveWaterClass(TileIndex tile)
45 if (HasTileWaterClass(tile)) return GetWaterClass(tile);
46 if (IsAqueductTile(tile)) return WATER_CLASS_CANAL;
47 if (IsNormalRailTile(tile)) {
48 assert(GetRailGroundType(tile) == RAIL_GROUND_WATER);
49 return WATER_CLASS_SEA;
51 NOT_REACHED();
54 static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
56 template <>
57 bool IsValidImageIndex<VEH_SHIP>(uint8 image_index)
59 return image_index < lengthof(_ship_sprites);
62 static inline TrackdirBits GetAvailShipTrackdirs(TileIndex tile, DiagDirection enterdir)
64 return GetTileWaterwayStatus(tile) & DiagdirReachesTrackdirs(enterdir);
67 static SpriteID GetShipIcon(EngineID engine, EngineImageType image_type)
69 const Engine *e = Engine::Get(engine);
70 uint8 spritenum = e->u.ship.image_index;
72 if (is_custom_sprite(spritenum)) {
73 SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W, image_type);
74 if (sprite != 0) return sprite;
76 spritenum = e->original_image_index;
79 assert(IsValidImageIndex<VEH_SHIP>(spritenum));
80 return DIR_W + _ship_sprites[spritenum];
83 void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
85 SpriteID sprite = GetShipIcon(engine, image_type);
86 const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
87 preferred_x = Clamp(preferred_x, left - UnScaleByZoom(real_sprite->x_offs, ZOOM_LVL_GUI), right - UnScaleByZoom(real_sprite->width, ZOOM_LVL_GUI) - UnScaleByZoom(real_sprite->x_offs, ZOOM_LVL_GUI));
88 DrawSprite(sprite, pal, preferred_x, y);
91 /**
92 * Get the size of the sprite of a ship sprite heading west (used for lists).
93 * @param engine The engine to get the sprite from.
94 * @param[out] width The width of the sprite.
95 * @param[out] height The height of the sprite.
96 * @param[out] xoffs Number of pixels to shift the sprite to the right.
97 * @param[out] yoffs Number of pixels to shift the sprite downwards.
98 * @param image_type Context the sprite is used in.
100 void GetShipSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
102 const Sprite *spr = GetSprite(GetShipIcon(engine, image_type), ST_NORMAL);
104 width = UnScaleByZoom(spr->width, ZOOM_LVL_GUI);
105 height = UnScaleByZoom(spr->height, ZOOM_LVL_GUI);
106 xoffs = UnScaleByZoom(spr->x_offs, ZOOM_LVL_GUI);
107 yoffs = UnScaleByZoom(spr->y_offs, ZOOM_LVL_GUI);
110 SpriteID Ship::GetImage(Direction direction, EngineImageType image_type) const
112 uint8 spritenum = this->spritenum;
114 if (is_custom_sprite(spritenum)) {
115 SpriteID sprite = GetCustomVehicleSprite(this, direction, image_type);
116 if (sprite != 0) return sprite;
118 spritenum = this->GetEngine()->original_image_index;
121 assert(IsValidImageIndex<VEH_SHIP>(spritenum));
122 return _ship_sprites[spritenum] + direction;
125 static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
127 /* Find the closest depot */
128 const Depot *depot;
129 const Depot *best_depot = NULL;
130 /* If we don't have a maximum distance, i.e. distance = 0,
131 * we want to find any depot so the best distance of no
132 * depot must be more than any correct distance. On the
133 * other hand if we have set a maximum distance, any depot
134 * further away than max_distance can safely be ignored. */
135 uint best_dist = max_distance == 0 ? UINT_MAX : max_distance + 1;
137 FOR_ALL_DEPOTS(depot) {
138 TileIndex tile = depot->xy;
139 if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
140 uint dist = DistanceManhattan(tile, v->tile);
141 if (dist < best_dist) {
142 best_dist = dist;
143 best_depot = depot;
148 return best_depot;
151 static void CheckIfShipNeedsService(Vehicle *v)
153 if (Company::Get(v->owner)->settings.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
154 if (v->IsChainInDepot()) {
155 VehicleServiceInDepot(v);
156 return;
159 uint max_distance;
160 switch (_settings_game.pf.pathfinder_for_ships) {
161 case VPF_OPF: max_distance = 12; break;
162 case VPF_YAPF: max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; break;
163 default: NOT_REACHED();
166 const Depot *depot = FindClosestShipDepot(v, max_distance);
168 if (depot == NULL) {
169 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
170 v->current_order.MakeDummy();
171 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
173 return;
176 v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
177 v->dest_tile = depot->xy;
178 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
182 * Update the caches of this ship.
184 void Ship::UpdateCache()
186 const ShipVehicleInfo *svi = ShipVehInfo(this->engine_type);
188 /* Get speed fraction for the current water type. Aqueducts are always canals. */
189 bool is_ocean = GetEffectiveWaterClass(this->tile) == WATER_CLASS_SEA;
190 uint raw_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed);
191 this->vcache.cached_max_speed = svi->ApplyWaterClassSpeedFrac(raw_speed, is_ocean);
193 /* Update cargo aging period. */
194 this->vcache.cached_cargo_age_period = GetVehicleProperty(this, PROP_SHIP_CARGO_AGE_PERIOD, EngInfo(this->engine_type)->cargo_age_period);
196 this->UpdateVisualEffect();
199 Money Ship::GetRunningCost() const
201 const Engine *e = this->GetEngine();
202 uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
203 return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
206 void Ship::OnNewDay()
208 if ((++this->day_counter & 7) == 0) {
209 DecreaseVehicleValue(this);
212 CheckVehicleBreakdown(this);
213 AgeVehicle(this);
214 CheckIfShipNeedsService(this);
216 CheckOrders(this);
218 if (this->running_ticks == 0) return;
220 CommandCost cost(EXPENSES_SHIP_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
222 this->profit_this_year -= cost.GetCost();
223 this->running_ticks = 0;
225 SubtractMoneyFromCompanyFract(this->owner, cost);
227 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
228 /* we need this for the profit */
229 SetWindowClassesDirty(WC_SHIPS_LIST);
232 ShipPathPos Ship::GetPos() const
234 if (this->vehstatus & VS_CRASHED) return ShipPathPos();
236 Trackdir td;
238 if (this->IsInDepot()) {
239 /* We'll assume the ship is facing outwards */
240 td = DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
241 } else if (this->trackdir == TRACKDIR_WORMHOLE) {
242 /* ship on aqueduct, so just use his direction and assume a diagonal track */
243 td = DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
244 } else {
245 td = this->trackdir;
248 return ShipPathPos(this->tile, td);
251 void Ship::MarkDirty()
253 this->colourmap = PAL_NONE;
254 this->UpdateViewport(true, false);
255 this->UpdateCache();
258 static void PlayShipSound(const Vehicle *v)
260 if (!PlayVehicleSound(v, VSE_START)) {
261 SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v);
265 void Ship::PlayLeaveStationSound() const
267 PlayShipSound(this);
270 TileIndex Ship::GetOrderStationLocation(StationID station)
272 if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
274 const Station *st = Station::Get(station);
275 if (!(st->facilities & FACIL_DOCK)) {
276 this->IncrementRealOrderIndex();
277 return 0;
280 return st->xy;
283 void Ship::UpdateDeltaXY(Direction direction)
285 static const int8 _delta_xy_table[8][4] = {
286 /* y_extent, x_extent, y_offs, x_offs */
287 { 6, 6, -3, -3}, // N
288 { 6, 32, -3, -16}, // NE
289 { 6, 6, -3, -3}, // E
290 {32, 6, -16, -3}, // SE
291 { 6, 6, -3, -3}, // S
292 { 6, 32, -3, -16}, // SW
293 { 6, 6, -3, -3}, // W
294 {32, 6, -16, -3}, // NW
297 const int8 *bb = _delta_xy_table[direction];
298 this->x_offs = bb[3];
299 this->y_offs = bb[2];
300 this->x_extent = bb[1];
301 this->y_extent = bb[0];
302 this->z_extent = 6;
305 static bool CheckShipLeaveDepot(Ship *v)
307 if (!v->IsChainInDepot()) return false;
309 /* We are leaving a depot, but have to go to the exact same one; re-enter */
310 if (v->current_order.IsType(OT_GOTO_DEPOT) &&
311 IsShipDepotTile(v->tile) && GetDepotIndex(v->tile) == v->current_order.GetDestination()) {
312 VehicleEnterDepot(v);
313 return true;
316 TileIndex tile = v->tile;
318 DiagDirection north_dir = GetShipDepotDirection(tile);
319 TileIndex north_neighbour = TILE_ADD(tile, TileOffsByDiagDir(north_dir));
320 DiagDirection south_dir = ReverseDiagDir(north_dir);
321 TileIndex south_neighbour = TILE_ADD(tile, 2 * TileOffsByDiagDir(south_dir));
323 TrackdirBits north_trackdirs = GetAvailShipTrackdirs(north_neighbour, north_dir);
324 TrackdirBits south_trackdirs = GetAvailShipTrackdirs(south_neighbour, south_dir);
325 if (north_trackdirs && south_trackdirs) {
326 /* Ask pathfinder for best direction */
327 bool reverse = false;
328 bool path_found;
329 switch (_settings_game.pf.pathfinder_for_ships) {
330 case VPF_OPF: reverse = OPFShipChooseTrack(v, north_neighbour, north_dir, north_trackdirs, path_found) == INVALID_TRACKDIR; break; // OPF always allows reversing
331 case VPF_YAPF: reverse = YapfShipCheckReverse(v); break;
332 default: NOT_REACHED();
334 if (reverse) north_trackdirs = TRACKDIR_BIT_NONE;
337 if (north_trackdirs) {
338 /* Leave towards north */
339 v->direction = DiagDirToDir(north_dir);
340 v->trackdir = DiagDirToDiagTrackdir(north_dir);
341 } else if (south_trackdirs) {
342 /* Leave towards south */
343 v->direction = DiagDirToDir(south_dir);
344 v->trackdir = DiagDirToDiagTrackdir(south_dir);
345 } else {
346 /* Both ways blocked */
347 return false;
350 v->vehstatus &= ~VS_HIDDEN;
352 v->cur_speed = 0;
353 v->UpdateViewport(true, true);
354 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
356 PlayShipSound(v);
357 VehicleServiceInDepot(v);
358 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
359 SetWindowClassesDirty(WC_SHIPS_LIST);
361 return false;
364 static bool ShipAccelerate(Vehicle *v)
366 uint spd;
367 byte t;
369 spd = min(v->cur_speed + 1, v->vcache.cached_max_speed);
370 spd = min(spd, v->current_order.max_speed * 2);
372 /* updates statusbar only if speed have changed to save CPU time */
373 if (spd != v->cur_speed) {
374 v->cur_speed = spd;
375 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
378 /* Convert direction-independent speed into direction-dependent speed. (old movement method) */
379 spd = v->GetOldAdvanceSpeed(spd);
381 if (spd == 0) return false;
382 if ((byte)++spd == 0) return true;
384 v->progress = (t = v->progress) - (byte)spd;
386 return (t < v->progress);
390 * Ship arrives at a dock. If it is the first time, send out a news item.
391 * @param v Ship that arrived.
392 * @param st Station being visited.
394 static void ShipArrivesAt(const Vehicle *v, Station *st)
396 /* Check if station was ever visited before */
397 if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
398 st->had_vehicle_of_type |= HVOT_SHIP;
400 SetDParam(0, st->index);
401 AddVehicleNewsItem(
402 STR_NEWS_FIRST_SHIP_ARRIVAL,
403 (v->owner == _local_company) ? NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
404 v->index,
405 st->index
407 AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
413 * Runs the pathfinder to choose a trackdir to continue along.
415 * @param v Ship to navigate
416 * @param tile Tile the ship is about to enter
417 * @param enterdir Direction of entering
418 * @param trackdirs Available trackdir choices on \a tile
419 * @return Trackdir to choose, or INVALID_TRACKDIR when to reverse.
421 static Trackdir ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirs)
423 assert(IsValidDiagDirection(enterdir));
425 bool path_found = true;
426 Trackdir trackdir;
427 switch (_settings_game.pf.pathfinder_for_ships) {
428 case VPF_OPF: trackdir = OPFShipChooseTrack(v, tile, enterdir, trackdirs, path_found); break;
429 case VPF_YAPF: trackdir = YapfShipChooseTrack(v, tile, enterdir, trackdirs, path_found); break;
430 default: NOT_REACHED();
433 v->HandlePathfindingResult(path_found);
434 return trackdir;
437 static const byte _ship_subcoord[TRACKDIR_END][3] = {
438 {15, 8, 1}, // TRACKDIR_X_NE
439 { 8, 0, 3}, // TRACKDIR_Y_SE
440 { 7, 0, 2}, // TRACKDIR_UPPER_E
441 {15, 8, 2}, // TRACKDIR_LOWER_E
442 { 8, 0, 4}, // TRACKDIR_LEFT_S
443 { 0, 8, 4}, // TRACKDIR_RIGHT_S
444 { 0, 0, 0},
445 { 0, 0, 0},
446 { 0, 8, 5}, // TRACKDIR_X_SW
447 { 8,15, 7}, // TRACKDIR_Y_NW
448 { 0, 7, 6}, // TRACKDIR_UPPER_W
449 { 8,15, 6}, // TRACKDIR_LOWER_W
450 {15, 7, 0}, // TRACKDIR_LEFT_N
451 { 7,15, 0}, // TRACKDIR_RIGHT_N
454 static void ShipController(Ship *v)
456 v->tick_counter++;
457 v->current_order_time++;
459 if (v->HandleBreakdown()) return;
461 if (v->vehstatus & VS_STOPPED) return;
463 ProcessOrders(v);
464 v->HandleLoading();
466 if (v->current_order.IsType(OT_LOADING)) return;
468 if (CheckShipLeaveDepot(v)) return;
470 v->ShowVisualEffect();
472 if (!ShipAccelerate(v)) return;
474 VehiclePos gp = GetNewVehiclePos(v);
475 if (v->trackdir == TRACKDIR_WORMHOLE) {
476 /* On a bridge */
477 if (gp.new_tile != v->tile) {
478 /* Still on the bridge */
479 v->x_pos = gp.x;
480 v->y_pos = gp.y;
481 VehicleUpdatePositionAndViewport(v);
482 return;
483 } else {
484 v->trackdir = DiagDirToDiagTrackdir(ReverseDiagDir(GetTunnelBridgeDirection(v->tile)));
486 } else if (v->trackdir == TRACKDIR_DEPOT) {
487 /* Inside depot */
488 assert(gp.new_tile == v->tile);
489 gp.x = v->x_pos;
490 gp.y = v->y_pos;
491 } else if (gp.new_tile == v->tile) {
492 /* Not on a bridge or in a depot, staying in the old tile */
494 /* A leave station order only needs one tick to get processed, so we can
495 * always skip ahead. */
496 if (v->current_order.IsType(OT_LEAVESTATION)) {
497 v->current_order.Free();
498 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
499 } else if (v->dest_tile != 0) {
500 /* We have a target, let's see if we reached it... */
501 if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
502 DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
503 /* We got within 3 tiles of our target buoy, so let's skip to our
504 * next order */
505 UpdateVehicleTimetable(v, true);
506 v->IncrementRealOrderIndex();
507 v->current_order.MakeDummy();
508 } else if (v->current_order.IsType(OT_GOTO_DEPOT)) {
509 if (v->dest_tile == gp.new_tile && (gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
510 VehicleEnterDepot(v);
511 return;
513 } else if (v->current_order.IsType(OT_GOTO_STATION)) {
514 StationID sid = v->current_order.GetDestination();
515 Station *st = Station::Get(sid);
516 if (st->IsDockingTile(gp.new_tile)) {
517 assert(st->facilities & FACIL_DOCK);
518 v->last_station_visited = sid;
519 /* Process station in the orderlist. */
520 ShipArrivesAt(v, st);
521 v->BeginLoading();
525 } else {
526 /* Not on a bridge or in a depot, about to enter a new tile */
528 if (!IsValidTile(gp.new_tile)) goto reverse_direction;
530 DiagDirection diagdir = DiagdirBetweenTiles(v->tile, gp.new_tile);
531 assert(diagdir != INVALID_DIAGDIR);
533 if (IsAqueductTile(v->tile) && GetTunnelBridgeDirection(v->tile) == diagdir) {
534 TileIndex end_tile = GetOtherBridgeEnd(v->tile);
535 if (end_tile != gp.new_tile) {
536 /* Entering an aqueduct */
537 v->tile = end_tile;
538 v->trackdir = TRACKDIR_WORMHOLE;
539 v->x_pos = gp.x;
540 v->y_pos = gp.y;
541 VehicleUpdatePositionAndViewport(v);
542 return;
546 TrackdirBits trackdirs = GetAvailShipTrackdirs(gp.new_tile, diagdir);
547 if (trackdirs == TRACKDIR_BIT_NONE) goto reverse_direction;
549 /* Choose a direction, and continue if we find one */
550 Trackdir trackdir = ChooseShipTrack(v, gp.new_tile, diagdir, trackdirs);
551 if (trackdir == INVALID_TRACKDIR) goto reverse_direction;
553 const byte *b = _ship_subcoord[trackdir];
555 gp.x = (gp.x & ~0xF) | b[0];
556 gp.y = (gp.y & ~0xF) | b[1];
558 WaterClass old_wc = GetEffectiveWaterClass(v->tile);
560 v->tile = gp.new_tile;
561 v->trackdir = trackdir;
563 /* Update ship cache when the water class changes. Aqueducts are always canals. */
564 WaterClass new_wc = GetEffectiveWaterClass(gp.new_tile);
565 if (old_wc != new_wc) v->UpdateCache();
567 v->direction = (Direction)b[2];
570 /* update image of ship, as well as delta XY */
571 v->x_pos = gp.x;
572 v->y_pos = gp.y;
573 v->z_pos = GetSlopePixelZ(gp.x, gp.y);
575 getout:
576 VehicleUpdatePosition(v);
577 v->UpdateViewport(true, true);
578 return;
580 reverse_direction:
581 v->direction = ReverseDir(v->direction);
582 v->trackdir = ReverseTrackdir(v->trackdir);
583 goto getout;
586 bool Ship::Tick()
588 if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
590 ShipController(this);
592 return true;
596 * Build a ship.
597 * @param tile tile of the depot where ship is built.
598 * @param flags type of operation.
599 * @param e the engine to build.
600 * @param data unused.
601 * @param ret[out] the vehicle that has been built.
602 * @return the cost of this operation or an error.
604 CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **ret)
606 tile = GetShipDepotNorthTile(tile);
607 if (flags & DC_EXEC) {
608 int x;
609 int y;
611 const ShipVehicleInfo *svi = &e->u.ship;
613 Ship *v = new Ship();
614 *ret = v;
616 v->owner = _current_company;
617 v->tile = tile;
618 x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
619 y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
620 v->x_pos = x;
621 v->y_pos = y;
622 v->z_pos = GetSlopePixelZ(x, y);
624 v->UpdateDeltaXY(v->direction);
625 v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
627 v->spritenum = svi->image_index;
628 v->cargo_type = e->GetDefaultCargoType();
629 v->cargo_cap = svi->capacity;
630 v->refit_cap = 0;
632 v->last_station_visited = INVALID_STATION;
633 v->last_loading_station = INVALID_STATION;
634 v->engine_type = e->index;
636 v->reliability = e->reliability;
637 v->reliability_spd_dec = e->reliability_spd_dec;
638 v->max_age = e->GetLifeLengthInDays();
639 _new_vehicle_id = v->index;
641 v->trackdir = TRACKDIR_DEPOT;
643 v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_ships);
644 v->date_of_last_service = _date;
645 v->build_year = _cur_year;
646 v->cur_image = SPR_IMG_QUERY;
647 v->random_bits = VehicleRandomBits();
649 v->UpdateCache();
651 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
652 v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
654 v->InvalidateNewGRFCacheOfChain();
656 v->cargo_cap = e->DetermineCapacity(v);
658 v->InvalidateNewGRFCacheOfChain();
660 VehicleUpdatePosition(v);
663 return CommandCost();
666 bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
668 const Depot *depot = FindClosestShipDepot(this, 0);
670 if (depot == NULL) return false;
672 if (location != NULL) *location = depot->xy;
673 if (destination != NULL) *destination = depot->index;
675 return true;