(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / ship_cmd.cpp
blob2a760e2ca7958849f63d144e51fdae17c7b11597
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 "pathfinder/opf/opf_ship.h"
32 #include "engine_base.h"
33 #include "company_base.h"
34 #include "tunnelbridge_map.h"
35 #include "zoom_func.h"
37 #include "table/strings.h"
39 #include "safeguards.h"
41 /**
42 * Determine the effective #WaterClass for a ship travelling on a tile.
43 * @param tile Tile of interest
44 * @return the waterclass to be used by the ship.
46 WaterClass GetEffectiveWaterClass(TileIndex tile)
48 if (HasTileWaterClass(tile)) return GetWaterClass(tile);
49 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
50 assert(GetTunnelBridgeTransportType(tile) == TRANSPORT_WATER);
51 return WATER_CLASS_CANAL;
53 if (IsTileType(tile, MP_RAILWAY)) {
54 assert(GetRailGroundType(tile) == RAIL_GROUND_WATER);
55 return WATER_CLASS_SEA;
57 NOT_REACHED();
60 static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
62 template <>
63 bool IsValidImageIndex<VEH_SHIP>(uint8 image_index)
65 return image_index < lengthof(_ship_sprites);
68 static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
70 return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
73 static void GetShipIcon(EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
75 const Engine *e = Engine::Get(engine);
76 uint8 spritenum = e->u.ship.image_index;
78 if (is_custom_sprite(spritenum)) {
79 GetCustomVehicleIcon(engine, DIR_W, image_type, result);
80 if (result->IsValid()) return;
82 spritenum = e->original_image_index;
85 assert(IsValidImageIndex<VEH_SHIP>(spritenum));
86 result->Set(DIR_W + _ship_sprites[spritenum]);
89 void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
91 VehicleSpriteSeq seq;
92 GetShipIcon(engine, image_type, &seq);
94 Rect rect;
95 seq.GetBounds(&rect);
96 preferred_x = Clamp(preferred_x,
97 left - UnScaleGUI(rect.left),
98 right - UnScaleGUI(rect.right));
100 seq.Draw(preferred_x, y, pal, pal == PALETTE_CRASH);
104 * Get the size of the sprite of a ship sprite heading west (used for lists).
105 * @param engine The engine to get the sprite from.
106 * @param[out] width The width of the sprite.
107 * @param[out] height The height of the sprite.
108 * @param[out] xoffs Number of pixels to shift the sprite to the right.
109 * @param[out] yoffs Number of pixels to shift the sprite downwards.
110 * @param image_type Context the sprite is used in.
112 void GetShipSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
114 VehicleSpriteSeq seq;
115 GetShipIcon(engine, image_type, &seq);
117 Rect rect;
118 seq.GetBounds(&rect);
120 width = UnScaleGUI(rect.right - rect.left + 1);
121 height = UnScaleGUI(rect.bottom - rect.top + 1);
122 xoffs = UnScaleGUI(rect.left);
123 yoffs = UnScaleGUI(rect.top);
126 void Ship::GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
128 uint8 spritenum = this->spritenum;
130 if (is_custom_sprite(spritenum)) {
131 GetCustomVehicleSprite(this, direction, image_type, result);
132 if (result->IsValid()) return;
134 spritenum = this->GetEngine()->original_image_index;
137 assert(IsValidImageIndex<VEH_SHIP>(spritenum));
138 result->Set(_ship_sprites[spritenum] + direction);
141 static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
143 /* Find the closest depot */
144 const Depot *depot;
145 const Depot *best_depot = NULL;
146 /* If we don't have a maximum distance, i.e. distance = 0,
147 * we want to find any depot so the best distance of no
148 * depot must be more than any correct distance. On the
149 * other hand if we have set a maximum distance, any depot
150 * further away than max_distance can safely be ignored. */
151 uint best_dist = max_distance == 0 ? UINT_MAX : max_distance + 1;
153 FOR_ALL_DEPOTS(depot) {
154 TileIndex tile = depot->xy;
155 if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
156 uint dist = DistanceManhattan(tile, v->tile);
157 if (dist < best_dist) {
158 best_dist = dist;
159 best_depot = depot;
164 return best_depot;
167 static void CheckIfShipNeedsService(Vehicle *v)
169 if (Company::Get(v->owner)->settings.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
170 if (v->IsChainInDepot()) {
171 VehicleServiceInDepot(v);
172 return;
175 uint max_distance;
176 switch (_settings_game.pf.pathfinder_for_ships) {
177 case VPF_OPF: max_distance = 12; break;
178 case VPF_NPF: max_distance = _settings_game.pf.npf.maximum_go_to_depot_penalty / NPF_TILE_LENGTH; break;
179 case VPF_YAPF: max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; break;
180 default: NOT_REACHED();
183 const Depot *depot = FindClosestShipDepot(v, max_distance);
185 if (depot == NULL) {
186 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
187 v->current_order.MakeDummy();
188 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
190 return;
193 v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
194 v->dest_tile = depot->xy;
195 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
199 * Update the caches of this ship.
201 void Ship::UpdateCache()
203 const ShipVehicleInfo *svi = ShipVehInfo(this->engine_type);
205 /* Get speed fraction for the current water type. Aqueducts are always canals. */
206 bool is_ocean = GetEffectiveWaterClass(this->tile) == WATER_CLASS_SEA;
207 uint raw_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed);
208 this->vcache.cached_max_speed = svi->ApplyWaterClassSpeedFrac(raw_speed, is_ocean);
210 /* Update cargo aging period. */
211 this->vcache.cached_cargo_age_period = GetVehicleProperty(this, PROP_SHIP_CARGO_AGE_PERIOD, EngInfo(this->engine_type)->cargo_age_period);
213 this->UpdateVisualEffect();
216 Money Ship::GetRunningCost() const
218 const Engine *e = this->GetEngine();
219 uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
220 return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
223 void Ship::OnNewDay()
225 if ((++this->day_counter & 7) == 0) {
226 DecreaseVehicleValue(this);
229 CheckVehicleBreakdown(this);
230 AgeVehicle(this);
231 CheckIfShipNeedsService(this);
233 CheckOrders(this);
235 if (this->running_ticks == 0) return;
237 CommandCost cost(EXPENSES_SHIP_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
239 this->profit_this_year -= cost.GetCost();
240 this->running_ticks = 0;
242 SubtractMoneyFromCompanyFract(this->owner, cost);
244 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
245 /* we need this for the profit */
246 SetWindowClassesDirty(WC_SHIPS_LIST);
249 Trackdir Ship::GetVehicleTrackdir() const
251 if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
253 if (this->IsInDepot()) {
254 /* We'll assume the ship is facing outwards */
255 return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
258 if (this->state == TRACK_BIT_WORMHOLE) {
259 /* ship on aqueduct, so just use his direction and assume a diagonal track */
260 return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
263 return TrackDirectionToTrackdir(FindFirstTrack(this->state), this->direction);
266 void Ship::MarkDirty()
268 this->colourmap = PAL_NONE;
269 this->UpdateViewport(true, false);
270 this->UpdateCache();
273 static void PlayShipSound(const Vehicle *v)
275 if (!PlayVehicleSound(v, VSE_START)) {
276 SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v);
280 void Ship::PlayLeaveStationSound() const
282 PlayShipSound(this);
285 TileIndex Ship::GetOrderStationLocation(StationID station)
287 if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
289 const Station *st = Station::Get(station);
290 if (st->dock_tile != INVALID_TILE) {
291 return TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
292 } else {
293 this->IncrementRealOrderIndex();
294 return 0;
298 void Ship::UpdateDeltaXY(Direction direction)
300 static const int8 _delta_xy_table[8][4] = {
301 /* y_extent, x_extent, y_offs, x_offs */
302 { 6, 6, -3, -3}, // N
303 { 6, 32, -3, -16}, // NE
304 { 6, 6, -3, -3}, // E
305 {32, 6, -16, -3}, // SE
306 { 6, 6, -3, -3}, // S
307 { 6, 32, -3, -16}, // SW
308 { 6, 6, -3, -3}, // W
309 {32, 6, -16, -3}, // NW
312 const int8 *bb = _delta_xy_table[direction];
313 this->x_offs = bb[3];
314 this->y_offs = bb[2];
315 this->x_extent = bb[1];
316 this->y_extent = bb[0];
317 this->z_extent = 6;
320 static const TileIndexDiffC _ship_leave_depot_offs[] = {
321 {-1, 0},
322 { 0, -1}
325 static bool CheckShipLeaveDepot(Ship *v)
327 if (!v->IsChainInDepot()) return false;
329 /* We are leaving a depot, but have to go to the exact same one; re-enter */
330 if (v->current_order.IsType(OT_GOTO_DEPOT) &&
331 IsShipDepotTile(v->tile) && GetDepotIndex(v->tile) == v->current_order.GetDestination()) {
332 VehicleEnterDepot(v);
333 return true;
336 TileIndex tile = v->tile;
337 Axis axis = GetShipDepotAxis(tile);
339 DiagDirection north_dir = ReverseDiagDir(AxisToDiagDir(axis));
340 TileIndex north_neighbour = TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[axis]));
341 DiagDirection south_dir = AxisToDiagDir(axis);
342 TileIndex south_neighbour = TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[axis]));
344 TrackBits north_tracks = DiagdirReachesTracks(north_dir) & GetTileShipTrackStatus(north_neighbour);
345 TrackBits south_tracks = DiagdirReachesTracks(south_dir) & GetTileShipTrackStatus(south_neighbour);
346 if (north_tracks && south_tracks) {
347 /* Ask pathfinder for best direction */
348 bool reverse = false;
349 bool path_found;
350 switch (_settings_game.pf.pathfinder_for_ships) {
351 case VPF_OPF: reverse = OPFShipChooseTrack(v, north_neighbour, north_dir, north_tracks, path_found) == INVALID_TRACK; break; // OPF always allows reversing
352 case VPF_NPF: reverse = NPFShipCheckReverse(v); break;
353 case VPF_YAPF: reverse = YapfShipCheckReverse(v); break;
354 default: NOT_REACHED();
356 if (reverse) north_tracks = TRACK_BIT_NONE;
359 if (north_tracks) {
360 /* Leave towards north */
361 v->direction = DiagDirToDir(north_dir);
362 } else if (south_tracks) {
363 /* Leave towards south */
364 v->direction = DiagDirToDir(south_dir);
365 } else {
366 /* Both ways blocked */
367 return false;
370 v->state = AxisToTrackBits(axis);
371 v->vehstatus &= ~VS_HIDDEN;
373 v->cur_speed = 0;
374 v->UpdateViewport(true, true);
375 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
377 PlayShipSound(v);
378 VehicleServiceInDepot(v);
379 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
380 SetWindowClassesDirty(WC_SHIPS_LIST);
382 return false;
385 static bool ShipAccelerate(Vehicle *v)
387 uint spd;
388 byte t;
390 spd = min(v->cur_speed + 1, v->vcache.cached_max_speed);
391 spd = min(spd, v->current_order.GetMaxSpeed() * 2);
393 /* updates statusbar only if speed have changed to save CPU time */
394 if (spd != v->cur_speed) {
395 v->cur_speed = spd;
396 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
399 /* Convert direction-independent speed into direction-dependent speed. (old movement method) */
400 spd = v->GetOldAdvanceSpeed(spd);
402 if (spd == 0) return false;
403 if ((byte)++spd == 0) return true;
405 v->progress = (t = v->progress) - (byte)spd;
407 return (t < v->progress);
411 * Ship arrives at a dock. If it is the first time, send out a news item.
412 * @param v Ship that arrived.
413 * @param st Station being visited.
415 static void ShipArrivesAt(const Vehicle *v, Station *st)
417 /* Check if station was ever visited before */
418 if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
419 st->had_vehicle_of_type |= HVOT_SHIP;
421 SetDParam(0, st->index);
422 AddVehicleNewsItem(
423 STR_NEWS_FIRST_SHIP_ARRIVAL,
424 (v->owner == _local_company) ? NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
425 v->index,
426 st->index
428 AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
434 * Runs the pathfinder to choose a track to continue along.
436 * @param v Ship to navigate
437 * @param tile Tile, the ship is about to enter
438 * @param enterdir Direction of entering
439 * @param tracks Available track choices on \a tile
440 * @return Track to choose, or INVALID_TRACK when to reverse.
442 static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
444 assert(IsValidDiagDirection(enterdir));
446 bool path_found = true;
447 Track track;
448 switch (_settings_game.pf.pathfinder_for_ships) {
449 case VPF_OPF: track = OPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
450 case VPF_NPF: track = NPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
451 case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
452 default: NOT_REACHED();
455 v->HandlePathfindingResult(path_found);
456 return track;
459 static const Direction _new_vehicle_direction_table[] = {
460 DIR_N , DIR_NW, DIR_W , INVALID_DIR,
461 DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
462 DIR_E , DIR_SE, DIR_S
465 static Direction ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile)
467 uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
468 TileX(new_tile) - TileX(old_tile) + 1;
469 assert(offs < 11 && offs != 3 && offs != 7);
470 return _new_vehicle_direction_table[offs];
473 static Direction ShipGetNewDirection(Vehicle *v, int x, int y)
475 uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
476 assert(offs < 11 && offs != 3 && offs != 7);
477 return _new_vehicle_direction_table[offs];
480 static inline TrackBits GetAvailShipTracks(TileIndex tile, DiagDirection dir)
482 return GetTileShipTrackStatus(tile) & DiagdirReachesTracks(dir);
485 static const byte _ship_subcoord[4][6][3] = {
487 {15, 8, 1},
488 { 0, 0, 0},
489 { 0, 0, 0},
490 {15, 8, 2},
491 {15, 7, 0},
492 { 0, 0, 0},
495 { 0, 0, 0},
496 { 8, 0, 3},
497 { 7, 0, 2},
498 { 0, 0, 0},
499 { 8, 0, 4},
500 { 0, 0, 0},
503 { 0, 8, 5},
504 { 0, 0, 0},
505 { 0, 7, 6},
506 { 0, 0, 0},
507 { 0, 0, 0},
508 { 0, 8, 4},
511 { 0, 0, 0},
512 { 8, 15, 7},
513 { 0, 0, 0},
514 { 8, 15, 6},
515 { 0, 0, 0},
516 { 7, 15, 0},
520 static void ShipController(Ship *v)
522 uint32 r;
523 const byte *b;
524 Direction dir;
525 Track track;
526 TrackBits tracks;
528 v->tick_counter++;
529 v->current_order_time++;
531 if (v->HandleBreakdown()) return;
533 if (v->vehstatus & VS_STOPPED) return;
535 ProcessOrders(v);
536 v->HandleLoading();
538 if (v->current_order.IsType(OT_LOADING)) return;
540 if (CheckShipLeaveDepot(v)) return;
542 v->ShowVisualEffect();
544 if (!ShipAccelerate(v)) return;
546 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
547 if (v->state != TRACK_BIT_WORMHOLE) {
548 /* Not on a bridge */
549 if (gp.old_tile == gp.new_tile) {
550 /* Staying in tile */
551 if (v->IsInDepot()) {
552 gp.x = v->x_pos;
553 gp.y = v->y_pos;
554 } else {
555 /* Not inside depot */
556 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
557 if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
559 /* A leave station order only needs one tick to get processed, so we can
560 * always skip ahead. */
561 if (v->current_order.IsType(OT_LEAVESTATION)) {
562 v->current_order.Free();
563 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
564 } else if (v->dest_tile != 0) {
565 /* We have a target, let's see if we reached it... */
566 if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
567 DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
568 /* We got within 3 tiles of our target buoy, so let's skip to our
569 * next order */
570 UpdateVehicleTimetable(v, true);
571 v->IncrementRealOrderIndex();
572 v->current_order.MakeDummy();
573 } else {
574 /* Non-buoy orders really need to reach the tile */
575 if (v->dest_tile == gp.new_tile) {
576 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
577 if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
578 VehicleEnterDepot(v);
579 return;
581 } else if (v->current_order.IsType(OT_GOTO_STATION)) {
582 v->last_station_visited = v->current_order.GetDestination();
584 /* Process station in the orderlist. */
585 Station *st = Station::Get(v->current_order.GetDestination());
586 if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
587 ShipArrivesAt(v, st);
588 v->BeginLoading();
589 } else { // leave stations without docks right aways
590 v->current_order.MakeLeaveStation();
591 v->IncrementRealOrderIndex();
598 } else {
599 /* New tile */
600 if (!IsValidTile(gp.new_tile)) goto reverse_direction;
602 dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
603 assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW);
604 DiagDirection diagdir = DirToDiagDir(dir);
605 tracks = GetAvailShipTracks(gp.new_tile, diagdir);
606 if (tracks == TRACK_BIT_NONE) goto reverse_direction;
608 /* Choose a direction, and continue if we find one */
609 track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
610 if (track == INVALID_TRACK) goto reverse_direction;
612 b = _ship_subcoord[diagdir][track];
614 gp.x = (gp.x & ~0xF) | b[0];
615 gp.y = (gp.y & ~0xF) | b[1];
617 /* Call the landscape function and tell it that the vehicle entered the tile */
618 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
619 if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
621 if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
622 v->tile = gp.new_tile;
623 v->state = TrackToTrackBits(track);
625 /* Update ship cache when the water class changes. Aqueducts are always canals. */
626 WaterClass old_wc = GetEffectiveWaterClass(gp.old_tile);
627 WaterClass new_wc = GetEffectiveWaterClass(gp.new_tile);
628 if (old_wc != new_wc) v->UpdateCache();
631 v->direction = (Direction)b[2];
633 } else {
634 /* On a bridge */
635 if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
636 v->x_pos = gp.x;
637 v->y_pos = gp.y;
638 v->UpdatePosition();
639 if ((v->vehstatus & VS_HIDDEN) == 0) v->Vehicle::UpdateViewport(true);
640 return;
644 /* update image of ship, as well as delta XY */
645 dir = ShipGetNewDirection(v, gp.x, gp.y);
646 v->x_pos = gp.x;
647 v->y_pos = gp.y;
648 v->z_pos = GetSlopePixelZ(gp.x, gp.y);
650 getout:
651 v->UpdatePosition();
652 v->UpdateViewport(true, true);
653 return;
655 reverse_direction:
656 dir = ReverseDir(v->direction);
657 v->direction = dir;
658 goto getout;
661 bool Ship::Tick()
663 if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
665 ShipController(this);
667 return true;
671 * Build a ship.
672 * @param tile tile of the depot where ship is built.
673 * @param flags type of operation.
674 * @param e the engine to build.
675 * @param data unused.
676 * @param ret[out] the vehicle that has been built.
677 * @return the cost of this operation or an error.
679 CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **ret)
681 tile = GetShipDepotNorthTile(tile);
682 if (flags & DC_EXEC) {
683 int x;
684 int y;
686 const ShipVehicleInfo *svi = &e->u.ship;
688 Ship *v = new Ship();
689 *ret = v;
691 v->owner = _current_company;
692 v->tile = tile;
693 x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
694 y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
695 v->x_pos = x;
696 v->y_pos = y;
697 v->z_pos = GetSlopePixelZ(x, y);
699 v->UpdateDeltaXY(v->direction);
700 v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
702 v->spritenum = svi->image_index;
703 v->cargo_type = e->GetDefaultCargoType();
704 v->cargo_cap = svi->capacity;
705 v->refit_cap = 0;
707 v->last_station_visited = INVALID_STATION;
708 v->last_loading_station = INVALID_STATION;
709 v->engine_type = e->index;
711 v->reliability = e->reliability;
712 v->reliability_spd_dec = e->reliability_spd_dec;
713 v->max_age = e->GetLifeLengthInDays();
714 _new_vehicle_id = v->index;
716 v->state = TRACK_BIT_DEPOT;
718 v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_ships);
719 v->date_of_last_service = _date;
720 v->build_year = _cur_year;
721 v->sprite_seq.Set(SPR_IMG_QUERY);
722 v->random_bits = VehicleRandomBits();
724 v->UpdateCache();
726 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
727 v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
729 v->InvalidateNewGRFCacheOfChain();
731 v->cargo_cap = e->DetermineCapacity(v);
733 v->InvalidateNewGRFCacheOfChain();
735 v->UpdatePosition();
738 return CommandCost();
741 bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
743 const Depot *depot = FindClosestShipDepot(this, 0);
745 if (depot == NULL) return false;
747 if (location != NULL) *location = depot->xy;
748 if (destination != NULL) *destination = depot->index;
750 return true;