Changeset - r717:02ff19796b81
[Not reviewed]
master
0 2 0
truelight - 20 years ago 2004-12-19 10:24:45
truelight@openttd.org
(svn r1169) -Fix: [Network] [ 1087591 ] When you want to be a spectator, you now
stay a spectator even if someone else joins.
2 files changed with 5 insertions and 8 deletions:
0 comments (0 inline, 0 general)
network_client.c
Show inline comments
 
@@ -127,702 +127,696 @@ DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_AC
 
	NetworkSend_Packet(p, MY_CLIENT);
 
}
 

	
 
// Send a command packet to the server
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_COMMAND)(CommandPacket *cp)
 
{
 
	//
 
	// Packet: CLIENT_COMMAND
 
	// Function: Send a DoCommand to the Server
 
	// Data:
 
	//    uint8:  PlayerID (0..MAX_PLAYERS-1)
 
	//    uint32: CommandID (see command.h)
 
	//    uint32: P1 (free variables used in DoCommand)
 
	//    uint32: P2
 
	//    uint32: Tile
 
	//    uint32: decode_params
 
	//      10 times the last one (lengthof(cp->dp))
 
	//    uint8:  CallBackID (see callback_table.c)
 
	//
 

	
 
	int i;
 
	char *dparam_char;
 
	Packet *p = NetworkSend_Init(PACKET_CLIENT_COMMAND);
 

	
 
	NetworkSend_uint8(p, cp->player);
 
	NetworkSend_uint32(p, cp->cmd);
 
	NetworkSend_uint32(p, cp->p1);
 
	NetworkSend_uint32(p, cp->p2);
 
	NetworkSend_uint32(p, (uint32)cp->tile);
 
	/* We are going to send them byte by byte, because dparam is misused
 
	    for chars (if it is used), and else we have a BigEndian / LittleEndian
 
	    problem.. we should fix the misuse of dparam... -- TrueLight */
 
	dparam_char = (char *)&cp->dp[0];
 
	for (i = 0; i < lengthof(cp->dp) * 4; i++) {
 
		NetworkSend_uint8(p, *dparam_char);
 
		dparam_char++;
 
	}
 
	NetworkSend_uint8(p, cp->callback);
 

	
 
	NetworkSend_Packet(p, MY_CLIENT);
 
}
 

	
 
// Send a chat-packet over the network
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_CHAT)(NetworkAction action, DestType desttype, int dest, const char *msg)
 
{
 
	//
 
	// Packet: CLIENT_CHAT
 
	// Function: Send a chat-packet to the serve
 
	// Data:
 
	//    uint8:  ActionID (see network_data.h, NetworkAction)
 
	//    uint8:  Destination Type (see network_data.h, DestType);
 
	//    uint8:  Destination Player (1..MAX_PLAYERS)
 
	//    String: Message (max MAX_TEXT_MSG_LEN)
 
	//
 

	
 
	Packet *p = NetworkSend_Init(PACKET_CLIENT_CHAT);
 

	
 
	NetworkSend_uint8(p, action);
 
	NetworkSend_uint8(p, desttype);
 
	NetworkSend_uint8(p, dest);
 
	NetworkSend_string(p, msg);
 
	NetworkSend_Packet(p, MY_CLIENT);
 
}
 

	
 
// Send an error-packet over the network
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_ERROR)(NetworkErrorCode errorno)
 
{
 
	//
 
	// Packet: CLIENT_ERROR
 
	// Function: The client made an error and is quiting the game
 
	// Data:
 
	//    uint8:  ErrorID (see network_data.h, NetworkErrorCode)
 
	//
 
	Packet *p = NetworkSend_Init(PACKET_CLIENT_ERROR);
 

	
 
	NetworkSend_uint8(p, errorno);
 
	NetworkSend_Packet(p, MY_CLIENT);
 
}
 

	
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_SET_PASSWORD)(const char *password)
 
{
 
	//
 
	// Packet: PACKET_CLIENT_SET_PASSWORD
 
	// Function: Set the password for the clients current company
 
	// Data:
 
	//    String: Password
 
	//
 
	Packet *p = NetworkSend_Init(PACKET_CLIENT_SET_PASSWORD);
 

	
 
	NetworkSend_string(p, password);
 
	NetworkSend_Packet(p, MY_CLIENT);
 
}
 

	
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_SET_NAME)(const char *name)
 
{
 
	//
 
	// Packet: PACKET_CLIENT_SET_NAME
 
	// Function: Gives the player a new name
 
	// Data:
 
	//    String: Name
 
	//
 
	Packet *p = NetworkSend_Init(PACKET_CLIENT_SET_NAME);
 

	
 
	NetworkSend_string(p, name);
 
	NetworkSend_Packet(p, MY_CLIENT);
 
}
 

	
 
// Send an quit-packet over the network
 
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_QUIT)(const char *leavemsg)
 
{
 
	//
 
	// Packet: CLIENT_QUIT
 
	// Function: The client is quiting the game
 
	// Data:
 
	//    String: leave-message
 
	//
 
	Packet *p = NetworkSend_Init(PACKET_CLIENT_QUIT);
 

	
 
	NetworkSend_string(p, leavemsg);
 
	NetworkSend_Packet(p, MY_CLIENT);
 
}
 

	
 

	
 
// **********
 
// Receiving functions
 
//   DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p
 
// **********
 

	
 
extern bool SafeSaveOrLoad(const char *filename, int mode, int newgm);
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_FULL)
 
{
 
	// We try to join a server which is full
 
	_switch_mode_errorstr = STR_NETWORK_ERR_SERVER_FULL;
 
	DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
 

	
 
	return NETWORK_RECV_STATUS_SERVER_FULL;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO)
 
