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