Changeset - r14740:9c3af6ca7b4c
[Not reviewed]
master
0 6 0
terkhen - 15 years ago 2010-03-06 12:42:53
terkhen@openttd.org
(svn r19338) -Codechange: Move the acceleration cache to GroundVehicle.
6 files changed with 56 insertions and 55 deletions:
0 comments (0 inline, 0 general)
src/ground_vehicle.hpp
Show inline comments
 
@@ -11,17 +11,41 @@
 

	
 
#ifndef GROUND_VEHICLE_HPP
 
#define GROUND_VEHICLE_HPP
 

	
 
#include "vehicle_base.h"
 

	
 
/** What is the status of our acceleration? */
 
enum AccelStatus {
 
	AS_ACCEL, ///< We want to go faster, if possible of course.
 
	AS_BRAKE  ///< We want to stop.
 
};
 

	
 
/**
 
 * Cached acceleration values.
 
 * All of these values except cached_slope_resistance are set only for the first part of a vehicle.
 
 */
 
struct AccelerationCache {
 
	/* Cached values, recalculated when the cargo on a vehicle changes (in addition to the conditions below) */
 
	uint32 cached_weight;           ///< Total weight of the consist.
 
	uint32 cached_slope_resistance; ///< Resistance caused by weight when this vehicle part is at a slope.
 
	uint32 cached_max_te;           ///< Maximum 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;  ///< Maximum consist speed limited by track type.
 
};
 

	
 
/**
 
 * Base class for all vehicles that move through ground.
 
 */
 
template <class T, VehicleType Type>
 
struct GroundVehicle : public SpecializedVehicle<T, Type> {
 
	AccelerationCache acc_cache;
 

	
 
	/**
 
	 * The constructor at SpecializedVehicle must be called.
 
	 */
 
	GroundVehicle() : SpecializedVehicle<T, Type>() {}
 

	
src/newgrf_engine.cpp
Show inline comments
 
@@ -775,16 +775,16 @@ static uint32 VehicleGetVariable(const R
 
		case VEH_TRAIN: {
 
			Train *t = Train::From(v);
 
			switch (variable - 0x80) {
 
				case 0x62: return t->track;
 
				case 0x66: return t->railtype;
 
				case 0x73: return t->tcache.cached_veh_length;
 
				case 0x74: return t->tcache.cached_power;
 
				case 0x75: return GB(t->tcache.cached_power,  8, 24);
 
				case 0x76: return GB(t->tcache.cached_power, 16, 16);
 
				case 0x77: return GB(t->tcache.cached_power, 24,  8);
 
				case 0x74: return t->acc_cache.cached_power;
 
				case 0x75: return GB(t->acc_cache.cached_power,  8, 24);
 
				case 0x76: return GB(t->acc_cache.cached_power, 16, 16);
 
				case 0x77: return GB(t->acc_cache.cached_power, 24,  8);
 
				case 0x7C: return t->First()->index;
 
				case 0x7D: return GB(t->First()->index, 8, 8);
 
				case 0x7F: return 0; // Used for vehicle reversing hack in TTDP
 
			}
 
		} break;
 

	
src/train.h
Show inline comments
 
@@ -53,31 +53,14 @@ void CheckTrainsLengths();
 

	
 
void FreeTrainTrackReservation(const Train *v, TileIndex origin = INVALID_TILE, Trackdir orig_td = INVALID_TRACKDIR);
 
bool TryPathReserve(Train *v, bool mark_as_stuck = false, bool first_tile_okay = false);
 

	
 
int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, int *station_ahead, int *station_length);
 

	
 
/**
 
 * Cached acceleration values.
 
 * All of these values except cached_slope_resistance are set only for the first part of a vehicle.
 
 */
 
struct AccelerationCache {
 
	/* cached values, recalculated when the cargo on a train changes (in addition to the conditions above) */
 
	uint32 cached_weight;           ///< total weight of the consist.
 
