fixed exiting players with custom elements when playing multi-player games
[rocksndiamonds.git] / src / game.c
index 2f57bf51cc7f32a6aee74dc3e04f5c3c5dd10fb3..c51b7ece8e596108139c1ebc94670c66497faa6a 100644 (file)
@@ -1096,7 +1096,7 @@ void ContinueMoving(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);
@@ -1112,6 +1112,7 @@ void TestIfGoodThingGetsHitByBadThing(int, int, int);
 void KillPlayer(struct PlayerInfo *);
 void BuryPlayer(struct PlayerInfo *);
 void RemovePlayer(struct PlayerInfo *);
+void RemovePlayerWithCleanup(struct PlayerInfo *);
 
 static int getInvisibleActiveFromInvisibleElement(int);
 static int getInvisibleFromInvisibleActiveElement(int);
@@ -1731,7 +1732,7 @@ static void InitPlayerField(int x, int y, int element, boolean init_game)
     if (game.use_block_last_field_bug)
       player->block_delay_adjustment = (player->block_last_field ? -1 : 1);
 
-    if (!options.network || player->connected)
+    if (!network.enabled || player->connected_network)
     {
       player->active = TRUE;
 
@@ -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);
@@ -3321,7 +3352,7 @@ void InitGame()
   InitGameControlValues();
 
   /* don't play tapes over network */
-  network_playing = (options.network && !tape.playing);
+  network_playing = (network.enabled && !tape.playing);
 
   for (i = 0; i < MAX_PLAYERS; i++)
   {
@@ -3503,11 +3534,9 @@ void InitGame()
 
   network_player_action_received = FALSE;
 
-#if defined(NETWORK_AVALIABLE)
   /* initial null action */
   if (network_playing)
     SendToServer_MovePlayer(MV_NONE);
-#endif
 
   ZX = ZY = -1;
   ExitX = ExitY = -1;
@@ -3545,18 +3574,6 @@ void InitGame()
 
   game.envelope_active = FALSE;
 
-  /* set focus to local player for network games, else to all players */
-  game.centered_player_nr = (network_playing ? local_player->index_nr : -1);
-  game.centered_player_nr_next = game.centered_player_nr;
-  game.set_centered_player = FALSE;
-
-  if (network_playing && tape.recording)
-  {
-    /* store client dependent player focus when recording network games */
-    tape.centered_player_nr_next = game.centered_player_nr_next;
-    tape.set_centered_player = TRUE;
-  }
-
   for (i = 0; i < NUM_BELTS; i++)
   {
     game.belt_dir[i] = MV_NONE;
@@ -3567,10 +3584,7 @@ void InitGame()
     AmoebaCnt[i] = AmoebaCnt2[i] = 0;
 
 #if DEBUG_INIT_PLAYER
-  if (options.debug)
-  {
-    printf("Player status at level initialization:\n");
-  }
+  DebugPrintPlayerStatus("Player status at level initialization");
 #endif
 
   SCAN_PLAYFIELD(x, y)
@@ -3684,22 +3698,33 @@ void InitGame()
       game.belt_dir_nr[i] = 3;         /* not moving, next moving left */
 
 #if USE_NEW_PLAYER_ASSIGNMENTS
-  /* !!! SAME AS init.c:InitPlayerInfo() -- FIX THIS !!! */
-  /* choose default local player */
-  local_player = &stored_player[0];
-
   for (i = 0; i < MAX_PLAYERS; i++)
+  {
     stored_player[i].connected = FALSE;
 
-  local_player->connected = TRUE;
-  /* !!! SAME AS init.c:InitPlayerInfo() -- FIX THIS !!! */
+    /* in network game mode, the local player might not be the first player */
+    if (stored_player[i].connected_locally)
+      local_player = &stored_player[i];
+  }
+
+  if (!network.enabled)
+    local_player->connected = TRUE;
 
   if (tape.playing)
   {
     for (i = 0; i < MAX_PLAYERS; i++)
       stored_player[i].connected = tape.player_participates[i];
   }
-  else if (game.team_mode && !options.network)
+  else if (network.enabled)
+  {
+    /* add team mode players connected over the network (needed for correct
+       assignment of player figures from level to locally playing players) */
+
+    for (i = 0; i < MAX_PLAYERS; i++)
+      if (stored_player[i].connected_network)
+       stored_player[i].connected = TRUE;
+  }
+  else if (game.team_mode)
   {
     /* try to guess locally connected team mode players (needed for correct
        assignment of player figures from level to locally playing players) */
@@ -3711,26 +3736,7 @@ void InitGame()
   }
 
 #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, active == %d",
-            i + 1,
-            player->present,
-            player->connected,
-            player->active);
-
-      if (local_player == player)
-       printf(" (local player)");
-
-      printf("\n");
-    }
-  }
+  DebugPrintPlayerStatus("Player status after level initialization");
 #endif
 
 #if DEBUG_INIT_PLAYER
