20 template <
class T, VehicleType Type>
23 assert(this->First() ==
this);
24 const T *v = T::From(
this);
26 uint32 total_power = 0;
28 uint32 number_of_parts = 0;
29 uint16 max_track_speed = this->vcache.cached_max_speed;
31 for (
const T *u = v; u !=
nullptr; u = u->Next()) {
32 uint32 current_power = u->GetPower() + u->GetPoweredPartPower(u);
33 total_power += current_power;
36 if (current_power > 0) max_te += u->GetWeight() * u->GetTractiveEffort();
40 uint16 track_speed = u->GetMaxTrackSpeed();
41 if (track_speed > 0) max_track_speed =
min(max_track_speed, track_speed);
45 byte air_drag_value = v->GetAirDrag();
48 if (air_drag_value == 0) {
49 uint16 max_speed = v->GetDisplayMaxSpeed();
51 air_drag = (max_speed <= 10) ? 192 :
max(2048 / max_speed, 1);
54 air_drag = (air_drag_value == 1) ? 0 : air_drag_value;
57 this->gcache.cached_air_drag = air_drag + 3 * air_drag * number_of_parts / 20;
61 if (this->gcache.cached_power != total_power || this->gcache.cached_max_te != max_te) {
63 if (total_power == 0) this->vehstatus |=
VS_STOPPED;
65 this->gcache.cached_power = total_power;
66 this->gcache.cached_max_te = max_te;
71 this->gcache.cached_max_track_speed = max_track_speed;
78 template <
class T, VehicleType Type>
81 assert(this->First() ==
this);
84 for (T *u = T::From(
this); u !=
nullptr; u = u->Next()) {
85 uint32 current_weight = u->GetWeight();
86 weight += current_weight;
88 u->gcache.cached_slope_resistance = current_weight * u->GetSlopeSteepness() * 100;
92 this->gcache.cached_weight = max<uint32>(1, weight);
94 this->gcache.cached_axle_resistance = 10 * weight;
104 template <
class T, VehicleType Type>
108 const T *v = T::From(
this);
110 int64 speed = v->GetCurrentSpeed();
113 int32 mass = this->gcache.cached_weight;
119 int64 power = this->gcache.cached_power * 746ll;
132 int64 resistance = 0;
134 bool maglev = v->GetAccelerationType() == 2;
136 const int area = v->GetAirDragArea();
139 resistance = this->gcache.cached_axle_resistance;
140 resistance += mass * v->GetRollingFriction();
144 resistance += (area * this->gcache.cached_air_drag * speed * speed) / 1000;
146 resistance += this->GetSlopeResistance();
151 const int max_te = this->gcache.cached_max_te;
158 force = power * 18 / (speed * 5);
159 if (mode ==
AS_ACCEL && force > max_te) force = max_te;
165 force = (mode ==
AS_ACCEL && !maglev) ?
min(max_te, power) : power;
166 force =
max(force, (mass * 8) + resistance);
171 if (force == resistance)
return 0;
178 int accel =
ClampToI32((force - resistance) / (mass * 4));
179 return force < resistance ?
min(-1, accel) :
max(1, accel);
189 template <
class T, VehicleType Type>
192 const T *v = this->First();
199 for (; v !=
nullptr; v = v->Next()) {
200 if (!v->T::IsInDepot() || v->tile != this->tile)
return false;
Vehicle is stopped by the player.
int GetAcceleration() const
Calculates the acceleration of the vehicle under its current conditions.
AccelStatus
What is the status of our acceleration?
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Base for the train class.
static const int GROUND_ACCELERATION
Acceleration due to gravity, 9.8 m/s^2.
bool IsChainInDepot() const override
Check whether the whole vehicle chain is in the depot.
void CargoChanged()
Recalculates the cached weight of a vehicle and its parts.
static T max(const T a, const T b)
Returns the maximum of two values.
Map related accessors for depots.
static bool IsDepotTypeTile(TileIndex tile, TransportType type)
Check if a tile is a depot and it is a depot of the given type.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
void PowerChanged()
Recalculates the cached total power of a vehicle.
Vehicle view; Window numbers:
static T min(const T a, const T b)
Returns the minimum of two values.
We want to go faster, if possible of course.
static int32 ClampToI32(const int64 a)
Reduce a signed 64-bit int to a signed 32-bit one.
void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, byte widget_index)
Mark a particular widget in a particular window as dirty (in need of repainting)
TransportType
Available types of transport.
Transport by road vehicle.
Vehicle details; Window numbers: