summaryrefslogtreecommitdiff
path: root/src/vehicle.cpp
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2018-03-11 13:23:26 +0000
committerfrosch <frosch@openttd.org>2018-03-11 13:23:26 +0000
commit55588996213dc551ad47625c0111c1bc72c50d23 (patch)
tree12acd2551cb1539758a02d76e731b8365de3b616 /src/vehicle.cpp
parent07d841d0efb3fc3ed8cca1f3519c3b21bead0bb9 (diff)
downloadopenttd-55588996213dc551ad47625c0111c1bc72c50d23.tar.xz
(svn r27986) -Codechange: Name magic constants in the vehicle position hash functions.
Diffstat (limited to 'src/vehicle.cpp')
-rw-r--r--src/vehicle.cpp47
1 files changed, 35 insertions, 12 deletions
diff --git a/src/vehicle.cpp b/src/vehicle.cpp
index e4a8c7bf1..be11e05be 100644
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -57,7 +57,30 @@
#include "safeguards.h"
-#define GEN_HASH(x, y) ((GB((y), 6 + ZOOM_LVL_SHIFT, 6) << 6) + GB((x), 7 + ZOOM_LVL_SHIFT, 6))
+/* Number of bits in the hash to use from each vehicle coord */
+static const uint GEN_HASHX_BITS = 6;
+static const uint GEN_HASHY_BITS = 6;
+
+/* Size of each hash bucket */
+static const uint GEN_HASHX_BUCKET_BITS = 7;
+static const uint GEN_HASHY_BUCKET_BITS = 6;
+
+/* Compute hash for vehicle coord */
+#define GEN_HASHX(x) GB((x), GEN_HASHX_BUCKET_BITS + ZOOM_LVL_SHIFT, GEN_HASHX_BITS)
+#define GEN_HASHY(y) (GB((y), GEN_HASHY_BUCKET_BITS + ZOOM_LVL_SHIFT, GEN_HASHY_BITS) << GEN_HASHX_BITS)
+#define GEN_HASH(x, y) (GEN_HASHY(y) + GEN_HASHX(x))
+
+/* Maximum size until hash repeats */
+static const int GEN_HASHX_SIZE = 1 << (GEN_HASHX_BUCKET_BITS + GEN_HASHX_BITS + ZOOM_LVL_SHIFT);
+static const int GEN_HASHY_SIZE = 1 << (GEN_HASHY_BUCKET_BITS + GEN_HASHY_BITS + ZOOM_LVL_SHIFT);
+
+/* Increments to reach next bucket in hash table */
+static const int GEN_HASHX_INC = 1;
+static const int GEN_HASHY_INC = 1 << GEN_HASHX_BITS;
+
+/* Mask to wrap-around buckets */
+static const uint GEN_HASHX_MASK = (1 << GEN_HASHX_BITS) - 1;
+static const uint GEN_HASHY_MASK = ((1 << GEN_HASHY_BITS) - 1) << GEN_HASHX_BITS;
VehicleID _new_vehicle_id;
uint16 _returned_refit_capacity; ///< Stores the capacity after a refit operation.
@@ -618,7 +641,7 @@ static void UpdateVehicleTileHash(Vehicle *v, bool remove)
v->hash_tile_current = new_hash;
}
-static Vehicle *_vehicle_viewport_hash[0x1000];
+static Vehicle *_vehicle_viewport_hash[1 << (GEN_HASHX_BITS + GEN_HASHY_BITS)];
static void UpdateVehicleViewportHash(Vehicle *v, int x, int y)
{
@@ -1088,26 +1111,26 @@ void ViewportAddVehicles(DrawPixelInfo *dpi)
/* The hash area to scan */
int xl, xu, yl, yu;
- if (dpi->width + (70 * ZOOM_LVL_BASE) < (1 << (7 + 6 + ZOOM_LVL_SHIFT))) {
- xl = GB(l - (70 * ZOOM_LVL_BASE), 7 + ZOOM_LVL_SHIFT, 6);
- xu = GB(r, 7 + ZOOM_LVL_SHIFT, 6);
+ if (dpi->width + (MAX_VEHICLE_PIXEL_X * ZOOM_LVL_BASE) < GEN_HASHX_SIZE) {
+ xl = GEN_HASHX(l - MAX_VEHICLE_PIXEL_X * ZOOM_LVL_BASE);
+ xu = GEN_HASHX(r);
} else {
/* scan whole hash row */
xl = 0;
- xu = 0x3F;
+ xu = GEN_HASHX_MASK;
}
- if (dpi->height + (70 * ZOOM_LVL_BASE) < (1 << (6 + 6 + ZOOM_LVL_SHIFT))) {
- yl = GB(t - (70 * ZOOM_LVL_BASE), 6 + ZOOM_LVL_SHIFT, 6) << 6;
- yu = GB(b, 6 + ZOOM_LVL_SHIFT, 6) << 6;
+ if (dpi->height + (MAX_VEHICLE_PIXEL_Y * ZOOM_LVL_BASE) < GEN_HASHY_SIZE) {
+ yl = GEN_HASHY(t - MAX_VEHICLE_PIXEL_Y * ZOOM_LVL_BASE);
+ yu = GEN_HASHY(b);
} else {
/* scan whole column */
yl = 0;
- yu = 0x3F << 6;
+ yu = GEN_HASHY_MASK;
}
- for (int y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) {
- for (int x = xl;; x = (x + 1) & 0x3F) {
+ for (int y = yl;; y = (y + GEN_HASHY_INC) & GEN_HASHY_MASK) {
+ for (int x = xl;; x = (x + GEN_HASHX_INC) & GEN_HASHX_MASK) {
const Vehicle *v = _vehicle_viewport_hash[x + y]; // already masked & 0xFFF
while (v != NULL) {