b986e81eae00fc64d28496ba2aacc9e3c7d57b10
[rocksndiamonds.git] / src / tools.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 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 "game.h"
18 #include "events.h"
19 #include "cartoons.h"
20 #include "network.h"
21 #include "tape.h"
22
23 /* tool button identifiers */
24 #define TOOL_CTRL_ID_YES        0
25 #define TOOL_CTRL_ID_NO         1
26 #define TOOL_CTRL_ID_CONFIRM    2
27 #define TOOL_CTRL_ID_PLAYER_1   3
28 #define TOOL_CTRL_ID_PLAYER_2   4
29 #define TOOL_CTRL_ID_PLAYER_3   5
30 #define TOOL_CTRL_ID_PLAYER_4   6
31
32 #define NUM_TOOL_BUTTONS        7
33
34 /* forward declaration for internal use */
35 static void UnmapToolButtons();
36 static void HandleToolButtons(struct GadgetInfo *);
37 static int el_act_dir2crm(int, int, int);
38
39 static struct GadgetInfo *tool_gadget[NUM_TOOL_BUTTONS];
40 static int request_gadget_id = -1;
41
42 void SetDrawtoField(int mode)
43 {
44   if (mode == DRAW_BUFFERED && setup.soft_scrolling)
45   {
46     FX = TILEX;
47     FY = TILEY;
48     BX1 = -1;
49     BY1 = -1;
50     BX2 = SCR_FIELDX;
51     BY2 = SCR_FIELDY;
52     redraw_x1 = 1;
53     redraw_y1 = 1;
54
55     drawto_field = fieldbuffer;
56   }
57   else  /* DRAW_DIRECT, DRAW_BACKBUFFER */
58   {
59     FX = SX;
60     FY = SY;
61     BX1 = 0;
62     BY1 = 0;
63     BX2 = SCR_FIELDX - 1;
64     BY2 = SCR_FIELDY - 1;
65     redraw_x1 = 0;
66     redraw_y1 = 0;
67
68     drawto_field = (mode == DRAW_DIRECT ? window :  backbuffer);
69   }
70 }
71
72 void RedrawPlayfield(boolean force_redraw, int x, int y, int width, int height)
73 {
74   if (game_status == GAME_MODE_PLAYING)
75   {
76     if (force_redraw)
77     {
78       x = gfx.sx - TILEX;
79       y = gfx.sy - TILEY;
80       width = gfx.sxsize + 2 * TILEX;
81       height = gfx.sysize + 2 * TILEY;
82     }
83
84     if (force_redraw || setup.direct_draw)
85     {
86       int xx, yy;
87       int x1 = (x - SX) / TILEX, y1 = (y - SY) / TILEY;
88       int x2 = (x - SX + width) / TILEX, y2 = (y - SY + height) / TILEY;
89
90       if (setup.direct_draw)
91         SetDrawtoField(DRAW_BACKBUFFER);
92
93       for(xx=BX1; xx<=BX2; xx++)
94         for(yy=BY1; yy<=BY2; yy++)
95           if (xx >= x1 && xx <= x2 && yy >= y1 && yy <= y2)
96             DrawScreenField(xx, yy);
97       DrawAllPlayers();
98
99       if (setup.direct_draw)
100         SetDrawtoField(DRAW_DIRECT);
101     }
102
103     if (setup.soft_scrolling)
104     {
105       int fx = FX, fy = FY;
106
107       fx += (ScreenMovDir & (MV_LEFT|MV_RIGHT) ? ScreenGfxPos : 0);
108       fy += (ScreenMovDir & (MV_UP|MV_DOWN)    ? ScreenGfxPos : 0);
109
110       BlitBitmap(fieldbuffer, backbuffer, fx,fy, SXSIZE,SYSIZE, SX,SY);
111     }
112   }
113
114   BlitBitmap(drawto, window, x, y, width, height, x, y);
115 }
116
117 void BackToFront()
118 {
119   int x,y;
120   DrawBuffer *buffer = (drawto_field == window ? backbuffer : drawto_field);
121
122   if (setup.direct_draw && game_status == GAME_MODE_PLAYING)
123     redraw_mask &= ~REDRAW_MAIN;
124
125   if (redraw_mask & REDRAW_TILES && redraw_tiles > REDRAWTILES_THRESHOLD)
126     redraw_mask |= REDRAW_FIELD;
127
128   if (redraw_mask & REDRAW_FIELD)
129     redraw_mask &= ~REDRAW_TILES;
130
131   if (redraw_mask == REDRAW_NONE)
132     return;
133
134   if (global.fps_slowdown && game_status == GAME_MODE_PLAYING)
135   {
136     static boolean last_frame_skipped = FALSE;
137     boolean skip_even_when_not_scrolling = TRUE;
138     boolean just_scrolling = (ScreenMovDir != 0);
139     boolean verbose = FALSE;
140
141     if (global.fps_slowdown_factor > 1 &&
142         (FrameCounter % global.fps_slowdown_factor) &&
143         (just_scrolling || skip_even_when_not_scrolling))
144     {
145       redraw_mask &= ~REDRAW_MAIN;
146
147       last_frame_skipped = TRUE;
148
149       if (verbose)
150         printf("FRAME SKIPPED\n");
151     }
152     else
153     {
154       if (last_frame_skipped)
155         redraw_mask |= REDRAW_FIELD;
156
157       last_frame_skipped = FALSE;
158
159       if (verbose)
160         printf("frame not skipped\n");
161     }
162   }
163
164   /* synchronize X11 graphics at this point; if we would synchronize the
165      display immediately after the buffer switching (after the XFlush),
166      this could mean that we have to wait for the graphics to complete,
167      although we could go on doing calculations for the next frame */
168
169   SyncDisplay();
170
171   if (redraw_mask & REDRAW_ALL)
172   {
173     BlitBitmap(backbuffer, window, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
174     redraw_mask = 0;
175   }
176
177   if (redraw_mask & REDRAW_FIELD)
178   {
179     if (game_status != GAME_MODE_PLAYING ||
180         redraw_mask & REDRAW_FROM_BACKBUFFER)
181     {
182       BlitBitmap(backbuffer, window,
183                  REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE, REAL_SX, REAL_SY);
184     }
185     else
186     {
187       int fx = FX, fy = FY;
188
189       if (setup.soft_scrolling)
190       {
191         fx += (ScreenMovDir & (MV_LEFT | MV_RIGHT) ? ScreenGfxPos : 0);
192         fy += (ScreenMovDir & (MV_UP | MV_DOWN)    ? ScreenGfxPos : 0);
193       }
194
195       if (setup.soft_scrolling ||
196           ABS(ScreenMovPos) + ScrollStepSize == TILEX ||
197           ABS(ScreenMovPos) == ScrollStepSize ||
198           redraw_tiles > REDRAWTILES_THRESHOLD)
199       {
200         BlitBitmap(buffer, window, fx, fy, SXSIZE, SYSIZE, SX, SY);
201
202 #ifdef DEBUG
203 #if 0
204         printf("redrawing all (ScreenGfxPos == %d) because %s\n",
205                ScreenGfxPos,
206                (setup.soft_scrolling ?
207                 "setup.soft_scrolling" :
208                 ABS(ScreenGfxPos) + ScrollStepSize == TILEX ?
209                 "ABS(ScreenGfxPos) + ScrollStepSize == TILEX" :
210                 ABS(ScreenGfxPos) == ScrollStepSize ?
211                 "ABS(ScreenGfxPos) == ScrollStepSize" :
212                 "redraw_tiles > REDRAWTILES_THRESHOLD"));
213 #endif
214 #endif
215       }
216     }
217
218     redraw_mask &= ~REDRAW_MAIN;
219   }
220
221   if (redraw_mask & REDRAW_DOORS)
222   {
223     if (redraw_mask & REDRAW_DOOR_1)
224       BlitBitmap(backbuffer, window, DX, DY, DXSIZE, DYSIZE, DX, DY);
225     if (redraw_mask & REDRAW_DOOR_2)
226     {
227       if ((redraw_mask & REDRAW_DOOR_2) == REDRAW_DOOR_2)
228         BlitBitmap(backbuffer, window, VX,VY, VXSIZE,VYSIZE, VX,VY);
229       else
230       {
231         if (redraw_mask & REDRAW_VIDEO_1)
232           BlitBitmap(backbuffer, window,
233                      VX+VIDEO_DISPLAY1_XPOS,VY+VIDEO_DISPLAY1_YPOS,
234                      VIDEO_DISPLAY_XSIZE,VIDEO_DISPLAY_YSIZE,
235                      VX+VIDEO_DISPLAY1_XPOS,VY+VIDEO_DISPLAY1_YPOS);
236         if (redraw_mask & REDRAW_VIDEO_2)
237           BlitBitmap(backbuffer, window,
238                      VX+VIDEO_DISPLAY2_XPOS,VY+VIDEO_DISPLAY2_YPOS,
239                      VIDEO_DISPLAY_XSIZE,VIDEO_DISPLAY_YSIZE,
240                      VX+VIDEO_DISPLAY2_XPOS,VY+VIDEO_DISPLAY2_YPOS);
241         if (redraw_mask & REDRAW_VIDEO_3)
242           BlitBitmap(backbuffer, window,
243                      VX+VIDEO_CONTROL_XPOS,VY+VIDEO_CONTROL_YPOS,
244                      VIDEO_CONTROL_XSIZE,VIDEO_CONTROL_YSIZE,
245                      VX+VIDEO_CONTROL_XPOS,VY+VIDEO_CONTROL_YPOS);
246       }
247     }
248
249     if (redraw_mask & REDRAW_DOOR_3)
250       BlitBitmap(backbuffer, window, EX, EY, EXSIZE, EYSIZE, EX, EY);
251
252     redraw_mask &= ~REDRAW_DOORS;
253   }
254
255   if (redraw_mask & REDRAW_MICROLEVEL)
256   {
257     BlitBitmap(backbuffer, window, SX, SY + 10 * TILEY, SXSIZE, 7 * TILEY,
258                SX, SY + 10 * TILEY);
259
260     redraw_mask &= ~REDRAW_MICROLEVEL;
261   }
262
263   if (redraw_mask & REDRAW_TILES)
264   {
265     for(x=0; x<SCR_FIELDX; x++)
266       for(y=0; y<SCR_FIELDY; y++)
267         if (redraw[redraw_x1 + x][redraw_y1 + y])
268           BlitBitmap(buffer, window,
269                      FX + x * TILEX, FX + y * TILEY, TILEX, TILEY,
270                      SX + x * TILEX, SY + y * TILEY);
271   }
272
273   if (redraw_mask & REDRAW_FPS)         /* display frames per second */
274   {
275     char text[100];
276     char info1[100];
277
278     sprintf(info1, " (only every %d. frame)", global.fps_slowdown_factor);
279     if (!global.fps_slowdown)
280       info1[0] = '\0';
281
282     sprintf(text, "%.1f fps%s", global.frames_per_second, info1);
283     DrawTextExt(window, SX, SY, text, FONT_TEXT_2, BLIT_OPAQUE);
284   }
285
286   FlushDisplay();
287
288   for(x=0; x<MAX_BUF_XSIZE; x++)
289     for(y=0; y<MAX_BUF_YSIZE; y++)
290       redraw[x][y] = 0;
291   redraw_tiles = 0;
292   redraw_mask = REDRAW_NONE;
293 }
294
295 void FadeToFront()
296 {
297 #if 0
298   long fading_delay = 300;
299
300   if (setup.fading && (redraw_mask & REDRAW_FIELD))
301   {
302 #endif
303
304 #if 0
305     int x,y;
306
307     ClearRectangle(window, REAL_SX,REAL_SY,FULL_SXSIZE,FULL_SYSIZE);
308     FlushDisplay();
309
310     for(i=0;i<2*FULL_SYSIZE;i++)
311     {
312       for(y=0;y<FULL_SYSIZE;y++)
313       {
314         BlitBitmap(backbuffer, window,
315                    REAL_SX,REAL_SY+i, FULL_SXSIZE,1, REAL_SX,REAL_SY+i);
316       }
317       FlushDisplay();
318       Delay(10);
319     }
320 #endif
321
322 #if 0
323     for(i=1;i<FULL_SYSIZE;i+=2)
324       BlitBitmap(backbuffer, window,
325                  REAL_SX,REAL_SY+i, FULL_SXSIZE,1, REAL_SX,REAL_SY+i);
326     FlushDisplay();
327     Delay(fading_delay);
328 #endif
329
330 #if 0
331     SetClipOrigin(clip_gc[PIX_FADEMASK], 0, 0);
332     BlitBitmapMasked(backbuffer, window,
333                      REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE,
334                      REAL_SX,REAL_SY);
335     FlushDisplay();
336     Delay(fading_delay);
337
338     SetClipOrigin(clip_gc[PIX_FADEMASK], -1, -1);
339     BlitBitmapMasked(backbuffer, window,
340                      REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE,
341                      REAL_SX,REAL_SY);
342     FlushDisplay();
343     Delay(fading_delay);
344
345     SetClipOrigin(clip_gc[PIX_FADEMASK], 0, -1);
346     BlitBitmapMasked(backbuffer, window,
347                      REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE,
348                      REAL_SX,REAL_SY);
349     FlushDisplay();
350     Delay(fading_delay);
351
352     SetClipOrigin(clip_gc[PIX_FADEMASK], -1, 0);
353     BlitBitmapMasked(backbuffer, window,
354                      REAL_SX,REAL_SY, FULL_SXSIZE,FULL_SYSIZE,
355                      REAL_SX,REAL_SY);
356     FlushDisplay();
357     Delay(fading_delay);
358
359     redraw_mask &= ~REDRAW_MAIN;
360   }
361 #endif
362
363   BackToFront();
364 }
365
366 void SetMainBackgroundImage(int graphic)
367 {
368   SetMainBackgroundBitmap(graphic == IMG_UNDEFINED ? NULL :
369                           graphic_info[graphic].bitmap ?
370                           graphic_info[graphic].bitmap :
371                           graphic_info[IMG_BACKGROUND].bitmap);
372 }
373
374 void SetDoorBackgroundImage(int graphic)
375 {
376   SetDoorBackgroundBitmap(graphic == IMG_UNDEFINED ? NULL :
377                           graphic_info[graphic].bitmap ?
378                           graphic_info[graphic].bitmap :
379                           graphic_info[IMG_BACKGROUND].bitmap);
380 }
381
382 void DrawBackground(int dest_x, int dest_y, int width, int height)
383 {
384   ClearRectangleOnBackground(backbuffer, dest_x, dest_y, width, height);
385
386   redraw_mask |= REDRAW_FIELD;
387 }
388
389 void ClearWindow()
390 {
391   DrawBackground(REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
392
393   if (setup.soft_scrolling && game_status == GAME_MODE_PLAYING)
394   {
395     ClearRectangle(fieldbuffer, 0, 0, FXSIZE, FYSIZE);
396     SetDrawtoField(DRAW_BUFFERED);
397   }
398   else
399     SetDrawtoField(DRAW_BACKBUFFER);
400
401   if (setup.direct_draw && game_status == GAME_MODE_PLAYING)
402   {
403     ClearRectangle(window, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
404     SetDrawtoField(DRAW_DIRECT);
405   }
406 }
407
408 void MarkTileDirty(int x, int y)
409 {
410   int xx = redraw_x1 + x;
411   int yy = redraw_y1 + y;
412
413   if (!redraw[xx][yy])
414     redraw_tiles++;
415
416   redraw[xx][yy] = TRUE;
417   redraw_mask |= REDRAW_TILES;
418 }
419
420 void SetBorderElement()
421 {
422   int x, y;
423
424   BorderElement = EL_EMPTY;
425
426   for(y=0; y<lev_fieldy && BorderElement == EL_EMPTY; y++)
427   {
428     for(x=0; x<lev_fieldx; x++)
429     {
430       if (!IS_INDESTRUCTIBLE(Feld[x][y]))
431         BorderElement = EL_STEELWALL;
432
433       if (y != 0 && y != lev_fieldy - 1 && x != lev_fieldx - 1)
434         x = lev_fieldx - 2;
435     }
436   }
437 }
438
439 void SetRandomAnimationValue(int x, int y)
440 {
441   gfx.anim_random_frame = GfxRandom[x][y];
442 }
443
444 inline int getGraphicAnimationFrame(int graphic, int sync_frame)
445 {
446   /* animation synchronized with global frame counter, not move position */
447   if (graphic_info[graphic].anim_global_sync || sync_frame < 0)
448     sync_frame = FrameCounter;
449
450   return getAnimationFrame(graphic_info[graphic].anim_frames,
451                            graphic_info[graphic].anim_delay,
452                            graphic_info[graphic].anim_mode,
453                            graphic_info[graphic].anim_start_frame,
454                            sync_frame);
455 }
456
457 inline void DrawGraphicAnimationExt(DrawBuffer *dst_bitmap, int x, int y,
458                                     int graphic, int sync_frame, int mask_mode)
459 {
460   int frame = getGraphicAnimationFrame(graphic, sync_frame);
461
462   if (mask_mode == USE_MASKING)
463     DrawGraphicThruMaskExt(dst_bitmap, x, y, graphic, frame);
464   else
465     DrawGraphicExt(dst_bitmap, x, y, graphic, frame);
466 }
467
468 inline void DrawGraphicAnimation(int x, int y, int graphic)
469 {
470   int lx = LEVELX(x), ly = LEVELY(y);
471
472   if (!IN_SCR_FIELD(x, y))
473     return;
474
475   DrawGraphicAnimationExt(drawto_field, FX + x * TILEX, FY + y * TILEY,
476                           graphic, GfxFrame[lx][ly], NO_MASKING);
477   MarkTileDirty(x, y);
478 }
479
480 void DrawLevelGraphicAnimation(int x, int y, int graphic)
481 {
482   DrawGraphicAnimation(SCREENX(x), SCREENY(y), graphic);
483 }
484
485 void DrawLevelElementAnimation(int x, int y, int element)
486 {
487 #if 1
488   int graphic = el_act_dir2img(element, GfxAction[x][y], MovDir[x][y]);
489
490   DrawGraphicAnimation(SCREENX(x), SCREENY(y), graphic);
491 #else
492   DrawGraphicAnimation(SCREENX(x), SCREENY(y), el2img(element));
493 #endif
494 }
495
496 inline void DrawLevelGraphicAnimationIfNeeded(int x, int y, int graphic)
497 {
498   int sx = SCREENX(x), sy = SCREENY(y);
499
500   if (!IN_LEV_FIELD(x, y) || !IN_SCR_FIELD(sx, sy))
501     return;
502
503   if (!IS_NEW_FRAME(GfxFrame[x][y], graphic))
504     return;
505
506   DrawGraphicAnimation(sx, sy, graphic);
507
508   if (CAN_BE_CRUMBLED(Feld[x][y]))
509     DrawLevelFieldCrumbledSand(x, y);
510 }
511
512 void DrawLevelElementAnimationIfNeeded(int x, int y, int element)
513 {
514   int sx = SCREENX(x), sy = SCREENY(y);
515   int graphic;
516
517   if (!IN_LEV_FIELD(x, y) || !IN_SCR_FIELD(sx, sy))
518     return;
519
520   graphic = el_act_dir2img(element, GfxAction[x][y], MovDir[x][y]);
521
522   if (!IS_NEW_FRAME(GfxFrame[x][y], graphic))
523     return;
524
525   DrawGraphicAnimation(sx, sy, graphic);
526
527   if (CAN_BE_CRUMBLED(element))
528     DrawLevelFieldCrumbledSand(x, y);
529 }
530
531 void DrawAllPlayers()
532 {
533   int i;
534
535   for(i=0; i<MAX_PLAYERS; i++)
536     if (stored_player[i].active)
537       DrawPlayer(&stored_player[i]);
538 }
539
540 void DrawPlayerField(int x, int y)
541 {
542   if (!IS_PLAYER(x, y))
543     return;
544
545   DrawPlayer(PLAYERINFO(x, y));
546 }
547
548 void DrawPlayer(struct PlayerInfo *player)
549 {
550 #if 0
551   int jx = player->jx, jy = player->jy;
552   int last_jx = player->last_jx, last_jy = player->last_jy;
553   int next_jx = jx + (jx - last_jx), next_jy = jy + (jy - last_jy);
554   int sx = SCREENX(jx), sy = SCREENY(jy);
555   int sxx = 0, syy = 0;
556   int element = Feld[jx][jy], last_element = Feld[last_jx][last_jy];
557   int graphic;
558   int frame = 0;
559   boolean player_is_moving = (last_jx != jx || last_jy != jy ? TRUE : FALSE);
560   int move_dir = player->MovDir;
561   int action = ACTION_DEFAULT;
562 #else
563   int jx = player->jx, jy = player->jy;
564   int move_dir = player->MovDir;
565   int dx = (move_dir == MV_LEFT ? -1 : move_dir == MV_RIGHT ? +1 : 0);
566   int dy = (move_dir == MV_UP   ? -1 : move_dir == MV_DOWN  ? +1 : 0);
567   int last_jx = (player->is_moving ? jx - dx : jx);
568   int last_jy = (player->is_moving ? jy - dy : jy);
569   int next_jx = jx + dx;
570   int next_jy = jy + dy;
571   int sx = SCREENX(jx), sy = SCREENY(jy);
572   int sxx = 0, syy = 0;
573   int element = Feld[jx][jy], last_element = Feld[last_jx][last_jy];
574   int graphic;
575   int frame = 0;
576   boolean player_is_moving = (player->MovPos ? TRUE : FALSE);
577   int action = ACTION_DEFAULT;
578 #endif
579
580   if (!player->active || !IN_SCR_FIELD(SCREENX(last_jx), SCREENY(last_jy)))
581     return;
582
583 #if DEBUG
584   if (!IN_LEV_FIELD(jx,jy))
585   {
586     printf("DrawPlayerField(): x = %d, y = %d\n",jx,jy);
587     printf("DrawPlayerField(): sx = %d, sy = %d\n",sx,sy);
588     printf("DrawPlayerField(): This should never happen!\n");
589     return;
590   }
591 #endif
592
593   if (element == EL_EXPLOSION)
594     return;
595
596   action = (player->Pushing ? ACTION_PUSHING :
597             player->is_digging ? ACTION_DIGGING :
598             player->is_collecting ? ACTION_COLLECTING :
599             player->is_moving ? ACTION_MOVING :
600             player->snapped ? ACTION_SNAPPING : ACTION_DEFAULT);
601
602 #if 0
603   printf("::: '%s'\n", element_action_info[action].suffix);
604 #endif
605
606   InitPlayerGfxAnimation(player, action, move_dir);
607
608   /* ----------------------------------------------------------------------- */
609   /* draw things in the field the player is leaving, if needed               */
610   /* ----------------------------------------------------------------------- */
611
612 #if 1
613   if (player->is_moving)
614 #else
615   if (player_is_moving)
616 #endif
617   {
618     if (Back[last_jx][last_jy] && IS_DRAWABLE(last_element))
619     {
620       DrawLevelElement(last_jx, last_jy, Back[last_jx][last_jy]);
621
622       if (last_element == EL_DYNAMITE_ACTIVE ||
623           last_element == EL_SP_DISK_RED_ACTIVE)
624         DrawDynamite(last_jx, last_jy);
625       else
626         DrawLevelFieldThruMask(last_jx, last_jy);
627     }
628     else if (last_element == EL_DYNAMITE_ACTIVE ||
629              last_element == EL_SP_DISK_RED_ACTIVE)
630       DrawDynamite(last_jx, last_jy);
631     else
632       DrawLevelField(last_jx, last_jy);
633
634     if (player->Pushing && IN_SCR_FIELD(SCREENX(next_jx), SCREENY(next_jy)))
635     {
636 #if 1
637 #if 1
638       DrawLevelElement(next_jx, next_jy, EL_EMPTY);
639 #else
640       if (player->GfxPos)
641       {
642         if (Feld[next_jx][next_jy] == EL_SOKOBAN_FIELD_FULL)
643           DrawLevelElement(next_jx, next_jy, EL_SOKOBAN_FIELD_EMPTY);
644         else
645           DrawLevelElement(next_jx, next_jy, EL_EMPTY);
646       }
647       else
648         DrawLevelField(next_jx, next_jy);
649 #endif
650 #endif
651     }
652   }
653
654   if (!IN_SCR_FIELD(sx, sy))
655     return;
656
657   if (setup.direct_draw)
658     SetDrawtoField(DRAW_BUFFERED);
659
660   /* ----------------------------------------------------------------------- */
661   /* draw things behind the player, if needed                                */
662   /* ----------------------------------------------------------------------- */
663
664   if (Back[jx][jy])
665     DrawLevelElement(jx, jy, Back[jx][jy]);
666   else if (IS_ACTIVE_BOMB(element))
667     DrawLevelElement(jx, jy, EL_EMPTY);
668   else
669   {
670     if (player_is_moving && GfxElement[jx][jy] != EL_UNDEFINED)
671     {
672 #if 1
673       if (CAN_BE_CRUMBLED(GfxElement[jx][jy]))
674         DrawLevelFieldCrumbledSandDigging(jx, jy, move_dir, player->StepFrame);
675 #else
676       if (GfxElement[jx][jy] == EL_SAND)
677         DrawLevelFieldCrumbledSandDigging(jx, jy, move_dir, player->StepFrame);
678 #endif
679       else
680       {
681         int old_element = GfxElement[jx][jy];
682         int old_graphic = el_act_dir2img(old_element, action, move_dir);
683         int frame = getGraphicAnimationFrame(old_graphic, player->StepFrame);
684
685         DrawGraphic(sx, sy, old_graphic, frame);
686       }
687     }
688     else
689     {
690       GfxElement[jx][jy] = EL_UNDEFINED;
691
692       DrawLevelField(jx, jy);
693     }
694   }
695
696   /* ----------------------------------------------------------------------- */
697   /* draw player himself                                                     */
698   /* ----------------------------------------------------------------------- */
699
700   if (player->use_murphy_graphic)
701   {
702     static int last_horizontal_dir = MV_LEFT;
703     int direction;
704
705     if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
706       last_horizontal_dir = move_dir;
707
708     direction = (player->snapped ? move_dir : last_horizontal_dir);
709
710     graphic = el_act_dir2img(EL_SP_MURPHY, player->GfxAction, direction);
711   }
712   else
713     graphic = el_act_dir2img(player->element_nr, player->GfxAction, move_dir);
714
715   frame = getGraphicAnimationFrame(graphic, player->Frame);
716
717   if (player->GfxPos)
718   {
719     if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
720       sxx = player->GfxPos;
721     else
722       syy = player->GfxPos;
723   }
724
725   if (!setup.soft_scrolling && ScreenMovPos)
726     sxx = syy = 0;
727
728   DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
729
730   if (SHIELD_ON(player))
731   {
732     int graphic = (player->shield_deadly_time_left ? IMG_SHIELD_DEADLY_ACTIVE :
733                    IMG_SHIELD_NORMAL_ACTIVE);
734     int frame = getGraphicAnimationFrame(graphic, -1);
735
736     DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
737   }
738
739   /* ----------------------------------------------------------------------- */
740   /* draw things the player is pushing, if needed                            */
741   /* ----------------------------------------------------------------------- */
742
743 #if 0
744   printf("::: %d, %d [%d, %d] [%d]\n",
745          player->Pushing, player_is_moving, player->GfxAction,
746          player->is_moving, player_is_moving);
747 #endif
748
749 #if 1
750   if (player->Pushing && player->is_moving)
751 #else
752   if (player->Pushing && player_is_moving)
753 #endif
754   {
755     int px = SCREENX(next_jx), py = SCREENY(next_jy);
756
757     if (Back[next_jx][next_jy])
758       DrawLevelElement(next_jx, next_jy, Back[next_jx][next_jy]);
759
760 #if 1
761     if ((sxx || syy) && element == EL_SOKOBAN_OBJECT)
762       DrawGraphicShiftedThruMask(px, py, sxx, syy, IMG_SOKOBAN_OBJECT, 0,
763                                  NO_CUTTING);
764 #else
765     if ((sxx || syy) &&
766         (element == EL_SOKOBAN_FIELD_EMPTY ||
767          Feld[next_jx][next_jy] == EL_SOKOBAN_FIELD_FULL))
768       DrawGraphicShiftedThruMask(px, py, sxx, syy, IMG_SOKOBAN_OBJECT, 0,
769                                  NO_CUTTING);
770 #endif
771     else
772     {
773 #if 1
774       int element = MovingOrBlocked2Element(next_jx, next_jy);
775 #else
776 #if 1
777       int element = Feld[jx][jy];
778 #else
779       int element = Feld[next_jx][next_jy];
780 #endif
781 #endif
782
783 #if 1
784       int graphic = el2img(element);
785       int frame = 0;
786
787 #if 0
788       if ((sxx || syy) && IS_PUSHABLE(element))
789 #endif
790       {
791         graphic = el_act_dir2img(element, ACTION_PUSHING, move_dir);
792         frame = getGraphicAnimationFrame(graphic, player->Frame);
793       }
794
795 #if 0
796       printf("::: pushing %d: %d ...\n", sxx, frame);
797 #endif
798
799       DrawGraphicShifted(px, py, sxx, syy, graphic, frame,
800                          NO_CUTTING, NO_MASKING);
801 #endif
802     }
803   }
804
805   /* ----------------------------------------------------------------------- */
806   /* draw things in front of player (active dynamite or dynabombs)           */
807   /* ----------------------------------------------------------------------- */
808
809   if (IS_ACTIVE_BOMB(element))
810   {
811     graphic = el2img(element);
812     frame = getGraphicAnimationFrame(graphic, GfxFrame[jx][jy]);
813
814     if (game.emulation == EMU_SUPAPLEX)
815       DrawGraphic(sx, sy, IMG_SP_DISK_RED, frame);
816     else
817       DrawGraphicThruMask(sx, sy, graphic, frame);
818   }
819
820   if (player_is_moving && last_element == EL_EXPLOSION)
821   {
822 #if 1
823     int graphic = el_act2img(GfxElement[last_jx][last_jy], ACTION_EXPLODING);
824 #else
825     int stored = Store[last_jx][last_jy];
826     int graphic = (game.emulation != EMU_SUPAPLEX ? IMG_EXPLOSION :
827                    stored == EL_SP_INFOTRON ? IMG_SP_EXPLOSION_INFOTRON :
828                    IMG_SP_EXPLOSION);
829 #endif
830     int delay = (game.emulation == EMU_SUPAPLEX ? 3 : 2);
831     int phase = ExplodePhase[last_jx][last_jy] - 1;
832     int frame = getGraphicAnimationFrame(graphic, phase - delay);
833
834     if (phase >= delay)
835       DrawGraphicThruMask(SCREENX(last_jx), SCREENY(last_jy), graphic, frame);
836   }
837
838   /* ----------------------------------------------------------------------- */
839   /* draw elements the player is just walking/passing through/under          */
840   /* ----------------------------------------------------------------------- */
841
842   /* handle the field the player is leaving ... */
843   if (player_is_moving && IS_ACCESSIBLE_INSIDE(last_element))
844     DrawLevelField(last_jx, last_jy);
845   else if (player_is_moving && IS_ACCESSIBLE_UNDER(last_element))
846     DrawLevelFieldThruMask(last_jx, last_jy);
847
848   /* ... and the field the player is entering */
849   if (IS_ACCESSIBLE_INSIDE(element))
850     DrawLevelField(jx, jy);
851   else if (IS_ACCESSIBLE_UNDER(element))
852     DrawLevelFieldThruMask(jx, jy);
853
854   if (setup.direct_draw)
855   {
856     int dest_x = SX + SCREENX(MIN(jx, last_jx)) * TILEX;
857     int dest_y = SY + SCREENY(MIN(jy, last_jy)) * TILEY;
858     int x_size = TILEX * (1 + ABS(jx - last_jx));
859     int y_size = TILEY * (1 + ABS(jy - last_jy));
860
861     BlitBitmap(drawto_field, window,
862                dest_x, dest_y, x_size, y_size, dest_x, dest_y);
863     SetDrawtoField(DRAW_DIRECT);
864   }
865
866   MarkTileDirty(sx,sy);
867 }
868
869 void getGraphicSource(int graphic, int frame, Bitmap **bitmap, int *x, int *y)
870 {
871   struct GraphicInfo *g = &graphic_info[graphic];
872
873   *bitmap = g->bitmap;
874
875   if (g->offset_y == 0)         /* frames are ordered horizontally */
876   {
877     int max_width = g->anim_frames_per_line * g->width;
878
879     *x = (g->src_x + frame * g->offset_x) % max_width;
880     *y = g->src_y + (g->src_x + frame * g->offset_x) / max_width * g->height;
881   }
882   else if (g->offset_x == 0)    /* frames are ordered vertically */
883   {
884     int max_height = g->anim_frames_per_line * g->height;
885
886     *x = g->src_x + (g->src_y + frame * g->offset_y) / max_height * g->width;
887     *y = (g->src_y + frame * g->offset_y) % max_height;
888   }
889   else                          /* frames are ordered diagonally */
890   {
891     *x = g->src_x + frame * g->offset_x;
892     *y = g->src_y + frame * g->offset_y;
893   }
894 }
895
896 void DrawGraphic(int x, int y, int graphic, int frame)
897 {
898 #if DEBUG
899   if (!IN_SCR_FIELD(x, y))
900   {
901     printf("DrawGraphic(): x = %d, y = %d, graphic = %d\n", x, y, graphic);
902     printf("DrawGraphic(): This should never happen!\n");
903     return;
904   }
905 #endif
906
907   DrawGraphicExt(drawto_field, FX + x * TILEX, FY + y * TILEY, graphic, frame);
908   MarkTileDirty(x, y);
909 }
910
911 void DrawGraphicExt(DrawBuffer *dst_bitmap, int x, int y, int graphic,
912                     int frame)
913 {
914   Bitmap *src_bitmap;
915   int src_x, src_y;
916
917   getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
918   BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, TILEX, TILEY, x, y);
919 }
920
921 void DrawGraphicThruMask(int x, int y, int graphic, int frame)
922 {
923 #if DEBUG
924   if (!IN_SCR_FIELD(x, y))
925   {
926     printf("DrawGraphicThruMask(): x = %d,y = %d, graphic = %d\n",x,y,graphic);
927     printf("DrawGraphicThruMask(): This should never happen!\n");
928     return;
929   }
930 #endif
931
932   DrawGraphicThruMaskExt(drawto_field, FX + x * TILEX, FY + y *TILEY, graphic,
933                          frame);
934   MarkTileDirty(x, y);
935 }
936
937 void DrawGraphicThruMaskExt(DrawBuffer *d, int dest_x, int dest_y, int graphic,
938                             int frame)
939 {
940 #if 1
941   Bitmap *src_bitmap;
942   int src_x, src_y;
943   GC drawing_gc;
944
945   getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
946   drawing_gc = src_bitmap->stored_clip_gc;
947 #else
948   GC drawing_gc = src_bitmap->stored_clip_gc;
949   Bitmap *src_bitmap = graphic_info[graphic].bitmap;
950   int src_x = graphic_info[graphic].src_x;
951   int src_y = graphic_info[graphic].src_y;
952   int offset_x = graphic_info[graphic].offset_x;
953   int offset_y = graphic_info[graphic].offset_y;
954
955   src_x += frame * offset_x;
956   src_y += frame * offset_y;
957
958 #endif
959
960   SetClipOrigin(src_bitmap, drawing_gc, dest_x - src_x, dest_y - src_y);
961   BlitBitmapMasked(src_bitmap, d, src_x, src_y, TILEX, TILEY, dest_x, dest_y);
962 }
963
964 void DrawMiniGraphic(int x, int y, int graphic)
965 {
966   DrawMiniGraphicExt(drawto, SX + x * MINI_TILEX,SY + y * MINI_TILEY, graphic);
967   MarkTileDirty(x / 2, y / 2);
968 }
969
970 void getMiniGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y)
971 {
972   struct GraphicInfo *g = &graphic_info[graphic];
973   int mini_startx = 0;
974   int mini_starty = g->bitmap->height * 2 / 3;
975
976   *bitmap = g->bitmap;
977   *x = mini_startx + g->src_x / 2;
978   *y = mini_starty + g->src_y / 2;
979 }
980
981 void DrawMiniGraphicExt(DrawBuffer *d, int x, int y, int graphic)
982 {
983   Bitmap *src_bitmap;
984   int src_x, src_y;
985
986   getMiniGraphicSource(graphic, &src_bitmap, &src_x, &src_y);
987   BlitBitmap(src_bitmap, d, src_x, src_y, MINI_TILEX, MINI_TILEY, x, y);
988 }
989
990 void DrawGraphicShifted(int x,int y, int dx,int dy, int graphic, int frame,
991                         int cut_mode, int mask_mode)
992 {
993   Bitmap *src_bitmap;
994   GC drawing_gc;
995   int src_x, src_y;
996   int width = TILEX, height = TILEY;
997   int cx = 0, cy = 0;
998   int dest_x, dest_y;
999
1000   if (graphic < 0)
1001   {
1002     DrawGraphic(x, y, graphic, frame);
1003     return;
1004   }
1005
1006   if (dx || dy)                 /* shifted graphic */
1007   {
1008     if (x < BX1)                /* object enters playfield from the left */
1009     {
1010       x = BX1;
1011       width = dx;
1012       cx = TILEX - dx;
1013       dx = 0;
1014     }
1015     else if (x > BX2)           /* object enters playfield from the right */
1016     {
1017       x = BX2;
1018       width = -dx;
1019       dx = TILEX + dx;
1020     }
1021     else if (x==BX1 && dx < 0)  /* object leaves playfield to the left */
1022     {
1023       width += dx;
1024       cx = -dx;
1025       dx = 0;
1026     }
1027     else if (x==BX2 && dx > 0)  /* object leaves playfield to the right */
1028       width -= dx;
1029     else if (dx)                /* general horizontal movement */
1030       MarkTileDirty(x + SIGN(dx), y);
1031
1032     if (y < BY1)                /* object enters playfield from the top */
1033     {
1034       if (cut_mode==CUT_BELOW)  /* object completely above top border */
1035         return;
1036
1037       y = BY1;
1038       height = dy;
1039       cy = TILEY - dy;
1040       dy = 0;
1041     }
1042     else if (y > BY2)           /* object enters playfield from the bottom */
1043     {
1044       y = BY2;
1045       height = -dy;
1046       dy = TILEY + dy;
1047     }
1048     else if (y==BY1 && dy < 0)  /* object leaves playfield to the top */
1049     {
1050       height += dy;
1051       cy = -dy;
1052       dy = 0;
1053     }
1054     else if (dy > 0 && cut_mode == CUT_ABOVE)
1055     {
1056       if (y == BY2)             /* object completely above bottom border */
1057         return;
1058
1059       height = dy;
1060       cy = TILEY - dy;
1061       dy = TILEY;
1062       MarkTileDirty(x, y + 1);
1063     }                           /* object leaves playfield to the bottom */
1064     else if (dy > 0 && (y == BY2 || cut_mode == CUT_BELOW))
1065       height -= dy;
1066     else if (dy)                /* general vertical movement */
1067       MarkTileDirty(x, y + SIGN(dy));
1068   }
1069
1070 #if 1
1071   getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
1072 #else
1073   src_bitmap = graphic_info[graphic].bitmap;
1074   src_x = graphic_info[graphic].src_x;
1075   src_y = graphic_info[graphic].src_y;
1076   offset_x = graphic_info[graphic].offset_x;
1077   offset_y = graphic_info[graphic].offset_y;
1078
1079   src_x += frame * offset_x;
1080   src_y += frame * offset_y;
1081 #endif
1082
1083   drawing_gc = src_bitmap->stored_clip_gc;
1084
1085   src_x += cx;
1086   src_y += cy;
1087
1088   dest_x = FX + x * TILEX + dx;
1089   dest_y = FY + y * TILEY + dy;
1090
1091 #if DEBUG
1092   if (!IN_SCR_FIELD(x,y))
1093   {
1094     printf("DrawGraphicShifted(): x = %d, y = %d, graphic = %d\n",x,y,graphic);
1095     printf("DrawGraphicShifted(): This should never happen!\n");
1096     return;
1097   }
1098 #endif
1099
1100   if (mask_mode == USE_MASKING)
1101   {
1102     SetClipOrigin(src_bitmap, drawing_gc, dest_x - src_x, dest_y - src_y);
1103     BlitBitmapMasked(src_bitmap, drawto_field, src_x, src_y, width, height,
1104                      dest_x, dest_y);
1105   }
1106   else
1107     BlitBitmap(src_bitmap, drawto_field, src_x, src_y, width, height,
1108                dest_x, dest_y);
1109
1110   MarkTileDirty(x,y);
1111 }
1112
1113 void DrawGraphicShiftedThruMask(int x, int y, int dx, int dy, int graphic,
1114                                 int frame, int cut_mode)
1115 {
1116   DrawGraphicShifted(x,y, dx,dy, graphic, frame, cut_mode, USE_MASKING);
1117 }
1118
1119 void DrawScreenElementExt(int x, int y, int dx, int dy, int element,
1120                           int cut_mode, int mask_mode)
1121 {
1122   int lx = LEVELX(x), ly = LEVELY(y);
1123   int graphic;
1124   int frame;
1125
1126   if (IN_LEV_FIELD(lx, ly))
1127   {
1128     SetRandomAnimationValue(lx, ly);
1129
1130     graphic = el_act_dir2img(element, GfxAction[lx][ly], MovDir[lx][ly]);
1131     frame = getGraphicAnimationFrame(graphic, GfxFrame[lx][ly]);
1132   }
1133   else  /* border element */
1134   {
1135     graphic = el2img(element);
1136     frame = getGraphicAnimationFrame(graphic, -1);
1137   }
1138
1139   if (element == EL_EXPANDABLE_WALL)
1140   {
1141     boolean left_stopped = FALSE, right_stopped = FALSE;
1142
1143     if (!IN_LEV_FIELD(lx - 1, ly) || IS_WALL(Feld[lx - 1][ly]))
1144       left_stopped = TRUE;
1145     if (!IN_LEV_FIELD(lx + 1, ly) || IS_WALL(Feld[lx + 1][ly]))
1146       right_stopped = TRUE;
1147
1148     if (left_stopped && right_stopped)
1149       graphic = IMG_WALL;
1150     else if (left_stopped)
1151     {
1152       graphic = IMG_EXPANDABLE_WALL_GROWING_RIGHT;
1153       frame = graphic_info[graphic].anim_frames - 1;
1154     }
1155     else if (right_stopped)
1156     {
1157       graphic = IMG_EXPANDABLE_WALL_GROWING_LEFT;
1158       frame = graphic_info[graphic].anim_frames - 1;
1159     }
1160   }
1161
1162   if (dx || dy)
1163     DrawGraphicShifted(x, y, dx, dy, graphic, frame, cut_mode, mask_mode);
1164   else if (mask_mode == USE_MASKING)
1165     DrawGraphicThruMask(x, y, graphic, frame);
1166   else
1167     DrawGraphic(x, y, graphic, frame);
1168 }
1169
1170 void DrawLevelElementExt(int x, int y, int dx, int dy, int element,
1171                          int cut_mode, int mask_mode)
1172 {
1173   if (IN_LEV_FIELD(x, y) && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
1174     DrawScreenElementExt(SCREENX(x), SCREENY(y), dx, dy, element,
1175                          cut_mode, mask_mode);
1176 }
1177
1178 void DrawScreenElementShifted(int x, int y, int dx, int dy, int element,
1179                               int cut_mode)
1180 {
1181   DrawScreenElementExt(x, y, dx, dy, element, cut_mode, NO_MASKING);
1182 }
1183
1184 void DrawLevelElementShifted(int x, int y, int dx, int dy, int element,
1185                              int cut_mode)
1186 {
1187   DrawLevelElementExt(x, y, dx, dy, element, cut_mode, NO_MASKING);
1188 }
1189
1190 void DrawLevelElementThruMask(int x, int y, int element)
1191 {
1192   DrawLevelElementExt(x, y, 0, 0, element, NO_CUTTING, USE_MASKING);
1193 }
1194
1195 void DrawLevelFieldThruMask(int x, int y)
1196 {
1197   DrawLevelElementExt(x, y, 0, 0, Feld[x][y], NO_CUTTING, USE_MASKING);
1198 }
1199
1200 static void DrawLevelFieldCrumbledSandExt(int x, int y, int graphic, int frame)
1201 {
1202   Bitmap *src_bitmap;
1203   int src_x, src_y;
1204   int sx = SCREENX(x), sy = SCREENY(y);
1205   int element;
1206   int width, height, cx, cy, i;
1207   int snip = TILEX / 8; /* number of border pixels from "crumbled graphic" */
1208   static int xy[4][2] =
1209   {
1210     { 0, -1 },
1211     { -1, 0 },
1212     { +1, 0 },
1213     { 0, +1 }
1214   };
1215
1216   if (!IN_LEV_FIELD(x, y))
1217     return;
1218
1219   element = (GfxElement[x][y] != EL_UNDEFINED && Feld[x][y] != EL_EXPLOSION ?
1220              GfxElement[x][y] : Feld[x][y]);
1221
1222   /* crumble field itself */
1223   if (CAN_BE_CRUMBLED(element) && !IS_MOVING(x, y))
1224   {
1225     if (!IN_SCR_FIELD(sx, sy))
1226       return;
1227
1228     getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
1229
1230     for(i=0; i<4; i++)
1231     {
1232       int xx = x + xy[i][0];
1233       int yy = y + xy[i][1];
1234
1235       element = (IN_LEV_FIELD(xx, yy) ? Feld[xx][yy] : BorderElement);
1236
1237       /* check if neighbour field is of same type */
1238       if (CAN_BE_CRUMBLED(element) && !IS_MOVING(xx, yy))
1239         continue;
1240
1241 #if 0
1242       if (Feld[x][y] == EL_CUSTOM_START + 123)
1243         printf("::: crumble [%d] THE CHAOS ENGINE (%d, %d): %d, %d\n",
1244                i, Feld[x][y], element,
1245                CAN_BE_CRUMBLED(element), IS_MOVING(x, y));
1246 #endif
1247
1248       if (i == 1 || i == 2)
1249       {
1250         width = snip;
1251         height = TILEY;
1252         cx = (i == 2 ? TILEX - snip : 0);
1253         cy = 0;
1254       }
1255       else
1256       {
1257         width = TILEX;
1258         height = snip;
1259         cx = 0;
1260         cy = (i == 3 ? TILEY - snip : 0);
1261       }
1262
1263       BlitBitmap(src_bitmap, drawto_field, src_x + cx, src_y + cy,
1264                  width, height, FX + sx * TILEX + cx, FY + sy * TILEY + cy);
1265     }
1266
1267     MarkTileDirty(sx, sy);
1268   }
1269   else          /* crumble neighbour fields */
1270   {
1271     getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
1272
1273     for(i=0; i<4; i++)
1274     {
1275       int xx = x + xy[i][0];
1276       int yy = y + xy[i][1];
1277       int sxx = sx + xy[i][0];
1278       int syy = sy + xy[i][1];
1279
1280       if (!IN_LEV_FIELD(xx, yy) ||
1281           !IN_SCR_FIELD(sxx, syy) ||
1282           !CAN_BE_CRUMBLED(Feld[xx][yy]) ||
1283           IS_MOVING(xx, yy))
1284         continue;
1285
1286       if (i == 1 || i == 2)
1287       {
1288         width = snip;
1289         height = TILEY;
1290         cx = (i == 1 ? TILEX - snip : 0);
1291         cy = 0;
1292       }
1293       else
1294       {
1295         width = TILEX;
1296         height = snip;
1297         cx = 0;
1298         cy = (i==0 ? TILEY-snip : 0);
1299       }
1300
1301       BlitBitmap(src_bitmap, drawto_field, src_x + cx, src_y + cy,
1302                  width, height, FX + sxx * TILEX + cx, FY + syy * TILEY + cy);
1303
1304       MarkTileDirty(sxx, syy);
1305     }
1306   }
1307 }
1308
1309 void DrawLevelFieldCrumbledSand(int x, int y)
1310 {
1311   DrawLevelFieldCrumbledSandExt(x, y, IMG_SAND_CRUMBLED, 0);
1312 }
1313
1314 void DrawLevelFieldCrumbledSandDigging(int x, int y, int direction,
1315                                        int step_frame)
1316 {
1317 #if 1
1318   int graphic1 = el_act_dir2img(GfxElement[x][y], ACTION_DIGGING, direction);
1319   int graphic2 = el_act_dir2crm(GfxElement[x][y], ACTION_DIGGING, direction);
1320 #else
1321   int graphic1 = el_act_dir2img(EL_SAND,          ACTION_DIGGING, direction);
1322   int graphic2 = el_act_dir2img(EL_SAND_CRUMBLED, ACTION_DIGGING, direction);
1323 #endif
1324   int frame1 = getGraphicAnimationFrame(graphic1, step_frame);
1325   int frame2 = getGraphicAnimationFrame(graphic2, step_frame);
1326   int sx = SCREENX(x), sy = SCREENY(y);
1327
1328   DrawGraphic(sx, sy, graphic1, frame1);
1329   DrawLevelFieldCrumbledSandExt(x, y, graphic2, frame2);
1330 }
1331
1332 void DrawLevelFieldCrumbledSandNeighbours(int x, int y)
1333 {
1334   int sx = SCREENX(x), sy = SCREENY(y);
1335   static int xy[4][2] =
1336   {
1337     { 0, -1 },
1338     { -1, 0 },
1339     { +1, 0 },
1340     { 0, +1 }
1341   };
1342   int i;
1343
1344   for(i=0; i<4; i++)
1345   {
1346     int xx = x + xy[i][0];
1347     int yy = y + xy[i][1];
1348     int sxx = sx + xy[i][0];
1349     int syy = sy + xy[i][1];
1350
1351     if (!IN_LEV_FIELD(xx, yy) ||
1352         !IN_SCR_FIELD(sxx, syy) ||
1353         !CAN_BE_CRUMBLED(Feld[xx][yy]) ||
1354         IS_MOVING(xx, yy))
1355       continue;
1356
1357     DrawLevelField(xx, yy);
1358   }
1359 }
1360
1361 static int getBorderElement(int x, int y)
1362 {
1363   int border[7][2] =
1364   {
1365     { EL_STEELWALL_TOPLEFT,             EL_INVISIBLE_STEELWALL_TOPLEFT     },
1366     { EL_STEELWALL_TOPRIGHT,            EL_INVISIBLE_STEELWALL_TOPRIGHT    },
1367     { EL_STEELWALL_BOTTOMLEFT,          EL_INVISIBLE_STEELWALL_BOTTOMLEFT  },
1368     { EL_STEELWALL_BOTTOMRIGHT,         EL_INVISIBLE_STEELWALL_BOTTOMRIGHT },
1369     { EL_STEELWALL_VERTICAL,            EL_INVISIBLE_STEELWALL_VERTICAL    },
1370     { EL_STEELWALL_HORIZONTAL,          EL_INVISIBLE_STEELWALL_HORIZONTAL  },
1371     { EL_STEELWALL,                     EL_INVISIBLE_STEELWALL             }
1372   };
1373   int steel_type = (BorderElement == EL_STEELWALL ? 0 : 1);
1374   int steel_position = (x == -1 && y == -1                      ? 0 :
1375                         x == lev_fieldx && y == -1              ? 1 :
1376                         x == -1 && y == lev_fieldy              ? 2 :
1377                         x == lev_fieldx && y == lev_fieldy      ? 3 :
1378                         x == -1 || x == lev_fieldx              ? 4 :
1379                         y == -1 || y == lev_fieldy              ? 5 : 6);
1380
1381   return border[steel_position][steel_type];
1382 }
1383
1384 void DrawScreenElement(int x, int y, int element)
1385 {
1386   DrawScreenElementExt(x, y, 0, 0, element, NO_CUTTING, NO_MASKING);
1387   DrawLevelFieldCrumbledSand(LEVELX(x), LEVELY(y));
1388 }
1389
1390 void DrawLevelElement(int x, int y, int element)
1391 {
1392   if (IN_LEV_FIELD(x, y) && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
1393     DrawScreenElement(SCREENX(x), SCREENY(y), element);
1394 }
1395
1396 void DrawScreenField(int x, int y)
1397 {
1398   int lx = LEVELX(x), ly = LEVELY(y);
1399   int element, content;
1400
1401   if (!IN_LEV_FIELD(lx, ly))
1402   {
1403     if (lx < -1 || lx > lev_fieldx || ly < -1 || ly > lev_fieldy)
1404       element = EL_EMPTY;
1405     else
1406       element = getBorderElement(lx, ly);
1407
1408     DrawScreenElement(x, y, element);
1409     return;
1410   }
1411
1412   element = Feld[lx][ly];
1413   content = Store[lx][ly];
1414
1415   if (IS_MOVING(lx, ly))
1416   {
1417     int horiz_move = (MovDir[lx][ly] == MV_LEFT || MovDir[lx][ly] == MV_RIGHT);
1418     boolean cut_mode = NO_CUTTING;
1419
1420     if (element == EL_QUICKSAND_EMPTYING ||
1421         element == EL_MAGIC_WALL_EMPTYING ||
1422         element == EL_BD_MAGIC_WALL_EMPTYING ||
1423         element == EL_AMOEBA_DROPPING)
1424       cut_mode = CUT_ABOVE;
1425     else if (element == EL_QUICKSAND_FILLING ||
1426              element == EL_MAGIC_WALL_FILLING ||
1427              element == EL_BD_MAGIC_WALL_FILLING)
1428       cut_mode = CUT_BELOW;
1429
1430     if (cut_mode == CUT_ABOVE)
1431       DrawScreenElementShifted(x, y, 0, 0, element, NO_CUTTING);
1432     else
1433       DrawScreenElement(x, y, EL_EMPTY);
1434
1435     if (horiz_move)
1436       DrawScreenElementShifted(x, y, MovPos[lx][ly], 0, element, NO_CUTTING);
1437     else if (cut_mode == NO_CUTTING)
1438       DrawScreenElementShifted(x, y, 0, MovPos[lx][ly], element, cut_mode);
1439     else
1440       DrawScreenElementShifted(x, y, 0, MovPos[lx][ly], content, cut_mode);
1441
1442     if (content == EL_ACID)
1443       DrawLevelElementThruMask(lx, ly + 1, EL_ACID);
1444   }
1445   else if (IS_BLOCKED(lx, ly))
1446   {
1447     int oldx, oldy;
1448     int sx, sy;
1449     int horiz_move;
1450     boolean cut_mode = NO_CUTTING;
1451     int element_old, content_old;
1452
1453     Blocked2Moving(lx, ly, &oldx, &oldy);
1454     sx = SCREENX(oldx);
1455     sy = SCREENY(oldy);
1456     horiz_move = (MovDir[oldx][oldy] == MV_LEFT ||
1457                   MovDir[oldx][oldy] == MV_RIGHT);
1458
1459     element_old = Feld[oldx][oldy];
1460     content_old = Store[oldx][oldy];
1461
1462     if (element_old == EL_QUICKSAND_EMPTYING ||
1463         element_old == EL_MAGIC_WALL_EMPTYING ||
1464         element_old == EL_BD_MAGIC_WALL_EMPTYING ||
1465         element_old == EL_AMOEBA_DROPPING)
1466       cut_mode = CUT_ABOVE;
1467
1468     DrawScreenElement(x, y, EL_EMPTY);
1469
1470     if (horiz_move)
1471       DrawScreenElementShifted(sx, sy, MovPos[oldx][oldy], 0, element_old,
1472                                NO_CUTTING);
1473     else if (cut_mode == NO_CUTTING)
1474       DrawScreenElementShifted(sx, sy, 0, MovPos[oldx][oldy], element_old,
1475                                cut_mode);
1476     else
1477       DrawScreenElementShifted(sx, sy, 0, MovPos[oldx][oldy], content_old,
1478                                cut_mode);
1479   }
1480   else if (IS_DRAWABLE(element))
1481     DrawScreenElement(x, y, element);
1482   else
1483     DrawScreenElement(x, y, EL_EMPTY);
1484 }
1485
1486 void DrawLevelField(int x, int y)
1487 {
1488   if (IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
1489     DrawScreenField(SCREENX(x), SCREENY(y));
1490   else if (IS_MOVING(x, y))
1491   {
1492     int newx,newy;
1493
1494     Moving2Blocked(x, y, &newx, &newy);
1495     if (IN_SCR_FIELD(SCREENX(newx), SCREENY(newy)))
1496       DrawScreenField(SCREENX(newx), SCREENY(newy));
1497   }
1498   else if (IS_BLOCKED(x, y))
1499   {
1500     int oldx, oldy;
1501
1502     Blocked2Moving(x, y, &oldx, &oldy);
1503     if (IN_SCR_FIELD(SCREENX(oldx), SCREENY(oldy)))
1504       DrawScreenField(SCREENX(oldx), SCREENY(oldy));
1505   }
1506 }
1507
1508 void DrawMiniElement(int x, int y, int element)
1509 {
1510   int graphic;
1511
1512   graphic = el2edimg(element);
1513   DrawMiniGraphic(x, y, graphic);
1514 }
1515
1516 void DrawMiniElementOrWall(int sx, int sy, int scroll_x, int scroll_y)
1517 {
1518   int x = sx + scroll_x, y = sy + scroll_y;
1519
1520   if (x < -1 || x > lev_fieldx || y < -1 || y > lev_fieldy)
1521     DrawMiniElement(sx, sy, EL_EMPTY);
1522   else if (x > -1 && x < lev_fieldx && y > -1 && y < lev_fieldy)
1523     DrawMiniElement(sx, sy, Feld[x][y]);
1524   else
1525     DrawMiniGraphic(sx, sy, el2edimg(getBorderElement(x, y)));
1526 }
1527
1528 void getMicroGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y)
1529 {
1530   Bitmap *src_bitmap = graphic_info[graphic].bitmap;
1531   int mini_startx = src_bitmap->width * 3 / 4;
1532   int mini_starty = src_bitmap->height * 2 / 3;
1533   int src_x = mini_startx + graphic_info[graphic].src_x / 8;
1534   int src_y = mini_starty + graphic_info[graphic].src_y / 8;
1535
1536   *bitmap = src_bitmap;
1537   *x = src_x;
1538   *y = src_y;
1539 }
1540
1541 void DrawMicroElement(int xpos, int ypos, int element)
1542 {
1543   Bitmap *src_bitmap;
1544   int src_x, src_y;
1545   int graphic = el2preimg(element);
1546
1547   getMicroGraphicSource(graphic, &src_bitmap, &src_x, &src_y);
1548   BlitBitmap(src_bitmap, drawto, src_x, src_y, MICRO_TILEX, MICRO_TILEY,
1549              xpos, ypos);
1550 }
1551
1552 void DrawLevel()
1553 {
1554   int x,y;
1555
1556   SetDrawBackgroundMask(REDRAW_NONE);
1557   ClearWindow();
1558
1559   for(x=BX1; x<=BX2; x++)
1560     for(y=BY1; y<=BY2; y++)
1561       DrawScreenField(x, y);
1562
1563   redraw_mask |= REDRAW_FIELD;
1564 }
1565
1566 void DrawMiniLevel(int size_x, int size_y, int scroll_x, int scroll_y)
1567 {
1568   int x,y;
1569
1570   for(x=0; x<size_x; x++)
1571     for(y=0; y<size_y; y++)
1572       DrawMiniElementOrWall(x, y, scroll_x, scroll_y);
1573
1574   redraw_mask |= REDRAW_FIELD;
1575 }
1576
1577 static void DrawMicroLevelExt(int xpos, int ypos, int from_x, int from_y)
1578 {
1579   int x, y;
1580
1581   DrawBackground(xpos, ypos, MICROLEV_XSIZE, MICROLEV_YSIZE);
1582
1583   if (lev_fieldx < STD_LEV_FIELDX)
1584     xpos += (STD_LEV_FIELDX - lev_fieldx) / 2 * MICRO_TILEX;
1585   if (lev_fieldy < STD_LEV_FIELDY)
1586     ypos += (STD_LEV_FIELDY - lev_fieldy) / 2 * MICRO_TILEY;
1587
1588   xpos += MICRO_TILEX;
1589   ypos += MICRO_TILEY;
1590
1591   for(x=-1; x<=STD_LEV_FIELDX; x++)
1592   {
1593     for(y=-1; y<=STD_LEV_FIELDY; y++)
1594     {
1595       int lx = from_x + x, ly = from_y + y;
1596
1597       if (lx >= 0 && lx < lev_fieldx && ly >= 0 && ly < lev_fieldy)
1598         DrawMicroElement(xpos + x * MICRO_TILEX, ypos + y * MICRO_TILEY,
1599                          level.field[lx][ly]);
1600       else if (lx >= -1 && lx < lev_fieldx+1 && ly >= -1 && ly < lev_fieldy+1
1601                && BorderElement != EL_EMPTY)
1602         DrawMicroElement(xpos + x * MICRO_TILEX, ypos + y * MICRO_TILEY,
1603                          getBorderElement(lx, ly));
1604     }
1605   }
1606
1607   redraw_mask |= REDRAW_MICROLEVEL;
1608 }
1609
1610 #define MICROLABEL_EMPTY                0
1611 #define MICROLABEL_LEVEL_NAME           1
1612 #define MICROLABEL_CREATED_BY           2
1613 #define MICROLABEL_LEVEL_AUTHOR         3
1614 #define MICROLABEL_IMPORTED_FROM        4
1615 #define MICROLABEL_LEVEL_IMPORT_INFO    5
1616
1617 static void DrawMicroLevelLabelExt(int mode)
1618 {
1619   char label_text[MAX_OUTPUT_LINESIZE + 1];
1620   int max_len_label_text;
1621   int font_nr = FONT_TEXT_2;
1622
1623   if (mode == MICROLABEL_CREATED_BY || mode == MICROLABEL_IMPORTED_FROM)
1624     font_nr = FONT_TEXT_3;
1625
1626   max_len_label_text = SXSIZE / getFontWidth(font_nr);
1627
1628   DrawBackground(SX, MICROLABEL_YPOS, SXSIZE, getFontHeight(font_nr));
1629
1630   strncpy(label_text, (mode == MICROLABEL_LEVEL_NAME ? level.name :
1631                        mode == MICROLABEL_CREATED_BY ? "created by" :
1632                        mode == MICROLABEL_LEVEL_AUTHOR ? level.author :
1633                        mode == MICROLABEL_IMPORTED_FROM ? "imported from" :
1634                        mode == MICROLABEL_LEVEL_IMPORT_INFO ?
1635                        leveldir_current->imported_from : ""),
1636           max_len_label_text);
1637   label_text[max_len_label_text] = '\0';
1638
1639   if (strlen(label_text) > 0)
1640   {
1641     int lxpos = SX + (SXSIZE - getTextWidth(label_text, font_nr)) / 2;
1642     int lypos = MICROLABEL_YPOS;
1643
1644     DrawText(lxpos, lypos, label_text, font_nr);
1645   }
1646
1647   redraw_mask |= REDRAW_MICROLEVEL;
1648 }
1649
1650 void DrawMicroLevel(int xpos, int ypos, boolean restart)
1651 {
1652   static unsigned long scroll_delay = 0;
1653   static unsigned long label_delay = 0;
1654   static int from_x, from_y, scroll_direction;
1655   static int label_state, label_counter;
1656   int last_game_status = game_status;   /* save current game status */
1657
1658   /* force PREVIEW font on preview level */
1659   game_status = GAME_MODE_PSEUDO_PREVIEW;
1660
1661   if (restart)
1662   {
1663     from_x = from_y = 0;
1664     scroll_direction = MV_RIGHT;
1665     label_state = 1;
1666     label_counter = 0;
1667
1668     DrawMicroLevelExt(xpos, ypos, from_x, from_y);
1669     DrawMicroLevelLabelExt(label_state);
1670
1671     /* initialize delay counters */
1672     DelayReached(&scroll_delay, 0);
1673     DelayReached(&label_delay, 0);
1674
1675     if (leveldir_current->name)
1676     {
1677       int text_width = getTextWidth(leveldir_current->name, FONT_TEXT_1);
1678       int lxpos = SX + (SXSIZE - text_width) / 2;
1679       int lypos = SY + 352;
1680
1681       DrawText(lxpos, lypos, leveldir_current->name, FONT_TEXT_1);
1682     }
1683
1684     game_status = last_game_status;     /* restore current game status */
1685
1686     return;
1687   }
1688
1689   /* scroll micro level, if needed */
1690   if ((lev_fieldx > STD_LEV_FIELDX || lev_fieldy > STD_LEV_FIELDY) &&
1691       DelayReached(&scroll_delay, MICROLEVEL_SCROLL_DELAY))
1692   {
1693     switch (scroll_direction)
1694     {
1695       case MV_LEFT:
1696         if (from_x > 0)
1697           from_x--;
1698         else
1699           scroll_direction = MV_UP;
1700         break;
1701
1702       case MV_RIGHT:
1703         if (from_x < lev_fieldx - STD_LEV_FIELDX)
1704           from_x++;
1705         else
1706           scroll_direction = MV_DOWN;
1707         break;
1708
1709       case MV_UP:
1710         if (from_y > 0)
1711           from_y--;
1712         else
1713           scroll_direction = MV_RIGHT;
1714         break;
1715
1716       case MV_DOWN:
1717         if (from_y < lev_fieldy - STD_LEV_FIELDY)
1718           from_y++;
1719         else
1720           scroll_direction = MV_LEFT;
1721         break;
1722
1723       default:
1724         break;
1725     }
1726
1727     DrawMicroLevelExt(xpos, ypos, from_x, from_y);
1728   }
1729
1730   /* redraw micro level label, if needed */
1731   if (strcmp(level.name, NAMELESS_LEVEL_NAME) != 0 &&
1732       strcmp(level.author, ANONYMOUS_NAME) != 0 &&
1733       strcmp(level.author, leveldir_current->name) != 0 &&
1734       DelayReached(&label_delay, MICROLEVEL_LABEL_DELAY))
1735   {
1736     int max_label_counter = 23;
1737
1738     if (leveldir_current->imported_from != NULL)
1739       max_label_counter += 14;
1740
1741     label_counter = (label_counter + 1) % max_label_counter;
1742     label_state = (label_counter >= 0 && label_counter <= 7 ?
1743                    MICROLABEL_LEVEL_NAME :
1744                    label_counter >= 9 && label_counter <= 12 ?
1745                    MICROLABEL_CREATED_BY :
1746                    label_counter >= 14 && label_counter <= 21 ?
1747                    MICROLABEL_LEVEL_AUTHOR :
1748                    label_counter >= 23 && label_counter <= 26 ?
1749                    MICROLABEL_IMPORTED_FROM :
1750                    label_counter >= 28 && label_counter <= 35 ?
1751                    MICROLABEL_LEVEL_IMPORT_INFO : MICROLABEL_EMPTY);
1752     DrawMicroLevelLabelExt(label_state);
1753   }
1754
1755   game_status = last_game_status;       /* restore current game status */
1756 }
1757
1758 int REQ_in_range(int x, int y)
1759 {
1760   if (y > DY+249 && y < DY+278)
1761   {
1762     if (x > DX+1 && x < DX+48)
1763       return 1;
1764     else if (x > DX+51 && x < DX+98) 
1765       return 2;
1766   }
1767   return 0;
1768 }
1769
1770 #define MAX_REQUEST_LINES               13
1771 #define MAX_REQUEST_LINE_LEN            7
1772
1773 boolean Request(char *text, unsigned int req_state)
1774 {
1775   int mx, my, ty, result = -1;
1776   unsigned int old_door_state;
1777   int last_game_status = game_status;   /* save current game status */
1778
1779 #if 1
1780   SetMouseCursor(CURSOR_DEFAULT);
1781 #endif
1782
1783 #if defined(PLATFORM_UNIX)
1784   /* pause network game while waiting for request to answer */
1785   if (options.network &&
1786       game_status == GAME_MODE_PLAYING &&
1787       req_state & REQUEST_WAIT_FOR)
1788     SendToServer_PausePlaying();
1789 #endif
1790
1791   old_door_state = GetDoorState();
1792
1793   /* simulate releasing mouse button over last gadget, if still pressed */
1794   if (button_status)
1795     HandleGadgets(-1, -1, 0);
1796
1797   UnmapAllGadgets();
1798
1799   CloseDoor(DOOR_CLOSE_1);
1800
1801   /* save old door content */
1802   BlitBitmap(bitmap_db_door, bitmap_db_door,
1803              DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE,
1804              DOOR_GFX_PAGEX2, DOOR_GFX_PAGEY1);
1805
1806   SetDrawBackgroundMask(REDRAW_FIELD | REDRAW_DOOR_1);
1807
1808   /* clear door drawing field */
1809   DrawBackground(DX, DY, DXSIZE, DYSIZE);
1810
1811   /* force DOOR font on preview level */
1812   game_status = GAME_MODE_PSEUDO_DOOR;
1813
1814   /* write text for request */
1815   for(ty=0; ty < MAX_REQUEST_LINES; ty++)
1816   {
1817     char text_line[MAX_REQUEST_LINE_LEN + 1];
1818     int tx, tl, tc;
1819
1820     if (!*text)
1821       break;
1822
1823     for(tl=0,tx=0; tx < MAX_REQUEST_LINE_LEN; tl++,tx++)
1824     {
1825       tc = *(text + tx);
1826       if (!tc || tc == ' ')
1827         break;
1828     }
1829
1830     if (!tl)
1831     { 
1832       text++; 
1833       ty--; 
1834       continue; 
1835     }
1836
1837     strncpy(text_line, text, tl);
1838     text_line[tl] = 0;
1839
1840     DrawText(DX + (DXSIZE - tl * getFontWidth(FONT_TEXT_2)) / 2,
1841              DY + 8 + ty * (getFontHeight(FONT_TEXT_2) + 2),
1842              text_line, FONT_TEXT_2);
1843
1844     text += tl + (tc == ' ' ? 1 : 0);
1845   }
1846
1847   game_status = last_game_status;       /* restore current game status */
1848
1849   if (req_state & REQ_ASK)
1850   {
1851     MapGadget(tool_gadget[TOOL_CTRL_ID_YES]);
1852     MapGadget(tool_gadget[TOOL_CTRL_ID_NO]);
1853   }
1854   else if (req_state & REQ_CONFIRM)
1855   {
1856     MapGadget(tool_gadget[TOOL_CTRL_ID_CONFIRM]);
1857   }
1858   else if (req_state & REQ_PLAYER)
1859   {
1860     MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_1]);
1861     MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_2]);
1862     MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_3]);
1863     MapGadget(tool_gadget[TOOL_CTRL_ID_PLAYER_4]);
1864   }
1865
1866   /* copy request gadgets to door backbuffer */
1867   BlitBitmap(drawto, bitmap_db_door,
1868              DX, DY, DXSIZE, DYSIZE,
1869              DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
1870
1871   OpenDoor(DOOR_OPEN_1);
1872
1873 #if 0
1874   ClearEventQueue();
1875 #endif
1876
1877   if (!(req_state & REQUEST_WAIT_FOR))
1878   {
1879     SetDrawBackgroundMask(REDRAW_FIELD);
1880
1881     return FALSE;
1882   }
1883
1884   if (game_status != GAME_MODE_MAIN)
1885     InitAnimation();
1886
1887   button_status = MB_RELEASED;
1888
1889   request_gadget_id = -1;
1890
1891   SetDrawBackgroundMask(REDRAW_FIELD | REDRAW_DOOR_1);
1892
1893 #if 0
1894   SetMouseCursor(CURSOR_DEFAULT);
1895 #endif
1896
1897   while(result < 0)
1898   {
1899     if (PendingEvent())
1900     {
1901       Event event;
1902
1903       NextEvent(&event);
1904
1905       switch(event.type)
1906       {
1907         case EVENT_BUTTONPRESS:
1908         case EVENT_BUTTONRELEASE:
1909         case EVENT_MOTIONNOTIFY:
1910         {
1911           if (event.type == EVENT_MOTIONNOTIFY)
1912           {
1913             if (!PointerInWindow(window))
1914               continue; /* window and pointer are on different screens */
1915
1916             if (!button_status)
1917               continue;
1918
1919             motion_status = TRUE;
1920             mx = ((MotionEvent *) &event)->x;
1921             my = ((MotionEvent *) &event)->y;
1922           }
1923           else
1924           {
1925             motion_status = FALSE;
1926             mx = ((ButtonEvent *) &event)->x;
1927             my = ((ButtonEvent *) &event)->y;
1928             if (event.type == EVENT_BUTTONPRESS)
1929               button_status = ((ButtonEvent *) &event)->button;
1930             else
1931               button_status = MB_RELEASED;
1932           }
1933
1934           /* this sets 'request_gadget_id' */
1935           HandleGadgets(mx, my, button_status);
1936
1937           switch(request_gadget_id)
1938           {
1939             case TOOL_CTRL_ID_YES:
1940               result = TRUE;
1941               break;
1942             case TOOL_CTRL_ID_NO:
1943               result = FALSE;
1944               break;
1945             case TOOL_CTRL_ID_CONFIRM:
1946               result = TRUE | FALSE;
1947               break;
1948
1949             case TOOL_CTRL_ID_PLAYER_1:
1950               result = 1;
1951               break;
1952             case TOOL_CTRL_ID_PLAYER_2:
1953               result = 2;
1954               break;
1955             case TOOL_CTRL_ID_PLAYER_3:
1956               result = 3;
1957               break;
1958             case TOOL_CTRL_ID_PLAYER_4:
1959               result = 4;
1960               break;
1961
1962             default:
1963               break;
1964           }
1965
1966           break;
1967         }
1968
1969         case EVENT_KEYPRESS:
1970           switch(GetEventKey((KeyEvent *)&event, TRUE))
1971           {
1972             case KSYM_Return:
1973               result = 1;
1974               break;
1975
1976             case KSYM_Escape:
1977               result = 0;
1978               break;
1979
1980             default:
1981               break;
1982           }
1983           if (req_state & REQ_PLAYER)
1984             result = 0;
1985           break;
1986
1987         case EVENT_KEYRELEASE:
1988           ClearPlayerAction();
1989           break;
1990
1991         default:
1992           HandleOtherEvents(&event);
1993           break;
1994       }
1995     }
1996     else if (AnyJoystickButton() == JOY_BUTTON_NEW_PRESSED)
1997     {
1998       int joy = AnyJoystick();
1999
2000       if (joy & JOY_BUTTON_1)
2001         result = 1;
2002       else if (joy & JOY_BUTTON_2)
2003         result = 0;
2004     }
2005
2006     DoAnimation();
2007
2008     /* don't eat all CPU time */
2009     Delay(10);
2010   }
2011
2012   if (game_status != GAME_MODE_MAIN)
2013     StopAnimation();
2014
2015   UnmapToolButtons();
2016
2017   if (!(req_state & REQ_STAY_OPEN))
2018   {
2019     CloseDoor(DOOR_CLOSE_1);
2020
2021     if (!(req_state & REQ_STAY_CLOSED) && (old_door_state & DOOR_OPEN_1))
2022     {
2023       BlitBitmap(bitmap_db_door, bitmap_db_door,
2024                  DOOR_GFX_PAGEX2,DOOR_GFX_PAGEY1, DXSIZE,DYSIZE,
2025                  DOOR_GFX_PAGEX1,DOOR_GFX_PAGEY1);
2026       OpenDoor(DOOR_OPEN_1);
2027     }
2028   }
2029
2030   RemapAllGadgets();
2031
2032   SetDrawBackgroundMask(REDRAW_FIELD);
2033
2034 #if defined(PLATFORM_UNIX)
2035   /* continue network game after request */
2036   if (options.network &&
2037       game_status == GAME_MODE_PLAYING &&
2038       req_state & REQUEST_WAIT_FOR)
2039     SendToServer_ContinuePlaying();
2040 #endif
2041
2042   return result;
2043 }
2044
2045 unsigned int OpenDoor(unsigned int door_state)
2046 {
2047   unsigned int new_door_state;
2048
2049   if (door_state & DOOR_COPY_BACK)
2050   {
2051     BlitBitmap(bitmap_db_door, bitmap_db_door,
2052                DOOR_GFX_PAGEX2, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE + VYSIZE,
2053                DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
2054     door_state &= ~DOOR_COPY_BACK;
2055   }
2056
2057   new_door_state = MoveDoor(door_state);
2058
2059   return(new_door_state);
2060 }
2061
2062 unsigned int CloseDoor(unsigned int door_state)
2063 {
2064   unsigned int new_door_state;
2065
2066   BlitBitmap(backbuffer, bitmap_db_door,
2067              DX, DY, DXSIZE, DYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
2068   BlitBitmap(backbuffer, bitmap_db_door,
2069              VX, VY, VXSIZE, VYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2);
2070
2071   new_door_state = MoveDoor(door_state);
2072
2073   return(new_door_state);
2074 }
2075
2076 unsigned int GetDoorState()
2077 {
2078   return MoveDoor(DOOR_GET_STATE);
2079 }
2080
2081 unsigned int SetDoorState(unsigned int door_state)
2082 {
2083   return MoveDoor(door_state | DOOR_SET_STATE);
2084 }
2085
2086 unsigned int MoveDoor(unsigned int door_state)
2087 {
2088   static int door1 = DOOR_OPEN_1;
2089   static int door2 = DOOR_CLOSE_2;
2090   static unsigned long door_delay = 0;
2091   int x, start, stepsize = door.step_offset;
2092   unsigned long door_delay_value = door.step_delay;
2093
2094   if (door_state == DOOR_GET_STATE)
2095     return(door1 | door2);
2096
2097   if (door_state & DOOR_SET_STATE)
2098   {
2099     if (door_state & DOOR_ACTION_1)
2100       door1 = door_state & DOOR_ACTION_1;
2101     if (door_state & DOOR_ACTION_2)
2102       door2 = door_state & DOOR_ACTION_2;
2103
2104     return(door1 | door2);
2105   }
2106
2107   if (door1 == DOOR_OPEN_1 && door_state & DOOR_OPEN_1)
2108     door_state &= ~DOOR_OPEN_1;
2109   else if (door1 == DOOR_CLOSE_1 && door_state & DOOR_CLOSE_1)
2110     door_state &= ~DOOR_CLOSE_1;
2111   if (door2 == DOOR_OPEN_2 && door_state & DOOR_OPEN_2)
2112     door_state &= ~DOOR_OPEN_2;
2113   else if (door2 == DOOR_CLOSE_2 && door_state & DOOR_CLOSE_2)
2114     door_state &= ~DOOR_CLOSE_2;
2115
2116   if (setup.quick_doors)
2117   {
2118     stepsize = 20;
2119     door_delay_value = 0;
2120
2121     StopSound(SND_DOOR_OPENING);
2122     StopSound(SND_DOOR_CLOSING);
2123   }
2124
2125   if (global.autoplay_leveldir)
2126   {
2127     door_state |= DOOR_NO_DELAY;
2128     door_state &= ~DOOR_CLOSE_ALL;
2129   }
2130
2131   if (door_state & DOOR_ACTION)
2132   {
2133     if (!(door_state & DOOR_NO_DELAY))
2134     {
2135       /* opening door sound has priority over simultaneously closing door */
2136       if (door_state & (DOOR_OPEN_1 | DOOR_OPEN_2))
2137         PlaySoundStereo(SND_DOOR_OPENING, SOUND_MIDDLE);
2138       else if (door_state & (DOOR_CLOSE_1 | DOOR_CLOSE_2))
2139         PlaySoundStereo(SND_DOOR_CLOSING, SOUND_MIDDLE);
2140     }
2141
2142     start = ((door_state & DOOR_NO_DELAY) ? DXSIZE : 0);
2143
2144     for(x=start; x<=DXSIZE; x+=stepsize)
2145     {
2146       Bitmap *bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
2147       GC gc = bitmap->stored_clip_gc;
2148
2149       if (!(door_state & DOOR_NO_DELAY))
2150         WaitUntilDelayReached(&door_delay, door_delay_value);
2151
2152       if (door_state & DOOR_ACTION_1)
2153       {
2154         int i = (door_state & DOOR_OPEN_1 ? DXSIZE-x : x);
2155         int j = (DXSIZE - i) / 3;
2156
2157         BlitBitmap(bitmap_db_door, drawto,
2158                    DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1 + i/2,
2159                    DXSIZE,DYSIZE - i/2, DX, DY);
2160
2161         ClearRectangle(drawto, DX, DY + DYSIZE - i/2, DXSIZE,i/2);
2162
2163         SetClipOrigin(bitmap, gc, DX - i, (DY + j) - DOOR_GFX_PAGEY1);
2164         BlitBitmapMasked(bitmap, drawto,
2165                          DXSIZE, DOOR_GFX_PAGEY1, i, 77,
2166                          DX + DXSIZE - i, DY + j);
2167         BlitBitmapMasked(bitmap, drawto,
2168                          DXSIZE, DOOR_GFX_PAGEY1 + 140, i, 63,
2169                          DX + DXSIZE - i, DY + 140 + j);
2170         SetClipOrigin(bitmap, gc, DX - DXSIZE + i, DY - (DOOR_GFX_PAGEY1 + j));
2171         BlitBitmapMasked(bitmap, drawto,
2172                          DXSIZE - i, DOOR_GFX_PAGEY1 + j, i, 77 - j,
2173                          DX, DY);
2174         BlitBitmapMasked(bitmap, drawto,
2175                          DXSIZE-i, DOOR_GFX_PAGEY1 + 140, i, 63,
2176                          DX, DY + 140 - j);
2177
2178         BlitBitmapMasked(bitmap, drawto,
2179                          DXSIZE - i, DOOR_GFX_PAGEY1 + 77, i, 63,
2180                          DX, DY + 77 - j);
2181         BlitBitmapMasked(bitmap, drawto,
2182                          DXSIZE - i, DOOR_GFX_PAGEY1 + 203, i, 77,
2183                          DX, DY + 203 - j);
2184         SetClipOrigin(bitmap, gc, DX - i, (DY + j) - DOOR_GFX_PAGEY1);
2185         BlitBitmapMasked(bitmap, drawto,
2186                          DXSIZE, DOOR_GFX_PAGEY1 + 77, i, 63,
2187                          DX + DXSIZE - i, DY + 77 + j);
2188         BlitBitmapMasked(bitmap, drawto,
2189                          DXSIZE, DOOR_GFX_PAGEY1 + 203, i, 77 - j,
2190                          DX + DXSIZE - i, DY + 203 + j);
2191
2192         redraw_mask |= REDRAW_DOOR_1;
2193       }
2194
2195       if (door_state & DOOR_ACTION_2)
2196       {
2197         int i = (door_state & DOOR_OPEN_2 ? VXSIZE - x : x);
2198         int j = (VXSIZE - i) / 3;
2199
2200         BlitBitmap(bitmap_db_door, drawto,
2201                    DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2 + i/2,
2202                    VXSIZE, VYSIZE - i/2, VX, VY);
2203
2204         ClearRectangle(drawto, VX, VY + VYSIZE-i/2, VXSIZE, i/2);
2205
2206         SetClipOrigin(bitmap, gc, VX - i, (VY + j) - DOOR_GFX_PAGEY2);
2207         BlitBitmapMasked(bitmap, drawto,
2208                          VXSIZE, DOOR_GFX_PAGEY2, i, VYSIZE / 2,
2209                          VX + VXSIZE-i, VY+j);
2210         SetClipOrigin(bitmap, gc,
2211                       VX - VXSIZE + i, VY - (DOOR_GFX_PAGEY2 + j));
2212         BlitBitmapMasked(bitmap, drawto,
2213                          VXSIZE - i, DOOR_GFX_PAGEY2 + j, i, VYSIZE / 2 - j,
2214                          VX, VY);
2215
2216         BlitBitmapMasked(bitmap, drawto,
2217                          VXSIZE - i, DOOR_GFX_PAGEY2 + VYSIZE / 2,
2218                          i, VYSIZE / 2, VX, VY + VYSIZE / 2 - j);
2219         SetClipOrigin(bitmap, gc, VX - i, (VY + j) - DOOR_GFX_PAGEY2);
2220         BlitBitmapMasked(bitmap, drawto,
2221                          VXSIZE, DOOR_GFX_PAGEY2 + VYSIZE / 2,
2222                          i, VYSIZE / 2 - j,
2223                          VX + VXSIZE - i, VY + VYSIZE / 2 + j);
2224
2225         redraw_mask |= REDRAW_DOOR_2;
2226       }
2227
2228       BackToFront();
2229
2230       if (game_status == GAME_MODE_MAIN)
2231         DoAnimation();
2232     }
2233   }
2234
2235   if (setup.quick_doors)
2236   {
2237     StopSound(SND_DOOR_OPENING);
2238     StopSound(SND_DOOR_CLOSING);
2239   }
2240
2241   if (door_state & DOOR_ACTION_1)
2242     door1 = door_state & DOOR_ACTION_1;
2243   if (door_state & DOOR_ACTION_2)
2244     door2 = door_state & DOOR_ACTION_2;
2245
2246   return (door1 | door2);
2247 }
2248
2249 void DrawSpecialEditorDoor()
2250 {
2251   /* draw bigger toolbox window */
2252   BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
2253              DOOR_GFX_PAGEX7, 0, EXSIZE + 8, 8,
2254              EX - 4, EY - 12);
2255   BlitBitmap(graphic_info[IMG_GLOBAL_BORDER].bitmap, drawto,
2256              EX - 4, VY - 4, EXSIZE + 8, EYSIZE - VYSIZE + 4,
2257              EX - 4, EY - 4);
2258
2259   redraw_mask |= REDRAW_ALL;
2260 }
2261
2262 void UndrawSpecialEditorDoor()
2263 {
2264   /* draw normal tape recorder window */
2265   BlitBitmap(graphic_info[IMG_GLOBAL_BORDER].bitmap, drawto,
2266              EX - 4, EY - 12, EXSIZE + 8, EYSIZE - VYSIZE + 12,
2267              EX - 4, EY - 12);
2268
2269   redraw_mask |= REDRAW_ALL;
2270 }
2271
2272
2273 /* ---------- new tool button stuff ---------------------------------------- */
2274
2275 /* graphic position values for tool buttons */
2276 #define TOOL_BUTTON_YES_XPOS            2
2277 #define TOOL_BUTTON_YES_YPOS            250
2278 #define TOOL_BUTTON_YES_GFX_YPOS        0
2279 #define TOOL_BUTTON_YES_XSIZE           46
2280 #define TOOL_BUTTON_YES_YSIZE           28
2281 #define TOOL_BUTTON_NO_XPOS             52
2282 #define TOOL_BUTTON_NO_YPOS             TOOL_BUTTON_YES_YPOS
2283 #define TOOL_BUTTON_NO_GFX_YPOS         TOOL_BUTTON_YES_GFX_YPOS
2284 #define TOOL_BUTTON_NO_XSIZE            TOOL_BUTTON_YES_XSIZE
2285 #define TOOL_BUTTON_NO_YSIZE            TOOL_BUTTON_YES_YSIZE
2286 #define TOOL_BUTTON_CONFIRM_XPOS        TOOL_BUTTON_YES_XPOS
2287 #define TOOL_BUTTON_CONFIRM_YPOS        TOOL_BUTTON_YES_YPOS
2288 #define TOOL_BUTTON_CONFIRM_GFX_YPOS    30
2289 #define TOOL_BUTTON_CONFIRM_XSIZE       96
2290 #define TOOL_BUTTON_CONFIRM_YSIZE       TOOL_BUTTON_YES_YSIZE
2291 #define TOOL_BUTTON_PLAYER_XSIZE        30
2292 #define TOOL_BUTTON_PLAYER_YSIZE        30
2293 #define TOOL_BUTTON_PLAYER_GFX_XPOS     5
2294 #define TOOL_BUTTON_PLAYER_GFX_YPOS     185
2295 #define TOOL_BUTTON_PLAYER_XPOS         (5 + TOOL_BUTTON_PLAYER_XSIZE / 2)
2296 #define TOOL_BUTTON_PLAYER_YPOS         (215 - TOOL_BUTTON_PLAYER_YSIZE / 2)
2297 #define TOOL_BUTTON_PLAYER1_XPOS        (TOOL_BUTTON_PLAYER_XPOS \
2298                                          + 0 * TOOL_BUTTON_PLAYER_XSIZE)
2299 #define TOOL_BUTTON_PLAYER2_XPOS        (TOOL_BUTTON_PLAYER_XPOS \
2300                                          + 1 * TOOL_BUTTON_PLAYER_XSIZE)
2301 #define TOOL_BUTTON_PLAYER3_XPOS        (TOOL_BUTTON_PLAYER_XPOS \
2302                                          + 0 * TOOL_BUTTON_PLAYER_XSIZE)
2303 #define TOOL_BUTTON_PLAYER4_XPOS        (TOOL_BUTTON_PLAYER_XPOS \
2304                                          + 1 * TOOL_BUTTON_PLAYER_XSIZE)
2305 #define TOOL_BUTTON_PLAYER1_YPOS        (TOOL_BUTTON_PLAYER_YPOS \
2306                                          + 0 * TOOL_BUTTON_PLAYER_YSIZE)
2307 #define TOOL_BUTTON_PLAYER2_YPOS        (TOOL_BUTTON_PLAYER_YPOS \
2308                                          + 0 * TOOL_BUTTON_PLAYER_YSIZE)
2309 #define TOOL_BUTTON_PLAYER3_YPOS        (TOOL_BUTTON_PLAYER_YPOS \
2310                                          + 1 * TOOL_BUTTON_PLAYER_YSIZE)
2311 #define TOOL_BUTTON_PLAYER4_YPOS        (TOOL_BUTTON_PLAYER_YPOS \
2312                                          + 1 * TOOL_BUTTON_PLAYER_YSIZE)
2313
2314 static struct
2315 {
2316   int xpos, ypos;
2317   int x, y;
2318   int width, height;
2319   int gadget_id;
2320   char *infotext;
2321 } toolbutton_info[NUM_TOOL_BUTTONS] =
2322 {
2323   {
2324     TOOL_BUTTON_YES_XPOS,       TOOL_BUTTON_YES_GFX_YPOS,
2325     TOOL_BUTTON_YES_XPOS,       TOOL_BUTTON_YES_YPOS,
2326     TOOL_BUTTON_YES_XSIZE,      TOOL_BUTTON_YES_YSIZE,
2327     TOOL_CTRL_ID_YES,
2328     "yes"
2329   },
2330   {
2331     TOOL_BUTTON_NO_XPOS,        TOOL_BUTTON_NO_GFX_YPOS,
2332     TOOL_BUTTON_NO_XPOS,        TOOL_BUTTON_NO_YPOS,
2333     TOOL_BUTTON_NO_XSIZE,       TOOL_BUTTON_NO_YSIZE,
2334     TOOL_CTRL_ID_NO,
2335     "no"
2336   },
2337   {
2338     TOOL_BUTTON_CONFIRM_XPOS,   TOOL_BUTTON_CONFIRM_GFX_YPOS,
2339     TOOL_BUTTON_CONFIRM_XPOS,   TOOL_BUTTON_CONFIRM_YPOS,
2340     TOOL_BUTTON_CONFIRM_XSIZE,  TOOL_BUTTON_CONFIRM_YSIZE,
2341     TOOL_CTRL_ID_CONFIRM,
2342     "confirm"
2343   },
2344   {
2345     TOOL_BUTTON_PLAYER_GFX_XPOS,TOOL_BUTTON_PLAYER_GFX_YPOS,
2346     TOOL_BUTTON_PLAYER1_XPOS,   TOOL_BUTTON_PLAYER1_YPOS,
2347     TOOL_BUTTON_PLAYER_XSIZE,   TOOL_BUTTON_PLAYER_YSIZE,
2348     TOOL_CTRL_ID_PLAYER_1,
2349     "player 1"
2350   },
2351   {
2352     TOOL_BUTTON_PLAYER_GFX_XPOS,TOOL_BUTTON_PLAYER_GFX_YPOS,
2353     TOOL_BUTTON_PLAYER2_XPOS,   TOOL_BUTTON_PLAYER2_YPOS,
2354     TOOL_BUTTON_PLAYER_XSIZE,   TOOL_BUTTON_PLAYER_YSIZE,
2355     TOOL_CTRL_ID_PLAYER_2,
2356     "player 2"
2357   },
2358   {
2359     TOOL_BUTTON_PLAYER_GFX_XPOS,TOOL_BUTTON_PLAYER_GFX_YPOS,
2360     TOOL_BUTTON_PLAYER3_XPOS,   TOOL_BUTTON_PLAYER3_YPOS,
2361     TOOL_BUTTON_PLAYER_XSIZE,   TOOL_BUTTON_PLAYER_YSIZE,
2362     TOOL_CTRL_ID_PLAYER_3,
2363     "player 3"
2364   },
2365   {
2366     TOOL_BUTTON_PLAYER_GFX_XPOS,TOOL_BUTTON_PLAYER_GFX_YPOS,
2367     TOOL_BUTTON_PLAYER4_XPOS,   TOOL_BUTTON_PLAYER4_YPOS,
2368     TOOL_BUTTON_PLAYER_XSIZE,   TOOL_BUTTON_PLAYER_YSIZE,
2369     TOOL_CTRL_ID_PLAYER_4,
2370     "player 4"
2371   }
2372 };
2373
2374 void CreateToolButtons()
2375 {
2376   int i;
2377
2378   for (i=0; i<NUM_TOOL_BUTTONS; i++)
2379   {
2380     Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
2381     Bitmap *deco_bitmap = None;
2382     int deco_x = 0, deco_y = 0, deco_xpos = 0, deco_ypos = 0;
2383     struct GadgetInfo *gi;
2384     unsigned long event_mask;
2385     int gd_xoffset, gd_yoffset;
2386     int gd_x1, gd_x2, gd_y;
2387     int id = i;
2388
2389     event_mask = GD_EVENT_RELEASED;
2390
2391     gd_xoffset = toolbutton_info[i].xpos;
2392     gd_yoffset = toolbutton_info[i].ypos;
2393     gd_x1 = DOOR_GFX_PAGEX4 + gd_xoffset;
2394     gd_x2 = DOOR_GFX_PAGEX3 + gd_xoffset;
2395     gd_y = DOOR_GFX_PAGEY1 + gd_yoffset;
2396
2397     if (id >= TOOL_CTRL_ID_PLAYER_1 && id <= TOOL_CTRL_ID_PLAYER_4)
2398     {
2399       int player_nr = id - TOOL_CTRL_ID_PLAYER_1;
2400
2401       getMiniGraphicSource(PLAYER_NR_GFX(IMG_PLAYER_1, player_nr),
2402                            &deco_bitmap, &deco_x, &deco_y);
2403       deco_xpos = (toolbutton_info[i].width - MINI_TILEX) / 2;
2404       deco_ypos = (toolbutton_info[i].height - MINI_TILEY) / 2;
2405     }
2406
2407     gi = CreateGadget(GDI_CUSTOM_ID, id,
2408                       GDI_INFO_TEXT, toolbutton_info[i].infotext,
2409                       GDI_X, DX + toolbutton_info[i].x,
2410                       GDI_Y, DY + toolbutton_info[i].y,
2411                       GDI_WIDTH, toolbutton_info[i].width,
2412                       GDI_HEIGHT, toolbutton_info[i].height,
2413                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
2414                       GDI_STATE, GD_BUTTON_UNPRESSED,
2415                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
2416                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
2417                       GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y,
2418                       GDI_DECORATION_POSITION, deco_xpos, deco_ypos,
2419                       GDI_DECORATION_SIZE, MINI_TILEX, MINI_TILEY,
2420                       GDI_DECORATION_SHIFTING, 1, 1,
2421                       GDI_EVENT_MASK, event_mask,
2422                       GDI_CALLBACK_ACTION, HandleToolButtons,
2423                       GDI_END);
2424
2425     if (gi == NULL)
2426       Error(ERR_EXIT, "cannot create gadget");
2427
2428     tool_gadget[id] = gi;
2429   }
2430 }
2431
2432 void FreeToolButtons()
2433 {
2434   int i;
2435
2436   for (i=0; i<NUM_TOOL_BUTTONS; i++)
2437     FreeGadget(tool_gadget[i]);
2438 }
2439
2440 static void UnmapToolButtons()
2441 {
2442   int i;
2443
2444   for (i=0; i<NUM_TOOL_BUTTONS; i++)
2445     UnmapGadget(tool_gadget[i]);
2446 }
2447
2448 static void HandleToolButtons(struct GadgetInfo *gi)
2449 {
2450   request_gadget_id = gi->custom_id;
2451 }
2452
2453 int get_next_element(int element)
2454 {
2455   switch(element)
2456   {
2457     case EL_QUICKSAND_FILLING:          return EL_QUICKSAND_FULL;
2458     case EL_QUICKSAND_EMPTYING:         return EL_QUICKSAND_EMPTY;
2459     case EL_MAGIC_WALL_FILLING:         return EL_MAGIC_WALL_FULL;
2460     case EL_MAGIC_WALL_EMPTYING:        return EL_MAGIC_WALL_ACTIVE;
2461     case EL_BD_MAGIC_WALL_FILLING:      return EL_BD_MAGIC_WALL_FULL;
2462     case EL_BD_MAGIC_WALL_EMPTYING:     return EL_BD_MAGIC_WALL_ACTIVE;
2463     case EL_AMOEBA_DROPPING:            return EL_AMOEBA_WET;
2464
2465     default:                            return element;
2466   }
2467 }
2468
2469 int el_act_dir2img(int element, int action, int direction)
2470 {
2471   element = GFX_ELEMENT(element);
2472   direction = MV_DIR_BIT(direction);
2473
2474   return element_info[element].direction_graphic[action][direction];
2475 }
2476
2477 static int el_act_dir2crm(int element, int action, int direction)
2478 {
2479   element = GFX_ELEMENT(element);
2480   direction = MV_DIR_BIT(direction);
2481
2482   return element_info[element].direction_crumbled[action][direction];
2483 }
2484
2485 int el_act2img(int element, int action)
2486 {
2487   element = GFX_ELEMENT(element);
2488
2489   return element_info[element].graphic[action];
2490 }
2491
2492 int el_dir2img(int element, int direction)
2493 {
2494   element = GFX_ELEMENT(element);
2495
2496   return el_act_dir2img(element, ACTION_DEFAULT, direction);
2497 }
2498
2499 int el2img(int element)
2500 {
2501   element = GFX_ELEMENT(element);
2502
2503   return element_info[element].graphic[ACTION_DEFAULT];
2504 }
2505
2506 int el2edimg(int element)
2507 {
2508   element = GFX_ELEMENT(element);
2509
2510   return element_info[element].special_graphic[GFX_SPECIAL_ARG_EDITOR];
2511 }
2512
2513 int el2preimg(int element)
2514 {
2515   element = GFX_ELEMENT(element);
2516
2517   return element_info[element].special_graphic[GFX_SPECIAL_ARG_PREVIEW];
2518 }