fixed exiting players with custom elements when playing multi-player games
[rocksndiamonds.git] / src / game.c
index d37dee3a5f7c162ce69acdf33167a2d3b64f2233..c51b7ece8e596108139c1ebc94670c66497faa6a 100644 (file)
@@ -1096,7 +1096,7 @@ void ContinueMoving(int, int);
 void Bang(int, int);
 void InitMovDir(int, int);
 void InitAmoebaNr(int, int);
 void Bang(int, int);
 void InitMovDir(int, int);
 void InitAmoebaNr(int, int);
-int NewHiScore(void);
+int NewHiScore(int);
 
 void TestIfGoodThingHitsBadThing(int, int, int);
 void TestIfBadThingHitsGoodThing(int, int, int);
 
 void TestIfGoodThingHitsBadThing(int, int, int);
 void TestIfBadThingHitsGoodThing(int, int, int);
@@ -1112,6 +1112,7 @@ void TestIfGoodThingGetsHitByBadThing(int, int, int);
 void KillPlayer(struct PlayerInfo *);
 void BuryPlayer(struct PlayerInfo *);
 void RemovePlayer(struct PlayerInfo *);
 void KillPlayer(struct PlayerInfo *);
 void BuryPlayer(struct PlayerInfo *);
 void RemovePlayer(struct PlayerInfo *);
+void RemovePlayerWithCleanup(struct PlayerInfo *);
 
 static int getInvisibleActiveFromInvisibleElement(int);
 static int getInvisibleFromInvisibleActiveElement(int);
 
 static int getInvisibleActiveFromInvisibleElement(int);
 static int getInvisibleFromInvisibleActiveElement(int);
@@ -3273,6 +3274,36 @@ int get_num_special_action(int element, int action_first, int action_last)
   =============================================================================
 */
 
   =============================================================================
 */
 
+#if DEBUG_INIT_PLAYER
+static void DebugPrintPlayerStatus(char *message)
+{
+  int i;
+
+  if (!options.debug)
+    return;
+
+  printf("%s:\n", message);
+
+  for (i = 0; i < MAX_PLAYERS; i++)
+  {
+    struct PlayerInfo *player = &stored_player[i];
+
+    printf("- player %d: present == %d, connected == %d [%d/%d], active == %d",
+          i + 1,
+          player->present,
+          player->connected,
+          player->connected_locally,
+          player->connected_network,
+          player->active);
+
+    if (local_player == player)
+      printf(" (local player)");
+
+    printf("\n");
+  }
+}
+#endif
+
 void InitGame()
 {
   int full_lev_fieldx = lev_fieldx + (BorderElement != EL_EMPTY ? 2 : 0);
 void InitGame()
 {
   int full_lev_fieldx = lev_fieldx + (BorderElement != EL_EMPTY ? 2 : 0);
@@ -3553,28 +3584,7 @@ void InitGame()
     AmoebaCnt[i] = AmoebaCnt2[i] = 0;
 
 #if DEBUG_INIT_PLAYER
     AmoebaCnt[i] = AmoebaCnt2[i] = 0;
 
 #if DEBUG_INIT_PLAYER
-  if (options.debug)
-  {
-    printf("Player status at level initialization:\n");
-
-    for (i = 0; i < MAX_PLAYERS; i++)
-    {
-      struct PlayerInfo *player = &stored_player[i];
-
-      printf("- player %d: present == %d, connected == %d [%d/%d], active == %d",
-            i + 1,
-            player->present,
-            player->connected,
-            player->connected_locally,
-            player->connected_network,
-            player->active);
-
-      if (local_player == player)
-       printf(" (local player)");
-
-      printf("\n");
-    }
-  }
+  DebugPrintPlayerStatus("Player status at level initialization");
 #endif
 
   SCAN_PLAYFIELD(x, y)
 #endif
 
   SCAN_PLAYFIELD(x, y)
@@ -3726,28 +3736,7 @@ void InitGame()
   }
 
 #if DEBUG_INIT_PLAYER
   }
 
 #if DEBUG_INIT_PLAYER
