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