summaryrefslogtreecommitdiff
path: root/src/vehicle_base.h
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2010-07-04 13:07:47 +0000
committerfrosch <frosch@openttd.org>2010-07-04 13:07:47 +0000
commita7d168b2e5be77e361336d61c21b2b22f932fcc5 (patch)
treeaca46a4f7a9e467b164537edb26b20769b9e1de8 /src/vehicle_base.h
parented2213df239a84c1ef7962e4cf7abe102f0fcb5f (diff)
downloadopenttd-a7d168b2e5be77e361336d61c21b2b22f932fcc5.tar.xz
(svn r20079) -Codechange [FS#3922]: Add helper functions to deal with the 192-256-magic of vehicle movement.
Diffstat (limited to 'src/vehicle_base.h')
-rw-r--r--src/vehicle_base.h47
1 files changed, 47 insertions, 0 deletions
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
@@ -227,6 +227,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
*/