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