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