added full compatibility for wrap-around levels in EM engine
[rocksndiamonds.git] / src / game_em / graphics.c
1 /* 2000-08-13T14:36:17Z
2  *
3  * graphics manipulation crap
4  */
5
6 #include "main_em.h"
7
8 #define MIN_SCREEN_XPOS_RAW     0
9 #define MIN_SCREEN_YPOS_RAW     0
10 #define MAX_SCREEN_XPOS_RAW     MAX(0, lev.width  - SCR_FIELDX)
11 #define MAX_SCREEN_YPOS_RAW     MAX(0, lev.height - SCR_FIELDY)
12
13 #define MIN_SCREEN_XPOS         (MIN_SCREEN_XPOS_RAW + CAVE_BUFFER_XOFFSET)
14 #define MIN_SCREEN_YPOS         (MIN_SCREEN_YPOS_RAW + CAVE_BUFFER_YOFFSET)
15 #define MAX_SCREEN_XPOS         (MAX_SCREEN_XPOS_RAW + CAVE_BUFFER_XOFFSET)
16 #define MAX_SCREEN_YPOS         (MAX_SCREEN_YPOS_RAW + CAVE_BUFFER_YOFFSET)
17
18 #define MIN_SCREEN_X            (MIN_SCREEN_XPOS * TILEX)
19 #define MIN_SCREEN_Y            (MIN_SCREEN_YPOS * TILEY)
20 #define MAX_SCREEN_X            (MAX_SCREEN_XPOS * TILEX)
21 #define MAX_SCREEN_Y            (MAX_SCREEN_YPOS * TILEY)
22
23 #define VALID_SCREEN_X(x)       ((x) < MIN_SCREEN_X ? MIN_SCREEN_X :    \
24                                  (x) > MAX_SCREEN_X ? MAX_SCREEN_X : (x))
25 #define VALID_SCREEN_Y(y)       ((y) < MIN_SCREEN_Y ? MIN_SCREEN_Y :    \
26                                  (y) > MAX_SCREEN_Y ? MAX_SCREEN_Y : (y))
27
28 #define PLAYER_POS_X(nr)        (((7 - frame) * ply[nr].prev_x +        \
29                                   (1 + frame) * ply[nr].x) * TILEX / 8)
30 #define PLAYER_POS_Y(nr)        (((7 - frame) * ply[nr].prev_y +        \
31                                   (1 + frame) * ply[nr].y) * TILEY / 8)
32
33 #define PLAYER_SCREEN_X(nr)     (PLAYER_POS_X(nr) -                     \
34                                  (SCR_FIELDX - 1) * TILEX / 2)
35 #define PLAYER_SCREEN_Y(nr)     (PLAYER_POS_Y(nr) -                     \
36                                  (SCR_FIELDY - 1) * TILEY / 2)
37
38 #define USE_EXTENDED_GRAPHICS_ENGINE            1
39
40
41 int frame;                              /* current screen frame */
42 int screen_x, screen_y;                 /* current scroll position */
43
44 /* tiles currently on screen */
45 static int screen_tiles[MAX_PLAYFIELD_WIDTH + 2][MAX_PLAYFIELD_HEIGHT + 2];
46 static int crumbled_state[MAX_PLAYFIELD_WIDTH + 2][MAX_PLAYFIELD_HEIGHT + 2];
47
48 /* graphic info for game objects/frames and players/actions/frames */
49 struct GraphicInfo_EM graphic_info_em_object[GAME_TILE_MAX][8];
50 struct GraphicInfo_EM graphic_info_em_player[MAX_PLAYERS][PLY_MAX][8];
51
52 static void setScreenCenteredToAllPlayers(int *, int *);
53
54 int getFieldbufferOffsetX_EM(void)
55 {
56   return screen_x % TILEX;
57 }
58
59 int getFieldbufferOffsetY_EM(void)
60 {
61   return screen_y % TILEY;
62 }
63
64 void BlitScreenToBitmap_EM(Bitmap *target_bitmap)
65 {
66   /* blit all (up to four) parts of the scroll buffer to the target bitmap */
67
68   int x = screen_x % (MAX_BUF_XSIZE * TILEX);
69   int y = screen_y % (MAX_BUF_YSIZE * TILEY);
70   int xsize = SXSIZE;
71   int ysize = SYSIZE;
72   int full_xsize = lev.width  * TILEX;
73   int full_ysize = lev.height * TILEY;
74   int sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
75   int sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
76   int sxsize = (full_xsize < xsize ? full_xsize : xsize);
77   int sysize = (full_ysize < ysize ? full_ysize : ysize);
78   int xxsize = MAX_BUF_XSIZE * TILEX - x;
79   int yysize = MAX_BUF_YSIZE * TILEY - y;
80   int xoffset = 2 * CAVE_BUFFER_XOFFSET * TILEX;
81   int yoffset = 2 * CAVE_BUFFER_YOFFSET * TILEY;
82
83   if (x < xoffset && y < yoffset)
84   {
85     BlitBitmap(screenBitmap, target_bitmap, x, y, sxsize, sysize,
86                sx, sy);
87   }
88   else if (x < xoffset && y >= yoffset)
89   {
90     BlitBitmap(screenBitmap, target_bitmap, x, y, sxsize, yysize,
91                sx, sy);
92     BlitBitmap(screenBitmap, target_bitmap, x, 0, sxsize, y - yoffset,
93                sx, sy + yysize);
94   }
95   else if (x >= xoffset && y < yoffset)
96   {
97     BlitBitmap(screenBitmap, target_bitmap, x, y, xxsize, sysize,
98                sx, sy);
99     BlitBitmap(screenBitmap, target_bitmap, 0, y, x - xoffset, sysize,
100                sx + xxsize, sy);
101   }
102   else
103   {
104     BlitBitmap(screenBitmap, target_bitmap, x, y, xxsize, yysize,
105                sx, sy);
106     BlitBitmap(screenBitmap, target_bitmap, 0, y, x - xoffset, yysize,
107                sx + xxsize, sy);
108     BlitBitmap(screenBitmap, target_bitmap, x, 0, xxsize, y - yoffset,
109                sx, sy + yysize);
110     BlitBitmap(screenBitmap, target_bitmap, 0, 0, x - xoffset, y - yoffset,
111                sx + xxsize, sy + yysize);
112   }
113 }
114
115 static void BackToFront_EM(void)
116 {
117   BlitBitmap(backbuffer, window, SX, SY, SXSIZE, SYSIZE, SX, SY);
118 }
119
120 static struct GraphicInfo_EM *getObjectGraphic(int x, int y)
121 {
122   int tile = lev.draw[x][y];
123   struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
124
125   if (!game.use_native_emc_graphics_engine)
126     getGraphicSourceObjectExt_EM(g, tile, frame, x - lev.left, y - lev.top);
127
128   return g;
129 }
130
131 static struct GraphicInfo_EM *getPlayerGraphic(int player_nr, int anim)
132 {
133   struct GraphicInfo_EM *g = &graphic_info_em_player[player_nr][anim][frame];
134
135   if (!game.use_native_emc_graphics_engine)
136     getGraphicSourcePlayerExt_EM(g, player_nr, anim, frame);
137
138   return g;
139 }
140
141 static void DrawLevelField_EM(int x, int y, int sx, int sy,
142                               boolean draw_masked)
143 {
144   struct GraphicInfo_EM *g = getObjectGraphic(x, y);
145   int src_x = g->src_x + g->src_offset_x * TILESIZE_VAR / TILESIZE;
146   int src_y = g->src_y + g->src_offset_y * TILESIZE_VAR / TILESIZE;
147   int dst_x = sx * TILEX + g->dst_offset_x * TILESIZE_VAR / TILESIZE;
148   int dst_y = sy * TILEY + g->dst_offset_y * TILESIZE_VAR / TILESIZE;
149   int width = g->width * TILESIZE_VAR / TILESIZE;
150   int height = g->height * TILESIZE_VAR / TILESIZE;
151   int left = screen_x / TILEX;
152   int top  = screen_y / TILEY;
153
154   /* do not draw fields that are outside the visible screen area */
155   if (x < left || x >= left + MAX_BUF_XSIZE ||
156       y < top  || y >= top  + MAX_BUF_YSIZE)
157     return;
158
159   if (draw_masked)
160   {
161     if (width > 0 && height > 0)
162       BlitBitmapMasked(g->bitmap, screenBitmap,
163                        src_x, src_y, width, height, dst_x, dst_y);
164   }
165   else
166   {
167     if ((width != TILEX || height != TILEY) && !g->preserve_background)
168       ClearRectangle(screenBitmap, sx * TILEX, sy * TILEY, TILEX, TILEY);
169
170     if (width > 0 && height > 0)
171       BlitBitmap(g->bitmap, screenBitmap,
172                  src_x, src_y, width, height, dst_x, dst_y);
173   }
174 }
175
176 static void DrawLevelFieldCrumbled_EM(int x, int y, int sx, int sy,
177                                       int crm, boolean draw_masked)
178 {
179   struct GraphicInfo_EM *g;
180   int crumbled_border_size;
181   int left = screen_x / TILEX;
182   int top  = screen_y / TILEY;
183   int i;
184
185   /* do not draw fields that are outside the visible screen area */
186   if (x < left || x >= left + MAX_BUF_XSIZE ||
187       y < top  || y >= top  + MAX_BUF_YSIZE)
188     return;
189
190   if (crm == 0)         /* no crumbled edges for this tile */
191     return;
192
193   g = getObjectGraphic(x, y);
194
195   crumbled_border_size =
196     g->crumbled_border_size * TILESIZE_VAR / g->crumbled_tile_size;
197
198   for (i = 0; i < 4; i++)
199   {
200     if (crm & (1 << i))
201     {
202       int width, height, cx, cy;
203
204       if (i == 1 || i == 2)
205       {
206         width = crumbled_border_size;
207         height = TILEY;
208         cx = (i == 2 ? TILEX - crumbled_border_size : 0);
209         cy = 0;
210       }
211       else
212       {
213         width = TILEX;
214         height = crumbled_border_size;
215         cx = 0;
216         cy = (i == 3 ? TILEY - crumbled_border_size : 0);
217       }
218
219       if (width > 0 && height > 0)
220       {
221         int src_x = g->crumbled_src_x + cx;
222         int src_y = g->crumbled_src_y + cy;
223         int dst_x = sx * TILEX + cx;
224         int dst_y = sy * TILEY + cy;
225
226         if (draw_masked)
227           BlitBitmapMasked(g->crumbled_bitmap, screenBitmap,
228                            src_x, src_y, width, height, dst_x, dst_y);
229         else
230           BlitBitmap(g->crumbled_bitmap, screenBitmap,
231                      src_x, src_y, width, height, dst_x, dst_y);
232       }
233     }
234   }
235 }
236
237 static void DrawLevelPlayer_EM(int x1, int y1, int player_nr, int anim,
238                                boolean draw_masked)
239 {
240   struct GraphicInfo_EM *g = getPlayerGraphic(player_nr, anim);
241   int src_x = g->src_x, src_y = g->src_y;
242   int dst_x, dst_y;
243
244   /* do not draw fields that are outside the visible screen area */
245   if (x1 < screen_x - TILEX || x1 >= screen_x + MAX_BUF_XSIZE * TILEX ||
246       y1 < screen_y - TILEY || y1 >= screen_y + MAX_BUF_YSIZE * TILEY)
247     return;
248
249   x1 %= MAX_BUF_XSIZE * TILEX;
250   y1 %= MAX_BUF_YSIZE * TILEY;
251
252   if (draw_masked)
253   {
254     /* draw the player to current location */
255     dst_x = x1;
256     dst_y = y1;
257     BlitBitmapMasked(g->bitmap, screenBitmap,
258                      src_x, src_y, TILEX, TILEY, dst_x, dst_y);
259
260     /* draw the player to opposite wrap-around column */
261     dst_x = x1 - MAX_BUF_XSIZE * TILEX;
262     dst_y = y1;
263     BlitBitmapMasked(g->bitmap, screenBitmap,
264                      g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
265
266     /* draw the player to opposite wrap-around row */
267     dst_x = x1;
268     dst_y = y1 - MAX_BUF_YSIZE * TILEY;
269     BlitBitmapMasked(g->bitmap, screenBitmap,
270                      g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
271   }
272   else
273   {
274     /* draw the player to current location */
275     dst_x = x1;
276     dst_y = y1;
277     BlitBitmap(g->bitmap, screenBitmap,
278                g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
279
280     /* draw the player to opposite wrap-around column */
281     dst_x = x1 - MAX_BUF_XSIZE * TILEX;
282     dst_y = y1;
283     BlitBitmap(g->bitmap, screenBitmap,
284                g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
285
286     /* draw the player to opposite wrap-around row */
287     dst_x = x1;
288     dst_y = y1 - MAX_BUF_YSIZE * TILEY;
289     BlitBitmap(g->bitmap, screenBitmap,
290                g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
291   }
292 }
293
294 /* draw differences between game tiles and screen tiles
295  *
296  * implicitly handles scrolling and restoring background under the sprites
297  */
298
299 static void animscreen(void)
300 {
301   int x, y, i;
302   int left = screen_x / TILEX;
303   int top  = screen_y / TILEY;
304   static int xy[4][2] =
305   {
306     { 0, -1 },
307     { -1, 0 },
308     { +1, 0 },
309     { 0, +1 }
310   };
311
312   if (!game.use_native_emc_graphics_engine)
313     for (y = lev.top; y < lev.bottom; y++)
314       for (x = lev.left; x < lev.right; x++)
315         SetGfxAnimation_EM(&graphic_info_em_object[lev.draw[x][y]][frame],
316                            lev.draw[x][y], frame,
317                            x - lev.left, y - lev.top);
318
319   for (y = top; y < top + MAX_BUF_YSIZE; y++)
320   {
321     for (x = left; x < left + MAX_BUF_XSIZE; x++)
322     {
323       int sx = x % MAX_BUF_XSIZE;
324       int sy = y % MAX_BUF_YSIZE;    
325       int tile = lev.draw[x][y];
326       struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
327       int obj = g->unique_identifier;
328       int crm = 0;
329       boolean redraw_screen_tile = FALSE;
330
331       /* re-calculate crumbled state of this tile */
332       if (g->has_crumbled_graphics)
333       {
334         for (i = 0; i < 4; i++)
335         {
336           int xx = x + xy[i][0];
337           int yy = y + xy[i][1];
338           int tile_next;
339
340           if (xx < 0 || xx >= CAVE_BUFFER_WIDTH ||
341               yy < 0 || yy >= CAVE_BUFFER_HEIGHT)
342             continue;
343
344           tile_next = lev.draw[xx][yy];
345
346           if (!graphic_info_em_object[tile_next][frame].has_crumbled_graphics)
347             crm |= (1 << i);
348         }
349       }
350
351       redraw_screen_tile = (screen_tiles[sx][sy]   != obj ||
352                             crumbled_state[sx][sy] != crm);
353
354       /* only redraw screen tiles if they (or their crumbled state) changed */
355       if (redraw_screen_tile)
356       {
357         DrawLevelField_EM(x, y, sx, sy, FALSE);
358         DrawLevelFieldCrumbled_EM(x, y, sx, sy, crm, FALSE);
359
360         screen_tiles[sx][sy] = obj;
361         crumbled_state[sx][sy] = crm;
362       }
363     }
364   }
365 }
366
367
368 /* blit players to the screen
369  *
370  * handles transparency and movement
371  */
372
373 static void blitplayer_ext(int nr)
374 {
375   int x1, y1, x2, y2;
376
377   if (!ply[nr].alive)
378     return;
379
380   /* x1/y1 are left/top and x2/y2 are right/down part of the player movement */
381   x1 = PLAYER_POS_X(nr);
382   y1 = PLAYER_POS_Y(nr);
383   x2 = x1 + TILEX - 1;
384   y2 = y1 + TILEY - 1;
385
386   if ((int)(x2 - screen_x) < ((MAX_BUF_XSIZE - 1) * TILEX - 1) &&
387       (int)(y2 - screen_y) < ((MAX_BUF_YSIZE - 1) * TILEY - 1))
388   {
389     /* some casts to "int" are needed because of negative calculation values */
390     int dx = (int)ply[nr].x - (int)ply[nr].prev_x;
391     int dy = (int)ply[nr].y - (int)ply[nr].prev_y;
392     int old_x = (int)ply[nr].prev_x + (int)frame * dx / 8;
393     int old_y = (int)ply[nr].prev_y + (int)frame * dy / 8;
394     int new_x = old_x + SIGN(dx);
395     int new_y = old_y + SIGN(dy);
396     int old_sx = old_x % MAX_BUF_XSIZE;
397     int old_sy = old_y % MAX_BUF_YSIZE;
398     int new_sx = new_x % MAX_BUF_XSIZE;
399     int new_sy = new_y % MAX_BUF_YSIZE;
400     int new_crm = crumbled_state[new_sx][new_sy];
401
402     /* only diggable elements can be crumbled in the classic EM engine */
403     boolean player_is_digging = (new_crm != 0);
404
405     if (player_is_digging)
406     {
407       /* draw the field the player is moving to (under the player) */
408       DrawLevelField_EM(new_x, new_y, new_sx, new_sy, FALSE);
409       DrawLevelFieldCrumbled_EM(new_x, new_y, new_sx, new_sy, new_crm, FALSE);
410
411       /* draw the player (masked) over the element he is just digging away */
412       DrawLevelPlayer_EM(x1, y1, ply[nr].num, ply[nr].anim, TRUE);
413
414       /* draw the field the player is moving from (masked over the player) */
415       DrawLevelField_EM(old_x, old_y, old_sx, old_sy, TRUE);
416     }
417     else
418     {
419       /* draw the player under the element which is on the same field */
420       DrawLevelPlayer_EM(x1, y1, ply[nr].num, ply[nr].anim, FALSE);
421
422       /* draw the field the player is moving from (masked over the player) */
423       DrawLevelField_EM(old_x, old_y, old_sx, old_sy, TRUE);
424
425       /* draw the field the player is moving to (masked over the player) */
426       DrawLevelField_EM(new_x, new_y, new_sx, new_sy, TRUE);
427     }
428
429     /* redraw screen tiles in the next frame (player may have left the tiles) */
430     screen_tiles[old_sx][old_sy] = -1;
431     screen_tiles[new_sx][new_sy] = -1;
432   }
433 }
434
435 static void blitplayer(int nr)
436 {
437   blitplayer_ext(nr);
438
439   /* check for wrap-around movement ... */
440   if (ply[nr].x < lev.left ||
441       ply[nr].x > lev.right - 1)
442   {
443     struct PLAYER ply_last = ply[nr];
444     int dx = ply[nr].x - ply[nr].prev_x;
445
446     ply[nr].x = (ply[nr].x < lev.left ? lev.right - 1 : lev.left);
447     ply[nr].prev_x = ply[nr].x - dx;
448
449     if (!lev.infinite_true)
450     {
451       int dy = ply[nr].y - ply[nr].prev_y;
452
453       ply[nr].y += (ply[nr].x == lev.left ? 1 : -1);
454       ply[nr].prev_y = ply[nr].y - dy;
455     }
456
457     /* draw player entering playfield from the opposite side */
458     blitplayer_ext(nr);
459
460     /* ... but keep the old player position until game logic */
461     ply[nr] = ply_last;
462   }
463 }
464
465 void game_initscreen(void)
466 {
467   int x, y, sx, sy;
468
469   frame = 1;
470
471   if (game.centered_player_nr == -1)
472   {
473     setScreenCenteredToAllPlayers(&sx, &sy);
474   }
475   else
476   {
477     sx = PLAYER_SCREEN_X(game.centered_player_nr);
478     sy = PLAYER_SCREEN_Y(game.centered_player_nr);
479   }
480
481   screen_x = VALID_SCREEN_X(sx);
482   screen_y = VALID_SCREEN_Y(sy);
483
484   for (y = 0; y < MAX_BUF_YSIZE; y++)
485   {
486     for (x = 0; x < MAX_BUF_XSIZE; x++)
487     {
488       screen_tiles[x][y] = -1;
489       crumbled_state[x][y] = 0;
490     }
491   }
492 }
493
494 static int getMaxCenterDistancePlayerNr(int center_x, int center_y)
495 {
496   int max_dx = 0, max_dy = 0;
497   int player_nr = game_em.last_moving_player;
498   int i;
499
500   for (i = 0; i < MAX_PLAYERS; i++)
501   {
502     if (ply[i].alive)
503     {
504       int sx = PLAYER_SCREEN_X(i);
505       int sy = PLAYER_SCREEN_Y(i);
506
507       if (game_em.last_player_direction[i] != MV_NONE &&
508           (ABS(sx - center_x) > max_dx ||
509            ABS(sy - center_y) > max_dy))
510       {
511         max_dx = MAX(max_dx, ABS(sx - center_x));
512         max_dy = MAX(max_dy, ABS(sy - center_y));
513
514         player_nr = i;
515       }
516     }
517   }
518
519   return player_nr;
520 }
521
522 static void setMinimalPlayerBoundaries(int *sx1, int *sy1, int *sx2, int *sy2)
523 {
524   boolean num_checked_players = 0;
525   int i;
526
527   for (i = 0; i < MAX_PLAYERS; i++)
528   {
529     if (ply[i].alive)
530     {
531       int sx = PLAYER_SCREEN_X(i);
532       int sy = PLAYER_SCREEN_Y(i);
533
534       if (num_checked_players == 0)
535       {
536         *sx1 = *sx2 = sx;
537         *sy1 = *sy2 = sy;
538       }
539       else
540       {
541         *sx1 = MIN(*sx1, sx);
542         *sy1 = MIN(*sy1, sy);
543         *sx2 = MAX(*sx2, sx);
544         *sy2 = MAX(*sy2, sy);
545       }
546
547       num_checked_players++;
548     }
549   }
550 }
551
552 boolean checkIfAllPlayersFitToScreen(void)
553 {
554   int sx1 = 0, sy1 = 0, sx2 = 0, sy2 = 0;
555
556   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
557
558   return (sx2 - sx1 <= SCR_FIELDX * TILEX &&
559           sy2 - sy1 <= SCR_FIELDY * TILEY);
560 }
561
562 static void setScreenCenteredToAllPlayers(int *sx, int *sy)
563 {
564   int sx1 = screen_x, sy1 = screen_y, sx2 = screen_x, sy2 = screen_y;
565
566   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
567
568   *sx = (sx1 + sx2) / 2;
569   *sy = (sy1 + sy2) / 2;
570 }
571
572 static void setMaxCenterDistanceForAllPlayers(int *max_dx, int *max_dy,
573                                               int center_x, int center_y)
574 {
575   int sx1 = center_x, sy1 = center_y, sx2 = center_x, sy2 = center_y;
576
577   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
578
579   *max_dx = MAX(ABS(sx1 - center_x), ABS(sx2 - center_x));
580   *max_dy = MAX(ABS(sy1 - center_y), ABS(sy2 - center_y));
581 }
582
583 static boolean checkIfAllPlayersAreVisible(int center_x, int center_y)
584 {
585   int max_dx, max_dy;
586
587   setMaxCenterDistanceForAllPlayers(&max_dx, &max_dy, center_x, center_y);
588
589   return (max_dx <= SCR_FIELDX * TILEX / 2 &&
590           max_dy <= SCR_FIELDY * TILEY / 2);
591 }
592
593 void RedrawPlayfield_EM(boolean force_redraw)
594 {
595   boolean draw_new_player_location = FALSE;
596   boolean draw_new_player_location_wrap = FALSE;
597   boolean quick_relocation = setup.quick_switch;
598   int max_center_distance_player_nr =
599     getMaxCenterDistancePlayerNr(screen_x, screen_y);
600   int stepsize = TILEX / 8;
601   int offset_raw = game.scroll_delay_value;
602   int offset_x = MIN(offset_raw, (SCR_FIELDX - 2) / 2) * TILEX;
603   int offset_y = MIN(offset_raw, (SCR_FIELDY - 2) / 2) * TILEY;
604   int screen_x_old = screen_x;
605   int screen_y_old = screen_y;
606   int x, y, sx, sy;
607   int i;
608
609   if (game.set_centered_player)
610   {
611     boolean all_players_fit_to_screen = checkIfAllPlayersFitToScreen();
612
613     /* switching to "all players" only possible if all players fit to screen */
614     if (game.centered_player_nr_next == -1 && !all_players_fit_to_screen)
615     {
616       game.centered_player_nr_next = game.centered_player_nr;
617       game.set_centered_player = FALSE;
618     }
619
620     /* do not switch focus to non-existing (or non-active) player */
621     if (game.centered_player_nr_next >= 0 &&
622         !ply[game.centered_player_nr_next].alive)
623     {
624       game.centered_player_nr_next = game.centered_player_nr;
625       game.set_centered_player = FALSE;
626     }
627   }
628
629   /* also allow focus switching when screen is scrolled to half tile */
630   if (game.set_centered_player)
631   {
632     game.centered_player_nr = game.centered_player_nr_next;
633
634     draw_new_player_location = TRUE;
635     draw_new_player_location_wrap = game.set_centered_player_wrap;
636     force_redraw = TRUE;
637
638     game.set_centered_player = FALSE;
639     game.set_centered_player_wrap = FALSE;
640   }
641
642   if (game.centered_player_nr == -1)
643   {
644     if (draw_new_player_location || offset_raw == 0)
645     {
646       setScreenCenteredToAllPlayers(&sx, &sy);
647     }
648     else
649     {
650       sx = PLAYER_SCREEN_X(max_center_distance_player_nr);
651       sy = PLAYER_SCREEN_Y(max_center_distance_player_nr);
652     }
653   }
654   else
655   {
656     sx = PLAYER_SCREEN_X(game.centered_player_nr);
657     sy = PLAYER_SCREEN_Y(game.centered_player_nr);
658   }
659
660   if (draw_new_player_location && quick_relocation)
661   {
662     screen_x = VALID_SCREEN_X(sx);
663     screen_y = VALID_SCREEN_Y(sy);
664     screen_x_old = screen_x;
665     screen_y_old = screen_y;
666   }
667
668   if (draw_new_player_location && !quick_relocation)
669   {
670     unsigned int frame_delay_value_old = GetVideoFrameDelay();
671     int wait_delay_value = frame_delay_value_old;
672     int screen_xx = VALID_SCREEN_X(sx);
673     int screen_yy = VALID_SCREEN_Y(sy);
674
675     if (draw_new_player_location_wrap)
676     {
677       if (lev.infinite_true)
678       {
679         // when wrapping around (horizontally), keep vertical player position
680         screen_yy = screen_y;
681       }
682
683       // scrolling for wrapping should be faster than for switching players
684       wait_delay_value /= 4;
685     }
686
687     SetVideoFrameDelay(wait_delay_value);
688
689     while (screen_x != screen_xx || screen_y != screen_yy)
690     {
691       int dx = (screen_xx < screen_x ? +1 : screen_xx > screen_x ? -1 : 0);
692       int dy = (screen_yy < screen_y ? +1 : screen_yy > screen_y ? -1 : 0);
693       int dxx = 0, dyy = 0;
694
695       if (dx == 0 && dy == 0)           /* no scrolling needed at all */
696         break;
697
698       if (ABS(screen_xx - screen_x) >= TILEX)
699       {
700         screen_x -= dx * TILEX;
701         dxx = dx * TILEX / 2;
702       }
703       else
704       {
705         screen_x = screen_xx;
706         dxx = 0;
707       }
708
709       if (ABS(screen_yy - screen_y) >= TILEY)
710       {
711         screen_y -= dy * TILEY;
712         dyy = dy * TILEY / 2;
713       }
714       else
715       {
716         screen_y = screen_yy;
717         dyy = 0;
718       }
719
720       /* scroll in two steps of half tile size to make things smoother */
721       screen_x += dxx;
722       screen_y += dyy;
723
724       animscreen();
725
726       for (i = 0; i < MAX_PLAYERS; i++)
727         blitplayer(i);
728
729       BlitScreenToBitmap_EM(backbuffer);
730       BackToFront_EM();
731
732       /* scroll second step to align at full tile size */
733       screen_x -= dxx;
734       screen_y -= dyy;
735
736       animscreen();
737
738       for (i = 0; i < MAX_PLAYERS; i++)
739         blitplayer(i);
740
741       BlitScreenToBitmap_EM(backbuffer);
742       BackToFront_EM();
743     }
744
745     SetVideoFrameDelay(frame_delay_value_old);
746
747     screen_x_old = screen_x;
748     screen_y_old = screen_y;
749   }
750
751   if (force_redraw)
752   {
753     for (y = 0; y < MAX_BUF_YSIZE; y++)
754     {
755       for (x = 0; x < MAX_BUF_XSIZE; x++)
756       {
757         screen_tiles[x][y] = -1;
758         crumbled_state[x][y] = 0;
759       }
760     }
761   }
762
763   /* calculate new screen scrolling position, with regard to scroll delay */
764   screen_x = VALID_SCREEN_X(sx + offset_x < screen_x ? sx + offset_x :
765                             sx - offset_x > screen_x ? sx - offset_x :
766                             screen_x);
767   screen_y = VALID_SCREEN_Y(sy + offset_y < screen_y ? sy + offset_y :
768                             sy - offset_y > screen_y ? sy - offset_y :
769                             screen_y);
770
771   /* prevent scrolling further than double player step size when scrolling */
772   if (ABS(screen_x - screen_x_old) > 2 * stepsize)
773   {
774     int dx = SIGN(screen_x - screen_x_old);
775
776     screen_x = screen_x_old + dx * 2 * stepsize;
777   }
778   if (ABS(screen_y - screen_y_old) > 2 * stepsize)
779   {
780     int dy = SIGN(screen_y - screen_y_old);
781
782     screen_y = screen_y_old + dy * 2 * stepsize;
783   }
784
785   /* prevent scrolling away from the other players when focus on all players */
786   if (game.centered_player_nr == -1)
787   {
788     /* check if all players are still visible with new scrolling position */
789     if (checkIfAllPlayersAreVisible(screen_x_old, screen_y_old) &&
790         !checkIfAllPlayersAreVisible(screen_x, screen_y))
791     {
792       /* reset horizontal scroll position to last value, if needed */
793       if (!checkIfAllPlayersAreVisible(screen_x, screen_y_old))
794         screen_x = screen_x_old;
795
796       /* reset vertical scroll position to last value, if needed */
797       if (!checkIfAllPlayersAreVisible(screen_x_old, screen_y))
798         screen_y = screen_y_old;
799     }
800   }
801
802   /* prevent scrolling (for screen correcting) if no player is moving */
803   if (!game_em.any_player_moving)
804   {
805     screen_x = screen_x_old;
806     screen_y = screen_y_old;
807   }
808   else
809   {
810     /* prevent scrolling against the players move direction */
811     int player_nr = (game.centered_player_nr == -1 ?
812                      max_center_distance_player_nr : game.centered_player_nr);
813     int player_move_dir = game_em.last_player_direction[player_nr];
814     int dx = SIGN(screen_x - screen_x_old);
815     int dy = SIGN(screen_y - screen_y_old);
816
817     if ((dx < 0 && player_move_dir != MV_LEFT) ||
818         (dx > 0 && player_move_dir != MV_RIGHT))
819       screen_x = screen_x_old;
820
821     if ((dy < 0 && player_move_dir != MV_UP) ||
822         (dy > 0 && player_move_dir != MV_DOWN))
823       screen_y = screen_y_old;
824   }
825
826   // skip redrawing playfield in warp mode or when testing tapes with "autotest"
827   if (DrawingDeactivatedField())
828     return;
829
830   animscreen();
831
832   for (i = 0; i < MAX_PLAYERS; i++)
833     blitplayer(i);
834 }