* Get the best company for an engine preview. * @param e Engine to preview. * @return Best company if it exists, #CompanyID::Invalid() otherwise. */
| 912 | * @return Best company if it exists, #CompanyID::Invalid() otherwise. |
| 913 | */ |
| 914 | static 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. |
no test coverage detected