{
 
	byte company_info_version;
 
	int i;
 

	
 
	company_info_version = NetworkRecv_uint8(p);
 

	
 
	if (company_info_version == 1) {
 
		byte total;
 
		byte current;
 

	
 
		total = NetworkRecv_uint8(p);
 
		_network_lobby_company_count = total;
 

	
 
		// There is no data at all..
 
		if (total == 0)
 
			return NETWORK_RECV_STATUS_CLOSE_QUERY;
 

	
 
		current = NetworkRecv_uint8(p) - 1;
 
		if (current >= MAX_PLAYERS)
 
			return NETWORK_RECV_STATUS_CLOSE_QUERY;
 

	
 
		NetworkRecv_string(p, _network_player_info[current].company_name, sizeof(_network_player_info[current].company_name));
 
		_network_player_info[current].inaugurated_year = NetworkRecv_uint8(p);
 
		_network_player_info[current].company_value = NetworkRecv_uint64(p);
 
		_network_player_info[current].money = NetworkRecv_uint64(p);
 
		_network_player_info[current].income = NetworkRecv_uint64(p);
 
		_network_player_info[current].performance = NetworkRecv_uint16(p);
 
		for (i = 0; i < NETWORK_VEHICLE_TYPES; i++)
 
			_network_player_info[current].num_vehicle[i] = NetworkRecv_uint16(p);
 
		for (i = 0; i < NETWORK_STATION_TYPES; i++)
 
			_network_player_info[current].num_station[i] = NetworkRecv_uint16(p);
 

	
 
		NetworkRecv_string(p, _network_player_info[current].players, sizeof(_network_player_info[current].players));
 

	
 
		InvalidateWindow(WC_NETWORK_WINDOW, 0);
 

	
 
		if (total == current + 1)
 
			// This was the last one
 
			return NETWORK_RECV_STATUS_CLOSE_QUERY;
 
		else
 
			return NETWORK_RECV_STATUS_OKAY;
 
	}
 

	
 
	return NETWORK_RECV_STATUS_CLOSE_QUERY;
 
}
 

	
 
// This packet contains info about the client (playas and name)
 
//  as client we save this in NetworkClientInfo, linked via 'index'
 
//  which is always an unique number on a server.
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO)
 
{
 
	NetworkClientInfo *ci;
 
	uint16 index = NetworkRecv_uint16(p);
 
	byte playas = NetworkRecv_uint8(p);
 
	char name[NETWORK_NAME_LENGTH];
 
	char unique_id[NETWORK_NAME_LENGTH];
 

	
 
	NetworkRecv_string(p, name, sizeof(name));
 
	NetworkRecv_string(p, unique_id, sizeof(unique_id));
 

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

	
 
	ci = NetworkFindClientInfoFromIndex(index);
 
	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, 1, ci->client_name, name);
 
		} else if (playas != ci->client_playas) {
 
			// The player changed from client-player..
 
			// Do not display that for now
 
		}
 

	
 
		ci->client_playas = playas;
 
		ttd_strlcpy(ci->client_name, name, sizeof(ci->client_name));
 

	
 
		return NETWORK_RECV_STATUS_OKAY;
 
	}
 

	
 
	// We don't have this index yet, find an empty index, and put the data there
 
	ci = NetworkFindClientInfoFromIndex(NETWORK_EMPTY_INDEX);
 
	if (ci != NULL) {
 
		ci->client_index = index;
 
		ci->client_playas = playas;
 

	
 
		ttd_strlcpy(ci->client_name, name, sizeof(ci->client_name));
 
		ttd_strlcpy(ci->unique_id, unique_id, sizeof(ci->unique_id));
 

	
 
		return NETWORK_RECV_STATUS_OKAY;
 
	}
 

	
 
	// Here the program should never ever come.....
 
	return NETWORK_RECV_STATUS_MALFORMED_PACKET;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR)
 
{
 
	NetworkErrorCode error = NetworkRecv_uint8(p);
 

	
 
	if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED ||
 
			error == NETWORK_ERROR_PLAYER_MISMATCH) {
 
		// We made an error in the protocol, and our connection is closed.... :(
 
		_switch_mode_errorstr = STR_NETWORK_ERR_SERVER_ERROR;
 
	} else if (error == NETWORK_ERROR_WRONG_REVISION) {
 
		// Wrong revision :(
 
		_switch_mode_errorstr = STR_NETWORK_ERR_WRONG_REVISION;
 
	} else if (error == NETWORK_ERROR_WRONG_PASSWORD) {
 
		// Wrong password
 
		_switch_mode_errorstr = STR_NETWORK_ERR_WRONG_PASSWORD;
 
	} else if (error == NETWORK_ERROR_KICKED) {
 
		_switch_mode_errorstr = STR_NETWORK_ERR_KICKED;
 
	}
 

	
 
	DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
 

	
 
	return NETWORK_RECV_STATUS_SERVER_ERROR;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEED_PASSWORD)
 
{
 
	NetworkPasswordType type;
 
	type = NetworkRecv_uint8(p);
 

	
 
	if (type == NETWORK_GAME_PASSWORD) {
 
		ShowNetworkNeedGamePassword();
 
		return NETWORK_RECV_STATUS_OKAY;
 
	} else if (type == NETWORK_COMPANY_PASSWORD) {
 
		ShowNetworkNeedCompanyPassword();
 
		return NETWORK_RECV_STATUS_OKAY;
 
	}
 

	
 
	return NETWORK_RECV_STATUS_MALFORMED_PACKET;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WELCOME)
 
{
 
	_network_own_client_index = NetworkRecv_uint16(p);
 

	
 
	// Start receiving the map
 
	SEND_COMMAND(PACKET_CLIENT_GETMAP)();
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WAIT)
 
{
 
	_network_join_status = NETWORK_JOIN_STATUS_WAITING;
 
	_network_join_waiting = NetworkRecv_uint8(p);
 
	InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
 

	
 
	// We are put on hold for receiving the map.. we need GUI for this ;)
 
	DEBUG(net, 1)("[NET] The server is currently busy sending the map to someone else.. please hold..." );
 
	DEBUG(net, 1)("[NET]  There are %d clients in front of you", _network_join_waiting);
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP)
 
