00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "ship.h"
00014 #include "landscape.h"
00015 #include "timetable.h"
00016 #include "command_func.h"
00017 #include "news_func.h"
00018 #include "company_func.h"
00019 #include "pathfinder/npf/npf_func.h"
00020 #include "depot_base.h"
00021 #include "station_base.h"
00022 #include "vehicle_gui.h"
00023 #include "newgrf_engine.h"
00024 #include "pathfinder/yapf/yapf.h"
00025 #include "newgrf_sound.h"
00026 #include "spritecache.h"
00027 #include "strings_func.h"
00028 #include "functions.h"
00029 #include "window_func.h"
00030 #include "date_func.h"
00031 #include "vehicle_func.h"
00032 #include "sound_func.h"
00033 #include "autoreplace_gui.h"
00034 #include "gfx_func.h"
00035 #include "effectvehicle_func.h"
00036 #include "ai/ai.hpp"
00037 #include "pathfinder/opf/opf_ship.h"
00038 #include "landscape_type.h"
00039 #include "infrastructure_func.h"
00040
00041 #include "table/strings.h"
00042 #include "table/sprites.h"
00043
00044 static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
00045
00046 static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
00047 {
00048 return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
00049 }
00050
00051 static SpriteID GetShipIcon(EngineID engine)
00052 {
00053 const Engine *e = Engine::Get(engine);
00054 uint8 spritenum = e->u.ship.image_index;
00055
00056 if (is_custom_sprite(spritenum)) {
00057 SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
00058 if (sprite != 0) return sprite;
00059
00060 spritenum = e->original_image_index;
00061 }
00062
00063 return DIR_W + _ship_sprites[spritenum];
00064 }
00065
00066 void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, SpriteID pal)
00067 {
00068 SpriteID sprite = GetShipIcon(engine);
00069 const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
00070 preferred_x = Clamp(preferred_x, left - real_sprite->x_offs, right - real_sprite->width - real_sprite->x_offs);
00071 DrawSprite(sprite, pal, preferred_x, y);
00072 }
00073
00079 void GetShipSpriteSize(EngineID engine, uint &width, uint &height)
00080 {
00081 const Sprite *spr = GetSprite(GetShipIcon(engine), ST_NORMAL);
00082
00083 width = spr->width;
00084 height = spr->height;
00085 }
00086
00087 SpriteID Ship::GetImage(Direction direction) const
00088 {
00089 uint8 spritenum = this->spritenum;
00090
00091 if (is_custom_sprite(spritenum)) {
00092 SpriteID sprite = GetCustomVehicleSprite(this, direction);
00093 if (sprite != 0) return sprite;
00094
00095 spritenum = Engine::Get(this->engine_type)->original_image_index;
00096 }
00097
00098 return _ship_sprites[spritenum] + direction;
00099 }
00100
00101 static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
00102 {
00103
00104 const Depot *depot;
00105 const Depot *best_depot = NULL;
00106
00107
00108
00109
00110
00111 uint best_dist = max_distance == 0 ? UINT_MAX : max_distance + 1;
00112
00113 FOR_ALL_DEPOTS(depot) {
00114 TileIndex tile = depot->xy;
00115 if (IsShipDepotTile(tile) && IsInfraTileUsageAllowed(tile, v->owner, VEH_SHIP)) {
00116 uint dist = DistanceManhattan(tile, v->tile);
00117 if (dist < best_dist) {
00118 best_dist = dist;
00119 best_depot = depot;
00120 }
00121 }
00122 }
00123
00124 return best_depot;
00125 }
00126
00127 static void CheckIfShipNeedsService(Vehicle *v)
00128 {
00129 if (Company::Get(v->owner)->settings.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
00130 if (v->IsInDepot()) {
00131 VehicleServiceInDepot(v);
00132 return;
00133 }
00134
00135 uint max_distance;
00136 switch (_settings_game.pf.pathfinder_for_ships) {
00137 case VPF_OPF: max_distance = 12; break;
00138 case VPF_NPF: max_distance = _settings_game.pf.npf.maximum_go_to_depot_penalty / NPF_TILE_LENGTH; break;
00139 case VPF_YAPF: max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; break;
00140 default: NOT_REACHED();
00141 }
00142
00143 const Depot *depot = FindClosestShipDepot(v, max_distance);
00144
00145 if (depot == NULL) {
00146 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
00147 v->current_order.MakeDummy();
00148 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00149 }
00150 return;
00151 }
00152
00153 v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
00154 v->dest_tile = depot->xy;
00155 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00156 }
00157
00158 Money Ship::GetRunningCost() const
00159 {
00160 const Engine *e = Engine::Get(this->engine_type);
00161 uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
00162 return GetPrice(PR_RUNNING_SHIP, cost_factor, e->grffile);
00163 }
00164
00165 void Ship::OnNewDay()
00166 {
00167 if ((++this->day_counter & 7) == 0)
00168 DecreaseVehicleValue(this);
00169
00170 CheckVehicleBreakdown(this);
00171 AgeVehicle(this);
00172 CheckIfShipNeedsService(this);
00173
00174 CheckOrders(this);
00175
00176 if (this->running_ticks == 0) return;
00177
00178 CommandCost cost(EXPENSES_SHIP_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
00179
00180 this->profit_this_year -= cost.GetCost();
00181 this->running_ticks = 0;
00182
00183 SubtractMoneyFromCompanyFract(this->owner, cost);
00184
00185 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
00186
00187 SetWindowClassesDirty(WC_SHIPS_LIST);
00188 }
00189
00190 Trackdir Ship::GetVehicleTrackdir() const
00191 {
00192 if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
00193
00194 if (this->IsInDepot()) {
00195
00196 return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
00197 }
00198
00199 if (this->state == TRACK_BIT_WORMHOLE) {
00200
00201 return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
00202 }
00203
00204 return TrackDirectionToTrackdir(FindFirstTrack(this->state), this->direction);
00205 }
00206
00207 static void HandleBrokenShip(Vehicle *v)
00208 {
00209 if (v->breakdown_ctr != 1) {
00210 v->breakdown_ctr = 1;
00211 v->cur_speed = 0;
00212
00213 if (v->breakdowns_since_last_service != 255)
00214 v->breakdowns_since_last_service++;
00215
00216 v->MarkDirty();
00217 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
00218 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
00219
00220 if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
00221 SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ?
00222 SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v);
00223 }
00224
00225 if (!(v->vehstatus & VS_HIDDEN)) {
00226 EffectVehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
00227 if (u != NULL) u->animation_state = v->breakdown_delay * 2;
00228 }
00229 }
00230
00231 if (!(v->tick_counter & 1)) {
00232 if (!--v->breakdown_delay) {
00233 v->breakdown_ctr = 0;
00234 v->MarkDirty();
00235 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
00236 }
00237 }
00238 }
00239
00240 void Ship::MarkDirty()
00241 {
00242 this->UpdateViewport(false, false);
00243 }
00244
00245 static void PlayShipSound(const Vehicle *v)
00246 {
00247 if (!PlayVehicleSound(v, VSE_START)) {
00248 SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v);
00249 }
00250 }
00251
00252 void Ship::PlayLeaveStationSound() const
00253 {
00254 PlayShipSound(this);
00255 }
00256
00257 TileIndex Ship::GetOrderStationLocation(StationID station)
00258 {
00259 if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
00260
00261 const Station *st = Station::Get(station);
00262 if (st->dock_tile != INVALID_TILE) {
00263 return TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
00264 } else {
00265 this->IncrementOrderIndex();
00266 return 0;
00267 }
00268 }
00269
00270 void Ship::UpdateDeltaXY(Direction direction)
00271 {
00272 #define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
00273 static const uint32 _delta_xy_table[8] = {
00274 MKIT( 6, 6, -3, -3),
00275 MKIT( 6, 32, -3, -16),
00276 MKIT( 6, 6, -3, -3),
00277 MKIT(32, 6, -16, -3),
00278 MKIT( 6, 6, -3, -3),
00279 MKIT( 6, 32, -3, -16),
00280 MKIT( 6, 6, -3, -3),
00281 MKIT(32, 6, -16, -3),
00282 };
00283 #undef MKIT
00284
00285 uint32 x = _delta_xy_table[direction];
00286 this->x_offs = GB(x, 0, 8);
00287 this->y_offs = GB(x, 8, 8);
00288 this->x_extent = GB(x, 16, 8);
00289 this->y_extent = GB(x, 24, 8);
00290 this->z_extent = 6;
00291 }
00292
00293 void RecalcShipStuff(Vehicle *v)
00294 {
00295 v->UpdateViewport(false, true);
00296 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
00297 }
00298
00299 static const TileIndexDiffC _ship_leave_depot_offs[] = {
00300 {-1, 0},
00301 { 0, -1}
00302 };
00303
00304 static void CheckShipLeaveDepot(Ship *v)
00305 {
00306 if (!v->IsInDepot()) return;
00307
00308 TileIndex tile = v->tile;
00309 Axis axis = GetShipDepotAxis(tile);
00310
00311
00312 if (DiagdirReachesTracks((DiagDirection)axis) & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
00313 v->direction = ReverseDir(AxisToDirection(axis));
00314
00315 } else if (DiagdirReachesTracks((DiagDirection)(axis + 2)) & GetTileShipTrackStatus(TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
00316 v->direction = AxisToDirection(axis);
00317 } else {
00318 return;
00319 }
00320
00321 v->state = AxisToTrackBits(axis);
00322 v->vehstatus &= ~VS_HIDDEN;
00323
00324 v->cur_speed = 0;
00325 RecalcShipStuff(v);
00326
00327 PlayShipSound(v);
00328 VehicleServiceInDepot(v);
00329 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
00330 SetWindowClassesDirty(WC_SHIPS_LIST);
00331 }
00332
00333 static bool ShipAccelerate(Vehicle *v)
00334 {
00335 uint spd;
00336 byte t;
00337
00338 spd = min(v->cur_speed + 1, GetVehicleProperty(v, PROP_SHIP_SPEED, v->max_speed));
00339
00340
00341 if (spd != v->cur_speed) {
00342 v->cur_speed = spd;
00343 if (_settings_client.gui.vehicle_speed)
00344 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00345 }
00346
00347
00348 if (!(v->direction & 1)) spd = spd * 3 / 4;
00349
00350 if (spd == 0) return false;
00351 if ((byte)++spd == 0) return true;
00352
00353 v->progress = (t = v->progress) - (byte)spd;
00354
00355 return (t < v->progress);
00356 }
00357
00358 static void ShipArrivesAt(const Vehicle *v, Station *st)
00359 {
00360
00361 if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
00362 st->had_vehicle_of_type |= HVOT_SHIP;
00363
00364 SetDParam(0, st->index);
00365 AddVehicleNewsItem(
00366 STR_NEWS_FIRST_SHIP_ARRIVAL,
00367 (v->owner == _local_company) ? NS_ARRIVAL_COMPANY : NS_ARRIVAL_OTHER,
00368 v->index,
00369 st->index
00370 );
00371 AI::NewEvent(v->owner, new AIEventStationFirstVehicle(st->index, v->index));
00372 }
00373 }
00374
00375
00379 static Track ChooseShipTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
00380 {
00381 assert(IsValidDiagDirection(enterdir));
00382
00383 switch (_settings_game.pf.pathfinder_for_ships) {
00384 case VPF_OPF: return OPFShipChooseTrack(v, tile, enterdir, tracks);
00385 case VPF_NPF: return NPFShipChooseTrack(v, tile, enterdir, tracks);
00386 case VPF_YAPF: return YapfShipChooseTrack(v, tile, enterdir, tracks);
00387 default: NOT_REACHED();
00388 }
00389 }
00390
00391 static const Direction _new_vehicle_direction_table[] = {
00392 DIR_N , DIR_NW, DIR_W , INVALID_DIR,
00393 DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
00394 DIR_E , DIR_SE, DIR_S
00395 };
00396
00397 static Direction ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile)
00398 {
00399 uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
00400 TileX(new_tile) - TileX(old_tile) + 1;
00401 assert(offs < 11 && offs != 3 && offs != 7);
00402 return _new_vehicle_direction_table[offs];
00403 }
00404
00405 static Direction ShipGetNewDirection(Vehicle *v, int x, int y)
00406 {
00407 uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
00408 assert(offs < 11 && offs != 3 && offs != 7);
00409 return _new_vehicle_direction_table[offs];
00410 }
00411
00412 static inline TrackBits GetAvailShipTracks(TileIndex tile, DiagDirection dir)
00413 {
00414 return GetTileShipTrackStatus(tile) & DiagdirReachesTracks(dir);
00415 }
00416
00417 static const byte _ship_subcoord[4][6][3] = {
00418 {
00419 {15, 8, 1},
00420 { 0, 0, 0},
00421 { 0, 0, 0},
00422 {15, 8, 2},
00423 {15, 7, 0},
00424 { 0, 0, 0},
00425 },
00426 {
00427 { 0, 0, 0},
00428 { 8, 0, 3},
00429 { 7, 0, 2},
00430 { 0, 0, 0},
00431 { 8, 0, 4},
00432 { 0, 0, 0},
00433 },
00434 {
00435 { 0, 8, 5},
00436 { 0, 0, 0},
00437 { 0, 7, 6},
00438 { 0, 0, 0},
00439 { 0, 0, 0},
00440 { 0, 8, 4},
00441 },
00442 {
00443 { 0, 0, 0},
00444 { 8, 15, 7},
00445 { 0, 0, 0},
00446 { 8, 15, 6},
00447 { 0, 0, 0},
00448 { 7, 15, 0},
00449 }
00450 };
00451
00452 static void ShipController(Ship *v)
00453 {
00454 uint32 r;
00455 const byte *b;
00456 Direction dir;
00457 Track track;
00458 TrackBits tracks;
00459
00460 v->tick_counter++;
00461 v->current_order_time++;
00462
00463 if (v->breakdown_ctr != 0) {
00464 if (v->breakdown_ctr <= 2) {
00465 HandleBrokenShip(v);
00466 return;
00467 }
00468 if (!v->current_order.IsType(OT_LOADING)) v->breakdown_ctr--;
00469 }
00470
00471 if (v->vehstatus & VS_STOPPED) return;
00472
00473 ProcessOrders(v);
00474 v->HandleLoading();
00475
00476 if (v->current_order.IsType(OT_LOADING)) return;
00477
00478 CheckShipLeaveDepot(v);
00479
00480 if (!ShipAccelerate(v)) return;
00481
00482 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00483 if (v->state != TRACK_BIT_WORMHOLE) {
00484
00485 if (gp.old_tile == gp.new_tile) {
00486
00487 if (v->IsInDepot()) {
00488 gp.x = v->x_pos;
00489 gp.y = v->y_pos;
00490 } else {
00491
00492 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
00493 if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
00494
00495
00496
00497 if (v->current_order.IsType(OT_LEAVESTATION)) {
00498 v->current_order.Free();
00499 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00500 } else if (v->dest_tile != 0) {
00501
00502 if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
00503 DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
00504
00505
00506 UpdateVehicleTimetable(v, true);
00507 v->IncrementOrderIndex();
00508 v->current_order.MakeDummy();
00509 } else {
00510
00511 if (v->dest_tile == gp.new_tile) {
00512 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
00513 if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
00514 VehicleEnterDepot(v);
00515 return;
00516 }
00517 } else if (v->current_order.IsType(OT_GOTO_STATION)) {
00518 v->last_station_visited = v->current_order.GetDestination();
00519
00520
00521 Station *st = Station::Get(v->current_order.GetDestination());
00522 if (st->facilities & FACIL_DOCK) {
00523 ShipArrivesAt(v, st);
00524 v->BeginLoading();
00525 } else {
00526 v->current_order.MakeLeaveStation();
00527 v->IncrementOrderIndex();
00528 }
00529 }
00530 }
00531 }
00532 }
00533 }
00534 } else {
00535 DiagDirection diagdir;
00536
00537 if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) {
00538 goto reverse_direction;
00539 }
00540
00541 dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
00542 assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW);
00543 diagdir = DirToDiagDir(dir);
00544 tracks = GetAvailShipTracks(gp.new_tile, diagdir);
00545 if (tracks == TRACK_BIT_NONE) goto reverse_direction;
00546
00547
00548 track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
00549 if (track == INVALID_TRACK) goto reverse_direction;
00550
00551 b = _ship_subcoord[diagdir][track];
00552
00553 gp.x = (gp.x & ~0xF) | b[0];
00554 gp.y = (gp.y & ~0xF) | b[1];
00555
00556
00557 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
00558 if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
00559
00560 if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
00561 v->tile = gp.new_tile;
00562 v->state = TrackToTrackBits(track);
00563 }
00564
00565 v->direction = (Direction)b[2];
00566 }
00567 } else {
00568
00569 if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
00570 v->x_pos = gp.x;
00571 v->y_pos = gp.y;
00572 VehicleMove(v, !(v->vehstatus & VS_HIDDEN));
00573 return;
00574 }
00575 }
00576
00577
00578 dir = ShipGetNewDirection(v, gp.x, gp.y);
00579 v->x_pos = gp.x;
00580 v->y_pos = gp.y;
00581 v->z_pos = GetSlopeZ(gp.x, gp.y);
00582
00583 getout:
00584 v->UpdateViewport(true, true);
00585 return;
00586
00587 reverse_direction:
00588 dir = ReverseDir(v->direction);
00589 v->direction = dir;
00590 goto getout;
00591 }
00592
00593 bool Ship::Tick()
00594 {
00595 if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
00596
00597 ShipController(this);
00598
00599 return true;
00600 }
00601
00610 CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00611 {
00612 UnitID unit_num;
00613
00614 if (!IsEngineBuildable(p1, VEH_SHIP, _current_company)) return_cmd_error(STR_ERROR_SHIP_NOT_AVAILABLE);
00615
00616 const Engine *e = Engine::Get(p1);
00617 CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
00618
00619
00620 if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
00621
00622 if (flags & DC_QUERY_COST) return value;
00623
00624
00625
00626 if (!IsShipDepotTile(tile)) return CMD_ERROR;
00627 if (!CheckInfraUsageAllowed(GetTileOwner(tile), VEH_SHIP)) return CMD_ERROR;
00628
00629 unit_num = (flags & DC_AUTOREPLACE) ? 0 : GetFreeUnitNumber(VEH_SHIP);
00630
00631 if (!Vehicle::CanAllocateItem() || unit_num > _settings_game.vehicle.max_ships)
00632 return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
00633
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 v->unitnumber = unit_num;
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 = GetSlopeZ(x, y);
00650
00651 v->running_ticks = 0;
00652
00653 v->UpdateDeltaXY(v->direction);
00654 v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
00655
00656 v->spritenum = svi->image_index;
00657 v->cargo_type = e->GetDefaultCargoType();
00658 v->cargo_subtype = 0;
00659 v->cargo_cap = svi->capacity;
00660 v->value = value.GetCost();
00661
00662 v->last_station_visited = INVALID_STATION;
00663 v->max_speed = svi->max_speed;
00664 v->engine_type = p1;
00665
00666 v->reliability = e->reliability;
00667 v->reliability_spd_dec = e->reliability_spd_dec;
00668 v->max_age = e->GetLifeLengthInDays();
00669 _new_vehicle_id = v->index;
00670
00671 v->name = NULL;
00672 v->state = TRACK_BIT_DEPOT;
00673
00674 v->service_interval = Company::Get(_current_company)->settings.vehicle.servint_ships;
00675 v->date_of_last_service = _date;
00676 v->build_year = _cur_year;
00677 v->cur_image = SPR_IMG_QUERY;
00678 v->random_bits = VehicleRandomBits();
00679
00680 v->vehicle_flags = 0;
00681 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
00682
00683 v->InvalidateNewGRFCacheOfChain();
00684
00685 v->cargo_cap = GetVehicleCapacity(v);
00686
00687 v->InvalidateNewGRFCacheOfChain();
00688
00689 VehicleMove(v, false);
00690
00691 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
00692 InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
00693 SetWindowDirty(WC_COMPANY, v->owner);
00694 if (IsLocalCompany())
00695 InvalidateAutoreplaceWindow(v->engine_type, v->group_id);
00696
00697 Company::Get(_current_company)->num_engines[p1]++;
00698 }
00699
00700 return value;
00701 }
00702
00711 CommandCost CmdSellShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00712 {
00713 Ship *v = Ship::GetIfValid(p1);
00714 if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
00715
00716 if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_SELL_DESTROYED_VEHICLE);
00717
00718 if (!v->IsStoppedInDepot()) {
00719 return_cmd_error(STR_ERROR_SHIP_MUST_BE_STOPPED_IN_DEPOT);
00720 }
00721
00722 CommandCost ret(EXPENSES_NEW_VEHICLES, -v->value);
00723
00724 if (flags & DC_EXEC) {
00725 delete v;
00726 }
00727
00728 return ret;
00729 }
00730
00731 bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
00732 {
00733 const Depot *depot = FindClosestShipDepot(this, 0);
00734
00735 if (depot == NULL) return false;
00736
00737 if (location != NULL) *location = depot->xy;
00738 if (destination != NULL) *destination = depot->index;
00739
00740 return true;
00741 }
00742
00753 CommandCost CmdSendShipToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00754 {
00755 if (p2 & DEPOT_MASS_SEND) {
00756
00757 if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
00758 return SendAllVehiclesToDepot(VEH_SHIP, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
00759 }
00760
00761 Ship *v = Ship::GetIfValid(p1);
00762 if (v == NULL) return CMD_ERROR;
00763
00764 return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
00765 }
00766
00767
00779 CommandCost CmdRefitShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00780 {
00781 CargoID new_cid = GB(p2, 0, 8);
00782 byte new_subtype = GB(p2, 8, 8);
00783
00784 Ship *v = Ship::GetIfValid(p1);
00785
00786 if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
00787 if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_SHIP_MUST_BE_STOPPED_IN_DEPOT);
00788 if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_REFIT_DESTROYED_VEHICLE);
00789
00790
00791 if (new_cid >= NUM_CARGO) return CMD_ERROR;
00792
00793 CommandCost cost = RefitVehicle(v, true, new_cid, new_subtype, flags);
00794
00795 if (flags & DC_EXEC) {
00796 v->colourmap = PAL_NONE;
00797 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
00798 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
00799 InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
00800 }
00801 v->InvalidateNewGRFCacheOfChain();
00802
00803 return cost;
00804
00805 }