# ground_vehicle.cpp

Go to the documentation of this file.

    00001 /* $Id: ground_vehicle.cpp 26714 2014-08-03 14:03:07Z frosch $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "train.h"
    00014 #include "roadveh.h"
    00015 #include "depot_map.h"
    00016
    00020 template <class T, VehicleType Type>
    00021 void GroundVehicle<T, Type>::PowerChanged()
    00022 {
    00023   assert(this->First() == this);
    00024   const T *v = T::From(this);
    00025
    00026   uint32 total_power = 0;
    00027   uint32 max_te = 0;
    00028   uint32 number_of_parts = 0;
    00029   uint16 max_track_speed = v->GetDisplayMaxSpeed();
    00030
    00031   for (const T *u = v; u != NULL; u = u->Next()) {
    00032     uint32 current_power = u->GetPower() + u->GetPoweredPartPower(u);
    00033     total_power += current_power;
    00034
    00035     /* Only powered parts add tractive effort. */
    00036     if (current_power > 0) max_te += u->GetWeight() * u->GetTractiveEffort();
    00037     number_of_parts++;
    00038
    00039     /* Get minimum max speed for this track. */
    00040     uint16 track_speed = u->GetMaxTrackSpeed();
    00041     if (track_speed > 0) max_track_speed = min(max_track_speed, track_speed);
    00042   }
    00043
    00044   byte air_drag;
    00045   byte air_drag_value = v->GetAirDrag();
    00046
    00047   /* If air drag is set to zero (default), the resulting air drag coefficient is dependent on max speed. */
    00048   if (air_drag_value == 0) {
    00049     uint16 max_speed = v->GetDisplayMaxSpeed();
    00050     /* Simplification of the method used in TTDPatch. It uses <= 10 to change more steadily from 128 to 196. */
    00051     air_drag = (max_speed <= 10) ? 192 : max(2048 / max_speed, 1);
    00052   } else {
    00053     /* According to the specs, a value of 0x01 in the air drag property means "no air drag". */
    00054     air_drag = (air_drag_value == 1) ? 0 : air_drag_value;
    00055   }
    00056
    00057   this->gcache.cached_air_drag = air_drag + 3 * air_drag * number_of_parts / 20;
    00058
    00059   max_te *= 10000; // Tractive effort in (tonnes * 1000 * 10 =) N.
    00060   max_te /= 256;   // Tractive effort is a [0-255] coefficient.
    00061   if (this->gcache.cached_power != total_power || this->gcache.cached_max_te != max_te) {
    00062     /* Stop the vehicle if it has no power. */
    00063     if (total_power == 0) this->vehstatus |= VS_STOPPED;
    00064
    00065     this->gcache.cached_power = total_power;
    00066     this->gcache.cached_max_te = max_te;
    00067     SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
    00068     SetWindowWidgetDirty(WC_VEHICLE_VIEW, this->index, WID_VV_START_STOP);
    00069   }
    00070
    00071   this->gcache.cached_max_track_speed = max_track_speed;
    00072 }
    00073
    00078 template <class T, VehicleType Type>
    00079 void GroundVehicle<T, Type>::CargoChanged()
    00080 {
    00081   assert(this->First() == this);
    00082   uint32 weight = 0;
    00083
    00084   for (T *u = T::From(this); u != NULL; u = u->Next()) {
    00085     uint32 current_weight = u->GetWeight();
    00086     weight += current_weight;
    00087     /* Slope steepness is in percent, result in N. */
    00088     u->gcache.cached_slope_resistance = current_weight * u->GetSlopeSteepness() * 100;
    00089   }
    00090
    00091   /* Store consist weight in cache. */
    00092   this->gcache.cached_weight = max<uint32>(1, weight);
    00093   /* Friction in bearings and other mechanical parts is 0.1% of the weight (result in N). */
    00094   this->gcache.cached_axle_resistance = 10 * weight;
    00095
    00096   /* Now update vehicle power (tractive effort is dependent on weight). */
    00097   this->PowerChanged();
    00098 }
    00099
    00104 template <class T, VehicleType Type>
    00105 int GroundVehicle<T, Type>::GetAcceleration() const
    00106 {
    00107   /* Templated class used for function calls for performance reasons. */
    00108   const T *v = T::From(this);
    00109   /* Speed is used squared later on, so U16 * U16, and then multiplied by other values. */
    00110   int64 speed = v->GetCurrentSpeed(); // [km/h-ish]
    00111
    00112   /* Weight is stored in tonnes. */
    00113   int32 mass = this->gcache.cached_weight;
    00114
    00115   /* Power is stored in HP, we need it in watts.
    00116    * Each vehicle can have U16 power, 128 vehicles, HP -> watt
    00117    * and km/h to m/s conversion below result in a maxium of
    00118    * about 1.1E11, way more than 4.3E9 of int32. */
    00119   int64 power = this->gcache.cached_power * 746ll;
    00120
    00121   /* This is constructed from:
    00122    *  - axle resistance:  U16 power * 10 for 128 vehicles.
    00123    *     * 8.3E7
    00124    *  - rolling friction: U16 power * 144 for 128 vehicles.
    00125    *     * 1.2E9
    00126    *  - slope resistance: U16 weight * 100 * 10 (steepness) for 128 vehicles.
    00127    *     * 8.4E9
    00128    *  - air drag: 28 * (U8 drag + 3 * U8 drag * 128 vehicles / 20) * U16 speed * U16 speed
    00129    *     * 6.2E14 before dividing by 1000
    00130    * Sum is 6.3E11, more than 4.3E9 of int32, so int64 is needed.
    00131    */
    00132   int64 resistance = 0;
    00133
    00134   bool maglev = v->GetAccelerationType() == 2;
    00135
    00136   const int area = v->GetAirDragArea();
    00137   if (!maglev) {
    00138     /* Static resistance plus rolling friction. */
    00139     resistance = this->gcache.cached_axle_resistance;
    00140     resistance += mass * v->GetRollingFriction();
    00141   }
    00142   /* Air drag; the air drag coefficient is in an arbitrary NewGRF-unit,
    00143    * so we need some magic conversion factor. */
    00144   resistance += (area * this->gcache.cached_air_drag * speed * speed) / 1000;
    00145
    00146   resistance += this->GetSlopeResistance();
    00147
    00148   /* This value allows to know if the vehicle is accelerating or braking. */
    00149   AccelStatus mode = v->GetAccelerationStatus();
    00150
    00151   const int max_te = this->gcache.cached_max_te; // [N]
    00152   /* Constructued from power, with need to multiply by 18 and assuming
    00153    * low speed, it needs to be a 64 bit integer too. */
    00154   int64 force;
    00155   if (speed > 0) {
    00156     if (!maglev) {
    00157       /* Conversion factor from km/h to m/s is 5/18 to get [N] in the end. */
    00158       force = power * 18 / (speed * 5);
    00159       if (mode == AS_ACCEL && force > max_te) force = max_te;
    00160     } else {
    00161       force = power / 25;
    00162     }
    00163   } else {
    00164     /* "Kickoff" acceleration. */
    00165     force = (mode == AS_ACCEL && !maglev) ? min(max_te, power) : power;
    00166     force = max(force, (mass * 8) + resistance);
    00167   }
    00168
    00169   if (mode == AS_ACCEL) {
    00170     /* Easy way out when there is no acceleration. */
    00171     if (force == resistance) return 0;
    00172
    00173     /* When we accelerate, make sure we always keep doing that, even when
    00174      * the excess force is more than the mass. Otherwise a vehicle going
    00175      * down hill will never slow down enough, and a vehicle that came up
    00176      * a hill will never speed up enough to (eventually) get back to the
    00177      * same (maximum) speed. */
    00178     int accel = ClampToI32((force - resistance) / (mass * 4));
    00179     return force < resistance ? min(-1, accel) : max(1, accel);
    00180   } else {
    00181     return ClampToI32(min(-force - resistance, -10000) / mass);
    00182   }
    00183 }
    00184
    00189 template <class T, VehicleType Type>
    00190 bool GroundVehicle<T, Type>::IsChainInDepot() const
    00191 {
    00192   const T *v = this->First();
    00193   /* Is the front engine stationary in the depot? */
    00194   assert_compile((int)TRANSPORT_RAIL == (int)VEH_TRAIN);
    00195   assert_compile((int)TRANSPORT_ROAD == (int)VEH_ROAD);
    00196   if (!IsDepotTypeTile(v->tile, (TransportType)Type) || v->cur_speed != 0) return false;
    00197
    00198   /* Check whether the rest is also already trying to enter the depot. */
    00199   for (; v != NULL; v = v->Next()) {
    00200     if (!v->T::IsInDepot() || v->tile != this->tile) return false;
    00201   }
    00202
    00203   return true;
    00204 }
    00205
    00206 /* Instantiation for Train */
    00207 template struct GroundVehicle<Train, VEH_TRAIN>;
    00208 /* Instantiation for RoadVehicle */
    00209 template struct GroundVehicle<RoadVehicle, VEH_ROAD>;