(svn r27950) -Merge: Documentation updates from 1.7 branch
[openttd.git] / src / ship_cmd.cpp
blob771863a24d782f91b5e037cd6948065a60af35f6
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 "pathfinder/npf/npf_func.h"
19 #include "depot_base.h"
20 #include "station_base.h"
21 #include "newgrf_engine.h"
22 #include "pathfinder/yapf/yapf.h"
23 #include "newgrf_sound.h"
24 #include "spritecache.h"
25 #include "strings_func.h"
26 #include "window_func.h"
27 #include "date_func.h"
28 #include "vehicle_func.h"
29 #include "sound_func.h"
30 #include "ai/ai.hpp"
31 #include "game/game.hpp"
32 #include "pathfinder/opf/opf_ship.h"
33 #include "engine_base.h"
34 #include "company_base.h"
35 #include "tunnelbridge_map.h"
36 #include "zoom_func.h"
38 #include "table/strings.h"
40 #include "safeguards.h"
42 /**
43 * Determine the effective #WaterClass for a ship travelling on a tile.
44 * @param tile Tile of interest
45 * @return the waterclass to be used by the ship.
47 WaterClass GetEffectiveWaterClass(TileIndex tile)
49 if (HasTileWaterClass(tile)) return GetWaterClass(tile);
50 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
51 assert(GetTunnelBridgeTransportType(tile) == TRANSPORT_WATER);
52 return WATER_CLASS_CANAL;
54 if (IsTileType(tile, MP_RAILWAY)) {
55 assert(GetRailGroundType(tile) == RAIL_GROUND_WATER);
56 return WATER_CLASS_SEA;
58 NOT_REACHED();
61 static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
63 template <>
64 bool IsValidImageIndex<VEH_SHIP>(uint8 image_index)
66 return image_index < lengthof(_ship_sprites);
69 static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
71 return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
74 static void GetShipIcon(EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
76 const Engine *e = Engine::Get(engine);
77 uint8 spritenum = e->u.ship.image_index;
79 if (is_custom_sprite(spritenum)) {
80 GetCustomVehicleIcon(engine, DIR_W, image_type, result);
81 if (result->IsValid()) return;
83 spritenum = e->original_image_index;
86 assert(IsValidImageIndex<VEH_SHIP>(spritenum));
87 result->Set(DIR_W + _ship_sprites[spritenum]);
90 void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
92 VehicleSpriteSeq seq;
93 GetShipIcon(engine, image_type, &seq);
95 Rect rect;
96 seq.GetBounds(&rect);
97 preferred_x = Clamp(preferred_x,
98 left - UnScaleGUI(rect.left),
99 right - UnScaleGUI(rect.right));
101 seq.Draw(preferred_x, y, pal, pal == PALETTE_CRASH);
105 * Get the size of the sprite of a ship sprite heading west (used for lists).
106 * @param engine The engine to get the sprite from.
107 * @param[out] width The width of the sprite.
108 * @param[out] height The height of the sprite.
109 * @param[out] xoffs Number of pixels to shift the sprite to the right.
110 * @param[out] yoffs Number of pixels to shift the sprite downwards.
111 * @param image_type Context the sprite is used in.
113 void GetShipSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
115 VehicleSpriteSeq seq;
116 GetShipIcon(engine, image_type, &seq);
118 Rect rect;
119 seq.GetBounds(&rect);
121 width = UnScaleGUI(rect.right - rect.left + 1);
122 height = UnScaleGUI(rect.bottom - rect.top + 1);
123 xoffs = UnScaleGUI(rect.left);
124 yoffs = UnScaleGUI(rect.top);
127 void Ship::GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
129 uint8 spritenum = this->spritenum;
131 if (is_custom_sprite(spritenum)) {
132 GetCustomVehicleSprite(this, direction, image_type, result);
133 if (result->IsValid()) return;
135 spritenum = this->GetEngine()->original_image_index;
138 assert(IsValidImageIndex<VEH_SHIP>(spritenum));
139 result->Set(_ship_sprites[spritenum] + direction);
142 static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
144 /* Find the closest depot */
145 const Depot *depot;
146 const Depot *best_depot = NULL;
147 /* If we don't have a maximum distance, i.e. distance = 0,
148 * we want to find any depot so the best distance of no
149 * depot must be more than any correct distance. On the
150 * other hand if we have set a maximum distance, any depot
151 * further away than max_distance can safely be ignored. */
152 uint best_dist = max_distance == 0 ? UINT_MAX : max_distance + 1;
154 FOR_ALL_DEPOTS(depot) {
155 TileIndex tile = depot->xy;
156 if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
157 uint dist = DistanceManhattan(tile, v->tile);
158 if (dist < best_dist) {
159 best_dist = dist;
160 best_depot = depot;
165 return best_depot;
168 static void CheckIfShipNeedsService(Vehicle *v)
170 if (Company::Get(v->owner)->settings.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
171 if (v->IsChainInDepot()) {
172 VehicleServiceInDepot(v);
173 return;
176 uint max_distance;
177 switch (_settings_game.pf.pathfinder_for_ships) {
178 case VPF_OPF: max_distance = 12; break;
179 case VPF_NPF: max_distance = _settings_game.pf.npf.maximum_go_to_depot_penalty / NPF_TILE_LENGTH; break;
180 case VPF_YAPF: max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; break;
181 default: NOT_REACHED();
184 const Depot *depot = FindClosestShipDepot(v, max_distance);
186 if (depot == NULL) {
187 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
188 v->current_order.MakeDummy();
189 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
191 return;
194 v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
195 v->dest_tile = depot->xy;
196 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
200 * Update the caches of this ship.
202 void Ship::UpdateCache()
204 const ShipVehicleInfo *svi = ShipVehInfo(this->engine_type);
206 /* Get speed fraction for the current water type. Aqueducts are always canals. */
207 bool is_ocean = GetEffectiveWaterClass(this->tile) == WATER_CLASS_SEA;
208 uint raw_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed);
209 this->vcache.cached_max_speed = svi->ApplyWaterClassSpeedFrac(raw_speed, is_ocean);
211 /* Update cargo aging period. */
212 this->vcache.cached_cargo_age_period = GetVehicleProperty(this, PROP_SHIP_CARGO_AGE_PERIOD, EngInfo(this->engine_type)->cargo_age_period);
214 this->UpdateVisualEffect();
217 Money Ship::GetRunningCost() const
219 const Engine *e = this->GetEngine();
220 uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
221 return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
224 void Ship::OnNewDay()
226 if ((++this->day_counter & 7) == 0) {
227 DecreaseVehicleValue(this);
230 CheckVehicleBreakdown(this);
231 AgeVehicle(this);
232 CheckIfShipNeedsService(this);
234 CheckOrders(this);
236 if (this->running_ticks == 0) return;
238 CommandCost cost(EXPENSES_SHIP_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
240 this->profit_this_year -= cost.GetCost();
241 this->running_ticks = 0;
243 SubtractMoneyFromCompanyFract(this->owner, cost);
245 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
246 /* we need this for the profit */
247 SetWindowClassesDirty(WC_SHIPS_LIST);
250 Trackdir Ship::GetVehicleTrackdir() const
252 if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
254 if (this->IsInDepot()) {
255 /* We'll assume the ship is facing outwards */
256 return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
259 if (this->state == TRACK_BIT_WORMHOLE) {
260 /* ship on aqueduct, so just use his direction and assume a diagonal track */
261 return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
264 return TrackDirectionToTrackdir(FindFirstTrack(this->state), this->direction);
267 void Ship::MarkDirty()
269 this->colourmap = PAL_NONE;
270 this->UpdateViewport(true, false);
271 this->UpdateCache();
274 static void PlayShipSound(const Vehicle *v)
276 if (!PlayVehicleSound(v, VSE_START)) {
277 SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v);
281 void Ship::PlayLeaveStationSound() const
283 PlayShipSound(this);
286 TileIndex Ship::GetOrderStationLocation(StationID station)
288 if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
290 const Station *st = Station::Get(station);
291 if (st->dock_tile != INVALID_TILE) {
292 return TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
293 } else {
294 this->IncrementRealOrderIndex();
295 return 0;
299 void Ship::UpdateDeltaXY(Direction direction)
301 static const int8 _delta_xy_table[8][4] = {
302 /* y_extent, x_extent, y_offs, x_offs */
303 { 6, 6, -3, -3}, // N
304 { 6, 32, -3, -16}, // NE
305 { 6, 6, -3, -3}, // E
306 {32, 6, -16, -3}, // SE
307 { 6, 6, -3, -3}, // S
308 { 6, 32, -3, -16}, // SW
309 { 6, 6, -3, -3}, // W
310 {32, 6, -16, -3}, // NW
313 const int8 *bb = _delta_xy_table[direction];
314 this->x_offs = bb[3];
315 this->y_offs = bb[2];
316 this->x_extent = bb[1];
317 this->y_extent = bb[0];
318 this->z_extent = 6;
321 static bool CheckShipLeaveDepot(Ship *v)
323 if (!v->IsChainInDepot()) return false;
325 /* We are leaving a depot, but have to go to the exact same one; re-enter */
326 if (v->current_order.IsType(OT_GOTO_DEPOT) &&
327 IsShipDepotTile(v->tile) && GetDepotIndex(v->tile) == v->current_order.GetDestination()) {
328 VehicleEnterDepot(v);
329 return true;
332 TileIndex tile = v->tile;
333 Axis axis = GetShipDepotAxis(tile);
335 DiagDirection north_dir = ReverseDiagDir(AxisToDiagDir(axis));
336 TileIndex north_neighbour = TILE_ADD(tile, TileOffsByDiagDir(north_dir));
337 DiagDirection south_dir = AxisToDiagDir(axis);
338 TileIndex south_neighbour = TILE_ADD(tile, 2 * TileOffsByDiagDir(south_dir));
340 TrackBits north_tracks = DiagdirReachesTracks(north_dir) & GetTileShipTrackStatus(north_neighbour);
341 TrackBits south_tracks = DiagdirReachesTracks(south_dir) & GetTileShipTrackStatus(south_neighbour);
342 if (north_tracks && south_tracks) {
343 /* Ask pathfinder for best direction */
344 bool reverse = false;
345 bool path_found;
346 switch (_settings_game.pf.pathfinder_for_ships) {
347 case VPF_OPF: reverse = OPFShipChooseTrack(v, north_neighbour, north_dir, north_tracks, path_found) == INVALID_TRACK; break; // OPF always allows reversing
348 case VPF_NPF: reverse = NPFShipCheckReverse(v); break;
349 case VPF_YAPF: reverse = YapfShipCheckReverse(v); break;
350 default: NOT_REACHED();
352 if (reverse) north_tracks = TRACK_BIT_NONE;
355 if (north_tracks) {
356 /* Leave towards north */
357 v->direction = DiagDirToDir(north_dir);
358 } else if (south_tracks) {
359 /* Leave towards south */
360 v->direction = DiagDirToDir(south_dir);
361 } else {
362 /* Both ways blocked */
363 return false;
366 v->state = AxisToTrackBits(axis);
367 v->vehstatus &= ~VS_HIDDEN;
369 v->cur_speed = 0;
370 v->UpdateViewport(true, true);
371 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
373 PlayShipSound(v);
374 VehicleServiceInDepot(v);
375 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
376 SetWindowClassesDirty(WC_SHIPS_LIST);
378 return false;
381 static bool ShipAccelerate(Vehicle *v)
383 uint spd;
384 byte t;
386 spd = min(v->cur_speed + 1, v->vcache.cached_max_speed);
387 spd = min(spd, v->current_order.GetMaxSpeed() * 2);
389 /* updates statusbar only if speed have changed to save CPU time */
390 if (spd != v->cur_speed) {
391 v->cur_speed = spd;
392 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
395 /* Convert direction-independent speed into direction-dependent speed. (old movement method) */
396 spd = v->GetOldAdvanceSpeed(spd);
398 if (spd == 0) return false;
399 if ((byte)++spd == 0) return true;
401 v->progress = (t = v->progress) - (byte)spd;
403 return (t < v->progress);
407 * Ship arrives at a dock. If it is the first time, send out a news item.
408 * @param v Ship that arrived.
409 * @param st Station being visited.
411 static void ShipArrivesAt(const Vehicle *v, Station *st)
413 /* Check if station was ever visited before */
414 if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
415 st->had_vehicle_of_type |= HVOT_SHIP;
417 SetDParam(0, st->index);
418 AddVehicleNewsItem(
419 STR_NEWS_FIRST_SHIP_ARRIVAL,
420 (v->owner == _local_company) ? NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
421 v->index,
422 st->index
424 AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
425 Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
431 * Runs the pathfinder to choose a track to continue along.
433 * @param v Ship to navigate
434 * @param tile Tile, the ship is about to enter
435 * @param enterdir Direction of entering
436 * @param tracks Available track choices on \a tile
437 * @return Track to choose, or INVALID_TRACK when to reverse.
439 static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
441 assert(IsValidDiagDirection(enterdir));
443 bool path_found = true;
444 Track track;
445 switch (_settings_game.pf.pathfinder_for_ships) {
446 case VPF_OPF: track = OPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
447 case VPF_NPF: track = NPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
448 case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
449 default: NOT_REACHED();
452 v->HandlePathfindingResult(path_found);
453 return track;
456 static inline TrackBits GetAvailShipTracks(TileIndex tile, DiagDirection dir)
458 return GetTileShipTrackStatus(tile) & DiagdirReachesTracks(dir);
461 static const byte _ship_subcoord[4][6][3] = {
463 {15, 8, 1},
464 { 0, 0, 0},
465 { 0, 0, 0},
466 {15, 8, 2},
467 {15, 7, 0},
468 { 0, 0, 0},
471 { 0, 0, 0},
472 { 8, 0, 3},
473 { 7, 0, 2},
474 { 0, 0, 0},
475 { 8, 0, 4},
476 { 0, 0, 0},
479 { 0, 8, 5},
480 { 0, 0, 0},
481 { 0, 7, 6},
482 { 0, 0, 0},
483 { 0, 0, 0},
484 { 0, 8, 4},
487 { 0, 0, 0},
488 { 8, 15, 7},
489 { 0, 0, 0},
490 { 8, 15, 6},
491 { 0, 0, 0},
492 { 7, 15, 0},
496 static void ShipController(Ship *v)
498 uint32 r;
499 const byte *b;
500 Direction dir;
501 Track track;
502 TrackBits tracks;
504 v->tick_counter++;
505 v->current_order_time++;
507 if (v->HandleBreakdown()) return;
509 if (v->vehstatus & VS_STOPPED) return;
511 ProcessOrders(v);
512 v->HandleLoading();
514 if (v->current_order.IsType(OT_LOADING)) return;
516 if (CheckShipLeaveDepot(v)) return;
518 v->ShowVisualEffect();
520 if (!ShipAccelerate(v)) return;
522 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
523 if (v->state != TRACK_BIT_WORMHOLE) {
524 /* Not on a bridge */
525 if (gp.old_tile == gp.new_tile) {
526 /* Staying in tile */
527 if (v->IsInDepot()) {
528 gp.x = v->x_pos;
529 gp.y = v->y_pos;
530 } else {
531 /* Not inside depot */
532 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
533 if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
535 /* A leave station order only needs one tick to get processed, so we can
536 * always skip ahead. */
537 if (v->current_order.IsType(OT_LEAVESTATION)) {
538 v->current_order.Free();
539 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
540 } else if (v->dest_tile != 0) {
541 /* We have a target, let's see if we reached it... */
542 if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
543 DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
544 /* We got within 3 tiles of our target buoy, so let's skip to our
545 * next order */
546 UpdateVehicleTimetable(v, true);
547 v->IncrementRealOrderIndex();
548 v->current_order.MakeDummy();
549 } else {
550 /* Non-buoy orders really need to reach the tile */
551 if (v->dest_tile == gp.new_tile) {
552 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
553 if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
554 VehicleEnterDepot(v);
555 return;
557 } else if (v->current_order.IsType(OT_GOTO_STATION)) {
558 v->last_station_visited = v->current_order.GetDestination();
560 /* Process station in the orderlist. */
561 Station *st = Station::Get(v->current_order.GetDestination());
562 if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
563 ShipArrivesAt(v, st);
564 v->BeginLoading();
565 } else { // leave stations without docks right aways
566 v->current_order.MakeLeaveStation();
567 v->IncrementRealOrderIndex();
574 } else {
575 /* New tile */
576 if (!IsValidTile(gp.new_tile)) goto reverse_direction;
578 DiagDirection diagdir = DiagdirBetweenTiles(gp.old_tile, gp.new_tile);
579 assert(diagdir != INVALID_DIAGDIR);
580 tracks = GetAvailShipTracks(gp.new_tile, diagdir);
581 if (tracks == TRACK_BIT_NONE) goto reverse_direction;
583 /* Choose a direction, and continue if we find one */
584 track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
585 if (track == INVALID_TRACK) goto reverse_direction;
587 b = _ship_subcoord[diagdir][track];
589 gp.x = (gp.x & ~0xF) | b[0];
590 gp.y = (gp.y & ~0xF) | b[1];
592 /* Call the landscape function and tell it that the vehicle entered the tile */
593 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
594 if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
596 if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
597 v->tile = gp.new_tile;
598 v->state = TrackToTrackBits(track);
600 /* Update ship cache when the water class changes. Aqueducts are always canals. */
601 WaterClass old_wc = GetEffectiveWaterClass(gp.old_tile);
602 WaterClass new_wc = GetEffectiveWaterClass(gp.new_tile);
603 if (old_wc != new_wc) v->UpdateCache();
606 v->direction = (Direction)b[2];
608 } else {
609 /* On a bridge */
610 if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
611 v->x_pos = gp.x;
612 v->y_pos = gp.y;
613 v->UpdatePosition();
614 if ((v->vehstatus & VS_HIDDEN) == 0) v->Vehicle::UpdateViewport(true);
615 return;
619 /* update image of ship, as well as delta XY */
620 v->x_pos = gp.x;
621 v->y_pos = gp.y;
622 v->z_pos = GetSlopePixelZ(gp.x, gp.y);
624 getout:
625 v->UpdatePosition();
626 v->UpdateViewport(true, true);
627 return;
629 reverse_direction:
630 dir = ReverseDir(v->direction);
631 v->direction = dir;
632 goto getout;
635 bool Ship::Tick()
637 if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
639 ShipController(this);
641 return true;
645 * Build a ship.
646 * @param tile tile of the depot where ship is built.
647 * @param flags type of operation.
648 * @param e the engine to build.
649 * @param data unused.
650 * @param ret[out] the vehicle that has been built.
651 * @return the cost of this operation or an error.
653 CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **ret)
655 tile = GetShipDepotNorthTile(tile);
656 if (flags & DC_EXEC) {
657 int x;
658 int y;
660 const ShipVehicleInfo *svi = &e->u.ship;
662 Ship *v = new Ship();
663 *ret = v;
665 v->owner = _current_company;
666 v->tile = tile;
667 x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
668 y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
669 v->x_pos = x;
670 v->y_pos = y;
671 v->z_pos = GetSlopePixelZ(x, y);
673 v->UpdateDeltaXY(v->direction);
674 v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
676 v->spritenum = svi->image_index;
677 v->cargo_type = e->GetDefaultCargoType();
678 v->cargo_cap = svi->capacity;
679 v->refit_cap = 0;
681 v->last_station_visited = INVALID_STATION;
682 v->last_loading_station = INVALID_STATION;
683 v->engine_type = e->index;
685 v->reliability = e->reliability;
686 v->reliability_spd_dec = e->reliability_spd_dec;
687 v->max_age = e->GetLifeLengthInDays();
688 _new_vehicle_id = v->index;
690 v->state = TRACK_BIT_DEPOT;
692 v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_ships);
693 v->date_of_last_service = _date;
694 v->build_year = _cur_year;
695 v->sprite_seq.Set(SPR_IMG_QUERY);
696 v->random_bits = VehicleRandomBits();
698 v->UpdateCache();
700 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
701 v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
703 v->InvalidateNewGRFCacheOfChain();
705 v->cargo_cap = e->DetermineCapacity(v);
707 v->InvalidateNewGRFCacheOfChain();
709 v->UpdatePosition();
712 return CommandCost();
715 bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
717 const Depot *depot = FindClosestShipDepot(this, 0);
719 if (depot == NULL) return false;
721 if (location != NULL) *location = depot->xy;
722 if (destination != NULL) *destination = depot->index;
724 return true;