Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / ground_vehicle.cpp
blob138dfa5fd089e80abea0d068fc1b8796770aee4c
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_map.h"
16 #include "tunnel_base.h"
17 #include "slope_type.h"
19 #include "safeguards.h"
21 /**
22 * Recalculates the cached total power of a vehicle. Should be called when the consist is changed.
24 template <class T, VehicleType Type>
25 void GroundVehicle<T, Type>::PowerChanged()
27 assert(this->First() == this);
28 const T *v = T::From(this);
30 uint32 total_power = 0;
31 uint32 max_te = 0;
32 uint32 number_of_parts = 0;
33 uint16 max_track_speed = v->GetDisplayMaxSpeed();
35 for (const T *u = v; u != NULL; u = u->Next()) {
36 uint32 current_power = u->GetPower() + u->GetPoweredPartPower(u);
37 total_power += current_power;
39 /* Only powered parts add tractive effort. */
40 if (current_power > 0) max_te += u->GetWeight() * u->GetTractiveEffort();
41 number_of_parts++;
43 /* Get minimum max speed for this track. */
44 uint16 track_speed = u->GetMaxTrackSpeed();
45 if (track_speed > 0) max_track_speed = min(max_track_speed, track_speed);
48 byte air_drag;
49 byte air_drag_value = v->GetAirDrag();
51 /* If air drag is set to zero (default), the resulting air drag coefficient is dependent on max speed. */
52 if (air_drag_value == 0) {
53 uint16 max_speed = v->GetDisplayMaxSpeed();
54 /* Simplification of the method used in TTDPatch. It uses <= 10 to change more steadily from 128 to 196. */
55 air_drag = (max_speed <= 10) ? 192 : max(2048 / max_speed, 1);
56 } else {
57 /* According to the specs, a value of 0x01 in the air drag property means "no air drag". */
58 air_drag = (air_drag_value == 1) ? 0 : air_drag_value;
61 this->gcache.cached_air_drag = air_drag + 3 * air_drag * number_of_parts / 20;
63 max_te *= 10000; // Tractive effort in (tonnes * 1000 * 10 =) N.
64 max_te /= 256; // Tractive effort is a [0-255] coefficient.
65 if (this->gcache.cached_power != total_power || this->gcache.cached_max_te != max_te) {
66 /* Stop the vehicle if it has no power. */
67 if (total_power == 0)
68 this->vehstatus |= VS_STOPPED;
70 this->gcache.cached_power = total_power;
71 this->gcache.cached_max_te = max_te;
72 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
73 SetWindowWidgetDirty(WC_VEHICLE_VIEW, this->index, WID_VV_START_STOP);
76 this->gcache.cached_max_track_speed = max_track_speed;
79 /**
80 * Recalculates the cached weight of a vehicle and its parts. Should be called each time the cargo on
81 * the consist changes.
83 template <class T, VehicleType Type>
84 void GroundVehicle<T, Type>::CargoChanged()
86 assert(this->First() == this);
87 uint32 weight = 0;
89 for (T *u = T::From(this); u != NULL; u = u->Next()) {
90 uint32 current_weight = u->GetWeight();
91 weight += current_weight;
92 /* Slope steepness is in percent, result in N. */
93 u->gcache.cached_slope_resistance = current_weight * u->GetSlopeSteepness() * 100;
96 /* Store consist weight in cache. */
97 this->gcache.cached_weight = max<uint32>(1, weight);
99 /* Now update vehicle power (tractive effort is dependent on weight). */
100 this->PowerChanged();
104 * Calculates the acceleration of the vehicle under its current conditions.
105 * @return Current acceleration of the vehicle.
107 template <class T, VehicleType Type>
108 int GroundVehicle<T, Type>::GetAcceleration() const
110 /* Templated class used for function calls for performance reasons. */
111 const T *v = T::From(this);
113 int64 speed = (v->GetCurrentSpeed() * 5) / 18; // [m/s-ish]
115 /* Weight is stored in tonnes. */
116 int32 mass = this->gcache.cached_weight;
118 /* Power is stored in HP, we need it in watts.
119 * Each vehicle can have U16 power, 128 vehicles, HP -> watt
120 * and km/h to m/s conversion below result in a maximum of
121 * about 1.1E11, way more than 4.3E9 of int32. */
122 int64 power = this->gcache.cached_power * 746ll;
124 /* This is constructed from:
125 * - axle resistance: U16 power * 10 for 128 vehicles.
126 * * 8.3E7
127 * - rolling friction: U16 power * 144 for 128 vehicles.
128 * * 1.2E9
129 * - slope resistance: U16 weight * 100 * 10 (steepness) for 128 vehicles.
130 * * 8.4E9
131 * - air drag: 28 * (U8 drag + 3 * U8 drag * 128 vehicles / 20) * U16 speed * U16 speed
132 * * 6.2E14 before dividing by 1000
133 * Sum is 6.3E11, more than 4.3E9 of int32, so int64 is needed.
135 int64 resistance = 0;
137 bool maglev = v->GetAccelerationType() == 2;
139 const int area = v->GetAirDragArea();
140 if (!maglev) {
141 resistance += mass * v->GetRollingFriction();
144 /* Air drag; the air drag coefficient is in an arbitrary NewGRF-unit,
145 * so we need some magic conversion factor. */
146 resistance += (area * this->gcache.cached_air_drag * speed * speed) / 1000;
148 resistance += this->GetSlopeResistance();
150 /* This value allows to know if the vehicle is accelerating or braking. */
151 AccelStatus mode = v->GetAccelerationStatus();
153 int max_te = this->gcache.cached_max_te; // [N]
155 /* Constructed from power, with need to multiply by 18 and assuming
156 * low speed, it needs to be a 64 bit integer too. */
157 int64 force;
158 if (speed > 0) {
159 if (!maglev) {
160 force = power / speed;
161 if (mode == AS_ACCEL && force > max_te) force = max_te;
162 } else {
163 force = power / 25;
165 } else {
166 /* "Kickoff" acceleration. */
167 force = (mode == AS_ACCEL && !maglev) ? min(max_te, power) : power;
168 force = max(force, (mass * 8) + resistance);
171 if (mode == AS_ACCEL) {
172 /* Easy way out when there is no acceleration. */
173 if (force == resistance) return 0;
175 /* When we accelerate, make sure we always keep doing that, even when
176 * the excess force is more than the mass. Otherwise a vehicle going
177 * down hill will never slow down enough, and a vehicle that came up
178 * a hill will never speed up enough to (eventually) get back to the
179 * same (maximum) speed. */
180 int accel = ClampToI32((force - resistance) / mass);
182 return force < resistance ? min(-1, accel) : max(1, accel);
183 } else {
184 return ClampToI32(min(-force - resistance, -10000) / mass);
189 * Check whether the whole vehicle chain is in the depot.
190 * @return true if and only if the whole chain is in the depot.
192 template <class T, VehicleType Type>
193 bool GroundVehicle<T, Type>::IsChainInDepot() const
195 const T *v = this->First();
196 /* Is the front engine stationary in the depot? */
197 assert_compile((int)TRANSPORT_RAIL == (int)VEH_TRAIN);
198 assert_compile((int)TRANSPORT_ROAD == (int)VEH_ROAD);
199 if (!IsDepotTypeTile(v->tile, (TransportType)Type) || v->cur_speed != 0) return false;
201 /* Check whether the rest is also already trying to enter the depot. */
202 for (; v != NULL; v = v->Next()) {
203 if (!v->T::IsInDepot() || v->tile != this->tile) return false;
206 return true;
210 * Updates vehicle's Z inclination inside a wormhole, where applicable.
212 template <class T, VehicleType Type>
213 void GroundVehicle<T, Type>::UpdateZPositionInWormhole()
215 if (!IsTunnel(this->tile)) return;
217 const Tunnel *t = Tunnel::GetByTile(this->tile);
218 if (!t->is_chunnel) return;
220 TileIndex pos_tile = TileVirtXY(this->x_pos, this->y_pos);
222 ClrBit(this->gv_flags, GVF_GOINGUP_BIT);
223 ClrBit(this->gv_flags, GVF_GOINGDOWN_BIT);
225 if (pos_tile == t->tile_n || pos_tile == t->tile_s) {
226 this->z_pos = 0;
227 return;
230 int north_coord, south_coord, pos_coord;
231 bool going_north;
232 Slope slope_north;
233 if (t->tile_s - t->tile_n > MapMaxX()) {
234 // tunnel extends along Y axis (DIAGDIR_SE from north end), has same X values
235 north_coord = TileY(t->tile_n);
236 south_coord = TileY(t->tile_s);
237 pos_coord = TileY(pos_tile);
238 going_north = (this->direction == DIR_NW);
239 slope_north = SLOPE_NW;
240 } else {
241 // tunnel extends along X axis (DIAGDIR_SW from north end), has same Y values
242 north_coord = TileX(t->tile_n);
243 south_coord = TileX(t->tile_s);
244 pos_coord = TileX(pos_tile);
245 going_north = (this->direction == DIR_NE);
246 slope_north = SLOPE_NE;
249 Slope slope = SLOPE_FLAT;
251 int delta;
252 if ((delta = pos_coord - north_coord) <= 3) {
253 this->z_pos = TILE_HEIGHT * (delta == 3 ? -2 : -1);
254 if (delta != 2) {
255 slope = slope_north;
256 SetBit(this->gv_flags, going_north ? GVF_GOINGUP_BIT : GVF_GOINGDOWN_BIT);
258 } else if ((delta = south_coord - pos_coord) <= 3) {
259 this->z_pos = TILE_HEIGHT * (delta == 3 ? -2 : -1);
260 if (delta != 2) {
261 slope = SLOPE_ELEVATED ^ slope_north;
262 SetBit(this->gv_flags, going_north ? GVF_GOINGDOWN_BIT : GVF_GOINGUP_BIT);
266 if (slope != SLOPE_FLAT) this->z_pos += GetPartialPixelZ(this->x_pos & 0xF, this->y_pos & 0xF, slope);
269 /* Instantiation for Train */
270 template struct GroundVehicle<Train, VEH_TRAIN>;
271 /* Instantiation for RoadVehicle */
272 template struct GroundVehicle<RoadVehicle, VEH_ROAD>;