Rearrange storage of reserved tracks for railway tiles
[openttd/fttd.git] / src / ground_vehicle.cpp
blob84e75ff2b421d4b78d87ebc9327a49fee379d8c5
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file ground_vehicle.cpp Implementation of GroundVehicle. */
12 #include "stdafx.h"
13 #include "train.h"
14 #include "roadveh.h"
15 #include "depot_func.h"
17 /**
18 * Recalculates the cached total power of a vehicle. Should be called when the consist is changed.
20 template <class T, VehicleType Type>
21 void GroundVehicle<T, Type>::PowerChanged()
23 assert(this->First() == this);
24 const T *v = T::From(this);
26 uint32 total_power = 0;
27 uint32 max_te = 0;
28 uint32 number_of_parts = 0;
29 uint16 max_track_speed = v->GetDisplayMaxSpeed();
31 for (const T *u = v; u != NULL; u = u->Next()) {
32 uint32 current_power = u->GetPower() + u->GetPoweredPartPower(u);
33 total_power += current_power;
35 /* Only powered parts add tractive effort. */
36 if (current_power > 0) max_te += u->GetWeight() * u->GetTractiveEffort();
37 number_of_parts++;
39 /* Get minimum max speed for this track. */
40 uint16 track_speed = u->GetMaxTrackSpeed();
41 if (track_speed > 0) max_track_speed = min(max_track_speed, track_speed);
44 byte air_drag;
45 byte air_drag_value = v->GetAirDrag();
47 /* If air drag is set to zero (default), the resulting air drag coefficient is dependent on max speed. */
48 if (air_drag_value == 0) {
49 uint16 max_speed = v->GetDisplayMaxSpeed();
50 /* Simplification of the method used in TTDPatch. It uses <= 10 to change more steadily from 128 to 196. */
51 air_drag = (max_speed <= 10) ? 192 : max(2048 / max_speed, 1);
52 } else {
53 /* According to the specs, a value of 0x01 in the air drag property means "no air drag". */
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;
59 max_te *= 10000; // Tractive effort in (tonnes * 1000 * 10 =) N.
60 max_te /= 256; // Tractive effort is a [0-255] coefficient.
61 if (this->gcache.cached_power != total_power || this->gcache.cached_max_te != max_te) {
62 /* Stop the vehicle if it has no power. */
63 if (total_power == 0) this->vehstatus |= VS_STOPPED;
65 this->gcache.cached_power = total_power;
66 this->gcache.cached_max_te = max_te;
67 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
68 SetWindowWidgetDirty(WC_VEHICLE_VIEW, this->index, WID_VV_START_STOP);
71 this->gcache.cached_max_track_speed = max_track_speed;
74 /**
75 * Recalculates the cached weight of a vehicle and its parts. Should be called each time the cargo on
76 * the consist changes.
78 template <class T, VehicleType Type>
79 void GroundVehicle<T, Type>::CargoChanged()
81 assert(this->First() == this);
82 uint32 weight = 0;
84 for (T *u = T::From(this); u != NULL; u = u->Next()) {
85 uint32 current_weight = u->GetWeight();
86 weight += current_weight;
87 /* Slope steepness is in percent, result in N. */
88 u->gcache.cached_slope_resistance = current_weight * u->GetSlopeSteepness() * 100;
91 /* Store consist weight in cache. */
92 this->gcache.cached_weight = max<uint32>(1, weight);
93 /* Friction in bearings and other mechanical parts is 0.1% of the weight (result in N). */
94 this->gcache.cached_axle_resistance = 10 * weight;
96 /* Now update vehicle power (tractive effort is dependent on weight). */
97 this->PowerChanged();
101 * Calculates the acceleration of the vehicle under its current conditions.
102 * @return Current acceleration of the vehicle.
104 template <class T, VehicleType Type>
105 int GroundVehicle<T, Type>::GetAcceleration() const
107 /* Templated class used for function calls for performance reasons. */
108 const T *v = T::From(this);
109 int32 speed = v->GetCurrentSpeed(); // [km/h-ish]
111 /* Weight is stored in tonnes. */
112 int32 mass = this->gcache.cached_weight;
114 /* Power is stored in HP, we need it in watts. */
115 int32 power = this->gcache.cached_power * 746;
117 int32 resistance = 0;
119 bool maglev = v->GetAccelerationType() == 2;
121 const int area = v->GetAirDragArea();
122 if (!maglev) {
123 /* Static resistance plus rolling friction. */
124 resistance = this->gcache.cached_axle_resistance;
125 resistance += mass * v->GetRollingFriction();
127 /* Air drag; the air drag coefficient is in an arbitrary NewGRF-unit,
128 * so we need some magic conversion factor. */
129 resistance += (area * this->gcache.cached_air_drag * speed * speed) / 1000;
131 resistance += this->GetSlopeResistance();
133 /* This value allows to know if the vehicle is accelerating or braking. */
134 AccelStatus mode = v->GetAccelerationStatus();
136 const int max_te = this->gcache.cached_max_te; // [N]
137 int force;
138 if (speed > 0) {
139 if (!maglev) {
140 /* Conversion factor from km/h to m/s is 5/18 to get [N] in the end. */
141 force = power * 18 / (speed * 5);
142 if (mode == AS_ACCEL && force > max_te) force = max_te;
143 } else {
144 force = power / 25;
146 } else {
147 /* "Kickoff" acceleration. */
148 force = (mode == AS_ACCEL && !maglev) ? min(max_te, power) : power;
149 force = max(force, (mass * 8) + resistance);
152 if (mode == AS_ACCEL) {
153 /* Easy way out when there is no acceleration. */
154 if (force == resistance) return 0;
156 /* When we accelerate, make sure we always keep doing that, even when
157 * the excess force is more than the mass. Otherwise a vehicle going
158 * down hill will never slow down enough, and a vehicle that came up
159 * a hill will never speed up enough to (eventually) get back to the
160 * same (maximum) speed. */
161 int accel = (force - resistance) / (mass * 4);
162 return force < resistance ? min(-1, accel) : max(1, accel);
163 } else {
164 return min(-force - resistance, -10000) / mass;
169 * Check whether the whole vehicle chain is in the depot.
170 * @return true if and only if the whole chain is in the depot.
172 template <class T, VehicleType Type>
173 bool GroundVehicle<T, Type>::IsChainInDepot() const
175 const T *v = this->First();
176 /* Is the front engine stationary in the depot? */
177 assert_compile((int)TRANSPORT_RAIL == (int)VEH_TRAIN);
178 assert_compile((int)TRANSPORT_ROAD == (int)VEH_ROAD);
179 if (!IsDepotTypeTile(v->tile, (TransportType)Type) || v->cur_speed != 0) return false;
181 /* Check whether the rest is also already trying to enter the depot. */
182 for (; v != NULL; v = v->Next()) {
183 if (!v->T::IsInDepot() || v->tile != this->tile) return false;
186 return true;
189 /* Instantiation for Train */
190 template struct GroundVehicle<Train, VEH_TRAIN>;
191 /* Instantiation for RoadVehicle */
192 template struct GroundVehicle<RoadVehicle, VEH_ROAD>;