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

Method GenerateBuildTrainList

src/build_vehicle_gui.cpp:1379–1447  ·  view source on GitHub ↗

Figure out what train EngineIDs to put in the list */

Source from the content-addressed store, hash-verified

1377
1378 /* Figure out what train EngineIDs to put in the list */
1379 void GenerateBuildTrainList(GUIEngineList &list)
1380 {
1381 FlatSet<EngineID> variants;
1382 EngineID sel_id = EngineID::Invalid();
1383 size_t num_engines = 0;
1384
1385 list.clear();
1386
1387 BadgeTextFilter btf(this->string_filter, GSF_TRAINS);
1388 BadgeDropdownFilter bdf(this->badge_filter_choices);
1389
1390 /* Make list of all available train engines and wagons.
1391 * Also check to see if the previously selected engine is still available,
1392 * and if not, reset selection to EngineID::Invalid(). This could be the case
1393 * when engines become obsolete and are removed */
1394 for (const Engine *e : Engine::IterateType(VEH_TRAIN)) {
1395 if (!this->show_hidden_engines && e->IsVariantHidden(_local_company)) continue;
1396 EngineID eid = e->index;
1397 const RailVehicleInfo *rvi = &e->VehInfo<RailVehicleInfo>();
1398
1399 if (this->filter.railtype != INVALID_RAILTYPE && !HasPowerOnRail(rvi->railtypes, this->filter.railtype)) continue;
1400 if (!IsEngineBuildable(eid, VEH_TRAIN, _local_company)) continue;
1401
1402 /* Filter now! So num_engines and num_wagons is valid */
1403 if (!FilterSingleEngine(eid)) continue;
1404
1405 if (!bdf.Filter(e->badges)) continue;
1406
1407 /* Filter by name or NewGRF extra text */
1408 if (!FilterByText(e) && !btf.Filter(e->badges)) continue;
1409
1410 list.emplace_back(eid, e->info.variant_id, e->display_flags, 0);
1411
1412 if (rvi->railveh_type != RAILVEH_WAGON) num_engines++;
1413
1414 /* Add all parent variants of this engine to the variant list */
1415 EngineID parent = e->info.variant_id;
1416 while (parent != EngineID::Invalid() && variants.insert(parent).second) {
1417 parent = Engine::Get(parent)->info.variant_id;
1418 }
1419
1420 if (eid == this->sel_engine) sel_id = eid;
1421 }
1422
1423 /* ensure primary engine of variant group is in list */
1424 for (const auto &variant : variants) {
1425 if (std::ranges::find(list, variant, &GUIEngineListItem::engine_id) == list.end()) {
1426 const Engine *e = Engine::Get(variant);
1427 list.emplace_back(variant, e->info.variant_id, e->display_flags | EngineDisplayFlag::Shaded, 0);
1428 if (e->VehInfo<RailVehicleInfo>().railveh_type != RAILVEH_WAGON) num_engines++;
1429 }
1430 }
1431
1432 this->SelectEngine(sel_id);
1433
1434 /* invalidate cached values for name sorter - engine names could change */
1435 _last_engine[0] = _last_engine[1] = EngineID::Invalid();
1436

Callers 1

GenerateBuildListMethod · 0.95

Calls 13

SelectEngineMethod · 0.95
InvalidFunction · 0.85
HasPowerOnRailFunction · 0.85
IsEngineBuildableFunction · 0.85
EngList_SortFunction · 0.85
EngList_SortPartialFunction · 0.85
IsVariantHiddenMethod · 0.80
findFunction · 0.50
clearMethod · 0.45
FilterMethod · 0.45
insertMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected