# ground_vehicle.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 "vehicle_gui.h"
    00016 #include "window_func.h"
    00017
    00018 template <class T, VehicleType Type>
    00019 void GroundVehicle<T, Type>::CalculatePower(uint32& total_power, uint32& max_te, bool breakdowns) const {
    00020
    00021   total_power = 0;
    00022   max_te = 0;
    00023
    00024   const T *v = T::From(this);
    00025
    00026   for (const T *u = v; u != NULL; u = u->Next()) {
    00027     uint32 current_power = u->GetPower() + u->GetPoweredPartPower(u);
    00028     total_power += current_power;
    00029
    00030     /* Only powered parts add tractive effort. */
    00031     if (current_power > 0) max_te += u->GetWeight() * u->GetTractiveEffort();
    00032
    00033     if (breakdowns && u->breakdown_ctr == 1 && u->breakdown_type == BREAKDOWN_LOW_POWER) {
    00034       total_power = total_power * u->breakdown_severity / 256;
    00035     }
    00036   }
    00037
    00038   max_te *= 10000; // Tractive effort in (tonnes * 1000 * 10 =) N.
    00039   max_te /= 256;   // Tractive effort is a [0-255] coefficient.
    00040 }
    00041
    00045 template <class T, VehicleType Type>
    00046 void GroundVehicle<T, Type>::PowerChanged()
    00047 {
    00048   assert(this->First() == this);
    00049   const T *v = T::From(this);
    00050
    00051   uint32 total_power = 0;
    00052   uint32 max_te = 0;
    00053   uint32 number_of_parts = 0;
    00054   uint16 max_track_speed = v->GetDisplayMaxSpeed();
    00055
    00056   this->CalculatePower(total_power, max_te, false);
    00057
    00058   for (const T *u = v; u != NULL; u = u->Next()) {
    00059     number_of_parts++;
    00060
    00061     /* Get minimum max speed for this track. */
    00062     uint16 track_speed = u->GetMaxTrackSpeed();
    00063     if (track_speed > 0) max_track_speed = min(max_track_speed, track_speed);
    00064   }
    00065
    00066   byte air_drag;
    00067   byte air_drag_value = v->GetAirDrag();
    00068
    00069   /* If air drag is set to zero (default), the resulting air drag coefficient is dependent on max speed. */
    00070   if (air_drag_value == 0) {
    00071     uint16 max_speed = v->GetDisplayMaxSpeed();
    00072     /* Simplification of the method used in TTDPatch. It uses <= 10 to change more steadily from 128 to 196. */
    00073     air_drag = (max_speed <= 10) ? 192 : max(2048 / max_speed, 1);
    00074   } else {
    00075     /* According to the specs, a value of 0x01 in the air drag property means "no air drag". */
    00076     air_drag = (air_drag_value == 1) ? 0 : air_drag_value;
    00077   }
    00078
    00079   this->gcache.cached_air_drag = air_drag + 3 * air_drag * number_of_parts / 20;
    00080
    00081   if (this->gcache.cached_power != total_power || this->gcache.cached_max_te != max_te) {
    00082     /* Stop the vehicle if it has no power. */
    00083     if (total_power == 0) this->vehstatus |= VS_STOPPED;
    00084
    00085     this->gcache.cached_power = total_power;
    00086     this->gcache.cached_max_te = max_te;
    00087     SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
    00088     SetWindowWidgetDirty(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
    00089   }
    00090
    00091   this->gcache.cached_max_track_speed = max_track_speed;
    00092 }
    00093
    00098 template <class T, VehicleType Type>
    00099 void GroundVehicle<T, Type>::CargoChanged()
    00100 {
    00101   assert(this->First() == this);
    00102   uint32 weight = 0;
    00103
    00104   for (T *u = T::From(this); u != NULL; u = u->Next()) {
    00105     uint32 current_weight = u->GetWeight();
    00106     weight += current_weight;
    00107     /* Slope steepness is in percent, result in N. */
    00108     u->gcache.cached_slope_resistance = current_weight * u->GetSlopeSteepness() * 100;
    00109   }
    00110
    00111   /* Store consist weight in cache. */
    00112   this->gcache.cached_weight = max<uint32>(1, weight);
    00113   /* Friction in bearings and other mechanical parts is 0.1% of the weight (result in N). */
    00114   this->gcache.cached_axle_resistance = 10 * weight;
    00115
    00116   /* Now update vehicle power (tractive effort is dependent on weight). */
    00117   this->PowerChanged();
    00118 }
    00119
    00124 template <class T, VehicleType Type>
    00125 int GroundVehicle<T, Type>::GetAcceleration()
    00126 {
    00127   /* Templated class used for function calls for performance reasons. */
    00128   const T *v = T::From(this);
    00129   int32 speed = v->GetCurrentSpeed(); // [km/h-ish]
    00130
    00131   /* Weight is stored in tonnes. */
    00132   int32 mass = this->gcache.cached_weight;
    00133
    00134   /* Power is stored in HP, we need it in watts. */
    00135   uint32 power = this->gcache.cached_power * 746;
    00136   uint32 max_te = this->gcache.cached_max_te; // [N]
    00137
    00138   int32 resistance = 0;
    00139
    00140   bool maglev = v->GetAccelerationType() == 2;
    00141
    00142   const int area = v->GetAirDragArea();
    00143   if (!maglev) {
    00144     /* Static resistance plus rolling friction. */
    00145     resistance = this->gcache.cached_axle_resistance;
    00146     resistance += mass * v->GetRollingFriction();
    00147   }
    00148   /* Air drag; the air drag coefficient is in an arbitrary NewGRF-unit,
    00149    * so we need some magic conversion factor. */
    00150   resistance += (area * this->gcache.cached_air_drag * speed * speed) / 1000;
    00151
    00152   resistance += this->GetSlopeResistance();
    00153
    00154   /* This value allows to know if the vehicle is accelerating or braking. */
    00155   AccelStatus mode = v->GetAccelerationStatus();
    00156
    00157   /* Handle breakdown power reduction. */
    00158   if(  Type == VEH_TRAIN  && mode == AS_ACCEL && HasBit(Train::From(this)->flags, VRF_BREAKDOWN_POWER)) {
    00159     /* TODO: We'd like to cache this, but changing cached_power has too many unwanted side-effects. */
    00160     this->CalculatePower(power, max_te, true);
    00161     power *= 746;
    00162   }
    00163
    00164   int force;
    00165   if (speed > 0) {
    00166     if (!maglev) {
    00167       /* Conversion factor from km/h to m/s is 5/18 to get [N] in the end. */
    00168       force = power * 18 / (speed * 5);
    00169       if (mode == AS_ACCEL && force > (int)max_te) force = max_te;
    00170     } else {
    00171       force = power / 25;
    00172     }
    00173   } else {
    00174     /* "Kickoff" acceleration. */
    00175     force = (mode == AS_ACCEL && !maglev) ? min(max_te, power) : power;
    00176     force = max(force, (mass * 8) + resistance);
    00177   }
    00178
    00179   /* If power is 0 because of a breakdown, we make the force 0 if accelerating. */
    00180   if ( Type == VEH_TRAIN && mode == AS_ACCEL && HasBit(Train::From(this)->flags, VRF_BREAKDOWN_POWER) && power == 0) {
    00181     force = 0;
    00182   }
    00183
    00184   /* Calculate the breakdown chance. */
    00185   if (_settings_game.vehicle.improved_breakdowns) {
    00186     assert(this->gcache.cached_max_track_speed > 0);
    00187     /* First, calculate (resistance / force * current speed / max speed) << 16.
    00188      * This yields a number x on a 0-1 scale, but shifted 16 bits to the left.
    00189      * We then calculate 64 + 128x, clamped to 0-255, but still shifted 16 bits to the left.
    00190      * Then we apply a correction for multiengine trains, and in the end we shift it 16 bits to the right to get a 0-255 number.
    00191      * @note A seperate correction for multiheaded engines is done in CheckVehicleBreakdown. We can't do that here because it would affect the whole consist. */
    00192     uint64 breakdown_factor = (uint64)abs(resistance) * (uint64)(this->cur_speed << 16);
    00193     breakdown_factor /= (max(force, 100) * this->gcache.cached_max_track_speed);
    00194     breakdown_factor = min((64 << 16) + (breakdown_factor * 128), 255 << 16);
    00195     if ( Type == VEH_TRAIN && Train::From(this)->tcache.cached_num_engines > 1) {
    00196       /* For multiengine trains, breakdown chance is multiplied by 3 / (num_engines + 2). */
    00197       breakdown_factor *= 3;
    00198       breakdown_factor /= (Train::From(this)->tcache.cached_num_engines + 2);
    00199     }
    00200     /* Breakdown_chance is at least 5 (5 / 128 = ~4% of the normal chance). */
    00201     this->breakdown_chance = max(breakdown_factor >> 16, (uint64)5);
    00202   } else {
    00203     this->breakdown_chance = 128;
    00204   }
    00205
    00206   if (mode == AS_ACCEL) {
    00207     /* Easy way out when there is no acceleration. */
    00208     if (force == resistance) return 0;
    00209
    00210     /* When we accelerate, make sure we always keep doing that, even when
    00211      * the excess force is more than the mass. Otherwise a vehicle going
    00212      * down hill will never slow down enough, and a vehicle that came up
    00213      * a hill will never speed up enough to (eventually) get back to the
    00214      * same (maximum) speed. */
    00215     int accel = (force - resistance) / (mass * 4);
    00216     accel = force < resistance ? min(-1, accel) : max(1, accel);
    00217     if (this->type == VEH_TRAIN ) {
    00218       if(_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL &&
    00219         HasBit(Train::From(this)->flags, VRF_BREAKDOWN_POWER)) {
    00220         /* We need to apply the power reducation for non-realistic acceleration here. */
    00221         CalculatePower(power, max_te, true);
    00222         accel = accel * power / this->gcache.cached_power;
    00223         accel -= this->acceleration >> 1;
    00224       }
    00225
    00226       if ( this->IsFrontEngine() && !(this->current_order_time & 0x1FF) &&
    00227         !(this->current_order.IsType(OT_LOADING)) &&
    00228         !(Train::From(this)->flags & (VRF_IS_BROKEN | (1 << VRF_TRAIN_STUCK))) &&
    00229         this->cur_speed < 3 && accel < 5) {
    00230         SetBit(Train::From(this)->flags, VRF_TO_HEAVY);
    00231       }
    00232     }
    00233
    00234     return accel;
    00235   } else {
    00236     return min(-force - resistance, -10000) / mass;
    00237   }
    00238 }
    00239
    00240 /* Instantiation for Train */
    00241 template struct GroundVehicle<Train, VEH_TRAIN>;
    00242 /* Instantiation for RoadVehicle */
    00243 template struct GroundVehicle<RoadVehicle, VEH_ROAD>;