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