fixed bug with shifted global playfield animations during envelope requests
[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   // when restarting global animations, do not redraw them, but stop here
861   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_RESTART)
862     return;
863
864   if (global.anim_status == GAME_MODE_LOADING)
865     return;
866
867   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
868   {
869     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
870     int anim_nr;
871
872     // when preparing source fading buffer, only draw animations to be stopped
873     if (drawing_target == DRAW_TO_FADE_SOURCE &&
874         game_mode_anim_action[mode_nr] != ANIM_STOP)
875       continue;
876
877     // when preparing target fading buffer, only draw animations to be started
878     if (drawing_target == DRAW_TO_FADE_TARGET &&
879         game_mode_anim_action[mode_nr] != ANIM_START)
880       continue;
881
882 #if 0
883     if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
884         mode_nr != game_status)
885       continue;
886 #endif
887
888     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
889     {
890       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
891       struct GraphicInfo *c = &anim->control_info;
892       int part_first, part_last;
893       int part_nr;
894
895       if (!(anim->state & ANIM_STATE_RUNNING))
896         continue;
897
898       part_first = part_last = anim->active_part_nr;
899
900       if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
901       {
902         int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
903
904         part_first = 0;
905         part_last = num_parts - 1;
906       }
907
908       for (part_nr = part_first; part_nr <= part_last; part_nr++)
909       {
910         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
911         struct GraphicInfo *g = &part->graphic_info;
912         Bitmap *src_bitmap;
913         int src_x, src_y;
914         int sync_frame;
915         int frame;
916         int last_anim_random_frame = gfx.anim_random_frame;
917
918         if (!(part->state & ANIM_STATE_RUNNING))
919           continue;
920
921         if (part->drawing_stage != drawing_stage)
922           continue;
923
924         sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
925
926         // re-initialize random animation frame after animation delay
927         if (g->anim_mode == ANIM_RANDOM &&
928             sync_frame % g->anim_delay == 0 &&
929             sync_frame > 0)
930           part->anim_random_frame = GetSimpleRandom(g->anim_frames);
931
932         gfx.anim_random_frame = part->anim_random_frame;
933
934         frame = getAnimationFrame(g->anim_frames, g->anim_delay,
935                                   g->anim_mode, g->anim_start_frame,
936                                   sync_frame);
937
938         gfx.anim_random_frame = last_anim_random_frame;
939
940         getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
941                                    &src_x, &src_y);
942
943         BlitGlobalAnimation(part, src_bitmap, src_x, src_y, drawing_target);
944       }
945     }
946   }
947
948   if (drawing_target == DRAW_TO_FADE_TARGET)
949   {
950     // stop animations determined to be (temporary) started above
951     for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
952       if (game_mode_anim_action[mode_nr] == ANIM_START)
953         HandleGlobalAnim(ANIM_STOP, mode_nr);
954
955     drawing_to_fading_buffer = FALSE;
956   }
957 }
958
959 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
960 {
961   int last_cursor_mode_override = gfx.cursor_mode_override;
962
963   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
964   {
965     ResetGlobalAnim_Clickable();
966
967     gfx.cursor_mode_override = CURSOR_UNDEFINED;
968   }
969
970   DrawGlobalAnimationsExt(drawing_target, drawing_stage);
971
972   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
973   {
974     ResetGlobalAnim_Clicked();
975   }
976
977   DrawEnvelopeRequestToScreen(drawing_target, drawing_stage);
978
979   if (gfx.cursor_mode_override != last_cursor_mode_override)
980     SetMouseCursor(gfx.cursor_mode);
981 }
982
983 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
984 {
985   int viewport_x;
986   int viewport_y;
987   int viewport_width;
988   int viewport_height;
989   boolean changed = FALSE;
990
991   if (part->last_anim_status == global.anim_status &&
992       part->control_info.class != get_hash_from_key("pointer"))
993     return FALSE;
994
995   part->last_anim_status = global.anim_status;
996
997   part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
998
999   if (part->control_info.class == get_hash_from_key("window") ||
1000       part->control_info.class == get_hash_from_key("border"))
1001   {
1002     viewport_x = 0;
1003     viewport_y = 0;
1004     viewport_width  = WIN_XSIZE;
1005     viewport_height = WIN_YSIZE;
1006
1007     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
1008   }
1009   else if (part->control_info.class == get_hash_from_key("pointer"))
1010   {
1011     int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
1012     int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
1013
1014     // prevent displaying off-screen custom mouse cursor in upper left corner
1015     if (gfx.mouse_x == POS_OFFSCREEN &&
1016         gfx.mouse_y == POS_OFFSCREEN)
1017       mx = my = POS_OFFSCREEN;
1018
1019     viewport_x = mx - part->control_info.x;
1020     viewport_y = my - part->control_info.y;
1021     viewport_width  = part->graphic_info.width;
1022     viewport_height = part->graphic_info.height;
1023
1024     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_3;
1025
1026     // do not use global animation mouse pointer when reloading artwork
1027     if (global.anim_status != GAME_MODE_LOADING)
1028       gfx.cursor_mode_override = CURSOR_NONE;
1029   }
1030   else if (part->control_info.class == get_hash_from_key("door_1"))
1031   {
1032     viewport_x = DX;
1033     viewport_y = DY;
1034     viewport_width  = DXSIZE;
1035     viewport_height = DYSIZE;
1036   }
1037   else if (part->control_info.class == get_hash_from_key("door_2"))
1038   {
1039     if (part->mode_nr == GAME_MODE_EDITOR)
1040     {
1041       viewport_x = EX;
1042       viewport_y = EY;
1043       viewport_width  = EXSIZE;
1044       viewport_height = EYSIZE;
1045     }
1046     else
1047     {
1048       viewport_x = VX;
1049       viewport_y = VY;
1050       viewport_width  = VXSIZE;
1051       viewport_height = VYSIZE;
1052     }
1053   }
1054   else          // default: "playfield"
1055   {
1056     viewport_x = REAL_SX;
1057     viewport_y = REAL_SY;
1058     viewport_width  = FULL_SXSIZE;
1059     viewport_height = FULL_SYSIZE;
1060   }
1061
1062   if (viewport_x != part->viewport_x ||
1063       viewport_y != part->viewport_y ||
1064       viewport_width  != part->viewport_width ||
1065       viewport_height != part->viewport_height)
1066   {
1067     part->viewport_x = viewport_x;
1068     part->viewport_y = viewport_y;
1069     part->viewport_width  = viewport_width;
1070     part->viewport_height = viewport_height;
1071
1072     if (part->control_info.class != get_hash_from_key("pointer"))
1073       changed = TRUE;
1074   }
1075
1076   return changed;
1077 }
1078
1079 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1080 {
1081   int sound = part->sound;
1082
1083   if (sound == SND_UNDEFINED)
1084     return;
1085
1086   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
1087       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
1088     return;
1089
1090   // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
1091   if (IS_LOOP_SOUND(sound))
1092     PlaySoundLoop(sound);
1093   else
1094     PlaySound(sound);
1095
1096 #if 0
1097   Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
1098          part->anim_nr, part->nr, part->mode_nr, sound);
1099 #endif
1100 }
1101
1102 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1103 {
1104   int sound = part->sound;
1105
1106   if (sound == SND_UNDEFINED)
1107     return;
1108
1109   StopSound(sound);
1110
1111 #if 0
1112   Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1113         part->anim_nr, part->nr, part->mode_nr, sound);
1114 #endif
1115 }
1116
1117 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1118 {
1119   int music = part->music;
1120
1121   if (music == MUS_UNDEFINED)
1122     return;
1123
1124   if (!setup.sound_music)
1125     return;
1126
1127   if (IS_LOOP_MUSIC(music))
1128     PlayMusicLoop(music);
1129   else
1130     PlayMusic(music);
1131
1132 #if 0
1133   Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1134         part->anim_nr, part->nr, part->mode_nr, music);
1135 #endif
1136 }
1137
1138 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1139 {
1140   int music = part->music;
1141
1142   if (music == MUS_UNDEFINED)
1143     return;
1144
1145   char *anim_music = getMusicInfoEntryFilename(music);
1146   char *curr_music = getCurrentlyPlayingMusicFilename();
1147
1148   // do not stop music if global anim music differs from current music
1149   if (!strEqual(curr_music, anim_music))
1150     return;
1151
1152   StopMusic();
1153
1154 #if 0
1155   Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1156         part->anim_nr, part->nr, part->mode_nr, music);
1157 #endif
1158 }
1159
1160 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1161 {
1162   // when drawing animations to fading buffer, do not play sounds or music
1163   if (drawing_to_fading_buffer)
1164     return;
1165
1166   PlayGlobalAnimSound(part);
1167   PlayGlobalAnimMusic(part);
1168 }
1169
1170 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1171 {
1172   StopGlobalAnimSound(part);
1173   StopGlobalAnimMusic(part);
1174 }
1175
1176 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1177 {
1178   // when drawing animations to fading buffer, do not play sounds
1179   if (drawing_to_fading_buffer)
1180     return;
1181
1182   // loop sounds only expire when playing
1183   if (game_status != GAME_MODE_PLAYING)
1184     return;
1185
1186   // check if any sound is defined for this animation part
1187   if (part->sound == SND_UNDEFINED)
1188     return;
1189
1190   // normal (non-loop) sounds do not expire when playing
1191   if (!IS_LOOP_SOUND(part->sound))
1192     return;
1193
1194   // prevent expiring loop sounds when playing
1195   PlayGlobalAnimSound(part);
1196 }
1197
1198 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1199 {
1200   int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1201   int mask_ce_only   = mask & ~ANIM_EVENT_PAGE_MASK;
1202
1203   if (mask & ANIM_EVENT_ANY)
1204     return (anim_event & ANIM_EVENT_ANY);
1205   else if (mask & ANIM_EVENT_SELF)
1206     return (anim_event & ANIM_EVENT_SELF);
1207   else if (mask & ANIM_EVENT_UNCLICK_ANY)
1208     return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1209   else if (mask & ANIM_EVENT_CE_CHANGE)
1210     return (anim_event == mask ||
1211             anim_event == mask_ce_only);
1212   else
1213     return (anim_event == mask ||
1214             anim_event == mask_anim_only);
1215 }
1216
1217 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1218 {
1219   struct GraphicInfo *c = &part->control_info;
1220   int i;
1221
1222   if (part->init_event_state)
1223   {
1224     int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1225
1226     for (i = 0; i < num_init_events; i++)
1227     {
1228       int init_event = GetGlobalAnimEventValue(c->init_event, i);
1229
1230       if (checkGlobalAnimEvent(init_event, mask))
1231         return TRUE;
1232     }
1233   }
1234
1235   if (part->anim_event_state)
1236   {
1237     int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1238
1239     for (i = 0; i < num_anim_events; i++)
1240     {
1241       int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1242
1243       if (checkGlobalAnimEvent(anim_event, mask))
1244         return TRUE;
1245     }
1246   }
1247
1248   return FALSE;
1249 }
1250
1251 static boolean isInsidePartStacked(struct GlobalAnimPartControlInfo *part,
1252                                    int mx, int my)
1253 {
1254   struct GraphicInfo *g = &part->graphic_info;
1255   struct GraphicInfo *c = &part->control_info;
1256   int part_x = part->viewport_x + part->x;
1257   int part_y = part->viewport_y + part->y;
1258   int part_width  = g->width;
1259   int part_height = g->height;
1260   int x, y;
1261
1262   for (y = 0; y < c->stacked_yfactor; y++)
1263   {
1264     for (x = 0; x < c->stacked_xfactor; x++)
1265     {
1266       int part_stacked_x = part_x + x * (part_width  + c->stacked_xoffset);
1267       int part_stacked_y = part_y + y * (part_height + c->stacked_yoffset);
1268
1269       if (mx >= part_stacked_x &&
1270           mx <  part_stacked_x + part_width &&
1271           my >= part_stacked_y &&
1272           my <  part_stacked_y + part_height)
1273         return TRUE;
1274     }
1275   }
1276
1277   return FALSE;
1278 }
1279
1280 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1281                              int mx, int my, boolean clicked)
1282 {
1283   // check if mouse click was detected at all
1284   if (!clicked)
1285     return FALSE;
1286
1287   // check if mouse click is outside the animation part's viewport
1288   if (mx <  part->viewport_x ||
1289       mx >= part->viewport_x + part->viewport_width ||
1290       my <  part->viewport_y ||
1291       my >= part->viewport_y + part->viewport_height)
1292     return FALSE;
1293
1294   // check if mouse click is inside the animation part's (stacked) graphic
1295   if (isInsidePartStacked(part, mx, my))
1296     return TRUE;
1297
1298   return FALSE;
1299 }
1300
1301 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1302 {
1303   return ((part->control_info.style & STYLE_BLOCK) ? TRUE : FALSE);
1304 }
1305
1306 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1307 {
1308   return ((part->control_info.style & STYLE_PASSTHROUGH) ? FALSE : TRUE);
1309 }
1310
1311 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1312                                      boolean *click_consumed,
1313                                      boolean *any_event_action,
1314                                      int event_value, char *info_text)
1315 {
1316   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1317
1318   int gic_anim_nr = part->old_anim_nr + 1;      // X as in "anim_X"
1319   int gic_part_nr = part->old_nr + 1;           // Y as in "part_Y"
1320   int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1321
1322   if (!part->is_base)
1323     mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1324
1325   int anim2_nr;
1326
1327   for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1328   {
1329     struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1330     int part2_nr;
1331
1332     for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1333     {
1334       struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1335
1336       if (!(part2->state & ANIM_STATE_RUNNING))
1337         continue;
1338
1339       if (isClickablePart(part2, mask))
1340       {
1341         part2->triggered = TRUE;
1342         *click_consumed |= clickConsumed(part);         // click was on "part"!
1343
1344 #if DEBUG_ANIM_EVENTS
1345         Debug("anim:InitGlobalAnim_Triggered",
1346               "%d.%d TRIGGERED BY %s OF %d.%d",
1347               part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1348               part->old_anim_nr + 1, part->old_nr + 1);
1349 #endif
1350 #if 0
1351         Debug("anim:InitGlobalAnim_Triggered",
1352               "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1353               part2->control_info.anim_event_action);
1354 #endif
1355
1356         // after executing event action, ignore any further actions
1357         if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1358           *any_event_action = TRUE;
1359       }
1360
1361 #if 0
1362       struct GraphicInfo *c = &part2->control_info;
1363
1364       if (isClickablePart(part2, mask))
1365         Debug("anim:InitGlobalAnim_Triggered",
1366               "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1367               anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1368               anim_nr, part_nr);
1369       else
1370         Debug("anim:InitGlobalAnim_Triggered",
1371               "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1372               anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1373 #endif
1374     }
1375   }
1376 }
1377
1378 static void InitGlobalAnim_Triggered_ByCustomElement(int nr, int page,
1379                                                      int x, int y)
1380 {
1381   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_PLAYING];
1382
1383   int event_value = ANIM_EVENT_CE_CHANGE;
1384   int event_bits = (nr << ANIM_EVENT_CE_BIT) | (page << ANIM_EVENT_PAGE_BIT);
1385   int mask = event_value | event_bits;
1386   int anim2_nr;
1387
1388   for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1389   {
1390     struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1391     int part2_nr;
1392
1393     for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1394     {
1395       struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1396
1397       if (!(part2->state & ANIM_STATE_RUNNING))
1398         continue;
1399
1400       if (isClickablePart(part2, mask) && !part2->triggered)
1401       {
1402         struct GraphicInfo *c = &part2->control_info;
1403
1404         if (c->position == POS_CE)
1405         {
1406           // store CE tile and offset position to handle scrolling
1407           part2->tile_x = x;
1408           part2->tile_y = y;
1409           part2->tile_xoffset = c->x;
1410           part2->tile_yoffset = c->y;
1411
1412           // restart animation (by using current sync frame)
1413           part2->initial_anim_sync_frame = anim_sync_frame;
1414         }
1415
1416         part2->triggered = TRUE;
1417
1418         // do not trigger any other animation if CE change event was consumed
1419         if (c->style == STYLE_CONSUME_CE_EVENT)
1420           return;
1421
1422 #if 0
1423         Debug("anim:InitGlobalAnim_Triggered_ByCustomElement",
1424               "%d.%d TRIGGERED BY CE %d", anim2_nr, part2_nr, nr + 1);
1425 #endif
1426       }
1427     }
1428   }
1429 }
1430
1431 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1432                                   int delay_type, char *info_text)
1433 {
1434 #if DEBUG_ANIM_DELAY
1435   Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1436         part->old_anim_nr + 1, part->old_nr + 1, info_text);
1437 #endif
1438
1439   DoGlobalAnim_DelayAction(part, delay_type);
1440 }
1441
1442 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1443                                   int event_value, char *info_text)
1444 {
1445 #if DEBUG_ANIM_EVENTS
1446   Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1447         part->old_anim_nr + 1, part->old_nr + 1, info_text);
1448 #endif
1449
1450   boolean click_consumed = FALSE;
1451   boolean any_event_action = FALSE;
1452
1453   // check if this event is defined to trigger other animations
1454   InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1455                            event_value, info_text);
1456 }
1457
1458 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1459                                  int state)
1460 {
1461   if (handle_click && !part->clicked)
1462     return state;
1463
1464   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1465   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1466   struct GraphicInfo *g = &part->graphic_info;
1467   struct GraphicInfo *c = &part->control_info;
1468   boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1469
1470   if (viewport_changed)
1471     state |= ANIM_STATE_RESTART;
1472
1473   if (state & ANIM_STATE_RESTART)
1474   {
1475     // when drawing animations to fading buffer, only start fixed animations
1476     if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1477                                      c->y == ARG_UNDEFINED_VALUE))
1478       return ANIM_STATE_INACTIVE;
1479
1480     ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1481
1482     part->init_delay_counter =
1483       (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1484
1485     part->anim_delay_counter =
1486       (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1487
1488     part->post_delay_counter = 0;
1489
1490     part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1491     part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1492
1493     part->initial_anim_sync_frame =
1494       (g->anim_global_sync || g->anim_global_anim_sync ? 0 :
1495        anim_sync_frame + part->init_delay_counter);
1496
1497     // do not re-initialize random animation frame after fade-in
1498     if (part->anim_random_frame == -1)
1499       part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1500
1501     if (c->direction & MV_HORIZONTAL)
1502     {
1503       int pos_bottom = part->viewport_height - g->height;
1504
1505       if (c->position == POS_TOP)
1506         part->y = 0;
1507       else if (c->position == POS_UPPER)
1508         part->y = GetSimpleRandom(pos_bottom / 2);
1509       else if (c->position == POS_MIDDLE)
1510         part->y = pos_bottom / 2;
1511       else if (c->position == POS_LOWER)
1512         part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1513       else if (c->position == POS_BOTTOM)
1514         part->y = pos_bottom;
1515       else
1516         part->y = GetSimpleRandom(pos_bottom);
1517
1518       if (c->direction == MV_RIGHT)
1519       {
1520         part->step_xoffset = c->step_offset;
1521         part->x = -g->width + part->step_xoffset;
1522       }
1523       else
1524       {
1525         part->step_xoffset = -c->step_offset;
1526         part->x = part->viewport_width + part->step_xoffset;
1527       }
1528
1529       part->step_yoffset = 0;
1530     }
1531     else if (c->direction & MV_VERTICAL)
1532     {
1533       int pos_right = part->viewport_width - g->width;
1534
1535       if (c->position == POS_LEFT)
1536         part->x = 0;
1537       else if (c->position == POS_RIGHT)
1538         part->x = pos_right;
1539       else
1540         part->x = GetSimpleRandom(pos_right);
1541
1542       if (c->direction == MV_DOWN)
1543       {
1544         part->step_yoffset = c->step_offset;
1545         part->y = -g->height + part->step_yoffset;
1546       }
1547       else
1548       {
1549         part->step_yoffset = -c->step_offset;
1550         part->y = part->viewport_height + part->step_yoffset;
1551       }
1552
1553       part->step_xoffset = 0;
1554     }
1555     else
1556     {
1557       part->x = 0;
1558       part->y = 0;
1559
1560       part->step_xoffset = 0;
1561       part->step_yoffset = 0;
1562     }
1563
1564     if (part->control_info.class != get_hash_from_key("pointer"))
1565     {
1566       if (c->x != ARG_UNDEFINED_VALUE)
1567         part->x = c->x;
1568       if (c->y != ARG_UNDEFINED_VALUE)
1569         part->y = c->y;
1570     }
1571
1572     if (c->position == POS_LAST &&
1573         anim->last_x > -g->width  && anim->last_x < part->viewport_width &&
1574         anim->last_y > -g->height && anim->last_y < part->viewport_height)
1575     {
1576       part->x = anim->last_x;
1577       part->y = anim->last_y;
1578     }
1579
1580     if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1581       part->step_xoffset = c->step_xoffset;
1582     if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1583       part->step_yoffset = c->step_yoffset;
1584
1585     if (part->init_delay_counter == 0 &&
1586         !part->init_event_state)
1587     {
1588       PlayGlobalAnimSoundAndMusic(part);
1589
1590       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
1591       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1592     }
1593     else
1594     {
1595       HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1596     }
1597   }
1598
1599   if (part->clicked &&
1600       part->init_event_state)
1601   {
1602     if (part->initial_anim_sync_frame > 0)
1603       part->initial_anim_sync_frame = anim_sync_frame;
1604
1605     part->init_delay_counter = 1;
1606     part->init_event_state = FALSE;
1607
1608     part->clicked = FALSE;
1609   }
1610
1611   if (part->clicked &&
1612       part->anim_event_state)
1613   {
1614     part->anim_delay_counter = 1;
1615     part->anim_event_state = FALSE;
1616
1617     part->clicked = FALSE;
1618   }
1619
1620   if (part->init_delay_counter > 0)
1621   {
1622     part->init_delay_counter--;
1623
1624     if (part->init_delay_counter == 0)
1625     {
1626       part->init_event_state = FALSE;
1627
1628       PlayGlobalAnimSoundAndMusic(part);
1629
1630       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
1631       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1632
1633       // continue with state ANIM_STATE_RUNNING (set below)
1634     }
1635     else
1636     {
1637       return ANIM_STATE_WAITING;
1638     }
1639   }
1640
1641   if (part->init_event_state)
1642     return ANIM_STATE_WAITING;
1643
1644   // animation part is now running/visible and therefore clickable
1645   part->clickable = TRUE;
1646
1647   // check if moving animation has left the visible screen area
1648   if ((part->x <= -g->width              && part->step_xoffset <= 0) ||
1649       (part->x >=  part->viewport_width  && part->step_xoffset >= 0) ||
1650       (part->y <= -g->height             && part->step_yoffset <= 0) ||
1651       (part->y >=  part->viewport_height && part->step_yoffset >= 0))
1652   {
1653     // do not wait for "anim" events for off-screen animations
1654     part->anim_event_state = FALSE;
1655
1656     // do not stop animation before "anim" or "post" counter are finished
1657     if (part->anim_delay_counter == 0 &&
1658         part->post_delay_counter == 0)
1659     {
1660       StopGlobalAnimSoundAndMusic(part);
1661
1662       HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1663
1664       part->post_delay_counter =
1665         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1666
1667       if (part->post_delay_counter > 0)
1668         return ANIM_STATE_RUNNING;
1669
1670       // drawing last frame not needed here -- animation not visible anymore
1671       return ANIM_STATE_RESTART;
1672     }
1673   }
1674
1675   if (part->anim_delay_counter > 0)
1676   {
1677     part->anim_delay_counter--;
1678
1679     if (part->anim_delay_counter == 0)
1680     {
1681       part->anim_event_state = FALSE;
1682
1683       StopGlobalAnimSoundAndMusic(part);
1684
1685       HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1686       HandleGlobalAnimEvent(part, ANIM_EVENT_END,  "END [ANIM_DELAY/EVENT]");
1687
1688       part->post_delay_counter =
1689         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1690
1691       if (part->post_delay_counter > 0)
1692         return ANIM_STATE_RUNNING;
1693
1694       // additional state "RUNNING" required to not skip drawing last frame
1695       return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1696     }
1697   }
1698
1699   if (part->post_delay_counter > 0)
1700   {
1701     part->post_delay_counter--;
1702
1703     if (part->post_delay_counter == 0)
1704     {
1705       HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1706       HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1707
1708       return ANIM_STATE_RESTART;
1709     }
1710
1711     return ANIM_STATE_WAITING;
1712   }
1713
1714   // special case to prevent expiring loop sounds when playing
1715   PlayGlobalAnimSoundIfLoop(part);
1716
1717   if (!DelayReachedExt(&part->step_delay, anim_sync_frame))
1718     return ANIM_STATE_RUNNING;
1719
1720 #if 0
1721   {
1722     static unsigned int last_counter = -1;
1723     unsigned int counter = Counter();
1724
1725     Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1726           anim_sync_frame, counter - last_counter);
1727
1728     last_counter = counter;
1729   }
1730 #endif
1731
1732   if (c->position == POS_CE)
1733   {
1734     // calculate playfield position (with scrolling) for related CE tile
1735     // (do not use FX/FY, which are incorrect during envelope requests)
1736     int FX0 = 2 * TILEX_VAR;    // same as FX during DRAW_TO_FIELDBUFFER
1737     int FY0 = 2 * TILEY_VAR;    // same as FY during DRAW_TO_FIELDBUFFER
1738     int fx = getFieldbufferOffsetX_RND(ScreenMovDir, ScreenGfxPos);
1739     int fy = getFieldbufferOffsetY_RND(ScreenMovDir, ScreenGfxPos);
1740     int sx = FX0 + SCREENX(part->tile_x) * TILEX_VAR;
1741     int sy = FY0 + SCREENY(part->tile_y) * TILEY_VAR;
1742     int cx = SX - REAL_SX;
1743     int cy = SY - REAL_SY;
1744     int x = sx - fx + cx;
1745     int y = sy - fy + cy;
1746
1747     part->tile_xoffset += part->step_xoffset;
1748     part->tile_yoffset += part->step_yoffset;
1749
1750     part->x = x + part->tile_xoffset;
1751     part->y = y + part->tile_yoffset;
1752   }
1753   else
1754   {
1755     part->x += part->step_xoffset;
1756     part->y += part->step_yoffset;
1757   }
1758
1759   anim->last_x = part->x;
1760   anim->last_y = part->y;
1761
1762   return ANIM_STATE_RUNNING;
1763 }
1764
1765 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1766                                   int action)
1767 {
1768   struct GlobalAnimPartControlInfo *part;
1769   struct GraphicInfo *c = &anim->control_info;
1770   int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1771   int state, active_part_nr;
1772   int i;
1773
1774 #if 0
1775   Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1776          anim->mode_nr, anim->nr, anim->num_parts);
1777   Debug("anim:HandleGlobalAnim_Main",
1778         "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1779 #endif
1780
1781 #if 0
1782   Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1783         (action == ANIM_START ? "ANIM_START" :
1784          action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1785          action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1786         anim->nr,
1787         anim->state & ANIM_STATE_RESTART,
1788         anim->state & ANIM_STATE_WAITING,
1789         anim->state & ANIM_STATE_RUNNING,
1790         anim->num_parts);
1791 #endif
1792
1793   switch (action)
1794   {
1795     case ANIM_START:
1796       anim->state = anim->last_state = ANIM_STATE_RESTART;
1797       anim->active_part_nr = anim->last_active_part_nr = 0;
1798       anim->part_counter = 0;
1799
1800       break;
1801
1802     case ANIM_CONTINUE:
1803       if (anim->state == ANIM_STATE_INACTIVE)
1804         return;
1805
1806       anim->state = anim->last_state;
1807       anim->active_part_nr = anim->last_active_part_nr;
1808
1809       break;
1810
1811     case ANIM_STOP:
1812       anim->state = ANIM_STATE_INACTIVE;
1813
1814       for (i = 0; i < num_parts; i++)
1815         StopGlobalAnimSoundAndMusic(&anim->part[i]);
1816
1817       return;
1818
1819     default:
1820       break;
1821   }
1822
1823   if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1824   {
1825 #if 0
1826     Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1827           anim->mode_nr, anim->nr, num_parts);
1828 #endif
1829
1830     for (i = 0; i < num_parts; i++)
1831     {
1832       part = &anim->part[i];
1833
1834       switch (action)
1835       {
1836         case ANIM_START:
1837           anim->state = ANIM_STATE_RUNNING;
1838           part->state = ANIM_STATE_RESTART;
1839
1840           break;
1841
1842         case ANIM_CONTINUE:
1843           if (part->state == ANIM_STATE_INACTIVE)
1844             continue;
1845
1846           break;
1847
1848         case ANIM_STOP:
1849           part->state = ANIM_STATE_INACTIVE;
1850
1851           continue;
1852
1853         default:
1854           break;
1855       }
1856
1857       part->state = HandleGlobalAnim_Part(part, part->state);
1858
1859       // when animation mode is "once", stop after animation was played once
1860       if (c->anim_mode & ANIM_ONCE &&
1861           part->state & ANIM_STATE_RESTART)
1862         part->state = ANIM_STATE_INACTIVE;
1863     }
1864
1865     anim->last_state = anim->state;
1866     anim->last_active_part_nr = anim->active_part_nr;
1867
1868     return;
1869   }
1870
1871   if (anim->state & ANIM_STATE_RESTART)         // directly after restart
1872     anim->active_part_nr = getGlobalAnimationPart(anim);
1873
1874   part = &anim->part[anim->active_part_nr];
1875
1876   // first set all animation parts to "inactive", ...
1877   for (i = 0; i < num_parts; i++)
1878     anim->part[i].state = ANIM_STATE_INACTIVE;
1879
1880   // ... then set current animation parts to "running"
1881   part->state = ANIM_STATE_RUNNING;
1882
1883   anim->state = HandleGlobalAnim_Part(part, anim->state);
1884
1885   if (anim->state & ANIM_STATE_RESTART)
1886     anim->part_counter++;
1887
1888   // when animation mode is "once", stop after all animations were played once
1889   if (c->anim_mode & ANIM_ONCE &&
1890       anim->part_counter == anim->num_parts)
1891     anim->state = ANIM_STATE_INACTIVE;
1892
1893   state = anim->state;
1894   active_part_nr = anim->active_part_nr;
1895
1896   // while the animation parts are pausing (waiting or inactive), play the base
1897   // (main) animation; this corresponds to the "boring player animation" logic
1898   // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1899   if (anim->has_base)
1900   {
1901     if (anim->state == ANIM_STATE_WAITING ||
1902         anim->state == ANIM_STATE_INACTIVE)
1903     {
1904       anim->active_part_nr = anim->num_parts;   // part nr of base animation
1905       part = &anim->part[anim->active_part_nr];
1906
1907       if (anim->state != anim->last_state)
1908         part->state = ANIM_STATE_RESTART;
1909
1910       anim->state = ANIM_STATE_RUNNING;
1911       part->state = HandleGlobalAnim_Part(part, part->state);
1912     }
1913   }
1914
1915   anim->last_state = state;
1916   anim->last_active_part_nr = active_part_nr;
1917 }
1918
1919 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1920 {
1921   int i;
1922
1923 #if 0
1924   Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1925 #endif
1926
1927   for (i = 0; i < ctrl->num_anims; i++)
1928     HandleGlobalAnim_Main(&ctrl->anim[i], action);
1929 }
1930
1931 static void HandleGlobalAnim(int action, int game_mode)
1932 {
1933 #if 0
1934   Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1935 #endif
1936
1937   HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1938 }
1939
1940 static void DoAnimationExt(void)
1941 {
1942   int i;
1943
1944 #if 0
1945   Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
1946 #endif
1947
1948   // global animations now synchronized with frame delay of screen update
1949   anim_sync_frame++;
1950
1951   for (i = 0; i < NUM_GAME_MODES; i++)
1952     HandleGlobalAnim(ANIM_CONTINUE, i);
1953
1954 #if 0
1955   // force screen redraw in next frame to continue drawing global animations
1956   redraw_mask = REDRAW_ALL;
1957 #endif
1958 }
1959
1960 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1961                                      int delay_type)
1962 {
1963   int delay_action =
1964     (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
1965      delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
1966      delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
1967      ANIM_DELAY_ACTION_NONE);
1968
1969   if (delay_action == ANIM_DELAY_ACTION_NONE)
1970     return;
1971
1972   PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
1973 }
1974
1975 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1976 {
1977   int event_action = (part->init_event_state ?
1978                       part->control_info.init_event_action :
1979                       part->control_info.anim_event_action);
1980
1981   if (event_action == ANIM_EVENT_ACTION_NONE)
1982     return FALSE;
1983
1984   if (event_action < MAX_IMAGE_FILES)
1985     PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
1986   else
1987     OpenURLFromHash(anim_url_hash, event_action);
1988
1989   // check if further actions are allowed to be executed
1990   if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1991     return FALSE;
1992
1993   return TRUE;
1994 }
1995
1996 static void InitGlobalAnim_Clickable(void)
1997 {
1998   int mode_nr;
1999
2000   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
2001   {
2002     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
2003     int anim_nr;
2004
2005     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
2006     {
2007       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
2008       int part_nr;
2009
2010       for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
2011       {
2012         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
2013
2014         if (part->triggered)
2015           part->clicked = TRUE;
2016
2017         part->triggered = FALSE;
2018         part->clickable = FALSE;
2019       }
2020     }
2021   }
2022 }
2023
2024 #define ANIM_CLICKED_RESET      0
2025 #define ANIM_CLICKED_PRESSED    1
2026 #define ANIM_CLICKED_RELEASED   2
2027
2028 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
2029 {
2030   boolean click_consumed = FALSE;
2031   boolean anything_clicked = FALSE;
2032   boolean any_part_clicked = FALSE;
2033   boolean any_event_action = FALSE;
2034   int mode_nr;
2035   int i;
2036
2037   // check game modes in reverse draw order (to stop when clicked)
2038   for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
2039   {
2040     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
2041     int anim_nr;
2042
2043     // check animations in reverse draw order (to stop when clicked)
2044     for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
2045     {
2046       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
2047       int part_nr;
2048
2049       // check animation parts in reverse draw order (to stop when clicked)
2050       for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
2051       {
2052         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
2053
2054         if (clicked_event == ANIM_CLICKED_RESET)
2055         {
2056           part->clicked = FALSE;
2057
2058           continue;
2059         }
2060
2061         if (!part->clickable)
2062           continue;
2063
2064         if (!(part->state & ANIM_STATE_RUNNING))
2065           continue;
2066
2067         // always handle "any" click events (clicking anywhere on screen) ...
2068         if (clicked_event == ANIM_CLICKED_PRESSED &&
2069             isClickablePart(part, ANIM_EVENT_ANY))
2070         {
2071 #if DEBUG_ANIM_EVENTS
2072           Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
2073                 part->old_anim_nr + 1, part->old_nr + 1);
2074 #endif
2075
2076           anything_clicked = part->clicked = TRUE;
2077           click_consumed |= clickConsumed(part);
2078         }
2079
2080         // always handle "unclick:any" events (releasing anywhere on screen) ...
2081         if (clicked_event == ANIM_CLICKED_RELEASED &&
2082             isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
2083         {
2084 #if DEBUG_ANIM_EVENTS
2085           Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
2086                 part->old_anim_nr + 1, part->old_nr + 1);
2087 #endif
2088
2089           anything_clicked = part->clicked = TRUE;
2090           click_consumed |= clickConsumed(part);
2091         }
2092
2093         // ... but only handle the first (topmost) clickable animation
2094         if (any_part_clicked)
2095           continue;
2096
2097         if (clicked_event == ANIM_CLICKED_PRESSED &&
2098             isClickedPart(part, mx, my, TRUE))
2099         {
2100 #if 0
2101           Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
2102                 anim_nr, part_nr, part->control_info.anim_event_action);
2103 #endif
2104
2105           // after executing event action, ignore any further actions
2106           if (!any_event_action && DoGlobalAnim_EventAction(part))
2107             any_event_action = TRUE;
2108
2109           // determine if mouse clicks should be blocked from other animations
2110           any_part_clicked |= clickConsumed(part);
2111
2112           if (isClickablePart(part, ANIM_EVENT_SELF))
2113           {
2114 #if DEBUG_ANIM_EVENTS
2115             Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
2116                   part->old_anim_nr + 1, part->old_nr + 1);
2117 #endif
2118
2119             anything_clicked = part->clicked = TRUE;
2120             click_consumed |= clickConsumed(part);
2121           }
2122
2123           // determine if mouse clicks should be blocked by this animation
2124           click_consumed |= clickBlocked(part);
2125
2126           // check if this click is defined to trigger other animations
2127           InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
2128                                    ANIM_EVENT_CLICK, "CLICK");
2129         }
2130       }
2131     }
2132   }
2133
2134   if (anything_clicked)
2135   {
2136     handle_click = TRUE;
2137
2138     for (i = 0; i < NUM_GAME_MODES; i++)
2139       HandleGlobalAnim(ANIM_CONTINUE, i);
2140
2141     handle_click = FALSE;
2142
2143     // prevent ignoring release event if processed within same game frame
2144     StopProcessingEvents();
2145   }
2146
2147   return (click_consumed || any_event_action);
2148 }
2149
2150 static void ResetGlobalAnim_Clickable(void)
2151 {
2152   InitGlobalAnim_Clickable();
2153 }
2154
2155 static void ResetGlobalAnim_Clicked(void)
2156 {
2157   InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
2158 }
2159
2160 void RestartGlobalAnimsByStatus(int status)
2161 {
2162   int anim_status_last = global.anim_status;
2163
2164   global.anim_status = status;
2165
2166   // force restarting global animations by changed global animation status
2167   DrawGlobalAnimationsExt(DRAW_TO_SCREEN, DRAW_GLOBAL_ANIM_STAGE_RESTART);
2168
2169   global.anim_status = anim_status_last;
2170 }
2171
2172 void SetAnimStatusBeforeFading(int status)
2173 {
2174   anim_status_last_before_fading = status;
2175 }
2176
2177 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
2178 {
2179   static boolean click_consumed = FALSE;
2180   static int last_button = 0;
2181   boolean press_event;
2182   boolean release_event;
2183   boolean click_consumed_current = click_consumed;
2184
2185   if (button != 0 && force_click)
2186     last_button = 0;
2187
2188   // check if button state has changed since last invocation
2189   press_event   = (button != 0 && last_button == 0);
2190   release_event = (button == 0 && last_button != 0);
2191   last_button = button;
2192
2193   if (press_event)
2194   {
2195     click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
2196     click_consumed_current = click_consumed;
2197   }
2198
2199   if (release_event)
2200   {
2201     InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
2202     click_consumed = FALSE;
2203   }
2204
2205   return click_consumed_current;
2206 }
2207
2208 int getGlobalAnimSyncFrame(void)
2209 {
2210   return anim_sync_frame;
2211 }
2212
2213 void HandleGlobalAnimEventByElementChange(int element, int page, int x, int y)
2214 {
2215   if (!IS_CUSTOM_ELEMENT(element))
2216     return;
2217
2218   // custom element stored as 0 to 255, change page stored as 1 to 32
2219   InitGlobalAnim_Triggered_ByCustomElement(element - EL_CUSTOM_START, page + 1,
2220                                            x, y);
2221 }