mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-02-04 05:15:21 +00:00
(svn r19338) -Codechange: Move the acceleration cache to GroundVehicle.
This commit is contained in:
parent
f428102092
commit
e4a5a556b4
@ -14,11 +14,35 @@
|
||||
|
||||
#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.
|
||||
|
@ -778,10 +778,10 @@ static uint32 VehicleGetVariable(const ResolverObject *object, byte variable, by
|
||||
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
|
||||
|
29
src/train.h
29
src/train.h
@ -56,25 +56,8 @@ bool TryPathReserve(Train *v, bool mark_as_stuck = false, bool first_tile_okay =
|
||||
|
||||
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;
|
||||
|
||||
@ -102,12 +85,6 @@ struct TrainCache : public AccelerationCache {
|
||||
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.
|
||||
*/
|
||||
@ -486,9 +463,9 @@ protected: // These functions should not be called outside acceleration code.
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -115,22 +115,22 @@ void Train::PowerChanged()
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@ -146,11 +146,11 @@ void Train::CargoChanged()
|
||||
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();
|
||||
@ -498,7 +498,7 @@ int Train::GetCurrentMaxSpeed() const
|
||||
}
|
||||
}
|
||||
|
||||
return min(max_speed, this->tcache.cached_max_track_speed);
|
||||
return min(max_speed, this->acc_cache.cached_max_track_speed);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -511,10 +511,10 @@ 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;
|
||||
|
||||
@ -523,11 +523,11 @@ int Train::GetAcceleration() const
|
||||
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();
|
||||
@ -536,7 +536,7 @@ int Train::GetAcceleration() const
|
||||
/* 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) {
|
||||
@ -564,10 +564,10 @@ void Train::UpdateAcceleration()
|
||||
{
|
||||
assert(this->IsFrontEngine());
|
||||
|
||||
this->max_speed = this->tcache.cached_max_track_speed;
|
||||
this->max_speed = this->acc_cache.cached_max_track_speed;
|
||||
|
||||
uint power = this->tcache.cached_power;
|
||||
uint weight = this->tcache.cached_weight;
|
||||
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);
|
||||
}
|
||||
@ -2265,7 +2265,7 @@ static bool CheckTrainStayInDepot(Train *v)
|
||||
}
|
||||
|
||||
/* 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;
|
||||
|
@ -78,7 +78,7 @@ CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1,
|
||||
|
||||
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:
|
||||
|
@ -1525,9 +1525,9 @@ struct VehicleDetailsWindow : Window {
|
||||
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;
|
||||
@ -1963,7 +1963,7 @@ public:
|
||||
} 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;
|
||||
|
Loading…
Reference in New Issue
Block a user