|
@@ -47,385 +47,385 @@
|
|
|
*/
|
|
|
WaterClass GetEffectiveWaterClass(TileIndex tile)
|
|
|
{
|
|
|
if (HasTileWaterClass(tile)) return GetWaterClass(tile);
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
|
|
assert(GetTunnelBridgeTransportType(tile) == TRANSPORT_WATER);
|
|
|
return WATER_CLASS_CANAL;
|
|
|
}
|
|
|
if (IsTileType(tile, MP_RAILWAY)) {
|
|
|
assert(GetRailGroundType(tile) == RAIL_GROUND_WATER);
|
|
|
return WATER_CLASS_SEA;
|
|
|
}
|
|
|
NOT_REACHED();
|
|
|
}
|
|
|
|
|
|
static const uint16_t _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
|
|
|
|
|
|
template <>
|
|
|
bool IsValidImageIndex<VEH_SHIP>(uint8_t image_index)
|
|
|
{
|
|
|
return image_index < lengthof(_ship_sprites);
|
|
|
}
|
|
|
|
|
|
static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
|
|
|
{
|
|
|
return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
|
|
|
}
|
|
|
|
|
|
static void GetShipIcon(EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
|
|
|
{
|
|
|
const Engine *e = Engine::Get(engine);
|
|
|
uint8_t spritenum = e->u.ship.image_index;
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
|
|
GetCustomVehicleIcon(engine, DIR_W, image_type, result);
|
|
|
if (result->IsValid()) return;
|
|
|
|
|
|
spritenum = e->original_image_index;
|
|
|
}
|
|
|
|
|
|
assert(IsValidImageIndex<VEH_SHIP>(spritenum));
|
|
|
result->Set(DIR_W + _ship_sprites[spritenum]);
|
|
|
}
|
|
|
|
|
|
void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
|
|
|
{
|
|
|
VehicleSpriteSeq seq;
|
|
|
GetShipIcon(engine, image_type, &seq);
|
|
|
|
|
|
Rect rect;
|
|
|
seq.GetBounds(&rect);
|
|
|
preferred_x = Clamp(preferred_x,
|
|
|
left - UnScaleGUI(rect.left),
|
|
|
right - UnScaleGUI(rect.right));
|
|
|
|
|
|
seq.Draw(preferred_x, y, pal, pal == PALETTE_CRASH);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Get the size of the sprite of a ship sprite heading west (used for lists).
|
|
|
* @param engine The engine to get the sprite from.
|
|
|
* @param[out] width The width of the sprite.
|
|
|
* @param[out] height The height of the sprite.
|
|
|
* @param[out] xoffs Number of pixels to shift the sprite to the right.
|
|
|
* @param[out] yoffs Number of pixels to shift the sprite downwards.
|
|
|
* @param image_type Context the sprite is used in.
|
|
|
*/
|
|
|
void GetShipSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
|
|
|
{
|
|
|
VehicleSpriteSeq seq;
|
|
|
GetShipIcon(engine, image_type, &seq);
|
|
|
|
|
|
Rect rect;
|
|
|
seq.GetBounds(&rect);
|
|
|
|
|
|
width = UnScaleGUI(rect.Width());
|
|
|
height = UnScaleGUI(rect.Height());
|
|
|
xoffs = UnScaleGUI(rect.left);
|
|
|
yoffs = UnScaleGUI(rect.top);
|
|
|
}
|
|
|
|
|
|
void Ship::GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
|
|
|
{
|
|
|
uint8_t spritenum = this->spritenum;
|
|
|
|
|
|
if (image_type == EIT_ON_MAP) direction = this->rotation;
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
|
|
GetCustomVehicleSprite(this, direction, image_type, result);
|
|
|
if (result->IsValid()) return;
|
|
|
|
|
|
spritenum = this->GetEngine()->original_image_index;
|
|
|
}
|
|
|
|
|
|
assert(IsValidImageIndex<VEH_SHIP>(spritenum));
|
|
|
result->Set(_ship_sprites[spritenum] + direction);
|
|
|
}
|
|
|
|
|
|
static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
|
|
|
{
|
|
|
/* Find the closest depot */
|
|
|
const Depot *best_depot = nullptr;
|
|
|
/* If we don't have a maximum distance, i.e. distance = 0,
|
|
|
* we want to find any depot so the best distance of no
|
|
|
* depot must be more than any correct distance. On the
|
|
|
* other hand if we have set a maximum distance, any depot
|
|
|
* further away than max_distance can safely be ignored. */
|
|
|
uint best_dist = max_distance == 0 ? UINT_MAX : max_distance + 1;
|
|
|
|
|
|
for (const Depot *depot : Depot::Iterate()) {
|
|
|
TileIndex tile = depot->xy;
|
|
|
if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
|
|
|
uint dist = DistanceManhattan(tile, v->tile);
|
|
|
if (dist < best_dist) {
|
|
|
best_dist = dist;
|
|
|
best_depot = depot;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return best_depot;
|
|
|
}
|
|
|
|
|
|
static void CheckIfShipNeedsService(Vehicle *v)
|
|
|
{
|
|
|
if (Company::Get(v->owner)->settings.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
|
|
|
if (v->IsChainInDepot()) {
|
|
|
VehicleServiceInDepot(v);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
uint max_distance;
|
|
|
switch (_settings_game.pf.pathfinder_for_ships) {
|
|
|
case VPF_NPF: max_distance = _settings_game.pf.npf.maximum_go_to_depot_penalty / NPF_TILE_LENGTH; break;
|
|
|
case VPF_YAPF: max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; break;
|
|
|
default: NOT_REACHED();
|
|
|
}
|
|
|
|
|
|
const Depot *depot = FindClosestShipDepot(v, max_distance);
|
|
|
|
|
|
if (depot == nullptr) {
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT)) {
|
|
|
v->current_order.MakeDummy();
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
|
|
|
v->SetDestTile(depot->xy);
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Update the caches of this ship.
|
|
|
*/
|
|
|
void Ship::UpdateCache()
|
|
|
{
|
|
|
const ShipVehicleInfo *svi = ShipVehInfo(this->engine_type);
|
|
|
|
|
|
/* Get speed fraction for the current water type. Aqueducts are always canals. */
|
|
|
bool is_ocean = GetEffectiveWaterClass(this->tile) == WATER_CLASS_SEA;
|
|
|
uint raw_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed);
|
|
|
this->vcache.cached_max_speed = svi->ApplyWaterClassSpeedFrac(raw_speed, is_ocean);
|
|
|
|
|
|
/* Update cargo aging period. */
|
|
|
this->vcache.cached_cargo_age_period = GetVehicleProperty(this, PROP_SHIP_CARGO_AGE_PERIOD, EngInfo(this->engine_type)->cargo_age_period);
|
|
|
|
|
|
this->UpdateVisualEffect();
|
|
|
}
|
|
|
|
|
|
Money Ship::GetRunningCost() const
|
|
|
{
|
|
|
const Engine *e = this->GetEngine();
|
|
|
uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
|
|
|
return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
|
|
|
}
|
|
|
|
|
|
void Ship::OnNewDay()
|
|
|
{
|
|
|
if ((++this->day_counter & 7) == 0) {
|
|
|
DecreaseVehicleValue(this);
|
|
|
}
|
|
|
|
|
|
CheckVehicleBreakdown(this);
|
|
|
AgeVehicle(this);
|
|
|
CheckIfShipNeedsService(this);
|
|
|
|
|
|
CheckOrders(this);
|
|
|
|
|
|
if (this->running_ticks == 0) return;
|
|
|
|
|
|
CommandCost cost(EXPENSES_SHIP_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * Ticks::DAY_TICKS));
|
|
|
CommandCost cost(EXPENSES_SHIP_RUN, this->GetRunningCost() * this->running_ticks / (CalendarTime::DAYS_IN_YEAR * Ticks::DAY_TICKS));
|
|
|
|
|
|
this->profit_this_year -= cost.GetCost();
|
|
|
this->running_ticks = 0;
|
|
|
|
|
|
SubtractMoneyFromCompanyFract(this->owner, cost);
|
|
|
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
|
|
|
/* we need this for the profit */
|
|
|
SetWindowClassesDirty(WC_SHIPS_LIST);
|
|
|
}
|
|
|
|
|
|
Trackdir Ship::GetVehicleTrackdir() const
|
|
|
{
|
|
|
if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
|
|
|
|
|
|
if (this->IsInDepot()) {
|
|
|
/* We'll assume the ship is facing outwards */
|
|
|
return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
|
|
|
}
|
|
|
|
|
|
if (this->state == TRACK_BIT_WORMHOLE) {
|
|
|
/* ship on aqueduct, so just use its direction and assume a diagonal track */
|
|
|
return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
|
|
|
}
|
|
|
|
|
|
return TrackDirectionToTrackdir(FindFirstTrack(this->state), this->direction);
|
|
|
}
|
|
|
|
|
|
void Ship::MarkDirty()
|
|
|
{
|
|
|
this->colourmap = PAL_NONE;
|
|
|
this->UpdateViewport(true, false);
|
|
|
this->UpdateCache();
|
|
|
}
|
|
|
|
|
|
void Ship::PlayLeaveStationSound(bool force) const
|
|
|
{
|
|
|
if (PlayVehicleSound(this, VSE_START, force)) return;
|
|
|
SndPlayVehicleFx(ShipVehInfo(this->engine_type)->sfx, this);
|
|
|
}
|
|
|
|
|
|
TileIndex Ship::GetOrderStationLocation(StationID station)
|
|
|
{
|
|
|
if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
|
|
|
|
|
|
const Station *st = Station::Get(station);
|
|
|
if (CanVehicleUseStation(this, st)) {
|
|
|
return st->xy;
|
|
|
} else {
|
|
|
this->IncrementRealOrderIndex();
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void Ship::UpdateDeltaXY()
|
|
|
{
|
|
|
static const int8_t _delta_xy_table[8][4] = {
|
|
|
/* y_extent, x_extent, y_offs, x_offs */
|
|
|
{ 6, 6, -3, -3}, // N
|
|
|
{ 6, 32, -3, -16}, // NE
|
|
|
{ 6, 6, -3, -3}, // E
|
|
|
{32, 6, -16, -3}, // SE
|
|
|
{ 6, 6, -3, -3}, // S
|
|
|
{ 6, 32, -3, -16}, // SW
|
|
|
{ 6, 6, -3, -3}, // W
|
|
|
{32, 6, -16, -3}, // NW
|
|
|
};
|
|
|
|
|
|
const int8_t *bb = _delta_xy_table[this->rotation];
|
|
|
this->x_offs = bb[3];
|
|
|
this->y_offs = bb[2];
|
|
|
this->x_extent = bb[1];
|
|
|
this->y_extent = bb[0];
|
|
|
this->z_extent = 6;
|
|
|
|
|
|
if (this->direction != this->rotation) {
|
|
|
/* If we are rotating, then it is possible the ship was moved to its next position. In that
|
|
|
* case, because we are still showing the old direction, the ship will appear to glitch sideways
|
|
|
* slightly. We can work around this by applying an additional offset to make the ship appear
|
|
|
* where it was before it moved. */
|
|
|
this->x_offs -= this->x_pos - this->rotation_x_pos;
|
|
|
this->y_offs -= this->y_pos - this->rotation_y_pos;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Test-procedure for HasVehicleOnPos to check for any ships which are visible and not stopped by the player.
|
|
|
*/
|
|
|
static Vehicle *EnsureNoMovingShipProc(Vehicle *v, void *data)
|
|
|
{
|
|
|
return v->type == VEH_SHIP && (v->vehstatus & (VS_HIDDEN | VS_STOPPED)) == 0 ? v : nullptr;
|
|
|
}
|
|
|
|
|
|
static bool CheckReverseShip(const Ship *v, Trackdir *trackdir = nullptr)
|
|
|
{
|
|
|
/* Ask pathfinder for best direction */
|
|
|
bool reverse = false;
|
|
|
switch (_settings_game.pf.pathfinder_for_ships) {
|
|
|
case VPF_NPF: reverse = NPFShipCheckReverse(v, trackdir); break;
|
|
|
case VPF_YAPF: reverse = YapfShipCheckReverse(v, trackdir); break;
|
|
|
default: NOT_REACHED();
|
|
|
}
|
|
|
return reverse;
|
|
|
}
|
|
|
|
|
|
static bool CheckShipLeaveDepot(Ship *v)
|
|
|
{
|
|
|
if (!v->IsChainInDepot()) return false;
|
|
|
|
|
|
/* We are leaving a depot, but have to go to the exact same one; re-enter */
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT) &&
|
|
|
IsShipDepotTile(v->tile) && GetDepotIndex(v->tile) == v->current_order.GetDestination()) {
|
|
|
VehicleEnterDepot(v);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
/* Don't leave depot if no destination set */
|
|
|
if (v->dest_tile == 0) return true;
|
|
|
|
|
|
/* Don't leave depot if another vehicle is already entering/leaving */
|
|
|
/* This helps avoid CPU load if many ships are set to start at the same time */
|
|
|
if (HasVehicleOnPos(v->tile, nullptr, &EnsureNoMovingShipProc)) return true;
|
|
|
|
|
|
TileIndex tile = v->tile;
|
|
|
Axis axis = GetShipDepotAxis(tile);
|
|
|
|
|
|
DiagDirection north_dir = ReverseDiagDir(AxisToDiagDir(axis));
|
|
|
TileIndex north_neighbour = TILE_ADD(tile, TileOffsByDiagDir(north_dir));
|
|
|
DiagDirection south_dir = AxisToDiagDir(axis);
|
|
|
TileIndex south_neighbour = TILE_ADD(tile, 2 * TileOffsByDiagDir(south_dir));
|
|
|
|
|
|
TrackBits north_tracks = DiagdirReachesTracks(north_dir) & GetTileShipTrackStatus(north_neighbour);
|
|
|
TrackBits south_tracks = DiagdirReachesTracks(south_dir) & GetTileShipTrackStatus(south_neighbour);
|
|
|
if (north_tracks && south_tracks) {
|
|
|
if (CheckReverseShip(v)) north_tracks = TRACK_BIT_NONE;
|
|
|
}
|
|
|
|
|
|
if (north_tracks) {
|
|
|
/* Leave towards north */
|
|
|
v->rotation = v->direction = DiagDirToDir(north_dir);
|
|
|
} else if (south_tracks) {
|
|
|
/* Leave towards south */
|
|
|
v->rotation = v->direction = DiagDirToDir(south_dir);
|
|
|
} else {
|
|
|
/* Both ways blocked */
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
v->state = AxisToTrackBits(axis);
|
|
|
v->vehstatus &= ~VS_HIDDEN;
|
|
|
|
|
|
v->cur_speed = 0;
|
|
|
v->UpdateViewport(true, true);
|
|
|
SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
|
|
v->PlayLeaveStationSound();
|
|
|
VehicleServiceInDepot(v);
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
|
|
SetWindowClassesDirty(WC_SHIPS_LIST);
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Accelerates the ship towards its target speed.
|
|
|
* @param v Ship to accelerate.
|
|
|
* @return Number of steps to move the ship.
|
|
|
*/
|
|
|
static uint ShipAccelerate(Vehicle *v)
|
|
|
{
|
|
|
uint speed;
|
|
|
|
|
|
speed = std::min<uint>(v->cur_speed + 1, v->vcache.cached_max_speed);
|
|
|
speed = std::min<uint>(speed, v->current_order.GetMaxSpeed() * 2);
|
|
|
|
|
|
/* updates statusbar only if speed have changed to save CPU time */
|
|
|
if (speed != v->cur_speed) {
|
|
|
v->cur_speed = speed;
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
|
|
}
|
|
|
|
|
|
const uint advance_speed = v->GetAdvanceSpeed(speed);
|
|
|
const uint number_of_steps = (advance_speed + v->progress) / v->GetAdvanceDistance();
|
|
|
const uint remainder = (advance_speed + v->progress) % v->GetAdvanceDistance();
|
|
|
assert(remainder <= std::numeric_limits<byte>::max());
|
|
|
v->progress = static_cast<byte>(remainder);
|
|
|
return number_of_steps;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Ship arrives at a dock. If it is the first time, send out a news item.
|
|
|
* @param v Ship that arrived.
|