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

Function IsEngineBuildable

src/engine.cpp:1246–1279  ·  view source on GitHub ↗

* Check if an engine is buildable. * @param engine index of the engine to check. * @param type the type the engine should be. * @param company index of the company. * @return True if an engine is valid, of the specified type, and buildable by * the given company. */

Source from the content-addressed store, hash-verified

1244 * the given company.
1245 */
1246bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
1247{
1248 const Engine *e = Engine::GetIfValid(engine);
1249
1250 /* check if it's an engine that is in the engine array */
1251 if (e == nullptr) return false;
1252
1253 /* check if it's an engine of specified type */
1254 if (e->type != type) return false;
1255
1256 /* check if it's available ... */
1257 if (company == OWNER_DEITY) {
1258 /* ... for any company (preview does not count) */
1259 if (!e->flags.Test(EngineFlag::Available) || e->company_avail.None()) return false;
1260 } else {
1261 /* ... for this company */
1262 if (!e->company_avail.Test(company)) return false;
1263 }
1264
1265 if (!e->IsEnabled()) return false;
1266
1267 if (type == VEH_TRAIN && company != OWNER_DEITY) {
1268 /* Check if the rail type is available to this company */
1269 const Company *c = Company::Get(company);
1270 if (!GetAllCompatibleRailTypes(e->VehInfo<RailVehicleInfo>().railtypes).Any(c->avail_railtypes)) return false;
1271 }
1272 if (type == VEH_ROAD && company != OWNER_DEITY) {
1273 /* Check if the road type is available to this company */
1274 const Company *c = Company::Get(company);
1275 if (!GetRoadTypeInfo(e->VehInfo<RoadVehicleInfo>().roadtype)->powered_roadtypes.Any(c->avail_roadtypes)) return false;
1276 }
1277
1278 return true;
1279}
1280
1281/**
1282 * Check if an engine is refittable.

Callers 10

CheckAutoreplaceValidityFunction · 0.85
GetNewEngineTypeFunction · 0.85
CmdBuildVehicleFunction · 0.85
GenerateBuildShipListMethod · 0.85
CmdSetVehicleVisibilityFunction · 0.85
IsValidEngineMethod · 0.85
IsBuildableMethod · 0.85

Calls 6

GetRoadTypeInfoFunction · 0.85
TestMethod · 0.80
NoneMethod · 0.80
AnyMethod · 0.80
IsEnabledMethod · 0.45

Tested by

no test coverage detected