renamed preprocessor constant for game element in MM engine
[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 playfield border
33   cSY2 = SY + dSY + 2;  // including playfield border
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 ClearWindow(void)
45 {
46   ClearRectangle(backbuffer, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
47
48   SetDrawtoField(DRAW_TO_BACKBUFFER);
49   SetDrawtoField_MM(DRAW_TO_BACKBUFFER);
50
51   redraw_mask |= REDRAW_FIELD;
52 }
53
54 void DrawGraphicAnimation_MM(int x, int y, int graphic, int frame)
55 {
56   Bitmap *bitmap;
57   int src_x, src_y;
58
59   getGraphicSource(graphic, frame, &bitmap, &src_x, &src_y);
60
61   BlitBitmap(bitmap, drawto_field, src_x, src_y, TILEX, TILEY,
62              cFX + x * TILEX, cFY + y * TILEY);
63 }
64
65 void DrawGraphic_MM(int x, int y, int graphic)
66 {
67 #if DEBUG
68   if (!IN_SCR_FIELD(x,y))
69   {
70     Debug("game:mm:DrawGraphic_MM", "x = %d, y = %d, graphic = %d",
71           x, y, graphic);
72     Debug("game:mm:DrawGraphic_MM", "This should never happen!");
73
74     return;
75   }
76 #endif
77
78   int frame = getGraphicAnimationFrameXY(graphic, x, y);
79
80   DrawGraphicAnimation_MM(x, y, graphic, frame);
81
82   MarkTileDirty(x, y);
83 }
84
85 void DrawGraphicExt_MM(DrawBuffer *d, int x, int y, int graphic)
86 {
87   Bitmap *bitmap;
88   int src_x, src_y;
89
90   getGraphicSource(graphic, 0, &bitmap, &src_x, &src_y);
91
92   BlitBitmap(bitmap, d, src_x, src_y, TILEX, TILEY, x, y);
93 }
94
95 void DrawGraphicThruMask_MM(int x, int y, int graphic, int frame)
96 {
97 #if DEBUG
98   if (!IN_SCR_FIELD(x,y))
99   {
100     Debug("game:mm:DrawGraphicThruMask_MM", "x = %d,y = %d, graphic = %d",
101           x, y, graphic);
102     Debug("game:mm:DrawGraphicThruMask_MM", "This should never happen!");
103
104     return;
105   }
106 #endif
107
108   DrawGraphicThruMaskExt_MM(drawto_field, cFX + x * TILEX, cFY + y * TILEY,
109                             graphic, frame);
110
111   MarkTileDirty(x,y);
112 }
113
114 void DrawGraphicThruMaskExt_MM(DrawBuffer *d, int dest_x, int dest_y,
115                                int graphic, int frame)
116 {
117   int src_x, src_y;
118   Bitmap *src_bitmap;
119
120   if (graphic == IMG_EMPTY)
121     return;
122
123   getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
124
125   BlitBitmapMasked(src_bitmap, d, src_x, src_y, TILEX, TILEY, dest_x, dest_y);
126 }
127
128 void DrawMiniGraphic_MM(int x, int y, int graphic)
129 {
130   DrawMiniGraphicExt_MM(drawto, cSX + x * MINI_TILEX, cSY + y * MINI_TILEY,
131                         graphic);
132
133   MarkTileDirty(x / 2, y / 2);
134 }
135
136 #if 0
137 static void getMicroGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y)
138 {
139   getSizedGraphicSource(graphic, 0, TILESIZE / 4, bitmap, x, y);
140 }
141 #endif
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_field,
257                      src_x, src_y, TILEX, TILEY, dest_x, dest_y);
258   else
259     BlitBitmap(src_bitmap, drawto_field,
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   DrawScreenField_MM(x, y);
368 }
369
370 void DrawMiniElement_MM(int x, int y, int element)
371 {
372   int graphic;
373
374   if (!element)
375   {
376     DrawMiniGraphic_MM(x, y, IMG_EMPTY);
377
378     return;
379   }
380
381   graphic = el2gfx(element);
382
383   DrawMiniGraphic_MM(x, y, graphic);
384 }
385
386 void DrawMiniElementOrWall_MM(int sx, int sy, int scroll_x, int scroll_y)
387 {
388   int x = sx + scroll_x, y = sy + scroll_y;
389
390   if (x < -1 || x > lev_fieldx || y < -1 || y > lev_fieldy)
391     DrawMiniElement_MM(sx, sy, EL_EMPTY);
392   else if (x > -1 && x < lev_fieldx && y > -1 && y < lev_fieldy)
393     DrawMiniElement_MM(sx, sy, Tile[x][y]);
394 }
395
396 void DrawField_MM(int x, int y)
397 {
398   int element = Tile[x][y];
399
400   DrawElement_MM(x, y, element);
401 }
402
403 void DrawLevel_MM(void)
404 {
405   int x,y;
406
407   ClearWindow();
408
409   for (x = 0; x < lev_fieldx; x++)
410     for (y = 0; y < lev_fieldy; y++)
411       DrawField_MM(x, y);
412
413   redraw_mask |= REDRAW_FIELD;
414 }
415
416 void DrawWallsExt_MM(int x, int y, int element, int draw_mask)
417 {
418   Bitmap *bitmap;
419   int graphic = el2gfx(WALL_BASE(element));
420   int gx, gy;
421   int i;
422
423   getMiniGraphicSource(graphic, &bitmap, &gx, &gy);
424
425   DrawGraphic_MM(x, y, IMG_EMPTY);
426
427   /*
428   if (IS_WALL_WOOD(element) || IS_WALL_AMOEBA(element) ||
429       IS_DF_WALL_WOOD(element))
430     gx += MINI_TILEX;
431   if (IS_WALL_ICE(element) || IS_WALL_AMOEBA(element))
432     gy += MINI_TILEY;
433   */
434
435   for (i = 0; i < 4; i++)
436   {
437     int dest_x = cSX + x * TILEX + MINI_TILEX * (i % 2);
438     int dest_y = cSY + y * TILEY + MINI_TILEY * (i / 2);
439
440     if (!((1 << i) & draw_mask))
441       continue;
442
443     if (element & (1 << i))
444       BlitBitmap(bitmap, drawto, gx, gy, MINI_TILEX, MINI_TILEY,
445                  dest_x, dest_y);
446     else
447       ClearRectangle(drawto, dest_x, dest_y, MINI_TILEX, MINI_TILEY);
448   }
449
450   MarkTileDirty(x, y);
451 }
452
453 void DrawWalls_MM(int x, int y, int element)
454 {
455   DrawWallsExt_MM(x, y, element, HIT_MASK_ALL);
456 }
457
458 void DrawWallsAnimation_MM(int x, int y, int element, int phase, int bit_mask)
459 {
460   int i;
461
462   if (phase == 0)
463   {
464     DrawWalls_MM(x, y, element);
465
466     return;
467   }
468
469   for (i = 0; i < 4; i++)
470   {
471     if (element & (1 << i))
472     {
473       int graphic;
474       int frame;
475       Bitmap *bitmap;
476       int src_x, src_y;
477       int dst_x = cSX + x * TILEX + (i % 2) * MINI_TILEX;
478       int dst_y = cSY + y * TILEY + (i / 2) * MINI_TILEY;
479
480       if (bit_mask & (1 << i))
481       {
482         graphic = (IS_WALL_AMOEBA(element) ?
483                    IMG_MM_AMOEBA_WALL_GROWING :
484                    IMG_MM_ICE_WALL_SHRINKING);
485         frame = phase;
486       }
487       else
488       {
489         graphic = (IS_WALL_AMOEBA(element) ?
490                    IMG_MM_AMOEBA_WALL :
491                    IMG_MM_ICE_WALL);
492         frame = 0;
493       }
494
495       getSizedGraphicSource(graphic, frame, MINI_TILESIZE, &bitmap,
496                             &src_x, &src_y);
497
498       BlitBitmap(bitmap, drawto, src_x, src_y, MINI_TILEX, MINI_TILEY,
499                  dst_x, dst_y);
500     }
501   }
502
503   MarkTileDirty(x, y);
504 }
505
506 void DrawElement_MM(int x, int y, int element)
507 {
508   if (element == EL_EMPTY)
509     DrawGraphic_MM(x, y, IMG_EMPTY);
510   else if (IS_WALL(element))
511     DrawWalls_MM(x, y, element);
512 #if 0
513   else if (IS_WALL_CHANGING(element) && IS_WALL_CHANGING(Tile[x][y]))
514   {
515     int wall_element = Tile[x][y] - EL_WALL_CHANGING + Store[x][y];
516
517     DrawWalls_MM(x, y, wall_element);
518   }
519 #endif
520   else if (element == EL_PACMAN)
521     DrawLevelField_MM(x, y);
522   else if (element == EL_FUSE_ON &&
523            laser.fuse_off &&
524            laser.fuse_x == x &&
525            laser.fuse_y == y)
526     DrawGraphic_MM(x, y, IMG_MM_FUSE);
527   else if (element == EL_GRAY_BALL_ACTIVE)
528     DrawGraphic_MM(x, y, el_act2gfx(EL_GRAY_BALL, MM_ACTION_ACTIVE));
529   else if (element == EL_GRAY_BALL_OPENING)
530     DrawGraphic_MM(x, y, el_act2gfx(EL_GRAY_BALL, MM_ACTION_OPENING));
531   else if (element == EL_BOMB_ACTIVE)
532     DrawGraphic_MM(x, y, el_act2gfx(EL_BOMB, MM_ACTION_ACTIVE));
533   else if (element == EL_MINE_ACTIVE)
534     DrawGraphic_MM(x, y, el_act2gfx(EL_MINE, MM_ACTION_ACTIVE));
535   else
536     DrawGraphic_MM(x, y, el2gfx(element));
537 }
538
539 #if 0
540 static void DrawMicroWalls_MM(int x, int y, int element)
541 {
542   Bitmap *bitmap;
543   int graphic = el2gfx(WALL_BASE(element));
544   int gx, gy;
545   int i;
546
547   getMicroGraphicSource(graphic, &bitmap, &gx, &gy);
548
549   for (i = 0; i < 4; i++)
550   {
551     int xpos = MICROLEV_XPOS + x * MICRO_TILEX + MICRO_WALLX * (i % 2);
552     int ypos = MICROLEV_YPOS + y * MICRO_TILEY + MICRO_WALLY * (i / 2);
553
554     if (element & (1 << i))
555       BlitBitmap(bitmap, drawto, gx, gy, MICRO_WALLX, MICRO_WALLY, xpos, ypos);
556     else
557       ClearRectangle(drawto, xpos, ypos, MICRO_WALLX, MICRO_WALLY);
558   }
559 }
560
561 static void DrawMicroElement_MM(int x, int y, int element)
562 {
563   Bitmap *bitmap;
564   int graphic = el2gfx(element);
565   int gx, gy;
566
567   if (element == EL_EMPTY)
568     return;
569
570   if (IS_WALL(element))
571   {
572     DrawMicroWalls_MM(x, y, element);
573
574     return;
575   }
576
577   getMicroGraphicSource(graphic, &bitmap, &gx, &gy);
578
579   BlitBitmap(bitmap, drawto, gx, gy, MICRO_TILEX, MICRO_TILEY,
580              MICROLEV_XPOS + x * MICRO_TILEX, MICROLEV_YPOS + y * MICRO_TILEY);
581 }
582
583 static void DrawMicroLevelExt_MM(int xpos, int ypos)
584 {
585   int x, y;
586
587   ClearRectangle(drawto, xpos, ypos, MICROLEV_XSIZE, MICROLEV_YSIZE);
588
589   for (x = 0; x < STD_LEV_FIELDX; x++)
590     for (y = 0; y < STD_LEV_FIELDY; y++)
591       DrawMicroElement_MM(x, y, Ur[x][y]);
592
593   redraw_mask |= REDRAW_FIELD;
594 }
595 #endif
596
597
598 // ----------------------------------------------------------------------------
599 // XSN
600 // ----------------------------------------------------------------------------
601
602 #define XSN_RND(x)              ((x) != 0 ? rand() % (x) : 0)
603 #define XSN_ALPHA_VALUE(x)      (SDL_ALPHA_OPAQUE * (x) / 100)
604
605 #define XSN_MAX_ITEMS           100
606 #define XSN_MAX_HEIGHT          40
607 #define XSN_MAX_DX              2
608 #define XSN_MAX_DY              10
609 #define XSN_CHECK_DELAY         3
610 #define XSN_START_DELAY         60
611 #define XSN_UPDATE_DELAY        50
612 #define XSN_GROWTH_DELAY        3
613 #define XSN_GROWTH_RATE         3
614 #define XSN_CHANGE_DELAY        30
615 #define XSN_CHANGE_FACTOR       3
616 #define XSN_ALPHA_DEFAULT       XSN_ALPHA_VALUE(95)
617 #define XSN_ALPHA_VISIBLE       XSN_ALPHA_VALUE(50)
618 #define XSN_DEBUG_STEPS         5
619
620 static byte xsn_bits_0[] = { 0x05, 0x02, 0x05 };
621 static byte xsn_bits_1[] = { 0x22, 0x6b, 0x14, 0x2a, 0x14, 0x6b, 0x22 };
622 static byte xsn_bits_2[] = { 0x14, 0x08, 0x49, 0x36, 0x49, 0x08, 0x14 };
623
624 char debug_xsn_mode[] = { 76,101,116,32,105,116,32,115,110,111,119,33,0 };
625
626 void setHideSetupEntry(void *);
627 void removeHideSetupEntry(void *);
628
629 static struct
630 {
631   int size;
632   byte *bits;
633   Bitmap *bitmap;
634 }
635 xsn_data[] =
636 {
637   { ARRAY_SIZE(xsn_bits_0), xsn_bits_0 },
638   { ARRAY_SIZE(xsn_bits_1), xsn_bits_1 },
639   { ARRAY_SIZE(xsn_bits_2), xsn_bits_2 },
640   { ARRAY_SIZE(xsn_bits_2), xsn_bits_2 },
641   { ARRAY_SIZE(xsn_bits_1), xsn_bits_1 },
642   { ARRAY_SIZE(xsn_bits_2), xsn_bits_2 },
643   { ARRAY_SIZE(xsn_bits_0), xsn_bits_0 },
644 };
645 static int num_xsn_data = ARRAY_SIZE(xsn_data);
646
647 struct XsnItem
648 {
649   int x;
650   int y;
651   int dx;
652   int dy;
653   int type;
654   int active;
655 };
656
657 struct Xsn
658 {
659   int area_xsize;
660   int area_ysize;
661
662   int num_items;
663   int max_items;
664   int max_height;
665   int max_dx;
666   int max_dy;
667
668   int change_delay;
669   int change_type;
670   int change_dir;
671
672   int *height;
673
674   struct XsnItem items[XSN_MAX_ITEMS];
675
676   Bitmap *bitmap;
677
678   int alpha;
679 };
680
681 static struct Xsn xsn = { 0 };
682
683 static int xsn_percent(void)
684 {
685   int xsn_m0 = -3;
686   int xsn_m1 = xsn_m0 + 10;
687   int xsn_m2 = xsn_m1 + 10;
688   int xsn_m3 = xsn_m2 + 10;
689   time_t xsn_e0 = time(NULL);
690   struct tm *xsn_t0 = localtime(&xsn_e0);
691   struct tm xsn_t1 = { 0,0,0, xsn_m2*3, xsn_m3/3, xsn_t0->tm_year, 0,0,-1 };
692   time_t xsn_e1 = mktime(&xsn_t1);
693   int xsn_c0 = (25 * xsn_m3) << xsn_m1;
694   int xsn_c1 = (xsn_t1.tm_wday - xsn_m1) * !!xsn_t1.tm_wday;
695
696   for (xsn_m0 = 5; xsn_m0 > 0; xsn_m0--)
697   {
698     int xsn_c2 = (xsn_m0 > 4 ? 0 : xsn_c1) - xsn_m1 * xsn_m0;
699     int xsn_off = (xsn_m0 > 4 ? xsn_c0 : 0);
700     time_t xsn_e3 = xsn_e1 - xsn_c2 * xsn_c0;
701
702     if (xsn_e0 > xsn_e3 - xsn_off &&
703         xsn_e0 < xsn_e3 + xsn_off + xsn_c0)
704       return xsn_m0 * (xsn_m3 - xsn_m1);
705   }
706
707   return xsn_m0;
708 }
709
710 static void xsn_init_item(int nr)
711 {
712   struct XsnItem *item = &xsn.items[nr];
713
714   item->type = XSN_RND(num_xsn_data);
715
716   if (xsn.change_type != 0)
717   {
718     int new_x = XSN_RND(xsn.area_xsize / 3);
719
720     item->x = (xsn.change_dir == 1 ? new_x : xsn.area_xsize - new_x);
721     item->y = XSN_RND(xsn.area_ysize);
722   }
723   else
724   {
725     item->x = XSN_RND(xsn.area_xsize - xsn_data[item->type].size);
726     item->y = XSN_RND(xsn.area_ysize / 10);
727   }
728
729   item->dy = XSN_RND(xsn.max_dy + 1) + 1;
730   item->dx = XSN_RND(item->dy / 4 + 1) * (XSN_RND(1000) > 500 ? -1 : 1);
731
732   item->active = 1;
733 }
734
735 static void xsn_update_item(int nr)
736 {
737   struct XsnItem *item = &xsn.items[nr];
738
739   if (!item->active)
740     xsn_init_item(nr);
741
742   if (xsn.change_type != 0)
743   {
744     int dx_new = ABS(item->dx) +
745       (xsn.change_type == 1 ?
746        XSN_RND(XSN_CHANGE_FACTOR + 1) - XSN_CHANGE_FACTOR / 2 :
747        XSN_RND(20));
748
749     item->dx = MIN(MAX(-50, dx_new * xsn.change_dir), 50);
750   }
751
752   int new_x = item->x + item->dx;
753   int new_y = item->y + item->dy;
754
755   item->active = (new_y < xsn.area_ysize);
756
757   if (xsn.change_type != 0)
758     item->active = (item->active && new_x > 0 && new_x < xsn.area_xsize);
759
760   int item_size = xsn_data[item->type].size;
761   int half_item_size = item_size / 2;
762   int mid_x = new_x + half_item_size;
763   int mid_y = new_y + half_item_size;
764   int upper_border = xsn.area_ysize - xsn.max_height;
765
766   if (item->active &&
767       new_y >= upper_border &&
768       new_x >= 0 &&
769       new_x <= xsn.area_xsize - item_size &&
770       mid_y >= xsn.height[mid_x] &&
771       mid_y < xsn.area_ysize)
772   {
773     Bitmap *item_bitmap = xsn_data[item->type].bitmap;
774     SDL_Surface *surface = xsn.bitmap->surface;
775     SDL_Surface *surface_masked = xsn.bitmap->surface_masked;
776     int item_alpha = XSN_ALPHA_VALUE(81 + XSN_RND(20));
777     int shrink = 1;
778     int i;
779
780     xsn.bitmap->surface = surface_masked;
781
782     SDLSetAlpha(item_bitmap->surface_masked, TRUE, item_alpha);
783
784     // blit to masked surface instead of opaque surface
785     BlitBitmapMasked(item_bitmap, xsn.bitmap, 0, 0, item_size, item_size,
786                      new_x, new_y - upper_border);
787
788     SDLSetAlpha(item_bitmap->surface_masked, TRUE, XSN_ALPHA_DEFAULT);
789
790     for (i = -half_item_size; i <= half_item_size; i++)
791     {
792       int xpos = mid_x + i;
793
794       if (xpos >= 0 && xpos < xsn.area_xsize)
795         xsn.height[xpos] = MIN(new_y + ABS(i), xsn.height[xpos]);
796     }
797
798     if (xsn.height[mid_x] <= upper_border + shrink)
799     {
800       int xpos1 = MAX(0, new_x - half_item_size);
801       int xpos2 = MIN(new_x + 3 * half_item_size, xsn.area_xsize);
802       int xsize = xpos2 - xpos1;
803       int ysize1 = XSN_RND(xsn.max_height - shrink);
804       int ysize2 = xsn.max_height - ysize1;
805
806       SDLSetAlpha(surface_masked, FALSE, 0);
807
808       FillRectangle(xsn.bitmap, xpos1, xsn.max_height, xsize, xsn.max_height,
809                     BLACK_PIXEL);
810       BlitBitmapMasked(xsn.bitmap, xsn.bitmap, xpos1, 0, xsize, ysize1,
811                        xpos1, xsn.max_height + shrink);
812       BlitBitmapMasked(xsn.bitmap, xsn.bitmap, xpos1, ysize1, xsize, ysize2,
813                        xpos1, xsn.max_height + ysize1);
814       FillRectangle(xsn.bitmap, xpos1, 0, xsize, xsn.max_height,
815                     BLACK_PIXEL);
816       BlitBitmapMasked(xsn.bitmap, xsn.bitmap, xpos1, xsn.max_height,
817                        xsize, xsn.max_height, xpos1, 0);
818
819       SDLSetAlpha(surface_masked, TRUE, xsn.alpha);
820
821       for (i = xpos1; i < xpos2; i++)
822         xsn.height[i] = MIN(xsn.height[i] + shrink, xsn.area_ysize - 1);
823     }
824
825     SDLFreeBitmapTextures(xsn.bitmap);
826     SDLCreateBitmapTextures(xsn.bitmap);
827
828     xsn.bitmap->surface = surface;
829
830     item->active = 0;
831   }
832
833   item->dx += XSN_RND(XSN_CHANGE_FACTOR) * (XSN_RND(1000) > 500 ? -1 : 1);
834
835   if (xsn.change_type == 0)
836     item->dx = MIN(MAX(-xsn.max_dx, item->dx), xsn.max_dx);
837
838   item->x = new_x;
839   item->y = new_y;
840 }
841
842 static void xsn_update_change(void)
843 {
844   if (XSN_RND(100) > 65)
845   {
846     xsn.change_dir = (XSN_RND(10) > 4 ? 1 : -1);
847     xsn.change_delay = XSN_RND(5) + 1;
848     xsn.change_type = 2;
849   }
850   else if (xsn.change_type == 2)
851   {
852     xsn.change_delay = XSN_RND(3) + 1;
853     xsn.change_type = 1;
854   }
855   else
856   {
857     xsn.change_delay = XSN_CHANGE_DELAY;
858     xsn.change_type = 0;
859   }
860 }
861
862 static void DrawTileCursor_Xsn(int draw_target)
863 {
864   static boolean initialized = FALSE;
865   static boolean started = FALSE;
866   static boolean active = FALSE;
867   static boolean debug = FALSE;
868   static DelayCounter check_delay = { XSN_CHECK_DELAY * 1000 };
869   static DelayCounter start_delay = { 0 };
870   static DelayCounter growth_delay = { 0 };
871   static DelayCounter update_delay = { 0 };
872   static DelayCounter change_delay = { 0 };
873   static int percent = 0;
874   static int debug_value = 0;
875   boolean reinitialize = FALSE;
876   boolean active_last = active;
877   int i, x, y;
878
879   if (draw_target != DRAW_TO_SCREEN)
880     return;
881
882   if (DelayReached(&check_delay))
883   {
884     percent = (debug ? debug_value * 100 / XSN_DEBUG_STEPS : xsn_percent());
885
886     if (debug)
887       setup.debug.xsn_percent = percent;
888
889     if (setup.debug.xsn_mode != AUTO)
890       percent = setup.debug.xsn_percent;
891
892     setup.debug.xsn_percent = percent;
893
894     active = (percent > 0);
895
896     if ((active && !active_last) || setup.debug.xsn_mode != AUTO)
897       removeHideSetupEntry(&setup.debug.xsn_mode);
898     else if (!active && active_last)
899       setHideSetupEntry(&setup.debug.xsn_mode);
900
901     if (setup.debug.xsn_mode == FALSE)
902       active = FALSE;
903   }
904   else if (tile_cursor.xsn_debug)
905   {
906     debug_value = (active ? 0 : MIN(debug_value + 1, XSN_DEBUG_STEPS));
907     debug = TRUE;
908     active = FALSE;
909
910     ResetDelayCounter(&check_delay);
911
912     setup.debug.xsn_mode = (debug_value > 0);
913     tile_cursor.xsn_debug = FALSE;
914   }
915
916   if (!active)
917     return;
918
919   if (!initialized)
920   {
921     xsn.area_xsize = gfx.win_xsize;
922     xsn.area_ysize = gfx.win_ysize;
923
924     for (i = 0; i < num_xsn_data; i++)
925     {
926       int size = xsn_data[i].size;
927       byte *bits = xsn_data[i].bits;
928       Bitmap *bitmap = CreateBitmap(size, size, DEFAULT_DEPTH);
929
930       FillRectangle(bitmap, 0, 0, size, size, BLACK_PIXEL);
931
932       for (y = 0; y < size; y++)
933         for (x = 0; x < size; x++)
934           if ((bits[y] >> x) & 0x01)
935             SDLPutPixel(bitmap, x, y, WHITE_PIXEL);
936
937       SDL_Surface *surface = bitmap->surface;
938
939       if ((bitmap->surface_masked = SDLGetNativeSurface(surface)) == NULL)
940         Fail("SDLGetNativeSurface() failed");
941
942       SDL_Surface *surface_masked = bitmap->surface_masked;
943
944       SDL_SetColorKey(surface_masked, SET_TRANSPARENT_PIXEL,
945                       SDL_MapRGB(surface_masked->format, 0x00, 0x00, 0x00));
946
947       SDLSetAlpha(surface, TRUE, XSN_ALPHA_DEFAULT);
948       SDLSetAlpha(surface_masked, TRUE, XSN_ALPHA_DEFAULT);
949
950       xsn_data[i].bitmap = bitmap;
951     }
952
953     srand((unsigned int)time(NULL));
954
955     initialized = TRUE;
956   }
957
958   if (!active_last)
959   {
960     start_delay.value = (debug || setup.debug.xsn_mode == TRUE ? 0 :
961                          (XSN_START_DELAY + XSN_RND(XSN_START_DELAY)) * 1000);
962     started = FALSE;
963
964     ResetDelayCounter(&start_delay);
965
966     reinitialize = TRUE;
967   }
968
969   if (reinitialize)
970   {
971     xsn.num_items  = 0;
972     xsn.max_items  = percent * XSN_MAX_ITEMS / 100;
973     xsn.max_height = percent * XSN_MAX_HEIGHT / 100;
974
975     xsn.max_dx = XSN_MAX_DX;
976     xsn.max_dy = XSN_MAX_DY;
977
978     xsn.change_delay = XSN_CHANGE_DELAY;
979     xsn.change_type  = 0;
980     xsn.change_dir   = 0;
981
982     xsn.alpha = XSN_ALPHA_DEFAULT;
983
984     for (i = 0; i < xsn.max_items; i++)
985       xsn_init_item(i);
986   }
987
988   if (xsn.area_xsize != gfx.win_xsize ||
989       xsn.area_ysize != gfx.win_ysize ||
990       reinitialize)
991   {
992     xsn.area_xsize = gfx.win_xsize;
993     xsn.area_ysize = gfx.win_ysize;
994
995     if (xsn.bitmap != NULL)
996       FreeBitmap(xsn.bitmap);
997
998     xsn.bitmap = CreateBitmap(xsn.area_xsize, xsn.max_height * 2,
999                               DEFAULT_DEPTH);
1000
1001     FillRectangle(xsn.bitmap, 0, 0, xsn.area_xsize, xsn.max_height,
1002                   BLACK_PIXEL);
1003
1004     SDL_Surface *surface = xsn.bitmap->surface;
1005
1006     if ((xsn.bitmap->surface_masked = SDLGetNativeSurface(surface)) == NULL)
1007       Fail("SDLGetNativeSurface() failed");
1008
1009     SDL_Surface *surface_masked = xsn.bitmap->surface_masked;
1010
1011     SDL_SetColorKey(surface_masked, SET_TRANSPARENT_PIXEL,
1012                     SDL_MapRGB(surface_masked->format, 0x00, 0x00, 0x00));
1013
1014     SDLSetAlpha(surface, TRUE, xsn.alpha);
1015     SDLSetAlpha(surface_masked, TRUE, xsn.alpha);
1016
1017     SDLCreateBitmapTextures(xsn.bitmap);
1018
1019     for (i = 0; i < num_xsn_data; i++)
1020     {
1021       SDLFreeBitmapTextures(xsn_data[i].bitmap);
1022       SDLCreateBitmapTextures(xsn_data[i].bitmap);
1023     }
1024
1025     if (xsn.height != NULL)
1026       checked_free(xsn.height);
1027
1028     xsn.height = checked_calloc(xsn.area_xsize * sizeof(int));
1029
1030     for (i = 0; i < xsn.area_xsize; i++)
1031       xsn.height[i] = xsn.area_ysize - 1;
1032   }
1033
1034   if (!started)
1035   {
1036     if (!DelayReached(&start_delay))
1037       return;
1038
1039     update_delay.value = XSN_UPDATE_DELAY;
1040     growth_delay.value = XSN_GROWTH_DELAY * 1000;
1041     change_delay.value = XSN_CHANGE_DELAY * 1000;
1042
1043     ResetDelayCounter(&growth_delay);
1044     ResetDelayCounter(&update_delay);
1045     ResetDelayCounter(&change_delay);
1046
1047     started = TRUE;
1048   }
1049
1050   if (xsn.num_items < xsn.max_items)
1051   {
1052     if (DelayReached(&growth_delay))
1053     {
1054       xsn.num_items += XSN_RND(XSN_GROWTH_RATE * 2);
1055       xsn.num_items = MIN(xsn.num_items, xsn.max_items);
1056     }
1057   }
1058
1059   if (DelayReached(&update_delay))
1060   {
1061     for (i = 0; i < xsn.num_items; i++)
1062       xsn_update_item(i);
1063   }
1064
1065   if (DelayReached(&change_delay))
1066   {
1067     xsn_update_change();
1068
1069     change_delay.value = xsn.change_delay * 1000;
1070   }
1071
1072   int xsn_alpha_dx = (gfx.mouse_y > xsn.area_ysize - xsn.max_height ?
1073                       (xsn.alpha > XSN_ALPHA_VISIBLE ? -1 : 0) :
1074                       (xsn.alpha < XSN_ALPHA_DEFAULT ? +1 : 0));
1075
1076   if (xsn_alpha_dx != 0)
1077   {
1078     xsn.alpha += xsn_alpha_dx;
1079
1080     SDLSetAlpha(xsn.bitmap->surface_masked, TRUE, xsn.alpha);
1081
1082     SDLFreeBitmapTextures(xsn.bitmap);
1083     SDLCreateBitmapTextures(xsn.bitmap);
1084   }
1085
1086   BlitToScreenMasked(xsn.bitmap, 0, 0, xsn.area_xsize, xsn.max_height,
1087                      0, xsn.area_ysize - xsn.max_height);
1088
1089   for (i = 0; i < xsn.num_items; i++)
1090   {
1091     int dst_x = xsn.items[i].x;
1092     int dst_y = xsn.items[i].y;
1093     int type = xsn.items[i].type;
1094     int size = xsn_data[type].size;
1095     Bitmap *bitmap = xsn_data[type].bitmap;
1096
1097     BlitToScreenMasked(bitmap, 0, 0, size, size, dst_x, dst_y);
1098   }
1099 }
1100
1101 void DrawTileCursor_MM(int draw_target, boolean tile_cursor_active)
1102 {
1103   if (program.headless)
1104     return;
1105
1106   Bitmap *fade_bitmap;
1107   Bitmap *src_bitmap;
1108   int src_x, src_y;
1109   int dst_x, dst_y;
1110   int graphic = IMG_GLOBAL_TILE_CURSOR;
1111   int frame = 0;
1112   int tilesize = TILESIZE_VAR;
1113   int width = tilesize;
1114   int height = tilesize;
1115
1116   DrawTileCursor_Xsn(draw_target);
1117
1118   if (!tile_cursor.enabled ||
1119       !tile_cursor.active ||
1120       !tile_cursor_active)
1121     return;
1122
1123   if (tile_cursor.moving)
1124   {
1125     int step = TILESIZE_VAR / 4;
1126     int dx = tile_cursor.target_x - tile_cursor.x;
1127     int dy = tile_cursor.target_y - tile_cursor.y;
1128
1129     if (ABS(dx) < step)
1130       tile_cursor.x = tile_cursor.target_x;
1131     else
1132       tile_cursor.x += SIGN(dx) * step;
1133
1134     if (ABS(dy) < step)
1135       tile_cursor.y = tile_cursor.target_y;
1136     else
1137       tile_cursor.y += SIGN(dy) * step;
1138
1139     if (tile_cursor.x == tile_cursor.target_x &&
1140         tile_cursor.y == tile_cursor.target_y)
1141       tile_cursor.moving = FALSE;
1142   }
1143
1144   dst_x = tile_cursor.x;
1145   dst_y = tile_cursor.y;
1146
1147   frame = getGraphicAnimationFrame(graphic, -1);
1148
1149   getSizedGraphicSource(graphic, frame, tilesize, &src_bitmap, &src_x, &src_y);
1150
1151   fade_bitmap =
1152     (draw_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
1153      draw_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
1154
1155   if (draw_target == DRAW_TO_SCREEN)
1156     BlitToScreenMasked(src_bitmap, src_x, src_y, width, height, dst_x, dst_y);
1157   else
1158     BlitBitmapMasked(src_bitmap, fade_bitmap, src_x, src_y, width, height,
1159                      dst_x, dst_y);
1160 }
1161
1162 #if 0
1163 static int REQ_in_range(int x, int y)
1164 {
1165   if (y > DY + 249 && y < DY + 278)
1166   {
1167     if (x > DX + 1 && x < DX + 48)
1168       return 1;
1169     else if (x > DX + 51 && x < DX + 98)
1170       return 2;
1171   }
1172
1173   return 0;
1174 }
1175 #endif
1176
1177 Pixel ReadPixel(DrawBuffer *bitmap, int x, int y)
1178 {
1179   return GetPixel(bitmap, x, y);
1180 }
1181
1182 void SetRGB(unsigned int pixel,
1183             unsigned short red, unsigned short green, unsigned short blue)
1184 {
1185 }
1186
1187 int get_base_element(int element)
1188 {
1189   if (IS_MIRROR(element))
1190     return EL_MIRROR_START;
1191   else if (IS_MIRROR_FIXED(element))
1192     return EL_MIRROR_FIXED_START;
1193   else if (IS_POLAR(element))
1194     return EL_POLAR_START;
1195   else if (IS_POLAR_CROSS(element))
1196     return EL_POLAR_CROSS_START;
1197   else if (IS_BEAMER(element))
1198     return EL_BEAMER_RED_START + BEAMER_NR(element) * 16;
1199   else if (IS_FIBRE_OPTIC(element))
1200     return EL_FIBRE_OPTIC_START + FIBRE_OPTIC_NR(element) * 2;
1201   else if (IS_MCDUFFIN(element))
1202     return EL_MCDUFFIN_START;
1203   else if (IS_LASER(element))
1204     return EL_LASER_START;
1205   else if (IS_RECEIVER(element))
1206     return EL_RECEIVER_START;
1207   else if (IS_DF_MIRROR(element))
1208     return EL_DF_MIRROR_START;
1209   else if (IS_DF_MIRROR_AUTO(element))
1210     return EL_DF_MIRROR_AUTO_START;
1211   else if (IS_PACMAN(element))
1212     return EL_PACMAN_START;
1213   else if (IS_GRID_STEEL(element))
1214     return EL_GRID_STEEL_START;
1215   else if (IS_GRID_WOOD(element))
1216     return EL_GRID_WOOD_START;
1217   else if (IS_GRID_STEEL_FIXED(element))
1218     return EL_GRID_STEEL_FIXED_START;
1219   else if (IS_GRID_WOOD_FIXED(element))
1220     return EL_GRID_WOOD_FIXED_START;
1221   else if (IS_GRID_STEEL_AUTO(element))
1222     return EL_GRID_STEEL_AUTO_START;
1223   else if (IS_GRID_WOOD_AUTO(element))
1224     return EL_GRID_WOOD_AUTO_START;
1225   else if (IS_WALL_STEEL(element))
1226     return EL_WALL_STEEL_START;
1227   else if (IS_WALL_WOOD(element))
1228     return EL_WALL_WOOD_START;
1229   else if (IS_WALL_ICE(element))
1230     return EL_WALL_ICE_START;
1231   else if (IS_WALL_AMOEBA(element))
1232     return EL_WALL_AMOEBA_START;
1233   else if (IS_DF_WALL_STEEL(element))
1234     return EL_DF_WALL_STEEL_START;
1235   else if (IS_DF_WALL_WOOD(element))
1236     return EL_DF_WALL_WOOD_START;
1237   else if (IS_CHAR(element))
1238     return EL_CHAR_START;
1239   else
1240     return element;
1241 }
1242
1243 int get_element_phase(int element)
1244 {
1245   return element - get_base_element(element);
1246 }
1247
1248 int get_num_elements(int element)
1249 {
1250   if (IS_MIRROR(element) ||
1251       IS_POLAR(element) ||
1252       IS_BEAMER(element) ||
1253       IS_DF_MIRROR(element) ||
1254       IS_DF_MIRROR_AUTO(element))
1255     return 16;
1256   else if (IS_GRID_STEEL_FIXED(element) ||
1257            IS_GRID_WOOD_FIXED(element) ||
1258            IS_GRID_STEEL_AUTO(element) ||
1259            IS_GRID_WOOD_AUTO(element))
1260     return 8;
1261   else if (IS_MIRROR_FIXED(element) ||
1262            IS_POLAR_CROSS(element) ||
1263            IS_MCDUFFIN(element) ||
1264            IS_LASER(element) ||
1265            IS_RECEIVER(element) ||
1266            IS_PACMAN(element) ||
1267            IS_GRID_STEEL(element) ||
1268            IS_GRID_WOOD(element))
1269     return 4;
1270   else
1271     return 1;
1272 }
1273
1274 int get_rotated_element(int element, int step)
1275 {
1276   int base_element = get_base_element(element);
1277   int num_elements = get_num_elements(element);
1278   int element_phase = element - base_element;
1279
1280   return base_element + (element_phase + step + num_elements) % num_elements;
1281 }
1282
1283 static int map_element(int element)
1284 {
1285   switch (element)
1286   {
1287     case EL_WALL_STEEL:         return EL_STEEL_WALL;
1288     case EL_WALL_WOOD:          return EL_WOODEN_WALL;
1289     case EL_WALL_ICE:           return EL_ICE_WALL;
1290     case EL_WALL_AMOEBA:        return EL_AMOEBA_WALL;
1291     case EL_DF_WALL_STEEL:      return EL_DF_STEEL_WALL;
1292     case EL_DF_WALL_WOOD:       return EL_DF_WOODEN_WALL;
1293
1294     default:                    return element;
1295   }
1296 }
1297
1298 int unmap_element(int element)
1299 {
1300   switch (element)
1301   {
1302     case EL_STEEL_WALL:         return EL_WALL_STEEL;
1303     case EL_WOODEN_WALL:        return EL_WALL_WOOD;
1304     case EL_ICE_WALL:           return EL_WALL_ICE;
1305     case EL_AMOEBA_WALL:        return EL_WALL_AMOEBA;
1306     case EL_DF_STEEL_WALL:      return EL_DF_WALL_STEEL;
1307     case EL_DF_WOODEN_WALL:     return EL_DF_WALL_WOOD;
1308
1309     default:                    return element;
1310   }
1311 }
1312
1313 int el2gfx(int element)
1314 {
1315   return el2img_mm(map_element(element));
1316 }
1317
1318 int el_act2gfx(int element, int action)
1319 {
1320   return el_act2img_mm(map_element(element), action);
1321 }
1322
1323 void RedrawPlayfield_MM(void)
1324 {
1325   DrawLevel_MM();
1326   DrawLaser_MM();
1327 }
1328
1329 void BlitScreenToBitmap_MM(Bitmap *target_bitmap)
1330 {
1331   BlitBitmap(drawto_field, target_bitmap,
1332              REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE, REAL_SX, REAL_SY);
1333 }