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