Changeset - r11951:a522cb996935
[Not reviewed]
master
0 11 0
rubidium - 16 years ago 2009-05-19 21:20:14
rubidium@openttd.org
(svn r16360) -Codechange: don't use _network_playas as a 'second' _local_company, but only as a storage location for the company you want to join in MP.
11 files changed with 38 insertions and 58 deletions:
0 comments (0 inline, 0 general)
src/company_cmd.cpp
Show inline comments
 
@@ -719,16 +719,16 @@ CommandCost CmdCompanyCtrl(TileIndex til
 
			if (!_networking) return CMD_ERROR;
 

	
 
#ifdef ENABLE_NETWORK
 

	
 
			/* Joining Client:
 
			 * _local_company: COMPANY_SPECTATOR
 
			 * _network_playas/cid = requested company/clientid
 
			 * cid = clientid
 
			 *
 
			 * Other client(s)/server:
 
			 * _local_company/_network_playas: what they play as
 
			 * _local_company: what they play as
 
			 * cid = requested company/company of joining client */
 
			ClientID cid = (ClientID)p2;
 

	
 
			/* Has the network client a correct ClientIndex? */
 
			if (!(flags & DC_EXEC)) return CommandCost();
 
			NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(cid);
 
@@ -741,14 +741,12 @@ CommandCost CmdCompanyCtrl(TileIndex til
 

	
 
			/* A new company could not be created, revert to being a spectator */
 
			if (c == NULL) {
 
				if (_network_server) {
 
					ci->client_playas = COMPANY_SPECTATOR;
 
					NetworkUpdateClientInfo(ci->client_id);
 
				} else if (_local_company == COMPANY_SPECTATOR) {
 
					_network_playas = COMPANY_SPECTATOR;
 
				}
 
				break;
 
			}
 

	
 
			/* This is the client (or non-dedicated server) who wants a new company */
 
			if (cid == _network_own_client_id) {
src/console_cmds.cpp
Show inline comments
 
@@ -766,52 +766,49 @@ DEF_CONSOLE_CMD(ConNetworkClients)
 

	
 
	return true;
 
}
 

	
 
DEF_CONSOLE_CMD(ConNetworkConnect)
 
{
 
	char *ip;
 
	const char *port = NULL;
 
	const char *company = NULL;
 
	uint16 rport;
 

	
 
	if (argc == 0) {
 
		IConsoleHelp("Connect to a remote OTTD server and join the game. Usage: 'connect <ip>'");
 
		IConsoleHelp("IP can contain port and company: 'IP[[#Company]:Port]', eg: 'server.ottd.org#2:443'");
 
		IConsoleHelp("Company #255 is spectator all others are a certain company with Company 1 being #1");
 
		return true;
 
	}
 

	
 
	if (argc < 2) return false;
 
	if (_networking) NetworkDisconnect(); // we are in network-mode, first close it!
 

	
 
	ip = argv[1];
 
	const char *port = NULL;
 
	const char *company = NULL;
 
	char *ip = argv[1];
 
	/* Default settings: default port and new company */
 
	rport = NETWORK_DEFAULT_PORT;
 
	_network_playas = COMPANY_NEW_COMPANY;
 
	uint16 rport = NETWORK_DEFAULT_PORT;
 
	CompanyID join_as = COMPANY_NEW_COMPANY;
 

	
 
	ParseConnectionString(&company, &port, ip);
 

	
 
	IConsolePrintF(CC_DEFAULT, "Connecting to %s...", ip);
 
	if (company != NULL) {
 
		_network_playas = (CompanyID)atoi(company);
 
		IConsolePrintF(CC_DEFAULT, "    company-no: %d", _network_playas);
 
		join_as = (CompanyID)atoi(company);
 
		IConsolePrintF(CC_DEFAULT, "    company-no: %d", join_as);
 

	
 
		/* From a user pov 0 is a new company, internally it's different and all
 
		 * companies are offset by one to ease up on users (eg companies 1-8 not 0-7) */
 
		if (_network_playas != COMPANY_SPECTATOR) {
 
			if (_network_playas > MAX_COMPANIES) return false;
 
			_network_playas--;
 
		if (join_as != COMPANY_SPECTATOR) {
 
			if (join_as > MAX_COMPANIES) return false;
 
			join_as--;
 
		}
 
	}
 
	if (port != NULL) {
 
		rport = atoi(port);
 
		IConsolePrintF(CC_DEFAULT, "    port: %s", port);
 
	}
 

	
 
	NetworkClientConnectGame(NetworkAddress(ip, rport));
 
	NetworkClientConnectGame(NetworkAddress(ip, rport), join_as);
 

	
 
	return true;
 
}
 

	
 
#endif /* ENABLE_NETWORK */
 

	
src/economy.cpp
Show inline comments
 
@@ -445,13 +445,12 @@ void ChangeOwnershipOfCompanyItems(Owner
 
static void ChangeNetworkOwner(Owner current_owner, Owner new_owner)
 
{
 
#ifdef ENABLE_NETWORK
 
	if (!_networking) return;
 

	
 
	if (current_owner == _local_company) {
 
		_network_playas = new_owner;
 
		SetLocalCompany(new_owner);
 
	}
 

	
 
	if (!_network_server) return;
 

	
 
	NetworkServerChangeOwner(current_owner, new_owner);
src/network/network.cpp
Show inline comments
 
@@ -711,20 +711,21 @@ public:
 
		NetworkClient_Connected();
 
	}
 
};
 

	
 

	
 
/* Used by clients, to connect to a server */
 
void NetworkClientConnectGame(NetworkAddress address)
 
void NetworkClientConnectGame(NetworkAddress address, CompanyID join_as)
 
{
 
	if (!_network_available) return;
 

	
 
	if (address.GetPort() == 0) return;
 

	
 
	strecpy(_settings_client.network.last_host, address.GetHostname(), lastof(_settings_client.network.last_host));
 
	_settings_client.network.last_port = address.GetPort();
 
	_network_join_as = join_as;
 

	
 
	NetworkDisconnect();
 
	NetworkInitialize();
 

	
 
	_network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
 
	ShowJoinStatusWindow();
 
@@ -775,15 +776,12 @@ bool NetworkServerStart()
 
	_frame_counter = 0;
 
	_frame_counter_server = 0;
 
	_frame_counter_max = 0;
 
	_last_sync_frame = 0;
 
	_network_own_client_id = CLIENT_ID_SERVER;
 

	
 
	/* Non-dedicated server will always be company #1 */
 
	if (!_network_dedicated) _network_playas = COMPANY_FIRST;
 

	
 
	_network_clients_connected = 0;
 

	
 
	NetworkInitGameInfo();
 

	
 
	/* execute server initialization script */
 
	IConsoleCmdExec("exec scripts/on_server.scr 0");
 
@@ -1123,9 +1121,6 @@ void NetworkShutDown()
 
bool IsNetworkCompatibleVersion(const char *other)
 
{
 
	return strncmp(_openttd_revision, other, NETWORK_REVISION_LENGTH - 1) == 0;
 
}
 

	
 
#endif /* ENABLE_NETWORK */
 

	
 
/* NOTE: this variable needs to be always available */
 
CompanyID _network_playas;
src/network/network.h
Show inline comments
 
@@ -30,11 +30,7 @@ static inline void NetworkDrawChatMessag
 
#define _network_server 0
 
#define _network_available 0
 
#define _network_dedicated 0
 
#define _is_network_server 0
 

	
 
#endif /* ENABLE_NETWORK */
 

	
 
/** As which company do we play? */
 
extern CompanyID _network_playas;
 

	
 
#endif /* NETWORK_H */
src/network/network_client.cpp
Show inline comments
 
@@ -41,12 +41,15 @@ static char _password_server_unique_id[N
 

	
 
/** Maximum number of companies of the currently joined server. */
 
static uint8 _network_server_max_companies;
 
/** Maximum number of spectators of the currently joined server. */
 
static uint8 _network_server_max_spectators;
 

	
 
/** Who would we like to join as. */
 
CompanyID _network_join_as;
 

	
 
/** Make sure the unique ID length is the same as a md5 hash. */
 
assert_compile(NETWORK_UNIQUE_ID_LENGTH == 16 * 2 + 1);
 

	
 
/**
 
 * Generates a hashed password for the company name.
 
 * @param password the password to 'encrypt'.
 
@@ -132,13 +135,13 @@ DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_JO
 
	_network_join_status = NETWORK_JOIN_STATUS_AUTHORIZING;
 
	InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
 

	
 
	p = NetworkSend_Init(PACKET_CLIENT_JOIN);
 
	p->Send_string(_openttd_revision);
 
	p->Send_string(_settings_client.network.client_name); // Client name
 
	p->Send_uint8 (_network_playas);      // PlayAs
 
	p->Send_uint8 (_network_join_as);     // PlayAs
 
	p->Send_uint8 (NETLANG_ANY);          // Language
 
	p->Send_string(_settings_client.network.network_id);
 
	MY_CLIENT->Send_Packet(p);
 
}
 

	
 
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)
 
@@ -403,15 +406,12 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER
 
	char name[NETWORK_NAME_LENGTH];
 

	
 
	p->Recv_string(name, sizeof(name));
 

	
 
	if (MY_CLIENT->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
 

	
 
	/* Do we receive a change of data? Most likely we changed playas */
 
	if (client_id == _network_own_client_id) _network_playas = playas;
 

	
 
	ci = NetworkFindClientInfoFromClientID(client_id);
 
	if (ci != NULL) {
 
		if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) {
 
			/* Client name changed, display the change */
 
			NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, name);
 
		} else if (playas != ci->client_playas) {
 
@@ -617,25 +617,25 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER
 

	
 
		/* Say we received the map and loaded it correctly! */
 
		SEND_COMMAND(PACKET_CLIENT_MAP_OK)();
 

	
 
		/* New company/spectator (invalid company) or company we want to join is not active
 
		 * Switch local company to spectator and await the server's judgement */
 
		if (_network_playas == COMPANY_NEW_COMPANY || !Company::IsValidID(_network_playas)) {
 
		if (_network_join_as == COMPANY_NEW_COMPANY || !Company::IsValidID(_network_join_as)) {
 
			SetLocalCompany(COMPANY_SPECTATOR);
 

	
 
			if (_network_playas != COMPANY_SPECTATOR) {
 
			if (_network_join_as != COMPANY_SPECTATOR) {
 
				/* We have arrived and ready to start playing; send a command to make a new company;
 
				 * the server will give us a client-id and let us in */
 
				_network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
 
				ShowJoinStatusWindow();
 
				NetworkSend_Command(0, 0, 0, CMD_COMPANY_CTRL, NULL, NULL);
 
			}
 
		} else {
 
			/* take control over an existing company */
 
			SetLocalCompany(_network_playas);
 
			SetLocalCompany(_network_join_as);
 
		}
 
	}
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
@@ -842,13 +842,12 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER
 
	if (ci == NULL) return NETWORK_RECV_STATUS_OKAY;
 

	
 
	/* if not valid player, force spectator, else check player exists */
 
	if (!Company::IsValidID(company_id)) company_id = COMPANY_SPECTATOR;
 

	
 
	if (client_id == _network_own_client_id) {
 
		_network_playas = company_id;
 
		SetLocalCompany(company_id);
 
	}
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
src/network/network_client.h
Show inline comments
 
@@ -20,9 +20,11 @@ DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_AC
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_RCON)(const char *pass, const char *command);
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_MOVE)(CompanyID company, const char *pass);
 

	
 
NetworkRecvStatus NetworkClient_ReadPackets(NetworkClientSocket *cs);
 
void NetworkClient_Connected();
 

	
 
extern CompanyID _network_join_as;
 

	
 
#endif /* ENABLE_NETWORK */
 

	
 
#endif /* NETWORK_CLIENT_H */
src/network/network_func.h
Show inline comments
 
@@ -36,13 +36,13 @@ void NetworkUDPGameLoop();
 
void NetworkUDPCloseAll();
 
void ParseConnectionString(const char **company, const char **port, char *connection_string);
 
void NetworkStartDebugLog(NetworkAddress address);
 
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats);
 

	
 
void NetworkUpdateClientInfo(ClientID client_id);
 
void NetworkClientConnectGame(NetworkAddress address);
 
void NetworkClientConnectGame(NetworkAddress address, CompanyID join_as);
 
void NetworkClientRequestMove(CompanyID company, const char *pass = "");
 
void NetworkClientSendRcon(const char *password, const char *command);
 
void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data = 0);
 
void NetworkClientSetPassword(const char *password);
 
bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio);
 
bool NetworkCompanyIsPassworded(CompanyID company_id);
src/network/network_gui.cpp
Show inline comments
 
@@ -20,12 +20,13 @@
 
#include "../gfx_func.h"
 
#include "../settings_type.h"
 
#include "../widgets/dropdown_func.h"
 
#include "../querystring_gui.h"
 
#include "../sortlist_type.h"
 
#include "../company_base.h"
 
#include "../company_func.h"
 

	
 
#include "table/strings.h"
 
#include "../table/sprites.h"
 

	
 

	
 
static void ShowNetworkStartServerWindow();
 
@@ -1531,24 +1532,21 @@ struct NetworkLobbyWindow : public Windo
 
				this->company = (id_v >= this->server->info.companies_on) ? INVALID_COMPANY : NetworkLobbyFindCompanyIndex(id_v);
 
				this->SetDirty();
 
			} break;
 

	
 
			case NLWW_JOIN:     // Join company
 
				/* Button can be clicked only when it is enabled */
 
				_network_playas = this->company;
 
				NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
 
				NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), this->company);
 
				break;
 

	
 
			case NLWW_NEW:      // New company
 
				_network_playas = COMPANY_NEW_COMPANY;
 
				NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
 
				NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_NEW_COMPANY);
 
				break;
 

	
 
			case NLWW_SPECTATE: // Spectate game
 
				_network_playas = COMPANY_SPECTATOR;
 
				NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
 
				NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_SPECTATOR);
 
				break;
 

	
 
			case NLWW_REFRESH:  // Refresh
 
				NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
 
				NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
 
				/* Clear the information so removed companies don't remain */
 
