Changeset - r14738:57a777833535
[Not reviewed]
master
0 2 0
terkhen - 14 years ago 2010-03-06 12:27:23
terkhen@openttd.org
(svn r19336) -Codechange: Move rail speed limit to its own function.
2 files changed with 26 insertions and 8 deletions:
0 comments (0 inline, 0 general)
src/train.h
Show inline comments
 
@@ -67,12 +67,13 @@ struct AccelerationCache {
 
	uint32 cached_max_te;           ///< max tractive effort of consist
 

	
 
	/* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */
 
	uint32 cached_power;            ///< total power of the consist.
 
	uint32 cached_air_drag;         ///< Air drag coefficient of the vehicle
 
	uint16 cached_axle_resistance;  ///< Resistance caused by the axles of the vehicle
 
	uint16 cached_max_track_speed;  ///< Max consist speed limited by track type
 
};
 

	
 
/** Variables that are cached to improve performance and such */
 
struct TrainCache : public AccelerationCache {
 
	/* Cached wagon override spritegroup */
 
	const struct SpriteGroup *cached_override;
 
@@ -83,13 +84,12 @@ struct TrainCache : public AccelerationC
 
	uint16 cached_total_length; ///< Length of the whole train, valid only for first engine.
 
	uint8 cached_veh_length;    ///< length of this vehicle in units of 1/8 of normal length, cached because this can be set by a callback
 
	bool cached_tilt;           ///< train can tilt; feature provides a bonus in curves
 

	
 
	/* cached max. speed / acceleration data */
 
	uint16 cached_max_speed;    ///< max speed of the consist. (minimum of the max speed of all vehicles in the consist)
 
	uint16 cached_max_rail_speed; ///< max consist speed limited by rail type
 
	int cached_max_curve_speed; ///< max consist speed limited by curves
 

	
 
	/**
 
	 * Position/type of visual effect.
 
	 * bit 0 - 3 = position of effect relative to vehicle. (0 = front, 8 = centre, 15 = rear)
 
	 * bit 4 - 5 = type of effect. (0 = default for engine class, 1 = steam, 2 = diesel, 3 = electric)
 
@@ -509,11 +509,29 @@ protected: // These functions should not
 
	 * @return Slope steepness used by the vehicle.
 
	 */
 
	FORCEINLINE uint32 GetSlopeSteepness() const
 
	{
 
		return 20 * _settings_game.vehicle.train_slope_steepness; // 1% slope * slope steepness
 
	}
 

	
 
	/**
 
	 * Gets the maximum speed of the vehicle, ignoring the limitations of the kind of track the vehicle is on.
 
	 * @return Maximum speed of the vehicle.
 
	 */
 
	FORCEINLINE uint16 GetInitialMaxSpeed() const
 
	{
 
		return this->tcache.cached_max_speed;
 
	}
 

	
 
	/**
 
	 * Gets the maximum speed allowed by the track for this vehicle.
 
	 * @return Maximum speed allowed.
 
	 */
 
	FORCEINLINE uint16 GetMaxTrackSpeed() const
 
	{
 
		return GetRailTypeInfo(GetRailType(this->tile))->max_speed;
 
	}
 
};
 

	
 
#define FOR_ALL_TRAINS(var) FOR_ALL_VEHICLES_OF_TYPE(Train, var)
 

	
 
#endif /* TRAIN_H */
src/train_cmd.cpp
Show inline comments
 
@@ -96,26 +96,26 @@ void Train::PowerChanged()
 
{
 
	assert(this->First() == this);
 

	
 
	uint32 total_power = 0;
 
	uint32 max_te = 0;
 
	uint32 number_of_parts = 0;
 
	uint16 max_rail_speed = this->tcache.cached_max_speed;
 
	uint16 max_track_speed = this->GetInitialMaxSpeed();
 

	
 
	for (const Train *u = this; u != NULL; u = u->Next()) {
 
		uint32 current_power = u->GetPower();
 
		total_power += current_power;
 

	
 
		/* Only powered parts add tractive effort */
 
		if (current_power > 0) max_te += u->GetWeight() * u->GetTractiveEffort();
 
		total_power += u->GetPoweredPartPower(this);
 
		number_of_parts++;
 

	
 
		/* Get minimum max speed for rail */
 
		uint16 rail_speed = GetRailTypeInfo(GetRailType(u->tile))->max_speed;
 
		if (rail_speed > 0) max_rail_speed = min(max_rail_speed, rail_speed);
 
		/* Get minimum max speed for this track */
 
		uint16 track_speed = u->GetMaxTrackSpeed();
 
		if (track_speed > 0) max_track_speed = min(max_track_speed, track_speed);
 
	}
 

	
 
	this->tcache.cached_axle_resistance = 60 * number_of_parts;
 
	this->tcache.cached_air_drag = 20 + 3 * number_of_parts;
 

	
 
	max_te *= 10000; // Tractive effort in (tonnes * 1000 * 10 =) N
 
@@ -127,13 +127,13 @@ void Train::PowerChanged()
 
		this->tcache.cached_power = total_power;
 
		this->tcache.cached_max_te = max_te;
 
		SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
 
		SetWindowWidgetDirty(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
 
	}
 

	
 
	this->tcache.cached_max_rail_speed = max_rail_speed;
 
	this->tcache.cached_max_track_speed = max_track_speed;
 
}
 

	
 

	
 
/**
 
 * Recalculates the cached weight of a train and its vehicles. Should be called each time the cargo on
 
 * the consist changes.
 
@@ -495,13 +495,13 @@ int Train::GetCurrentMaxSpeed() const
 
		if (u->track == TRACK_BIT_DEPOT) {
 
			max_speed = min(max_speed, 61);
 
			break;
 
		}
 
	}
 

	
 
	return min(max_speed, this->tcache.cached_max_rail_speed);
 
	return min(max_speed, this->tcache.cached_max_track_speed);
 
}
 

	
 
/**
 
 * Calculates the acceleration of the vehicle under its current conditions.
 
 * @return Current acceleration of the vehicle.
 
 */
 
@@ -561,13 +561,13 @@ int Train::GetAcceleration() const
 
}
 

	
 
void Train::UpdateAcceleration()
 
{
 
	assert(this->IsFrontEngine());
 

	
 
	this->max_speed = this->tcache.cached_max_rail_speed;
 
	this->max_speed = this->tcache.cached_max_track_speed;
 

	
 
	uint power = this->tcache.cached_power;
 
	uint weight = this->tcache.cached_weight;
 
	assert(weight != 0);
 
	this->acceleration = Clamp(power / weight * 4, 1, 255);
 
}
0 comments (0 inline, 0 general)