{
 
	static char filename[256];
 
	static FILE *file_pointer;
 

	
 
	byte maptype;
 

	
 
	maptype = NetworkRecv_uint8(p);
 

	
 
	// First packet, init some stuff
 
	if (maptype == MAP_PACKET_START) {
 
		// The name for the temp-map
 
		sprintf(filename, "%s%snetwork_client.tmp",  _path.autosave_dir, PATHSEP);
 

	
 
		file_pointer = fopen(filename, "wb");
 
		if (file_pointer == NULL) {
 
			_switch_mode_errorstr = STR_NETWORK_ERR_SAVEGAMEERROR;
 
			return NETWORK_RECV_STATUS_SAVEGAME;
 
		}
 

	
 
		_frame_counter = _frame_counter_server = _frame_counter_max = NetworkRecv_uint32(p);
 

	
 
		_network_join_status = NETWORK_JOIN_STATUS_DOWNLOADING;
 
		_network_join_kbytes = 0;
 
		_network_join_kbytes_total = NetworkRecv_uint32(p) / 1024;
 
		InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
 

	
 
		// The first packet does not contain any more data
 
		return NETWORK_RECV_STATUS_OKAY;
 
	}
 

	
 
	if (maptype == MAP_PACKET_NORMAL) {
 
		// We are still receiving data, put it to the file
 
		fwrite(p->buffer + p->pos, 1, p->size - p->pos, file_pointer);
 

	
 
		_network_join_kbytes = ftell(file_pointer) / 1024;
 
		InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
 
	}
 

	
 
	if (maptype == MAP_PACKET_PATCH) {
 
		NetworkRecvPatchSettings(p);
 
	}
 

	
 
	// Check if this was the last packet
 
	if (maptype == MAP_PACKET_END) {
 
		// We also get, very nice, the player_seeds in this packet
 
		int i;
 
		for (i = 0; i < MAX_PLAYERS; i++) {
 
			_player_seeds[i][0] = NetworkRecv_uint32(p);
 
			_player_seeds[i][1] = NetworkRecv_uint32(p);
 
		}
 

	
 
		fclose(file_pointer);
 

	
 
		_network_join_status = NETWORK_JOIN_STATUS_PROCESSING;
 
		InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
 

	
 
		// The map is done downloading, load it
 
		// Load the map
 
		if (!SafeSaveOrLoad(filename, SL_LOAD, GM_NORMAL)) {
 
			DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
 
			_switch_mode_errorstr = STR_NETWORK_ERR_SAVEGAMEERROR;
 
			return NETWORK_RECV_STATUS_SAVEGAME;
 
		}
 
		_opt_mod_ptr = &_opt;
 

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

	
 
		if (_network_playas == 0 || _network_playas > MAX_PLAYERS ||
 
				!DEREF_PLAYER(_network_playas - 1)->is_active) {
 

	
 
			if (_network_playas == OWNER_SPECTATOR) {
 
				// The client wants to be a spectator..
 
				_local_player = OWNER_SPECTATOR;
 
				DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
 
			} else {
 
				// send a command to make a new player
 
				_local_player = 0;
 
				NetworkSend_Command(0, 0, 0, CMD_PLAYER_CTRL, NULL);
 
				_local_player = OWNER_SPECTATOR;
 
			}
 
		} else {
 
			// take control over an existing company
 
			_local_player = _network_playas - 1;
 
			DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
 
		}
 

	
 
		// Remeber the player
 
		if (_local_player != OWNER_SPECTATOR)
 
			_network_playas = _local_player + 1;
 
		else
 
			_network_playas = OWNER_SPECTATOR;
 
	}
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_FRAME)
 
{
 
	_frame_counter_server = NetworkRecv_uint32(p);
 
	_frame_counter_max = NetworkRecv_uint32(p);
 
#ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
 
	// Test if the server supports this option
 
	//  and if we are at the frame the server is
 
	if (p->pos < p->size) {
 
		_sync_frame = _frame_counter_server;
 
		_sync_seed_1 = NetworkRecv_uint32(p);
 
#ifdef NETWORK_SEND_DOUBLE_SEED
 
		_sync_seed_2 = NetworkRecv_uint32(p);
 
#endif
 
	}
 
#endif
 
	DEBUG(net, 7)("[NET] Received FRAME %d",_frame_counter_server);
 

	
 
	// Let the server know that we received this frame correctly
 
	//  We do this only once per day, to save some bandwidth ;)
 
	if (!_network_first_time && last_ack_frame < _frame_counter) {
 
		last_ack_frame = _frame_counter + DAY_TICKS;
 
		DEBUG(net,6)("[NET] Sent ACK at %d", _frame_counter);
 
		SEND_COMMAND(PACKET_CLIENT_ACK)();
 
	}
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_SYNC)
 
{
 
	_sync_frame = NetworkRecv_uint32(p);
 
	_sync_seed_1 = NetworkRecv_uint32(p);
 
#ifdef NETWORK_SEND_DOUBLE_SEED
 
	_sync_seed_2 = NetworkRecv_uint32(p);
 
#endif
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMMAND)
 
{
 
	int i;
 
	char *dparam_char;
 
	CommandPacket *cp = malloc(sizeof(CommandPacket));
 
	cp->player = NetworkRecv_uint8(p);
 
	cp->cmd = NetworkRecv_uint32(p);
 
	cp->p1 = NetworkRecv_uint32(p);
 
	cp->p2 = NetworkRecv_uint32(p);
 
	cp->tile = NetworkRecv_uint32(p);
 
	/* We are going to send them byte by byte, because dparam is misused
 
	    for chars (if it is used), and else we have a BigEndian / LittleEndian
 
	    problem.. we should fix the misuse of dparam... -- TrueLight */
 
	dparam_char = (char *)&cp->dp[0];
 
	for (i = 0; i < lengthof(cp->dp) * 4; i++) {
 
		*dparam_char = NetworkRecv_uint8(p);
 
		dparam_char++;
 
	}
 
	cp->callback = NetworkRecv_uint8(p);
 
	cp->frame = NetworkRecv_uint32(p);
 
	cp->next = NULL;
 

	
 
	// The server did send us this command..
 
	//  queue it in our own queue, so we can handle it in the upcoming frame!
 

	
 
	if (_local_command_queue == NULL) {
 
		_local_command_queue = cp;
 
	} else {
 
		// Find last packet
 
		CommandPacket *c = _local_command_queue;
 
		while (c->next != NULL) c = c->next;
 
		c->next = cp;
 
	}
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT)
 
{
 
	NetworkAction action = NetworkRecv_uint8(p);
 
	char msg[MAX_TEXT_MSG_LEN];
 
	NetworkClientInfo *ci, *ci_to;
 
	uint16 index;
 
	char name[NETWORK_NAME_LENGTH];
 

	
 
	index = NetworkRecv_uint16(p);
 
	NetworkRecv_string(p, msg, MAX_TEXT_MSG_LEN);
 

	
 
	ci_to = NetworkFindClientInfoFromIndex(index);
 
	if (ci_to == NULL) return NETWORK_RECV_STATUS_OKAY;
 

	
 
	if (action == NETWORK_ACTION_CHAT_TO_CLIENT) {
 
		snprintf(name, sizeof(name), "%s", ci_to->client_name);
 
		ci = NetworkFindClientInfoFromIndex(_network_own_client_index);
 
	} else if (action == NETWORK_ACTION_CHAT_TO_PLAYER) {
 
		GetString(name, DEREF_PLAYER(ci_to->client_playas-1)->name_1);
 
		ci = NetworkFindClientInfoFromIndex(_network_own_client_index);
 
	} else {
 
		snprintf(name, sizeof(name), "%s", ci_to->client_name);
 
		ci = ci_to;
 
	}
 

	
 
	if (ci != NULL)
 
		NetworkTextMessage(action, GetDrawStringPlayerColor(ci->client_playas-1), name, "%s", msg);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT)
 
