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