diff --git a/src/road.cpp b/src/road.cpp index 399971c..3f0d436 100644 --- a/src/road.cpp +++ b/src/road.cpp @@ -247,3 +247,54 @@ bool RoadTypeIdentifier::UnpackIfValid(uint32 data) assert(ret); return result; } + +/** + * Returns the available RoadSubTypes for the provided RoadType + * @param rt the RoadType to filter + * @param c the company to check the roadtype against + * @param only_existing whether to return only currently introduced vehicles or also future ones + * @returns the existing RoadSubTypes + */ +RoadSubTypes ExistingRoadSubTypesForRoadType(RoadType rt, CompanyID c, bool only_existing = false) +{ + RoadSubTypes known_roadsubtypes = ROADSUBTYPES_NONE; + RoadSubTypes available_roadsubtypes = ROADSUBTYPES_NONE; + + /* Road is always visible and available. */ + if (rt == ROADTYPE_ROAD) known_roadsubtypes |= ROADSUBTYPES_NORMAL; // TODO + + /* Find used roadtypes */ + Engine *e; + FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { + /* Check if the subtype can be used in the current climate */ + if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue; + + RoadTypeIdentifier rtid = e->GetRoadType(); + if (rtid.basetype != rt) continue; + + known_roadsubtypes |= GetRoadTypeInfo(rtid)->introduces_roadtypes; + } + + if (!only_existing) { + /* Get the date introduced roadtypes as well. */ + known_roadsubtypes = AddDateIntroducedRoadTypes(rt, known_roadsubtypes, MAX_DAY); + } + + const Company *company = Company::GetIfValid(c); + + /* If it's not used ever, don't show it to the user. */ + RoadTypeIdentifier rtid; + FOR_ALL_SORTED_ROADTYPES(rtid, rt) { + if (!HasBit(known_roadsubtypes, rtid.subtype)) continue; + + if (company != NULL) { + if (only_existing && !HasBit(company->avail_roadtypes[rtid.basetype], rtid.subtype)) { + continue; + } + } + + SetBit(available_roadsubtypes, rtid.subtype); + } + + return available_roadsubtypes; +}