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