(svn r18836) -Codechange: make TrainUpdateSpeed a class function and update some comments (Terkhen)

pull/155/head
rubidium 15 years ago
parent 43edfbf7c0
commit 64c249d50e

@ -1279,7 +1279,7 @@ static Vehicle *UpdateTrainPowerProc(Vehicle *v, void *data)
{ {
if (v->type != VEH_TRAIN) return NULL; if (v->type != VEH_TRAIN) return NULL;
/* Similiar checks as in TrainPowerChanged() */ /* Similar checks as in Train::PowerChanged() */
Train *t = Train::From(v); Train *t = Train::From(v);
if (t->IsArticulatedPart()) return NULL; if (t->IsArticulatedPart()) return NULL;

@ -139,6 +139,8 @@ struct Train : public SpecializedVehicle<Train, VEH_TRAIN> {
void CargoChanged(); void CargoChanged();
void PowerChanged(); void PowerChanged();
int UpdateSpeed();
void UpdateAcceleration(); void UpdateAcceleration();
/** /**

@ -141,7 +141,6 @@ void Train::PowerChanged()
/** /**
* Recalculates the cached weight of a train and its vehicles. Should be called each time the cargo on * Recalculates the cached weight of a train and its vehicles. Should be called each time the cargo on
* the consist changes. * the consist changes.
* @param v First vehicle of the consist.
*/ */
void Train::CargoChanged() void Train::CargoChanged()
{ {
@ -2956,34 +2955,33 @@ void Train::MarkDirty()
* where n is the number of straight (long) tracks the train can * where n is the number of straight (long) tracks the train can
* traverse. This means that moving along a straight track costs 256 * traverse. This means that moving along a straight track costs 256
* "speed" and a diagonal track costs 192 "speed". * "speed" and a diagonal track costs 192 "speed".
* @param v The vehicle to update the speed of.
* @return distance to drive. * @return distance to drive.
*/ */
static int UpdateTrainSpeed(Train *v) int Train::UpdateSpeed()
{ {
uint accel; uint accel;
if ((v->vehstatus & VS_STOPPED) || HasBit(v->flags, VRF_REVERSING) || HasBit(v->flags, VRF_TRAIN_STUCK)) { if ((this->vehstatus & VS_STOPPED) || HasBit(this->flags, VRF_REVERSING) || HasBit(this->flags, VRF_TRAIN_STUCK)) {
switch (_settings_game.vehicle.train_acceleration_model) { switch (_settings_game.vehicle.train_acceleration_model) {
default: NOT_REACHED(); default: NOT_REACHED();
case TAM_ORIGINAL: accel = v->acceleration * -4; break; case TAM_ORIGINAL: accel = this->acceleration * -4; break;
case TAM_REALISTIC: accel = GetTrainAcceleration(v, AM_BRAKE); break; case TAM_REALISTIC: accel = GetTrainAcceleration(this, AM_BRAKE); break;
} }
} else { } else {
switch (_settings_game.vehicle.train_acceleration_model) { switch (_settings_game.vehicle.train_acceleration_model) {
default: NOT_REACHED(); default: NOT_REACHED();
case TAM_ORIGINAL: accel = v->acceleration * 2; break; case TAM_ORIGINAL: accel = this->acceleration * 2; break;
case TAM_REALISTIC: accel = GetTrainAcceleration(v, AM_ACCEL); break; case TAM_REALISTIC: accel = GetTrainAcceleration(this, AM_ACCEL); break;
} }
} }
uint spd = v->subspeed + accel; uint spd = this->subspeed + accel;
v->subspeed = (byte)spd; this->subspeed = (byte)spd;
{ {
int tempmax = v->max_speed; int tempmax = this->max_speed;
if (v->cur_speed > v->max_speed) if (this->cur_speed > this->max_speed)
tempmax = v->cur_speed - (v->cur_speed / 10) - 1; tempmax = this->cur_speed - (this->cur_speed / 10) - 1;
v->cur_speed = spd = Clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax); this->cur_speed = spd = Clamp(this->cur_speed + ((int)spd >> 8), 0, tempmax);
} }
/* Scale speed by 3/4. Previously this was only done when the train was /* Scale speed by 3/4. Previously this was only done when the train was
@ -2995,12 +2993,12 @@ static int UpdateTrainSpeed(Train *v)
* *
* The scaling is done in this direction and not by multiplying the amount * The scaling is done in this direction and not by multiplying the amount
* to be subtracted by 4/3 so that the leftover speed can be saved in a * to be subtracted by 4/3 so that the leftover speed can be saved in a
* byte in v->progress. * byte in this->progress.
*/ */
int scaled_spd = spd * 3 >> 2; int scaled_spd = spd * 3 >> 2;
scaled_spd += v->progress; scaled_spd += this->progress;
v->progress = 0; // set later in TrainLocoHandler or TrainController this->progress = 0; // set later in TrainLocoHandler or TrainController
return scaled_spd; return scaled_spd;
} }
@ -4015,7 +4013,7 @@ static bool TrainLocoHandler(Train *v, bool mode)
return true; return true;
} }
int j = UpdateTrainSpeed(v); int j = v->UpdateSpeed();
/* we need to invalidate the widget if we are stopping from 'Stopping 0 km/h' to 'Stopped' */ /* we need to invalidate the widget if we are stopping from 'Stopping 0 km/h' to 'Stopped' */
if (v->cur_speed == 0 && v->tcache.last_speed == 0 && (v->vehstatus & VS_STOPPED)) { if (v->cur_speed == 0 && v->tcache.last_speed == 0 && (v->vehstatus & VS_STOPPED)) {

Loading…
Cancel
Save