	uint32 cached_slope_resistance; ///< Resistance caused by weight when this vehicle part is at a slope
 
	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 {
 
struct TrainCache {
 
	/* Cached wagon override spritegroup */
 
	const struct SpriteGroup *cached_override;
 

	
 
	uint16 last_speed; // NOSAVE: only used in UI
 

	
 
	/* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */
 
@@ -99,18 +82,12 @@ struct TrainCache : public AccelerationC
 
	byte cached_vis_effect;
 
	byte user_def_data;
 

	
 
	EngineID first_engine;  ///< cached EngineID of the front vehicle. INVALID_ENGINE for the front vehicle itself.
 
};
 

	
 
/** What is the status of our acceleration? */
 
enum AccelStatus {
 
	AS_ACCEL, ///< We want to go faster, if possible ofcourse
 
	AS_BRAKE  ///< We want to stop
 
};
 

	
 
/**
 
 * 'Train' is either a loco or a wagon.
 
 */
 
struct Train : public GroundVehicle<Train, VEH_TRAIN> {
 
	TrainCache tcache;
 

	
 
@@ -483,15 +460,15 @@ protected: // These functions should not
 
	FORCEINLINE int32 GetSlopeResistance() const
 
	{
 
		int32 incl = 0;
 

	
 
		for (const Train *u = this; u != NULL; u = u->Next()) {
 
			if (HasBit(u->flags, VRF_GOINGUP)) {
 
				incl += u->tcache.cached_slope_resistance;
 
				incl += u->acc_cache.cached_slope_resistance;
 
			} else if (HasBit(u->flags, VRF_GOINGDOWN)) {
 
				incl -= u->tcache.cached_slope_resistance;
 
				incl -= u->acc_cache.cached_slope_resistance;
 
			}
 
		}
 

	
 
		return incl;
 
	}
 

	
src/train_cmd.cpp
Show inline comments
 
@@ -112,28 +112,28 @@ void Train::PowerChanged()
 

	
 
		/* 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;
 
	this->acc_cache.cached_axle_resistance = 60 * number_of_parts;
 
	this->acc_cache.cached_air_drag = 20 + 3 * number_of_parts;
 

	
 
	max_te *= 10000; // Tractive effort in (tonnes * 1000 * 10 =) N
 
	max_te /= 256;   // Tractive effort is a [0-255] coefficient
 
	if (this->tcache.cached_power != total_power || this->tcache.cached_max_te != max_te) {
 
	if (this->acc_cache.cached_power != total_power || this->acc_cache.cached_max_te != max_te) {
 
		/* Stop the vehicle if it has no power */
 
		if (total_power == 0) this->vehstatus |= VS_STOPPED;
 

	
 
		this->tcache.cached_power = total_power;
 
		this->tcache.cached_max_te = max_te;
 
		this->acc_cache.cached_power = total_power;
 
		this->acc_cache.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_track_speed = max_track_speed;
 
	this->acc_cache.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.
 
@@ -143,17 +143,17 @@ void Train::CargoChanged()
 
	assert(this->First() == this);
 
	uint32 weight = 0;
 

	
 
	for (Train *u = this; u != NULL; u = u->Next()) {
 
		uint32 current_weight = u->GetWeight();
 
		weight += current_weight;
 
		u->tcache.cached_slope_resistance = current_weight * u->GetSlopeSteepness();
 
		u->acc_cache.cached_slope_resistance = current_weight * u->GetSlopeSteepness();
 
	}
 

	
 
	/* store consist weight in cache */
 
	this->tcache.cached_weight = weight;
 
	this->acc_cache.cached_weight = weight;
 

	
 
	/* Now update train power (tractive effort is dependent on weight) */
 
	this->PowerChanged();
 
}
 

	
 

	
 
@@ -495,51 +495,51 @@ 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_track_speed);
 