-  if (options.debug)
-  {
-    printf("Player status after level initialization:\n");
-
-    for (i = 0; i < MAX_PLAYERS; i++)
-    {
-      struct PlayerInfo *player = &stored_player[i];
-
-      printf("- player %d: present == %d, connected == %d [%d/%d], active == %d",
-            i + 1,
-            player->present,
-            player->connected,
-            player->connected_locally,
-            player->connected_network,
-            player->active);
-
-      if (local_player == player)
-       printf(" (local player)");
-
-      printf("\n");
-    }
-  }
+  DebugPrintPlayerStatus("Player status after level initialization");
 #endif
 
 #if DEBUG_INIT_PLAYER
 #endif
 
 #if DEBUG_INIT_PLAYER
@@ -3834,28 +3823,7 @@ void InitGame()
   }
 
 #if DEBUG_INIT_PLAYER
   }
 
 #if DEBUG_INIT_PLAYER
-  if (options.debug)
-  {
-    printf("Player status after player assignment (first stage):\n");
-
-    for (i = 0; i < MAX_PLAYERS; i++)
-    {
-      struct PlayerInfo *player = &stored_player[i];
-
-      printf("- player %d: present == %d, connected == %d [%d/%d], active == %d",
-            i + 1,
-            player->present,
-            player->connected,
-            player->connected_locally,
-            player->connected_network,
-            player->active);
-
-      if (local_player == player)
-       printf(" (local player)");
-
-      printf("\n");
-    }
-  }
+  DebugPrintPlayerStatus("Player status after player assignment (first stage)");
 #endif
 
 #else
 #endif
 
 #else
@@ -3996,28 +3964,7 @@ void InitGame()
   }
 
 #if DEBUG_INIT_PLAYER
   }
 
 #if DEBUG_INIT_PLAYER
-  if (options.debug)
-  {
-    printf("Player status after player assignment (final stage):\n");
-
-    for (i = 0; i < MAX_PLAYERS; i++)
-    {
-      struct PlayerInfo *player = &stored_player[i];
-
-      printf("- player %d: present == %d, connected == %d [%d/%d], active == %d",
-            i + 1,
-            player->present,
-            player->connected,
-            player->connected_locally,
-            player->connected_network,
-            player->active);
-
-      if (local_player == player)
-       printf(" (local player)");
-
-      printf("\n");
-    }
-  }
+  DebugPrintPlayerStatus("Player status after player assignment (final stage)");
 #endif
 
   if (BorderElement == EL_EMPTY)
 #endif
 
   if (BorderElement == EL_EMPTY)
@@ -4217,28 +4164,7 @@ void InitGame()
     KeyboardAutoRepeatOffUnlessAutoplay();
 
 #if DEBUG_INIT_PLAYER
     KeyboardAutoRepeatOffUnlessAutoplay();
 
 #if DEBUG_INIT_PLAYER
-    if (options.debug)
-    {
-      printf("Player status (final):\n");
-
-      for (i = 0; i < MAX_PLAYERS; i++)
-      {
-       struct PlayerInfo *player = &stored_player[i];
-
-       printf("- player %d: present == %d, connected == %d [%d/%d], active == %d",
-              i + 1,
-              player->present,
-              player->connected,
-              player->connected_locally,
-              player->connected_network,
-              player->active);
-
-       if (local_player == player)
-         printf(" (local player)");
-
-       printf("\n");
-      }
-    }
+    DebugPrintPlayerStatus("Player status (final)");
 #endif
   }
 
 #endif
   }
 
