X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=src%2Fnetwork.c;h=f5148cd44615bd890860122333d5a4d216a4f1b0;hp=cabcfd3030485ce0b55fc24ba40f363f171bb89f;hb=fa7336e50e90fef00ce1f9c88dada7b530172e25;hpb=fcc5bdf84b1500eb31db02ef5aab51012338db7f diff --git a/src/network.c b/src/network.c index cabcfd30..f5148cd4 100644 --- a/src/network.c +++ b/src/network.c @@ -52,24 +52,45 @@ static boolean stop_network_game = FALSE; static void DrawNetworkTextExt(char *message, int font_nr, boolean initialize) { static int xpos = 0, ypos = 0; + static int max_line_width = 0; int font_width = getFontWidth(font_nr); int font_height = getFontHeight(font_nr); + int ypos_1 = 120; + int ypos_2 = 150; if (initialize) { - xpos = (WIN_XSIZE - getTextWidth(message, font_nr)) / 2; - ypos = 120; + if (game_status == GAME_MODE_LOADING) + { + max_line_width = WIN_XSIZE; + + xpos = (max_line_width - getTextWidth(message, font_nr)) / 2; + ypos = ypos_1; + + DrawText(xpos, ypos, message, font_nr); + + xpos = 0; + ypos = ypos_2; + } + else + { + max_line_width = SXSIZE; - DrawText(xpos, ypos, message, font_nr); + DrawTextSCentered(ypos_1, font_nr, message); - xpos = 0; - ypos = 150; + /* calculate offset to x position caused by rounding */ + int max_chars_per_line = max_line_width / font_width; + int xoffset = (max_line_width - max_chars_per_line * font_width) / 2; + + xpos = SX + xoffset; + ypos = SY + ypos_2; + } Error(ERR_DEBUG, "========== %s ==========", message); } else { - int max_chars_per_line = WIN_XSIZE / font_width; + int max_chars_per_line = max_line_width / font_width; int max_lines_per_text = 10; int num_lines_spacing = (font_nr == FC_YELLOW ? 1 : 3); int num_lines_printed = DrawTextBuffer(xpos, ypos, message, font_nr, @@ -219,25 +240,34 @@ boolean ConnectToServer(char *hostname, int port) DrawNetworkText("Looking for local network server ..."); - if (SDLNet_CheckSockets(udp_socket_set, 500) == 1) + /* wait for any local network server to answer UDP broadcast */ + for (i = 0; i < 5; i++) { - int num_packets = SDLNet_UDP_Recv(udp, &packet); - - if (num_packets == 1) + if (SDLNet_CheckSockets(udp_socket_set, 0) == 1) { - DrawNetworkText_Success("Network server found!"); + int num_packets = SDLNet_UDP_Recv(udp, &packet); - server_host = SDLNet_Read32(&packet.address.host); + if (num_packets == 1) + { + DrawNetworkText_Success("Network server found!"); + + server_host = SDLNet_Read32(&packet.address.host); + } + else + { + DrawNetworkText_Failed("No answer from network server!"); + } + + break; } else { - DrawNetworkText_Failed("No answer from network server!"); + Delay_WithScreenUpdates(100); } } - else - { + + if (server_host == 0) DrawNetworkText_Failed("No network server found!"); - } } rfds = SDLNet_AllocSocketSet(1); @@ -307,7 +337,7 @@ boolean ConnectToServer(char *hostname, int port) return TRUE; } - Delay(100); + Delay_WithScreenUpdates(100); } DrawNetworkText_Failed("Failed to connect to network server!"); @@ -432,6 +462,8 @@ static void Handle_OP_YOUR_NUMBER() Error(ERR_EXIT, "sorry, more than %d players not allowed", MAX_PLAYERS); Error(ERR_NETWORK_CLIENT, "you get client # %d", new_client_nr); + + stored_player[new_index_nr].connected_network = TRUE; } static void Handle_OP_NUMBER_WANTED() @@ -488,6 +520,9 @@ static void Handle_OP_NUMBER_WANTED() Error(ERR_NETWORK_CLIENT, "cannot switch -- you keep client # %d", new_client_nr); } + + if (game_status == GAME_MODE_MAIN) + DrawNetworkPlayers(); } static void Handle_OP_PLAYER_NAME(unsigned int len) @@ -547,6 +582,24 @@ static void Handle_OP_PLAYER_DISCONNECTED() stored_player[index_nr].connected_locally = FALSE; stored_player[index_nr].connected_network = FALSE; + + if (game_status == GAME_MODE_PLAYING) + { + char message[100]; + + sprintf(message, "Player %d left network server! Network game stopped!", + player_nr); + + Request(message, REQ_CONFIRM | REQ_STAY_CLOSED); + + SetGameStatus(GAME_MODE_MAIN); + + DrawMainMenu(); + } + else if (game_status == GAME_MODE_MAIN) + { + DrawNetworkPlayers(); + } } static void Handle_OP_START_PLAYING() @@ -567,8 +620,9 @@ static void Handle_OP_START_PLAYING() { Error(ERR_WARN, "no such level identifier: '%s'", new_leveldir_identifier); - new_leveldir = leveldir_first; - Error(ERR_WARN, "using default level set: '%s'", new_leveldir->identifier); + stop_network_game = TRUE; + + return; } printf("OP_START_PLAYING: %d\n", buffer[0]); @@ -591,8 +645,11 @@ static void Handle_OP_PAUSE_PLAYING() printf("OP_PAUSE_PLAYING: %d\n", buffer[0]); Error(ERR_NETWORK_CLIENT, "client %d pauses game", buffer[0]); - tape.pausing = TRUE; - DrawVideoDisplay(VIDEO_STATE_PAUSE_ON,0); + if (game_status == GAME_MODE_PLAYING) + { + tape.pausing = TRUE; + DrawVideoDisplay(VIDEO_STATE_PAUSE_ON,0); + } } static void Handle_OP_CONTINUE_PLAYING() @@ -600,8 +657,11 @@ static void Handle_OP_CONTINUE_PLAYING() printf("OP_CONTINUE_PLAYING: %d\n", buffer[0]); Error(ERR_NETWORK_CLIENT, "client %d continues game", buffer[0]); - tape.pausing = FALSE; - DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0); + if (game_status == GAME_MODE_PLAYING) + { + tape.pausing = FALSE; + DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0); + } } static void Handle_OP_STOP_PLAYING() @@ -615,19 +675,21 @@ static void Handle_OP_STOP_PLAYING() int index_nr = client_nr - 1; struct PlayerInfo *client_player = &stored_player[index_nr]; boolean stopped_by_remote_player = (!client_player->connected_locally); - char *message = (buffer[2] == NETWORK_STOP_BY_PLAYER ? - "Network game stopped by player!" : - buffer[2] == NETWORK_STOP_BY_ERROR ? - "Network game stopped due to internal error!" : - "Network game stopped!"); + char message[100]; + + sprintf(message, (buffer[2] == NETWORK_STOP_BY_PLAYER ? + "Network game stopped by player %d!" : + buffer[2] == NETWORK_STOP_BY_ERROR ? + "Network game stopped due to internal error!" : + "Network game stopped!"), client_nr); if (buffer[2] != NETWORK_STOP_BY_PLAYER || stopped_by_remote_player) Request(message, REQ_CONFIRM | REQ_STAY_CLOSED); - } - SetGameStatus(GAME_MODE_MAIN); + SetGameStatus(GAME_MODE_MAIN); - DrawMainMenu(); + DrawMainMenu(); + } } static void Handle_OP_MOVE_PLAYER(unsigned int len) @@ -769,6 +831,17 @@ static char *HandleNetworkingPackets() return NULL; } +static void FreeNetworkClientPlayerInfo(struct NetworkClientPlayerInfo *player) +{ + if (player == NULL) + return; + + if (player->next) + FreeNetworkClientPlayerInfo(player->next); + + checked_free(player); +} + static void HandleNetworkingDisconnect() { int i; @@ -785,6 +858,11 @@ static void HandleNetworkingDisconnect() for (i = 0; i < MAX_PLAYERS; i++) stored_player[i].connected_network = FALSE; + + FreeNetworkClientPlayerInfo(first_player.next); + + first_player.nr = 0; + first_player.next = NULL; } void HandleNetworking() @@ -806,6 +884,9 @@ void HandleNetworking() else { Request(error_message, REQ_CONFIRM); + + if (game_status == GAME_MODE_MAIN) + ClearNetworkPlayers(); } } } @@ -820,5 +901,5 @@ void DisconnectFromNetworkServer() DrawNetworkText_Success("Successfully disconnected!"); /* short time to recognize result of network initialization */ - Delay(1000); + Delay_WithScreenUpdates(1000); }