added support for special media buttons on Amazon Fire TV remote control
[rocksndiamonds.git] / src / tools.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // tools.c
10 // ============================================================================
11
12 #include <math.h>
13
14 #include "libgame/libgame.h"
15
16 #include "tools.h"
17 #include "init.h"
18 #include "game.h"
19 #include "events.h"
20 #include "anim.h"
21 #include "network.h"
22 #include "tape.h"
23 #include "screens.h"
24
25
26 /* select level set with EMC X11 graphics before activating EM GFX debugging */
27 #define DEBUG_EM_GFX            FALSE
28 #define DEBUG_FRAME_TIME        FALSE
29
30 /* tool button identifiers */
31 #define TOOL_CTRL_ID_YES        0
32 #define TOOL_CTRL_ID_NO         1
33 #define TOOL_CTRL_ID_CONFIRM    2
34 #define TOOL_CTRL_ID_PLAYER_1   3
35 #define TOOL_CTRL_ID_PLAYER_2   4
36 #define TOOL_CTRL_ID_PLAYER_3   5
37 #define TOOL_CTRL_ID_PLAYER_4   6
38
39 #define NUM_TOOL_BUTTONS        7
40
41 /* constants for number of doors and door parts */
42 #define NUM_DOORS               2
43 #define NUM_PANELS              NUM_DOORS
44 // #define NUM_PANELS           0
45 #define MAX_PARTS_PER_DOOR      8
46 #define MAX_DOOR_PARTS          (NUM_DOORS * MAX_PARTS_PER_DOOR + NUM_PANELS)
47 #define DOOR_PART_IS_PANEL(i)   ((i) >= NUM_DOORS * MAX_PARTS_PER_DOOR)
48
49
50 struct DoorPartOrderInfo
51 {
52   int nr;
53   int sort_priority;
54 };
55
56 static struct DoorPartOrderInfo door_part_order[MAX_DOOR_PARTS];
57
58 struct DoorPartControlInfo
59 {
60   int door_token;
61   int graphic;
62   struct DoorPartPosInfo *pos;
63 };
64
65 static struct DoorPartControlInfo door_part_controls[] =
66 {
67   {
68     DOOR_1,
69     IMG_GFX_DOOR_1_PART_1,
70     &door_1.part_1
71   },
72   {
73     DOOR_1,
74     IMG_GFX_DOOR_1_PART_2,
75     &door_1.part_2
76   },
77   {
78     DOOR_1,
79     IMG_GFX_DOOR_1_PART_3,
80     &door_1.part_3
81   },
82   {
83     DOOR_1,
84     IMG_GFX_DOOR_1_PART_4,
85     &door_1.part_4
86   },
87   {
88     DOOR_1,
89     IMG_GFX_DOOR_1_PART_5,
90     &door_1.part_5
91   },
92   {
93     DOOR_1,
94     IMG_GFX_DOOR_1_PART_6,
95     &door_1.part_6
96   },
97   {
98     DOOR_1,
99     IMG_GFX_DOOR_1_PART_7,
100     &door_1.part_7
101   },
102   {
103     DOOR_1,
104     IMG_GFX_DOOR_1_PART_8,
105     &door_1.part_8
106   },
107
108   {
109     DOOR_2,
110     IMG_GFX_DOOR_2_PART_1,
111     &door_2.part_1
112   },
113   {
114     DOOR_2,
115     IMG_GFX_DOOR_2_PART_2,
116     &door_2.part_2
117   },
118   {
119     DOOR_2,
120     IMG_GFX_DOOR_2_PART_3,
121     &door_2.part_3
122   },
123   {
124     DOOR_2,
125     IMG_GFX_DOOR_2_PART_4,
126     &door_2.part_4
127   },
128   {
129     DOOR_2,
130     IMG_GFX_DOOR_2_PART_5,
131     &door_2.part_5
132   },
133   {
134     DOOR_2,
135     IMG_GFX_DOOR_2_PART_6,
136     &door_2.part_6
137   },
138   {
139     DOOR_2,
140     IMG_GFX_DOOR_2_PART_7,
141     &door_2.part_7
142   },
143   {
144     DOOR_2,
145     IMG_GFX_DOOR_2_PART_8,
146     &door_2.part_8
147   },
148
149   {
150     DOOR_1,
151     IMG_BACKGROUND_PANEL,
152     &door_1.panel
153   },
154   {
155     DOOR_2,
156     IMG_BACKGROUND_TAPE,
157     &door_2.panel
158   },
159
160   {
161     -1,
162     -1,
163     NULL
164   }
165 };
166
167
168 /* forward declaration for internal use */
169 static void UnmapToolButtons();
170 static void HandleToolButtons(struct GadgetInfo *);
171 static int el_act_dir2crm(int, int, int);
172 static int el_act2crm(int, int);
173
174 static struct GadgetInfo *tool_gadget[NUM_TOOL_BUTTONS];
175 static int request_gadget_id = -1;
176
177 static char *print_if_not_empty(int element)
178 {
179   static char *s = NULL;
180   char *token_name = element_info[element].token_name;
181
182   if (s != NULL)
183     free(s);
184
185   s = checked_malloc(strlen(token_name) + 10 + 1);
186
187   if (element != EL_EMPTY)
188     sprintf(s, "%d\t['%s']", element, token_name);
189   else
190     sprintf(s, "%d", element);
191
192   return s;
193 }
194
195 int correctLevelPosX_EM(int lx)
196 {
197   lx -= 1;
198   lx -= (BorderElement != EL_EMPTY ? 1 : 0);
199
200   return lx;
201 }
202
203 int correctLevelPosY_EM(int ly)
204 {
205   ly -= 1;
206   ly -= (BorderElement != EL_EMPTY ? 1 : 0);
207
208   return ly;
209 }
210
211 static int getFieldbufferOffsetX_RND()
212 {
213   int full_lev_fieldx = lev_fieldx + (BorderElement != EL_EMPTY ? 2 : 0);
214   int dx = (ScreenMovDir & (MV_LEFT | MV_RIGHT) ? ScreenGfxPos : 0);
215   int dx_var = dx * TILESIZE_VAR / TILESIZE;
216   int fx = FX;
217
218   if (EVEN(SCR_FIELDX))
219   {
220     int ffx = (scroll_x - SBX_Left)  * TILEX_VAR + dx_var;
221
222     if (ffx < SBX_Right * TILEX_VAR + TILEX_VAR / 2 + TILEX_VAR)
223       fx += dx_var - MIN(ffx, TILEX_VAR / 2) + TILEX_VAR;
224     else
225       fx += (dx_var > 0 ? TILEX_VAR : 0);
226   }
227   else
228   {
229     fx += dx_var;
230   }
231
232   if (full_lev_fieldx <= SCR_FIELDX)
233   {
234     if (EVEN(SCR_FIELDX))
235       fx = 2 * TILEX_VAR - (ODD(lev_fieldx)  ? TILEX_VAR / 2 : 0);
236     else
237       fx = 2 * TILEX_VAR - (EVEN(lev_fieldx) ? TILEX_VAR / 2 : 0);
238   }
239
240   return fx;
241 }
242
243 static int getFieldbufferOffsetY_RND()
244 {
245   int full_lev_fieldy = lev_fieldy + (BorderElement != EL_EMPTY ? 2 : 0);
246   int dy = (ScreenMovDir & (MV_UP | MV_DOWN)    ? ScreenGfxPos : 0);
247   int dy_var = dy * TILESIZE_VAR / TILESIZE;
248   int fy = FY;
249
250   if (EVEN(SCR_FIELDY))
251   {
252     int ffy = (scroll_y - SBY_Upper) * TILEY_VAR + dy_var;
253
254     if (ffy < SBY_Lower * TILEY_VAR + TILEY_VAR / 2 + TILEY_VAR)
255       fy += dy_var - MIN(ffy, TILEY_VAR / 2) + TILEY_VAR;
256     else
257       fy += (dy_var > 0 ? TILEY_VAR : 0);
258   }
259   else
260   {
261     fy += dy_var;
262   }
263
264   if (full_lev_fieldy <= SCR_FIELDY)
265   {
266     if (EVEN(SCR_FIELDY))
267       fy = 2 * TILEY_VAR - (ODD(lev_fieldy)  ? TILEY_VAR / 2 : 0);
268     else
269       fy = 2 * TILEY_VAR - (EVEN(lev_fieldy) ? TILEY_VAR / 2 : 0);
270   }
271
272   return fy;
273 }
274
275 static int getLevelFromScreenX_RND(int sx)
276 {
277   int fx = getFieldbufferOffsetX_RND();
278   int dx = fx - FX;
279   int px = sx - SX;
280   int lx = LEVELX((px + dx) / TILESIZE_VAR);
281
282   return lx;
283 }
284
285 static int getLevelFromScreenY_RND(int sy)
286 {
287   int fy = getFieldbufferOffsetY_RND();
288   int dy = fy - FY;
289   int py = sy - SY;
290   int ly = LEVELY((py + dy) / TILESIZE_VAR);
291
292   return ly;
293 }
294
295 static int getLevelFromScreenX_EM(int sx)
296 {
297   int level_xsize = level.native_em_level->lev->width;
298   int full_xsize = level_xsize * TILESIZE_VAR;
299
300   sx -= (full_xsize < SXSIZE ? (SXSIZE - full_xsize) / 2 : 0);
301
302   int fx = getFieldbufferOffsetX_EM();
303   int dx = fx;
304   int px = sx - SX;
305   int lx = LEVELX((px + dx) / TILESIZE_VAR);
306
307   lx = correctLevelPosX_EM(lx);
308
309   return lx;
310 }
311
312 static int getLevelFromScreenY_EM(int sy)
313 {
314   int level_ysize = level.native_em_level->lev->height;
315   int full_ysize = level_ysize * TILESIZE_VAR;
316
317   sy -= (full_ysize < SYSIZE ? (SYSIZE - full_ysize) / 2 : 0);
318
319   int fy = getFieldbufferOffsetY_EM();
320   int dy = fy;
321   int py = sy - SY;
322   int ly = LEVELY((py + dy) / TILESIZE_VAR);
323
324   ly = correctLevelPosY_EM(ly);
325
326   return ly;
327 }
328
329 static int getLevelFromScreenX_SP(int sx)
330 {
331   int menBorder = setup.sp_show_border_elements;
332   int level_xsize = level.native_sp_level->width;
333   int full_xsize = (level_xsize - (menBorder ? 0 : 1)) * TILESIZE_VAR;
334
335   sx += (full_xsize < SXSIZE ? (SXSIZE - full_xsize) / 2 : 0);
336
337   int fx = getFieldbufferOffsetX_SP();
338   int dx = fx - FX;
339   int px = sx - SX;
340   int lx = LEVELX((px + dx) / TILESIZE_VAR);
341
342   return lx;
343 }
344
345 static int getLevelFromScreenY_SP(int sy)
346 {
347   int menBorder = setup.sp_show_border_elements;
348   int level_ysize = level.native_sp_level->height;
349   int full_ysize = (level_ysize - (menBorder ? 0 : 1)) * TILESIZE_VAR;
350
351   sy += (full_ysize < SYSIZE ? (SYSIZE - full_ysize) / 2 : 0);
352
353   int fy = getFieldbufferOffsetY_SP();
354   int dy = fy - FY;
355   int py = sy - SY;
356   int ly = LEVELY((py + dy) / TILESIZE_VAR);
357
358   return ly;
359 }
360
361 int getLevelFromScreenX(int x)
362 {
363   if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
364     return getLevelFromScreenX_EM(x);
365   if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
366     return getLevelFromScreenX_SP(x);
367   else
368     return getLevelFromScreenX_RND(x);
369 }
370
371 int getLevelFromScreenY(int y)
372 {
373   if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
374     return getLevelFromScreenY_EM(y);
375   if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
376     return getLevelFromScreenY_SP(y);
377   else
378     return getLevelFromScreenY_RND(y);
379 }
380
381 void DumpTile(int x, int y)
382 {
383   int sx = SCREENX(x);
384   int sy = SCREENY(y);
385
386   printf_line("-", 79);
387   printf("Field Info: SCREEN(%d, %d), LEVEL(%d, %d)\n", sx, sy, x, y);
388   printf_line("-", 79);
389
390   if (!IN_LEV_FIELD(x, y))
391   {
392     printf("(not in level field)\n");
393     printf("\n");
394
395     return;
396   }
397
398   printf("  Feld:        %d\t['%s']\n", Feld[x][y],
399          element_info[Feld[x][y]].token_name);
400   printf("  Back:        %s\n", print_if_not_empty(Back[x][y]));
401   printf("  Store:       %s\n", print_if_not_empty(Store[x][y]));
402   printf("  Store2:      %s\n", print_if_not_empty(Store2[x][y]));
403   printf("  StorePlayer: %s\n", print_if_not_empty(StorePlayer[x][y]));
404   printf("  MovPos:      %d\n", MovPos[x][y]);
405   printf("  MovDir:      %d\n", MovDir[x][y]);
406   printf("  MovDelay:    %d\n", MovDelay[x][y]);
407   printf("  ChangeDelay: %d\n", ChangeDelay[x][y]);
408   printf("  CustomValue: %d\n", CustomValue[x][y]);
409   printf("  GfxElement:  %d\n", GfxElement[x][y]);
410   printf("  GfxAction:   %d\n", GfxAction[x][y]);
411   printf("  GfxFrame:    %d [%d]\n", GfxFrame[x][y], FrameCounter);
412   printf("  Player x/y:  %d, %d\n", local_player->jx, local_player->jy);
413   printf("\n");
414 }
415
416 void DumpTileFromScreen(int sx, int sy)
417 {
418   int lx = getLevelFromScreenX(sx);
419   int ly = getLevelFromScreenY(sy);
420
421   DumpTile(lx, ly);
422 }
423
424 void SetDrawtoField(int mode)
425 {
426   if (mode == DRAW_TO_FIELDBUFFER)
427   {
428     FX = 2 * TILEX_VAR;
429     FY = 2 * TILEY_VAR;
430     BX1 = -2;
431     BY1 = -2;
432     BX2 = SCR_FIELDX + 1;
433     BY2 = SCR_FIELDY + 1;
434
435     drawto_field = fieldbuffer;
436   }
437   else  /* DRAW_TO_BACKBUFFER */
438   {
439     FX = SX;
440     FY = SY;
441     BX1 = 0;
442     BY1 = 0;
443     BX2 = SCR_FIELDX - 1;
444     BY2 = SCR_FIELDY - 1;
445
446     drawto_field = backbuffer;
447   }
448 }
449
450 static void RedrawPlayfield_RND()
451 {
452   if (game.envelope_active)
453     return;
454
455   DrawLevel(REDRAW_ALL);
456   DrawAllPlayers();
457 }
458
459 void RedrawPlayfield()
460 {
461   if (game_status != GAME_MODE_PLAYING)
462     return;
463
464   if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
465     RedrawPlayfield_EM(TRUE);
466   else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
467     RedrawPlayfield_SP(TRUE);
468   else if (level.game_engine_type == GAME_ENGINE_TYPE_RND)
469     RedrawPlayfield_RND();
470
471   BlitScreenToBitmap(backbuffer);
472
473   BlitBitmap(drawto, window, gfx.sx, gfx.sy, gfx.sxsize, gfx.sysize,
474              gfx.sx, gfx.sy);
475 }
476
477 static void DrawMaskedBorderExt_Rect(int x, int y, int width, int height,
478                                      int draw_target)
479 {
480   Bitmap *src_bitmap = getGlobalBorderBitmapFromStatus(global.border_status);
481   Bitmap *dst_bitmap = gfx.masked_border_bitmap_ptr;
482
483   if (x == -1 && y == -1)
484     return;
485
486   if (draw_target == DRAW_TO_SCREEN)
487     BlitToScreenMasked(src_bitmap, x, y, width, height, x, y);
488   else
489     BlitBitmapMasked(src_bitmap, dst_bitmap, x, y, width, height, x, y);
490 }
491
492 static void DrawMaskedBorderExt_FIELD(int draw_target)
493 {
494   if (global.border_status >= GAME_MODE_MAIN &&
495       global.border_status <= GAME_MODE_PLAYING &&
496       border.draw_masked[global.border_status])
497     DrawMaskedBorderExt_Rect(REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE,
498                              draw_target);
499 }
500
501 static void DrawMaskedBorderExt_DOOR_1(int draw_target)
502 {
503   // when drawing to backbuffer, never draw border over open doors
504   if (draw_target == DRAW_TO_BACKBUFFER &&
505       (GetDoorState() & DOOR_OPEN_1))
506     return;
507
508   if (border.draw_masked[GFX_SPECIAL_ARG_DOOR] &&
509       (global.border_status != GAME_MODE_EDITOR ||
510        border.draw_masked[GFX_SPECIAL_ARG_EDITOR]))
511     DrawMaskedBorderExt_Rect(DX, DY, DXSIZE, DYSIZE, draw_target);
512 }
513
514 static void DrawMaskedBorderExt_DOOR_2(int draw_target)
515 {
516   // when drawing to backbuffer, never draw border over open doors
517   if (draw_target == DRAW_TO_BACKBUFFER &&
518       (GetDoorState() & DOOR_OPEN_2))
519     return;
520
521   if (border.draw_masked[GFX_SPECIAL_ARG_DOOR] &&
522       global.border_status != GAME_MODE_EDITOR)
523     DrawMaskedBorderExt_Rect(VX, VY, VXSIZE, VYSIZE, draw_target);
524 }
525
526 static void DrawMaskedBorderExt_DOOR_3(int draw_target)
527 {
528   /* currently not available */
529 }
530
531 static void DrawMaskedBorderExt_ALL(int draw_target)
532 {
533   DrawMaskedBorderExt_FIELD(draw_target);
534   DrawMaskedBorderExt_DOOR_1(draw_target);
535   DrawMaskedBorderExt_DOOR_2(draw_target);
536   DrawMaskedBorderExt_DOOR_3(draw_target);
537 }
538
539 static void DrawMaskedBorderExt(int redraw_mask, int draw_target)
540 {
541   /* never draw masked screen borders on borderless screens */
542   if (global.border_status == GAME_MODE_LOADING ||
543       global.border_status == GAME_MODE_TITLE)
544     return;
545
546   if (redraw_mask & REDRAW_ALL)
547     DrawMaskedBorderExt_ALL(draw_target);
548   else
549   {
550     if (redraw_mask & REDRAW_FIELD)
551       DrawMaskedBorderExt_FIELD(draw_target);
552     if (redraw_mask & REDRAW_DOOR_1)
553       DrawMaskedBorderExt_DOOR_1(draw_target);
554     if (redraw_mask & REDRAW_DOOR_2)
555       DrawMaskedBorderExt_DOOR_2(draw_target);
556     if (redraw_mask & REDRAW_DOOR_3)
557       DrawMaskedBorderExt_DOOR_3(draw_target);
558   }
559 }
560
561 void DrawMaskedBorder_FIELD()
562 {
563   DrawMaskedBorderExt_FIELD(DRAW_TO_BACKBUFFER);
564 }
565
566 void DrawMaskedBorder(int redraw_mask)
567 {
568   DrawMaskedBorderExt(redraw_mask, DRAW_TO_BACKBUFFER);
569 }
570
571 void DrawMaskedBorderToTarget(int draw_target)
572 {
573   if (draw_target == DRAW_TO_BACKBUFFER ||
574       draw_target == DRAW_TO_SCREEN)
575   {
576     DrawMaskedBorderExt(REDRAW_ALL, draw_target);
577   }
578   else
579   {
580     int last_border_status = global.border_status;
581
582     if (draw_target == DRAW_TO_FADE_SOURCE)
583     {
584       global.border_status = gfx.fade_border_source_status;
585       gfx.masked_border_bitmap_ptr = gfx.fade_bitmap_source;
586     }
587     else if (draw_target == DRAW_TO_FADE_TARGET)
588     {
589       global.border_status = gfx.fade_border_target_status;
590       gfx.masked_border_bitmap_ptr = gfx.fade_bitmap_target;
591     }
592
593     DrawMaskedBorderExt(REDRAW_ALL, draw_target);
594
595     global.border_status = last_border_status;
596     gfx.masked_border_bitmap_ptr = backbuffer;
597   }
598 }
599
600 void BlitScreenToBitmap_RND(Bitmap *target_bitmap)
601 {
602   int fx = getFieldbufferOffsetX_RND();
603   int fy = getFieldbufferOffsetY_RND();
604
605   BlitBitmap(drawto_field, target_bitmap, fx, fy, SXSIZE, SYSIZE, SX, SY);
606 }
607
608 void BlitScreenToBitmap(Bitmap *target_bitmap)
609 {
610   if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
611     BlitScreenToBitmap_EM(target_bitmap);
612   else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
613     BlitScreenToBitmap_SP(target_bitmap);
614   else if (level.game_engine_type == GAME_ENGINE_TYPE_RND)
615     BlitScreenToBitmap_RND(target_bitmap);
616
617   redraw_mask |= REDRAW_FIELD;
618 }
619
620 void DrawFramesPerSecond()
621 {
622   char text[100];
623   int font_nr = FONT_TEXT_2;
624   int font_width = getFontWidth(font_nr);
625
626   sprintf(text, "%04.1f fps", global.frames_per_second);
627
628   DrawTextExt(backbuffer, WIN_XSIZE - font_width * strlen(text), 0, text,
629               font_nr, BLIT_OPAQUE);
630 }
631
632 #if DEBUG_FRAME_TIME
633 static void PrintFrameTimeDebugging()
634 {
635   static unsigned int last_counter = 0;
636   unsigned int counter = Counter();
637   int diff_1 = counter - last_counter;
638   int diff_2 = diff_1 - GAME_FRAME_DELAY;
639   int diff_2_max = 20;
640   int diff_2_cut = MIN(ABS(diff_2), diff_2_max);
641   char diff_bar[2 * diff_2_max + 5];
642   int pos = 0;
643   int i;
644
645   diff_bar[pos++] = (diff_2 < -diff_2_max ? '<' : ' ');
646
647   for (i = 0; i < diff_2_max; i++)
648     diff_bar[pos++] = (diff_2 >= 0 ? ' ' :
649                        i >= diff_2_max - diff_2_cut ? '-' : ' ');
650
651   diff_bar[pos++] = '|';
652
653   for (i = 0; i < diff_2_max; i++)
654     diff_bar[pos++] = (diff_2 <= 0 ? ' ' : i < diff_2_cut ? '+' : ' ');
655
656   diff_bar[pos++] = (diff_2 > diff_2_max ? '>' : ' ');
657
658   diff_bar[pos++] = '\0';
659
660   Error(ERR_INFO, "%06d [%02d] [%c%02d] %s",
661         counter,
662         diff_1,
663         (diff_2 < 0 ? '-' : diff_2 > 0 ? '+' : ' '), ABS(diff_2),
664         diff_bar);
665
666   last_counter = counter;
667 }
668 #endif
669
670 static int unifiedRedrawMask(int mask)
671 {
672   if (mask & REDRAW_ALL)
673     return REDRAW_ALL;
674
675   if (mask & REDRAW_FIELD && mask & REDRAW_DOORS)
676     return REDRAW_ALL;
677
678   return mask;
679 }
680
681 static boolean equalRedrawMasks(int mask_1, int mask_2)
682 {
683   return unifiedRedrawMask(mask_1) == unifiedRedrawMask(mask_2);
684 }
685
686 void BackToFront()
687 {
688   static int last_redraw_mask = REDRAW_NONE;
689
690   // force screen redraw in every frame to continue drawing global animations
691   // (but always use the last redraw mask to prevent unwanted side effects)
692   if (redraw_mask == REDRAW_NONE)
693     redraw_mask = last_redraw_mask;
694
695   last_redraw_mask = redraw_mask;
696
697 #if 1
698   // masked border now drawn immediately when blitting backbuffer to window
699 #else
700   // draw masked border to all viewports, if defined
701   DrawMaskedBorder(redraw_mask);
702 #endif
703
704   // draw frames per second (only if debug mode is enabled)
705   if (redraw_mask & REDRAW_FPS)
706     DrawFramesPerSecond();
707
708   // remove playfield redraw before potentially merging with doors redraw
709   if (DrawingDeactivated(REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE))
710     redraw_mask &= ~REDRAW_FIELD;
711
712   // redraw complete window if both playfield and (some) doors need redraw
713   if (redraw_mask & REDRAW_FIELD && redraw_mask & REDRAW_DOORS)
714     redraw_mask = REDRAW_ALL;
715
716   /* although redrawing the whole window would be fine for normal gameplay,
717      being able to only redraw the playfield is required for deactivating
718      certain drawing areas (mainly playfield) to work, which is needed for
719      warp-forward to be fast enough (by skipping redraw of most frames) */
720
721   if (redraw_mask & REDRAW_ALL)
722   {
723     BlitBitmap(backbuffer, window, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
724   }
725   else if (redraw_mask & REDRAW_FIELD)
726   {
727     BlitBitmap(backbuffer, window,
728                REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE, REAL_SX, REAL_SY);
729   }
730   else if (redraw_mask & REDRAW_DOORS)
731   {
732     // merge door areas to prevent calling screen redraw more than once
733     int x1 = WIN_XSIZE;
734     int y1 = WIN_YSIZE;
735     int x2 = 0;
736     int y2 = 0;
737
738     if (redraw_mask & REDRAW_DOOR_1)
739     {
740       x1 = MIN(x1, DX);
741       y1 = MIN(y1, DY);
742       x2 = MAX(x2, DX + DXSIZE);
743       y2 = MAX(y2, DY + DYSIZE);
744     }
745
746     if (redraw_mask & REDRAW_DOOR_2)
747     {
748       x1 = MIN(x1, VX);
749       y1 = MIN(y1, VY);
750       x2 = MAX(x2, VX + VXSIZE);
751       y2 = MAX(y2, VY + VYSIZE);
752     }
753
754     if (redraw_mask & REDRAW_DOOR_3)
755     {
756       x1 = MIN(x1, EX);
757       y1 = MIN(y1, EY);
758       x2 = MAX(x2, EX + EXSIZE);
759       y2 = MAX(y2, EY + EYSIZE);
760     }
761
762     // make sure that at least one pixel is blitted, and inside the screen
763     // (else nothing is blitted, causing the animations not to be updated)
764     x1 = MIN(MAX(0, x1), WIN_XSIZE - 1);
765     y1 = MIN(MAX(0, y1), WIN_YSIZE - 1);
766     x2 = MIN(MAX(1, x2), WIN_XSIZE);
767     y2 = MIN(MAX(1, y2), WIN_YSIZE);
768
769     BlitBitmap(backbuffer, window, x1, y1, x2 - x1, y2 - y1, x1, y1);
770   }
771
772   redraw_mask = REDRAW_NONE;
773
774 #if DEBUG_FRAME_TIME
775   PrintFrameTimeDebugging();
776 #endif
777 }
778
779 void BackToFront_WithFrameDelay(unsigned int frame_delay_value)
780 {
781   unsigned int frame_delay_value_old = GetVideoFrameDelay();
782
783   SetVideoFrameDelay(frame_delay_value);
784
785   BackToFront();
786
787   SetVideoFrameDelay(frame_delay_value_old);
788 }
789
790 static int fade_type_skip = FADE_TYPE_NONE;
791
792 static void FadeExt(int fade_mask, int fade_mode, int fade_type)
793 {
794   void (*draw_border_function)(void) = NULL;
795   int x, y, width, height;
796   int fade_delay, post_delay;
797
798   if (fade_type == FADE_TYPE_FADE_OUT)
799   {
800     if (fade_type_skip != FADE_TYPE_NONE)
801     {
802       /* skip all fade operations until specified fade operation */
803       if (fade_type & fade_type_skip)
804         fade_type_skip = FADE_TYPE_NONE;
805
806       return;
807     }
808
809     if (fading.fade_mode & FADE_TYPE_TRANSFORM)
810       return;
811   }
812
813   redraw_mask |= fade_mask;
814
815   if (fade_type == FADE_TYPE_SKIP)
816   {
817     fade_type_skip = fade_mode;
818
819     return;
820   }
821
822   fade_delay = fading.fade_delay;
823   post_delay = (fade_mode == FADE_MODE_FADE_OUT ? fading.post_delay : 0);
824
825   if (fade_type_skip != FADE_TYPE_NONE)
826   {
827     /* skip all fade operations until specified fade operation */
828     if (fade_type & fade_type_skip)
829       fade_type_skip = FADE_TYPE_NONE;
830
831     fade_delay = 0;
832   }
833
834   if (global.autoplay_leveldir)
835   {
836     return;
837   }
838
839   if (fade_mask == REDRAW_FIELD)
840   {
841     x = FADE_SX;
842     y = FADE_SY;
843     width  = FADE_SXSIZE;
844     height = FADE_SYSIZE;
845
846     if (border.draw_masked_when_fading)
847       draw_border_function = DrawMaskedBorder_FIELD;    /* update when fading */
848     else
849       DrawMaskedBorder_FIELD();                         /* draw once */
850   }
851   else          /* REDRAW_ALL */
852   {
853     x = 0;
854     y = 0;
855     width  = WIN_XSIZE;
856     height = WIN_YSIZE;
857   }
858
859   if (!setup.fade_screens ||
860       fade_delay == 0 ||
861       fading.fade_mode == FADE_MODE_NONE)
862   {
863     if (fade_mode == FADE_MODE_FADE_OUT)
864       return;
865
866     BlitBitmap(backbuffer, window, x, y, width, height, x, y);
867
868     redraw_mask &= ~fade_mask;
869
870     return;
871   }
872
873   FadeRectangle(x, y, width, height, fade_mode, fade_delay, post_delay,
874                 draw_border_function);
875
876   redraw_mask &= ~fade_mask;
877 }
878
879 static void SetScreenStates_BeforeFadingIn()
880 {
881   // temporarily set screen mode for animations to screen after fading in
882   global.anim_status = global.anim_status_next;
883
884   // store backbuffer with all animations that will be started after fading in
885   if (fade_type_skip != FADE_MODE_SKIP_FADE_IN)
886     PrepareFadeBitmap(DRAW_TO_FADE_TARGET);
887
888   // set screen mode for animations back to fading
889   global.anim_status = GAME_MODE_PSEUDO_FADING;
890 }
891
892 static void SetScreenStates_AfterFadingIn()
893 {
894   // store new source screen (to use correct masked border for fading)
895   gfx.fade_border_source_status = global.border_status;
896
897   global.anim_status = global.anim_status_next;
898 }
899
900 static void SetScreenStates_BeforeFadingOut()
901 {
902   // store new target screen (to use correct masked border for fading)
903   gfx.fade_border_target_status = game_status;
904
905   // set screen mode for animations to fading
906   global.anim_status = GAME_MODE_PSEUDO_FADING;
907
908   // store backbuffer with all animations that will be stopped for fading out
909   if (fade_type_skip != FADE_MODE_SKIP_FADE_OUT)
910     PrepareFadeBitmap(DRAW_TO_FADE_SOURCE);
911 }
912
913 static void SetScreenStates_AfterFadingOut()
914 {
915   global.border_status = game_status;
916 }
917
918 void FadeIn(int fade_mask)
919 {
920   SetScreenStates_BeforeFadingIn();
921
922 #if 1
923   DrawMaskedBorder(REDRAW_ALL);
924 #endif
925
926   if (fading.fade_mode & FADE_TYPE_TRANSFORM)
927     FadeExt(fade_mask, fading.fade_mode, FADE_TYPE_FADE_IN);
928   else
929     FadeExt(fade_mask, FADE_MODE_FADE_IN, FADE_TYPE_FADE_IN);
930
931   FADE_SX = REAL_SX;
932   FADE_SY = REAL_SY;
933   FADE_SXSIZE = FULL_SXSIZE;
934   FADE_SYSIZE = FULL_SYSIZE;
935
936   if (game_status == GAME_MODE_PLAYING &&
937       strEqual(setup.touch.control_type, TOUCH_CONTROL_VIRTUAL_BUTTONS))
938     SetOverlayActive(TRUE);
939
940   SetScreenStates_AfterFadingIn();
941
942   // force update of global animation status in case of rapid screen changes
943   redraw_mask = REDRAW_ALL;
944   BackToFront();
945 }
946
947 void FadeOut(int fade_mask)
948 {
949   // update screen if areas covered by "fade_mask" and "redraw_mask" differ
950   if (!equalRedrawMasks(fade_mask, redraw_mask))
951     BackToFront();
952
953   SetScreenStates_BeforeFadingOut();
954
955   SetOverlayActive(FALSE);
956
957 #if 0
958   DrawMaskedBorder(REDRAW_ALL);
959 #endif
960
961   if (fading.fade_mode & FADE_TYPE_TRANSFORM)
962     FadeExt(fade_mask, fading.fade_mode, FADE_TYPE_FADE_OUT);
963   else
964     FadeExt(fade_mask, FADE_MODE_FADE_OUT, FADE_TYPE_FADE_OUT);
965
966   SetScreenStates_AfterFadingOut();
967 }
968
969 static void FadeSetLeaveNext(struct TitleFadingInfo fading_leave, boolean set)
970 {
971   static struct TitleFadingInfo fading_leave_stored;
972
973   if (set)
974     fading_leave_stored = fading_leave;
975   else
976     fading = fading_leave_stored;
977 }
978
979 void FadeSetEnterMenu()
980 {
981   fading = menu.enter_menu;
982
983   FadeSetLeaveNext(fading, TRUE);       /* (keep same fade mode) */
984 }
985
986 void FadeSetLeaveMenu()
987 {
988   fading = menu.leave_menu;
989
990   FadeSetLeaveNext(fading, TRUE);       /* (keep same fade mode) */
991 }
992
993 void FadeSetEnterScreen()
994 {
995   fading = menu.enter_screen[game_status];
996
997   FadeSetLeaveNext(menu.leave_screen[game_status], TRUE);       /* store */
998 }
999
1000 void FadeSetNextScreen()
1001 {
1002   fading = menu.next_screen[game_status];
1003
1004   // (do not overwrite fade mode set by FadeSetEnterScreen)
1005   // FadeSetLeaveNext(fading, TRUE);    /* (keep same fade mode) */
1006 }
1007
1008 void FadeSetLeaveScreen()
1009 {
1010   FadeSetLeaveNext(menu.leave_screen[game_status], FALSE);      /* recall */
1011 }
1012
1013 void FadeSetFromType(int type)
1014 {
1015   if (type & TYPE_ENTER_SCREEN)
1016     FadeSetEnterScreen();
1017   else if (type & TYPE_ENTER)
1018     FadeSetEnterMenu();
1019   else if (type & TYPE_LEAVE)
1020     FadeSetLeaveMenu();
1021 }
1022
1023 void FadeSetDisabled()
1024 {
1025   static struct TitleFadingInfo fading_none = { FADE_MODE_NONE, -1, -1, -1 };
1026
1027   fading = fading_none;
1028 }
1029
1030 void FadeSkipNextFadeIn()
1031 {
1032   FadeExt(0, FADE_MODE_SKIP_FADE_IN, FADE_TYPE_SKIP);
1033 }
1034
1035 void FadeSkipNextFadeOut()
1036 {
1037   FadeExt(0, FADE_MODE_SKIP_FADE_OUT, FADE_TYPE_SKIP);
1038 }
1039
1040 Bitmap *getBitmapFromGraphicOrDefault(int graphic, int default_graphic)
1041 {
1042   boolean redefined = getImageListEntryFromImageID(graphic)->redefined;
1043
1044   return (graphic == IMG_UNDEFINED ? NULL :
1045           graphic_info[graphic].bitmap != NULL || redefined ?
1046           graphic_info[graphic].bitmap :
1047           graphic_info[default_graphic].bitmap);
1048 }
1049
1050 Bitmap *getBackgroundBitmap(int graphic)
1051 {
1052   return getBitmapFromGraphicOrDefault(graphic, IMG_BACKGROUND);
1053 }
1054
1055 Bitmap *getGlobalBorderBitmap(int graphic)
1056 {
1057   return getBitmapFromGraphicOrDefault(graphic, IMG_GLOBAL_BORDER);
1058 }
1059
1060 Bitmap *getGlobalBorderBitmapFromStatus(int status)
1061 {
1062   int graphic =
1063     (status == GAME_MODE_MAIN ||
1064      status == GAME_MODE_PSEUDO_TYPENAME        ? IMG_GLOBAL_BORDER_MAIN :
1065      status == GAME_MODE_SCORES                 ? IMG_GLOBAL_BORDER_SCORES :
1066      status == GAME_MODE_EDITOR                 ? IMG_GLOBAL_BORDER_EDITOR :
1067      status == GAME_MODE_PLAYING                ? IMG_GLOBAL_BORDER_PLAYING :
1068      IMG_GLOBAL_BORDER);
1069
1070   return getGlobalBorderBitmap(graphic);
1071 }
1072
1073 void SetWindowBackgroundImageIfDefined(int graphic)
1074 {
1075   if (graphic_info[graphic].bitmap)
1076     SetWindowBackgroundBitmap(graphic_info[graphic].bitmap);
1077 }
1078
1079 void SetMainBackgroundImageIfDefined(int graphic)
1080 {
1081   if (graphic_info[graphic].bitmap)
1082     SetMainBackgroundBitmap(graphic_info[graphic].bitmap);
1083 }
1084
1085 void SetDoorBackgroundImageIfDefined(int graphic)
1086 {
1087   if (graphic_info[graphic].bitmap)
1088     SetDoorBackgroundBitmap(graphic_info[graphic].bitmap);
1089 }
1090
1091 void SetWindowBackgroundImage(int graphic)
1092 {
1093   SetWindowBackgroundBitmap(getBackgroundBitmap(graphic));
1094 }
1095
1096 void SetMainBackgroundImage(int graphic)
1097 {
1098   SetMainBackgroundBitmap(getBackgroundBitmap(graphic));
1099 }
1100
1101 void SetDoorBackgroundImage(int graphic)
1102 {
1103   SetDoorBackgroundBitmap(getBackgroundBitmap(graphic));
1104 }
1105
1106 void SetPanelBackground()
1107 {
1108   struct GraphicInfo *gfx = &graphic_info[IMG_BACKGROUND_PANEL];
1109
1110   BlitBitmapTiled(gfx->bitmap, bitmap_db_panel, gfx->src_x, gfx->src_y,
1111                   gfx->width, gfx->height, 0, 0, DXSIZE, DYSIZE);
1112
1113   SetDoorBackgroundBitmap(bitmap_db_panel);
1114 }
1115
1116 void DrawBackground(int x, int y, int width, int height)
1117 {
1118   /* "drawto" might still point to playfield buffer here (hall of fame) */
1119   ClearRectangleOnBackground(backbuffer, x, y, width, height);
1120
1121   if (IN_GFX_FIELD_FULL(x, y))
1122     redraw_mask |= REDRAW_FIELD;
1123   else if (IN_GFX_DOOR_1(x, y))
1124     redraw_mask |= REDRAW_DOOR_1;
1125   else if (IN_GFX_DOOR_2(x, y))
1126     redraw_mask |= REDRAW_DOOR_2;
1127   else if (IN_GFX_DOOR_3(x, y))
1128     redraw_mask |= REDRAW_DOOR_3;
1129 }
1130
1131 void DrawBackgroundForFont(int x, int y, int width, int height, int font_nr)
1132 {
1133   struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
1134
1135   if (font->bitmap == NULL)
1136     return;
1137
1138   DrawBackground(x, y, width, height);
1139 }
1140
1141 void DrawBackgroundForGraphic(int x, int y, int width, int height, int graphic)
1142 {
1143   struct GraphicInfo *g = &graphic_info[graphic];
1144
1145   if (g->bitmap == NULL)
1146     return;
1147
1148   DrawBackground(x, y, width, height);
1149 }
1150
1151 static int game_status_last = -1;
1152 static Bitmap *global_border_bitmap_last = NULL;
1153 static Bitmap *global_border_bitmap = NULL;
1154 static int real_sx_last = -1, real_sy_last = -1;
1155 static int full_sxsize_last = -1, full_sysize_last = -1;
1156 static int dx_last = -1, dy_last = -1;
1157 static int dxsize_last = -1, dysize_last = -1;
1158 static int vx_last = -1, vy_last = -1;
1159 static int vxsize_last = -1, vysize_last = -1;
1160
1161 boolean CheckIfGlobalBorderHasChanged()
1162 {
1163   // if game status has not changed, global border has not changed either
1164   if (game_status == game_status_last)
1165     return FALSE;
1166
1167   // determine and store new global border bitmap for current game status
1168   global_border_bitmap = getGlobalBorderBitmapFromStatus(game_status);
1169
1170   return (global_border_bitmap_last != global_border_bitmap);
1171 }
1172
1173 boolean CheckIfGlobalBorderRedrawIsNeeded()
1174 {
1175   // if game status has not changed, nothing has to be redrawn
1176   if (game_status == game_status_last)
1177     return FALSE;
1178
1179   // redraw if last screen was title screen
1180   if (game_status_last == GAME_MODE_TITLE)
1181     return TRUE;
1182
1183   // redraw if global screen border has changed
1184   if (CheckIfGlobalBorderHasChanged())
1185     return TRUE;
1186
1187   // redraw if position or size of playfield area has changed
1188   if (real_sx_last != REAL_SX || real_sy_last != REAL_SY ||
1189       full_sxsize_last != FULL_SXSIZE || full_sysize_last != FULL_SYSIZE)
1190     return TRUE;
1191
1192   // redraw if position or size of door area has changed
1193   if (dx_last != DX || dy_last != DY ||
1194       dxsize_last != DXSIZE || dysize_last != DYSIZE)
1195     return TRUE;
1196
1197   // redraw if position or size of tape area has changed
1198   if (vx_last != VX || vy_last != VY ||
1199       vxsize_last != VXSIZE || vysize_last != VYSIZE)
1200     return TRUE;
1201
1202   return FALSE;
1203 }
1204
1205 void RedrawGlobalBorderFromBitmap(Bitmap *bitmap)
1206 {
1207   if (bitmap)
1208     BlitBitmap(bitmap, backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
1209   else
1210     ClearRectangle(backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE);
1211 }
1212
1213 void RedrawGlobalBorder()
1214 {
1215   Bitmap *bitmap = getGlobalBorderBitmapFromStatus(game_status);
1216
1217   RedrawGlobalBorderFromBitmap(bitmap);
1218
1219   redraw_mask = REDRAW_ALL;
1220 }
1221
1222 static void RedrawGlobalBorderIfNeeded()
1223 {
1224   if (game_status == game_status_last)
1225     return;
1226
1227   // copy current draw buffer to later copy back areas that have not changed
1228   if (game_status_last != GAME_MODE_TITLE)
1229     BlitBitmap(backbuffer, bitmap_db_store_1, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
1230
1231   if (CheckIfGlobalBorderRedrawIsNeeded())
1232   {
1233     // redraw global screen border (or clear, if defined to be empty)
1234     RedrawGlobalBorderFromBitmap(global_border_bitmap);
1235
1236     // copy previous playfield and door areas, if they are defined on both
1237     // previous and current screen and if they still have the same size
1238
1239     if (real_sx_last != -1 && real_sy_last != -1 &&
1240         REAL_SX != -1 && REAL_SY != -1 &&
1241         full_sxsize_last == FULL_SXSIZE && full_sysize_last == FULL_SYSIZE)
1242       BlitBitmap(bitmap_db_store_1, backbuffer,
1243                  real_sx_last, real_sy_last, FULL_SXSIZE, FULL_SYSIZE,
1244                  REAL_SX, REAL_SY);
1245
1246     if (dx_last != -1 && dy_last != -1 &&
1247         DX != -1 && DY != -1 &&
1248         dxsize_last == DXSIZE && dysize_last == DYSIZE)
1249       BlitBitmap(bitmap_db_store_1, backbuffer,
1250                  dx_last, dy_last, DXSIZE, DYSIZE, DX, DY);
1251
1252     if (vx_last != -1 && vy_last != -1 &&
1253         VX != -1 && VY != -1 &&
1254         vxsize_last == VXSIZE && vysize_last == VYSIZE)
1255       BlitBitmap(bitmap_db_store_1, backbuffer,
1256                  vx_last, vy_last, VXSIZE, VYSIZE, VX, VY);
1257
1258     redraw_mask = REDRAW_ALL;
1259   }
1260
1261   game_status_last = game_status;
1262
1263   global_border_bitmap_last = global_border_bitmap;
1264
1265   real_sx_last = REAL_SX;
1266   real_sy_last = REAL_SY;
1267   full_sxsize_last = FULL_SXSIZE;
1268   full_sysize_last = FULL_SYSIZE;
1269   dx_last = DX;
1270   dy_last = DY;
1271   dxsize_last = DXSIZE;
1272   dysize_last = DYSIZE;
1273   vx_last = VX;
1274   vy_last = VY;
1275   vxsize_last = VXSIZE;
1276   vysize_last = VYSIZE;
1277 }
1278
1279 void ClearField()
1280 {
1281   RedrawGlobalBorderIfNeeded();
1282
1283   /* !!! "drawto" might still point to playfield buffer here (see above) !!! */
1284   /* (when entering hall of fame after playing) */
1285   DrawBackground(REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
1286
1287   /* !!! maybe this should be done before clearing the background !!! */
1288   if (game_status == GAME_MODE_PLAYING)
1289   {
1290     ClearRectangle(fieldbuffer, 0, 0, FXSIZE, FYSIZE);
1291     SetDrawtoField(DRAW_TO_FIELDBUFFER);
1292   }
1293   else
1294   {
1295     SetDrawtoField(DRAW_TO_BACKBUFFER);
1296   }
1297 }
1298
1299 void MarkTileDirty(int x, int y)
1300 {
1301   redraw_mask |= REDRAW_FIELD;
1302 }
1303
1304 void SetBorderElement()
1305 {
1306   int x, y;
1307
1308   BorderElement = EL_EMPTY;
1309
1310   for (y = 0; y < lev_fieldy && BorderElement == EL_EMPTY; y++)
1311   {
1312     for (x = 0; x < lev_fieldx; x++)
1313     {
1314       if (!IS_INDESTRUCTIBLE(Feld[x][y]))
1315         BorderElement = EL_STEELWALL;
1316
1317       if (y != 0 && y != lev_fieldy - 1 && x != lev_fieldx - 1)
1318         x = lev_fieldx - 2;
1319     }
1320   }
1321 }
1322
1323 void FloodFillLevel(int from_x, int from_y, int fill_element,
1324                     short field[MAX_LEV_FIELDX][MAX_LEV_FIELDY],
1325                     int max_fieldx, int max_fieldy)
1326 {
1327   int i,x,y;
1328   int old_element;
1329   static int check[4][2] = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };
1330   static int safety = 0;
1331
1332   /* check if starting field still has the desired content */
1333   if (field[from_x][from_y] == fill_element)
1334     return;
1335
1336   safety++;
1337
1338   if (safety > max_fieldx * max_fieldy)
1339     Error(ERR_EXIT, "Something went wrong in 'FloodFill()'. Please debug.");
1340
1341   old_element = field[from_x][from_y];
1342   field[from_x][from_y] = fill_element;
1343
1344   for (i = 0; i < 4; i++)
1345   {
1346     x = from_x + check[i][0];
1347     y = from_y + check[i][1];
1348
1349     if (IN_FIELD(x, y, max_fieldx, max_fieldy) && field[x][y] == old_element)
1350       FloodFillLevel(x, y, fill_element, field, max_fieldx, max_fieldy);
1351   }
1352
1353   safety--;
1354 }
1355
1356 void SetRandomAnimationValue(int x, int y)
1357 {
1358   gfx.anim_random_frame = GfxRandom[x][y];
1359 }
1360
1361 int getGraphicAnimationFrame(int graphic, int sync_frame)
1362 {
1363   /* animation synchronized with global frame counter, not move position */
1364   if (graphic_info[graphic].anim_global_sync || sync_frame < 0)
1365     sync_frame = FrameCounter;
1366
1367   return getAnimationFrame(graphic_info[graphic].anim_frames,
1368                            graphic_info[graphic].anim_delay,
1369                            graphic_info[graphic].anim_mode,
1370                            graphic_info[graphic].anim_start_frame,
1371                            sync_frame);
1372 }
1373
1374 void getGraphicSourceBitmap(int graphic, int tilesize, Bitmap **bitmap)
1375 {
1376   struct GraphicInfo *g = &graphic_info[graphic];
1377   int tilesize_capped = MIN(MAX(1, tilesize), TILESIZE);
1378
1379   if (tilesize == gfx.standard_tile_size)
1380     *bitmap = g->bitmaps[IMG_BITMAP_STANDARD];
1381   else if (tilesize == game.tile_size)
1382     *bitmap = g->bitmaps[IMG_BITMAP_GAME];
1383   else
1384     *bitmap = g->bitmaps[IMG_BITMAP_1x1 - log_2(tilesize_capped)];
1385 }
1386
1387 void getGraphicSourceXY(int graphic, int frame, int *x, int *y,
1388                         boolean get_backside)
1389 {
1390   struct GraphicInfo *g = &graphic_info[graphic];
1391   int src_x = g->src_x + (get_backside ? g->offset2_x : 0);
1392   int src_y = g->src_y + (get_backside ? g->offset2_y : 0);
1393
1394   if (g->offset_y == 0)         /* frames are ordered horizontally */
1395   {
1396     int max_width = g->anim_frames_per_line * g->width;
1397     int pos = (src_y / g->height) * max_width + src_x + frame * g->offset_x;
1398
1399     *x = pos % max_width;
1400     *y = src_y % g->height + pos / max_width * g->height;
1401   }
1402   else if (g->offset_x == 0)    /* frames are ordered vertically */
1403   {
1404     int max_height = g->anim_frames_per_line * g->height;
1405     int pos = (src_x / g->width) * max_height + src_y + frame * g->offset_y;
1406
1407     *x = src_x % g->width + pos / max_height * g->width;
1408     *y = pos % max_height;
1409   }
1410   else                          /* frames are ordered diagonally */
1411   {
1412     *x = src_x + frame * g->offset_x;
1413     *y = src_y + frame * g->offset_y;
1414   }
1415 }
1416
1417 void getSizedGraphicSourceExt(int graphic, int frame, int tilesize,
1418                               Bitmap **bitmap, int *x, int *y,
1419                               boolean get_backside)
1420 {
1421   struct GraphicInfo *g = &graphic_info[graphic];
1422
1423   // if no in-game graphics defined, always use standard graphic size
1424   if (g->bitmaps[IMG_BITMAP_GAME] == NULL)
1425     tilesize = TILESIZE;
1426
1427   getGraphicSourceBitmap(graphic, tilesize, bitmap);
1428   getGraphicSourceXY(graphic, frame, x, y, get_backside);
1429
1430   *x = *x * tilesize / g->tile_size;
1431   *y = *y * tilesize / g->tile_size;
1432 }
1433
1434 void getFixedGraphicSourceExt(int graphic, int frame, Bitmap **bitmap,
1435                               int *x, int *y, boolean get_backside)
1436 {
1437   getSizedGraphicSourceExt(graphic, frame, TILESIZE, bitmap, x, y,
1438                            get_backside);
1439 }
1440
1441 void getSizedGraphicSource(int graphic, int frame, int tilesize,
1442                            Bitmap **bitmap, int *x, int *y)
1443 {
1444   getSizedGraphicSourceExt(graphic, frame, tilesize, bitmap, x, y, FALSE);
1445 }
1446
1447 void getFixedGraphicSource(int graphic, int frame,
1448                            Bitmap **bitmap, int *x, int *y)
1449 {
1450   getSizedGraphicSourceExt(graphic, frame, TILESIZE, bitmap, x, y, FALSE);
1451 }
1452
1453 void getMiniGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y)
1454 {
1455   getSizedGraphicSource(graphic, 0, MINI_TILESIZE, bitmap, x, y);
1456 }
1457
1458 inline static void getGraphicSourceExt(int graphic, int frame, Bitmap **bitmap,
1459                                        int *x, int *y, boolean get_backside)
1460 {
1461   getSizedGraphicSourceExt(graphic, frame, TILESIZE_VAR, bitmap, x, y,
1462                            get_backside);
1463 }
1464
1465 void getGraphicSource(int graphic, int frame, Bitmap **bitmap, int *x, int *y)
1466 {
1467   getGraphicSourceExt(graphic, frame, bitmap, x, y, FALSE);
1468 }
1469
1470 void DrawGraphic(int x, int y, int graphic, int frame)
1471 {
1472 #if DEBUG
1473   if (!IN_SCR_FIELD(x, y))
1474   {
1475     printf("DrawGraphic(): x = %d, y = %d, graphic = %d\n", x, y, graphic);
1476     printf("DrawGraphic(): This should never happen!\n");
1477     return;
1478   }
1479 #endif
1480
1481   DrawGraphicExt(drawto_field, FX + x * TILEX_VAR, FY + y * TILEY_VAR, graphic,
1482                  frame);
1483
1484   MarkTileDirty(x, y);
1485 }
1486
1487 void DrawFixedGraphic(int x, int y, int graphic, int frame)
1488 {
1489 #if DEBUG
1490   if (!IN_SCR_FIELD(x, y))
1491   {
1492     printf("DrawGraphic(): x = %d, y = %d, graphic = %d\n", x, y, graphic);
1493     printf("DrawGraphic(): This should never happen!\n");
1494     return;
1495   }
1496 #endif
1497
1498   DrawFixedGraphicExt(drawto_field, FX + x * TILEX, FY + y * TILEY, graphic,
1499                       frame);
1500   MarkTileDirty(x, y);
1501 }
1502
1503 void DrawGraphicExt(DrawBuffer *dst_bitmap, int x, int y, int graphic,
1504                     int frame)
1505 {
1506   Bitmap *src_bitmap;
1507   int src_x, src_y;
1508
1509   getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
1510
1511   BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, TILEX_VAR, TILEY_VAR, x, y);
1512 }
1513
1514 void DrawFixedGraphicExt(DrawBuffer *dst_bitmap, int x, int y, int graphic,
1515                          int frame)
1516 {
1517   Bitmap *src_bitmap;
1518   int src_x, src_y;
1519
1520   getFixedGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
1521   BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, TILEX, TILEY, x, y);
1522 }
1523
1524 void DrawGraphicThruMask(int x, int y, int graphic, int frame)
1525 {
1526 #if DEBUG
1527   if (!IN_SCR_FIELD(x, y))
1528   {
1529     printf("DrawGraphicThruMask(): x = %d,y = %d, graphic = %d\n",x,y,graphic);
1530     printf("DrawGraphicThruMask(): This should never happen!\n");
1531     return;
1532   }
1533 #endif
1534
1535   DrawGraphicThruMaskExt(drawto_field, FX + x * TILEX_VAR, FY + y * TILEY_VAR,
1536                          graphic, frame);
1537
1538   MarkTileDirty(x, y);
1539 }
1540
1541 void DrawFixedGraphicThruMask(int x, int y, int graphic, int frame)
1542 {
1543 #if DEBUG
1544   if (!IN_SCR_FIELD(x, y))
1545   {
1546     printf("DrawGraphicThruMask(): x = %d,y = %d, graphic = %d\n",x,y,graphic);
1547     printf("DrawGraphicThruMask(): This should never happen!\n");
1548     return;
1549   }
1550 #endif
1551
1552   DrawFixedGraphicThruMaskExt(drawto_field, FX + x * TILEX, FY + y * TILEY,
1553                               graphic, frame);
1554   MarkTileDirty(x, y);
1555 }
1556
1557 void DrawGraphicThruMaskExt(DrawBuffer *d, int dst_x, int dst_y, int graphic,
1558                             int frame)
1559 {
1560   Bitmap *src_bitmap;
1561   int src_x, src_y;
1562
1563   getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
1564
1565   BlitBitmapMasked(src_bitmap, d, src_x, src_y, TILEX_VAR, TILEY_VAR,
1566                    dst_x, dst_y);
1567 }
1568
1569 void DrawFixedGraphicThruMaskExt(DrawBuffer *d, int dst_x, int dst_y,
1570                                  int graphic, int frame)
1571 {
1572   Bitmap *src_bitmap;
1573   int src_x, src_y;
1574
1575   getFixedGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
1576
1577   BlitBitmapMasked(src_bitmap, d, src_x, src_y, TILEX, TILEY,
1578                    dst_x, dst_y);
1579 }
1580
1581 void DrawSizedGraphic(int x, int y, int graphic, int frame, int tilesize)
1582 {
1583   DrawSizedGraphicExt(drawto, SX + x * tilesize, SY + y * tilesize, graphic,
1584                       frame, tilesize);
1585   MarkTileDirty(x / tilesize, y / tilesize);
1586 }
1587
1588 void DrawSizedGraphicExt(DrawBuffer *d, int x, int y, int graphic, int frame,
1589                          int tilesize)
1590 {
1591   Bitmap *src_bitmap;
1592   int src_x, src_y;
1593
1594   getSizedGraphicSource(graphic, frame, tilesize, &src_bitmap, &src_x, &src_y);
1595   BlitBitmap(src_bitmap, d, src_x, src_y, tilesize, tilesize, x, y);
1596 }
1597
1598 void DrawMiniGraphic(int x, int y, int graphic)
1599 {
1600   DrawMiniGraphicExt(drawto, SX + x * MINI_TILEX,SY + y * MINI_TILEY, graphic);
1601   MarkTileDirty(x / 2, y / 2);
1602 }
1603
1604 void DrawMiniGraphicExt(DrawBuffer *d, int x, int y, int graphic)
1605 {
1606   Bitmap *src_bitmap;
1607   int src_x, src_y;
1608
1609   getMiniGraphicSource(graphic, &src_bitmap, &src_x, &src_y);
1610   BlitBitmap(src_bitmap, d, src_x, src_y, MINI_TILEX, MINI_TILEY, x, y);
1611 }
1612
1613 inline static void DrawGraphicShiftedNormal(int x, int y, int dx, int dy,
1614                                             int graphic, int frame,
1615                                             int cut_mode, int mask_mode)
1616 {
1617   Bitmap *src_bitmap;
1618   int src_x, src_y;
1619   int dst_x, dst_y;
1620   int width = TILEX, height = TILEY;
1621   int cx = 0, cy = 0;
1622
1623   if (dx || dy)                 /* shifted graphic */
1624   {
1625     if (x < BX1)                /* object enters playfield from the left */
1626     {
1627       x = BX1;
1628       width = dx;
1629       cx = TILEX - dx;
1630       dx = 0;
1631     }
1632     else if (x > BX2)           /* object enters playfield from the right */
1633     {
1634       x = BX2;
1635       width = -dx;
1636       dx = TILEX + dx;
1637     }
1638     else if (x == BX1 && dx < 0) /* object leaves playfield to the left */
1639     {
1640       width += dx;
1641       cx = -dx;
1642       dx = 0;
1643     }
1644     else if (x == BX2 && dx > 0) /* object leaves playfield to the right */
1645       width -= dx;
1646     else if (dx)                /* general horizontal movement */
1647       MarkTileDirty(x + SIGN(dx), y);
1648
1649     if (y < BY1)                /* object enters playfield from the top */
1650     {
1651       if (cut_mode == CUT_BELOW) /* object completely above top border */
1652         return;
1653
1654       y = BY1;
1655       height = dy;
1656       cy = TILEY - dy;
1657       dy = 0;
1658     }
1659     else if (y > BY2)           /* object enters playfield from the bottom */
1660     {
1661       y = BY2;
1662       height = -dy;
1663       dy = TILEY + dy;
1664     }
1665     else if (y == BY1 && dy < 0) /* object leaves playfield to the top */
1666     {
1667       height += dy;
1668       cy = -dy;
1669       dy = 0;
1670     }
1671     else if (dy > 0 && cut_mode == CUT_ABOVE)
1672     {
1673       if (y == BY2)             /* object completely above bottom border */
1674         return;
1675
1676       height = dy;
1677       cy = TILEY - dy;
1678       dy = TILEY;
1679       MarkTileDirty(x, y + 1);
1680     }                           /* object leaves playfield to the bottom */
1681     else if (dy > 0 && (y == BY2 || cut_mode == CUT_BELOW))
1682       height -= dy;
1683     else if (dy)                /* general vertical movement */
1684       MarkTileDirty(x, y + SIGN(dy));
1685   }
1686
1687 #if DEBUG
1688   if (!IN_SCR_FIELD(x, y))
1689   {
1690     printf("DrawGraphicShifted(): x = %d, y = %d, graphic = %d\n",x,y,graphic);
1691     printf("DrawGraphicShifted(): This should never happen!\n");
1692     return;
1693   }
1694 #endif
1695
1696   width = width * TILESIZE_VAR / TILESIZE;
1697   height = height * TILESIZE_VAR / TILESIZE;
1698   cx = cx * TILESIZE_VAR / TILESIZE;
1699   cy = cy * TILESIZE_VAR / TILESIZE;
1700   dx = dx * TILESIZE_VAR / TILESIZE;
1701   dy = dy * TILESIZE_VAR / TILESIZE;
1702
1703   if (width > 0 && height > 0)
1704   {
1705     getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
1706
1707     src_x += cx;
1708     src_y += cy;
1709
1710     dst_x = FX + x * TILEX_VAR + dx;
1711     dst_y = FY + y * TILEY_VAR + dy;
1712
1713     if (mask_mode == USE_MASKING)
1714       BlitBitmapMasked(src_bitmap, drawto_field, src_x, src_y, width, height,
1715                        dst_x, dst_y);
1716     else
1717       BlitBitmap(src_bitmap, drawto_field, src_x, src_y, width, height,
1718                  dst_x, dst_y);
1719
1720     MarkTileDirty(x, y);
1721   }
1722 }
1723
1724 inline static void DrawGraphicShiftedDouble(int x, int y, int dx, int dy,
1725                                             int graphic, int frame,
1726                                             int cut_mode, int mask_mode)
1727 {
1728   Bitmap *src_bitmap;
1729   int src_x, src_y;
1730   int dst_x, dst_y;
1731   int width = TILEX_VAR, height = TILEY_VAR;
1732   int x1 = x;
1733   int y1 = y;
1734   int x2 = x + SIGN(dx);
1735   int y2 = y + SIGN(dy);
1736
1737   /* movement with two-tile animations must be sync'ed with movement position,
1738      not with current GfxFrame (which can be higher when using slow movement) */
1739   int anim_pos = (dx ? ABS(dx) : ABS(dy));
1740   int anim_frames = graphic_info[graphic].anim_frames;
1741
1742   /* (we also need anim_delay here for movement animations with less frames) */
1743   int anim_delay = graphic_info[graphic].anim_delay;
1744   int sync_frame = anim_pos * anim_frames * anim_delay / TILESIZE;
1745
1746   boolean draw_start_tile = (cut_mode != CUT_ABOVE);    /* only for falling! */
1747   boolean draw_end_tile   = (cut_mode != CUT_BELOW);    /* only for falling! */
1748
1749   /* re-calculate animation frame for two-tile movement animation */
1750   frame = getGraphicAnimationFrame(graphic, sync_frame);
1751
1752   /* check if movement start graphic inside screen area and should be drawn */
1753   if (draw_start_tile && IN_SCR_FIELD(x1, y1))
1754   {
1755     getGraphicSourceExt(graphic, frame, &src_bitmap, &src_x, &src_y, TRUE);
1756
1757     dst_x = FX + x1 * TILEX_VAR;
1758     dst_y = FY + y1 * TILEY_VAR;
1759
1760     if (mask_mode == USE_MASKING)
1761       BlitBitmapMasked(src_bitmap, drawto_field, src_x, src_y, width, height,
1762                        dst_x, dst_y);
1763     else
1764       BlitBitmap(src_bitmap, drawto_field, src_x, src_y, width, height,
1765                  dst_x, dst_y);
1766
1767     MarkTileDirty(x1, y1);
1768   }
1769
1770   /* check if movement end graphic inside screen area and should be drawn */
1771   if (draw_end_tile && IN_SCR_FIELD(x2, y2))
1772   {
1773     getGraphicSourceExt(graphic, frame, &src_bitmap, &src_x, &src_y, FALSE);
1774
1775     dst_x = FX + x2 * TILEX_VAR;
1776     dst_y = FY + y2 * TILEY_VAR;
1777
1778     if (mask_mode == USE_MASKING)
1779       BlitBitmapMasked(src_bitmap, drawto_field, src_x, src_y, width, height,
1780                        dst_x, dst_y);
1781     else
1782       BlitBitmap(src_bitmap, drawto_field, src_x, src_y, width, height,
1783                  dst_x, dst_y);
1784
1785     MarkTileDirty(x2, y2);
1786   }
1787 }
1788
1789 static void DrawGraphicShifted(int x, int y, int dx, int dy,
1790                                int graphic, int frame,
1791                                int cut_mode, int mask_mode)
1792 {
1793   if (graphic < 0)
1794   {
1795     DrawGraphic(x, y, graphic, frame);
1796
1797     return;
1798   }
1799
1800   if (graphic_info[graphic].double_movement)    /* EM style movement images */
1801     DrawGraphicShiftedDouble(x, y, dx, dy, graphic, frame, cut_mode,mask_mode);
1802   else
1803     DrawGraphicShiftedNormal(x, y, dx, dy, graphic, frame, cut_mode,mask_mode);
1804 }
1805
1806 void DrawGraphicShiftedThruMask(int x, int y, int dx, int dy, int graphic,
1807                                 int frame, int cut_mode)
1808 {
1809   DrawGraphicShifted(x, y, dx, dy, graphic, frame, cut_mode, USE_MASKING);
1810 }
1811
1812 void DrawScreenElementExt(int x, int y, int dx, int dy, int element,
1813                           int cut_mode, int mask_mode)
1814 {
1815   int lx = LEVELX(x), ly = LEVELY(y);
1816   int graphic;
1817   int frame;
1818
1819   if (IN_LEV_FIELD(lx, ly))
1820   {
1821     SetRandomAnimationValue(lx, ly);
1822
1823     graphic = el_act_dir2img(element, GfxAction[lx][ly], GfxDir[lx][ly]);
1824     frame = getGraphicAnimationFrame(graphic, GfxFrame[lx][ly]);
1825
1826     /* do not use double (EM style) movement graphic when not moving */
1827     if (graphic_info[graphic].double_movement && !dx && !dy)
1828     {
1829       graphic = el_act_dir2img(element, ACTION_DEFAULT, GfxDir[lx][ly]);
1830       frame = getGraphicAnimationFrame(graphic, GfxFrame[lx][ly]);
1831     }
1832   }
1833   else  /* border element */
1834   {
1835     graphic = el2img(element);
1836     frame = getGraphicAnimationFrame(graphic, -1);
1837   }
1838
1839   if (element == EL_EXPANDABLE_WALL)
1840   {
1841     boolean left_stopped = FALSE, right_stopped = FALSE;
1842
1843     if (!IN_LEV_FIELD(lx - 1, ly) || IS_WALL(Feld[lx - 1][ly]))
1844       left_stopped = TRUE;
1845     if (!IN_LEV_FIELD(lx + 1, ly) || IS_WALL(Feld[lx + 1][ly]))
1846       right_stopped = TRUE;
1847
1848     if (left_stopped && right_stopped)
1849       graphic = IMG_WALL;
1850     else if (left_stopped)
1851     {
1852       graphic = IMG_EXPANDABLE_WALL_GROWING_RIGHT;
1853       frame = graphic_info[graphic].anim_frames - 1;
1854     }
1855     else if (right_stopped)
1856     {
1857       graphic = IMG_EXPANDABLE_WALL_GROWING_LEFT;
1858       frame = graphic_info[graphic].anim_frames - 1;
1859     }
1860   }
1861
1862   if (dx || dy)
1863     DrawGraphicShifted(x, y, dx, dy, graphic, frame, cut_mode, mask_mode);
1864   else if (mask_mode == USE_MASKING)
1865     DrawGraphicThruMask(x, y, graphic, frame);
1866   else
1867     DrawGraphic(x, y, graphic, frame);
1868 }
1869
1870 void DrawLevelElementExt(int x, int y, int dx, int dy, int element,
1871                          int cut_mode, int mask_mode)
1872 {
1873   if (IN_LEV_FIELD(x, y) && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
1874     DrawScreenElementExt(SCREENX(x), SCREENY(y), dx, dy, element,
1875                          cut_mode, mask_mode);
1876 }
1877
1878 void DrawScreenElementShifted(int x, int y, int dx, int dy, int element,
1879                               int cut_mode)
1880 {
1881   DrawScreenElementExt(x, y, dx, dy, element, cut_mode, NO_MASKING);
1882 }
1883
1884 void DrawLevelElementShifted(int x, int y, int dx, int dy, int element,
1885                              int cut_mode)
1886 {
1887   DrawLevelElementExt(x, y, dx, dy, element, cut_mode, NO_MASKING);
1888 }
1889
1890 void DrawLevelElementThruMask(int x, int y, int element)
1891 {
1892   DrawLevelElementExt(x, y, 0, 0, element, NO_CUTTING, USE_MASKING);
1893 }
1894
1895 void DrawLevelFieldThruMask(int x, int y)
1896 {
1897   DrawLevelElementExt(x, y, 0, 0, Feld[x][y], NO_CUTTING, USE_MASKING);
1898 }
1899
1900 /* !!! implementation of quicksand is totally broken !!! */
1901 #define IS_CRUMBLED_TILE(x, y, e)                                       \
1902         (GFX_CRUMBLED(e) && (!IN_LEV_FIELD(x, y) ||                     \
1903                              !IS_MOVING(x, y) ||                        \
1904                              (e) == EL_QUICKSAND_EMPTYING ||            \
1905                              (e) == EL_QUICKSAND_FAST_EMPTYING))
1906
1907 static void DrawLevelFieldCrumbledInnerCorners(int x, int y, int dx, int dy,
1908                                                int graphic)
1909 {
1910   Bitmap *src_bitmap;
1911   int src_x, src_y;
1912   int width, height, cx, cy;
1913   int sx = SCREENX(x), sy = SCREENY(y);
1914   int crumbled_border_size = graphic_info[graphic].border_size;
1915   int crumbled_tile_size = graphic_info[graphic].tile_size;
1916   int crumbled_border_size_var =
1917     crumbled_border_size * TILESIZE_VAR / crumbled_tile_size;
1918   int i;
1919
1920   getGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
1921
1922   for (i = 1; i < 4; i++)
1923   {
1924     int dxx = (i & 1 ? dx : 0);
1925     int dyy = (i & 2 ? dy : 0);
1926     int xx = x + dxx;
1927     int yy = y + dyy;
1928     int element = (IN_LEV_FIELD(xx, yy) ? TILE_GFX_ELEMENT(xx, yy) :
1929                    BorderElement);
1930
1931     /* check if neighbour field is of same crumble type */
1932     boolean same = (IS_CRUMBLED_TILE(xx, yy, element) &&
1933                     graphic_info[graphic].class ==
1934                     graphic_info[el_act2crm(element, ACTION_DEFAULT)].class);
1935
1936     /* return if check prevents inner corner */
1937     if (same == (dxx == dx && dyy == dy))
1938       return;
1939   }
1940
1941   /* if we reach this point, we have an inner corner */
1942
1943   getGraphicSource(graphic, 1, &src_bitmap, &src_x, &src_y);
1944
1945   width  = crumbled_border_size_var;
1946   height = crumbled_border_size_var;
1947   cx = (dx > 0 ? TILESIZE_VAR - width  : 0);
1948   cy = (dy > 0 ? TILESIZE_VAR - height : 0);
1949
1950   BlitBitmap(src_bitmap, drawto_field, src_x + cx, src_y + cy,
1951              width, height, FX + sx * TILEX_VAR + cx, FY + sy * TILEY_VAR + cy);
1952 }
1953
1954 static void DrawLevelFieldCrumbledBorders(int x, int y, int graphic, int frame,
1955                                           int dir)
1956 {
1957   Bitmap *src_bitmap;
1958   int src_x, src_y;
1959   int width, height, bx, by, cx, cy;
1960   int sx = SCREENX(x), sy = SCREENY(y);
1961   int crumbled_border_size = graphic_info[graphic].border_size;
1962   int crumbled_tile_size = graphic_info[graphic].tile_size;
1963   int crumbled_border_size_var =
1964     crumbled_border_size * TILESIZE_VAR / crumbled_tile_size;
1965   int crumbled_border_pos_var = TILESIZE_VAR - crumbled_border_size_var;
1966   int i;
1967
1968   getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
1969
1970   /* draw simple, sloppy, non-corner-accurate crumbled border */
1971
1972   width  = (dir == 1 || dir == 2 ? crumbled_border_size_var : TILESIZE_VAR);
1973   height = (dir == 0 || dir == 3 ? crumbled_border_size_var : TILESIZE_VAR);
1974   cx = (dir == 2 ? crumbled_border_pos_var : 0);
1975   cy = (dir == 3 ? crumbled_border_pos_var : 0);
1976
1977   BlitBitmap(src_bitmap, drawto_field, src_x + cx, src_y + cy, width, height,
1978              FX + sx * TILEX_VAR + cx,
1979              FY + sy * TILEY_VAR + cy);
1980
1981   /* (remaining middle border part must be at least as big as corner part) */
1982   if (!(graphic_info[graphic].style & STYLE_ACCURATE_BORDERS) ||
1983       crumbled_border_size_var >= TILESIZE_VAR / 3)
1984     return;
1985
1986   /* correct corners of crumbled border, if needed */
1987
1988   for (i = -1; i <= 1; i += 2)
1989   {
1990     int xx = x + (dir == 0 || dir == 3 ? i : 0);
1991     int yy = y + (dir == 1 || dir == 2 ? i : 0);
1992     int element = (IN_LEV_FIELD(xx, yy) ? TILE_GFX_ELEMENT(xx, yy) :
1993                    BorderElement);
1994
1995     /* check if neighbour field is of same crumble type */
1996     if (IS_CRUMBLED_TILE(xx, yy, element) &&
1997         graphic_info[graphic].class ==
1998         graphic_info[el_act2crm(element, ACTION_DEFAULT)].class)
1999     {
2000       /* no crumbled corner, but continued crumbled border */
2001
2002       int c1 = (dir == 2 || dir == 3 ? crumbled_border_pos_var : 0);
2003       int c2 = (i == 1 ? crumbled_border_pos_var : 0);
2004       int b1 = (i == 1 ? crumbled_border_size_var :
2005                 TILESIZE_VAR - 2 * crumbled_border_size_var);
2006
2007       width  = crumbled_border_size_var;
2008       height = crumbled_border_size_var;
2009
2010       if (dir == 1 || dir == 2)
2011       {
2012         cx = c1;
2013         cy = c2;
2014         bx = cx;
2015         by = b1;
2016       }
2017       else
2018       {
2019         cx = c2;
2020         cy = c1;
2021         bx = b1;
2022         by = cy;
2023       }
2024
2025       BlitBitmap(src_bitmap, drawto_field, src_x + bx, src_y + by,
2026                  width, height,
2027                  FX + sx * TILEX_VAR + cx,
2028                  FY + sy * TILEY_VAR + cy);
2029     }
2030   }
2031 }
2032
2033 static void DrawLevelFieldCrumbledExt(int x, int y, int graphic, int frame)
2034 {
2035   int sx = SCREENX(x), sy = SCREENY(y);
2036   int element;
2037   int i;
2038   static int xy[4][2] =
2039   {
2040     { 0, -1 },
2041     { -1, 0 },
2042     { +1, 0 },
2043     { 0, +1 }
2044   };
2045
2046   if (!IN_LEV_FIELD(x, y))
2047     return;
2048
2049   element = TILE_GFX_ELEMENT(x, y);
2050
2051   if (IS_CRUMBLED_TILE(x, y, element))          /* crumble field itself */
2052   {
2053     if (!IN_SCR_FIELD(sx, sy))
2054       return;
2055
2056     /* crumble field borders towards direct neighbour fields */
2057     for (i = 0; i < 4; i++)
2058     {
2059       int xx = x + xy[i][0];
2060       int yy = y + xy[i][1];
2061
2062       element = (IN_LEV_FIELD(xx, yy) ? TILE_GFX_ELEMENT(xx, yy) :
2063                  BorderElement);
2064
2065       /* check if neighbour field is of same crumble type */
2066       if (IS_CRUMBLED_TILE(xx, yy, element) &&
2067           graphic_info[graphic].class ==
2068           graphic_info[el_act2crm(element, ACTION_DEFAULT)].class)
2069         continue;
2070
2071       DrawLevelFieldCrumbledBorders(x, y, graphic, frame, i);
2072     }
2073
2074     /* crumble inner field corners towards corner neighbour fields */
2075     if ((graphic_info[graphic].style & STYLE_INNER_CORNERS) &&
2076         graphic_info[graphic].anim_frames == 2)
2077     {
2078       for (i = 0; i < 4; i++)
2079       {
2080         int dx = (i & 1 ? +1 : -1);
2081         int dy = (i & 2 ? +1 : -1);
2082
2083         DrawLevelFieldCrumbledInnerCorners(x, y, dx, dy, graphic);
2084       }
2085     }
2086
2087     MarkTileDirty(sx, sy);
2088   }
2089   else          /* center field is not crumbled -- crumble neighbour fields */
2090   {
2091     /* crumble field borders of direct neighbour fields */
2092     for (i = 0; i < 4; i++)
2093     {
2094       int xx = x + xy[i][0];
2095       int yy = y + xy[i][1];
2096       int sxx = sx + xy[i][0];
2097       int syy = sy + xy[i][1];
2098
2099       if (!IN_LEV_FIELD(xx, yy) ||
2100           !IN_SCR_FIELD(sxx, syy))
2101         continue;
2102
2103       if (Feld[xx][yy] == EL_ELEMENT_SNAPPING)
2104         continue;
2105
2106       element = TILE_GFX_ELEMENT(xx, yy);
2107
2108       if (!IS_CRUMBLED_TILE(xx, yy, element))
2109         continue;
2110
2111       graphic = el_act2crm(element, ACTION_DEFAULT);
2112
2113       DrawLevelFieldCrumbledBorders(xx, yy, graphic, 0, 3 - i);
2114
2115       MarkTileDirty(sxx, syy);
2116     }
2117
2118     /* crumble inner field corners of corner neighbour fields */
2119     for (i = 0; i < 4; i++)
2120     {
2121       int dx = (i & 1 ? +1 : -1);
2122       int dy = (i & 2 ? +1 : -1);
2123       int xx = x + dx;
2124       int yy = y + dy;
2125       int sxx = sx + dx;
2126       int syy = sy + dy;
2127
2128       if (!IN_LEV_FIELD(xx, yy) ||
2129           !IN_SCR_FIELD(sxx, syy))
2130         continue;
2131
2132       if (Feld[xx][yy] == EL_ELEMENT_SNAPPING)
2133         continue;
2134
2135       element = TILE_GFX_ELEMENT(xx, yy);
2136
2137       if (!IS_CRUMBLED_TILE(xx, yy, element))
2138         continue;
2139
2140       graphic = el_act2crm(element, ACTION_DEFAULT);
2141
2142       if ((graphic_info[graphic].style & STYLE_INNER_CORNERS) &&
2143           graphic_info[graphic].anim_frames == 2)
2144         DrawLevelFieldCrumbledInnerCorners(xx, yy, -dx, -dy, graphic);
2145
2146       MarkTileDirty(sxx, syy);
2147     }
2148   }
2149 }
2150
2151 void DrawLevelFieldCrumbled(int x, int y)
2152 {
2153   int graphic;
2154
2155   if (!IN_LEV_FIELD(x, y))
2156     return;
2157
2158   if (Feld[x][y] == EL_ELEMENT_SNAPPING &&
2159       GfxElement[x][y] != EL_UNDEFINED &&
2160       GFX_CRUMBLED(GfxElement[x][y]))
2161   {
2162     DrawLevelFieldCrumbledDigging(x, y, GfxDir[x][y], GfxFrame[x][y]);
2163
2164     return;
2165   }
2166
2167   graphic = el_act2crm(TILE_GFX_ELEMENT(x, y), ACTION_DEFAULT);
2168
2169   DrawLevelFieldCrumbledExt(x, y, graphic, 0);
2170 }
2171
2172 void DrawLevelFieldCrumbledDigging(int x, int y, int direction,
2173                                    int step_frame)
2174 {
2175   int graphic1 = el_act_dir2img(GfxElement[x][y], ACTION_DIGGING, direction);
2176   int graphic2 = el_act_dir2crm(GfxElement[x][y], ACTION_DIGGING, direction);
2177   int frame1 = getGraphicAnimationFrame(graphic1, step_frame);
2178   int frame2 = getGraphicAnimationFrame(graphic2, step_frame);
2179   int sx = SCREENX(x), sy = SCREENY(y);
2180
2181   DrawGraphic(sx, sy, graphic1, frame1);
2182   DrawLevelFieldCrumbledExt(x, y, graphic2, frame2);
2183 }
2184
2185 void DrawLevelFieldCrumbledNeighbours(int x, int y)
2186 {
2187   int sx = SCREENX(x), sy = SCREENY(y);
2188   static int xy[4][2] =
2189   {
2190     { 0, -1 },
2191     { -1, 0 },
2192     { +1, 0 },
2193     { 0, +1 }
2194   };
2195   int i;
2196
2197   /* crumble direct neighbour fields (required for field borders) */
2198   for (i = 0; i < 4; i++)
2199   {
2200     int xx = x + xy[i][0];
2201     int yy = y + xy[i][1];
2202     int sxx = sx + xy[i][0];
2203     int syy = sy + xy[i][1];
2204
2205     if (!IN_LEV_FIELD(xx, yy) ||
2206         !IN_SCR_FIELD(sxx, syy) ||
2207         !GFX_CRUMBLED(Feld[xx][yy]) ||
2208         IS_MOVING(xx, yy))
2209       continue;
2210
2211     DrawLevelField(xx, yy);
2212   }
2213
2214   /* crumble corner neighbour fields (required for inner field corners) */
2215   for (i = 0; i < 4; i++)
2216   {
2217     int dx = (i & 1 ? +1 : -1);
2218     int dy = (i & 2 ? +1 : -1);
2219     int xx = x + dx;
2220     int yy = y + dy;
2221     int sxx = sx + dx;
2222     int syy = sy + dy;
2223
2224     if (!IN_LEV_FIELD(xx, yy) ||
2225         !IN_SCR_FIELD(sxx, syy) ||
2226         !GFX_CRUMBLED(Feld[xx][yy]) ||
2227         IS_MOVING(xx, yy))
2228       continue;
2229
2230     int element = TILE_GFX_ELEMENT(xx, yy);
2231     int graphic = el_act2crm(element, ACTION_DEFAULT);
2232
2233     if ((graphic_info[graphic].style & STYLE_INNER_CORNERS) &&
2234         graphic_info[graphic].anim_frames == 2)
2235       DrawLevelField(xx, yy);
2236   }
2237 }
2238
2239 static int getBorderElement(int x, int y)
2240 {
2241   int border[7][2] =
2242   {
2243     { EL_STEELWALL_TOPLEFT,             EL_INVISIBLE_STEELWALL_TOPLEFT     },
2244     { EL_STEELWALL_TOPRIGHT,            EL_INVISIBLE_STEELWALL_TOPRIGHT    },
2245     { EL_STEELWALL_BOTTOMLEFT,          EL_INVISIBLE_STEELWALL_BOTTOMLEFT  },
2246     { EL_STEELWALL_BOTTOMRIGHT,         EL_INVISIBLE_STEELWALL_BOTTOMRIGHT },
2247     { EL_STEELWALL_VERTICAL,            EL_INVISIBLE_STEELWALL_VERTICAL    },
2248     { EL_STEELWALL_HORIZONTAL,          EL_INVISIBLE_STEELWALL_HORIZONTAL  },
2249     { EL_STEELWALL,                     EL_INVISIBLE_STEELWALL             }
2250   };
2251   int steel_type = (BorderElement == EL_STEELWALL ? 0 : 1);
2252   int steel_position = (x == -1         && y == -1              ? 0 :
2253                         x == lev_fieldx && y == -1              ? 1 :
2254                         x == -1         && y == lev_fieldy      ? 2 :
2255                         x == lev_fieldx && y == lev_fieldy      ? 3 :
2256                         x == -1         || x == lev_fieldx      ? 4 :
2257                         y == -1         || y == lev_fieldy      ? 5 : 6);
2258
2259   return border[steel_position][steel_type];
2260 }
2261
2262 void DrawScreenElement(int x, int y, int element)
2263 {
2264   DrawScreenElementExt(x, y, 0, 0, element, NO_CUTTING, NO_MASKING);
2265   DrawLevelFieldCrumbled(LEVELX(x), LEVELY(y));
2266 }
2267
2268 void DrawLevelElement(int x, int y, int element)
2269 {
2270   if (IN_LEV_FIELD(x, y) && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
2271     DrawScreenElement(SCREENX(x), SCREENY(y), element);
2272 }
2273
2274 void DrawScreenField(int x, int y)
2275 {
2276   int lx = LEVELX(x), ly = LEVELY(y);
2277   int element, content;
2278
2279   if (!IN_LEV_FIELD(lx, ly))
2280   {
2281     if (lx < -1 || lx > lev_fieldx || ly < -1 || ly > lev_fieldy)
2282       element = EL_EMPTY;
2283     else
2284       element = getBorderElement(lx, ly);
2285
2286     DrawScreenElement(x, y, element);
2287
2288     return;
2289   }
2290
2291   element = Feld[lx][ly];
2292   content = Store[lx][ly];
2293
2294   if (IS_MOVING(lx, ly))
2295   {
2296     int horiz_move = (MovDir[lx][ly] == MV_LEFT || MovDir[lx][ly] == MV_RIGHT);
2297     boolean cut_mode = NO_CUTTING;
2298
2299     if (element == EL_QUICKSAND_EMPTYING ||
2300         element == EL_QUICKSAND_FAST_EMPTYING ||
2301         element == EL_MAGIC_WALL_EMPTYING ||
2302         element == EL_BD_MAGIC_WALL_EMPTYING ||
2303         element == EL_DC_MAGIC_WALL_EMPTYING ||
2304         element == EL_AMOEBA_DROPPING)
2305       cut_mode = CUT_ABOVE;
2306     else if (element == EL_QUICKSAND_FILLING ||
2307              element == EL_QUICKSAND_FAST_FILLING ||
2308              element == EL_MAGIC_WALL_FILLING ||
2309              element == EL_BD_MAGIC_WALL_FILLING ||
2310              element == EL_DC_MAGIC_WALL_FILLING)
2311       cut_mode = CUT_BELOW;
2312
2313     if (cut_mode == CUT_ABOVE)
2314       DrawScreenElement(x, y, element);
2315     else
2316       DrawScreenElement(x, y, EL_EMPTY);
2317
2318     if (horiz_move)
2319       DrawScreenElementShifted(x, y, MovPos[lx][ly], 0, element, NO_CUTTING);
2320     else if (cut_mode == NO_CUTTING)
2321       DrawScreenElementShifted(x, y, 0, MovPos[lx][ly], element, cut_mode);
2322     else
2323     {
2324       DrawScreenElementShifted(x, y, 0, MovPos[lx][ly], content, cut_mode);
2325
2326       if (cut_mode == CUT_BELOW &&
2327           IN_LEV_FIELD(lx, ly + 1) && IN_SCR_FIELD(x, y + 1))
2328         DrawLevelElement(lx, ly + 1, element);
2329     }
2330
2331     if (content == EL_ACID)
2332     {
2333       int dir = MovDir[lx][ly];
2334       int newlx = lx + (dir == MV_LEFT ? -1 : dir == MV_RIGHT ? +1 : 0);
2335       int newly = ly + (dir == MV_UP   ? -1 : dir == MV_DOWN  ? +1 : 0);
2336
2337       DrawLevelElementThruMask(newlx, newly, EL_ACID);
2338
2339       // prevent target field from being drawn again (but without masking)
2340       // (this would happen if target field is scanned after moving element)
2341       Stop[newlx][newly] = TRUE;
2342     }
2343   }
2344   else if (IS_BLOCKED(lx, ly))
2345   {
2346     int oldx, oldy;
2347     int sx, sy;
2348     int horiz_move;
2349     boolean cut_mode = NO_CUTTING;
2350     int element_old, content_old;
2351
2352     Blocked2Moving(lx, ly, &oldx, &oldy);
2353     sx = SCREENX(oldx);
2354     sy = SCREENY(oldy);
2355     horiz_move = (MovDir[oldx][oldy] == MV_LEFT ||
2356                   MovDir[oldx][oldy] == MV_RIGHT);
2357
2358     element_old = Feld[oldx][oldy];
2359     content_old = Store[oldx][oldy];
2360
2361     if (element_old == EL_QUICKSAND_EMPTYING ||
2362         element_old == EL_QUICKSAND_FAST_EMPTYING ||
2363         element_old == EL_MAGIC_WALL_EMPTYING ||
2364         element_old == EL_BD_MAGIC_WALL_EMPTYING ||
2365         element_old == EL_DC_MAGIC_WALL_EMPTYING ||
2366         element_old == EL_AMOEBA_DROPPING)
2367       cut_mode = CUT_ABOVE;
2368
2369     DrawScreenElement(x, y, EL_EMPTY);
2370
2371     if (horiz_move)
2372       DrawScreenElementShifted(sx, sy, MovPos[oldx][oldy], 0, element_old,
2373                                NO_CUTTING);
2374     else if (cut_mode == NO_CUTTING)
2375       DrawScreenElementShifted(sx, sy, 0, MovPos[oldx][oldy], element_old,
2376                                cut_mode);
2377     else
2378       DrawScreenElementShifted(sx, sy, 0, MovPos[oldx][oldy], content_old,
2379                                cut_mode);
2380   }
2381   else if (IS_DRAWABLE(element))
2382     DrawScreenElement(x, y, element);
2383   else
2384     DrawScreenElement(x, y, EL_EMPTY);
2385 }
2386
2387 void DrawLevelField(int x, int y)
2388 {
2389   if (IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
2390     DrawScreenField(SCREENX(x), SCREENY(y));
2391   else if (IS_MOVING(x, y))
2392   {
2393     int newx,newy;
2394
2395     Moving2Blocked(x, y, &newx, &newy);
2396     if (IN_SCR_FIELD(SCREENX(newx), SCREENY(newy)))
2397       DrawScreenField(SCREENX(newx), SCREENY(newy));
2398   }
2399   else if (IS_BLOCKED(x, y))
2400   {
2401     int oldx, oldy;
2402
2403     Blocked2Moving(x, y, &oldx, &oldy);
2404     if (IN_SCR_FIELD(SCREENX(oldx), SCREENY(oldy)))
2405       DrawScreenField(SCREENX(oldx), SCREENY(oldy));
2406   }
2407 }
2408
2409 void DrawSizedElement(int x, int y, int element, int tilesize)
2410 {
2411   int graphic;
2412
2413   graphic = el2edimg(element);
2414   DrawSizedGraphic(x, y, graphic, 0, tilesize);
2415 }
2416
2417 void DrawMiniElement(int x, int y, int element)
2418 {
2419   int graphic;
2420
2421   graphic = el2edimg(element);
2422   DrawMiniGraphic(x, y, graphic);
2423 }
2424
2425 void DrawSizedElementOrWall(int sx, int sy, int scroll_x, int scroll_y,
2426                             int tilesize)
2427 {
2428   int x = sx + scroll_x, y = sy + scroll_y;
2429
2430   if (x < -1 || x > lev_fieldx || y < -1 || y > lev_fieldy)
2431     DrawSizedElement(sx, sy, EL_EMPTY, tilesize);
2432   else if (x > -1 && x < lev_fieldx && y > -1 && y < lev_fieldy)
2433     DrawSizedElement(sx, sy, Feld[x][y], tilesize);
2434   else
2435     DrawSizedGraphic(sx, sy, el2edimg(getBorderElement(x, y)), 0, tilesize);
2436 }
2437
2438 void DrawMiniElementOrWall(int sx, int sy, int scroll_x, int scroll_y)
2439 {
2440   int x = sx + scroll_x, y = sy + scroll_y;
2441
2442   if (x < -1 || x > lev_fieldx || y < -1 || y > lev_fieldy)
2443     DrawMiniElement(sx, sy, EL_EMPTY);
2444   else if (x > -1 && x < lev_fieldx && y > -1 && y < lev_fieldy)
2445     DrawMiniElement(sx, sy, Feld[x][y]);
2446   else
2447     DrawMiniGraphic(sx, sy, el2edimg(getBorderElement(x, y)));
2448 }
2449
2450 void DrawEnvelopeBackgroundTiles(int graphic, int startx, int starty,
2451                                  int x, int y, int xsize, int ysize,
2452                                  int tile_width, int tile_height)
2453 {
2454   Bitmap *src_bitmap;
2455   int src_x, src_y;
2456   int dst_x = startx + x * tile_width;
2457   int dst_y = starty + y * tile_height;
2458   int width  = graphic_info[graphic].width;
2459   int height = graphic_info[graphic].height;
2460   int inner_width_raw  = MAX(width  - 2 * tile_width,  tile_width);
2461   int inner_height_raw = MAX(height - 2 * tile_height, tile_height);
2462   int inner_width  = inner_width_raw  - (inner_width_raw  % tile_width);
2463   int inner_height = inner_height_raw - (inner_height_raw % tile_height);
2464   int inner_sx = (width  >= 3 * tile_width  ? tile_width  : 0);
2465   int inner_sy = (height >= 3 * tile_height ? tile_height : 0);
2466   boolean draw_masked = graphic_info[graphic].draw_masked;
2467
2468   getFixedGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
2469
2470   if (src_bitmap == NULL || width < tile_width || height < tile_height)
2471   {
2472     ClearRectangle(drawto, dst_x, dst_y, tile_width, tile_height);
2473     return;
2474   }
2475
2476   src_x += (x == 0 ? 0 : x == xsize - 1 ? width  - tile_width  :
2477             inner_sx + (x - 1) * tile_width  % inner_width);
2478   src_y += (y == 0 ? 0 : y == ysize - 1 ? height - tile_height :
2479             inner_sy + (y - 1) * tile_height % inner_height);
2480
2481   if (draw_masked)
2482     BlitBitmapMasked(src_bitmap, drawto, src_x, src_y, tile_width, tile_height,
2483                      dst_x, dst_y);
2484   else
2485     BlitBitmap(src_bitmap, drawto, src_x, src_y, tile_width, tile_height,
2486                dst_x, dst_y);
2487 }
2488
2489 void DrawEnvelopeBackground(int graphic, int startx, int starty,
2490                             int x, int y, int xsize, int ysize, int font_nr)
2491 {
2492   int font_width  = getFontWidth(font_nr);
2493   int font_height = getFontHeight(font_nr);
2494
2495   DrawEnvelopeBackgroundTiles(graphic, startx, starty, x, y, xsize, ysize,
2496                               font_width, font_height);
2497 }
2498
2499 void AnimateEnvelope(int envelope_nr, int anim_mode, int action)
2500 {
2501   int graphic = IMG_BACKGROUND_ENVELOPE_1 + envelope_nr;
2502   Bitmap *src_bitmap = graphic_info[graphic].bitmap;
2503   int mask_mode = (src_bitmap != NULL ? BLIT_MASKED : BLIT_ON_BACKGROUND);
2504   boolean ffwd_delay = (tape.playing && tape.fast_forward);
2505   boolean no_delay = (tape.warp_forward);
2506   unsigned int anim_delay = 0;
2507   int frame_delay_value = (ffwd_delay ? FfwdFrameDelay : GameFrameDelay);
2508   int anim_delay_value = (no_delay ? 0 : frame_delay_value) / 2;
2509   int font_nr = FONT_ENVELOPE_1 + envelope_nr;
2510   int font_width = getFontWidth(font_nr);
2511   int font_height = getFontHeight(font_nr);
2512   int max_xsize = level.envelope[envelope_nr].xsize;
2513   int max_ysize = level.envelope[envelope_nr].ysize;
2514   int xstart = (anim_mode & ANIM_VERTICAL ? max_xsize : 0);
2515   int ystart = (anim_mode & ANIM_HORIZONTAL ? max_ysize : 0);
2516   int xend = max_xsize;
2517   int yend = (anim_mode != ANIM_DEFAULT ? max_ysize : 0);
2518   int xstep = (xstart < xend ? 1 : 0);
2519   int ystep = (ystart < yend || xstep == 0 ? 1 : 0);
2520   int start = 0;
2521   int end = MAX(xend - xstart, yend - ystart);
2522   int i;
2523
2524   for (i = start; i <= end; i++)
2525   {
2526     int last_frame = end;       // last frame of this "for" loop
2527     int x = xstart + i * xstep;
2528     int y = ystart + i * ystep;
2529     int xsize = (action == ACTION_CLOSING ? xend - (x - xstart) : x) + 2;
2530     int ysize = (action == ACTION_CLOSING ? yend - (y - ystart) : y) + 2;
2531     int sx = SX + (SXSIZE - xsize * font_width)  / 2;
2532     int sy = SY + (SYSIZE - ysize * font_height) / 2;
2533     int xx, yy;
2534
2535     SetDrawtoField(DRAW_TO_FIELDBUFFER);
2536
2537     BlitScreenToBitmap(backbuffer);
2538
2539     SetDrawtoField(DRAW_TO_BACKBUFFER);
2540
2541     for (yy = 0; yy < ysize; yy++)
2542       for (xx = 0; xx < xsize; xx++)
2543         DrawEnvelopeBackground(graphic, sx, sy, xx, yy, xsize, ysize, font_nr);
2544
2545     DrawTextBuffer(sx + font_width, sy + font_height,
2546                    level.envelope[envelope_nr].text, font_nr, max_xsize,
2547                    xsize - 2, ysize - 2, 0, mask_mode,
2548                    level.envelope[envelope_nr].autowrap,
2549                    level.envelope[envelope_nr].centered, FALSE);
2550
2551     redraw_mask |= REDRAW_FIELD;
2552     BackToFront();
2553
2554     SkipUntilDelayReached(&anim_delay, anim_delay_value, &i, last_frame);
2555   }
2556 }
2557
2558 void ShowEnvelope(int envelope_nr)
2559 {
2560   int element = EL_ENVELOPE_1 + envelope_nr;
2561   int graphic = IMG_BACKGROUND_ENVELOPE_1 + envelope_nr;
2562   int sound_opening = element_info[element].sound[ACTION_OPENING];
2563   int sound_closing = element_info[element].sound[ACTION_CLOSING];
2564   boolean ffwd_delay = (tape.playing && tape.fast_forward);
2565   boolean no_delay = (tape.warp_forward);
2566   int normal_delay_value = ONE_SECOND_DELAY / (ffwd_delay ? 2 : 1);
2567   int wait_delay_value = (no_delay ? 0 : normal_delay_value);
2568   int anim_mode = graphic_info[graphic].anim_mode;
2569   int main_anim_mode = (anim_mode == ANIM_NONE ? ANIM_VERTICAL|ANIM_HORIZONTAL:
2570                         anim_mode == ANIM_DEFAULT ? ANIM_VERTICAL : anim_mode);
2571
2572   game.envelope_active = TRUE;  /* needed for RedrawPlayfield() events */
2573
2574   PlayMenuSoundStereo(sound_opening, SOUND_MIDDLE);
2575
2576   if (anim_mode == ANIM_DEFAULT)
2577     AnimateEnvelope(envelope_nr, ANIM_DEFAULT, ACTION_OPENING);
2578
2579   AnimateEnvelope(envelope_nr, main_anim_mode, ACTION_OPENING);
2580
2581   if (tape.playing)
2582     Delay(wait_delay_value);
2583   else
2584     WaitForEventToContinue();
2585
2586   PlayMenuSoundStereo(sound_closing, SOUND_MIDDLE);
2587
2588   if (anim_mode != ANIM_NONE)
2589     AnimateEnvelope(envelope_nr, main_anim_mode, ACTION_CLOSING);
2590
2591   if (anim_mode == ANIM_DEFAULT)
2592     AnimateEnvelope(envelope_nr, ANIM_DEFAULT, ACTION_CLOSING);
2593
2594   game.envelope_active = FALSE;
2595
2596   SetDrawtoField(DRAW_TO_FIELDBUFFER);
2597
2598   redraw_mask |= REDRAW_FIELD;
2599   BackToFront();
2600 }
2601
2602 static void setRequestBasePosition(int *x, int *y)
2603 {
2604   int sx_base, sy_base;
2605
2606   if (request.x != -1)
2607     sx_base = request.x;
2608   else if (request.align == ALIGN_LEFT)
2609     sx_base = SX;
2610   else if (request.align == ALIGN_RIGHT)
2611     sx_base = SX + SXSIZE;
2612   else
2613     sx_base = SX + SXSIZE / 2;
2614
2615   if (request.y != -1)
2616     sy_base = request.y;
2617   else if (request.valign == VALIGN_TOP)
2618     sy_base = SY;
2619   else if (request.valign == VALIGN_BOTTOM)
2620     sy_base = SY + SYSIZE;
2621   else
2622     sy_base = SY + SYSIZE / 2;
2623
2624   *x = sx_base;
2625   *y = sy_base;
2626 }
2627
2628 static void setRequestPositionExt(int *x, int *y, int width, int height,
2629                                   boolean add_border_size)
2630 {
2631   int border_size = request.border_size;
2632   int sx_base, sy_base;
2633   int sx, sy;
2634
2635   setRequestBasePosition(&sx_base, &sy_base);
2636
2637   if (request.align == ALIGN_LEFT)
2638     sx = sx_base;
2639   else if (request.align == ALIGN_RIGHT)
2640     sx = sx_base - width;
2641   else
2642     sx = sx_base - width  / 2;
2643
2644   if (request.valign == VALIGN_TOP)
2645     sy = sy_base;
2646   else if (request.valign == VALIGN_BOTTOM)
2647     sy = sy_base - height;
2648   else
2649     sy = sy_base - height / 2;
2650
2651   sx = MAX(0, MIN(sx, WIN_XSIZE - width));
2652   sy = MAX(0, MIN(sy, WIN_YSIZE - height));
2653
2654   if (add_border_size)
2655   {
2656     sx += border_size;
2657     sy += border_size;
2658   }
2659
2660   *x = sx;
2661   *y = sy;
2662 }
2663
2664 static void setRequestPosition(int *x, int *y, boolean add_border_size)
2665 {
2666   setRequestPositionExt(x, y, request.width, request.height, add_border_size);
2667 }
2668
2669 void DrawEnvelopeRequest(char *text)
2670 {
2671   char *text_final = text;
2672   char *text_door_style = NULL;
2673   int graphic = IMG_BACKGROUND_REQUEST;
2674   Bitmap *src_bitmap = graphic_info[graphic].bitmap;
2675   int mask_mode = (src_bitmap != NULL ? BLIT_MASKED : BLIT_ON_BACKGROUND);
2676   int font_nr = FONT_REQUEST;
2677   int font_width = getFontWidth(font_nr);
2678   int font_height = getFontHeight(font_nr);
2679   int border_size = request.border_size;
2680   int line_spacing = request.line_spacing;
2681   int line_height = font_height + line_spacing;
2682   int max_text_width  = request.width  - 2 * border_size;
2683   int max_text_height = request.height - 2 * border_size;
2684   int line_length = max_text_width  / font_width;
2685   int max_lines   = max_text_height / line_height;
2686   int text_width = line_length * font_width;
2687   int width = request.width;
2688   int height = request.height;
2689   int tile_size = MAX(request.step_offset, 1);
2690   int x_steps = width  / tile_size;
2691   int y_steps = height / tile_size;
2692   int sx_offset = border_size;
2693   int sy_offset = border_size;
2694   int sx, sy;
2695   int i, x, y;
2696
2697   if (request.centered)
2698     sx_offset = (request.width - text_width) / 2;
2699
2700   if (request.wrap_single_words && !request.autowrap)
2701   {
2702     char *src_text_ptr, *dst_text_ptr;
2703
2704     text_door_style = checked_malloc(2 * strlen(text) + 1);
2705
2706     src_text_ptr = text;
2707     dst_text_ptr = text_door_style;
2708
2709     while (*src_text_ptr)
2710     {
2711       if (*src_text_ptr == ' ' ||
2712           *src_text_ptr == '?' ||
2713           *src_text_ptr == '!')
2714         *dst_text_ptr++ = '\n';
2715
2716       if (*src_text_ptr != ' ')
2717         *dst_text_ptr++ = *src_text_ptr;
2718
2719       src_text_ptr++;
2720     }
2721
2722     *dst_text_ptr = '\0';
2723
2724     text_final = text_door_style;
2725   }
2726
2727   setRequestPosition(&sx, &sy, FALSE);
2728
2729   ClearRectangle(backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE);
2730
2731   for (y = 0; y < y_steps; y++)
2732     for (x = 0; x < x_steps; x++)
2733       DrawEnvelopeBackgroundTiles(graphic, sx, sy,
2734                                   x, y, x_steps, y_steps,
2735                                   tile_size, tile_size);
2736
2737   /* force DOOR font inside door area */
2738   SetFontStatus(GAME_MODE_PSEUDO_DOOR);
2739
2740   DrawTextBuffer(sx + sx_offset, sy + sy_offset, text_final, font_nr,
2741                  line_length, -1, max_lines, line_spacing, mask_mode,
2742                  request.autowrap, request.centered, FALSE);
2743
2744   ResetFontStatus();
2745
2746   for (i = 0; i < NUM_TOOL_BUTTONS; i++)
2747     RedrawGadget(tool_gadget[i]);
2748
2749   // store readily prepared envelope request for later use when animating
2750   BlitBitmap(backbuffer, bitmap_db_store_2, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
2751
2752   if (text_door_style)
2753     free(text_door_style);
2754 }
2755
2756 void AnimateEnvelopeRequest(int anim_mode, int action)
2757 {
2758   int graphic = IMG_BACKGROUND_REQUEST;
2759   boolean draw_masked = graphic_info[graphic].draw_masked;
2760   int delay_value_normal = request.step_delay;
2761   int delay_value_fast = delay_value_normal / 2;
2762   boolean ffwd_delay = (tape.playing && tape.fast_forward);
2763   boolean no_delay = (tape.warp_forward);
2764   int delay_value = (ffwd_delay ? delay_value_fast : delay_value_normal);
2765   int anim_delay_value = (no_delay ? 0 : delay_value + 500 * 0) / 2;
2766   unsigned int anim_delay = 0;
2767
2768   int tile_size = MAX(request.step_offset, 1);
2769   int max_xsize = request.width  / tile_size;
2770   int max_ysize = request.height / tile_size;
2771   int max_xsize_inner = max_xsize - 2;
2772   int max_ysize_inner = max_ysize - 2;
2773
2774   int xstart = (anim_mode & ANIM_VERTICAL ? max_xsize_inner : 0);
2775   int ystart = (anim_mode & ANIM_HORIZONTAL ? max_ysize_inner : 0);
2776   int xend = max_xsize_inner;
2777   int yend = (anim_mode != ANIM_DEFAULT ? max_ysize_inner : 0);
2778   int xstep = (xstart < xend ? 1 : 0);
2779   int ystep = (ystart < yend || xstep == 0 ? 1 : 0);
2780   int start = 0;
2781   int end = MAX(xend - xstart, yend - ystart);
2782   int i;
2783
2784   if (setup.quick_doors)
2785   {
2786     xstart = xend;
2787     ystart = yend;
2788     end = 0;
2789   }
2790
2791   for (i = start; i <= end; i++)
2792   {
2793     int last_frame = end;       // last frame of this "for" loop
2794     int x = xstart + i * xstep;
2795     int y = ystart + i * ystep;
2796     int xsize = (action == ACTION_CLOSING ? xend - (x - xstart) : x) + 2;
2797     int ysize = (action == ACTION_CLOSING ? yend - (y - ystart) : y) + 2;
2798     int xsize_size_left = (xsize - 1) * tile_size;
2799     int ysize_size_top  = (ysize - 1) * tile_size;
2800     int max_xsize_pos = (max_xsize - 1) * tile_size;
2801     int max_ysize_pos = (max_ysize - 1) * tile_size;
2802     int width  = xsize * tile_size;
2803     int height = ysize * tile_size;
2804     int src_x, src_y;
2805     int dst_x, dst_y;
2806     int xx, yy;
2807
2808     setRequestPosition(&src_x, &src_y, FALSE);
2809     setRequestPositionExt(&dst_x, &dst_y, width, height, FALSE);
2810
2811     BlitBitmap(bitmap_db_store_1, backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
2812
2813     for (yy = 0; yy < 2; yy++)
2814     {
2815       for (xx = 0; xx < 2; xx++)
2816       {
2817         int src_xx = src_x + xx * max_xsize_pos;
2818         int src_yy = src_y + yy * max_ysize_pos;
2819         int dst_xx = dst_x + xx * xsize_size_left;
2820         int dst_yy = dst_y + yy * ysize_size_top;
2821         int xx_size = (xx ? tile_size : xsize_size_left);
2822         int yy_size = (yy ? tile_size : ysize_size_top);
2823
2824         if (draw_masked)
2825           BlitBitmapMasked(bitmap_db_store_2, backbuffer,
2826                            src_xx, src_yy, xx_size, yy_size, dst_xx, dst_yy);
2827         else
2828           BlitBitmap(bitmap_db_store_2, backbuffer,
2829                      src_xx, src_yy, xx_size, yy_size, dst_xx, dst_yy);
2830       }
2831     }
2832
2833     redraw_mask |= REDRAW_FIELD;
2834
2835     BackToFront();
2836
2837     SkipUntilDelayReached(&anim_delay, anim_delay_value, &i, last_frame);
2838   }
2839 }
2840
2841 void ShowEnvelopeRequest(char *text, unsigned int req_state, int action)
2842 {
2843   int graphic = IMG_BACKGROUND_REQUEST;
2844   int sound_opening = SND_REQUEST_OPENING;
2845   int sound_closing = SND_REQUEST_CLOSING;
2846   int anim_mode_1 = request.anim_mode;                  /* (higher priority) */
2847   int anim_mode_2 = graphic_info[graphic].anim_mode;    /* (lower priority) */
2848   int anim_mode = (anim_mode_1 != ANIM_DEFAULT ? anim_mode_1 : anim_mode_2);
2849   int main_anim_mode = (anim_mode == ANIM_NONE ? ANIM_VERTICAL|ANIM_HORIZONTAL:
2850                         anim_mode == ANIM_DEFAULT ? ANIM_VERTICAL : anim_mode);
2851
2852   if (game_status == GAME_MODE_PLAYING)
2853     BlitScreenToBitmap(backbuffer);
2854
2855   SetDrawtoField(DRAW_TO_BACKBUFFER);
2856
2857   // SetDrawBackgroundMask(REDRAW_NONE);
2858
2859   if (action == ACTION_OPENING)
2860   {
2861     BlitBitmap(backbuffer, bitmap_db_store_1, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
2862
2863     if (req_state & REQ_ASK)
2864     {
2865       MapGadget(tool_gadget[TOOL_CTRL_ID_YES]);
2866       MapGadget(tool_gadget[TOOL_CTRL_ID_NO]);
2867     }
2868     else if (req_state & REQ_CONFIRM)
2869     {
2870       MapGadget(tool_gadget[TOOL_CTRL_ID_CONFIRM]);
2871     }
2872     else if (req_state & REQ_PLAYER)
2873     {
2874       MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_1]);
2875       MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_2]);
2876       MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_3]);
2877       MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_4]);
2878     }
2879
2880     DrawEnvelopeRequest(text);
2881   }
2882
2883   game.envelope_active = TRUE;  /* needed for RedrawPlayfield() events */
2884
2885   if (action == ACTION_OPENING)
2886   {
2887     PlayMenuSoundStereo(sound_opening, SOUND_MIDDLE);
2888
2889     if (anim_mode == ANIM_DEFAULT)
2890       AnimateEnvelopeRequest(ANIM_DEFAULT, ACTION_OPENING);
2891
2892     AnimateEnvelopeRequest(main_anim_mode, ACTION_OPENING);
2893   }
2894   else
2895   {
2896     PlayMenuSoundStereo(sound_closing, SOUND_MIDDLE);
2897
2898     if (anim_mode != ANIM_NONE)
2899       AnimateEnvelopeRequest(main_anim_mode, ACTION_CLOSING);
2900
2901     if (anim_mode == ANIM_DEFAULT)
2902       AnimateEnvelopeRequest(ANIM_DEFAULT, ACTION_CLOSING);
2903   }
2904
2905   game.envelope_active = FALSE;
2906
2907   if (action == ACTION_CLOSING)
2908     BlitBitmap(bitmap_db_store_1, backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
2909
2910   // SetDrawBackgroundMask(last_draw_background_mask);
2911
2912   redraw_mask |= REDRAW_FIELD;
2913
2914   BackToFront();
2915
2916   if (action == ACTION_CLOSING &&
2917       game_status == GAME_MODE_PLAYING &&
2918       level.game_engine_type == GAME_ENGINE_TYPE_RND)
2919     SetDrawtoField(DRAW_TO_FIELDBUFFER);
2920 }
2921
2922 void DrawPreviewElement(int dst_x, int dst_y, int element, int tilesize)
2923 {
2924   Bitmap *src_bitmap;
2925   int src_x, src_y;
2926   int graphic = el2preimg(element);
2927
2928   getSizedGraphicSource(graphic, 0, tilesize, &src_bitmap, &src_x, &src_y);
2929   BlitBitmap(src_bitmap, drawto, src_x, src_y, tilesize, tilesize, dst_x,dst_y);
2930 }
2931
2932 void DrawLevel(int draw_background_mask)
2933 {
2934   int x,y;
2935
2936   SetMainBackgroundImage(IMG_BACKGROUND_PLAYING);
2937   SetDrawBackgroundMask(draw_background_mask);
2938
2939   ClearField();
2940
2941   for (x = BX1; x <= BX2; x++)
2942     for (y = BY1; y <= BY2; y++)
2943       DrawScreenField(x, y);
2944
2945   redraw_mask |= REDRAW_FIELD;
2946 }
2947
2948 void DrawSizedLevel(int size_x, int size_y, int scroll_x, int scroll_y,
2949                     int tilesize)
2950 {
2951   int x,y;
2952
2953   for (x = 0; x < size_x; x++)
2954     for (y = 0; y < size_y; y++)
2955       DrawSizedElementOrWall(x, y, scroll_x, scroll_y, tilesize);
2956
2957   redraw_mask |= REDRAW_FIELD;
2958 }
2959
2960 void DrawMiniLevel(int size_x, int size_y, int scroll_x, int scroll_y)
2961 {
2962   int x,y;
2963
2964   for (x = 0; x < size_x; x++)
2965     for (y = 0; y < size_y; y++)
2966       DrawMiniElementOrWall(x, y, scroll_x, scroll_y);
2967
2968   redraw_mask |= REDRAW_FIELD;
2969 }
2970
2971 static void DrawPreviewLevelPlayfieldExt(int from_x, int from_y)
2972 {
2973   boolean show_level_border = (BorderElement != EL_EMPTY);
2974   int level_xsize = lev_fieldx + (show_level_border ? 2 : 0);
2975   int level_ysize = lev_fieldy + (show_level_border ? 2 : 0);
2976   int tile_size = preview.tile_size;
2977   int preview_width  = preview.xsize * tile_size;
2978   int preview_height = preview.ysize * tile_size;
2979   int real_preview_xsize = MIN(level_xsize, preview.xsize);
2980   int real_preview_ysize = MIN(level_ysize, preview.ysize);
2981   int real_preview_width  = real_preview_xsize * tile_size;
2982   int real_preview_height = real_preview_ysize * tile_size;
2983   int dst_x = SX + ALIGNED_XPOS(preview.x, preview_width, preview.align);
2984   int dst_y = SY + ALIGNED_YPOS(preview.y, preview_height, preview.valign);
2985   int x, y;
2986
2987   if (!IN_GFX_FIELD_FULL(dst_x, dst_y + preview_height - 1))
2988     return;
2989
2990   DrawBackground(dst_x, dst_y, preview_width, preview_height);
2991
2992   dst_x += (preview_width  - real_preview_width)  / 2;
2993   dst_y += (preview_height - real_preview_height) / 2;
2994
2995   for (x = 0; x < real_preview_xsize; x++)
2996   {
2997     for (y = 0; y < real_preview_ysize; y++)
2998     {
2999       int lx = from_x + x + (show_level_border ? -1 : 0);
3000       int ly = from_y + y + (show_level_border ? -1 : 0);
3001       int element = (IN_LEV_FIELD(lx, ly) ? level.field[lx][ly] :
3002                      getBorderElement(lx, ly));
3003
3004       DrawPreviewElement(dst_x + x * tile_size, dst_y + y * tile_size,
3005                          element, tile_size);
3006     }
3007   }
3008
3009   redraw_mask |= REDRAW_FIELD;
3010 }
3011
3012 #define MICROLABEL_EMPTY                0
3013 #define MICROLABEL_LEVEL_NAME           1
3014 #define MICROLABEL_LEVEL_AUTHOR_HEAD    2
3015 #define MICROLABEL_LEVEL_AUTHOR         3
3016 #define MICROLABEL_IMPORTED_FROM_HEAD   4
3017 #define MICROLABEL_IMPORTED_FROM        5
3018 #define MICROLABEL_IMPORTED_BY_HEAD     6
3019 #define MICROLABEL_IMPORTED_BY          7
3020
3021 static int getMaxTextLength(struct TextPosInfo *pos, int font_nr)
3022 {
3023   int max_text_width = SXSIZE;
3024   int font_width = getFontWidth(font_nr);
3025
3026   if (pos->align == ALIGN_CENTER)
3027     max_text_width = (pos->x < SXSIZE / 2 ? pos->x * 2 : (SXSIZE - pos->x) * 2);
3028   else if (pos->align == ALIGN_RIGHT)
3029     max_text_width = pos->x;
3030   else
3031     max_text_width = SXSIZE - pos->x;
3032
3033   return max_text_width / font_width;
3034 }
3035
3036 static void DrawPreviewLevelLabelExt(int mode)
3037 {
3038   struct TextPosInfo *pos = &menu.main.text.level_info_2;
3039   char label_text[MAX_OUTPUT_LINESIZE + 1];
3040   int max_len_label_text;
3041   int font_nr = pos->font;
3042   int i;
3043
3044   if (!IN_GFX_FIELD_FULL(pos->x, pos->y + getFontHeight(pos->font)))
3045     return;
3046
3047   if (mode == MICROLABEL_LEVEL_AUTHOR_HEAD ||
3048       mode == MICROLABEL_IMPORTED_FROM_HEAD ||
3049       mode == MICROLABEL_IMPORTED_BY_HEAD)
3050     font_nr = pos->font_alt;
3051
3052   max_len_label_text = getMaxTextLength(pos, font_nr);
3053
3054   if (pos->size != -1)
3055     max_len_label_text = pos->size;
3056
3057   for (i = 0; i < max_len_label_text; i++)
3058     label_text[i] = ' ';
3059   label_text[max_len_label_text] = '\0';
3060
3061   if (strlen(label_text) > 0)
3062     DrawTextSAligned(pos->x, pos->y, label_text, font_nr, pos->align);
3063
3064   strncpy(label_text,
3065           (mode == MICROLABEL_LEVEL_NAME ? level.name :
3066            mode == MICROLABEL_LEVEL_AUTHOR_HEAD ? "created by" :
3067            mode == MICROLABEL_LEVEL_AUTHOR ? level.author :
3068            mode == MICROLABEL_IMPORTED_FROM_HEAD ? "imported from" :
3069            mode == MICROLABEL_IMPORTED_FROM ? leveldir_current->imported_from :
3070            mode == MICROLABEL_IMPORTED_BY_HEAD ? "imported by" :
3071            mode == MICROLABEL_IMPORTED_BY ? leveldir_current->imported_by :""),
3072           max_len_label_text);
3073   label_text[max_len_label_text] = '\0';
3074
3075   if (strlen(label_text) > 0)
3076     DrawTextSAligned(pos->x, pos->y, label_text, font_nr, pos->align);
3077
3078   redraw_mask |= REDRAW_FIELD;
3079 }
3080
3081 static void DrawPreviewLevelExt(boolean restart)
3082 {
3083   static unsigned int scroll_delay = 0;
3084   static unsigned int label_delay = 0;
3085   static int from_x, from_y, scroll_direction;
3086   static int label_state, label_counter;
3087   unsigned int scroll_delay_value = preview.step_delay;
3088   boolean show_level_border = (BorderElement != EL_EMPTY);
3089   int level_xsize = lev_fieldx + (show_level_border ? 2 : 0);
3090   int level_ysize = lev_fieldy + (show_level_border ? 2 : 0);
3091
3092   if (restart)
3093   {
3094     from_x = 0;
3095     from_y = 0;
3096
3097     if (preview.anim_mode == ANIM_CENTERED)
3098     {
3099       if (level_xsize > preview.xsize)
3100         from_x = (level_xsize - preview.xsize) / 2;
3101       if (level_ysize > preview.ysize)
3102         from_y = (level_ysize - preview.ysize) / 2;
3103     }
3104
3105     from_x += preview.xoffset;
3106     from_y += preview.yoffset;
3107
3108     scroll_direction = MV_RIGHT;
3109     label_state = 1;
3110     label_counter = 0;
3111
3112     DrawPreviewLevelPlayfieldExt(from_x, from_y);
3113     DrawPreviewLevelLabelExt(label_state);
3114
3115     /* initialize delay counters */
3116     DelayReached(&scroll_delay, 0);
3117     DelayReached(&label_delay, 0);
3118
3119     if (leveldir_current->name)
3120     {
3121       struct TextPosInfo *pos = &menu.main.text.level_info_1;
3122       char label_text[MAX_OUTPUT_LINESIZE + 1];
3123       int font_nr = pos->font;
3124       int max_len_label_text = getMaxTextLength(pos, font_nr);
3125
3126       if (pos->size != -1)
3127         max_len_label_text = pos->size;
3128
3129       strncpy(label_text, leveldir_current->name, max_len_label_text);
3130       label_text[max_len_label_text] = '\0';
3131
3132       if (IN_GFX_FIELD_FULL(pos->x, pos->y + getFontHeight(pos->font)))
3133         DrawTextSAligned(pos->x, pos->y, label_text, font_nr, pos->align);
3134     }
3135
3136     return;
3137   }
3138
3139   /* scroll preview level, if needed */
3140   if (preview.anim_mode != ANIM_NONE &&
3141       (level_xsize > preview.xsize || level_ysize > preview.ysize) &&
3142       DelayReached(&scroll_delay, scroll_delay_value))
3143   {
3144     switch (scroll_direction)
3145     {
3146       case MV_LEFT:
3147         if (from_x > 0)
3148         {
3149           from_x -= preview.step_offset;
3150           from_x = (from_x < 0 ? 0 : from_x);
3151         }
3152         else
3153           scroll_direction = MV_UP;
3154         break;
3155
3156       case MV_RIGHT:
3157         if (from_x < level_xsize - preview.xsize)
3158         {
3159           from_x += preview.step_offset;
3160           from_x = (from_x > level_xsize - preview.xsize ?
3161                     level_xsize - preview.xsize : from_x);
3162         }
3163         else
3164           scroll_direction = MV_DOWN;
3165         break;
3166
3167       case MV_UP:
3168         if (from_y > 0)
3169         {
3170           from_y -= preview.step_offset;
3171           from_y = (from_y < 0 ? 0 : from_y);
3172         }
3173         else
3174           scroll_direction = MV_RIGHT;
3175         break;
3176
3177       case MV_DOWN:
3178         if (from_y < level_ysize - preview.ysize)
3179         {
3180           from_y += preview.step_offset;
3181           from_y = (from_y > level_ysize - preview.ysize ?
3182                     level_ysize - preview.ysize : from_y);
3183         }
3184         else
3185           scroll_direction = MV_LEFT;
3186         break;
3187
3188       default:
3189         break;
3190     }
3191
3192     DrawPreviewLevelPlayfieldExt(from_x, from_y);
3193   }
3194
3195   /* !!! THIS ALL SUCKS -- SHOULD BE CLEANLY REWRITTEN !!! */
3196   /* redraw micro level label, if needed */
3197   if (!strEqual(level.name, NAMELESS_LEVEL_NAME) &&
3198       !strEqual(level.author, ANONYMOUS_NAME) &&
3199       !strEqual(level.author, leveldir_current->name) &&
3200       DelayReached(&label_delay, MICROLEVEL_LABEL_DELAY))
3201   {
3202     int max_label_counter = 23;
3203
3204     if (leveldir_current->imported_from != NULL &&
3205         strlen(leveldir_current->imported_from) > 0)
3206       max_label_counter += 14;
3207     if (leveldir_current->imported_by != NULL &&
3208         strlen(leveldir_current->imported_by) > 0)
3209       max_label_counter += 14;
3210
3211     label_counter = (label_counter + 1) % max_label_counter;
3212     label_state = (label_counter >= 0 && label_counter <= 7 ?
3213                    MICROLABEL_LEVEL_NAME :
3214                    label_counter >= 9 && label_counter <= 12 ?
3215                    MICROLABEL_LEVEL_AUTHOR_HEAD :
3216                    label_counter >= 14 && label_counter <= 21 ?
3217                    MICROLABEL_LEVEL_AUTHOR :
3218                    label_counter >= 23 && label_counter <= 26 ?
3219                    MICROLABEL_IMPORTED_FROM_HEAD :
3220                    label_counter >= 28 && label_counter <= 35 ?
3221                    MICROLABEL_IMPORTED_FROM :
3222                    label_counter >= 37 && label_counter <= 40 ?
3223                    MICROLABEL_IMPORTED_BY_HEAD :
3224                    label_counter >= 42 && label_counter <= 49 ?
3225                    MICROLABEL_IMPORTED_BY : MICROLABEL_EMPTY);
3226
3227     if (leveldir_current->imported_from == NULL &&
3228         (label_state == MICROLABEL_IMPORTED_FROM_HEAD ||
3229          label_state == MICROLABEL_IMPORTED_FROM))
3230       label_state = (label_state == MICROLABEL_IMPORTED_FROM_HEAD ?
3231                      MICROLABEL_IMPORTED_BY_HEAD : MICROLABEL_IMPORTED_BY);
3232
3233     DrawPreviewLevelLabelExt(label_state);
3234   }
3235 }
3236
3237 void DrawPreviewLevelInitial()
3238 {
3239   DrawPreviewLevelExt(TRUE);
3240 }
3241
3242 void DrawPreviewLevelAnimation()
3243 {
3244   DrawPreviewLevelExt(FALSE);
3245 }
3246
3247 inline static void DrawGraphicAnimationExt(DrawBuffer *dst_bitmap, int x, int y,
3248                                            int graphic, int sync_frame,
3249                                            int mask_mode)
3250 {
3251   int frame = getGraphicAnimationFrame(graphic, sync_frame);
3252
3253   if (mask_mode == USE_MASKING)
3254     DrawGraphicThruMaskExt(dst_bitmap, x, y, graphic, frame);
3255   else
3256     DrawGraphicExt(dst_bitmap, x, y, graphic, frame);
3257 }
3258
3259 void DrawFixedGraphicAnimationExt(DrawBuffer *dst_bitmap, int x, int y,
3260                                   int graphic, int sync_frame, int mask_mode)
3261 {
3262   int frame = getGraphicAnimationFrame(graphic, sync_frame);
3263
3264   if (mask_mode == USE_MASKING)
3265     DrawFixedGraphicThruMaskExt(dst_bitmap, x, y, graphic, frame);
3266   else
3267     DrawFixedGraphicExt(dst_bitmap, x, y, graphic, frame);
3268 }
3269
3270 inline static void DrawGraphicAnimation(int x, int y, int graphic)
3271 {
3272   int lx = LEVELX(x), ly = LEVELY(y);
3273
3274   if (!IN_SCR_FIELD(x, y))
3275     return;
3276
3277   DrawGraphicAnimationExt(drawto_field, FX + x * TILEX_VAR, FY + y * TILEY_VAR,
3278                           graphic, GfxFrame[lx][ly], NO_MASKING);
3279
3280   MarkTileDirty(x, y);
3281 }
3282
3283 void DrawFixedGraphicAnimation(int x, int y, int graphic)
3284 {
3285   int lx = LEVELX(x), ly = LEVELY(y);
3286
3287   if (!IN_SCR_FIELD(x, y))
3288     return;
3289
3290   DrawGraphicAnimationExt(drawto_field, FX + x * TILEX, FY + y * TILEY,
3291                           graphic, GfxFrame[lx][ly], NO_MASKING);
3292   MarkTileDirty(x, y);
3293 }
3294
3295 void DrawLevelGraphicAnimation(int x, int y, int graphic)
3296 {
3297   DrawGraphicAnimation(SCREENX(x), SCREENY(y), graphic);
3298 }
3299
3300 void DrawLevelElementAnimation(int x, int y, int element)
3301 {
3302   int graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
3303
3304   DrawGraphicAnimation(SCREENX(x), SCREENY(y), graphic);
3305 }
3306
3307 void DrawLevelGraphicAnimationIfNeeded(int x, int y, int graphic)
3308 {
3309   int sx = SCREENX(x), sy = SCREENY(y);
3310
3311   if (!IN_LEV_FIELD(x, y) || !IN_SCR_FIELD(sx, sy))
3312     return;
3313
3314   if (!IS_NEW_FRAME(GfxFrame[x][y], graphic))
3315     return;
3316
3317   DrawGraphicAnimation(sx, sy, graphic);
3318
3319 #if 1
3320   if (GFX_CRUMBLED(TILE_GFX_ELEMENT(x, y)))
3321     DrawLevelFieldCrumbled(x, y);
3322 #else
3323   if (GFX_CRUMBLED(Feld[x][y]))
3324     DrawLevelFieldCrumbled(x, y);
3325 #endif
3326 }
3327
3328 void DrawLevelElementAnimationIfNeeded(int x, int y, int element)
3329 {
3330   int sx = SCREENX(x), sy = SCREENY(y);
3331   int graphic;
3332
3333   if (!IN_LEV_FIELD(x, y) || !IN_SCR_FIELD(sx, sy))
3334     return;
3335
3336   graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
3337
3338   if (!IS_NEW_FRAME(GfxFrame[x][y], graphic))
3339     return;
3340
3341   DrawGraphicAnimation(sx, sy, graphic);
3342
3343   if (GFX_CRUMBLED(element))
3344     DrawLevelFieldCrumbled(x, y);
3345 }
3346
3347 static int getPlayerGraphic(struct PlayerInfo *player, int move_dir)
3348 {
3349   if (player->use_murphy)
3350   {
3351     /* this works only because currently only one player can be "murphy" ... */
3352     static int last_horizontal_dir = MV_LEFT;
3353     int graphic = el_act_dir2img(EL_SP_MURPHY, player->GfxAction, move_dir);
3354
3355     if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
3356       last_horizontal_dir = move_dir;
3357
3358     if (graphic == IMG_SP_MURPHY)       /* undefined => use special graphic */
3359     {
3360       int direction = (player->is_snapping ? move_dir : last_horizontal_dir);
3361
3362       graphic = el_act_dir2img(EL_SP_MURPHY, player->GfxAction, direction);
3363     }
3364
3365     return graphic;
3366   }
3367   else
3368     return el_act_dir2img(player->artwork_element, player->GfxAction,move_dir);
3369 }
3370
3371 static boolean equalGraphics(int graphic1, int graphic2)
3372 {
3373   struct GraphicInfo *g1 = &graphic_info[graphic1];
3374   struct GraphicInfo *g2 = &graphic_info[graphic2];
3375
3376   return (g1->bitmap      == g2->bitmap &&
3377           g1->src_x       == g2->src_x &&
3378           g1->src_y       == g2->src_y &&
3379           g1->anim_frames == g2->anim_frames &&
3380           g1->anim_delay  == g2->anim_delay &&
3381           g1->anim_mode   == g2->anim_mode);
3382 }
3383
3384 void DrawAllPlayers()
3385 {
3386   int i;
3387
3388   for (i = 0; i < MAX_PLAYERS; i++)
3389     if (stored_player[i].active)
3390       DrawPlayer(&stored_player[i]);
3391 }
3392
3393 void DrawPlayerField(int x, int y)
3394 {
3395   if (!IS_PLAYER(x, y))
3396     return;
3397
3398   DrawPlayer(PLAYERINFO(x, y));
3399 }
3400
3401 #define DRAW_PLAYER_OVER_PUSHED_ELEMENT 1
3402
3403 void DrawPlayer(struct PlayerInfo *player)
3404 {
3405   int jx = player->jx;
3406   int jy = player->jy;
3407   int move_dir = player->MovDir;
3408   int dx = (move_dir == MV_LEFT ? -1 : move_dir == MV_RIGHT ? +1 : 0);
3409   int dy = (move_dir == MV_UP   ? -1 : move_dir == MV_DOWN  ? +1 : 0);
3410   int last_jx = (player->is_moving ? jx - dx : jx);
3411   int last_jy = (player->is_moving ? jy - dy : jy);
3412   int next_jx = jx + dx;
3413   int next_jy = jy + dy;
3414   boolean player_is_moving = (player->MovPos ? TRUE : FALSE);
3415   boolean player_is_opaque = FALSE;
3416   int sx = SCREENX(jx), sy = SCREENY(jy);
3417   int sxx = 0, syy = 0;
3418   int element = Feld[jx][jy], last_element = Feld[last_jx][last_jy];
3419   int graphic;
3420   int action = ACTION_DEFAULT;
3421   int last_player_graphic = getPlayerGraphic(player, move_dir);
3422   int last_player_frame = player->Frame;
3423   int frame = 0;
3424
3425   /* GfxElement[][] is set to the element the player is digging or collecting;
3426      remove also for off-screen player if the player is not moving anymore */
3427   if (IN_LEV_FIELD(jx, jy) && !player_is_moving)
3428     GfxElement[jx][jy] = EL_UNDEFINED;
3429
3430   if (!player->active || !IN_SCR_FIELD(SCREENX(last_jx), SCREENY(last_jy)))
3431     return;
3432
3433 #if DEBUG
3434   if (!IN_LEV_FIELD(jx, jy))
3435   {
3436     printf("DrawPlayerField(): x = %d, y = %d\n",jx,jy);
3437     printf("DrawPlayerField(): sx = %d, sy = %d\n",sx,sy);
3438     printf("DrawPlayerField(): This should never happen!\n");
3439     return;
3440   }
3441 #endif
3442
3443   if (element == EL_EXPLOSION)
3444     return;
3445
3446   action = (player->is_pushing    ? ACTION_PUSHING         :
3447             player->is_digging    ? ACTION_DIGGING         :
3448             player->is_collecting ? ACTION_COLLECTING      :
3449             player->is_moving     ? ACTION_MOVING          :
3450             player->is_snapping   ? ACTION_SNAPPING        :
3451             player->is_dropping   ? ACTION_DROPPING        :
3452             player->is_waiting    ? player->action_waiting : ACTION_DEFAULT);
3453
3454   if (player->is_waiting)
3455     move_dir = player->dir_waiting;
3456
3457   InitPlayerGfxAnimation(player, action, move_dir);
3458
3459   /* ----------------------------------------------------------------------- */
3460   /* draw things in the field the player is leaving, if needed               */
3461   /* ----------------------------------------------------------------------- */
3462
3463   if (player->is_moving)
3464   {
3465     if (Back[last_jx][last_jy] && IS_DRAWABLE(last_element))
3466     {
3467       DrawLevelElement(last_jx, last_jy, Back[last_jx][last_jy]);
3468
3469       if (last_element == EL_DYNAMITE_ACTIVE ||
3470           last_element == EL_EM_DYNAMITE_ACTIVE ||
3471           last_element == EL_SP_DISK_RED_ACTIVE)
3472         DrawDynamite(last_jx, last_jy);
3473       else
3474         DrawLevelFieldThruMask(last_jx, last_jy);
3475     }
3476     else if (last_element == EL_DYNAMITE_ACTIVE ||
3477              last_element == EL_EM_DYNAMITE_ACTIVE ||
3478              last_element == EL_SP_DISK_RED_ACTIVE)
3479       DrawDynamite(last_jx, last_jy);
3480 #if 0
3481     /* !!! this is not enough to prevent flickering of players which are
3482        moving next to each others without a free tile between them -- this
3483        can only be solved by drawing all players layer by layer (first the
3484        background, then the foreground etc.) !!! => TODO */
3485     else if (!IS_PLAYER(last_jx, last_jy))
3486       DrawLevelField(last_jx, last_jy);
3487 #else
3488     else
3489       DrawLevelField(last_jx, last_jy);
3490 #endif
3491
3492     if (player->is_pushing && IN_SCR_FIELD(SCREENX(next_jx), SCREENY(next_jy)))
3493       DrawLevelElement(next_jx, next_jy, EL_EMPTY);
3494   }
3495
3496   if (!IN_SCR_FIELD(sx, sy))
3497     return;
3498
3499   /* ----------------------------------------------------------------------- */
3500   /* draw things behind the player, if needed                                */
3501   /* ----------------------------------------------------------------------- */
3502
3503   if (Back[jx][jy])
3504     DrawLevelElement(jx, jy, Back[jx][jy]);
3505   else if (IS_ACTIVE_BOMB(element))
3506     DrawLevelElement(jx, jy, EL_EMPTY);
3507   else
3508   {
3509     if (player_is_moving && GfxElement[jx][jy] != EL_UNDEFINED)
3510     {
3511       int old_element = GfxElement[jx][jy];
3512       int old_graphic = el_act_dir2img(old_element, action, move_dir);
3513       int frame = getGraphicAnimationFrame(old_graphic, player->StepFrame);
3514
3515       if (GFX_CRUMBLED(old_element))
3516         DrawLevelFieldCrumbledDigging(jx, jy, move_dir, player->StepFrame);
3517       else
3518         DrawGraphic(sx, sy, old_graphic, frame);
3519
3520       if (graphic_info[old_graphic].anim_mode & ANIM_OPAQUE_PLAYER)
3521         player_is_opaque = TRUE;
3522     }
3523     else
3524     {
3525       GfxElement[jx][jy] = EL_UNDEFINED;
3526
3527       /* make sure that pushed elements are drawn with correct frame rate */
3528       graphic = el_act_dir2img(element, ACTION_PUSHING, move_dir);
3529
3530       if (player->is_pushing && player->is_moving && !IS_ANIM_MODE_CE(graphic))
3531         GfxFrame[jx][jy] = player->StepFrame;
3532
3533       DrawLevelField(jx, jy);
3534     }
3535   }
3536
3537 #if !DRAW_PLAYER_OVER_PUSHED_ELEMENT
3538   /* ----------------------------------------------------------------------- */
3539   /* draw player himself                                                     */
3540   /* ----------------------------------------------------------------------- */
3541
3542   graphic = getPlayerGraphic(player, move_dir);
3543
3544   /* in the case of changed player action or direction, prevent the current
3545      animation frame from being restarted for identical animations */
3546   if (player->Frame == 0 && equalGraphics(graphic, last_player_graphic))
3547     player->Frame = last_player_frame;
3548
3549   frame = getGraphicAnimationFrame(graphic, player->Frame);
3550
3551   if (player->GfxPos)
3552   {
3553     if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
3554       sxx = player->GfxPos;
3555     else
3556       syy = player->GfxPos;
3557   }
3558
3559   if (player_is_opaque)
3560     DrawGraphicShifted(sx, sy, sxx, syy, graphic, frame,NO_CUTTING,NO_MASKING);
3561   else
3562     DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
3563
3564   if (SHIELD_ON(player))
3565   {
3566     int graphic = (player->shield_deadly_time_left ? IMG_SHIELD_DEADLY_ACTIVE :
3567                    IMG_SHIELD_NORMAL_ACTIVE);
3568     int frame = getGraphicAnimationFrame(graphic, -1);
3569
3570     DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
3571   }
3572 #endif
3573
3574 #if DRAW_PLAYER_OVER_PUSHED_ELEMENT
3575   if (player->GfxPos)
3576   {
3577     if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
3578       sxx = player->GfxPos;
3579     else
3580       syy = player->GfxPos;
3581   }
3582 #endif
3583
3584   /* ----------------------------------------------------------------------- */
3585   /* draw things the player is pushing, if needed                            */
3586   /* ----------------------------------------------------------------------- */
3587
3588   if (player->is_pushing && player->is_moving)
3589   {
3590     int px = SCREENX(jx), py = SCREENY(jy);
3591     int pxx = (TILEX - ABS(sxx)) * dx;
3592     int pyy = (TILEY - ABS(syy)) * dy;
3593     int gfx_frame = GfxFrame[jx][jy];
3594
3595     int graphic;
3596     int sync_frame;
3597     int frame;
3598
3599     if (!IS_MOVING(jx, jy))             /* push movement already finished */
3600     {
3601       element = Feld[next_jx][next_jy];
3602       gfx_frame = GfxFrame[next_jx][next_jy];
3603     }
3604
3605     graphic = el_act_dir2img(element, ACTION_PUSHING, move_dir);
3606
3607     sync_frame = (IS_ANIM_MODE_CE(graphic) ? gfx_frame : player->StepFrame);
3608     frame = getGraphicAnimationFrame(graphic, sync_frame);
3609
3610     /* draw background element under pushed element (like the Sokoban field) */
3611     if (game.use_masked_pushing && IS_MOVING(jx, jy))
3612     {
3613       /* this allows transparent pushing animation over non-black background */
3614
3615       if (Back[jx][jy])
3616         DrawLevelElement(jx, jy, Back[jx][jy]);
3617       else
3618         DrawLevelElement(jx, jy, EL_EMPTY);
3619
3620       if (Back[next_jx][next_jy])
3621         DrawLevelElement(next_jx, next_jy, Back[next_jx][next_jy]);
3622       else
3623         DrawLevelElement(next_jx, next_jy, EL_EMPTY);
3624     }
3625     else if (Back[next_jx][next_jy])
3626       DrawLevelElement(next_jx, next_jy, Back[next_jx][next_jy]);
3627
3628 #if 1
3629     /* do not draw (EM style) pushing animation when pushing is finished */
3630     /* (two-tile animations usually do not contain start and end frame) */
3631     if (graphic_info[graphic].double_movement && !IS_MOVING(jx, jy))
3632       DrawLevelElement(next_jx, next_jy, Feld[next_jx][next_jy]);
3633     else
3634       DrawGraphicShiftedThruMask(px, py, pxx, pyy, graphic, frame, NO_CUTTING);
3635 #else
3636     /* masked drawing is needed for EMC style (double) movement graphics */
3637     /* !!! (ONLY WHEN DRAWING PUSHED ELEMENT OVER THE PLAYER) !!! */
3638     DrawGraphicShiftedThruMask(px, py, pxx, pyy, graphic, frame, NO_CUTTING);
3639 #endif
3640   }
3641
3642 #if DRAW_PLAYER_OVER_PUSHED_ELEMENT
3643   /* ----------------------------------------------------------------------- */
3644   /* draw player himself                                                     */
3645   /* ----------------------------------------------------------------------- */
3646
3647   graphic = getPlayerGraphic(player, move_dir);
3648
3649   /* in the case of changed player action or direction, prevent the current
3650      animation frame from being restarted for identical animations */
3651   if (player->Frame == 0 && equalGraphics(graphic, last_player_graphic))
3652     player->Frame = last_player_frame;
3653
3654   frame = getGraphicAnimationFrame(graphic, player->Frame);
3655
3656   if (player->GfxPos)
3657   {
3658     if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
3659       sxx = player->GfxPos;
3660     else
3661       syy = player->GfxPos;
3662   }
3663
3664   if (player_is_opaque)
3665     DrawGraphicShifted(sx, sy, sxx, syy, graphic, frame,NO_CUTTING,NO_MASKING);
3666   else
3667     DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
3668
3669   if (SHIELD_ON(player))
3670   {
3671     int graphic = (player->shield_deadly_time_left ? IMG_SHIELD_DEADLY_ACTIVE :
3672                    IMG_SHIELD_NORMAL_ACTIVE);
3673     int frame = getGraphicAnimationFrame(graphic, -1);
3674
3675     DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
3676   }
3677 #endif
3678
3679   /* ----------------------------------------------------------------------- */
3680   /* draw things in front of player (active dynamite or dynabombs)           */
3681   /* ----------------------------------------------------------------------- */
3682
3683   if (IS_ACTIVE_BOMB(element))
3684   {
3685     graphic = el2img(element);
3686     frame = getGraphicAnimationFrame(graphic, GfxFrame[jx][jy]);
3687
3688     if (game.emulation == EMU_SUPAPLEX)
3689       DrawGraphic(sx, sy, IMG_SP_DISK_RED, frame);
3690     else
3691       DrawGraphicThruMask(sx, sy, graphic, frame);
3692   }
3693
3694   if (player_is_moving && last_element == EL_EXPLOSION)
3695   {
3696     int element = (GfxElement[last_jx][last_jy] != EL_UNDEFINED ?
3697                    GfxElement[last_jx][last_jy] :  EL_EMPTY);
3698     int graphic = el_act2img(element, ACTION_EXPLODING);
3699     int delay = (game.emulation == EMU_SUPAPLEX ? 3 : 2);
3700     int phase = ExplodePhase[last_jx][last_jy] - 1;
3701     int frame = getGraphicAnimationFrame(graphic, phase - delay);
3702
3703     if (phase >= delay)
3704       DrawGraphicThruMask(SCREENX(last_jx), SCREENY(last_jy), graphic, frame);
3705   }
3706
3707   /* ----------------------------------------------------------------------- */
3708   /* draw elements the player is just walking/passing through/under          */
3709   /* ----------------------------------------------------------------------- */
3710
3711   if (player_is_moving)
3712   {
3713     /* handle the field the player is leaving ... */
3714     if (IS_ACCESSIBLE_INSIDE(last_element))
3715       DrawLevelField(last_jx, last_jy);
3716     else if (IS_ACCESSIBLE_UNDER(last_element))
3717       DrawLevelFieldThruMask(last_jx, last_jy);
3718   }
3719
3720   /* do not redraw accessible elements if the player is just pushing them */
3721   if (!player_is_moving || !player->is_pushing)
3722   {
3723     /* ... and the field the player is entering */
3724     if (IS_ACCESSIBLE_INSIDE(element))
3725       DrawLevelField(jx, jy);
3726     else if (IS_ACCESSIBLE_UNDER(element))
3727       DrawLevelFieldThruMask(jx, jy);
3728   }
3729
3730   MarkTileDirty(sx, sy);
3731 }
3732
3733 /* ------------------------------------------------------------------------- */
3734
3735 void WaitForEventToContinue()
3736 {
3737   boolean still_wait = TRUE;
3738
3739   if (program.headless)
3740     return;
3741
3742   /* simulate releasing mouse button over last gadget, if still pressed */
3743   if (button_status)
3744     HandleGadgets(-1, -1, 0);
3745
3746   button_status = MB_RELEASED;
3747
3748   ClearEventQueue();
3749
3750   while (still_wait)
3751   {
3752     if (PendingEvent())
3753     {
3754       Event event;
3755
3756       NextEvent(&event);
3757
3758       switch (event.type)
3759       {
3760         case EVENT_BUTTONPRESS:
3761         case EVENT_KEYPRESS:
3762           still_wait = FALSE;
3763           break;
3764
3765         case EVENT_KEYRELEASE:
3766           ClearPlayerAction();
3767           break;
3768
3769         default:
3770           HandleOtherEvents(&event);
3771           break;
3772       }
3773     }
3774     else if (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED)
3775     {
3776       still_wait = FALSE;
3777     }
3778
3779     BackToFront();
3780   }
3781 }
3782
3783 #define MAX_REQUEST_LINES               13
3784 #define MAX_REQUEST_LINE_FONT1_LEN      7
3785 #define MAX_REQUEST_LINE_FONT2_LEN      10
3786
3787 static int RequestHandleEvents(unsigned int req_state)
3788 {
3789   boolean level_solved = (game_status == GAME_MODE_PLAYING &&
3790                           local_player->LevelSolved_GameEnd);
3791   int width  = request.width;
3792   int height = request.height;
3793   int sx, sy;
3794   int result;
3795
3796   setRequestPosition(&sx, &sy, FALSE);
3797
3798   button_status = MB_RELEASED;
3799
3800   request_gadget_id = -1;
3801   result = -1;
3802
3803   while (result < 0)
3804   {
3805     if (level_solved)
3806     {
3807       SetDrawtoField(DRAW_TO_FIELDBUFFER);
3808
3809       HandleGameActions();
3810
3811       SetDrawtoField(DRAW_TO_BACKBUFFER);
3812
3813       if (global.use_envelope_request)
3814       {
3815         /* copy current state of request area to middle of playfield area */
3816         BlitBitmap(bitmap_db_store_2, drawto, sx, sy, width, height, sx, sy);
3817       }
3818     }
3819
3820     if (PendingEvent())
3821     {
3822       Event event;
3823
3824       while (NextValidEvent(&event))
3825       {
3826         switch (event.type)
3827         {
3828           case EVENT_BUTTONPRESS:
3829           case EVENT_BUTTONRELEASE:
3830           case EVENT_MOTIONNOTIFY:
3831           {
3832             int mx, my;
3833
3834             if (event.type == EVENT_MOTIONNOTIFY)
3835             {
3836               if (!button_status)
3837                 continue;
3838
3839               motion_status = TRUE;
3840               mx = ((MotionEvent *) &event)->x;
3841               my = ((MotionEvent *) &event)->y;
3842             }
3843             else
3844             {
3845               motion_status = FALSE;
3846               mx = ((ButtonEvent *) &event)->x;
3847               my = ((ButtonEvent *) &event)->y;
3848               if (event.type == EVENT_BUTTONPRESS)
3849                 button_status = ((ButtonEvent *) &event)->button;
3850               else
3851                 button_status = MB_RELEASED;
3852             }
3853
3854             /* this sets 'request_gadget_id' */
3855             HandleGadgets(mx, my, button_status);
3856
3857             switch (request_gadget_id)
3858             {
3859               case TOOL_CTRL_ID_YES:
3860                 result = TRUE;
3861                 break;
3862               case TOOL_CTRL_ID_NO:
3863                 result = FALSE;
3864                 break;
3865               case TOOL_CTRL_ID_CONFIRM:
3866                 result = TRUE | FALSE;
3867                 break;
3868
3869               case TOOL_CTRL_ID_PLAYER_1:
3870                 result = 1;
3871                 break;
3872               case TOOL_CTRL_ID_PLAYER_2:
3873                 result = 2;
3874                 break;
3875               case TOOL_CTRL_ID_PLAYER_3:
3876                 result = 3;
3877                 break;
3878               case TOOL_CTRL_ID_PLAYER_4:
3879                 result = 4;
3880                 break;
3881
3882               default:
3883                 break;
3884             }
3885
3886             break;
3887           }
3888
3889 #if defined(TARGET_SDL2)
3890           case SDL_WINDOWEVENT:
3891             HandleWindowEvent((WindowEvent *) &event);
3892             break;
3893
3894           case SDL_APP_WILLENTERBACKGROUND:
3895           case SDL_APP_DIDENTERBACKGROUND:
3896           case SDL_APP_WILLENTERFOREGROUND:
3897           case SDL_APP_DIDENTERFOREGROUND:
3898             HandlePauseResumeEvent((PauseResumeEvent *) &event);
3899             break;
3900 #endif
3901
3902           case EVENT_KEYPRESS:
3903           {
3904             Key key = GetEventKey((KeyEvent *)&event, TRUE);
3905
3906             switch (key)
3907             {
3908               case KSYM_space:
3909                 if (req_state & REQ_CONFIRM)
3910                   result = 1;
3911                 break;
3912
3913               case KSYM_Return:
3914 #if defined(TARGET_SDL2)
3915               case KSYM_Menu:
3916 #if defined(KSYM_Rewind)
3917               case KSYM_Rewind:         /* for Amazon Fire TV remote */
3918 #endif
3919 #endif
3920                 result = 1;
3921                 break;
3922
3923               case KSYM_Escape:
3924 #if defined(TARGET_SDL2)
3925               case KSYM_Back:
3926 #if defined(KSYM_FastForward)
3927               case KSYM_FastForward:    /* for Amazon Fire TV remote */
3928 #endif
3929 #endif
3930                 result = 0;
3931                 break;
3932
3933               default:
3934                 HandleKeysDebug(key);
3935                 break;
3936             }
3937
3938             if (req_state & REQ_PLAYER)
3939               result = 0;
3940
3941             break;
3942           }
3943
3944           case EVENT_KEYRELEASE:
3945             ClearPlayerAction();
3946             break;
3947
3948 #if defined(TARGET_SDL2)
3949           case SDL_CONTROLLERBUTTONDOWN:
3950             switch (event.cbutton.button)
3951             {
3952               case SDL_CONTROLLER_BUTTON_A:
3953               case SDL_CONTROLLER_BUTTON_X:
3954               case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
3955                 result = 1;
3956                 break;
3957
3958               case SDL_CONTROLLER_BUTTON_B:
3959               case SDL_CONTROLLER_BUTTON_Y:
3960               case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
3961               case SDL_CONTROLLER_BUTTON_BACK:
3962                 result = 0;
3963                 break;
3964             }
3965
3966             if (req_state & REQ_PLAYER)
3967               result = 0;
3968
3969             break;
3970
3971           case SDL_CONTROLLERBUTTONUP:
3972             HandleJoystickEvent(&event);
3973             ClearPlayerAction();
3974             break;
3975 #endif
3976
3977           default:
3978             HandleOtherEvents(&event);
3979             break;
3980         }
3981       }
3982     }
3983     else if (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED)
3984     {
3985       int joy = AnyJoystick();
3986
3987       if (joy & JOY_BUTTON_1)
3988         result = 1;
3989       else if (joy & JOY_BUTTON_2)
3990         result = 0;
3991     }
3992
3993     if (level_solved)
3994     {
3995       if (global.use_envelope_request)
3996       {
3997         /* copy back current state of pressed buttons inside request area */
3998         BlitBitmap(drawto, bitmap_db_store_2, sx, sy, width, height, sx, sy);
3999       }
4000     }
4001
4002     BackToFront();
4003   }
4004
4005   return result;
4006 }
4007
4008 static boolean RequestDoor(char *text, unsigned int req_state)
4009 {
4010   unsigned int old_door_state;
4011   int max_request_line_len = MAX_REQUEST_LINE_FONT1_LEN;
4012   int font_nr = FONT_TEXT_2;
4013   char *text_ptr;
4014   int result;
4015   int ty;
4016
4017   if (maxWordLengthInString(text) > MAX_REQUEST_LINE_FONT1_LEN)
4018   {
4019     max_request_line_len = MAX_REQUEST_LINE_FONT2_LEN;
4020     font_nr = FONT_TEXT_1;
4021   }
4022
4023   if (game_status == GAME_MODE_PLAYING)
4024     BlitScreenToBitmap(backbuffer);
4025
4026   /* disable deactivated drawing when quick-loading level tape recording */
4027   if (tape.playing && tape.deactivate_display)
4028     TapeDeactivateDisplayOff(TRUE);
4029
4030   SetMouseCursor(CURSOR_DEFAULT);
4031
4032 #if defined(NETWORK_AVALIABLE)
4033   /* pause network game while waiting for request to answer */
4034   if (options.network &&
4035       game_status == GAME_MODE_PLAYING &&
4036       req_state & REQUEST_WAIT_FOR_INPUT)
4037     SendToServer_PausePlaying();
4038 #endif
4039
4040   old_door_state = GetDoorState();
4041
4042   /* simulate releasing mouse button over last gadget, if still pressed */
4043   if (button_status)
4044     HandleGadgets(-1, -1, 0);
4045
4046   UnmapAllGadgets();
4047
4048   /* draw released gadget before proceeding */
4049   // BackToFront();
4050
4051   if (old_door_state & DOOR_OPEN_1)
4052   {
4053     CloseDoor(DOOR_CLOSE_1);
4054
4055     /* save old door content */
4056     BlitBitmap(bitmap_db_door_1, bitmap_db_door_1,
4057                0 * DXSIZE, 0, DXSIZE, DYSIZE, 1 * DXSIZE, 0);
4058   }
4059
4060   SetDoorBackgroundImage(IMG_BACKGROUND_DOOR);
4061   SetDrawBackgroundMask(REDRAW_FIELD | REDRAW_DOOR_1);
4062
4063   /* clear door drawing field */
4064   DrawBackground(DX, DY, DXSIZE, DYSIZE);
4065
4066   /* force DOOR font inside door area */
4067   SetFontStatus(GAME_MODE_PSEUDO_DOOR);
4068
4069   /* write text for request */
4070   for (text_ptr = text, ty = 0; ty < MAX_REQUEST_LINES; ty++)
4071   {
4072     char text_line[max_request_line_len + 1];
4073     int tx, tl, tc = 0;
4074
4075     if (!*text_ptr)
4076       break;
4077
4078     for (tl = 0, tx = 0; tx < max_request_line_len; tl++, tx++)
4079     {
4080       tc = *(text_ptr + tx);
4081       // if (!tc || tc == ' ')
4082       if (!tc || tc == ' ' || tc == '?' || tc == '!')
4083         break;
4084     }
4085
4086     if ((tc == '?' || tc == '!') && tl == 0)
4087       tl = 1;
4088
4089     if (!tl)
4090     { 
4091       text_ptr++; 
4092       ty--; 
4093       continue; 
4094     }
4095
4096     strncpy(text_line, text_ptr, tl);
4097     text_line[tl] = 0;
4098
4099     DrawText(DX + (DXSIZE - tl * getFontWidth(font_nr)) / 2,
4100              DY + 8 + ty * (getFontHeight(font_nr) + 2),
4101              text_line, font_nr);
4102
4103     text_ptr += tl + (tc == ' ' ? 1 : 0);
4104     // text_ptr += tl + (tc == ' ' || tc == '?' || tc == '!' ? 1 : 0);
4105   }
4106
4107   ResetFontStatus();
4108
4109   if (req_state & REQ_ASK)
4110   {
4111     MapGadget(tool_gadget[TOOL_CTRL_ID_YES]);
4112     MapGadget(tool_gadget[TOOL_CTRL_ID_NO]);
4113   }
4114   else if (req_state & REQ_CONFIRM)
4115   {
4116     MapGadget(tool_gadget[TOOL_CTRL_ID_CONFIRM]);
4117   }
4118   else if (req_state & REQ_PLAYER)
4119   {
4120     MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_1]);
4121     MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_2]);
4122     MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_3]);
4123     MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_4]);
4124   }
4125
4126   /* copy request gadgets to door backbuffer */
4127   BlitBitmap(drawto, bitmap_db_door_1, DX, DY, DXSIZE, DYSIZE, 0, 0);
4128
4129   OpenDoor(DOOR_OPEN_1);
4130
4131   if (!(req_state & REQUEST_WAIT_FOR_INPUT))
4132   {
4133     if (game_status == GAME_MODE_PLAYING)
4134     {
4135       SetPanelBackground();
4136       SetDrawBackgroundMask(REDRAW_DOOR_1);
4137     }
4138     else
4139     {
4140       SetDrawBackgroundMask(REDRAW_FIELD);
4141     }
4142
4143     return FALSE;
4144   }
4145
4146   SetDrawBackgroundMask(REDRAW_FIELD | REDRAW_DOOR_1);
4147
4148   // ---------- handle request buttons ----------
4149   result = RequestHandleEvents(req_state);
4150
4151   UnmapToolButtons();
4152
4153   if (!(req_state & REQ_STAY_OPEN))
4154   {
4155     CloseDoor(DOOR_CLOSE_1);
4156
4157     if (((old_door_state & DOOR_OPEN_1) && !(req_state & REQ_STAY_CLOSED)) ||
4158         (req_state & REQ_REOPEN))
4159       OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
4160   }
4161
4162   RemapAllGadgets();
4163
4164   if (game_status == GAME_MODE_PLAYING)
4165   {
4166     SetPanelBackground();
4167     SetDrawBackgroundMask(REDRAW_DOOR_1);
4168   }
4169   else
4170   {
4171     SetDrawBackgroundMask(REDRAW_FIELD);
4172   }
4173
4174 #if defined(NETWORK_AVALIABLE)
4175   /* continue network game after request */
4176   if (options.network &&
4177       game_status == GAME_MODE_PLAYING &&
4178       req_state & REQUEST_WAIT_FOR_INPUT)
4179     SendToServer_ContinuePlaying();
4180 #endif
4181
4182   /* restore deactivated drawing when quick-loading level tape recording */
4183   if (tape.playing && tape.deactivate_display)
4184     TapeDeactivateDisplayOn();
4185
4186   return result;
4187 }
4188
4189 static boolean RequestEnvelope(char *text, unsigned int req_state)
4190 {
4191   int result;
4192
4193   if (game_status == GAME_MODE_PLAYING)
4194     BlitScreenToBitmap(backbuffer);
4195
4196   /* disable deactivated drawing when quick-loading level tape recording */
4197   if (tape.playing && tape.deactivate_display)
4198     TapeDeactivateDisplayOff(TRUE);
4199
4200   SetMouseCursor(CURSOR_DEFAULT);
4201
4202 #if defined(NETWORK_AVALIABLE)
4203   /* pause network game while waiting for request to answer */
4204   if (options.network &&
4205       game_status == GAME_MODE_PLAYING &&
4206       req_state & REQUEST_WAIT_FOR_INPUT)
4207     SendToServer_PausePlaying();
4208 #endif
4209
4210   /* simulate releasing mouse button over last gadget, if still pressed */
4211   if (button_status)
4212     HandleGadgets(-1, -1, 0);
4213
4214   UnmapAllGadgets();
4215
4216   // (replace with setting corresponding request background)
4217   // SetDoorBackgroundImage(IMG_BACKGROUND_DOOR);
4218   // SetDrawBackgroundMask(REDRAW_FIELD | REDRAW_DOOR_1);
4219
4220   /* clear door drawing field */
4221   // DrawBackground(DX, DY, DXSIZE, DYSIZE);
4222
4223   ShowEnvelopeRequest(text, req_state, ACTION_OPENING);
4224
4225   if (!(req_state & REQUEST_WAIT_FOR_INPUT))
4226   {
4227     if (game_status == GAME_MODE_PLAYING)
4228     {
4229       SetPanelBackground();
4230       SetDrawBackgroundMask(REDRAW_DOOR_1);
4231     }
4232     else
4233     {
4234       SetDrawBackgroundMask(REDRAW_FIELD);
4235     }
4236
4237     return FALSE;
4238   }
4239
4240   SetDrawBackgroundMask(REDRAW_FIELD | REDRAW_DOOR_1);
4241
4242   // ---------- handle request buttons ----------
4243   result = RequestHandleEvents(req_state);
4244
4245   UnmapToolButtons();
4246
4247   ShowEnvelopeRequest(text, req_state, ACTION_CLOSING);
4248
4249   RemapAllGadgets();
4250
4251   if (game_status == GAME_MODE_PLAYING)
4252   {
4253     SetPanelBackground();
4254     SetDrawBackgroundMask(REDRAW_DOOR_1);
4255   }
4256   else
4257   {
4258     SetDrawBackgroundMask(REDRAW_FIELD);
4259   }
4260
4261 #if defined(NETWORK_AVALIABLE)
4262   /* continue network game after request */
4263   if (options.network &&
4264       game_status == GAME_MODE_PLAYING &&
4265       req_state & REQUEST_WAIT_FOR_INPUT)
4266     SendToServer_ContinuePlaying();
4267 #endif
4268
4269   /* restore deactivated drawing when quick-loading level tape recording */
4270   if (tape.playing && tape.deactivate_display)
4271     TapeDeactivateDisplayOn();
4272
4273   return result;
4274 }
4275
4276 boolean Request(char *text, unsigned int req_state)
4277 {
4278   boolean overlay_active = GetOverlayActive();
4279   boolean result;
4280
4281   SetOverlayActive(FALSE);
4282
4283   if (global.use_envelope_request)
4284     result = RequestEnvelope(text, req_state);
4285   else
4286     result = RequestDoor(text, req_state);
4287
4288   SetOverlayActive(overlay_active);
4289
4290   return result;
4291 }
4292
4293 static int compareDoorPartOrderInfo(const void *object1, const void *object2)
4294 {
4295   const struct DoorPartOrderInfo *dpo1 = (struct DoorPartOrderInfo *)object1;
4296   const struct DoorPartOrderInfo *dpo2 = (struct DoorPartOrderInfo *)object2;
4297   int compare_result;
4298
4299   if (dpo1->sort_priority != dpo2->sort_priority)
4300     compare_result = dpo1->sort_priority - dpo2->sort_priority;
4301   else
4302     compare_result = dpo1->nr - dpo2->nr;
4303
4304   return compare_result;
4305 }
4306
4307 void InitGraphicCompatibilityInfo_Doors()
4308 {
4309   struct
4310   {
4311     int door_token;
4312     int part_1, part_8;
4313     struct DoorInfo *door;
4314   }
4315   doors[] =
4316   {
4317     { DOOR_1,   IMG_GFX_DOOR_1_PART_1,  IMG_GFX_DOOR_1_PART_8,  &door_1 },
4318     { DOOR_2,   IMG_GFX_DOOR_2_PART_1,  IMG_GFX_DOOR_2_PART_8,  &door_2 },
4319
4320     { -1,       -1,                     -1,                     NULL    }
4321   };
4322   struct Rect door_rect_list[] =
4323   {
4324     { DX, DY, DXSIZE, DYSIZE },
4325     { VX, VY, VXSIZE, VYSIZE }
4326   };
4327   int i, j;
4328
4329   for (i = 0; doors[i].door_token != -1; i++)
4330   {
4331     int door_token = doors[i].door_token;
4332     int door_index = DOOR_INDEX_FROM_TOKEN(door_token);
4333     int part_1 = doors[i].part_1;
4334     int part_8 = doors[i].part_8;
4335     int part_2 = part_1 + 1;
4336     int part_3 = part_1 + 2;
4337     struct DoorInfo *door = doors[i].door;
4338     struct Rect *door_rect = &door_rect_list[door_index];
4339     boolean door_gfx_redefined = FALSE;
4340
4341     /* check if any door part graphic definitions have been redefined */
4342
4343     for (j = 0; door_part_controls[j].door_token != -1; j++)
4344     {
4345       struct DoorPartControlInfo *dpc = &door_part_controls[j];
4346       struct FileInfo *fi = getImageListEntryFromImageID(dpc->graphic);
4347
4348       if (dpc->door_token == door_token && fi->redefined)
4349         door_gfx_redefined = TRUE;
4350     }
4351
4352     /* check for old-style door graphic/animation modifications */
4353
4354     if (!door_gfx_redefined)
4355     {
4356       if (door->anim_mode & ANIM_STATIC_PANEL)
4357       {
4358         door->panel.step_xoffset = 0;
4359         door->panel.step_yoffset = 0;
4360       }
4361
4362       if (door->anim_mode & (ANIM_HORIZONTAL | ANIM_VERTICAL))
4363       {
4364         struct GraphicInfo *g_part_1 = &graphic_info[part_1];
4365         struct GraphicInfo *g_part_2 = &graphic_info[part_2];
4366         int num_door_steps, num_panel_steps;
4367
4368         /* remove door part graphics other than the two default wings */
4369
4370         for (j = 0; door_part_controls[j].door_token != -1; j++)
4371         {
4372           struct DoorPartControlInfo *dpc = &door_part_controls[j];
4373           struct GraphicInfo *g = &graphic_info[dpc->graphic];
4374
4375           if (dpc->graphic >= part_3 &&
4376               dpc->graphic <= part_8)
4377             g->bitmap = NULL;
4378         }
4379
4380         /* set graphics and screen positions of the default wings */
4381
4382         g_part_1->width  = door_rect->width;
4383         g_part_1->height = door_rect->height;
4384         g_part_2->width  = door_rect->width;
4385         g_part_2->height = door_rect->height;
4386         g_part_2->src_x = door_rect->width;
4387         g_part_2->src_y = g_part_1->src_y;
4388
4389         door->part_2.x = door->part_1.x;
4390         door->part_2.y = door->part_1.y;
4391
4392         if (door->width != -1)
4393         {
4394           g_part_1->width = door->width;
4395           g_part_2->width = door->width;
4396
4397           // special treatment for graphics and screen position of right wing
4398           g_part_2->src_x += door_rect->width - door->width;
4399           door->part_2.x  += door_rect->width - door->width;
4400         }
4401
4402         if (door->height != -1)
4403         {
4404           g_part_1->height = door->height;
4405           g_part_2->height = door->height;
4406
4407           // special treatment for graphics and screen position of bottom wing
4408           g_part_2->src_y += door_rect->height - door->height;
4409           door->part_2.y  += door_rect->height - door->height;
4410         }
4411
4412         /* set animation delays for the default wings and panels */
4413
4414         door->part_1.step_delay = door->step_delay;
4415         door->part_2.step_delay = door->step_delay;
4416         door->panel.step_delay  = door->step_delay;
4417
4418         /* set animation draw order for the default wings */
4419
4420         door->part_1.sort_priority = 2; /* draw left wing over ... */
4421         door->part_2.sort_priority = 1; /*          ... right wing */
4422
4423         /* set animation draw offset for the default wings */
4424
4425         if (door->anim_mode & ANIM_HORIZONTAL)
4426         {
4427           door->part_1.step_xoffset = door->step_offset;
4428           door->part_1.step_yoffset = 0;
4429           door->part_2.step_xoffset = door->step_offset * -1;
4430           door->part_2.step_yoffset = 0;
4431
4432           num_door_steps = g_part_1->width / door->step_offset;
4433         }
4434         else    // ANIM_VERTICAL
4435         {
4436           door->part_1.step_xoffset = 0;
4437           door->part_1.step_yoffset = door->step_offset;
4438           door->part_2.step_xoffset = 0;
4439           door->part_2.step_yoffset = door->step_offset * -1;
4440
4441           num_door_steps = g_part_1->height / door->step_offset;
4442         }
4443
4444         /* set animation draw offset for the default panels */
4445
4446         if (door->step_offset > 1)
4447         {
4448           num_panel_steps = 2 * door_rect->height / door->step_offset;
4449           door->panel.start_step = num_panel_steps - num_door_steps;
4450           door->panel.start_step_closing = door->panel.start_step;
4451         }
4452         else
4453         {
4454           num_panel_steps = door_rect->height / door->step_offset;
4455           door->panel.start_step = num_panel_steps - num_door_steps / 2;
4456           door->panel.start_step_closing = door->panel.start_step;
4457           door->panel.step_delay *= 2;
4458         }
4459       }
4460     }
4461   }
4462 }
4463
4464 void InitDoors()
4465 {
4466   int i;
4467
4468   for (i = 0; door_part_controls[i].door_token != -1; i++)
4469   {
4470     struct DoorPartControlInfo *dpc = &door_part_controls[i];
4471     struct DoorPartOrderInfo *dpo = &door_part_order[i];
4472
4473     /* initialize "start_step_opening" and "start_step_closing", if needed */
4474     if (dpc->pos->start_step_opening == 0 &&
4475         dpc->pos->start_step_closing == 0)
4476     {
4477       // dpc->pos->start_step_opening = dpc->pos->start_step;
4478       dpc->pos->start_step_closing = dpc->pos->start_step;
4479     }
4480
4481     /* fill structure for door part draw order (sorted below) */
4482     dpo->nr = i;
4483     dpo->sort_priority = dpc->pos->sort_priority;
4484   }
4485
4486   /* sort door part controls according to sort_priority and graphic number */
4487   qsort(door_part_order, MAX_DOOR_PARTS,
4488         sizeof(struct DoorPartOrderInfo), compareDoorPartOrderInfo);
4489 }
4490
4491 unsigned int OpenDoor(unsigned int door_state)
4492 {
4493   if (door_state & DOOR_COPY_BACK)
4494   {
4495     if (door_state & DOOR_OPEN_1)
4496       BlitBitmap(bitmap_db_door_1, bitmap_db_door_1,
4497                  1 * DXSIZE, 0, DXSIZE, DYSIZE, 0 * DXSIZE, 0);
4498
4499     if (door_state & DOOR_OPEN_2)
4500       BlitBitmap(bitmap_db_door_2, bitmap_db_door_2,
4501                  1 * VXSIZE, 0, VXSIZE, VYSIZE, 0 * VXSIZE, 0);
4502
4503     door_state &= ~DOOR_COPY_BACK;
4504   }
4505
4506   return MoveDoor(door_state);
4507 }
4508
4509 unsigned int CloseDoor(unsigned int door_state)
4510 {
4511   unsigned int old_door_state = GetDoorState();
4512
4513   if (!(door_state & DOOR_NO_COPY_BACK))
4514   {
4515     if (old_door_state & DOOR_OPEN_1)
4516       BlitBitmap(backbuffer, bitmap_db_door_1,
4517                  DX, DY, DXSIZE, DYSIZE, 0, 0);
4518
4519     if (old_door_state & DOOR_OPEN_2)
4520       BlitBitmap(backbuffer, bitmap_db_door_2,
4521                  VX, VY, VXSIZE, VYSIZE, 0, 0);
4522
4523     door_state &= ~DOOR_NO_COPY_BACK;
4524   }
4525
4526   return MoveDoor(door_state);
4527 }
4528
4529 unsigned int GetDoorState()
4530 {
4531   return MoveDoor(DOOR_GET_STATE);
4532 }
4533
4534 unsigned int SetDoorState(unsigned int door_state)
4535 {
4536   return MoveDoor(door_state | DOOR_SET_STATE);
4537 }
4538
4539 int euclid(int a, int b)
4540 {
4541   return (b ? euclid(b, a % b) : a);
4542 }
4543
4544 unsigned int MoveDoor(unsigned int door_state)
4545 {
4546   struct Rect door_rect_list[] =
4547   {
4548     { DX, DY, DXSIZE, DYSIZE },
4549     { VX, VY, VXSIZE, VYSIZE }
4550   };
4551   static int door1 = DOOR_CLOSE_1;
4552   static int door2 = DOOR_CLOSE_2;
4553   unsigned int door_delay = 0;
4554   unsigned int door_delay_value;
4555   int i;
4556
4557   if (door_state == DOOR_GET_STATE)
4558     return (door1 | door2);
4559
4560   if (door_state & DOOR_SET_STATE)
4561   {
4562     if (door_state & DOOR_ACTION_1)
4563       door1 = door_state & DOOR_ACTION_1;
4564     if (door_state & DOOR_ACTION_2)
4565       door2 = door_state & DOOR_ACTION_2;
4566
4567     return (door1 | door2);
4568   }
4569
4570   if (!(door_state & DOOR_FORCE_REDRAW))
4571   {
4572     if (door1 == DOOR_OPEN_1 && door_state & DOOR_OPEN_1)
4573       door_state &= ~DOOR_OPEN_1;
4574     else if (door1 == DOOR_CLOSE_1 && door_state & DOOR_CLOSE_1)
4575       door_state &= ~DOOR_CLOSE_1;
4576     if (door2 == DOOR_OPEN_2 && door_state & DOOR_OPEN_2)
4577       door_state &= ~DOOR_OPEN_2;
4578     else if (door2 == DOOR_CLOSE_2 && door_state & DOOR_CLOSE_2)
4579       door_state &= ~DOOR_CLOSE_2;
4580   }
4581
4582   if (global.autoplay_leveldir)
4583   {
4584     door_state |= DOOR_NO_DELAY;
4585     door_state &= ~DOOR_CLOSE_ALL;
4586   }
4587
4588   if (game_status == GAME_MODE_EDITOR)
4589     door_state |= DOOR_NO_DELAY;
4590
4591   if (door_state & DOOR_ACTION)
4592   {
4593     boolean door_panel_drawn[NUM_DOORS];
4594     boolean panel_has_doors[NUM_DOORS];
4595     boolean door_part_skip[MAX_DOOR_PARTS];
4596     boolean door_part_done[MAX_DOOR_PARTS];
4597     boolean door_part_done_all;
4598     int num_steps[MAX_DOOR_PARTS];
4599     int max_move_delay = 0;     // delay for complete animations of all doors
4600     int max_step_delay = 0;     // delay (ms) between two animation frames
4601     int num_move_steps = 0;     // number of animation steps for all doors
4602     int max_move_delay_doors_only = 0;  // delay for doors only (no panel)
4603     int num_move_steps_doors_only = 0;  // steps for doors only (no panel)
4604     int current_move_delay = 0;
4605     int start = 0;
4606     int k;
4607
4608     for (i = 0; i < NUM_DOORS; i++)
4609       panel_has_doors[i] = FALSE;
4610
4611     for (i = 0; i < MAX_DOOR_PARTS; i++)
4612     {
4613       struct DoorPartControlInfo *dpc = &door_part_controls[i];
4614       struct GraphicInfo *g = &graphic_info[dpc->graphic];
4615       int door_token = dpc->door_token;
4616
4617       door_part_done[i] = FALSE;
4618       door_part_skip[i] = (!(door_state & door_token) ||
4619                            !g->bitmap);
4620     }
4621
4622     for (i = 0; i < MAX_DOOR_PARTS; i++)
4623     {
4624       int nr = door_part_order[i].nr;
4625       struct DoorPartControlInfo *dpc = &door_part_controls[nr];
4626       struct DoorPartPosInfo *pos = dpc->pos;
4627       struct GraphicInfo *g = &graphic_info[dpc->graphic];
4628       int door_token = dpc->door_token;
4629       int door_index = DOOR_INDEX_FROM_TOKEN(door_token);
4630       boolean is_panel = DOOR_PART_IS_PANEL(nr);
4631       int step_xoffset = ABS(pos->step_xoffset);
4632       int step_yoffset = ABS(pos->step_yoffset);
4633       int step_delay = pos->step_delay;
4634       int current_door_state = door_state & door_token;
4635       boolean door_opening = ((current_door_state & DOOR_OPEN)  != 0);
4636       boolean door_closing = ((current_door_state & DOOR_CLOSE) != 0);
4637       boolean part_opening = (is_panel ? door_closing : door_opening);
4638       int start_step = (part_opening ? pos->start_step_opening :
4639                         pos->start_step_closing);
4640       float move_xsize = (step_xoffset ? g->width  : 0);
4641       float move_ysize = (step_yoffset ? g->height : 0);
4642       int move_xsteps = (step_xoffset ? ceil(move_xsize / step_xoffset) : 0);
4643       int move_ysteps = (step_yoffset ? ceil(move_ysize / step_yoffset) : 0);
4644       int move_steps = (move_xsteps && move_ysteps ?
4645                         MIN(move_xsteps, move_ysteps) :
4646                         move_xsteps ? move_xsteps : move_ysteps) - start_step;
4647       int move_delay = move_steps * step_delay;
4648
4649       if (door_part_skip[nr])
4650         continue;
4651
4652       max_move_delay = MAX(max_move_delay, move_delay);
4653       max_step_delay = (max_step_delay == 0 ? step_delay :
4654                         euclid(max_step_delay, step_delay));
4655       num_steps[nr] = move_steps;
4656
4657       if (!is_panel)
4658       {
4659         max_move_delay_doors_only = MAX(max_move_delay_doors_only, move_delay);
4660
4661         panel_has_doors[door_index] = TRUE;
4662       }
4663     }
4664
4665     max_step_delay = MAX(1, max_step_delay);    // prevent division by zero
4666
4667     num_move_steps = max_move_delay / max_step_delay;
4668     num_move_steps_doors_only = max_move_delay_doors_only / max_step_delay;
4669
4670     door_delay_value = max_step_delay;
4671
4672     if ((door_state & DOOR_NO_DELAY) || setup.quick_doors)
4673     {
4674       start = num_move_steps - 1;
4675     }
4676     else
4677     {
4678       /* opening door sound has priority over simultaneously closing door */
4679       if (door_state & (DOOR_OPEN_1 | DOOR_OPEN_2))
4680         PlayMenuSoundStereo(SND_DOOR_OPENING, SOUND_MIDDLE);
4681       else if (door_state & (DOOR_CLOSE_1 | DOOR_CLOSE_2))
4682         PlayMenuSoundStereo(SND_DOOR_CLOSING, SOUND_MIDDLE);
4683     }
4684
4685     for (k = start; k < num_move_steps; k++)
4686     {
4687       int last_frame = num_move_steps - 1;      // last frame of this "for" loop
4688
4689       door_part_done_all = TRUE;
4690
4691       for (i = 0; i < NUM_DOORS; i++)
4692         door_panel_drawn[i] = FALSE;
4693
4694       for (i = 0; i < MAX_DOOR_PARTS; i++)
4695       {
4696         int nr = door_part_order[i].nr;
4697         struct DoorPartControlInfo *dpc = &door_part_controls[nr];
4698         struct DoorPartPosInfo *pos = dpc->pos;
4699         struct GraphicInfo *g = &graphic_info[dpc->graphic];
4700         int door_token = dpc->door_token;
4701         int door_index = DOOR_INDEX_FROM_TOKEN(door_token);
4702         boolean is_panel = DOOR_PART_IS_PANEL(nr);
4703         boolean is_panel_and_door_has_closed = FALSE;
4704         struct Rect *door_rect = &door_rect_list[door_index];
4705         Bitmap *bitmap_db_door = (door_token == DOOR_1 ? bitmap_db_door_1 :
4706                                   bitmap_db_door_2);
4707         Bitmap *bitmap = (is_panel ? bitmap_db_door : g->bitmap);
4708         int current_door_state = door_state & door_token;
4709         boolean door_opening = ((current_door_state & DOOR_OPEN)  != 0);
4710         boolean door_closing = !door_opening;
4711         boolean part_opening = (is_panel ? door_closing : door_opening);
4712         boolean part_closing = !part_opening;
4713         int start_step = (part_opening ? pos->start_step_opening :
4714                           pos->start_step_closing);
4715         int step_delay = pos->step_delay;
4716         int step_factor = step_delay / max_step_delay;
4717         int k1 = (step_factor ? k / step_factor + 1 : k);
4718         int k2 = (part_opening ? k1 + start_step : num_steps[nr] - k1);
4719         int kk = MAX(0, k2);
4720         int g_src_x = 0;
4721         int g_src_y = 0;
4722         int src_x, src_y, src_xx, src_yy;
4723         int dst_x, dst_y, dst_xx, dst_yy;
4724         int width, height;
4725
4726         if (door_part_skip[nr])
4727           continue;
4728
4729         if (!(door_state & door_token))
4730           continue;
4731
4732         if (!g->bitmap)
4733           continue;
4734
4735         if (!is_panel)
4736         {
4737           int k2_door = (door_opening ? k : num_move_steps_doors_only - k - 1);
4738           int kk_door = MAX(0, k2_door);
4739           int sync_frame = kk_door * door_delay_value;
4740           int frame = getGraphicAnimationFrame(dpc->graphic, sync_frame);
4741
4742           getFixedGraphicSource(dpc->graphic, frame, &bitmap,
4743                                 &g_src_x, &g_src_y);
4744         }
4745
4746         // draw door panel
4747
4748         if (!door_panel_drawn[door_index])
4749         {
4750           ClearRectangle(drawto, door_rect->x, door_rect->y,
4751                          door_rect->width, door_rect->height);
4752
4753           door_panel_drawn[door_index] = TRUE;
4754         }
4755
4756         // draw opening or closing door parts
4757
4758         if (pos->step_xoffset < 0)      // door part on right side
4759         {
4760           src_xx = 0;
4761           dst_xx = pos->x + ABS(kk * pos->step_xoffset);
4762           width = g->width;
4763
4764           if (dst_xx + width > door_rect->width)
4765             width = door_rect->width - dst_xx;
4766         }
4767         else                            // door part on left side
4768         {
4769           src_xx = 0;
4770           dst_xx = pos->x - kk * pos->step_xoffset;
4771
4772           if (dst_xx < 0)
4773           {
4774             src_xx = ABS(dst_xx);
4775             dst_xx = 0;
4776           }
4777
4778           width = g->width - src_xx;
4779
4780           if (width > door_rect->width)
4781             width = door_rect->width;
4782
4783           // printf("::: k == %d [%d] \n", k, start_step);
4784         }
4785
4786         if (pos->step_yoffset < 0)      // door part on bottom side
4787         {
4788           src_yy = 0;
4789           dst_yy = pos->y + ABS(kk * pos->step_yoffset);
4790           height = g->height;
4791
4792           if (dst_yy + height > door_rect->height)
4793             height = door_rect->height - dst_yy;
4794         }
4795         else                            // door part on top side
4796         {
4797           src_yy = 0;
4798           dst_yy = pos->y - kk * pos->step_yoffset;
4799
4800           if (dst_yy < 0)
4801           {
4802             src_yy = ABS(dst_yy);
4803             dst_yy = 0;
4804           }
4805
4806           height = g->height - src_yy;
4807         }
4808
4809         src_x = g_src_x + src_xx;
4810         src_y = g_src_y + src_yy;
4811
4812         dst_x = door_rect->x + dst_xx;
4813         dst_y = door_rect->y + dst_yy;
4814
4815         is_panel_and_door_has_closed =
4816           (is_panel &&
4817            door_closing &&
4818            panel_has_doors[door_index] &&
4819            k >= num_move_steps_doors_only - 1);
4820
4821         if (width  >= 0 && width  <= g->width &&
4822             height >= 0 && height <= g->height &&
4823             !is_panel_and_door_has_closed)
4824         {
4825           if (is_panel || !pos->draw_masked)
4826             BlitBitmap(bitmap, drawto, src_x, src_y, width, height,
4827                        dst_x, dst_y);
4828           else
4829             BlitBitmapMasked(bitmap, drawto, src_x, src_y, width, height,
4830                              dst_x, dst_y);
4831         }
4832
4833         redraw_mask |= REDRAW_DOOR_FROM_TOKEN(door_token);
4834
4835         if ((part_opening && (width < 0         || height < 0)) ||
4836             (part_closing && (width >= g->width && height >= g->height)))
4837           door_part_done[nr] = TRUE;
4838
4839         // continue door part animations, but not panel after door has closed
4840         if (!door_part_done[nr] && !is_panel_and_door_has_closed)
4841           door_part_done_all = FALSE;
4842       }
4843
4844       if (!(door_state & DOOR_NO_DELAY))
4845       {
4846         BackToFront();
4847
4848         SkipUntilDelayReached(&door_delay, door_delay_value, &k, last_frame);
4849
4850         current_move_delay += max_step_delay;
4851       }
4852
4853       if (door_part_done_all)
4854         break;
4855     }
4856   }
4857
4858   if (door_state & DOOR_ACTION_1)
4859     door1 = door_state & DOOR_ACTION_1;
4860   if (door_state & DOOR_ACTION_2)
4861     door2 = door_state & DOOR_ACTION_2;
4862
4863   // draw masked border over door area
4864   DrawMaskedBorder(REDRAW_DOOR_1);
4865   DrawMaskedBorder(REDRAW_DOOR_2);
4866
4867   return (door1 | door2);
4868 }
4869
4870 static boolean useSpecialEditorDoor()
4871 {
4872   int graphic = IMG_GLOBAL_BORDER_EDITOR;
4873   boolean redefined = getImageListEntryFromImageID(graphic)->redefined;
4874
4875   // do not draw special editor door if editor border defined or redefined
4876   if (graphic_info[graphic].bitmap != NULL || redefined)
4877     return FALSE;
4878
4879   // do not draw special editor door if global border defined to be empty
4880   if (graphic_info[IMG_GLOBAL_BORDER].bitmap == NULL)
4881     return FALSE;
4882
4883   // do not draw special editor door if viewport definitions do not match
4884   if (EX != VX ||
4885       EY >= VY ||
4886       EXSIZE != VXSIZE ||
4887       EY + EYSIZE != VY + VYSIZE)
4888     return FALSE;
4889
4890   return TRUE;
4891 }
4892
4893 void DrawSpecialEditorDoor()
4894 {
4895   struct GraphicInfo *gfx1 = &graphic_info[IMG_DOOR_2_TOP_BORDER_CORRECTION];
4896   int top_border_width = gfx1->width;
4897   int top_border_height = gfx1->height;
4898   int outer_border = viewport.door_2[GAME_MODE_EDITOR].border_size;
4899   int ex = EX - outer_border;
4900   int ey = EY - outer_border;
4901   int vy = VY - outer_border;
4902   int exsize = EXSIZE + 2 * outer_border;
4903
4904   if (!useSpecialEditorDoor())
4905     return;
4906
4907   /* draw bigger level editor toolbox window */
4908   BlitBitmap(gfx1->bitmap, drawto, gfx1->src_x, gfx1->src_y,
4909              top_border_width, top_border_height, ex, ey - top_border_height);
4910   BlitBitmap(graphic_info[IMG_GLOBAL_BORDER].bitmap, drawto, ex, vy,
4911              exsize, EYSIZE - VYSIZE + outer_border, ex, ey);
4912
4913   redraw_mask |= REDRAW_ALL;
4914 }
4915
4916 void UndrawSpecialEditorDoor()
4917 {
4918   struct GraphicInfo *gfx1 = &graphic_info[IMG_DOOR_2_TOP_BORDER_CORRECTION];
4919   int top_border_width = gfx1->width;
4920   int top_border_height = gfx1->height;
4921   int outer_border = viewport.door_2[GAME_MODE_EDITOR].border_size;
4922   int ex = EX - outer_border;
4923   int ey = EY - outer_border;
4924   int ey_top = ey - top_border_height;
4925   int exsize = EXSIZE + 2 * outer_border;
4926   int eysize = EYSIZE + 2 * outer_border;
4927
4928   if (!useSpecialEditorDoor())
4929     return;
4930
4931   /* draw normal tape recorder window */
4932   if (graphic_info[IMG_GLOBAL_BORDER].bitmap)
4933   {
4934     BlitBitmap(graphic_info[IMG_GLOBAL_BORDER].bitmap, drawto,
4935                ex, ey_top, top_border_width, top_border_height,
4936                ex, ey_top);
4937     BlitBitmap(graphic_info[IMG_GLOBAL_BORDER].bitmap, drawto,
4938                ex, ey, exsize, eysize, ex, ey);
4939   }
4940   else
4941   {
4942     // if screen background is set to "[NONE]", clear editor toolbox window
4943     ClearRectangle(drawto, ex, ey_top, top_border_width, top_border_height);
4944     ClearRectangle(drawto, ex, ey, exsize, eysize);
4945   }
4946
4947   redraw_mask |= REDRAW_ALL;
4948 }
4949
4950
4951 /* ---------- new tool button stuff ---------------------------------------- */
4952
4953 static struct
4954 {
4955   int graphic;
4956   struct TextPosInfo *pos;
4957   int gadget_id;
4958   char *infotext;
4959 } toolbutton_info[NUM_TOOL_BUTTONS] =
4960 {
4961   {
4962     IMG_GFX_REQUEST_BUTTON_YES,         &request.button.yes,
4963     TOOL_CTRL_ID_YES,                   "yes"
4964   },
4965   {
4966     IMG_GFX_REQUEST_BUTTON_NO,          &request.button.no,
4967     TOOL_CTRL_ID_NO,                    "no"
4968   },
4969   {
4970     IMG_GFX_REQUEST_BUTTON_CONFIRM,     &request.button.confirm,
4971     TOOL_CTRL_ID_CONFIRM,               "confirm"
4972   },
4973   {
4974     IMG_GFX_REQUEST_BUTTON_PLAYER_1,    &request.button.player_1,
4975     TOOL_CTRL_ID_PLAYER_1,              "player 1"
4976   },
4977   {
4978     IMG_GFX_REQUEST_BUTTON_PLAYER_2,    &request.button.player_2,
4979     TOOL_CTRL_ID_PLAYER_2,              "player 2"
4980   },
4981   {
4982     IMG_GFX_REQUEST_BUTTON_PLAYER_3,    &request.button.player_3,
4983     TOOL_CTRL_ID_PLAYER_3,              "player 3"
4984   },
4985   {
4986     IMG_GFX_REQUEST_BUTTON_PLAYER_4,    &request.button.player_4,
4987     TOOL_CTRL_ID_PLAYER_4,              "player 4"
4988   }
4989 };
4990
4991 void CreateToolButtons()
4992 {
4993   int i;
4994
4995   for (i = 0; i < NUM_TOOL_BUTTONS; i++)
4996   {
4997     struct GraphicInfo *gfx = &graphic_info[toolbutton_info[i].graphic];
4998     struct TextPosInfo *pos = toolbutton_info[i].pos;
4999     struct GadgetInfo *gi;
5000     Bitmap *deco_bitmap = None;
5001     int deco_x = 0, deco_y = 0, deco_xpos = 0, deco_ypos = 0;
5002     unsigned int event_mask = GD_EVENT_RELEASED;
5003     int dx = DX;
5004     int dy = DY;
5005     int gd_x = gfx->src_x;
5006     int gd_y = gfx->src_y;
5007     int gd_xp = gfx->src_x + gfx->pressed_xoffset;
5008     int gd_yp = gfx->src_y + gfx->pressed_yoffset;
5009     int id = i;
5010
5011     if (global.use_envelope_request)
5012       setRequestPosition(&dx, &dy, TRUE);
5013
5014     if (id >= TOOL_CTRL_ID_PLAYER_1 && id <= TOOL_CTRL_ID_PLAYER_4)
5015     {
5016       int player_nr = id - TOOL_CTRL_ID_PLAYER_1;
5017
5018       getSizedGraphicSource(PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0,
5019                             pos->size, &deco_bitmap, &deco_x, &deco_y);
5020       deco_xpos = (gfx->width  - pos->size) / 2;
5021       deco_ypos = (gfx->height - pos->size) / 2;
5022     }
5023
5024     gi = CreateGadget(GDI_CUSTOM_ID, id,
5025                       GDI_INFO_TEXT, toolbutton_info[i].infotext,
5026                       GDI_X, dx + GDI_ACTIVE_POS(pos->x),
5027                       GDI_Y, dy + GDI_ACTIVE_POS(pos->y),
5028                       GDI_WIDTH, gfx->width,
5029                       GDI_HEIGHT, gfx->height,
5030                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
5031                       GDI_STATE, GD_BUTTON_UNPRESSED,
5032                       GDI_DESIGN_UNPRESSED, gfx->bitmap, gd_x, gd_y,
5033                       GDI_DESIGN_PRESSED, gfx->bitmap, gd_xp, gd_yp,
5034                       GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y,
5035                       GDI_DECORATION_POSITION, deco_xpos, deco_ypos,
5036                       GDI_DECORATION_SIZE, pos->size, pos->size,
5037                       GDI_DECORATION_SHIFTING, 1, 1,
5038                       GDI_DIRECT_DRAW, FALSE,
5039                       GDI_EVENT_MASK, event_mask,
5040                       GDI_CALLBACK_ACTION, HandleToolButtons,
5041                       GDI_END);
5042
5043     if (gi == NULL)
5044       Error(ERR_EXIT, "cannot create gadget");
5045
5046     tool_gadget[id] = gi;
5047   }
5048 }
5049
5050 void FreeToolButtons()
5051 {
5052   int i;
5053
5054   for (i = 0; i < NUM_TOOL_BUTTONS; i++)
5055     FreeGadget(tool_gadget[i]);
5056 }
5057
5058 static void UnmapToolButtons()
5059 {
5060   int i;
5061
5062   for (i = 0; i < NUM_TOOL_BUTTONS; i++)
5063     UnmapGadget(tool_gadget[i]);
5064 }
5065
5066 static void HandleToolButtons(struct GadgetInfo *gi)
5067 {
5068   request_gadget_id = gi->custom_id;
5069 }
5070
5071 static struct Mapping_EM_to_RND_object
5072 {
5073   int element_em;
5074   boolean is_rnd_to_em_mapping;         /* unique mapping EM <-> RND */
5075   boolean is_backside;                  /* backside of moving element */
5076
5077   int element_rnd;
5078   int action;
5079   int direction;
5080 }
5081 em_object_mapping_list[] =
5082 {
5083   {
5084     Xblank,                             TRUE,   FALSE,
5085     EL_EMPTY,                           -1, -1
5086   },
5087   {
5088     Yacid_splash_eB,                    FALSE,  FALSE,
5089     EL_ACID_SPLASH_RIGHT,               -1, -1
5090   },
5091   {
5092     Yacid_splash_wB,                    FALSE,  FALSE,
5093     EL_ACID_SPLASH_LEFT,                -1, -1
5094   },
5095
5096 #ifdef EM_ENGINE_BAD_ROLL
5097   {
5098     Xstone_force_e,                     FALSE,  FALSE,
5099     EL_ROCK,                            -1, MV_BIT_RIGHT
5100   },
5101   {
5102     Xstone_force_w,                     FALSE,  FALSE,
5103     EL_ROCK,                            -1, MV_BIT_LEFT
5104   },
5105   {
5106     Xnut_force_e,                       FALSE,  FALSE,
5107     EL_NUT,                             -1, MV_BIT_RIGHT
5108   },
5109   {
5110     Xnut_force_w,                       FALSE,  FALSE,
5111     EL_NUT,                             -1, MV_BIT_LEFT
5112   },
5113   {
5114     Xspring_force_e,                    FALSE,  FALSE,
5115     EL_SPRING,                          -1, MV_BIT_RIGHT
5116   },
5117   {
5118     Xspring_force_w,                    FALSE,  FALSE,
5119     EL_SPRING,                          -1, MV_BIT_LEFT
5120   },
5121   {
5122     Xemerald_force_e,                   FALSE,  FALSE,
5123     EL_EMERALD,                         -1, MV_BIT_RIGHT
5124   },
5125   {
5126     Xemerald_force_w,                   FALSE,  FALSE,
5127     EL_EMERALD,                         -1, MV_BIT_LEFT
5128   },
5129   {
5130     Xdiamond_force_e,                   FALSE,  FALSE,
5131     EL_DIAMOND,                         -1, MV_BIT_RIGHT
5132   },
5133   {
5134     Xdiamond_force_w,                   FALSE,  FALSE,
5135     EL_DIAMOND,                         -1, MV_BIT_LEFT
5136   },
5137   {
5138     Xbomb_force_e,                      FALSE,  FALSE,
5139     EL_BOMB,                            -1, MV_BIT_RIGHT
5140   },
5141   {
5142     Xbomb_force_w,                      FALSE,  FALSE,
5143     EL_BOMB,                            -1, MV_BIT_LEFT
5144   },
5145 #endif  /* EM_ENGINE_BAD_ROLL */
5146
5147   {
5148     Xstone,                             TRUE,   FALSE,
5149     EL_ROCK,                            -1, -1
5150   },
5151   {
5152     Xstone_pause,                       FALSE,  FALSE,
5153     EL_ROCK,                            -1, -1
5154   },
5155   {
5156     Xstone_fall,                        FALSE,  FALSE,
5157     EL_ROCK,                            -1, -1
5158   },
5159   {
5160     Ystone_s,                           FALSE,  FALSE,
5161     EL_ROCK,                            ACTION_FALLING, -1
5162   },
5163   {
5164     Ystone_sB,                          FALSE,  TRUE,
5165     EL_ROCK,                            ACTION_FALLING, -1
5166   },
5167   {
5168     Ystone_e,                           FALSE,  FALSE,
5169     EL_ROCK,                            ACTION_MOVING, MV_BIT_RIGHT
5170   },
5171   {
5172     Ystone_eB,                          FALSE,  TRUE,
5173     EL_ROCK,                            ACTION_MOVING, MV_BIT_RIGHT
5174   },
5175   {
5176     Ystone_w,                           FALSE,  FALSE,
5177     EL_ROCK,                            ACTION_MOVING, MV_BIT_LEFT
5178   },
5179   {
5180     Ystone_wB,                          FALSE,  TRUE,
5181     EL_ROCK,                            ACTION_MOVING, MV_BIT_LEFT
5182   },
5183   {
5184     Xnut,                               TRUE,   FALSE,
5185     EL_NUT,                             -1, -1
5186   },
5187   {
5188     Xnut_pause,                         FALSE,  FALSE,
5189     EL_NUT,                             -1, -1
5190   },
5191   {
5192     Xnut_fall,                          FALSE,  FALSE,
5193     EL_NUT,                             -1, -1
5194   },
5195   {
5196     Ynut_s,                             FALSE,  FALSE,
5197     EL_NUT,                             ACTION_FALLING, -1
5198   },
5199   {
5200     Ynut_sB,                            FALSE,  TRUE,
5201     EL_NUT,                             ACTION_FALLING, -1
5202   },
5203   {
5204     Ynut_e,                             FALSE,  FALSE,
5205     EL_NUT,                             ACTION_MOVING, MV_BIT_RIGHT
5206   },
5207   {
5208     Ynut_eB,                            FALSE,  TRUE,
5209     EL_NUT,                             ACTION_MOVING, MV_BIT_RIGHT
5210   },
5211   {
5212     Ynut_w,                             FALSE,  FALSE,
5213     EL_NUT,                             ACTION_MOVING, MV_BIT_LEFT
5214   },
5215   {
5216     Ynut_wB,                            FALSE,  TRUE,
5217     EL_NUT,                             ACTION_MOVING, MV_BIT_LEFT
5218   },
5219   {
5220     Xbug_n,                             TRUE,   FALSE,
5221     EL_BUG_UP,                          -1, -1
5222   },
5223   {
5224     Xbug_e,                             TRUE,   FALSE,
5225     EL_BUG_RIGHT,                       -1, -1
5226   },
5227   {
5228     Xbug_s,                             TRUE,   FALSE,
5229     EL_BUG_DOWN,                        -1, -1
5230   },
5231   {
5232     Xbug_w,                             TRUE,   FALSE,
5233     EL_BUG_LEFT,                        -1, -1
5234   },
5235   {
5236     Xbug_gon,                           FALSE,  FALSE,
5237     EL_BUG_UP,                          -1, -1
5238   },
5239   {
5240     Xbug_goe,                           FALSE,  FALSE,
5241     EL_BUG_RIGHT,                       -1, -1
5242   },
5243   {
5244     Xbug_gos,                           FALSE,  FALSE,
5245     EL_BUG_DOWN,                        -1, -1
5246   },
5247   {
5248     Xbug_gow,                           FALSE,  FALSE,
5249     EL_BUG_LEFT,                        -1, -1
5250   },
5251   {
5252     Ybug_n,                             FALSE,  FALSE,
5253     EL_BUG,                             ACTION_MOVING, MV_BIT_UP
5254   },
5255   {
5256     Ybug_nB,                            FALSE,  TRUE,
5257     EL_BUG,                             ACTION_MOVING, MV_BIT_UP
5258   },
5259   {
5260     Ybug_e,                             FALSE,  FALSE,
5261     EL_BUG,                             ACTION_MOVING, MV_BIT_RIGHT
5262   },
5263   {
5264     Ybug_eB,                            FALSE,  TRUE,
5265     EL_BUG,                             ACTION_MOVING, MV_BIT_RIGHT
5266   },
5267   {
5268     Ybug_s,                             FALSE,  FALSE,
5269     EL_BUG,                             ACTION_MOVING, MV_BIT_DOWN
5270   },
5271   {
5272     Ybug_sB,                            FALSE,  TRUE,
5273     EL_BUG,                             ACTION_MOVING, MV_BIT_DOWN
5274   },
5275   {
5276     Ybug_w,                             FALSE,  FALSE,
5277     EL_BUG,                             ACTION_MOVING, MV_BIT_LEFT
5278   },
5279   {
5280     Ybug_wB,                            FALSE,  TRUE,
5281     EL_BUG,                             ACTION_MOVING, MV_BIT_LEFT
5282   },
5283   {
5284     Ybug_w_n,                           FALSE,  FALSE,
5285     EL_BUG,                             ACTION_TURNING_FROM_LEFT, MV_BIT_UP
5286   },
5287   {
5288     Ybug_n_e,                           FALSE,  FALSE,
5289     EL_BUG,                             ACTION_TURNING_FROM_UP, MV_BIT_RIGHT
5290   },
5291   {
5292     Ybug_e_s,                           FALSE,  FALSE,
5293     EL_BUG,                             ACTION_TURNING_FROM_RIGHT, MV_BIT_DOWN
5294   },
5295   {
5296     Ybug_s_w,                           FALSE,  FALSE,
5297     EL_BUG,                             ACTION_TURNING_FROM_DOWN, MV_BIT_LEFT
5298   },
5299   {
5300     Ybug_e_n,                           FALSE,  FALSE,
5301     EL_BUG,                             ACTION_TURNING_FROM_RIGHT, MV_BIT_UP
5302   },
5303   {
5304     Ybug_s_e,                           FALSE,  FALSE,
5305     EL_BUG,                             ACTION_TURNING_FROM_DOWN, MV_BIT_RIGHT
5306   },
5307   {
5308     Ybug_w_s,                           FALSE,  FALSE,
5309     EL_BUG,                             ACTION_TURNING_FROM_LEFT, MV_BIT_DOWN
5310   },
5311   {
5312     Ybug_n_w,                           FALSE,  FALSE,
5313     EL_BUG,                             ACTION_TURNING_FROM_UP, MV_BIT_LEFT
5314   },
5315   {
5316     Ybug_stone,                         FALSE,  FALSE,
5317     EL_BUG,                             ACTION_SMASHED_BY_ROCK, -1
5318   },
5319   {
5320     Ybug_spring,                        FALSE,  FALSE,
5321     EL_BUG,                             ACTION_SMASHED_BY_SPRING, -1
5322   },
5323   {
5324     Xtank_n,                            TRUE,   FALSE,
5325     EL_SPACESHIP_UP,                    -1, -1
5326   },
5327   {
5328     Xtank_e,                            TRUE,   FALSE,
5329     EL_SPACESHIP_RIGHT,                 -1, -1
5330   },
5331   {
5332     Xtank_s,                            TRUE,   FALSE,
5333     EL_SPACESHIP_DOWN,                  -1, -1
5334   },
5335   {
5336     Xtank_w,                            TRUE,   FALSE,
5337     EL_SPACESHIP_LEFT,                  -1, -1
5338   },
5339   {
5340     Xtank_gon,                          FALSE,  FALSE,
5341     EL_SPACESHIP_UP,                    -1, -1
5342   },
5343   {
5344     Xtank_goe,                          FALSE,  FALSE,
5345     EL_SPACESHIP_RIGHT,                 -1, -1
5346   },
5347   {
5348     Xtank_gos,                          FALSE,  FALSE,
5349     EL_SPACESHIP_DOWN,                  -1, -1
5350   },
5351   {
5352     Xtank_gow,                          FALSE,  FALSE,
5353     EL_SPACESHIP_LEFT,                  -1, -1
5354   },
5355   {
5356     Ytank_n,                            FALSE,  FALSE,
5357     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_UP
5358   },
5359   {
5360     Ytank_nB,                           FALSE,  TRUE,
5361     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_UP
5362   },
5363   {
5364     Ytank_e,                            FALSE,  FALSE,
5365     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_RIGHT
5366   },
5367   {
5368     Ytank_eB,                           FALSE,  TRUE,
5369     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_RIGHT
5370   },
5371   {
5372     Ytank_s,                            FALSE,  FALSE,
5373     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_DOWN
5374   },
5375   {
5376     Ytank_sB,                           FALSE,  TRUE,
5377     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_DOWN
5378   },
5379   {
5380     Ytank_w,                            FALSE,  FALSE,
5381     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_LEFT
5382   },
5383   {
5384     Ytank_wB,                           FALSE,  TRUE,
5385     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_LEFT
5386   },
5387   {
5388     Ytank_w_n,                          FALSE,  FALSE,
5389     EL_SPACESHIP,                       ACTION_TURNING_FROM_LEFT, MV_BIT_UP
5390   },
5391   {
5392     Ytank_n_e,                          FALSE,  FALSE,
5393     EL_SPACESHIP,                       ACTION_TURNING_FROM_UP, MV_BIT_RIGHT
5394   },
5395   {
5396     Ytank_e_s,                          FALSE,  FALSE,
5397     EL_SPACESHIP,                       ACTION_TURNING_FROM_RIGHT, MV_BIT_DOWN
5398   },
5399   {
5400     Ytank_s_w,                          FALSE,  FALSE,
5401     EL_SPACESHIP,                       ACTION_TURNING_FROM_DOWN, MV_BIT_LEFT
5402   },
5403   {
5404     Ytank_e_n,                          FALSE,  FALSE,
5405     EL_SPACESHIP,                       ACTION_TURNING_FROM_RIGHT, MV_BIT_UP
5406   },
5407   {
5408     Ytank_s_e,                          FALSE,  FALSE,
5409     EL_SPACESHIP,                       ACTION_TURNING_FROM_DOWN, MV_BIT_RIGHT
5410   },
5411   {
5412     Ytank_w_s,                          FALSE,  FALSE,
5413     EL_SPACESHIP,                       ACTION_TURNING_FROM_LEFT, MV_BIT_DOWN
5414   },
5415   {
5416     Ytank_n_w,                          FALSE,  FALSE,
5417     EL_SPACESHIP,                       ACTION_TURNING_FROM_UP, MV_BIT_LEFT
5418   },
5419   {
5420     Ytank_stone,                        FALSE,  FALSE,
5421     EL_SPACESHIP,                       ACTION_SMASHED_BY_ROCK, -1
5422   },
5423   {
5424     Ytank_spring,                       FALSE,  FALSE,
5425     EL_SPACESHIP,                       ACTION_SMASHED_BY_SPRING, -1
5426   },
5427   {
5428     Xandroid,                           TRUE,   FALSE,
5429     EL_EMC_ANDROID,                     ACTION_ACTIVE, -1
5430   },
5431   {
5432     Xandroid_1_n,                       FALSE,  FALSE,
5433     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_UP
5434   },
5435   {
5436     Xandroid_2_n,                       FALSE,  FALSE,
5437     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_UP
5438   },
5439   {
5440     Xandroid_1_e,                       FALSE,  FALSE,
5441     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_RIGHT
5442   },
5443   {
5444     Xandroid_2_e,                       FALSE,  FALSE,
5445     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_RIGHT
5446   },
5447   {
5448     Xandroid_1_w,                       FALSE,  FALSE,
5449     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_LEFT
5450   },
5451   {
5452     Xandroid_2_w,                       FALSE,  FALSE,
5453     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_LEFT
5454   },
5455   {
5456     Xandroid_1_s,                       FALSE,  FALSE,
5457     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_DOWN
5458   },
5459   {
5460     Xandroid_2_s,                       FALSE,  FALSE,
5461     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_DOWN
5462   },
5463   {
5464     Yandroid_n,                         FALSE,  FALSE,
5465     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_UP
5466   },
5467   {
5468     Yandroid_nB,                        FALSE,  TRUE,
5469     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_UP
5470   },
5471   {
5472     Yandroid_ne,                        FALSE,  FALSE,
5473     EL_EMC_ANDROID,                     ACTION_GROWING, MV_BIT_UPRIGHT
5474   },
5475   {
5476     Yandroid_neB,                       FALSE,  TRUE,
5477     EL_EMC_ANDROID,                     ACTION_SHRINKING, MV_BIT_UPRIGHT
5478   },
5479   {
5480     Yandroid_e,                         FALSE,  FALSE,
5481     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_RIGHT
5482   },
5483   {
5484     Yandroid_eB,                        FALSE,  TRUE,
5485     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_RIGHT
5486   },
5487   {
5488     Yandroid_se,                        FALSE,  FALSE,
5489     EL_EMC_ANDROID,                     ACTION_GROWING, MV_BIT_DOWNRIGHT
5490   },
5491   {
5492     Yandroid_seB,                       FALSE,  TRUE,
5493     EL_EMC_ANDROID,                     ACTION_SHRINKING, MV_BIT_DOWNRIGHT
5494   },
5495   {
5496     Yandroid_s,                         FALSE,  FALSE,
5497     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_DOWN
5498   },
5499   {
5500     Yandroid_sB,                        FALSE,  TRUE,
5501     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_DOWN
5502   },
5503   {
5504     Yandroid_sw,                        FALSE,  FALSE,
5505     EL_EMC_ANDROID,                     ACTION_GROWING, MV_BIT_DOWNLEFT
5506   },
5507   {
5508     Yandroid_swB,                       FALSE,  TRUE,
5509     EL_EMC_ANDROID,                     ACTION_SHRINKING, MV_BIT_DOWNLEFT
5510   },
5511   {
5512     Yandroid_w,                         FALSE,  FALSE,
5513     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_LEFT
5514   },
5515   {
5516     Yandroid_wB,                        FALSE,  TRUE,
5517     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_LEFT
5518   },
5519   {
5520     Yandroid_nw,                        FALSE,  FALSE,
5521     EL_EMC_ANDROID,                     ACTION_GROWING, MV_BIT_UPLEFT
5522   },
5523   {
5524     Yandroid_nwB,                       FALSE,  TRUE,
5525     EL_EMC_ANDROID,                     ACTION_SHRINKING, MV_BIT_UPLEFT
5526   },
5527   {
5528     Xspring,                            TRUE,   FALSE,
5529     EL_SPRING,                          -1, -1
5530   },
5531   {
5532     Xspring_pause,                      FALSE,  FALSE,
5533     EL_SPRING,                          -1, -1
5534   },
5535   {
5536     Xspring_e,                          FALSE,  FALSE,
5537     EL_SPRING,                          -1, -1
5538   },
5539   {
5540     Xspring_w,                          FALSE,  FALSE,
5541     EL_SPRING,                          -1, -1
5542   },
5543   {
5544     Xspring_fall,                       FALSE,  FALSE,
5545     EL_SPRING,                          -1, -1
5546   },
5547   {
5548     Yspring_s,                          FALSE,  FALSE,
5549     EL_SPRING,                          ACTION_FALLING, -1
5550   },
5551   {
5552     Yspring_sB,                         FALSE,  TRUE,
5553     EL_SPRING,                          ACTION_FALLING, -1
5554   },
5555   {
5556     Yspring_e,                          FALSE,  FALSE,
5557     EL_SPRING,                          ACTION_MOVING, MV_BIT_RIGHT
5558   },
5559   {
5560     Yspring_eB,                         FALSE,  TRUE,
5561     EL_SPRING,                          ACTION_MOVING, MV_BIT_RIGHT
5562   },
5563   {
5564     Yspring_w,                          FALSE,  FALSE,
5565     EL_SPRING,                          ACTION_MOVING, MV_BIT_LEFT
5566   },
5567   {
5568     Yspring_wB,                         FALSE,  TRUE,
5569     EL_SPRING,                          ACTION_MOVING, MV_BIT_LEFT
5570   },
5571   {
5572     Yspring_kill_e,                     FALSE,  FALSE,
5573     EL_SPRING,                          ACTION_EATING, MV_BIT_RIGHT
5574   },
5575   {
5576     Yspring_kill_eB,                    FALSE,  TRUE,
5577     EL_SPRING,                          ACTION_EATING, MV_BIT_RIGHT
5578   },
5579   {
5580     Yspring_kill_w,                     FALSE,  FALSE,
5581     EL_SPRING,                          ACTION_EATING, MV_BIT_LEFT
5582   },
5583   {
5584     Yspring_kill_wB,                    FALSE,  TRUE,
5585     EL_SPRING,                          ACTION_EATING, MV_BIT_LEFT
5586   },
5587   {
5588     Xeater_n,                           TRUE,   FALSE,
5589     EL_YAMYAM_UP,                       -1, -1
5590   },
5591   {
5592     Xeater_e,                           TRUE,   FALSE,
5593     EL_YAMYAM_RIGHT,                    -1, -1
5594   },
5595   {
5596     Xeater_w,                           TRUE,   FALSE,
5597     EL_YAMYAM_LEFT,                     -1, -1
5598   },
5599   {
5600     Xeater_s,                           TRUE,   FALSE,
5601     EL_YAMYAM_DOWN,                     -1, -1
5602   },
5603   {
5604     Yeater_n,                           FALSE,  FALSE,
5605     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_UP
5606   },
5607   {
5608     Yeater_nB,                          FALSE,  TRUE,
5609     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_UP
5610   },
5611   {
5612     Yeater_e,                           FALSE,  FALSE,
5613     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_RIGHT
5614   },
5615   {
5616     Yeater_eB,                          FALSE,  TRUE,
5617     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_RIGHT
5618   },
5619   {
5620     Yeater_s,                           FALSE,  FALSE,
5621     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_DOWN
5622   },
5623   {
5624     Yeater_sB,                          FALSE,  TRUE,
5625     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_DOWN
5626   },
5627   {
5628     Yeater_w,                           FALSE,  FALSE,
5629     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_LEFT
5630   },
5631   {
5632     Yeater_wB,                          FALSE,  TRUE,
5633     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_LEFT
5634   },
5635   {
5636     Yeater_stone,                       FALSE,  FALSE,
5637     EL_YAMYAM,                          ACTION_SMASHED_BY_ROCK, -1
5638   },
5639   {
5640     Yeater_spring,                      FALSE,  FALSE,
5641     EL_YAMYAM,                          ACTION_SMASHED_BY_SPRING, -1
5642   },
5643   {
5644     Xalien,                             TRUE,   FALSE,
5645     EL_ROBOT,                           -1, -1
5646   },
5647   {
5648     Xalien_pause,                       FALSE,  FALSE,
5649     EL_ROBOT,                           -1, -1
5650   },
5651   {
5652     Yalien_n,                           FALSE,  FALSE,
5653     EL_ROBOT,                           ACTION_MOVING, MV_BIT_UP
5654   },
5655   {
5656     Yalien_nB,                          FALSE,  TRUE,
5657     EL_ROBOT,                           ACTION_MOVING, MV_BIT_UP
5658   },
5659   {
5660     Yalien_e,                           FALSE,  FALSE,
5661     EL_ROBOT,                           ACTION_MOVING, MV_BIT_RIGHT
5662   },
5663   {
5664     Yalien_eB,                          FALSE,  TRUE,
5665     EL_ROBOT,                           ACTION_MOVING, MV_BIT_RIGHT
5666   },
5667   {
5668     Yalien_s,                           FALSE,  FALSE,
5669     EL_ROBOT,                           ACTION_MOVING, MV_BIT_DOWN
5670   },
5671   {
5672     Yalien_sB,                          FALSE,  TRUE,
5673     EL_ROBOT,                           ACTION_MOVING, MV_BIT_DOWN
5674   },
5675   {
5676     Yalien_w,                           FALSE,  FALSE,
5677     EL_ROBOT,                           ACTION_MOVING, MV_BIT_LEFT
5678   },
5679   {
5680     Yalien_wB,                          FALSE,  TRUE,
5681     EL_ROBOT,                           ACTION_MOVING, MV_BIT_LEFT
5682   },
5683   {
5684     Yalien_stone,                       FALSE,  FALSE,
5685     EL_ROBOT,                           ACTION_SMASHED_BY_ROCK, -1
5686   },
5687   {
5688     Yalien_spring,                      FALSE,  FALSE,
5689     EL_ROBOT,                           ACTION_SMASHED_BY_SPRING, -1
5690   },
5691   {
5692     Xemerald,                           TRUE,   FALSE,
5693     EL_EMERALD,                         -1, -1
5694   },
5695   {
5696     Xemerald_pause,                     FALSE,  FALSE,
5697     EL_EMERALD,                         -1, -1
5698   },
5699   {
5700     Xemerald_fall,                      FALSE,  FALSE,
5701     EL_EMERALD,                         -1, -1
5702   },
5703   {
5704     Xemerald_shine,                     FALSE,  FALSE,
5705     EL_EMERALD,                         ACTION_TWINKLING, -1
5706   },
5707   {
5708     Yemerald_s,                         FALSE,  FALSE,
5709     EL_EMERALD,                         ACTION_FALLING, -1
5710   },
5711   {
5712     Yemerald_sB,                        FALSE,  TRUE,
5713     EL_EMERALD,                         ACTION_FALLING, -1
5714   },
5715   {
5716     Yemerald_e,                         FALSE,  FALSE,
5717     EL_EMERALD,                         ACTION_MOVING, MV_BIT_RIGHT
5718   },
5719   {
5720     Yemerald_eB,                        FALSE,  TRUE,
5721     EL_EMERALD,                         ACTION_MOVING, MV_BIT_RIGHT
5722   },
5723   {
5724     Yemerald_w,                         FALSE,  FALSE,
5725     EL_EMERALD,                         ACTION_MOVING, MV_BIT_LEFT
5726   },
5727   {
5728     Yemerald_wB,                        FALSE,  TRUE,
5729     EL_EMERALD,                         ACTION_MOVING, MV_BIT_LEFT
5730   },
5731   {
5732     Yemerald_eat,                       FALSE,  FALSE,
5733     EL_EMERALD,                         ACTION_COLLECTING, -1
5734   },
5735   {
5736     Yemerald_stone,                     FALSE,  FALSE,
5737     EL_NUT,                             ACTION_BREAKING, -1
5738   },
5739   {
5740     Xdiamond,                           TRUE,   FALSE,
5741     EL_DIAMOND,                         -1, -1
5742   },
5743   {
5744     Xdiamond_pause,                     FALSE,  FALSE,
5745     EL_DIAMOND,                         -1, -1
5746   },
5747   {
5748     Xdiamond_fall,                      FALSE,  FALSE,
5749     EL_DIAMOND,                         -1, -1
5750   },
5751   {
5752     Xdiamond_shine,                     FALSE,  FALSE,
5753     EL_DIAMOND,                         ACTION_TWINKLING, -1
5754   },
5755   {
5756     Ydiamond_s,                         FALSE,  FALSE,
5757     EL_DIAMOND,                         ACTION_FALLING, -1
5758   },
5759   {
5760     Ydiamond_sB,                        FALSE,  TRUE,
5761     EL_DIAMOND,                         ACTION_FALLING, -1
5762   },
5763   {
5764     Ydiamond_e,                         FALSE,  FALSE,
5765     EL_DIAMOND,                         ACTION_MOVING, MV_BIT_RIGHT
5766   },
5767   {
5768     Ydiamond_eB,                        FALSE,  TRUE,
5769     EL_DIAMOND,                         ACTION_MOVING, MV_BIT_RIGHT
5770   },
5771   {
5772     Ydiamond_w,                         FALSE,  FALSE,
5773     EL_DIAMOND,                         ACTION_MOVING, MV_BIT_LEFT
5774   },
5775   {
5776     Ydiamond_wB,                        FALSE,  TRUE,
5777     EL_DIAMOND,                         ACTION_MOVING, MV_BIT_LEFT
5778   },
5779   {
5780     Ydiamond_eat,                       FALSE,  FALSE,
5781     EL_DIAMOND,                         ACTION_COLLECTING, -1
5782   },
5783   {
5784     Ydiamond_stone,                     FALSE,  FALSE,
5785     EL_DIAMOND,                         ACTION_SMASHED_BY_ROCK, -1
5786   },
5787   {
5788     Xdrip_fall,                         TRUE,   FALSE,
5789     EL_AMOEBA_DROP,                     -1, -1
5790   },
5791   {
5792     Xdrip_stretch,                      FALSE,  FALSE,
5793     EL_AMOEBA_DROP,                     ACTION_FALLING, -1
5794   },
5795   {
5796     Xdrip_stretchB,                     FALSE,  TRUE,
5797     EL_AMOEBA_DROP,                     ACTION_FALLING, -1
5798   },
5799   {
5800     Xdrip_eat,                          FALSE,  FALSE,
5801     EL_AMOEBA_DROP,                     ACTION_GROWING, -1
5802   },
5803   {
5804     Ydrip_s1,                           FALSE,  FALSE,
5805     EL_AMOEBA_DROP,                     ACTION_FALLING, -1
5806   },
5807   {
5808     Ydrip_s1B,                          FALSE,  TRUE,
5809     EL_AMOEBA_DROP,                     ACTION_FALLING, -1
5810   },
5811   {
5812     Ydrip_s2,                           FALSE,  FALSE,
5813     EL_AMOEBA_DROP,                     ACTION_FALLING, -1
5814   },
5815   {
5816     Ydrip_s2B,                          FALSE,  TRUE,
5817     EL_AMOEBA_DROP,                     ACTION_FALLING, -1
5818   },
5819   {
5820     Xbomb,                              TRUE,   FALSE,
5821     EL_BOMB,                            -1, -1
5822   },
5823   {
5824     Xbomb_pause,                        FALSE,  FALSE,
5825     EL_BOMB,                            -1, -1
5826   },
5827   {
5828     Xbomb_fall,                         FALSE,  FALSE,
5829     EL_BOMB,                            -1, -1
5830   },
5831   {
5832     Ybomb_s,                            FALSE,  FALSE,
5833     EL_BOMB,                            ACTION_FALLING, -1
5834   },
5835   {
5836     Ybomb_sB,                           FALSE,  TRUE,
5837     EL_BOMB,                            ACTION_FALLING, -1
5838   },
5839   {
5840     Ybomb_e,                            FALSE,  FALSE,
5841     EL_BOMB,                            ACTION_MOVING, MV_BIT_RIGHT
5842   },
5843   {
5844     Ybomb_eB,                           FALSE,  TRUE,
5845     EL_BOMB,                            ACTION_MOVING, MV_BIT_RIGHT
5846   },
5847   {
5848     Ybomb_w,                            FALSE,  FALSE,
5849     EL_BOMB,                            ACTION_MOVING, MV_BIT_LEFT
5850   },
5851   {
5852     Ybomb_wB,                           FALSE,  TRUE,
5853     EL_BOMB,                            ACTION_MOVING, MV_BIT_LEFT
5854   },
5855   {
5856     Ybomb_eat,                          FALSE,  FALSE,
5857     EL_BOMB,                            ACTION_ACTIVATING, -1
5858   },
5859   {
5860     Xballoon,                           TRUE,   FALSE,
5861     EL_BALLOON,                         -1, -1
5862   },
5863   {
5864     Yballoon_n,                         FALSE,  FALSE,
5865     EL_BALLOON,                         ACTION_MOVING, MV_BIT_UP
5866   },
5867   {
5868     Yballoon_nB,                        FALSE,  TRUE,
5869     EL_BALLOON,                         ACTION_MOVING, MV_BIT_UP
5870   },
5871   {
5872     Yballoon_e,                         FALSE,  FALSE,
5873     EL_BALLOON,                         ACTION_MOVING, MV_BIT_RIGHT
5874   },
5875   {
5876     Yballoon_eB,                        FALSE,  TRUE,
5877     EL_BALLOON,                         ACTION_MOVING, MV_BIT_RIGHT
5878   },
5879   {
5880     Yballoon_s,                         FALSE,  FALSE,
5881     EL_BALLOON,                         ACTION_MOVING, MV_BIT_DOWN
5882   },
5883   {
5884     Yballoon_sB,                        FALSE,  TRUE,
5885     EL_BALLOON,                         ACTION_MOVING, MV_BIT_DOWN
5886   },
5887   {
5888     Yballoon_w,                         FALSE,  FALSE,
5889     EL_BALLOON,                         ACTION_MOVING, MV_BIT_LEFT
5890   },
5891   {
5892     Yballoon_wB,                        FALSE,  TRUE,
5893     EL_BALLOON,                         ACTION_MOVING, MV_BIT_LEFT
5894   },
5895   {
5896     Xgrass,                             TRUE,   FALSE,
5897     EL_EMC_GRASS,                       -1, -1
5898   },
5899   {
5900     Ygrass_nB,                          FALSE,  FALSE,
5901     EL_EMC_GRASS,                       ACTION_DIGGING, MV_BIT_UP
5902   },
5903   {
5904     Ygrass_eB,                          FALSE,  FALSE,
5905     EL_EMC_GRASS,                       ACTION_DIGGING, MV_BIT_RIGHT
5906   },
5907   {
5908     Ygrass_sB,                          FALSE,  FALSE,
5909     EL_EMC_GRASS,                       ACTION_DIGGING, MV_BIT_DOWN
5910   },
5911   {
5912     Ygrass_wB,                          FALSE,  FALSE,
5913     EL_EMC_GRASS,                       ACTION_DIGGING, MV_BIT_LEFT
5914   },
5915   {
5916     Xdirt,                              TRUE,   FALSE,
5917     EL_SAND,                            -1, -1
5918   },
5919   {
5920     Ydirt_nB,                           FALSE,  FALSE,
5921     EL_SAND,                            ACTION_DIGGING, MV_BIT_UP
5922   },
5923   {
5924     Ydirt_eB,                           FALSE,  FALSE,
5925     EL_SAND,                            ACTION_DIGGING, MV_BIT_RIGHT
5926   },
5927   {
5928     Ydirt_sB,                           FALSE,  FALSE,
5929     EL_SAND,                            ACTION_DIGGING, MV_BIT_DOWN
5930   },
5931   {
5932     Ydirt_wB,                           FALSE,  FALSE,
5933     EL_SAND,                            ACTION_DIGGING, MV_BIT_LEFT
5934   },
5935   {
5936     Xacid_ne,                           TRUE,   FALSE,
5937     EL_ACID_POOL_TOPRIGHT,              -1, -1
5938   },
5939   {
5940     Xacid_se,                           TRUE,   FALSE,
5941     EL_ACID_POOL_BOTTOMRIGHT,           -1, -1
5942   },
5943   {
5944     Xacid_s,                            TRUE,   FALSE,
5945     EL_ACID_POOL_BOTTOM,                -1, -1
5946   },
5947   {
5948     Xacid_sw,                           TRUE,   FALSE,
5949     EL_ACID_POOL_BOTTOMLEFT,            -1, -1
5950   },
5951   {
5952     Xacid_nw,                           TRUE,   FALSE,
5953     EL_ACID_POOL_TOPLEFT,               -1, -1
5954   },
5955   {
5956     Xacid_1,                            TRUE,   FALSE,
5957     EL_ACID,                            -1, -1
5958   },
5959   {
5960     Xacid_2,                            FALSE,  FALSE,
5961     EL_ACID,                            -1, -1
5962   },
5963   {
5964     Xacid_3,                            FALSE,  FALSE,
5965     EL_ACID,                            -1, -1
5966   },
5967   {
5968     Xacid_4,                            FALSE,  FALSE,
5969     EL_ACID,                            -1, -1
5970   },
5971   {
5972     Xacid_5,                            FALSE,  FALSE,
5973     EL_ACID,                            -1, -1
5974   },
5975   {
5976     Xacid_6,                            FALSE,  FALSE,
5977     EL_ACID,                            -1, -1
5978   },
5979   {
5980     Xacid_7,                            FALSE,  FALSE,
5981     EL_ACID,                            -1, -1
5982   },
5983   {
5984     Xacid_8,                            FALSE,  FALSE,
5985     EL_ACID,                            -1, -1
5986   },
5987   {
5988     Xball_1,                            TRUE,   FALSE,
5989     EL_EMC_MAGIC_BALL,                  -1, -1
5990   },
5991   {
5992     Xball_1B,                           FALSE,  FALSE,
5993     EL_EMC_MAGIC_BALL,                  ACTION_ACTIVE, -1
5994   },
5995   {
5996     Xball_2,                            FALSE,  FALSE,
5997     EL_EMC_MAGIC_BALL,                  ACTION_ACTIVE, -1
5998   },
5999   {
6000     Xball_2B,                           FALSE,  FALSE,
6001     EL_EMC_MAGIC_BALL,                  ACTION_ACTIVE, -1
6002   },
6003   {
6004     Yball_eat,                          FALSE,  FALSE,
6005     EL_EMC_MAGIC_BALL,                  ACTION_DROPPING, -1
6006   },
6007   {
6008     Ykey_1_eat,                         FALSE,  FALSE,
6009     EL_EM_KEY_1,                        ACTION_COLLECTING, -1
6010   },
6011   {
6012     Ykey_2_eat,                         FALSE,  FALSE,
6013     EL_EM_KEY_2,                        ACTION_COLLECTING, -1
6014   },
6015   {
6016     Ykey_3_eat,                         FALSE,  FALSE,
6017     EL_EM_KEY_3,                        ACTION_COLLECTING, -1
6018   },
6019   {
6020     Ykey_4_eat,                         FALSE,  FALSE,
6021     EL_EM_KEY_4,                        ACTION_COLLECTING, -1
6022   },
6023   {
6024     Ykey_5_eat,                         FALSE,  FALSE,
6025     EL_EMC_KEY_5,                       ACTION_COLLECTING, -1
6026   },
6027   {
6028     Ykey_6_eat,                         FALSE,  FALSE,
6029     EL_EMC_KEY_6,                       ACTION_COLLECTING, -1
6030   },
6031   {
6032     Ykey_7_eat,                         FALSE,  FALSE,
6033     EL_EMC_KEY_7,                       ACTION_COLLECTING, -1
6034   },
6035   {
6036     Ykey_8_eat,                         FALSE,  FALSE,
6037     EL_EMC_KEY_8,                       ACTION_COLLECTING, -1
6038   },
6039   {
6040     Ylenses_eat,                        FALSE,  FALSE,
6041     EL_EMC_LENSES,                      ACTION_COLLECTING, -1
6042   },
6043   {
6044     Ymagnify_eat,                       FALSE,  FALSE,
6045     EL_EMC_MAGNIFIER,                   ACTION_COLLECTING, -1
6046   },
6047   {
6048     Ygrass_eat,                         FALSE,  FALSE,
6049     EL_EMC_GRASS,                       ACTION_SNAPPING, -1
6050   },
6051   {
6052     Ydirt_eat,                          FALSE,  FALSE,
6053     EL_SAND,                            ACTION_SNAPPING, -1
6054   },
6055   {
6056     Xgrow_ns,                           TRUE,   FALSE,
6057     EL_EXPANDABLE_WALL_VERTICAL,        -1, -1
6058   },
6059   {
6060     Ygrow_ns_eat,                       FALSE,  FALSE,
6061     EL_EXPANDABLE_WALL_VERTICAL,        ACTION_GROWING, -1
6062   },
6063   {
6064     Xgrow_ew,                           TRUE,   FALSE,
6065     EL_EXPANDABLE_WALL_HORIZONTAL,      -1, -1
6066   },
6067   {
6068     Ygrow_ew_eat,                       FALSE,  FALSE,
6069     EL_EXPANDABLE_WALL_HORIZONTAL,      ACTION_GROWING, -1
6070   },
6071   {
6072     Xwonderwall,                        TRUE,   FALSE,
6073     EL_MAGIC_WALL,                      -1, -1
6074   },
6075   {
6076     XwonderwallB,                       FALSE,  FALSE,
6077     EL_MAGIC_WALL,                      ACTION_ACTIVE, -1
6078   },
6079   {
6080     Xamoeba_1,                          TRUE,   FALSE,
6081     EL_AMOEBA_DRY,                      ACTION_OTHER, -1
6082   },
6083   {
6084     Xamoeba_2,                          FALSE,  FALSE,
6085     EL_AMOEBA_DRY,                      ACTION_OTHER, -1
6086   },
6087   {
6088     Xamoeba_3,                          FALSE,  FALSE,
6089     EL_AMOEBA_DRY,                      ACTION_OTHER, -1
6090   },
6091   {
6092     Xamoeba_4,                          FALSE,  FALSE,
6093     EL_AMOEBA_DRY,                      ACTION_OTHER, -1
6094   },
6095   {
6096     Xamoeba_5,                          TRUE,   FALSE,
6097     EL_AMOEBA_WET,                      ACTION_OTHER, -1
6098   },
6099   {
6100     Xamoeba_6,                          FALSE,  FALSE,
6101     EL_AMOEBA_WET,                      ACTION_OTHER, -1
6102   },
6103   {
6104     Xamoeba_7,                          FALSE,  FALSE,
6105     EL_AMOEBA_WET,                      ACTION_OTHER, -1
6106   },
6107   {
6108     Xamoeba_8,                          FALSE,  FALSE,
6109     EL_AMOEBA_WET,                      ACTION_OTHER, -1
6110   },
6111   {
6112     Xdoor_1,                            TRUE,   FALSE,
6113     EL_EM_GATE_1,                       -1, -1
6114   },
6115   {
6116     Xdoor_2,                            TRUE,   FALSE,
6117     EL_EM_GATE_2,                       -1, -1
6118   },
6119   {
6120     Xdoor_3,                            TRUE,   FALSE,
6121     EL_EM_GATE_3,                       -1, -1
6122   },
6123   {
6124     Xdoor_4,                            TRUE,   FALSE,
6125     EL_EM_GATE_4,                       -1, -1
6126   },
6127   {
6128     Xdoor_5,                            TRUE,   FALSE,
6129     EL_EMC_GATE_5,                      -1, -1
6130   },
6131   {
6132     Xdoor_6,                            TRUE,   FALSE,
6133     EL_EMC_GATE_6,                      -1, -1
6134   },
6135   {
6136     Xdoor_7,                            TRUE,   FALSE,
6137     EL_EMC_GATE_7,                      -1, -1
6138   },
6139   {
6140     Xdoor_8,                            TRUE,   FALSE,
6141     EL_EMC_GATE_8,                      -1, -1
6142   },
6143   {
6144     Xkey_1,                             TRUE,   FALSE,
6145     EL_EM_KEY_1,                        -1, -1
6146   },
6147   {
6148     Xkey_2,                             TRUE,   FALSE,
6149     EL_EM_KEY_2,                        -1, -1
6150   },
6151   {
6152     Xkey_3,                             TRUE,   FALSE,
6153     EL_EM_KEY_3,                        -1, -1
6154   },
6155   {
6156     Xkey_4,                             TRUE,   FALSE,
6157     EL_EM_KEY_4,                        -1, -1
6158   },
6159   {
6160     Xkey_5,                             TRUE,   FALSE,
6161     EL_EMC_KEY_5,                       -1, -1
6162   },
6163   {
6164     Xkey_6,                             TRUE,   FALSE,
6165     EL_EMC_KEY_6,                       -1, -1
6166   },
6167   {
6168     Xkey_7,                             TRUE,   FALSE,
6169     EL_EMC_KEY_7,                       -1, -1
6170   },
6171   {
6172     Xkey_8,                             TRUE,   FALSE,
6173     EL_EMC_KEY_8,                       -1, -1
6174   },
6175   {
6176     Xwind_n,                            TRUE,   FALSE,
6177     EL_BALLOON_SWITCH_UP,               -1, -1
6178   },
6179   {
6180     Xwind_e,                            TRUE,   FALSE,
6181     EL_BALLOON_SWITCH_RIGHT,            -1, -1
6182   },
6183   {
6184     Xwind_s,                            TRUE,   FALSE,
6185     EL_BALLOON_SWITCH_DOWN,             -1, -1
6186   },
6187   {
6188     Xwind_w,                            TRUE,   FALSE,
6189     EL_BALLOON_SWITCH_LEFT,             -1, -1
6190   },
6191   {
6192     Xwind_nesw,                         TRUE,   FALSE,
6193     EL_BALLOON_SWITCH_ANY,              -1, -1
6194   },
6195   {
6196     Xwind_stop,                         TRUE,   FALSE,
6197     EL_BALLOON_SWITCH_NONE,             -1, -1
6198   },
6199   {
6200     Xexit,                              TRUE,   FALSE,
6201     EL_EM_EXIT_CLOSED,                  -1, -1
6202   },
6203   {
6204     Xexit_1,                            TRUE,   FALSE,
6205     EL_EM_EXIT_OPEN,                    -1, -1
6206   },
6207   {
6208     Xexit_2,                            FALSE,  FALSE,
6209     EL_EM_EXIT_OPEN,                    -1, -1
6210   },
6211   {
6212     Xexit_3,                            FALSE,  FALSE,
6213     EL_EM_EXIT_OPEN,                    -1, -1
6214   },
6215   {
6216     Xdynamite,                          TRUE,   FALSE,
6217     EL_EM_DYNAMITE,                     -1, -1
6218   },
6219   {
6220     Ydynamite_eat,                      FALSE,  FALSE,
6221     EL_EM_DYNAMITE,                     ACTION_COLLECTING, -1
6222   },
6223   {
6224     Xdynamite_1,                        TRUE,   FALSE,
6225     EL_EM_DYNAMITE_ACTIVE,              -1, -1
6226   },
6227   {
6228     Xdynamite_2,                        FALSE,  FALSE,
6229     EL_EM_DYNAMITE_ACTIVE,              -1, -1
6230   },
6231   {
6232     Xdynamite_3,                        FALSE,  FALSE,
6233     EL_EM_DYNAMITE_ACTIVE,              -1, -1
6234   },
6235   {
6236     Xdynamite_4,                        FALSE,  FALSE,
6237     EL_EM_DYNAMITE_ACTIVE,              -1, -1
6238   },
6239   {
6240     Xbumper,                            TRUE,   FALSE,
6241     EL_EMC_SPRING_BUMPER,               -1, -1
6242   },
6243   {
6244     XbumperB,                           FALSE,  FALSE,
6245     EL_EMC_SPRING_BUMPER,               ACTION_ACTIVE, -1
6246   },
6247   {
6248     Xwheel,                             TRUE,   FALSE,
6249     EL_ROBOT_WHEEL,                     -1, -1
6250   },
6251   {
6252     XwheelB,                            FALSE,  FALSE,
6253     EL_ROBOT_WHEEL,                     ACTION_ACTIVE, -1
6254   },
6255   {
6256     Xswitch,                            TRUE,   FALSE,
6257     EL_EMC_MAGIC_BALL_SWITCH,           -1, -1
6258   },
6259   {
6260     XswitchB,                           FALSE,  FALSE,
6261     EL_EMC_MAGIC_BALL_SWITCH,           ACTION_ACTIVE, -1
6262   },
6263   {
6264     Xsand,                              TRUE,   FALSE,
6265     EL_QUICKSAND_EMPTY,                 -1, -1
6266   },
6267   {
6268     Xsand_stone,                        TRUE,   FALSE,
6269     EL_QUICKSAND_FULL,                  -1, -1
6270   },
6271   {
6272     Xsand_stonein_1,                    FALSE,  TRUE,
6273     EL_ROCK,                            ACTION_FILLING, -1
6274   },
6275   {
6276     Xsand_stonein_2,                    FALSE,  TRUE,
6277     EL_ROCK,                            ACTION_FILLING, -1
6278   },
6279   {
6280     Xsand_stonein_3,                    FALSE,  TRUE,
6281     EL_ROCK,                            ACTION_FILLING, -1
6282   },
6283   {
6284     Xsand_stonein_4,                    FALSE,  TRUE,
6285     EL_ROCK,                            ACTION_FILLING, -1
6286   },
6287   {
6288     Xsand_stonesand_1,                  FALSE,  FALSE,
6289     EL_QUICKSAND_EMPTYING,              -1, -1
6290   },
6291   {
6292     Xsand_stonesand_2,                  FALSE,  FALSE,
6293     EL_QUICKSAND_EMPTYING,              -1, -1
6294   },
6295   {
6296     Xsand_stonesand_3,                  FALSE,  FALSE,
6297     EL_QUICKSAND_EMPTYING,              -1, -1
6298   },
6299   {
6300     Xsand_stonesand_4,                  FALSE,  FALSE,
6301     EL_QUICKSAND_EMPTYING,              -1, -1
6302   },
6303   {
6304     Xsand_stonesand_quickout_1,         FALSE,  FALSE,
6305     EL_QUICKSAND_EMPTYING,              -1, -1
6306   },
6307   {
6308     Xsand_stonesand_quickout_2,         FALSE,  FALSE,
6309     EL_QUICKSAND_EMPTYING,              -1, -1
6310   },
6311   {
6312     Xsand_stoneout_1,                   FALSE,  FALSE,
6313     EL_ROCK,                            ACTION_EMPTYING, -1
6314   },
6315   {
6316     Xsand_stoneout_2,                   FALSE,  FALSE,
6317     EL_ROCK,                            ACTION_EMPTYING, -1
6318   },
6319   {
6320     Xsand_sandstone_1,                  FALSE,  FALSE,
6321     EL_QUICKSAND_FILLING,               -1, -1
6322   },
6323   {
6324     Xsand_sandstone_2,                  FALSE,  FALSE,
6325     EL_QUICKSAND_FILLING,               -1, -1
6326   },
6327   {
6328     Xsand_sandstone_3,                  FALSE,  FALSE,
6329     EL_QUICKSAND_FILLING,               -1, -1
6330   },
6331   {
6332     Xsand_sandstone_4,                  FALSE,  FALSE,
6333     EL_QUICKSAND_FILLING,               -1, -1
6334   },
6335   {
6336     Xplant,                             TRUE,   FALSE,
6337     EL_EMC_PLANT,                       -1, -1
6338   },
6339   {
6340     Yplant,                             FALSE,  FALSE,
6341     EL_EMC_PLANT,                       -1, -1
6342   },
6343   {
6344     Xlenses,                            TRUE,   FALSE,
6345     EL_EMC_LENSES,                      -1, -1
6346   },
6347   {
6348     Xmagnify,                           TRUE,   FALSE,
6349     EL_EMC_MAGNIFIER,                   -1, -1
6350   },
6351   {
6352     Xdripper,                           TRUE,   FALSE,
6353     EL_EMC_DRIPPER,                     -1, -1
6354   },
6355   {
6356     XdripperB,                          FALSE,  FALSE,
6357     EL_EMC_DRIPPER,                     ACTION_ACTIVE, -1
6358   },
6359   {
6360     Xfake_blank,                        TRUE,   FALSE,
6361     EL_INVISIBLE_WALL,                  -1, -1
6362   },
6363   {
6364     Xfake_blankB,                       FALSE,  FALSE,
6365     EL_INVISIBLE_WALL,                  ACTION_ACTIVE, -1
6366   },
6367   {
6368     Xfake_grass,                        TRUE,   FALSE,
6369     EL_EMC_FAKE_GRASS,                  -1, -1
6370   },
6371   {
6372     Xfake_grassB,                       FALSE,  FALSE,
6373     EL_EMC_FAKE_GRASS,                  ACTION_ACTIVE, -1
6374   },
6375   {
6376     Xfake_door_1,                       TRUE,   FALSE,
6377     EL_EM_GATE_1_GRAY,                  -1, -1
6378   },
6379   {
6380     Xfake_door_2,                       TRUE,   FALSE,
6381     EL_EM_GATE_2_GRAY,                  -1, -1
6382   },
6383   {
6384     Xfake_door_3,                       TRUE,   FALSE,
6385     EL_EM_GATE_3_GRAY,                  -1, -1
6386   },
6387   {
6388     Xfake_door_4,                       TRUE,   FALSE,
6389     EL_EM_GATE_4_GRAY,                  -1, -1
6390   },
6391   {
6392     Xfake_door_5,                       TRUE,   FALSE,
6393     EL_EMC_GATE_5_GRAY,                 -1, -1
6394   },
6395   {
6396     Xfake_door_6,                       TRUE,   FALSE,
6397     EL_EMC_GATE_6_GRAY,                 -1, -1
6398   },
6399   {
6400     Xfake_door_7,                       TRUE,   FALSE,
6401     EL_EMC_GATE_7_GRAY,                 -1, -1
6402   },
6403   {
6404     Xfake_door_8,                       TRUE,   FALSE,
6405     EL_EMC_GATE_8_GRAY,                 -1, -1
6406   },
6407   {
6408     Xfake_acid_1,                       TRUE,   FALSE,
6409     EL_EMC_FAKE_ACID,                   -1, -1
6410   },
6411   {
6412     Xfake_acid_2,                       FALSE,  FALSE,
6413     EL_EMC_FAKE_ACID,                   -1, -1
6414   },
6415   {
6416     Xfake_acid_3,                       FALSE,  FALSE,
6417     EL_EMC_FAKE_ACID,                   -1, -1
6418   },
6419   {
6420     Xfake_acid_4,                       FALSE,  FALSE,
6421     EL_EMC_FAKE_ACID,                   -1, -1
6422   },
6423   {
6424     Xfake_acid_5,                       FALSE,  FALSE,
6425     EL_EMC_FAKE_ACID,                   -1, -1
6426   },
6427   {
6428     Xfake_acid_6,                       FALSE,  FALSE,
6429     EL_EMC_FAKE_ACID,                   -1, -1
6430   },
6431   {
6432     Xfake_acid_7,                       FALSE,  FALSE,
6433     EL_EMC_FAKE_ACID,                   -1, -1
6434   },
6435   {
6436     Xfake_acid_8,                       FALSE,  FALSE,
6437     EL_EMC_FAKE_ACID,                   -1, -1
6438   },
6439   {
6440     Xsteel_1,                           TRUE,   FALSE,
6441     EL_STEELWALL,                       -1, -1
6442   },
6443   {
6444     Xsteel_2,                           TRUE,   FALSE,
6445     EL_EMC_STEELWALL_2,                 -1, -1
6446   },
6447   {
6448     Xsteel_3,                           TRUE,   FALSE,
6449     EL_EMC_STEELWALL_3,                 -1, -1
6450   },
6451   {
6452     Xsteel_4,                           TRUE,   FALSE,
6453     EL_EMC_STEELWALL_4,                 -1, -1
6454   },
6455   {
6456     Xwall_1,                            TRUE,   FALSE,
6457     EL_WALL,                            -1, -1
6458   },
6459   {
6460     Xwall_2,                            TRUE,   FALSE,
6461     EL_EMC_WALL_14,                     -1, -1
6462   },
6463   {
6464     Xwall_3,                            TRUE,   FALSE,
6465     EL_EMC_WALL_15,                     -1, -1
6466   },
6467   {
6468     Xwall_4,                            TRUE,   FALSE,
6469     EL_EMC_WALL_16,                     -1, -1
6470   },
6471   {
6472     Xround_wall_1,                      TRUE,   FALSE,
6473     EL_WALL_SLIPPERY,                   -1, -1
6474   },
6475   {
6476     Xround_wall_2,                      TRUE,   FALSE,
6477     EL_EMC_WALL_SLIPPERY_2,             -1, -1
6478   },
6479   {
6480     Xround_wall_3,                      TRUE,   FALSE,
6481     EL_EMC_WALL_SLIPPERY_3,             -1, -1
6482   },
6483   {
6484     Xround_wall_4,                      TRUE,   FALSE,
6485     EL_EMC_WALL_SLIPPERY_4,             -1, -1
6486   },
6487   {
6488     Xdecor_1,                           TRUE,   FALSE,
6489     EL_EMC_WALL_8,                      -1, -1
6490   },
6491   {
6492     Xdecor_2,                           TRUE,   FALSE,
6493     EL_EMC_WALL_6,                      -1, -1
6494   },
6495   {
6496     Xdecor_3,                           TRUE,   FALSE,
6497     EL_EMC_WALL_4,                      -1, -1
6498   },
6499   {
6500     Xdecor_4,                           TRUE,   FALSE,
6501     EL_EMC_WALL_7,                      -1, -1
6502   },
6503   {
6504     Xdecor_5,                           TRUE,   FALSE,
6505     EL_EMC_WALL_5,                      -1, -1
6506   },
6507   {
6508     Xdecor_6,                           TRUE,   FALSE,
6509     EL_EMC_WALL_9,                      -1, -1
6510   },
6511   {
6512     Xdecor_7,                           TRUE,   FALSE,
6513     EL_EMC_WALL_10,                     -1, -1
6514   },
6515   {
6516     Xdecor_8,                           TRUE,   FALSE,
6517     EL_EMC_WALL_1,                      -1, -1
6518   },
6519   {
6520     Xdecor_9,                           TRUE,   FALSE,
6521     EL_EMC_WALL_2,                      -1, -1
6522   },
6523   {
6524     Xdecor_10,                          TRUE,   FALSE,
6525     EL_EMC_WALL_3,                      -1, -1
6526   },
6527   {
6528     Xdecor_11,                          TRUE,   FALSE,
6529     EL_EMC_WALL_11,                     -1, -1
6530   },
6531   {
6532     Xdecor_12,                          TRUE,   FALSE,
6533     EL_EMC_WALL_12,                     -1, -1
6534   },
6535   {
6536     Xalpha_0,                           TRUE,   FALSE,
6537     EL_CHAR('0'),                       -1, -1
6538   },
6539   {
6540     Xalpha_1,                           TRUE,   FALSE,
6541     EL_CHAR('1'),                       -1, -1
6542   },
6543   {
6544     Xalpha_2,                           TRUE,   FALSE,
6545     EL_CHAR('2'),                       -1, -1
6546   },
6547   {
6548     Xalpha_3,                           TRUE,   FALSE,
6549     EL_CHAR('3'),                       -1, -1
6550   },
6551   {
6552     Xalpha_4,                           TRUE,   FALSE,
6553     EL_CHAR('4'),                       -1, -1
6554   },
6555   {
6556     Xalpha_5,                           TRUE,   FALSE,
6557     EL_CHAR('5'),                       -1, -1
6558   },
6559   {
6560     Xalpha_6,                           TRUE,   FALSE,
6561     EL_CHAR('6'),                       -1, -1
6562   },
6563   {
6564     Xalpha_7,                           TRUE,   FALSE,
6565     EL_CHAR('7'),                       -1, -1
6566   },
6567   {
6568     Xalpha_8,                           TRUE,   FALSE,
6569     EL_CHAR('8'),                       -1, -1
6570   },
6571   {
6572     Xalpha_9,                           TRUE,   FALSE,
6573     EL_CHAR('9'),                       -1, -1
6574   },
6575   {
6576     Xalpha_excla,                       TRUE,   FALSE,
6577     EL_CHAR('!'),                       -1, -1
6578   },
6579   {
6580     Xalpha_quote,                       TRUE,   FALSE,
6581     EL_CHAR('"'),                       -1, -1
6582   },
6583   {
6584     Xalpha_comma,                       TRUE,   FALSE,
6585     EL_CHAR(','),                       -1, -1
6586   },
6587   {
6588     Xalpha_minus,                       TRUE,   FALSE,
6589     EL_CHAR('-'),                       -1, -1
6590   },
6591   {
6592     Xalpha_perio,                       TRUE,   FALSE,
6593     EL_CHAR('.'),                       -1, -1
6594   },
6595   {
6596     Xalpha_colon,                       TRUE,   FALSE,
6597     EL_CHAR(':'),                       -1, -1
6598   },
6599   {
6600     Xalpha_quest,                       TRUE,   FALSE,
6601     EL_CHAR('?'),                       -1, -1
6602   },
6603   {
6604     Xalpha_a,                           TRUE,   FALSE,
6605     EL_CHAR('A'),                       -1, -1
6606   },
6607   {
6608     Xalpha_b,                           TRUE,   FALSE,
6609     EL_CHAR('B'),                       -1, -1
6610   },
6611   {
6612     Xalpha_c,                           TRUE,   FALSE,
6613     EL_CHAR('C'),                       -1, -1
6614   },
6615   {
6616     Xalpha_d,                           TRUE,   FALSE,
6617     EL_CHAR('D'),                       -1, -1
6618   },
6619   {
6620     Xalpha_e,                           TRUE,   FALSE,
6621     EL_CHAR('E'),                       -1, -1
6622   },
6623   {
6624     Xalpha_f,                           TRUE,   FALSE,
6625     EL_CHAR('F'),                       -1, -1
6626   },
6627   {
6628     Xalpha_g,                           TRUE,   FALSE,
6629     EL_CHAR('G'),                       -1, -1
6630   },
6631   {
6632     Xalpha_h,                           TRUE,   FALSE,
6633     EL_CHAR('H'),                       -1, -1
6634   },
6635   {
6636     Xalpha_i,                           TRUE,   FALSE,
6637     EL_CHAR('I'),                       -1, -1
6638   },
6639   {
6640     Xalpha_j,                           TRUE,   FALSE,
6641     EL_CHAR('J'),                       -1, -1
6642   },
6643   {
6644     Xalpha_k,                           TRUE,   FALSE,
6645     EL_CHAR('K'),                       -1, -1
6646   },
6647   {
6648     Xalpha_l,                           TRUE,   FALSE,
6649     EL_CHAR('L'),                       -1, -1
6650   },
6651   {
6652     Xalpha_m,                           TRUE,   FALSE,
6653     EL_CHAR('M'),                       -1, -1
6654   },
6655   {
6656     Xalpha_n,                           TRUE,   FALSE,
6657     EL_CHAR('N'),                       -1, -1
6658   },
6659   {
6660     Xalpha_o,                           TRUE,   FALSE,
6661     EL_CHAR('O'),                       -1, -1
6662   },
6663   {
6664     Xalpha_p,                           TRUE,   FALSE,
6665     EL_CHAR('P'),                       -1, -1
6666   },
6667   {
6668     Xalpha_q,                           TRUE,   FALSE,
6669     EL_CHAR('Q'),                       -1, -1
6670   },
6671   {
6672     Xalpha_r,                           TRUE,   FALSE,
6673     EL_CHAR('R'),                       -1, -1
6674   },
6675   {
6676     Xalpha_s,                           TRUE,   FALSE,
6677     EL_CHAR('S'),                       -1, -1
6678   },
6679   {
6680     Xalpha_t,                           TRUE,   FALSE,
6681     EL_CHAR('T'),                       -1, -1
6682   },
6683   {
6684     Xalpha_u,                           TRUE,   FALSE,
6685     EL_CHAR('U'),                       -1, -1
6686   },
6687   {
6688     Xalpha_v,                           TRUE,   FALSE,
6689     EL_CHAR('V'),                       -1, -1
6690   },
6691   {
6692     Xalpha_w,                           TRUE,   FALSE,
6693     EL_CHAR('W'),                       -1, -1
6694   },
6695   {
6696     Xalpha_x,                           TRUE,   FALSE,
6697     EL_CHAR('X'),                       -1, -1
6698   },
6699   {
6700     Xalpha_y,                           TRUE,   FALSE,
6701     EL_CHAR('Y'),                       -1, -1
6702   },
6703   {
6704     Xalpha_z,                           TRUE,   FALSE,
6705     EL_CHAR('Z'),                       -1, -1
6706   },
6707   {
6708     Xalpha_arrow_e,                     TRUE,   FALSE,
6709     EL_CHAR('>'),                       -1, -1
6710   },
6711   {
6712     Xalpha_arrow_w,                     TRUE,   FALSE,
6713     EL_CHAR('<'),                       -1, -1
6714   },
6715   {
6716     Xalpha_copyr,                       TRUE,   FALSE,
6717     EL_CHAR(CHAR_BYTE_COPYRIGHT),       -1, -1
6718   },
6719
6720   {
6721     Xboom_bug,                          FALSE,  FALSE,
6722     EL_BUG,                             ACTION_EXPLODING, -1
6723   },
6724   {
6725     Xboom_bomb,                         FALSE,  FALSE,
6726     EL_BOMB,                            ACTION_EXPLODING, -1
6727   },
6728   {
6729     Xboom_android,                      FALSE,  FALSE,
6730     EL_EMC_ANDROID,                     ACTION_OTHER, -1
6731   },
6732   {
6733     Xboom_1,                            FALSE,  FALSE,
6734     EL_DEFAULT,                         ACTION_EXPLODING, -1
6735   },
6736   {
6737     Xboom_2,                            FALSE,  FALSE,
6738     EL_DEFAULT,                         ACTION_EXPLODING, -1
6739   },
6740   {
6741     Znormal,                            FALSE,  FALSE,
6742     EL_EMPTY,                           -1, -1
6743   },
6744   {
6745     Zdynamite,                          FALSE,  FALSE,
6746     EL_EMPTY,                           -1, -1
6747   },
6748   {
6749     Zplayer,                            FALSE,  FALSE,
6750     EL_EMPTY,                           -1, -1
6751   },
6752   {
6753     ZBORDER,                            FALSE,  FALSE,
6754     EL_EMPTY,                           -1, -1
6755   },
6756
6757   {
6758     -1,                                 FALSE,  FALSE,
6759     -1,                                 -1, -1
6760   }
6761 };
6762
6763 static struct Mapping_EM_to_RND_player
6764 {
6765   int action_em;
6766   int player_nr;
6767
6768   int element_rnd;
6769   int action;
6770   int direction;
6771 }
6772 em_player_mapping_list[] =
6773 {
6774   {
6775     SPR_walk + 0,                       0,
6776     EL_PLAYER_1,                        ACTION_MOVING, MV_BIT_UP,
6777   },
6778   {
6779     SPR_walk + 1,                       0,
6780     EL_PLAYER_1,                        ACTION_MOVING, MV_BIT_RIGHT,
6781   },
6782   {
6783     SPR_walk + 2,                       0,
6784     EL_PLAYER_1,                        ACTION_MOVING, MV_BIT_DOWN,
6785   },
6786   {
6787     SPR_walk + 3,                       0,
6788     EL_PLAYER_1,                        ACTION_MOVING, MV_BIT_LEFT,
6789   },
6790   {
6791     SPR_push + 0,                       0,
6792     EL_PLAYER_1,                        ACTION_PUSHING, MV_BIT_UP,
6793   },
6794   {
6795     SPR_push + 1,                       0,
6796     EL_PLAYER_1,                        ACTION_PUSHING, MV_BIT_RIGHT,
6797   },
6798   {
6799     SPR_push + 2,                       0,
6800     EL_PLAYER_1,                        ACTION_PUSHING, MV_BIT_DOWN,
6801   },
6802   {
6803     SPR_push + 3,                       0,
6804     EL_PLAYER_1,                        ACTION_PUSHING, MV_BIT_LEFT,
6805   },
6806   {
6807     SPR_spray + 0,                      0,
6808     EL_PLAYER_1,                        ACTION_SNAPPING, MV_BIT_UP,
6809   },
6810   {
6811     SPR_spray + 1,                      0,
6812     EL_PLAYER_1,                        ACTION_SNAPPING, MV_BIT_RIGHT,
6813   },
6814   {
6815     SPR_spray + 2,                      0,
6816     EL_PLAYER_1,                        ACTION_SNAPPING, MV_BIT_DOWN,
6817   },
6818   {
6819     SPR_spray + 3,                      0,
6820     EL_PLAYER_1,                        ACTION_SNAPPING, MV_BIT_LEFT,
6821   },
6822   {
6823     SPR_walk + 0,                       1,
6824     EL_PLAYER_2,                        ACTION_MOVING, MV_BIT_UP,
6825   },
6826   {
6827     SPR_walk + 1,                       1,
6828     EL_PLAYER_2,                        ACTION_MOVING, MV_BIT_RIGHT,
6829   },
6830   {
6831     SPR_walk + 2,                       1,
6832     EL_PLAYER_2,                        ACTION_MOVING, MV_BIT_DOWN,
6833   },
6834   {
6835     SPR_walk + 3,                       1,
6836     EL_PLAYER_2,                        ACTION_MOVING, MV_BIT_LEFT,
6837   },
6838   {
6839     SPR_push + 0,                       1,
6840     EL_PLAYER_2,                        ACTION_PUSHING, MV_BIT_UP,
6841   },
6842   {
6843     SPR_push + 1,                       1,
6844     EL_PLAYER_2,                        ACTION_PUSHING, MV_BIT_RIGHT,
6845   },
6846   {
6847     SPR_push + 2,                       1,
6848     EL_PLAYER_2,                        ACTION_PUSHING, MV_BIT_DOWN,
6849   },
6850   {
6851     SPR_push + 3,                       1,
6852     EL_PLAYER_2,                        ACTION_PUSHING, MV_BIT_LEFT,
6853   },
6854   {
6855     SPR_spray + 0,                      1,
6856     EL_PLAYER_2,                        ACTION_SNAPPING, MV_BIT_UP,
6857   },
6858   {
6859     SPR_spray + 1,                      1,
6860     EL_PLAYER_2,                        ACTION_SNAPPING, MV_BIT_RIGHT,
6861   },
6862   {
6863     SPR_spray + 2,                      1,
6864     EL_PLAYER_2,                        ACTION_SNAPPING, MV_BIT_DOWN,
6865   },
6866   {
6867     SPR_spray + 3,                      1,
6868     EL_PLAYER_2,                        ACTION_SNAPPING, MV_BIT_LEFT,
6869   },
6870   {
6871     SPR_still,                          0,
6872     EL_PLAYER_1,                        ACTION_DEFAULT, -1,
6873   },
6874   {
6875     SPR_still,                          1,
6876     EL_PLAYER_2,                        ACTION_DEFAULT, -1,
6877   },
6878   {
6879     SPR_walk + 0,                       2,
6880     EL_PLAYER_3,                        ACTION_MOVING, MV_BIT_UP,
6881   },
6882   {
6883     SPR_walk + 1,                       2,
6884     EL_PLAYER_3,                        ACTION_MOVING, MV_BIT_RIGHT,
6885   },
6886   {
6887     SPR_walk + 2,                       2,
6888     EL_PLAYER_3,                        ACTION_MOVING, MV_BIT_DOWN,
6889   },
6890   {
6891     SPR_walk + 3,                       2,
6892     EL_PLAYER_3,                        ACTION_MOVING, MV_BIT_LEFT,
6893   },
6894   {
6895     SPR_push + 0,                       2,
6896     EL_PLAYER_3,                        ACTION_PUSHING, MV_BIT_UP,
6897   },
6898   {
6899     SPR_push + 1,                       2,
6900     EL_PLAYER_3,                        ACTION_PUSHING, MV_BIT_RIGHT,
6901   },
6902   {
6903     SPR_push + 2,                       2,
6904     EL_PLAYER_3,                        ACTION_PUSHING, MV_BIT_DOWN,
6905   },
6906   {
6907     SPR_push + 3,                       2,
6908     EL_PLAYER_3,                        ACTION_PUSHING, MV_BIT_LEFT,
6909   },
6910   {
6911     SPR_spray + 0,                      2,
6912     EL_PLAYER_3,                        ACTION_SNAPPING, MV_BIT_UP,
6913   },
6914   {
6915     SPR_spray + 1,                      2,
6916     EL_PLAYER_3,                        ACTION_SNAPPING, MV_BIT_RIGHT,
6917   },
6918   {
6919     SPR_spray + 2,                      2,
6920     EL_PLAYER_3,                        ACTION_SNAPPING, MV_BIT_DOWN,
6921   },
6922   {
6923     SPR_spray + 3,                      2,
6924     EL_PLAYER_3,                        ACTION_SNAPPING, MV_BIT_LEFT,
6925   },
6926   {
6927     SPR_walk + 0,                       3,
6928     EL_PLAYER_4,                        ACTION_MOVING, MV_BIT_UP,
6929   },
6930   {
6931     SPR_walk + 1,                       3,
6932     EL_PLAYER_4,                        ACTION_MOVING, MV_BIT_RIGHT,
6933   },
6934   {
6935     SPR_walk + 2,                       3,
6936     EL_PLAYER_4,                        ACTION_MOVING, MV_BIT_DOWN,
6937   },
6938   {
6939     SPR_walk + 3,                       3,
6940     EL_PLAYER_4,                        ACTION_MOVING, MV_BIT_LEFT,
6941   },
6942   {
6943     SPR_push + 0,                       3,
6944     EL_PLAYER_4,                        ACTION_PUSHING, MV_BIT_UP,
6945   },
6946   {
6947     SPR_push + 1,                       3,
6948     EL_PLAYER_4,                        ACTION_PUSHING, MV_BIT_RIGHT,
6949   },
6950   {
6951     SPR_push + 2,                       3,
6952     EL_PLAYER_4,                        ACTION_PUSHING, MV_BIT_DOWN,
6953   },
6954   {
6955     SPR_push + 3,                       3,
6956     EL_PLAYER_4,                        ACTION_PUSHING, MV_BIT_LEFT,
6957   },
6958   {
6959     SPR_spray + 0,                      3,
6960     EL_PLAYER_4,                        ACTION_SNAPPING, MV_BIT_UP,
6961   },
6962   {
6963     SPR_spray + 1,                      3,
6964     EL_PLAYER_4,                        ACTION_SNAPPING, MV_BIT_RIGHT,
6965   },
6966   {
6967     SPR_spray + 2,                      3,
6968     EL_PLAYER_4,                        ACTION_SNAPPING, MV_BIT_DOWN,
6969   },
6970   {
6971     SPR_spray + 3,                      3,
6972     EL_PLAYER_4,                        ACTION_SNAPPING, MV_BIT_LEFT,
6973   },
6974   {
6975     SPR_still,                          2,
6976     EL_PLAYER_3,                        ACTION_DEFAULT, -1,
6977   },
6978   {
6979     SPR_still,                          3,
6980     EL_PLAYER_4,                        ACTION_DEFAULT, -1,
6981   },
6982
6983   {
6984     -1,                                 -1,
6985     -1,                                 -1, -1
6986   }
6987 };
6988
6989 int map_element_RND_to_EM(int element_rnd)
6990 {
6991   static unsigned short mapping_RND_to_EM[NUM_FILE_ELEMENTS];
6992   static boolean mapping_initialized = FALSE;
6993
6994   if (!mapping_initialized)
6995   {
6996     int i;
6997
6998     /* return "Xalpha_quest" for all undefined elements in mapping array */
6999     for (i = 0; i < NUM_FILE_ELEMENTS; i++)
7000       mapping_RND_to_EM[i] = Xalpha_quest;
7001
7002     for (i = 0; em_object_mapping_list[i].element_em != -1; i++)
7003       if (em_object_mapping_list[i].is_rnd_to_em_mapping)
7004         mapping_RND_to_EM[em_object_mapping_list[i].element_rnd] =
7005           em_object_mapping_list[i].element_em;
7006
7007     mapping_initialized = TRUE;
7008   }
7009
7010   if (element_rnd >= 0 && element_rnd < NUM_FILE_ELEMENTS)
7011     return mapping_RND_to_EM[element_rnd];
7012
7013   Error(ERR_WARN, "invalid RND level element %d", element_rnd);
7014
7015   return EL_UNKNOWN;
7016 }
7017
7018 int map_element_EM_to_RND(int element_em)
7019 {
7020   static unsigned short mapping_EM_to_RND[TILE_MAX];
7021   static boolean mapping_initialized = FALSE;
7022
7023   if (!mapping_initialized)
7024   {
7025     int i;
7026
7027     /* return "EL_UNKNOWN" for all undefined elements in mapping array */
7028     for (i = 0; i < TILE_MAX; i++)
7029       mapping_EM_to_RND[i] = EL_UNKNOWN;
7030
7031     for (i = 0; em_object_mapping_list[i].element_em != -1; i++)
7032       mapping_EM_to_RND[em_object_mapping_list[i].element_em] =
7033         em_object_mapping_list[i].element_rnd;
7034
7035     mapping_initialized = TRUE;
7036   }
7037
7038   if (element_em >= 0 && element_em < TILE_MAX)
7039     return mapping_EM_to_RND[element_em];
7040
7041   Error(ERR_WARN, "invalid EM level element %d", element_em);
7042
7043   return EL_UNKNOWN;
7044 }
7045
7046 void map_android_clone_elements_RND_to_EM(struct LevelInfo *level)
7047 {
7048   struct LevelInfo_EM *level_em = level->native_em_level;
7049   struct LEVEL *lev = level_em->lev;
7050   int i, j;
7051
7052   for (i = 0; i < TILE_MAX; i++)
7053     lev->android_array[i] = Xblank;
7054
7055   for (i = 0; i < level->num_android_clone_elements; i++)
7056   {
7057     int element_rnd = level->android_clone_element[i];
7058     int element_em = map_element_RND_to_EM(element_rnd);
7059
7060     for (j = 0; em_object_mapping_list[j].element_em != -1; j++)
7061       if (em_object_mapping_list[j].element_rnd == element_rnd)
7062         lev->android_array[em_object_mapping_list[j].element_em] = element_em;
7063   }
7064 }
7065
7066 void map_android_clone_elements_EM_to_RND(struct LevelInfo *level)
7067 {
7068   struct LevelInfo_EM *level_em = level->native_em_level;
7069   struct LEVEL *lev = level_em->lev;
7070   int i, j;
7071
7072   level->num_android_clone_elements = 0;
7073
7074   for (i = 0; i < TILE_MAX; i++)
7075   {
7076     int element_em = lev->android_array[i];
7077     int element_rnd;
7078     boolean element_found = FALSE;
7079
7080     if (element_em == Xblank)
7081       continue;
7082
7083     element_rnd = map_element_EM_to_RND(element_em);
7084
7085     for (j = 0; j < level->num_android_clone_elements; j++)
7086       if (level->android_clone_element[j] == element_rnd)
7087         element_found = TRUE;
7088
7089     if (!element_found)
7090     {
7091       level->android_clone_element[level->num_android_clone_elements++] =
7092         element_rnd;
7093
7094       if (level->num_android_clone_elements == MAX_ANDROID_ELEMENTS)
7095         break;
7096     }
7097   }
7098
7099   if (level->num_android_clone_elements == 0)
7100   {
7101     level->num_android_clone_elements = 1;
7102     level->android_clone_element[0] = EL_EMPTY;
7103   }
7104 }
7105
7106 int map_direction_RND_to_EM(int direction)
7107 {
7108   return (direction == MV_UP    ? 0 :
7109           direction == MV_RIGHT ? 1 :
7110           direction == MV_DOWN  ? 2 :
7111           direction == MV_LEFT  ? 3 :
7112           -1);
7113 }
7114
7115 int map_direction_EM_to_RND(int direction)
7116 {
7117   return (direction == 0 ? MV_UP    :
7118           direction == 1 ? MV_RIGHT :
7119           direction == 2 ? MV_DOWN  :
7120           direction == 3 ? MV_LEFT  :
7121           MV_NONE);
7122 }
7123
7124 int map_element_RND_to_SP(int element_rnd)
7125 {
7126   int element_sp = 0x20;        /* map unknown elements to yellow "hardware" */
7127
7128   if (element_rnd >= EL_SP_START &&
7129       element_rnd <= EL_SP_END)
7130     element_sp = element_rnd - EL_SP_START;
7131   else if (element_rnd == EL_EMPTY_SPACE)
7132     element_sp = 0x00;
7133   else if (element_rnd == EL_INVISIBLE_WALL)
7134     element_sp = 0x28;
7135
7136   return element_sp;
7137 }
7138
7139 int map_element_SP_to_RND(int element_sp)
7140 {
7141   int element_rnd = EL_UNKNOWN;
7142
7143   if (element_sp >= 0x00 &&
7144       element_sp <= 0x27)
7145     element_rnd = EL_SP_START + element_sp;
7146   else if (element_sp == 0x28)
7147     element_rnd = EL_INVISIBLE_WALL;
7148
7149   return element_rnd;
7150 }
7151
7152 int map_action_SP_to_RND(int action_sp)
7153 {
7154   switch (action_sp)
7155   {
7156     case actActive:             return ACTION_ACTIVE;
7157     case actImpact:             return ACTION_IMPACT;
7158     case actExploding:          return ACTION_EXPLODING;
7159     case actDigging:            return ACTION_DIGGING;
7160     case actSnapping:           return ACTION_SNAPPING;
7161     case actCollecting:         return ACTION_COLLECTING;
7162     case actPassing:            return ACTION_PASSING;
7163     case actPushing:            return ACTION_PUSHING;
7164     case actDropping:           return ACTION_DROPPING;
7165
7166     default:                    return ACTION_DEFAULT;
7167   }
7168 }
7169
7170 int get_next_element(int element)
7171 {
7172   switch (element)
7173   {
7174     case EL_QUICKSAND_FILLING:          return EL_QUICKSAND_FULL;
7175     case EL_QUICKSAND_EMPTYING:         return EL_QUICKSAND_EMPTY;
7176     case EL_QUICKSAND_FAST_FILLING:     return EL_QUICKSAND_FAST_FULL;
7177     case EL_QUICKSAND_FAST_EMPTYING:    return EL_QUICKSAND_FAST_EMPTY;
7178     case EL_MAGIC_WALL_FILLING:         return EL_MAGIC_WALL_FULL;
7179     case EL_MAGIC_WALL_EMPTYING:        return EL_MAGIC_WALL_ACTIVE;
7180     case EL_BD_MAGIC_WALL_FILLING:      return EL_BD_MAGIC_WALL_FULL;
7181     case EL_BD_MAGIC_WALL_EMPTYING:     return EL_BD_MAGIC_WALL_ACTIVE;
7182     case EL_DC_MAGIC_WALL_FILLING:      return EL_DC_MAGIC_WALL_FULL;
7183     case EL_DC_MAGIC_WALL_EMPTYING:     return EL_DC_MAGIC_WALL_ACTIVE;
7184     case EL_AMOEBA_DROPPING:            return EL_AMOEBA_WET;
7185
7186     default:                            return element;
7187   }
7188 }
7189
7190 int el_act_dir2img(int element, int action, int direction)
7191 {
7192   element = GFX_ELEMENT(element);
7193   direction = MV_DIR_TO_BIT(direction); /* default: MV_NONE => MV_DOWN */
7194
7195   /* direction_graphic[][] == graphic[] for undefined direction graphics */
7196   return element_info[element].direction_graphic[action][direction];
7197 }
7198
7199 static int el_act_dir2crm(int element, int action, int direction)
7200 {
7201   element = GFX_ELEMENT(element);
7202   direction = MV_DIR_TO_BIT(direction); /* default: MV_NONE => MV_DOWN */
7203
7204   /* direction_graphic[][] == graphic[] for undefined direction graphics */
7205   return element_info[element].direction_crumbled[action][direction];
7206 }
7207
7208 int el_act2img(int element, int action)
7209 {
7210   element = GFX_ELEMENT(element);
7211
7212   return element_info[element].graphic[action];
7213 }
7214
7215 int el_act2crm(int element, int action)
7216 {
7217   element = GFX_ELEMENT(element);
7218
7219   return element_info[element].crumbled[action];
7220 }
7221
7222 int el_dir2img(int element, int direction)
7223 {
7224   element = GFX_ELEMENT(element);
7225
7226   return el_act_dir2img(element, ACTION_DEFAULT, direction);
7227 }
7228
7229 int el2baseimg(int element)
7230 {
7231   return element_info[element].graphic[ACTION_DEFAULT];
7232 }
7233
7234 int el2img(int element)
7235 {
7236   element = GFX_ELEMENT(element);
7237
7238   return element_info[element].graphic[ACTION_DEFAULT];
7239 }
7240
7241 int el2edimg(int element)
7242 {
7243   element = GFX_ELEMENT(element);
7244
7245   return element_info[element].special_graphic[GFX_SPECIAL_ARG_EDITOR];
7246 }
7247
7248 int el2preimg(int element)
7249 {
7250   element = GFX_ELEMENT(element);
7251
7252   return element_info[element].special_graphic[GFX_SPECIAL_ARG_PREVIEW];
7253 }
7254
7255 int el2panelimg(int element)
7256 {
7257   element = GFX_ELEMENT(element);
7258
7259   return element_info[element].special_graphic[GFX_SPECIAL_ARG_PANEL];
7260 }
7261
7262 int font2baseimg(int font_nr)
7263 {
7264   return font_info[font_nr].special_graphic[GFX_SPECIAL_ARG_DEFAULT];
7265 }
7266
7267 int getBeltNrFromBeltElement(int element)
7268 {
7269   return (element < EL_CONVEYOR_BELT_2_LEFT ? 0 :
7270           element < EL_CONVEYOR_BELT_3_LEFT ? 1 :
7271           element < EL_CONVEYOR_BELT_4_LEFT ? 2 : 3);
7272 }
7273
7274 int getBeltNrFromBeltActiveElement(int element)
7275 {
7276   return (element < EL_CONVEYOR_BELT_2_LEFT_ACTIVE ? 0 :
7277           element < EL_CONVEYOR_BELT_3_LEFT_ACTIVE ? 1 :
7278           element < EL_CONVEYOR_BELT_4_LEFT_ACTIVE ? 2 : 3);
7279 }
7280
7281 int getBeltNrFromBeltSwitchElement(int element)
7282 {
7283   return (element < EL_CONVEYOR_BELT_2_SWITCH_LEFT ? 0 :
7284           element < EL_CONVEYOR_BELT_3_SWITCH_LEFT ? 1 :
7285           element < EL_CONVEYOR_BELT_4_SWITCH_LEFT ? 2 : 3);
7286 }
7287
7288 int getBeltDirNrFromBeltElement(int element)
7289 {
7290   static int belt_base_element[4] =
7291   {
7292     EL_CONVEYOR_BELT_1_LEFT,
7293     EL_CONVEYOR_BELT_2_LEFT,
7294     EL_CONVEYOR_BELT_3_LEFT,
7295     EL_CONVEYOR_BELT_4_LEFT
7296   };
7297
7298   int belt_nr = getBeltNrFromBeltElement(element);
7299   int belt_dir_nr = element - belt_base_element[belt_nr];
7300
7301   return (belt_dir_nr % 3);
7302 }
7303
7304 int getBeltDirNrFromBeltSwitchElement(int element)
7305 {
7306   static int belt_base_element[4] =
7307   {
7308     EL_CONVEYOR_BELT_1_SWITCH_LEFT,
7309     EL_CONVEYOR_BELT_2_SWITCH_LEFT,
7310     EL_CONVEYOR_BELT_3_SWITCH_LEFT,
7311     EL_CONVEYOR_BELT_4_SWITCH_LEFT
7312   };
7313
7314   int belt_nr = getBeltNrFromBeltSwitchElement(element);
7315   int belt_dir_nr = element - belt_base_element[belt_nr];
7316
7317   return (belt_dir_nr % 3);
7318 }
7319
7320 int getBeltDirFromBeltElement(int element)
7321 {
7322   static int belt_move_dir[3] =
7323   {
7324     MV_LEFT,
7325     MV_NONE,
7326     MV_RIGHT
7327   };
7328
7329   int belt_dir_nr = getBeltDirNrFromBeltElement(element);
7330
7331   return belt_move_dir[belt_dir_nr];
7332 }
7333
7334 int getBeltDirFromBeltSwitchElement(int element)
7335 {
7336   static int belt_move_dir[3] =
7337   {
7338     MV_LEFT,
7339     MV_NONE,
7340     MV_RIGHT
7341   };
7342
7343   int belt_dir_nr = getBeltDirNrFromBeltSwitchElement(element);
7344
7345   return belt_move_dir[belt_dir_nr];
7346 }
7347
7348 int getBeltElementFromBeltNrAndBeltDirNr(int belt_nr, int belt_dir_nr)
7349 {
7350   static int belt_base_element[4] =
7351   {
7352     EL_CONVEYOR_BELT_1_LEFT,
7353     EL_CONVEYOR_BELT_2_LEFT,
7354     EL_CONVEYOR_BELT_3_LEFT,
7355     EL_CONVEYOR_BELT_4_LEFT
7356   };
7357
7358   return belt_base_element[belt_nr] + belt_dir_nr;
7359 }
7360
7361 int getBeltElementFromBeltNrAndBeltDir(int belt_nr, int belt_dir)
7362 {
7363   int belt_dir_nr = (belt_dir == MV_LEFT ? 0 : belt_dir == MV_RIGHT ? 2 : 1);
7364
7365   return getBeltElementFromBeltNrAndBeltDirNr(belt_nr, belt_dir_nr);
7366 }
7367
7368 int getBeltSwitchElementFromBeltNrAndBeltDirNr(int belt_nr, int belt_dir_nr)
7369 {
7370   static int belt_base_element[4] =
7371   {
7372     EL_CONVEYOR_BELT_1_SWITCH_LEFT,
7373     EL_CONVEYOR_BELT_2_SWITCH_LEFT,
7374     EL_CONVEYOR_BELT_3_SWITCH_LEFT,
7375     EL_CONVEYOR_BELT_4_SWITCH_LEFT
7376   };
7377
7378   return belt_base_element[belt_nr] + belt_dir_nr;
7379 }
7380
7381 int getBeltSwitchElementFromBeltNrAndBeltDir(int belt_nr, int belt_dir)
7382 {
7383   int belt_dir_nr = (belt_dir == MV_LEFT ? 0 : belt_dir == MV_RIGHT ? 2 : 1);
7384
7385   return getBeltSwitchElementFromBeltNrAndBeltDirNr(belt_nr, belt_dir_nr);
7386 }
7387
7388 boolean getTeamMode_EM()
7389 {
7390   return game.team_mode;
7391 }
7392
7393 int getGameFrameDelay_EM(int native_em_game_frame_delay)
7394 {
7395   int game_frame_delay_value;
7396
7397   game_frame_delay_value =
7398     (tape.playing && tape.fast_forward ? FfwdFrameDelay :
7399      GameFrameDelay == GAME_FRAME_DELAY ? native_em_game_frame_delay :
7400      GameFrameDelay);
7401
7402   if (tape.playing && tape.warp_forward && !tape.pausing)
7403     game_frame_delay_value = 0;
7404
7405   return game_frame_delay_value;
7406 }
7407
7408 unsigned int InitRND(int seed)
7409 {
7410   if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
7411     return InitEngineRandom_EM(seed);
7412   else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
7413     return InitEngineRandom_SP(seed);
7414   else
7415     return InitEngineRandom_RND(seed);
7416 }
7417
7418 static struct Mapping_EM_to_RND_object object_mapping[TILE_MAX];
7419 static struct Mapping_EM_to_RND_player player_mapping[MAX_PLAYERS][SPR_MAX];
7420
7421 inline static int get_effective_element_EM(int tile, int frame_em)
7422 {
7423   int element             = object_mapping[tile].element_rnd;
7424   int action              = object_mapping[tile].action;
7425   boolean is_backside     = object_mapping[tile].is_backside;
7426   boolean action_removing = (action == ACTION_DIGGING ||
7427                              action == ACTION_SNAPPING ||
7428                              action == ACTION_COLLECTING);
7429
7430   if (frame_em < 7)
7431   {
7432     switch (tile)
7433     {
7434       case Yacid_splash_eB:
7435       case Yacid_splash_wB:
7436         return (frame_em > 5 ? EL_EMPTY : element);
7437
7438       default:
7439         return element;
7440     }
7441   }
7442   else  /* frame_em == 7 */
7443   {
7444     switch (tile)
7445     {
7446       case Yacid_splash_eB:
7447       case Yacid_splash_wB:
7448         return EL_EMPTY;
7449
7450       case Yemerald_stone:
7451         return EL_EMERALD;
7452
7453       case Ydiamond_stone:
7454         return EL_ROCK;
7455
7456       case Xdrip_stretch:
7457       case Xdrip_stretchB:
7458       case Ydrip_s1:
7459       case Ydrip_s1B:
7460       case Xball_1B:
7461       case Xball_2:
7462       case Xball_2B:
7463       case Yball_eat:
7464       case Ykey_1_eat:
7465       case Ykey_2_eat:
7466       case Ykey_3_eat:
7467       case Ykey_4_eat:
7468       case Ykey_5_eat:
7469       case Ykey_6_eat:
7470       case Ykey_7_eat:
7471       case Ykey_8_eat:
7472       case Ylenses_eat:
7473       case Ymagnify_eat:
7474       case Ygrass_eat:
7475       case Ydirt_eat:
7476       case Xsand_stonein_1:
7477       case Xsand_stonein_2:
7478       case Xsand_stonein_3:
7479       case Xsand_stonein_4:
7480         return element;
7481
7482       default:
7483         return (is_backside || action_removing ? EL_EMPTY : element);
7484     }
7485   }
7486 }
7487
7488 inline static boolean check_linear_animation_EM(int tile)
7489 {
7490   switch (tile)
7491   {
7492     case Xsand_stonesand_1:
7493     case Xsand_stonesand_quickout_1:
7494     case Xsand_sandstone_1:
7495     case Xsand_stonein_1:
7496     case Xsand_stoneout_1:
7497     case Xboom_1:
7498     case Xdynamite_1:
7499     case Ybug_w_n:
7500     case Ybug_n_e:
7501     case Ybug_e_s:
7502     case Ybug_s_w:
7503     case Ybug_e_n:
7504     case Ybug_s_e:
7505     case Ybug_w_s:
7506     case Ybug_n_w:
7507     case Ytank_w_n:
7508     case Ytank_n_e:
7509     case Ytank_e_s:
7510     case Ytank_s_w:
7511     case Ytank_e_n:
7512     case Ytank_s_e:
7513     case Ytank_w_s:
7514     case Ytank_n_w:
7515     case Yacid_splash_eB:
7516     case Yacid_splash_wB:
7517     case Yemerald_stone:
7518       return TRUE;
7519   }
7520
7521   return FALSE;
7522 }
7523
7524 inline static void set_crumbled_graphics_EM(struct GraphicInfo_EM *g_em,
7525                                             boolean has_crumbled_graphics,
7526                                             int crumbled, int sync_frame)
7527 {
7528   /* if element can be crumbled, but certain action graphics are just empty
7529      space (like instantly snapping sand to empty space in 1 frame), do not
7530      treat these empty space graphics as crumbled graphics in EMC engine */
7531   if (crumbled == IMG_EMPTY_SPACE)
7532     has_crumbled_graphics = FALSE;
7533
7534   if (has_crumbled_graphics)
7535   {
7536     struct GraphicInfo *g_crumbled = &graphic_info[crumbled];
7537     int frame_crumbled = getAnimationFrame(g_crumbled->anim_frames,
7538                                            g_crumbled->anim_delay,
7539                                            g_crumbled->anim_mode,
7540                                            g_crumbled->anim_start_frame,
7541                                            sync_frame);
7542
7543     getGraphicSource(crumbled, frame_crumbled, &g_em->crumbled_bitmap,
7544                      &g_em->crumbled_src_x, &g_em->crumbled_src_y);
7545
7546     g_em->crumbled_border_size = graphic_info[crumbled].border_size;
7547     g_em->crumbled_tile_size = graphic_info[crumbled].tile_size;
7548
7549     g_em->has_crumbled_graphics = TRUE;
7550   }
7551   else
7552   {
7553     g_em->crumbled_bitmap = NULL;
7554     g_em->crumbled_src_x = 0;
7555     g_em->crumbled_src_y = 0;
7556     g_em->crumbled_border_size = 0;
7557     g_em->crumbled_tile_size = 0;
7558
7559     g_em->has_crumbled_graphics = FALSE;
7560   }
7561 }
7562
7563 void ResetGfxAnimation_EM(int x, int y, int tile)
7564 {
7565   GfxFrame[x][y] = 0;
7566 }
7567
7568 void SetGfxAnimation_EM(struct GraphicInfo_EM *g_em,
7569                         int tile, int frame_em, int x, int y)
7570 {
7571   int action = object_mapping[tile].action;
7572   int direction = object_mapping[tile].direction;
7573   int effective_element = get_effective_element_EM(tile, frame_em);
7574   int graphic = (direction == MV_NONE ?
7575                  el_act2img(effective_element, action) :
7576                  el_act_dir2img(effective_element, action, direction));
7577   struct GraphicInfo *g = &graphic_info[graphic];
7578   int sync_frame;
7579   boolean action_removing = (action == ACTION_DIGGING ||
7580                              action == ACTION_SNAPPING ||
7581                              action == ACTION_COLLECTING);
7582   boolean action_moving   = (action == ACTION_FALLING ||
7583                              action == ACTION_MOVING ||
7584                              action == ACTION_PUSHING ||
7585                              action == ACTION_EATING ||
7586                              action == ACTION_FILLING ||
7587                              action == ACTION_EMPTYING);
7588   boolean action_falling  = (action == ACTION_FALLING ||
7589                              action == ACTION_FILLING ||
7590                              action == ACTION_EMPTYING);
7591
7592   /* special case: graphic uses "2nd movement tile" and has defined
7593      7 frames for movement animation (or less) => use default graphic
7594      for last (8th) frame which ends the movement animation */
7595   if (g->double_movement && g->anim_frames < 8 && frame_em == 7)
7596   {
7597     action = ACTION_DEFAULT;    /* (keep action_* unchanged for now) */
7598     graphic = (direction == MV_NONE ?
7599                el_act2img(effective_element, action) :
7600                el_act_dir2img(effective_element, action, direction));
7601
7602     g = &graphic_info[graphic];
7603   }
7604
7605   if ((action_removing || check_linear_animation_EM(tile)) && frame_em == 0)
7606   {
7607     GfxFrame[x][y] = 0;
7608   }
7609   else if (action_moving)
7610   {
7611     boolean is_backside = object_mapping[tile].is_backside;
7612
7613     if (is_backside)
7614     {
7615       int direction = object_mapping[tile].direction;
7616       int move_dir = (action_falling ? MV_DOWN : direction);
7617
7618       GfxFrame[x][y]++;
7619
7620 #if 1
7621       /* !!! TEST !!! NEW !!! DOES NOT WORK RIGHT YET !!! */
7622       if (g->double_movement && frame_em == 0)
7623         GfxFrame[x][y] = 0;
7624 #endif
7625
7626       if (move_dir == MV_LEFT)
7627         GfxFrame[x - 1][y] = GfxFrame[x][y];
7628       else if (move_dir == MV_RIGHT)
7629         GfxFrame[x + 1][y] = GfxFrame[x][y];
7630       else if (move_dir == MV_UP)
7631         GfxFrame[x][y - 1] = GfxFrame[x][y];
7632       else if (move_dir == MV_DOWN)
7633         GfxFrame[x][y + 1] = GfxFrame[x][y];
7634     }
7635   }
7636   else
7637   {
7638     GfxFrame[x][y]++;
7639
7640     /* special case: animation for Xsand_stonesand_quickout_1/2 twice as fast */
7641     if (tile == Xsand_stonesand_quickout_1 ||
7642         tile == Xsand_stonesand_quickout_2)
7643       GfxFrame[x][y]++;
7644   }
7645
7646   if (graphic_info[graphic].anim_global_sync)
7647     sync_frame = FrameCounter;
7648   else if (IN_FIELD(x, y, MAX_LEV_FIELDX, MAX_LEV_FIELDY))
7649     sync_frame = GfxFrame[x][y];
7650   else
7651     sync_frame = 0;     /* playfield border (pseudo steel) */
7652
7653   SetRandomAnimationValue(x, y);
7654
7655   int frame = getAnimationFrame(g->anim_frames,
7656                                 g->anim_delay,
7657                                 g->anim_mode,
7658                                 g->anim_start_frame,
7659                                 sync_frame);
7660
7661   g_em->unique_identifier =
7662     (graphic << 16) | ((frame % 8) << 12) | (g_em->width << 6) | g_em->height;
7663 }
7664
7665 void getGraphicSourceObjectExt_EM(struct GraphicInfo_EM *g_em,
7666                                   int tile, int frame_em, int x, int y)
7667 {
7668   int action = object_mapping[tile].action;
7669   int direction = object_mapping[tile].direction;
7670   boolean is_backside = object_mapping[tile].is_backside;
7671   int effective_element = get_effective_element_EM(tile, frame_em);
7672   int effective_action = action;
7673   int graphic = (direction == MV_NONE ?
7674                  el_act2img(effective_element, effective_action) :
7675                  el_act_dir2img(effective_element, effective_action,
7676                                 direction));
7677   int crumbled = (direction == MV_NONE ?
7678                   el_act2crm(effective_element, effective_action) :
7679                   el_act_dir2crm(effective_element, effective_action,
7680                                  direction));
7681   int base_graphic = el_act2img(effective_element, ACTION_DEFAULT);
7682   int base_crumbled = el_act2crm(effective_element, ACTION_DEFAULT);
7683   boolean has_crumbled_graphics = (base_crumbled != base_graphic);
7684   struct GraphicInfo *g = &graphic_info[graphic];
7685   int sync_frame;
7686
7687   /* special case: graphic uses "2nd movement tile" and has defined
7688      7 frames for movement animation (or less) => use default graphic
7689      for last (8th) frame which ends the movement animation */
7690   if (g->double_movement && g->anim_frames < 8 && frame_em == 7)
7691   {
7692     effective_action = ACTION_DEFAULT;
7693     graphic = (direction == MV_NONE ?
7694                el_act2img(effective_element, effective_action) :
7695                el_act_dir2img(effective_element, effective_action,
7696                               direction));
7697     crumbled = (direction == MV_NONE ?
7698                 el_act2crm(effective_element, effective_action) :
7699                 el_act_dir2crm(effective_element, effective_action,
7700                                direction));
7701
7702     g = &graphic_info[graphic];
7703   }
7704
7705   if (graphic_info[graphic].anim_global_sync)
7706     sync_frame = FrameCounter;
7707   else if (IN_FIELD(x, y, MAX_LEV_FIELDX, MAX_LEV_FIELDY))
7708     sync_frame = GfxFrame[x][y];
7709   else
7710     sync_frame = 0;     /* playfield border (pseudo steel) */
7711
7712   SetRandomAnimationValue(x, y);
7713
7714   int frame = getAnimationFrame(g->anim_frames,
7715                                 g->anim_delay,
7716                                 g->anim_mode,
7717                                 g->anim_start_frame,
7718                                 sync_frame);
7719
7720   getGraphicSourceExt(graphic, frame, &g_em->bitmap, &g_em->src_x, &g_em->src_y,
7721                       g->double_movement && is_backside);
7722
7723   /* (updating the "crumbled" graphic definitions is probably not really needed,
7724      as animations for crumbled graphics can't be longer than one EMC cycle) */
7725   set_crumbled_graphics_EM(g_em, has_crumbled_graphics, crumbled,
7726                            sync_frame);
7727 }
7728
7729 void getGraphicSourcePlayerExt_EM(struct GraphicInfo_EM *g_em,
7730                                   int player_nr, int anim, int frame_em)
7731 {
7732   int element   = player_mapping[player_nr][anim].element_rnd;
7733   int action    = player_mapping[player_nr][anim].action;
7734   int direction = player_mapping[player_nr][anim].direction;
7735   int graphic = (direction == MV_NONE ?
7736                  el_act2img(element, action) :
7737                  el_act_dir2img(element, action, direction));
7738   struct GraphicInfo *g = &graphic_info[graphic];
7739   int sync_frame;
7740
7741   InitPlayerGfxAnimation(&stored_player[player_nr], action, direction);
7742
7743   stored_player[player_nr].StepFrame = frame_em;
7744
7745   sync_frame = stored_player[player_nr].Frame;
7746
7747   int frame = getAnimationFrame(g->anim_frames,
7748                                 g->anim_delay,
7749                                 g->anim_mode,
7750                                 g->anim_start_frame,
7751                                 sync_frame);
7752
7753   getGraphicSourceExt(graphic, frame, &g_em->bitmap,
7754                       &g_em->src_x, &g_em->src_y, FALSE);
7755 }
7756
7757 void InitGraphicInfo_EM(void)
7758 {
7759   int i, j, p;
7760
7761 #if DEBUG_EM_GFX
7762   int num_em_gfx_errors = 0;
7763
7764   if (graphic_info_em_object[0][0].bitmap == NULL)
7765   {
7766     /* EM graphics not yet initialized in em_open_all() */
7767
7768     return;
7769   }
7770
7771   printf("::: [4 errors can be ignored (1 x 'bomb', 3 x 'em_dynamite']\n");
7772 #endif
7773
7774   /* always start with reliable default values */
7775   for (i = 0; i < TILE_MAX; i++)
7776   {
7777     object_mapping[i].element_rnd = EL_UNKNOWN;
7778     object_mapping[i].is_backside = FALSE;
7779     object_mapping[i].action = ACTION_DEFAULT;
7780     object_mapping[i].direction = MV_NONE;
7781   }
7782
7783   /* always start with reliable default values */
7784   for (p = 0; p < MAX_PLAYERS; p++)
7785   {
7786     for (i = 0; i < SPR_MAX; i++)
7787     {
7788       player_mapping[p][i].element_rnd = EL_UNKNOWN;
7789       player_mapping[p][i].action = ACTION_DEFAULT;
7790       player_mapping[p][i].direction = MV_NONE;
7791     }
7792   }
7793
7794   for (i = 0; em_object_mapping_list[i].element_em != -1; i++)
7795   {
7796     int e = em_object_mapping_list[i].element_em;
7797
7798     object_mapping[e].element_rnd = em_object_mapping_list[i].element_rnd;
7799     object_mapping[e].is_backside = em_object_mapping_list[i].is_backside;
7800
7801     if (em_object_mapping_list[i].action != -1)
7802       object_mapping[e].action = em_object_mapping_list[i].action;
7803
7804     if (em_object_mapping_list[i].direction != -1)
7805       object_mapping[e].direction =
7806         MV_DIR_FROM_BIT(em_object_mapping_list[i].direction);
7807   }
7808
7809   for (i = 0; em_player_mapping_list[i].action_em != -1; i++)
7810   {
7811     int a = em_player_mapping_list[i].action_em;
7812     int p = em_player_mapping_list[i].player_nr;
7813
7814     player_mapping[p][a].element_rnd = em_player_mapping_list[i].element_rnd;
7815
7816     if (em_player_mapping_list[i].action != -1)
7817       player_mapping[p][a].action = em_player_mapping_list[i].action;
7818
7819     if (em_player_mapping_list[i].direction != -1)
7820       player_mapping[p][a].direction =
7821         MV_DIR_FROM_BIT(em_player_mapping_list[i].direction);
7822   }
7823
7824   for (i = 0; i < TILE_MAX; i++)
7825   {
7826     int element = object_mapping[i].element_rnd;
7827     int action = object_mapping[i].action;
7828     int direction = object_mapping[i].direction;
7829     boolean is_backside = object_mapping[i].is_backside;
7830     boolean action_exploding = ((action == ACTION_EXPLODING ||
7831                                  action == ACTION_SMASHED_BY_ROCK ||
7832                                  action == ACTION_SMASHED_BY_SPRING) &&
7833                                 element != EL_DIAMOND);
7834     boolean action_active = (action == ACTION_ACTIVE);
7835     boolean action_other = (action == ACTION_OTHER);
7836
7837     for (j = 0; j < 8; j++)
7838     {
7839       int effective_element = get_effective_element_EM(i, j);
7840       int effective_action = (j < 7 ? action :
7841                               i == Xdrip_stretch ? action :
7842                               i == Xdrip_stretchB ? action :
7843                               i == Ydrip_s1 ? action :
7844                               i == Ydrip_s1B ? action :
7845                               i == Xball_1B ? action :
7846                               i == Xball_2 ? action :
7847                               i == Xball_2B ? action :
7848                               i == Yball_eat ? action :
7849                               i == Ykey_1_eat ? action :
7850                               i == Ykey_2_eat ? action :
7851                               i == Ykey_3_eat ? action :
7852                               i == Ykey_4_eat ? action :
7853                               i == Ykey_5_eat ? action :
7854                               i == Ykey_6_eat ? action :
7855                               i == Ykey_7_eat ? action :
7856                               i == Ykey_8_eat ? action :
7857                               i == Ylenses_eat ? action :
7858                               i == Ymagnify_eat ? action :
7859                               i == Ygrass_eat ? action :
7860                               i == Ydirt_eat ? action :
7861                               i == Xsand_stonein_1 ? action :
7862                               i == Xsand_stonein_2 ? action :
7863                               i == Xsand_stonein_3 ? action :
7864                               i == Xsand_stonein_4 ? action :
7865                               i == Xsand_stoneout_1 ? action :
7866                               i == Xsand_stoneout_2 ? action :
7867                               i == Xboom_android ? ACTION_EXPLODING :
7868                               action_exploding ? ACTION_EXPLODING :
7869                               action_active ? action :
7870                               action_other ? action :
7871                               ACTION_DEFAULT);
7872       int graphic = (el_act_dir2img(effective_element, effective_action,
7873                                     direction));
7874       int crumbled = (el_act_dir2crm(effective_element, effective_action,
7875                                      direction));
7876       int base_graphic = el_act2img(effective_element, ACTION_DEFAULT);
7877       int base_crumbled = el_act2crm(effective_element, ACTION_DEFAULT);
7878       boolean has_action_graphics = (graphic != base_graphic);
7879       boolean has_crumbled_graphics = (base_crumbled != base_graphic);
7880       struct GraphicInfo *g = &graphic_info[graphic];
7881       struct GraphicInfo_EM *g_em = &graphic_info_em_object[i][7 - j];
7882       Bitmap *src_bitmap;
7883       int src_x, src_y;
7884       /* ensure to get symmetric 3-frame, 2-delay animations as used in EM */
7885       boolean special_animation = (action != ACTION_DEFAULT &&
7886                                    g->anim_frames == 3 &&
7887                                    g->anim_delay == 2 &&
7888                                    g->anim_mode & ANIM_LINEAR);
7889       int sync_frame = (i == Xdrip_stretch ? 7 :
7890                         i == Xdrip_stretchB ? 7 :
7891                         i == Ydrip_s2 ? j + 8 :
7892                         i == Ydrip_s2B ? j + 8 :
7893                         i == Xacid_1 ? 0 :
7894                         i == Xacid_2 ? 10 :
7895                         i == Xacid_3 ? 20 :
7896                         i == Xacid_4 ? 30 :
7897                         i == Xacid_5 ? 40 :
7898                         i == Xacid_6 ? 50 :
7899                         i == Xacid_7 ? 60 :
7900                         i == Xacid_8 ? 70 :
7901                         i == Xfake_acid_1 ? 0 :
7902                         i == Xfake_acid_2 ? 10 :
7903                         i == Xfake_acid_3 ? 20 :
7904                         i == Xfake_acid_4 ? 30 :
7905                         i == Xfake_acid_5 ? 40 :
7906                         i == Xfake_acid_6 ? 50 :
7907                         i == Xfake_acid_7 ? 60 :
7908                         i == Xfake_acid_8 ? 70 :
7909                         i == Xball_2 ? 7 :
7910                         i == Xball_2B ? j + 8 :
7911                         i == Yball_eat ? j + 1 :
7912                         i == Ykey_1_eat ? j + 1 :
7913                         i == Ykey_2_eat ? j + 1 :
7914                         i == Ykey_3_eat ? j + 1 :
7915                         i == Ykey_4_eat ? j + 1 :
7916                         i == Ykey_5_eat ? j + 1 :
7917                         i == Ykey_6_eat ? j + 1 :
7918                         i == Ykey_7_eat ? j + 1 :
7919                         i == Ykey_8_eat ? j + 1 :
7920                         i == Ylenses_eat ? j + 1 :
7921                         i == Ymagnify_eat ? j + 1 :
7922                         i == Ygrass_eat ? j + 1 :
7923                         i == Ydirt_eat ? j + 1 :
7924                         i == Xamoeba_1 ? 0 :
7925                         i == Xamoeba_2 ? 1 :
7926                         i == Xamoeba_3 ? 2 :
7927                         i == Xamoeba_4 ? 3 :
7928                         i == Xamoeba_5 ? 0 :
7929                         i == Xamoeba_6 ? 1 :
7930                         i == Xamoeba_7 ? 2 :
7931                         i == Xamoeba_8 ? 3 :
7932                         i == Xexit_2 ? j + 8 :
7933                         i == Xexit_3 ? j + 16 :
7934                         i == Xdynamite_1 ? 0 :
7935                         i == Xdynamite_2 ? 8 :
7936                         i == Xdynamite_3 ? 16 :
7937                         i == Xdynamite_4 ? 24 :
7938                         i == Xsand_stonein_1 ? j + 1 :
7939                         i == Xsand_stonein_2 ? j + 9 :
7940                         i == Xsand_stonein_3 ? j + 17 :
7941                         i == Xsand_stonein_4 ? j + 25 :
7942                         i == Xsand_stoneout_1 && j == 0 ? 0 :
7943                         i == Xsand_stoneout_1 && j == 1 ? 0 :
7944                         i == Xsand_stoneout_1 && j == 2 ? 1 :
7945                         i == Xsand_stoneout_1 && j == 3 ? 2 :
7946                         i == Xsand_stoneout_1 && j == 4 ? 2 :
7947                         i == Xsand_stoneout_1 && j == 5 ? 3 :
7948                         i == Xsand_stoneout_1 && j == 6 ? 4 :
7949                         i == Xsand_stoneout_1 && j == 7 ? 4 :
7950                         i == Xsand_stoneout_2 && j == 0 ? 5 :
7951                         i == Xsand_stoneout_2 && j == 1 ? 6 :
7952                         i == Xsand_stoneout_2 && j == 2 ? 7 :
7953                         i == Xsand_stoneout_2 && j == 3 ? 8 :
7954                         i == Xsand_stoneout_2 && j == 4 ? 9 :
7955                         i == Xsand_stoneout_2 && j == 5 ? 11 :
7956                         i == Xsand_stoneout_2 && j == 6 ? 13 :
7957                         i == Xsand_stoneout_2 && j == 7 ? 15 :
7958                         i == Xboom_bug && j == 1 ? 2 :
7959                         i == Xboom_bug && j == 2 ? 2 :
7960                         i == Xboom_bug && j == 3 ? 4 :
7961                         i == Xboom_bug && j == 4 ? 4 :
7962                         i == Xboom_bug && j == 5 ? 2 :
7963                         i == Xboom_bug && j == 6 ? 2 :
7964                         i == Xboom_bug && j == 7 ? 0 :
7965                         i == Xboom_bomb && j == 1 ? 2 :
7966                         i == Xboom_bomb && j == 2 ? 2 :
7967                         i == Xboom_bomb && j == 3 ? 4 :
7968                         i == Xboom_bomb && j == 4 ? 4 :
7969                         i == Xboom_bomb && j == 5 ? 2 :
7970                         i == Xboom_bomb && j == 6 ? 2 :
7971                         i == Xboom_bomb && j == 7 ? 0 :
7972                         i == Xboom_android && j == 7 ? 6 :
7973                         i == Xboom_1 && j == 1 ? 2 :
7974                         i == Xboom_1 && j == 2 ? 2 :
7975                         i == Xboom_1 && j == 3 ? 4 :
7976                         i == Xboom_1 && j == 4 ? 4 :
7977                         i == Xboom_1 && j == 5 ? 6 :
7978                         i == Xboom_1 && j == 6 ? 6 :
7979                         i == Xboom_1 && j == 7 ? 8 :
7980                         i == Xboom_2 && j == 0 ? 8 :
7981                         i == Xboom_2 && j == 1 ? 8 :
7982                         i == Xboom_2 && j == 2 ? 10 :
7983                         i == Xboom_2 && j == 3 ? 10 :
7984                         i == Xboom_2 && j == 4 ? 10 :
7985                         i == Xboom_2 && j == 5 ? 12 :
7986                         i == Xboom_2 && j == 6 ? 12 :
7987                         i == Xboom_2 && j == 7 ? 12 :
7988                         special_animation && j == 4 ? 3 :
7989                         effective_action != action ? 0 :
7990                         j);
7991
7992 #if DEBUG_EM_GFX
7993       Bitmap *debug_bitmap = g_em->bitmap;
7994       int debug_src_x = g_em->src_x;
7995       int debug_src_y = g_em->src_y;
7996 #endif
7997
7998       int frame = getAnimationFrame(g->anim_frames,
7999                                     g->anim_delay,
8000                                     g->anim_mode,
8001                                     g->anim_start_frame,
8002                                     sync_frame);
8003
8004       getGraphicSourceExt(graphic, frame, &src_bitmap, &src_x, &src_y,
8005                           g->double_movement && is_backside);
8006
8007       g_em->bitmap = src_bitmap;
8008       g_em->src_x = src_x;
8009       g_em->src_y = src_y;
8010       g_em->src_offset_x = 0;
8011       g_em->src_offset_y = 0;
8012       g_em->dst_offset_x = 0;
8013       g_em->dst_offset_y = 0;
8014       g_em->width  = TILEX;
8015       g_em->height = TILEY;
8016
8017       g_em->preserve_background = FALSE;
8018
8019       set_crumbled_graphics_EM(g_em, has_crumbled_graphics, crumbled,
8020                                sync_frame);
8021
8022       if ((!g->double_movement && (effective_action == ACTION_FALLING ||
8023                                    effective_action == ACTION_MOVING  ||
8024                                    effective_action == ACTION_PUSHING ||
8025                                    effective_action == ACTION_EATING)) ||
8026           (!has_action_graphics && (effective_action == ACTION_FILLING ||
8027                                     effective_action == ACTION_EMPTYING)))
8028       {
8029         int move_dir =
8030           (effective_action == ACTION_FALLING ||
8031            effective_action == ACTION_FILLING ||
8032            effective_action == ACTION_EMPTYING ? MV_DOWN : direction);
8033         int dx = (move_dir == MV_LEFT ? -1 : move_dir == MV_RIGHT ? 1 : 0);
8034         int dy = (move_dir == MV_UP   ? -1 : move_dir == MV_DOWN  ? 1 : 0);
8035         int num_steps = (i == Ydrip_s1  ? 16 :
8036                          i == Ydrip_s1B ? 16 :
8037                          i == Ydrip_s2  ? 16 :
8038                          i == Ydrip_s2B ? 16 :
8039                          i == Xsand_stonein_1 ? 32 :
8040                          i == Xsand_stonein_2 ? 32 :
8041                          i == Xsand_stonein_3 ? 32 :
8042                          i == Xsand_stonein_4 ? 32 :
8043                          i == Xsand_stoneout_1 ? 16 :
8044                          i == Xsand_stoneout_2 ? 16 : 8);
8045         int cx = ABS(dx) * (TILEX / num_steps);
8046         int cy = ABS(dy) * (TILEY / num_steps);
8047         int step_frame = (i == Ydrip_s2         ? j + 8 :
8048                           i == Ydrip_s2B        ? j + 8 :
8049                           i == Xsand_stonein_2  ? j + 8 :
8050                           i == Xsand_stonein_3  ? j + 16 :
8051                           i == Xsand_stonein_4  ? j + 24 :
8052                           i == Xsand_stoneout_2 ? j + 8 : j) + 1;
8053         int step = (is_backside ? step_frame : num_steps - step_frame);
8054
8055         if (is_backside)        /* tile where movement starts */
8056         {
8057           if (dx < 0 || dy < 0)
8058           {
8059             g_em->src_offset_x = cx * step;
8060             g_em->src_offset_y = cy * step;
8061           }
8062           else
8063           {
8064             g_em->dst_offset_x = cx * step;
8065             g_em->dst_offset_y = cy * step;
8066           }
8067         }
8068         else                    /* tile where movement ends */
8069         {
8070           if (dx < 0 || dy < 0)
8071           {
8072             g_em->dst_offset_x = cx * step;
8073             g_em->dst_offset_y = cy * step;
8074           }
8075           else
8076           {
8077             g_em->src_offset_x = cx * step;
8078             g_em->src_offset_y = cy * step;
8079           }
8080         }
8081
8082         g_em->width  = TILEX - cx * step;
8083         g_em->height = TILEY - cy * step;
8084       }
8085
8086       /* create unique graphic identifier to decide if tile must be redrawn */
8087       /* bit 31 - 16 (16 bit): EM style graphic
8088          bit 15 - 12 ( 4 bit): EM style frame
8089          bit 11 -  6 ( 6 bit): graphic width
8090          bit  5 -  0 ( 6 bit): graphic height */
8091       g_em->unique_identifier =
8092         (graphic << 16) | (frame << 12) | (g_em->width << 6) | g_em->height;
8093
8094 #if DEBUG_EM_GFX
8095
8096       /* skip check for EMC elements not contained in original EMC artwork */
8097       if (element == EL_EMC_FAKE_ACID)
8098         continue;
8099
8100       if (g_em->bitmap != debug_bitmap ||
8101           g_em->src_x != debug_src_x ||
8102           g_em->src_y != debug_src_y ||
8103           g_em->src_offset_x != 0 ||
8104           g_em->src_offset_y != 0 ||
8105           g_em->dst_offset_x != 0 ||
8106           g_em->dst_offset_y != 0 ||
8107           g_em->width != TILEX ||
8108           g_em->height != TILEY)
8109       {
8110         static int last_i = -1;
8111
8112         if (i != last_i)
8113         {
8114           printf("\n");
8115           last_i = i;
8116         }
8117
8118         printf("::: EMC GFX ERROR for element %d -> %d ('%s', '%s', %d)",
8119                i, element, element_info[element].token_name,
8120                element_action_info[effective_action].suffix, direction);
8121
8122         if (element != effective_element)
8123           printf(" [%d ('%s')]",
8124                  effective_element,
8125                  element_info[effective_element].token_name);
8126
8127         printf("\n");
8128
8129         if (g_em->bitmap != debug_bitmap)
8130           printf("    %d (%d): different bitmap! (0x%08x != 0x%08x)\n",
8131                  j, is_backside, (int)(g_em->bitmap), (int)(debug_bitmap));
8132
8133         if (g_em->src_x != debug_src_x ||
8134             g_em->src_y != debug_src_y)
8135           printf("    frame %d (%c): %d,%d (%d,%d) should be %d,%d (%d,%d)\n",
8136                  j, (is_backside ? 'B' : 'F'),
8137                  g_em->src_x, g_em->src_y,
8138                  g_em->src_x / 32, g_em->src_y / 32,
8139                  debug_src_x, debug_src_y,
8140                  debug_src_x / 32, debug_src_y / 32);
8141
8142         if (g_em->src_offset_x != 0 ||
8143             g_em->src_offset_y != 0 ||
8144             g_em->dst_offset_x != 0 ||
8145             g_em->dst_offset_y != 0)
8146           printf("    %d (%d): offsets %d,%d and %d,%d should be all 0\n",
8147                  j, is_backside,
8148                  g_em->src_offset_x, g_em->src_offset_y,
8149                  g_em->dst_offset_x, g_em->dst_offset_y);
8150
8151         if (g_em->width != TILEX ||
8152             g_em->height != TILEY)
8153           printf("    %d (%d): size %d,%d should be %d,%d\n",
8154                  j, is_backside,
8155                  g_em->width, g_em->height, TILEX, TILEY);
8156
8157         num_em_gfx_errors++;
8158       }
8159 #endif
8160
8161     }
8162   }
8163
8164   for (i = 0; i < TILE_MAX; i++)
8165   {
8166     for (j = 0; j < 8; j++)
8167     {
8168       int element = object_mapping[i].element_rnd;
8169       int action = object_mapping[i].action;
8170       int direction = object_mapping[i].direction;
8171       boolean is_backside = object_mapping[i].is_backside;
8172       int graphic_action  = el_act_dir2img(element, action, direction);
8173       int graphic_default = el_act_dir2img(element, ACTION_DEFAULT, direction);
8174
8175       if ((action == ACTION_SMASHED_BY_ROCK ||
8176            action == ACTION_SMASHED_BY_SPRING ||
8177            action == ACTION_EATING) &&
8178           graphic_action == graphic_default)
8179       {
8180         int e = (action == ACTION_SMASHED_BY_ROCK   ? Ystone_s  :
8181                  action == ACTION_SMASHED_BY_SPRING ? Yspring_s :
8182                  direction == MV_LEFT  ? (is_backside? Yspring_wB: Yspring_w) :
8183                  direction == MV_RIGHT ? (is_backside? Yspring_eB: Yspring_e) :
8184                  Xspring);
8185
8186         /* no separate animation for "smashed by rock" -- use rock instead */
8187         struct GraphicInfo_EM *g_em = &graphic_info_em_object[i][7 - j];
8188         struct GraphicInfo_EM *g_xx = &graphic_info_em_object[e][7 - j];
8189
8190         g_em->bitmap            = g_xx->bitmap;
8191         g_em->src_x             = g_xx->src_x;
8192         g_em->src_y             = g_xx->src_y;
8193         g_em->src_offset_x      = g_xx->src_offset_x;
8194         g_em->src_offset_y      = g_xx->src_offset_y;
8195         g_em->dst_offset_x      = g_xx->dst_offset_x;
8196         g_em->dst_offset_y      = g_xx->dst_offset_y;
8197         g_em->width             = g_xx->width;
8198         g_em->height            = g_xx->height;
8199         g_em->unique_identifier = g_xx->unique_identifier;
8200
8201         if (!is_backside)
8202           g_em->preserve_background = TRUE;
8203       }
8204     }
8205   }
8206
8207   for (p = 0; p < MAX_PLAYERS; p++)
8208   {
8209     for (i = 0; i < SPR_MAX; i++)
8210     {
8211       int element = player_mapping[p][i].element_rnd;
8212       int action = player_mapping[p][i].action;
8213       int direction = player_mapping[p][i].direction;
8214
8215       for (j = 0; j < 8; j++)
8216       {
8217         int effective_element = element;
8218         int effective_action = action;
8219         int graphic = (direction == MV_NONE ?
8220                        el_act2img(effective_element, effective_action) :
8221                        el_act_dir2img(effective_element, effective_action,
8222                                       direction));
8223         struct GraphicInfo *g = &graphic_info[graphic];
8224         struct GraphicInfo_EM *g_em = &graphic_info_em_player[p][i][7 - j];
8225         Bitmap *src_bitmap;
8226         int src_x, src_y;
8227         int sync_frame = j;
8228
8229 #if DEBUG_EM_GFX
8230         Bitmap *debug_bitmap = g_em->bitmap;
8231         int debug_src_x = g_em->src_x;
8232         int debug_src_y = g_em->src_y;
8233 #endif
8234
8235         int frame = getAnimationFrame(g->anim_frames,
8236                                       g->anim_delay,
8237                                       g->anim_mode,
8238                                       g->anim_start_frame,
8239                                       sync_frame);
8240
8241         getGraphicSourceExt(graphic, frame, &src_bitmap, &src_x, &src_y, FALSE);
8242
8243         g_em->bitmap = src_bitmap;
8244         g_em->src_x = src_x;
8245         g_em->src_y = src_y;
8246         g_em->src_offset_x = 0;
8247         g_em->src_offset_y = 0;
8248         g_em->dst_offset_x = 0;
8249         g_em->dst_offset_y = 0;
8250         g_em->width  = TILEX;
8251         g_em->height = TILEY;
8252
8253 #if DEBUG_EM_GFX
8254
8255         /* skip check for EMC elements not contained in original EMC artwork */
8256         if (element == EL_PLAYER_3 ||
8257             element == EL_PLAYER_4)
8258           continue;
8259
8260         if (g_em->bitmap != debug_bitmap ||
8261             g_em->src_x != debug_src_x ||
8262             g_em->src_y != debug_src_y)
8263         {
8264           static int last_i = -1;
8265
8266           if (i != last_i)
8267           {
8268             printf("\n");
8269             last_i = i;
8270           }
8271
8272           printf("::: EMC GFX ERROR for p/a %d/%d -> %d ('%s', '%s', %d)",
8273                  p, i, element, element_info[element].token_name,
8274                  element_action_info[effective_action].suffix, direction);
8275
8276           if (element != effective_element)
8277             printf(" [%d ('%s')]",
8278                    effective_element,
8279                    element_info[effective_element].token_name);
8280
8281           printf("\n");
8282
8283           if (g_em->bitmap != debug_bitmap)
8284             printf("    %d: different bitmap! (0x%08x != 0x%08x)\n",
8285                    j, (int)(g_em->bitmap), (int)(debug_bitmap));
8286
8287           if (g_em->src_x != debug_src_x ||
8288               g_em->src_y != debug_src_y)
8289             printf("    frame %d: %d,%d (%d,%d) should be %d,%d (%d,%d)\n",
8290                    j,
8291                    g_em->src_x, g_em->src_y,
8292                    g_em->src_x / 32, g_em->src_y / 32,
8293                    debug_src_x, debug_src_y,
8294                    debug_src_x / 32, debug_src_y / 32);
8295
8296           num_em_gfx_errors++;
8297         }
8298 #endif
8299
8300       }
8301     }
8302   }
8303
8304 #if DEBUG_EM_GFX
8305   printf("\n");
8306   printf("::: [%d errors found]\n", num_em_gfx_errors);
8307
8308   exit(0);
8309 #endif
8310 }
8311
8312 void CheckSaveEngineSnapshot_EM(byte action[MAX_PLAYERS], int frame,
8313                                 boolean any_player_moving,
8314                                 boolean any_player_snapping,
8315                                 boolean any_player_dropping)
8316 {
8317   if (frame == 0 && !any_player_dropping)
8318   {
8319     if (!local_player->was_waiting)
8320     {
8321       if (!CheckSaveEngineSnapshotToList())
8322         return;
8323
8324       local_player->was_waiting = TRUE;
8325     }
8326   }
8327   else if (any_player_moving || any_player_snapping || any_player_dropping)
8328   {
8329     local_player->was_waiting = FALSE;
8330   }
8331 }
8332
8333 void CheckSaveEngineSnapshot_SP(boolean murphy_is_waiting,
8334                                 boolean murphy_is_dropping)
8335 {
8336   if (murphy_is_waiting)
8337   {
8338     if (!local_player->was_waiting)
8339     {
8340       if (!CheckSaveEngineSnapshotToList())
8341         return;
8342
8343       local_player->was_waiting = TRUE;
8344     }
8345   }
8346   else
8347   {
8348     local_player->was_waiting = FALSE;
8349   }
8350 }
8351
8352 void CheckSingleStepMode_EM(byte action[MAX_PLAYERS], int frame,
8353                             boolean any_player_moving,
8354                             boolean any_player_snapping,
8355                             boolean any_player_dropping)
8356 {
8357   if (tape.single_step && tape.recording && !tape.pausing)
8358     if (frame == 0 && !any_player_dropping)
8359       TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
8360
8361   CheckSaveEngineSnapshot_EM(action, frame, any_player_moving,
8362                              any_player_snapping, any_player_dropping);
8363 }
8364
8365 void CheckSingleStepMode_SP(boolean murphy_is_waiting,
8366                             boolean murphy_is_dropping)
8367 {
8368   if (tape.single_step && tape.recording && !tape.pausing)
8369     if (murphy_is_waiting)
8370       TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
8371
8372   CheckSaveEngineSnapshot_SP(murphy_is_waiting, murphy_is_dropping);
8373 }
8374
8375 void getGraphicSource_SP(struct GraphicInfo_SP *g_sp,
8376                          int graphic, int sync_frame, int x, int y)
8377 {
8378   int frame = getGraphicAnimationFrame(graphic, sync_frame);
8379
8380   getGraphicSource(graphic, frame, &g_sp->bitmap, &g_sp->src_x, &g_sp->src_y);
8381 }
8382
8383 boolean isNextAnimationFrame_SP(int graphic, int sync_frame)
8384 {
8385   return (IS_NEXT_FRAME(sync_frame, graphic));
8386 }
8387
8388 int getGraphicInfo_Delay(int graphic)
8389 {
8390   return graphic_info[graphic].anim_delay;
8391 }
8392
8393 void PlayMenuSoundExt(int sound)
8394 {
8395   if (sound == SND_UNDEFINED)
8396     return;
8397
8398   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
8399       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
8400     return;
8401
8402   if (IS_LOOP_SOUND(sound))
8403     PlaySoundLoop(sound);
8404   else
8405     PlaySound(sound);
8406 }
8407
8408 void PlayMenuSound()
8409 {
8410   PlayMenuSoundExt(menu.sound[game_status]);
8411 }
8412
8413 void PlayMenuSoundStereo(int sound, int stereo_position)
8414 {
8415   if (sound == SND_UNDEFINED)
8416     return;
8417
8418   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
8419       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
8420     return;
8421
8422   if (IS_LOOP_SOUND(sound))
8423     PlaySoundExt(sound, SOUND_MAX_VOLUME, stereo_position, SND_CTRL_PLAY_LOOP);
8424   else
8425     PlaySoundStereo(sound, stereo_position);
8426 }
8427
8428 void PlayMenuSoundIfLoopExt(int sound)
8429 {
8430   if (sound == SND_UNDEFINED)
8431     return;
8432
8433   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
8434       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
8435     return;
8436
8437   if (IS_LOOP_SOUND(sound))
8438     PlaySoundLoop(sound);
8439 }
8440
8441 void PlayMenuSoundIfLoop()
8442 {
8443   PlayMenuSoundIfLoopExt(menu.sound[game_status]);
8444 }
8445
8446 void PlayMenuMusicExt(int music)
8447 {
8448   if (music == MUS_UNDEFINED)
8449     return;
8450
8451   if (!setup.sound_music)
8452     return;
8453
8454   PlayMusic(music);
8455 }
8456
8457 void PlayMenuMusic()
8458 {
8459   char *curr_music = getCurrentlyPlayingMusicFilename();
8460   char *next_music = getMusicListEntry(menu.music[game_status])->filename;
8461
8462   if (!strEqual(curr_music, next_music))
8463     PlayMenuMusicExt(menu.music[game_status]);
8464 }
8465
8466 void PlayMenuSoundsAndMusic()
8467 {
8468   PlayMenuSound();
8469   PlayMenuMusic();
8470 }
8471
8472 static void FadeMenuSounds()
8473 {
8474   FadeSounds();
8475 }
8476
8477 static void FadeMenuMusic()
8478 {
8479   char *curr_music = getCurrentlyPlayingMusicFilename();
8480   char *next_music = getMusicListEntry(menu.music[game_status])->filename;
8481
8482   if (!strEqual(curr_music, next_music))
8483     FadeMusic();
8484 }
8485
8486 void FadeMenuSoundsAndMusic()
8487 {
8488   FadeMenuSounds();
8489   FadeMenuMusic();
8490 }
8491
8492 void PlaySoundActivating()
8493 {
8494 #if 0
8495   PlaySound(SND_MENU_ITEM_ACTIVATING);
8496 #endif
8497 }
8498
8499 void PlaySoundSelecting()
8500 {
8501 #if 0
8502   PlaySound(SND_MENU_ITEM_SELECTING);
8503 #endif
8504 }
8505
8506 void ToggleFullscreenOrChangeWindowScalingIfNeeded()
8507 {
8508   boolean change_fullscreen = (setup.fullscreen !=
8509                                video.fullscreen_enabled);
8510   boolean change_window_scaling_percent = (!video.fullscreen_enabled &&
8511                                            setup.window_scaling_percent !=
8512                                            video.window_scaling_percent);
8513
8514   if (change_window_scaling_percent && video.fullscreen_enabled)
8515     return;
8516
8517   if (!change_window_scaling_percent && !video.fullscreen_available)
8518     return;
8519
8520 #if defined(TARGET_SDL2)
8521   if (change_window_scaling_percent)
8522   {
8523     SDLSetWindowScaling(setup.window_scaling_percent);
8524
8525     return;
8526   }
8527   else if (change_fullscreen)
8528   {
8529     SDLSetWindowFullscreen(setup.fullscreen);
8530
8531     /* set setup value according to successfully changed fullscreen mode */
8532     setup.fullscreen = video.fullscreen_enabled;
8533
8534     return;
8535   }
8536 #endif
8537
8538   if (change_fullscreen ||
8539       change_window_scaling_percent)
8540   {
8541     Bitmap *tmp_backbuffer = CreateBitmap(WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH);
8542
8543     /* save backbuffer content which gets lost when toggling fullscreen mode */
8544     BlitBitmap(backbuffer, tmp_backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
8545
8546     if (change_window_scaling_percent)
8547     {
8548       /* keep window mode, but change window scaling */
8549       video.fullscreen_enabled = TRUE;          /* force new window scaling */
8550     }
8551
8552     /* toggle fullscreen */
8553     ChangeVideoModeIfNeeded(setup.fullscreen);
8554
8555     /* set setup value according to successfully changed fullscreen mode */
8556     setup.fullscreen = video.fullscreen_enabled;
8557
8558     /* restore backbuffer content from temporary backbuffer backup bitmap */
8559     BlitBitmap(tmp_backbuffer, backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
8560
8561     FreeBitmap(tmp_backbuffer);
8562
8563     /* update visible window/screen */
8564     BlitBitmap(backbuffer, window, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
8565   }
8566 }
8567
8568 void JoinRectangles(int *x, int *y, int *width, int *height,
8569                     int x2, int y2, int width2, int height2)
8570 {
8571   // do not join with "off-screen" rectangle
8572   if (x2 == -1 || y2 == -1)
8573     return;
8574
8575   *x = MIN(*x, x2);
8576   *y = MIN(*y, y2);
8577   *width = MAX(*width, width2);
8578   *height = MAX(*height, height2);
8579 }
8580
8581 void SetAnimStatus(int anim_status_new)
8582 {
8583   if (anim_status_new == GAME_MODE_MAIN)
8584     anim_status_new = GAME_MODE_PSEUDO_MAINONLY;
8585   else if (anim_status_new == GAME_MODE_SCORES)
8586     anim_status_new = GAME_MODE_PSEUDO_SCORESOLD;
8587
8588   global.anim_status_next = anim_status_new;
8589
8590   // directly set screen modes that are entered without fading
8591   if ((global.anim_status      == GAME_MODE_PSEUDO_MAINONLY &&
8592        global.anim_status_next == GAME_MODE_PSEUDO_TYPENAME) ||
8593       (global.anim_status      == GAME_MODE_PSEUDO_TYPENAME &&
8594        global.anim_status_next == GAME_MODE_PSEUDO_MAINONLY))
8595     global.anim_status = global.anim_status_next;
8596 }
8597
8598 void SetGameStatus(int game_status_new)
8599 {
8600   if (game_status_new != game_status)
8601     game_status_last_screen = game_status;
8602
8603   game_status = game_status_new;
8604
8605   SetAnimStatus(game_status_new);
8606 }
8607
8608 void SetFontStatus(int game_status_new)
8609 {
8610   static int last_game_status = -1;
8611
8612   if (game_status_new != -1)
8613   {
8614     // set game status for font use after storing last game status
8615     last_game_status = game_status;
8616     game_status = game_status_new;
8617   }
8618   else
8619   {
8620     // reset game status after font use from last stored game status
8621     game_status = last_game_status;
8622   }
8623 }
8624
8625 void ResetFontStatus()
8626 {
8627   SetFontStatus(-1);
8628 }
8629
8630 void ChangeViewportPropertiesIfNeeded()
8631 {
8632   int gfx_game_mode = game_status;
8633   int gfx_game_mode2 = (game_status == GAME_MODE_EDITOR ? GAME_MODE_DEFAULT :
8634                         game_status);
8635   struct RectWithBorder *vp_window    = &viewport.window[gfx_game_mode];
8636   struct RectWithBorder *vp_playfield = &viewport.playfield[gfx_game_mode];
8637   struct RectWithBorder *vp_door_1    = &viewport.door_1[gfx_game_mode];
8638   struct RectWithBorder *vp_door_2    = &viewport.door_2[gfx_game_mode2];
8639   struct RectWithBorder *vp_door_3    = &viewport.door_2[GAME_MODE_EDITOR];
8640   int new_win_xsize     = vp_window->width;
8641   int new_win_ysize     = vp_window->height;
8642   int border_size       = vp_playfield->border_size;
8643   int new_sx            = vp_playfield->x + border_size;
8644   int new_sy            = vp_playfield->y + border_size;
8645   int new_sxsize        = vp_playfield->width  - 2 * border_size;
8646   int new_sysize        = vp_playfield->height - 2 * border_size;
8647   int new_real_sx       = vp_playfield->x;
8648   int new_real_sy       = vp_playfield->y;
8649   int new_full_sxsize   = vp_playfield->width;
8650   int new_full_sysize   = vp_playfield->height;
8651   int new_dx            = vp_door_1->x;
8652   int new_dy            = vp_door_1->y;
8653   int new_dxsize        = vp_door_1->width;
8654   int new_dysize        = vp_door_1->height;
8655   int new_vx            = vp_door_2->x;
8656   int new_vy            = vp_door_2->y;
8657   int new_vxsize        = vp_door_2->width;
8658   int new_vysize        = vp_door_2->height;
8659   int new_ex            = vp_door_3->x;
8660   int new_ey            = vp_door_3->y;
8661   int new_exsize        = vp_door_3->width;
8662   int new_eysize        = vp_door_3->height;
8663   int new_tilesize_var =
8664     (setup.small_game_graphics ? MINI_TILESIZE : game.tile_size);
8665
8666   int tilesize = (gfx_game_mode == GAME_MODE_PLAYING ? new_tilesize_var :
8667                   gfx_game_mode == GAME_MODE_EDITOR ? MINI_TILESIZE : TILESIZE);
8668   int new_scr_fieldx = new_sxsize / tilesize;
8669   int new_scr_fieldy = new_sysize / tilesize;
8670   int new_scr_fieldx_buffers = new_sxsize / new_tilesize_var;
8671   int new_scr_fieldy_buffers = new_sysize / new_tilesize_var;
8672   boolean init_gfx_buffers = FALSE;
8673   boolean init_video_buffer = FALSE;
8674   boolean init_gadgets_and_anims = FALSE;
8675   boolean init_em_graphics = FALSE;
8676
8677   if (new_win_xsize != WIN_XSIZE ||
8678       new_win_ysize != WIN_YSIZE)
8679   {
8680     WIN_XSIZE = new_win_xsize;
8681     WIN_YSIZE = new_win_ysize;
8682
8683     init_video_buffer = TRUE;
8684     init_gfx_buffers = TRUE;
8685     init_gadgets_and_anims = TRUE;
8686
8687     // printf("::: video: init_video_buffer, init_gfx_buffers\n");
8688   }
8689
8690   if (new_scr_fieldx != SCR_FIELDX ||
8691       new_scr_fieldy != SCR_FIELDY)
8692   {
8693     /* this always toggles between MAIN and GAME when using small tile size */
8694
8695     SCR_FIELDX = new_scr_fieldx;
8696     SCR_FIELDY = new_scr_fieldy;
8697
8698     // printf("::: new_scr_fieldx != SCR_FIELDX ...\n");
8699   }
8700
8701   if (new_sx != SX ||
8702       new_sy != SY ||
8703       new_dx != DX ||
8704       new_dy != DY ||
8705       new_vx != VX ||
8706       new_vy != VY ||
8707       new_ex != EX ||
8708       new_ey != EY ||
8709       new_sxsize != SXSIZE ||
8710       new_sysize != SYSIZE ||
8711       new_dxsize != DXSIZE ||
8712       new_dysize != DYSIZE ||
8713       new_vxsize != VXSIZE ||
8714       new_vysize != VYSIZE ||
8715       new_exsize != EXSIZE ||
8716       new_eysize != EYSIZE ||
8717       new_real_sx != REAL_SX ||
8718       new_real_sy != REAL_SY ||
8719       new_full_sxsize != FULL_SXSIZE ||
8720       new_full_sysize != FULL_SYSIZE ||
8721       new_tilesize_var != TILESIZE_VAR
8722       )
8723   {
8724     // ------------------------------------------------------------------------
8725     // determine next fading area for changed viewport definitions
8726     // ------------------------------------------------------------------------
8727
8728     // start with current playfield area (default fading area)
8729     FADE_SX = REAL_SX;
8730     FADE_SY = REAL_SY;
8731     FADE_SXSIZE = FULL_SXSIZE;
8732     FADE_SYSIZE = FULL_SYSIZE;
8733
8734     // add new playfield area if position or size has changed
8735     if (new_real_sx != REAL_SX || new_real_sy != REAL_SY ||
8736         new_full_sxsize != FULL_SXSIZE || new_full_sysize != FULL_SYSIZE)
8737     {
8738       JoinRectangles(&FADE_SX, &FADE_SY, &FADE_SXSIZE, &FADE_SYSIZE,
8739                      new_real_sx, new_real_sy, new_full_sxsize,new_full_sysize);
8740     }
8741
8742     // add current and new door 1 area if position or size has changed
8743     if (new_dx != DX || new_dy != DY ||
8744         new_dxsize != DXSIZE || new_dysize != DYSIZE)
8745     {
8746       JoinRectangles(&FADE_SX, &FADE_SY, &FADE_SXSIZE, &FADE_SYSIZE,
8747                      DX, DY, DXSIZE, DYSIZE);
8748       JoinRectangles(&FADE_SX, &FADE_SY, &FADE_SXSIZE, &FADE_SYSIZE,
8749                      new_dx, new_dy, new_dxsize, new_dysize);
8750     }
8751
8752     // add current and new door 2 area if position or size has changed
8753     if (new_dx != VX || new_dy != VY ||
8754         new_dxsize != VXSIZE || new_dysize != VYSIZE)
8755     {
8756       JoinRectangles(&FADE_SX, &FADE_SY, &FADE_SXSIZE, &FADE_SYSIZE,
8757                      VX, VY, VXSIZE, VYSIZE);
8758       JoinRectangles(&FADE_SX, &FADE_SY, &FADE_SXSIZE, &FADE_SYSIZE,
8759                      new_vx, new_vy, new_vxsize, new_vysize);
8760     }
8761
8762     // ------------------------------------------------------------------------
8763     // handle changed tile size
8764     // ------------------------------------------------------------------------
8765
8766     if (new_tilesize_var != TILESIZE_VAR)
8767     {
8768       // printf("::: new_tilesize_var != TILESIZE_VAR\n");
8769
8770       // changing tile size invalidates scroll values of engine snapshots
8771       FreeEngineSnapshotSingle();
8772
8773       // changing tile size requires update of graphic mapping for EM engine
8774       init_em_graphics = TRUE;
8775     }
8776
8777     SX = new_sx;
8778     SY = new_sy;
8779     DX = new_dx;
8780     DY = new_dy;
8781     VX = new_vx;
8782     VY = new_vy;
8783     EX = new_ex;
8784     EY = new_ey;
8785     SXSIZE = new_sxsize;
8786     SYSIZE = new_sysize;
8787     DXSIZE = new_dxsize;
8788     DYSIZE = new_dysize;
8789     VXSIZE = new_vxsize;
8790     VYSIZE = new_vysize;
8791     EXSIZE = new_exsize;
8792     EYSIZE = new_eysize;
8793     REAL_SX = new_real_sx;
8794     REAL_SY = new_real_sy;
8795     FULL_SXSIZE = new_full_sxsize;
8796     FULL_SYSIZE = new_full_sysize;
8797     TILESIZE_VAR = new_tilesize_var;
8798
8799     init_gfx_buffers = TRUE;
8800     init_gadgets_and_anims = TRUE;
8801
8802     // printf("::: viewports: init_gfx_buffers\n");
8803     // printf("::: viewports: init_gadgets_and_anims\n");
8804   }
8805
8806   if (init_gfx_buffers)
8807   {
8808     // printf("::: init_gfx_buffers\n");
8809
8810     SCR_FIELDX = new_scr_fieldx_buffers;
8811     SCR_FIELDY = new_scr_fieldy_buffers;
8812
8813     InitGfxBuffers();
8814
8815     SCR_FIELDX = new_scr_fieldx;
8816     SCR_FIELDY = new_scr_fieldy;
8817
8818     SetDrawDeactivationMask(REDRAW_NONE);
8819     SetDrawBackgroundMask(REDRAW_FIELD);
8820   }
8821
8822   if (init_video_buffer)
8823   {
8824     // printf("::: init_video_buffer\n");
8825
8826     InitVideoBuffer(WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH, setup.fullscreen);
8827     InitImageTextures();
8828   }
8829
8830   if (init_gadgets_and_anims)
8831   {
8832     // printf("::: init_gadgets_and_anims\n");
8833
8834     InitGadgets();
8835     InitGlobalAnimations();
8836   }
8837
8838   if (init_em_graphics)
8839   {
8840       InitGraphicInfo_EM();
8841   }
8842 }