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