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 #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
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;
00039 max_te /= 256;
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
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
00070 if (air_drag_value == 0) {
00071 uint16 max_speed = v->GetDisplayMaxSpeed();
00072
00073 air_drag = (max_speed <= 10) ? 192 : max(2048 / max_speed, 1);
00074 } else {
00075
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
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
00108 u->gcache.cached_slope_resistance = current_weight * u->GetSlopeSteepness() * 100;
00109 }
00110
00111
00112 this->gcache.cached_weight = max<uint32>(1, weight);
00113
00114 this->gcache.cached_axle_resistance = 10 * weight;
00115
00116
00117 this->PowerChanged();
00118 }
00119
00124 template <class T, VehicleType Type>
00125 int GroundVehicle<T, Type>::GetAcceleration()
00126 {
00127
00128 const T *v = T::From(this);
00129 int32 speed = v->GetCurrentSpeed();
00130
00131
00132 int32 mass = this->gcache.cached_weight;
00133
00134
00135 uint32 power = this->gcache.cached_power * 746;
00136 uint32 max_te = this->gcache.cached_max_te;
00137
00138 int32 resistance = 0;
00139
00140 bool maglev = v->GetAccelerationType() == 2;
00141
00142 const int area = v->GetAirDragArea();
00143 if (!maglev) {
00144
00145 resistance = this->gcache.cached_axle_resistance;
00146 resistance += mass * v->GetRollingFriction();
00147 }
00148
00149
00150 resistance += (area * this->gcache.cached_air_drag * speed * speed) / 1000;
00151
00152 resistance += this->GetSlopeResistance();
00153
00154
00155 AccelStatus mode = v->GetAccelerationStatus();
00156
00157
00158 if( Type == VEH_TRAIN && mode == AS_ACCEL && HasBit(Train::From(this)->flags, VRF_BREAKDOWN_POWER)) {
00159
00160 this->CalculatePower(power, max_te, true);
00161 power *= 746;
00162 }
00163
00164 int force;
00165 if (speed > 0) {
00166 if (!maglev) {
00167
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
00175 force = (mode == AS_ACCEL && !maglev) ? min(max_te, power) : power;
00176 force = max(force, (mass * 8) + resistance);
00177 }
00178
00179
00180 if ( Type == VEH_TRAIN && mode == AS_ACCEL && HasBit(Train::From(this)->flags, VRF_BREAKDOWN_POWER) && power == 0) {
00181 force = 0;
00182 }
00183
00184
00185 if (_settings_game.vehicle.improved_breakdowns) {
00186 assert(this->gcache.cached_max_track_speed > 0);
00187
00188
00189
00190
00191
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
00197 breakdown_factor *= 3;
00198 breakdown_factor /= (Train::From(this)->tcache.cached_num_engines + 2);
00199 }
00200
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
00208 if (force == resistance) return 0;
00209
00210
00211
00212
00213
00214
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
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
00241 template struct GroundVehicle<Train, VEH_TRAIN>;
00242
00243 template struct GroundVehicle<RoadVehicle, VEH_ROAD>;