MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / GetFreeUnitNumber

Function GetFreeUnitNumber

src/vehicle.cpp:1876–1892  ·  view source on GitHub ↗

* Get an unused unit number for a vehicle (if allowed). * @param type Type of vehicle * @return A unused unit number for the given type of vehicle if it is allowed to build one, else \c UINT16_MAX. */

Source from the content-addressed store, hash-verified

1874 * @return A unused unit number for the given type of vehicle if it is allowed to build one, else \c UINT16_MAX.
1875 */
1876UnitID GetFreeUnitNumber(VehicleType type)
1877{
1878 /* Check whether it is allowed to build another vehicle. */
1879 uint max_veh;
1880 switch (type) {
1881 case VEH_TRAIN: max_veh = _settings_game.vehicle.max_trains; break;
1882 case VEH_ROAD: max_veh = _settings_game.vehicle.max_roadveh; break;
1883 case VEH_SHIP: max_veh = _settings_game.vehicle.max_ships; break;
1884 case VEH_AIRCRAFT: max_veh = _settings_game.vehicle.max_aircraft; break;
1885 default: NOT_REACHED();
1886 }
1887
1888 const Company *c = Company::Get(_current_company);
1889 if (c->group_all[type].num_vehicle >= max_veh) return UINT16_MAX; // Currently already at the limit, no room to make a new one.
1890
1891 return c->freeunits[type].NextID();
1892}
1893
1894
1895/**

Callers 3

CheckNewTrainFunction · 0.85
NormaliseTrainHeadFunction · 0.85
CmdBuildVehicleFunction · 0.85

Calls 2

NOT_REACHEDFunction · 0.85
NextIDMethod · 0.80

Tested by

no test coverage detected