network.c
changeset 722 f420fa9bd521
parent 716 8af847728d5b
child 732 0b3002da2589
equal deleted inserted replaced
721:617a397f8b4b 722:f420fa9bd521
    36 static uint16 _network_client_index = NETWORK_SERVER_INDEX + 1;
    36 static uint16 _network_client_index = NETWORK_SERVER_INDEX + 1;
    37 
    37 
    38 /* Some externs / forwards */
    38 /* Some externs / forwards */
    39 extern void ShowJoinStatusWindow();
    39 extern void ShowJoinStatusWindow();
    40 extern void StateGameLoop();
    40 extern void StateGameLoop();
       
    41 extern uint GetCurrentCurrencyRate();
    41 
    42 
    42 // Function that looks up the CI for a given client-index
    43 // Function that looks up the CI for a given client-index
    43 NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index)
    44 NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index)
    44 {
    45 {
    45 	NetworkClientInfo *ci;
    46 	NetworkClientInfo *ci;
    74 		snprintf(client_name, size, "%s", ci->client_name);
    75 		snprintf(client_name, size, "%s", ci->client_name);
    75 }
    76 }
    76 
    77 
    77 // This puts a text-message to the console, or in the future, the chat-box,
    78 // This puts a text-message to the console, or in the future, the chat-box,
    78 //  (to keep it all a bit more general)
    79 //  (to keep it all a bit more general)
    79 void CDECL NetworkTextMessage(NetworkAction action, uint16 color, const char *name, const char *str, ...)
    80 // If 'self_send' is true, this is the client who is sending the message
       
    81 void CDECL NetworkTextMessage(NetworkAction action, uint16 color, bool self_send, const char *name, const char *str, ...)
    80 {
    82 {
    81 	char buf[1024];
    83 	char buf[1024];
    82 	va_list va;
    84 	va_list va;
    83 	const int duration = 10; // Game days the messages stay visible
    85 	const int duration = 10; // Game days the messages stay visible
       
    86 	char message[1024];
       
    87 	char temp[1024];
       
    88 	StringID TempStr = STR_NULL;
    84 
    89 
    85 	va_start(va, str);
    90 	va_start(va, str);
    86 	vsprintf(buf, str, va);
    91 	vsprintf(buf, str, va);
    87 	va_end(va);
    92 	va_end(va);
    88 
    93 
    89 	switch (action) {
    94 	switch (action) {
    90 		case NETWORK_ACTION_JOIN_LEAVE:
    95 		case NETWORK_ACTION_JOIN:
    91 			IConsolePrintF(color, "*** %s %s", name, buf);
    96 			GetString(temp, STR_NETWORK_CLIENT_JOINED);
    92 			AddTextMessage(color, duration, "*** %s %s", name, buf);
    97 			snprintf(message, sizeof(message), "*** %s %s", name, temp);
       
    98 			break;
       
    99 		case NETWORK_ACTION_LEAVE:
       
   100 			GetString(temp, STR_NETWORK_ERR_LEFT);
       
   101 			snprintf(message, sizeof(message), "*** %s %s (%s)", name, temp, buf);
    93 			break;
   102 			break;
    94 		case NETWORK_ACTION_GIVE_MONEY:
   103 		case NETWORK_ACTION_GIVE_MONEY:
    95 			IConsolePrintF(color, "*** %s %s", name, buf);
   104 			if (self_send) {
    96 			AddTextMessage(color, duration, "*** %s %s", name, buf);
   105 				TempStr = AllocateName(name, 0);
       
   106 				SetDParam(0, TempStr);
       
   107 				SetDParam(1, atoi(buf));
       
   108 				GetString(temp, STR_NETWORK_GAVE_MONEY_AWAY);
       
   109 				DeleteName(TempStr);
       
   110 				snprintf(message, sizeof(message), "*** %s", temp);
       
   111 			} else {
       
   112 				SetDParam(0, atoi(buf));
       
   113 				GetString(temp, STR_NETWORK_GIVE_MONEY);
       
   114 				snprintf(message, sizeof(message), "*** %s %s", name, temp);
       
   115 			}
    97 			break;
   116 			break;
    98 		case NETWORK_ACTION_CHAT_PLAYER:
   117 		case NETWORK_ACTION_CHAT_PLAYER:
    99 			IConsolePrintF(color, "[Team] %s: %s", name, buf);
   118 			if (self_send) {
   100 			AddTextMessage(color, duration, "[Team] %s: %s", name, buf);
   119 				TempStr = AllocateName(name, 0);
       
   120 				SetDParam(0, TempStr);
       
   121 				GetString(temp, STR_NETWORK_CHAT_TO_COMPANY);
       
   122 				DeleteName(TempStr);
       
   123 				snprintf(message, sizeof(message), "%s %s", temp, buf);
       
   124 			} else {
       
   125 				TempStr = AllocateName(name, 0);
       
   126 				SetDParam(0, TempStr);
       
   127 				GetString(temp, STR_NETWORK_CHAT_COMPANY);
       
   128 				DeleteName(TempStr);
       
   129 				snprintf(message, sizeof(message), "%s %s", temp, buf);
       
   130 			}
   101 			break;
   131 			break;
   102 		case NETWORK_ACTION_CHAT_CLIENT:
   132 		case NETWORK_ACTION_CHAT_CLIENT:
   103 			IConsolePrintF(color, "[Private] %s: %s", name, buf);
   133 			if (self_send) {
   104 			AddTextMessage(color, duration, "[Private] %s: %s", name, buf);
   134 				TempStr = AllocateName(name, 0);
   105 			break;
   135 				SetDParam(0, TempStr);
   106 		case NETWORK_ACTION_CHAT_TO_CLIENT:
   136 				GetString(temp, STR_NETWORK_CHAT_TO_CLIENT);
   107 			IConsolePrintF(color, "[Private] To %s: %s", name, buf);
   137 				DeleteName(TempStr);
   108 			AddTextMessage(color, duration, "[Private] To %s: %s", name, buf);
   138 				snprintf(message, sizeof(message), "%s %s", temp, buf);
   109 			break;
   139 			} else {
   110 		case NETWORK_ACTION_CHAT_TO_PLAYER:
   140 				TempStr = AllocateName(name, 0);
   111 			IConsolePrintF(color, "[Team] To %s: %s", name, buf);
   141 				SetDParam(0, TempStr);
   112 			AddTextMessage(color, duration, "[Team] To %s: %s", name, buf);
   142 				GetString(temp, STR_NETWORK_CHAT_CLIENT);
       
   143 				DeleteName(TempStr);
       
   144 				snprintf(message, sizeof(message), "%s %s", temp, buf);
       
   145 			}
   113 			break;
   146 			break;
   114 		case NETWORK_ACTION_NAME_CHANGE:
   147 		case NETWORK_ACTION_NAME_CHANGE:
   115 			IConsolePrintF(color, "*** %s changed his name to %s", name, buf);
   148 			GetString(temp, STR_NETWORK_NAME_CHANGE);
   116 			AddTextMessage(color, duration, "*** %s changed his name to %s", name, buf);
   149 			snprintf(message, sizeof(message), "*** %s %s %s", name, temp, buf);
   117 			break;
   150 			break;
   118 		default:
   151 		default:
   119 			IConsolePrintF(color, "[All] %s: %s", name, buf);
   152 			TempStr = AllocateName(name, 0);
   120 			AddTextMessage(color, duration, "[All] %s: %s", name, buf);
   153 			SetDParam(0, TempStr);
       
   154 			GetString(temp, STR_NETWORK_CHAT_ALL);
       
   155 			DeleteName(TempStr);
       
   156 			snprintf(message, sizeof(message), "%s %s", temp, buf);
   121 			break;
   157 			break;
   122 	}
   158 	}
       
   159 
       
   160 	IConsolePrintF(color, message);
       
   161 	AddTextMessage(color, duration, message);
   123 }
   162 }
   124 
   163 
   125 // Calculate the frame-lag of a client
   164 // Calculate the frame-lag of a client
   126 uint NetworkCalculateLag(const NetworkClientState *cs)
   165 uint NetworkCalculateLag(const NetworkClientState *cs)
   127 {
   166 {
   442 	DEBUG(net, 1) ("[NET] Closed client connection");
   481 	DEBUG(net, 1) ("[NET] Closed client connection");
   443 
   482 
   444 	if (!cs->quited && _network_server && cs->status > STATUS_INACTIVE) {
   483 	if (!cs->quited && _network_server && cs->status > STATUS_INACTIVE) {
   445 		// We did not receive a leave message from this client...
   484 		// We did not receive a leave message from this client...
   446 		NetworkErrorCode errorno = NETWORK_ERROR_CONNECTION_LOST;
   485 		NetworkErrorCode errorno = NETWORK_ERROR_CONNECTION_LOST;
   447 		char str1[100], str2[100];
   486 		char str[100];
   448 		char client_name[NETWORK_NAME_LENGTH];
   487 		char client_name[NETWORK_NAME_LENGTH];
   449 		NetworkClientState *new_cs;
   488 		NetworkClientState *new_cs;
   450 
   489 
   451 		NetworkGetClientName(client_name, sizeof(client_name), cs);
   490 		NetworkGetClientName(client_name, sizeof(client_name), cs);
   452 
   491 
   453 		GetString(str1, STR_NETWORK_ERR_LEFT);
   492 		GetString(str, STR_NETWORK_ERR_CLIENT_GENERAL + errorno);
   454 		GetString(str2, STR_NETWORK_ERR_CLIENT_GENERAL + errorno);
   493 
   455 
   494 		NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, client_name, str);
   456 		NetworkTextMessage(NETWORK_ACTION_JOIN_LEAVE, 1, client_name, "%s (%s)", str1, str2);
       
   457 
   495 
   458 		// Inform other clients of this... strange leaving ;)
   496 		// Inform other clients of this... strange leaving ;)
   459 		FOR_ALL_CLIENTS(new_cs) {
   497 		FOR_ALL_CLIENTS(new_cs) {
   460 			if (new_cs->status > STATUS_AUTH && cs != new_cs) {
   498 			if (new_cs->status > STATUS_AUTH && cs != new_cs) {
   461 				SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, errorno);
   499 				SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, errorno);