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