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

Function GetPreviewCompany

src/engine.cpp:914–939  ·  view source on GitHub ↗

* Get the best company for an engine preview. * @param e Engine to preview. * @return Best company if it exists, #CompanyID::Invalid() otherwise. */

Source from the content-addressed store, hash-verified

912 * @return Best company if it exists, #CompanyID::Invalid() otherwise.
913 */
914static CompanyID GetPreviewCompany(Engine *e)
915{
916 CompanyID best_company = CompanyID::Invalid();
917
918 /* For trains the cargomask has no useful meaning, since you can attach other wagons */
919 CargoTypes cargomask = e->type != VEH_TRAIN ? GetUnionOfArticulatedRefitMasks(e->index, true) : ALL_CARGOTYPES;
920
921 int32_t best_hist = -1;
922 for (const Company *c : Company::Iterate()) {
923 if (c->block_preview == 0 && !e->preview_asked.Test(c->index) &&
924 c->old_economy[0].performance_history > best_hist) {
925
926 /* Check whether the company uses similar vehicles */
927 for (const Vehicle *v : Vehicle::Iterate()) {
928 if (v->owner != c->index || v->type != e->type) continue;
929 if (!v->GetEngine()->CanCarryCargo() || !HasBit(cargomask, v->cargo_type)) continue;
930
931 best_hist = c->old_economy[0].performance_history;
932 best_company = c->index;
933 break;
934 }
935 }
936 }
937
938 return best_company;
939}
940
941/**
942 * Checks if a vehicle type is disabled for all/ai companies.

Callers 1

engine.cppFile · 0.85

Calls 6

InvalidFunction · 0.85
HasBitFunction · 0.85
TestMethod · 0.80
CanCarryCargoMethod · 0.80
GetEngineMethod · 0.45

Tested by

no test coverage detected