Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "train.h"
00014 #include "roadveh.h"
00015
00019 template <class T, VehicleType Type>
00020 void GroundVehicle<T, Type>::PowerChanged()
00021 {
00022 assert(this->First() == this);
00023 const T *v = T::From(this);
00024
00025 uint32 total_power = 0;
00026 uint32 max_te = 0;
00027 uint32 number_of_parts = 0;
00028 uint16 max_track_speed = v->GetDisplayMaxSpeed();
00029
00030 for (const T *u = v; u != NULL; u = u->Next()) {
00031 uint32 current_power = u->GetPower() + u->GetPoweredPartPower(u);
00032 total_power += current_power;
00033
00034
00035 if (current_power > 0) max_te += u->GetWeight() * u->GetTractiveEffort();
00036 number_of_parts++;
00037
00038
00039 uint16 track_speed = u->GetMaxTrackSpeed();
00040 if (track_speed > 0) max_track_speed = min(max_track_speed, track_speed);
00041 }
00042
00043 byte air_drag;
00044 byte air_drag_value = v->GetAirDrag();
00045
00046
00047 if (air_drag_value == 0) {
00048 uint16 max_speed = v->GetDisplayMaxSpeed();
00049
00050 air_drag = (max_speed <= 10) ? 192 : max(2048 / max_speed, 1);
00051 } else {
00052
00053 air_drag = (air_drag_value == 1) ? 0 : air_drag_value;
00054 }
00055
00056 this->gcache.cached_air_drag = air_drag + 3 * air_drag * number_of_parts / 20;
00057
00058 max_te *= 10000;
00059 max_te /= 256;
00060 if (this->gcache.cached_power != total_power || this->gcache.cached_max_te != max_te) {
00061
00062 if (total_power == 0) this->vehstatus |= VS_STOPPED;
00063
00064 this->gcache.cached_power = total_power;
00065 this->gcache.cached_max_te = max_te;
00066 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
00067 SetWindowWidgetDirty(WC_VEHICLE_VIEW, this->index, WID_VV_START_STOP);
00068 }
00069
00070 this->gcache.cached_max_track_speed = max_track_speed;
00071 }
00072
00077 template <class T, VehicleType Type>
00078 void GroundVehicle<T, Type>::CargoChanged()
00079 {
00080 assert(this->First() == this);
00081 uint32 weight = 0;
00082
00083 for (T *u = T::From(this); u != NULL; u = u->Next()) {
00084 uint32 current_weight = u->GetWeight();
00085 weight += current_weight;
00086
00087 u->gcache.cached_slope_resistance = current_weight * u->GetSlopeSteepness() * 100;
00088 }
00089
00090
00091 this->gcache.cached_weight = max<uint32>(1, weight);
00092
00093 this->gcache.cached_axle_resistance = 10 * weight;
00094
00095
00096 this->PowerChanged();
00097 }
00098
00103 template <class T, VehicleType Type>
00104 int GroundVehicle<T, Type>::GetAcceleration() const
00105 {
00106
00107 const T *v = T::From(this);
00108 int32 speed = v->GetCurrentSpeed();
00109
00110
00111 int32 mass = this->gcache.cached_weight;
00112
00113
00114 int32 power = this->gcache.cached_power * 746;
00115
00116 int32 resistance = 0;
00117
00118 bool maglev = v->GetAccelerationType() == 2;
00119
00120 const int area = v->GetAirDragArea();
00121 if (!maglev) {
00122
00123 resistance = this->gcache.cached_axle_resistance;
00124 resistance += mass * v->GetRollingFriction();
00125 }
00126
00127
00128 resistance += (area * this->gcache.cached_air_drag * speed * speed) / 1000;
00129
00130 resistance += this->GetSlopeResistance();
00131
00132
00133 AccelStatus mode = v->GetAccelerationStatus();
00134
00135 const int max_te = this->gcache.cached_max_te;
00136 int force;
00137 if (speed > 0) {
00138 if (!maglev) {
00139
00140 force = power * 18 / (speed * 5);
00141 if (mode == AS_ACCEL && force > max_te) force = max_te;
00142 } else {
00143 force = power / 25;
00144 }
00145 } else {
00146
00147 force = (mode == AS_ACCEL && !maglev) ? min(max_te, power) : power;
00148 force = max(force, (mass * 8) + resistance);
00149 }
00150
00151 if (mode == AS_ACCEL) {
00152
00153 if (force == resistance) return 0;
00154
00155
00156
00157
00158
00159
00160 int accel = (force - resistance) / (mass * 4);
00161 return force < resistance ? min(-1, accel) : max(1, accel);
00162 } else {
00163 return min(-force - resistance, -10000) / mass;
00164 }
00165 }
00166
00167
00168 template struct GroundVehicle<Train, VEH_TRAIN>;
00169
00170 template struct GroundVehicle<RoadVehicle, VEH_ROAD>;