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