Changeset - r17165:9871e31ef1c8
[Not reviewed]
master
0 2 0
rubidium - 13 years ago 2011-01-26 17:34:07
rubidium@openttd.org
(svn r21914) -Codechange: move the algorithmic part of Train::UpdateSpeed to a function in GroundVehicle
2 files changed with 38 insertions and 27 deletions:
0 comments (0 inline, 0 general)
src/ground_vehicle.hpp
Show inline comments
 
@@ -377,6 +377,41 @@ struct GroundVehicle : public Specialize
 
			this->gcache.last_speed = this->cur_speed;
 
		}
 
	}
 

	
 
protected:
 
	/**
 
	 * Update the speed of the vehicle.
 
	 *
 
	 * It updates the cur_speed and subspeed variables depending on the state
 
	 * of the vehicle; in this case the current acceleration, minimum and
 
	 * maximum speeds of the vehicle. It returns the distance that that the
 
	 * vehicle can drive this tick. #Vehicle::GetAdvanceDistance() determines
 
	 * the distance to drive before moving a step on the map.
 
	 * @param accel     The acceleration we would like to give this vehicle.
 
	 * @param min_speed The minimum speed here, in vehicle specific units.
 
	 * @param max_speed The maximum speed here, in vehicle specific units.
 
	 * @return Distance to drive.
 
	 */
 
	FORCEINLINE uint DoUpdateSpeed(uint accel, int min_speed, int max_speed)
 
	{
 
		uint spd = this->subspeed + accel;
 
		this->subspeed = (byte)spd;
 

	
 
		/* When we are going faster than the maximum speed, reduce the speed
 
		 * somewhat gradually. But never lower than the maximum speed. */
 
		int tempmax = max_speed;
 
		if (this->cur_speed > max_speed) {
 
			tempmax = max(this->cur_speed - (this->cur_speed / 10) - 1, max_speed);
 
		}
 

	
 
		this->cur_speed = spd = Clamp(this->cur_speed + ((int)spd >> 8), min_speed, tempmax);
 

	
 
		int scaled_spd = this->GetAdvanceSpeed(spd);
 

	
 
		scaled_spd += this->progress;
 
		this->progress = 0; // set later in *Handler or *Controller
 
		return scaled_spd;
 
	}
 
};
 

	
 
#endif /* GROUND_VEHICLE_HPP */
src/train_cmd.cpp
Show inline comments
 
@@ -2630,38 +2630,14 @@ void Train::MarkDirty()
 
 */
 
int Train::UpdateSpeed()
 
{
 
	uint accel;
 
	uint16 max_speed;
 

	
 
	switch (_settings_game.vehicle.train_acceleration_model) {
 
		default: NOT_REACHED();
 
		case AM_ORIGINAL:
 
			max_speed = this->gcache.cached_max_track_speed;
 
			accel = this->acceleration * (this->GetAccelerationStatus() == AS_BRAKE ? -4 : 2);
 
			break;
 
			return this->DoUpdateSpeed(this->acceleration * (this->GetAccelerationStatus() == AS_BRAKE ? -4 : 2), 0, this->gcache.cached_max_track_speed);
 

	
 
		case AM_REALISTIC:
 
			max_speed = this->GetCurrentMaxSpeed();
 
			accel = this->GetAcceleration();
 
			break;
 
			return this->DoUpdateSpeed(this->GetAcceleration(), this->GetAccelerationStatus() == AS_BRAKE ? 0 : 2, this->GetCurrentMaxSpeed());
 
	}
 

	
 
	uint spd = this->subspeed + accel;
 
	this->subspeed = (byte)spd;
 
	{
 
		int tempmax = max_speed;
 
		if (this->cur_speed > max_speed) {
 
			tempmax = max(this->cur_speed - (this->cur_speed / 10) - 1, tempmax);
 
		}
 
		/* Force a minimum speed of 1 km/h when realistic acceleration is on and the train is not braking. */
 
		int min_speed = (_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL || this->GetAccelerationStatus() == AS_BRAKE) ? 0 : 2;
 
		this->cur_speed = spd = Clamp(this->cur_speed + ((int)spd >> 8), min_speed, tempmax);
 
	}
 

	
 
	int scaled_spd = this->GetAdvanceSpeed(spd);
 

	
 
	scaled_spd += this->progress;
 
	this->progress = 0; // set later in TrainLocoHandler or TrainController
 
	return scaled_spd;
 
}
 

	
 
/**
0 comments (0 inline, 0 general)