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