|
@@ -283,20 +283,20 @@ const int TOTAL_HASH_SIZE = 1 << (HASH_B
|
|
|
const int TOTAL_HASH_MASK = TOTAL_HASH_SIZE - 1;
|
|
|
|
|
|
/* Resolution of the hash, 0 = 1*1 tile, 1 = 2*2 tiles, 2 = 4*4 tiles, etc.
|
|
|
* Profiling results show that 0 is fastest. */
|
|
|
const int HASH_RES = 0;
|
|
|
|
|
|
static Vehicle *_new_vehicle_position_hash[TOTAL_HASH_SIZE];
|
|
|
static Vehicle *_vehicle_tile_hash[TOTAL_HASH_SIZE];
|
|
|
|
|
|
static Vehicle *VehicleFromHash(int xl, int yl, int xu, int yu, void *data, VehicleFromPosProc *proc, bool find_first)
|
|
|
static Vehicle *VehicleFromTileHash(int xl, int yl, int xu, int yu, void *data, VehicleFromPosProc *proc, bool find_first)
|
|
|
{
|
|
|
for (int y = yl; ; y = (y + (1 << HASH_BITS)) & (HASH_MASK << HASH_BITS)) {
|
|
|
for (int x = xl; ; x = (x + 1) & HASH_MASK) {
|
|
|
Vehicle *v = _new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK];
|
|
|
for (; v != NULL; v = v->next_new_hash) {
|
|
|
Vehicle *v = _vehicle_tile_hash[(x + y) & TOTAL_HASH_MASK];
|
|
|
for (; v != NULL; v = v->hash_tile_next) {
|
|
|
Vehicle *a = proc(v, data);
|
|
|
if (find_first && a != NULL) return a;
|
|
|
}
|
|
|
if (x == xu) break;
|
|
|
}
|
|
|
if (y == yu) break;
|
|
@@ -324,13 +324,13 @@ static Vehicle *VehicleFromPosXY(int x,
|
|
|
/* Hash area to scan is from xl,yl to xu,yu */
|
|
|
int xl = GB((x - COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS);
|
|
|
int xu = GB((x + COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS);
|
|
|
int yl = GB((y - COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS) << HASH_BITS;
|
|
|
int yu = GB((y + COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS) << HASH_BITS;
|
|
|
|
|
|
return VehicleFromHash(xl, yl, xu, yu, data, proc, find_first);
|
|
|
return VehicleFromTileHash(xl, yl, xu, yu, data, proc, find_first);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Find a vehicle from a specific location. It will call proc for ALL vehicles
|
|
|
* on the tile and YOU must make SURE that the "best one" is stored in the
|
|
|
* data value and is ALWAYS the same regardless of the order of the vehicles
|
|
@@ -377,14 +377,14 @@ bool HasVehicleOnPosXY(int x, int y, voi
|
|
|
*/
|
|
|
static Vehicle *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc, bool find_first)
|
|
|
{
|
|
|
int x = GB(TileX(tile), HASH_RES, HASH_BITS);
|
|
|
int y = GB(TileY(tile), HASH_RES, HASH_BITS) << HASH_BITS;
|
|
|
|
|
|
Vehicle *v = _new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK];
|
|
|
for (; v != NULL; v = v->next_new_hash) {
|
|
|
Vehicle *v = _vehicle_tile_hash[(x + y) & TOTAL_HASH_MASK];
|
|
|
for (; v != NULL; v = v->hash_tile_next) {
|
|
|
if (v->tile != tile) continue;
|
|
|
|
|
|
Vehicle *a = proc(v, data);
|
|
|
if (find_first && a != NULL) return a;
|
|
|
}
|
|
|
|
|
@@ -515,79 +515,79 @@ CommandCost EnsureNoTrainOnTrackBits(Til
|
|
|
*/
|
|
|
Vehicle *v = VehicleFromPos(tile, &track_bits, &EnsureNoTrainOnTrackProc, true);
|
|
|
if (v != NULL) return_cmd_error(STR_ERROR_TRAIN_IN_THE_WAY + v->type);
|
|
|
return CommandCost();
|
|
|
}
|
|
|
|
|
|
static void UpdateNewVehiclePosHash(Vehicle *v, bool remove)
|
|
|
static void UpdateVehicleTileHash(Vehicle *v, bool remove)
|
|
|
{
|
|
|
Vehicle **old_hash = v->old_new_hash;
|
|
|
Vehicle **old_hash = v->hash_tile_current;
|
|
|
Vehicle **new_hash;
|
|
|
|
|
|
if (remove) {
|
|
|
new_hash = NULL;
|
|
|
} else {
|
|
|
int x = GB(TileX(v->tile), HASH_RES, HASH_BITS);
|
|
|
int y = GB(TileY(v->tile), HASH_RES, HASH_BITS) << HASH_BITS;
|
|
|
new_hash = &_new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK];
|
|
|
new_hash = &_vehicle_tile_hash[(x + y) & TOTAL_HASH_MASK];
|
|
|
}
|
|
|
|
|
|
if (old_hash == new_hash) return;
|
|
|
|
|
|
/* Remove from the old position in the hash table */
|
|
|
if (old_hash != NULL) {
|
|
|
if (v->next_new_hash != NULL) v->next_new_hash->prev_new_hash = v->prev_new_hash;
|
|
|
*v->prev_new_hash = v->next_new_hash;
|
|
|
if (v->hash_tile_next != NULL) v->hash_tile_next->hash_tile_prev = v->hash_tile_prev;
|
|
|
*v->hash_tile_prev = v->hash_tile_next;
|
|
|
}
|
|
|
|
|
|
/* Insert vehicle at beginning of the new position in the hash table */
|
|
|
if (new_hash != NULL) {
|
|
|
v->next_new_hash = *new_hash;
|
|
|
if (v->next_new_hash != NULL) v->next_new_hash->prev_new_hash = &v->next_new_hash;
|
|
|
v->prev_new_hash = new_hash;
|
|
|
v->hash_tile_next = *new_hash;
|
|
|
if (v->hash_tile_next != NULL) v->hash_tile_next->hash_tile_prev = &v->hash_tile_next;
|
|
|
v->hash_tile_prev = new_hash;
|
|
|
*new_hash = v;
|
|
|
}
|
|
|
|
|
|
/* Remember current hash position */
|
|
|
v->old_new_hash = new_hash;
|
|
|
v->hash_tile_current = new_hash;
|
|
|
}
|
|
|
|
|
|
static Vehicle *_vehicle_position_hash[0x1000];
|
|
|
static Vehicle *_vehicle_viewport_hash[0x1000];
|
|
|
|
|
|
static void UpdateVehiclePosHash(Vehicle *v, int x, int y)
|
|
|
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;
|
|
|
|
|
|
new_hash = (x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(x, y)];
|
|
|
old_hash = (old_x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(old_x, old_y)];
|
|
|
new_hash = (x == INVALID_COORD) ? NULL : &_vehicle_viewport_hash[GEN_HASH(x, y)];
|
|
|
old_hash = (old_x == INVALID_COORD) ? NULL : &_vehicle_viewport_hash[GEN_HASH(old_x, old_y)];
|
|
|
|
|
|
if (old_hash == new_hash) return;
|
|
|
|
|
|
/* remove from hash table? */
|
|
|
if (old_hash != NULL) {
|
|
|
if (v->next_hash != NULL) v->next_hash->prev_hash = v->prev_hash;
|
|
|
*v->prev_hash = v->next_hash;
|
|
|
if (v->hash_viewport_next != NULL) v->hash_viewport_next->hash_viewport_prev = v->hash_viewport_prev;
|
|
|
*v->hash_viewport_prev = v->hash_viewport_next;
|
|
|
}
|
|
|
|
|
|
/* insert into hash table? */
|
|
|
if (new_hash != NULL) {
|
|
|
v->next_hash = *new_hash;
|
|
|
if (v->next_hash != NULL) v->next_hash->prev_hash = &v->next_hash;
|
|
|
v->prev_hash = new_hash;
|
|
|
v->hash_viewport_next = *new_hash;
|
|
|
if (v->hash_viewport_next != NULL) v->hash_viewport_next->hash_viewport_prev = &v->hash_viewport_next;
|
|
|
v->hash_viewport_prev = new_hash;
|
|
|
*new_hash = v;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void ResetVehiclePosHash()
|
|
|
void ResetVehicleHash()
|
|
|
{
|
|
|
Vehicle *v;
|
|
|
FOR_ALL_VEHICLES(v) { v->old_new_hash = NULL; }
|
|
|
memset(_vehicle_position_hash, 0, sizeof(_vehicle_position_hash));
|
|
|
memset(_new_vehicle_position_hash, 0, sizeof(_new_vehicle_position_hash));
|
|
|
FOR_ALL_VEHICLES(v) { v->hash_tile_current = NULL; }
|
|
|
memset(_vehicle_viewport_hash, 0, sizeof(_vehicle_viewport_hash));
|
|
|
memset(_vehicle_tile_hash, 0, sizeof(_vehicle_tile_hash));
|
|
|
}
|
|
|
|
|
|
void ResetVehicleColourMap()
|
|
|
{
|
|
|
Vehicle *v;
|
|
|
FOR_ALL_VEHICLES(v) { v->colourmap = PAL_NONE; }
|
|
@@ -600,13 +600,13 @@ void ResetVehicleColourMap()
|
|
|
typedef SmallMap<Vehicle *, bool, 4> AutoreplaceMap;
|
|
|
static AutoreplaceMap _vehicles_to_autoreplace;
|
|
|
|
|
|
void InitializeVehicles()
|
|
|
{
|
|
|
_vehicles_to_autoreplace.Reset();
|
|
|
ResetVehiclePosHash();
|
|
|
ResetVehicleHash();
|
|
|
}
|
|
|
|
|
|
uint CountVehiclesInChain(const Vehicle *v)
|
|
|
{
|
|
|
uint count = 0;
|
|
|
do count++; while ((v = v->Next()) != NULL);
|
|
@@ -788,14 +788,14 @@ Vehicle::~Vehicle()
|
|
|
|
|
|
Vehicle *v = this->Next();
|
|
|
this->SetNext(NULL);
|
|
|
|
|
|
delete v;
|
|
|
|
|
|
UpdateVehiclePosHash(this, INVALID_COORD, 0);
|
|
|
UpdateNewVehiclePosHash(this, true);
|
|
|
UpdateVehicleTileHash(this, true);
|
|
|
UpdateVehicleViewportHash(this, INVALID_COORD, 0);
|
|
|
DeleteVehicleNews(this->index, INVALID_STRING_ID);
|
|
|
DeleteNewGRFInspectWindow(GetGrfSpecFeature(this->type), this->index);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Adds a vehicle to the list of vehicles that visited a depot this tick
|
|
@@ -996,23 +996,23 @@ void ViewportAddVehicles(DrawPixelInfo *
|
|
|
yl = 0;
|
|
|
yu = 0x3F << 6;
|
|
|
}
|
|
|
|
|
|
for (int y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) {
|
|
|
for (int x = xl;; x = (x + 1) & 0x3F) {
|
|
|
const Vehicle *v = _vehicle_position_hash[x + y]; // already masked & 0xFFF
|
|
|
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 &&
|
|
|
r >= v->coord.left &&
|
|
|
b >= v->coord.top) {
|
|
|
DoDrawVehicle(v);
|
|
|
}
|
|
|
v = v->next_hash;
|
|
|
v = v->hash_viewport_next;
|
|
|
}
|
|
|
|
|
|
if (x == xu) break;
|
|
|
}
|
|
|
|
|
|
if (y == yu) break;
|
|
@@ -1391,22 +1391,22 @@ void VehicleEnterDepot(Vehicle *v)
|
|
|
* if requested.
|
|
|
* @param v vehicle to move
|
|
|
* @param update_viewport whether to dirty the viewport
|
|
|
*/
|
|
|
void VehicleMove(Vehicle *v, bool update_viewport)
|
|
|
{
|
|
|
UpdateNewVehiclePosHash(v, false);
|
|
|
UpdateVehicleTileHash(v, false);
|
|
|
|
|
|
int img = v->cur_image;
|
|
|
Point pt = RemapCoords(v->x_pos + v->x_offs, v->y_pos + v->y_offs, v->z_pos);
|
|
|
const Sprite *spr = GetSprite(img, ST_NORMAL);
|
|
|
|
|
|
pt.x += spr->x_offs;
|
|
|
pt.y += spr->y_offs;
|
|
|
|
|
|
UpdateVehiclePosHash(v, pt.x, pt.y);
|
|
|
UpdateVehicleViewportHash(v, pt.x, pt.y);
|
|
|
|
|
|
Rect old_coord = v->coord;
|
|
|
v->coord.left = pt.x;
|
|
|
v->coord.top = pt.y;
|
|
|
v->coord.right = pt.x + spr->width + 2 * ZOOM_LVL_BASE;
|
|
|
v->coord.bottom = pt.y + spr->height + 2 * ZOOM_LVL_BASE;
|