@@ -1814,13 +1812,13 @@ struct NetworkClientListPopupWindow : Wi
 
		}
 
		GetString(this->action[i], STR_NETWORK_CLIENTLIST_SPEAK_TO_ALL, lastof(this->action[i]));
 
		this->proc[i++] = &ClientList_SpeakToAll;
 

	
 
		if (_network_own_client_id != ci->client_id) {
 
			/* We are no spectator and the company we want to give money to is no spectator and money gifts are allowed */
 
			if (Company::IsValidID(_network_playas) && Company::IsValidID(ci->client_playas) && _settings_game.economy.give_money) {
 
			if (Company::IsValidID(_local_company) && Company::IsValidID(ci->client_playas) && _settings_game.economy.give_money) {
 
				GetString(this->action[i], STR_NETWORK_CLIENTLIST_GIVE_MONEY, lastof(this->action[i]));
 
				this->proc[i++] = &ClientList_GiveMoney;
 
			}
 
		}
 

	
 
		/* A server can kick clients (but not himself) */
src/openttd.cpp
Show inline comments
 
@@ -672,32 +672,30 @@ int ttd_main(int argc, char *argv[])
 

	
 
#ifdef ENABLE_NETWORK
 
	if (network && _network_available) {
 
		if (network_conn != NULL) {
 
			const char *port = NULL;
 
			const char *company = NULL;
 
			uint16 rport;
 

	
 
			rport = NETWORK_DEFAULT_PORT;
 
			_network_playas = COMPANY_NEW_COMPANY;
 
			uint16 rport = NETWORK_DEFAULT_PORT;
 
			CompanyID join_as = COMPANY_NEW_COMPANY;
 

	
 
			ParseConnectionString(&company, &port, network_conn);
 

	
 
			if (company != NULL) {
 
				_network_playas = (CompanyID)atoi(company);
 
				join_as = (CompanyID)atoi(company);
 

	
 
				if (_network_playas != COMPANY_SPECTATOR) {
 
					_network_playas--;
 
					if (_network_playas >= MAX_COMPANIES) return false;
 
				if (join_as != COMPANY_SPECTATOR) {
 
					join_as--;
 
					if (join_as >= MAX_COMPANIES) return false;
 
				}
 
			}
 
			if (port != NULL) rport = atoi(port);
 

	
 
			LoadIntroGame();
 
			_switch_mode = SM_NONE;
 
			NetworkClientConnectGame(NetworkAddress(network_conn, rport));
 
			NetworkClientConnectGame(NetworkAddress(network_conn, rport), join_as);
 
		}
 
	}
 
#endif /* ENABLE_NETWORK */
 

	
 
	_video_driver->MainLoop();
 

	
 
@@ -1194,14 +1192,13 @@ void GameLoop()
 
		/* Multiplayer */
 
		NetworkGameLoop();
 
	} else {
 
		if (_network_reconnect > 0 && --_network_reconnect == 0) {
 
			/* This means that we want to reconnect to the last host
 
			 * We do this here, because it means that the network is really closed */
 
			_network_playas = COMPANY_SPECTATOR;
 
			NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port));
 
			NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_SPECTATOR);
 
		}
 
		/* Singleplayer */
 
		StateGameLoop();
 
	}
 
#else
 
	StateGameLoop();
src/video/dedicated_v.cpp
Show inline comments
 
@@ -252,13 +252,12 @@ void VideoDriver_Dedicated::MainLoop()
 
	signal(SIGQUIT, DedicatedSignalHandler);
 
#endif
 

	
 
	/* Load the dedicated server stuff */
 
	_is_network_server = true;
 
	_network_dedicated = true;
 
	_network_playas = COMPANY_SPECTATOR;
 
	_local_company = COMPANY_SPECTATOR;
 

	
 
	/* If SwitchMode is SM_LOAD, it means that the user used the '-g' options */
 
	if (_switch_mode != SM_LOAD) {
 
		StartNewGameWithoutGUI(GENERATE_NEW_SEED);
 
		SwitchToMode(_switch_mode);
0 comments (0 inline, 0 general)