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