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 
00021 template <class T, VehicleType Type>
00022 void GroundVehicle<T, Type>::PowerChanged()
00023 {
00024   assert(this->First() == this);
00025   const T *v = T::From(this);
00026 
00027   uint32 total_power = 0;
00028   uint32 max_te = 0;
00029   uint32 number_of_parts = 0;
00030   uint16 max_track_speed = v->GetDisplayMaxSpeed();
00031 
00032   for (const T *u = v; u != NULL; u = u->Next()) {
00033     uint32 current_power = u->GetPower() + u->GetPoweredPartPower(u);
00034     total_power += current_power;
00035 
00036     /* Only powered parts add tractive effort. */
00037     if (current_power > 0) max_te += u->GetWeight() * u->GetTractiveEffort();
00038     number_of_parts++;
00039 
00040     /* Get minimum max speed for this track. */
00041     uint16 track_speed = u->GetMaxTrackSpeed();
00042     if (track_speed > 0) max_track_speed = min(max_track_speed, track_speed);
00043   }
00044 
00045   byte air_drag;
00046   byte air_drag_value = v->GetAirDrag();
00047 
00048   /* If air drag is set to zero (default), the resulting air drag coefficient is dependent on max speed. */
00049   if (air_drag_value == 0) {
00050     uint16 max_speed = v->GetDisplayMaxSpeed();
00051     /* Simplification of the method used in TTDPatch. It uses <= 10 to change more steadily from 128 to 196. */
00052     air_drag = (max_speed <= 10) ? 192 : max(2048 / max_speed, 1);
00053   } else {
00054     /* According to the specs, a value of 0x01 in the air drag property means "no air drag". */
00055     air_drag = (air_drag_value == 1) ? 0 : air_drag_value;
00056   }
00057 
00058   this->gcache.cached_air_drag = air_drag + 3 * air_drag * number_of_parts / 20;
00059 
00060   max_te *= 10000; // Tractive effort in (tonnes * 1000 * 10 =) N.
00061   max_te /= 256;   // Tractive effort is a [0-255] coefficient.
00062   if (this->gcache.cached_power != total_power || this->gcache.cached_max_te != max_te) {
00063     /* Stop the vehicle if it has no power. */
00064     if (total_power == 0) this->vehstatus |= VS_STOPPED;
00065 
00066     this->gcache.cached_power = total_power;
00067     this->gcache.cached_max_te = max_te;
00068     SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
00069     SetWindowWidgetDirty(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
00070   }
00071 
00072   this->gcache.cached_max_track_speed = max_track_speed;
00073 }
00074 
00079 template <class T, VehicleType Type>
00080 void GroundVehicle<T, Type>::CargoChanged()
00081 {
00082   assert(this->First() == this);
00083   uint32 weight = 0;
00084 
00085   for (T *u = T::From(this); u != NULL; u = u->Next()) {
00086     uint32 current_weight = u->GetWeight();
00087     weight += current_weight;
00088     /* Slope steepness is in percent, result in N. */
00089     u->gcache.cached_slope_resistance = current_weight * u->GetSlopeSteepness() * 100;
00090   }
00091 
00092   /* Store consist weight in cache. */
00093   this->gcache.cached_weight = max<uint32>(1, weight);
00094   /* Friction in bearings and other mechanical parts is 0.1% of the weight (result in N). */
00095   this->gcache.cached_axle_resistance = 10 * weight;
00096 
00097   /* Now update vehicle power (tractive effort is dependent on weight). */
00098   this->PowerChanged();
00099 }
00100 
00105 template <class T, VehicleType Type>
00106 int GroundVehicle<T, Type>::GetAcceleration() const
00107 {
00108   /* Templated class used for function calls for performance reasons. */
00109   const T *v = T::From(this);
00110   int32 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   int32 power = this->gcache.cached_power * 746;
00117 
00118   int32 resistance = 0;
00119 
00120   bool maglev = v->GetAccelerationType() == 2;
00121 
00122   const int area = v->GetAirDragArea();
00123   if (!maglev) {
00124     /* Static resistance plus rolling friction. */
00125     resistance = this->gcache.cached_axle_resistance;
00126     resistance += mass * v->GetRollingFriction();
00127   }
00128   /* Air drag; the air drag coefficient is in an arbitrary NewGRF-unit,
00129    * so we need some magic conversion factor. */
00130   resistance += (area * this->gcache.cached_air_drag * speed * speed) / 1000;
00131 
00132   resistance += this->GetSlopeResistance();
00133 
00134   /* This value allows to know if the vehicle is accelerating or braking. */
00135   AccelStatus mode = v->GetAccelerationStatus();
00136 
00137   const int max_te = this->gcache.cached_max_te; // [N]
00138   int force;
00139   if (speed > 0) {
00140     if (!maglev) {
00141       /* Conversion factor from km/h to m/s is 5/18 to get [N] in the end. */
00142       force = power * 18 / (speed * 5);
00143       if (mode == AS_ACCEL && force > max_te) force = max_te;
00144     } else {
00145       force = power / 25;
00146     }
00147   } else {
00148     /* "Kickoff" acceleration. */
00149     force = (mode == AS_ACCEL && !maglev) ? min(max_te, power) : power;
00150     force = max(force, (mass * 8) + resistance);
00151   }
00152 
00153   if (mode == AS_ACCEL) {
00154     /* Easy way out when there is no acceleration. */
00155     if (force == resistance) return 0;
00156 
00157     /* When we accelerate, make sure we always keep doing that, even when
00158      * the excess force is more than the mass. Otherwise a vehicle going
00159      * down hill will never slow down enough, and a vehicle that came up
00160      * a hill will never speed up enough to (eventually) get back to the
00161      * same (maximum) speed. */
00162     int accel = (force - resistance) / (mass * 4);
00163     return force < resistance ? min(-1, accel) : max(1, accel);
00164   } else {
00165     return min(-force - resistance, -10000) / mass;
00166   }
00167 }
00168 
00169 /* Instantiation for Train */
00170 template struct GroundVehicle<Train, VEH_TRAIN>;
00171 /* Instantiation for RoadVehicle */
00172 template struct GroundVehicle<RoadVehicle, VEH_ROAD>;

Generated on Sun May 8 07:30:12 2011 for OpenTTD by  doxygen 1.6.1