fixed bug with CE triggered global animations treated as "off-screen"
[rocksndiamonds.git] / src / anim.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // anim.c
10 // ============================================================================
11
12 #include "libgame/libgame.h"
13
14 #include "anim.h"
15 #include "main.h"
16 #include "tools.h"
17 #include "files.h"
18 #include "events.h"
19 #include "screens.h"
20 #include "tape.h"
21
22
23 #define DEBUG_ANIM_DELAY                0
24 #define DEBUG_ANIM_EVENTS               0
25
26
27 // values for global toon animation definition
28 #define NUM_GLOBAL_TOON_ANIMS           1
29 #define NUM_GLOBAL_TOON_PARTS           MAX_NUM_TOONS
30
31 // values for global animation definition (including toons)
32 #define NUM_GLOBAL_ANIMS_AND_TOONS      (NUM_GLOBAL_ANIMS +             \
33                                          NUM_GLOBAL_TOON_ANIMS)
34 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL,  \
35                                             NUM_GLOBAL_TOON_PARTS)
36
37 #define ANIM_CLASS_BIT_TITLE_INITIAL    0
38 #define ANIM_CLASS_BIT_TITLE            1
39 #define ANIM_CLASS_BIT_MAIN             2
40 #define ANIM_CLASS_BIT_NAMES            3
41 #define ANIM_CLASS_BIT_SCORES           4
42 #define ANIM_CLASS_BIT_SCORESONLY       5
43 #define ANIM_CLASS_BIT_SUBMENU          6
44 #define ANIM_CLASS_BIT_MENU             7
45 #define ANIM_CLASS_BIT_TOONS            8
46 #define ANIM_CLASS_BIT_NO_TITLE         9
47
48 #define NUM_ANIM_CLASSES                10
49
50 #define ANIM_CLASS_NONE                 0
51 #define ANIM_CLASS_TITLE_INITIAL        (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
52 #define ANIM_CLASS_TITLE                (1 << ANIM_CLASS_BIT_TITLE)
53 #define ANIM_CLASS_MAIN                 (1 << ANIM_CLASS_BIT_MAIN)
54 #define ANIM_CLASS_NAMES                (1 << ANIM_CLASS_BIT_NAMES)
55 #define ANIM_CLASS_SCORES               (1 << ANIM_CLASS_BIT_SCORES)
56 #define ANIM_CLASS_SCORESONLY           (1 << ANIM_CLASS_BIT_SCORESONLY)
57 #define ANIM_CLASS_SUBMENU              (1 << ANIM_CLASS_BIT_SUBMENU)
58 #define ANIM_CLASS_MENU                 (1 << ANIM_CLASS_BIT_MENU)
59 #define ANIM_CLASS_TOONS                (1 << ANIM_CLASS_BIT_TOONS)
60 #define ANIM_CLASS_NO_TITLE             (1 << ANIM_CLASS_BIT_NO_TITLE)
61
62 #define ANIM_CLASS_TOONS_SCORES         (ANIM_CLASS_TOONS       |       \
63                                          ANIM_CLASS_SCORES      |       \
64                                          ANIM_CLASS_NO_TITLE)
65
66 #define ANIM_CLASS_TOONS_SCORESONLY     (ANIM_CLASS_TOONS       |       \
67                                          ANIM_CLASS_SCORES      |       \
68                                          ANIM_CLASS_SCORESONLY  |       \
69                                          ANIM_CLASS_NO_TITLE)
70
71 #define ANIM_CLASS_TOONS_MENU_MAIN      (ANIM_CLASS_TOONS       |       \
72                                          ANIM_CLASS_MENU        |       \
73                                          ANIM_CLASS_MAIN        |       \
74                                          ANIM_CLASS_NO_TITLE)
75
76 #define ANIM_CLASS_TOONS_MENU_SUBMENU   (ANIM_CLASS_TOONS       |       \
77                                          ANIM_CLASS_MENU        |       \
78                                          ANIM_CLASS_SUBMENU     |       \
79                                          ANIM_CLASS_NO_TITLE)
80
81 #define ANIM_CLASS_TOONS_MENU_SUBMENU_2 (ANIM_CLASS_TOONS       |       \
82                                          ANIM_CLASS_MENU        |       \
83                                          ANIM_CLASS_SUBMENU     |       \
84                                          ANIM_CLASS_NAMES       |       \
85                                          ANIM_CLASS_NO_TITLE)
86
87 // values for global animation states
88 #define ANIM_STATE_INACTIVE             0
89 #define ANIM_STATE_RESTART              (1 << 0)
90 #define ANIM_STATE_WAITING              (1 << 1)
91 #define ANIM_STATE_RUNNING              (1 << 2)
92
93 // values for global animation control
94 #define ANIM_NO_ACTION                  0
95 #define ANIM_START                      1
96 #define ANIM_CONTINUE                   2
97 #define ANIM_STOP                       3
98
99
100 struct GlobalAnimPartControlInfo
101 {
102   int old_nr;           // position before mapping animation parts linearly
103   int old_anim_nr;      // position before mapping animations linearly
104
105   int nr;
106   int anim_nr;
107   int mode_nr;
108
109   boolean is_base;      // animation part is base/main/default animation part
110
111   int sound;
112   int music;
113   int graphic;
114
115   struct GraphicInfo graphic_info;
116   struct GraphicInfo control_info;
117
118   boolean class_playfield;
119
120   int viewport_x;
121   int viewport_y;
122   int viewport_width;
123   int viewport_height;
124
125   int x, y;
126   int step_xoffset, step_yoffset;
127
128   int tile_x, tile_y;
129   int tile_xoffset, tile_yoffset;
130
131   unsigned int initial_anim_sync_frame;
132   unsigned int anim_random_frame;
133
134   DelayCounter step_delay;
135
136   int init_delay_counter;
137   int anim_delay_counter;
138   int post_delay_counter;
139
140   boolean init_event_state;
141   boolean anim_event_state;
142
143   boolean triggered;
144   boolean clickable;
145   boolean clicked;
146
147   int drawing_stage;
148
149   int state;
150   int last_anim_status;
151 };
152
153 struct GlobalAnimMainControlInfo
154 {
155   struct GlobalAnimPartControlInfo base;
156   struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
157
158   int nr;
159   int mode_nr;
160
161   struct GraphicInfo control_info;
162
163   int num_parts;        // number of animation parts, but without base part
164   int num_parts_all;    // number of animation parts, including base part
165   int part_counter;
166   int active_part_nr;
167
168   boolean has_base;     // animation has base/main/default animation part
169
170   int last_x, last_y;
171
172   int init_delay_counter;
173
174   int state;
175
176   int last_state, last_active_part_nr;
177 };
178
179 struct GlobalAnimControlInfo
180 {
181   struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
182
183   int nr;
184   int num_anims;
185 };
186
187 struct GameModeAnimClass
188 {
189   int game_mode;
190   int class;
191 } game_mode_anim_classes_list[] =
192 {
193   { GAME_MODE_TITLE_INITIAL_1,          ANIM_CLASS_TITLE_INITIAL        },
194   { GAME_MODE_TITLE_INITIAL_2,          ANIM_CLASS_TITLE_INITIAL        },
195   { GAME_MODE_TITLE_INITIAL_3,          ANIM_CLASS_TITLE_INITIAL        },
196   { GAME_MODE_TITLE_INITIAL_4,          ANIM_CLASS_TITLE_INITIAL        },
197   { GAME_MODE_TITLE_INITIAL_5,          ANIM_CLASS_TITLE_INITIAL        },
198   { GAME_MODE_TITLE_1,                  ANIM_CLASS_TITLE                },
199   { GAME_MODE_TITLE_2,                  ANIM_CLASS_TITLE                },
200   { GAME_MODE_TITLE_3,                  ANIM_CLASS_TITLE                },
201   { GAME_MODE_TITLE_4,                  ANIM_CLASS_TITLE                },
202   { GAME_MODE_TITLE_5,                  ANIM_CLASS_TITLE                },
203   { GAME_MODE_NAMES,                    ANIM_CLASS_TOONS_MENU_SUBMENU   },
204   { GAME_MODE_LEVELS,                   ANIM_CLASS_TOONS_MENU_SUBMENU   },
205   { GAME_MODE_LEVELNR,                  ANIM_CLASS_TOONS_MENU_SUBMENU   },
206   { GAME_MODE_INFO,                     ANIM_CLASS_TOONS_MENU_SUBMENU   },
207   { GAME_MODE_SETUP,                    ANIM_CLASS_TOONS_MENU_SUBMENU   },
208   { GAME_MODE_PSEUDO_NAMESONLY,         ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
209   { GAME_MODE_PSEUDO_TYPENAMES,         ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
210   { GAME_MODE_PSEUDO_MAINONLY,          ANIM_CLASS_TOONS_MENU_MAIN      },
211   { GAME_MODE_PSEUDO_TYPENAME,          ANIM_CLASS_TOONS_MENU_MAIN      },
212   { GAME_MODE_PSEUDO_SCORESOLD,         ANIM_CLASS_TOONS_SCORESONLY     },
213   { GAME_MODE_PSEUDO_SCORESNEW,         ANIM_CLASS_TOONS_SCORESONLY     },
214   { GAME_MODE_SCOREINFO,                ANIM_CLASS_TOONS_SCORES         },
215   { GAME_MODE_EDITOR,                   ANIM_CLASS_NO_TITLE             },
216   { GAME_MODE_PLAYING,                  ANIM_CLASS_NO_TITLE             },
217
218   { -1,                                 -1                              }
219 };
220
221 struct AnimClassGameMode
222 {
223   int class_bit;
224   int game_mode;
225 } anim_class_game_modes_list[] =
226 {
227   { ANIM_CLASS_BIT_TITLE_INITIAL,       GAME_MODE_TITLE_INITIAL         },
228   { ANIM_CLASS_BIT_TITLE,               GAME_MODE_TITLE                 },
229   { ANIM_CLASS_BIT_MAIN,                GAME_MODE_MAIN                  },
230   { ANIM_CLASS_BIT_NAMES,               GAME_MODE_NAMES                 },
231   { ANIM_CLASS_BIT_SCORES,              GAME_MODE_SCORES                },
232   { ANIM_CLASS_BIT_SCORESONLY,          GAME_MODE_PSEUDO_SCORESONLY     },
233   { ANIM_CLASS_BIT_SUBMENU,             GAME_MODE_PSEUDO_SUBMENU        },
234   { ANIM_CLASS_BIT_MENU,                GAME_MODE_PSEUDO_MENU           },
235   { ANIM_CLASS_BIT_TOONS,               GAME_MODE_PSEUDO_TOONS          },
236   { ANIM_CLASS_BIT_NO_TITLE,            GAME_MODE_PSEUDO_NO_TITLE       },
237
238   { -1,                                 -1                              }
239 };
240
241 // forward declaration for internal use
242 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *, int);
243 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *);
244 static void HandleGlobalAnim(int, int);
245 static void DoAnimationExt(void);
246 static void ResetGlobalAnim_Clickable(void);
247 static void ResetGlobalAnim_Clicked(void);
248
249 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
250
251 static unsigned int anim_sync_frame = 0;
252
253 static int game_mode_anim_classes[NUM_GAME_MODES];
254 static int anim_class_game_modes[NUM_ANIM_CLASSES];
255
256 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
257 static int anim_status_last = GAME_MODE_DEFAULT;
258 static int anim_classes_last = ANIM_CLASS_NONE;
259
260 static boolean drawing_to_fading_buffer = FALSE;
261
262 static boolean handle_click = FALSE;
263
264
265 // ============================================================================
266 // generic animation frame calculation
267 // ============================================================================
268
269 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
270                       int sync_frame)
271 {
272   int frame = 0;
273
274   if (delay < 1)                        // delay must be at least 1
275     delay = 1;
276
277   sync_frame += start_frame * delay;
278
279   if (mode & ANIM_LOOP)                 // looping animation
280   {
281     frame = (sync_frame % (delay * num_frames)) / delay;
282   }
283   else if (mode & ANIM_LINEAR)          // linear (non-looping) animation
284   {
285     frame = sync_frame / delay;
286
287     if (frame > num_frames - 1)
288       frame = num_frames - 1;
289   }
290   else if (mode & ANIM_PINGPONG)        // oscillate (border frames once)
291   {
292     int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
293
294     frame = (sync_frame % (delay * max_anim_frames)) / delay;
295     frame = (frame < num_frames ? frame : max_anim_frames - frame);
296   }
297   else if (mode & ANIM_PINGPONG2)       // oscillate (border frames twice)
298   {
299     int max_anim_frames = 2 * num_frames;
300
301     frame = (sync_frame % (delay * max_anim_frames)) / delay;
302     frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
303   }
304   else if (mode & ANIM_RANDOM)          // play frames in random order
305   {
306     // note: expect different frames for the same delay cycle!
307
308     if (gfx.anim_random_frame < 0)
309       frame = GetSimpleRandom(num_frames);
310     else
311       frame = gfx.anim_random_frame % num_frames;
312   }
313   else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
314   {
315     frame = sync_frame % num_frames;
316   }
317
318   if (mode & ANIM_REVERSE)              // use reverse animation direction
319     frame = num_frames - frame - 1;
320
321   return frame;
322 }
323
324
325 // ============================================================================
326 // global animation functions
327 // ============================================================================
328
329 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
330 {
331   struct GraphicInfo *c = &anim->control_info;
332   int last_anim_random_frame = gfx.anim_random_frame;
333   int part_nr;
334
335   gfx.anim_random_frame = -1;   // (use simple, ad-hoc random numbers)
336
337   part_nr = getAnimationFrame(anim->num_parts, 1,
338                               c->anim_mode, c->anim_start_frame,
339                               anim->part_counter);
340
341   gfx.anim_random_frame = last_anim_random_frame;
342
343   return part_nr;
344 }
345
346 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
347 {
348   const struct GlobalAnimPartControlInfo *o1 =
349     (struct GlobalAnimPartControlInfo *)obj1;
350   const struct GlobalAnimPartControlInfo *o2 =
351     (struct GlobalAnimPartControlInfo *)obj2;
352   int compare_result;
353
354   if (o1->control_info.draw_order != o2->control_info.draw_order)
355     compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
356   else
357     compare_result = o1->nr - o2->nr;
358
359   return compare_result;
360 }
361
362 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
363 {
364   const struct GlobalAnimMainControlInfo *o1 =
365     (struct GlobalAnimMainControlInfo *)obj1;
366   const struct GlobalAnimMainControlInfo *o2 =
367     (struct GlobalAnimMainControlInfo *)obj2;
368   int compare_result;
369
370   if (o1->control_info.draw_order != o2->control_info.draw_order)
371     compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
372   else
373     compare_result = o1->nr - o2->nr;
374
375   return compare_result;
376 }
377
378 static boolean isPausedOnPlayfield(struct GlobalAnimPartControlInfo *part)
379 {
380   // only pause playfield animations when playing
381   if (game_status != GAME_MODE_PLAYING)
382     return FALSE;
383
384   // do not pause animations when game ended (and engine is running)
385   if (checkGameEnded())
386     return FALSE;
387
388   // only pause animations on playfield
389   if (!part->class_playfield)
390     return FALSE;
391
392   // only pause animations when engine is paused or request dialog is open(ing)
393   if (!tape.pausing && !game.request_active_or_moving)
394     return FALSE;
395
396   return TRUE;
397 }
398
399 static void InitToonControls(void)
400 {
401   int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
402   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
403   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
404   int mode_nr, anim_nr, part_nr;
405   int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
406   int num_toons = MAX_NUM_TOONS;
407   int i;
408
409   if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
410     num_toons = global.num_toons;
411
412   mode_nr = mode_nr_toons;
413   anim_nr = ctrl->num_anims;
414
415   anim->nr = anim_nr;
416   anim->mode_nr = mode_nr;
417   anim->control_info = graphic_info[control];
418
419   anim->num_parts = 0;
420   anim->num_parts_all = 0;
421   anim->part_counter = 0;
422   anim->active_part_nr = 0;
423
424   anim->has_base = FALSE;
425
426   anim->last_x = POS_OFFSCREEN;
427   anim->last_y = POS_OFFSCREEN;
428
429   anim->init_delay_counter = 0;
430
431   anim->state = ANIM_STATE_INACTIVE;
432
433   part_nr = 0;
434
435   for (i = 0; i < num_toons; i++)
436   {
437     struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
438     int sound = SND_UNDEFINED;
439     int music = MUS_UNDEFINED;
440     int graphic = IMG_TOON_1 + i;
441
442     control = graphic;
443
444     part->nr = part_nr;
445     part->anim_nr = anim_nr;
446     part->mode_nr = mode_nr;
447
448     part->is_base = FALSE;
449
450     part->sound = sound;
451     part->music = music;
452     part->graphic = graphic;
453
454     part->graphic_info = graphic_info[graphic];
455     part->control_info = graphic_info[control];
456
457     part->graphic_info.anim_delay *= part->graphic_info.step_delay;
458
459     part->control_info.init_delay_fixed = 0;
460     part->control_info.init_delay_random = 150;
461
462     part->control_info.x = ARG_UNDEFINED_VALUE;
463     part->control_info.y = ARG_UNDEFINED_VALUE;
464
465     part->initial_anim_sync_frame = 0;
466     part->anim_random_frame = -1;
467
468     part->step_delay.count = 0;
469     part->step_delay.value = graphic_info[control].step_delay;
470
471     part->state = ANIM_STATE_INACTIVE;
472     part->last_anim_status = -1;
473
474     anim->num_parts++;
475     anim->num_parts_all++;
476
477     part_nr++;
478   }
479
480   ctrl->num_anims++;
481 }
482
483 static void InitGlobalAnimControls(void)
484 {
485   int i, m, a, p;
486   int mode_nr, anim_nr, part_nr;
487   int sound, music, graphic, control;
488
489   anim_sync_frame = 0;
490
491   for (m = 0; m < NUM_GAME_MODES; m++)
492   {
493     mode_nr = m;
494
495     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
496
497     ctrl->nr = mode_nr;
498     ctrl->num_anims = 0;
499
500     anim_nr = 0;
501
502     for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
503     {
504       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
505       int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
506
507       control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
508
509       // if no base animation parameters defined, use default values
510       if (control == IMG_UNDEFINED)
511         control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
512
513       anim->nr = anim_nr;
514       anim->mode_nr = mode_nr;
515       anim->control_info = graphic_info[control];
516
517       anim->num_parts = 0;
518       anim->num_parts_all = 0;
519       anim->part_counter = 0;
520       anim->active_part_nr = 0;
521
522       anim->has_base = FALSE;
523
524       anim->last_x = POS_OFFSCREEN;
525       anim->last_y = POS_OFFSCREEN;
526
527       anim->init_delay_counter = 0;
528
529       anim->state = ANIM_STATE_INACTIVE;
530
531       part_nr = 0;
532
533       for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
534       {
535         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
536
537         sound   = global_anim_info[a].sound[p][m];
538         music   = global_anim_info[a].music[p][m];
539         graphic = global_anim_info[a].graphic[p][m];
540         control = global_anim_info[ctrl_id].graphic[p][m];
541
542         if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
543             control == IMG_UNDEFINED)
544           continue;
545
546 #if 0
547         Debug("anim:InitGlobalAnimControls",
548               "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
549               m, a, p, mode_nr, anim_nr, part_nr, control);
550 #endif
551
552 #if 0
553         Debug("anim:InitGlobalAnimControls",
554               "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
555               m, a, p, mode_nr, anim_nr, part_nr, sound);
556 #endif
557
558         part->old_nr = p;
559         part->old_anim_nr = a;
560
561         part->nr = part_nr;
562         part->anim_nr = anim_nr;
563         part->mode_nr = mode_nr;
564
565         part->sound = sound;
566         part->music = music;
567         part->graphic = graphic;
568
569         part->graphic_info = graphic_info[graphic];
570         part->control_info = graphic_info[control];
571
572         part->initial_anim_sync_frame = 0;
573         part->anim_random_frame = -1;
574
575         part->step_delay.count = 0;
576         part->step_delay.value = graphic_info[control].step_delay;
577
578         part->state = ANIM_STATE_INACTIVE;
579         part->last_anim_status = -1;
580
581         anim->num_parts_all++;
582
583         if (p < GLOBAL_ANIM_ID_PART_BASE)
584         {
585           part->is_base = FALSE;
586
587           anim->num_parts++;
588           part_nr++;
589         }
590         else
591         {
592           part->is_base = TRUE;
593
594           anim->base = *part;
595           anim->has_base = TRUE;
596         }
597
598         // apply special settings for pointer-style animations
599         if (part->control_info.class == get_hash_from_key("pointer"))
600         {
601           // force animation to be on top (must set anim and part control)
602           if (anim->control_info.draw_order == 0)
603             anim->control_info.draw_order = 1000000;
604           if (part->control_info.draw_order == 0)
605             part->control_info.draw_order = 1000000;
606
607           // force animation to pass-through clicks (must set part control)
608           if (part->control_info.style == STYLE_DEFAULT)
609             part->control_info.style |= STYLE_PASSTHROUGH;
610         }
611       }
612
613       if (anim->num_parts > 0 || anim->has_base)
614       {
615         ctrl->num_anims++;
616         anim_nr++;
617       }
618     }
619   }
620
621   InitToonControls();
622
623   // sort all animations according to draw_order and animation number
624   for (m = 0; m < NUM_GAME_MODES; m++)
625   {
626     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
627
628     // sort all main animations for this game mode
629     qsort(ctrl->anim, ctrl->num_anims,
630           sizeof(struct GlobalAnimMainControlInfo),
631           compareGlobalAnimMainControlInfo);
632
633     for (a = 0; a < ctrl->num_anims; a++)
634     {
635       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
636
637       // sort all animation parts for this main animation
638       qsort(anim->part, anim->num_parts,
639             sizeof(struct GlobalAnimPartControlInfo),
640             compareGlobalAnimPartControlInfo);
641     }
642   }
643
644   for (i = 0; i < NUM_GAME_MODES; i++)
645     game_mode_anim_classes[i] = ANIM_CLASS_NONE;
646   for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
647     game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
648       game_mode_anim_classes_list[i].class;
649
650   for (i = 0; i < NUM_ANIM_CLASSES; i++)
651     anim_class_game_modes[i] = GAME_MODE_DEFAULT;
652   for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
653     anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
654       anim_class_game_modes_list[i].game_mode;
655
656   anim_status_last_before_fading = GAME_MODE_LOADING;
657   anim_status_last = GAME_MODE_LOADING;
658   anim_classes_last = ANIM_CLASS_NONE;
659 }
660
661 static void SetGlobalAnimEventsForCustomElements(int list_pos)
662 {
663   int num_events = GetGlobalAnimEventValueCount(list_pos);
664   int i;
665
666   for (i = 0; i < num_events; i++)
667   {
668     int event = GetGlobalAnimEventValue(list_pos, i);
669
670     if (event & ANIM_EVENT_CE_CHANGE)
671     {
672       int nr = (event >> ANIM_EVENT_CE_BIT) & 0xff;
673
674       if (nr >= 0 && nr < NUM_CUSTOM_ELEMENTS)
675         element_info[EL_CUSTOM_START + nr].has_anim_event = TRUE;
676     }
677   }
678 }
679
680 void InitGlobalAnimEventsForCustomElements(void)
681 {
682   int m, a, p;
683   int control;
684
685   // custom element events for global animations only relevant while playing
686   m = GAME_MODE_PLAYING;
687
688   for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
689   {
690     int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
691
692     control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
693
694     // if no base animation parameters defined, use default values
695     if (control == IMG_UNDEFINED)
696       control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
697
698     SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
699     SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
700
701     for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
702     {
703       control = global_anim_info[ctrl_id].graphic[p][m];
704
705       if (control == IMG_UNDEFINED)
706         continue;
707
708       SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
709       SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
710     }
711   }
712 }
713
714 void InitGlobalAnimations(void)
715 {
716   InitGlobalAnimControls();
717 }
718
719 static void BlitGlobalAnimation(struct GlobalAnimPartControlInfo *part,
720                                 Bitmap *src_bitmap, int src_x0, int src_y0,
721                                 int drawing_target)
722 {
723   struct GraphicInfo *g = &part->graphic_info;
724   struct GraphicInfo *c = &part->control_info;
725   void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
726     (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
727   void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
728     (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
729   Bitmap *fade_bitmap =
730     (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
731      drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
732   int x, y;
733
734   for (y = 0; y < c->stacked_yfactor; y++)
735   {
736     for (x = 0; x < c->stacked_xfactor; x++)
737     {
738       int src_x = src_x0;
739       int src_y = src_y0;
740       int dst_x = part->x + x * (g->width  + c->stacked_xoffset);
741       int dst_y = part->y + y * (g->height + c->stacked_yoffset);
742       int cut_x = 0;
743       int cut_y = 0;
744       int width  = g->width;
745       int height = g->height;
746
747       if (dst_x < 0)
748       {
749         width += dst_x;
750         cut_x = -dst_x;
751         dst_x = 0;
752       }
753       else if (dst_x > part->viewport_width - g->width)
754       {
755         width -= (dst_x - (part->viewport_width - g->width));
756       }
757
758       if (dst_y < 0)
759       {
760         height += dst_y;
761         cut_y  = -dst_y;
762         dst_y = 0;
763       }
764       else if (dst_y > part->viewport_height - g->height)
765       {
766         height -= (dst_y - (part->viewport_height - g->height));
767       }
768
769       if (width <= 0 || height <= 0)
770         continue;
771
772       src_x += cut_x;
773       src_y += cut_y;
774
775       dst_x += part->viewport_x;
776       dst_y += part->viewport_y;
777
778       if (drawing_target == DRAW_TO_SCREEN)
779         blit_screen(src_bitmap, src_x, src_y, width, height,
780                     dst_x, dst_y);
781       else
782         blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
783                     dst_x, dst_y);
784     }
785   }
786 }
787
788 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
789 {
790   int game_mode_anim_action[NUM_GAME_MODES];
791   int mode_nr;
792
793   if (!setup.toons)
794     return;
795
796   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
797       drawing_target == DRAW_TO_SCREEN)
798     DoAnimationExt();
799
800   // always start with reliable default values (no animation actions)
801   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
802     game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
803
804   if (global.anim_status != anim_status_last)
805   {
806     boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
807     boolean after_fading  = (anim_status_last   == GAME_MODE_PSEUDO_FADING);
808     int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
809     int i;
810
811     if (drawing_target == DRAW_TO_FADE_TARGET)
812       after_fading = TRUE;
813
814     // special case: changing from/to these screens is done without fading
815     if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME  ||
816         global.anim_status == GAME_MODE_PSEUDO_TYPENAMES ||
817         anim_status_last   == GAME_MODE_PSEUDO_TYPENAME  ||
818         anim_status_last   == GAME_MODE_PSEUDO_TYPENAMES)
819       after_fading = TRUE;
820
821     // ---------- part 1 ------------------------------------------------------
822     // start or stop global animations by change of game mode
823     // (special handling of animations for "current screen" and "all screens")
824
825     if (global.anim_status_next != anim_status_last_before_fading)
826     {
827       // stop animations for last screen before fading to new screen
828       game_mode_anim_action[anim_status_last] = ANIM_STOP;
829
830       // start animations for current screen after fading to new screen
831       game_mode_anim_action[global.anim_status] = ANIM_START;
832     }
833
834     // start animations for all screens after loading new artwork set
835     if (anim_status_last == GAME_MODE_LOADING)
836       game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
837
838     // ---------- part 2 ------------------------------------------------------
839     // start or stop global animations by change of animation class
840     // (generic handling of animations for "class of screens")
841
842     for (i = 0; i < NUM_ANIM_CLASSES; i++)
843     {
844       int anim_class_check = (1 << i);
845       int anim_class_game_mode = anim_class_game_modes[i];
846       int anim_class_last = anim_classes_last & anim_class_check;
847       int anim_class_next = anim_classes_next & anim_class_check;
848
849       // stop animations for changed screen class before fading to new screen
850       if (before_fading && anim_class_last && !anim_class_next)
851         game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
852
853       // start animations for changed screen class after fading to new screen
854       if (after_fading && !anim_class_last && anim_class_next)
855         game_mode_anim_action[anim_class_game_mode] = ANIM_START;
856     }
857
858     if (drawing_target == DRAW_TO_SCREEN)
859     {
860       if (after_fading)
861       {
862         anim_classes_last = anim_classes_next;
863         anim_status_last_before_fading = global.anim_status;
864       }
865
866       anim_status_last = global.anim_status;
867
868       // start or stop animations determined to be started or stopped above
869       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
870         if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
871           HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
872     }
873     else if (drawing_target == DRAW_TO_FADE_TARGET)
874     {
875       drawing_to_fading_buffer = TRUE;
876
877       // start animations determined to be (temporary) started above
878       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
879         if (game_mode_anim_action[mode_nr] == ANIM_START)
880           HandleGlobalAnim(ANIM_START, mode_nr);
881     }
882   }
883
884   // when restarting global animations, do not redraw them, but stop here
885   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_RESTART)
886     return;
887
888   if (global.anim_status == GAME_MODE_LOADING)
889     return;
890
891   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
892   {
893     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
894     int anim_nr;
895
896     // when preparing source fading buffer, only draw animations to be stopped
897     if (drawing_target == DRAW_TO_FADE_SOURCE &&
898         game_mode_anim_action[mode_nr] != ANIM_STOP)
899       continue;
900
901     // when preparing target fading buffer, only draw animations to be started
902     if (drawing_target == DRAW_TO_FADE_TARGET &&
903         game_mode_anim_action[mode_nr] != ANIM_START)
904       continue;
905
906 #if 0
907     if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
908         mode_nr != game_status)
909       continue;
910 #endif
911
912     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
913     {
914       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
915       struct GraphicInfo *c = &anim->control_info;
916       int part_first, part_last;
917       int part_nr;
918
919       if (!(anim->state & ANIM_STATE_RUNNING))
920         continue;
921
922       part_first = part_last = anim->active_part_nr;
923
924       if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
925       {
926         int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
927
928         part_first = 0;
929         part_last = num_parts - 1;
930       }
931
932       for (part_nr = part_first; part_nr <= part_last; part_nr++)
933       {
934         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
935         struct GraphicInfo *g = &part->graphic_info;
936         Bitmap *src_bitmap;
937         int src_x, src_y;
938         int sync_frame;
939         int frame;
940         int last_anim_random_frame = gfx.anim_random_frame;
941
942         if (!(part->state & ANIM_STATE_RUNNING))
943           continue;
944
945         if (part->drawing_stage != drawing_stage)
946           continue;
947
948         // if game is paused, also pause playfield animations
949         if (isPausedOnPlayfield(part))
950           part->initial_anim_sync_frame++;
951
952         sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
953
954         // re-initialize random animation frame after animation delay
955         if (g->anim_mode == ANIM_RANDOM &&
956             sync_frame % g->anim_delay == 0 &&
957             sync_frame > 0)
958           part->anim_random_frame = GetSimpleRandom(g->anim_frames);
959
960         gfx.anim_random_frame = part->anim_random_frame;
961
962         frame = getAnimationFrame(g->anim_frames, g->anim_delay,
963                                   g->anim_mode, g->anim_start_frame,
964                                   sync_frame);
965
966         gfx.anim_random_frame = last_anim_random_frame;
967
968         getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
969                                    &src_x, &src_y);
970
971         BlitGlobalAnimation(part, src_bitmap, src_x, src_y, drawing_target);
972       }
973     }
974   }
975
976   if (drawing_target == DRAW_TO_FADE_TARGET)
977   {
978     // stop animations determined to be (temporary) started above
979     for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
980       if (game_mode_anim_action[mode_nr] == ANIM_START)
981         HandleGlobalAnim(ANIM_STOP, mode_nr);
982
983     drawing_to_fading_buffer = FALSE;
984   }
985 }
986
987 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
988 {
989   int last_cursor_mode_override = gfx.cursor_mode_override;
990
991   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
992   {
993     ResetGlobalAnim_Clickable();
994
995     gfx.cursor_mode_override = CURSOR_UNDEFINED;
996   }
997
998   DrawGlobalAnimationsExt(drawing_target, drawing_stage);
999
1000   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
1001   {
1002     ResetGlobalAnim_Clicked();
1003   }
1004
1005   if (gfx.cursor_mode_override != last_cursor_mode_override)
1006     SetMouseCursor(gfx.cursor_mode);
1007 }
1008
1009 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
1010 {
1011   int viewport_x;
1012   int viewport_y;
1013   int viewport_width;
1014   int viewport_height;
1015   boolean changed = FALSE;
1016
1017   if (part->last_anim_status == global.anim_status &&
1018       part->control_info.class != get_hash_from_key("pointer"))
1019     return FALSE;
1020
1021   part->last_anim_status = global.anim_status;
1022
1023   part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
1024
1025   part->class_playfield = FALSE;
1026
1027   if (part->control_info.class == get_hash_from_key("window") ||
1028       part->control_info.class == get_hash_from_key("border"))
1029   {
1030     viewport_x = 0;
1031     viewport_y = 0;
1032     viewport_width  = WIN_XSIZE;
1033     viewport_height = WIN_YSIZE;
1034
1035     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
1036   }
1037   else if (part->control_info.class == get_hash_from_key("pointer"))
1038   {
1039     int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
1040     int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
1041
1042     // prevent displaying off-screen custom mouse cursor in upper left corner
1043     if (gfx.mouse_x == POS_OFFSCREEN &&
1044         gfx.mouse_y == POS_OFFSCREEN)
1045       mx = my = POS_OFFSCREEN;
1046
1047     viewport_x = mx - part->control_info.x;
1048     viewport_y = my - part->control_info.y;
1049     viewport_width  = part->graphic_info.width;
1050     viewport_height = part->graphic_info.height;
1051
1052     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_3;
1053
1054     // do not use global animation mouse pointer when reloading artwork
1055     if (global.anim_status != GAME_MODE_LOADING)
1056       gfx.cursor_mode_override = CURSOR_NONE;
1057   }
1058   else if (part->control_info.class == get_hash_from_key("door_1"))
1059   {
1060     viewport_x = DX;
1061     viewport_y = DY;
1062     viewport_width  = DXSIZE;
1063     viewport_height = DYSIZE;
1064   }
1065   else if (part->control_info.class == get_hash_from_key("door_2"))
1066   {
1067     if (part->mode_nr == GAME_MODE_EDITOR)
1068     {
1069       viewport_x = EX;
1070       viewport_y = EY;
1071       viewport_width  = EXSIZE;
1072       viewport_height = EYSIZE;
1073     }
1074     else
1075     {
1076       viewport_x = VX;
1077       viewport_y = VY;
1078       viewport_width  = VXSIZE;
1079       viewport_height = VYSIZE;
1080     }
1081   }
1082   else          // default: "playfield"
1083   {
1084     viewport_x = REAL_SX;
1085     viewport_y = REAL_SY;
1086     viewport_width  = FULL_SXSIZE;
1087     viewport_height = FULL_SYSIZE;
1088
1089     part->class_playfield = TRUE;
1090   }
1091
1092   if (viewport_x != part->viewport_x ||
1093       viewport_y != part->viewport_y ||
1094       viewport_width  != part->viewport_width ||
1095       viewport_height != part->viewport_height)
1096   {
1097     part->viewport_x = viewport_x;
1098     part->viewport_y = viewport_y;
1099     part->viewport_width  = viewport_width;
1100     part->viewport_height = viewport_height;
1101
1102     if (part->control_info.class != get_hash_from_key("pointer"))
1103       changed = TRUE;
1104   }
1105
1106   return changed;
1107 }
1108
1109 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1110 {
1111   int sound = part->sound;
1112
1113   if (sound == SND_UNDEFINED)
1114     return;
1115
1116   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
1117       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
1118     return;
1119
1120   // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
1121   if (IS_LOOP_SOUND(sound))
1122     PlaySoundLoop(sound);
1123   else
1124     PlaySound(sound);
1125
1126 #if 0
1127   Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
1128          part->anim_nr, part->nr, part->mode_nr, sound);
1129 #endif
1130 }
1131
1132 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1133 {
1134   int sound = part->sound;
1135
1136   if (sound == SND_UNDEFINED)
1137     return;
1138
1139   StopSound(sound);
1140
1141 #if 0
1142   Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1143         part->anim_nr, part->nr, part->mode_nr, sound);
1144 #endif
1145 }
1146
1147 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1148 {
1149   int music = part->music;
1150
1151   if (music == MUS_UNDEFINED)
1152     return;
1153
1154   if (!setup.sound_music)
1155     return;
1156
1157   if (IS_LOOP_MUSIC(music))
1158     PlayMusicLoop(music);
1159   else
1160     PlayMusic(music);
1161
1162 #if 0
1163   Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1164         part->anim_nr, part->nr, part->mode_nr, music);
1165 #endif
1166 }
1167
1168 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1169 {
1170   int music = part->music;
1171
1172   if (music == MUS_UNDEFINED)
1173     return;
1174
1175   char *anim_music = getMusicInfoEntryFilename(music);
1176   char *curr_music = getCurrentlyPlayingMusicFilename();
1177
1178   // do not stop music if global anim music differs from current music
1179   if (!strEqual(curr_music, anim_music))
1180     return;
1181
1182   StopMusic();
1183
1184 #if 0
1185   Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1186         part->anim_nr, part->nr, part->mode_nr, music);
1187 #endif
1188 }
1189
1190 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1191 {
1192   // when drawing animations to fading buffer, do not play sounds or music
1193   if (drawing_to_fading_buffer)
1194     return;
1195
1196   PlayGlobalAnimSound(part);
1197   PlayGlobalAnimMusic(part);
1198 }
1199
1200 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1201 {
1202   StopGlobalAnimSound(part);
1203   StopGlobalAnimMusic(part);
1204 }
1205
1206 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1207 {
1208   // when drawing animations to fading buffer, do not play sounds
1209   if (drawing_to_fading_buffer)
1210     return;
1211
1212   // loop sounds only expire when playing
1213   if (game_status != GAME_MODE_PLAYING)
1214     return;
1215
1216   // check if any sound is defined for this animation part
1217   if (part->sound == SND_UNDEFINED)
1218     return;
1219
1220   // normal (non-loop) sounds do not expire when playing
1221   if (!IS_LOOP_SOUND(part->sound))
1222     return;
1223
1224   // prevent expiring loop sounds when playing
1225   PlayGlobalAnimSound(part);
1226 }
1227
1228 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1229 {
1230   int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1231   int mask_ce_only   = mask & ~ANIM_EVENT_PAGE_MASK;
1232
1233   if (mask & ANIM_EVENT_ANY)
1234     return (anim_event & ANIM_EVENT_ANY);
1235   else if (mask & ANIM_EVENT_SELF)
1236     return (anim_event & ANIM_EVENT_SELF);
1237   else if (mask & ANIM_EVENT_UNCLICK_ANY)
1238     return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1239   else if (mask & ANIM_EVENT_CE_CHANGE)
1240     return (anim_event == mask ||
1241             anim_event == mask_ce_only);
1242   else
1243     return (anim_event == mask ||
1244             anim_event == mask_anim_only);
1245 }
1246
1247 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1248 {
1249   struct GraphicInfo *c = &part->control_info;
1250   int i;
1251
1252   if (part->init_event_state)
1253   {
1254     int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1255
1256     for (i = 0; i < num_init_events; i++)
1257     {
1258       int init_event = GetGlobalAnimEventValue(c->init_event, i);
1259
1260       if (checkGlobalAnimEvent(init_event, mask))
1261         return TRUE;
1262     }
1263   }
1264
1265   if (part->anim_event_state)
1266   {
1267     int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1268
1269     for (i = 0; i < num_anim_events; i++)
1270     {
1271       int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1272
1273       if (checkGlobalAnimEvent(anim_event, mask))
1274         return TRUE;
1275     }
1276   }
1277
1278   return FALSE;
1279 }
1280
1281 static boolean isInsidePartStacked(struct GlobalAnimPartControlInfo *part,
1282                                    int mx, int my)
1283 {
1284   struct GraphicInfo *g = &part->graphic_info;
1285   struct GraphicInfo *c = &part->control_info;
1286   int part_x = part->viewport_x + part->x;
1287   int part_y = part->viewport_y + part->y;
1288   int part_width  = g->width;
1289   int part_height = g->height;
1290   int x, y;
1291
1292   for (y = 0; y < c->stacked_yfactor; y++)
1293   {
1294     for (x = 0; x < c->stacked_xfactor; x++)
1295     {
1296       int part_stacked_x = part_x + x * (part_width  + c->stacked_xoffset);
1297       int part_stacked_y = part_y + y * (part_height + c->stacked_yoffset);
1298
1299       if (mx >= part_stacked_x &&
1300           mx <  part_stacked_x + part_width &&
1301           my >= part_stacked_y &&
1302           my <  part_stacked_y + part_height)
1303         return TRUE;
1304     }
1305   }
1306
1307   return FALSE;
1308 }
1309
1310 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1311                              int mx, int my, boolean clicked)
1312 {
1313   // check if mouse click was detected at all
1314   if (!clicked)
1315     return FALSE;
1316
1317   // check if mouse click is outside the animation part's viewport
1318   if (mx <  part->viewport_x ||
1319       mx >= part->viewport_x + part->viewport_width ||
1320       my <  part->viewport_y ||
1321       my >= part->viewport_y + part->viewport_height)
1322     return FALSE;
1323
1324   // check if mouse click is inside the animation part's (stacked) graphic
1325   if (isInsidePartStacked(part, mx, my))
1326     return TRUE;
1327
1328   return FALSE;
1329 }
1330
1331 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1332 {
1333   return ((part->control_info.style & STYLE_BLOCK) ? TRUE : FALSE);
1334 }
1335
1336 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1337 {
1338   return ((part->control_info.style & STYLE_PASSTHROUGH) ? FALSE : TRUE);
1339 }
1340
1341 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1342                                      boolean *click_consumed,
1343                                      boolean *any_event_action,
1344                                      int event_value, char *info_text)
1345 {
1346   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1347
1348   int gic_anim_nr = part->old_anim_nr + 1;      // X as in "anim_X"
1349   int gic_part_nr = part->old_nr + 1;           // Y as in "part_Y"
1350   int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1351
1352   if (!part->is_base)
1353     mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1354
1355   int anim2_nr;
1356
1357   for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1358   {
1359     struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1360     int part2_nr;
1361
1362     for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1363     {
1364       struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1365
1366       if (!(part2->state & ANIM_STATE_RUNNING))
1367         continue;
1368
1369       if (isClickablePart(part2, mask))
1370       {
1371         part2->triggered = TRUE;
1372         *click_consumed |= clickConsumed(part);         // click was on "part"!
1373
1374 #if DEBUG_ANIM_EVENTS
1375         Debug("anim:InitGlobalAnim_Triggered",
1376               "%d.%d TRIGGERED BY %s OF %d.%d",
1377               part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1378               part->old_anim_nr + 1, part->old_nr + 1);
1379 #endif
1380 #if 0
1381         Debug("anim:InitGlobalAnim_Triggered",
1382               "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1383               part2->control_info.anim_event_action);
1384 #endif
1385
1386         // after executing event action, ignore any further actions
1387         if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1388           *any_event_action = TRUE;
1389       }
1390
1391 #if 0
1392       struct GraphicInfo *c = &part2->control_info;
1393
1394       if (isClickablePart(part2, mask))
1395         Debug("anim:InitGlobalAnim_Triggered",
1396               "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1397               anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1398               anim_nr, part_nr);
1399       else
1400         Debug("anim:InitGlobalAnim_Triggered",
1401               "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1402               anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1403 #endif
1404     }
1405   }
1406 }
1407
1408 static void InitGlobalAnim_Triggered_ByCustomElement(int nr, int page,
1409                                                      int x, int y)
1410 {
1411   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_PLAYING];
1412
1413   int event_value = ANIM_EVENT_CE_CHANGE;
1414   int event_bits = (nr << ANIM_EVENT_CE_BIT) | (page << ANIM_EVENT_PAGE_BIT);
1415   int mask = event_value | event_bits;
1416   int anim2_nr;
1417
1418   for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1419   {
1420     struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1421     int part2_nr;
1422
1423     for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1424     {
1425       struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1426
1427       if (!(part2->state & ANIM_STATE_RUNNING))
1428         continue;
1429
1430       if (isClickablePart(part2, mask) && !part2->triggered)
1431       {
1432         struct GraphicInfo *c = &part2->control_info;
1433
1434         if (c->position == POS_CE)
1435         {
1436           // may contain negative tile offset that is treated as "off-screen"
1437           part2->x = 0;
1438           part2->y = 0;
1439
1440           // store CE tile and offset position to handle scrolling
1441           part2->tile_x = x;
1442           part2->tile_y = y;
1443           part2->tile_xoffset = c->x;
1444           part2->tile_yoffset = c->y;
1445
1446           // restart animation (by using current sync frame)
1447           part2->initial_anim_sync_frame = anim_sync_frame;
1448         }
1449
1450         part2->triggered = TRUE;
1451
1452         // do not trigger any other animation if CE change event was consumed
1453         if (c->style == STYLE_CONSUME_CE_EVENT)
1454           return;
1455
1456 #if 0
1457         Debug("anim:InitGlobalAnim_Triggered_ByCustomElement",
1458               "%d.%d TRIGGERED BY CE %d", anim2_nr, part2_nr, nr + 1);
1459 #endif
1460       }
1461     }
1462   }
1463 }
1464
1465 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1466                                   int delay_type, char *info_text)
1467 {
1468 #if DEBUG_ANIM_DELAY
1469   Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1470         part->old_anim_nr + 1, part->old_nr + 1, info_text);
1471 #endif
1472
1473   DoGlobalAnim_DelayAction(part, delay_type);
1474 }
1475
1476 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1477                                   int event_value, char *info_text)
1478 {
1479 #if DEBUG_ANIM_EVENTS
1480   Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1481         part->old_anim_nr + 1, part->old_nr + 1, info_text);
1482 #endif
1483
1484   boolean click_consumed = FALSE;
1485   boolean any_event_action = FALSE;
1486
1487   // check if this event is defined to trigger other animations
1488   InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1489                            event_value, info_text);
1490 }
1491
1492 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1493                                  int state)
1494 {
1495   if (handle_click && !part->clicked)
1496     return state;
1497
1498   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1499   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1500   struct GraphicInfo *g = &part->graphic_info;
1501   struct GraphicInfo *c = &part->control_info;
1502   boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1503
1504   // if game is paused, also pause playfield animations
1505   if (isPausedOnPlayfield(part))
1506     return state;
1507
1508   if (viewport_changed)
1509     state |= ANIM_STATE_RESTART;
1510
1511   if (state & ANIM_STATE_RESTART)
1512   {
1513     // when drawing animations to fading buffer, only start fixed animations
1514     if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1515                                      c->y == ARG_UNDEFINED_VALUE))
1516       return ANIM_STATE_INACTIVE;
1517
1518     ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1519
1520     part->init_delay_counter =
1521       (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1522
1523     part->anim_delay_counter =
1524       (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1525
1526     part->post_delay_counter = 0;
1527
1528     part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1529     part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1530
1531     part->initial_anim_sync_frame =
1532       (g->anim_global_sync || g->anim_global_anim_sync ? 0 :
1533        anim_sync_frame + part->init_delay_counter);
1534
1535     // do not re-initialize random animation frame after fade-in
1536     if (part->anim_random_frame == -1)
1537       part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1538
1539     if (c->direction & MV_HORIZONTAL)
1540     {
1541       int pos_bottom = part->viewport_height - g->height;
1542
1543       if (c->position == POS_TOP)
1544         part->y = 0;
1545       else if (c->position == POS_UPPER)
1546         part->y = GetSimpleRandom(pos_bottom / 2);
1547       else if (c->position == POS_MIDDLE)
1548         part->y = pos_bottom / 2;
1549       else if (c->position == POS_LOWER)
1550         part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1551       else if (c->position == POS_BOTTOM)
1552         part->y = pos_bottom;
1553       else
1554         part->y = GetSimpleRandom(pos_bottom);
1555
1556       if (c->direction == MV_RIGHT)
1557       {
1558         part->step_xoffset = c->step_offset;
1559         part->x = -g->width + part->step_xoffset;
1560       }
1561       else
1562       {
1563         part->step_xoffset = -c->step_offset;
1564         part->x = part->viewport_width + part->step_xoffset;
1565       }
1566
1567       part->step_yoffset = 0;
1568     }
1569     else if (c->direction & MV_VERTICAL)
1570     {
1571       int pos_right = part->viewport_width - g->width;
1572
1573       if (c->position == POS_LEFT)
1574         part->x = 0;
1575       else if (c->position == POS_RIGHT)
1576         part->x = pos_right;
1577       else
1578         part->x = GetSimpleRandom(pos_right);
1579
1580       if (c->direction == MV_DOWN)
1581       {
1582         part->step_yoffset = c->step_offset;
1583         part->y = -g->height + part->step_yoffset;
1584       }
1585       else
1586       {
1587         part->step_yoffset = -c->step_offset;
1588         part->y = part->viewport_height + part->step_yoffset;
1589       }
1590
1591       part->step_xoffset = 0;
1592     }
1593     else
1594     {
1595       part->x = 0;
1596       part->y = 0;
1597
1598       part->step_xoffset = 0;
1599       part->step_yoffset = 0;
1600     }
1601
1602     if (part->control_info.class != get_hash_from_key("pointer"))
1603     {
1604       if (c->x != ARG_UNDEFINED_VALUE)
1605         part->x = c->x;
1606       if (c->y != ARG_UNDEFINED_VALUE)
1607         part->y = c->y;
1608     }
1609
1610     if (c->position == POS_LAST &&
1611         anim->last_x > -g->width  && anim->last_x < part->viewport_width &&
1612         anim->last_y > -g->height && anim->last_y < part->viewport_height)
1613     {
1614       part->x = anim->last_x;
1615       part->y = anim->last_y;
1616     }
1617
1618     if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1619       part->step_xoffset = c->step_xoffset;
1620     if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1621       part->step_yoffset = c->step_yoffset;
1622
1623     if (part->init_delay_counter == 0 &&
1624         !part->init_event_state)
1625     {
1626       PlayGlobalAnimSoundAndMusic(part);
1627
1628       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
1629       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1630     }
1631     else
1632     {
1633       HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1634     }
1635   }
1636
1637   if (part->clicked &&
1638       part->init_event_state)
1639   {
1640     if (part->initial_anim_sync_frame > 0)
1641       part->initial_anim_sync_frame = anim_sync_frame;
1642
1643     part->init_delay_counter = 1;
1644     part->init_event_state = FALSE;
1645
1646     part->clicked = FALSE;
1647   }
1648
1649   if (part->clicked &&
1650       part->anim_event_state)
1651   {
1652     part->anim_delay_counter = 1;
1653     part->anim_event_state = FALSE;
1654
1655     part->clicked = FALSE;
1656   }
1657
1658   if (part->init_delay_counter > 0)
1659   {
1660     part->init_delay_counter--;
1661
1662     if (part->init_delay_counter == 0)
1663     {
1664       part->init_event_state = FALSE;
1665
1666       PlayGlobalAnimSoundAndMusic(part);
1667
1668       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
1669       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1670
1671       // continue with state ANIM_STATE_RUNNING (set below)
1672     }
1673     else
1674     {
1675       return ANIM_STATE_WAITING;
1676     }
1677   }
1678
1679   if (part->init_event_state)
1680     return ANIM_STATE_WAITING;
1681
1682   // animation part is now running/visible and therefore clickable
1683   part->clickable = TRUE;
1684
1685   // check if moving animation has left the visible screen area
1686   if ((part->x <= -g->width              && part->step_xoffset <= 0) ||
1687       (part->x >=  part->viewport_width  && part->step_xoffset >= 0) ||
1688       (part->y <= -g->height             && part->step_yoffset <= 0) ||
1689       (part->y >=  part->viewport_height && part->step_yoffset >= 0))
1690   {
1691     // do not wait for "anim" events for off-screen animations
1692     part->anim_event_state = FALSE;
1693
1694     // do not stop animation before "anim" or "post" counter are finished
1695     if (part->anim_delay_counter == 0 &&
1696         part->post_delay_counter == 0)
1697     {
1698       StopGlobalAnimSoundAndMusic(part);
1699
1700       HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1701
1702       part->post_delay_counter =
1703         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1704
1705       if (part->post_delay_counter > 0)
1706         return ANIM_STATE_RUNNING;
1707
1708       // drawing last frame not needed here -- animation not visible anymore
1709       return ANIM_STATE_RESTART;
1710     }
1711   }
1712
1713   if (part->anim_delay_counter > 0)
1714   {
1715     part->anim_delay_counter--;
1716
1717     if (part->anim_delay_counter == 0)
1718     {
1719       part->anim_event_state = FALSE;
1720
1721       StopGlobalAnimSoundAndMusic(part);
1722
1723       HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1724       HandleGlobalAnimEvent(part, ANIM_EVENT_END,  "END [ANIM_DELAY/EVENT]");
1725
1726       part->post_delay_counter =
1727         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1728
1729       if (part->post_delay_counter > 0)
1730         return ANIM_STATE_RUNNING;
1731
1732       // additional state "RUNNING" required to not skip drawing last frame
1733       return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1734     }
1735   }
1736
1737   if (part->post_delay_counter > 0)
1738   {
1739     part->post_delay_counter--;
1740
1741     if (part->post_delay_counter == 0)
1742     {
1743       HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1744       HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1745
1746       return ANIM_STATE_RESTART;
1747     }
1748
1749     return ANIM_STATE_WAITING;
1750   }
1751
1752   // special case to prevent expiring loop sounds when playing
1753   PlayGlobalAnimSoundIfLoop(part);
1754
1755   if (!DelayReachedExt(&part->step_delay, anim_sync_frame))
1756     return ANIM_STATE_RUNNING;
1757
1758 #if 0
1759   {
1760     static unsigned int last_counter = -1;
1761     unsigned int counter = Counter();
1762
1763     Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1764           anim_sync_frame, counter - last_counter);
1765
1766     last_counter = counter;
1767   }
1768 #endif
1769
1770   if (c->position == POS_CE)
1771   {
1772     // calculate playfield position (with scrolling) for related CE tile
1773     // (do not use FX/FY, which are incorrect during envelope requests)
1774     int FX0 = 2 * TILEX_VAR;    // same as FX during DRAW_TO_FIELDBUFFER
1775     int FY0 = 2 * TILEY_VAR;    // same as FY during DRAW_TO_FIELDBUFFER
1776     int fx = getFieldbufferOffsetX_RND(ScreenMovDir, ScreenGfxPos);
1777     int fy = getFieldbufferOffsetY_RND(ScreenMovDir, ScreenGfxPos);
1778     int sx = FX0 + SCREENX(part->tile_x) * TILEX_VAR;
1779     int sy = FY0 + SCREENY(part->tile_y) * TILEY_VAR;
1780     int cx = SX - REAL_SX;
1781     int cy = SY - REAL_SY;
1782     int x = sx - fx + cx;
1783     int y = sy - fy + cy;
1784
1785     part->tile_xoffset += part->step_xoffset;
1786     part->tile_yoffset += part->step_yoffset;
1787
1788     part->x = x + part->tile_xoffset;
1789     part->y = y + part->tile_yoffset;
1790   }
1791   else
1792   {
1793     part->x += part->step_xoffset;
1794     part->y += part->step_yoffset;
1795   }
1796
1797   anim->last_x = part->x;
1798   anim->last_y = part->y;
1799
1800   return ANIM_STATE_RUNNING;
1801 }
1802
1803 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1804                                   int action)
1805 {
1806   struct GlobalAnimPartControlInfo *part;
1807   struct GraphicInfo *c = &anim->control_info;
1808   int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1809   int state, active_part_nr;
1810   int i;
1811
1812 #if 0
1813   Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1814          anim->mode_nr, anim->nr, anim->num_parts);
1815   Debug("anim:HandleGlobalAnim_Main",
1816         "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1817 #endif
1818
1819 #if 0
1820   Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1821         (action == ANIM_START ? "ANIM_START" :
1822          action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1823          action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1824         anim->nr,
1825         anim->state & ANIM_STATE_RESTART,
1826         anim->state & ANIM_STATE_WAITING,
1827         anim->state & ANIM_STATE_RUNNING,
1828         anim->num_parts);
1829 #endif
1830
1831   switch (action)
1832   {
1833     case ANIM_START:
1834       anim->state = anim->last_state = ANIM_STATE_RESTART;
1835       anim->active_part_nr = anim->last_active_part_nr = 0;
1836       anim->part_counter = 0;
1837
1838       break;
1839
1840     case ANIM_CONTINUE:
1841       if (anim->state == ANIM_STATE_INACTIVE)
1842         return;
1843
1844       anim->state = anim->last_state;
1845       anim->active_part_nr = anim->last_active_part_nr;
1846
1847       break;
1848
1849     case ANIM_STOP:
1850       anim->state = ANIM_STATE_INACTIVE;
1851
1852       for (i = 0; i < num_parts; i++)
1853         StopGlobalAnimSoundAndMusic(&anim->part[i]);
1854
1855       return;
1856
1857     default:
1858       break;
1859   }
1860
1861   if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1862   {
1863 #if 0
1864     Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1865           anim->mode_nr, anim->nr, num_parts);
1866 #endif
1867
1868     for (i = 0; i < num_parts; i++)
1869     {
1870       part = &anim->part[i];
1871
1872       switch (action)
1873       {
1874         case ANIM_START:
1875           anim->state = ANIM_STATE_RUNNING;
1876           part->state = ANIM_STATE_RESTART;
1877
1878           break;
1879
1880         case ANIM_CONTINUE:
1881           if (part->state == ANIM_STATE_INACTIVE)
1882             continue;
1883
1884           break;
1885
1886         case ANIM_STOP:
1887           part->state = ANIM_STATE_INACTIVE;
1888
1889           continue;
1890
1891         default:
1892           break;
1893       }
1894
1895       part->state = HandleGlobalAnim_Part(part, part->state);
1896
1897       // when animation mode is "once", stop after animation was played once
1898       if (c->anim_mode & ANIM_ONCE &&
1899           part->state & ANIM_STATE_RESTART)
1900         part->state = ANIM_STATE_INACTIVE;
1901     }
1902
1903     anim->last_state = anim->state;
1904     anim->last_active_part_nr = anim->active_part_nr;
1905
1906     return;
1907   }
1908
1909   if (anim->state & ANIM_STATE_RESTART)         // directly after restart
1910     anim->active_part_nr = getGlobalAnimationPart(anim);
1911
1912   part = &anim->part[anim->active_part_nr];
1913
1914   // first set all animation parts to "inactive", ...
1915   for (i = 0; i < num_parts; i++)
1916     anim->part[i].state = ANIM_STATE_INACTIVE;
1917
1918   // ... then set current animation parts to "running"
1919   part->state = ANIM_STATE_RUNNING;
1920
1921   anim->state = HandleGlobalAnim_Part(part, anim->state);
1922
1923   if (anim->state & ANIM_STATE_RESTART)
1924     anim->part_counter++;
1925
1926   // when animation mode is "once", stop after all animations were played once
1927   if (c->anim_mode & ANIM_ONCE &&
1928       anim->part_counter == anim->num_parts)
1929     anim->state = ANIM_STATE_INACTIVE;
1930
1931   state = anim->state;
1932   active_part_nr = anim->active_part_nr;
1933
1934   // while the animation parts are pausing (waiting or inactive), play the base
1935   // (main) animation; this corresponds to the "boring player animation" logic
1936   // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1937   if (anim->has_base)
1938   {
1939     if (anim->state == ANIM_STATE_WAITING ||
1940         anim->state == ANIM_STATE_INACTIVE)
1941     {
1942       anim->active_part_nr = anim->num_parts;   // part nr of base animation
1943       part = &anim->part[anim->active_part_nr];
1944
1945       if (anim->state != anim->last_state)
1946         part->state = ANIM_STATE_RESTART;
1947
1948       anim->state = ANIM_STATE_RUNNING;
1949       part->state = HandleGlobalAnim_Part(part, part->state);
1950     }
1951   }
1952
1953   anim->last_state = state;
1954   anim->last_active_part_nr = active_part_nr;
1955 }
1956
1957 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1958 {
1959   int i;
1960
1961 #if 0
1962   Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1963 #endif
1964
1965   for (i = 0; i < ctrl->num_anims; i++)
1966     HandleGlobalAnim_Main(&ctrl->anim[i], action);
1967 }
1968
1969 static void HandleGlobalAnim(int action, int game_mode)
1970 {
1971 #if 0
1972   Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1973 #endif
1974
1975   HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1976 }
1977
1978 static void DoAnimationExt(void)
1979 {
1980   int i;
1981
1982 #if 0
1983   Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
1984 #endif
1985
1986   // global animations now synchronized with frame delay of screen update
1987   anim_sync_frame++;
1988
1989   for (i = 0; i < NUM_GAME_MODES; i++)
1990     HandleGlobalAnim(ANIM_CONTINUE, i);
1991
1992 #if 0
1993   // force screen redraw in next frame to continue drawing global animations
1994   redraw_mask = REDRAW_ALL;
1995 #endif
1996 }
1997
1998 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1999                                      int delay_type)
2000 {
2001   int delay_action =
2002     (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
2003      delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
2004      delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
2005      ANIM_DELAY_ACTION_NONE);
2006
2007   if (delay_action == ANIM_DELAY_ACTION_NONE)
2008     return;
2009
2010   PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
2011 }
2012
2013 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
2014 {
2015   int event_action = (part->init_event_state ?
2016                       part->control_info.init_event_action :
2017                       part->control_info.anim_event_action);
2018
2019   if (event_action == ANIM_EVENT_ACTION_NONE)
2020     return FALSE;
2021
2022   if (event_action < MAX_IMAGE_FILES)
2023     PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
2024   else
2025     OpenURLFromHash(anim_url_hash, event_action);
2026
2027   // check if further actions are allowed to be executed
2028   if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
2029     return FALSE;
2030
2031   return TRUE;
2032 }
2033
2034 static void InitGlobalAnim_Clickable(void)
2035 {
2036   int mode_nr;
2037
2038   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
2039   {
2040     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
2041     int anim_nr;
2042
2043     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
2044     {
2045       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
2046       int part_nr;
2047
2048       for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
2049       {
2050         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
2051
2052         if (part->triggered)
2053           part->clicked = TRUE;
2054
2055         part->triggered = FALSE;
2056         part->clickable = FALSE;
2057       }
2058     }
2059   }
2060 }
2061
2062 #define ANIM_CLICKED_RESET      0
2063 #define ANIM_CLICKED_PRESSED    1
2064 #define ANIM_CLICKED_RELEASED   2
2065
2066 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
2067 {
2068   boolean click_consumed = FALSE;
2069   boolean anything_clicked = FALSE;
2070   boolean any_part_clicked = FALSE;
2071   boolean any_event_action = FALSE;
2072   int mode_nr;
2073   int i;
2074
2075   // check game modes in reverse draw order (to stop when clicked)
2076   for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
2077   {
2078     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
2079     int anim_nr;
2080
2081     // check animations in reverse draw order (to stop when clicked)
2082     for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
2083     {
2084       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
2085       int part_nr;
2086
2087       // check animation parts in reverse draw order (to stop when clicked)
2088       for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
2089       {
2090         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
2091
2092         if (clicked_event == ANIM_CLICKED_RESET)
2093         {
2094           part->clicked = FALSE;
2095
2096           continue;
2097         }
2098
2099         if (!part->clickable)
2100           continue;
2101
2102         if (!(part->state & ANIM_STATE_RUNNING))
2103           continue;
2104
2105         // always handle "any" click events (clicking anywhere on screen) ...
2106         if (clicked_event == ANIM_CLICKED_PRESSED &&
2107             isClickablePart(part, ANIM_EVENT_ANY))
2108         {
2109 #if DEBUG_ANIM_EVENTS
2110           Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
2111                 part->old_anim_nr + 1, part->old_nr + 1);
2112 #endif
2113
2114           anything_clicked = part->clicked = TRUE;
2115           click_consumed |= clickConsumed(part);
2116         }
2117
2118         // always handle "unclick:any" events (releasing anywhere on screen) ...
2119         if (clicked_event == ANIM_CLICKED_RELEASED &&
2120             isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
2121         {
2122 #if DEBUG_ANIM_EVENTS
2123           Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
2124                 part->old_anim_nr + 1, part->old_nr + 1);
2125 #endif
2126
2127           anything_clicked = part->clicked = TRUE;
2128           click_consumed |= clickConsumed(part);
2129         }
2130
2131         // ... but only handle the first (topmost) clickable animation
2132         if (any_part_clicked)
2133           continue;
2134
2135         if (clicked_event == ANIM_CLICKED_PRESSED &&
2136             isClickedPart(part, mx, my, TRUE))
2137         {
2138 #if 0
2139           Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
2140                 anim_nr, part_nr, part->control_info.anim_event_action);
2141 #endif
2142
2143           // after executing event action, ignore any further actions
2144           if (!any_event_action && DoGlobalAnim_EventAction(part))
2145             any_event_action = TRUE;
2146
2147           // determine if mouse clicks should be blocked from other animations
2148           any_part_clicked |= clickConsumed(part);
2149
2150           if (isClickablePart(part, ANIM_EVENT_SELF))
2151           {
2152 #if DEBUG_ANIM_EVENTS
2153             Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
2154                   part->old_anim_nr + 1, part->old_nr + 1);
2155 #endif
2156
2157             anything_clicked = part->clicked = TRUE;
2158             click_consumed |= clickConsumed(part);
2159           }
2160
2161           // determine if mouse clicks should be blocked by this animation
2162           click_consumed |= clickBlocked(part);
2163
2164           // check if this click is defined to trigger other animations
2165           InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
2166                                    ANIM_EVENT_CLICK, "CLICK");
2167         }
2168       }
2169     }
2170   }
2171
2172   if (anything_clicked)
2173   {
2174     handle_click = TRUE;
2175
2176     for (i = 0; i < NUM_GAME_MODES; i++)
2177       HandleGlobalAnim(ANIM_CONTINUE, i);
2178
2179     handle_click = FALSE;
2180
2181     // prevent ignoring release event if processed within same game frame
2182     StopProcessingEvents();
2183   }
2184
2185   return (click_consumed || any_event_action);
2186 }
2187
2188 static void ResetGlobalAnim_Clickable(void)
2189 {
2190   InitGlobalAnim_Clickable();
2191 }
2192
2193 static void ResetGlobalAnim_Clicked(void)
2194 {
2195   InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
2196 }
2197
2198 void RestartGlobalAnimsByStatus(int status)
2199 {
2200   int anim_status_last = global.anim_status;
2201
2202   global.anim_status = status;
2203
2204   // force restarting global animations by changed global animation status
2205   DrawGlobalAnimationsExt(DRAW_TO_SCREEN, DRAW_GLOBAL_ANIM_STAGE_RESTART);
2206
2207   global.anim_status = anim_status_last;
2208 }
2209
2210 void SetAnimStatusBeforeFading(int status)
2211 {
2212   anim_status_last_before_fading = status;
2213 }
2214
2215 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
2216 {
2217   static boolean click_consumed = FALSE;
2218   static int last_button = 0;
2219   boolean press_event;
2220   boolean release_event;
2221   boolean click_consumed_current = click_consumed;
2222
2223   if (button != 0 && force_click)
2224     last_button = 0;
2225
2226   // check if button state has changed since last invocation
2227   press_event   = (button != 0 && last_button == 0);
2228   release_event = (button == 0 && last_button != 0);
2229   last_button = button;
2230
2231   if (press_event)
2232   {
2233     click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
2234     click_consumed_current = click_consumed;
2235   }
2236
2237   if (release_event)
2238   {
2239     InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
2240     click_consumed = FALSE;
2241   }
2242
2243   return click_consumed_current;
2244 }
2245
2246 int getGlobalAnimSyncFrame(void)
2247 {
2248   return anim_sync_frame;
2249 }
2250
2251 void HandleGlobalAnimEventByElementChange(int element, int page, int x, int y)
2252 {
2253   if (!IS_CUSTOM_ELEMENT(element))
2254     return;
2255
2256   // custom element stored as 0 to 255, change page stored as 1 to 32
2257   InitGlobalAnim_Triggered_ByCustomElement(element - EL_CUSTOM_START, page + 1,
2258                                            x, y);
2259 }