4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file ground_vehicle.cpp Implementation of GroundVehicle. */
15 #include "vehicle_gui.h"
16 #include "window_func.h"
19 * Recalculates the cached total power of a vehicle. Should be called when the consist is changed.
21 template <class T
, VehicleType Type
>
22 void GroundVehicle
<T
, Type
>::PowerChanged()
24 assert(this->First() == this);
25 const T
*v
= T::From(this);
27 uint32 total_power
= 0;
29 uint32 number_of_parts
= 0;
30 uint16 max_track_speed
= v
->GetDisplayMaxSpeed();
32 for (const T
*u
= v
; u
!= NULL
; u
= u
->Next()) {
33 uint32 current_power
= u
->GetPower() + u
->GetPoweredPartPower(u
);
34 total_power
+= current_power
;
36 /* Only powered parts add tractive effort. */
37 if (current_power
> 0) max_te
+= u
->GetWeight() * u
->GetTractiveEffort();
40 /* Get minimum max speed for this track. */
41 uint16 track_speed
= u
->GetMaxTrackSpeed();
42 if (track_speed
> 0) max_track_speed
= min(max_track_speed
, track_speed
);
46 byte air_drag_value
= v
->GetAirDrag();
48 /* If air drag is set to zero (default), the resulting air drag coefficient is dependent on max speed. */
49 if (air_drag_value
== 0) {
50 uint16 max_speed
= v
->GetDisplayMaxSpeed();
51 /* Simplification of the method used in TTDPatch. It uses <= 10 to change more steadily from 128 to 196. */
52 air_drag
= (max_speed
<= 10) ? 192 : max(2048 / max_speed
, 1);
54 /* According to the specs, a value of 0x01 in the air drag property means "no air drag". */
55 air_drag
= (air_drag_value
== 1) ? 0 : air_drag_value
;
58 this->gcache
.cached_air_drag
= air_drag
+ 3 * air_drag
* number_of_parts
/ 20;
60 max_te
*= 10000; // Tractive effort in (tonnes * 1000 * 10 =) N.
61 max_te
/= 256; // Tractive effort is a [0-255] coefficient.
62 if (this->gcache
.cached_power
!= total_power
|| this->gcache
.cached_max_te
!= max_te
) {
63 /* Stop the vehicle if it has no power. */
64 if (total_power
== 0) this->vehstatus
|= VS_STOPPED
;
66 this->gcache
.cached_power
= total_power
;
67 this->gcache
.cached_max_te
= max_te
;
68 SetWindowDirty(WC_VEHICLE_DETAILS
, this->index
);
69 SetWindowWidgetDirty(WC_VEHICLE_VIEW
, this->index
, VVW_WIDGET_START_STOP_VEH
);
72 this->gcache
.cached_max_track_speed
= max_track_speed
;
76 * Recalculates the cached weight of a vehicle and its parts. Should be called each time the cargo on
77 * the consist changes.
79 template <class T
, VehicleType Type
>
80 void GroundVehicle
<T
, Type
>::CargoChanged()
82 assert(this->First() == this);
85 for (T
*u
= T::From(this); u
!= NULL
; u
= u
->Next()) {
86 uint32 current_weight
= u
->GetWeight();
87 weight
+= current_weight
;
88 /* Slope steepness is in percent, result in N. */
89 u
->gcache
.cached_slope_resistance
= current_weight
* u
->GetSlopeSteepness() * 100;
92 /* Store consist weight in cache. */
93 this->gcache
.cached_weight
= max
<uint32
>(1, weight
);
94 /* Friction in bearings and other mechanical parts is 0.1% of the weight (result in N). */
95 this->gcache
.cached_axle_resistance
= 10 * weight
;
97 /* Now update vehicle power (tractive effort is dependent on weight). */
102 * Calculates the acceleration of the vehicle under its current conditions.
103 * @return Current acceleration of the vehicle.
105 template <class T
, VehicleType Type
>
106 int GroundVehicle
<T
, Type
>::GetAcceleration() const
108 /* Templated class used for function calls for performance reasons. */
109 const T
*v
= T::From(this);
110 int32 speed
= v
->GetCurrentSpeed(); // [km/h-ish]
112 /* Weight is stored in tonnes. */
113 int32 mass
= this->gcache
.cached_weight
;
115 /* Power is stored in HP, we need it in watts. */
116 int32 power
= this->gcache
.cached_power
* 746;
118 int32 resistance
= 0;
120 bool maglev
= v
->GetAccelerationType() == 2;
122 const int area
= v
->GetAirDragArea();
124 /* Static resistance plus rolling friction. */
125 resistance
= this->gcache
.cached_axle_resistance
;
126 resistance
+= mass
* v
->GetRollingFriction();
128 /* Air drag; the air drag coefficient is in an arbitrary NewGRF-unit,
129 * so we need some magic conversion factor. */
130 resistance
+= (area
* this->gcache
.cached_air_drag
* speed
* speed
) / 1000;
132 resistance
+= this->GetSlopeResistance();
134 /* This value allows to know if the vehicle is accelerating or braking. */
135 AccelStatus mode
= v
->GetAccelerationStatus();
137 const int max_te
= this->gcache
.cached_max_te
; // [N]
141 /* Conversion factor from km/h to m/s is 5/18 to get [N] in the end. */
142 force
= power
* 18 / (speed
* 5);
143 if (mode
== AS_ACCEL
&& force
> max_te
) force
= max_te
;
148 /* "Kickoff" acceleration. */
149 force
= (mode
== AS_ACCEL
&& !maglev
) ? min(max_te
, power
) : power
;
150 force
= max(force
, (mass
* 8) + resistance
);
153 if (mode
== AS_ACCEL
) {
154 /* Easy way out when there is no acceleration. */
155 if (force
== resistance
) return 0;
157 /* When we accelerate, make sure we always keep doing that, even when
158 * the excess force is more than the mass. Otherwise a vehicle going
159 * down hill will never slow down enough, and a vehicle that came up
160 * a hill will never speed up enough to (eventually) get back to the
161 * same (maximum) speed. */
162 int accel
= (force
- resistance
) / (mass
* 4);
163 return force
< resistance
? min(-1, accel
) : max(1, accel
);
165 return min(-force
- resistance
, -10000) / mass
;
169 /* Instantiation for Train */
170 template struct GroundVehicle
<Train
, VEH_TRAIN
>;
171 /* Instantiation for RoadVehicle */
172 template struct GroundVehicle
<RoadVehicle
, VEH_ROAD
>;