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