rnd-20070907-2-src
[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 int frame;                      /* current screen frame */
33 int screen_x;                   /* current scroll position */
34 int screen_y;
35
36 /* tiles currently on screen */
37 static int screentiles[MAX_BUF_YSIZE][MAX_BUF_XSIZE];
38 static int crumbled_state[MAX_BUF_YSIZE][MAX_BUF_XSIZE];
39
40 static boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
41
42 #if 0
43 #if 1
44 int centered_player_nr;
45 #else
46 static int centered_player_nr;
47 #endif
48 #endif
49
50 /* copy the entire screen to the window at the scroll position */
51
52 void BlitScreenToBitmap_EM(Bitmap *target_bitmap)
53 {
54   int x = screen_x % (MAX_BUF_XSIZE * TILEX);
55   int y = screen_y % (MAX_BUF_YSIZE * TILEY);
56
57   if (x < 2 * TILEX && y < 2 * TILEY)
58   {
59     BlitBitmap(screenBitmap, target_bitmap, x, y,
60                SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
61   }
62   else if (x < 2 * TILEX && y >= 2 * TILEY)
63   {
64     BlitBitmap(screenBitmap, target_bitmap, x, y,
65                SCR_FIELDX * TILEX, MAX_BUF_YSIZE * TILEY - y,
66                SX, SY);
67     BlitBitmap(screenBitmap, target_bitmap, x, 0,
68                SCR_FIELDX * TILEX, y - 2 * TILEY,
69                SX, SY + MAX_BUF_YSIZE * TILEY - y);
70   }
71   else if (x >= 2 * TILEX && y < 2 * TILEY)
72   {
73     BlitBitmap(screenBitmap, target_bitmap, x, y,
74                MAX_BUF_XSIZE * TILEX - x, SCR_FIELDY * TILEY,
75                SX, SY);
76     BlitBitmap(screenBitmap, target_bitmap, 0, y,
77                x - 2 * TILEX, SCR_FIELDY * TILEY,
78                SX + MAX_BUF_XSIZE * TILEX - x, SY);
79   }
80   else
81   {
82     BlitBitmap(screenBitmap, target_bitmap, x, y,
83                MAX_BUF_XSIZE * TILEX - x, MAX_BUF_YSIZE * TILEY - y,
84                SX, SY);
85     BlitBitmap(screenBitmap, target_bitmap, 0, y,
86                x - 2 * TILEX, MAX_BUF_YSIZE * TILEY - y,
87                SX + MAX_BUF_XSIZE * TILEX - x, SY);
88     BlitBitmap(screenBitmap, target_bitmap, x, 0,
89                MAX_BUF_XSIZE * TILEX - x, y - 2 * TILEY,
90                SX, SY + MAX_BUF_YSIZE * TILEY - y);
91     BlitBitmap(screenBitmap, target_bitmap, 0, 0,
92                x - 2 * TILEX, y - 2 * TILEY,
93                SX + MAX_BUF_XSIZE * TILEX - x, SY + MAX_BUF_YSIZE * TILEY - y);
94   }
95 }
96
97 void BackToFront_EM(void)
98 {
99   static boolean scrolling_last = FALSE;
100   int left = screen_x / TILEX;
101   int top  = screen_y / TILEY;
102   boolean scrolling = (screen_x % TILEX != 0 || screen_y % TILEY != 0);
103   int x, y;
104
105   SyncDisplay();
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     for (x = 0; x < SCR_FIELDX; x++)
118     {
119       for (y = 0; y < SCR_FIELDY; y++)
120       {
121         int xx = (left + x) % MAX_BUF_XSIZE;
122         int yy = (top  + y) % MAX_BUF_YSIZE;
123
124         if (redraw[xx][yy])
125           BlitBitmap(screenBitmap, window,
126                      xx * TILEX, yy * TILEY, TILEX, TILEY,
127                      SX + x * TILEX, SY + y * TILEY);
128       }
129     }
130   }
131
132   FlushDisplay();
133
134   for (x = 0; x < MAX_BUF_XSIZE; x++)
135     for (y = 0; y < MAX_BUF_YSIZE; y++)
136       redraw[x][y] = FALSE;
137   redraw_tiles = 0;
138
139   scrolling_last = scrolling;
140 }
141
142 void blitscreen(void)
143 {
144   BackToFront_EM();
145 }
146
147 static struct GraphicInfo_EM *getObjectGraphic(int x, int y)
148 {
149   int tile = Draw[y][x];
150   struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
151
152   if (!game.use_native_emc_graphics_engine)
153     getGraphicSourceObjectExt_EM(g, tile, 7 - frame, x - 2, y - 2);
154
155   return g;
156 }
157
158 static struct GraphicInfo_EM *getPlayerGraphic(int player_nr, int anim)
159 {
160   struct GraphicInfo_EM *g = &graphic_info_em_player[player_nr][anim][frame];
161
162   if (!game.use_native_emc_graphics_engine)
163     getGraphicSourcePlayerExt_EM(g, player_nr, anim, 7 - frame);
164
165   return g;
166 }
167
168 static void DrawLevelField_EM(int x, int y, int sx, int sy,
169                               boolean draw_masked)
170 {
171   struct GraphicInfo_EM *g = getObjectGraphic(x, y);
172   int src_x = g->src_x + g->src_offset_x;
173   int src_y = g->src_y + g->src_offset_y;
174   int dst_x = sx * TILEX + g->dst_offset_x;
175   int dst_y = sy * TILEY + g->dst_offset_y;
176   int width = g->width;
177   int height = g->height;
178   int left = screen_x / TILEX;
179   int top  = screen_y / TILEY;
180
181   /* do not draw fields that are outside the visible screen area */
182   if (x < left || x >= left + MAX_BUF_XSIZE ||
183       y < top  || y >= top  + MAX_BUF_YSIZE)
184     return;
185
186   if (draw_masked)
187   {
188     if (width > 0 && height > 0)
189     {
190       SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
191                     dst_x - src_x, dst_y - src_y);
192       BlitBitmapMasked(g->bitmap, screenBitmap,
193                        src_x, src_y, width, height, dst_x, dst_y);
194     }
195   }
196   else
197   {
198     if ((width != TILEX || height != TILEY) && !g->preserve_background)
199       ClearRectangle(screenBitmap, sx * TILEX, sy * TILEY, TILEX, TILEY);
200
201     if (width > 0 && height > 0)
202       BlitBitmap(g->bitmap, screenBitmap,
203                  src_x, src_y, width, height, dst_x, dst_y);
204   }
205 }
206
207 static void DrawLevelFieldCrumbled_EM(int x, int y, int sx, int sy,
208                                       int crm, boolean draw_masked)
209 {
210   struct GraphicInfo_EM *g = getObjectGraphic(x, y);
211   int left = screen_x / TILEX;
212   int top  = screen_y / TILEY;
213   int i;
214
215   /* do not draw fields that are outside the visible screen area */
216   if (x < left || x >= left + MAX_BUF_XSIZE ||
217       y < top  || y >= top  + MAX_BUF_YSIZE)
218     return;
219
220   if (crm == 0)         /* no crumbled edges for this tile */
221     return;
222
223 #if 0
224   if (x == 3 && y == 3 && frame == 0)
225     printf("::: %d, %d\n",
226            graphic_info_em_object[207][0].crumbled_src_x,
227            graphic_info_em_object[207][0].crumbled_src_y);
228 #endif
229
230   for (i = 0; i < 4; i++)
231   {
232     if (crm & (1 << i))
233     {
234       int width, height, cx, cy;
235
236       if (i == 1 || i == 2)
237       {
238         width = g->crumbled_border_size;
239         height = TILEY;
240         cx = (i == 2 ? TILEX - g->crumbled_border_size : 0);
241         cy = 0;
242       }
243       else
244       {
245         width = TILEX;
246         height = g->crumbled_border_size;
247         cx = 0;
248         cy = (i == 3 ? TILEY - g->crumbled_border_size : 0);
249       }
250
251       if (width > 0 && height > 0)
252       {
253         int src_x = g->crumbled_src_x + cx;
254         int src_y = g->crumbled_src_y + cy;
255         int dst_x = sx * TILEX + cx;
256         int dst_y = sy * TILEY + cy;
257
258         if (draw_masked)
259         {
260           SetClipOrigin(g->crumbled_bitmap, g->crumbled_bitmap->stored_clip_gc,
261                         dst_x - src_x, dst_y - src_y);
262           BlitBitmapMasked(g->crumbled_bitmap, screenBitmap,
263                            src_x, src_y, width, height, dst_x, dst_y);
264         }
265         else
266           BlitBitmap(g->crumbled_bitmap, screenBitmap,
267                      src_x, src_y, width, height, dst_x, dst_y);
268       }
269     }
270   }
271 }
272
273 static void DrawLevelPlayer_EM(int x1, int y1, int player_nr, int anim,
274                                boolean draw_masked)
275 {
276   struct GraphicInfo_EM *g = getPlayerGraphic(player_nr, anim);
277   int src_x = g->src_x, src_y = g->src_y;
278   int dst_x, dst_y;
279
280   /* do not draw fields that are outside the visible screen area */
281   if (x1 < screen_x - TILEX || x1 >= screen_x + MAX_BUF_XSIZE * TILEX ||
282       y1 < screen_y - TILEY || y1 >= screen_y + MAX_BUF_YSIZE * TILEY)
283     return;
284
285   x1 %= MAX_BUF_XSIZE * TILEX;
286   y1 %= MAX_BUF_YSIZE * TILEY;
287
288   if (draw_masked)
289   {
290     /* draw the player to current location */
291     dst_x = x1;
292     dst_y = y1;
293     SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
294                   dst_x - src_x, dst_y - src_y);
295     BlitBitmapMasked(g->bitmap, screenBitmap,
296                      src_x, src_y, TILEX, TILEY, dst_x, dst_y);
297
298     /* draw the player to opposite wrap-around column */
299     dst_x = x1 - MAX_BUF_XSIZE * TILEX;
300     dst_y = y1;
301     SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
302                   dst_x - src_x, dst_y - src_y);
303     BlitBitmapMasked(g->bitmap, screenBitmap,
304                      g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
305
306     /* draw the player to opposite wrap-around row */
307     dst_x = x1;
308     dst_y = y1 - MAX_BUF_YSIZE * TILEY;
309     SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
310                   dst_x - src_x, dst_y - src_y);
311     BlitBitmapMasked(g->bitmap, screenBitmap,
312                      g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
313   }
314   else
315   {
316     /* draw the player to current location */
317     dst_x = x1;
318     dst_y = y1;
319     BlitBitmap(g->bitmap, screenBitmap,
320                g->src_x, g->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     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 row */
329     dst_x = x1;
330     dst_y = y1 - MAX_BUF_YSIZE * TILEY;
331     BlitBitmap(g->bitmap, screenBitmap,
332                g->src_x, g->src_y, TILEX, TILEY, dst_x, dst_y);
333   }
334 }
335
336 /* draw differences between game tiles and screen tiles
337  *
338  * implicitly handles scrolling and restoring background under the sprites
339  */
340
341 static void animscreen(void)
342 {
343   int x, y, i;
344   int left = screen_x / TILEX;
345   int top  = screen_y / TILEY;
346   static int xy[4][2] =
347   {
348     { 0, -1 },
349     { -1, 0 },
350     { +1, 0 },
351     { 0, +1 }
352   };
353
354   if (!game.use_native_emc_graphics_engine)
355     for (y = 2; y < EM_MAX_CAVE_HEIGHT - 2; y++)
356       for (x = 2; x < EM_MAX_CAVE_WIDTH - 2; x++)
357         SetGfxAnimation_EM(&graphic_info_em_object[Draw[y][x]][frame],
358                            Draw[y][x], 7 - frame, x - 2, y - 2);
359
360   for (y = top; y < top + MAX_BUF_YSIZE; y++)
361   {
362     for (x = left; x < left + MAX_BUF_XSIZE; x++)
363     {
364       int sx = x % MAX_BUF_XSIZE;
365       int sy = y % MAX_BUF_YSIZE;    
366       int tile = Draw[y][x];
367       struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
368       int obj = g->unique_identifier;
369       int crm = 0;
370       boolean redraw_screen_tile = FALSE;
371
372       /* re-calculate crumbled state of this tile */
373       if (g->has_crumbled_graphics)
374       {
375         for (i = 0; i < 4; i++)
376         {
377           int xx = x + xy[i][0];
378           int yy = y + xy[i][1];
379           int tile_next;
380
381           if (xx < 0 || xx >= EM_MAX_CAVE_WIDTH ||
382               yy < 0 || yy >= EM_MAX_CAVE_HEIGHT)
383             continue;
384
385           tile_next = Draw[yy][xx];
386
387           if (!graphic_info_em_object[tile_next][frame].has_crumbled_graphics)
388             crm |= (1 << i);
389         }
390       }
391
392       redraw_screen_tile = (screentiles[sy][sx]    != obj ||
393                             crumbled_state[sy][sx] != crm);
394
395 #if 0
396       /* !!! TEST ONLY -- CHANGE THIS !!! */
397       if (!game.use_native_emc_graphics_engine)
398         redraw_screen_tile = TRUE;
399 #endif
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_XSIZE;
448     int new_sx = new_x % MAX_BUF_XSIZE;
449     int new_sy = new_y % MAX_BUF_XSIZE;
450 #if 0
451     int old_crm = crumbled_state[old_sy][old_sx];
452 #endif
453     int new_crm = crumbled_state[new_sy][new_sx];
454
455     /* only diggable elements can be crumbled in the classic EM engine */
456     boolean player_is_digging = (new_crm != 0);
457
458 #if 0
459     x1 %= MAX_BUF_XSIZE * TILEX;
460     y1 %= MAX_BUF_YSIZE * TILEY;
461     x2 %= MAX_BUF_XSIZE * TILEX;
462     y2 %= MAX_BUF_YSIZE * TILEY;
463 #endif
464
465     if (player_is_digging)
466     {
467 #if 0
468       /* draw the field the player is moving from (under the player) */
469       DrawLevelField_EM(old_x, old_y, old_sx, old_sy, FALSE);
470       DrawLevelFieldCrumbled_EM(old_x, old_y, old_sx, old_sy, old_crm, FALSE);
471 #endif
472
473       /* draw the field the player is moving to (under the player) */
474       DrawLevelField_EM(new_x, new_y, new_sx, new_sy, FALSE);
475       DrawLevelFieldCrumbled_EM(new_x, new_y, new_sx, new_sy, new_crm, FALSE);
476
477       /* draw the player (masked) over the element he is just digging away */
478       DrawLevelPlayer_EM(x1, y1, ply->num, ply->anim, TRUE);
479
480 #if 1
481       /* draw the field the player is moving from (masked over the player) */
482       DrawLevelField_EM(old_x, old_y, old_sx, old_sy, TRUE);
483 #endif
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 x,y;
511   int dynamite_state = ply[0].dynamite;         /* !!! ONLY PLAYER 1 !!! */
512   int all_keys_state = ply[0].keys | ply[1].keys | ply[2].keys | ply[3].keys;
513   int player_nr;
514
515   frame = 6;
516
517 #if 0
518   game.centered_player_nr = getCenteredPlayerNr_EM();
519 #endif
520
521   player_nr = (game.centered_player_nr != -1 ? game.centered_player_nr : 0);
522
523   screen_x = VALID_SCREEN_X(PLAYER_SCREEN_X(player_nr));
524   screen_y = VALID_SCREEN_Y(PLAYER_SCREEN_Y(player_nr));
525
526   for (y = 0; y < MAX_BUF_YSIZE; y++)
527   {
528     for (x = 0; x < MAX_BUF_XSIZE; x++)
529     {
530       screentiles[y][x] = -1;
531       crumbled_state[y][x] = 0;
532     }
533   }
534
535   DrawAllGameValues(lev.required, dynamite_state, lev.score,
536                     lev.time, all_keys_state);
537 }
538
539 #if 0
540 void DrawRelocatePlayer(struct PlayerInfo *player, boolean quick_relocation)
541 {
542   boolean ffwd_delay = (tape.playing && tape.fast_forward);
543   boolean no_delay = (tape.warp_forward);
544   int frame_delay_value = (ffwd_delay ? FfwdFrameDelay : GameFrameDelay);
545   int wait_delay_value = (no_delay ? 0 : frame_delay_value);
546   int jx = player->jx;
547   int jy = player->jy;
548
549   if (quick_relocation)
550   {
551     int offset = game.scroll_delay_value;
552
553     if (!IN_VIS_FIELD(SCREENX(jx), SCREENY(jy)))
554     {
555       scroll_x = (player->jx < SBX_Left  + MIDPOSX ? SBX_Left :
556                   player->jx > SBX_Right + MIDPOSX ? SBX_Right :
557                   player->jx - MIDPOSX);
558
559       scroll_y = (player->jy < SBY_Upper + MIDPOSY ? SBY_Upper :
560                   player->jy > SBY_Lower + MIDPOSY ? SBY_Lower :
561                   player->jy - MIDPOSY);
562     }
563     else
564     {
565       if ((player->MovDir == MV_LEFT  && scroll_x > jx - MIDPOSX + offset) ||
566           (player->MovDir == MV_RIGHT && scroll_x < jx - MIDPOSX - offset))
567         scroll_x = jx - MIDPOSX + (scroll_x < jx-MIDPOSX ? -offset : +offset);
568
569       if ((player->MovDir == MV_UP  && scroll_y > jy - MIDPOSY + offset) ||
570           (player->MovDir == MV_DOWN && scroll_y < jy - MIDPOSY - offset))
571         scroll_y = jy - MIDPOSY + (scroll_y < jy-MIDPOSY ? -offset : +offset);
572
573       /* don't scroll over playfield boundaries */
574       if (scroll_x < SBX_Left || scroll_x > SBX_Right)
575         scroll_x = (scroll_x < SBX_Left ? SBX_Left : SBX_Right);
576
577       /* don't scroll over playfield boundaries */
578       if (scroll_y < SBY_Upper || scroll_y > SBY_Lower)
579         scroll_y = (scroll_y < SBY_Upper ? SBY_Upper : SBY_Lower);
580     }
581
582     RedrawPlayfield(TRUE, 0,0,0,0);
583   }
584   else
585   {
586     int scroll_xx = -999, scroll_yy = -999;
587
588     ScrollScreen(NULL, SCROLL_GO_ON);   /* scroll last frame to full tile */
589
590     while (scroll_xx != scroll_x || scroll_yy != scroll_y)
591     {
592       int dx = 0, dy = 0;
593       int fx = FX, fy = FY;
594
595       scroll_xx = (player->jx < SBX_Left  + MIDPOSX ? SBX_Left :
596                    player->jx > SBX_Right + MIDPOSX ? SBX_Right :
597                    player->jx - MIDPOSX);
598
599       scroll_yy = (player->jy < SBY_Upper + MIDPOSY ? SBY_Upper :
600                    player->jy > SBY_Lower + MIDPOSY ? SBY_Lower :
601                    player->jy - MIDPOSY);
602
603       dx = (scroll_xx < scroll_x ? +1 : scroll_xx > scroll_x ? -1 : 0);
604       dy = (scroll_yy < scroll_y ? +1 : scroll_yy > scroll_y ? -1 : 0);
605
606       if (dx == 0 && dy == 0)           /* no scrolling needed at all */
607         break;
608
609       scroll_x -= dx;
610       scroll_y -= dy;
611
612       fx += dx * TILEX / 2;
613       fy += dy * TILEY / 2;
614
615       ScrollLevel(dx, dy);
616       DrawAllPlayers();
617
618       /* scroll in two steps of half tile size to make things smoother */
619       BlitBitmap(drawto_field, window, fx, fy, SXSIZE, SYSIZE, SX, SY);
620       FlushDisplay();
621       Delay(wait_delay_value);
622
623       /* scroll second step to align at full tile size */
624       BackToFront();
625       Delay(wait_delay_value);
626     }
627
628     DrawPlayer(player);
629     BackToFront();
630     Delay(wait_delay_value);
631   }
632 }
633 #endif
634
635 static int getMaxCenterDistancePlayerNr(int center_x, int center_y)
636 {
637   int max_dx = 0, max_dy = 0;
638   int player_nr = game_em.last_moving_player;
639   int i;
640
641   for (i = 0; i < MAX_PLAYERS; i++)
642   {
643     if (ply[i].alive)
644     {
645       int sx = PLAYER_SCREEN_X(i);
646       int sy = PLAYER_SCREEN_Y(i);
647
648       if (game_em.last_player_direction[i] != MV_NONE &&
649           (ABS(sx - center_x) > max_dx ||
650            ABS(sy - center_y) > max_dy))
651       {
652         max_dx = MAX(max_dx, ABS(sx - center_x));
653         max_dy = MAX(max_dy, ABS(sy - center_y));
654
655         player_nr = i;
656       }
657     }
658   }
659
660   return player_nr;
661 }
662
663 static void setMinimalPlayerBoundaries(int *sx1, int *sy1, int *sx2, int *sy2)
664 {
665   boolean num_checked_players = 0;
666   int i;
667
668   for (i = 0; i < MAX_PLAYERS; i++)
669   {
670     if (ply[i].alive)
671     {
672       int sx = PLAYER_SCREEN_X(i);
673       int sy = PLAYER_SCREEN_Y(i);
674
675       if (num_checked_players == 0)
676       {
677         *sx1 = *sx2 = sx;
678         *sy1 = *sy2 = sy;
679       }
680       else
681       {
682         *sx1 = MIN(*sx1, sx);
683         *sy1 = MIN(*sy1, sy);
684         *sx2 = MAX(*sx2, sx);
685         *sy2 = MAX(*sy2, sy);
686       }
687
688       num_checked_players++;
689     }
690   }
691 }
692
693 boolean checkIfAllPlayersFitToScreen()
694 {
695   int sx1 = 0, sy1 = 0, sx2 = 0, sy2 = 0;
696
697   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
698
699   return (sx2 - sx1 <= SCR_FIELDX * TILEX &&
700           sy2 - sy1 <= SCR_FIELDY * TILEY);
701 }
702
703 static void setScreenCenteredToAllPlayers(int *sx, int *sy)
704 {
705   int sx1 = screen_x, sy1 = screen_y, sx2 = screen_x, sy2 = screen_y;
706
707   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
708
709   *sx = (sx1 + sx2) / 2;
710   *sy = (sy1 + sy2) / 2;
711 }
712
713 static void setMaxCenterDistanceForAllPlayers(int *max_dx, int *max_dy,
714                                               int center_x, int center_y)
715 {
716   int sx1 = center_x, sy1 = center_y, sx2 = center_x, sy2 = center_y;
717
718   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
719
720   *max_dx = MAX(ABS(sx1 - center_x), ABS(sx2 - center_x));
721   *max_dy = MAX(ABS(sy1 - center_y), ABS(sy2 - center_y));
722 }
723
724 static boolean checkIfAllPlayersAreVisible(int center_x, int center_y)
725 {
726   int max_dx, max_dy;
727
728   setMaxCenterDistanceForAllPlayers(&max_dx, &max_dy, center_x, center_y);
729
730   return (max_dx <= SCR_FIELDX * TILEX / 2 &&
731           max_dy <= SCR_FIELDY * TILEY / 2);
732 }
733
734 void RedrawPlayfield_EM(boolean force_redraw)
735 {
736 #if 0
737   boolean all_players_visible = checkIfAllPlayersAreVisible();
738 #endif
739   boolean draw_new_player_location = FALSE;
740   boolean quick_relocation = setup.quick_switch;
741 #if 0
742   boolean scrolling = (screen_x % TILEX != 0 || screen_y % TILEY != 0);
743 #endif
744 #if 0
745   boolean game.set_centered_player = getSetCenteredPlayer_EM();
746   int game.centered_player_nr_next = getCenteredPlayerNr_EM();
747 #endif
748 #if 1
749   int max_center_distance_player_nr =
750     getMaxCenterDistancePlayerNr(screen_x, screen_y);
751 #else
752   int player_nr = game_em.last_moving_player;
753 #endif
754   int stepsize = TILEX / 8;
755   int offset = game.scroll_delay_value * TILEX;
756   int offset_x = offset;
757   int offset_y = offset;
758   int screen_x_old = screen_x;
759   int screen_y_old = screen_y;
760   int x, y, sx, sy;
761   int i;
762
763   if (game.set_centered_player)
764   {
765     boolean all_players_fit_to_screen = checkIfAllPlayersFitToScreen();
766
767     /* switching to "all players" only possible if all players fit to screen */
768     if (game.centered_player_nr_next == -1 && !all_players_fit_to_screen)
769     {
770       game.centered_player_nr_next = game.centered_player_nr;
771       game.set_centered_player = FALSE;
772     }
773
774     /* do not switch focus to non-existing (or non-active) player */
775     if (game.centered_player_nr_next >= 0 &&
776         !ply[game.centered_player_nr_next].alive)
777     {
778       game.centered_player_nr_next = game.centered_player_nr;
779       game.set_centered_player = FALSE;
780     }
781   }
782
783 #if 1
784   /* also allow focus switching when screen is scrolled to half tile */
785 #else
786   if (!scrolling)       /* screen currently aligned at tile position */
787 #endif
788   {
789 #if 1
790     if (game.set_centered_player)
791 #else
792     if (game.centered_player_nr != game.centered_player_nr_next)
793 #endif
794     {
795       game.centered_player_nr = game.centered_player_nr_next;
796
797       draw_new_player_location = TRUE;
798       force_redraw = TRUE;
799
800       game.set_centered_player = FALSE;
801     }
802   }
803
804   if (game.centered_player_nr == -1)
805   {
806 #if 1
807     if (draw_new_player_location || offset == 0)
808 #else
809     if (draw_new_player_location)
810 #endif
811     {
812       setScreenCenteredToAllPlayers(&sx, &sy);
813     }
814     else
815     {
816 #if 1
817       sx = PLAYER_SCREEN_X(max_center_distance_player_nr);
818       sy = PLAYER_SCREEN_Y(max_center_distance_player_nr);
819 #else
820       sx = PLAYER_SCREEN_X(game_em.last_moving_player);
821       sy = PLAYER_SCREEN_Y(game_em.last_moving_player);
822 #endif
823     }
824   }
825   else
826   {
827     sx = PLAYER_SCREEN_X(game.centered_player_nr);
828     sy = PLAYER_SCREEN_Y(game.centered_player_nr);
829   }
830
831   if (draw_new_player_location && quick_relocation)
832   {
833     screen_x = VALID_SCREEN_X(sx);
834     screen_y = VALID_SCREEN_Y(sy);
835     screen_x_old = screen_x;
836     screen_y_old = screen_y;
837
838 #if 0
839     offset_x = 0;
840     offset_y = 0;
841 #endif
842   }
843
844   if (draw_new_player_location && !quick_relocation)
845   {
846 #if 1
847     unsigned long game_frame_delay_value = getGameFrameDelay_EM(20);
848 #else
849     unsigned long game_frame_delay_value = getGameFrameDelay_EM(25);
850 #endif
851     int wait_delay_value = game_frame_delay_value;
852     int screen_xx = VALID_SCREEN_X(sx);
853     int screen_yy = VALID_SCREEN_Y(sy);
854
855     while (screen_x != screen_xx || screen_y != screen_yy)
856     {
857       int dx = (screen_xx < screen_x ? +1 : screen_xx > screen_x ? -1 : 0);
858       int dy = (screen_yy < screen_y ? +1 : screen_yy > screen_y ? -1 : 0);
859       int dxx = 0, dyy = 0;
860
861       if (dx == 0 && dy == 0)           /* no scrolling needed at all */
862         break;
863
864 #if 1
865
866       if (ABS(screen_xx - screen_x) >= TILEX)
867       {
868         screen_x -= dx * TILEX;
869         dxx = dx * TILEX / 2;
870       }
871       else
872       {
873         screen_x = screen_xx;
874         dxx = 0;
875       }
876
877       if (ABS(screen_yy - screen_y) >= TILEY)
878       {
879         screen_y -= dy * TILEY;
880         dyy = dy * TILEY / 2;
881       }
882       else
883       {
884         screen_y = screen_yy;
885         dyy = 0;
886       }
887
888 #else
889
890 #if 1
891       if (ABS(screen_xx - screen_x) >= TILEX ||
892           ABS(screen_yy - screen_y) >= TILEY)
893       {
894         screen_x -= dx * TILEX;
895         screen_y -= dy * TILEY;
896
897         dxx = dx * TILEX / 2;
898         dyy = dy * TILEY / 2;
899       }
900       else
901       {
902         screen_x = screen_xx;
903         screen_y = screen_yy;
904
905         dxx = 0;
906         dyy = 0;
907       }
908 #else
909       screen_x -= dx * TILEX;
910       screen_y -= dy * TILEY;
911
912       dxx += dx * TILEX / 2;
913       dyy += dy * TILEY / 2;
914 #endif
915
916 #endif
917
918       /* scroll in two steps of half tile size to make things smoother */
919       screen_x += dxx;
920       screen_y += dyy;
921
922       animscreen();
923
924       for (i = 0; i < MAX_PLAYERS; i++)
925         blitplayer(&ply[i]);
926
927       blitscreen();
928
929       Delay(wait_delay_value);
930
931       /* scroll second step to align at full tile size */
932       screen_x -= dxx;
933       screen_y -= dyy;
934
935 #if 0
936       SyncDisplay();
937 #endif
938
939       animscreen();
940
941       for (i = 0; i < MAX_PLAYERS; i++)
942         blitplayer(&ply[i]);
943
944       blitscreen();
945
946       Delay(wait_delay_value);
947     }
948
949     screen_x_old = screen_x;
950     screen_y_old = screen_y;
951   }
952
953   if (force_redraw)
954   {
955     for (y = 0; y < MAX_BUF_YSIZE; y++)
956     {
957       for (x = 0; x < MAX_BUF_XSIZE; x++)
958       {
959         screentiles[y][x] = -1;
960         crumbled_state[y][x] = 0;
961       }
962     }
963   }
964
965   /* calculate new screen scrolling position, with regard to scroll delay */
966   screen_x = VALID_SCREEN_X(sx + offset_x < screen_x ? sx + offset_x :
967                             sx - offset_x > screen_x ? sx - offset_x :
968                             screen_x);
969   screen_y = VALID_SCREEN_Y(sy + offset_y < screen_y ? sy + offset_y :
970                             sy - offset_y > screen_y ? sy - offset_y :
971                             screen_y);
972
973 #if 0
974   printf("::: (%d, %d) => (%d, %d) [(%d, %d), (%d, %d)] [%d, %d] [%d / %d]\n",
975          screen_x_old, screen_y_old,
976          screen_x, screen_y,
977          ply[max_center_distance_player_nr].oldx,
978          ply[max_center_distance_player_nr].x,
979          ply[max_center_distance_player_nr].oldy,
980          ply[max_center_distance_player_nr].y,
981          sx, sy,
982          ABS(screen_x - screen_x_old),
983          ABS(screen_y - screen_y_old));
984 #endif
985
986 #if 1
987
988 #if 1
989   /* prevent scrolling further than double player step size when scrolling */
990   if (ABS(screen_x - screen_x_old) > 2 * stepsize)
991   {
992     int dx = SIGN(screen_x - screen_x_old);
993
994     screen_x = screen_x_old + dx * 2 * stepsize;
995   }
996   if (ABS(screen_y - screen_y_old) > 2 * stepsize)
997   {
998     int dy = SIGN(screen_y - screen_y_old);
999
1000     screen_y = screen_y_old + dy * 2 * stepsize;
1001   }
1002 #else
1003   /* prevent scrolling further than double player step size when scrolling */
1004   if (ABS(screen_x - screen_x_old) > 2 * stepsize ||
1005       ABS(screen_y - screen_y_old) > 2 * stepsize)
1006   {
1007     int dx = SIGN(screen_x - screen_x_old);
1008     int dy = SIGN(screen_y - screen_y_old);
1009
1010     screen_x = screen_x_old + dx * 2 * stepsize;
1011     screen_y = screen_y_old + dy * 2 * stepsize;
1012   }
1013 #endif
1014
1015 #else
1016   /* prevent scrolling further than player step size when scrolling */
1017   if (ABS(screen_x - screen_x_old) > stepsize ||
1018       ABS(screen_y - screen_y_old) > stepsize)
1019   {
1020     int dx = SIGN(screen_x - screen_x_old);
1021     int dy = SIGN(screen_y - screen_y_old);
1022
1023     screen_x = screen_x_old + dx * stepsize;
1024     screen_y = screen_y_old + dy * stepsize;
1025   }
1026 #endif
1027
1028   /* prevent scrolling away from the other players when focus on all players */
1029   if (game.centered_player_nr == -1)
1030   {
1031 #if 1
1032     /* check if all players are still visible with new scrolling position */
1033     if (checkIfAllPlayersAreVisible(screen_x_old, screen_y_old) &&
1034         !checkIfAllPlayersAreVisible(screen_x, screen_y))
1035     {
1036       /* reset horizontal scroll position to last value, if needed */
1037       if (!checkIfAllPlayersAreVisible(screen_x, screen_y_old))
1038         screen_x = screen_x_old;
1039
1040       /* reset vertical scroll position to last value, if needed */
1041       if (!checkIfAllPlayersAreVisible(screen_x_old, screen_y))
1042         screen_y = screen_y_old;
1043     }
1044 #else
1045     boolean all_players_visible = checkIfAllPlayersAreVisible();
1046
1047     if (!all_players_visible)
1048     {
1049       printf("::: not all players visible\n");
1050
1051       screen_x = screen_x_old;
1052       screen_y = screen_y_old;
1053     }
1054 #endif
1055   }
1056
1057   /* prevent scrolling (for screen correcting) if no player is moving */
1058   if (!game_em.any_player_moving)
1059   {
1060     screen_x = screen_x_old;
1061     screen_y = screen_y_old;
1062   }
1063   else
1064   {
1065     /* prevent scrolling against the players move direction */
1066 #if 0
1067     int player_nr = game_em.last_moving_player;
1068 #endif
1069     int player_nr = (game.centered_player_nr == -1 ?
1070                      max_center_distance_player_nr : game.centered_player_nr);
1071     int player_move_dir = game_em.last_player_direction[player_nr];
1072     int dx = SIGN(screen_x - screen_x_old);
1073     int dy = SIGN(screen_y - screen_y_old);
1074
1075     if ((dx < 0 && player_move_dir != MV_LEFT) ||
1076         (dx > 0 && player_move_dir != MV_RIGHT))
1077       screen_x = screen_x_old;
1078
1079     if ((dy < 0 && player_move_dir != MV_UP) ||
1080         (dy > 0 && player_move_dir != MV_DOWN))
1081       screen_y = screen_y_old;
1082   }
1083
1084   animscreen();
1085
1086   for (i = 0; i < MAX_PLAYERS; i++)
1087     blitplayer(&ply[i]);
1088
1089 #if 0
1090 #if 0
1091   SyncDisplay();
1092 #endif
1093
1094   blitscreen();
1095 #endif
1096 }
1097
1098 void game_animscreen(void)
1099 {
1100   RedrawPlayfield_EM(FALSE);
1101 }
1102
1103 void DrawGameDoorValues_EM()
1104 {
1105 #if 1
1106   int dynamite_state;
1107   int key_state;
1108 #else
1109   int dynamite_state = ply[0].dynamite;         /* !!! ONLY PLAYER 1 !!! */
1110   int key_state = ply[0].keys | ply[1].keys | ply[2].keys | ply[3].keys;
1111 #endif
1112
1113 #if 1
1114   if (game.centered_player_nr == -1)
1115   {
1116 #if 1
1117     int i;
1118
1119     dynamite_state = 0;
1120     key_state = 0;
1121
1122     for (i = 0; i < MAX_PLAYERS; i++)
1123     {
1124       dynamite_state += ply[i].dynamite;
1125       key_state |= ply[i].keys;
1126     }
1127
1128 #else
1129
1130     dynamite_state = ply[0].dynamite;           /* !!! ONLY PLAYER 1 !!! */
1131     key_state = ply[0].keys | ply[1].keys | ply[2].keys | ply[3].keys;
1132 #endif
1133   }
1134   else
1135   {
1136     int player_nr = game.centered_player_nr;
1137
1138     dynamite_state = ply[player_nr].dynamite;
1139     key_state = ply[player_nr].keys;
1140   }
1141 #endif
1142
1143 #if 1
1144   DrawAllGameValues(lev.required, dynamite_state, lev.score,
1145                     lev.time, key_state);
1146 #else
1147   DrawAllGameValues(lev.required, ply1.dynamite, lev.score,
1148                     DISPLAY_TIME(lev.time), ply1.keys | ply2.keys);
1149 #endif
1150 }