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

Function NetworkAutoCleanCompanies

src/network/network_server.cpp:1551–1601  ·  view source on GitHub ↗

* Remove companies that have not been used depending on the \c autoclean_companies setting * and values for \c autoclean_protected, which removes any company, and * \c autoclean_novehicles, which removes companies without vehicles. */

Source from the content-addressed store, hash-verified

1549 * \c autoclean_novehicles, which removes companies without vehicles.
1550 */
1551static void NetworkAutoCleanCompanies()
1552{
1553 CompanyMask has_clients{};
1554 CompanyMask has_vehicles{};
1555
1556 if (!_settings_client.network.autoclean_companies) return;
1557
1558 /* Detect the active companies */
1559 for (const NetworkClientInfo *ci : NetworkClientInfo::Iterate()) {
1560 if (Company::IsValidID(ci->client_playas)) has_clients.Set(ci->client_playas);
1561 }
1562
1563 if (!_network_dedicated) {
1564 const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
1565 assert(ci != nullptr);
1566 if (Company::IsValidID(ci->client_playas)) has_clients.Set(ci->client_playas);
1567 }
1568
1569 if (_settings_client.network.autoclean_novehicles != 0) {
1570 for (const Company *c : Company::Iterate()) {
1571 if (std::any_of(std::begin(c->group_all), std::end(c->group_all), [](const GroupStatistics &gs) { return gs.num_vehicle != 0; })) has_vehicles.Set(c->index);
1572 }
1573 }
1574
1575 /* Go through all the companies */
1576 for (Company *c : Company::Iterate()) {
1577 /* Skip the non-active once */
1578 if (c->is_ai) continue;
1579
1580 if (!has_clients.Test(c->index)) {
1581 /* The company is empty for one month more */
1582 if (c->months_empty != std::numeric_limits<decltype(c->months_empty)>::max()) c->months_empty++;
1583
1584 /* Is the company empty for autoclean_protected-months? */
1585 if (_settings_client.network.autoclean_protected != 0 && c->months_empty > _settings_client.network.autoclean_protected) {
1586 /* Shut the company down */
1587 Command<CMD_COMPANY_CTRL>::Post(CCA_DELETE, c->index, CRR_AUTOCLEAN, INVALID_CLIENT_ID);
1588 IConsolePrint(CC_INFO, "Auto-cleaned company #{}.", c->index + 1);
1589 }
1590 /* Is the company empty for autoclean_novehicles-months, and has no vehicles? */
1591 if (_settings_client.network.autoclean_novehicles != 0 && c->months_empty > _settings_client.network.autoclean_novehicles && !has_vehicles.Test(c->index)) {
1592 /* Shut the company down */
1593 Command<CMD_COMPANY_CTRL>::Post(CCA_DELETE, c->index, CRR_AUTOCLEAN, INVALID_CLIENT_ID);
1594 IConsolePrint(CC_INFO, "Auto-cleaned company #{} with no vehicles.", c->index + 1);
1595 }
1596 } else {
1597 /* It is not empty, reset the date */
1598 c->months_empty = 0;
1599 }
1600 }
1601}
1602
1603/**
1604 * Check whether a name is unique, and otherwise try to make it unique.

Callers 1

network_server.cppFile · 0.85

Calls 5

TestMethod · 0.80
beginFunction · 0.50
endFunction · 0.50
IConsolePrintFunction · 0.50
SetMethod · 0.45

Tested by

no test coverage detected