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