@@ -3817,26 +3823,7 @@ void InitGame()
   }
 
 #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, active == %d",
-            i + 1,
-            player->present,
-            player->connected,
-            player->active);
-
-      if (local_player == player)
-       printf(" (local player)");
-
-      printf("\n");
-    }
-  }
+  DebugPrintPlayerStatus("Player status after player assignment (first stage)");
 #endif
 
 #else
@@ -3884,6 +3871,18 @@ void InitGame()
   printf("::: local_player->present == %d\n", local_player->present);
 #endif
 
+  /* set focus to local player for network games, else to all players */
+  game.centered_player_nr = (network_playing ? local_player->index_nr : -1);
+  game.centered_player_nr_next = game.centered_player_nr;
+  game.set_centered_player = FALSE;
+
+  if (network_playing && tape.recording)
+  {
+    /* store client dependent player focus when recording network games */
+    tape.centered_player_nr_next = game.centered_player_nr_next;
+    tape.set_centered_player = TRUE;
+  }
+
   if (tape.playing)
   {
     /* when playing a tape, eliminate all players who do not participate */
@@ -3929,28 +3928,23 @@ void InitGame()
     }
 #endif
   }
-  else if (!options.network && !game.team_mode)                /* && !tape.playing */
+  else if (!network.enabled && !game.team_mode)                /* && !tape.playing */
   {
-    /* when in single player mode, eliminate all but the first active player */
+    /* when in single player mode, eliminate all but the local player */
 
     for (i = 0; i < MAX_PLAYERS; i++)
     {
-      if (stored_player[i].active)
+      struct PlayerInfo *player = &stored_player[i];
+
+      if (player->active && player != local_player)
       {
-       for (j = i + 1; j < MAX_PLAYERS; j++)
-       {
-         if (stored_player[j].active)
-         {
-           struct PlayerInfo *player = &stored_player[j];
-           int jx = player->jx, jy = player->jy;
+       int jx = player->jx, jy = player->jy;
 
-           player->active = FALSE;
-           player->present = FALSE;
+       player->active = FALSE;
+       player->present = FALSE;
 
-           StorePlayer[jx][jy] = 0;
-           Feld[jx][jy] = EL_EMPTY;
-         }
-       }
+       StorePlayer[jx][jy] = 0;
+       Feld[jx][jy] = EL_EMPTY;
       }
     }
   }
@@ -3970,26 +3964,7 @@ void InitGame()
   }
 
 #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, active == %d",
-            i + 1,
-            player->present,
-            player->connected,
-            player->active);
-
-      if (local_player == player)
-       printf(" (local player)");
-
-      printf("\n");
-    }
-  }
+  DebugPrintPlayerStatus("Player status after player assignment (final stage)");
 #endif
 
   if (BorderElement == EL_EMPTY)
