Fix const issues around GetTrainStopLocation/GetCurrentMaxSpeed

pull/213/head
Jonathan G Rennison 3 years ago
parent b711f94d1c
commit 4e0804b791

@ -3618,7 +3618,7 @@ static VehicleEnterTileStatus VehicleEnter_Station(Vehicle *v, TileIndex tile, i
int station_ahead; int station_ahead;
int station_length; int station_length;
int stop = GetTrainStopLocation(station_id, tile, Train::From(v), &station_ahead, &station_length, x, y); int stop = GetTrainStopLocation(station_id, tile, Train::From(v), true, &station_ahead, &station_length, x, y);
/* Stop whenever that amount of station ahead + the distance from the /* Stop whenever that amount of station ahead + the distance from the
* begin of the platform to the stop location is longer than the length * begin of the platform to the stop location is longer than the length

@ -169,7 +169,20 @@ struct Train FINAL : public GroundVehicle<Train, VEH_TRAIN> {
int strict_max_speed; int strict_max_speed;
int advisory_max_speed; int advisory_max_speed;
}; };
MaxSpeedInfo GetCurrentMaxSpeedInfo() const;
private:
MaxSpeedInfo GetCurrentMaxSpeedInfoInternal(bool update_state) const;
public:
MaxSpeedInfo GetCurrentMaxSpeedInfo() const
{
return this->GetCurrentMaxSpeedInfoInternal(false);
}
MaxSpeedInfo GetCurrentMaxSpeedInfoAndUpdate()
{
return this->GetCurrentMaxSpeedInfoInternal(true);
}
int GetCurrentMaxSpeed() const; int GetCurrentMaxSpeed() const;
@ -438,11 +451,11 @@ inline int GetTileMarginInFrontOfTrain(const Train *v)
return GetTileMarginInFrontOfTrain(v, v->x_pos, v->y_pos); return GetTileMarginInFrontOfTrain(v, v->x_pos, v->y_pos);
} }
int GetTrainStopLocation(StationID station_id, TileIndex tile, Train *v, int *station_ahead, int *station_length, int x_pos, int y_pos); int GetTrainStopLocation(StationID station_id, TileIndex tile, Train *v, bool update_train_state, int *station_ahead, int *station_length, int x_pos, int y_pos);
inline int GetTrainStopLocation(StationID station_id, TileIndex tile, Train *v, int *station_ahead, int *station_length) inline int GetTrainStopLocation(StationID station_id, TileIndex tile, Train *v, bool update_train_state, int *station_ahead, int *station_length)
{ {
return GetTrainStopLocation(station_id, tile, v, station_ahead, station_length, v->x_pos, v->y_pos); return GetTrainStopLocation(station_id, tile, v, update_train_state, station_ahead, station_length, v->x_pos, v->y_pos);
} }
#endif /* TRAIN_H */ #endif /* TRAIN_H */

@ -398,13 +398,14 @@ int GetTileMarginInFrontOfTrain(const Train *v, int x_pos, int y_pos)
* @param station_id the ID of the station where we're stopping * @param station_id the ID of the station where we're stopping
* @param tile the tile where the vehicle currently is * @param tile the tile where the vehicle currently is
* @param v the vehicle to get the stop location of * @param v the vehicle to get the stop location of
* @param update_train_state whether the state of the train v may be changed
* @param station_ahead 'return' the amount of 1/16th tiles in front of the train * @param station_ahead 'return' the amount of 1/16th tiles in front of the train
* @param station_length 'return' the station length in 1/16th tiles * @param station_length 'return' the station length in 1/16th tiles
* @param x_pos vehicle x position * @param x_pos vehicle x position
* @param y_pos vehicle y position * @param y_pos vehicle y position
* @return the location, calculated from the begin of the station to stop at. * @return the location, calculated from the begin of the station to stop at.
*/ */
int GetTrainStopLocation(StationID station_id, TileIndex tile, Train *v, int *station_ahead, int *station_length, int x_pos, int y_pos) int GetTrainStopLocation(StationID station_id, TileIndex tile, Train *v, bool update_train_state, int *station_ahead, int *station_length, int x_pos, int y_pos)
{ {
Train *front = v->First(); Train *front = v->First();
if (IsRailWaypoint(tile)) { if (IsRailWaypoint(tile)) {
@ -439,7 +440,7 @@ int GetTrainStopLocation(StationID station_id, TileIndex tile, Train *v, int *st
if (osl == OSL_PLATFORM_THROUGH && overhang > 0) { if (osl == OSL_PLATFORM_THROUGH && overhang > 0) {
/* The train is longer than the station, and we can run through the station to load/unload */ /* The train is longer than the station, and we can run through the station to load/unload */
for (Train *u = v; u != nullptr; u = u->Next()) { for (Train *u = v; u != nullptr; u = u->Next()) {
if (overhang > 0 && !HasBit(u->flags, VRF_BEYOND_PLATFORM_END) && !u->IsArticulatedPart()) { if (update_train_state && overhang > 0 && !HasBit(u->flags, VRF_BEYOND_PLATFORM_END) && !u->IsArticulatedPart()) {
bool skip = true; bool skip = true;
for (const Train *part = u; part != nullptr; part = part->HasArticulatedPart() ? part->GetNextArticulatedPart() : nullptr) { for (const Train *part = u; part != nullptr; part = part->HasArticulatedPart() ? part->GetNextArticulatedPart() : nullptr) {
if (part->cargo_cap != 0) { if (part->cargo_cap != 0) {
@ -903,7 +904,7 @@ static void AdvanceLookAheadPosition(Train *v)
* Calculates the maximum speed information of the vehicle under its current conditions. * Calculates the maximum speed information of the vehicle under its current conditions.
* @return Maximum speed information of the vehicle. * @return Maximum speed information of the vehicle.
*/ */
Train::MaxSpeedInfo Train::GetCurrentMaxSpeedInfo() const Train::MaxSpeedInfo Train::GetCurrentMaxSpeedInfoInternal(bool update_state) const
{ {
int max_speed = _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL ? int max_speed = _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL ?
this->gcache.cached_max_track_speed : this->gcache.cached_max_track_speed :
@ -921,7 +922,7 @@ Train::MaxSpeedInfo Train::GetCurrentMaxSpeedInfo() const
if (this->current_order.ShouldStopAtStation(this, sid, IsRailWaypoint(platform_tile))) { if (this->current_order.ShouldStopAtStation(this, sid, IsRailWaypoint(platform_tile))) {
int station_ahead; int station_ahead;
int station_length; int station_length;
int stop_at = GetTrainStopLocation(sid, platform_tile, v_platform, &station_ahead, &station_length); int stop_at = GetTrainStopLocation(sid, platform_tile, v_platform, update_state, &station_ahead, &station_length);
/* The distance to go is whatever is still ahead of the train minus the /* The distance to go is whatever is still ahead of the train minus the
* distance from the train's stop location to the end of the platform */ * distance from the train's stop location to the end of the platform */
@ -4260,7 +4261,7 @@ void Train::MarkDirty()
int Train::UpdateSpeed() int Train::UpdateSpeed()
{ {
AccelStatus accel_status = this->GetAccelerationStatus(); AccelStatus accel_status = this->GetAccelerationStatus();
MaxSpeedInfo max_speed_info = this->GetCurrentMaxSpeedInfo(); MaxSpeedInfo max_speed_info = this->GetCurrentMaxSpeedInfoAndUpdate();
if (this->lookahead != nullptr && HasBit(this->lookahead->flags, TRLF_APPLY_ADVISORY) && this->cur_speed <= max_speed_info.strict_max_speed) { if (this->lookahead != nullptr && HasBit(this->lookahead->flags, TRLF_APPLY_ADVISORY) && this->cur_speed <= max_speed_info.strict_max_speed) {
ClrBit(this->lookahead->flags, TRLF_APPLY_ADVISORY); ClrBit(this->lookahead->flags, TRLF_APPLY_ADVISORY);
} }

Loading…
Cancel
Save