mirror of https://github.com/OpenTTD/OpenTTD
(svn r23855) -Fix [FS#5007]: out of bounds read for slowdown parameter caused desync when railtype >= 4, vehicles were fast, and the original acceleration model was used
parent
a48e3cb891
commit
6045c9e383
|
@ -2794,19 +2794,18 @@ static inline bool CheckCompatibleRail(const Train *v, TileIndex tile)
|
||||||
(!v->IsFrontEngine() || HasBit(v->compatible_railtypes, GetRailType(tile)));
|
(!v->IsFrontEngine() || HasBit(v->compatible_railtypes, GetRailType(tile)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Data structure for storing engine speed changes of a rail type. */
|
/** Data structure for storing engine speed changes of an acceleration type. */
|
||||||
struct RailtypeSlowdownParams {
|
struct AccelerationSlowdownParams {
|
||||||
byte small_turn; ///< Speed change due to a small turn.
|
byte small_turn; ///< Speed change due to a small turn.
|
||||||
byte large_turn; ///< Speed change due to a large turn.
|
byte large_turn; ///< Speed change due to a large turn.
|
||||||
byte z_up; ///< Fraction to remove when moving up.
|
byte z_up; ///< Fraction to remove when moving up.
|
||||||
byte z_down; ///< Fraction to add when moving down.
|
byte z_down; ///< Fraction to add when moving down.
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Speed update fractions for each rail type. */
|
/** Speed update fractions for each acceleration type. */
|
||||||
static const RailtypeSlowdownParams _railtype_slowdown[] = {
|
static const AccelerationSlowdownParams _accel_slowdown[] = {
|
||||||
/* normal accel */
|
/* normal accel */
|
||||||
{256 / 4, 256 / 2, 256 / 4, 2}, ///< normal
|
{256 / 4, 256 / 2, 256 / 4, 2}, ///< normal
|
||||||
{256 / 4, 256 / 2, 256 / 4, 2}, ///< electrified
|
|
||||||
{256 / 4, 256 / 2, 256 / 4, 2}, ///< monorail
|
{256 / 4, 256 / 2, 256 / 4, 2}, ///< monorail
|
||||||
{0, 256 / 2, 256 / 4, 2}, ///< maglev
|
{0, 256 / 2, 256 / 4, 2}, ///< maglev
|
||||||
};
|
};
|
||||||
|
@ -2820,12 +2819,12 @@ static inline void AffectSpeedByZChange(Train *v, int old_z)
|
||||||
{
|
{
|
||||||
if (old_z == v->z_pos || _settings_game.vehicle.train_acceleration_model != AM_ORIGINAL) return;
|
if (old_z == v->z_pos || _settings_game.vehicle.train_acceleration_model != AM_ORIGINAL) return;
|
||||||
|
|
||||||
const RailtypeSlowdownParams *rsp = &_railtype_slowdown[v->railtype];
|
const AccelerationSlowdownParams *asp = &_accel_slowdown[GetRailTypeInfo(v->railtype)->acceleration_type];
|
||||||
|
|
||||||
if (old_z < v->z_pos) {
|
if (old_z < v->z_pos) {
|
||||||
v->cur_speed -= (v->cur_speed * rsp->z_up >> 8);
|
v->cur_speed -= (v->cur_speed * asp->z_up >> 8);
|
||||||
} else {
|
} else {
|
||||||
uint16 spd = v->cur_speed + rsp->z_down;
|
uint16 spd = v->cur_speed + asp->z_down;
|
||||||
if (spd <= v->gcache.cached_max_track_speed) v->cur_speed = spd;
|
if (spd <= v->gcache.cached_max_track_speed) v->cur_speed = spd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3242,9 +3241,9 @@ bool TrainController(Train *v, Vehicle *nomove, bool reverse)
|
||||||
|
|
||||||
if (chosen_dir != v->direction) {
|
if (chosen_dir != v->direction) {
|
||||||
if (prev == NULL && _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) {
|
if (prev == NULL && _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) {
|
||||||
const RailtypeSlowdownParams *rsp = &_railtype_slowdown[v->railtype];
|
const AccelerationSlowdownParams *asp = &_accel_slowdown[GetRailTypeInfo(v->railtype)->acceleration_type];
|
||||||
DirDiff diff = DirDifference(v->direction, chosen_dir);
|
DirDiff diff = DirDifference(v->direction, chosen_dir);
|
||||||
v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8;
|
v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? asp->small_turn : asp->large_turn) * v->cur_speed >> 8;
|
||||||
}
|
}
|
||||||
direction_changed = true;
|
direction_changed = true;
|
||||||
v->direction = chosen_dir;
|
v->direction = chosen_dir;
|
||||||
|
|
Loading…
Reference in New Issue