{
 
	int errorno;
 
	char str1[100], str2[100];
 
	uint16 index;
 
	NetworkClientInfo *ci;
 

	
 
	index = NetworkRecv_uint16(p);
 
	errorno = NetworkRecv_uint8(p);
 

	
 
	GetString(str1, STR_NETWORK_ERR_LEFT);
 
	GetString(str2, STR_NETWORK_ERR_CLIENT_GENERAL + errorno);
 

	
 
	ci = NetworkFindClientInfoFromIndex(index);
 
	if (ci != NULL) {
 
		NetworkTextMessage(NETWORK_ACTION_JOIN_LEAVE, 1, ci->client_name, "%s (%s)", str1, str2);
 

	
 
		// The client is gone, give the NetworkClientInfo free
 
		ci->client_index = NETWORK_EMPTY_INDEX;
 
	}
 

	
 
	InvalidateWindow(WC_CLIENT_LIST, 0);
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_QUIT)
 
{
 
	char str1[100], str2[100];
 
	uint16 index;
 
	NetworkClientInfo *ci;
 

	
 
	index = NetworkRecv_uint16(p);
 
	NetworkRecv_string(p, str2, 100);
 

	
 
	GetString(str1, STR_NETWORK_ERR_LEFT);
 

	
 
	ci = NetworkFindClientInfoFromIndex(index);
 
	if (ci != NULL) {
 
		NetworkTextMessage(NETWORK_ACTION_JOIN_LEAVE, 1, ci->client_name, "%s (%s)", str1, str2);
 

	
 
		// The client is gone, give the NetworkClientInfo free
 
		ci->client_index = NETWORK_EMPTY_INDEX;
 
	} else {
 
		DEBUG(net, 0)("[NET] Error - unknown client (%d) is leaving the game", index);
 
	}
 

	
 
	InvalidateWindow(WC_CLIENT_LIST, 0);
 

	
 
	// If we come here it means we could not locate the client.. strange :s
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_JOIN)
 
{
 
	char str1[100];
 
	uint16 index;
 
	NetworkClientInfo *ci;
 

	
 
	index = NetworkRecv_uint16(p);
 

	
 
	GetString(str1, STR_NETWORK_CLIENT_JOINED);
 

	
 
	ci = NetworkFindClientInfoFromIndex(index);
 
	if (ci != NULL) {
 
		NetworkTextMessage(NETWORK_ACTION_JOIN_LEAVE, 1, ci->client_name, "%s", str1);
 
	}
 

	
 
	InvalidateWindow(WC_CLIENT_LIST, 0);
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_SHUTDOWN)
 
{
 
	_switch_mode_errorstr = STR_NETWORK_SERVER_SHUTDOWN;
 

	
 
	return NETWORK_RECV_STATUS_SERVER_ERROR;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEWGAME)
 
{
 
	// To trottle the reconnects a bit, every clients waits
 
	//  his _local_player value before reconnecting
 
	// OWNER_SPECTATOR is currently 255, so to avoid long wait periods
 
	//  set the max to 10.
 
	_network_reconnect = min(_local_player + 1, 10);
 
	_switch_mode_errorstr = STR_NETWORK_SERVER_REBOOT;
 

	
 
	return NETWORK_RECV_STATUS_SERVER_ERROR;
 
}
 

	
 

	
 

	
 

	
 
// The layout for the receive-functions by the client
 
typedef NetworkRecvStatus NetworkClientPacket(Packet *p);
 

	
 
// This array matches PacketType. At an incoming
 
//  packet it is matches against this array
 
//  and that way the right function to handle that
 
//  packet is found.
 
static NetworkClientPacket* const _network_client_packet[] = {
 
	RECEIVE_COMMAND(PACKET_SERVER_FULL),
 
	NULL, /*PACKET_CLIENT_JOIN,*/
 
	RECEIVE_COMMAND(PACKET_SERVER_ERROR),
 
	NULL, /*PACKET_CLIENT_COMPANY_INFO,*/
 
	RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO),
 
	RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO),
 
	RECEIVE_COMMAND(PACKET_SERVER_NEED_PASSWORD),
 
	NULL, /*PACKET_CLIENT_PASSWORD,*/
 
	RECEIVE_COMMAND(PACKET_SERVER_WELCOME),
 
	NULL, /*PACKET_CLIENT_GETMAP,*/
 
	RECEIVE_COMMAND(PACKET_SERVER_WAIT),
 
	RECEIVE_COMMAND(PACKET_SERVER_MAP),
 
	NULL, /*PACKET_CLIENT_MAP_OK,*/
 
	RECEIVE_COMMAND(PACKET_SERVER_JOIN),
 
	RECEIVE_COMMAND(PACKET_SERVER_FRAME),
 
	RECEIVE_COMMAND(PACKET_SERVER_SYNC),
 
	NULL, /*PACKET_CLIENT_ACK,*/
 
	NULL, /*PACKET_CLIENT_COMMAND,*/
 
	RECEIVE_COMMAND(PACKET_SERVER_COMMAND),
 
	NULL, /*PACKET_CLIENT_CHAT,*/
 
	RECEIVE_COMMAND(PACKET_SERVER_CHAT),
 
	NULL, /*PACKET_CLIENT_SET_PASSWORD,*/
 
	NULL, /*PACKET_CLIENT_SET_NAME,*/
 
	NULL, /*PACKET_CLIENT_QUIT,*/
 
	NULL, /*PACKET_CLIENT_ERROR,*/
 
	RECEIVE_COMMAND(PACKET_SERVER_QUIT),
 
	RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT),
 
	RECEIVE_COMMAND(PACKET_SERVER_SHUTDOWN),
 
	RECEIVE_COMMAND(PACKET_SERVER_NEWGAME),
 
};
 

	
 
// If this fails, check the array above with network_data.h
 
assert_compile(lengthof(_network_client_packet) == PACKET_END);
 

	
 
extern const SettingDesc patch_settings[];
 

	
 
// This is a TEMPORARY solution to get the patch-settings
 
//  to the client. When the patch-settings are saved in the savegame
 
//  this should be removed!!
 
void NetworkRecvPatchSettings(Packet *p)
 
{
 
	const SettingDesc *item;
 

	
 
	item = patch_settings;
 

	
 
	while (item->name != NULL) {
 
		switch (item->flags) {
 
			case SDT_BOOL:
 
			case SDT_INT8:
 
			case SDT_UINT8:
 
				*(uint8 *)(item->ptr) = NetworkRecv_uint8(p);
 
				break;
 
			case SDT_INT16:
 
			case SDT_UINT16:
 
				*(uint16 *)(item->ptr) = NetworkRecv_uint16(p);
 
				break;
 
			case SDT_INT32:
 
			case SDT_UINT32:
 
				*(uint32 *)(item->ptr) = NetworkRecv_uint32(p);
 
				break;
 
		}
 
		item++;
 
	}
 
}
 

	
 
