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