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

Function CanBuildVehicleInfrastructure

src/vehicle.cpp:1903–1941  ·  view source on GitHub ↗

* Check whether we can build infrastructure for the given * vehicle type. This to disable building stations etc. when * you are not allowed/able to have the vehicle type yet. * @param type the vehicle type to check this for * @return true if there is any reason why you may build * the infrastructure for the given vehicle type */

Source from the content-addressed store, hash-verified

1901 * the infrastructure for the given vehicle type
1902 */
1903bool CanBuildVehicleInfrastructure(VehicleType type, uint8_t subtype)
1904{
1905 assert(IsCompanyBuildableVehicleType(type));
1906
1907 if (!Company::IsValidID(_local_company)) return false;
1908
1909 UnitID max;
1910 switch (type) {
1911 case VEH_TRAIN:
1912 if (!HasAnyRailTypesAvail(_local_company)) return false;
1913 max = _settings_game.vehicle.max_trains;
1914 break;
1915 case VEH_ROAD:
1916 if (!HasAnyRoadTypesAvail(_local_company, (RoadTramType)subtype)) return false;
1917 max = _settings_game.vehicle.max_roadveh;
1918 break;
1919 case VEH_SHIP: max = _settings_game.vehicle.max_ships; break;
1920 case VEH_AIRCRAFT: max = _settings_game.vehicle.max_aircraft; break;
1921 default: NOT_REACHED();
1922 }
1923
1924 /* We can build vehicle infrastructure when we may build the vehicle type */
1925 if (max > 0) {
1926 /* Can we actually build the vehicle type? */
1927 for (const Engine *e : Engine::IterateType(type)) {
1928 if (type == VEH_ROAD && GetRoadTramType(e->VehInfo<RoadVehicleInfo>().roadtype) != (RoadTramType)subtype) continue;
1929 if (e->company_avail.Test(_local_company)) return true;
1930 }
1931 return false;
1932 }
1933
1934 /* We should be able to build infrastructure when we have the actual vehicle type */
1935 for (const Vehicle *v : Vehicle::Iterate()) {
1936 if (v->type == VEH_ROAD && GetRoadTramType(RoadVehicle::From(v)->roadtype) != (RoadTramType)subtype) continue;
1937 if (v->owner == _local_company && v->type == type) return true;
1938 }
1939
1940 return false;
1941}
1942
1943
1944/**

Callers 5

OnInvalidateDataMethod · 0.85
OnInvalidateDataMethod · 0.85
OnInvalidateDataMethod · 0.85
OnTooltipMethod · 0.85
OnInvalidateDataMethod · 0.85

Calls 6

HasAnyRailTypesAvailFunction · 0.85
HasAnyRoadTypesAvailFunction · 0.85
NOT_REACHEDFunction · 0.85
GetRoadTramTypeFunction · 0.85
TestMethod · 0.80

Tested by

no test coverage detected