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