rocksndiamonds-3.3.0.0
[rocksndiamonds.git] / src / network.c
index 104b83f9bb2df69b3e36c2f83107fe37d0bd6772..fd98b62d8e72ca9ce7522f905d799ca468a563cd 100644 (file)
@@ -1,7 +1,7 @@
 /***********************************************************
 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
 *----------------------------------------------------------*
-* (c) 1995-2002 Artsoft Entertainment                      *
+* (c) 1995-2006 Artsoft Entertainment                      *
 *               Holger Schemel                             *
 *               Detmolder Strasse 189                      *
 *               33604 Bielefeld                            *
@@ -66,6 +66,7 @@ static byte realbuffer[512];
 static byte readbuffer[MAX_BUFFER_SIZE], writbuffer[MAX_BUFFER_SIZE];
 static byte *buffer = realbuffer + 4;
 static int nread = 0, nwrite = 0;
+static boolean stop_network_game = FALSE;
 
 static void SendBufferToServer(int size)
 {
@@ -307,7 +308,7 @@ void SendToServer_NrWanted(int nr_wanted)
 
 void SendToServer_StartPlaying()
 {
-  unsigned long new_random_seed = InitRND(NEW_RANDOMIZE);
+  unsigned long new_random_seed = InitRND(level.random_seed);
 
   int dummy = 0;               /* !!! HAS NO MEANING ANYMORE !!! */
                                /* the name of the level must be enough */
@@ -342,11 +343,12 @@ void SendToServer_ContinuePlaying()
   SendBufferToServer(2);
 }
 
-void SendToServer_StopPlaying()
+void SendToServer_StopPlaying(int cause_for_stopping)
 {
   buffer[1] = OP_STOP_PLAYING;
+  buffer[2] = cause_for_stopping;
 
-  SendBufferToServer(2);
+  SendBufferToServer(3);
 }
 
 void SendToServer_MovePlayer(byte player_action)
@@ -429,11 +431,11 @@ static void Handle_OP_NUMBER_WANTED()
   }
   else if (old_client_nr == first_player.nr)   /* failed -- local player? */
   {
-    char *color[] = { "yellow", "red", "green", "blue" };
     char request[100];
 
-    sprintf(request, "Sorry ! %s player still exists ! You are %s player !",
-           color[index_nr_wanted], color[new_index_nr]);
+    sprintf(request, "Sorry ! Player %d already exists ! You are player %d !",
+           index_nr_wanted + 1, new_index_nr + 1);
+
     Request(request, REQ_CONFIRM);
 
     Error(ERR_NETWORK_CLIENT, "cannot switch -- you keep client # %d",
@@ -532,20 +534,7 @@ static void Handle_OP_START_PLAYING()
   LoadTape(level_nr);
   LoadLevel(level_nr);
 
-#if 1
   StartGameActions(FALSE, setup.autorecord, new_random_seed);
-#else
-  if (setup.autorecord)
-    TapeStartRecording();
-
-  if (tape.recording)
-    tape.random_seed = new_random_seed;
-
-  InitRND(new_random_seed);
-
-  game_status = GAME_MODE_PLAYING;
-  InitGame();
-#endif
 }
 
 static void Handle_OP_PAUSE_PLAYING()
@@ -568,8 +557,18 @@ static void Handle_OP_CONTINUE_PLAYING()
 
 static void Handle_OP_STOP_PLAYING()
 {
-  printf("OP_STOP_PLAYING: %d\n", buffer[0]);
-  Error(ERR_NETWORK_CLIENT, "client %d stops game", buffer[0]);
+  printf("OP_STOP_PLAYING: %d [%d]\n", buffer[0], buffer[2]);
+  Error(ERR_NETWORK_CLIENT, "client %d stops game [%d]", buffer[0], buffer[2]);
+
+  if (game_status == GAME_MODE_PLAYING)
+  {
+    if (buffer[2] == NETWORK_STOP_BY_PLAYER)
+      Request("Network game stopped by player!", REQ_CONFIRM);
+    else if (buffer[2] == NETWORK_STOP_BY_ERROR)
+      Request("Network game stopped due to internal error!", REQ_CONFIRM);
+    else
+      Request("Network game stopped !", REQ_CONFIRM);
+  }
 
   game_status = GAME_MODE_MAIN;
   DrawMainMenu();
@@ -586,12 +585,21 @@ static void Handle_OP_MOVE_PLAYER(unsigned int len)
   server_frame_counter =
     (buffer[2] << 24) | (buffer[3] << 16) | (buffer[4] << 8) | (buffer[5]);
 
+#if 0
+  Error(ERR_NETWORK_CLIENT, "receiving server frame counter value %d [%d]",
+       server_frame_counter, FrameCounter);
+#endif
+
   if (server_frame_counter != FrameCounter)
   {
-    Error(ERR_RETURN, "client and servers frame counters out of sync");
-    Error(ERR_RETURN, "frame counter of client is %d", FrameCounter);
-    Error(ERR_RETURN, "frame counter of server is %d", server_frame_counter);
-    Error(ERR_EXIT,   "this should not happen -- please debug");
+    Error(ERR_INFO, "client and servers frame counters out of sync");
+    Error(ERR_INFO, "frame counter of client is %d", FrameCounter);
+    Error(ERR_INFO, "frame counter of server is %d", server_frame_counter);
+    Error(ERR_INFO, "this should not happen -- please debug");
+
+    stop_network_game = TRUE;
+
+    return;
   }
 
   /* copy valid player actions */
@@ -606,6 +614,8 @@ static void HandleNetworkingMessages()
 {
   unsigned int message_length;
 
+  stop_network_game = FALSE;
+
   while (nread >= 4 && nread >= 4 + readbuffer[3])
   {
     message_length = readbuffer[3];
@@ -616,7 +626,7 @@ static void HandleNetworkingMessages()
     nread -= 4 + message_length;
     memmove(readbuffer, readbuffer + 4 + message_length, nread);
 
-    switch(buffer[1])
+    switch (buffer[1])
     {
       case OP_BAD_PROTOCOL_VERSION:
        Handle_OP_BAD_PROTOCOL_VERSION();
@@ -670,6 +680,10 @@ static void HandleNetworkingMessages()
   }
 
   fflush(stdout);
+
+  /* in case of internal error, stop network game */
+  if (stop_network_game)
+    SendToServer_StopPlaying(NETWORK_STOP_BY_ERROR);
 }
 
 /* TODO */