mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
This commit is contained in:
parent
66fcdc5e6d
commit
59333131a5
42
vehicle.c
42
vehicle.c
|
@ -2004,19 +2004,41 @@ uint32 VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y)
|
||||||
|
|
||||||
UnitID GetFreeUnitNumber(byte type)
|
UnitID GetFreeUnitNumber(byte type)
|
||||||
{
|
{
|
||||||
UnitID unit_num = 0;
|
UnitID unit, max;
|
||||||
Vehicle *u;
|
const Vehicle *u;
|
||||||
|
static bool *cache = NULL;
|
||||||
|
static UnitID gmax = 0;
|
||||||
|
|
||||||
restart:
|
switch (type) {
|
||||||
unit_num++;
|
case VEH_Train: max = _patches.max_trains; break;
|
||||||
FOR_ALL_VEHICLES(u) {
|
case VEH_Road: max = _patches.max_roadveh; break;
|
||||||
if (u->type == type && u->owner == _current_player &&
|
case VEH_Ship: max = _patches.max_ships; break;
|
||||||
unit_num == u->unitnumber)
|
case VEH_Aircraft: max = _patches.max_aircraft; break;
|
||||||
goto restart;
|
default: assert(0);
|
||||||
}
|
}
|
||||||
return unit_num;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (max > gmax) {
|
||||||
|
gmax = max;
|
||||||
|
free(cache);
|
||||||
|
cache = malloc((max + 1) * sizeof(*cache));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear the cache
|
||||||
|
memset(cache, 0, (max + 1) * sizeof(*cache));
|
||||||
|
|
||||||
|
// Fill the cache
|
||||||
|
FOR_ALL_VEHICLES(u) {
|
||||||
|
if (u->type == type && u->owner == _current_player && u->unitnumber != 0 && u->unitnumber <= max)
|
||||||
|
cache[u->unitnumber] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the first unused unit number
|
||||||
|
for (unit = 1; unit <= max; unit++) {
|
||||||
|
if (!cache[unit]) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return unit;
|
||||||
|
}
|
||||||
|
|
||||||
// Save and load of vehicles
|
// Save and load of vehicles
|
||||||
const SaveLoad _common_veh_desc[] = {
|
const SaveLoad _common_veh_desc[] = {
|
||||||
|
|
Loading…
Reference in New Issue