@@ -4186,34 +4161,10 @@ void InitGame()
 
     OpenDoor(DOOR_OPEN_ALL);
 
-    PlaySound(SND_GAME_STARTING);
-
-    if (setup.sound_music)
-      PlayLevelMusic();
-
     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, active == %d",
-              i + 1,
-              player->present,
-              player->connected,
-              player->active);
-
-       if (local_player == player)
-         printf(" (local player)");
-
-       printf("\n");
-      }
-    }
+    DebugPrintPlayerStatus("Player status (final)");
 #endif
   }
 
@@ -4236,6 +4187,14 @@ void InitGame()
     InitGameActions_MM();
 
   SaveEngineSnapshotToListInitial();
+
+  if (!game.restart_level)
+  {
+    PlaySound(SND_GAME_STARTING);
+
+    if (setup.sound_music)
+      PlayLevelMusic();
+  }
 }
 
 void UpdateEngineValues(int actual_scroll_x, int actual_scroll_y,
@@ -4692,7 +4651,7 @@ void GameWon()
 void GameEnd()
 {
   int hi_pos;
-  boolean raise_level = FALSE;
+  int last_level_nr = level_nr;
 
   local_player->LevelSolved_GameEnd = TRUE;
 
@@ -4735,35 +4694,39 @@ void GameEnd()
 
   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);
+    level_nr++;                /* advance to next level */
+    TapeErase();       /* start with empty tape */
 
-    DrawHallOfFame(hi_pos);
-
-    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();
   }
+  else
+  {
+    StartGameActions(network.enabled, setup.autorecord, level.random_seed);
+  }
 }
 
-int NewHiScore()
+int NewHiScore(int level_nr)
 {
   int k, l;
   int position = -1;
@@ -9813,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))
-         PlayerWins(&stored_player[i]);
+         RemovePlayerWithCleanup(&stored_player[i]);
+
+      if (AllPlayersGone)
+       PlayerWins(local_player);
 
       break;
     }
@@ -11250,14 +11216,12 @@ void StartGameActions(boolean init_network_game, boolean record_tape,
   if (record_tape)
     TapeStartRecording(new_random_seed);
 
-#if defined(NETWORK_AVALIABLE)
   if (init_network_game)
   {
     SendToServer_StartPlaying();
 
     return;
   }
-#endif
 
   InitGame();
 }
@@ -11293,7 +11257,7 @@ void GameActionsExt()
   }
 
   if (game.restart_level)
-    StartGameActions(options.network, setup.autorecord, level.random_seed);
+    StartGameActions(network.enabled, setup.autorecord, level.random_seed);
 
   /* !!! SAME CODE AS IN "CheckLevelTime()" -- FIX THIS !!! */
   if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
@@ -11380,10 +11344,8 @@ void GameActionsExt()
   {
     /* try to get network player actions in time */
 
-#if defined(NETWORK_AVALIABLE)
     /* last chance to get network player actions without main loop delay */
     HandleNetworking();
-#endif
 
     /* game was quit by network peer */
     if (game_status != GAME_MODE_PLAYING)
@@ -11429,14 +11391,12 @@ void GameActionsExt()
       stored_player[i].effective_action = stored_player[i].action;
   }
 
-#if defined(NETWORK_AVALIABLE)
   if (network_playing)
     SendToServer_MovePlayer(summarized_player_action);
-#endif
 
   // summarize all actions at local players mapped input device position
   // (this allows using different input devices in single player mode)
-  if (!options.network && !game.team_mode)
+  if (!network.enabled && !game.team_mode)
     stored_player[map_player_action[local_player->index_nr]].effective_action =
       summarized_player_action;
 
@@ -12346,7 +12306,7 @@ boolean MovePlayerOneStep(struct PlayerInfo *player,
     }
   }
 
