Index: src/newgrf_commons.cpp =================================================================== --- src/newgrf_commons.cpp (revision 12066) +++ src/newgrf_commons.cpp (working copy) @@ -107,30 +107,32 @@ */ uint16 OverrideManagerBase::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id) { - uint16 id = this->GetID(grf_local_id, grfid); - EntityIDMapping *map; + uint16 first_free_id = invalid_ID; - /* Look to see if this entity has already been added. This is done - * separately from the loop below in case a GRF has been deleted, and there - * are any gaps in the array. + /* Search all IDs for a free slot, and test if it has already been added. + * Note: In case a GRF has been deleted, there can be gaps in the array */ - if (id != invalid_ID) { - return id; - } + for (uint16 id = max_offset; id < max_new_entities; id++) { + EntityIDMapping *map = &mapping_ID[id]; - /* This entity hasn't been defined before, so give it an ID now. */ - for (id = max_offset; id < max_new_entities; id++) { - map = &mapping_ID[id]; + /* Has the entity already been added? */ + if (map->entity_id == grf_local_id && map->grfid == grfid) return id; - if (CheckValidNewID(id) && map->entity_id == 0 && map->grfid == 0) { - map->entity_id = grf_local_id; - map->grfid = grfid; - map->substitute_id = substitute_id; - return id; + /* Find and save first free slot */ + if (first_free_id == invalid_ID && CheckValidNewID(id) && map->entity_id == 0 && map->grfid == 0) { + first_free_id = id; } } - return invalid_ID; + /* The entity has not yet been added, add it to the first free slot (if there is one) */ + if (first_free_id != invalid_ID) { + EntityIDMapping *map = &mapping_ID[first_free_id]; + map->entity_id = grf_local_id; + map->grfid = grfid; + map->substitute_id = substitute_id; + } + + return first_free_id; } /** Gives the substitute of the entity, as specified by the grf file