(svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.

This commit is contained in:
peter1138 2007-01-09 16:27:25 +00:00
parent c600e8cec5
commit 207f7eb167
2 changed files with 17 additions and 24 deletions

View File

@ -263,7 +263,7 @@ static Vehicle *InitializeVehicle(Vehicle *v)
v->left_coord = INVALID_COORD; v->left_coord = INVALID_COORD;
v->first = NULL; v->first = NULL;
v->next = NULL; v->next = NULL;
v->next_hash = INVALID_VEHICLE; v->next_hash = NULL;
v->string_id = 0; v->string_id = 0;
v->next_shared = NULL; v->next_shared = NULL;
v->prev_shared = NULL; v->prev_shared = NULL;
@ -368,7 +368,7 @@ bool AllocateVehicles(Vehicle **vl, int num)
} }
VehicleID _vehicle_position_hash[0x1000]; static Vehicle *_vehicle_position_hash[0x1000];
void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc) void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
{ {
@ -385,14 +385,13 @@ void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
for (y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) { for (y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) {
for (x = xl;; x = (x + 1) & 0x3F) { for (x = xl;; x = (x + 1) & 0x3F) {
VehicleID veh = _vehicle_position_hash[(x + y) & 0xFFFF]; Vehicle *v = _vehicle_position_hash[(x + y) & 0xFFFF];
while (veh != INVALID_VEHICLE) { while (v != NULL) {
Vehicle *v = GetVehicle(veh);
void* a = proc(v, data); void* a = proc(v, data);
if (a != NULL) return a; if (a != NULL) return a;
veh = v->next_hash; v = v->next_hash;
} }
if (x == xu) break; if (x == xu) break;
@ -406,10 +405,9 @@ void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
static void UpdateVehiclePosHash(Vehicle* v, int x, int y) static void UpdateVehiclePosHash(Vehicle* v, int x, int y)
{ {
VehicleID *old_hash, *new_hash; Vehicle **old_hash, **new_hash;
int old_x = v->left_coord; int old_x = v->left_coord;
int old_y = v->top_coord; int old_y = v->top_coord;
Vehicle *u;
new_hash = (x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(x,y)]; 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)]; old_hash = (old_x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(old_x, old_y)];
@ -419,11 +417,11 @@ static void UpdateVehiclePosHash(Vehicle* v, int x, int y)
/* remove from hash table? */ /* remove from hash table? */
if (old_hash != NULL) { if (old_hash != NULL) {
Vehicle *last = NULL; Vehicle *last = NULL;
VehicleID idx = *old_hash; Vehicle *u = *old_hash;
while ((u = GetVehicle(idx)) != v) { while (u != v) {
idx = u->next_hash;
assert(idx != INVALID_VEHICLE);
last = u; last = u;
u = u->next_hash;
assert(u != NULL);
} }
if (last == NULL) { if (last == NULL) {
@ -436,16 +434,13 @@ static void UpdateVehiclePosHash(Vehicle* v, int x, int y)
/* insert into hash table? */ /* insert into hash table? */
if (new_hash != NULL) { if (new_hash != NULL) {
v->next_hash = *new_hash; v->next_hash = *new_hash;
*new_hash = v->index; *new_hash = v;
} }
} }
void ResetVehiclePosHash(void) void ResetVehiclePosHash(void)
{ {
uint i; memset(_vehicle_position_hash, 0, sizeof(_vehicle_position_hash));
for (i = 0; i < lengthof(_vehicle_position_hash); i++) {
_vehicle_position_hash[i] = INVALID_VEHICLE;
}
} }
void InitializeVehicles(void) void InitializeVehicles(void)
@ -574,7 +569,7 @@ void DestroyVehicle(Vehicle *v)
} }
UpdateVehiclePosHash(v, INVALID_COORD, 0); UpdateVehiclePosHash(v, INVALID_COORD, 0);
v->next_hash = INVALID_VEHICLE; v->next_hash = NULL;
if (v->orders != NULL) DeleteVehicleOrders(v); if (v->orders != NULL) DeleteVehicleOrders(v);
/* Now remove any artic part. This will trigger an other /* Now remove any artic part. This will trigger an other
@ -831,11 +826,9 @@ void ViewportAddVehicles(DrawPixelInfo *dpi)
for (y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) { for (y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) {
for (x = xl;; x = (x + 1) & 0x3F) { for (x = xl;; x = (x + 1) & 0x3F) {
VehicleID veh = _vehicle_position_hash[(x + y) & 0xFFFF]; const Vehicle *v = _vehicle_position_hash[(x + y) & 0xFFFF];
while (veh != INVALID_VEHICLE) {
const Vehicle* v = GetVehicle(veh);
while (v != NULL) {
if (!(v->vehstatus & VS_HIDDEN) && if (!(v->vehstatus & VS_HIDDEN) &&
l <= v->right_coord && l <= v->right_coord &&
t <= v->bottom_coord && t <= v->bottom_coord &&
@ -843,7 +836,7 @@ void ViewportAddVehicles(DrawPixelInfo *dpi)
b >= v->top_coord) { b >= v->top_coord) {
DoDrawVehicle(v); DoDrawVehicle(v);
} }
veh = v->next_hash; v = v->next_hash;
} }
if (x == xu) break; if (x == xu) break;

View File

@ -219,7 +219,7 @@ struct Vehicle {
int32 top_coord; int32 top_coord;
int32 right_coord; int32 right_coord;
int32 bottom_coord; int32 bottom_coord;
VehicleID next_hash; Vehicle *next_hash;
// Related to age and service time // Related to age and service time
Date age; // Age in days Date age; // Age in days