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