// Is called after a client is connected to the server
 
void NetworkClient_Connected(void)
 
{
 
	// Set the frame-counter to 0 so nothing happens till we are ready
 
	_frame_counter = 0;
 
	_frame_counter_server = 0;
 
	last_ack_frame = 0;
 
	// Request the game-info
 
	SEND_COMMAND(PACKET_CLIENT_JOIN)();
 
}
 

	
 
// Reads the packets from the socket-stream, if available
 
NetworkRecvStatus NetworkClient_ReadPackets(NetworkClientState *cs)
 
{
 
	Packet *p;
 
	NetworkRecvStatus res = NETWORK_RECV_STATUS_OKAY;
 

	
 
	while (res == NETWORK_RECV_STATUS_OKAY && (p = NetworkRecv_Packet(cs, &res)) != NULL) {
 
		byte type = NetworkRecv_uint8(p);
 
		if (type < PACKET_END && _network_client_packet[type] != NULL) {
 
			res = _network_client_packet[type](p);
 
		}	else {
 
			res = NETWORK_RECV_STATUS_MALFORMED_PACKET;
 
			DEBUG(net, 0)("[NET][client] Received invalid packet type %d", type);
 
		}
 

	
 
		free(p);
 
	}
 

	
 
	return res;
 
}
 

	
 
#endif /* ENABLE_NETWORK */
players.c
Show inline comments
 
@@ -272,615 +272,618 @@ bool CheckOwnership(byte owner)
 
	_error_message = STR_013B_OWNED_BY;
 
	GetNameOfOwner(owner, 0);
 
	return false;
 
}
 

	
 
bool CheckTileOwnership(uint tile)
 
{
 
	byte owner = _map_owner[tile];
 
	assert(owner <= OWNER_WATER);
 
	if (owner == _current_player)
 
		return true;
 
	_error_message = STR_013B_OWNED_BY;
 

	
 
	// no need to get the name of the owner unless we're the local player (saves some time)
 
	if (_current_player == _local_player)
 
		GetNameOfOwner(owner, tile);
 
	return false;
 
}
 

	
 
static void GenerateCompanyName(Player *p)
 
{
 
	uint tile;
 
	Town *t;
 
	StringID str;
 
	Player *pp;
 
	uint32 strp;
 
	char buffer[100];
 

	
 
	if (p->name_1 != STR_SV_UNNAMED)
 
		return;
 

	
 
	tile = p->last_build_coordinate;
 
	if (tile == 0)
 
		return;
 

	
 
	t = ClosestTownFromTile(tile, (uint)-1);
 

	
 
	if (IS_INT_INSIDE(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST+1)) {
 
		str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
 
		strp = t->townnameparts;
 

	
 
verify_name:;
 
		// No player must have this name already
 
		FOR_ALL_PLAYERS(pp) {
 
			if (pp->name_1 == str && pp->name_2 == strp)
 
				goto bad_town_name;
 
		}
 

	
 
		GetString(buffer, str);
 
		if (strlen(buffer) >= 32 || GetStringWidth(buffer) >= 150)
 
			goto bad_town_name;
 

	
 
set_name:;
 
		p->name_1 = str;
 
		p->name_2 = strp;
 

	
 
		MarkWholeScreenDirty();
 

	
 
		if (!IS_HUMAN_PLAYER(p->index)) {
 
			SetDParam(0, t->index);
 
			AddNewsItem(p->index + (4 << 4), NEWS_FLAGS(NM_CALLBACK, NF_TILE, NT_COMPANY_INFO, DNC_BANKRUPCY), p->last_build_coordinate, 0);
 
		}
 
		return;
 
	}
 
bad_town_name:;
 

	
 
	if (p->president_name_1 == SPECSTR_PRESIDENT_NAME) {
 
		str = SPECSTR_ANDCO_NAME;
 
		strp = p->president_name_2;
 
		goto set_name;
 
	} else {
 
		str = SPECSTR_ANDCO_NAME;
 
		strp = Random();
 
		goto verify_name;
 
	}
 
}
 

	
 
#define COLOR_SWAP(i,j) do { byte t=colors[i];colors[i]=colors[j];colors[j]=t; } while(0)
 

	
 
static const byte _color_sort[16] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1};
 
static const byte _color_similar_1[16] = {8, 6, 255, 12,  255, 0, 1, 1, 0, 13,  11,  10, 3,   9,  15, 14};
 
static const byte _color_similar_2[16] = {5, 7, 255, 255, 255, 8, 7, 6, 5, 12, 255, 255, 9, 255, 255, 255};
 

	
 
static byte GeneratePlayerColor()
 
{
 
	byte colors[16], pcolor, t2;
 
	int i,j,n;
 
	uint32 r;
 
	Player *p;
 

	
 
	// Initialize array
 
	for(i=0; i!=16; i++)
 
		colors[i] = i;
 

	
 
	// And randomize it
 
	n = 100;
 
	do {
 
		r = Random();
 
		COLOR_SWAP(r & 0xF, (r >> 4) & 0xF);
 
	} while (--n);
 

	
 
	// Bubble sort it according to the values in table 1
 
	i = 16;
 
	do {
 
		for(j=0; j!=15; j++) {
 
			if (_color_sort[colors[j]] < _color_sort[colors[j+1]]) {
 
				COLOR_SWAP(j,j+1);
 
			}
 
		}
 
	} while (--i);
 

	
 
	// Move the colors that look similar to each player's color to the side
 
	FOR_ALL_PLAYERS(p) if (p->is_active) {
 
		pcolor = p->player_color;
 
		for(i=0; i!=16; i++) if (colors[i] == pcolor) {
 
			colors[i] = 0xFF;
 

	
 
			t2 = _color_similar_1[pcolor];
 
			if (t2 == 0xFF) break;
 
			for(i=0; i!=15; i++) {
 
				if (colors[i] == t2) {
 
					do COLOR_SWAP(i,i+1); while (++i != 15);
 
					break;
 
				}
 
			}
 

	
 
			t2 = _color_similar_2[pcolor];
 
			if (t2 == 0xFF) break;
 
			for(i=0; i!=15; i++) {
 
				if (colors[i] == t2) {
 
					do COLOR_SWAP(i,i+1); while (++i != 15);
 
					break;
 
				}
 
			}
 
			break;
 
		}
 
	}
 

	
 
	// Return the first available color
 
	i = 0;
 
	for(;;) {
 
		if (colors[i] != 0xFF)
 
			return colors[i];
 
		i++;
 
	}
 
}
 

	
 
static void GeneratePresidentName(Player *p)
 