-  if (!options.network && game.centered_player_nr == -1 &&
+  if (!network.enabled && game.centered_player_nr == -1 &&
       !AllPlayersInSight(player, new_jx, new_jy))
     return MP_NO_ACTION;
 
@@ -12533,7 +12493,7 @@ boolean MovePlayer(struct PlayerInfo *player, int dx, int dy)
 
     if (scroll_x != old_scroll_x || scroll_y != old_scroll_y)
     {
-      if (!options.network && game.centered_player_nr == -1 &&
+      if (!network.enabled && game.centered_player_nr == -1 &&
          !AllPlayersInVisibleScreen())
       {
        scroll_x = old_scroll_x;
@@ -12685,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 */
     {
-      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 */
@@ -13446,6 +13406,12 @@ void RemovePlayer(struct PlayerInfo *player)
   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];
@@ -14927,11 +14893,9 @@ void RequestQuitGameExt(boolean skip_request, boolean quick_quit, char *message)
     if (!skip_request)
       CloseDoor(DOOR_CLOSE_1);
 
-#if defined(NETWORK_AVALIABLE)
-    if (options.network)
+    if (network.enabled)
       SendToServer_StopPlaying(NETWORK_STOP_BY_PLAYER);
     else
-#endif
     {
       if (quick_quit)
        FadeSkipNextFadeIn();
@@ -14968,7 +14932,7 @@ void RequestRestartGame(char *message)
 
   if (Request(message, REQ_ASK | REQ_STAY_CLOSED))
   {
-    StartGameActions(options.network, setup.autorecord, level.random_seed);
+    StartGameActions(network.enabled, setup.autorecord, level.random_seed);
   }
   else
   {
@@ -15430,7 +15394,8 @@ void CreateGameButtons()
 
   for (i = 0; i < NUM_GAME_BUTTONS; i++)
   {
-    struct GraphicInfo *gfx = &graphic_info[gamebutton_info[i].graphic];
+    int graphic = gamebutton_info[i].graphic;
+    struct GraphicInfo *gfx = &graphic_info[graphic];
     struct XY *pos = gamebutton_info[i].pos;
     struct GadgetInfo *gi;
     int button_type;
@@ -15484,6 +15449,7 @@ void CreateGameButtons()
     }
 
     gi = CreateGadget(GDI_CUSTOM_ID, id,
+                     GDI_IMAGE_ID, graphic,
                      GDI_INFO_TEXT, gamebutton_info[i].infotext,
                      GDI_X, base_x + GDI_ACTIVE_POS(pos->x),
                      GDI_Y, base_y + GDI_ACTIVE_POS(pos->y),
@@ -15735,14 +15701,12 @@ static void HandleGameButtonsExt(int id, int button)
     case GAME_CTRL_ID_PAUSE:
     case GAME_CTRL_ID_PAUSE2:
     case GAME_CTRL_ID_PANEL_PAUSE:
-      if (options.network && game_status == GAME_MODE_PLAYING)
+      if (network.enabled && game_status == GAME_MODE_PLAYING)
       {
-#if defined(NETWORK_AVALIABLE)
        if (tape.pausing)
          SendToServer_ContinuePlaying();
        else
          SendToServer_PausePlaying();
-#endif
       }
       else
        TapeTogglePause(TAPE_TOGGLE_MANUAL);
@@ -15755,15 +15719,13 @@ static void HandleGameButtonsExt(int id, int button)
     case GAME_CTRL_ID_PANEL_PLAY:
       if (game_status == GAME_MODE_MAIN)
       {
-        StartGameActions(options.network, setup.autorecord, level.random_seed);
+        StartGameActions(network.enabled, setup.autorecord, level.random_seed);
       }
       else if (tape.pausing)
       {
-#if defined(NETWORK_AVALIABLE)
-       if (options.network)
+       if (network.enabled)
          SendToServer_ContinuePlaying();
        else
-#endif
          TapeTogglePause(TAPE_TOGGLE_MANUAL | TAPE_TOGGLE_PLAY_PAUSE);
       }
       break;