changeset 18202:f5b712935c89 draft

(svn r23031) -Fix [FS#4804]: for the admin "bots" there was no distinction between bankruptcy and manual removal of companies even though the API suggested that
author rubidium <rubidium@openttd.org>
date Sat, 15 Oct 2011 20:42:32 +0000
parents fbfc95479140
children 67b103c3d56e
files src/ai/ai_gui.cpp src/company_cmd.cpp src/company_type.h src/console_cmds.cpp src/economy.cpp src/network/core/tcp_admin.cpp src/network/core/tcp_admin.h src/network/network_server.cpp
diffstat 8 files changed, 41 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/ai_gui.cpp
+++ b/src/ai/ai_gui.cpp
@@ -1027,7 +1027,7 @@
 		switch (widget) {
 			case AID_WIDGET_RELOAD_TOGGLE:
 				/* First kill the company of the AI, then start a new one. This should start the current AI again */
-				DoCommandP(0, 2 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
+				DoCommandP(0, 2 | ai_debug_company << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
 				DoCommandP(0, 1 | ai_debug_company << 16, 0, CMD_COMPANY_CTRL);
 				break;
 
--- a/src/company_cmd.cpp
+++ b/src/company_cmd.cpp
@@ -770,13 +770,14 @@
 }
 
 /**
- * Called whenever a company goes bankrupt in order to notify admins.
- * @param company_id The company that went bankrupt.
+ * Called whenever a company is removed in order to notify admins.
+ * @param company_id The company that was removed.
+ * @param reason     The reason the company was removed.
  */
-void CompanyAdminBankrupt(CompanyID company_id)
+void CompanyAdminRemove(CompanyID company_id, CompanyRemoveReason reason)
 {
 #ifdef ENABLE_NETWORK
-	if (_network_server) NetworkAdminCompanyRemove(company_id, ADMIN_CRR_BANKRUPT);
+	if (_network_server) NetworkAdminCompanyRemove(company_id, (AdminCompanyRemoveReason)reason);
 #endif /* ENABLE_NETWORK */
 }
 
@@ -843,7 +844,6 @@
 			}
 
 			if (_network_server) {
-				CompanyID old_playas = ci->client_playas;
 				ci->client_playas = c->index;
 				NetworkUpdateClientInfo(ci->client_id);
 
@@ -882,6 +882,9 @@
 			break;
 
 		case 2: { // Delete a company
+			CompanyRemoveReason reason = (CompanyRemoveReason)GB(p2, 0, 2);
+			if (reason >= CRR_END) return CMD_ERROR;
+
 			Company *c = Company::GetIfValid(company_id);
 			if (c == NULL) return CMD_ERROR;
 
@@ -905,7 +908,7 @@
 			CompanyID c_index = c->index;
 			delete c;
 			AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
-			CompanyAdminBankrupt(c_index);
+			CompanyAdminRemove(c_index, (CompanyRemoveReason)reason);
 			break;
 		}
 
--- a/src/company_type.h
+++ b/src/company_type.h
@@ -54,4 +54,13 @@
 struct Company;
 typedef uint32 CompanyManagerFace; ///< Company manager face bits, info see in company_manager_face.h
 
+/** The reason why the company was removed. */
+enum CompanyRemoveReason {
+	CRR_MANUAL,    ///< The company is manually removed.
+	CRR_AUTOCLEAN, ///< The company is removed due to autoclean.
+	CRR_BANKRUPT,  ///< The company went belly-up.
+
+	CRR_END        ///< Sentinel for end.
+};
+
 #endif /* COMPANY_TYPE_H */
--- a/src/console_cmds.cpp
+++ b/src/console_cmds.cpp
@@ -836,7 +836,7 @@
 	}
 
 	/* It is safe to remove this company */
-	DoCommandP(0, 2 | index << 16, 0, CMD_COMPANY_CTRL);
+	DoCommandP(0, 2 | index << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
 	IConsolePrint(CC_DEFAULT, "Company deleted.");
 
 	return true;
@@ -1196,7 +1196,7 @@
 	}
 
 	/* First kill the company of the AI, then start a new one. This should start the current AI again */
-	DoCommandP(0, 2 | company_id << 16, 0, CMD_COMPANY_CTRL);
+	DoCommandP(0, 2 | company_id << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
 	DoCommandP(0, 1 | company_id << 16, 0, CMD_COMPANY_CTRL);
 	IConsolePrint(CC_DEFAULT, "AI reloaded.");
 
@@ -1233,7 +1233,7 @@
 	}
 
 	/* Now kill the company of the AI. */
-	DoCommandP(0, 2 | company_id << 16, 0, CMD_COMPANY_CTRL);
+	DoCommandP(0, 2 | company_id << 16, CRR_MANUAL, CMD_COMPANY_CTRL);
 	IConsolePrint(CC_DEFAULT, "AI stopped, company deleted.");
 
 	return true;
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -561,7 +561,7 @@
 			 * that changing the current company is okay. In case of single
 			 * player we are sure (the above check) that we are not the local
 			 * company and thus we won't be moved. */
-			if (!_networking || _network_server) DoCommandP(0, 2 | (c->index << 16), 0, CMD_COMPANY_CTRL);
+			if (!_networking || _network_server) DoCommandP(0, 2 | (c->index << 16), CRR_BANKRUPT, CMD_COMPANY_CTRL);
 			break;
 	}
 }
