+ int sx1 = center_x, sy1 = center_y, sx2 = center_x, sy2 = center_y;
+
+ setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
+
+ *max_dx = MAX(ABS(sx1 - center_x), ABS(sx2 - center_x));
+ *max_dy = MAX(ABS(sy1 - center_y), ABS(sy2 - center_y));
+}
+
+static boolean checkIfAllPlayersAreVisible(int center_x, int center_y)
+{
+ int max_dx, max_dy;
+
+ setMaxCenterDistanceForAllPlayers(&max_dx, &max_dy, center_x, center_y);
+
+ return (max_dx <= SCR_FIELDX * TILEX / 2 &&
+ max_dy <= SCR_FIELDY * TILEY / 2);
+}
+
+void RedrawPlayfield_EM(boolean force_redraw)
+{
+ boolean draw_new_player_location = FALSE;
+ boolean quick_relocation = setup.quick_switch;
+ int max_center_distance_player_nr =
+ getMaxCenterDistancePlayerNr(screen_x, screen_y);
+ int stepsize = TILEX / 8;
+ int offset = game.scroll_delay_value * TILEX;
+ int offset_x = offset;
+ int offset_y = offset;
+ int screen_x_old = screen_x;
+ int screen_y_old = screen_y;
+ int x, y, sx, sy;
+ int i;
+
+ if (game.set_centered_player)
+ {
+ boolean all_players_fit_to_screen = checkIfAllPlayersFitToScreen();
+
+ /* switching to "all players" only possible if all players fit to screen */
+ if (game.centered_player_nr_next == -1 && !all_players_fit_to_screen)
+ {
+ game.centered_player_nr_next = game.centered_player_nr;
+ game.set_centered_player = FALSE;
+ }
+
+ /* do not switch focus to non-existing (or non-active) player */
+ if (game.centered_player_nr_next >= 0 &&
+ !ply[game.centered_player_nr_next].alive)
+ {
+ game.centered_player_nr_next = game.centered_player_nr;
+ game.set_centered_player = FALSE;
+ }
+ }
+
+ /* also allow focus switching when screen is scrolled to half tile */
+ if (game.set_centered_player)
+ {
+ game.centered_player_nr = game.centered_player_nr_next;
+
+ draw_new_player_location = TRUE;
+ force_redraw = TRUE;
+
+ game.set_centered_player = FALSE;
+ }
+
+ if (game.centered_player_nr == -1)
+ {
+ if (draw_new_player_location || offset == 0)
+ {
+ setScreenCenteredToAllPlayers(&sx, &sy);
+ }
+ else
+ {
+ sx = PLAYER_SCREEN_X(max_center_distance_player_nr);
+ sy = PLAYER_SCREEN_Y(max_center_distance_player_nr);
+ }
+ }
+ else
+ {
+ sx = PLAYER_SCREEN_X(game.centered_player_nr);
+ sy = PLAYER_SCREEN_Y(game.centered_player_nr);
+ }
+
+ if (draw_new_player_location && quick_relocation)
+ {
+ screen_x = VALID_SCREEN_X(sx);
+ screen_y = VALID_SCREEN_Y(sy);
+ screen_x_old = screen_x;
+ screen_y_old = screen_y;
+ }
+
+ if (draw_new_player_location && !quick_relocation)
+ {
+ unsigned int game_frame_delay_value = getGameFrameDelay_EM(20);
+ int wait_delay_value = game_frame_delay_value;
+ int screen_xx = VALID_SCREEN_X(sx);
+ int screen_yy = VALID_SCREEN_Y(sy);
+
+ while (screen_x != screen_xx || screen_y != screen_yy)
+ {
+ int dx = (screen_xx < screen_x ? +1 : screen_xx > screen_x ? -1 : 0);
+ int dy = (screen_yy < screen_y ? +1 : screen_yy > screen_y ? -1 : 0);
+ int dxx = 0, dyy = 0;
+
+ if (dx == 0 && dy == 0) /* no scrolling needed at all */
+ break;
+
+ if (ABS(screen_xx - screen_x) >= TILEX)
+ {
+ screen_x -= dx * TILEX;
+ dxx = dx * TILEX / 2;
+ }
+ else
+ {
+ screen_x = screen_xx;
+ dxx = 0;
+ }
+
+ if (ABS(screen_yy - screen_y) >= TILEY)
+ {
+ screen_y -= dy * TILEY;
+ dyy = dy * TILEY / 2;
+ }
+ else
+ {
+ screen_y = screen_yy;
+ dyy = 0;
+ }
+
+ /* scroll in two steps of half tile size to make things smoother */
+ screen_x += dxx;
+ screen_y += dyy;
+
+ animscreen();
+
+ for (i = 0; i < MAX_PLAYERS; i++)
+ blitplayer(&ply[i]);
+
+ BlitScreenToBitmap_EM(backbuffer);
+ BackToFront_EM();
+
+ Delay(wait_delay_value);
+
+ /* scroll second step to align at full tile size */
+ screen_x -= dxx;
+ screen_y -= dyy;
+
+ animscreen();
+
+ for (i = 0; i < MAX_PLAYERS; i++)
+ blitplayer(&ply[i]);
+
+ BlitScreenToBitmap_EM(backbuffer);
+ BackToFront_EM();
+
+ Delay(wait_delay_value);
+ }
+
+ screen_x_old = screen_x;
+ screen_y_old = screen_y;
+ }
+
+ if (force_redraw)
+ {
+ for (y = 0; y < MAX_BUF_YSIZE; y++)
+ {
+ for (x = 0; x < MAX_BUF_XSIZE; x++)
+ {
+ screentiles[y][x] = -1;
+ crumbled_state[y][x] = 0;
+ }
+ }
+ }
+
+ /* calculate new screen scrolling position, with regard to scroll delay */
+ screen_x = VALID_SCREEN_X(sx + offset_x < screen_x ? sx + offset_x :
+ sx - offset_x > screen_x ? sx - offset_x :
+ screen_x);
+ screen_y = VALID_SCREEN_Y(sy + offset_y < screen_y ? sy + offset_y :
+ sy - offset_y > screen_y ? sy - offset_y :
+ screen_y);
+
+ /* prevent scrolling further than double player step size when scrolling */
+ if (ABS(screen_x - screen_x_old) > 2 * stepsize)
+ {
+ int dx = SIGN(screen_x - screen_x_old);
+
+ screen_x = screen_x_old + dx * 2 * stepsize;
+ }
+ if (ABS(screen_y - screen_y_old) > 2 * stepsize)
+ {
+ int dy = SIGN(screen_y - screen_y_old);
+
+ screen_y = screen_y_old + dy * 2 * stepsize;
+ }
+
+ /* prevent scrolling away from the other players when focus on all players */
+ if (game.centered_player_nr == -1)
+ {
+ /* check if all players are still visible with new scrolling position */
+ if (checkIfAllPlayersAreVisible(screen_x_old, screen_y_old) &&
+ !checkIfAllPlayersAreVisible(screen_x, screen_y))
+ {
+ /* reset horizontal scroll position to last value, if needed */
+ if (!checkIfAllPlayersAreVisible(screen_x, screen_y_old))
+ screen_x = screen_x_old;
+
+ /* reset vertical scroll position to last value, if needed */
+ if (!checkIfAllPlayersAreVisible(screen_x_old, screen_y))
+ screen_y = screen_y_old;
+ }
+ }
+
+ /* prevent scrolling (for screen correcting) if no player is moving */
+ if (!game_em.any_player_moving)
+ {
+ screen_x = screen_x_old;
+ screen_y = screen_y_old;
+ }
+ else
+ {
+ /* prevent scrolling against the players move direction */
+ int player_nr = (game.centered_player_nr == -1 ?
+ max_center_distance_player_nr : game.centered_player_nr);
+ int player_move_dir = game_em.last_player_direction[player_nr];
+ int dx = SIGN(screen_x - screen_x_old);
+ int dy = SIGN(screen_y - screen_y_old);
+
+ if ((dx < 0 && player_move_dir != MV_LEFT) ||
+ (dx > 0 && player_move_dir != MV_RIGHT))
+ screen_x = screen_x_old;
+
+ if ((dy < 0 && player_move_dir != MV_UP) ||
+ (dy > 0 && player_move_dir != MV_DOWN))
+ screen_y = screen_y_old;
+ }
+
+ // skip redrawing playfield in warp mode or when testing tapes with "autotest"
+ if (DrawingDeactivatedField())
+ return;
+
+ animscreen();
+
+ for (i = 0; i < MAX_PLAYERS; i++)
+ blitplayer(&ply[i]);