	return min(max_speed, this->acc_cache.cached_max_track_speed);
 
}
 

	
 
/**
 
 * Calculates the acceleration of the vehicle under its current conditions.
 
 * @return Current acceleration of the vehicle.
 
 */
 

	
 
int Train::GetAcceleration() const
 
{
 
	int32 speed = this->GetCurrentSpeed();
 

	
 
	/* Weight is stored in tonnes */
 
	int32 mass = this->tcache.cached_weight;
 
	int32 mass = this->acc_cache.cached_weight;
 

	
 
	/* Power is stored in HP, we need it in watts. */
 
	int32 power = this->tcache.cached_power * 746;
 
	int32 power = this->acc_cache.cached_power * 746;
 

	
 
	int32 resistance = 0;
 

	
 
	bool maglev = this->GetAccelerationType() == 2;
 

	
 
	const int area = 120;
 
	if (!maglev) {
 
		resistance = (13 * mass) / 10;
 
		resistance += this->tcache.cached_axle_resistance;
 
		resistance += this->acc_cache.cached_axle_resistance;
 
		resistance += (this->GetRollingFriction() * mass * speed) / 1000;
 
		resistance += (area * this->tcache.cached_air_drag * speed * speed) / 10000;
 
		resistance += (area * this->acc_cache.cached_air_drag * speed * speed) / 10000;
 
	} else {
 
		resistance += (area * this->tcache.cached_air_drag * speed * speed) / 20000;
 
		resistance += (area * this->acc_cache.cached_air_drag * speed * speed) / 20000;
 
	}
 

	
 
	resistance += this->GetSlopeResistance();
 
	resistance *= 4; //[N]
 

	
 
	/* This value allows to know if the vehicle is accelerating or braking. */
 
	AccelStatus mode = this->GetAccelerationStatus();
 

	
 
	const int max_te = this->tcache.cached_max_te; // [N]
 
	const int max_te = this->acc_cache.cached_max_te; // [N]
 
	int force;
 
	if (speed > 0) {
 
		if (!maglev) {
 
			force = power / speed; //[N]
 
			force *= 22;
 
			force /= 10;
 
@@ -561,16 +561,16 @@ int Train::GetAcceleration() const
 
}
 

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

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

	
 
	uint power = this->tcache.cached_power;
 
	uint weight = this->tcache.cached_weight;
 
	this->max_speed = this->acc_cache.cached_max_track_speed;
 

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

	
 
/**
 
 * Get the width of a train vehicle image in the GUI.
 
@@ -2262,13 +2262,13 @@ static bool CheckTrainStayInDepot(Train 
 
	/* bail out if not all wagons are in the same depot or not in a depot at all */
 
	for (const Train *u = v; u != NULL; u = u->Next()) {
 
		if (u->track != TRACK_BIT_DEPOT || u->tile != v->tile) return false;
 
	}
 

	
 
	/* if the train got no power, then keep it in the depot */
 
	if (v->tcache.cached_power == 0) {
 
	if (v->acc_cache.cached_power == 0) {
 
		v->vehstatus |= VS_STOPPED;
 
		SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
 
		return true;
 
	}
 

	
 
	SigSegState seg_state;
src/vehicle_cmd.cpp
Show inline comments
 
@@ -75,13 +75,13 @@ CommandCost CmdStartStopVehicle(TileInde
 

	
 
	Vehicle *v = Vehicle::GetIfValid(p1);
 
	if (v == NULL || !CheckOwnership(v->owner) || !v->IsPrimaryVehicle()) return CMD_ERROR;
 

	
 
	switch (v->type) {
 
		case VEH_TRAIN:
 
			if ((v->vehstatus & VS_STOPPED) && Train::From(v)->tcache.cached_power == 0) return_cmd_error(STR_ERROR_TRAIN_START_NO_CATENARY);
 
			if ((v->vehstatus & VS_STOPPED) && Train::From(v)->acc_cache.cached_power == 0) return_cmd_error(STR_ERROR_TRAIN_START_NO_CATENARY);
 
			break;
 

	
 
		case VEH_SHIP:
 
		case VEH_ROAD:
 
			break;
 

	
src/vehicle_gui.cpp
Show inline comments
 
@@ -1522,15 +1522,15 @@ struct VehicleDetailsWindow : Window {
 
				y += FONT_HEIGHT_NORMAL;
 

	
 
				/* Draw max speed */
 
				switch (v->type) {
 
					case VEH_TRAIN:
 
						SetDParam(2, v->GetDisplayMaxSpeed());
 
						SetDParam(1, Train::From(v)->tcache.cached_power);
 
						SetDParam(0, Train::From(v)->tcache.cached_weight);
 
						SetDParam(3, Train::From(v)->tcache.cached_max_te / 1000);
 
						SetDParam(1, Train::From(v)->acc_cache.cached_power);
 
						SetDParam(0, Train::From(v)->acc_cache.cached_weight);
 
						SetDParam(3, Train::From(v)->acc_cache.cached_max_te / 1000);
 
						DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL && GetRailTypeInfo(Train::From(v)->railtype)->acceleration_type != 2) ?
 
								STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED_MAX_TE : STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED);
 
						break;
 

	
 
					case VEH_ROAD:
 
					case VEH_SHIP:
 
@@ -1960,13 +1960,13 @@ public:
 
			str = STR_VEHICLE_STATUS_CRASHED;
 
		} else if (v->type != VEH_AIRCRAFT && v->breakdown_ctr == 1) { // check for aircraft necessary?
 
			str = STR_VEHICLE_STATUS_BROKEN_DOWN;
 
		} else if (v->vehstatus & VS_STOPPED) {
 
			if (v->type == VEH_TRAIN) {
 
				if (v->cur_speed == 0) {
 
					if (Train::From(v)->tcache.cached_power == 0) {
 
					if (Train::From(v)->acc_cache.cached_power == 0) {
 
						str = STR_VEHICLE_STATUS_TRAIN_NO_POWER;
 
					} else {
 
						str = STR_VEHICLE_STATUS_STOPPED;
 
					}
 
				} else {
 
					SetDParam(0, v->GetDisplaySpeed());
0 comments (0 inline, 0 general)