--- a/src/network/core/tcp_admin.cpp
+++ b/src/network/core/tcp_admin.cpp
@@ -19,6 +19,12 @@
 #include "tcp_admin.h"
 #include "../../debug.h"
 
+/* Make sure that these enums match. */
+assert_compile((int)CRR_MANUAL    == (int)ADMIN_CRR_MANUAL);
+assert_compile((int)CRR_AUTOCLEAN == (int)ADMIN_CRR_AUTOCLEAN);
+assert_compile((int)CRR_BANKRUPT  == (int)ADMIN_CRR_BANKRUPT);
+assert_compile((int)CRR_END       == (int)ADMIN_CRR_END);
+
 /**
  * Create the admin handler for the given socket.
  * @param s The socket to communicate over.
@@ -55,13 +61,13 @@
 		case ADMIN_PACKET_ADMIN_CHAT:             return this->Receive_ADMIN_CHAT(p);
 		case ADMIN_PACKET_ADMIN_RCON:             return this->Receive_ADMIN_RCON(p);
 
-		case ADMIN_PACKET_SERVER_FULL:             return this->Receive_SERVER_FULL(p);
-		case ADMIN_PACKET_SERVER_BANNED:           return this->Receive_SERVER_BANNED(p);
-		case ADMIN_PACKET_SERVER_ERROR:            return this->Receive_SERVER_ERROR(p);
-		case ADMIN_PACKET_SERVER_PROTOCOL:         return this->Receive_SERVER_PROTOCOL(p);
-		case ADMIN_PACKET_SERVER_WELCOME:          return this->Receive_SERVER_WELCOME(p);
-		case ADMIN_PACKET_SERVER_NEWGAME:          return this->Receive_SERVER_NEWGAME(p);
-		case ADMIN_PACKET_SERVER_SHUTDOWN:         return this->Receive_SERVER_SHUTDOWN(p);
+		case ADMIN_PACKET_SERVER_FULL:            return this->Receive_SERVER_FULL(p);
+		case ADMIN_PACKET_SERVER_BANNED:          return this->Receive_SERVER_BANNED(p);
+		case ADMIN_PACKET_SERVER_ERROR:           return this->Receive_SERVER_ERROR(p);
+		case ADMIN_PACKET_SERVER_PROTOCOL:        return this->Receive_SERVER_PROTOCOL(p);
+		case ADMIN_PACKET_SERVER_WELCOME:         return this->Receive_SERVER_WELCOME(p);
+		case ADMIN_PACKET_SERVER_NEWGAME:         return this->Receive_SERVER_NEWGAME(p);
+		case ADMIN_PACKET_SERVER_SHUTDOWN:        return this->Receive_SERVER_SHUTDOWN(p);
 
 		case ADMIN_PACKET_SERVER_DATE:            return this->Receive_SERVER_DATE(p);
 		case ADMIN_PACKET_SERVER_CLIENT_JOIN:     return this->Receive_SERVER_CLIENT_JOIN(p);
--- a/src/network/core/tcp_admin.h
+++ b/src/network/core/tcp_admin.h
@@ -99,7 +99,9 @@
 enum AdminCompanyRemoveReason {
 	ADMIN_CRR_MANUAL,    ///< The company is manually removed.
 	ADMIN_CRR_AUTOCLEAN, ///< The company is removed due to autoclean.
-	ADMIN_CRR_BANKRUPT   ///< The company went belly-up.
+	ADMIN_CRR_BANKRUPT,  ///< The company went belly-up.
+
+	ADMIN_CRR_END        ///< Sentinel for end.
 };
 
 /** Main socket handler for admin related connections. */
--- a/src/network/network_server.cpp
+++ b/src/network/network_server.cpp
@@ -1643,8 +1643,7 @@
 			/* 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 && StrEmpty(_network_company_states[c->index].password)) {
 				/* Shut the company down */
-				DoCommandP(0, 2 | c->index << 16, 0, CMD_COMPANY_CTRL);
-				NetworkAdminCompanyRemove(c->index, ADMIN_CRR_AUTOCLEAN);
+				DoCommandP(0, 2 | c->index << 16, CRR_AUTOCLEAN, CMD_COMPANY_CTRL);
 				IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d with no password", c->index + 1);
 			}
 			/* Is the company empty for autoclean_protected-months, and there is a protection? */
@@ -1658,8 +1657,7 @@
 			/* 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) {
 				/* Shut the company down */
-				DoCommandP(0, 2 | c->index << 16, 0, CMD_COMPANY_CTRL);
-				NetworkAdminCompanyRemove(c->index, ADMIN_CRR_AUTOCLEAN);
+				DoCommandP(0, 2 | c->index << 16, CRR_AUTOCLEAN, CMD_COMPANY_CTRL);
 				IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d with no vehicles", c->index + 1);
 			}
 		} else {