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 "ai/ai.hpp"
00034 #include "pathfinder/opf/opf_ship.h"
00035 #include "landscape_type.h"
00036 #include "engine_base.h"
00037 #include "company_base.h"
00038
00039 #include "table/strings.h"
00040
00041 static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
00042
00043 static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
00044 {
00045 return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
00046 }
00047
00048 static SpriteID GetShipIcon(EngineID engine)
00049 {
00050 const Engine *e = Engine::Get(engine);
00051 uint8 spritenum = e->u.ship.image_index;
00052
00053 if (is_custom_sprite(spritenum)) {
00054 SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
00055 if (sprite != 0) return sprite;
00056
00057 spritenum = e->original_image_index;
00058 }
00059
00060 return DIR_W + _ship_sprites[spritenum];
00061 }
00062
00063 void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal)
00064 {
00065 SpriteID sprite = GetShipIcon(engine);
00066 const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
00067 preferred_x = Clamp(preferred_x, left - real_sprite->x_offs, right - real_sprite->width - real_sprite->x_offs);
00068 DrawSprite(sprite, pal, preferred_x, y);
00069 }
00070
00077 void GetShipSpriteSize(EngineID engine, uint &width, uint &height)
00078 {
00079 const Sprite *spr = GetSprite(GetShipIcon(engine), ST_NORMAL);
00080
00081 width = spr->width;
00082 height = spr->height;
00083 }
00084
00085 SpriteID Ship::GetImage(Direction direction) const
00086 {
00087 uint8 spritenum = this->spritenum;
00088
00089 if (is_custom_sprite(spritenum)) {
00090 SpriteID sprite = GetCustomVehicleSprite(this, direction);
00091 if (sprite != 0) return sprite;
00092
00093 spritenum = Engine::Get(this->engine_type)->original_image_index;
00094 }
00095
00096 return _ship_sprites[spritenum] + direction;
00097 }
00098
00099 static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
00100 {
00101
00102 const Depot *depot;
00103 const Depot *best_depot = NULL;
00104
00105
00106
00107
00108
00109 uint best_dist = max_distance == 0 ? UINT_MAX : max_distance + 1;
00110
00111 FOR_ALL_DEPOTS(depot) {
00112 TileIndex tile = depot->xy;
00113 if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
00114 uint dist = DistanceManhattan(tile, v->tile);
00115 if (dist < best_dist) {
00116 best_dist = dist;
00117 best_depot = depot;
00118 }
00119 }
00120 }
00121
00122 return best_depot;
00123 }
00124
00125 static void CheckIfShipNeedsService(Vehicle *v)
00126 {
00127 if (Company::Get(v->owner)->settings.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
00128 if (v->IsInDepot()) {
00129 VehicleServiceInDepot(v);
00130 return;
00131 }
00132
00133 uint max_distance;
00134 switch (_settings_game.pf.pathfinder_for_ships) {
00135 case VPF_OPF: max_distance = 12; break;
00136 case VPF_NPF: max_distance = _settings_game.pf.npf.maximum_go_to_depot_penalty / NPF_TILE_LENGTH; break;
00137 case VPF_YAPF: max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; break;
00138 default: NOT_REACHED();
00139 }
00140
00141 const Depot *depot = FindClosestShipDepot(v, max_distance);
00142
00143 if (depot == NULL) {
00144 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
00145 v->current_order.MakeDummy();
00146 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00147 }
00148 return;
00149 }
00150
00151 v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
00152 v->dest_tile = depot->xy;
00153 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00154 }
00155
00156 void Ship::UpdateCache()
00157 {
00158 this->vcache.cached_max_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, ShipVehInfo(this->engine_type)->max_speed);
00159
00160 this->UpdateVisualEffect();
00161 }
00162
00163 Money Ship::GetRunningCost() const
00164 {
00165 const Engine *e = Engine::Get(this->engine_type);
00166 uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
00167 return GetPrice(PR_RUNNING_SHIP, cost_factor, e->grf_prop.grffile);
00168 }
00169
00170 void Ship::OnNewDay()
00171 {
00172 if ((++this->day_counter & 7) == 0) {
00173 DecreaseVehicleValue(this);
00174 }
00175
00176 CheckVehicleBreakdown(this);
00177 AgeVehicle(this);
00178 CheckIfShipNeedsService(this);
00179
00180 CheckOrders(this);
00181
00182 if (this->running_ticks == 0) return;
00183
00184 CommandCost cost(EXPENSES_SHIP_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
00185
00186 this->profit_this_year -= cost.GetCost();
00187 this->running_ticks = 0;
00188
00189 SubtractMoneyFromCompanyFract(this->owner, cost);
00190
00191 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
00192
00193 SetWindowClassesDirty(WC_SHIPS_LIST);
00194 }
00195
00196 Trackdir Ship::GetVehicleTrackdir() const
00197 {
00198 if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
00199
00200 if (this->IsInDepot()) {
00201
00202 return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
00203 }
00204
00205 if (this->state == TRACK_BIT_WORMHOLE) {
00206
00207 return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
00208 }
00209
00210 return TrackDirectionToTrackdir(FindFirstTrack(this->state), this->direction);
00211 }
00212
00213 void Ship::MarkDirty()
00214 {
00215 this->UpdateViewport(false, false);
00216 this->UpdateCache();
00217 }
00218
00219 static void PlayShipSound(const Vehicle *v)
00220 {
00221 if (!PlayVehicleSound(v, VSE_START)) {
00222 SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v);
00223 }
00224 }
00225
00226 void Ship::PlayLeaveStationSound() const
00227 {
00228 PlayShipSound(this);
00229 }
00230
00231 TileIndex Ship::GetOrderStationLocation(StationID station)
00232 {
00233 if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
00234
00235 const Station *st = Station::Get(station);
00236 if (st->dock_tile != INVALID_TILE) {
00237 return TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
00238 } else {
00239 this->IncrementOrderIndex();
00240 return 0;
00241 }
00242 }
00243
00244 void Ship::UpdateDeltaXY(Direction direction)
00245 {
00246 #define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
00247 static const uint32 _delta_xy_table[8] = {
00248 MKIT( 6, 6, -3, -3),
00249 MKIT( 6, 32, -3, -16),
00250 MKIT( 6, 6, -3, -3),
00251 MKIT(32, 6, -16, -3),
00252 MKIT( 6, 6, -3, -3),
00253 MKIT( 6, 32, -3, -16),
00254 MKIT( 6, 6, -3, -3),
00255 MKIT(32, 6, -16, -3),
00256 };
00257 #undef MKIT
00258
00259 uint32 x = _delta_xy_table[direction];
00260 this->x_offs = GB(x, 0, 8);
00261 this->y_offs = GB(x, 8, 8);
00262 this->x_extent = GB(x, 16, 8);
00263 this->y_extent = GB(x, 24, 8);
00264 this->z_extent = 6;
00265 }
00266
00267 static const TileIndexDiffC _ship_leave_depot_offs[] = {
00268 {-1, 0},
00269 { 0, -1}
00270 };
00271
00272 static void CheckShipLeaveDepot(Ship *v)
00273 {
00274 if (!v->IsInDepot()) return;
00275
00276 TileIndex tile = v->tile;
00277 Axis axis = GetShipDepotAxis(tile);
00278
00279
00280 if (DiagdirReachesTracks((DiagDirection)axis) & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
00281 v->direction = ReverseDir(AxisToDirection(axis));
00282
00283 } else if (DiagdirReachesTracks((DiagDirection)(axis + 2)) & GetTileShipTrackStatus(TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
00284 v->direction = AxisToDirection(axis);
00285 } else {
00286 return;
00287 }
00288
00289 v->state = AxisToTrackBits(axis);
00290 v->vehstatus &= ~VS_HIDDEN;
00291
00292 v->cur_speed = 0;
00293 v->UpdateViewport(false, true);
00294 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
00295
00296 PlayShipSound(v);
00297 VehicleServiceInDepot(v);
00298 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
00299 SetWindowClassesDirty(WC_SHIPS_LIST);
00300 }
00301
00302 static bool ShipAccelerate(Vehicle *v)
00303 {
00304 uint spd;
00305 byte t;
00306
00307 spd = min(v->cur_speed + 1, v->vcache.cached_max_speed);
00308
00309
00310 if (spd != v->cur_speed) {
00311 v->cur_speed = spd;
00312 if (_settings_client.gui.vehicle_speed) {
00313 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00314 }
00315 }
00316
00317
00318 spd = v->GetOldAdvanceSpeed(spd);
00319
00320 if (spd == 0) return false;
00321 if ((byte)++spd == 0) return true;
00322
00323 v->progress = (t = v->progress) - (byte)spd;
00324
00325 return (t < v->progress);
00326 }
00327
00333 static void ShipArrivesAt(const Vehicle *v, Station *st)
00334 {
00335
00336 if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
00337 st->had_vehicle_of_type |= HVOT_SHIP;
00338
00339 SetDParam(0, st->index);
00340 AddVehicleNewsItem(
00341 STR_NEWS_FIRST_SHIP_ARRIVAL,
00342 (v->owner == _local_company) ? NS_ARRIVAL_COMPANY : NS_ARRIVAL_OTHER,
00343 v->index,
00344 st->index
00345 );
00346 AI::NewEvent(v->owner, new AIEventStationFirstVehicle(st->index, v->index));
00347 }
00348 }
00349
00350
00356 static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
00357 {
00358 assert(IsValidDiagDirection(enterdir));
00359
00360 bool path_found = true;
00361 Track track;
00362 switch (_settings_game.pf.pathfinder_for_ships) {
00363 case VPF_OPF: track = OPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
00364 case VPF_NPF: track = NPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
00365 case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
00366 default: NOT_REACHED();
00367 }
00368
00369 v->HandlePathfindingResult(path_found);
00370 return track;
00371 }
00372
00373 static const Direction _new_vehicle_direction_table[] = {
00374 DIR_N , DIR_NW, DIR_W , INVALID_DIR,
00375 DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
00376 DIR_E , DIR_SE, DIR_S
00377 };
00378
00379 static Direction ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile)
00380 {
00381 uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
00382 TileX(new_tile) - TileX(old_tile) + 1;
00383 assert(offs < 11 && offs != 3 && offs != 7);
00384 return _new_vehicle_direction_table[offs];
00385 }
00386
00387 static Direction ShipGetNewDirection(Vehicle *v, int x, int y)
00388 {
00389 uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
00390 assert(offs < 11 && offs != 3 && offs != 7);
00391 return _new_vehicle_direction_table[offs];
00392 }
00393
00394 static inline TrackBits GetAvailShipTracks(TileIndex tile, DiagDirection dir)
00395 {
00396 return GetTileShipTrackStatus(tile) & DiagdirReachesTracks(dir);
00397 }
00398
00399 static const byte _ship_subcoord[4][6][3] = {
00400 {
00401 {15, 8, 1},
00402 { 0, 0, 0},
00403 { 0, 0, 0},
00404 {15, 8, 2},
00405 {15, 7, 0},
00406 { 0, 0, 0},
00407 },
00408 {
00409 { 0, 0, 0},
00410 { 8, 0, 3},
00411 { 7, 0, 2},
00412 { 0, 0, 0},
00413 { 8, 0, 4},
00414 { 0, 0, 0},
00415 },
00416 {
00417 { 0, 8, 5},
00418 { 0, 0, 0},
00419 { 0, 7, 6},
00420 { 0, 0, 0},
00421 { 0, 0, 0},
00422 { 0, 8, 4},
00423 },
00424 {
00425 { 0, 0, 0},
00426 { 8, 15, 7},
00427 { 0, 0, 0},
00428 { 8, 15, 6},
00429 { 0, 0, 0},
00430 { 7, 15, 0},
00431 }
00432 };
00433
00434 static void ShipController(Ship *v)
00435 {
00436 uint32 r;
00437 const byte *b;
00438 Direction dir;
00439 Track track;
00440 TrackBits tracks;
00441
00442 v->tick_counter++;
00443 v->current_order_time++;
00444
00445 if (v->HandleBreakdown()) return;
00446
00447 if (v->vehstatus & VS_STOPPED) return;
00448
00449 ProcessOrders(v);
00450 v->HandleLoading();
00451
00452 if (v->current_order.IsType(OT_LOADING)) return;
00453
00454 CheckShipLeaveDepot(v);
00455
00456 v->ShowVisualEffect();
00457
00458 if (!ShipAccelerate(v)) return;
00459
00460 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
00461 if (v->state != TRACK_BIT_WORMHOLE) {
00462
00463 if (gp.old_tile == gp.new_tile) {
00464
00465 if (v->IsInDepot()) {
00466 gp.x = v->x_pos;
00467 gp.y = v->y_pos;
00468 } else {
00469
00470 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
00471 if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
00472
00473
00474
00475 if (v->current_order.IsType(OT_LEAVESTATION)) {
00476 v->current_order.Free();
00477 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
00478 } else if (v->dest_tile != 0) {
00479
00480 if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
00481 DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
00482
00483
00484 UpdateVehicleTimetable(v, true);
00485 v->IncrementOrderIndex();
00486 v->current_order.MakeDummy();
00487 } else {
00488
00489 if (v->dest_tile == gp.new_tile) {
00490 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
00491 if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
00492 VehicleEnterDepot(v);
00493 return;
00494 }
00495 } else if (v->current_order.IsType(OT_GOTO_STATION)) {
00496 v->last_station_visited = v->current_order.GetDestination();
00497
00498
00499 Station *st = Station::Get(v->current_order.GetDestination());
00500 if (st->facilities & FACIL_DOCK) {
00501 ShipArrivesAt(v, st);
00502 v->BeginLoading();
00503 } else {
00504 v->current_order.MakeLeaveStation();
00505 v->IncrementOrderIndex();
00506 }
00507 }
00508 }
00509 }
00510 }
00511 }
00512 } else {
00513 DiagDirection diagdir;
00514
00515 if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) {
00516 goto reverse_direction;
00517 }
00518
00519 dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
00520 assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW);
00521 diagdir = DirToDiagDir(dir);
00522 tracks = GetAvailShipTracks(gp.new_tile, diagdir);
00523 if (tracks == TRACK_BIT_NONE) goto reverse_direction;
00524
00525
00526 track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
00527 if (track == INVALID_TRACK) goto reverse_direction;
00528
00529 b = _ship_subcoord[diagdir][track];
00530
00531 gp.x = (gp.x & ~0xF) | b[0];
00532 gp.y = (gp.y & ~0xF) | b[1];
00533
00534
00535 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
00536 if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
00537
00538 if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
00539 v->tile = gp.new_tile;
00540 v->state = TrackToTrackBits(track);
00541 }
00542
00543 v->direction = (Direction)b[2];
00544 }
00545 } else {
00546
00547 if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
00548 v->x_pos = gp.x;
00549 v->y_pos = gp.y;
00550 VehicleMove(v, !(v->vehstatus & VS_HIDDEN));
00551 return;
00552 }
00553 }
00554
00555
00556 dir = ShipGetNewDirection(v, gp.x, gp.y);
00557 v->x_pos = gp.x;
00558 v->y_pos = gp.y;
00559 v->z_pos = GetSlopeZ(gp.x, gp.y);
00560
00561 getout:
00562 v->UpdateViewport(true, true);
00563 return;
00564
00565 reverse_direction:
00566 dir = ReverseDir(v->direction);
00567 v->direction = dir;
00568 goto getout;
00569 }
00570
00571 bool Ship::Tick()
00572 {
00573 if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
00574
00575 ShipController(this);
00576
00577 return true;
00578 }
00579
00589 CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **ret)
00590 {
00591 tile = GetShipDepotNorthTile(tile);
00592 if (flags & DC_EXEC) {
00593 int x;
00594 int y;
00595
00596 const ShipVehicleInfo *svi = &e->u.ship;
00597
00598 Ship *v = new Ship();
00599 *ret = v;
00600
00601 v->owner = _current_company;
00602 v->tile = tile;
00603 x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
00604 y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
00605 v->x_pos = x;
00606 v->y_pos = y;
00607 v->z_pos = GetSlopeZ(x, y);
00608
00609 v->UpdateDeltaXY(v->direction);
00610 v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
00611
00612 v->spritenum = svi->image_index;
00613 v->cargo_type = e->GetDefaultCargoType();
00614 v->cargo_cap = svi->capacity;
00615
00616 v->last_station_visited = INVALID_STATION;
00617 v->engine_type = e->index;
00618
00619 v->reliability = e->reliability;
00620 v->reliability_spd_dec = e->reliability_spd_dec;
00621 v->max_age = e->GetLifeLengthInDays();
00622 _new_vehicle_id = v->index;
00623
00624 v->state = TRACK_BIT_DEPOT;
00625
00626 v->service_interval = Company::Get(_current_company)->settings.vehicle.servint_ships;
00627 v->date_of_last_service = _date;
00628 v->build_year = _cur_year;
00629 v->cur_image = SPR_IMG_QUERY;
00630 v->random_bits = VehicleRandomBits();
00631
00632 v->UpdateCache();
00633
00634 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
00635
00636 v->InvalidateNewGRFCacheOfChain();
00637
00638 v->cargo_cap = GetVehicleCapacity(v);
00639
00640 v->InvalidateNewGRFCacheOfChain();
00641
00642 VehicleMove(v, false);
00643 }
00644
00645 return CommandCost();
00646 }
00647
00648 bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
00649 {
00650 const Depot *depot = FindClosestShipDepot(this, 0);
00651
00652 if (depot == NULL) return false;
00653
00654 if (location != NULL) *location = depot->xy;
00655 if (destination != NULL) *destination = depot->index;
00656
00657 return true;
00658 }