Changeset - r22758:0aeaa5bf84b8
[Not reviewed]
master
0 2 0
frosch - 6 years ago 2018-03-11 13:23:26
frosch@openttd.org
(svn r27986) -Codechange: Name magic constants in the vehicle position hash functions.
2 files changed with 37 insertions and 12 deletions:
0 comments (0 inline, 0 general)
src/tile_type.h
Show inline comments
 
@@ -15,12 +15,14 @@
 
static const uint TILE_SIZE           = 16;                    ///< Tile size in world coordinates.
 
static const uint TILE_UNIT_MASK      = TILE_SIZE - 1;         ///< For masking in/out the inner-tile world coordinate units.
 
static const uint TILE_PIXELS         = 32;                    ///< Pixel distance between tile columns/rows in #ZOOM_LVL_BASE.
 
static const uint TILE_HEIGHT         =  8;                    ///< Height of a height level in world coordinate AND in pixels in #ZOOM_LVL_BASE.
 

	
 
static const uint MAX_BUILDING_PIXELS = 200;                   ///< Maximum height of a building in pixels in #ZOOM_LVL_BASE. (Also applies to "bridge buildings" on the bridge floor.)
 
static const int MAX_VEHICLE_PIXEL_X  = 70;                    ///< Maximum width of a vehicle in pixels in #ZOOM_LVL_BASE.
 
static const int MAX_VEHICLE_PIXEL_Y  = 70;                    ///< Maximum height of a vehicle in pixels in #ZOOM_LVL_BASE.
 

	
 
static const uint MAX_TILE_HEIGHT     = 255;                   ///< Maximum allowed tile height
 

	
 
static const uint MIN_MAX_HEIGHTLEVEL = 15;                    ///< Lower bound of maximum allowed heightlevel (in the construction settings)
 
static const uint DEF_MAX_HEIGHTLEVEL = 30;                    ///< Default maximum allowed heightlevel (in the construction settings)
 
static const uint MAX_MAX_HEIGHTLEVEL = MAX_TILE_HEIGHT;       ///< Upper bound of maximum allowed heightlevel (in the construction settings)
src/vehicle.cpp
Show inline comments
 
@@ -54,13 +54,36 @@
 
#include "linkgraph/refresh.h"
 

	
 
#include "table/strings.h"
 

	
 
#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.
 
uint16 _returned_mail_refit_capacity; ///< Stores the mail capacity after a refit operation (Aircraft only).
 

	
 

	
 
@@ -615,13 +638,13 @@ static void UpdateVehicleTileHash(Vehicl
 
	}
 

	
 
	/* Remember current hash position */
 
	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)
 
{
 
	Vehicle **old_hash, **new_hash;
 
	int old_x = v->coord.left;
 
	int old_y = v->coord.top;
 
@@ -1085,32 +1108,32 @@ void ViewportAddVehicles(DrawPixelInfo *
 
	const int t = dpi->top;
 
	const int b = dpi->top + dpi->height;
 

	
 
	/* 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) {
 
				if (!(v->vehstatus & VS_HIDDEN) &&
 
						l <= v->coord.right &&
 
						t <= v->coord.bottom &&
0 comments (0 inline, 0 general)