added optional button to restart game (door, panel and touch variants)
[rocksndiamonds.git] / src / game_mm / mm_tools.c
1 // ============================================================================
2 // Mirror Magic -- McDuffin's Revenge
3 // ----------------------------------------------------------------------------
4 // (c) 1994-2017 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // mm_tools.c
10 // ============================================================================
11
12 #include <time.h>
13
14 #include "main_mm.h"
15
16 #include "mm_main.h"
17 #include "mm_tools.h"
18
19
20 void SetDrawtoField_MM(int mode)
21 {
22   int full_xsize = lev_fieldx * TILESIZE_VAR;
23   int full_ysize = lev_fieldy * TILESIZE_VAR;
24
25   // distance (delta) from screen border (SX/SY) to centered level playfield
26   dSX = (full_xsize < SXSIZE ? (SXSIZE - full_xsize) / 2 : 0);
27   dSY = (full_ysize < SYSIZE ? (SYSIZE - full_ysize) / 2 : 0);
28
29   // for convenience, absolute screen position to centered level playfield
30   cSX = SX + dSX;
31   cSY = SY + dSY;
32   cSX2 = SX + dSX + 2;          // including half laser line size
33   cSY2 = SY + dSY + 2;          // including half laser line size
34
35   if (mode == DRAW_TO_BACKBUFFER)
36   {
37     cFX = FX + dSX;
38     cFY = FY + dSY;
39   }
40
41   SetTileCursorSXSY(cSX, cSY);
42 }
43
44 void BackToFront_MM(void)
45 {
46   BlitScreenToBitmap_MM(backbuffer);
47
48   BackToFront();
49 }
50
51 void ClearWindow(void)
52 {
53   ClearRectangle(drawto_mm, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
54
55   SetDrawtoField(DRAW_TO_BACKBUFFER);
56   SetDrawtoField_MM(DRAW_TO_BACKBUFFER);
57
58   redraw_mask |= REDRAW_FIELD;
59 }
60
61 void DrawGraphicAnimation_MM(int x, int y, int graphic, int frame)
62 {
63   Bitmap *bitmap;
64   int src_x, src_y;
65
66   getGraphicSource(graphic, frame, &bitmap, &src_x, &src_y);
67
68   BlitBitmap(bitmap, drawto_mm, src_x, src_y, TILEX, TILEY,
69              cFX + x * TILEX, cFY + y * TILEY);
70 }
71
72 void DrawGraphic_MM(int x, int y, int graphic)
73 {
74 #if DEBUG
75   if (!IN_SCR_FIELD(x, y))
76   {
77     Debug("game:mm:DrawGraphic_MM", "x = %d, y = %d, graphic = %d",
78           x, y, graphic);
79     Debug("game:mm:DrawGraphic_MM", "This should never happen!");
80
81     return;
82   }
83 #endif
84
85   int frame = getGraphicAnimationFrameXY(graphic, x, y);
86
87   DrawGraphicAnimation_MM(x, y, graphic, frame);
88
89   MarkTileDirty(x, y);
90 }
91
92 void DrawGraphicExt_MM(DrawBuffer *d, int x, int y, int graphic)
93 {
94   Bitmap *bitmap;
95   int src_x, src_y;
96
97   getGraphicSource(graphic, 0, &bitmap, &src_x, &src_y);
98
99   BlitBitmap(bitmap, d, src_x, src_y, TILEX, TILEY, x, y);
100 }
101
102 void DrawGraphicThruMask_MM(int x, int y, int graphic, int frame)
103 {
104 #if DEBUG
105   if (!IN_SCR_FIELD(x, y))
106   {
107     Debug("game:mm:DrawGraphicThruMask_MM", "x = %d, y = %d, graphic = %d",
108           x, y, graphic);
109     Debug("game:mm:DrawGraphicThruMask_MM", "This should never happen!");
110
111     return;
112   }
113 #endif
114
115   DrawGraphicThruMaskExt_MM(drawto_mm, cFX + x * TILEX, cFY + y * TILEY,
116                             graphic, frame);
117
118   MarkTileDirty(x, y);
119 }
120
121 void DrawGraphicThruMaskExt_MM(DrawBuffer *d, int dest_x, int dest_y,
122                                int graphic, int frame)
123 {
124   int src_x, src_y;
125   Bitmap *src_bitmap;
126
127   if (graphic == IMG_EMPTY)
128     return;
129
130   getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
131
132   BlitBitmapMasked(src_bitmap, d, src_x, src_y, TILEX, TILEY, dest_x, dest_y);
133 }
134
135 void DrawMiniGraphic_MM(int x, int y, int graphic)
136 {
137   DrawMiniGraphicExt_MM(drawto_mm, cSX + x * MINI_TILEX, cSY + y * MINI_TILEY,
138                         graphic);
139
140   MarkTileDirty(x / 2, y / 2);
141 }
142
143 void DrawMiniGraphicExt_MM(DrawBuffer *d, int x, int y, int graphic)
144 {
145   Bitmap *bitmap;
146   int src_x, src_y;
147
148   getMiniGraphicSource(graphic, &bitmap, &src_x, &src_y);
149
150   BlitBitmap(bitmap, d, src_x, src_y, MINI_TILEX, MINI_TILEY, x, y);
151 }
152
153 void DrawGraphicShifted_MM(int x, int y, int dx, int dy, int graphic,
154                            int cut_mode, int mask_mode)
155 {
156   int width = TILEX, height = TILEY;
157   int cx = 0, cy = 0;
158   int src_x, src_y, dest_x, dest_y;
159   Bitmap *src_bitmap;
160
161   if (graphic < 0)
162   {
163     DrawGraphic_MM(x, y, graphic);
164
165     return;
166   }
167
168   if (dx || dy)                 // Verschiebung der Grafik?
169   {
170     if (x < BX1)                // Element kommt von links ins Bild
171     {
172       x = BX1;
173       width = dx;
174       cx = TILEX - dx;
175       dx = 0;
176     }
177     else if (x > BX2)           // Element kommt von rechts ins Bild
178     {
179       x = BX2;
180       width = -dx;
181       dx = TILEX + dx;
182     }
183     else if (x==BX1 && dx < 0)  // Element verläßt links das Bild
184     {
185       width += dx;
186       cx = -dx;
187       dx = 0;
188     }
189     else if (x==BX2 && dx > 0)  // Element verläßt rechts das Bild
190       width -= dx;
191     else if (dx)                // allg. Bewegung in x-Richtung
192       MarkTileDirty(x + SIGN(dx), y);
193
194     if (y < BY1)                // Element kommt von oben ins Bild
195     {
196       if (cut_mode==CUT_BELOW)  // Element oberhalb des Bildes
197         return;
198
199       y = BY1;
200       height = dy;
201       cy = TILEY - dy;
202       dy = 0;
203     }
204     else if (y > BY2)           // Element kommt von unten ins Bild
205     {
206       y = BY2;
207       height = -dy;
208       dy = TILEY + dy;
209     }
210     else if (y==BY1 && dy < 0)  // Element verläßt oben das Bild
211     {
212       height += dy;
213       cy = -dy;
214       dy = 0;
215     }
216     else if (dy > 0 && cut_mode == CUT_ABOVE)
217     {
218       if (y == BY2)             // Element unterhalb des Bildes
219         return;
220
221       height = dy;
222       cy = TILEY - dy;
223       dy = TILEY;
224       MarkTileDirty(x, y + 1);
225     }                           // Element verläßt unten das Bild
226     else if (dy > 0 && (y == BY2 || cut_mode == CUT_BELOW))
227     {
228       height -= dy;
229     }
230     else if (dy)                // allg. Bewegung in y-Richtung
231     {
232       MarkTileDirty(x, y + SIGN(dy));
233     }
234   }
235
236   getGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
237
238   src_x += cx;
239   src_y += cy;
240
241   dest_x = cFX + x * TILEX + dx;
242   dest_y = cFY + y * TILEY + dy;
243
244 #if DEBUG
245   if (!IN_SCR_FIELD(x, y))
246   {
247     Debug("game:mm:DrawGraphicShifted_MM", "x = %d, y = %d, graphic = %d",
248           x, y, graphic);
249     Debug("game:mm:DrawGraphicShifted_MM", "This should never happen!");
250
251     return;
252   }
253 #endif
254
255   if (mask_mode == USE_MASKING)
256     BlitBitmapMasked(src_bitmap, drawto_mm,
257                      src_x, src_y, TILEX, TILEY, dest_x, dest_y);
258   else
259     BlitBitmap(src_bitmap, drawto_mm,
260                src_x, src_y, width, height, dest_x, dest_y);
261
262   MarkTileDirty(x, y);
263 }
264
265 void DrawScreenElementExt_MM(int x, int y, int dx, int dy, int element,
266                           int cut_mode, int mask_mode)
267 {
268   int ux = LEVELX(x), uy = LEVELY(y);
269   int graphic = el2gfx(element);
270   int phase8 = ABS(MovPos[ux][uy]) / (TILEX / 8);
271   int phase2  = phase8 / 4;
272   int dir = MovDir[ux][uy];
273
274   if (element == EL_PACMAN)
275   {
276     graphic = (phase2 ? IMG_MM_PACMAN_RIGHT : IMG_MM_PACMAN_EATING_RIGHT);
277
278     if (dir == MV_UP)
279       graphic += 1;
280     else if (dir == MV_LEFT)
281       graphic += 2;
282     else if (dir == MV_DOWN)
283       graphic += 3;
284   }
285
286   if (dx || dy)
287     DrawGraphicShifted_MM(x, y, dx, dy, graphic, cut_mode, mask_mode);
288   else if (mask_mode == USE_MASKING)
289     DrawGraphicThruMask_MM(x, y, graphic, 0);
290   else
291     DrawGraphic_MM(x, y, graphic);
292 }
293
294 void DrawLevelElementExt_MM(int x, int y, int dx, int dy, int element,
295                          int cut_mode, int mask_mode)
296 {
297   if (IN_LEV_FIELD(x, y) && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
298     DrawScreenElementExt_MM(SCREENX(x), SCREENY(y), dx, dy, element,
299                          cut_mode, mask_mode);
300 }
301
302 void DrawScreenElementShifted_MM(int x, int y, int dx, int dy, int element,
303                               int cut_mode)
304 {
305   DrawScreenElementExt_MM(x, y, dx, dy, element, cut_mode, NO_MASKING);
306 }
307
308 void DrawScreenElement_MM(int x, int y, int element)
309 {
310   DrawScreenElementExt_MM(x, y, 0, 0, element, NO_CUTTING, NO_MASKING);
311 }
312
313 void DrawScreenField_MM(int x, int y)
314 {
315   int element = Tile[x][y];
316
317   if (!IN_LEV_FIELD(x, y))
318     return;
319
320   if (IS_MOVING(x, y))
321   {
322     int horiz_move = (MovDir[x][y] == MV_LEFT || MovDir[x][y] == MV_RIGHT);
323
324     DrawScreenElement_MM(x, y, EL_EMPTY);
325
326     if (horiz_move)
327       DrawScreenElementShifted_MM(x, y, MovPos[x][y], 0, element, NO_CUTTING);
328     else
329       DrawScreenElementShifted_MM(x, y, 0, MovPos[x][y], element, NO_CUTTING);
330   }
331   else if (IS_BLOCKED(x, y))
332   {
333     int oldx, oldy;
334     int sx, sy;
335     int horiz_move;
336
337     Blocked2Moving(x, y, &oldx, &oldy);
338
339     sx = SCREENX(oldx);
340     sy = SCREENY(oldy);
341     horiz_move = (MovDir[oldx][oldy] == MV_LEFT ||
342                   MovDir[oldx][oldy] == MV_RIGHT);
343
344     DrawScreenElement_MM(x, y, EL_EMPTY);
345
346     element = Tile[oldx][oldy];
347
348     if (horiz_move)
349       DrawScreenElementShifted_MM(sx, sy, MovPos[oldx][oldy], 0, element,
350                                   NO_CUTTING);
351     else
352       DrawScreenElementShifted_MM(sx, sy, 0, MovPos[oldx][oldy], element,
353                                   NO_CUTTING);
354   }
355   else if (IS_DRAWABLE(element))
356   {
357     DrawScreenElement_MM(x, y, element);
358   }
359   else
360   {
361     DrawScreenElement_MM(x, y, EL_EMPTY);
362   }
363 }
364
365 void DrawLevelField_MM(int x, int y)
366 {
367   if (IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
368     DrawScreenField_MM(SCREENX(x), SCREENY(y));
369   else if (IS_MOVING(x, y))
370   {
371     int newx, newy;
372
373     Moving2Blocked(x, y, &newx, &newy);
374     if (IN_SCR_FIELD(SCREENX(newx), SCREENY(newy)))
375       DrawScreenField_MM(SCREENX(newx), SCREENY(newy));
376   }
377   else if (IS_BLOCKED(x, y))
378   {
379     int oldx, oldy;
380
381     Blocked2Moving(x, y, &oldx, &oldy);
382     if (IN_SCR_FIELD(SCREENX(oldx), SCREENY(oldy)))
383       DrawScreenField_MM(SCREENX(oldx), SCREENY(oldy));
384   }
385 }
386
387 void DrawMiniElement_MM(int x, int y, int element)
388 {
389   int graphic;
390
391   if (!element)
392   {
393     DrawMiniGraphic_MM(x, y, IMG_EMPTY);
394
395     return;
396   }
397
398   graphic = el2gfx(element);
399
400   DrawMiniGraphic_MM(x, y, graphic);
401 }
402
403 void DrawMiniElementOrWall_MM(int sx, int sy, int scroll_x, int scroll_y)
404 {
405   int x = sx + scroll_x, y = sy + scroll_y;
406
407   if (x < -1 || x > lev_fieldx || y < -1 || y > lev_fieldy)
408     DrawMiniElement_MM(sx, sy, EL_EMPTY);
409   else if (x > -1 && x < lev_fieldx && y > -1 && y < lev_fieldy)
410     DrawMiniElement_MM(sx, sy, Tile[x][y]);
411 }
412
413 void DrawField_MM(int x, int y)
414 {
415   int element = Tile[x][y];
416
417   DrawElement_MM(x, y, element);
418 }
419
420 void DrawLevel_MM(void)
421 {
422   int x, y;
423
424   ClearWindow();
425
426   for (x = 0; x < lev_fieldx; x++)
427     for (y = 0; y < lev_fieldy; y++)
428       DrawField_MM(x, y);
429
430   redraw_mask |= REDRAW_FIELD;
431 }
432
433 void DrawWallsExt_MM(int x, int y, int element, int draw_mask)
434 {
435   Bitmap *bitmap;
436   int graphic = el2gfx(WALL_BASE(element));
437   int gx, gy;
438   int i;
439
440   getMiniGraphicSource(graphic, &bitmap, &gx, &gy);
441
442   DrawGraphic_MM(x, y, IMG_EMPTY);
443
444   /*
445   if (IS_WALL_WOOD(element) || IS_WALL_AMOEBA(element) ||
446       IS_DF_WALL_WOOD(element))
447     gx += MINI_TILEX;
448   if (IS_WALL_ICE(element) || IS_WALL_AMOEBA(element))
449     gy += MINI_TILEY;
450   */
451
452   for (i = 0; i < 4; i++)
453   {
454     int dest_x = cSX + x * TILEX + MINI_TILEX * (i % 2);
455     int dest_y = cSY + y * TILEY + MINI_TILEY * (i / 2);
456
457     if (!((1 << i) & draw_mask))
458       continue;
459
460     if (element & (1 << i))
461       BlitBitmap(bitmap, drawto_mm, gx, gy, MINI_TILEX, MINI_TILEY,
462                  dest_x, dest_y);
463     else
464       ClearRectangle(drawto_mm, dest_x, dest_y, MINI_TILEX, MINI_TILEY);
465   }
466
467   MarkTileDirty(x, y);
468 }
469
470 void DrawWalls_MM(int x, int y, int element)
471 {
472   DrawWallsExt_MM(x, y, element, HIT_MASK_ALL);
473 }
474
475 void DrawWallsAnimation_MM(int x, int y, int element, int phase, int bit_mask)
476 {
477   int i;
478
479   if (phase == 0)
480   {
481     DrawWalls_MM(x, y, element);
482
483     return;
484   }
485
486   for (i = 0; i < 4; i++)
487   {
488     if (element & (1 << i))
489     {
490       int graphic;
491       int frame;
492       Bitmap *bitmap;
493       int src_x, src_y;
494       int dst_x = cSX + x * TILEX + (i % 2) * MINI_TILEX;
495       int dst_y = cSY + y * TILEY + (i / 2) * MINI_TILEY;
496
497       if (bit_mask & (1 << i))
498       {
499         graphic = (IS_WALL_AMOEBA(element) ?
500                    IMG_MM_AMOEBA_WALL_GROWING :
501                    IMG_MM_ICE_WALL_SHRINKING);
502         frame = phase;
503       }
504       else
505       {
506         graphic = (IS_WALL_AMOEBA(element) ?
507                    IMG_MM_AMOEBA_WALL :
508                    IMG_MM_ICE_WALL);
509         frame = 0;
510       }
511
512       getSizedGraphicSource(graphic, frame, MINI_TILESIZE, &bitmap,
513                             &src_x, &src_y);
514
515       BlitBitmap(bitmap, drawto_mm, src_x, src_y, MINI_TILEX, MINI_TILEY,
516                  dst_x, dst_y);
517     }
518   }
519
520   MarkTileDirty(x, y);
521 }
522
523 void DrawElement_MM(int x, int y, int element)
524 {
525   if (element == EL_EMPTY)
526     DrawGraphic_MM(x, y, IMG_EMPTY);
527   else if (IS_WALL(element))
528     DrawWalls_MM(x, y, element);
529 #if 0
530   else if (IS_WALL_CHANGING(element) && IS_WALL_CHANGING(Tile[x][y]))
531   {
532     int wall_element = Tile[x][y] - EL_WALL_CHANGING + Store[x][y];
533
534     DrawWalls_MM(x, y, wall_element);
535   }
536 #endif
537   else if (element == EL_PACMAN)
538     DrawLevelField_MM(x, y);
539   else if (element == EL_FUSE_ON &&
540            laser.fuse_off &&
541            laser.fuse_x == x &&
542            laser.fuse_y == y)
543     DrawGraphic_MM(x, y, IMG_MM_FUSE);
544   else if (element == EL_GRAY_BALL_ACTIVE)
545     DrawGraphic_MM(x, y, el_act2gfx(EL_GRAY_BALL, MM_ACTION_ACTIVE));
546   else if (element == EL_GRAY_BALL_OPENING)
547     DrawGraphic_MM(x, y, el_act2gfx(EL_GRAY_BALL, MM_ACTION_OPENING));
548   else if (element == EL_BOMB_ACTIVE)
549     DrawGraphic_MM(x, y, el_act2gfx(EL_BOMB, MM_ACTION_ACTIVE));
550   else if (element == EL_MINE_ACTIVE)
551     DrawGraphic_MM(x, y, el_act2gfx(EL_MINE, MM_ACTION_ACTIVE));
552   else
553     DrawGraphic_MM(x, y, el2gfx(element));
554 }
555
556
557 // ----------------------------------------------------------------------------
558 // XSN
559 // ----------------------------------------------------------------------------
560
561 #define XSN_RND(x)              ((x) != 0 ? rand() % (x) : 0)
562 #define XSN_ALPHA_VALUE(x)      (SDL_ALPHA_OPAQUE * (x) / 100)
563
564 #define XSN_MAX_ITEMS           100
565 #define XSN_MAX_HEIGHT          40
566 #define XSN_MAX_DX              2
567 #define XSN_MAX_DY              10
568 #define XSN_CHECK_DELAY         3
569 #define XSN_START_DELAY         60
570 #define XSN_UPDATE_DELAY        50
571 #define XSN_GROWTH_DELAY        3
572 #define XSN_GROWTH_RATE         3
573 #define XSN_CHANGE_DELAY        30
574 #define XSN_CHANGE_FACTOR       3
575 #define XSN_ALPHA_DEFAULT       XSN_ALPHA_VALUE(95)
576 #define XSN_ALPHA_VISIBLE       XSN_ALPHA_VALUE(50)
577 #define XSN_DEBUG_STEPS         5
578
579 static byte xsn_bits_0[] = { 0x05, 0x02, 0x05 };
580 static byte xsn_bits_1[] = { 0x22, 0x6b, 0x14, 0x2a, 0x14, 0x6b, 0x22 };
581 static byte xsn_bits_2[] = { 0x14, 0x08, 0x49, 0x36, 0x49, 0x08, 0x14 };
582
583 char debug_xsn_mode[] = { 76,101,116,32,105,116,32,115,110,111,119,33,0 };
584
585 void setHideSetupEntry(void *);
586 void removeHideSetupEntry(void *);
587
588 static struct
589 {
590   int size;
591   byte *bits;
592   Bitmap *bitmap;
593 }
594 xsn_data[] =
595 {
596   { ARRAY_SIZE(xsn_bits_0), xsn_bits_0 },
597   { ARRAY_SIZE(xsn_bits_1), xsn_bits_1 },
598   { ARRAY_SIZE(xsn_bits_2), xsn_bits_2 },
599   { ARRAY_SIZE(xsn_bits_2), xsn_bits_2 },
600   { ARRAY_SIZE(xsn_bits_1), xsn_bits_1 },
601   { ARRAY_SIZE(xsn_bits_2), xsn_bits_2 },
602   { ARRAY_SIZE(xsn_bits_0), xsn_bits_0 },
603 };
604 static int num_xsn_data = ARRAY_SIZE(xsn_data);
605
606 struct XsnItem
607 {
608   int x;
609   int y;
610   int dx;
611   int dy;
612   int type;
613   int active;
614 };
615
616 struct Xsn
617 {
618   int area_xsize;
619   int area_ysize;
620
621   int num_items;
622   int max_items;
623   int max_height;
624   int max_dx;
625   int max_dy;
626
627   int change_delay;
628   int change_type;
629   int change_dir;
630
631   int *height;
632
633   struct XsnItem items[XSN_MAX_ITEMS];
634
635   Bitmap *bitmap;
636
637   int alpha;
638 };
639
640 static struct Xsn xsn = { 0 };
641
642 static int xsn_percent(void)
643 {
644   int xsn_m0 = -3;
645   int xsn_m1 = xsn_m0 + 10;
646   int xsn_m2 = xsn_m1 + 10;
647   int xsn_m3 = xsn_m2 + 10;
648   time_t xsn_e0 = time(NULL);
649   struct tm *xsn_t0 = localtime(&xsn_e0);
650   struct tm xsn_t1 = { 0,0,0, xsn_m2 * 3, xsn_m3 / 3, xsn_t0->tm_year, 0,0,-1 };
651   time_t xsn_e1 = mktime(&xsn_t1);
652   int xsn_c0 = (25 * xsn_m3) << xsn_m1;
653   int xsn_c1 = (xsn_t1.tm_wday - xsn_m1) * !!xsn_t1.tm_wday;
654
655   for (xsn_m0 = 5; xsn_m0 > 0; xsn_m0--)
656   {
657     int xsn_c2 = (xsn_m0 > 4 ? 0 : xsn_c1) - xsn_m1 * xsn_m0;
658     int xsn_off = (xsn_m0 > 4 ? xsn_c0 : 0);
659     time_t xsn_e3 = xsn_e1 - xsn_c2 * xsn_c0;
660
661     if (xsn_e0 > xsn_e3 - xsn_off &&
662         xsn_e0 < xsn_e3 + xsn_off + xsn_c0)
663       return xsn_m0 * (xsn_m3 - xsn_m1);
664   }
665
666   return xsn_m0;
667 }
668
669 static void xsn_init_item(int nr)
670 {
671   struct XsnItem *item = &xsn.items[nr];
672
673   item->type = XSN_RND(num_xsn_data);
674
675   if (xsn.change_type != 0)
676   {
677     int new_x = XSN_RND(xsn.area_xsize / 3);
678
679     item->x = (xsn.change_dir == 1 ? new_x : xsn.area_xsize - new_x);
680     item->y = XSN_RND(xsn.area_ysize);
681   }
682   else
683   {
684     item->x = XSN_RND(xsn.area_xsize - xsn_data[item->type].size);
685     item->y = XSN_RND(xsn.area_ysize / 10);
686   }
687
688   item->dy = XSN_RND(xsn.max_dy + 1) + 1;
689   item->dx = XSN_RND(item->dy / 4 + 1) * (XSN_RND(1000) > 500 ? -1 : 1);
690
691   item->active = 1;
692 }
693
694 static void xsn_update_item(int nr)
695 {
696   struct XsnItem *item = &xsn.items[nr];
697
698   if (!item->active)
699     xsn_init_item(nr);
700
701   if (xsn.change_type != 0)
702   {
703     int dx_new = ABS(item->dx) +
704       (xsn.change_type == 1 ?
705        XSN_RND(XSN_CHANGE_FACTOR + 1) - XSN_CHANGE_FACTOR / 2 :
706        XSN_RND(20));
707
708     item->dx = MIN(MAX(-50, dx_new * xsn.change_dir), 50);
709   }
710
711   int new_x = item->x + item->dx;
712   int new_y = item->y + item->dy;
713
714   item->active = (new_y < xsn.area_ysize);
715
716   if (xsn.change_type != 0)
717     item->active = (item->active && new_x > 0 && new_x < xsn.area_xsize);
718
719   int item_size = xsn_data[item->type].size;
720   int half_item_size = item_size / 2;
721   int mid_x = new_x + half_item_size;
722   int mid_y = new_y + half_item_size;
723   int upper_border = xsn.area_ysize - xsn.max_height;
724
725   if (item->active &&
726       new_y >= upper_border &&
727       new_x >= 0 &&
728       new_x <= xsn.area_xsize - item_size &&
729       mid_y >= xsn.height[mid_x] &&
730       mid_y < xsn.area_ysize)
731   {
732     Bitmap *item_bitmap = xsn_data[item->type].bitmap;
733     SDL_Surface *surface = xsn.bitmap->surface;
734     SDL_Surface *surface_masked = xsn.bitmap->surface_masked;
735     int item_alpha = XSN_ALPHA_VALUE(81 + XSN_RND(20));
736     int shrink = 1;
737     int i;
738
739     xsn.bitmap->surface = surface_masked;
740
741     SDLSetAlpha(item_bitmap->surface_masked, TRUE, item_alpha);
742
743     // blit to masked surface instead of opaque surface
744     BlitBitmapMasked(item_bitmap, xsn.bitmap, 0, 0, item_size, item_size,
745                      new_x, new_y - upper_border);
746
747     SDLSetAlpha(item_bitmap->surface_masked, TRUE, XSN_ALPHA_DEFAULT);
748
749     for (i = -half_item_size; i <= half_item_size; i++)
750     {
751       int xpos = mid_x + i;
752
753       if (xpos >= 0 && xpos < xsn.area_xsize)
754         xsn.height[xpos] = MIN(new_y + ABS(i), xsn.height[xpos]);
755     }
756
757     if (xsn.height[mid_x] <= upper_border + shrink)
758     {
759       int xpos1 = MAX(0, new_x - half_item_size);
760       int xpos2 = MIN(new_x + 3 * half_item_size, xsn.area_xsize);
761       int xsize = xpos2 - xpos1;
762       int ysize1 = XSN_RND(xsn.max_height - shrink);
763       int ysize2 = xsn.max_height - ysize1;
764
765       SDLSetAlpha(surface_masked, FALSE, 0);
766
767       FillRectangle(xsn.bitmap, xpos1, xsn.max_height, xsize, xsn.max_height,
768                     BLACK_PIXEL);
769       BlitBitmapMasked(xsn.bitmap, xsn.bitmap, xpos1, 0, xsize, ysize1,
770                        xpos1, xsn.max_height + shrink);
771       BlitBitmapMasked(xsn.bitmap, xsn.bitmap, xpos1, ysize1, xsize, ysize2,
772                        xpos1, xsn.max_height + ysize1);
773       FillRectangle(xsn.bitmap, xpos1, 0, xsize, xsn.max_height,
774                     BLACK_PIXEL);
775       BlitBitmapMasked(xsn.bitmap, xsn.bitmap, xpos1, xsn.max_height,
776                        xsize, xsn.max_height, xpos1, 0);
777
778       SDLSetAlpha(surface_masked, TRUE, xsn.alpha);
779
780       for (i = xpos1; i < xpos2; i++)
781         xsn.height[i] = MIN(xsn.height[i] + shrink, xsn.area_ysize - 1);
782     }
783
784     SDLFreeBitmapTextures(xsn.bitmap);
785     SDLCreateBitmapTextures(xsn.bitmap);
786
787     xsn.bitmap->surface = surface;
788
789     item->active = 0;
790   }
791
792   item->dx += XSN_RND(XSN_CHANGE_FACTOR) * (XSN_RND(1000) > 500 ? -1 : 1);
793
794   if (xsn.change_type == 0)
795     item->dx = MIN(MAX(-xsn.max_dx, item->dx), xsn.max_dx);
796
797   item->x = new_x;
798   item->y = new_y;
799 }
800
801 static void xsn_update_change(void)
802 {
803   if (XSN_RND(100) > 65)
804   {
805     xsn.change_dir = (XSN_RND(10) > 4 ? 1 : -1);
806     xsn.change_delay = XSN_RND(5) + 1;
807     xsn.change_type = 2;
808   }
809   else if (xsn.change_type == 2)
810   {
811     xsn.change_delay = XSN_RND(3) + 1;
812     xsn.change_type = 1;
813   }
814   else
815   {
816     xsn.change_delay = XSN_CHANGE_DELAY;
817     xsn.change_type = 0;
818   }
819 }
820
821 static void DrawTileCursor_Xsn(int draw_target)
822 {
823   static boolean initialized = FALSE;
824   static boolean started = FALSE;
825   static boolean active = FALSE;
826   static boolean debug = FALSE;
827   static DelayCounter check_delay = { XSN_CHECK_DELAY * 1000 };
828   static DelayCounter start_delay = { 0 };
829   static DelayCounter growth_delay = { 0 };
830   static DelayCounter update_delay = { 0 };
831   static DelayCounter change_delay = { 0 };
832   static int percent = 0;
833   static int debug_value = 0;
834   boolean reinitialize = FALSE;
835   boolean active_last = active;
836   int i, x, y;
837
838   if (draw_target != DRAW_TO_SCREEN)
839     return;
840
841   if (DelayReached(&check_delay))
842   {
843     percent = (debug ? debug_value * 100 / XSN_DEBUG_STEPS : xsn_percent());
844
845     if (debug)
846       setup.debug.xsn_percent = percent;
847
848     if (setup.debug.xsn_mode != AUTO)
849       percent = setup.debug.xsn_percent;
850
851     setup.debug.xsn_percent = percent;
852
853     active = (percent > 0);
854
855     if ((active && !active_last) || setup.debug.xsn_mode != AUTO)
856       removeHideSetupEntry(&setup.debug.xsn_mode);
857     else if (!active && active_last)
858       setHideSetupEntry(&setup.debug.xsn_mode);
859
860     if (setup.debug.xsn_mode == FALSE)
861       active = FALSE;
862   }
863   else if (tile_cursor.xsn_debug)
864   {
865     debug_value = (active ? 0 : MIN(debug_value + 1, XSN_DEBUG_STEPS));
866     debug = TRUE;
867     active = FALSE;
868
869     ResetDelayCounter(&check_delay);
870
871     setup.debug.xsn_mode = (debug_value > 0);
872     tile_cursor.xsn_debug = FALSE;
873   }
874
875   if (!active)
876     return;
877
878   if (!initialized)
879   {
880     xsn.area_xsize = gfx.win_xsize;
881     xsn.area_ysize = gfx.win_ysize;
882
883     for (i = 0; i < num_xsn_data; i++)
884     {
885       int size = xsn_data[i].size;
886       byte *bits = xsn_data[i].bits;
887       Bitmap *bitmap = CreateBitmap(size, size, DEFAULT_DEPTH);
888
889       FillRectangle(bitmap, 0, 0, size, size, BLACK_PIXEL);
890
891       for (y = 0; y < size; y++)
892         for (x = 0; x < size; x++)
893           if ((bits[y] >> x) & 0x01)
894             SDLPutPixel(bitmap, x, y, WHITE_PIXEL);
895
896       SDL_Surface *surface = bitmap->surface;
897
898       if ((bitmap->surface_masked = SDLGetNativeSurface(surface)) == NULL)
899         Fail("SDLGetNativeSurface() failed");
900
901       SDL_Surface *surface_masked = bitmap->surface_masked;
902
903       SDL_SetColorKey(surface_masked, SET_TRANSPARENT_PIXEL,
904                       SDL_MapRGB(surface_masked->format, 0x00, 0x00, 0x00));
905
906       SDLSetAlpha(surface, TRUE, XSN_ALPHA_DEFAULT);
907       SDLSetAlpha(surface_masked, TRUE, XSN_ALPHA_DEFAULT);
908
909       xsn_data[i].bitmap = bitmap;
910     }
911
912     srand((unsigned int)time(NULL));
913
914     initialized = TRUE;
915   }
916
917   if (!active_last)
918   {
919     start_delay.value = (debug || setup.debug.xsn_mode == TRUE ? 0 :
920                          (XSN_START_DELAY + XSN_RND(XSN_START_DELAY)) * 1000);
921     started = FALSE;
922
923     ResetDelayCounter(&start_delay);
924
925     reinitialize = TRUE;
926   }
927
928   if (reinitialize)
929   {
930     xsn.num_items  = 0;
931     xsn.max_items  = percent * XSN_MAX_ITEMS / 100;
932     xsn.max_height = percent * XSN_MAX_HEIGHT / 100;
933
934     xsn.max_dx = XSN_MAX_DX;
935     xsn.max_dy = XSN_MAX_DY;
936
937     xsn.change_delay = XSN_CHANGE_DELAY;
938     xsn.change_type  = 0;
939     xsn.change_dir   = 0;
940
941     xsn.alpha = XSN_ALPHA_DEFAULT;
942
943     for (i = 0; i < xsn.max_items; i++)
944       xsn_init_item(i);
945   }
946
947   if (xsn.area_xsize != gfx.win_xsize ||
948       xsn.area_ysize != gfx.win_ysize ||
949       reinitialize)
950   {
951     xsn.area_xsize = gfx.win_xsize;
952     xsn.area_ysize = gfx.win_ysize;
953
954     if (xsn.bitmap != NULL)
955       FreeBitmap(xsn.bitmap);
956
957     xsn.bitmap = CreateBitmap(xsn.area_xsize, xsn.max_height * 2,
958                               DEFAULT_DEPTH);
959
960     FillRectangle(xsn.bitmap, 0, 0, xsn.area_xsize, xsn.max_height,
961                   BLACK_PIXEL);
962
963     SDL_Surface *surface = xsn.bitmap->surface;
964
965     if ((xsn.bitmap->surface_masked = SDLGetNativeSurface(surface)) == NULL)
966       Fail("SDLGetNativeSurface() failed");
967
968     SDL_Surface *surface_masked = xsn.bitmap->surface_masked;
969
970     SDL_SetColorKey(surface_masked, SET_TRANSPARENT_PIXEL,
971                     SDL_MapRGB(surface_masked->format, 0x00, 0x00, 0x00));
972
973     SDLSetAlpha(surface, TRUE, xsn.alpha);
974     SDLSetAlpha(surface_masked, TRUE, xsn.alpha);
975
976     SDLCreateBitmapTextures(xsn.bitmap);
977
978     for (i = 0; i < num_xsn_data; i++)
979     {
980       SDLFreeBitmapTextures(xsn_data[i].bitmap);
981       SDLCreateBitmapTextures(xsn_data[i].bitmap);
982     }
983
984     if (xsn.height != NULL)
985       checked_free(xsn.height);
986
987     xsn.height = checked_calloc(xsn.area_xsize * sizeof(int));
988
989     for (i = 0; i < xsn.area_xsize; i++)
990       xsn.height[i] = xsn.area_ysize - 1;
991   }
992
993   if (!started)
994   {
995     if (!DelayReached(&start_delay))
996       return;
997
998     update_delay.value = XSN_UPDATE_DELAY;
999     growth_delay.value = XSN_GROWTH_DELAY * 1000;
1000     change_delay.value = XSN_CHANGE_DELAY * 1000;
1001
1002     ResetDelayCounter(&growth_delay);
1003     ResetDelayCounter(&update_delay);
1004     ResetDelayCounter(&change_delay);
1005
1006     started = TRUE;
1007   }
1008
1009   if (xsn.num_items < xsn.max_items)
1010   {
1011     if (DelayReached(&growth_delay))
1012     {
1013       xsn.num_items += XSN_RND(XSN_GROWTH_RATE * 2);
1014       xsn.num_items = MIN(xsn.num_items, xsn.max_items);
1015     }
1016   }
1017
1018   if (DelayReached(&update_delay))
1019   {
1020     for (i = 0; i < xsn.num_items; i++)
1021       xsn_update_item(i);
1022   }
1023
1024   if (DelayReached(&change_delay))
1025   {
1026     xsn_update_change();
1027
1028     change_delay.value = xsn.change_delay * 1000;
1029   }
1030
1031   int xsn_alpha_dx = (gfx.mouse_y > xsn.area_ysize - xsn.max_height ?
1032                       (xsn.alpha > XSN_ALPHA_VISIBLE ? -1 : 0) :
1033                       (xsn.alpha < XSN_ALPHA_DEFAULT ? +1 : 0));
1034
1035   if (xsn_alpha_dx != 0)
1036   {
1037     xsn.alpha += xsn_alpha_dx;
1038
1039     SDLSetAlpha(xsn.bitmap->surface_masked, TRUE, xsn.alpha);
1040
1041     SDLFreeBitmapTextures(xsn.bitmap);
1042     SDLCreateBitmapTextures(xsn.bitmap);
1043   }
1044
1045   BlitToScreenMasked(xsn.bitmap, 0, 0, xsn.area_xsize, xsn.max_height,
1046                      0, xsn.area_ysize - xsn.max_height);
1047
1048   for (i = 0; i < xsn.num_items; i++)
1049   {
1050     int dst_x = xsn.items[i].x;
1051     int dst_y = xsn.items[i].y;
1052     int type = xsn.items[i].type;
1053     int size = xsn_data[type].size;
1054     Bitmap *bitmap = xsn_data[type].bitmap;
1055
1056     BlitToScreenMasked(bitmap, 0, 0, size, size, dst_x, dst_y);
1057   }
1058 }
1059
1060 void DrawTileCursor_MM(int draw_target, int drawing_stage,
1061                        boolean tile_cursor_active)
1062 {
1063   if (program.headless)
1064     return;
1065
1066   Bitmap *fade_bitmap;
1067   Bitmap *src_bitmap;
1068   int src_x, src_y;
1069   int dst_x, dst_y;
1070   int graphic = IMG_GLOBAL_TILE_CURSOR;
1071   int frame = 0;
1072   int tilesize = TILESIZE_VAR;
1073   int width = tilesize;
1074   int height = tilesize;
1075
1076   if (!drawing_stage)
1077   {
1078     DrawTileCursor_Xsn(draw_target);
1079
1080     return;
1081   }
1082
1083   if (!tile_cursor.enabled ||
1084       !tile_cursor.active ||
1085       !tile_cursor_active)
1086     return;
1087
1088   if (tile_cursor.moving)
1089   {
1090     int step = TILESIZE_VAR / 4;
1091     int dx = tile_cursor.target_x - tile_cursor.x;
1092     int dy = tile_cursor.target_y - tile_cursor.y;
1093
1094     if (ABS(dx) < step)
1095       tile_cursor.x = tile_cursor.target_x;
1096     else
1097       tile_cursor.x += SIGN(dx) * step;
1098
1099     if (ABS(dy) < step)
1100       tile_cursor.y = tile_cursor.target_y;
1101     else
1102       tile_cursor.y += SIGN(dy) * step;
1103
1104     if (tile_cursor.x == tile_cursor.target_x &&
1105         tile_cursor.y == tile_cursor.target_y)
1106       tile_cursor.moving = FALSE;
1107   }
1108
1109   dst_x = tile_cursor.x;
1110   dst_y = tile_cursor.y;
1111
1112   frame = getGraphicAnimationFrame(graphic, -1);
1113
1114   getSizedGraphicSource(graphic, frame, tilesize, &src_bitmap, &src_x, &src_y);
1115
1116   fade_bitmap =
1117     (draw_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
1118      draw_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
1119
1120   if (draw_target == DRAW_TO_SCREEN)
1121     BlitToScreenMasked(src_bitmap, src_x, src_y, width, height, dst_x, dst_y);
1122   else
1123     BlitBitmapMasked(src_bitmap, fade_bitmap, src_x, src_y, width, height,
1124                      dst_x, dst_y);
1125 }
1126
1127 Pixel ReadPixel(DrawBuffer *bitmap, int x, int y)
1128 {
1129   return GetPixel(bitmap, x, y);
1130 }
1131
1132 int get_base_element(int element)
1133 {
1134   if (IS_MIRROR(element))
1135     return EL_MIRROR_START;
1136   else if (IS_MIRROR_FIXED(element))
1137     return EL_MIRROR_FIXED_START;
1138   else if (IS_POLAR(element))
1139     return EL_POLAR_START;
1140   else if (IS_POLAR_CROSS(element))
1141     return EL_POLAR_CROSS_START;
1142   else if (IS_BEAMER(element))
1143     return EL_BEAMER_RED_START + BEAMER_NR(element) * 16;
1144   else if (IS_FIBRE_OPTIC(element))
1145     return EL_FIBRE_OPTIC_START + FIBRE_OPTIC_NR(element) * 2;
1146   else if (IS_MCDUFFIN(element))
1147     return EL_MCDUFFIN_START;
1148   else if (IS_LASER(element))
1149     return EL_LASER_START;
1150   else if (IS_RECEIVER(element))
1151     return EL_RECEIVER_START;
1152   else if (IS_DF_MIRROR(element))
1153     return EL_DF_MIRROR_START;
1154   else if (IS_DF_MIRROR_AUTO(element))
1155     return EL_DF_MIRROR_AUTO_START;
1156   else if (IS_DF_MIRROR_FIXED(element))
1157     return EL_DF_MIRROR_FIXED_START;
1158   else if (IS_DF_SLOPE(element))
1159     return EL_DF_SLOPE_START;
1160   else if (IS_PACMAN(element))
1161     return EL_PACMAN_START;
1162   else if (IS_GRID_STEEL(element))
1163     return EL_GRID_STEEL_START;
1164   else if (IS_GRID_WOOD(element))
1165     return EL_GRID_WOOD_START;
1166   else if (IS_GRID_STEEL_FIXED(element))
1167     return EL_GRID_STEEL_FIXED_START;
1168   else if (IS_GRID_WOOD_FIXED(element))
1169     return EL_GRID_WOOD_FIXED_START;
1170   else if (IS_GRID_STEEL_AUTO(element))
1171     return EL_GRID_STEEL_AUTO_START;
1172   else if (IS_GRID_WOOD_AUTO(element))
1173     return EL_GRID_WOOD_AUTO_START;
1174   else if (IS_WALL_STEEL(element))
1175     return EL_WALL_STEEL_START;
1176   else if (IS_WALL_WOOD(element))
1177     return EL_WALL_WOOD_START;
1178   else if (IS_WALL_ICE(element))
1179     return EL_WALL_ICE_START;
1180   else if (IS_WALL_AMOEBA(element))
1181     return EL_WALL_AMOEBA_START;
1182   else if (IS_DF_WALL_STEEL(element))
1183     return EL_DF_WALL_STEEL_START;
1184   else if (IS_DF_WALL_WOOD(element))
1185     return EL_DF_WALL_WOOD_START;
1186   else if (IS_CHAR(element))
1187     return EL_CHAR_START;
1188   else
1189     return element;
1190 }
1191
1192 int get_element_phase(int element)
1193 {
1194   return element - get_base_element(element);
1195 }
1196
1197 int get_num_elements(int element)
1198 {
1199   if (IS_MIRROR(element) ||
1200       IS_POLAR(element) ||
1201       IS_BEAMER(element) ||
1202       IS_DF_MIRROR(element) ||
1203       IS_DF_MIRROR_AUTO(element) ||
1204       IS_DF_MIRROR_FIXED(element))
1205     return 16;
1206   else if (IS_GRID_STEEL_FIXED(element) ||
1207            IS_GRID_WOOD_FIXED(element) ||
1208            IS_GRID_STEEL_AUTO(element) ||
1209            IS_GRID_WOOD_AUTO(element))
1210     return 8;
1211   else if (IS_MIRROR_FIXED(element) ||
1212            IS_POLAR_CROSS(element) ||
1213            IS_MCDUFFIN(element) ||
1214            IS_LASER(element) ||
1215            IS_RECEIVER(element) ||
1216            IS_PACMAN(element) ||
1217            IS_GRID_STEEL(element) ||
1218            IS_GRID_WOOD(element) ||
1219            IS_DF_SLOPE(element))
1220     return 4;
1221   else
1222     return 1;
1223 }
1224
1225 int get_rotated_element(int element, int step)
1226 {
1227   int base_element = get_base_element(element);
1228   int num_elements = get_num_elements(element);
1229   int element_phase = element - base_element;
1230
1231   return base_element + (element_phase + step + num_elements) % num_elements;
1232 }
1233
1234 static boolean has_full_rotation(int element)
1235 {
1236   return (IS_BEAMER(element) ||
1237           IS_MCDUFFIN(element) ||
1238           IS_LASER(element) ||
1239           IS_RECEIVER(element) ||
1240           IS_PACMAN(element));
1241 }
1242
1243 #define MM_FLIP_X                       0
1244 #define MM_FLIP_Y                       1
1245 #define MM_FLIP_XY                      2
1246
1247 static int getFlippedTileExt_MM(int element, int mode)
1248 {
1249   if (IS_WALL(element))
1250   {
1251     int base = WALL_BASE(element);
1252     int bits = WALL_BITS(element);
1253
1254     if (mode == MM_FLIP_X)
1255     {
1256       bits = ((bits & 1) << 1 |
1257               (bits & 2) >> 1 |
1258               (bits & 4) << 1 |
1259               (bits & 8) >> 1);
1260     }
1261     else if (mode == MM_FLIP_Y)
1262     {
1263       bits = ((bits & 1) << 2 |
1264               (bits & 2) << 2 |
1265               (bits & 4) >> 2 |
1266               (bits & 8) >> 2);
1267     }
1268     else if (mode == MM_FLIP_XY)
1269     {
1270       bits = ((bits & 1) << 0 |
1271               (bits & 2) << 1 |
1272               (bits & 4) >> 1 |
1273               (bits & 8) >> 0);
1274     }
1275
1276     element = base | bits;
1277   }
1278   else
1279   {
1280     int base_element = get_base_element(element);
1281     int num_elements = get_num_elements(element);
1282     int element_phase = element - base_element;
1283
1284     if (IS_GRID_STEEL(element) || IS_GRID_WOOD(element))
1285     {
1286       if ((mode == MM_FLIP_XY && element_phase < 2) ||
1287           (mode != MM_FLIP_XY && element_phase > 1))
1288         element_phase ^= 1;
1289     }
1290     else if (IS_DF_SLOPE(element))
1291     {
1292       element_phase = (mode == MM_FLIP_X  ? 5 - element_phase :
1293                        mode == MM_FLIP_Y  ? 3 - element_phase :
1294                        mode == MM_FLIP_XY ? 4 - element_phase :
1295                        element_phase);
1296     }
1297     else
1298     {
1299       int num_elements_flip = num_elements;
1300
1301       if (has_full_rotation(element))
1302       {
1303         if (mode == MM_FLIP_X)
1304           num_elements_flip = num_elements / 2;
1305         else if (mode == MM_FLIP_XY)
1306           num_elements_flip = num_elements * 3 / 4;
1307       }
1308       else
1309       {
1310         if (mode == MM_FLIP_XY)
1311           num_elements_flip = num_elements / 2;
1312       }
1313
1314       element_phase = num_elements_flip - element_phase;
1315     }
1316
1317     element = base_element + (element_phase + num_elements) % num_elements;
1318   }
1319
1320   return element;
1321 }
1322
1323 int getFlippedTileX_MM(int element)
1324 {
1325   return getFlippedTileExt_MM(element, MM_FLIP_X);
1326 }
1327
1328 int getFlippedTileY_MM(int element)
1329 {
1330   return getFlippedTileExt_MM(element, MM_FLIP_Y);
1331 }
1332
1333 int getFlippedTileXY_MM(int element)
1334 {
1335   return getFlippedTileExt_MM(element, MM_FLIP_XY);
1336 }
1337
1338 int map_wall_from_base_element(int element)
1339 {
1340   switch (element)
1341   {
1342     case EL_WALL_STEEL_BASE:    return EL_WALL_STEEL;
1343     case EL_WALL_WOOD_BASE:     return EL_WALL_WOOD;
1344     case EL_WALL_ICE_BASE:      return EL_WALL_ICE;
1345     case EL_WALL_AMOEBA_BASE:   return EL_WALL_AMOEBA;
1346     case EL_DF_WALL_STEEL_BASE: return EL_DF_WALL_STEEL;
1347     case EL_DF_WALL_WOOD_BASE:  return EL_DF_WALL_WOOD;
1348
1349     default:                    return element;
1350   }
1351 }
1352
1353 int map_wall_to_base_element(int element)
1354 {
1355   switch (element)
1356   {
1357     case EL_WALL_STEEL:         return EL_WALL_STEEL_BASE;
1358     case EL_WALL_WOOD:          return EL_WALL_WOOD_BASE;
1359     case EL_WALL_ICE:           return EL_WALL_ICE_BASE;
1360     case EL_WALL_AMOEBA:        return EL_WALL_AMOEBA_BASE;
1361     case EL_DF_WALL_STEEL:      return EL_DF_WALL_STEEL_BASE;
1362     case EL_DF_WALL_WOOD:       return EL_DF_WALL_WOOD_BASE;
1363
1364     default:                    return element;
1365   }
1366 }
1367
1368 int el2gfx(int element)
1369 {
1370   return el2img_mm(map_wall_from_base_element(element));
1371 }
1372
1373 int el_act2gfx(int element, int action)
1374 {
1375   return el_act2img_mm(map_wall_from_base_element(element), action);
1376 }
1377
1378 void RedrawPlayfield_MM(void)
1379 {
1380   DrawLevel_MM();
1381   DrawLaser_MM();
1382 }
1383
1384 void BlitScreenToBitmap_MM(Bitmap *target_bitmap)
1385 {
1386   BlitBitmap(drawto_mm, target_bitmap,
1387              REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE, REAL_SX, REAL_SY);
1388 }