rnd-20070904-1-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(Draw[y][x], 7 - frame, x - 2, y - 2);
358
359   for (y = top; y < top + MAX_BUF_YSIZE; y++)
360   {
361     for (x = left; x < left + MAX_BUF_XSIZE; x++)
362     {
363       int sx = x % MAX_BUF_XSIZE;
364       int sy = y % MAX_BUF_YSIZE;    
365       int tile = Draw[y][x];
366       struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
367       int obj = g->unique_identifier;
368       int crm = 0;
369       boolean redraw_screen_tile = FALSE;
370
371       /* re-calculate crumbled state of this tile */
372       if (g->has_crumbled_graphics)
373       {
374         for (i = 0; i < 4; i++)
375         {
376           int xx = x + xy[i][0];
377           int yy = y + xy[i][1];
378           int tile_next;
379
380           if (xx < 0 || xx >= EM_MAX_CAVE_WIDTH ||
381               yy < 0 || yy >= EM_MAX_CAVE_HEIGHT)
382             continue;
383
384           tile_next = Draw[yy][xx];
385
386           if (!graphic_info_em_object[tile_next][frame].has_crumbled_graphics)
387             crm |= (1 << i);
388         }
389       }
390
391       redraw_screen_tile = (screentiles[sy][sx]    != obj ||
392                             crumbled_state[sy][sx] != crm);
393
394       /* !!! TEST ONLY -- CHANGE THIS !!! */
395       if (!game.use_native_emc_graphics_engine)
396         redraw_screen_tile = TRUE;
397
398       /* only redraw screen tiles if they (or their crumbled state) changed */
399       if (redraw_screen_tile)
400       {
401         DrawLevelField_EM(x, y, sx, sy, FALSE);
402         DrawLevelFieldCrumbled_EM(x, y, sx, sy, crm, FALSE);
403
404         screentiles[sy][sx] = obj;
405         crumbled_state[sy][sx] = crm;
406
407         redraw[sx][sy] = TRUE;
408         redraw_tiles++;
409       }
410     }
411   }
412 }
413
414
415 /* blit players to the screen
416  *
417  * handles transparency and movement
418  */
419
420 static void blitplayer(struct PLAYER *ply)
421 {
422   int x1, y1, x2, y2;
423
424   if (!ply->alive)
425     return;
426
427   /* x1/y1 are left/top and x2/y2 are right/down part of the player movement */
428   x1 = (frame * ply->oldx + (8 - frame) * ply->x) * TILEX / 8;
429   y1 = (frame * ply->oldy + (8 - frame) * ply->y) * TILEY / 8;
430   x2 = x1 + TILEX - 1;
431   y2 = y1 + TILEY - 1;
432
433   if ((int)(x2 - screen_x) < ((MAX_BUF_XSIZE - 1) * TILEX - 1) &&
434       (int)(y2 - screen_y) < ((MAX_BUF_YSIZE - 1) * TILEY - 1))
435   {
436     /* some casts to "int" are needed because of negative calculation values */
437     int dx = (int)ply->x - (int)ply->oldx;
438     int dy = (int)ply->y - (int)ply->oldy;
439     int old_x = (int)ply->oldx + (7 - (int)frame) * dx / 8;
440     int old_y = (int)ply->oldy + (7 - (int)frame) * dy / 8;
441     int new_x = old_x + SIGN(dx);
442     int new_y = old_y + SIGN(dy);
443     int old_sx = old_x % MAX_BUF_XSIZE;
444     int old_sy = old_y % MAX_BUF_XSIZE;
445     int new_sx = new_x % MAX_BUF_XSIZE;
446     int new_sy = new_y % MAX_BUF_XSIZE;
447 #if 0
448     int old_crm = crumbled_state[old_sy][old_sx];
449 #endif
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 0
456     x1 %= MAX_BUF_XSIZE * TILEX;
457     y1 %= MAX_BUF_YSIZE * TILEY;
458     x2 %= MAX_BUF_XSIZE * TILEX;
459     y2 %= MAX_BUF_YSIZE * TILEY;
460 #endif
461
462     if (player_is_digging)
463     {
464 #if 0
465       /* draw the field the player is moving from (under the player) */
466       DrawLevelField_EM(old_x, old_y, old_sx, old_sy, FALSE);
467       DrawLevelFieldCrumbled_EM(old_x, old_y, old_sx, old_sy, old_crm, FALSE);
468 #endif
469
470       /* draw the field the player is moving to (under the player) */
471       DrawLevelField_EM(new_x, new_y, new_sx, new_sy, FALSE);
472       DrawLevelFieldCrumbled_EM(new_x, new_y, new_sx, new_sy, new_crm, FALSE);
473
474       /* draw the player (masked) over the element he is just digging away */
475       DrawLevelPlayer_EM(x1, y1, ply->num, ply->anim, TRUE);
476
477 #if 1
478       /* draw the field the player is moving from (masked over the player) */
479       DrawLevelField_EM(old_x, old_y, old_sx, old_sy, TRUE);
480 #endif
481     }
482     else
483     {
484       /* draw the player under the element which is on the same field */
485       DrawLevelPlayer_EM(x1, y1, ply->num, ply->anim, FALSE);
486
487       /* draw the field the player is moving from (masked over the player) */
488       DrawLevelField_EM(old_x, old_y, old_sx, old_sy, TRUE);
489
490       /* draw the field the player is moving to (masked over the player) */
491       DrawLevelField_EM(new_x, new_y, new_sx, new_sy, TRUE);
492     }
493
494     /* redraw screen tiles in the next frame (player may have left the tiles) */
495     screentiles[old_sy][old_sx] = -1;
496     screentiles[new_sy][new_sx] = -1;
497
498     /* mark screen tiles as dirty (force screen refresh with changed content) */
499     redraw[old_sx][old_sy] = TRUE;
500     redraw[new_sx][new_sy] = TRUE;
501     redraw_tiles += 2;
502   }
503 }
504
505 void game_initscreen(void)
506 {
507   int x,y;
508   int dynamite_state = ply[0].dynamite;         /* !!! ONLY PLAYER 1 !!! */
509   int all_keys_state = ply[0].keys | ply[1].keys | ply[2].keys | ply[3].keys;
510   int player_nr;
511
512   frame = 6;
513
514 #if 0
515   game.centered_player_nr = getCenteredPlayerNr_EM();
516 #endif
517
518   player_nr = (game.centered_player_nr != -1 ? game.centered_player_nr : 0);
519
520   screen_x = VALID_SCREEN_X(PLAYER_SCREEN_X(player_nr));
521   screen_y = VALID_SCREEN_Y(PLAYER_SCREEN_Y(player_nr));
522
523   for (y = 0; y < MAX_BUF_YSIZE; y++)
524   {
525     for (x = 0; x < MAX_BUF_XSIZE; x++)
526     {
527       screentiles[y][x] = -1;
528       crumbled_state[y][x] = 0;
529     }
530   }
531
532   DrawAllGameValues(lev.required, dynamite_state, lev.score,
533                     lev.time, all_keys_state);
534 }
535
536 #if 0
537 void DrawRelocatePlayer(struct PlayerInfo *player, boolean quick_relocation)
538 {
539   boolean ffwd_delay = (tape.playing && tape.fast_forward);
540   boolean no_delay = (tape.warp_forward);
541   int frame_delay_value = (ffwd_delay ? FfwdFrameDelay : GameFrameDelay);
542   int wait_delay_value = (no_delay ? 0 : frame_delay_value);
543   int jx = player->jx;
544   int jy = player->jy;
545
546   if (quick_relocation)
547   {
548     int offset = game.scroll_delay_value;
549
550     if (!IN_VIS_FIELD(SCREENX(jx), SCREENY(jy)))
551     {
552       scroll_x = (player->jx < SBX_Left  + MIDPOSX ? SBX_Left :
553                   player->jx > SBX_Right + MIDPOSX ? SBX_Right :
554                   player->jx - MIDPOSX);
555
556       scroll_y = (player->jy < SBY_Upper + MIDPOSY ? SBY_Upper :
557                   player->jy > SBY_Lower + MIDPOSY ? SBY_Lower :
558                   player->jy - MIDPOSY);
559     }
560     else
561     {
562       if ((player->MovDir == MV_LEFT  && scroll_x > jx - MIDPOSX + offset) ||
563           (player->MovDir == MV_RIGHT && scroll_x < jx - MIDPOSX - offset))
564         scroll_x = jx - MIDPOSX + (scroll_x < jx-MIDPOSX ? -offset : +offset);
565
566       if ((player->MovDir == MV_UP  && scroll_y > jy - MIDPOSY + offset) ||
567           (player->MovDir == MV_DOWN && scroll_y < jy - MIDPOSY - offset))
568         scroll_y = jy - MIDPOSY + (scroll_y < jy-MIDPOSY ? -offset : +offset);
569
570       /* don't scroll over playfield boundaries */
571       if (scroll_x < SBX_Left || scroll_x > SBX_Right)
572         scroll_x = (scroll_x < SBX_Left ? SBX_Left : SBX_Right);
573
574       /* don't scroll over playfield boundaries */
575       if (scroll_y < SBY_Upper || scroll_y > SBY_Lower)
576         scroll_y = (scroll_y < SBY_Upper ? SBY_Upper : SBY_Lower);
577     }
578
579     RedrawPlayfield(TRUE, 0,0,0,0);
580   }
581   else
582   {
583     int scroll_xx = -999, scroll_yy = -999;
584
585     ScrollScreen(NULL, SCROLL_GO_ON);   /* scroll last frame to full tile */
586
587     while (scroll_xx != scroll_x || scroll_yy != scroll_y)
588     {
589       int dx = 0, dy = 0;
590       int fx = FX, fy = FY;
591
592       scroll_xx = (player->jx < SBX_Left  + MIDPOSX ? SBX_Left :
593                    player->jx > SBX_Right + MIDPOSX ? SBX_Right :
594                    player->jx - MIDPOSX);
595
596       scroll_yy = (player->jy < SBY_Upper + MIDPOSY ? SBY_Upper :
597                    player->jy > SBY_Lower + MIDPOSY ? SBY_Lower :
598                    player->jy - MIDPOSY);
599
600       dx = (scroll_xx < scroll_x ? +1 : scroll_xx > scroll_x ? -1 : 0);
601       dy = (scroll_yy < scroll_y ? +1 : scroll_yy > scroll_y ? -1 : 0);
602
603       if (dx == 0 && dy == 0)           /* no scrolling needed at all */
604         break;
605
606       scroll_x -= dx;
607       scroll_y -= dy;
608
609       fx += dx * TILEX / 2;
610       fy += dy * TILEY / 2;
611
612       ScrollLevel(dx, dy);
613       DrawAllPlayers();
614
615       /* scroll in two steps of half tile size to make things smoother */
616       BlitBitmap(drawto_field, window, fx, fy, SXSIZE, SYSIZE, SX, SY);
617       FlushDisplay();
618       Delay(wait_delay_value);
619
620       /* scroll second step to align at full tile size */
621       BackToFront();
622       Delay(wait_delay_value);
623     }
624
625     DrawPlayer(player);
626     BackToFront();
627     Delay(wait_delay_value);
628   }
629 }
630 #endif
631
632 static int getMaxCenterDistancePlayerNr(int center_x, int center_y)
633 {
634   int max_dx = 0, max_dy = 0;
635   int player_nr = game_em.last_moving_player;
636   int i;
637
638   for (i = 0; i < MAX_PLAYERS; i++)
639   {
640     if (ply[i].alive)
641     {
642       int sx = PLAYER_SCREEN_X(i);
643       int sy = PLAYER_SCREEN_Y(i);
644
645       if (game_em.last_player_direction[i] != MV_NONE &&
646           (ABS(sx - center_x) > max_dx ||
647            ABS(sy - center_y) > max_dy))
648       {
649         max_dx = MAX(max_dx, ABS(sx - center_x));
650         max_dy = MAX(max_dy, ABS(sy - center_y));
651
652         player_nr = i;
653       }
654     }
655   }
656
657   return player_nr;
658 }
659
660 static void setMinimalPlayerBoundaries(int *sx1, int *sy1, int *sx2, int *sy2)
661 {
662   boolean num_checked_players = 0;
663   int i;
664
665   for (i = 0; i < MAX_PLAYERS; i++)
666   {
667     if (ply[i].alive)
668     {
669       int sx = PLAYER_SCREEN_X(i);
670       int sy = PLAYER_SCREEN_Y(i);
671
672       if (num_checked_players == 0)
673       {
674         *sx1 = *sx2 = sx;
675         *sy1 = *sy2 = sy;
676       }
677       else
678       {
679         *sx1 = MIN(*sx1, sx);
680         *sy1 = MIN(*sy1, sy);
681         *sx2 = MAX(*sx2, sx);
682         *sy2 = MAX(*sy2, sy);
683       }
684
685       num_checked_players++;
686     }
687   }
688 }
689
690 boolean checkIfAllPlayersFitToScreen()
691 {
692   int sx1 = 0, sy1 = 0, sx2 = 0, sy2 = 0;
693
694   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
695
696   return (sx2 - sx1 <= SCR_FIELDX * TILEX &&
697           sy2 - sy1 <= SCR_FIELDY * TILEY);
698 }
699
700 static void setScreenCenteredToAllPlayers(int *sx, int *sy)
701 {
702   int sx1 = screen_x, sy1 = screen_y, sx2 = screen_x, sy2 = screen_y;
703
704   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
705
706   *sx = (sx1 + sx2) / 2;
707   *sy = (sy1 + sy2) / 2;
708 }
709
710 static void setMaxCenterDistanceForAllPlayers(int *max_dx, int *max_dy,
711                                               int center_x, int center_y)
712 {
713   int sx1 = center_x, sy1 = center_y, sx2 = center_x, sy2 = center_y;
714
715   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
716
717   *max_dx = MAX(ABS(sx1 - center_x), ABS(sx2 - center_x));
718   *max_dy = MAX(ABS(sy1 - center_y), ABS(sy2 - center_y));
719 }
720
721 static boolean checkIfAllPlayersAreVisible(int center_x, int center_y)
722 {
723   int max_dx, max_dy;
724
725   setMaxCenterDistanceForAllPlayers(&max_dx, &max_dy, center_x, center_y);
726
727   return (max_dx <= SCR_FIELDX * TILEX / 2 &&
728           max_dy <= SCR_FIELDY * TILEY / 2);
729 }
730
731 void RedrawPlayfield_EM(boolean force_redraw)
732 {
733 #if 0
734   boolean all_players_visible = checkIfAllPlayersAreVisible();
735 #endif
736   boolean draw_new_player_location = FALSE;
737   boolean quick_relocation = setup.quick_switch;
738 #if 0
739   boolean scrolling = (screen_x % TILEX != 0 || screen_y % TILEY != 0);
740 #endif
741 #if 0
742   boolean game.set_centered_player = getSetCenteredPlayer_EM();
743   int game.centered_player_nr_next = getCenteredPlayerNr_EM();
744 #endif
745 #if 1
746   int max_center_distance_player_nr =
747     getMaxCenterDistancePlayerNr(screen_x, screen_y);
748 #else
749   int player_nr = game_em.last_moving_player;
750 #endif
751   int stepsize = TILEX / 8;
752   int offset = game.scroll_delay_value * TILEX;
753   int offset_x = offset;
754   int offset_y = offset;
755   int screen_x_old = screen_x;
756   int screen_y_old = screen_y;
757   int x, y, sx, sy;
758   int i;
759
760   if (game.set_centered_player)
761   {
762     boolean all_players_fit_to_screen = checkIfAllPlayersFitToScreen();
763
764     /* switching to "all players" only possible if all players fit to screen */
765     if (game.centered_player_nr_next == -1 && !all_players_fit_to_screen)
766     {
767       game.centered_player_nr_next = game.centered_player_nr;
768       game.set_centered_player = FALSE;
769     }
770
771     /* do not switch focus to non-existing (or non-active) player */
772     if (game.centered_player_nr_next >= 0 &&
773         !ply[game.centered_player_nr_next].alive)
774     {
775       game.centered_player_nr_next = game.centered_player_nr;
776       game.set_centered_player = FALSE;
777     }
778   }
779
780 #if 1
781   /* also allow focus switching when screen is scrolled to half tile */
782 #else
783   if (!scrolling)       /* screen currently aligned at tile position */
784 #endif
785   {
786 #if 1
787     if (game.set_centered_player)
788 #else
789     if (game.centered_player_nr != game.centered_player_nr_next)
790 #endif
791     {
792       game.centered_player_nr = game.centered_player_nr_next;
793
794       draw_new_player_location = TRUE;
795       force_redraw = TRUE;
796
797       game.set_centered_player = FALSE;
798     }
799   }
800
801   if (game.centered_player_nr == -1)
802   {
803 #if 1
804     if (draw_new_player_location || offset == 0)
805 #else
806     if (draw_new_player_location)
807 #endif
808     {
809       setScreenCenteredToAllPlayers(&sx, &sy);
810     }
811     else
812     {
813 #if 1
814       sx = PLAYER_SCREEN_X(max_center_distance_player_nr);
815       sy = PLAYER_SCREEN_Y(max_center_distance_player_nr);
816 #else
817       sx = PLAYER_SCREEN_X(game_em.last_moving_player);
818       sy = PLAYER_SCREEN_Y(game_em.last_moving_player);
819 #endif
820     }
821   }
822   else
823   {
824     sx = PLAYER_SCREEN_X(game.centered_player_nr);
825     sy = PLAYER_SCREEN_Y(game.centered_player_nr);
826   }
827
828   if (draw_new_player_location && quick_relocation)
829   {
830     screen_x = VALID_SCREEN_X(sx);
831     screen_y = VALID_SCREEN_Y(sy);
832     screen_x_old = screen_x;
833     screen_y_old = screen_y;
834
835 #if 0
836     offset_x = 0;
837     offset_y = 0;
838 #endif
839   }
840
841   if (draw_new_player_location && !quick_relocation)
842   {
843 #if 1
844     unsigned long game_frame_delay_value = getGameFrameDelay_EM(20);
845 #else
846     unsigned long game_frame_delay_value = getGameFrameDelay_EM(25);
847 #endif
848     int wait_delay_value = game_frame_delay_value;
849     int screen_xx = VALID_SCREEN_X(sx);
850     int screen_yy = VALID_SCREEN_Y(sy);
851
852     while (screen_x != screen_xx || screen_y != screen_yy)
853     {
854       int dx = (screen_xx < screen_x ? +1 : screen_xx > screen_x ? -1 : 0);
855       int dy = (screen_yy < screen_y ? +1 : screen_yy > screen_y ? -1 : 0);
856       int dxx = 0, dyy = 0;
857
858       if (dx == 0 && dy == 0)           /* no scrolling needed at all */
859         break;
860
861 #if 1
862
863       if (ABS(screen_xx - screen_x) >= TILEX)
864       {
865         screen_x -= dx * TILEX;
866         dxx = dx * TILEX / 2;
867       }
868       else
869       {
870         screen_x = screen_xx;
871         dxx = 0;
872       }
873
874       if (ABS(screen_yy - screen_y) >= TILEY)
875       {
876         screen_y -= dy * TILEY;
877         dyy = dy * TILEY / 2;
878       }
879       else
880       {
881         screen_y = screen_yy;
882         dyy = 0;
883       }
884
885 #else
886
887 #if 1
888       if (ABS(screen_xx - screen_x) >= TILEX ||
889           ABS(screen_yy - screen_y) >= TILEY)
890       {
891         screen_x -= dx * TILEX;
892         screen_y -= dy * TILEY;
893
894         dxx = dx * TILEX / 2;
895         dyy = dy * TILEY / 2;
896       }
897       else
898       {
899         screen_x = screen_xx;
900         screen_y = screen_yy;
901
902         dxx = 0;
903         dyy = 0;
904       }
905 #else
906       screen_x -= dx * TILEX;
907       screen_y -= dy * TILEY;
908
909       dxx += dx * TILEX / 2;
910       dyy += dy * TILEY / 2;
911 #endif
912
913 #endif
914
915       /* scroll in two steps of half tile size to make things smoother */
916       screen_x += dxx;
917       screen_y += dyy;
918
919       animscreen();
920
921       for (i = 0; i < MAX_PLAYERS; i++)
922         blitplayer(&ply[i]);
923
924       blitscreen();
925
926       Delay(wait_delay_value);
927
928       /* scroll second step to align at full tile size */
929       screen_x -= dxx;
930       screen_y -= dyy;
931
932 #if 0
933       SyncDisplay();
934 #endif
935
936       animscreen();
937
938       for (i = 0; i < MAX_PLAYERS; i++)
939         blitplayer(&ply[i]);
940
941       blitscreen();
942
943       Delay(wait_delay_value);
944     }
945
946     screen_x_old = screen_x;
947     screen_y_old = screen_y;
948   }
949
950   if (force_redraw)
951   {
952     for (y = 0; y < MAX_BUF_YSIZE; y++)
953     {
954       for (x = 0; x < MAX_BUF_XSIZE; x++)
955       {
956         screentiles[y][x] = -1;
957         crumbled_state[y][x] = 0;
958       }
959     }
960   }
961
962   /* calculate new screen scrolling position, with regard to scroll delay */
963   screen_x = VALID_SCREEN_X(sx + offset_x < screen_x ? sx + offset_x :
964                             sx - offset_x > screen_x ? sx - offset_x :
965                             screen_x);
966   screen_y = VALID_SCREEN_Y(sy + offset_y < screen_y ? sy + offset_y :
967                             sy - offset_y > screen_y ? sy - offset_y :
968                             screen_y);
969
970 #if 0
971   printf("::: (%d, %d) => (%d, %d) [(%d, %d), (%d, %d)] [%d, %d] [%d / %d]\n",
972          screen_x_old, screen_y_old,
973          screen_x, screen_y,
974          ply[max_center_distance_player_nr].oldx,
975          ply[max_center_distance_player_nr].x,
976          ply[max_center_distance_player_nr].oldy,
977          ply[max_center_distance_player_nr].y,
978          sx, sy,
979          ABS(screen_x - screen_x_old),
980          ABS(screen_y - screen_y_old));
981 #endif
982
983 #if 1
984
985 #if 1
986   /* prevent scrolling further than double player step size when scrolling */
987   if (ABS(screen_x - screen_x_old) > 2 * stepsize)
988   {
989     int dx = SIGN(screen_x - screen_x_old);
990
991     screen_x = screen_x_old + dx * 2 * stepsize;
992   }
993   if (ABS(screen_y - screen_y_old) > 2 * stepsize)
994   {
995     int dy = SIGN(screen_y - screen_y_old);
996
997     screen_y = screen_y_old + dy * 2 * stepsize;
998   }
999 #else
1000   /* prevent scrolling further than double player step size when scrolling */
1001   if (ABS(screen_x - screen_x_old) > 2 * stepsize ||
1002       ABS(screen_y - screen_y_old) > 2 * stepsize)
1003   {
1004     int dx = SIGN(screen_x - screen_x_old);
1005     int dy = SIGN(screen_y - screen_y_old);
1006
1007     screen_x = screen_x_old + dx * 2 * stepsize;
1008     screen_y = screen_y_old + dy * 2 * stepsize;
1009   }
1010 #endif
1011
1012 #else
1013   /* prevent scrolling further than player step size when scrolling */
1014   if (ABS(screen_x - screen_x_old) > stepsize ||
1015       ABS(screen_y - screen_y_old) > stepsize)
1016   {
1017     int dx = SIGN(screen_x - screen_x_old);
1018     int dy = SIGN(screen_y - screen_y_old);
1019
1020     screen_x = screen_x_old + dx * stepsize;
1021     screen_y = screen_y_old + dy * stepsize;
1022   }
1023 #endif
1024
1025   /* prevent scrolling away from the other players when focus on all players */
1026   if (game.centered_player_nr == -1)
1027   {
1028 #if 1
1029     /* check if all players are still visible with new scrolling position */
1030     if (checkIfAllPlayersAreVisible(screen_x_old, screen_y_old) &&
1031         !checkIfAllPlayersAreVisible(screen_x, screen_y))
1032     {
1033       /* reset horizontal scroll position to last value, if needed */
1034       if (!checkIfAllPlayersAreVisible(screen_x, screen_y_old))
1035         screen_x = screen_x_old;
1036
1037       /* reset vertical scroll position to last value, if needed */
1038       if (!checkIfAllPlayersAreVisible(screen_x_old, screen_y))
1039         screen_y = screen_y_old;
1040     }
1041 #else
1042     boolean all_players_visible = checkIfAllPlayersAreVisible();
1043
1044     if (!all_players_visible)
1045     {
1046       printf("::: not all players visible\n");
1047
1048       screen_x = screen_x_old;
1049       screen_y = screen_y_old;
1050     }
1051 #endif
1052   }
1053
1054   /* prevent scrolling (for screen correcting) if no player is moving */
1055   if (!game_em.any_player_moving)
1056   {
1057     screen_x = screen_x_old;
1058     screen_y = screen_y_old;
1059   }
1060   else
1061   {
1062     /* prevent scrolling against the players move direction */
1063 #if 0
1064     int player_nr = game_em.last_moving_player;
1065 #endif
1066     int player_nr = (game.centered_player_nr == -1 ?
1067                      max_center_distance_player_nr : game.centered_player_nr);
1068     int player_move_dir = game_em.last_player_direction[player_nr];
1069     int dx = SIGN(screen_x - screen_x_old);
1070     int dy = SIGN(screen_y - screen_y_old);
1071
1072     if ((dx < 0 && player_move_dir != MV_LEFT) ||
1073         (dx > 0 && player_move_dir != MV_RIGHT))
1074       screen_x = screen_x_old;
1075
1076     if ((dy < 0 && player_move_dir != MV_UP) ||
1077         (dy > 0 && player_move_dir != MV_DOWN))
1078       screen_y = screen_y_old;
1079   }
1080
1081   animscreen();
1082
1083   for (i = 0; i < MAX_PLAYERS; i++)
1084     blitplayer(&ply[i]);
1085
1086 #if 0
1087 #if 0
1088   SyncDisplay();
1089 #endif
1090
1091   blitscreen();
1092 #endif
1093 }
1094
1095 void game_animscreen(void)
1096 {
1097   RedrawPlayfield_EM(FALSE);
1098 }
1099
1100 void DrawGameDoorValues_EM()
1101 {
1102 #if 1
1103   int dynamite_state;
1104   int key_state;
1105 #else
1106   int dynamite_state = ply[0].dynamite;         /* !!! ONLY PLAYER 1 !!! */
1107   int key_state = ply[0].keys | ply[1].keys | ply[2].keys | ply[3].keys;
1108 #endif
1109
1110 #if 1
1111   if (game.centered_player_nr == -1)
1112   {
1113 #if 1
1114     int i;
1115
1116     dynamite_state = 0;
1117     key_state = 0;
1118
1119     for (i = 0; i < MAX_PLAYERS; i++)
1120     {
1121       dynamite_state += ply[i].dynamite;
1122       key_state |= ply[i].keys;
1123     }
1124
1125 #else
1126
1127     dynamite_state = ply[0].dynamite;           /* !!! ONLY PLAYER 1 !!! */
1128     key_state = ply[0].keys | ply[1].keys | ply[2].keys | ply[3].keys;
1129 #endif
1130   }
1131   else
1132   {
1133     int player_nr = game.centered_player_nr;
1134
1135     dynamite_state = ply[player_nr].dynamite;
1136     key_state = ply[player_nr].keys;
1137   }
1138 #endif
1139
1140 #if 1
1141   DrawAllGameValues(lev.required, dynamite_state, lev.score,
1142                     lev.time, key_state);
1143 #else
1144   DrawAllGameValues(lev.required, ply1.dynamite, lev.score,
1145                     DISPLAY_TIME(lev.time), ply1.keys | ply2.keys);
1146 #endif
1147 }