1
0
Fork 0

Compare commits

...

4 Commits

Author SHA1 Message Date
Peter Nelson 06a5fa6239 Codechange: Use CompanyMask to pass Companies to PopupMainCompanyToolbMenu().
It's like the type was designed for it.
2024-01-05 22:26:07 +00:00
Peter Nelson 6828b6014a Codechange: Use company group statistics to test for vehicles for drop down list state.
This avoids iterating full the vehicle pool to find out if a company has any vehicles of a particular type.
2024-01-05 22:26:07 +00:00
SamuXarick 847f3f660d
Fix #10511: Delay 'go to nearest depot' orders (#11548)
Delay the nearest depot order search for a day if the vehicle can't find its destination, which happens when it has already attempted to do so and failed to find a valid destination.
2024-01-05 21:23:01 +00:00
Peter Nelson 7a5106042f
Codechange: Use CompanyMask and company group statistics for auto clean tests. (#11693)
This function does not need to know how many vehicles there are, only that there is at least one.
2024-01-05 20:58:24 +00:00
3 changed files with 17 additions and 18 deletions

View File

@ -1521,30 +1521,25 @@ static void NetworkCheckRestartMap()
*/ */
static void NetworkAutoCleanCompanies() static void NetworkAutoCleanCompanies()
{ {
bool clients_in_company[MAX_COMPANIES]; CompanyMask has_clients = 0;
int vehicles_in_company[MAX_COMPANIES]; CompanyMask has_vehicles = 0;
if (!_settings_client.network.autoclean_companies) return; if (!_settings_client.network.autoclean_companies) return;
memset(clients_in_company, 0, sizeof(clients_in_company));
/* Detect the active companies */ /* Detect the active companies */
for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) { for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true; if (Company::IsValidID(ci->client_playas)) SetBit(has_clients, ci->client_playas);
} }
if (!_network_dedicated) { if (!_network_dedicated) {
const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER); const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
assert(ci != nullptr); assert(ci != nullptr);
if (Company::IsValidID(ci->client_playas)) clients_in_company[ci->client_playas] = true; if (Company::IsValidID(ci->client_playas)) SetBit(has_clients, ci->client_playas);
} }
if (_settings_client.network.autoclean_novehicles != 0) { if (_settings_client.network.autoclean_novehicles != 0) {
memset(vehicles_in_company, 0, sizeof(vehicles_in_company)); for (const Company *c : Company::Iterate()) {
if (std::any_of(std::begin(c->group_all), std::end(c->group_all), [](const GroupStatistics &gs) { return gs.num_vehicle != 0; })) SetBit(has_vehicles, c->index);
for (const Vehicle *v : Vehicle::Iterate()) {
if (!Company::IsValidID(v->owner) || !v->IsPrimaryVehicle()) continue;
vehicles_in_company[v->owner]++;
} }
} }
@ -1553,7 +1548,7 @@ static void NetworkAutoCleanCompanies()
/* Skip the non-active once */ /* Skip the non-active once */
if (c->is_ai) continue; if (c->is_ai) continue;
if (!clients_in_company[c->index]) { if (!HasBit(has_clients, c->index)) {
/* The company is empty for one month more */ /* The company is empty for one month more */
_network_company_states[c->index].months_empty++; _network_company_states[c->index].months_empty++;
@ -1572,7 +1567,7 @@ static void NetworkAutoCleanCompanies()
NetworkServerUpdateCompanyPassworded(c->index, false); NetworkServerUpdateCompanyPassworded(c->index, false);
} }
/* Is the company empty for autoclean_novehicles-months, and has no vehicles? */ /* Is the company empty for autoclean_novehicles-months, and has no vehicles? */
if (_settings_client.network.autoclean_novehicles != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_novehicles && vehicles_in_company[c->index] == 0) { if (_settings_client.network.autoclean_novehicles != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_novehicles && !HasBit(has_vehicles, c->index)) {
/* Shut the company down */ /* Shut the company down */
Command<CMD_COMPANY_CTRL>::Post(CCA_DELETE, c->index, CRR_AUTOCLEAN, INVALID_CLIENT_ID); Command<CMD_COMPANY_CTRL>::Post(CCA_DELETE, c->index, CRR_AUTOCLEAN, INVALID_CLIENT_ID);
IConsolePrint(CC_INFO, "Auto-cleaned company #{} with no vehicles.", c->index + 1); IConsolePrint(CC_INFO, "Auto-cleaned company #{} with no vehicles.", c->index + 1);

View File

@ -1961,6 +1961,10 @@ bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool
} }
if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) { if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
/* If the vehicle can't find its destination, delay its next search.
* In case many vehicles are in this state, use the vehicle index to spread out pathfinder calls. */
if (v->dest_tile == 0 && TimerGameCalendar::date_fract != (v->index % Ticks::DAY_TICKS)) break;
/* We need to search for the nearest depot (hangar). */ /* We need to search for the nearest depot (hangar). */
ClosestDepot closestDepot = v->FindClosestDepot(); ClosestDepot closestDepot = v->FindClosestDepot();

View File

@ -145,9 +145,9 @@ static const int CTMN_SPECTATOR = -3; ///< Show a company window as spectator
* Pop up a generic company list menu. * Pop up a generic company list menu.
* @param w The toolbar window. * @param w The toolbar window.
* @param widget The button widget id. * @param widget The button widget id.
* @param grey A bitbask of which items to mark as disabled. * @param grey A bitmask of which companies to mark as disabled.
*/ */
static void PopupMainCompanyToolbMenu(Window *w, WidgetID widget, int grey = 0) static void PopupMainCompanyToolbMenu(Window *w, WidgetID widget, CompanyMask grey = 0)
{ {
DropDownList list; DropDownList list;
@ -728,10 +728,10 @@ static CallBackFunction MenuClickIndustry(int index)
static void ToolbarVehicleClick(Window *w, VehicleType veh) static void ToolbarVehicleClick(Window *w, VehicleType veh)
{ {
int dis = ~0; CompanyMask dis = 0;
for (const Vehicle *v : Vehicle::Iterate()) { for (const Company *c : Company::Iterate()) {
if (v->type == veh && v->IsPrimaryVehicle()) ClrBit(dis, v->owner); if (c->group_all[veh].num_vehicle == 0) SetBit(dis, c->index);
} }
PopupMainCompanyToolbMenu(w, WID_TN_VEHICLE_START + veh, dis); PopupMainCompanyToolbMenu(w, WID_TN_VEHICLE_START + veh, dis);
} }