{
 
	Player *pp;
 
	char buffer[100], buffer2[40];
 

	
 
	for(;;) {
 
restart:;
 

	
 
		p->president_name_2 = Random();
 
		p->president_name_1 = SPECSTR_PRESIDENT_NAME;
 

	
 
		SetDParam(0, p->president_name_2);
 
		GetString(buffer, p->president_name_1);
 
		if (strlen(buffer) >= 32 || GetStringWidth(buffer) >= 94)
 
			continue;
 

	
 
		FOR_ALL_PLAYERS(pp) {
 
			if (pp->is_active && p != pp) {
 
				SetDParam(0, pp->president_name_2);
 
				GetString(buffer2, pp->president_name_1);
 
				if (str_eq(buffer2, buffer))
 
					goto restart;
 
			}
 
		}
 
		return;
 
	}
 
}
 

	
 
extern int GetPlayerMaxRailtype(int p);
 

	
 
static Player *AllocatePlayer()
 
{
 
	Player *p;
 
	// Find a free slot
 
	FOR_ALL_PLAYERS(p) {
 
		if (!p->is_active) {
 
			int i = p->index;
 
			memset(p, 0, sizeof(Player));
 
			p->index = i;
 
			return p;
 
		}
 
	}
 
	return NULL;
 
}
 

	
 
Player *DoStartupNewPlayer(bool is_ai)
 
{
 
	Player *p;
 
	int index;
 

	
 
	p = AllocatePlayer();
 
	if (p == NULL) return NULL;
 

	
 
	index = p->index;
 

	
 
	// Make a color
 
	p->player_color = GeneratePlayerColor();
 
	_player_colors[index] = p->player_color;
 
	p->name_1 = STR_SV_UNNAMED;
 
	p->is_active = true;
 

	
 
	p->money64 = p->player_money = p->current_loan = 100000;
 

	
 
	p->is_ai = is_ai;
 
	p->ai.state = 5; /* AIS_WANT_NEW_ROUTE */
 
	p->share_owners[0] = p->share_owners[1] = p->share_owners[2] = p->share_owners[3] = 0xFF;
 

	
 
	p->max_railtype = GetPlayerMaxRailtype(index);
 
	p->inaugurated_year = _cur_year;
 
	p->face = Random();
 

	
 
	GeneratePresidentName(p);
 

	
 
	InvalidateWindow(WC_GRAPH_LEGEND, 0);
 
	InvalidateWindow(WC_TOOLBAR_MENU, 0);
 
	InvalidateWindow(WC_CLIENT_LIST, 0);
 

	
 
	return p;
 
}
 

	
 
void StartupPlayers()
 
{
 
	// The AI starts like in the setting with +2 month max
 
	_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + RandomRange(60 * DAY_TICKS) + 1;
 
}
 

	
 
static void MaybeStartNewPlayer()
 
{
 
	uint n;
 
	Player *p;
 

	
 
	// count number of competitors
 
	n = 0;
 
	for(p=_players; p!=endof(_players); p++)
 
		if (p->is_active && p->is_ai)
 
			n++;
 

	
 
	// when there's a lot of computers in game, the probability that a new one starts is lower
 
	if (n < (uint)_opt.diff.max_no_competitors && n < RandomRange(_opt.diff.max_no_competitors + 2))
 
		DoStartupNewPlayer(true);
 

	
 
	// The next AI starts like the difficulty setting said, with +2 month max
 
	_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + RandomRange(60 * DAY_TICKS) + 1;
 
}
 

	
 
void InitializePlayers()
 
{
 
	int i;
 
	memset(_players, 0, sizeof(_players));
 
	for(i = 0; i != MAX_PLAYERS; i++)
 
		_players[i].index=i;
 
	_cur_player_tick_index = 0;
 
}
 

	
 
void OnTick_Players()
 
{
 
	Player *p;
 

	
 
	if (_game_mode == GM_EDITOR)
 
		return;
 

	
 
	p = DEREF_PLAYER(_cur_player_tick_index);
 
	_cur_player_tick_index = (_cur_player_tick_index + 1) % MAX_PLAYERS;
 
	if (p->name_1 != 0) GenerateCompanyName(p);
 

	
 
	if (!_networking && _game_mode != GM_MENU && !--_next_competitor_start) {
 
		MaybeStartNewPlayer();
 
	}
 
}
 

	
 
void RunOtherPlayersLoop()
 
{
 
	Player *p;
 

	
 
	_is_ai_player = true;
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active && p->is_ai) {
 
			_current_player = p->index;
 
			if (_patches.ainew_active)
 
				AiNewDoGameLoop(p);
 
			else
 
				AiDoGameLoop(p);
 
		}
 
	}
 

	
 
	_is_ai_player = false;
 
	_current_player = OWNER_NONE;
 
}
 

	
 
// index is the next parameter in _decode_parameters to set up
 
StringID GetPlayerNameString(byte player, byte index)
 
{
 
	if (IS_HUMAN_PLAYER(player) && player < MAX_PLAYERS) {
 
		SetDParam(index, player+1);
 
		return STR_7002_PLAYER;
 
	}
 
	return STR_EMPTY;
 
}
 

	
 
extern void ShowPlayerFinances(int player);
 

	
 
void PlayersYearlyLoop()
 
{
 
	Player *p;
 

	
 
	// Copy statistics
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active) {
 
			memmove(&p->yearly_expenses[1], &p->yearly_expenses[0], sizeof(p->yearly_expenses) - sizeof(p->yearly_expenses[0]));
 
			memset(&p->yearly_expenses[0], 0, sizeof(p->yearly_expenses[0]));
 
			InvalidateWindow(WC_FINANCES, p->index);
 
		}
 
	}
 

	
 
	if (_patches.show_finances && _local_player != OWNER_SPECTATOR) {
 
		ShowPlayerFinances(_local_player);
 
		p = DEREF_PLAYER(_local_player);
 
		if (p->num_valid_stat_ent > 5 && p->old_economy[0].performance_history < p->old_economy[4].performance_history) {
 
			SndPlayFx(SND_01_BAD_YEAR);
 
		} else {
 
			SndPlayFx(SND_00_GOOD_YEAR);
 
		}
 
	}
 
}
 

	
 
void DeletePlayerWindows(int pi)
 
{
 
	DeleteWindowById(WC_COMPANY, pi);
 
	DeleteWindowById(WC_FINANCES, pi);
 
	DeleteWindowById(WC_STATION_LIST, pi);
 
	DeleteWindowById(WC_TRAINS_LIST, pi);
 
	DeleteWindowById(WC_ROADVEH_LIST, pi);
 
	DeleteWindowById(WC_SHIPS_LIST, pi);
 
	DeleteWindowById(WC_AIRCRAFT_LIST, pi);
 
	DeleteWindowById(WC_BUY_COMPANY, pi);
 
}
 

	
 
