Changeset - r28406:6f56267f7e6c
[Not reviewed]
master
0 1 0
Peter Nelson - 11 months ago 2024-01-05 20:58:24
peter1138@openttd.org
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.
1 file changed with 8 insertions and 13 deletions:
0 comments (0 inline, 0 general)
src/network/network_server.cpp
Show inline comments
 
@@ -1518,45 +1518,40 @@ static void NetworkCheckRestartMap()
 
 *     1) If a company is not protected, it is closed after 1 year (for example)
 
 *     2) If a company is protected, protection is disabled after 3 years (for example)
 
 *          (and item 1. happens a year later)
 
 */
 
static void NetworkAutoCleanCompanies()
 
{
 
	bool clients_in_company[MAX_COMPANIES];
 
	int vehicles_in_company[MAX_COMPANIES];
 
	CompanyMask has_clients = 0;
 
	CompanyMask has_vehicles = 0;
 

	
 
	if (!_settings_client.network.autoclean_companies) return;
 

	
 
	memset(clients_in_company, 0, sizeof(clients_in_company));
 

	
 
	/* Detect the active companies */
 
	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) {
 
		const NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
 
		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) {
 
		memset(vehicles_in_company, 0, sizeof(vehicles_in_company));
 

	
 
		for (const Vehicle *v : Vehicle::Iterate()) {
 
			if (!Company::IsValidID(v->owner) || !v->IsPrimaryVehicle()) continue;
 
			vehicles_in_company[v->owner]++;
 
		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);
 
		}
 
	}
 

	
 
	/* Go through all the companies */
 
	for (const Company *c : Company::Iterate()) {
 
		/* Skip the non-active once */
 
		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 */
 
			_network_company_states[c->index].months_empty++;
 

	
 
			/* Is the company empty for autoclean_unprotected-months, and is there no protection? */
 
			if (_settings_client.network.autoclean_unprotected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_unprotected && _network_company_states[c->index].password.empty()) {
 
				/* Shut the company down */
 
@@ -1569,13 +1564,13 @@ static void NetworkAutoCleanCompanies()
 
				_network_company_states[c->index].password.clear();
 
				IConsolePrint(CC_INFO, "Auto-removed protection from company #{}.", c->index + 1);
 
				_network_company_states[c->index].months_empty = 0;
 
				NetworkServerUpdateCompanyPassworded(c->index, false);
 
			}
 
			/* 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 */
 
				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);
 
			}
 
		} else {
 
			/* It is not empty, reset the date */
0 comments (0 inline, 0 general)