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