ship_cmd.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "ship.h"
00014 #include "landscape.h"
00015 #include "timetable.h"
00016 #include "news_func.h"
00017 #include "company_func.h"
00018 #include "pathfinder/npf/npf_func.h"
00019 #include "depot_base.h"
00020 #include "station_base.h"
00021 #include "newgrf_engine.h"
00022 #include "pathfinder/yapf/yapf.h"
00023 #include "newgrf_sound.h"
00024 #include "spritecache.h"
00025 #include "strings_func.h"
00026 #include "window_func.h"
00027 #include "date_func.h"
00028 #include "vehicle_func.h"
00029 #include "sound_func.h"
00030 #include "ai/ai.hpp"
00031 #include "pathfinder/opf/opf_ship.h"
00032 #include "engine_base.h"
00033 #include "company_base.h"
00034 #include "tunnelbridge_map.h"
00035 #include "zoom_func.h"
00036 
00037 #include "table/strings.h"
00038 
00044 WaterClass GetEffectiveWaterClass(TileIndex tile)
00045 {
00046   if (HasTileWaterClass(tile)) return GetWaterClass(tile);
00047   if (IsTileType(tile, MP_TUNNELBRIDGE)) {
00048     assert(GetTunnelBridgeTransportType(tile) == TRANSPORT_WATER);
00049     return WATER_CLASS_CANAL;
00050   }
00051   if (IsTileType(tile, MP_RAILWAY)) {
00052     assert(GetRailGroundType(tile) == RAIL_GROUND_WATER);
00053     return WATER_CLASS_SEA;
00054   }
00055   NOT_REACHED();
00056 }
00057 
00058 static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
00059 
00060 static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
00061 {
00062   return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
00063 }
00064 
00065 static SpriteID GetShipIcon(EngineID engine, EngineImageType image_type)
00066 {
00067   const Engine *e = Engine::Get(engine);
00068   uint8 spritenum = e->u.ship.image_index;
00069 
00070   if (is_custom_sprite(spritenum)) {
00071     SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W, image_type);
00072     if (sprite != 0) return sprite;
00073 
00074     spritenum = e->original_image_index;
00075   }
00076 
00077   return DIR_W + _ship_sprites[spritenum];
00078 }
00079 
00080 void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
00081 {
00082   SpriteID sprite = GetShipIcon(engine, image_type);
00083   const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
00084   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));
00085   DrawSprite(sprite, pal, preferred_x, y);
00086 }
00087 
00094 void GetShipSpriteSize(EngineID engine, uint &width, uint &height, EngineImageType image_type)
00095 {
00096   const Sprite *spr = GetSprite(GetShipIcon(engine, image_type), ST_NORMAL);
00097 
00098   width  = UnScaleByZoom(spr->width, ZOOM_LVL_GUI);
00099   height = UnScaleByZoom(spr->height, ZOOM_LVL_GUI);
00100 }
00101 
00102 SpriteID Ship::GetImage(Direction direction, EngineImageType image_type) const
00103 {
00104   uint8 spritenum = this->spritenum;
00105 
00106   if (is_custom_sprite(spritenum)) {
00107     SpriteID sprite = GetCustomVehicleSprite(this, direction, image_type);
00108     if (sprite != 0) return sprite;
00109 
00110     spritenum = this->GetEngine()->original_image_index;
00111   }
00112 
00113   return _ship_sprites[spritenum] + direction;
00114 }
00115 
00116 static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
00117 {
00118   /* Find the closest depot */
00119   const Depot *depot;
00120   const Depot *best_depot = NULL;
00121   /* If we don't have a maximum distance, i.e. distance = 0,
00122    * we want to find any depot so the best distance of no
00123    * depot must be more than any correct distance. On the
00124    * other hand if we have set a maximum distance, any depot
00125    * further away than max_distance can safely be ignored. */
00126   uint best_dist = max_distance == 0 ? UINT_MAX : max_distance + 1;
00127 
00128   FOR_ALL_DEPOTS(depot) {
00129     TileIndex tile = depot->xy;
00130     if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
00131       uint dist = DistanceManhattan(tile, v->tile);
00132       if (dist < best_dist) {
00133         best_dist = dist;
00134         best_depot = depot;
00135       }
00136     }
00137   }
00138 
00139   return best_depot;
00140 }
00141 
00142 static void CheckIfShipNeedsService(Vehicle *v)
00143 {
00144   if (Company::Get(v->owner)->settings.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
00145   if (v->IsInDepot()) {
00146     VehicleServiceInDepot(v);
00147     return;
00148   }
00149 
00150   uint max_distance;
00151   switch (_settings_game.pf.pathfinder_for_ships) {
00152     case VPF_OPF:  max_distance = 12; break;
00153     case VPF_NPF:  max_distance = _settings_game.pf.npf.maximum_go_to_depot_penalty  / NPF_TILE_LENGTH;  break;
00154     case VPF_YAPF: max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; break;
00155     default: NOT_REACHED();
00156   }
00157 
00158   const Depot *depot = FindClosestShipDepot(v, max_distance);
00159 
00160   if (depot == NULL) {
00161     if (v->current_order.IsType(OT_GOTO_DEPOT)) {
00162       v->current_order.MakeDummy();
00163       SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
00164     }
00165     return;
00166   }
00167 
00168   v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
00169   v->dest_tile = depot->xy;
00170   SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
00171 }
00172 
00176 void Ship::UpdateCache()
00177 {
00178   const ShipVehicleInfo *svi = ShipVehInfo(this->engine_type);
00179 
00180   /* Get speed fraction for the current water type. Aqueducts are always canals. */
00181   bool is_ocean = GetEffectiveWaterClass(this->tile) == WATER_CLASS_SEA;
00182   uint raw_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed);
00183   this->vcache.cached_max_speed = svi->ApplyWaterClassSpeedFrac(raw_speed, is_ocean);
00184 
00185   /* Update cargo aging period. */
00186   this->vcache.cached_cargo_age_period = GetVehicleProperty(this, PROP_SHIP_CARGO_AGE_PERIOD, EngInfo(this->engine_type)->cargo_age_period);
00187 
00188   this->UpdateVisualEffect();
00189 }
00190 
00191 Money Ship::GetRunningCost() const
00192 {
00193   const Engine *e = this->GetEngine();
00194   uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
00195   return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
00196 }
00197 
00198 void Ship::OnNewDay()
00199 {
00200   if ((++this->day_counter & 7) == 0) {
00201     DecreaseVehicleValue(this);
00202   }
00203 
00204   CheckVehicleBreakdown(this);
00205   AgeVehicle(this);
00206   CheckIfShipNeedsService(this);
00207 
00208   CheckOrders(this);
00209 
00210   if (this->running_ticks == 0) return;
00211 
00212   CommandCost cost(EXPENSES_SHIP_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
00213 
00214   this->profit_this_year -= cost.GetCost();
00215   this->running_ticks = 0;
00216 
00217   SubtractMoneyFromCompanyFract(this->owner, cost);
00218 
00219   SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
00220   /* we need this for the profit */
00221   SetWindowClassesDirty(WC_SHIPS_LIST);
00222 }
00223 
00224 Trackdir Ship::GetVehicleTrackdir() const
00225 {
00226   if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
00227 
00228   if (this->IsInDepot()) {
00229     /* We'll assume the ship is facing outwards */
00230     return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
00231   }
00232 
00233   if (this->state == TRACK_BIT_WORMHOLE) {
00234     /* ship on aqueduct, so just use his direction and assume a diagonal track */
00235     return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
00236   }
00237 
00238   return TrackDirectionToTrackdir(FindFirstTrack(this->state), this->direction);
00239 }
00240 
00241 void Ship::MarkDirty()
00242 {
00243   this->UpdateViewport(false, false);
00244   this->UpdateCache();
00245 }
00246 
00247 static void PlayShipSound(const Vehicle *v)
00248 {
00249   if (!PlayVehicleSound(v, VSE_START)) {
00250     SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v);
00251   }
00252 }
00253 
00254 void Ship::PlayLeaveStationSound() const
00255 {
00256   PlayShipSound(this);
00257 }
00258 
00259 TileIndex Ship::GetOrderStationLocation(StationID station)
00260 {
00261   if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
00262 
00263   const Station *st = Station::Get(station);
00264   if (st->dock_tile != INVALID_TILE) {
00265     return TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
00266   } else {
00267     this->IncrementRealOrderIndex();
00268     return 0;
00269   }
00270 }
00271 
00272 void Ship::UpdateDeltaXY(Direction direction)
00273 {
00274   static const int8 _delta_xy_table[8][4] = {
00275     /* y_extent, x_extent, y_offs, x_offs */
00276     { 6,  6,  -3,  -3}, // N
00277     { 6, 32,  -3, -16}, // NE
00278     { 6,  6,  -3,  -3}, // E
00279     {32,  6, -16,  -3}, // SE
00280     { 6,  6,  -3,  -3}, // S
00281     { 6, 32,  -3, -16}, // SW
00282     { 6,  6,  -3,  -3}, // W
00283     {32,  6, -16,  -3}, // NW
00284   };
00285 
00286   const int8 *bb = _delta_xy_table[direction];
00287   this->x_offs        = bb[3];
00288   this->y_offs        = bb[2];
00289   this->x_extent      = bb[1];
00290   this->y_extent      = bb[0];
00291   this->z_extent      = 6;
00292 }
00293 
00294 static const TileIndexDiffC _ship_leave_depot_offs[] = {
00295   {-1,  0},
00296   { 0, -1}
00297 };
00298 
00299 static bool CheckShipLeaveDepot(Ship *v)
00300 {
00301   if (!v->IsInDepot()) return false;
00302 
00303   /* We are leaving a depot, but have to go to the exact same one; re-enter */
00304   if (v->current_order.IsType(OT_GOTO_DEPOT) &&
00305       IsShipDepotTile(v->tile) && GetDepotIndex(v->tile) == v->current_order.GetDestination()) {
00306     VehicleEnterDepot(v);
00307     return true;
00308   }
00309 
00310   TileIndex tile = v->tile;
00311   Axis axis = GetShipDepotAxis(tile);
00312 
00313   /* Check first (north) side */
00314   if (DiagdirReachesTracks((DiagDirection)axis) & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
00315     v->direction = ReverseDir(AxisToDirection(axis));
00316   /* Check second (south) side */
00317   } else if (DiagdirReachesTracks((DiagDirection)(axis + 2)) & GetTileShipTrackStatus(TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
00318     v->direction = AxisToDirection(axis);
00319   } else {
00320     return false;
00321   }
00322 
00323   v->state = AxisToTrackBits(axis);
00324   v->vehstatus &= ~VS_HIDDEN;
00325 
00326   v->cur_speed = 0;
00327   v->UpdateViewport(true, true);
00328   SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
00329 
00330   PlayShipSound(v);
00331   VehicleServiceInDepot(v);
00332   InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
00333   SetWindowClassesDirty(WC_SHIPS_LIST);
00334 
00335   return false;
00336 }
00337 
00338 static bool ShipAccelerate(Vehicle *v)
00339 {
00340   uint spd;
00341   byte t;
00342 
00343   spd = min(v->cur_speed + 1, v->vcache.cached_max_speed);
00344   spd = min(spd, v->current_order.max_speed * 2);
00345 
00346   /* updates statusbar only if speed have changed to save CPU time */
00347   if (spd != v->cur_speed) {
00348     v->cur_speed = spd;
00349     SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
00350   }
00351 
00352   /* Convert direction-indepenent speed into direction-dependent speed. (old movement method) */
00353   spd = v->GetOldAdvanceSpeed(spd);
00354 
00355   if (spd == 0) return false;
00356   if ((byte)++spd == 0) return true;
00357 
00358   v->progress = (t = v->progress) - (byte)spd;
00359 
00360   return (t < v->progress);
00361 }
00362 
00368 static void ShipArrivesAt(const Vehicle *v, Station *st)
00369 {
00370   /* Check if station was ever visited before */
00371   if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
00372     st->had_vehicle_of_type |= HVOT_SHIP;
00373 
00374     SetDParam(0, st->index);
00375     AddVehicleNewsItem(
00376       STR_NEWS_FIRST_SHIP_ARRIVAL,
00377       (v->owner == _local_company) ? NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
00378       v->index,
00379       st->index
00380     );
00381     AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
00382   }
00383 }
00384 
00385 
00391 static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
00392 {
00393   assert(IsValidDiagDirection(enterdir));
00394 
00395   bool path_found = true;
00396   Track track;
00397   switch (_settings_game.pf.pathfinder_for_ships) {
00398     case VPF_OPF: track = OPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
00399     case VPF_NPF: track = NPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
00400     case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
00401     default: NOT_REACHED();
00402   }
00403 
00404   v->HandlePathfindingResult(path_found);
00405   return track;
00406 }
00407 
00408 static const Direction _new_vehicle_direction_table[] = {
00409   DIR_N , DIR_NW, DIR_W , INVALID_DIR,
00410   DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
00411   DIR_E , DIR_SE, DIR_S
00412 };
00413 
00414 static Direction ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile)
00415 {
00416   uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
00417               TileX(new_tile) - TileX(old_tile) + 1;
00418   assert(offs < 11 && offs != 3 && offs != 7);
00419   return _new_vehicle_direction_table[offs];
00420 }
00421 
00422 static Direction ShipGetNewDirection(Vehicle *v, int x, int y)
00423 {
00424   uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
00425   assert(offs < 11 && offs != 3 && offs != 7);
00426   return _new_vehicle_direction_table[offs];
00427 }
00428 
00429 static inline TrackBits GetAvailShipTracks(TileIndex tile, DiagDirection dir)
00430 {
00431   return GetTileShipTrackStatus(tile) & DiagdirReachesTracks(dir);
00432 }
00433 
00434 static const byte _ship_subcoord[4][6][3] = {
00435   {
00436     {15, 8, 1},
00437     { 0, 0, 0},
00438     { 0, 0, 0},
00439     {15, 8, 2},
00440     {15, 7, 0},
00441     { 0, 0, 0},
00442   },
00443   {
00444     { 0, 0, 0},
00445     { 8, 0, 3},
00446     { 7, 0, 2},
00447     { 0, 0, 0},
00448     { 8, 0, 4},
00449     { 0, 0, 0},
00450   },
00451   {
00452     { 0, 8, 5},
00453     { 0, 0, 0},
00454     { 0, 7, 6},
00455     { 0, 0, 0},
00456     { 0, 0, 0},
00457     { 0, 8, 4},
00458   },
00459   {
00460     { 0, 0, 0},
00461     { 8, 15, 7},
00462     { 0, 0, 0},
00463     { 8, 15, 6},
00464     { 0, 0, 0},
00465     { 7, 15, 0},
00466   }
00467 };
00468 
00469 static void ShipController(Ship *v)
00470 {
00471   uint32 r;
00472   const byte *b;
00473   Direction dir;
00474   Track track;
00475   TrackBits tracks;
00476 
00477   v->tick_counter++;
00478   v->current_order_time++;
00479 
00480   if (v->HandleBreakdown()) return;
00481 
00482   if (v->vehstatus & VS_STOPPED) return;
00483 
00484   ProcessOrders(v);
00485   v->HandleLoading();
00486 
00487   if (v->current_order.IsType(OT_LOADING)) return;
00488 
00489   if (CheckShipLeaveDepot(v)) return;
00490 
00491   v->ShowVisualEffect();
00492 
00493   if (!ShipAccelerate(v)) return;
00494 
00495   GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00496   if (v->state != TRACK_BIT_WORMHOLE) {
00497     /* Not on a bridge */
00498     if (gp.old_tile == gp.new_tile) {
00499       /* Staying in tile */
00500       if (v->IsInDepot()) {
00501         gp.x = v->x_pos;
00502         gp.y = v->y_pos;
00503       } else {
00504         /* Not inside depot */
00505         r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
00506         if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
00507 
00508         /* A leave station order only needs one tick to get processed, so we can
00509          * always skip ahead. */
00510         if (v->current_order.IsType(OT_LEAVESTATION)) {
00511           v->current_order.Free();
00512           SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
00513         } else if (v->dest_tile != 0) {
00514           /* We have a target, let's see if we reached it... */
00515           if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
00516               DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
00517             /* We got within 3 tiles of our target buoy, so let's skip to our
00518              * next order */
00519             UpdateVehicleTimetable(v, true);
00520             v->IncrementRealOrderIndex();
00521             v->current_order.MakeDummy();
00522           } else {
00523             /* Non-buoy orders really need to reach the tile */
00524             if (v->dest_tile == gp.new_tile) {
00525               if (v->current_order.IsType(OT_GOTO_DEPOT)) {
00526                 if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
00527                   VehicleEnterDepot(v);
00528                   return;
00529                 }
00530               } else if (v->current_order.IsType(OT_GOTO_STATION)) {
00531                 v->last_station_visited = v->current_order.GetDestination();
00532 
00533                 /* Process station in the orderlist. */
00534                 Station *st = Station::Get(v->current_order.GetDestination());
00535                 if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
00536                   ShipArrivesAt(v, st);
00537                   v->BeginLoading();
00538                 } else { // leave stations without docks right aways
00539                   v->current_order.MakeLeaveStation();
00540                   v->IncrementRealOrderIndex();
00541                 }
00542               }
00543             }
00544           }
00545         }
00546       }
00547     } else {
00548       DiagDirection diagdir;
00549       /* New tile */
00550       if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) {
00551         goto reverse_direction;
00552       }
00553 
00554       dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
00555       assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW);
00556       diagdir = DirToDiagDir(dir);
00557       tracks = GetAvailShipTracks(gp.new_tile, diagdir);
00558       if (tracks == TRACK_BIT_NONE) goto reverse_direction;
00559 
00560       /* Choose a direction, and continue if we find one */
00561       track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
00562       if (track == INVALID_TRACK) goto reverse_direction;
00563 
00564       b = _ship_subcoord[diagdir][track];
00565 
00566       gp.x = (gp.x & ~0xF) | b[0];
00567       gp.y = (gp.y & ~0xF) | b[1];
00568 
00569       /* Call the landscape function and tell it that the vehicle entered the tile */
00570       r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
00571       if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
00572 
00573       if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
00574         v->tile = gp.new_tile;
00575         v->state = TrackToTrackBits(track);
00576 
00577         /* Update ship cache when the water class changes. Aqueducts are always canals. */
00578         WaterClass old_wc = GetEffectiveWaterClass(gp.old_tile);
00579         WaterClass new_wc = GetEffectiveWaterClass(gp.new_tile);
00580         if (old_wc != new_wc) v->UpdateCache();
00581       }
00582 
00583       v->direction = (Direction)b[2];
00584     }
00585   } else {
00586     /* On a bridge */
00587     if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
00588       v->x_pos = gp.x;
00589       v->y_pos = gp.y;
00590       VehicleUpdatePosition(v);
00591       if ((v->vehstatus & VS_HIDDEN) == 0) VehicleUpdateViewport(v, true);
00592       return;
00593     }
00594   }
00595 
00596   /* update image of ship, as well as delta XY */
00597   dir = ShipGetNewDirection(v, gp.x, gp.y);
00598   v->x_pos = gp.x;
00599   v->y_pos = gp.y;
00600   v->z_pos = GetSlopePixelZ(gp.x, gp.y);
00601 
00602 getout:
00603   VehicleUpdatePosition(v);
00604   v->UpdateViewport(true, true);
00605   return;
00606 
00607 reverse_direction:
00608   dir = ReverseDir(v->direction);
00609   v->direction = dir;
00610   goto getout;
00611 }
00612 
00613 bool Ship::Tick()
00614 {
00615   if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
00616 
00617   ShipController(this);
00618 
00619   return true;
00620 }
00621 
00631 CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **ret)
00632 {
00633   tile = GetShipDepotNorthTile(tile);
00634   if (flags & DC_EXEC) {
00635     int x;
00636     int y;
00637 
00638     const ShipVehicleInfo *svi = &e->u.ship;
00639 
00640     Ship *v = new Ship();
00641     *ret = v;
00642 
00643     v->owner = _current_company;
00644     v->tile = tile;
00645     x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
00646     y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
00647     v->x_pos = x;
00648     v->y_pos = y;
00649     v->z_pos = GetSlopePixelZ(x, y);
00650 
00651     v->UpdateDeltaXY(v->direction);
00652     v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
00653 
00654     v->spritenum = svi->image_index;
00655     v->cargo_type = e->GetDefaultCargoType();
00656     v->cargo_cap = svi->capacity;
00657 
00658     v->last_station_visited = INVALID_STATION;
00659     v->last_loading_station = INVALID_STATION;
00660     v->engine_type = e->index;
00661 
00662     v->reliability = e->reliability;
00663     v->reliability_spd_dec = e->reliability_spd_dec;
00664     v->max_age = e->GetLifeLengthInDays();
00665     _new_vehicle_id = v->index;
00666 
00667     v->state = TRACK_BIT_DEPOT;
00668 
00669     v->service_interval = Company::Get(_current_company)->settings.vehicle.servint_ships;
00670     v->date_of_last_service = _date;
00671     v->build_year = _cur_year;
00672     v->cur_image = SPR_IMG_QUERY;
00673     v->random_bits = VehicleRandomBits();
00674 
00675     v->UpdateCache();
00676 
00677     if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
00678 
00679     v->InvalidateNewGRFCacheOfChain();
00680 
00681     v->cargo_cap = e->DetermineCapacity(v);
00682 
00683     v->InvalidateNewGRFCacheOfChain();
00684 
00685     VehicleUpdatePosition(v);
00686   }
00687 
00688   return CommandCost();
00689 }
00690 
00691 bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
00692 {
00693   const Depot *depot = FindClosestShipDepot(this, 0);
00694 
00695   if (depot == NULL) return false;
00696 
00697   if (location    != NULL) *location    = depot->xy;
00698   if (destination != NULL) *destination = depot->index;
00699 
00700   return true;
00701 }