static void DeletePlayerStuff(int pi)
 
{
 
	Player *p;
 

	
 
	DeletePlayerWindows(pi);
 
	p = DEREF_PLAYER(pi);
 
	DeleteName(p->name_1);
 
	DeleteName(p->president_name_1);
 
	p->name_1 = 0;
 
	p->president_name_1 = 0;
 
}
 

	
 
// functionality.
 
// 0 - make new player
 
// 1 - make new AI player
 
// 2 - delete player (p2)
 
// 3 - join player (p1 >> 8) & 0xFF with (p1 >> 16) & 0xFF
 
int32 CmdPlayerCtrl(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	int pi;
 
	Player *p;
 

	
 
	if (!(flags & DC_EXEC))
 
		return 0;
 

	
 
	_current_player = OWNER_NONE;
 

	
 
	switch(p1 & 0xff) {
 
	case 0: // make new player
 
		p = DoStartupNewPlayer(false);
 

	
 
#ifdef ENABLE_NETWORK
 
		if (_networking && !_network_server && _local_player == OWNER_SPECTATOR)
 
			/* In case we are a client joining a server... */
 
			DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
 
#endif /* ENABLE_NETWORK */
 

	
 
		if (p != NULL) {
 
			if (_local_player == OWNER_SPECTATOR) {
 
				_local_player = p->index;
 
				MarkWholeScreenDirty();
 
				/* Check if we do not want to be a spectator in network */
 
				if (!_networking || _network_server || _network_playas != OWNER_SPECTATOR) {
 
					_local_player = p->index;
 
					MarkWholeScreenDirty();
 
				}
 
			}
 
#ifdef ENABLE_NETWORK
 
			if (_network_server) {
 
				NetworkClientInfo *ci;
 
				// UGLY! p2 is mis-used to fetch the client-id
 
				ci = &_network_client_info[p2];
 
				ci->client_playas = p->index + 1;
 
				NetworkUpdateClientInfo(ci->client_index);
 

	
 
				if (ci->client_playas != 0 && ci->client_playas <= MAX_PLAYERS) {
 
					_network_player_info[p->index].months_empty = 0;
 

	
 
					memcpy(_decode_parameters, ci->client_name, 32);
 
					/* XXX - What are the consequents of this? It is needed, but is it bad? */
 
					_docommand_recursive = 0;
 
					DoCommandP(0, ci->client_playas-1, 0, NULL, CMD_CHANGE_PRESIDENT_NAME | CMD_MSG(STR_700D_CAN_T_CHANGE_PRESIDENT));
 
				}
 
			}
 
		} else {
 
			if (_network_server) {
 
				NetworkClientInfo *ci;
 
				// UGLY! p2 is mis-used to fetch the client-id
 
				ci = &_network_client_info[p2];
 
				ci->client_playas = OWNER_SPECTATOR;
 
				NetworkUpdateClientInfo(ci->client_index);
 
			}
 
#endif /* ENABLE_NETWORK */
 
		}
 
		break;
 
	case 1: // make new ai player
 
		DoStartupNewPlayer(true);
 
		break;
 
	case 2: // delete player
 
		p = DEREF_PLAYER(p2);
 

	
 
		/* Only allow removal of HUMAN companies */
 
		if (IS_HUMAN_PLAYER(p2)) {
 
			/* Delete any open window of the company */
 
			DeletePlayerWindows(p2);
 

	
 
			/* Show the bankrupt news */
 
			SetDParam(0, p->name_1);
 
			SetDParam(1, p->name_2);
 
			AddNewsItem( (StringID)(p2 + 16*3), NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
 

	
 
			/* Remove the company */
 
			ChangeOwnershipOfPlayerItems(p2, 255);
 
			p->money64 = p->player_money = 100000000;
 
			p->is_active = false;
 
		}
 
		break;
 

	
 
	case 3: // join player
 
		pi = (byte)(p1 >> 8);
 
		ChangeOwnershipOfPlayerItems(pi, (byte)(p1 >> 16));
 
		DeletePlayerStuff(pi);
 
		break;
 
	}
 

	
 

	
 
	return 0;
 
}
 

	
 
// Save/load of players
 
static const byte _player_desc[] = {
 
	SLE_VAR(Player,name_2,					SLE_UINT32),
 
	SLE_VAR(Player,name_1,					SLE_STRINGID),
 

	
 
	SLE_VAR(Player,president_name_1,SLE_UINT16),
 
	SLE_VAR(Player,president_name_2,SLE_UINT32),
 

	
 
	SLE_VAR(Player,face,						SLE_UINT32),
 

	
 
	// money was changed to a 64 bit field in savegame version 1.
 
	SLE_CONDVAR(Player,money64,			SLE_VAR_I64 | SLE_FILE_I32, 0, 0),
 
	SLE_CONDVAR(Player,money64,			SLE_INT64, 1, 255),
 

	
 
	SLE_VAR(Player,current_loan,		SLE_INT32),
 

	
 
	SLE_VAR(Player,player_color,		SLE_UINT8),
 
	SLE_VAR(Player,player_money_fraction,SLE_UINT8),
 
	SLE_VAR(Player,max_railtype,		SLE_UINT8),
 
	SLE_VAR(Player,block_preview,		SLE_UINT8),
 

	
 
	SLE_VAR(Player,cargo_types,			SLE_UINT16),
 
	SLE_VAR(Player,location_of_house,SLE_UINT16),
 
	SLE_VAR(Player,last_build_coordinate,SLE_UINT16),
 
	SLE_VAR(Player,inaugurated_year,SLE_UINT8),
 

	
 
	SLE_ARR(Player,share_owners,		SLE_UINT8, 4),
 

	
 
	SLE_VAR(Player,num_valid_stat_ent,SLE_UINT8),
 

	
 
	SLE_VAR(Player,quarters_of_bankrupcy,SLE_UINT8),
 
	SLE_VAR(Player,bankrupt_asked,	SLE_UINT8),
 
	SLE_VAR(Player,bankrupt_timeout,SLE_INT16),
 
	SLE_VAR(Player,bankrupt_value,	SLE_INT32),
 

	
 
	// yearly expenses was changed to 64-bit in savegame version 2.
 
	SLE_CONDARR(Player,yearly_expenses,	SLE_FILE_I32|SLE_VAR_I64, 3*13, 0, 1),
 
	SLE_CONDARR(Player,yearly_expenses,	SLE_INT64, 3*13, 2, 255),
 

	
 
	SLE_CONDVAR(Player,is_ai,			SLE_UINT8, 2, 255),
 
	SLE_CONDVAR(Player,is_active,	SLE_UINT8, 4, 255),
 

	
 
	// reserve extra space in savegame here. (currently 64 bytes)
 
	SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 8, 2, 255),
 

	
 
	SLE_END()
 
};
 

	
 
