rnd-20131216-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 DrawEnvelopeBackground(int envelope_nr, int startx, int starty,
2469                             int x, int y, int xsize, int ysize, int font_nr)
2470 {
2471   int font_width  = getFontWidth(font_nr);
2472   int font_height = getFontHeight(font_nr);
2473   int graphic = IMG_BACKGROUND_ENVELOPE_1 + envelope_nr;
2474   Bitmap *src_bitmap;
2475   int src_x, src_y;
2476   int dst_x = SX + startx + x * font_width;
2477   int dst_y = SY + starty + y * font_height;
2478   int width  = graphic_info[graphic].width;
2479   int height = graphic_info[graphic].height;
2480   int inner_width  = MAX(width  - 2 * font_width,  font_width);
2481   int inner_height = MAX(height - 2 * font_height, font_height);
2482   int inner_sx = (width >= 3 * font_width ? font_width : 0);
2483   int inner_sy = (height >= 3 * font_height ? font_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 < font_width || height < font_height)
2489   {
2490     ClearRectangle(drawto, dst_x, dst_y, font_width, font_height);
2491     return;
2492   }
2493
2494   src_x += (x == 0 ? 0 : x == xsize - 1 ? width  - font_width  :
2495             inner_sx + (x - 1) * font_width  % inner_width);
2496   src_y += (y == 0 ? 0 : y == ysize - 1 ? height - font_height :
2497             inner_sy + (y - 1) * font_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, font_width, font_height,
2504                      dst_x, dst_y);
2505   }
2506   else
2507     BlitBitmap(src_bitmap, drawto, src_x, src_y, font_width, font_height,
2508                dst_x, dst_y);
2509 }
2510
2511 void AnimateEnvelope(int envelope_nr, int anim_mode, int action)
2512 {
2513   int graphic = IMG_BACKGROUND_ENVELOPE_1 + envelope_nr;
2514   Bitmap *src_bitmap = graphic_info[graphic].bitmap;
2515   int mask_mode = (src_bitmap != NULL ? BLIT_MASKED : BLIT_ON_BACKGROUND);
2516   boolean ffwd_delay = (tape.playing && tape.fast_forward);
2517   boolean no_delay = (tape.warp_forward);
2518   unsigned int anim_delay = 0;
2519   int frame_delay_value = (ffwd_delay ? FfwdFrameDelay : GameFrameDelay);
2520   int anim_delay_value = (no_delay ? 0 : frame_delay_value);
2521   int font_nr = FONT_ENVELOPE_1 + envelope_nr;
2522   int font_width = getFontWidth(font_nr);
2523   int font_height = getFontHeight(font_nr);
2524   int max_xsize = level.envelope[envelope_nr].xsize;
2525   int max_ysize = level.envelope[envelope_nr].ysize;
2526   int xstart = (anim_mode & ANIM_VERTICAL ? max_xsize : 0);
2527   int ystart = (anim_mode & ANIM_HORIZONTAL ? max_ysize : 0);
2528   int xend = max_xsize;
2529   int yend = (anim_mode != ANIM_DEFAULT ? max_ysize : 0);
2530   int xstep = (xstart < xend ? 1 : 0);
2531   int ystep = (ystart < yend || xstep == 0 ? 1 : 0);
2532   int x, y;
2533
2534   for (x = xstart, y = ystart; x <= xend && y <= yend; x += xstep, y += ystep)
2535   {
2536     int xsize = (action == ACTION_CLOSING ? xend - (x - xstart) : x) + 2;
2537     int ysize = (action == ACTION_CLOSING ? yend - (y - ystart) : y) + 2;
2538     int sx = (SXSIZE - xsize * font_width)  / 2;
2539     int sy = (SYSIZE - ysize * font_height) / 2;
2540     int xx, yy;
2541
2542     SetDrawtoField(DRAW_BUFFERED);
2543
2544     BlitBitmap(fieldbuffer, backbuffer, FX, FY, SXSIZE, SYSIZE, SX, SY);
2545
2546     SetDrawtoField(DRAW_BACKBUFFER);
2547
2548     for (yy = 0; yy < ysize; yy++) for (xx = 0; xx < xsize; xx++)
2549       DrawEnvelopeBackground(envelope_nr, sx,sy, xx,yy, xsize, ysize, font_nr);
2550
2551 #if 1
2552     DrawTextBuffer(SX + sx + font_width, SY + sy + font_height,
2553                    level.envelope[envelope_nr].text, font_nr, max_xsize,
2554                    xsize - 2, ysize - 2, 0, mask_mode,
2555                    level.envelope[envelope_nr].autowrap,
2556                    level.envelope[envelope_nr].centered, FALSE);
2557 #else
2558     DrawTextToTextArea(SX + sx + font_width, SY + sy + font_height,
2559                        level.envelope[envelope_nr].text, font_nr, max_xsize,
2560                        xsize - 2, ysize - 2, mask_mode);
2561 #endif
2562
2563     redraw_mask |= REDRAW_FIELD | REDRAW_FROM_BACKBUFFER;
2564     BackToFront();
2565
2566     WaitUntilDelayReached(&anim_delay, anim_delay_value / 2);
2567   }
2568 }
2569
2570 void AnimateEnvelopeDoor(char *text, int anim_mode, int action)
2571 {
2572 #if 1
2573   int envelope_nr = 0;
2574 #endif
2575   int graphic = IMG_BACKGROUND_ENVELOPE_1 + envelope_nr;
2576   Bitmap *src_bitmap = graphic_info[graphic].bitmap;
2577   int mask_mode = (src_bitmap != NULL ? BLIT_MASKED : BLIT_ON_BACKGROUND);
2578   boolean ffwd_delay = (tape.playing && tape.fast_forward);
2579   boolean no_delay = (tape.warp_forward);
2580   unsigned int anim_delay = 0;
2581   int frame_delay_value = (ffwd_delay ? FfwdFrameDelay : GameFrameDelay);
2582   int anim_delay_value = (no_delay ? 0 : frame_delay_value);
2583 #if 1
2584   int max_word_len = maxWordLengthInString(text);
2585   int font_nr = (max_word_len > 7 ? FONT_TEXT_1 : FONT_TEXT_2);
2586 #else
2587   int font_nr = FONT_ENVELOPE_1 + envelope_nr;
2588 #endif
2589   int font_width = getFontWidth(font_nr);
2590   int font_height = getFontHeight(font_nr);
2591 #if 1
2592
2593 #if 1
2594   int max_xsize = DXSIZE / font_width;
2595   int max_ysize = DYSIZE / font_height;
2596 #else
2597   int max_xsize = 7;    /* tools.c: MAX_REQUEST_LINE_FONT1_LEN == 7 */
2598   int max_ysize = 13;   /* tools.c: MAX_REQUEST_LINES == 13 */
2599 #endif
2600
2601 #else
2602   int max_xsize = level.envelope[envelope_nr].xsize;
2603   int max_ysize = level.envelope[envelope_nr].ysize;
2604 #endif
2605   int xstart = (anim_mode & ANIM_VERTICAL ? max_xsize : 0);
2606   int ystart = (anim_mode & ANIM_HORIZONTAL ? max_ysize : 0);
2607   int xend = max_xsize;
2608   int yend = (anim_mode != ANIM_DEFAULT ? max_ysize : 0);
2609   int xstep = (xstart < xend ? 1 : 0);
2610   int ystep = (ystart < yend || xstep == 0 ? 1 : 0);
2611   int x, y;
2612
2613 #if 1
2614   char *text_ptr;
2615   char *text_copy = getStringCopy(text);
2616 #else
2617 #if 1
2618   font_nr = FONT_TEXT_2;
2619
2620   if (maxWordLengthInString(text) > 7)  /* MAX_REQUEST_LINE_FONT1_LEN == 7 */
2621   {
2622     max_xsize = 10;     /* tools.c: MAX_REQUEST_LINE_FONT2_LEN == 10 */
2623     font_nr = FONT_TEXT_1;
2624   }
2625 #else
2626   int max_word_len = 0;
2627   char *text_ptr;
2628   char *text_copy = getStringCopy(text);
2629
2630   font_nr = FONT_TEXT_2;
2631
2632   for (text_ptr = text; *text_ptr; text_ptr++)
2633   {
2634     max_word_len = (*text_ptr != ' ' ? max_word_len + 1 : 0);
2635
2636     if (max_word_len > 7)       /* tools.c: MAX_REQUEST_LINE_FONT1_LEN == 7 */
2637     {
2638       max_xsize = 10;   /* tools.c: MAX_REQUEST_LINE_FONT2_LEN == 10 */
2639       font_nr = FONT_TEXT_1;
2640
2641       break;
2642     }
2643   }
2644 #endif
2645 #endif
2646
2647 #if 1
2648   for (text_ptr = text_copy; *text_ptr; text_ptr++)
2649     if (*text_ptr == ' ')
2650       *text_ptr = '\n';
2651 #endif
2652
2653 #if 1
2654   dDX = SX + (SXSIZE - DXSIZE) / 2 - DX;
2655   dDY = SY + (SYSIZE - DYSIZE) / 2 - DY;
2656 #else
2657   dDX = SX + SXSIZE / 2 - max_xsize * font_width  / 2 - DX;
2658   dDY = SY + SYSIZE / 2 - max_ysize * font_height / 2 - DY;
2659 #endif
2660
2661   for (x = xstart, y = ystart; x <= xend && y <= yend; x += xstep, y += ystep)
2662   {
2663     int xsize = (action == ACTION_CLOSING ? xend - (x - xstart) : x) + 2;
2664     int ysize = (action == ACTION_CLOSING ? yend - (y - ystart) : y) + 2;
2665     int sx = (SXSIZE - xsize * font_width)  / 2;
2666     int sy = (SYSIZE - ysize * font_height) / 2;
2667     int xx, yy;
2668
2669 #if 1
2670     BlitBitmap(bitmap_db_store, backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
2671 #else
2672     SetDrawtoField(DRAW_BUFFERED);
2673
2674     BlitBitmap(fieldbuffer, backbuffer, FX, FY, SXSIZE, SYSIZE, SX, SY);
2675
2676     SetDrawtoField(DRAW_BACKBUFFER);
2677 #endif
2678
2679     for (yy = 0; yy < ysize; yy++) for (xx = 0; xx < xsize; xx++)
2680       DrawEnvelopeBackground(envelope_nr, sx,sy, xx,yy, xsize, ysize, font_nr);
2681
2682 #if 1
2683
2684 #if 1
2685     DrawTextBuffer(SX + sx + font_width, SY + sy + font_height + 8,
2686                    text_copy, font_nr, max_xsize,
2687                    xsize - 2, ysize - 2, 2, mask_mode,
2688                    FALSE, TRUE, FALSE);
2689 #else
2690     DrawTextBuffer(SX + sx + font_width, SY + sy + font_height,
2691                    level.envelope[envelope_nr].text, font_nr, max_xsize,
2692                    xsize - 2, ysize - 2, 0, mask_mode,
2693                    level.envelope[envelope_nr].autowrap,
2694                    level.envelope[envelope_nr].centered, FALSE);
2695 #endif
2696
2697 #else
2698     DrawTextToTextArea(SX + sx + font_width, SY + sy + font_height,
2699                        level.envelope[envelope_nr].text, font_nr, max_xsize,
2700                        xsize - 2, ysize - 2, mask_mode);
2701 #endif
2702
2703     /* copy request gadgets to door backbuffer */
2704 #if 1
2705     if ((ysize - 2) > 13)
2706       BlitBitmap(bitmap_db_door, drawto,
2707                  DOOR_GFX_PAGEX1 + (DXSIZE - (xsize - 2) * font_width) / 2,
2708                  DOOR_GFX_PAGEY1 + 13 * font_height,
2709                  (xsize - 2) * font_width,
2710                  (ysize - 2 - 13) * font_height,
2711                  SX + sx + font_width,
2712                  SY + sy + font_height * (1 + 13));
2713 #else
2714     if ((ysize - 2) > 13)
2715       BlitBitmap(bitmap_db_door, drawto,
2716                  DOOR_GFX_PAGEX1 + (DXSIZE - (xsize - 2) * font_width) / 2,
2717                  DOOR_GFX_PAGEY1 + 13 * font_height,
2718                  (xsize - 2) * font_width,
2719                  (ysize - 2 - 13) * font_height,
2720                  SX + sx + font_width,
2721                  SY + sy + font_height * (1 + 13));
2722 #endif
2723
2724 #if 1
2725     redraw_mask = REDRAW_FIELD | REDRAW_FROM_BACKBUFFER;
2726     // redraw_mask |= REDRAW_ALL | REDRAW_FROM_BACKBUFFER;
2727 #else
2728     redraw_mask |= REDRAW_FIELD | REDRAW_FROM_BACKBUFFER;
2729 #endif
2730
2731 #if 1
2732     DoAnimation();
2733     BackToFront();
2734 #else
2735     BackToFront();
2736 #endif
2737
2738     WaitUntilDelayReached(&anim_delay, anim_delay_value / 2);
2739   }
2740
2741 #if 1
2742   free(text_copy);
2743 #endif
2744 }
2745
2746 void ShowEnvelope(int envelope_nr)
2747 {
2748   int element = EL_ENVELOPE_1 + envelope_nr;
2749   int graphic = IMG_BACKGROUND_ENVELOPE_1 + envelope_nr;
2750   int sound_opening = element_info[element].sound[ACTION_OPENING];
2751   int sound_closing = element_info[element].sound[ACTION_CLOSING];
2752   boolean ffwd_delay = (tape.playing && tape.fast_forward);
2753   boolean no_delay = (tape.warp_forward);
2754   int normal_delay_value = ONE_SECOND_DELAY / (ffwd_delay ? 2 : 1);
2755   int wait_delay_value = (no_delay ? 0 : normal_delay_value);
2756   int anim_mode = graphic_info[graphic].anim_mode;
2757   int main_anim_mode = (anim_mode == ANIM_NONE ? ANIM_VERTICAL|ANIM_HORIZONTAL:
2758                         anim_mode == ANIM_DEFAULT ? ANIM_VERTICAL : anim_mode);
2759
2760   game.envelope_active = TRUE;  /* needed for RedrawPlayfield() events */
2761
2762   PlayMenuSoundStereo(sound_opening, SOUND_MIDDLE);
2763
2764   if (anim_mode == ANIM_DEFAULT)
2765     AnimateEnvelope(envelope_nr, ANIM_DEFAULT, ACTION_OPENING);
2766
2767   AnimateEnvelope(envelope_nr, main_anim_mode, ACTION_OPENING);
2768
2769   if (tape.playing)
2770     Delay(wait_delay_value);
2771   else
2772     WaitForEventToContinue();
2773
2774   PlayMenuSoundStereo(sound_closing, SOUND_MIDDLE);
2775
2776   if (anim_mode != ANIM_NONE)
2777     AnimateEnvelope(envelope_nr, main_anim_mode, ACTION_CLOSING);
2778
2779   if (anim_mode == ANIM_DEFAULT)
2780     AnimateEnvelope(envelope_nr, ANIM_DEFAULT, ACTION_CLOSING);
2781
2782   game.envelope_active = FALSE;
2783
2784   SetDrawtoField(DRAW_BUFFERED);
2785
2786   redraw_mask |= REDRAW_FIELD;
2787   BackToFront();
2788 }
2789
2790 void ShowEnvelopeDoor(char *text, int action)
2791 {
2792 #if 1
2793   int last_game_status = game_status;   /* save current game status */
2794   // int last_draw_background_mask = gfx.draw_background_mask;
2795   int envelope_nr = 0;
2796 #endif
2797   int element = EL_ENVELOPE_1 + envelope_nr;
2798   int graphic = IMG_BACKGROUND_ENVELOPE_1 + envelope_nr;
2799   int sound_opening = element_info[element].sound[ACTION_OPENING];
2800   int sound_closing = element_info[element].sound[ACTION_CLOSING];
2801 #if 0
2802   boolean ffwd_delay = (tape.playing && tape.fast_forward);
2803   boolean no_delay = (tape.warp_forward);
2804   int normal_delay_value = ONE_SECOND_DELAY / (ffwd_delay ? 2 : 1);
2805   int wait_delay_value = (no_delay ? 0 : normal_delay_value);
2806 #endif
2807   int anim_mode = graphic_info[graphic].anim_mode;
2808   int main_anim_mode = (anim_mode == ANIM_NONE ? ANIM_VERTICAL|ANIM_HORIZONTAL:
2809                         anim_mode == ANIM_DEFAULT ? ANIM_VERTICAL : anim_mode);
2810
2811 #if 1
2812   if (game_status == GAME_MODE_PLAYING)
2813   {
2814     if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
2815       BlitScreenToBitmap_EM(backbuffer);
2816     else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
2817       BlitScreenToBitmap_SP(backbuffer);
2818     else
2819     {
2820       BlitBitmap(fieldbuffer, backbuffer, FX, FY, SXSIZE, SYSIZE, SX, SY);
2821     }
2822   }
2823
2824   SetDrawtoField(DRAW_BACKBUFFER);
2825
2826   // SetDrawBackgroundMask(REDRAW_NONE);
2827
2828   if (action == ACTION_OPENING)
2829   {
2830     BlitBitmap(backbuffer, bitmap_db_store, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
2831
2832     if (game_status != GAME_MODE_MAIN)
2833       InitAnimation();
2834   }
2835
2836   /* force DOOR font inside door area */
2837   game_status = GAME_MODE_PSEUDO_DOOR;
2838 #endif
2839
2840   game.envelope_active = TRUE;  /* needed for RedrawPlayfield() events */
2841
2842   if (action == ACTION_OPENING)
2843   {
2844     PlayMenuSoundStereo(sound_opening, SOUND_MIDDLE);
2845
2846     if (anim_mode == ANIM_DEFAULT)
2847       AnimateEnvelopeDoor(text, ANIM_DEFAULT, ACTION_OPENING);
2848
2849     AnimateEnvelopeDoor(text, main_anim_mode, ACTION_OPENING);
2850
2851 #if 0
2852     if (tape.playing)
2853       Delay(wait_delay_value);
2854     else
2855       WaitForEventToContinue();
2856 #endif
2857   }
2858   else
2859   {
2860     PlayMenuSoundStereo(sound_closing, SOUND_MIDDLE);
2861
2862     if (anim_mode != ANIM_NONE)
2863       AnimateEnvelopeDoor(text, main_anim_mode, ACTION_CLOSING);
2864
2865     if (anim_mode == ANIM_DEFAULT)
2866       AnimateEnvelopeDoor(text, ANIM_DEFAULT, ACTION_CLOSING);
2867   }
2868
2869   game.envelope_active = FALSE;
2870
2871 #if 1
2872   // game_status = last_game_status;    /* restore current game status */
2873
2874   if (action == ACTION_CLOSING)
2875   {
2876     if (game_status != GAME_MODE_MAIN)
2877       StopAnimation();
2878
2879     BlitBitmap(bitmap_db_store, backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
2880   }
2881 #else
2882   SetDrawtoField(DRAW_BUFFERED);
2883 #endif
2884
2885   // SetDrawBackgroundMask(last_draw_background_mask);
2886
2887 #if 1
2888   redraw_mask = REDRAW_FIELD;
2889   // redraw_mask |= REDRAW_ALL;
2890 #else
2891   redraw_mask |= REDRAW_FIELD;
2892 #endif
2893
2894 #if 1
2895   if (game_status == GAME_MODE_MAIN)
2896     DoAnimation();
2897
2898   BackToFront();
2899
2900   /* (important: after "BackToFront()", but before "SetDrawtoField()") */
2901   game_status = last_game_status;       /* restore current game status */
2902
2903   if (game_status == GAME_MODE_PLAYING &&
2904       level.game_engine_type == GAME_ENGINE_TYPE_RND)
2905     SetDrawtoField(DRAW_BUFFERED);
2906 #else
2907   BackToFront();
2908 #endif
2909 }
2910
2911 void DrawPreviewElement(int dst_x, int dst_y, int element, int tilesize)
2912 {
2913   Bitmap *src_bitmap;
2914   int src_x, src_y;
2915   int graphic = el2preimg(element);
2916
2917   getSizedGraphicSource(graphic, 0, tilesize, &src_bitmap, &src_x, &src_y);
2918   BlitBitmap(src_bitmap, drawto, src_x, src_y, tilesize, tilesize, dst_x,dst_y);
2919 }
2920
2921 void DrawLevel()
2922 {
2923   int x,y;
2924
2925 #if 1
2926   SetMainBackgroundImage(IMG_BACKGROUND_PLAYING);
2927   SetDrawBackgroundMask(REDRAW_FIELD);
2928 #else
2929   SetDrawBackgroundMask(REDRAW_NONE);
2930 #endif
2931
2932   ClearField();
2933
2934   for (x = BX1; x <= BX2; x++)
2935     for (y = BY1; y <= BY2; y++)
2936       DrawScreenField(x, y);
2937
2938   redraw_mask |= REDRAW_FIELD;
2939 }
2940
2941 void DrawMiniLevel(int size_x, int size_y, int scroll_x, int scroll_y)
2942 {
2943   int x,y;
2944
2945   for (x = 0; x < size_x; x++)
2946     for (y = 0; y < size_y; y++)
2947       DrawMiniElementOrWall(x, y, scroll_x, scroll_y);
2948
2949   redraw_mask |= REDRAW_FIELD;
2950 }
2951
2952 static void DrawPreviewLevelExt(int from_x, int from_y)
2953 {
2954   boolean show_level_border = (BorderElement != EL_EMPTY);
2955   int level_xsize = lev_fieldx + (show_level_border ? 2 : 0);
2956   int level_ysize = lev_fieldy + (show_level_border ? 2 : 0);
2957   int tile_size = preview.tile_size;
2958   int preview_width  = preview.xsize * tile_size;
2959   int preview_height = preview.ysize * tile_size;
2960   int real_preview_xsize = MIN(level_xsize, preview.xsize);
2961   int real_preview_ysize = MIN(level_ysize, preview.ysize);
2962   int dst_x = SX + ALIGNED_XPOS(preview.x, preview_width, preview.align);
2963   int dst_y = SY + ALIGNED_YPOS(preview.y, preview_height, preview.valign);
2964   int x, y;
2965
2966   DrawBackground(dst_x, dst_y, preview_width, preview_height);
2967
2968   dst_x += (preview_width  - real_preview_xsize * tile_size) / 2;
2969   dst_y += (preview_height - real_preview_ysize * tile_size) / 2;
2970
2971   for (x = 0; x < real_preview_xsize; x++)
2972   {
2973     for (y = 0; y < real_preview_ysize; y++)
2974     {
2975       int lx = from_x + x + (show_level_border ? -1 : 0);
2976       int ly = from_y + y + (show_level_border ? -1 : 0);
2977       int element = (IN_LEV_FIELD(lx, ly) ? level.field[lx][ly] :
2978                      getBorderElement(lx, ly));
2979
2980       DrawPreviewElement(dst_x + x * tile_size, dst_y + y * tile_size,
2981                          element, tile_size);
2982     }
2983   }
2984
2985   redraw_mask |= REDRAW_MICROLEVEL;
2986 }
2987
2988 #define MICROLABEL_EMPTY                0
2989 #define MICROLABEL_LEVEL_NAME           1
2990 #define MICROLABEL_LEVEL_AUTHOR_HEAD    2
2991 #define MICROLABEL_LEVEL_AUTHOR         3
2992 #define MICROLABEL_IMPORTED_FROM_HEAD   4
2993 #define MICROLABEL_IMPORTED_FROM        5
2994 #define MICROLABEL_IMPORTED_BY_HEAD     6
2995 #define MICROLABEL_IMPORTED_BY          7
2996
2997 static int getMaxTextLength(struct TextPosInfo *pos, int font_nr)
2998 {
2999   int max_text_width = SXSIZE;
3000   int font_width = getFontWidth(font_nr);
3001
3002   if (pos->align == ALIGN_CENTER)
3003     max_text_width = (pos->x < SXSIZE / 2 ? pos->x * 2 : (SXSIZE - pos->x) * 2);
3004   else if (pos->align == ALIGN_RIGHT)
3005     max_text_width = pos->x;
3006   else
3007     max_text_width = SXSIZE - pos->x;
3008
3009   return max_text_width / font_width;
3010 }
3011
3012 static void DrawPreviewLevelLabelExt(int mode)
3013 {
3014   struct TextPosInfo *pos = &menu.main.text.level_info_2;
3015   char label_text[MAX_OUTPUT_LINESIZE + 1];
3016   int max_len_label_text;
3017 #if 1
3018   int font_nr = pos->font;
3019   int i;
3020
3021   if (mode == MICROLABEL_LEVEL_AUTHOR_HEAD ||
3022       mode == MICROLABEL_IMPORTED_FROM_HEAD ||
3023       mode == MICROLABEL_IMPORTED_BY_HEAD)
3024     font_nr = pos->font_alt;
3025 #else
3026   int font_nr = FONT_TEXT_2;
3027   int i;
3028
3029   if (mode == MICROLABEL_LEVEL_AUTHOR_HEAD ||
3030       mode == MICROLABEL_IMPORTED_FROM_HEAD ||
3031       mode == MICROLABEL_IMPORTED_BY_HEAD)
3032     font_nr = FONT_TEXT_3;
3033 #endif
3034
3035 #if 1
3036   max_len_label_text = getMaxTextLength(pos, font_nr);
3037 #else
3038   max_len_label_text = SXSIZE / getFontWidth(font_nr);
3039 #endif
3040
3041 #if 1
3042   if (pos->size != -1)
3043     max_len_label_text = pos->size;
3044 #endif
3045
3046   for (i = 0; i < max_len_label_text; i++)
3047     label_text[i] = ' ';
3048   label_text[max_len_label_text] = '\0';
3049
3050   if (strlen(label_text) > 0)
3051   {
3052 #if 1
3053     DrawTextSAligned(pos->x, pos->y, label_text, font_nr, pos->align);
3054 #else
3055     int lxpos = SX + (SXSIZE - getTextWidth(label_text, font_nr)) / 2;
3056     int lypos = MICROLABEL2_YPOS;
3057
3058     DrawText(lxpos, lypos, label_text, font_nr);
3059 #endif
3060   }
3061
3062   strncpy(label_text,
3063           (mode == MICROLABEL_LEVEL_NAME ? level.name :
3064            mode == MICROLABEL_LEVEL_AUTHOR_HEAD ? "created by" :
3065            mode == MICROLABEL_LEVEL_AUTHOR ? level.author :
3066            mode == MICROLABEL_IMPORTED_FROM_HEAD ? "imported from" :
3067            mode == MICROLABEL_IMPORTED_FROM ? leveldir_current->imported_from :
3068            mode == MICROLABEL_IMPORTED_BY_HEAD ? "imported by" :
3069            mode == MICROLABEL_IMPORTED_BY ? leveldir_current->imported_by :""),
3070           max_len_label_text);
3071   label_text[max_len_label_text] = '\0';
3072
3073   if (strlen(label_text) > 0)
3074   {
3075 #if 1
3076     DrawTextSAligned(pos->x, pos->y, label_text, font_nr, pos->align);
3077 #else
3078     int lxpos = SX + (SXSIZE - getTextWidth(label_text, font_nr)) / 2;
3079     int lypos = MICROLABEL2_YPOS;
3080
3081     DrawText(lxpos, lypos, label_text, font_nr);
3082 #endif
3083   }
3084
3085   redraw_mask |= REDRAW_MICROLEVEL;
3086 }
3087
3088 void DrawPreviewLevel(boolean restart)
3089 {
3090   static unsigned int scroll_delay = 0;
3091   static unsigned int label_delay = 0;
3092   static int from_x, from_y, scroll_direction;
3093   static int label_state, label_counter;
3094   unsigned int scroll_delay_value = preview.step_delay;
3095   boolean show_level_border = (BorderElement != EL_EMPTY);
3096   int level_xsize = lev_fieldx + (show_level_border ? 2 : 0);
3097   int level_ysize = lev_fieldy + (show_level_border ? 2 : 0);
3098   int last_game_status = game_status;           /* save current game status */
3099
3100 #if 0
3101   /* force PREVIEW font on preview level */
3102   game_status = GAME_MODE_PSEUDO_PREVIEW;
3103 #endif
3104
3105   if (restart)
3106   {
3107     from_x = 0;
3108     from_y = 0;
3109
3110     if (preview.anim_mode == ANIM_CENTERED)
3111     {
3112       if (level_xsize > preview.xsize)
3113         from_x = (level_xsize - preview.xsize) / 2;
3114       if (level_ysize > preview.ysize)
3115         from_y = (level_ysize - preview.ysize) / 2;
3116     }
3117
3118     from_x += preview.xoffset;
3119     from_y += preview.yoffset;
3120
3121     scroll_direction = MV_RIGHT;
3122     label_state = 1;
3123     label_counter = 0;
3124
3125     DrawPreviewLevelExt(from_x, from_y);
3126     DrawPreviewLevelLabelExt(label_state);
3127
3128     /* initialize delay counters */
3129     DelayReached(&scroll_delay, 0);
3130     DelayReached(&label_delay, 0);
3131
3132     if (leveldir_current->name)
3133     {
3134       struct TextPosInfo *pos = &menu.main.text.level_info_1;
3135       char label_text[MAX_OUTPUT_LINESIZE + 1];
3136 #if 1
3137       int font_nr = pos->font;
3138 #else
3139       int font_nr = FONT_TEXT_1;
3140 #endif
3141 #if 1
3142       int max_len_label_text = getMaxTextLength(pos, font_nr);
3143 #else
3144       int max_len_label_text = SXSIZE / getFontWidth(font_nr);
3145 #endif
3146 #if 0
3147       int text_width;
3148       int lxpos, lypos;
3149 #endif
3150
3151 #if 1
3152       if (pos->size != -1)
3153         max_len_label_text = pos->size;
3154 #endif
3155
3156       strncpy(label_text, leveldir_current->name, max_len_label_text);
3157       label_text[max_len_label_text] = '\0';
3158
3159 #if 1
3160       DrawTextSAligned(pos->x, pos->y, label_text, font_nr, pos->align);
3161 #else
3162       lxpos = SX + (SXSIZE - getTextWidth(label_text, font_nr)) / 2;
3163       lypos = SY + MICROLABEL1_YPOS;
3164
3165       DrawText(lxpos, lypos, label_text, font_nr);
3166 #endif
3167     }
3168
3169     game_status = last_game_status;     /* restore current game status */
3170
3171     return;
3172   }
3173
3174   /* scroll preview level, if needed */
3175   if (preview.anim_mode != ANIM_NONE &&
3176       (level_xsize > preview.xsize || level_ysize > preview.ysize) &&
3177       DelayReached(&scroll_delay, scroll_delay_value))
3178   {
3179     switch (scroll_direction)
3180     {
3181       case MV_LEFT:
3182         if (from_x > 0)
3183         {
3184           from_x -= preview.step_offset;
3185           from_x = (from_x < 0 ? 0 : from_x);
3186         }
3187         else
3188           scroll_direction = MV_UP;
3189         break;
3190
3191       case MV_RIGHT:
3192         if (from_x < level_xsize - preview.xsize)
3193         {
3194           from_x += preview.step_offset;
3195           from_x = (from_x > level_xsize - preview.xsize ?
3196                     level_xsize - preview.xsize : from_x);
3197         }
3198         else
3199           scroll_direction = MV_DOWN;
3200         break;
3201
3202       case MV_UP:
3203         if (from_y > 0)
3204         {
3205           from_y -= preview.step_offset;
3206           from_y = (from_y < 0 ? 0 : from_y);
3207         }
3208         else
3209           scroll_direction = MV_RIGHT;
3210         break;
3211
3212       case MV_DOWN:
3213         if (from_y < level_ysize - preview.ysize)
3214         {
3215           from_y += preview.step_offset;
3216           from_y = (from_y > level_ysize - preview.ysize ?
3217                     level_ysize - preview.ysize : from_y);
3218         }
3219         else
3220           scroll_direction = MV_LEFT;
3221         break;
3222
3223       default:
3224         break;
3225     }
3226
3227     DrawPreviewLevelExt(from_x, from_y);
3228   }
3229
3230   /* !!! THIS ALL SUCKS -- SHOULD BE CLEANLY REWRITTEN !!! */
3231   /* redraw micro level label, if needed */
3232   if (!strEqual(level.name, NAMELESS_LEVEL_NAME) &&
3233       !strEqual(level.author, ANONYMOUS_NAME) &&
3234       !strEqual(level.author, leveldir_current->name) &&
3235       DelayReached(&label_delay, MICROLEVEL_LABEL_DELAY))
3236   {
3237     int max_label_counter = 23;
3238
3239     if (leveldir_current->imported_from != NULL &&
3240         strlen(leveldir_current->imported_from) > 0)
3241       max_label_counter += 14;
3242     if (leveldir_current->imported_by != NULL &&
3243         strlen(leveldir_current->imported_by) > 0)
3244       max_label_counter += 14;
3245
3246     label_counter = (label_counter + 1) % max_label_counter;
3247     label_state = (label_counter >= 0 && label_counter <= 7 ?
3248                    MICROLABEL_LEVEL_NAME :
3249                    label_counter >= 9 && label_counter <= 12 ?
3250                    MICROLABEL_LEVEL_AUTHOR_HEAD :
3251                    label_counter >= 14 && label_counter <= 21 ?
3252                    MICROLABEL_LEVEL_AUTHOR :
3253                    label_counter >= 23 && label_counter <= 26 ?
3254                    MICROLABEL_IMPORTED_FROM_HEAD :
3255                    label_counter >= 28 && label_counter <= 35 ?
3256                    MICROLABEL_IMPORTED_FROM :
3257                    label_counter >= 37 && label_counter <= 40 ?
3258                    MICROLABEL_IMPORTED_BY_HEAD :
3259                    label_counter >= 42 && label_counter <= 49 ?
3260                    MICROLABEL_IMPORTED_BY : MICROLABEL_EMPTY);
3261
3262     if (leveldir_current->imported_from == NULL &&
3263         (label_state == MICROLABEL_IMPORTED_FROM_HEAD ||
3264          label_state == MICROLABEL_IMPORTED_FROM))
3265       label_state = (label_state == MICROLABEL_IMPORTED_FROM_HEAD ?
3266                      MICROLABEL_IMPORTED_BY_HEAD : MICROLABEL_IMPORTED_BY);
3267
3268     DrawPreviewLevelLabelExt(label_state);
3269   }
3270
3271   game_status = last_game_status;       /* restore current game status */
3272 }
3273
3274 inline void DrawGraphicAnimationExt(DrawBuffer *dst_bitmap, int x, int y,
3275                                     int graphic, int sync_frame, int mask_mode)
3276 {
3277   int frame = getGraphicAnimationFrame(graphic, sync_frame);
3278
3279   if (mask_mode == USE_MASKING)
3280     DrawGraphicThruMaskExt(dst_bitmap, x, y, graphic, frame);
3281   else
3282     DrawGraphicExt(dst_bitmap, x, y, graphic, frame);
3283 }
3284
3285 inline void DrawFixedGraphicAnimationExt(DrawBuffer *dst_bitmap, int x, int y,
3286                                          int graphic, int sync_frame,
3287                                          int mask_mode)
3288 {
3289   int frame = getGraphicAnimationFrame(graphic, sync_frame);
3290
3291   if (mask_mode == USE_MASKING)
3292     DrawFixedGraphicThruMaskExt(dst_bitmap, x, y, graphic, frame);
3293   else
3294     DrawFixedGraphicExt(dst_bitmap, x, y, graphic, frame);
3295 }
3296
3297 inline void DrawGraphicAnimation(int x, int y, int graphic)
3298 {
3299   int lx = LEVELX(x), ly = LEVELY(y);
3300
3301   if (!IN_SCR_FIELD(x, y))
3302     return;
3303
3304 #if NEW_TILESIZE
3305   DrawGraphicAnimationExt(drawto_field, FX + x * TILEX_VAR, FY + y * TILEY_VAR,
3306                           graphic, GfxFrame[lx][ly], NO_MASKING);
3307 #else
3308   DrawGraphicAnimationExt(drawto_field, FX + x * TILEX, FY + y * TILEY,
3309                           graphic, GfxFrame[lx][ly], NO_MASKING);
3310 #endif
3311   MarkTileDirty(x, y);
3312 }
3313
3314 inline void DrawFixedGraphicAnimation(int x, int y, int graphic)
3315 {
3316   int lx = LEVELX(x), ly = LEVELY(y);
3317
3318   if (!IN_SCR_FIELD(x, y))
3319     return;
3320
3321   DrawGraphicAnimationExt(drawto_field, FX + x * TILEX, FY + y * TILEY,
3322                           graphic, GfxFrame[lx][ly], NO_MASKING);
3323   MarkTileDirty(x, y);
3324 }
3325
3326 void DrawLevelGraphicAnimation(int x, int y, int graphic)
3327 {
3328   DrawGraphicAnimation(SCREENX(x), SCREENY(y), graphic);
3329 }
3330
3331 void DrawLevelElementAnimation(int x, int y, int element)
3332 {
3333   int graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
3334
3335   DrawGraphicAnimation(SCREENX(x), SCREENY(y), graphic);
3336 }
3337
3338 inline void DrawLevelGraphicAnimationIfNeeded(int x, int y, int graphic)
3339 {
3340   int sx = SCREENX(x), sy = SCREENY(y);
3341
3342   if (!IN_LEV_FIELD(x, y) || !IN_SCR_FIELD(sx, sy))
3343     return;
3344
3345   if (!IS_NEW_FRAME(GfxFrame[x][y], graphic))
3346     return;
3347
3348   DrawGraphicAnimation(sx, sy, graphic);
3349
3350 #if 1
3351   if (GFX_CRUMBLED(TILE_GFX_ELEMENT(x, y)))
3352     DrawLevelFieldCrumbled(x, y);
3353 #else
3354   if (GFX_CRUMBLED(Feld[x][y]))
3355     DrawLevelFieldCrumbled(x, y);
3356 #endif
3357 }
3358
3359 void DrawLevelElementAnimationIfNeeded(int x, int y, int element)
3360 {
3361   int sx = SCREENX(x), sy = SCREENY(y);
3362   int graphic;
3363
3364   if (!IN_LEV_FIELD(x, y) || !IN_SCR_FIELD(sx, sy))
3365     return;
3366
3367   graphic = el_act_dir2img(element, GfxAction[x][y], GfxDir[x][y]);
3368
3369   if (!IS_NEW_FRAME(GfxFrame[x][y], graphic))
3370     return;
3371
3372   DrawGraphicAnimation(sx, sy, graphic);
3373
3374   if (GFX_CRUMBLED(element))
3375     DrawLevelFieldCrumbled(x, y);
3376 }
3377
3378 static int getPlayerGraphic(struct PlayerInfo *player, int move_dir)
3379 {
3380   if (player->use_murphy)
3381   {
3382     /* this works only because currently only one player can be "murphy" ... */
3383     static int last_horizontal_dir = MV_LEFT;
3384     int graphic = el_act_dir2img(EL_SP_MURPHY, player->GfxAction, move_dir);
3385
3386     if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
3387       last_horizontal_dir = move_dir;
3388
3389     if (graphic == IMG_SP_MURPHY)       /* undefined => use special graphic */
3390     {
3391       int direction = (player->is_snapping ? move_dir : last_horizontal_dir);
3392
3393       graphic = el_act_dir2img(EL_SP_MURPHY, player->GfxAction, direction);
3394     }
3395
3396     return graphic;
3397   }
3398   else
3399     return el_act_dir2img(player->artwork_element, player->GfxAction,move_dir);
3400 }
3401
3402 static boolean equalGraphics(int graphic1, int graphic2)
3403 {
3404   struct GraphicInfo *g1 = &graphic_info[graphic1];
3405   struct GraphicInfo *g2 = &graphic_info[graphic2];
3406
3407   return (g1->bitmap      == g2->bitmap &&
3408           g1->src_x       == g2->src_x &&
3409           g1->src_y       == g2->src_y &&
3410           g1->anim_frames == g2->anim_frames &&
3411           g1->anim_delay  == g2->anim_delay &&
3412           g1->anim_mode   == g2->anim_mode);
3413 }
3414
3415 void DrawAllPlayers()
3416 {
3417   int i;
3418
3419   for (i = 0; i < MAX_PLAYERS; i++)
3420     if (stored_player[i].active)
3421       DrawPlayer(&stored_player[i]);
3422 }
3423
3424 void DrawPlayerField(int x, int y)
3425 {
3426   if (!IS_PLAYER(x, y))
3427     return;
3428
3429   DrawPlayer(PLAYERINFO(x, y));
3430 }
3431
3432 #define DRAW_PLAYER_OVER_PUSHED_ELEMENT 1
3433
3434 void DrawPlayer(struct PlayerInfo *player)
3435 {
3436   int jx = player->jx;
3437   int jy = player->jy;
3438   int move_dir = player->MovDir;
3439   int dx = (move_dir == MV_LEFT ? -1 : move_dir == MV_RIGHT ? +1 : 0);
3440   int dy = (move_dir == MV_UP   ? -1 : move_dir == MV_DOWN  ? +1 : 0);
3441   int last_jx = (player->is_moving ? jx - dx : jx);
3442   int last_jy = (player->is_moving ? jy - dy : jy);
3443   int next_jx = jx + dx;
3444   int next_jy = jy + dy;
3445   boolean player_is_moving = (player->MovPos ? TRUE : FALSE);
3446   boolean player_is_opaque = FALSE;
3447   int sx = SCREENX(jx), sy = SCREENY(jy);
3448   int sxx = 0, syy = 0;
3449   int element = Feld[jx][jy], last_element = Feld[last_jx][last_jy];
3450   int graphic;
3451   int action = ACTION_DEFAULT;
3452   int last_player_graphic = getPlayerGraphic(player, move_dir);
3453   int last_player_frame = player->Frame;
3454   int frame = 0;
3455
3456   /* GfxElement[][] is set to the element the player is digging or collecting;
3457      remove also for off-screen player if the player is not moving anymore */
3458   if (IN_LEV_FIELD(jx, jy) && !player_is_moving)
3459     GfxElement[jx][jy] = EL_UNDEFINED;
3460
3461   if (!player->active || !IN_SCR_FIELD(SCREENX(last_jx), SCREENY(last_jy)))
3462     return;
3463
3464 #if DEBUG
3465   if (!IN_LEV_FIELD(jx, jy))
3466   {
3467     printf("DrawPlayerField(): x = %d, y = %d\n",jx,jy);
3468     printf("DrawPlayerField(): sx = %d, sy = %d\n",sx,sy);
3469     printf("DrawPlayerField(): This should never happen!\n");
3470     return;
3471   }
3472 #endif
3473
3474   if (element == EL_EXPLOSION)
3475     return;
3476
3477   action = (player->is_pushing    ? ACTION_PUSHING         :
3478             player->is_digging    ? ACTION_DIGGING         :
3479             player->is_collecting ? ACTION_COLLECTING      :
3480             player->is_moving     ? ACTION_MOVING          :
3481             player->is_snapping   ? ACTION_SNAPPING        :
3482             player->is_dropping   ? ACTION_DROPPING        :
3483             player->is_waiting    ? player->action_waiting : ACTION_DEFAULT);
3484
3485   if (player->is_waiting)
3486     move_dir = player->dir_waiting;
3487
3488   InitPlayerGfxAnimation(player, action, move_dir);
3489
3490   /* ----------------------------------------------------------------------- */
3491   /* draw things in the field the player is leaving, if needed               */
3492   /* ----------------------------------------------------------------------- */
3493
3494   if (player->is_moving)
3495   {
3496     if (Back[last_jx][last_jy] && IS_DRAWABLE(last_element))
3497     {
3498       DrawLevelElement(last_jx, last_jy, Back[last_jx][last_jy]);
3499
3500       if (last_element == EL_DYNAMITE_ACTIVE ||
3501           last_element == EL_EM_DYNAMITE_ACTIVE ||
3502           last_element == EL_SP_DISK_RED_ACTIVE)
3503         DrawDynamite(last_jx, last_jy);
3504       else
3505         DrawLevelFieldThruMask(last_jx, last_jy);
3506     }
3507     else if (last_element == EL_DYNAMITE_ACTIVE ||
3508              last_element == EL_EM_DYNAMITE_ACTIVE ||
3509              last_element == EL_SP_DISK_RED_ACTIVE)
3510       DrawDynamite(last_jx, last_jy);
3511 #if 0
3512     /* !!! this is not enough to prevent flickering of players which are
3513        moving next to each others without a free tile between them -- this
3514        can only be solved by drawing all players layer by layer (first the
3515        background, then the foreground etc.) !!! => TODO */
3516     else if (!IS_PLAYER(last_jx, last_jy))
3517       DrawLevelField(last_jx, last_jy);
3518 #else
3519     else
3520       DrawLevelField(last_jx, last_jy);
3521 #endif
3522
3523     if (player->is_pushing && IN_SCR_FIELD(SCREENX(next_jx), SCREENY(next_jy)))
3524       DrawLevelElement(next_jx, next_jy, EL_EMPTY);
3525   }
3526
3527   if (!IN_SCR_FIELD(sx, sy))
3528     return;
3529
3530   /* ----------------------------------------------------------------------- */
3531   /* draw things behind the player, if needed                                */
3532   /* ----------------------------------------------------------------------- */
3533
3534   if (Back[jx][jy])
3535     DrawLevelElement(jx, jy, Back[jx][jy]);
3536   else if (IS_ACTIVE_BOMB(element))
3537     DrawLevelElement(jx, jy, EL_EMPTY);
3538   else
3539   {
3540     if (player_is_moving && GfxElement[jx][jy] != EL_UNDEFINED)
3541     {
3542       int old_element = GfxElement[jx][jy];
3543       int old_graphic = el_act_dir2img(old_element, action, move_dir);
3544       int frame = getGraphicAnimationFrame(old_graphic, player->StepFrame);
3545
3546       if (GFX_CRUMBLED(old_element))
3547         DrawLevelFieldCrumbledDigging(jx, jy, move_dir, player->StepFrame);
3548       else
3549         DrawGraphic(sx, sy, old_graphic, frame);
3550
3551       if (graphic_info[old_graphic].anim_mode & ANIM_OPAQUE_PLAYER)
3552         player_is_opaque = TRUE;
3553     }
3554     else
3555     {
3556       GfxElement[jx][jy] = EL_UNDEFINED;
3557
3558       /* make sure that pushed elements are drawn with correct frame rate */
3559 #if 1
3560       graphic = el_act_dir2img(element, ACTION_PUSHING, move_dir);
3561
3562       if (player->is_pushing && player->is_moving && !IS_ANIM_MODE_CE(graphic))
3563         GfxFrame[jx][jy] = player->StepFrame;
3564 #else
3565       if (player->is_pushing && player->is_moving)
3566         GfxFrame[jx][jy] = player->StepFrame;
3567 #endif
3568
3569       DrawLevelField(jx, jy);
3570     }
3571   }
3572
3573 #if !DRAW_PLAYER_OVER_PUSHED_ELEMENT
3574   /* ----------------------------------------------------------------------- */
3575   /* draw player himself                                                     */
3576   /* ----------------------------------------------------------------------- */
3577
3578   graphic = getPlayerGraphic(player, move_dir);
3579
3580   /* in the case of changed player action or direction, prevent the current
3581      animation frame from being restarted for identical animations */
3582   if (player->Frame == 0 && equalGraphics(graphic, last_player_graphic))
3583     player->Frame = last_player_frame;
3584
3585   frame = getGraphicAnimationFrame(graphic, player->Frame);
3586
3587   if (player->GfxPos)
3588   {
3589     if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
3590       sxx = player->GfxPos;
3591     else
3592       syy = player->GfxPos;
3593   }
3594
3595   if (!setup.soft_scrolling && ScreenMovPos)
3596     sxx = syy = 0;
3597
3598   if (player_is_opaque)
3599     DrawGraphicShifted(sx, sy, sxx, syy, graphic, frame,NO_CUTTING,NO_MASKING);
3600   else
3601     DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
3602
3603   if (SHIELD_ON(player))
3604   {
3605     int graphic = (player->shield_deadly_time_left ? IMG_SHIELD_DEADLY_ACTIVE :
3606                    IMG_SHIELD_NORMAL_ACTIVE);
3607     int frame = getGraphicAnimationFrame(graphic, -1);
3608
3609     DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
3610   }
3611 #endif
3612
3613 #if DRAW_PLAYER_OVER_PUSHED_ELEMENT
3614   if (player->GfxPos)
3615   {
3616     if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
3617       sxx = player->GfxPos;
3618     else
3619       syy = player->GfxPos;
3620   }
3621 #endif
3622
3623   /* ----------------------------------------------------------------------- */
3624   /* draw things the player is pushing, if needed                            */
3625   /* ----------------------------------------------------------------------- */
3626
3627 #if 0
3628   printf("::: %d, %d [%d, %d] [%d]\n",
3629          player->is_pushing, player_is_moving, player->GfxAction,
3630          player->is_moving, player_is_moving);
3631 #endif
3632
3633 #if 1
3634   if (player->is_pushing && player->is_moving)
3635   {
3636     int px = SCREENX(jx), py = SCREENY(jy);
3637     int pxx = (TILEX - ABS(sxx)) * dx;
3638     int pyy = (TILEY - ABS(syy)) * dy;
3639     int gfx_frame = GfxFrame[jx][jy];
3640
3641     int graphic;
3642     int sync_frame;
3643     int frame;
3644
3645     if (!IS_MOVING(jx, jy))             /* push movement already finished */
3646     {
3647       element = Feld[next_jx][next_jy];
3648       gfx_frame = GfxFrame[next_jx][next_jy];
3649     }
3650
3651     graphic = el_act_dir2img(element, ACTION_PUSHING, move_dir);
3652
3653 #if 1
3654     sync_frame = (IS_ANIM_MODE_CE(graphic) ? gfx_frame : player->StepFrame);
3655     frame = getGraphicAnimationFrame(graphic, sync_frame);
3656 #else
3657     frame = getGraphicAnimationFrame(graphic, player->StepFrame);
3658 #endif
3659
3660     /* draw background element under pushed element (like the Sokoban field) */
3661 #if 1
3662     if (game.use_masked_pushing && IS_MOVING(jx, jy))
3663     {
3664       /* this allows transparent pushing animation over non-black background */
3665
3666       if (Back[jx][jy])
3667         DrawLevelElement(jx, jy, Back[jx][jy]);
3668       else
3669         DrawLevelElement(jx, jy, EL_EMPTY);
3670
3671       if (Back[next_jx][next_jy])
3672         DrawLevelElement(next_jx, next_jy, Back[next_jx][next_jy]);
3673       else
3674         DrawLevelElement(next_jx, next_jy, EL_EMPTY);
3675     }
3676     else if (Back[next_jx][next_jy])
3677       DrawLevelElement(next_jx, next_jy, Back[next_jx][next_jy]);
3678 #else
3679     if (Back[next_jx][next_jy])
3680       DrawLevelElement(next_jx, next_jy, Back[next_jx][next_jy]);
3681 #endif
3682
3683 #if 0
3684     printf("::: %d, %d, %d, %d [%d] [%d, %d, %d] [%d] [%d, %d] [%d, %d]\n",
3685            jx, px, player->GfxPos, player->StepFrame,
3686            player->is_pushing,
3687            dx, sxx, pxx,
3688            IS_MOVING(jx, jy),
3689            graphic, frame,
3690            GfxFrame[jx][jy], GfxFrame[next_jx][next_jy]);
3691 #endif
3692
3693 #if 1
3694     /* do not draw (EM style) pushing animation when pushing is finished */
3695     /* (two-tile animations usually do not contain start and end frame) */
3696     if (graphic_info[graphic].double_movement && !IS_MOVING(jx, jy))
3697       DrawLevelElement(next_jx, next_jy, Feld[next_jx][next_jy]);
3698     else
3699       DrawGraphicShiftedThruMask(px, py, pxx, pyy, graphic, frame, NO_CUTTING);
3700 #else
3701     /* masked drawing is needed for EMC style (double) movement graphics */
3702     /* !!! (ONLY WHEN DRAWING PUSHED ELEMENT OVER THE PLAYER) !!! */
3703     DrawGraphicShiftedThruMask(px, py, pxx, pyy, graphic, frame, NO_CUTTING);
3704 #endif
3705   }
3706 #endif
3707
3708 #if DRAW_PLAYER_OVER_PUSHED_ELEMENT
3709   /* ----------------------------------------------------------------------- */
3710   /* draw player himself                                                     */
3711   /* ----------------------------------------------------------------------- */
3712
3713   graphic = getPlayerGraphic(player, move_dir);
3714
3715   /* in the case of changed player action or direction, prevent the current
3716      animation frame from being restarted for identical animations */
3717   if (player->Frame == 0 && equalGraphics(graphic, last_player_graphic))
3718     player->Frame = last_player_frame;
3719
3720   frame = getGraphicAnimationFrame(graphic, player->Frame);
3721
3722   if (player->GfxPos)
3723   {
3724     if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
3725       sxx = player->GfxPos;
3726     else
3727       syy = player->GfxPos;
3728   }
3729
3730   if (!setup.soft_scrolling && ScreenMovPos)
3731     sxx = syy = 0;
3732
3733   if (player_is_opaque)
3734     DrawGraphicShifted(sx, sy, sxx, syy, graphic, frame,NO_CUTTING,NO_MASKING);
3735   else
3736     DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
3737
3738   if (SHIELD_ON(player))
3739   {
3740     int graphic = (player->shield_deadly_time_left ? IMG_SHIELD_DEADLY_ACTIVE :
3741                    IMG_SHIELD_NORMAL_ACTIVE);
3742     int frame = getGraphicAnimationFrame(graphic, -1);
3743
3744     DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
3745   }
3746 #endif
3747
3748   /* ----------------------------------------------------------------------- */
3749   /* draw things in front of player (active dynamite or dynabombs)           */
3750   /* ----------------------------------------------------------------------- */
3751
3752   if (IS_ACTIVE_BOMB(element))
3753   {
3754     graphic = el2img(element);
3755     frame = getGraphicAnimationFrame(graphic, GfxFrame[jx][jy]);
3756
3757     if (game.emulation == EMU_SUPAPLEX)
3758       DrawGraphic(sx, sy, IMG_SP_DISK_RED, frame);
3759     else
3760       DrawGraphicThruMask(sx, sy, graphic, frame);
3761   }
3762
3763   if (player_is_moving && last_element == EL_EXPLOSION)
3764   {
3765     int element = (GfxElement[last_jx][last_jy] != EL_UNDEFINED ?
3766                    GfxElement[last_jx][last_jy] :  EL_EMPTY);
3767     int graphic = el_act2img(element, ACTION_EXPLODING);
3768     int delay = (game.emulation == EMU_SUPAPLEX ? 3 : 2);
3769     int phase = ExplodePhase[last_jx][last_jy] - 1;
3770     int frame = getGraphicAnimationFrame(graphic, phase - delay);
3771
3772     if (phase >= delay)
3773       DrawGraphicThruMask(SCREENX(last_jx), SCREENY(last_jy), graphic, frame);
3774   }
3775
3776   /* ----------------------------------------------------------------------- */
3777   /* draw elements the player is just walking/passing through/under          */
3778   /* ----------------------------------------------------------------------- */
3779
3780   if (player_is_moving)
3781   {
3782     /* handle the field the player is leaving ... */
3783     if (IS_ACCESSIBLE_INSIDE(last_element))
3784       DrawLevelField(last_jx, last_jy);
3785     else if (IS_ACCESSIBLE_UNDER(last_element))
3786       DrawLevelFieldThruMask(last_jx, last_jy);
3787   }
3788
3789   /* do not redraw accessible elements if the player is just pushing them */
3790   if (!player_is_moving || !player->is_pushing)
3791   {
3792     /* ... and the field the player is entering */
3793     if (IS_ACCESSIBLE_INSIDE(element))
3794       DrawLevelField(jx, jy);
3795     else if (IS_ACCESSIBLE_UNDER(element))
3796       DrawLevelFieldThruMask(jx, jy);
3797   }
3798
3799   MarkTileDirty(sx, sy);
3800 }
3801
3802 /* ------------------------------------------------------------------------- */
3803
3804 void WaitForEventToContinue()
3805 {
3806   boolean still_wait = TRUE;
3807
3808   /* simulate releasing mouse button over last gadget, if still pressed */
3809   if (button_status)
3810     HandleGadgets(-1, -1, 0);
3811
3812   button_status = MB_RELEASED;
3813
3814 #if 1
3815   ClearEventQueue();
3816 #endif
3817
3818   while (still_wait)
3819   {
3820     if (PendingEvent())
3821     {
3822       Event event;
3823
3824       NextEvent(&event);
3825
3826       switch (event.type)
3827       {
3828         case EVENT_BUTTONPRESS:
3829         case EVENT_KEYPRESS:
3830           still_wait = FALSE;
3831           break;
3832
3833         case EVENT_KEYRELEASE:
3834           ClearPlayerAction();
3835           break;
3836
3837         default:
3838           HandleOtherEvents(&event);
3839           break;
3840       }
3841     }
3842     else if (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED)
3843     {
3844       still_wait = FALSE;
3845     }
3846
3847     DoAnimation();
3848
3849     /* don't eat all CPU time */
3850     Delay(10);
3851   }
3852 }
3853
3854 #define MAX_REQUEST_LINES               13
3855 #define MAX_REQUEST_LINE_FONT1_LEN      7
3856 #define MAX_REQUEST_LINE_FONT2_LEN      10
3857
3858 boolean Request(char *text, unsigned int req_state)
3859 {
3860   int mx, my, ty, result = -1;
3861   unsigned int old_door_state;
3862   int last_game_status = game_status;   /* save current game status */
3863   int max_request_line_len = MAX_REQUEST_LINE_FONT1_LEN;
3864   int font_nr = FONT_TEXT_2;
3865 #if 0
3866   int max_word_len = 0;
3867 #endif
3868   char *text_ptr;
3869   int i;
3870
3871 #if 1
3872   global.use_envelope_request = 0;
3873 #endif
3874
3875 #if 1
3876   if (maxWordLengthInString(text) > MAX_REQUEST_LINE_FONT1_LEN)
3877   {
3878     max_request_line_len = MAX_REQUEST_LINE_FONT2_LEN;
3879     font_nr = FONT_TEXT_1;
3880   }
3881 #else
3882   for (text_ptr = text; *text_ptr; text_ptr++)
3883   {
3884     max_word_len = (*text_ptr != ' ' ? max_word_len + 1 : 0);
3885
3886     if (max_word_len > MAX_REQUEST_LINE_FONT1_LEN)
3887     {
3888       max_request_line_len = MAX_REQUEST_LINE_FONT2_LEN;
3889 #if 1
3890       font_nr = FONT_TEXT_1;
3891 #else
3892       font_nr = FONT_LEVEL_NUMBER;
3893 #endif
3894
3895       break;
3896     }
3897   }
3898 #endif
3899
3900   if (game_status == GAME_MODE_PLAYING)
3901   {
3902     if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
3903       BlitScreenToBitmap_EM(backbuffer);
3904     else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
3905       BlitScreenToBitmap_SP(backbuffer);
3906   }
3907
3908   /* disable deactivated drawing when quick-loading level tape recording */
3909   if (tape.playing && tape.deactivate_display)
3910     TapeDeactivateDisplayOff(TRUE);
3911
3912   SetMouseCursor(CURSOR_DEFAULT);
3913
3914 #if defined(NETWORK_AVALIABLE)
3915   /* pause network game while waiting for request to answer */
3916   if (options.network &&
3917       game_status == GAME_MODE_PLAYING &&
3918       req_state & REQUEST_WAIT_FOR_INPUT)
3919     SendToServer_PausePlaying();
3920 #endif
3921
3922   old_door_state = GetDoorState();
3923
3924   /* simulate releasing mouse button over last gadget, if still pressed */
3925   if (button_status)
3926     HandleGadgets(-1, -1, 0);
3927
3928   UnmapAllGadgets();
3929
3930   /* draw released gadget before proceeding */
3931   // BackToFront();
3932
3933 #if 0
3934   if (old_door_state & DOOR_OPEN_1 && !global.use_envelope_request)
3935 #else
3936   if (old_door_state & DOOR_OPEN_1)
3937 #endif
3938   {
3939 #if 1
3940     if (!global.use_envelope_request)
3941       CloseDoor(DOOR_CLOSE_1);
3942 #else
3943     CloseDoor(DOOR_CLOSE_1);
3944 #endif
3945
3946     /* save old door content */
3947     BlitBitmap(bitmap_db_door, bitmap_db_door,
3948                DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE,
3949                DOOR_GFX_PAGEX2, DOOR_GFX_PAGEY1);
3950   }
3951
3952 #if 1
3953   SetDoorBackgroundImage(IMG_BACKGROUND_DOOR);
3954 #endif
3955
3956   SetDrawBackgroundMask(REDRAW_FIELD | REDRAW_DOOR_1);
3957
3958   /* clear door drawing field */
3959   DrawBackground(DX, DY, DXSIZE, DYSIZE);
3960
3961   /* force DOOR font inside door area */
3962   game_status = GAME_MODE_PSEUDO_DOOR;
3963
3964   /* write text for request */
3965   for (text_ptr = text, ty = 0; ty < MAX_REQUEST_LINES; ty++)
3966   {
3967     char text_line[max_request_line_len + 1];
3968     int tx, tl, tc = 0;
3969
3970     if (!*text_ptr)
3971       break;
3972
3973     for (tl = 0, tx = 0; tx < max_request_line_len; tl++, tx++)
3974     {
3975       tc = *(text_ptr + tx);
3976       if (!tc || tc == ' ')
3977         break;
3978     }
3979
3980     if (!tl)
3981     { 
3982       text_ptr++; 
3983       ty--; 
3984       continue; 
3985     }
3986
3987     strncpy(text_line, text_ptr, tl);
3988     text_line[tl] = 0;
3989
3990     DrawText(DX + (DXSIZE - tl * getFontWidth(font_nr)) / 2,
3991              DY + 8 + ty * (getFontHeight(font_nr) + 2),
3992              text_line, font_nr);
3993
3994     text_ptr += tl + (tc == ' ' ? 1 : 0);
3995   }
3996
3997   game_status = last_game_status;       /* restore current game status */
3998
3999 #if 1
4000   if (global.use_envelope_request)
4001   {
4002     /* !!! TMP !!! */
4003     FreeToolButtons();
4004     CreateToolButtons();
4005   }
4006 #endif
4007
4008   if (req_state & REQ_ASK)
4009   {
4010     MapGadget(tool_gadget[TOOL_CTRL_ID_YES]);
4011     MapGadget(tool_gadget[TOOL_CTRL_ID_NO]);
4012   }
4013   else if (req_state & REQ_CONFIRM)
4014   {
4015     MapGadget(tool_gadget[TOOL_CTRL_ID_CONFIRM]);
4016   }
4017   else if (req_state & REQ_PLAYER)
4018   {
4019     MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_1]);
4020     MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_2]);
4021     MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_3]);
4022     MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_4]);
4023   }
4024
4025   /* copy request gadgets to door backbuffer */
4026   BlitBitmap(drawto, bitmap_db_door,
4027              DX, DY, DXSIZE, DYSIZE,
4028              DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
4029
4030 #if 1
4031   if (global.use_envelope_request)
4032   {
4033     ShowEnvelopeDoor(text, ACTION_OPENING);
4034
4035     for (i = 0; i < NUM_TOOL_BUTTONS; i++)
4036     {
4037       if ((req_state & REQ_ASK && (i == TOOL_CTRL_ID_YES ||
4038                                    i == TOOL_CTRL_ID_NO)) ||
4039           (req_state & REQ_CONFIRM && i == TOOL_CTRL_ID_CONFIRM) ||
4040           (req_state & REQ_PLAYER && (i == TOOL_CTRL_ID_PLAYER_1 &&
4041                                       i == TOOL_CTRL_ID_PLAYER_2 &&
4042                                       i == TOOL_CTRL_ID_PLAYER_3 &&
4043                                       i == TOOL_CTRL_ID_PLAYER_4)))
4044       {
4045         int x = tool_gadget[i]->x + dDX;
4046         int y = tool_gadget[i]->y + dDY;
4047
4048         ModifyGadget(tool_gadget[i], GDI_X, x, GDI_Y, y, GDI_END);
4049       }
4050     }
4051   }
4052 #endif
4053
4054 #if 1
4055   if (!global.use_envelope_request)
4056     OpenDoor(DOOR_OPEN_1);
4057 #else
4058   OpenDoor(DOOR_OPEN_1);
4059 #endif
4060
4061   if (!(req_state & REQUEST_WAIT_FOR_INPUT))
4062   {
4063     if (game_status == GAME_MODE_PLAYING)
4064     {
4065       SetPanelBackground();
4066       SetDrawBackgroundMask(REDRAW_DOOR_1);
4067     }
4068     else
4069     {
4070       SetDrawBackgroundMask(REDRAW_FIELD);
4071     }
4072
4073     return FALSE;
4074   }
4075
4076 #if 1
4077   if (game_status != GAME_MODE_MAIN && !global.use_envelope_request)
4078     InitAnimation();
4079 #else
4080   if (game_status != GAME_MODE_MAIN)
4081     InitAnimation();
4082 #endif
4083
4084   button_status = MB_RELEASED;
4085
4086   request_gadget_id = -1;
4087
4088   SetDrawBackgroundMask(REDRAW_FIELD | REDRAW_DOOR_1);
4089
4090   while (result < 0)
4091   {
4092     if (PendingEvent())
4093     {
4094       Event event;
4095
4096       NextEvent(&event);
4097
4098       switch (event.type)
4099       {
4100         case EVENT_BUTTONPRESS:
4101         case EVENT_BUTTONRELEASE:
4102         case EVENT_MOTIONNOTIFY:
4103         {
4104           if (event.type == EVENT_MOTIONNOTIFY)
4105           {
4106             if (!PointerInWindow(window))
4107               continue; /* window and pointer are on different screens */
4108
4109             if (!button_status)
4110               continue;
4111
4112             motion_status = TRUE;
4113             mx = ((MotionEvent *) &event)->x;
4114             my = ((MotionEvent *) &event)->y;
4115           }
4116           else
4117           {
4118             motion_status = FALSE;
4119             mx = ((ButtonEvent *) &event)->x;
4120             my = ((ButtonEvent *) &event)->y;
4121             if (event.type == EVENT_BUTTONPRESS)
4122               button_status = ((ButtonEvent *) &event)->button;
4123             else
4124               button_status = MB_RELEASED;
4125           }
4126
4127           /* this sets 'request_gadget_id' */
4128           HandleGadgets(mx, my, button_status);
4129
4130           switch (request_gadget_id)
4131           {
4132             case TOOL_CTRL_ID_YES:
4133               result = TRUE;
4134               break;
4135             case TOOL_CTRL_ID_NO:
4136               result = FALSE;
4137               break;
4138             case TOOL_CTRL_ID_CONFIRM:
4139               result = TRUE | FALSE;
4140               break;
4141
4142             case TOOL_CTRL_ID_PLAYER_1:
4143               result = 1;
4144               break;
4145             case TOOL_CTRL_ID_PLAYER_2:
4146               result = 2;
4147               break;
4148             case TOOL_CTRL_ID_PLAYER_3:
4149               result = 3;
4150               break;
4151             case TOOL_CTRL_ID_PLAYER_4:
4152               result = 4;
4153               break;
4154
4155             default:
4156               break;
4157           }
4158
4159           break;
4160         }
4161
4162         case EVENT_KEYPRESS:
4163           switch (GetEventKey((KeyEvent *)&event, TRUE))
4164           {
4165             case KSYM_space:
4166               if (req_state & REQ_CONFIRM)
4167                 result = 1;
4168               break;
4169
4170             case KSYM_Return:
4171               result = 1;
4172               break;
4173
4174             case KSYM_Escape:
4175             case KSYM_Back:
4176               result = 0;
4177               break;
4178
4179             default:
4180               break;
4181           }
4182
4183           if (req_state & REQ_PLAYER)
4184             result = 0;
4185           break;
4186
4187         case EVENT_KEYRELEASE:
4188           ClearPlayerAction();
4189           break;
4190
4191         default:
4192           HandleOtherEvents(&event);
4193           break;
4194       }
4195     }
4196     else if (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED)
4197     {
4198       int joy = AnyJoystick();
4199
4200       if (joy & JOY_BUTTON_1)
4201         result = 1;
4202       else if (joy & JOY_BUTTON_2)
4203         result = 0;
4204     }
4205
4206 #if 1
4207
4208     if (game_status == GAME_MODE_PLAYING && local_player->LevelSolved_GameEnd)
4209     {
4210       HandleGameActions();
4211     }
4212     else
4213     {
4214       DoAnimation();
4215
4216       if (!PendingEvent())      /* delay only if no pending events */
4217         Delay(10);
4218     }
4219
4220 #if 1
4221     game_status = GAME_MODE_PSEUDO_DOOR;
4222 #endif
4223
4224     BackToFront();
4225
4226 #if 1
4227     game_status = last_game_status;     /* restore current game status */
4228 #endif
4229
4230 #else
4231
4232     DoAnimation();
4233
4234 #if 1
4235     if (!PendingEvent())        /* delay only if no pending events */
4236       Delay(10);
4237 #else
4238     /* don't eat all CPU time */
4239     Delay(10);
4240 #endif
4241
4242 #endif
4243   }
4244
4245   if (game_status != GAME_MODE_MAIN)
4246     StopAnimation();
4247
4248   UnmapToolButtons();
4249
4250 #if 1
4251   if (global.use_envelope_request)
4252     ShowEnvelopeDoor(text, ACTION_CLOSING);
4253 #endif
4254
4255 #if 1
4256   if (!(req_state & REQ_STAY_OPEN) && !global.use_envelope_request)
4257 #else
4258   if (!(req_state & REQ_STAY_OPEN))
4259 #endif
4260   {
4261     CloseDoor(DOOR_CLOSE_1);
4262
4263     if (((old_door_state & DOOR_OPEN_1) && !(req_state & REQ_STAY_CLOSED)) ||
4264         (req_state & REQ_REOPEN))
4265       OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
4266   }
4267
4268   RemapAllGadgets();
4269
4270   if (game_status == GAME_MODE_PLAYING)
4271   {
4272     SetPanelBackground();
4273     SetDrawBackgroundMask(REDRAW_DOOR_1);
4274   }
4275   else
4276   {
4277     SetDrawBackgroundMask(REDRAW_FIELD);
4278   }
4279
4280 #if defined(NETWORK_AVALIABLE)
4281   /* continue network game after request */
4282   if (options.network &&
4283       game_status == GAME_MODE_PLAYING &&
4284       req_state & REQUEST_WAIT_FOR_INPUT)
4285     SendToServer_ContinuePlaying();
4286 #endif
4287
4288   /* restore deactivated drawing when quick-loading level tape recording */
4289   if (tape.playing && tape.deactivate_display)
4290     TapeDeactivateDisplayOn();
4291
4292   return result;
4293 }
4294
4295 unsigned int OpenDoor(unsigned int door_state)
4296 {
4297   if (door_state & DOOR_COPY_BACK)
4298   {
4299     if (door_state & DOOR_OPEN_1)
4300       BlitBitmap(bitmap_db_door, bitmap_db_door,
4301                  DOOR_GFX_PAGEX2, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE,
4302                  DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
4303
4304     if (door_state & DOOR_OPEN_2)
4305       BlitBitmap(bitmap_db_door, bitmap_db_door,
4306                  DOOR_GFX_PAGEX2, DOOR_GFX_PAGEY2, VXSIZE, VYSIZE,
4307                  DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2);
4308
4309     door_state &= ~DOOR_COPY_BACK;
4310   }
4311
4312   return MoveDoor(door_state);
4313 }
4314
4315 unsigned int CloseDoor(unsigned int door_state)
4316 {
4317   unsigned int old_door_state = GetDoorState();
4318
4319   if (!(door_state & DOOR_NO_COPY_BACK))
4320   {
4321     if (old_door_state & DOOR_OPEN_1)
4322       BlitBitmap(backbuffer, bitmap_db_door,
4323                  DX, DY, DXSIZE, DYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
4324
4325     if (old_door_state & DOOR_OPEN_2)
4326       BlitBitmap(backbuffer, bitmap_db_door,
4327                  VX, VY, VXSIZE, VYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2);
4328
4329     door_state &= ~DOOR_NO_COPY_BACK;
4330   }
4331
4332   return MoveDoor(door_state);
4333 }
4334
4335 unsigned int GetDoorState()
4336 {
4337   return MoveDoor(DOOR_GET_STATE);
4338 }
4339
4340 unsigned int SetDoorState(unsigned int door_state)
4341 {
4342   return MoveDoor(door_state | DOOR_SET_STATE);
4343 }
4344
4345 unsigned int MoveDoor(unsigned int door_state)
4346 {
4347   static int door1 = DOOR_OPEN_1;
4348   static int door2 = DOOR_CLOSE_2;
4349   unsigned int door_delay = 0;
4350   unsigned int door_delay_value;
4351   int stepsize = 1;
4352
4353   if (door_1.width < 0 || door_1.width > DXSIZE)
4354     door_1.width = DXSIZE;
4355   if (door_1.height < 0 || door_1.height > DYSIZE)
4356     door_1.height = DYSIZE;
4357   if (door_2.width < 0 || door_2.width > VXSIZE)
4358     door_2.width = VXSIZE;
4359   if (door_2.height < 0 || door_2.height > VYSIZE)
4360     door_2.height = VYSIZE;
4361
4362   if (door_state == DOOR_GET_STATE)
4363     return (door1 | door2);
4364
4365   if (door_state & DOOR_SET_STATE)
4366   {
4367     if (door_state & DOOR_ACTION_1)
4368       door1 = door_state & DOOR_ACTION_1;
4369     if (door_state & DOOR_ACTION_2)
4370       door2 = door_state & DOOR_ACTION_2;
4371
4372     return (door1 | door2);
4373   }
4374
4375   if (!(door_state & DOOR_FORCE_REDRAW))
4376   {
4377     if (door1 == DOOR_OPEN_1 && door_state & DOOR_OPEN_1)
4378       door_state &= ~DOOR_OPEN_1;
4379     else if (door1 == DOOR_CLOSE_1 && door_state & DOOR_CLOSE_1)
4380       door_state &= ~DOOR_CLOSE_1;
4381     if (door2 == DOOR_OPEN_2 && door_state & DOOR_OPEN_2)
4382       door_state &= ~DOOR_OPEN_2;
4383     else if (door2 == DOOR_CLOSE_2 && door_state & DOOR_CLOSE_2)
4384       door_state &= ~DOOR_CLOSE_2;
4385   }
4386
4387   door_delay_value = (door_state & DOOR_ACTION_1 ? door_1.step_delay :
4388                       door_2.step_delay);
4389
4390   if (setup.quick_doors)
4391   {
4392     stepsize = 20;              /* must be chosen to always draw last frame */
4393     door_delay_value = 0;
4394   }
4395
4396   if (global.autoplay_leveldir)
4397   {
4398     door_state |= DOOR_NO_DELAY;
4399     door_state &= ~DOOR_CLOSE_ALL;
4400   }
4401
4402 #if 1
4403   if (game_status == GAME_MODE_EDITOR)
4404     door_state |= DOOR_NO_DELAY;
4405 #endif
4406
4407   if (door_state & DOOR_ACTION)
4408   {
4409     boolean handle_door_1 = (door_state & DOOR_ACTION_1);
4410     boolean handle_door_2 = (door_state & DOOR_ACTION_2);
4411     boolean door_1_done = (!handle_door_1);
4412     boolean door_2_done = (!handle_door_2);
4413     boolean door_1_vertical = (door_1.anim_mode & ANIM_VERTICAL);
4414     boolean door_2_vertical = (door_2.anim_mode & ANIM_VERTICAL);
4415     int door_size_1 = (door_1_vertical ? door_1.height : door_1.width);
4416     int door_size_2 = (door_2_vertical ? door_2.height : door_2.width);
4417     int max_door_size_1 = (door_1_vertical ? DYSIZE : DXSIZE);
4418     int max_door_size_2 = (door_2_vertical ? VYSIZE : VXSIZE);
4419     int door_size     = (handle_door_1 ? door_size_1     : door_size_2);
4420     int max_door_size = (handle_door_1 ? max_door_size_1 : max_door_size_2);
4421     int door_skip = max_door_size - door_size;
4422     int end = door_size;
4423     int start = ((door_state & DOOR_NO_DELAY) ? end : 0);
4424     int k;
4425
4426     if (!(door_state & DOOR_NO_DELAY) && !setup.quick_doors)
4427     {
4428       /* opening door sound has priority over simultaneously closing door */
4429       if (door_state & (DOOR_OPEN_1 | DOOR_OPEN_2))
4430         PlayMenuSoundStereo(SND_DOOR_OPENING, SOUND_MIDDLE);
4431       else if (door_state & (DOOR_CLOSE_1 | DOOR_CLOSE_2))
4432         PlayMenuSoundStereo(SND_DOOR_CLOSING, SOUND_MIDDLE);
4433     }
4434
4435     for (k = start; k <= end && !(door_1_done && door_2_done); k += stepsize)
4436     {
4437       int x = k;
4438       Bitmap *bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
4439       GC gc = bitmap->stored_clip_gc;
4440
4441       if (door_state & DOOR_ACTION_1)
4442       {
4443         int a = MIN(x * door_1.step_offset, end);
4444         int p = (door_state & DOOR_OPEN_1 ? end - a : a);
4445         int i = p + door_skip;
4446
4447         if (door_1.anim_mode & ANIM_STATIC_PANEL)
4448         {
4449           BlitBitmap(bitmap_db_door, drawto,
4450                      DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1,
4451                      DXSIZE, DYSIZE, DX, DY);
4452         }
4453         else if (x <= a)
4454         {
4455           BlitBitmap(bitmap_db_door, drawto,
4456                      DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1 + p / 2,
4457                      DXSIZE, DYSIZE - p / 2, DX, DY);
4458
4459           ClearRectangle(drawto, DX, DY + DYSIZE - p / 2, DXSIZE, p / 2);
4460         }
4461
4462         if (door_1.anim_mode & ANIM_HORIZONTAL && x <= DXSIZE)
4463         {
4464           int src1_x = DXSIZE,          src1_y = DOOR_GFX_PAGEY1;
4465           int dst1_x = DX + DXSIZE - i, dst1_y = DY;
4466           int src2_x = DXSIZE - i,      src2_y = DOOR_GFX_PAGEY1;
4467           int dst2_x = DX,              dst2_y = DY;
4468           int width = i, height = DYSIZE;
4469
4470           SetClipOrigin(bitmap, gc, dst1_x - src1_x, dst1_y - src1_y);
4471           BlitBitmapMasked(bitmap, drawto, src1_x, src1_y, width, height,
4472                            dst1_x, dst1_y);
4473
4474           SetClipOrigin(bitmap, gc, dst2_x - src2_x, dst2_y - src2_y);
4475           BlitBitmapMasked(bitmap, drawto, src2_x, src2_y, width, height,
4476                            dst2_x, dst2_y);
4477         }
4478         else if (door_1.anim_mode & ANIM_VERTICAL && x <= DYSIZE)
4479         {
4480           int src1_x = DXSIZE,          src1_y = DOOR_GFX_PAGEY1;
4481           int dst1_x = DX,              dst1_y = DY + DYSIZE - i;
4482           int src2_x = 0,               src2_y = DOOR_GFX_PAGEY1 + DYSIZE - i;
4483           int dst2_x = DX,              dst2_y = DY;
4484           int width = DXSIZE, height = i;
4485
4486           SetClipOrigin(bitmap, gc, dst1_x - src1_x, dst1_y - src1_y);
4487           BlitBitmapMasked(bitmap, drawto, src1_x, src1_y, width, height,
4488                            dst1_x, dst1_y);
4489
4490           SetClipOrigin(bitmap, gc, dst2_x - src2_x, dst2_y - src2_y);
4491           BlitBitmapMasked(bitmap, drawto, src2_x, src2_y, width, height,
4492                            dst2_x, dst2_y);
4493         }
4494         else if (x <= DXSIZE)   /* ANIM_DEFAULT */
4495         {
4496           int j = (door_1.anim_mode == ANIM_DEFAULT ? (DXSIZE - i) / 3 : 0);
4497
4498           SetClipOrigin(bitmap, gc, DX - i, (DY + j) - DOOR_GFX_PAGEY1);
4499           BlitBitmapMasked(bitmap, drawto,
4500                            DXSIZE, DOOR_GFX_PAGEY1, i, 77,
4501                            DX + DXSIZE - i, DY + j);
4502           BlitBitmapMasked(bitmap, drawto,
4503                            DXSIZE, DOOR_GFX_PAGEY1 + 140, i, 63,
4504                            DX + DXSIZE - i, DY + 140 + j);
4505           SetClipOrigin(bitmap, gc, DX - DXSIZE + i,
4506                         DY - (DOOR_GFX_PAGEY1 + j));
4507           BlitBitmapMasked(bitmap, drawto,
4508                            DXSIZE - i, DOOR_GFX_PAGEY1 + j, i, 77 - j,
4509                            DX, DY);
4510           BlitBitmapMasked(bitmap, drawto,
4511                            DXSIZE-i, DOOR_GFX_PAGEY1 + 140, i, 63,
4512                            DX, DY + 140 - j);
4513
4514           BlitBitmapMasked(bitmap, drawto,
4515                            DXSIZE - i, DOOR_GFX_PAGEY1 + 77, i, 63,
4516                            DX, DY + 77 - j);
4517           BlitBitmapMasked(bitmap, drawto,
4518                            DXSIZE - i, DOOR_GFX_PAGEY1 + 203, i, 77,
4519                            DX, DY + 203 - j);
4520           SetClipOrigin(bitmap, gc, DX - i, (DY + j) - DOOR_GFX_PAGEY1);
4521           BlitBitmapMasked(bitmap, drawto,
4522                            DXSIZE, DOOR_GFX_PAGEY1 + 77, i, 63,
4523                            DX + DXSIZE - i, DY + 77 + j);
4524           BlitBitmapMasked(bitmap, drawto,
4525                            DXSIZE, DOOR_GFX_PAGEY1 + 203, i, 77 - j,
4526                            DX + DXSIZE - i, DY + 203 + j);
4527         }
4528
4529         redraw_mask |= REDRAW_DOOR_1;
4530         door_1_done = (a == end);
4531       }
4532
4533       if (door_state & DOOR_ACTION_2)
4534       {
4535         int a = MIN(x * door_2.step_offset, door_size);
4536         int p = (door_state & DOOR_OPEN_2 ? door_size - a : a);
4537         int i = p + door_skip;
4538
4539         if (door_2.anim_mode & ANIM_STATIC_PANEL)
4540         {
4541           BlitBitmap(bitmap_db_door, drawto,
4542                      DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2,
4543                      VXSIZE, VYSIZE, VX, VY);
4544         }
4545         else if (x <= VYSIZE)
4546         {
4547           BlitBitmap(bitmap_db_door, drawto,
4548                      DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2 + p / 2,
4549                      VXSIZE, VYSIZE - p / 2, VX, VY);
4550
4551           ClearRectangle(drawto, VX, VY + VYSIZE - p / 2, VXSIZE, p / 2);
4552         }
4553
4554         if (door_2.anim_mode & ANIM_HORIZONTAL && x <= VXSIZE)
4555         {
4556           int src1_x = VXSIZE,          src1_y = DOOR_GFX_PAGEY2;
4557           int dst1_x = VX + VXSIZE - i, dst1_y = VY;
4558           int src2_x = VXSIZE - i,      src2_y = DOOR_GFX_PAGEY2;
4559           int dst2_x = VX,              dst2_y = VY;
4560           int width = i, height = VYSIZE;
4561
4562           SetClipOrigin(bitmap, gc, dst1_x - src1_x, dst1_y - src1_y);
4563           BlitBitmapMasked(bitmap, drawto, src1_x, src1_y, width, height,
4564                            dst1_x, dst1_y);
4565
4566           SetClipOrigin(bitmap, gc, dst2_x - src2_x, dst2_y - src2_y);
4567           BlitBitmapMasked(bitmap, drawto, src2_x, src2_y, width, height,
4568                            dst2_x, dst2_y);
4569         }
4570         else if (door_2.anim_mode & ANIM_VERTICAL && x <= VYSIZE)
4571         {
4572           int src1_x = VXSIZE,          src1_y = DOOR_GFX_PAGEY2;
4573           int dst1_x = VX,              dst1_y = VY + VYSIZE - i;
4574           int src2_x = 0,               src2_y = DOOR_GFX_PAGEY2 + VYSIZE - i;
4575           int dst2_x = VX,              dst2_y = VY;
4576           int width = VXSIZE, height = i;
4577
4578           SetClipOrigin(bitmap, gc, dst1_x - src1_x, dst1_y - src1_y);
4579           BlitBitmapMasked(bitmap, drawto, src1_x, src1_y, width, height,
4580                            dst1_x, dst1_y);
4581
4582           SetClipOrigin(bitmap, gc, dst2_x - src2_x, dst2_y - src2_y);
4583           BlitBitmapMasked(bitmap, drawto, src2_x, src2_y, width, height,
4584                            dst2_x, dst2_y);
4585         }
4586         else if (x <= VXSIZE)   /* ANIM_DEFAULT */
4587         {
4588           int j = (door_2.anim_mode == ANIM_DEFAULT ? (VXSIZE - i) / 3 : 0);
4589
4590           SetClipOrigin(bitmap, gc, VX - i, (VY + j) - DOOR_GFX_PAGEY2);
4591           BlitBitmapMasked(bitmap, drawto,
4592                            VXSIZE, DOOR_GFX_PAGEY2, i, VYSIZE / 2,
4593                            VX + VXSIZE - i, VY + j);
4594           SetClipOrigin(bitmap, gc,
4595                         VX - VXSIZE + i, VY - (DOOR_GFX_PAGEY2 + j));
4596           BlitBitmapMasked(bitmap, drawto,
4597                            VXSIZE - i, DOOR_GFX_PAGEY2 + j, i, VYSIZE / 2 - j,
4598                            VX, VY);
4599
4600           BlitBitmapMasked(bitmap, drawto,
4601                            VXSIZE - i, DOOR_GFX_PAGEY2 + VYSIZE / 2,
4602                            i, VYSIZE / 2, VX, VY + VYSIZE / 2 - j);
4603           SetClipOrigin(bitmap, gc, VX - i, (VY + j) - DOOR_GFX_PAGEY2);
4604           BlitBitmapMasked(bitmap, drawto,
4605                            VXSIZE, DOOR_GFX_PAGEY2 + VYSIZE / 2,
4606                            i, VYSIZE / 2 - j,
4607                            VX + VXSIZE - i, VY + VYSIZE / 2 + j);
4608         }
4609
4610         redraw_mask |= REDRAW_DOOR_2;
4611         door_2_done = (a == VXSIZE);
4612       }
4613
4614       if (!(door_state & DOOR_NO_DELAY))
4615       {
4616         BackToFront();
4617
4618         if (game_status == GAME_MODE_MAIN)
4619           DoAnimation();
4620
4621         WaitUntilDelayReached(&door_delay, door_delay_value);
4622       }
4623     }
4624   }
4625
4626   if (door_state & DOOR_ACTION_1)
4627     door1 = door_state & DOOR_ACTION_1;
4628   if (door_state & DOOR_ACTION_2)
4629     door2 = door_state & DOOR_ACTION_2;
4630
4631   return (door1 | door2);
4632 }
4633
4634 void DrawSpecialEditorDoor()
4635 {
4636   /* draw bigger toolbox window */
4637   BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
4638              DOOR_GFX_PAGEX7, 0, EXSIZE + 8, 8,
4639              EX - 4, EY - 12);
4640   BlitBitmap(graphic_info[IMG_GLOBAL_BORDER].bitmap, drawto,
4641              EX - 6, VY - 4, EXSIZE + 12, EYSIZE - VYSIZE + 4,
4642              EX - 6, EY - 4);
4643
4644   redraw_mask |= REDRAW_ALL;
4645 }
4646
4647 void UndrawSpecialEditorDoor()
4648 {
4649   /* draw normal tape recorder window */
4650   BlitBitmap(graphic_info[IMG_GLOBAL_BORDER].bitmap, drawto,
4651              EX - 6, EY - 12, EXSIZE + 12, EYSIZE - VYSIZE + 12,
4652              EX - 6, EY - 12);
4653
4654   redraw_mask |= REDRAW_ALL;
4655 }
4656
4657
4658 /* ---------- new tool button stuff ---------------------------------------- */
4659
4660 /* graphic position values for tool buttons */
4661 #define TOOL_BUTTON_YES_XPOS            2
4662 #define TOOL_BUTTON_YES_YPOS            250
4663 #define TOOL_BUTTON_YES_GFX_YPOS        0
4664 #define TOOL_BUTTON_YES_XSIZE           46
4665 #define TOOL_BUTTON_YES_YSIZE           28
4666 #define TOOL_BUTTON_NO_XPOS             52
4667 #define TOOL_BUTTON_NO_YPOS             TOOL_BUTTON_YES_YPOS
4668 #define TOOL_BUTTON_NO_GFX_YPOS         TOOL_BUTTON_YES_GFX_YPOS
4669 #define TOOL_BUTTON_NO_XSIZE            TOOL_BUTTON_YES_XSIZE
4670 #define TOOL_BUTTON_NO_YSIZE            TOOL_BUTTON_YES_YSIZE
4671 #define TOOL_BUTTON_CONFIRM_XPOS        TOOL_BUTTON_YES_XPOS
4672 #define TOOL_BUTTON_CONFIRM_YPOS        TOOL_BUTTON_YES_YPOS
4673 #define TOOL_BUTTON_CONFIRM_GFX_YPOS    30
4674 #define TOOL_BUTTON_CONFIRM_XSIZE       96
4675 #define TOOL_BUTTON_CONFIRM_YSIZE       TOOL_BUTTON_YES_YSIZE
4676 #define TOOL_BUTTON_PLAYER_XSIZE        30
4677 #define TOOL_BUTTON_PLAYER_YSIZE        30
4678 #define TOOL_BUTTON_PLAYER_GFX_XPOS     5
4679 #define TOOL_BUTTON_PLAYER_GFX_YPOS     185
4680 #define TOOL_BUTTON_PLAYER_XPOS         (5 + TOOL_BUTTON_PLAYER_XSIZE / 2)
4681 #define TOOL_BUTTON_PLAYER_YPOS         (215 - TOOL_BUTTON_PLAYER_YSIZE / 2)
4682 #define TOOL_BUTTON_PLAYER1_XPOS        (TOOL_BUTTON_PLAYER_XPOS \
4683                                          + 0 * TOOL_BUTTON_PLAYER_XSIZE)
4684 #define TOOL_BUTTON_PLAYER2_XPOS        (TOOL_BUTTON_PLAYER_XPOS \
4685                                          + 1 * TOOL_BUTTON_PLAYER_XSIZE)
4686 #define TOOL_BUTTON_PLAYER3_XPOS        (TOOL_BUTTON_PLAYER_XPOS \
4687                                          + 0 * TOOL_BUTTON_PLAYER_XSIZE)
4688 #define TOOL_BUTTON_PLAYER4_XPOS        (TOOL_BUTTON_PLAYER_XPOS \
4689                                          + 1 * TOOL_BUTTON_PLAYER_XSIZE)
4690 #define TOOL_BUTTON_PLAYER1_YPOS        (TOOL_BUTTON_PLAYER_YPOS \
4691                                          + 0 * TOOL_BUTTON_PLAYER_YSIZE)
4692 #define TOOL_BUTTON_PLAYER2_YPOS        (TOOL_BUTTON_PLAYER_YPOS \
4693                                          + 0 * TOOL_BUTTON_PLAYER_YSIZE)
4694 #define TOOL_BUTTON_PLAYER3_YPOS        (TOOL_BUTTON_PLAYER_YPOS \
4695                                          + 1 * TOOL_BUTTON_PLAYER_YSIZE)
4696 #define TOOL_BUTTON_PLAYER4_YPOS        (TOOL_BUTTON_PLAYER_YPOS \
4697                                          + 1 * TOOL_BUTTON_PLAYER_YSIZE)
4698
4699 static struct
4700 {
4701   int xpos, ypos;
4702   int x, y;
4703   int width, height;
4704   int gadget_id;
4705   char *infotext;
4706 } toolbutton_info[NUM_TOOL_BUTTONS] =
4707 {
4708   {
4709     TOOL_BUTTON_YES_XPOS,       TOOL_BUTTON_YES_GFX_YPOS,
4710     TOOL_BUTTON_YES_XPOS,       TOOL_BUTTON_YES_YPOS,
4711     TOOL_BUTTON_YES_XSIZE,      TOOL_BUTTON_YES_YSIZE,
4712     TOOL_CTRL_ID_YES,
4713     "yes"
4714   },
4715   {
4716     TOOL_BUTTON_NO_XPOS,        TOOL_BUTTON_NO_GFX_YPOS,
4717     TOOL_BUTTON_NO_XPOS,        TOOL_BUTTON_NO_YPOS,
4718     TOOL_BUTTON_NO_XSIZE,       TOOL_BUTTON_NO_YSIZE,
4719     TOOL_CTRL_ID_NO,
4720     "no"
4721   },
4722   {
4723     TOOL_BUTTON_CONFIRM_XPOS,   TOOL_BUTTON_CONFIRM_GFX_YPOS,
4724     TOOL_BUTTON_CONFIRM_XPOS,   TOOL_BUTTON_CONFIRM_YPOS,
4725     TOOL_BUTTON_CONFIRM_XSIZE,  TOOL_BUTTON_CONFIRM_YSIZE,
4726     TOOL_CTRL_ID_CONFIRM,
4727     "confirm"
4728   },
4729   {
4730     TOOL_BUTTON_PLAYER_GFX_XPOS,TOOL_BUTTON_PLAYER_GFX_YPOS,
4731     TOOL_BUTTON_PLAYER1_XPOS,   TOOL_BUTTON_PLAYER1_YPOS,
4732     TOOL_BUTTON_PLAYER_XSIZE,   TOOL_BUTTON_PLAYER_YSIZE,
4733     TOOL_CTRL_ID_PLAYER_1,
4734     "player 1"
4735   },
4736   {
4737     TOOL_BUTTON_PLAYER_GFX_XPOS,TOOL_BUTTON_PLAYER_GFX_YPOS,
4738     TOOL_BUTTON_PLAYER2_XPOS,   TOOL_BUTTON_PLAYER2_YPOS,
4739     TOOL_BUTTON_PLAYER_XSIZE,   TOOL_BUTTON_PLAYER_YSIZE,
4740     TOOL_CTRL_ID_PLAYER_2,
4741     "player 2"
4742   },
4743   {
4744     TOOL_BUTTON_PLAYER_GFX_XPOS,TOOL_BUTTON_PLAYER_GFX_YPOS,
4745     TOOL_BUTTON_PLAYER3_XPOS,   TOOL_BUTTON_PLAYER3_YPOS,
4746     TOOL_BUTTON_PLAYER_XSIZE,   TOOL_BUTTON_PLAYER_YSIZE,
4747     TOOL_CTRL_ID_PLAYER_3,
4748     "player 3"
4749   },
4750   {
4751     TOOL_BUTTON_PLAYER_GFX_XPOS,TOOL_BUTTON_PLAYER_GFX_YPOS,
4752     TOOL_BUTTON_PLAYER4_XPOS,   TOOL_BUTTON_PLAYER4_YPOS,
4753     TOOL_BUTTON_PLAYER_XSIZE,   TOOL_BUTTON_PLAYER_YSIZE,
4754     TOOL_CTRL_ID_PLAYER_4,
4755     "player 4"
4756   }
4757 };
4758
4759 void CreateToolButtons()
4760 {
4761   int i;
4762
4763   for (i = 0; i < NUM_TOOL_BUTTONS; i++)
4764   {
4765     Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
4766     Bitmap *deco_bitmap = None;
4767     int deco_x = 0, deco_y = 0, deco_xpos = 0, deco_ypos = 0;
4768     struct GadgetInfo *gi;
4769     unsigned int event_mask;
4770     int gd_xoffset, gd_yoffset;
4771     int gd_x1, gd_x2, gd_y;
4772     int id = i;
4773
4774     event_mask = GD_EVENT_RELEASED;
4775
4776     gd_xoffset = toolbutton_info[i].xpos;
4777     gd_yoffset = toolbutton_info[i].ypos;
4778     gd_x1 = DOOR_GFX_PAGEX4 + gd_xoffset;
4779     gd_x2 = DOOR_GFX_PAGEX3 + gd_xoffset;
4780     gd_y = DOOR_GFX_PAGEY1 + gd_yoffset;
4781
4782     if (id >= TOOL_CTRL_ID_PLAYER_1 && id <= TOOL_CTRL_ID_PLAYER_4)
4783     {
4784       int player_nr = id - TOOL_CTRL_ID_PLAYER_1;
4785
4786       getMiniGraphicSource(PLAYER_NR_GFX(IMG_PLAYER_1, player_nr),
4787                            &deco_bitmap, &deco_x, &deco_y);
4788       deco_xpos = (toolbutton_info[i].width - MINI_TILEX) / 2;
4789       deco_ypos = (toolbutton_info[i].height - MINI_TILEY) / 2;
4790     }
4791
4792     gi = CreateGadget(GDI_CUSTOM_ID, id,
4793                       GDI_INFO_TEXT, toolbutton_info[i].infotext,
4794                       GDI_X, DX + toolbutton_info[i].x,
4795                       GDI_Y, DY + toolbutton_info[i].y,
4796                       GDI_WIDTH, toolbutton_info[i].width,
4797                       GDI_HEIGHT, toolbutton_info[i].height,
4798                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
4799                       GDI_STATE, GD_BUTTON_UNPRESSED,
4800                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
4801                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
4802                       GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y,
4803                       GDI_DECORATION_POSITION, deco_xpos, deco_ypos,
4804                       GDI_DECORATION_SIZE, MINI_TILEX, MINI_TILEY,
4805                       GDI_DECORATION_SHIFTING, 1, 1,
4806                       GDI_DIRECT_DRAW, FALSE,
4807                       GDI_EVENT_MASK, event_mask,
4808                       GDI_CALLBACK_ACTION, HandleToolButtons,
4809                       GDI_END);
4810
4811     if (gi == NULL)
4812       Error(ERR_EXIT, "cannot create gadget");
4813
4814     tool_gadget[id] = gi;
4815   }
4816 }
4817
4818 void FreeToolButtons()
4819 {
4820   int i;
4821
4822   for (i = 0; i < NUM_TOOL_BUTTONS; i++)
4823     FreeGadget(tool_gadget[i]);
4824 }
4825
4826 static void UnmapToolButtons()
4827 {
4828   int i;
4829
4830   for (i = 0; i < NUM_TOOL_BUTTONS; i++)
4831     UnmapGadget(tool_gadget[i]);
4832 }
4833
4834 static void HandleToolButtons(struct GadgetInfo *gi)
4835 {
4836   request_gadget_id = gi->custom_id;
4837 }
4838
4839 static struct Mapping_EM_to_RND_object
4840 {
4841   int element_em;
4842   boolean is_rnd_to_em_mapping;         /* unique mapping EM <-> RND */
4843   boolean is_backside;                  /* backside of moving element */
4844
4845   int element_rnd;
4846   int action;
4847   int direction;
4848 }
4849 em_object_mapping_list[] =
4850 {
4851   {
4852     Xblank,                             TRUE,   FALSE,
4853     EL_EMPTY,                           -1, -1
4854   },
4855   {
4856     Yacid_splash_eB,                    FALSE,  FALSE,
4857     EL_ACID_SPLASH_RIGHT,               -1, -1
4858   },
4859   {
4860     Yacid_splash_wB,                    FALSE,  FALSE,
4861     EL_ACID_SPLASH_LEFT,                -1, -1
4862   },
4863
4864 #ifdef EM_ENGINE_BAD_ROLL
4865   {
4866     Xstone_force_e,                     FALSE,  FALSE,
4867     EL_ROCK,                            -1, MV_BIT_RIGHT
4868   },
4869   {
4870     Xstone_force_w,                     FALSE,  FALSE,
4871     EL_ROCK,                            -1, MV_BIT_LEFT
4872   },
4873   {
4874     Xnut_force_e,                       FALSE,  FALSE,
4875     EL_NUT,                             -1, MV_BIT_RIGHT
4876   },
4877   {
4878     Xnut_force_w,                       FALSE,  FALSE,
4879     EL_NUT,                             -1, MV_BIT_LEFT
4880   },
4881   {
4882     Xspring_force_e,                    FALSE,  FALSE,
4883     EL_SPRING,                          -1, MV_BIT_RIGHT
4884   },
4885   {
4886     Xspring_force_w,                    FALSE,  FALSE,
4887     EL_SPRING,                          -1, MV_BIT_LEFT
4888   },
4889   {
4890     Xemerald_force_e,                   FALSE,  FALSE,
4891     EL_EMERALD,                         -1, MV_BIT_RIGHT
4892   },
4893   {
4894     Xemerald_force_w,                   FALSE,  FALSE,
4895     EL_EMERALD,                         -1, MV_BIT_LEFT
4896   },
4897   {
4898     Xdiamond_force_e,                   FALSE,  FALSE,
4899     EL_DIAMOND,                         -1, MV_BIT_RIGHT
4900   },
4901   {
4902     Xdiamond_force_w,                   FALSE,  FALSE,
4903     EL_DIAMOND,                         -1, MV_BIT_LEFT
4904   },
4905   {
4906     Xbomb_force_e,                      FALSE,  FALSE,
4907     EL_BOMB,                            -1, MV_BIT_RIGHT
4908   },
4909   {
4910     Xbomb_force_w,                      FALSE,  FALSE,
4911     EL_BOMB,                            -1, MV_BIT_LEFT
4912   },
4913 #endif  /* EM_ENGINE_BAD_ROLL */
4914
4915   {
4916     Xstone,                             TRUE,   FALSE,
4917     EL_ROCK,                            -1, -1
4918   },
4919   {
4920     Xstone_pause,                       FALSE,  FALSE,
4921     EL_ROCK,                            -1, -1
4922   },
4923   {
4924     Xstone_fall,                        FALSE,  FALSE,
4925     EL_ROCK,                            -1, -1
4926   },
4927   {
4928     Ystone_s,                           FALSE,  FALSE,
4929     EL_ROCK,                            ACTION_FALLING, -1
4930   },
4931   {
4932     Ystone_sB,                          FALSE,  TRUE,
4933     EL_ROCK,                            ACTION_FALLING, -1
4934   },
4935   {
4936     Ystone_e,                           FALSE,  FALSE,
4937     EL_ROCK,                            ACTION_MOVING, MV_BIT_RIGHT
4938   },
4939   {
4940     Ystone_eB,                          FALSE,  TRUE,
4941     EL_ROCK,                            ACTION_MOVING, MV_BIT_RIGHT
4942   },
4943   {
4944     Ystone_w,                           FALSE,  FALSE,
4945     EL_ROCK,                            ACTION_MOVING, MV_BIT_LEFT
4946   },
4947   {
4948     Ystone_wB,                          FALSE,  TRUE,
4949     EL_ROCK,                            ACTION_MOVING, MV_BIT_LEFT
4950   },
4951   {
4952     Xnut,                               TRUE,   FALSE,
4953     EL_NUT,                             -1, -1
4954   },
4955   {
4956     Xnut_pause,                         FALSE,  FALSE,
4957     EL_NUT,                             -1, -1
4958   },
4959   {
4960     Xnut_fall,                          FALSE,  FALSE,
4961     EL_NUT,                             -1, -1
4962   },
4963   {
4964     Ynut_s,                             FALSE,  FALSE,
4965     EL_NUT,                             ACTION_FALLING, -1
4966   },
4967   {
4968     Ynut_sB,                            FALSE,  TRUE,
4969     EL_NUT,                             ACTION_FALLING, -1
4970   },
4971   {
4972     Ynut_e,                             FALSE,  FALSE,
4973     EL_NUT,                             ACTION_MOVING, MV_BIT_RIGHT
4974   },
4975   {
4976     Ynut_eB,                            FALSE,  TRUE,
4977     EL_NUT,                             ACTION_MOVING, MV_BIT_RIGHT
4978   },
4979   {
4980     Ynut_w,                             FALSE,  FALSE,
4981     EL_NUT,                             ACTION_MOVING, MV_BIT_LEFT
4982   },
4983   {
4984     Ynut_wB,                            FALSE,  TRUE,
4985     EL_NUT,                             ACTION_MOVING, MV_BIT_LEFT
4986   },
4987   {
4988     Xbug_n,                             TRUE,   FALSE,
4989     EL_BUG_UP,                          -1, -1
4990   },
4991   {
4992     Xbug_e,                             TRUE,   FALSE,
4993     EL_BUG_RIGHT,                       -1, -1
4994   },
4995   {
4996     Xbug_s,                             TRUE,   FALSE,
4997     EL_BUG_DOWN,                        -1, -1
4998   },
4999   {
5000     Xbug_w,                             TRUE,   FALSE,
5001     EL_BUG_LEFT,                        -1, -1
5002   },
5003   {
5004     Xbug_gon,                           FALSE,  FALSE,
5005     EL_BUG_UP,                          -1, -1
5006   },
5007   {
5008     Xbug_goe,                           FALSE,  FALSE,
5009     EL_BUG_RIGHT,                       -1, -1
5010   },
5011   {
5012     Xbug_gos,                           FALSE,  FALSE,
5013     EL_BUG_DOWN,                        -1, -1
5014   },
5015   {
5016     Xbug_gow,                           FALSE,  FALSE,
5017     EL_BUG_LEFT,                        -1, -1
5018   },
5019   {
5020     Ybug_n,                             FALSE,  FALSE,
5021     EL_BUG,                             ACTION_MOVING, MV_BIT_UP
5022   },
5023   {
5024     Ybug_nB,                            FALSE,  TRUE,
5025     EL_BUG,                             ACTION_MOVING, MV_BIT_UP
5026   },
5027   {
5028     Ybug_e,                             FALSE,  FALSE,
5029     EL_BUG,                             ACTION_MOVING, MV_BIT_RIGHT
5030   },
5031   {
5032     Ybug_eB,                            FALSE,  TRUE,
5033     EL_BUG,                             ACTION_MOVING, MV_BIT_RIGHT
5034   },
5035   {
5036     Ybug_s,                             FALSE,  FALSE,
5037     EL_BUG,                             ACTION_MOVING, MV_BIT_DOWN
5038   },
5039   {
5040     Ybug_sB,                            FALSE,  TRUE,
5041     EL_BUG,                             ACTION_MOVING, MV_BIT_DOWN
5042   },
5043   {
5044     Ybug_w,                             FALSE,  FALSE,
5045     EL_BUG,                             ACTION_MOVING, MV_BIT_LEFT
5046   },
5047   {
5048     Ybug_wB,                            FALSE,  TRUE,
5049     EL_BUG,                             ACTION_MOVING, MV_BIT_LEFT
5050   },
5051   {
5052     Ybug_w_n,                           FALSE,  FALSE,
5053     EL_BUG,                             ACTION_TURNING_FROM_LEFT, MV_BIT_UP
5054   },
5055   {
5056     Ybug_n_e,                           FALSE,  FALSE,
5057     EL_BUG,                             ACTION_TURNING_FROM_UP, MV_BIT_RIGHT
5058   },
5059   {
5060     Ybug_e_s,                           FALSE,  FALSE,
5061     EL_BUG,                             ACTION_TURNING_FROM_RIGHT, MV_BIT_DOWN
5062   },
5063   {
5064     Ybug_s_w,                           FALSE,  FALSE,
5065     EL_BUG,                             ACTION_TURNING_FROM_DOWN, MV_BIT_LEFT
5066   },
5067   {
5068     Ybug_e_n,                           FALSE,  FALSE,
5069     EL_BUG,                             ACTION_TURNING_FROM_RIGHT, MV_BIT_UP
5070   },
5071   {
5072     Ybug_s_e,                           FALSE,  FALSE,
5073     EL_BUG,                             ACTION_TURNING_FROM_DOWN, MV_BIT_RIGHT
5074   },
5075   {
5076     Ybug_w_s,                           FALSE,  FALSE,
5077     EL_BUG,                             ACTION_TURNING_FROM_LEFT, MV_BIT_DOWN
5078   },
5079   {
5080     Ybug_n_w,                           FALSE,  FALSE,
5081     EL_BUG,                             ACTION_TURNING_FROM_UP, MV_BIT_LEFT
5082   },
5083   {
5084     Ybug_stone,                         FALSE,  FALSE,
5085     EL_BUG,                             ACTION_SMASHED_BY_ROCK, -1
5086   },
5087   {
5088     Ybug_spring,                        FALSE,  FALSE,
5089     EL_BUG,                             ACTION_SMASHED_BY_SPRING, -1
5090   },
5091   {
5092     Xtank_n,                            TRUE,   FALSE,
5093     EL_SPACESHIP_UP,                    -1, -1
5094   },
5095   {
5096     Xtank_e,                            TRUE,   FALSE,
5097     EL_SPACESHIP_RIGHT,                 -1, -1
5098   },
5099   {
5100     Xtank_s,                            TRUE,   FALSE,
5101     EL_SPACESHIP_DOWN,                  -1, -1
5102   },
5103   {
5104     Xtank_w,                            TRUE,   FALSE,
5105     EL_SPACESHIP_LEFT,                  -1, -1
5106   },
5107   {
5108     Xtank_gon,                          FALSE,  FALSE,
5109     EL_SPACESHIP_UP,                    -1, -1
5110   },
5111   {
5112     Xtank_goe,                          FALSE,  FALSE,
5113     EL_SPACESHIP_RIGHT,                 -1, -1
5114   },
5115   {
5116     Xtank_gos,                          FALSE,  FALSE,
5117     EL_SPACESHIP_DOWN,                  -1, -1
5118   },
5119   {
5120     Xtank_gow,                          FALSE,  FALSE,
5121     EL_SPACESHIP_LEFT,                  -1, -1
5122   },
5123   {
5124     Ytank_n,                            FALSE,  FALSE,
5125     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_UP
5126   },
5127   {
5128     Ytank_nB,                           FALSE,  TRUE,
5129     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_UP
5130   },
5131   {
5132     Ytank_e,                            FALSE,  FALSE,
5133     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_RIGHT
5134   },
5135   {
5136     Ytank_eB,                           FALSE,  TRUE,
5137     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_RIGHT
5138   },
5139   {
5140     Ytank_s,                            FALSE,  FALSE,
5141     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_DOWN
5142   },
5143   {
5144     Ytank_sB,                           FALSE,  TRUE,
5145     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_DOWN
5146   },
5147   {
5148     Ytank_w,                            FALSE,  FALSE,
5149     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_LEFT
5150   },
5151   {
5152     Ytank_wB,                           FALSE,  TRUE,
5153     EL_SPACESHIP,                       ACTION_MOVING, MV_BIT_LEFT
5154   },
5155   {
5156     Ytank_w_n,                          FALSE,  FALSE,
5157     EL_SPACESHIP,                       ACTION_TURNING_FROM_LEFT, MV_BIT_UP
5158   },
5159   {
5160     Ytank_n_e,                          FALSE,  FALSE,
5161     EL_SPACESHIP,                       ACTION_TURNING_FROM_UP, MV_BIT_RIGHT
5162   },
5163   {
5164     Ytank_e_s,                          FALSE,  FALSE,
5165     EL_SPACESHIP,                       ACTION_TURNING_FROM_RIGHT, MV_BIT_DOWN
5166   },
5167   {
5168     Ytank_s_w,                          FALSE,  FALSE,
5169     EL_SPACESHIP,                       ACTION_TURNING_FROM_DOWN, MV_BIT_LEFT
5170   },
5171   {
5172     Ytank_e_n,                          FALSE,  FALSE,
5173     EL_SPACESHIP,                       ACTION_TURNING_FROM_RIGHT, MV_BIT_UP
5174   },
5175   {
5176     Ytank_s_e,                          FALSE,  FALSE,
5177     EL_SPACESHIP,                       ACTION_TURNING_FROM_DOWN, MV_BIT_RIGHT
5178   },
5179   {
5180     Ytank_w_s,                          FALSE,  FALSE,
5181     EL_SPACESHIP,                       ACTION_TURNING_FROM_LEFT, MV_BIT_DOWN
5182   },
5183   {
5184     Ytank_n_w,                          FALSE,  FALSE,
5185     EL_SPACESHIP,                       ACTION_TURNING_FROM_UP, MV_BIT_LEFT
5186   },
5187   {
5188     Ytank_stone,                        FALSE,  FALSE,
5189     EL_SPACESHIP,                       ACTION_SMASHED_BY_ROCK, -1
5190   },
5191   {
5192     Ytank_spring,                       FALSE,  FALSE,
5193     EL_SPACESHIP,                       ACTION_SMASHED_BY_SPRING, -1
5194   },
5195   {
5196     Xandroid,                           TRUE,   FALSE,
5197     EL_EMC_ANDROID,                     ACTION_ACTIVE, -1
5198   },
5199   {
5200     Xandroid_1_n,                       FALSE,  FALSE,
5201     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_UP
5202   },
5203   {
5204     Xandroid_2_n,                       FALSE,  FALSE,
5205     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_UP
5206   },
5207   {
5208     Xandroid_1_e,                       FALSE,  FALSE,
5209     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_RIGHT
5210   },
5211   {
5212     Xandroid_2_e,                       FALSE,  FALSE,
5213     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_RIGHT
5214   },
5215   {
5216     Xandroid_1_w,                       FALSE,  FALSE,
5217     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_LEFT
5218   },
5219   {
5220     Xandroid_2_w,                       FALSE,  FALSE,
5221     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_LEFT
5222   },
5223   {
5224     Xandroid_1_s,                       FALSE,  FALSE,
5225     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_DOWN
5226   },
5227   {
5228     Xandroid_2_s,                       FALSE,  FALSE,
5229     EL_EMC_ANDROID,                     ACTION_ACTIVE, MV_BIT_DOWN
5230   },
5231   {
5232     Yandroid_n,                         FALSE,  FALSE,
5233     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_UP
5234   },
5235   {
5236     Yandroid_nB,                        FALSE,  TRUE,
5237     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_UP
5238   },
5239   {
5240     Yandroid_ne,                        FALSE,  FALSE,
5241     EL_EMC_ANDROID,                     ACTION_GROWING, MV_BIT_UPRIGHT
5242   },
5243   {
5244     Yandroid_neB,                       FALSE,  TRUE,
5245     EL_EMC_ANDROID,                     ACTION_SHRINKING, MV_BIT_UPRIGHT
5246   },
5247   {
5248     Yandroid_e,                         FALSE,  FALSE,
5249     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_RIGHT
5250   },
5251   {
5252     Yandroid_eB,                        FALSE,  TRUE,
5253     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_RIGHT
5254   },
5255   {
5256     Yandroid_se,                        FALSE,  FALSE,
5257     EL_EMC_ANDROID,                     ACTION_GROWING, MV_BIT_DOWNRIGHT
5258   },
5259   {
5260     Yandroid_seB,                       FALSE,  TRUE,
5261     EL_EMC_ANDROID,                     ACTION_SHRINKING, MV_BIT_DOWNRIGHT
5262   },
5263   {
5264     Yandroid_s,                         FALSE,  FALSE,
5265     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_DOWN
5266   },
5267   {
5268     Yandroid_sB,                        FALSE,  TRUE,
5269     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_DOWN
5270   },
5271   {
5272     Yandroid_sw,                        FALSE,  FALSE,
5273     EL_EMC_ANDROID,                     ACTION_GROWING, MV_BIT_DOWNLEFT
5274   },
5275   {
5276     Yandroid_swB,                       FALSE,  TRUE,
5277     EL_EMC_ANDROID,                     ACTION_SHRINKING, MV_BIT_DOWNLEFT
5278   },
5279   {
5280     Yandroid_w,                         FALSE,  FALSE,
5281     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_LEFT
5282   },
5283   {
5284     Yandroid_wB,                        FALSE,  TRUE,
5285     EL_EMC_ANDROID,                     ACTION_MOVING, MV_BIT_LEFT
5286   },
5287   {
5288     Yandroid_nw,                        FALSE,  FALSE,
5289     EL_EMC_ANDROID,                     ACTION_GROWING, MV_BIT_UPLEFT
5290   },
5291   {
5292     Yandroid_nwB,                       FALSE,  TRUE,
5293     EL_EMC_ANDROID,                     ACTION_SHRINKING, MV_BIT_UPLEFT
5294   },
5295   {
5296     Xspring,                            TRUE,   FALSE,
5297     EL_SPRING,                          -1, -1
5298   },
5299   {
5300     Xspring_pause,                      FALSE,  FALSE,
5301     EL_SPRING,                          -1, -1
5302   },
5303   {
5304     Xspring_e,                          FALSE,  FALSE,
5305     EL_SPRING,                          -1, -1
5306   },
5307   {
5308     Xspring_w,                          FALSE,  FALSE,
5309     EL_SPRING,                          -1, -1
5310   },
5311   {
5312     Xspring_fall,                       FALSE,  FALSE,
5313     EL_SPRING,                          -1, -1
5314   },
5315   {
5316     Yspring_s,                          FALSE,  FALSE,
5317     EL_SPRING,                          ACTION_FALLING, -1
5318   },
5319   {
5320     Yspring_sB,                         FALSE,  TRUE,
5321     EL_SPRING,                          ACTION_FALLING, -1
5322   },
5323   {
5324     Yspring_e,                          FALSE,  FALSE,
5325     EL_SPRING,                          ACTION_MOVING, MV_BIT_RIGHT
5326   },
5327   {
5328     Yspring_eB,                         FALSE,  TRUE,
5329     EL_SPRING,                          ACTION_MOVING, MV_BIT_RIGHT
5330   },
5331   {
5332     Yspring_w,                          FALSE,  FALSE,
5333     EL_SPRING,                          ACTION_MOVING, MV_BIT_LEFT
5334   },
5335   {
5336     Yspring_wB,                         FALSE,  TRUE,
5337     EL_SPRING,                          ACTION_MOVING, MV_BIT_LEFT
5338   },
5339   {
5340     Yspring_kill_e,                     FALSE,  FALSE,
5341     EL_SPRING,                          ACTION_EATING, MV_BIT_RIGHT
5342   },
5343   {
5344     Yspring_kill_eB,                    FALSE,  TRUE,
5345     EL_SPRING,                          ACTION_EATING, MV_BIT_RIGHT
5346   },
5347   {
5348     Yspring_kill_w,                     FALSE,  FALSE,
5349     EL_SPRING,                          ACTION_EATING, MV_BIT_LEFT
5350   },
5351   {
5352     Yspring_kill_wB,                    FALSE,  TRUE,
5353     EL_SPRING,                          ACTION_EATING, MV_BIT_LEFT
5354   },
5355   {
5356     Xeater_n,                           TRUE,   FALSE,
5357     EL_YAMYAM_UP,                       -1, -1
5358   },
5359   {
5360     Xeater_e,                           TRUE,   FALSE,
5361     EL_YAMYAM_RIGHT,                    -1, -1
5362   },
5363   {
5364     Xeater_w,                           TRUE,   FALSE,
5365     EL_YAMYAM_LEFT,                     -1, -1
5366   },
5367   {
5368     Xeater_s,                           TRUE,   FALSE,
5369     EL_YAMYAM_DOWN,                     -1, -1
5370   },
5371   {
5372     Yeater_n,                           FALSE,  FALSE,
5373     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_UP
5374   },
5375   {
5376     Yeater_nB,                          FALSE,  TRUE,
5377     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_UP
5378   },
5379   {
5380     Yeater_e,                           FALSE,  FALSE,
5381     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_RIGHT
5382   },
5383   {
5384     Yeater_eB,                          FALSE,  TRUE,
5385     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_RIGHT
5386   },
5387   {
5388     Yeater_s,                           FALSE,  FALSE,
5389     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_DOWN
5390   },
5391   {
5392     Yeater_sB,                          FALSE,  TRUE,
5393     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_DOWN
5394   },
5395   {
5396     Yeater_w,                           FALSE,  FALSE,
5397     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_LEFT
5398   },
5399   {
5400     Yeater_wB,                          FALSE,  TRUE,
5401     EL_YAMYAM,                          ACTION_MOVING, MV_BIT_LEFT
5402   },
5403   {
5404     Yeater_stone,                       FALSE,  FALSE,
5405     EL_YAMYAM,                          ACTION_SMASHED_BY_ROCK, -1
5406   },
5407   {
5408     Yeater_spring,                      FALSE,  FALSE,
5409     EL_YAMYAM,                          ACTION_SMASHED_BY_SPRING, -1
5410   },
5411   {
5412     Xalien,                             TRUE,   FALSE,
5413     EL_ROBOT,                           -1, -1
5414   },
5415   {
5416     Xalien_pause,                       FALSE,  FALSE,
5417     EL_ROBOT,                           -1, -1
5418   },
5419   {
5420     Yalien_n,                           FALSE,  FALSE,
5421     EL_ROBOT,                           ACTION_MOVING, MV_BIT_UP
5422   },
5423   {
5424     Yalien_nB,                          FALSE,  TRUE,
5425     EL_ROBOT,                           ACTION_MOVING, MV_BIT_UP
5426   },
5427   {
5428     Yalien_e,                           FALSE,  FALSE,
5429     EL_ROBOT,                           ACTION_MOVING, MV_BIT_RIGHT
5430   },
5431   {
5432     Yalien_eB,                          FALSE,  TRUE,
5433     EL_ROBOT,                           ACTION_MOVING, MV_BIT_RIGHT
5434   },
5435   {
5436     Yalien_s,                           FALSE,  FALSE,
5437     EL_ROBOT,                           ACTION_MOVING, MV_BIT_DOWN
5438   },
5439   {
5440     Yalien_sB,                          FALSE,  TRUE,
5441     EL_ROBOT,                           ACTION_MOVING, MV_BIT_DOWN
5442   },
5443   {
5444     Yalien_w,                           FALSE,  FALSE,
5445     EL_ROBOT,                           ACTION_MOVING, MV_BIT_LEFT
5446   },
5447   {
5448     Yalien_wB,                          FALSE,  TRUE,
5449     EL_ROBOT,                           ACTION_MOVING, MV_BIT_LEFT
5450   },
5451   {
5452     Yalien_stone,                       FALSE,  FALSE,
5453     EL_ROBOT,                           ACTION_SMASHED_BY_ROCK, -1
5454   },
5455   {
5456     Yalien_spring,                      FALSE,  FALSE,
5457     EL_ROBOT,                           ACTION_SMASHED_BY_SPRING, -1
5458   },
5459   {
5460     Xemerald,                           TRUE,   FALSE,
5461     EL_EMERALD,                         -1, -1
5462   },
5463   {
5464     Xemerald_pause,                     FALSE,  FALSE,
5465     EL_EMERALD,                         -1, -1
5466   },
5467   {
5468     Xemerald_fall,                      FALSE,  FALSE,
5469     EL_EMERALD,                         -1, -1
5470   },
5471   {
5472     Xemerald_shine,                     FALSE,  FALSE,
5473     EL_EMERALD,                         ACTION_TWINKLING, -1
5474   },
5475   {
5476     Yemerald_s,                         FALSE,  FALSE,
5477     EL_EMERALD,                         ACTION_FALLING, -1
5478   },
5479   {
5480     Yemerald_sB,                        FALSE,  TRUE,
5481     EL_EMERALD,                         ACTION_FALLING, -1
5482   },
5483   {
5484     Yemerald_e,                         FALSE,  FALSE,
5485     EL_EMERALD,                         ACTION_MOVING, MV_BIT_RIGHT
5486   },
5487   {
5488     Yemerald_eB,                        FALSE,  TRUE,
5489     EL_EMERALD,                         ACTION_MOVING, MV_BIT_RIGHT
5490   },
5491   {
5492     Yemerald_w,                         FALSE,  FALSE,
5493     EL_EMERALD,                         ACTION_MOVING, MV_BIT_LEFT
5494   },
5495   {
5496     Yemerald_wB,                        FALSE,  TRUE,
5497     EL_EMERALD,                         ACTION_MOVING, MV_BIT_LEFT
5498   },
5499   {
5500     Yemerald_eat,                       FALSE,  FALSE,
5501     EL_EMERALD,                         ACTION_COLLECTING, -1
5502   },
5503   {
5504     Yemerald_stone,                     FALSE,  FALSE,
5505     EL_NUT,                             ACTION_BREAKING, -1
5506   },
5507   {
5508     Xdiamond,                           TRUE,   FALSE,
5509     EL_DIAMOND,                         -1, -1
5510   },
5511   {
5512     Xdiamond_pause,                     FALSE,  FALSE,
5513     EL_DIAMOND,                         -1, -1
5514   },
5515   {
5516     Xdiamond_fall,                      FALSE,  FALSE,
5517     EL_DIAMOND,                         -1, -1
5518   },
5519   {
5520     Xdiamond_shine,                     FALSE,  FALSE,
5521     EL_DIAMOND,                         ACTION_TWINKLING, -1
5522   },
5523   {
5524     Ydiamond_s,                         FALSE,  FALSE,
5525     EL_DIAMOND,                         ACTION_FALLING, -1
5526   },
5527   {
5528     Ydiamond_sB,                        FALSE,  TRUE,
5529     EL_DIAMOND,                         ACTION_FALLING, -1
5530   },
5531   {
5532     Ydiamond_e,                         FALSE,  FALSE,
5533     EL_DIAMOND,                         ACTION_MOVING, MV_BIT_RIGHT
5534   },
5535   {
5536     Ydiamond_eB,                        FALSE,  TRUE,
5537     EL_DIAMOND,                         ACTION_MOVING, MV_BIT_RIGHT
5538   },
5539   {
5540     Ydiamond_w,                         FALSE,  FALSE,
5541     EL_DIAMOND,                         ACTION_MOVING, MV_BIT_LEFT
5542   },
5543   {
5544     Ydiamond_wB,                        FALSE,  TRUE,
5545     EL_DIAMOND,                         ACTION_MOVING, MV_BIT_LEFT
5546   },
5547   {
5548     Ydiamond_eat,                       FALSE,  FALSE,
5549     EL_DIAMOND,                         ACTION_COLLECTING, -1
5550   },
5551   {
5552     Ydiamond_stone,                     FALSE,  FALSE,
5553     EL_DIAMOND,                         ACTION_SMASHED_BY_ROCK, -1
5554   },
5555   {
5556     Xdrip_fall,                         TRUE,   FALSE,
5557     EL_AMOEBA_DROP,                     -1, -1
5558   },
5559   {
5560     Xdrip_stretch,                      FALSE,  FALSE,
5561     EL_AMOEBA_DROP,                     ACTION_FALLING, -1
5562   },
5563   {
5564     Xdrip_stretchB,                     FALSE,  TRUE,
5565     EL_AMOEBA_DROP,                     ACTION_FALLING, -1
5566   },
5567   {
5568     Xdrip_eat,                          FALSE,  FALSE,
5569     EL_AMOEBA_DROP,                     ACTION_GROWING, -1
5570   },
5571   {
5572     Ydrip_s1,                           FALSE,  FALSE,
5573     EL_AMOEBA_DROP,                     ACTION_FALLING, -1
5574   },
5575   {
5576     Ydrip_s1B,                          FALSE,  TRUE,
5577     EL_AMOEBA_DROP,                     ACTION_FALLING, -1
5578   },
5579   {
5580     Ydrip_s2,                           FALSE,  FALSE,
5581     EL_AMOEBA_DROP,                     ACTION_FALLING, -1
5582   },
5583   {
5584     Ydrip_s2B,                          FALSE,  TRUE,
5585     EL_AMOEBA_DROP,                     ACTION_FALLING, -1
5586   },
5587   {
5588     Xbomb,                              TRUE,   FALSE,
5589     EL_BOMB,                            -1, -1
5590   },
5591   {
5592     Xbomb_pause,                        FALSE,  FALSE,
5593     EL_BOMB,                            -1, -1
5594   },
5595   {
5596     Xbomb_fall,                         FALSE,  FALSE,
5597     EL_BOMB,                            -1, -1
5598   },
5599   {
5600     Ybomb_s,                            FALSE,  FALSE,
5601     EL_BOMB,                            ACTION_FALLING, -1
5602   },
5603   {
5604     Ybomb_sB,                           FALSE,  TRUE,
5605     EL_BOMB,                            ACTION_FALLING, -1
5606   },
5607   {
5608     Ybomb_e,                            FALSE,  FALSE,
5609     EL_BOMB,                            ACTION_MOVING, MV_BIT_RIGHT
5610   },
5611   {
5612     Ybomb_eB,                           FALSE,  TRUE,
5613     EL_BOMB,                            ACTION_MOVING, MV_BIT_RIGHT
5614   },
5615   {
5616     Ybomb_w,                            FALSE,  FALSE,
5617     EL_BOMB,                            ACTION_MOVING, MV_BIT_LEFT
5618   },
5619   {
5620     Ybomb_wB,                           FALSE,  TRUE,
5621     EL_BOMB,                            ACTION_MOVING, MV_BIT_LEFT
5622   },
5623   {
5624     Ybomb_eat,                          FALSE,  FALSE,
5625     EL_BOMB,                            ACTION_ACTIVATING, -1
5626   },
5627   {
5628     Xballoon,                           TRUE,   FALSE,
5629     EL_BALLOON,                         -1, -1
5630   },
5631   {
5632     Yballoon_n,                         FALSE,  FALSE,
5633     EL_BALLOON,                         ACTION_MOVING, MV_BIT_UP
5634   },
5635   {
5636     Yballoon_nB,                        FALSE,  TRUE,
5637     EL_BALLOON,                         ACTION_MOVING, MV_BIT_UP
5638   },
5639   {
5640     Yballoon_e,                         FALSE,  FALSE,
5641     EL_BALLOON,                         ACTION_MOVING, MV_BIT_RIGHT
5642   },
5643   {
5644     Yballoon_eB,                        FALSE,  TRUE,
5645     EL_BALLOON,                         ACTION_MOVING, MV_BIT_RIGHT
5646   },
5647   {
5648     Yballoon_s,                         FALSE,  FALSE,
5649     EL_BALLOON,                         ACTION_MOVING, MV_BIT_DOWN
5650   },
5651   {
5652     Yballoon_sB,                        FALSE,  TRUE,
5653     EL_BALLOON,                         ACTION_MOVING, MV_BIT_DOWN
5654   },
5655   {
5656     Yballoon_w,                         FALSE,  FALSE,
5657     EL_BALLOON,                         ACTION_MOVING, MV_BIT_LEFT
5658   },
5659   {
5660     Yballoon_wB,                        FALSE,  TRUE,
5661     EL_BALLOON,                         ACTION_MOVING, MV_BIT_LEFT
5662   },
5663   {
5664     Xgrass,                             TRUE,   FALSE,
5665     EL_EMC_GRASS,                       -1, -1
5666   },
5667   {
5668     Ygrass_nB,                          FALSE,  FALSE,
5669     EL_EMC_GRASS,                       ACTION_DIGGING, MV_BIT_UP
5670   },
5671   {
5672     Ygrass_eB,                          FALSE,  FALSE,
5673     EL_EMC_GRASS,                       ACTION_DIGGING, MV_BIT_RIGHT
5674   },
5675   {
5676     Ygrass_sB,                          FALSE,  FALSE,
5677     EL_EMC_GRASS,                       ACTION_DIGGING, MV_BIT_DOWN
5678   },
5679   {
5680     Ygrass_wB,                          FALSE,  FALSE,
5681     EL_EMC_GRASS,                       ACTION_DIGGING, MV_BIT_LEFT
5682   },
5683   {
5684     Xdirt,                              TRUE,   FALSE,
5685     EL_SAND,                            -1, -1
5686   },
5687   {
5688     Ydirt_nB,                           FALSE,  FALSE,
5689     EL_SAND,                            ACTION_DIGGING, MV_BIT_UP
5690   },
5691   {
5692     Ydirt_eB,                           FALSE,  FALSE,
5693     EL_SAND,                            ACTION_DIGGING, MV_BIT_RIGHT
5694   },
5695   {
5696     Ydirt_sB,                           FALSE,  FALSE,
5697     EL_SAND,                            ACTION_DIGGING, MV_BIT_DOWN
5698   },
5699   {
5700     Ydirt_wB,                           FALSE,  FALSE,
5701     EL_SAND,                            ACTION_DIGGING, MV_BIT_LEFT
5702   },
5703   {
5704     Xacid_ne,                           TRUE,   FALSE,
5705     EL_ACID_POOL_TOPRIGHT,              -1, -1
5706   },
5707   {
5708     Xacid_se,                           TRUE,   FALSE,
5709     EL_ACID_POOL_BOTTOMRIGHT,           -1, -1
5710   },
5711   {
5712     Xacid_s,                            TRUE,   FALSE,
5713     EL_ACID_POOL_BOTTOM,                -1, -1
5714   },
5715   {
5716     Xacid_sw,                           TRUE,   FALSE,
5717     EL_ACID_POOL_BOTTOMLEFT,            -1, -1
5718   },
5719   {
5720     Xacid_nw,                           TRUE,   FALSE,
5721     EL_ACID_POOL_TOPLEFT,               -1, -1
5722   },
5723   {
5724     Xacid_1,                            TRUE,   FALSE,
5725     EL_ACID,                            -1, -1
5726   },
5727   {
5728     Xacid_2,                            FALSE,  FALSE,
5729     EL_ACID,                            -1, -1
5730   },
5731   {
5732     Xacid_3,                            FALSE,  FALSE,
5733     EL_ACID,                            -1, -1
5734   },
5735   {
5736     Xacid_4,                            FALSE,  FALSE,
5737     EL_ACID,                            -1, -1
5738   },
5739   {
5740     Xacid_5,                            FALSE,  FALSE,
5741     EL_ACID,                            -1, -1
5742   },
5743   {
5744     Xacid_6,                            FALSE,  FALSE,
5745     EL_ACID,                            -1, -1
5746   },
5747   {
5748     Xacid_7,                            FALSE,  FALSE,
5749     EL_ACID,                            -1, -1
5750   },
5751   {
5752     Xacid_8,                            FALSE,  FALSE,
5753     EL_ACID,                            -1, -1
5754   },
5755   {
5756     Xball_1,                            TRUE,   FALSE,
5757     EL_EMC_MAGIC_BALL,                  -1, -1
5758   },
5759   {
5760     Xball_1B,                           FALSE,  FALSE,
5761     EL_EMC_MAGIC_BALL,                  ACTION_ACTIVE, -1
5762   },
5763   {
5764     Xball_2,                            FALSE,  FALSE,
5765     EL_EMC_MAGIC_BALL,                  ACTION_ACTIVE, -1
5766   },
5767   {
5768     Xball_2B,                           FALSE,  FALSE,
5769     EL_EMC_MAGIC_BALL,                  ACTION_ACTIVE, -1
5770   },
5771   {
5772     Yball_eat,                          FALSE,  FALSE,
5773     EL_EMC_MAGIC_BALL,                  ACTION_DROPPING, -1
5774   },
5775   {
5776     Ykey_1_eat,                         FALSE,  FALSE,
5777     EL_EM_KEY_1,                        ACTION_COLLECTING, -1
5778   },
5779   {
5780     Ykey_2_eat,                         FALSE,  FALSE,
5781     EL_EM_KEY_2,                        ACTION_COLLECTING, -1
5782   },
5783   {
5784     Ykey_3_eat,                         FALSE,  FALSE,
5785     EL_EM_KEY_3,                        ACTION_COLLECTING, -1
5786   },
5787   {
5788     Ykey_4_eat,                         FALSE,  FALSE,
5789     EL_EM_KEY_4,                        ACTION_COLLECTING, -1
5790   },
5791   {
5792     Ykey_5_eat,                         FALSE,  FALSE,
5793     EL_EMC_KEY_5,                       ACTION_COLLECTING, -1
5794   },
5795   {
5796     Ykey_6_eat,                         FALSE,  FALSE,
5797     EL_EMC_KEY_6,                       ACTION_COLLECTING, -1
5798   },
5799   {
5800     Ykey_7_eat,                         FALSE,  FALSE,
5801     EL_EMC_KEY_7,                       ACTION_COLLECTING, -1
5802   },
5803   {
5804     Ykey_8_eat,                         FALSE,  FALSE,
5805     EL_EMC_KEY_8,                       ACTION_COLLECTING, -1
5806   },
5807   {
5808     Ylenses_eat,                        FALSE,  FALSE,
5809     EL_EMC_LENSES,                      ACTION_COLLECTING, -1
5810   },
5811   {
5812     Ymagnify_eat,                       FALSE,  FALSE,
5813     EL_EMC_MAGNIFIER,                   ACTION_COLLECTING, -1
5814   },
5815   {
5816     Ygrass_eat,                         FALSE,  FALSE,
5817     EL_EMC_GRASS,                       ACTION_SNAPPING, -1
5818   },
5819   {
5820     Ydirt_eat,                          FALSE,  FALSE,
5821     EL_SAND,                            ACTION_SNAPPING, -1
5822   },
5823   {
5824     Xgrow_ns,                           TRUE,   FALSE,
5825     EL_EXPANDABLE_WALL_VERTICAL,        -1, -1
5826   },
5827   {
5828     Ygrow_ns_eat,                       FALSE,  FALSE,
5829     EL_EXPANDABLE_WALL_VERTICAL,        ACTION_GROWING, -1
5830   },
5831   {
5832     Xgrow_ew,                           TRUE,   FALSE,
5833     EL_EXPANDABLE_WALL_HORIZONTAL,      -1, -1
5834   },
5835   {
5836     Ygrow_ew_eat,                       FALSE,  FALSE,
5837     EL_EXPANDABLE_WALL_HORIZONTAL,      ACTION_GROWING, -1
5838   },
5839   {
5840     Xwonderwall,                        TRUE,   FALSE,
5841     EL_MAGIC_WALL,                      -1, -1
5842   },
5843   {
5844     XwonderwallB,                       FALSE,  FALSE,
5845     EL_MAGIC_WALL,                      ACTION_ACTIVE, -1
5846   },
5847   {
5848     Xamoeba_1,                          TRUE,   FALSE,
5849     EL_AMOEBA_DRY,                      ACTION_OTHER, -1
5850   },
5851   {
5852     Xamoeba_2,                          FALSE,  FALSE,
5853     EL_AMOEBA_DRY,                      ACTION_OTHER, -1
5854   },
5855   {
5856     Xamoeba_3,                          FALSE,  FALSE,
5857     EL_AMOEBA_DRY,                      ACTION_OTHER, -1
5858   },
5859   {
5860     Xamoeba_4,                          FALSE,  FALSE,
5861     EL_AMOEBA_DRY,                      ACTION_OTHER, -1
5862   },
5863   {
5864     Xamoeba_5,                          TRUE,   FALSE,
5865     EL_AMOEBA_WET,                      ACTION_OTHER, -1
5866   },
5867   {
5868     Xamoeba_6,                          FALSE,  FALSE,
5869     EL_AMOEBA_WET,                      ACTION_OTHER, -1
5870   },
5871   {
5872     Xamoeba_7,                          FALSE,  FALSE,
5873     EL_AMOEBA_WET,                      ACTION_OTHER, -1
5874   },
5875   {
5876     Xamoeba_8,                          FALSE,  FALSE,
5877     EL_AMOEBA_WET,                      ACTION_OTHER, -1
5878   },
5879   {
5880     Xdoor_1,                            TRUE,   FALSE,
5881     EL_EM_GATE_1,                       -1, -1
5882   },
5883   {
5884     Xdoor_2,                            TRUE,   FALSE,
5885     EL_EM_GATE_2,                       -1, -1
5886   },
5887   {
5888     Xdoor_3,                            TRUE,   FALSE,
5889     EL_EM_GATE_3,                       -1, -1
5890   },
5891   {
5892     Xdoor_4,                            TRUE,   FALSE,
5893     EL_EM_GATE_4,                       -1, -1
5894   },
5895   {
5896     Xdoor_5,                            TRUE,   FALSE,
5897     EL_EMC_GATE_5,                      -1, -1
5898   },
5899   {
5900     Xdoor_6,                            TRUE,   FALSE,
5901     EL_EMC_GATE_6,                      -1, -1
5902   },
5903   {
5904     Xdoor_7,                            TRUE,   FALSE,
5905     EL_EMC_GATE_7,                      -1, -1
5906   },
5907   {
5908     Xdoor_8,                            TRUE,   FALSE,
5909     EL_EMC_GATE_8,                      -1, -1
5910   },
5911   {
5912     Xkey_1,                             TRUE,   FALSE,
5913     EL_EM_KEY_1,                        -1, -1
5914   },
5915   {
5916     Xkey_2,                             TRUE,   FALSE,
5917     EL_EM_KEY_2,                        -1, -1
5918   },
5919   {
5920     Xkey_3,                             TRUE,   FALSE,
5921     EL_EM_KEY_3,                        -1, -1
5922   },
5923   {
5924     Xkey_4,                             TRUE,   FALSE,
5925     EL_EM_KEY_4,                        -1, -1
5926   },
5927   {
5928     Xkey_5,                             TRUE,   FALSE,
5929     EL_EMC_KEY_5,                       -1, -1
5930   },
5931   {
5932     Xkey_6,                             TRUE,   FALSE,
5933     EL_EMC_KEY_6,                       -1, -1
5934   },
5935   {
5936     Xkey_7,                             TRUE,   FALSE,
5937     EL_EMC_KEY_7,                       -1, -1
5938   },
5939   {
5940     Xkey_8,                             TRUE,   FALSE,
5941     EL_EMC_KEY_8,                       -1, -1
5942   },
5943   {
5944     Xwind_n,                            TRUE,   FALSE,
5945     EL_BALLOON_SWITCH_UP,               -1, -1
5946   },
5947   {
5948     Xwind_e,                            TRUE,   FALSE,
5949     EL_BALLOON_SWITCH_RIGHT,            -1, -1
5950   },
5951   {
5952     Xwind_s,                            TRUE,   FALSE,
5953     EL_BALLOON_SWITCH_DOWN,             -1, -1
5954   },
5955   {
5956     Xwind_w,                            TRUE,   FALSE,
5957     EL_BALLOON_SWITCH_LEFT,             -1, -1
5958   },
5959   {
5960     Xwind_nesw,                         TRUE,   FALSE,
5961     EL_BALLOON_SWITCH_ANY,              -1, -1
5962   },
5963   {
5964     Xwind_stop,                         TRUE,   FALSE,
5965     EL_BALLOON_SWITCH_NONE,             -1, -1
5966   },
5967   {
5968     Xexit,                              TRUE,   FALSE,
5969     EL_EM_EXIT_CLOSED,                  -1, -1
5970   },
5971   {
5972     Xexit_1,                            TRUE,   FALSE,
5973     EL_EM_EXIT_OPEN,                    -1, -1
5974   },
5975   {
5976     Xexit_2,                            FALSE,  FALSE,
5977     EL_EM_EXIT_OPEN,                    -1, -1
5978   },
5979   {
5980     Xexit_3,                            FALSE,  FALSE,
5981     EL_EM_EXIT_OPEN,                    -1, -1
5982   },
5983   {
5984     Xdynamite,                          TRUE,   FALSE,
5985     EL_EM_DYNAMITE,                     -1, -1
5986   },
5987   {
5988     Ydynamite_eat,                      FALSE,  FALSE,
5989     EL_EM_DYNAMITE,                     ACTION_COLLECTING, -1
5990   },
5991   {
5992     Xdynamite_1,                        TRUE,   FALSE,
5993     EL_EM_DYNAMITE_ACTIVE,              -1, -1
5994   },
5995   {
5996     Xdynamite_2,                        FALSE,  FALSE,
5997     EL_EM_DYNAMITE_ACTIVE,              -1, -1
5998   },
5999   {
6000     Xdynamite_3,                        FALSE,  FALSE,
6001     EL_EM_DYNAMITE_ACTIVE,              -1, -1
6002   },
6003   {
6004     Xdynamite_4,                        FALSE,  FALSE,
6005     EL_EM_DYNAMITE_ACTIVE,              -1, -1
6006   },
6007   {
6008     Xbumper,                            TRUE,   FALSE,
6009     EL_EMC_SPRING_BUMPER,               -1, -1
6010   },
6011   {
6012     XbumperB,                           FALSE,  FALSE,
6013     EL_EMC_SPRING_BUMPER,               ACTION_ACTIVE, -1
6014   },
6015   {
6016     Xwheel,                             TRUE,   FALSE,
6017     EL_ROBOT_WHEEL,                     -1, -1
6018   },
6019   {
6020     XwheelB,                            FALSE,  FALSE,
6021     EL_ROBOT_WHEEL,                     ACTION_ACTIVE, -1
6022   },
6023   {
6024     Xswitch,                            TRUE,   FALSE,
6025     EL_EMC_MAGIC_BALL_SWITCH,           -1, -1
6026   },
6027   {
6028     XswitchB,                           FALSE,  FALSE,
6029     EL_EMC_MAGIC_BALL_SWITCH,           ACTION_ACTIVE, -1
6030   },
6031   {
6032     Xsand,                              TRUE,   FALSE,
6033     EL_QUICKSAND_EMPTY,                 -1, -1
6034   },
6035   {
6036     Xsand_stone,                        TRUE,   FALSE,
6037     EL_QUICKSAND_FULL,                  -1, -1
6038   },
6039   {
6040     Xsand_stonein_1,                    FALSE,  TRUE,
6041     EL_ROCK,                            ACTION_FILLING, -1
6042   },
6043   {
6044     Xsand_stonein_2,                    FALSE,  TRUE,
6045     EL_ROCK,                            ACTION_FILLING, -1
6046   },
6047   {
6048     Xsand_stonein_3,                    FALSE,  TRUE,
6049     EL_ROCK,                            ACTION_FILLING, -1
6050   },
6051   {
6052     Xsand_stonein_4,                    FALSE,  TRUE,
6053     EL_ROCK,                            ACTION_FILLING, -1
6054   },
6055 #if 1
6056   {
6057     Xsand_stonesand_1,                  FALSE,  FALSE,
6058     EL_QUICKSAND_EMPTYING,              -1, -1
6059   },
6060   {
6061     Xsand_stonesand_2,                  FALSE,  FALSE,
6062     EL_QUICKSAND_EMPTYING,              -1, -1
6063   },
6064   {
6065     Xsand_stonesand_3,                  FALSE,  FALSE,
6066     EL_QUICKSAND_EMPTYING,              -1, -1
6067   },
6068   {
6069     Xsand_stonesand_4,                  FALSE,  FALSE,
6070     EL_QUICKSAND_EMPTYING,              -1, -1
6071   },
6072   {
6073     Xsand_stonesand_quickout_1,         FALSE,  FALSE,
6074     EL_QUICKSAND_EMPTYING,              -1, -1
6075   },
6076   {
6077     Xsand_stonesand_quickout_2,         FALSE,  FALSE,
6078     EL_QUICKSAND_EMPTYING,              -1, -1
6079   },
6080 #else
6081   {
6082     Xsand_stonesand_1,                  FALSE,  FALSE,
6083     EL_QUICKSAND_FULL,                  -1, -1
6084   },
6085   {
6086     Xsand_stonesand_2,                  FALSE,  FALSE,
6087     EL_QUICKSAND_FULL,                  -1, -1
6088   },
6089   {
6090     Xsand_stonesand_3,                  FALSE,  FALSE,
6091     EL_QUICKSAND_FULL,                  -1, -1
6092   },
6093   {
6094     Xsand_stonesand_4,                  FALSE,  FALSE,
6095     EL_QUICKSAND_FULL,                  -1, -1
6096   },
6097 #endif
6098   {
6099     Xsand_stoneout_1,                   FALSE,  FALSE,
6100     EL_ROCK,                            ACTION_EMPTYING, -1
6101   },
6102   {
6103     Xsand_stoneout_2,                   FALSE,  FALSE,
6104     EL_ROCK,                            ACTION_EMPTYING, -1
6105   },
6106 #if 1
6107   {
6108     Xsand_sandstone_1,                  FALSE,  FALSE,
6109     EL_QUICKSAND_FILLING,               -1, -1
6110   },
6111   {
6112     Xsand_sandstone_2,                  FALSE,  FALSE,
6113     EL_QUICKSAND_FILLING,               -1, -1
6114   },
6115   {
6116     Xsand_sandstone_3,                  FALSE,  FALSE,
6117     EL_QUICKSAND_FILLING,               -1, -1
6118   },
6119   {
6120     Xsand_sandstone_4,                  FALSE,  FALSE,
6121     EL_QUICKSAND_FILLING,               -1, -1
6122   },
6123 #else
6124   {
6125     Xsand_sandstone_1,                  FALSE,  FALSE,
6126     EL_QUICKSAND_FULL,                  -1, -1
6127   },
6128   {
6129     Xsand_sandstone_2,                  FALSE,  FALSE,
6130     EL_QUICKSAND_FULL,                  -1, -1
6131   },
6132   {
6133     Xsand_sandstone_3,                  FALSE,  FALSE,
6134     EL_QUICKSAND_FULL,                  -1, -1
6135   },
6136   {
6137     Xsand_sandstone_4,                  FALSE,  FALSE,
6138     EL_QUICKSAND_FULL,                  -1, -1
6139   },
6140 #endif
6141   {
6142     Xplant,                             TRUE,   FALSE,
6143     EL_EMC_PLANT,                       -1, -1
6144   },
6145   {
6146     Yplant,                             FALSE,  FALSE,
6147     EL_EMC_PLANT,                       -1, -1
6148   },
6149   {
6150     Xlenses,                            TRUE,   FALSE,
6151     EL_EMC_LENSES,                      -1, -1
6152   },
6153   {
6154     Xmagnify,                           TRUE,   FALSE,
6155     EL_EMC_MAGNIFIER,                   -1, -1
6156   },
6157   {
6158     Xdripper,                           TRUE,   FALSE,
6159     EL_EMC_DRIPPER,                     -1, -1
6160   },
6161   {
6162     XdripperB,                          FALSE,  FALSE,
6163     EL_EMC_DRIPPER,                     ACTION_ACTIVE, -1
6164   },
6165   {
6166     Xfake_blank,                        TRUE,   FALSE,
6167     EL_INVISIBLE_WALL,                  -1, -1
6168   },
6169   {
6170     Xfake_blankB,                       FALSE,  FALSE,
6171     EL_INVISIBLE_WALL,                  ACTION_ACTIVE, -1
6172   },
6173   {
6174     Xfake_grass,                        TRUE,   FALSE,
6175     EL_EMC_FAKE_GRASS,                  -1, -1
6176   },
6177   {
6178     Xfake_grassB,                       FALSE,  FALSE,
6179     EL_EMC_FAKE_GRASS,                  ACTION_ACTIVE, -1
6180   },
6181   {
6182     Xfake_door_1,                       TRUE,   FALSE,
6183     EL_EM_GATE_1_GRAY,                  -1, -1
6184   },
6185   {
6186     Xfake_door_2,                       TRUE,   FALSE,
6187     EL_EM_GATE_2_GRAY,                  -1, -1
6188   },
6189   {
6190     Xfake_door_3,                       TRUE,   FALSE,
6191     EL_EM_GATE_3_GRAY,                  -1, -1
6192   },
6193   {
6194     Xfake_door_4,                       TRUE,   FALSE,
6195     EL_EM_GATE_4_GRAY,                  -1, -1
6196   },
6197   {
6198     Xfake_door_5,                       TRUE,   FALSE,
6199     EL_EMC_GATE_5_GRAY,                 -1, -1
6200   },
6201   {
6202     Xfake_door_6,                       TRUE,   FALSE,
6203     EL_EMC_GATE_6_GRAY,                 -1, -1
6204   },
6205   {
6206     Xfake_door_7,                       TRUE,   FALSE,
6207     EL_EMC_GATE_7_GRAY,                 -1, -1
6208   },
6209   {
6210     Xfake_door_8,                       TRUE,   FALSE,
6211     EL_EMC_GATE_8_GRAY,                 -1, -1
6212   },
6213   {
6214     Xfake_acid_1,                       TRUE,   FALSE,
6215     EL_EMC_FAKE_ACID,                   -1, -1
6216   },
6217   {
6218     Xfake_acid_2,                       FALSE,  FALSE,
6219     EL_EMC_FAKE_ACID,                   -1, -1
6220   },
6221   {
6222     Xfake_acid_3,                       FALSE,  FALSE,
6223     EL_EMC_FAKE_ACID,                   -1, -1
6224   },
6225   {
6226     Xfake_acid_4,                       FALSE,  FALSE,
6227     EL_EMC_FAKE_ACID,                   -1, -1
6228   },
6229   {
6230     Xfake_acid_5,                       FALSE,  FALSE,
6231     EL_EMC_FAKE_ACID,                   -1, -1
6232   },
6233   {
6234     Xfake_acid_6,                       FALSE,  FALSE,
6235     EL_EMC_FAKE_ACID,                   -1, -1
6236   },
6237   {
6238     Xfake_acid_7,                       FALSE,  FALSE,
6239     EL_EMC_FAKE_ACID,                   -1, -1
6240   },
6241   {
6242     Xfake_acid_8,                       FALSE,  FALSE,
6243     EL_EMC_FAKE_ACID,                   -1, -1
6244   },
6245   {
6246     Xsteel_1,                           TRUE,   FALSE,
6247     EL_STEELWALL,                       -1, -1
6248   },
6249   {
6250     Xsteel_2,                           TRUE,   FALSE,
6251     EL_EMC_STEELWALL_2,                 -1, -1
6252   },
6253   {
6254     Xsteel_3,                           TRUE,   FALSE,
6255     EL_EMC_STEELWALL_3,                 -1, -1
6256   },
6257   {
6258     Xsteel_4,                           TRUE,   FALSE,
6259     EL_EMC_STEELWALL_4,                 -1, -1
6260   },
6261   {
6262     Xwall_1,                            TRUE,   FALSE,
6263     EL_WALL,                            -1, -1
6264   },
6265   {
6266     Xwall_2,                            TRUE,   FALSE,
6267     EL_EMC_WALL_14,                     -1, -1
6268   },
6269   {
6270     Xwall_3,                            TRUE,   FALSE,
6271     EL_EMC_WALL_15,                     -1, -1
6272   },
6273   {
6274     Xwall_4,                            TRUE,   FALSE,
6275     EL_EMC_WALL_16,                     -1, -1
6276   },
6277   {
6278     Xround_wall_1,                      TRUE,   FALSE,
6279     EL_WALL_SLIPPERY,                   -1, -1
6280   },
6281   {
6282     Xround_wall_2,                      TRUE,   FALSE,
6283     EL_EMC_WALL_SLIPPERY_2,             -1, -1
6284   },
6285   {
6286     Xround_wall_3,                      TRUE,   FALSE,
6287     EL_EMC_WALL_SLIPPERY_3,             -1, -1
6288   },
6289   {
6290     Xround_wall_4,                      TRUE,   FALSE,
6291     EL_EMC_WALL_SLIPPERY_4,             -1, -1
6292   },
6293   {
6294     Xdecor_1,                           TRUE,   FALSE,
6295     EL_EMC_WALL_8,                      -1, -1
6296   },
6297   {
6298     Xdecor_2,                           TRUE,   FALSE,
6299     EL_EMC_WALL_6,                      -1, -1
6300   },
6301   {
6302     Xdecor_3,                           TRUE,   FALSE,
6303     EL_EMC_WALL_4,                      -1, -1
6304   },
6305   {
6306     Xdecor_4,                           TRUE,   FALSE,
6307     EL_EMC_WALL_7,                      -1, -1
6308   },
6309   {
6310     Xdecor_5,                           TRUE,   FALSE,
6311     EL_EMC_WALL_5,                      -1, -1
6312   },
6313   {
6314     Xdecor_6,                           TRUE,   FALSE,
6315     EL_EMC_WALL_9,                      -1, -1
6316   },
6317   {
6318     Xdecor_7,                           TRUE,   FALSE,
6319     EL_EMC_WALL_10,                     -1, -1
6320   },
6321   {
6322     Xdecor_8,                           TRUE,   FALSE,
6323     EL_EMC_WALL_1,                      -1, -1
6324   },
6325   {
6326     Xdecor_9,                           TRUE,   FALSE,
6327     EL_EMC_WALL_2,                      -1, -1
6328   },
6329   {
6330     Xdecor_10,                          TRUE,   FALSE,
6331     EL_EMC_WALL_3,                      -1, -1
6332   },
6333   {
6334     Xdecor_11,                          TRUE,   FALSE,
6335     EL_EMC_WALL_11,                     -1, -1
6336   },
6337   {
6338     Xdecor_12,                          TRUE,   FALSE,
6339     EL_EMC_WALL_12,                     -1, -1
6340   },
6341   {
6342     Xalpha_0,                           TRUE,   FALSE,
6343     EL_CHAR('0'),                       -1, -1
6344   },
6345   {
6346     Xalpha_1,                           TRUE,   FALSE,
6347     EL_CHAR('1'),                       -1, -1
6348   },
6349   {
6350     Xalpha_2,                           TRUE,   FALSE,
6351     EL_CHAR('2'),                       -1, -1
6352   },
6353   {
6354     Xalpha_3,                           TRUE,   FALSE,
6355     EL_CHAR('3'),                       -1, -1
6356   },
6357   {
6358     Xalpha_4,                           TRUE,   FALSE,
6359     EL_CHAR('4'),                       -1, -1
6360   },
6361   {
6362     Xalpha_5,                           TRUE,   FALSE,
6363     EL_CHAR('5'),                       -1, -1
6364   },
6365   {
6366     Xalpha_6,                           TRUE,   FALSE,
6367     EL_CHAR('6'),                       -1, -1
6368   },
6369   {
6370     Xalpha_7,                           TRUE,   FALSE,
6371     EL_CHAR('7'),                       -1, -1
6372   },
6373   {
6374     Xalpha_8,                           TRUE,   FALSE,
6375     EL_CHAR('8'),                       -1, -1
6376   },
6377   {
6378     Xalpha_9,                           TRUE,   FALSE,
6379     EL_CHAR('9'),                       -1, -1
6380   },
6381   {
6382     Xalpha_excla,                       TRUE,   FALSE,
6383     EL_CHAR('!'),                       -1, -1
6384   },
6385   {
6386     Xalpha_quote,                       TRUE,   FALSE,
6387     EL_CHAR('"'),                       -1, -1
6388   },
6389   {
6390     Xalpha_comma,                       TRUE,   FALSE,
6391     EL_CHAR(','),                       -1, -1
6392   },
6393   {
6394     Xalpha_minus,                       TRUE,   FALSE,
6395     EL_CHAR('-'),                       -1, -1
6396   },
6397   {
6398     Xalpha_perio,                       TRUE,   FALSE,
6399     EL_CHAR('.'),                       -1, -1
6400   },
6401   {
6402     Xalpha_colon,                       TRUE,   FALSE,
6403     EL_CHAR(':'),                       -1, -1
6404   },
6405   {
6406     Xalpha_quest,                       TRUE,   FALSE,
6407     EL_CHAR('?'),                       -1, -1
6408   },
6409   {
6410     Xalpha_a,                           TRUE,   FALSE,
6411     EL_CHAR('A'),                       -1, -1
6412   },
6413   {
6414     Xalpha_b,                           TRUE,   FALSE,
6415     EL_CHAR('B'),                       -1, -1
6416   },
6417   {
6418     Xalpha_c,                           TRUE,   FALSE,
6419     EL_CHAR('C'),                       -1, -1
6420   },
6421   {
6422     Xalpha_d,                           TRUE,   FALSE,
6423     EL_CHAR('D'),                       -1, -1
6424   },
6425   {
6426     Xalpha_e,                           TRUE,   FALSE,
6427     EL_CHAR('E'),                       -1, -1
6428   },
6429   {
6430     Xalpha_f,                           TRUE,   FALSE,
6431     EL_CHAR('F'),                       -1, -1
6432   },
6433   {
6434     Xalpha_g,                           TRUE,   FALSE,
6435     EL_CHAR('G'),                       -1, -1
6436   },
6437   {
6438     Xalpha_h,                           TRUE,   FALSE,
6439     EL_CHAR('H'),                       -1, -1
6440   },
6441   {
6442     Xalpha_i,                           TRUE,   FALSE,
6443     EL_CHAR('I'),                       -1, -1
6444   },
6445   {
6446     Xalpha_j,                           TRUE,   FALSE,
6447     EL_CHAR('J'),                       -1, -1
6448   },
6449   {
6450     Xalpha_k,                           TRUE,   FALSE,
6451     EL_CHAR('K'),                       -1, -1
6452   },
6453   {
6454     Xalpha_l,                           TRUE,   FALSE,
6455     EL_CHAR('L'),                       -1, -1
6456   },
6457   {
6458     Xalpha_m,                           TRUE,   FALSE,
6459     EL_CHAR('M'),                       -1, -1
6460   },
6461   {
6462     Xalpha_n,                           TRUE,   FALSE,
6463     EL_CHAR('N'),                       -1, -1
6464   },
6465   {
6466     Xalpha_o,                           TRUE,   FALSE,
6467     EL_CHAR('O'),                       -1, -1
6468   },
6469   {
6470     Xalpha_p,                           TRUE,   FALSE,
6471     EL_CHAR('P'),                       -1, -1
6472   },
6473   {
6474     Xalpha_q,                           TRUE,   FALSE,
6475     EL_CHAR('Q'),                       -1, -1
6476   },
6477   {
6478     Xalpha_r,                           TRUE,   FALSE,
6479     EL_CHAR('R'),                       -1, -1
6480   },
6481   {
6482     Xalpha_s,                           TRUE,   FALSE,
6483     EL_CHAR('S'),                       -1, -1
6484   },
6485   {
6486     Xalpha_t,                           TRUE,   FALSE,
6487     EL_CHAR('T'),                       -1, -1
6488   },
6489   {
6490     Xalpha_u,                           TRUE,   FALSE,
6491     EL_CHAR('U'),                       -1, -1
6492   },
6493   {
6494     Xalpha_v,                           TRUE,   FALSE,
6495     EL_CHAR('V'),                       -1, -1
6496   },
6497   {
6498     Xalpha_w,                           TRUE,   FALSE,
6499     EL_CHAR('W'),                       -1, -1
6500   },
6501   {
6502     Xalpha_x,                           TRUE,   FALSE,
6503     EL_CHAR('X'),                       -1, -1
6504   },
6505   {
6506     Xalpha_y,                           TRUE,   FALSE,
6507     EL_CHAR('Y'),                       -1, -1
6508   },
6509   {
6510     Xalpha_z,                           TRUE,   FALSE,
6511     EL_CHAR('Z'),                       -1, -1
6512   },
6513   {
6514     Xalpha_arrow_e,                     TRUE,   FALSE,
6515     EL_CHAR('>'),                       -1, -1
6516   },
6517   {
6518     Xalpha_arrow_w,                     TRUE,   FALSE,
6519     EL_CHAR('<'),                       -1, -1
6520   },
6521   {
6522     Xalpha_copyr,                       TRUE,   FALSE,
6523     EL_CHAR('©'),                       -1, -1
6524   },
6525
6526   {
6527     Xboom_bug,                          FALSE,  FALSE,
6528     EL_BUG,                             ACTION_EXPLODING, -1
6529   },
6530   {
6531     Xboom_bomb,                         FALSE,  FALSE,
6532     EL_BOMB,                            ACTION_EXPLODING, -1
6533   },
6534   {
6535     Xboom_android,                      FALSE,  FALSE,
6536     EL_EMC_ANDROID,                     ACTION_OTHER, -1
6537   },
6538   {
6539     Xboom_1,                            FALSE,  FALSE,
6540     EL_DEFAULT,                         ACTION_EXPLODING, -1
6541   },
6542   {
6543     Xboom_2,                            FALSE,  FALSE,
6544     EL_DEFAULT,                         ACTION_EXPLODING, -1
6545   },
6546   {
6547     Znormal,                            FALSE,  FALSE,
6548     EL_EMPTY,                           -1, -1
6549   },
6550   {
6551     Zdynamite,                          FALSE,  FALSE,
6552     EL_EMPTY,                           -1, -1
6553   },
6554   {
6555     Zplayer,                            FALSE,  FALSE,
6556     EL_EMPTY,                           -1, -1
6557   },
6558   {
6559     ZBORDER,                            FALSE,  FALSE,
6560     EL_EMPTY,                           -1, -1
6561   },
6562
6563   {
6564     -1,                                 FALSE,  FALSE,
6565     -1,                                 -1, -1
6566   }
6567 };
6568
6569 static struct Mapping_EM_to_RND_player
6570 {
6571   int action_em;
6572   int player_nr;
6573
6574   int element_rnd;
6575   int action;
6576   int direction;
6577 }
6578 em_player_mapping_list[] =
6579 {
6580   {
6581     SPR_walk + 0,                       0,
6582     EL_PLAYER_1,                        ACTION_MOVING, MV_BIT_UP,
6583   },
6584   {
6585     SPR_walk + 1,                       0,
6586     EL_PLAYER_1,                        ACTION_MOVING, MV_BIT_RIGHT,
6587   },
6588   {
6589     SPR_walk + 2,                       0,
6590     EL_PLAYER_1,                        ACTION_MOVING, MV_BIT_DOWN,
6591   },
6592   {
6593     SPR_walk + 3,                       0,
6594     EL_PLAYER_1,                        ACTION_MOVING, MV_BIT_LEFT,
6595   },
6596   {
6597     SPR_push + 0,                       0,
6598     EL_PLAYER_1,                        ACTION_PUSHING, MV_BIT_UP,
6599   },
6600   {
6601     SPR_push + 1,                       0,
6602     EL_PLAYER_1,                        ACTION_PUSHING, MV_BIT_RIGHT,
6603   },
6604   {
6605     SPR_push + 2,                       0,
6606     EL_PLAYER_1,                        ACTION_PUSHING, MV_BIT_DOWN,
6607   },
6608   {
6609     SPR_push + 3,                       0,
6610     EL_PLAYER_1,                        ACTION_PUSHING, MV_BIT_LEFT,
6611   },
6612   {
6613     SPR_spray + 0,                      0,
6614     EL_PLAYER_1,                        ACTION_SNAPPING, MV_BIT_UP,
6615   },
6616   {
6617     SPR_spray + 1,                      0,
6618     EL_PLAYER_1,                        ACTION_SNAPPING, MV_BIT_RIGHT,
6619   },
6620   {
6621     SPR_spray + 2,                      0,
6622     EL_PLAYER_1,                        ACTION_SNAPPING, MV_BIT_DOWN,
6623   },
6624   {
6625     SPR_spray + 3,                      0,
6626     EL_PLAYER_1,                        ACTION_SNAPPING, MV_BIT_LEFT,
6627   },
6628   {
6629     SPR_walk + 0,                       1,
6630     EL_PLAYER_2,                        ACTION_MOVING, MV_BIT_UP,
6631   },
6632   {
6633     SPR_walk + 1,                       1,
6634     EL_PLAYER_2,                        ACTION_MOVING, MV_BIT_RIGHT,
6635   },
6636   {
6637     SPR_walk + 2,                       1,
6638     EL_PLAYER_2,                        ACTION_MOVING, MV_BIT_DOWN,
6639   },
6640   {
6641     SPR_walk + 3,                       1,
6642     EL_PLAYER_2,                        ACTION_MOVING, MV_BIT_LEFT,
6643   },
6644   {
6645     SPR_push + 0,                       1,
6646     EL_PLAYER_2,                        ACTION_PUSHING, MV_BIT_UP,
6647   },
6648   {
6649     SPR_push + 1,                       1,
6650     EL_PLAYER_2,                        ACTION_PUSHING, MV_BIT_RIGHT,
6651   },
6652   {
6653     SPR_push + 2,                       1,
6654     EL_PLAYER_2,                        ACTION_PUSHING, MV_BIT_DOWN,
6655   },
6656   {
6657     SPR_push + 3,                       1,
6658     EL_PLAYER_2,                        ACTION_PUSHING, MV_BIT_LEFT,
6659   },
6660   {
6661     SPR_spray + 0,                      1,
6662     EL_PLAYER_2,                        ACTION_SNAPPING, MV_BIT_UP,
6663   },
6664   {
6665     SPR_spray + 1,                      1,
6666     EL_PLAYER_2,                        ACTION_SNAPPING, MV_BIT_RIGHT,
6667   },
6668   {
6669     SPR_spray + 2,                      1,
6670     EL_PLAYER_2,                        ACTION_SNAPPING, MV_BIT_DOWN,
6671   },
6672   {
6673     SPR_spray + 3,                      1,
6674     EL_PLAYER_2,                        ACTION_SNAPPING, MV_BIT_LEFT,
6675   },
6676   {
6677     SPR_still,                          0,
6678     EL_PLAYER_1,                        ACTION_DEFAULT, -1,
6679   },
6680   {
6681     SPR_still,                          1,
6682     EL_PLAYER_2,                        ACTION_DEFAULT, -1,
6683   },
6684   {
6685     SPR_walk + 0,                       2,
6686     EL_PLAYER_3,                        ACTION_MOVING, MV_BIT_UP,
6687   },
6688   {
6689     SPR_walk + 1,                       2,
6690     EL_PLAYER_3,                        ACTION_MOVING, MV_BIT_RIGHT,
6691   },
6692   {
6693     SPR_walk + 2,                       2,
6694     EL_PLAYER_3,                        ACTION_MOVING, MV_BIT_DOWN,
6695   },
6696   {
6697     SPR_walk + 3,                       2,
6698     EL_PLAYER_3,                        ACTION_MOVING, MV_BIT_LEFT,
6699   },
6700   {
6701     SPR_push + 0,                       2,
6702     EL_PLAYER_3,                        ACTION_PUSHING, MV_BIT_UP,
6703   },
6704   {
6705     SPR_push + 1,                       2,
6706     EL_PLAYER_3,                        ACTION_PUSHING, MV_BIT_RIGHT,
6707   },
6708   {
6709     SPR_push + 2,                       2,
6710     EL_PLAYER_3,                        ACTION_PUSHING, MV_BIT_DOWN,
6711   },
6712   {
6713     SPR_push + 3,                       2,
6714     EL_PLAYER_3,                        ACTION_PUSHING, MV_BIT_LEFT,
6715   },
6716   {
6717     SPR_spray + 0,                      2,
6718     EL_PLAYER_3,                        ACTION_SNAPPING, MV_BIT_UP,
6719   },
6720   {
6721     SPR_spray + 1,                      2,
6722     EL_PLAYER_3,                        ACTION_SNAPPING, MV_BIT_RIGHT,
6723   },
6724   {
6725     SPR_spray + 2,                      2,
6726     EL_PLAYER_3,                        ACTION_SNAPPING, MV_BIT_DOWN,
6727   },
6728   {
6729     SPR_spray + 3,                      2,
6730     EL_PLAYER_3,                        ACTION_SNAPPING, MV_BIT_LEFT,
6731   },
6732   {
6733     SPR_walk + 0,                       3,
6734     EL_PLAYER_4,                        ACTION_MOVING, MV_BIT_UP,
6735   },
6736   {
6737     SPR_walk + 1,                       3,
6738     EL_PLAYER_4,                        ACTION_MOVING, MV_BIT_RIGHT,
6739   },
6740   {
6741     SPR_walk + 2,                       3,
6742     EL_PLAYER_4,                        ACTION_MOVING, MV_BIT_DOWN,
6743   },
6744   {
6745     SPR_walk + 3,                       3,
6746     EL_PLAYER_4,                        ACTION_MOVING, MV_BIT_LEFT,
6747   },
6748   {
6749     SPR_push + 0,                       3,
6750     EL_PLAYER_4,                        ACTION_PUSHING, MV_BIT_UP,
6751   },
6752   {
6753     SPR_push + 1,                       3,
6754     EL_PLAYER_4,                        ACTION_PUSHING, MV_BIT_RIGHT,
6755   },
6756   {
6757     SPR_push + 2,                       3,
6758     EL_PLAYER_4,                        ACTION_PUSHING, MV_BIT_DOWN,
6759   },
6760   {
6761     SPR_push + 3,                       3,
6762     EL_PLAYER_4,                        ACTION_PUSHING, MV_BIT_LEFT,
6763   },
6764   {
6765     SPR_spray + 0,                      3,
6766     EL_PLAYER_4,                        ACTION_SNAPPING, MV_BIT_UP,
6767   },
6768   {
6769     SPR_spray + 1,                      3,
6770     EL_PLAYER_4,                        ACTION_SNAPPING, MV_BIT_RIGHT,
6771   },
6772   {
6773     SPR_spray + 2,                      3,
6774     EL_PLAYER_4,                        ACTION_SNAPPING, MV_BIT_DOWN,
6775   },
6776   {
6777     SPR_spray + 3,                      3,
6778     EL_PLAYER_4,                        ACTION_SNAPPING, MV_BIT_LEFT,
6779   },
6780   {
6781     SPR_still,                          2,
6782     EL_PLAYER_3,                        ACTION_DEFAULT, -1,
6783   },
6784   {
6785     SPR_still,                          3,
6786     EL_PLAYER_4,                        ACTION_DEFAULT, -1,
6787   },
6788
6789   {
6790     -1,                                 -1,
6791     -1,                                 -1, -1
6792   }
6793 };
6794
6795 int map_element_RND_to_EM(int element_rnd)
6796 {
6797   static unsigned short mapping_RND_to_EM[NUM_FILE_ELEMENTS];
6798   static boolean mapping_initialized = FALSE;
6799
6800   if (!mapping_initialized)
6801   {
6802     int i;
6803
6804     /* return "Xalpha_quest" for all undefined elements in mapping array */
6805     for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6806       mapping_RND_to_EM[i] = Xalpha_quest;
6807
6808     for (i = 0; em_object_mapping_list[i].element_em != -1; i++)
6809       if (em_object_mapping_list[i].is_rnd_to_em_mapping)
6810         mapping_RND_to_EM[em_object_mapping_list[i].element_rnd] =
6811           em_object_mapping_list[i].element_em;
6812
6813     mapping_initialized = TRUE;
6814   }
6815
6816   if (element_rnd >= 0 && element_rnd < NUM_FILE_ELEMENTS)
6817     return mapping_RND_to_EM[element_rnd];
6818
6819   Error(ERR_WARN, "invalid RND level element %d", element_rnd);
6820
6821   return EL_UNKNOWN;
6822 }
6823
6824 int map_element_EM_to_RND(int element_em)
6825 {
6826   static unsigned short mapping_EM_to_RND[TILE_MAX];
6827   static boolean mapping_initialized = FALSE;
6828
6829   if (!mapping_initialized)
6830   {
6831     int i;
6832
6833     /* return "EL_UNKNOWN" for all undefined elements in mapping array */
6834     for (i = 0; i < TILE_MAX; i++)
6835       mapping_EM_to_RND[i] = EL_UNKNOWN;
6836
6837     for (i = 0; em_object_mapping_list[i].element_em != -1; i++)
6838       mapping_EM_to_RND[em_object_mapping_list[i].element_em] =
6839         em_object_mapping_list[i].element_rnd;
6840
6841     mapping_initialized = TRUE;
6842   }
6843
6844   if (element_em >= 0 && element_em < TILE_MAX)
6845     return mapping_EM_to_RND[element_em];
6846
6847   Error(ERR_WARN, "invalid EM level element %d", element_em);
6848
6849   return EL_UNKNOWN;
6850 }
6851
6852 void map_android_clone_elements_RND_to_EM(struct LevelInfo *level)
6853 {
6854   struct LevelInfo_EM *level_em = level->native_em_level;
6855   struct LEVEL *lev = level_em->lev;
6856   int i, j;
6857
6858   for (i = 0; i < TILE_MAX; i++)
6859     lev->android_array[i] = Xblank;
6860
6861   for (i = 0; i < level->num_android_clone_elements; i++)
6862   {
6863     int element_rnd = level->android_clone_element[i];
6864     int element_em = map_element_RND_to_EM(element_rnd);
6865
6866     for (j = 0; em_object_mapping_list[j].element_em != -1; j++)
6867       if (em_object_mapping_list[j].element_rnd == element_rnd)
6868         lev->android_array[em_object_mapping_list[j].element_em] = element_em;
6869   }
6870 }
6871
6872 void map_android_clone_elements_EM_to_RND(struct LevelInfo *level)
6873 {
6874   struct LevelInfo_EM *level_em = level->native_em_level;
6875   struct LEVEL *lev = level_em->lev;
6876   int i, j;
6877
6878   level->num_android_clone_elements = 0;
6879
6880   for (i = 0; i < TILE_MAX; i++)
6881   {
6882     int element_em = lev->android_array[i];
6883     int element_rnd;
6884     boolean element_found = FALSE;
6885
6886     if (element_em == Xblank)
6887       continue;
6888
6889     element_rnd = map_element_EM_to_RND(element_em);
6890
6891     for (j = 0; j < level->num_android_clone_elements; j++)
6892       if (level->android_clone_element[j] == element_rnd)
6893         element_found = TRUE;
6894
6895     if (!element_found)
6896     {
6897       level->android_clone_element[level->num_android_clone_elements++] =
6898         element_rnd;
6899
6900       if (level->num_android_clone_elements == MAX_ANDROID_ELEMENTS)
6901         break;
6902     }
6903   }
6904
6905   if (level->num_android_clone_elements == 0)
6906   {
6907     level->num_android_clone_elements = 1;
6908     level->android_clone_element[0] = EL_EMPTY;
6909   }
6910 }
6911
6912 int map_direction_RND_to_EM(int direction)
6913 {
6914   return (direction == MV_UP    ? 0 :
6915           direction == MV_RIGHT ? 1 :
6916           direction == MV_DOWN  ? 2 :
6917           direction == MV_LEFT  ? 3 :
6918           -1);
6919 }
6920
6921 int map_direction_EM_to_RND(int direction)
6922 {
6923   return (direction == 0 ? MV_UP    :
6924           direction == 1 ? MV_RIGHT :
6925           direction == 2 ? MV_DOWN  :
6926           direction == 3 ? MV_LEFT  :
6927           MV_NONE);
6928 }
6929
6930 int map_element_RND_to_SP(int element_rnd)
6931 {
6932   int element_sp = 0x20;        /* map unknown elements to yellow "hardware" */
6933
6934   if (element_rnd >= EL_SP_START &&
6935       element_rnd <= EL_SP_END)
6936     element_sp = element_rnd - EL_SP_START;
6937   else if (element_rnd == EL_EMPTY_SPACE)
6938     element_sp = 0x00;
6939   else if (element_rnd == EL_INVISIBLE_WALL)
6940     element_sp = 0x28;
6941
6942   return element_sp;
6943 }
6944
6945 int map_element_SP_to_RND(int element_sp)
6946 {
6947   int element_rnd = EL_UNKNOWN;
6948
6949   if (element_sp >= 0x00 &&
6950       element_sp <= 0x27)
6951     element_rnd = EL_SP_START + element_sp;
6952   else if (element_sp == 0x28)
6953     element_rnd = EL_INVISIBLE_WALL;
6954
6955   return element_rnd;
6956 }
6957
6958 int map_action_SP_to_RND(int action_sp)
6959 {
6960   switch (action_sp)
6961   {
6962     case actActive:             return ACTION_ACTIVE;
6963     case actImpact:             return ACTION_IMPACT;
6964     case actExploding:          return ACTION_EXPLODING;
6965     case actDigging:            return ACTION_DIGGING;
6966     case actSnapping:           return ACTION_SNAPPING;
6967     case actCollecting:         return ACTION_COLLECTING;
6968     case actPassing:            return ACTION_PASSING;
6969     case actPushing:            return ACTION_PUSHING;
6970     case actDropping:           return ACTION_DROPPING;
6971
6972     default:                    return ACTION_DEFAULT;
6973   }
6974 }
6975
6976 int get_next_element(int element)
6977 {
6978   switch (element)
6979   {
6980     case EL_QUICKSAND_FILLING:          return EL_QUICKSAND_FULL;
6981     case EL_QUICKSAND_EMPTYING:         return EL_QUICKSAND_EMPTY;
6982     case EL_QUICKSAND_FAST_FILLING:     return EL_QUICKSAND_FAST_FULL;
6983     case EL_QUICKSAND_FAST_EMPTYING:    return EL_QUICKSAND_FAST_EMPTY;
6984     case EL_MAGIC_WALL_FILLING:         return EL_MAGIC_WALL_FULL;
6985     case EL_MAGIC_WALL_EMPTYING:        return EL_MAGIC_WALL_ACTIVE;
6986     case EL_BD_MAGIC_WALL_FILLING:      return EL_BD_MAGIC_WALL_FULL;
6987     case EL_BD_MAGIC_WALL_EMPTYING:     return EL_BD_MAGIC_WALL_ACTIVE;
6988     case EL_DC_MAGIC_WALL_FILLING:      return EL_DC_MAGIC_WALL_FULL;
6989     case EL_DC_MAGIC_WALL_EMPTYING:     return EL_DC_MAGIC_WALL_ACTIVE;
6990     case EL_AMOEBA_DROPPING:            return EL_AMOEBA_WET;
6991
6992     default:                            return element;
6993   }
6994 }
6995
6996 #if 0
6997 int el_act_dir2img(int element, int action, int direction)
6998 {
6999   element = GFX_ELEMENT(element);
7000
7001   if (direction == MV_NONE)
7002     return element_info[element].graphic[action];
7003
7004   direction = MV_DIR_TO_BIT(direction);
7005
7006   return element_info[element].direction_graphic[action][direction];
7007 }
7008 #else
7009 int el_act_dir2img(int element, int action, int direction)
7010 {
7011   element = GFX_ELEMENT(element);
7012   direction = MV_DIR_TO_BIT(direction); /* default: MV_NONE => MV_DOWN */
7013
7014   /* direction_graphic[][] == graphic[] for undefined direction graphics */
7015   return element_info[element].direction_graphic[action][direction];
7016 }
7017 #endif
7018
7019 #if 0
7020 static int el_act_dir2crm(int element, int action, int direction)
7021 {
7022   element = GFX_ELEMENT(element);
7023
7024   if (direction == MV_NONE)
7025     return element_info[element].crumbled[action];
7026
7027   direction = MV_DIR_TO_BIT(direction);
7028
7029   return element_info[element].direction_crumbled[action][direction];
7030 }
7031 #else
7032 static int el_act_dir2crm(int element, int action, int direction)
7033 {
7034   element = GFX_ELEMENT(element);
7035   direction = MV_DIR_TO_BIT(direction); /* default: MV_NONE => MV_DOWN */
7036
7037   /* direction_graphic[][] == graphic[] for undefined direction graphics */
7038   return element_info[element].direction_crumbled[action][direction];
7039 }
7040 #endif
7041
7042 int el_act2img(int element, int action)
7043 {
7044   element = GFX_ELEMENT(element);
7045
7046   return element_info[element].graphic[action];
7047 }
7048
7049 int el_act2crm(int element, int action)
7050 {
7051   element = GFX_ELEMENT(element);
7052
7053   return element_info[element].crumbled[action];
7054 }
7055
7056 int el_dir2img(int element, int direction)
7057 {
7058   element = GFX_ELEMENT(element);
7059
7060   return el_act_dir2img(element, ACTION_DEFAULT, direction);
7061 }
7062
7063 int el2baseimg(int element)
7064 {
7065   return element_info[element].graphic[ACTION_DEFAULT];
7066 }
7067
7068 int el2img(int element)
7069 {
7070   element = GFX_ELEMENT(element);
7071
7072   return element_info[element].graphic[ACTION_DEFAULT];
7073 }
7074
7075 int el2edimg(int element)
7076 {
7077   element = GFX_ELEMENT(element);
7078
7079   return element_info[element].special_graphic[GFX_SPECIAL_ARG_EDITOR];
7080 }
7081
7082 int el2preimg(int element)
7083 {
7084   element = GFX_ELEMENT(element);
7085
7086   return element_info[element].special_graphic[GFX_SPECIAL_ARG_PREVIEW];
7087 }
7088
7089 int el2panelimg(int element)
7090 {
7091   element = GFX_ELEMENT(element);
7092
7093   return element_info[element].special_graphic[GFX_SPECIAL_ARG_PANEL];
7094 }
7095
7096 int font2baseimg(int font_nr)
7097 {
7098   return font_info[font_nr].special_graphic[GFX_SPECIAL_ARG_DEFAULT];
7099 }
7100
7101 int getBeltNrFromBeltElement(int element)
7102 {
7103   return (element < EL_CONVEYOR_BELT_2_LEFT ? 0 :
7104           element < EL_CONVEYOR_BELT_3_LEFT ? 1 :
7105           element < EL_CONVEYOR_BELT_4_LEFT ? 2 : 3);
7106 }
7107
7108 int getBeltNrFromBeltActiveElement(int element)
7109 {
7110   return (element < EL_CONVEYOR_BELT_2_LEFT_ACTIVE ? 0 :
7111           element < EL_CONVEYOR_BELT_3_LEFT_ACTIVE ? 1 :
7112           element < EL_CONVEYOR_BELT_4_LEFT_ACTIVE ? 2 : 3);
7113 }
7114
7115 int getBeltNrFromBeltSwitchElement(int element)
7116 {
7117   return (element < EL_CONVEYOR_BELT_2_SWITCH_LEFT ? 0 :
7118           element < EL_CONVEYOR_BELT_3_SWITCH_LEFT ? 1 :
7119           element < EL_CONVEYOR_BELT_4_SWITCH_LEFT ? 2 : 3);
7120 }
7121
7122 int getBeltDirNrFromBeltElement(int element)
7123 {
7124   static int belt_base_element[4] =
7125   {
7126     EL_CONVEYOR_BELT_1_LEFT,
7127     EL_CONVEYOR_BELT_2_LEFT,
7128     EL_CONVEYOR_BELT_3_LEFT,
7129     EL_CONVEYOR_BELT_4_LEFT
7130   };
7131
7132   int belt_nr = getBeltNrFromBeltElement(element);
7133   int belt_dir_nr = element - belt_base_element[belt_nr];
7134
7135   return (belt_dir_nr % 3);
7136 }
7137
7138 int getBeltDirNrFromBeltSwitchElement(int element)
7139 {
7140   static int belt_base_element[4] =
7141   {
7142     EL_CONVEYOR_BELT_1_SWITCH_LEFT,
7143     EL_CONVEYOR_BELT_2_SWITCH_LEFT,
7144     EL_CONVEYOR_BELT_3_SWITCH_LEFT,
7145     EL_CONVEYOR_BELT_4_SWITCH_LEFT
7146   };
7147
7148   int belt_nr = getBeltNrFromBeltSwitchElement(element);
7149   int belt_dir_nr = element - belt_base_element[belt_nr];
7150
7151   return (belt_dir_nr % 3);
7152 }
7153
7154 int getBeltDirFromBeltElement(int element)
7155 {
7156   static int belt_move_dir[3] =
7157   {
7158     MV_LEFT,
7159     MV_NONE,
7160     MV_RIGHT
7161   };
7162
7163   int belt_dir_nr = getBeltDirNrFromBeltElement(element);
7164
7165   return belt_move_dir[belt_dir_nr];
7166 }
7167
7168 int getBeltDirFromBeltSwitchElement(int element)
7169 {
7170   static int belt_move_dir[3] =
7171   {
7172     MV_LEFT,
7173     MV_NONE,
7174     MV_RIGHT
7175   };
7176
7177   int belt_dir_nr = getBeltDirNrFromBeltSwitchElement(element);
7178
7179   return belt_move_dir[belt_dir_nr];
7180 }
7181
7182 int getBeltElementFromBeltNrAndBeltDirNr(int belt_nr, int belt_dir_nr)
7183 {
7184   static int belt_base_element[4] =
7185   {
7186     EL_CONVEYOR_BELT_1_LEFT,
7187     EL_CONVEYOR_BELT_2_LEFT,
7188     EL_CONVEYOR_BELT_3_LEFT,
7189     EL_CONVEYOR_BELT_4_LEFT
7190   };
7191
7192   return belt_base_element[belt_nr] + belt_dir_nr;
7193 }
7194
7195 int getBeltElementFromBeltNrAndBeltDir(int belt_nr, int belt_dir)
7196 {
7197   int belt_dir_nr = (belt_dir == MV_LEFT ? 0 : belt_dir == MV_RIGHT ? 2 : 1);
7198
7199   return getBeltElementFromBeltNrAndBeltDirNr(belt_nr, belt_dir_nr);
7200 }
7201
7202 int getBeltSwitchElementFromBeltNrAndBeltDirNr(int belt_nr, int belt_dir_nr)
7203 {
7204   static int belt_base_element[4] =
7205   {
7206     EL_CONVEYOR_BELT_1_SWITCH_LEFT,
7207     EL_CONVEYOR_BELT_2_SWITCH_LEFT,
7208     EL_CONVEYOR_BELT_3_SWITCH_LEFT,
7209     EL_CONVEYOR_BELT_4_SWITCH_LEFT
7210   };
7211
7212   return belt_base_element[belt_nr] + belt_dir_nr;
7213 }
7214
7215 int getBeltSwitchElementFromBeltNrAndBeltDir(int belt_nr, int belt_dir)
7216 {
7217   int belt_dir_nr = (belt_dir == MV_LEFT ? 0 : belt_dir == MV_RIGHT ? 2 : 1);
7218
7219   return getBeltSwitchElementFromBeltNrAndBeltDirNr(belt_nr, belt_dir_nr);
7220 }
7221
7222 int getNumActivePlayers_EM()
7223 {
7224   int num_players = 0;
7225   int i;
7226
7227   if (!tape.playing)
7228     return -1;
7229
7230   for (i = 0; i < MAX_PLAYERS; i++)
7231     if (tape.player_participates[i])
7232       num_players++;
7233
7234   return num_players;
7235 }
7236
7237 int getGameFrameDelay_EM(int native_em_game_frame_delay)
7238 {
7239   int game_frame_delay_value;
7240
7241   game_frame_delay_value =
7242     (tape.playing && tape.fast_forward ? FfwdFrameDelay :
7243      GameFrameDelay == GAME_FRAME_DELAY ? native_em_game_frame_delay :
7244      GameFrameDelay);
7245
7246   if (tape.playing && tape.warp_forward && !tape.pausing)
7247     game_frame_delay_value = 0;
7248
7249   return game_frame_delay_value;
7250 }
7251
7252 unsigned int InitRND(int seed)
7253 {
7254   if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
7255     return InitEngineRandom_EM(seed);
7256   else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
7257     return InitEngineRandom_SP(seed);
7258   else
7259     return InitEngineRandom_RND(seed);
7260 }
7261
7262 #if 1
7263 static struct Mapping_EM_to_RND_object object_mapping[TILE_MAX];
7264 static struct Mapping_EM_to_RND_player player_mapping[MAX_PLAYERS][SPR_MAX];
7265 #endif
7266
7267 inline static int get_effective_element_EM(int tile, int frame_em)
7268 {
7269   int element             = object_mapping[tile].element_rnd;
7270   int action              = object_mapping[tile].action;
7271   boolean is_backside     = object_mapping[tile].is_backside;
7272   boolean action_removing = (action == ACTION_DIGGING ||
7273                              action == ACTION_SNAPPING ||
7274                              action == ACTION_COLLECTING);
7275
7276   if (frame_em < 7)
7277   {
7278     switch (tile)
7279     {
7280       case Yacid_splash_eB:
7281       case Yacid_splash_wB:
7282         return (frame_em > 5 ? EL_EMPTY : element);
7283
7284 #if 0
7285         /* !!! FIX !!! */
7286       case Ydiamond_stone:
7287         //  if (!game.use_native_emc_graphics_engine)
7288         return EL_ROCK;
7289 #endif
7290
7291       default:
7292         return element;
7293     }
7294   }
7295   else  /* frame_em == 7 */
7296   {
7297     switch (tile)
7298     {
7299       case Yacid_splash_eB:
7300       case Yacid_splash_wB:
7301         return EL_EMPTY;
7302
7303       case Yemerald_stone:
7304         return EL_EMERALD;
7305
7306       case Ydiamond_stone:
7307         return EL_ROCK;
7308
7309       case Xdrip_stretch:
7310       case Xdrip_stretchB:
7311       case Ydrip_s1:
7312       case Ydrip_s1B:
7313       case Xball_1B:
7314       case Xball_2:
7315       case Xball_2B:
7316       case Yball_eat:
7317       case Ykey_1_eat:
7318       case Ykey_2_eat:
7319       case Ykey_3_eat:
7320       case Ykey_4_eat:
7321       case Ykey_5_eat:
7322       case Ykey_6_eat:
7323       case Ykey_7_eat:
7324       case Ykey_8_eat:
7325       case Ylenses_eat:
7326       case Ymagnify_eat:
7327       case Ygrass_eat:
7328       case Ydirt_eat:
7329       case Xsand_stonein_1:
7330       case Xsand_stonein_2:
7331       case Xsand_stonein_3:
7332       case Xsand_stonein_4:
7333         return element;
7334
7335       default:
7336         return (is_backside || action_removing ? EL_EMPTY : element);
7337     }
7338   }
7339 }
7340
7341 inline static boolean check_linear_animation_EM(int tile)
7342 {
7343   switch (tile)
7344   {
7345     case Xsand_stonesand_1:
7346     case Xsand_stonesand_quickout_1:
7347     case Xsand_sandstone_1:
7348     case Xsand_stonein_1:
7349     case Xsand_stoneout_1:
7350     case Xboom_1:
7351     case Xdynamite_1:
7352     case Ybug_w_n:
7353     case Ybug_n_e:
7354     case Ybug_e_s:
7355     case Ybug_s_w:
7356     case Ybug_e_n:
7357     case Ybug_s_e:
7358     case Ybug_w_s:
7359     case Ybug_n_w:
7360     case Ytank_w_n:
7361     case Ytank_n_e:
7362     case Ytank_e_s:
7363     case Ytank_s_w:
7364     case Ytank_e_n:
7365     case Ytank_s_e:
7366     case Ytank_w_s:
7367     case Ytank_n_w:
7368 #if 1
7369     case Yacid_splash_eB:
7370     case Yacid_splash_wB:
7371     case Yemerald_stone:
7372 #endif
7373       return TRUE;
7374   }
7375
7376   return FALSE;
7377 }
7378
7379 inline static void set_crumbled_graphics_EM(struct GraphicInfo_EM *g_em,
7380                                             boolean has_crumbled_graphics,
7381                                             int crumbled, int sync_frame)
7382 {
7383   /* if element can be crumbled, but certain action graphics are just empty
7384      space (like instantly snapping sand to empty space in 1 frame), do not
7385      treat these empty space graphics as crumbled graphics in EMC engine */
7386   if (crumbled == IMG_EMPTY_SPACE)
7387     has_crumbled_graphics = FALSE;
7388
7389   if (has_crumbled_graphics)
7390   {
7391     struct GraphicInfo *g_crumbled = &graphic_info[crumbled];
7392     int frame_crumbled = getAnimationFrame(g_crumbled->anim_frames,
7393                                            g_crumbled->anim_delay,
7394                                            g_crumbled->anim_mode,
7395                                            g_crumbled->anim_start_frame,
7396                                            sync_frame);
7397
7398     getGraphicSource(crumbled, frame_crumbled, &g_em->crumbled_bitmap,
7399                      &g_em->crumbled_src_x, &g_em->crumbled_src_y);
7400
7401     g_em->crumbled_border_size = graphic_info[crumbled].border_size;
7402
7403     g_em->has_crumbled_graphics = TRUE;
7404   }
7405   else
7406   {
7407     g_em->crumbled_bitmap = NULL;
7408     g_em->crumbled_src_x = 0;
7409     g_em->crumbled_src_y = 0;
7410     g_em->crumbled_border_size = 0;
7411
7412     g_em->has_crumbled_graphics = FALSE;
7413   }
7414 }
7415
7416 void ResetGfxAnimation_EM(int x, int y, int tile)
7417 {
7418   GfxFrame[x][y] = 0;
7419 }
7420
7421 void SetGfxAnimation_EM(struct GraphicInfo_EM *g_em,
7422                         int tile, int frame_em, int x, int y)
7423 {
7424   int action = object_mapping[tile].action;
7425 #if 1
7426   int direction = object_mapping[tile].direction;
7427   int effective_element = get_effective_element_EM(tile, frame_em);
7428   int graphic = (direction == MV_NONE ?
7429                  el_act2img(effective_element, action) :
7430                  el_act_dir2img(effective_element, action, direction));
7431   struct GraphicInfo *g = &graphic_info[graphic];
7432   int sync_frame;
7433 #endif
7434   boolean action_removing = (action == ACTION_DIGGING ||
7435                              action == ACTION_SNAPPING ||
7436                              action == ACTION_COLLECTING);
7437   boolean action_moving   = (action == ACTION_FALLING ||
7438                              action == ACTION_MOVING ||
7439                              action == ACTION_PUSHING ||
7440                              action == ACTION_EATING ||
7441                              action == ACTION_FILLING ||
7442                              action == ACTION_EMPTYING);
7443   boolean action_falling  = (action == ACTION_FALLING ||
7444                              action == ACTION_FILLING ||
7445                              action == ACTION_EMPTYING);
7446
7447   /* special case: graphic uses "2nd movement tile" and has defined
7448      7 frames for movement animation (or less) => use default graphic
7449      for last (8th) frame which ends the movement animation */
7450   if (g->double_movement && g->anim_frames < 8 && frame_em == 7)
7451   {
7452     action = ACTION_DEFAULT;    /* (keep action_* unchanged for now) */
7453     graphic = (direction == MV_NONE ?
7454                el_act2img(effective_element, action) :
7455                el_act_dir2img(effective_element, action, direction));
7456
7457     g = &graphic_info[graphic];
7458   }
7459
7460 #if 0
7461   if (tile == Xsand_stonesand_1 ||
7462       tile == Xsand_stonesand_2 ||
7463       tile == Xsand_stonesand_3 ||
7464       tile == Xsand_stonesand_4)
7465     printf("::: 1: quicksand frame %d [%d]\n", GfxFrame[x][y], tile);
7466 #endif
7467
7468 #if 1
7469   if ((action_removing || check_linear_animation_EM(tile)) && frame_em == 0)
7470   {
7471     GfxFrame[x][y] = 0;
7472
7473     // printf("::: resetting... [%d]\n", tile);
7474   }
7475 #else
7476   if (action_removing || check_linear_animation_EM(tile))
7477   {
7478     GfxFrame[x][y] = frame_em;
7479
7480     // printf("::: resetting... [%d]\n", tile);
7481   }
7482 #endif
7483   else if (action_moving)
7484   {
7485     boolean is_backside = object_mapping[tile].is_backside;
7486
7487     if (is_backside)
7488     {
7489       int direction = object_mapping[tile].direction;
7490       int move_dir = (action_falling ? MV_DOWN : direction);
7491
7492       GfxFrame[x][y]++;
7493
7494 #if 1
7495       /* !!! TEST !!! NEW !!! DOES NOT WORK RIGHT YET !!! */
7496       if (g->double_movement && frame_em == 0)
7497       {
7498         GfxFrame[x][y] = 0;
7499
7500         // printf("::: resetting... [%d]\n", tile);
7501       }
7502 #endif
7503
7504       if (move_dir == MV_LEFT)
7505         GfxFrame[x - 1][y] = GfxFrame[x][y];
7506       else if (move_dir == MV_RIGHT)
7507         GfxFrame[x + 1][y] = GfxFrame[x][y];
7508       else if (move_dir == MV_UP)
7509         GfxFrame[x][y - 1] = GfxFrame[x][y];
7510       else if (move_dir == MV_DOWN)
7511         GfxFrame[x][y + 1] = GfxFrame[x][y];
7512     }
7513   }
7514   else
7515   {
7516     GfxFrame[x][y]++;
7517
7518     /* special case: animation for Xsand_stonesand_quickout_1/2 twice as fast */
7519     if (tile == Xsand_stonesand_quickout_1 ||
7520         tile == Xsand_stonesand_quickout_2)
7521       GfxFrame[x][y]++;
7522   }
7523
7524 #if 0
7525   if (tile == Xsand_stonesand_1 ||
7526       tile == Xsand_stonesand_2 ||
7527       tile == Xsand_stonesand_3 ||
7528       tile == Xsand_stonesand_4)
7529     printf("::: 2: quicksand frame %d [%d]\n", GfxFrame[x][y], tile);
7530 #endif
7531
7532 #if 1
7533   if (graphic_info[graphic].anim_global_sync)
7534     sync_frame = FrameCounter;
7535   else if (IN_FIELD(x, y, MAX_LEV_FIELDX, MAX_LEV_FIELDY))
7536     sync_frame = GfxFrame[x][y];
7537   else
7538     sync_frame = 0;     /* playfield border (pseudo steel) */
7539
7540   SetRandomAnimationValue(x, y);
7541
7542   int frame = getAnimationFrame(g->anim_frames,
7543                                 g->anim_delay,
7544                                 g->anim_mode,
7545                                 g->anim_start_frame,
7546                                 sync_frame);
7547
7548   g_em->unique_identifier =
7549     (graphic << 16) | ((frame % 8) << 12) | (g_em->width << 6) | g_em->height;
7550 #endif
7551 }
7552
7553 void getGraphicSourceObjectExt_EM(struct GraphicInfo_EM *g_em,
7554                                   int tile, int frame_em, int x, int y)
7555 {
7556   int action = object_mapping[tile].action;
7557   int direction = object_mapping[tile].direction;
7558   boolean is_backside = object_mapping[tile].is_backside;
7559   int effective_element = get_effective_element_EM(tile, frame_em);
7560 #if 1
7561   int effective_action = action;
7562 #else
7563   int effective_action = (frame_em < 7 ? action : ACTION_DEFAULT);
7564 #endif
7565   int graphic = (direction == MV_NONE ?
7566                  el_act2img(effective_element, effective_action) :
7567                  el_act_dir2img(effective_element, effective_action,
7568                                 direction));
7569   int crumbled = (direction == MV_NONE ?
7570                   el_act2crm(effective_element, effective_action) :
7571                   el_act_dir2crm(effective_element, effective_action,
7572                                  direction));
7573   int base_graphic = el_act2img(effective_element, ACTION_DEFAULT);
7574   int base_crumbled = el_act2crm(effective_element, ACTION_DEFAULT);
7575   boolean has_crumbled_graphics = (base_crumbled != base_graphic);
7576   struct GraphicInfo *g = &graphic_info[graphic];
7577 #if 0
7578   struct GraphicInfo *g_crumbled = &graphic_info[crumbled];
7579 #endif
7580   int sync_frame;
7581
7582   /* special case: graphic uses "2nd movement tile" and has defined
7583      7 frames for movement animation (or less) => use default graphic
7584      for last (8th) frame which ends the movement animation */
7585   if (g->double_movement && g->anim_frames < 8 && frame_em == 7)
7586   {
7587     effective_action = ACTION_DEFAULT;
7588     graphic = (direction == MV_NONE ?
7589                el_act2img(effective_element, effective_action) :
7590                el_act_dir2img(effective_element, effective_action,
7591                               direction));
7592     crumbled = (direction == MV_NONE ?
7593                 el_act2crm(effective_element, effective_action) :
7594                 el_act_dir2crm(effective_element, effective_action,
7595                                direction));
7596
7597     g = &graphic_info[graphic];
7598   }
7599
7600 #if 0
7601   if (frame_em == 7)
7602     return;
7603 #endif
7604
7605
7606 #if 0
7607   if (frame_em == 0)    /* reset animation frame for certain elements */
7608   {
7609     if (check_linear_animation_EM(tile))
7610       GfxFrame[x][y] = 0;
7611   }
7612 #endif
7613
7614   if (graphic_info[graphic].anim_global_sync)
7615     sync_frame = FrameCounter;
7616   else if (IN_FIELD(x, y, MAX_LEV_FIELDX, MAX_LEV_FIELDY))
7617     sync_frame = GfxFrame[x][y];
7618   else
7619     sync_frame = 0;     /* playfield border (pseudo steel) */
7620
7621   SetRandomAnimationValue(x, y);
7622
7623 #if 0
7624   int i = tile;
7625   int j = frame_em;
7626   int xxx_sync_frame = (i == Xdrip_stretch ? 7 :
7627                         i == Xdrip_stretchB ? 7 :
7628                         i == Ydrip_s2 ? j + 8 :
7629                         i == Ydrip_s2B ? j + 8 :
7630                         i == Xacid_1 ? 0 :
7631                         i == Xacid_2 ? 10 :
7632                         i == Xacid_3 ? 20 :
7633                         i == Xacid_4 ? 30 :
7634                         i == Xacid_5 ? 40 :
7635                         i == Xacid_6 ? 50 :
7636                         i == Xacid_7 ? 60 :
7637                         i == Xacid_8 ? 70 :
7638                         i == Xfake_acid_1 ? 0 :
7639                         i == Xfake_acid_2 ? 10 :
7640                         i == Xfake_acid_3 ? 20 :
7641                         i == Xfake_acid_4 ? 30 :
7642                         i == Xfake_acid_5 ? 40 :
7643                         i == Xfake_acid_6 ? 50 :
7644                         i == Xfake_acid_7 ? 60 :
7645                         i == Xfake_acid_8 ? 70 :
7646                         i == Xball_2 ? 7 :
7647                         i == Xball_2B ? j + 8 :
7648                         i == Yball_eat ? j + 1 :
7649                         i == Ykey_1_eat ? j + 1 :
7650                         i == Ykey_2_eat ? j + 1 :
7651                         i == Ykey_3_eat ? j + 1 :
7652                         i == Ykey_4_eat ? j + 1 :
7653                         i == Ykey_5_eat ? j + 1 :
7654                         i == Ykey_6_eat ? j + 1 :
7655                         i == Ykey_7_eat ? j + 1 :
7656                         i == Ykey_8_eat ? j + 1 :
7657                         i == Ylenses_eat ? j + 1 :
7658                         i == Ymagnify_eat ? j + 1 :
7659                         i == Ygrass_eat ? j + 1 :
7660                         i == Ydirt_eat ? j + 1 :
7661                         i == Xamoeba_1 ? 0 :
7662                         i == Xamoeba_2 ? 1 :
7663                         i == Xamoeba_3 ? 2 :
7664                         i == Xamoeba_4 ? 3 :
7665                         i == Xamoeba_5 ? 0 :
7666                         i == Xamoeba_6 ? 1 :
7667                         i == Xamoeba_7 ? 2 :
7668                         i == Xamoeba_8 ? 3 :
7669                         i == Xexit_2 ? j + 8 :
7670                         i == Xexit_3 ? j + 16 :
7671                         i == Xdynamite_1 ? 0 :
7672                         i == Xdynamite_2 ? 8 :
7673                         i == Xdynamite_3 ? 16 :
7674                         i == Xdynamite_4 ? 24 :
7675                         i == Xsand_stonein_1 ? j + 1 :
7676                         i == Xsand_stonein_2 ? j + 9 :
7677                         i == Xsand_stonein_3 ? j + 17 :
7678                         i == Xsand_stonein_4 ? j + 25 :
7679                         i == Xsand_stoneout_1 && j == 0 ? 0 :
7680                         i == Xsand_stoneout_1 && j == 1 ? 0 :
7681                         i == Xsand_stoneout_1 && j == 2 ? 1 :
7682                         i == Xsand_stoneout_1 && j == 3 ? 2 :
7683                         i == Xsand_stoneout_1 && j == 4 ? 2 :
7684                         i == Xsand_stoneout_1 && j == 5 ? 3 :
7685                         i == Xsand_stoneout_1 && j == 6 ? 4 :
7686                         i == Xsand_stoneout_1 && j == 7 ? 4 :
7687                         i == Xsand_stoneout_2 && j == 0 ? 5 :
7688                         i == Xsand_stoneout_2 && j == 1 ? 6 :
7689                         i == Xsand_stoneout_2 && j == 2 ? 7 :
7690                         i == Xsand_stoneout_2 && j == 3 ? 8 :
7691                         i == Xsand_stoneout_2 && j == 4 ? 9 :
7692                         i == Xsand_stoneout_2 && j == 5 ? 11 :
7693                         i == Xsand_stoneout_2 && j == 6 ? 13 :
7694                         i == Xsand_stoneout_2 && j == 7 ? 15 :
7695                         i == Xboom_bug && j == 1 ? 2 :
7696                         i == Xboom_bug && j == 2 ? 2 :
7697                         i == Xboom_bug && j == 3 ? 4 :
7698                         i == Xboom_bug && j == 4 ? 4 :
7699                         i == Xboom_bug && j == 5 ? 2 :
7700                         i == Xboom_bug && j == 6 ? 2 :
7701                         i == Xboom_bug && j == 7 ? 0 :
7702                         i == Xboom_bomb && j == 1 ? 2 :
7703                         i == Xboom_bomb && j == 2 ? 2 :
7704                         i == Xboom_bomb && j == 3 ? 4 :
7705                         i == Xboom_bomb && j == 4 ? 4 :
7706                         i == Xboom_bomb && j == 5 ? 2 :
7707                         i == Xboom_bomb && j == 6 ? 2 :
7708                         i == Xboom_bomb && j == 7 ? 0 :
7709                         i == Xboom_android && j == 7 ? 6 :
7710                         i == Xboom_1 && j == 1 ? 2 :
7711                         i == Xboom_1 && j == 2 ? 2 :
7712                         i == Xboom_1 && j == 3 ? 4 :
7713                         i == Xboom_1 && j == 4 ? 4 :
7714                         i == Xboom_1 && j == 5 ? 6 :
7715                         i == Xboom_1 && j == 6 ? 6 :
7716                         i == Xboom_1 && j == 7 ? 8 :
7717                         i == Xboom_2 && j == 0 ? 8 :
7718                         i == Xboom_2 && j == 1 ? 8 :
7719                         i == Xboom_2 && j == 2 ? 10 :
7720                         i == Xboom_2 && j == 3 ? 10 :
7721                         i == Xboom_2 && j == 4 ? 10 :
7722                         i == Xboom_2 && j == 5 ? 12 :
7723                         i == Xboom_2 && j == 6 ? 12 :
7724                         i == Xboom_2 && j == 7 ? 12 :
7725 #if 0
7726                         special_animation && j == 4 ? 3 :
7727                         effective_action != action ? 0 :
7728 #endif
7729                         j);
7730 #endif
7731
7732 #if 0
7733   int xxx_effective_action;
7734   int xxx_has_action_graphics;
7735
7736   {
7737     int element = object_mapping[i].element_rnd;
7738     int action = object_mapping[i].action;
7739     int direction = object_mapping[i].direction;
7740     boolean is_backside = object_mapping[i].is_backside;
7741 #if 0
7742     boolean action_removing = (action == ACTION_DIGGING ||
7743                                action == ACTION_SNAPPING ||
7744                                action == ACTION_COLLECTING);
7745 #endif
7746     boolean action_exploding = ((action == ACTION_EXPLODING ||
7747                                  action == ACTION_SMASHED_BY_ROCK ||
7748                                  action == ACTION_SMASHED_BY_SPRING) &&
7749                                 element != EL_DIAMOND);
7750     boolean action_active = (action == ACTION_ACTIVE);
7751     boolean action_other = (action == ACTION_OTHER);
7752
7753     {
7754 #if 1
7755       int effective_element = get_effective_element_EM(i, j);
7756 #else
7757       int effective_element = (j > 5 && i == Yacid_splash_eB ? EL_EMPTY :
7758                                j > 5 && i == Yacid_splash_wB ? EL_EMPTY :
7759                                j < 7 ? element :
7760                                i == Xdrip_stretch ? element :
7761                                i == Xdrip_stretchB ? element :
7762                                i == Ydrip_s1 ? element :
7763                                i == Ydrip_s1B ? element :
7764                                i == Xball_1B ? element :
7765                                i == Xball_2 ? element :
7766                                i == Xball_2B ? element :
7767                                i == Yball_eat ? element :
7768                                i == Ykey_1_eat ? element :
7769                                i == Ykey_2_eat ? element :
7770                                i == Ykey_3_eat ? element :
7771                                i == Ykey_4_eat ? element :
7772                                i == Ykey_5_eat ? element :
7773                                i == Ykey_6_eat ? element :
7774                                i == Ykey_7_eat ? element :
7775                                i == Ykey_8_eat ? element :
7776                                i == Ylenses_eat ? element :
7777                                i == Ymagnify_eat ? element :
7778                                i == Ygrass_eat ? element :
7779                                i == Ydirt_eat ? element :
7780                                i == Yemerald_stone ? EL_EMERALD :
7781                                i == Ydiamond_stone ? EL_ROCK :
7782                                i == Xsand_stonein_1 ? element :
7783                                i == Xsand_stonein_2 ? element :
7784                                i == Xsand_stonein_3 ? element :
7785                                i == Xsand_stonein_4 ? element :
7786                                is_backside ? EL_EMPTY :
7787                                action_removing ? EL_EMPTY :
7788                                element);
7789 #endif
7790       int effective_action = (j < 7 ? action :
7791                               i == Xdrip_stretch ? action :
7792                               i == Xdrip_stretchB ? action :
7793                               i == Ydrip_s1 ? action :
7794                               i == Ydrip_s1B ? action :
7795                               i == Xball_1B ? action :
7796                               i == Xball_2 ? action :
7797                               i == Xball_2B ? action :
7798                               i == Yball_eat ? action :
7799                               i == Ykey_1_eat ? action :
7800                               i == Ykey_2_eat ? action :
7801                               i == Ykey_3_eat ? action :
7802                               i == Ykey_4_eat ? action :
7803                               i == Ykey_5_eat ? action :
7804                               i == Ykey_6_eat ? action :
7805                               i == Ykey_7_eat ? action :
7806                               i == Ykey_8_eat ? action :
7807                               i == Ylenses_eat ? action :
7808                               i == Ymagnify_eat ? action :
7809                               i == Ygrass_eat ? action :
7810                               i == Ydirt_eat ? action :
7811                               i == Xsand_stonein_1 ? action :
7812                               i == Xsand_stonein_2 ? action :
7813                               i == Xsand_stonein_3 ? action :
7814                               i == Xsand_stonein_4 ? action :
7815                               i == Xsand_stoneout_1 ? action :
7816                               i == Xsand_stoneout_2 ? action :
7817                               i == Xboom_android ? ACTION_EXPLODING :
7818                               action_exploding ? ACTION_EXPLODING :
7819                               action_active ? action :
7820                               action_other ? action :
7821                               ACTION_DEFAULT);
7822       int graphic = (el_act_dir2img(effective_element, effective_action,
7823                                     direction));
7824       int crumbled = (el_act_dir2crm(effective_element, effective_action,
7825                                      direction));
7826       int base_graphic = el_act2img(effective_element, ACTION_DEFAULT);
7827       int base_crumbled = el_act2crm(effective_element, ACTION_DEFAULT);
7828       boolean has_action_graphics = (graphic != base_graphic);
7829       boolean has_crumbled_graphics = (base_crumbled != base_graphic);
7830       struct GraphicInfo *g = &graphic_info[graphic];
7831 #if 0
7832       struct GraphicInfo *g_crumbled = &graphic_info[crumbled];
7833 #endif
7834       struct GraphicInfo_EM *g_em = &graphic_info_em_object[i][7 - j];
7835       Bitmap *src_bitmap;
7836       int src_x, src_y;
7837       /* ensure to get symmetric 3-frame, 2-delay animations as used in EM */
7838       boolean special_animation = (action != ACTION_DEFAULT &&
7839                                    g->anim_frames == 3 &&
7840                                    g->anim_delay == 2 &&
7841                                    g->anim_mode & ANIM_LINEAR);
7842       xxx_sync_frame = (i == Xdrip_stretch ? 7 :
7843                         i == Xdrip_stretchB ? 7 :
7844                         i == Ydrip_s2 ? j + 8 :
7845                         i == Ydrip_s2B ? j + 8 :
7846                         i == Xacid_1 ? 0 :
7847                         i == Xacid_2 ? 10 :
7848                         i == Xacid_3 ? 20 :
7849                         i == Xacid_4 ? 30 :
7850                         i == Xacid_5 ? 40 :
7851                         i == Xacid_6 ? 50 :
7852                         i == Xacid_7 ? 60 :
7853                         i == Xacid_8 ? 70 :
7854                         i == Xfake_acid_1 ? 0 :
7855                         i == Xfake_acid_2 ? 10 :
7856                         i == Xfake_acid_3 ? 20 :
7857                         i == Xfake_acid_4 ? 30 :
7858                         i == Xfake_acid_5 ? 40 :
7859                         i == Xfake_acid_6 ? 50 :
7860                         i == Xfake_acid_7 ? 60 :
7861                         i == Xfake_acid_8 ? 70 :
7862                         i == Xball_2 ? 7 :
7863                         i == Xball_2B ? j + 8 :
7864                         i == Yball_eat ? j + 1 :
7865                         i == Ykey_1_eat ? j + 1 :
7866                         i == Ykey_2_eat ? j + 1 :
7867                         i == Ykey_3_eat ? j + 1 :
7868                         i == Ykey_4_eat ? j + 1 :
7869                         i == Ykey_5_eat ? j + 1 :
7870                         i == Ykey_6_eat ? j + 1 :
7871                         i == Ykey_7_eat ? j + 1 :
7872                         i == Ykey_8_eat ? j + 1 :
7873                         i == Ylenses_eat ? j + 1 :
7874                         i == Ymagnify_eat ? j + 1 :
7875                         i == Ygrass_eat ? j + 1 :
7876                         i == Ydirt_eat ? j + 1 :
7877                         i == Xamoeba_1 ? 0 :
7878                         i == Xamoeba_2 ? 1 :
7879                         i == Xamoeba_3 ? 2 :
7880                         i == Xamoeba_4 ? 3 :
7881                         i == Xamoeba_5 ? 0 :
7882                         i == Xamoeba_6 ? 1 :
7883                         i == Xamoeba_7 ? 2 :
7884                         i == Xamoeba_8 ? 3 :
7885                         i == Xexit_2 ? j + 8 :
7886                         i == Xexit_3 ? j + 16 :
7887                         i == Xdynamite_1 ? 0 :
7888                         i == Xdynamite_2 ? 8 :
7889                         i == Xdynamite_3 ? 16 :
7890                         i == Xdynamite_4 ? 24 :
7891                         i == Xsand_stonein_1 ? j + 1 :
7892                         i == Xsand_stonein_2 ? j + 9 :
7893                         i == Xsand_stonein_3 ? j + 17 :
7894                         i == Xsand_stonein_4 ? j + 25 :
7895                         i == Xsand_stoneout_1 && j == 0 ? 0 :
7896                         i == Xsand_stoneout_1 && j == 1 ? 0 :
7897                         i == Xsand_stoneout_1 && j == 2 ? 1 :
7898                         i == Xsand_stoneout_1 && j == 3 ? 2 :
7899                         i == Xsand_stoneout_1 && j == 4 ? 2 :
7900                         i == Xsand_stoneout_1 && j == 5 ? 3 :
7901                         i == Xsand_stoneout_1 && j == 6 ? 4 :
7902                         i == Xsand_stoneout_1 && j == 7 ? 4 :
7903                         i == Xsand_stoneout_2 && j == 0 ? 5 :
7904                         i == Xsand_stoneout_2 && j == 1 ? 6 :
7905                         i == Xsand_stoneout_2 && j == 2 ? 7 :
7906                         i == Xsand_stoneout_2 && j == 3 ? 8 :
7907                         i == Xsand_stoneout_2 && j == 4 ? 9 :
7908                         i == Xsand_stoneout_2 && j == 5 ? 11 :
7909                         i == Xsand_stoneout_2 && j == 6 ? 13 :
7910                         i == Xsand_stoneout_2 && j == 7 ? 15 :
7911                         i == Xboom_bug && j == 1 ? 2 :
7912                         i == Xboom_bug && j == 2 ? 2 :
7913                         i == Xboom_bug && j == 3 ? 4 :
7914                         i == Xboom_bug && j == 4 ? 4 :
7915                         i == Xboom_bug && j == 5 ? 2 :
7916                         i == Xboom_bug && j == 6 ? 2 :
7917                         i == Xboom_bug && j == 7 ? 0 :
7918                         i == Xboom_bomb && j == 1 ? 2 :
7919                         i == Xboom_bomb && j == 2 ? 2 :
7920                         i == Xboom_bomb && j == 3 ? 4 :
7921                         i == Xboom_bomb && j == 4 ? 4 :
7922                         i == Xboom_bomb && j == 5 ? 2 :
7923                         i == Xboom_bomb && j == 6 ? 2 :
7924                         i == Xboom_bomb && j == 7 ? 0 :
7925                         i == Xboom_android && j == 7 ? 6 :
7926                         i == Xboom_1 && j == 1 ? 2 :
7927                         i == Xboom_1 && j == 2 ? 2 :
7928                         i == Xboom_1 && j == 3 ? 4 :
7929                         i == Xboom_1 && j == 4 ? 4 :
7930                         i == Xboom_1 && j == 5 ? 6 :
7931                         i == Xboom_1 && j == 6 ? 6 :
7932                         i == Xboom_1 && j == 7 ? 8 :
7933                         i == Xboom_2 && j == 0 ? 8 :
7934                         i == Xboom_2 && j == 1 ? 8 :
7935                         i == Xboom_2 && j == 2 ? 10 :
7936                         i == Xboom_2 && j == 3 ? 10 :
7937                         i == Xboom_2 && j == 4 ? 10 :
7938                         i == Xboom_2 && j == 5 ? 12 :
7939                         i == Xboom_2 && j == 6 ? 12 :
7940                         i == Xboom_2 && j == 7 ? 12 :
7941                         special_animation && j == 4 ? 3 :
7942                         effective_action != action ? 0 :
7943                         j);
7944
7945       xxx_effective_action = effective_action;
7946       xxx_has_action_graphics = has_action_graphics;
7947     }
7948   }
7949 #endif
7950
7951   int frame = getAnimationFrame(g->anim_frames,
7952                                 g->anim_delay,
7953                                 g->anim_mode,
7954                                 g->anim_start_frame,
7955                                 sync_frame);
7956
7957
7958 #if 0
7959   return;
7960 #endif
7961
7962 #if 0
7963   if (frame_em == 7)
7964     return;
7965 #endif
7966
7967 #if 0
7968   int old_src_x = g_em->src_x;
7969   int old_src_y = g_em->src_y;
7970 #endif
7971
7972 #if 1
7973   getGraphicSourceExt(graphic, frame, &g_em->bitmap, &g_em->src_x, &g_em->src_y,
7974                       g->double_movement && is_backside);
7975 #else
7976   getGraphicSourceExt(graphic, frame, &g_em->bitmap,
7977                       &g_em->src_x, &g_em->src_y, FALSE);
7978 #endif
7979
7980
7981 #if 0
7982   if (tile == Ydiamond_stone)
7983     printf("::: stone smashing diamond... %d: %d, %d, %d, %d, %d -> %d [%d, %d, %d, %d, %d, %d] [%d]\n",
7984            frame_em,
7985            g->anim_frames,
7986            g->anim_delay,
7987            g->anim_mode,
7988            g->anim_start_frame,
7989            sync_frame,
7990            frame,
7991            g_em->src_x, g_em->src_y,
7992            g_em->src_offset_x, g_em->src_offset_y,
7993            g_em->dst_offset_x, g_em->dst_offset_y,
7994            graphic);
7995 #endif
7996
7997
7998 #if 0
7999   return;
8000 #endif
8001
8002 #if 0
8003   if (frame_em == 7)
8004   {
8005     if (graphic == IMG_BUG_MOVING_RIGHT)
8006       printf("::: %d, %d, %d: %d, %d [%d, %d -> %d, %d]\n", graphic, x, y,
8007              g->double_movement, is_backside,
8008              old_src_x, old_src_y, g_em->src_x, g_em->src_y);
8009
8010     return;
8011   }
8012 #endif
8013
8014
8015 #if 0
8016   g_em->src_offset_x = 0;
8017   g_em->src_offset_y = 0;
8018   g_em->dst_offset_x = 0;
8019   g_em->dst_offset_y = 0;
8020   g_em->width  = TILEX;
8021   g_em->height = TILEY;
8022
8023   g_em->preserve_background = FALSE;
8024 #endif
8025
8026   /* (updating the "crumbled" graphic definitions is probably not really needed,
8027      as animations for crumbled graphics can't be longer than one EMC cycle) */
8028 #if 1
8029   set_crumbled_graphics_EM(g_em, has_crumbled_graphics, crumbled,
8030                            sync_frame);
8031
8032 #else
8033
8034   g_em->crumbled_bitmap = NULL;
8035   g_em->crumbled_src_x = 0;
8036   g_em->crumbled_src_y = 0;
8037
8038   g_em->has_crumbled_graphics = FALSE;
8039
8040   if (has_crumbled_graphics && crumbled != IMG_EMPTY_SPACE)
8041   {
8042     int frame_crumbled = getAnimationFrame(g_crumbled->anim_frames,
8043                                            g_crumbled->anim_delay,
8044                                            g_crumbled->anim_mode,
8045                                            g_crumbled->anim_start_frame,
8046                                            sync_frame);
8047
8048     getGraphicSource(crumbled, frame_crumbled, &g_em->crumbled_bitmap,
8049                      &g_em->crumbled_src_x, &g_em->crumbled_src_y);
8050
8051     g_em->has_crumbled_graphics = TRUE;
8052   }
8053 #endif
8054
8055 #if 0
8056  {
8057    int effective_action = xxx_effective_action;
8058    int has_action_graphics = xxx_has_action_graphics;
8059
8060       if ((!g->double_movement && (effective_action == ACTION_FALLING ||
8061                                    effective_action == ACTION_MOVING  ||
8062                                    effective_action == ACTION_PUSHING ||
8063                                    effective_action == ACTION_EATING)) ||
8064           (!has_action_graphics && (effective_action == ACTION_FILLING ||
8065                                     effective_action == ACTION_EMPTYING)))
8066       {
8067         int move_dir =
8068           (effective_action == ACTION_FALLING ||
8069            effective_action == ACTION_FILLING ||
8070            effective_action == ACTION_EMPTYING ? MV_DOWN : direction);
8071         int dx = (move_dir == MV_LEFT ? -1 : move_dir == MV_RIGHT ? 1 : 0);
8072         int dy = (move_dir == MV_UP   ? -1 : move_dir == MV_DOWN  ? 1 : 0);
8073         int num_steps = (i == Ydrip_s1  ? 16 :
8074                          i == Ydrip_s1B ? 16 :
8075                          i == Ydrip_s2  ? 16 :
8076                          i == Ydrip_s2B ? 16 :
8077                          i == Xsand_stonein_1 ? 32 :
8078                          i == Xsand_stonein_2 ? 32 :
8079                          i == Xsand_stonein_3 ? 32 :
8080                          i == Xsand_stonein_4 ? 32 :
8081                          i == Xsand_stoneout_1 ? 16 :
8082                          i == Xsand_stoneout_2 ? 16 : 8);
8083         int cx = ABS(dx) * (TILEX / num_steps);
8084         int cy = ABS(dy) * (TILEY / num_steps);
8085         int step_frame = (i == Ydrip_s2         ? j + 8 :
8086                           i == Ydrip_s2B        ? j + 8 :
8087                           i == Xsand_stonein_2  ? j + 8 :
8088                           i == Xsand_stonein_3  ? j + 16 :
8089                           i == Xsand_stonein_4  ? j + 24 :
8090                           i == Xsand_stoneout_2 ? j + 8 : j) + 1;
8091         int step = (is_backside ? step_frame : num_steps - step_frame);
8092
8093         if (is_backside)        /* tile where movement starts */
8094         {
8095           if (dx < 0 || dy < 0)
8096           {
8097             g_em->src_offset_x = cx * step;
8098             g_em->src_offset_y = cy * step;
8099           }
8100           else
8101           {
8102             g_em->dst_offset_x = cx * step;
8103             g_em->dst_offset_y = cy * step;
8104           }
8105         }
8106         else                    /* tile where movement ends */
8107         {
8108           if (dx < 0 || dy < 0)
8109           {
8110             g_em->dst_offset_x = cx * step;
8111             g_em->dst_offset_y = cy * step;
8112           }
8113           else
8114           {
8115             g_em->src_offset_x = cx * step;
8116             g_em->src_offset_y = cy * step;
8117           }
8118         }
8119
8120         g_em->width  = TILEX - cx * step;
8121         g_em->height = TILEY - cy * step;
8122       }
8123
8124       /* create unique graphic identifier to decide if tile must be redrawn */
8125       /* bit 31 - 16 (16 bit): EM style graphic
8126          bit 15 - 12 ( 4 bit): EM style frame
8127          bit 11 -  6 ( 6 bit): graphic width
8128          bit  5 -  0 ( 6 bit): graphic height */
8129       g_em->unique_identifier =
8130         (graphic << 16) | (frame << 12) | (g_em->width << 6) | g_em->height;
8131  }
8132 #endif
8133
8134 }
8135
8136 void getGraphicSourcePlayerExt_EM(struct GraphicInfo_EM *g_em,
8137                                   int player_nr, int anim, int frame_em)
8138 {
8139   int element   = player_mapping[player_nr][anim].element_rnd;
8140   int action    = player_mapping[player_nr][anim].action;
8141   int direction = player_mapping[player_nr][anim].direction;
8142   int graphic = (direction == MV_NONE ?
8143                  el_act2img(element, action) :
8144                  el_act_dir2img(element, action, direction));
8145   struct GraphicInfo *g = &graphic_info[graphic];
8146   int sync_frame;
8147
8148   InitPlayerGfxAnimation(&stored_player[player_nr], action, direction);
8149
8150   stored_player[player_nr].StepFrame = frame_em;
8151
8152   sync_frame = stored_player[player_nr].Frame;
8153
8154   int frame = getAnimationFrame(g->anim_frames,
8155                                 g->anim_delay,
8156                                 g->anim_mode,
8157                                 g->anim_start_frame,
8158                                 sync_frame);
8159
8160   getGraphicSourceExt(graphic, frame, &g_em->bitmap,
8161                       &g_em->src_x, &g_em->src_y, FALSE);
8162
8163 #if 0
8164   printf("::: %d: %d, %d [%d]\n",
8165          player_nr,
8166          stored_player[player_nr].Frame,
8167          stored_player[player_nr].StepFrame,
8168          FrameCounter);
8169 #endif
8170 }
8171
8172 void InitGraphicInfo_EM(void)
8173 {
8174 #if 0
8175   struct Mapping_EM_to_RND_object object_mapping[TILE_MAX];
8176   struct Mapping_EM_to_RND_player player_mapping[MAX_PLAYERS][SPR_MAX];
8177 #endif
8178   int i, j, p;
8179
8180 #if DEBUG_EM_GFX
8181   int num_em_gfx_errors = 0;
8182
8183   if (graphic_info_em_object[0][0].bitmap == NULL)
8184   {
8185     /* EM graphics not yet initialized in em_open_all() */
8186
8187     return;
8188   }
8189
8190   printf("::: [4 errors can be ignored (1 x 'bomb', 3 x 'em_dynamite']\n");
8191 #endif
8192
8193   /* always start with reliable default values */
8194   for (i = 0; i < TILE_MAX; i++)
8195   {
8196     object_mapping[i].element_rnd = EL_UNKNOWN;
8197     object_mapping[i].is_backside = FALSE;
8198     object_mapping[i].action = ACTION_DEFAULT;
8199     object_mapping[i].direction = MV_NONE;
8200   }
8201
8202   /* always start with reliable default values */
8203   for (p = 0; p < MAX_PLAYERS; p++)
8204   {
8205     for (i = 0; i < SPR_MAX; i++)
8206     {
8207       player_mapping[p][i].element_rnd = EL_UNKNOWN;
8208       player_mapping[p][i].action = ACTION_DEFAULT;
8209       player_mapping[p][i].direction = MV_NONE;
8210     }
8211   }
8212
8213   for (i = 0; em_object_mapping_list[i].element_em != -1; i++)
8214   {
8215     int e = em_object_mapping_list[i].element_em;
8216
8217     object_mapping[e].element_rnd = em_object_mapping_list[i].element_rnd;
8218     object_mapping[e].is_backside = em_object_mapping_list[i].is_backside;
8219
8220     if (em_object_mapping_list[i].action != -1)
8221       object_mapping[e].action = em_object_mapping_list[i].action;
8222
8223     if (em_object_mapping_list[i].direction != -1)
8224       object_mapping[e].direction =
8225         MV_DIR_FROM_BIT(em_object_mapping_list[i].direction);
8226   }
8227
8228   for (i = 0; em_player_mapping_list[i].action_em != -1; i++)
8229   {
8230     int a = em_player_mapping_list[i].action_em;
8231     int p = em_player_mapping_list[i].player_nr;
8232
8233     player_mapping[p][a].element_rnd = em_player_mapping_list[i].element_rnd;
8234
8235     if (em_player_mapping_list[i].action != -1)
8236       player_mapping[p][a].action = em_player_mapping_list[i].action;
8237
8238     if (em_player_mapping_list[i].direction != -1)
8239       player_mapping[p][a].direction =
8240         MV_DIR_FROM_BIT(em_player_mapping_list[i].direction);
8241   }
8242
8243   for (i = 0; i < TILE_MAX; i++)
8244   {
8245     int element = object_mapping[i].element_rnd;
8246     int action = object_mapping[i].action;
8247     int direction = object_mapping[i].direction;
8248     boolean is_backside = object_mapping[i].is_backside;
8249 #if 0
8250     boolean action_removing = (action == ACTION_DIGGING ||
8251                                action == ACTION_SNAPPING ||
8252                                action == ACTION_COLLECTING);
8253 #endif
8254     boolean action_exploding = ((action == ACTION_EXPLODING ||
8255                                  action == ACTION_SMASHED_BY_ROCK ||
8256                                  action == ACTION_SMASHED_BY_SPRING) &&
8257                                 element != EL_DIAMOND);
8258     boolean action_active = (action == ACTION_ACTIVE);
8259     boolean action_other = (action == ACTION_OTHER);
8260
8261     for (j = 0; j < 8; j++)
8262     {
8263 #if 1
8264       int effective_element = get_effective_element_EM(i, j);
8265 #else
8266       int effective_element = (j > 5 && i == Yacid_splash_eB ? EL_EMPTY :
8267                                j > 5 && i == Yacid_splash_wB ? EL_EMPTY :
8268                                j < 7 ? element :
8269                                i == Xdrip_stretch ? element :
8270                                i == Xdrip_stretchB ? element :
8271                                i == Ydrip_s1 ? element :
8272                                i == Ydrip_s1B ? element :
8273                                i == Xball_1B ? element :
8274                                i == Xball_2 ? element :
8275                                i == Xball_2B ? element :
8276                                i == Yball_eat ? element :
8277                                i == Ykey_1_eat ? element :
8278                                i == Ykey_2_eat ? element :
8279                                i == Ykey_3_eat ? element :
8280                                i == Ykey_4_eat ? element :
8281                                i == Ykey_5_eat ? element :
8282                                i == Ykey_6_eat ? element :
8283                                i == Ykey_7_eat ? element :
8284                                i == Ykey_8_eat ? element :
8285                                i == Ylenses_eat ? element :
8286                                i == Ymagnify_eat ? element :
8287                                i == Ygrass_eat ? element :
8288                                i == Ydirt_eat ? element :
8289                                i == Yemerald_stone ? EL_EMERALD :
8290                                i == Ydiamond_stone ? EL_ROCK :
8291                                i == Xsand_stonein_1 ? element :
8292                                i == Xsand_stonein_2 ? element :
8293                                i == Xsand_stonein_3 ? element :
8294                                i == Xsand_stonein_4 ? element :
8295                                is_backside ? EL_EMPTY :
8296                                action_removing ? EL_EMPTY :
8297                                element);
8298 #endif
8299       int effective_action = (j < 7 ? action :
8300                               i == Xdrip_stretch ? action :
8301                               i == Xdrip_stretchB ? action :
8302                               i == Ydrip_s1 ? action :
8303                               i == Ydrip_s1B ? action :
8304                               i == Xball_1B ? action :
8305                               i == Xball_2 ? action :
8306                               i == Xball_2B ? action :
8307                               i == Yball_eat ? action :
8308                               i == Ykey_1_eat ? action :
8309                               i == Ykey_2_eat ? action :
8310                               i == Ykey_3_eat ? action :
8311                               i == Ykey_4_eat ? action :
8312                               i == Ykey_5_eat ? action :
8313                               i == Ykey_6_eat ? action :
8314                               i == Ykey_7_eat ? action :
8315                               i == Ykey_8_eat ? action :
8316                               i == Ylenses_eat ? action :
8317                               i == Ymagnify_eat ? action :
8318                               i == Ygrass_eat ? action :
8319                               i == Ydirt_eat ? action :
8320                               i == Xsand_stonein_1 ? action :
8321                               i == Xsand_stonein_2 ? action :
8322                               i == Xsand_stonein_3 ? action :
8323                               i == Xsand_stonein_4 ? action :
8324                               i == Xsand_stoneout_1 ? action :
8325                               i == Xsand_stoneout_2 ? action :
8326                               i == Xboom_android ? ACTION_EXPLODING :
8327                               action_exploding ? ACTION_EXPLODING :
8328                               action_active ? action :
8329                               action_other ? action :
8330                               ACTION_DEFAULT);
8331       int graphic = (el_act_dir2img(effective_element, effective_action,
8332                                     direction));
8333       int crumbled = (el_act_dir2crm(effective_element, effective_action,
8334                                      direction));
8335       int base_graphic = el_act2img(effective_element, ACTION_DEFAULT);
8336       int base_crumbled = el_act2crm(effective_element, ACTION_DEFAULT);
8337       boolean has_action_graphics = (graphic != base_graphic);
8338       boolean has_crumbled_graphics = (base_crumbled != base_graphic);
8339       struct GraphicInfo *g = &graphic_info[graphic];
8340 #if 0
8341       struct GraphicInfo *g_crumbled = &graphic_info[crumbled];
8342 #endif
8343       struct GraphicInfo_EM *g_em = &graphic_info_em_object[i][7 - j];
8344       Bitmap *src_bitmap;
8345       int src_x, src_y;
8346       /* ensure to get symmetric 3-frame, 2-delay animations as used in EM */
8347       boolean special_animation = (action != ACTION_DEFAULT &&
8348                                    g->anim_frames == 3 &&
8349                                    g->anim_delay == 2 &&
8350                                    g->anim_mode & ANIM_LINEAR);
8351       int sync_frame = (i == Xdrip_stretch ? 7 :
8352                         i == Xdrip_stretchB ? 7 :
8353                         i == Ydrip_s2 ? j + 8 :
8354                         i == Ydrip_s2B ? j + 8 :
8355                         i == Xacid_1 ? 0 :
8356                         i == Xacid_2 ? 10 :
8357                         i == Xacid_3 ? 20 :
8358                         i == Xacid_4 ? 30 :
8359                         i == Xacid_5 ? 40 :
8360                         i == Xacid_6 ? 50 :
8361                         i == Xacid_7 ? 60 :
8362                         i == Xacid_8 ? 70 :
8363                         i == Xfake_acid_1 ? 0 :
8364                         i == Xfake_acid_2 ? 10 :
8365                         i == Xfake_acid_3 ? 20 :
8366                         i == Xfake_acid_4 ? 30 :
8367                         i == Xfake_acid_5 ? 40 :
8368                         i == Xfake_acid_6 ? 50 :
8369                         i == Xfake_acid_7 ? 60 :
8370                         i == Xfake_acid_8 ? 70 :
8371                         i == Xball_2 ? 7 :
8372                         i == Xball_2B ? j + 8 :
8373                         i == Yball_eat ? j + 1 :
8374                         i == Ykey_1_eat ? j + 1 :
8375                         i == Ykey_2_eat ? j + 1 :
8376                         i == Ykey_3_eat ? j + 1 :
8377                         i == Ykey_4_eat ? j + 1 :
8378                         i == Ykey_5_eat ? j + 1 :
8379                         i == Ykey_6_eat ? j + 1 :
8380                         i == Ykey_7_eat ? j + 1 :
8381                         i == Ykey_8_eat ? j + 1 :
8382                         i == Ylenses_eat ? j + 1 :
8383                         i == Ymagnify_eat ? j + 1 :
8384                         i == Ygrass_eat ? j + 1 :
8385                         i == Ydirt_eat ? j + 1 :
8386                         i == Xamoeba_1 ? 0 :
8387                         i == Xamoeba_2 ? 1 :
8388                         i == Xamoeba_3 ? 2 :
8389                         i == Xamoeba_4 ? 3 :
8390                         i == Xamoeba_5 ? 0 :
8391                         i == Xamoeba_6 ? 1 :
8392                         i == Xamoeba_7 ? 2 :
8393                         i == Xamoeba_8 ? 3 :
8394                         i == Xexit_2 ? j + 8 :
8395                         i == Xexit_3 ? j + 16 :
8396                         i == Xdynamite_1 ? 0 :
8397                         i == Xdynamite_2 ? 8 :
8398                         i == Xdynamite_3 ? 16 :
8399                         i == Xdynamite_4 ? 24 :
8400                         i == Xsand_stonein_1 ? j + 1 :
8401                         i == Xsand_stonein_2 ? j + 9 :
8402                         i == Xsand_stonein_3 ? j + 17 :
8403                         i == Xsand_stonein_4 ? j + 25 :
8404                         i == Xsand_stoneout_1 && j == 0 ? 0 :
8405                         i == Xsand_stoneout_1 && j == 1 ? 0 :
8406                         i == Xsand_stoneout_1 && j == 2 ? 1 :
8407                         i == Xsand_stoneout_1 && j == 3 ? 2 :
8408                         i == Xsand_stoneout_1 && j == 4 ? 2 :
8409                         i == Xsand_stoneout_1 && j == 5 ? 3 :
8410                         i == Xsand_stoneout_1 && j == 6 ? 4 :
8411                         i == Xsand_stoneout_1 && j == 7 ? 4 :
8412                         i == Xsand_stoneout_2 && j == 0 ? 5 :
8413                         i == Xsand_stoneout_2 && j == 1 ? 6 :
8414                         i == Xsand_stoneout_2 && j == 2 ? 7 :
8415                         i == Xsand_stoneout_2 && j == 3 ? 8 :
8416                         i == Xsand_stoneout_2 && j == 4 ? 9 :
8417                         i == Xsand_stoneout_2 && j == 5 ? 11 :
8418                         i == Xsand_stoneout_2 && j == 6 ? 13 :
8419                         i == Xsand_stoneout_2 && j == 7 ? 15 :
8420                         i == Xboom_bug && j == 1 ? 2 :
8421                         i == Xboom_bug && j == 2 ? 2 :
8422                         i == Xboom_bug && j == 3 ? 4 :
8423                         i == Xboom_bug && j == 4 ? 4 :
8424                         i == Xboom_bug && j == 5 ? 2 :
8425                         i == Xboom_bug && j == 6 ? 2 :
8426                         i == Xboom_bug && j == 7 ? 0 :
8427                         i == Xboom_bomb && j == 1 ? 2 :
8428                         i == Xboom_bomb && j == 2 ? 2 :
8429                         i == Xboom_bomb && j == 3 ? 4 :
8430                         i == Xboom_bomb && j == 4 ? 4 :
8431                         i == Xboom_bomb && j == 5 ? 2 :
8432                         i == Xboom_bomb && j == 6 ? 2 :
8433                         i == Xboom_bomb && j == 7 ? 0 :
8434                         i == Xboom_android && j == 7 ? 6 :
8435                         i == Xboom_1 && j == 1 ? 2 :
8436                         i == Xboom_1 && j == 2 ? 2 :
8437                         i == Xboom_1 && j == 3 ? 4 :
8438                         i == Xboom_1 && j == 4 ? 4 :
8439                         i == Xboom_1 && j == 5 ? 6 :
8440                         i == Xboom_1 && j == 6 ? 6 :
8441                         i == Xboom_1 && j == 7 ? 8 :
8442                         i == Xboom_2 && j == 0 ? 8 :
8443                         i == Xboom_2 && j == 1 ? 8 :
8444                         i == Xboom_2 && j == 2 ? 10 :
8445                         i == Xboom_2 && j == 3 ? 10 :
8446                         i == Xboom_2 && j == 4 ? 10 :
8447                         i == Xboom_2 && j == 5 ? 12 :
8448                         i == Xboom_2 && j == 6 ? 12 :
8449                         i == Xboom_2 && j == 7 ? 12 :
8450                         special_animation && j == 4 ? 3 :
8451                         effective_action != action ? 0 :
8452                         j);
8453
8454 #if DEBUG_EM_GFX
8455       Bitmap *debug_bitmap = g_em->bitmap;
8456       int debug_src_x = g_em->src_x;
8457       int debug_src_y = g_em->src_y;
8458 #endif
8459
8460       int frame = getAnimationFrame(g->anim_frames,
8461                                     g->anim_delay,
8462                                     g->anim_mode,
8463                                     g->anim_start_frame,
8464                                     sync_frame);
8465
8466       getGraphicSourceExt(graphic, frame, &src_bitmap, &src_x, &src_y,
8467                           g->double_movement && is_backside);
8468
8469       g_em->bitmap = src_bitmap;
8470       g_em->src_x = src_x;
8471       g_em->src_y = src_y;
8472       g_em->src_offset_x = 0;
8473       g_em->src_offset_y = 0;
8474       g_em->dst_offset_x = 0;
8475       g_em->dst_offset_y = 0;
8476       g_em->width  = TILEX;
8477       g_em->height = TILEY;
8478
8479       g_em->preserve_background = FALSE;
8480
8481 #if 1
8482       set_crumbled_graphics_EM(g_em, has_crumbled_graphics, crumbled,
8483                                sync_frame);
8484
8485 #else
8486
8487       g_em->crumbled_bitmap = NULL;
8488       g_em->crumbled_src_x = 0;
8489       g_em->crumbled_src_y = 0;
8490       g_em->crumbled_border_size = 0;
8491
8492       g_em->has_crumbled_graphics = FALSE;
8493
8494 #if 0
8495       if (has_crumbled_graphics && crumbled == IMG_EMPTY_SPACE)
8496         printf("::: empty crumbled: %d [%s], %d, %d\n",
8497                effective_element, element_info[effective_element].token_name,
8498                effective_action, direction);
8499 #endif
8500
8501       /* if element can be crumbled, but certain action graphics are just empty
8502          space (like instantly snapping sand to empty space in 1 frame), do not
8503          treat these empty space graphics as crumbled graphics in EMC engine */
8504       if (has_crumbled_graphics && crumbled != IMG_EMPTY_SPACE)
8505       {
8506         int frame_crumbled = getAnimationFrame(g_crumbled->anim_frames,
8507                                                g_crumbled->anim_delay,
8508                                                g_crumbled->anim_mode,
8509                                                g_crumbled->anim_start_frame,
8510                                                sync_frame);
8511
8512         getGraphicSource(crumbled, frame_crumbled, &src_bitmap, &src_x, &src_y);
8513
8514         g_em->has_crumbled_graphics = TRUE;
8515         g_em->crumbled_bitmap = src_bitmap;
8516         g_em->crumbled_src_x = src_x;
8517         g_em->crumbled_src_y = src_y;
8518         g_em->crumbled_border_size = graphic_info[crumbled].border_size;
8519
8520
8521 #if 0
8522         if (g_em == &graphic_info_em_object[207][0])
8523           printf("... %d, %d [%d, %d, %d, %d] [%d, %d, %d, %d, %d, %d => %d]\n",
8524                  graphic_info_em_object[207][0].crumbled_src_x,
8525                  graphic_info_em_object[207][0].crumbled_src_y,
8526
8527                  crumbled, frame, src_x, src_y,
8528
8529                  g->anim_frames,
8530                  g->anim_delay,
8531                  g->anim_mode,
8532                  g->anim_start_frame,
8533                  sync_frame,
8534                  gfx.anim_random_frame,
8535                  frame);
8536 #endif
8537
8538 #if 0
8539         printf("::: EMC tile %d is crumbled\n", i);
8540 #endif
8541       }
8542 #endif
8543
8544 #if 0
8545       if (element == EL_ROCK &&
8546           effective_action == ACTION_FILLING)
8547         printf("::: has_action_graphics == %d\n", has_action_graphics);
8548 #endif
8549
8550       if ((!g->double_movement && (effective_action == ACTION_FALLING ||
8551                                    effective_action == ACTION_MOVING  ||
8552                                    effective_action == ACTION_PUSHING ||
8553                                    effective_action == ACTION_EATING)) ||
8554           (!has_action_graphics && (effective_action == ACTION_FILLING ||
8555                                     effective_action == ACTION_EMPTYING)))
8556       {
8557         int move_dir =
8558           (effective_action == ACTION_FALLING ||
8559            effective_action == ACTION_FILLING ||
8560            effective_action == ACTION_EMPTYING ? MV_DOWN : direction);
8561         int dx = (move_dir == MV_LEFT ? -1 : move_dir == MV_RIGHT ? 1 : 0);
8562         int dy = (move_dir == MV_UP   ? -1 : move_dir == MV_DOWN  ? 1 : 0);
8563         int num_steps = (i == Ydrip_s1  ? 16 :
8564                          i == Ydrip_s1B ? 16 :
8565                          i == Ydrip_s2  ? 16 :
8566                          i == Ydrip_s2B ? 16 :
8567                          i == Xsand_stonein_1 ? 32 :
8568                          i == Xsand_stonein_2 ? 32 :
8569                          i == Xsand_stonein_3 ? 32 :
8570                          i == Xsand_stonein_4 ? 32 :
8571                          i == Xsand_stoneout_1 ? 16 :
8572                          i == Xsand_stoneout_2 ? 16 : 8);
8573         int cx = ABS(dx) * (TILEX / num_steps);
8574         int cy = ABS(dy) * (TILEY / num_steps);
8575         int step_frame = (i == Ydrip_s2         ? j + 8 :
8576                           i == Ydrip_s2B        ? j + 8 :
8577                           i == Xsand_stonein_2  ? j + 8 :
8578                           i == Xsand_stonein_3  ? j + 16 :
8579                           i == Xsand_stonein_4  ? j + 24 :
8580                           i == Xsand_stoneout_2 ? j + 8 : j) + 1;
8581         int step = (is_backside ? step_frame : num_steps - step_frame);
8582
8583         if (is_backside)        /* tile where movement starts */
8584         {
8585           if (dx < 0 || dy < 0)
8586           {
8587             g_em->src_offset_x = cx * step;
8588             g_em->src_offset_y = cy * step;
8589           }
8590           else
8591           {
8592             g_em->dst_offset_x = cx * step;
8593             g_em->dst_offset_y = cy * step;
8594           }
8595         }
8596         else                    /* tile where movement ends */
8597         {
8598           if (dx < 0 || dy < 0)
8599           {
8600             g_em->dst_offset_x = cx * step;
8601             g_em->dst_offset_y = cy * step;
8602           }
8603           else
8604           {
8605             g_em->src_offset_x = cx * step;
8606             g_em->src_offset_y = cy * step;
8607           }
8608         }
8609
8610         g_em->width  = TILEX - cx * step;
8611         g_em->height = TILEY - cy * step;
8612       }
8613
8614       /* create unique graphic identifier to decide if tile must be redrawn */
8615       /* bit 31 - 16 (16 bit): EM style graphic
8616          bit 15 - 12 ( 4 bit): EM style frame
8617          bit 11 -  6 ( 6 bit): graphic width
8618          bit  5 -  0 ( 6 bit): graphic height */
8619       g_em->unique_identifier =
8620         (graphic << 16) | (frame << 12) | (g_em->width << 6) | g_em->height;
8621
8622 #if DEBUG_EM_GFX
8623
8624       /* skip check for EMC elements not contained in original EMC artwork */
8625       if (element == EL_EMC_FAKE_ACID)
8626         continue;
8627
8628       if (g_em->bitmap != debug_bitmap ||
8629           g_em->src_x != debug_src_x ||
8630           g_em->src_y != debug_src_y ||
8631           g_em->src_offset_x != 0 ||
8632           g_em->src_offset_y != 0 ||
8633           g_em->dst_offset_x != 0 ||
8634           g_em->dst_offset_y != 0 ||
8635           g_em->width != TILEX ||
8636           g_em->height != TILEY)
8637       {
8638         static int last_i = -1;
8639
8640         if (i != last_i)
8641         {
8642           printf("\n");
8643           last_i = i;
8644         }
8645
8646         printf("::: EMC GFX ERROR for element %d -> %d ('%s', '%s', %d)",
8647                i, element, element_info[element].token_name,
8648                element_action_info[effective_action].suffix, direction);
8649
8650         if (element != effective_element)
8651           printf(" [%d ('%s')]",
8652                  effective_element,
8653                  element_info[effective_element].token_name);
8654
8655         printf("\n");
8656
8657         if (g_em->bitmap != debug_bitmap)
8658           printf("    %d (%d): different bitmap! (0x%08x != 0x%08x)\n",
8659                  j, is_backside, (int)(g_em->bitmap), (int)(debug_bitmap));
8660
8661         if (g_em->src_x != debug_src_x ||
8662             g_em->src_y != debug_src_y)
8663           printf("    frame %d (%c): %d,%d (%d,%d) should be %d,%d (%d,%d)\n",
8664                  j, (is_backside ? 'B' : 'F'),
8665                  g_em->src_x, g_em->src_y,
8666                  g_em->src_x / 32, g_em->src_y / 32,
8667                  debug_src_x, debug_src_y,
8668                  debug_src_x / 32, debug_src_y / 32);
8669
8670         if (g_em->src_offset_x != 0 ||
8671             g_em->src_offset_y != 0 ||
8672             g_em->dst_offset_x != 0 ||
8673             g_em->dst_offset_y != 0)
8674           printf("    %d (%d): offsets %d,%d and %d,%d should be all 0\n",
8675                  j, is_backside,
8676                  g_em->src_offset_x, g_em->src_offset_y,
8677                  g_em->dst_offset_x, g_em->dst_offset_y);
8678
8679         if (g_em->width != TILEX ||
8680             g_em->height != TILEY)
8681           printf("    %d (%d): size %d,%d should be %d,%d\n",
8682                  j, is_backside,
8683                  g_em->width, g_em->height, TILEX, TILEY);
8684
8685         num_em_gfx_errors++;
8686       }
8687 #endif
8688
8689     }
8690   }
8691
8692   for (i = 0; i < TILE_MAX; i++)
8693   {
8694     for (j = 0; j < 8; j++)
8695     {
8696       int element = object_mapping[i].element_rnd;
8697       int action = object_mapping[i].action;
8698       int direction = object_mapping[i].direction;
8699       boolean is_backside = object_mapping[i].is_backside;
8700       int graphic_action  = el_act_dir2img(element, action, direction);
8701       int graphic_default = el_act_dir2img(element, ACTION_DEFAULT, direction);
8702
8703       if ((action == ACTION_SMASHED_BY_ROCK ||
8704            action == ACTION_SMASHED_BY_SPRING ||
8705            action == ACTION_EATING) &&
8706           graphic_action == graphic_default)
8707       {
8708         int e = (action == ACTION_SMASHED_BY_ROCK   ? Ystone_s  :
8709                  action == ACTION_SMASHED_BY_SPRING ? Yspring_s :
8710                  direction == MV_LEFT  ? (is_backside? Yspring_wB: Yspring_w) :
8711                  direction == MV_RIGHT ? (is_backside? Yspring_eB: Yspring_e) :
8712                  Xspring);
8713
8714         /* no separate animation for "smashed by rock" -- use rock instead */
8715         struct GraphicInfo_EM *g_em = &graphic_info_em_object[i][7 - j];
8716         struct GraphicInfo_EM *g_xx = &graphic_info_em_object[e][7 - j];
8717
8718         g_em->bitmap            = g_xx->bitmap;
8719         g_em->src_x             = g_xx->src_x;
8720         g_em->src_y             = g_xx->src_y;
8721         g_em->src_offset_x      = g_xx->src_offset_x;
8722         g_em->src_offset_y      = g_xx->src_offset_y;
8723         g_em->dst_offset_x      = g_xx->dst_offset_x;
8724         g_em->dst_offset_y      = g_xx->dst_offset_y;
8725         g_em->width             = g_xx->width;
8726         g_em->height            = g_xx->height;
8727         g_em->unique_identifier = g_xx->unique_identifier;
8728
8729         if (!is_backside)
8730           g_em->preserve_background = TRUE;
8731       }
8732     }
8733   }
8734
8735   for (p = 0; p < MAX_PLAYERS; p++)
8736   {
8737     for (i = 0; i < SPR_MAX; i++)
8738     {
8739       int element = player_mapping[p][i].element_rnd;
8740       int action = player_mapping[p][i].action;
8741       int direction = player_mapping[p][i].direction;
8742
8743       for (j = 0; j < 8; j++)
8744       {
8745         int effective_element = element;
8746         int effective_action = action;
8747         int graphic = (direction == MV_NONE ?
8748                        el_act2img(effective_element, effective_action) :
8749                        el_act_dir2img(effective_element, effective_action,
8750                                       direction));
8751         struct GraphicInfo *g = &graphic_info[graphic];
8752         struct GraphicInfo_EM *g_em = &graphic_info_em_player[p][i][7 - j];
8753         Bitmap *src_bitmap;
8754         int src_x, src_y;
8755         int sync_frame = j;
8756
8757 #if DEBUG_EM_GFX
8758         Bitmap *debug_bitmap = g_em->bitmap;
8759         int debug_src_x = g_em->src_x;
8760         int debug_src_y = g_em->src_y;
8761 #endif
8762
8763         int frame = getAnimationFrame(g->anim_frames,
8764                                       g->anim_delay,
8765                                       g->anim_mode,
8766                                       g->anim_start_frame,
8767                                       sync_frame);
8768
8769         getGraphicSourceExt(graphic, frame, &src_bitmap, &src_x, &src_y, FALSE);
8770
8771         g_em->bitmap = src_bitmap;
8772         g_em->src_x = src_x;
8773         g_em->src_y = src_y;
8774         g_em->src_offset_x = 0;
8775         g_em->src_offset_y = 0;
8776         g_em->dst_offset_x = 0;
8777         g_em->dst_offset_y = 0;
8778         g_em->width  = TILEX;
8779         g_em->height = TILEY;
8780
8781 #if DEBUG_EM_GFX
8782
8783         /* skip check for EMC elements not contained in original EMC artwork */
8784         if (element == EL_PLAYER_3 ||
8785             element == EL_PLAYER_4)
8786           continue;
8787
8788         if (g_em->bitmap != debug_bitmap ||
8789             g_em->src_x != debug_src_x ||
8790             g_em->src_y != debug_src_y)
8791         {
8792           static int last_i = -1;
8793
8794           if (i != last_i)
8795           {
8796             printf("\n");
8797             last_i = i;
8798           }
8799
8800           printf("::: EMC GFX ERROR for p/a %d/%d -> %d ('%s', '%s', %d)",
8801                  p, i, element, element_info[element].token_name,
8802                  element_action_info[effective_action].suffix, direction);
8803
8804           if (element != effective_element)
8805             printf(" [%d ('%s')]",
8806                    effective_element,
8807                    element_info[effective_element].token_name);
8808
8809           printf("\n");
8810
8811           if (g_em->bitmap != debug_bitmap)
8812             printf("    %d: different bitmap! (0x%08x != 0x%08x)\n",
8813                    j, (int)(g_em->bitmap), (int)(debug_bitmap));
8814
8815           if (g_em->src_x != debug_src_x ||
8816               g_em->src_y != debug_src_y)
8817             printf("    frame %d: %d,%d (%d,%d) should be %d,%d (%d,%d)\n",
8818                    j,
8819                    g_em->src_x, g_em->src_y,
8820                    g_em->src_x / 32, g_em->src_y / 32,
8821                    debug_src_x, debug_src_y,
8822                    debug_src_x / 32, debug_src_y / 32);
8823
8824           num_em_gfx_errors++;
8825         }
8826 #endif
8827
8828       }
8829     }
8830   }
8831
8832 #if DEBUG_EM_GFX
8833   printf("\n");
8834   printf("::: [%d errors found]\n", num_em_gfx_errors);
8835
8836   exit(0);
8837 #endif
8838 }
8839
8840 void CheckSingleStepMode_EM(byte action[MAX_PLAYERS], int frame,
8841                             boolean any_player_moving,
8842                             boolean player_is_dropping)
8843 {
8844   if (tape.single_step && tape.recording && !tape.pausing)
8845   {
8846 #if 0
8847     boolean active_players = FALSE;
8848     int i;
8849
8850     for (i = 0; i < MAX_PLAYERS; i++)
8851       if (action[i] != JOY_NO_ACTION)
8852         active_players = TRUE;
8853 #endif
8854
8855     // if (frame == 0)
8856     if (frame == 0 && !player_is_dropping)
8857       TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
8858   }
8859 }
8860
8861 void CheckSingleStepMode_SP(boolean murphy_is_waiting,
8862                             boolean murphy_is_dropping)
8863 {
8864 #if 0
8865   printf("::: waiting: %d, dropping: %d\n",
8866          murphy_is_waiting, murphy_is_dropping);
8867 #endif
8868
8869   if (tape.single_step && tape.recording && !tape.pausing)
8870   {
8871     // if (murphy_is_waiting || murphy_is_dropping)
8872     if (murphy_is_waiting)
8873     {
8874 #if 0
8875       printf("::: murphy is waiting -> pause mode\n");
8876 #endif
8877
8878       TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
8879     }
8880   }
8881 }
8882
8883 void getGraphicSource_SP(struct GraphicInfo_SP *g_sp,
8884                          int graphic, int sync_frame, int x, int y)
8885 {
8886   int frame = getGraphicAnimationFrame(graphic, sync_frame);
8887
8888   getGraphicSource(graphic, frame, &g_sp->bitmap, &g_sp->src_x, &g_sp->src_y);
8889 }
8890
8891 boolean isNextAnimationFrame_SP(int graphic, int sync_frame)
8892 {
8893   return (IS_NEXT_FRAME(sync_frame, graphic));
8894 }
8895
8896 int getGraphicInfo_Delay(int graphic)
8897 {
8898   return graphic_info[graphic].anim_delay;
8899 }
8900
8901 void PlayMenuSoundExt(int sound)
8902 {
8903   if (sound == SND_UNDEFINED)
8904     return;
8905
8906   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
8907       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
8908     return;
8909
8910   if (IS_LOOP_SOUND(sound))
8911     PlaySoundLoop(sound);
8912   else
8913     PlaySound(sound);
8914 }
8915
8916 void PlayMenuSound()
8917 {
8918   PlayMenuSoundExt(menu.sound[game_status]);
8919 }
8920
8921 void PlayMenuSoundStereo(int sound, int stereo_position)
8922 {
8923   if (sound == SND_UNDEFINED)
8924     return;
8925
8926   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
8927       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
8928     return;
8929
8930   if (IS_LOOP_SOUND(sound))
8931     PlaySoundExt(sound, SOUND_MAX_VOLUME, stereo_position, SND_CTRL_PLAY_LOOP);
8932   else
8933     PlaySoundStereo(sound, stereo_position);
8934 }
8935
8936 void PlayMenuSoundIfLoopExt(int sound)
8937 {
8938   if (sound == SND_UNDEFINED)
8939     return;
8940
8941   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
8942       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
8943     return;
8944
8945   if (IS_LOOP_SOUND(sound))
8946     PlaySoundLoop(sound);
8947 }
8948
8949 void PlayMenuSoundIfLoop()
8950 {
8951   PlayMenuSoundIfLoopExt(menu.sound[game_status]);
8952 }
8953
8954 void PlayMenuMusicExt(int music)
8955 {
8956   if (music == MUS_UNDEFINED)
8957     return;
8958
8959   if (!setup.sound_music)
8960     return;
8961
8962   PlayMusic(music);
8963 }
8964
8965 void PlayMenuMusic()
8966 {
8967   PlayMenuMusicExt(menu.music[game_status]);
8968 }
8969
8970 void PlaySoundActivating()
8971 {
8972 #if 0
8973   PlaySound(SND_MENU_ITEM_ACTIVATING);
8974 #endif
8975 }
8976
8977 void PlaySoundSelecting()
8978 {
8979 #if 0
8980   PlaySound(SND_MENU_ITEM_SELECTING);
8981 #endif
8982 }
8983
8984 void ToggleFullscreenIfNeeded()
8985 {
8986   boolean change_fullscreen = (setup.fullscreen !=
8987                                video.fullscreen_enabled);
8988   boolean change_fullscreen_mode = (video.fullscreen_enabled &&
8989                                     !strEqual(setup.fullscreen_mode,
8990                                               video.fullscreen_mode_current));
8991
8992   if (!video.fullscreen_available)
8993     return;
8994
8995   if (change_fullscreen || change_fullscreen_mode)
8996   {
8997     Bitmap *tmp_backbuffer = CreateBitmap(WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH);
8998
8999     /* save backbuffer content which gets lost when toggling fullscreen mode */
9000     BlitBitmap(backbuffer, tmp_backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
9001
9002     if (change_fullscreen_mode)
9003     {
9004       /* keep fullscreen, but change fullscreen mode (screen resolution) */
9005       video.fullscreen_enabled = FALSE;         /* force new fullscreen mode */
9006     }
9007
9008     /* toggle fullscreen */
9009     ChangeVideoModeIfNeeded(setup.fullscreen);
9010
9011     setup.fullscreen = video.fullscreen_enabled;
9012
9013     /* restore backbuffer content from temporary backbuffer backup bitmap */
9014     BlitBitmap(tmp_backbuffer, backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
9015
9016     FreeBitmap(tmp_backbuffer);
9017
9018 #if 1
9019     /* update visible window/screen */
9020     BlitBitmap(backbuffer, window, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
9021 #else
9022     redraw_mask = REDRAW_ALL;
9023 #endif
9024   }
9025 }
9026
9027 void ChangeViewportPropertiesIfNeeded()
9028 {
9029   int *door_1_x = &DX;
9030   int *door_1_y = &DY;
9031   int *door_2_x = (game_status == GAME_MODE_EDITOR ? &EX : &VX);
9032   int *door_2_y = (game_status == GAME_MODE_EDITOR ? &EY : &VY);
9033   int gfx_game_mode = (game_status == GAME_MODE_PLAYING ||
9034                        game_status == GAME_MODE_EDITOR ? game_status :
9035                        GAME_MODE_MAIN);
9036   struct RectWithBorder *vp_playfield = &viewport.playfield[gfx_game_mode];
9037   struct RectWithBorder *vp_door_1 = &viewport.door_1[gfx_game_mode];
9038   struct RectWithBorder *vp_door_2 = &viewport.door_2[gfx_game_mode];
9039   int border_size = vp_playfield->border_size;
9040   int new_sx = vp_playfield->x + border_size;
9041   int new_sy = vp_playfield->y + border_size;
9042   int new_sxsize = vp_playfield->width  - 2 * border_size;
9043   int new_sysize = vp_playfield->height - 2 * border_size;
9044   int new_real_sx = vp_playfield->x;
9045   int new_real_sy = vp_playfield->y;
9046   int new_full_sxsize = vp_playfield->width;
9047   int new_full_sysize = vp_playfield->height;
9048 #if NEW_TILESIZE
9049   int new_tilesize_var = TILESIZE / (setup.small_game_graphics ? 2 : 1);
9050   int tilesize = (gfx_game_mode == GAME_MODE_PLAYING ? new_tilesize_var :
9051                   gfx_game_mode == GAME_MODE_EDITOR ? MINI_TILESIZE : TILESIZE);
9052   int new_scr_fieldx = new_sxsize / tilesize;
9053   int new_scr_fieldy = new_sysize / tilesize;
9054   int new_scr_fieldx_buffers = new_sxsize / new_tilesize_var;
9055   int new_scr_fieldy_buffers = new_sysize / new_tilesize_var;
9056 #else
9057   int new_scr_fieldx = (vp_playfield->width  - 2 * border_size) / TILESIZE;
9058   int new_scr_fieldy = (vp_playfield->height - 2 * border_size) / TILESIZE;
9059 #endif
9060   boolean init_gfx_buffers = FALSE;
9061   boolean init_video_buffer = FALSE;
9062   boolean init_gadgets_and_toons = FALSE;
9063
9064 #if 0
9065   /* !!! TEST ONLY !!! */
9066   // InitGfxBuffers();
9067   return;
9068 #endif
9069
9070   if (viewport.window.width  != WIN_XSIZE ||
9071       viewport.window.height != WIN_YSIZE)
9072   {
9073     WIN_XSIZE = viewport.window.width;
9074     WIN_YSIZE = viewport.window.height;
9075
9076 #if 1
9077     init_video_buffer = TRUE;
9078     init_gfx_buffers = TRUE;
9079 #else
9080     InitVideoBuffer(WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH, setup.fullscreen);
9081     InitGfxBuffers();
9082
9083 #if 1
9084     SetDrawDeactivationMask(REDRAW_NONE);
9085     SetDrawBackgroundMask(REDRAW_FIELD);
9086
9087     // RedrawBackground();
9088 #endif
9089 #endif
9090
9091     // printf("::: video: init_video_buffer, init_gfx_buffers\n");
9092   }
9093
9094   if (new_scr_fieldx != SCR_FIELDX ||
9095       new_scr_fieldy != SCR_FIELDY)
9096   {
9097     /* this always toggles between MAIN and GAME when using small tile size */
9098
9099     SCR_FIELDX = new_scr_fieldx;
9100     SCR_FIELDY = new_scr_fieldy;
9101
9102     // printf("::: new_scr_fieldx != SCR_FIELDX ...\n");
9103   }
9104
9105 #if 0
9106   if (new_tilesize_var != TILESIZE_VAR &&
9107       gfx_game_mode == GAME_MODE_PLAYING)
9108   {
9109     /* doing this outside GAME_MODE_PLAYING would give wrong playfield size */
9110
9111     TILESIZE_VAR = new_tilesize_var;
9112
9113     init_gfx_buffers = TRUE;
9114
9115     // printf("::: tilesize: init_gfx_buffers\n");
9116   }
9117 #endif
9118
9119   if (new_sx != SX ||
9120       new_sy != SY ||
9121       new_sxsize != SXSIZE ||
9122       new_sysize != SYSIZE ||
9123       new_real_sx != REAL_SX ||
9124       new_real_sy != REAL_SY ||
9125       new_full_sxsize != FULL_SXSIZE ||
9126       new_full_sysize != FULL_SYSIZE ||
9127       new_tilesize_var != TILESIZE_VAR ||
9128       vp_door_1->x != *door_1_x ||
9129       vp_door_1->y != *door_1_y ||
9130       vp_door_2->x != *door_2_x ||
9131       vp_door_2->y != *door_2_y)
9132   {
9133     SX = new_sx;
9134     SY = new_sy;
9135     SXSIZE = new_sxsize;
9136     SYSIZE = new_sysize;
9137     REAL_SX = new_real_sx;
9138     REAL_SY = new_real_sy;
9139     FULL_SXSIZE = new_full_sxsize;
9140     FULL_SYSIZE = new_full_sysize;
9141     TILESIZE_VAR = new_tilesize_var;
9142
9143 #if 0
9144     printf("::: %d, %d, %d [%d]\n",
9145            SCR_FIELDX, SCR_FIELDY, TILESIZE_VAR,
9146            setup.small_game_graphics);
9147 #endif
9148
9149     *door_1_x = vp_door_1->x;
9150     *door_1_y = vp_door_1->y;
9151     *door_2_x = vp_door_2->x;
9152     *door_2_y = vp_door_2->y;
9153
9154 #if 1
9155     init_gfx_buffers = TRUE;
9156
9157     // printf("::: viewports: init_gfx_buffers\n");
9158 #else
9159     InitGfxBuffers();
9160 #endif
9161
9162     if (gfx_game_mode == GAME_MODE_MAIN)
9163     {
9164 #if 1
9165       init_gadgets_and_toons = TRUE;
9166
9167       // printf("::: viewports: init_gadgets_and_toons\n");
9168 #else
9169       InitGadgets();
9170       InitToons();
9171 #endif
9172     }
9173   }
9174
9175   if (init_gfx_buffers)
9176   {
9177     // printf("::: init_gfx_buffers\n");
9178
9179     SCR_FIELDX = new_scr_fieldx_buffers;
9180     SCR_FIELDY = new_scr_fieldy_buffers;
9181
9182     InitGfxBuffers();
9183
9184     SCR_FIELDX = new_scr_fieldx;
9185     SCR_FIELDY = new_scr_fieldy;
9186   }
9187
9188   if (init_video_buffer)
9189   {
9190     // printf("::: init_video_buffer\n");
9191
9192     InitVideoBuffer(WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH, setup.fullscreen);
9193
9194     SetDrawDeactivationMask(REDRAW_NONE);
9195     SetDrawBackgroundMask(REDRAW_FIELD);
9196   }
9197
9198   if (init_gadgets_and_toons)
9199   {
9200     // printf("::: init_gadgets_and_toons\n");
9201
9202     InitGadgets();
9203     InitToons();
9204   }
9205
9206 #if 0
9207   printf("::: %d, %d  /  %d, %d [%d]\n", VX, VY, EX, EY, game_status);
9208 #endif
9209 }