@@ -4725,7 +4651,7 @@ void GameWon()
 void GameEnd()
 {
   int hi_pos;
 void GameEnd()
 {
   int hi_pos;
-  boolean raise_level = FALSE;
+  int last_level_nr = level_nr;
 
   local_player->LevelSolved_GameEnd = TRUE;
 
 
   local_player->LevelSolved_GameEnd = TRUE;
 
@@ -4768,35 +4694,39 @@ void GameEnd()
 
   if (setup.increment_levels &&
       level_nr < leveldir_current->last_level)
 
   if (setup.increment_levels &&
       level_nr < leveldir_current->last_level)
-    raise_level = TRUE;                        /* advance to next level */
-
-  if ((hi_pos = NewHiScore()) >= 0) 
   {
   {
-    SetGameStatus(GAME_MODE_SCORES);
-
-    DrawHallOfFame(hi_pos);
+    level_nr++;                /* advance to next level */
+    TapeErase();       /* start with empty tape */
 
 
-    if (raise_level)
+    if (setup.auto_play_next_level)
     {
     {
-      level_nr++;
-      TapeErase();
+      LoadLevel(level_nr);
+
+      SaveLevelSetup_SeriesInfo();
     }
   }
     }
   }
-  else
+
+  hi_pos = NewHiScore(last_level_nr);
+
+  if (hi_pos >= 0 && !setup.skip_scores_after_game)
   {
   {
-    SetGameStatus(GAME_MODE_MAIN);
+    SetGameStatus(GAME_MODE_SCORES);
 
 
-    if (raise_level)
-    {
-      level_nr++;
-      TapeErase();
-    }
+    DrawHallOfFame(last_level_nr, hi_pos);
+  }
+  else if (!setup.auto_play_next_level || !setup.increment_levels)
+  {
+    SetGameStatus(GAME_MODE_MAIN);
 
     DrawMainMenu();
   }
 
     DrawMainMenu();
   }
+  else
+  {
+    StartGameActions(network.enabled, setup.autorecord, level.random_seed);
+  }
 }
 
 }
 
-int NewHiScore()
+int NewHiScore(int level_nr)
 {
   int k, l;
   int position = -1;
 {
   int k, l;
   int position = -1;
@@ -9846,7 +9776,10 @@ static void ExecuteCustomElementAction(int x, int y, int element, int page)
     {
       for (i = 0; i < MAX_PLAYERS; i++)
        if (action_arg_player_bits & (1 << i))
     {
       for (i = 0; i < MAX_PLAYERS; i++)
        if (action_arg_player_bits & (1 << i))
-         PlayerWins(&stored_player[i]);
+         RemovePlayerWithCleanup(&stored_player[i]);
+
+      if (AllPlayersGone)
+       PlayerWins(local_player);
 
       break;
     }
 
       break;
     }
@@ -12712,12 +12645,12 @@ void ScrollPlayer(struct PlayerInfo *player, int mode)
        Feld[jx][jy] == EL_SP_EXIT_OPEN ||
        Feld[jx][jy] == EL_SP_EXIT_OPENING)     /* <-- special case */
     {
        Feld[jx][jy] == EL_SP_EXIT_OPEN ||
        Feld[jx][jy] == EL_SP_EXIT_OPENING)     /* <-- special case */
     {
-      DrawPlayer(player);      /* needed here only to cleanup last field */
-      RemovePlayer(player);
+      RemovePlayerWithCleanup(player);
 
 
-      if (local_player->friends_still_needed == 0 ||
-         IS_SP_ELEMENT(Feld[jx][jy]))
-       PlayerWins(player);
+      if ((local_player->friends_still_needed == 0 ||
+          IS_SP_ELEMENT(Feld[jx][jy])) &&
+         AllPlayersGone)
+       PlayerWins(local_player);
     }
 
     /* this breaks one level: "machine", level 000 */
     }
 
     /* this breaks one level: "machine", level 000 */
@@ -13473,6 +13406,12 @@ void RemovePlayer(struct PlayerInfo *player)
   ExitY = ZY = jy;
 }
 
   ExitY = ZY = jy;
 }
 
+void RemovePlayerWithCleanup(struct PlayerInfo *player)
+{
+  DrawPlayer(player);  /* needed here only to cleanup last field */
+  RemovePlayer(player);
+}
+
 static void setFieldForSnapping(int x, int y, int element, int direction)
 {
   struct ElementInfo *ei = &element_info[element];
 static void setFieldForSnapping(int x, int y, int element, int direction)
 {
   struct ElementInfo *ei = &element_info[element];