summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2014-05-11 12:52:21 +0000
committerrubidium <rubidium@openttd.org>2014-05-11 12:52:21 +0000
commit4a523008385fe24dd2bc1683bb41c169285e6d3b (patch)
treed673bb01f3f80a4566979ddd91a68a6d76b1e310
parenta363b2c317c681888ca0f09c94f3c811f04b9921 (diff)
downloadopenttd-4a523008385fe24dd2bc1683bb41c169285e6d3b.tar.xz
(svn r26576) -Fix [FS#6003]: [Network] AIs would not reset certain network state information upon creation of their company
-rw-r--r--src/company_cmd.cpp50
-rw-r--r--src/network/network_func.h2
-rw-r--r--src/network/network_server.cpp27
-rw-r--r--src/network/network_server.h1
4 files changed, 34 insertions, 46 deletions
diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp
index d83df30af..d094836ed 100644
--- a/src/company_cmd.cpp
+++ b/src/company_cmd.cpp
@@ -855,59 +855,19 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3
MarkWholeScreenDirty();
}
- if (_network_server) {
- if (ci != NULL) {
- /* ci is only NULL when replaying.
- * When replaying no client is actually in need of an update. */
- ci->client_playas = c->index;
- NetworkUpdateClientInfo(ci->client_id);
- }
-
- if (Company::IsValidID(c->index)) {
- _network_company_states[c->index].months_empty = 0;
- _network_company_states[c->index].password[0] = '\0';
- NetworkServerUpdateCompanyPassworded(c->index, false);
-
- /* XXX - When a client joins, we automatically set its name to the
- * client's name (for some reason). As it stands now only the server
- * knows the client's name, so it needs to send out a "broadcast" to
- * do this. To achieve this we send a network command. However, it
- * uses _local_company to execute the command as. To prevent abuse
- * (eg. only yourself can change your name/company), we 'cheat' by
- * impersonation _local_company as the server. Not the best solution;
- * but it works.
- * TODO: Perhaps this could be improved by when the client is ready
- * with joining to let it send itself the command, and not the server?
- * For example in network_client.c:534? */
- if (ci != NULL) {
- /* ci is only NULL when replaying.
- * When replaying, the command to rename the president will
- * automatically be ran, so this is not even needed to get
- * the exact same state. */
- NetworkSendCommand(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, ci->client_name, c->index);
- }
- }
-
- /* Announce new company on network. */
- NetworkAdminCompanyInfo(c, true);
-
- if (ci != NULL) {
- /* ci is only NULL when replaying.
- * When replaying, the message that someone started a new company
- * is not interesting at all. */
- NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, c->index + 1);
- }
- }
+ NetworkServerNewCompany(c, ci);
#endif /* ENABLE_NETWORK */
break;
}
- case 1: // Make a new AI company
+ case 1: { // Make a new AI company
if (!(flags & DC_EXEC)) return CommandCost();
if (company_id != INVALID_COMPANY && (company_id >= MAX_COMPANIES || Company::IsValidID(company_id))) return CMD_ERROR;
- DoStartupNewCompany(true, company_id);
+ Company *c = DoStartupNewCompany(true, company_id);
+ if (c != NULL) NetworkServerNewCompany(c, NULL);
break;
+ }
case 2: { // Delete a company
CompanyRemoveReason reason = (CompanyRemoveReason)GB(p2, 0, 2);
diff --git a/src/network/network_func.h b/src/network/network_func.h
index defa1cc70..67d4c8d48 100644
--- a/src/network/network_func.h
+++ b/src/network/network_func.h
@@ -71,7 +71,7 @@ void NetworkServerYearlyLoop();
void NetworkServerSendConfigUpdate();
void NetworkServerShowStatusToConsole();
bool NetworkServerStart();
-void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded);
+void NetworkServerNewCompany(const Company *company, NetworkClientInfo *ci);
bool NetworkServerChangeClientName(ClientID client_id, const char *new_name);
diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp
index 84337f5f7..9ac1a03aa 100644
--- a/src/network/network_server.cpp
+++ b/src/network/network_server.cpp
@@ -2173,4 +2173,31 @@ void NetworkPrintClients()
}
}
+/**
+ * Perform all the server specific administration of a new company.
+ * @param c The newly created company; can't be NULL.
+ * @param ci The client information of the client that made the company; can be NULL.
+ */
+void NetworkServerNewCompany(const Company *c, NetworkClientInfo *ci)
+{
+ assert(c != NULL);
+
+ if (!_network_server) return;
+
+ _network_company_states[c->index].months_empty = 0;
+ _network_company_states[c->index].password[0] = '\0';
+ NetworkServerUpdateCompanyPassworded(c->index, false);
+
+ if (ci != NULL) {
+ /* ci is NULL when replaying, or for AIs. In neither case there is a client. */
+ ci->client_playas = c->index;
+ NetworkUpdateClientInfo(ci->client_id);
+ NetworkSendCommand(0, 0, 0, CMD_RENAME_PRESIDENT, NULL, ci->client_name, c->index);
+ NetworkServerSendChat(NETWORK_ACTION_COMPANY_NEW, DESTTYPE_BROADCAST, 0, "", ci->client_id, c->index + 1);
+ }
+
+ /* Announce new company on network. */
+ NetworkAdminCompanyInfo(c, true);
+}
+
#endif /* ENABLE_NETWORK */
diff --git a/src/network/network_server.h b/src/network/network_server.h
index eb502dc10..a52b2c936 100644
--- a/src/network/network_server.h
+++ b/src/network/network_server.h
@@ -123,6 +123,7 @@ public:
void NetworkServer_Tick(bool send_frame);
void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed = true);
+void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded);
/**
* Iterate over all the sockets from a given starting point.