static const byte _player_economy_desc[] = {
 
	// these were changed to 64-bit in savegame format 2
 
	SLE_CONDVAR(PlayerEconomyEntry,income,							SLE_INT32, 0, 1),
 
	SLE_CONDVAR(PlayerEconomyEntry,expenses,						SLE_INT32, 0, 1),
 
	SLE_CONDVAR(PlayerEconomyEntry,company_value,				SLE_INT32, 0, 1),
 
	SLE_CONDVAR(PlayerEconomyEntry,income,	SLE_FILE_I64 | SLE_VAR_I32, 2, 255),
 
	SLE_CONDVAR(PlayerEconomyEntry,expenses,SLE_FILE_I64 | SLE_VAR_I32, 2, 255),
 
	SLE_CONDVAR(PlayerEconomyEntry,company_value,SLE_FILE_I64 | SLE_VAR_I32, 2, 255),
 

	
 
	SLE_VAR(PlayerEconomyEntry,delivered_cargo,			SLE_INT32),
 
	SLE_VAR(PlayerEconomyEntry,performance_history,	SLE_INT32),
 

	
 
	SLE_END()
 
};
 

	
 
static const byte _player_ai_desc[] = {
 
	SLE_VAR(PlayerAI,state,							SLE_UINT8),
 
	SLE_VAR(PlayerAI,tick,							SLE_UINT8),
 
	SLE_VAR(PlayerAI,state_counter,			SLE_UINT16),
 
	SLE_VAR(PlayerAI,timeout_counter,		SLE_UINT16),
 

	
 
	SLE_VAR(PlayerAI,state_mode,				SLE_UINT8),
 
	SLE_VAR(PlayerAI,banned_tile_count,	SLE_UINT8),
 
	SLE_VAR(PlayerAI,railtype_to_use,		SLE_UINT8),
 

	
 
	SLE_VAR(PlayerAI,cargo_type,				SLE_UINT8),
 
	SLE_VAR(PlayerAI,num_wagons,				SLE_UINT8),
 
	SLE_VAR(PlayerAI,build_kind,				SLE_UINT8),
 
	SLE_VAR(PlayerAI,num_build_rec,			SLE_UINT8),
 
	SLE_VAR(PlayerAI,num_loco_to_build,	SLE_UINT8),
 
	SLE_VAR(PlayerAI,num_want_fullload,	SLE_UINT8),
 

	
 
	SLE_VAR(PlayerAI,route_type_mask,		SLE_UINT8),
 

	
 
	SLE_VAR(PlayerAI,start_tile_a,			SLE_UINT16),
 
	SLE_VAR(PlayerAI,cur_tile_a,				SLE_UINT16),
 
	SLE_VAR(PlayerAI,start_dir_a,				SLE_UINT8),
 
	SLE_VAR(PlayerAI,cur_dir_a,					SLE_UINT8),
 

	
 
	SLE_VAR(PlayerAI,start_tile_b,			SLE_UINT16),
 
	SLE_VAR(PlayerAI,cur_tile_b,				SLE_UINT16),
 
	SLE_VAR(PlayerAI,start_dir_b,				SLE_UINT8),
 
	SLE_VAR(PlayerAI,cur_dir_b,					SLE_UINT8),
 

	
 
	SLE_REF(PlayerAI,cur_veh,						REF_VEHICLE),
 

	
 
	SLE_ARR(PlayerAI,wagon_list,				SLE_UINT16, 9),
 
	SLE_ARR(PlayerAI,order_list_blocks,	SLE_UINT8, 20),
 
	SLE_ARR(PlayerAI,banned_tiles,			SLE_UINT16, 16),
 

	
 
	SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 8, 2, 255),
 
	SLE_END()
 
};
 

	
 
static const byte _player_ai_build_rec_desc[] = {
 
	SLE_VAR(AiBuildRec,spec_tile,			SLE_UINT16),
 
	SLE_VAR(AiBuildRec,use_tile,			SLE_UINT16),
 
	SLE_VAR(AiBuildRec,rand_rng,			SLE_UINT8),
 
	SLE_VAR(AiBuildRec,cur_building_rule,SLE_UINT8),
 
	SLE_VAR(AiBuildRec,unk6,					SLE_UINT8),
 
	SLE_VAR(AiBuildRec,unk7,					SLE_UINT8),
 
	SLE_VAR(AiBuildRec,buildcmd_a,		SLE_UINT8),
 
	SLE_VAR(AiBuildRec,buildcmd_b,		SLE_UINT8),
 
	SLE_VAR(AiBuildRec,direction,			SLE_UINT8),
 
	SLE_VAR(AiBuildRec,cargo,					SLE_UINT8),
 
	SLE_END()
 
};
 

	
 
static void SaveLoad_PLYR(Player *p) {
 
	int i;
 

	
 
	SlObject(p, _player_desc);
 

	
 
	// Write AI?
 
	if (!IS_HUMAN_PLAYER(p->index)) {
 
		SlObject(&p->ai, _player_ai_desc);
 
		for(i=0; i!=p->ai.num_build_rec; i++)
 
			SlObject(&p->ai.src + i, _player_ai_build_rec_desc);
 
	}
 

	
 
	// Write economy
 
	SlObject(&p->cur_economy, _player_economy_desc);
 

	
 
	// Write old economy entries.
 
	{
 
		PlayerEconomyEntry *pe;
 
		for(i=p->num_valid_stat_ent,pe=p->old_economy; i!=0; i--,pe++)
 
			SlObject(pe, _player_economy_desc);
 
	}
 
}
 

	
 
static void Save_PLYR()
 
{
 
	Player *p;
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active) {
 
			SlSetArrayIndex(p->index);
 
			SlAutolength((AutolengthProc*)SaveLoad_PLYR, p);
 
		}
 
	}
 
}
 

	
 
static void Load_PLYR()
 
{
 
	int index;
 
	while ((index = SlIterateArray()) != -1) {
 
		Player *p = DEREF_PLAYER(index);
 
		p->is_ai = (index != 0);
 
		SaveLoad_PLYR(p);
 
		_player_colors[index] = p->player_color;
 
		UpdatePlayerMoney32(p);
 
	}
 
}
 

	
 
const ChunkHandler _player_chunk_handlers[] = {
 
	{ 'PLYR', Save_PLYR, Load_PLYR, CH_ARRAY | CH_LAST},
 
};
 

	
0 comments (0 inline, 0 general)