File diff r27883:ddbd33508a8a → r27884:803962be0328
src/ship_cmd.cpp
Show inline comments
 
@@ -143,193 +143,193 @@ void Ship::GetImage(Direction 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 */