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