From a7d168b2e5be77e361336d61c21b2b22f932fcc5 Mon Sep 17 00:00:00 2001 From: frosch Date: Sun, 4 Jul 2010 13:07:47 +0000 Subject: (svn r20079) -Codechange [FS#3922]: Add helper functions to deal with the 192-256-magic of vehicle movement. --- src/vehicle_base.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src/vehicle_base.h') diff --git a/src/vehicle_base.h b/src/vehicle_base.h index ea626a40f..f12eb16be 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -226,6 +226,53 @@ public: */ virtual void UpdateDeltaXY(Direction direction) {} + /** + * Determines the effective direction-specific vehicle movement speed. + * + * This method belongs to the old vehicle movement method: + * A vehicle moves a step every 256 progress units. + * The vehicle speed is scaled by 3/4 when moving in X or Y direction due to the longer distance. + * + * However, this method is slightly wrong in corners, as the leftover progress is not scaled correctly + * when changing movement direction. #GetAdvanceSpeed() and #GetAdvanceDistance() are better wrt. this. + * + * @param speed Direction-independent unscaled speed. + * @return speed scaled by movement direction. 256 units are required for each movement step. + */ + FORCEINLINE uint GetOldAdvanceSpeed(uint speed) + { + return (this->direction & 1) ? speed : speed * 3 / 4; + } + + /** + * Determines the effective vehicle movement speed. + * + * Together with #GetAdvanceDistance() this function is a replacement for #GetOldAdvanceSpeed(). + * + * A vehicle progresses independent of it's movement direction. + * However different amounts of "progress" are needed for moving a step in a specific direction. + * That way the leftover progress does not need any adaption when changing movement direction. + * + * @param speed Direction-independent unscaled speed. + * @return speed, scaled to match #GetAdvanceDistance(). + */ + static FORCEINLINE uint GetAdvanceSpeed(uint speed) + { + return speed * 3 / 4; + } + + /** + * Determines the vehicle "progress" needed for moving a step. + * + * Together with #GetAdvanceSpeed() this function is a replacement for #GetOldAdvanceSpeed(). + * + * @return distance to drive for a movement step on the map. + */ + FORCEINLINE uint GetAdvanceDistance() + { + return (this->direction & 1) ? 192 : 256; + } + /** * Sets the expense type associated to this vehicle type * @param income whether this is income or (running) expenses of the vehicle -- cgit v1.2.3-54-g00ecf