moved duplicated code to separate function
[rocksndiamonds.git] / src / game.c
index 18d020bdf802d695ae9d10429b854a4613916456..572bd209fc06fb15067f771af201052e6ee4408a 100644 (file)
@@ -4667,6 +4667,8 @@ void GameWon(void)
 
 void GameEnd(void)
 {
+  /* used instead of "level_nr" (needed for network games) */
+  int last_level_nr = levelset.level_nr;
   int hi_pos;
 
   local_player->LevelSolved_GameEnd = TRUE;
@@ -4723,16 +4725,16 @@ void GameEnd(void)
     }
   }
 
-  /* used instead of last "level_nr" (for network games) */
-  hi_pos = NewHiScore(levelset.level_nr);
+  hi_pos = NewHiScore(last_level_nr);
 
   if (hi_pos >= 0 && !setup.skip_scores_after_game)
   {
     SetGameStatus(GAME_MODE_SCORES);
 
-    DrawHallOfFame(levelset.level_nr, hi_pos);
+    DrawHallOfFame(last_level_nr, hi_pos);
   }
   else if (setup.auto_play_next_level && setup.increment_levels &&
+          last_level_nr < leveldir_current->last_level &&
           !network_playing)
   {
     StartGameActions(network.enabled, setup.autorecord, level.random_seed);
@@ -8896,7 +8898,6 @@ static void Life(int ax, int ay)
   int *life_parameter = (element == EL_GAME_OF_LIFE ? level.game_of_life :
                         level.biomaze);
   boolean changed = FALSE;
-  boolean use_life_bugs = FALSE;
 
   if (IS_ANIMATED(graphic))
     DrawLevelGraphicAnimationIfNeeded(ax, ay, graphic);
@@ -8933,7 +8934,7 @@ static void Life(int ax, int ay)
       boolean is_player_cell = (element == EL_GAME_OF_LIFE && IS_PLAYER(x, y));
       boolean is_neighbour = FALSE;
 
-      if (use_life_bugs)
+      if (level.use_life_bugs)
        is_neighbour =
          (((Feld[x][y] == element || is_player_cell) && !Stop[x][y]) ||
           (IS_FREE(x, y)                             &&  Stop[x][y]));
@@ -8947,7 +8948,7 @@ static void Life(int ax, int ay)
 
     boolean is_free = FALSE;
 
-    if (use_life_bugs)
+    if (level.use_life_bugs)
       is_free = (IS_FREE(xx, yy));
     else
       is_free = (IS_FREE(xx, yy) && Last[xx][yy] == EL_EMPTY);
@@ -11080,11 +11081,8 @@ static void SetTapeActionFromMouseAction(byte *tape_action,
   tape_action[TAPE_ACTION_BUTTON] = mouse_action->button;
 }
 
-static void CheckLevelTime(void)
+static void CheckLevelSolved(void)
 {
-  int i;
-
-  /* !!! SAME CODE AS IN "GameActions()" -- FIX THIS !!! */
   if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
   {
     if (level.native_em_level->lev->home == 0) /* all players at home */
@@ -11132,6 +11130,11 @@ static void CheckLevelTime(void)
     if (game_mm.game_over)                             /* game lost */
       AllPlayersGone = TRUE;
   }
+}
+
+static void CheckLevelTime(void)
+{
+  int i;
 
   if (TimeFrames >= FRAMES_PER_SECOND)
   {
@@ -11296,54 +11299,7 @@ static void GameActionsExt(void)
   if (game.restart_level)
     StartGameActions(network.enabled, setup.autorecord, level.random_seed);
 
-  /* !!! SAME CODE AS IN "CheckLevelTime()" -- FIX THIS !!! */
-  if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
-  {
-    if (level.native_em_level->lev->home == 0) /* all players at home */
-    {
-      PlayerWins(local_player);
-
-      AllPlayersGone = TRUE;
-
-      level.native_em_level->lev->home = -1;
-    }
-
-    if (level.native_em_level->ply[0]->alive == 0 &&
-       level.native_em_level->ply[1]->alive == 0 &&
-       level.native_em_level->ply[2]->alive == 0 &&
-       level.native_em_level->ply[3]->alive == 0)      /* all dead */
-      AllPlayersGone = TRUE;
-  }
-  else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
-  {
-    if (game_sp.LevelSolved &&
-       !game_sp.GameOver)                              /* game won */
-    {
-      PlayerWins(local_player);
-
-      game_sp.GameOver = TRUE;
-
-      AllPlayersGone = TRUE;
-    }
-
-    if (game_sp.GameOver)                              /* game lost */
-      AllPlayersGone = TRUE;
-  }
-  else if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
-  {
-    if (game_mm.level_solved &&
-       !game_mm.game_over)                             /* game won */
-    {
-      PlayerWins(local_player);
-
-      game_mm.game_over = TRUE;
-
-      AllPlayersGone = TRUE;
-    }
-
-    if (game_mm.game_over)                             /* game lost */
-      AllPlayersGone = TRUE;
-  }
+  CheckLevelSolved();
 
   if (local_player->LevelSolved && !local_player->LevelSolved_GameEnd)
     GameWon();
@@ -11542,6 +11498,7 @@ static void GameActionsExt(void)
 
   BlitScreenToBitmap(backbuffer);
 
+  CheckLevelSolved();
   CheckLevelTime();
 
   AdvanceFrameAndPlayerCounters(-1);   /* advance counters for all players */