moved code to different function where it belongs to
[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 void InitGlobalAnimations(void)
635 {
636   InitGlobalAnimControls();
637 }
638
639 static void BlitGlobalAnimation(struct GlobalAnimPartControlInfo *part,
640                                 Bitmap *src_bitmap, int src_x0, int src_y0,
641                                 int drawing_target)
642 {
643   struct GraphicInfo *g = &part->graphic_info;
644   struct GraphicInfo *c = &part->control_info;
645   void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
646     (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
647   void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
648     (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
649   Bitmap *fade_bitmap =
650     (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
651      drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
652   int x, y;
653
654   for (y = 0; y < c->stacked_yfactor; y++)
655   {
656     for (x = 0; x < c->stacked_xfactor; x++)
657     {
658       int src_x = src_x0;
659       int src_y = src_y0;
660       int dst_x = part->x + x * (g->width  + c->stacked_xoffset);
661       int dst_y = part->y + y * (g->height + c->stacked_yoffset);
662       int cut_x = 0;
663       int cut_y = 0;
664       int width  = g->width;
665       int height = g->height;
666
667       if (dst_x < 0)
668       {
669         width += dst_x;
670         cut_x = -dst_x;
671         dst_x = 0;
672       }
673       else if (dst_x > part->viewport_width - g->width)
674       {
675         width -= (dst_x - (part->viewport_width - g->width));
676       }
677
678       if (dst_y < 0)
679       {
680         height += dst_y;
681         cut_y  = -dst_y;
682         dst_y = 0;
683       }
684       else if (dst_y > part->viewport_height - g->height)
685       {
686         height -= (dst_y - (part->viewport_height - g->height));
687       }
688
689       if (width <= 0 || height <= 0)
690         continue;
691
692       src_x += cut_x;
693       src_y += cut_y;
694
695       dst_x += part->viewport_x;
696       dst_y += part->viewport_y;
697
698       if (drawing_target == DRAW_TO_SCREEN)
699         blit_screen(src_bitmap, src_x, src_y, width, height,
700                     dst_x, dst_y);
701       else
702         blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
703                     dst_x, dst_y);
704     }
705   }
706 }
707
708 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
709 {
710   int game_mode_anim_action[NUM_GAME_MODES];
711   int mode_nr;
712
713   if (!setup.toons)
714     return;
715
716   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
717       drawing_target == DRAW_TO_SCREEN)
718     DoAnimationExt();
719
720   // always start with reliable default values (no animation actions)
721   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
722     game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
723
724   if (global.anim_status != anim_status_last)
725   {
726     boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
727     boolean after_fading  = (anim_status_last   == GAME_MODE_PSEUDO_FADING);
728     int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
729     int i;
730
731     if (drawing_target == DRAW_TO_FADE_TARGET)
732       after_fading = TRUE;
733
734     // special case: changing from/to these screens is done without fading
735     if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME  ||
736         global.anim_status == GAME_MODE_PSEUDO_TYPENAMES ||
737         anim_status_last   == GAME_MODE_PSEUDO_TYPENAME  ||
738         anim_status_last   == GAME_MODE_PSEUDO_TYPENAMES)
739       after_fading = TRUE;
740
741     // ---------- part 1 ------------------------------------------------------
742     // start or stop global animations by change of game mode
743     // (special handling of animations for "current screen" and "all screens")
744
745     if (global.anim_status_next != anim_status_last_before_fading)
746     {
747       // stop animations for last screen before fading to new screen
748       game_mode_anim_action[anim_status_last] = ANIM_STOP;
749
750       // start animations for current screen after fading to new screen
751       game_mode_anim_action[global.anim_status] = ANIM_START;
752     }
753
754     // start animations for all screens after loading new artwork set
755     if (anim_status_last == GAME_MODE_LOADING)
756       game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
757
758     // ---------- part 2 ------------------------------------------------------
759     // start or stop global animations by change of animation class
760     // (generic handling of animations for "class of screens")
761
762     for (i = 0; i < NUM_ANIM_CLASSES; i++)
763     {
764       int anim_class_check = (1 << i);
765       int anim_class_game_mode = anim_class_game_modes[i];
766       int anim_class_last = anim_classes_last & anim_class_check;
767       int anim_class_next = anim_classes_next & anim_class_check;
768
769       // stop animations for changed screen class before fading to new screen
770       if (before_fading && anim_class_last && !anim_class_next)
771         game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
772
773       // start animations for changed screen class after fading to new screen
774       if (after_fading && !anim_class_last && anim_class_next)
775         game_mode_anim_action[anim_class_game_mode] = ANIM_START;
776     }
777
778     if (drawing_target == DRAW_TO_SCREEN)
779     {
780       if (after_fading)
781       {
782         anim_classes_last = anim_classes_next;
783         anim_status_last_before_fading = global.anim_status;
784       }
785
786       anim_status_last = global.anim_status;
787
788       // start or stop animations determined to be started or stopped above
789       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
790         if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
791           HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
792     }
793     else if (drawing_target == DRAW_TO_FADE_TARGET)
794     {
795       drawing_to_fading_buffer = TRUE;
796
797       // start animations determined to be (temporary) started above
798       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
799         if (game_mode_anim_action[mode_nr] == ANIM_START)
800           HandleGlobalAnim(ANIM_START, mode_nr);
801     }
802   }
803
804   if (global.anim_status == GAME_MODE_LOADING)
805     return;
806
807   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
808   {
809     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
810     int anim_nr;
811
812     // when preparing source fading buffer, only draw animations to be stopped
813     if (drawing_target == DRAW_TO_FADE_SOURCE &&
814         game_mode_anim_action[mode_nr] != ANIM_STOP)
815       continue;
816
817     // when preparing target fading buffer, only draw animations to be started
818     if (drawing_target == DRAW_TO_FADE_TARGET &&
819         game_mode_anim_action[mode_nr] != ANIM_START)
820       continue;
821
822 #if 0
823     if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
824         mode_nr != game_status)
825       continue;
826 #endif
827
828     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
829     {
830       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
831       struct GraphicInfo *c = &anim->control_info;
832       int part_first, part_last;
833       int part_nr;
834
835       if (!(anim->state & ANIM_STATE_RUNNING))
836         continue;
837
838       part_first = part_last = anim->active_part_nr;
839
840       if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
841       {
842         int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
843
844         part_first = 0;
845         part_last = num_parts - 1;
846       }
847
848       for (part_nr = part_first; part_nr <= part_last; part_nr++)
849       {
850         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
851         struct GraphicInfo *g = &part->graphic_info;
852         Bitmap *src_bitmap;
853         int src_x, src_y;
854         int sync_frame;
855         int frame;
856         int last_anim_random_frame = gfx.anim_random_frame;
857
858         if (!(part->state & ANIM_STATE_RUNNING))
859           continue;
860
861         if (part->drawing_stage != drawing_stage)
862           continue;
863
864         sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
865
866         // re-initialize random animation frame after animation delay
867         if (g->anim_mode == ANIM_RANDOM &&
868             sync_frame % g->anim_delay == 0 &&
869             sync_frame > 0)
870           part->anim_random_frame = GetSimpleRandom(g->anim_frames);
871
872         gfx.anim_random_frame = part->anim_random_frame;
873
874         frame = getAnimationFrame(g->anim_frames, g->anim_delay,
875                                   g->anim_mode, g->anim_start_frame,
876                                   sync_frame);
877
878         gfx.anim_random_frame = last_anim_random_frame;
879
880         getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
881                                    &src_x, &src_y);
882
883         BlitGlobalAnimation(part, src_bitmap, src_x, src_y, drawing_target);
884       }
885     }
886   }
887
888   if (drawing_target == DRAW_TO_FADE_TARGET)
889   {
890     // stop animations determined to be (temporary) started above
891     for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
892       if (game_mode_anim_action[mode_nr] == ANIM_START)
893         HandleGlobalAnim(ANIM_STOP, mode_nr);
894
895     drawing_to_fading_buffer = FALSE;
896   }
897 }
898
899 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
900 {
901   int last_cursor_mode_override = gfx.cursor_mode_override;
902
903   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
904   {
905     ResetGlobalAnim_Clickable();
906
907     gfx.cursor_mode_override = CURSOR_UNDEFINED;
908   }
909
910   DrawGlobalAnimationsExt(drawing_target, drawing_stage);
911
912   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
913   {
914     ResetGlobalAnim_Clicked();
915   }
916
917   DrawEnvelopeRequestToScreen(drawing_target, drawing_stage);
918
919   if (gfx.cursor_mode_override != last_cursor_mode_override)
920     SetMouseCursor(gfx.cursor_mode);
921 }
922
923 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
924 {
925   int viewport_x;
926   int viewport_y;
927   int viewport_width;
928   int viewport_height;
929   boolean changed = FALSE;
930
931   if (part->last_anim_status == global.anim_status &&
932       part->control_info.class != get_hash_from_key("pointer"))
933     return FALSE;
934
935   part->last_anim_status = global.anim_status;
936
937   part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
938
939   if (part->control_info.class == get_hash_from_key("window") ||
940       part->control_info.class == get_hash_from_key("border"))
941   {
942     viewport_x = 0;
943     viewport_y = 0;
944     viewport_width  = WIN_XSIZE;
945     viewport_height = WIN_YSIZE;
946
947     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
948   }
949   else if (part->control_info.class == get_hash_from_key("pointer"))
950   {
951     int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
952     int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
953
954     // prevent displaying off-screen custom mouse cursor in upper left corner
955     if (gfx.mouse_x == POS_OFFSCREEN &&
956         gfx.mouse_y == POS_OFFSCREEN)
957       mx = my = POS_OFFSCREEN;
958
959     viewport_x = mx - part->control_info.x;
960     viewport_y = my - part->control_info.y;
961     viewport_width  = part->graphic_info.width;
962     viewport_height = part->graphic_info.height;
963
964     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
965
966     // do not use global animation mouse pointer when reloading artwork
967     if (global.anim_status != GAME_MODE_LOADING)
968       gfx.cursor_mode_override = CURSOR_NONE;
969   }
970   else if (part->control_info.class == get_hash_from_key("door_1"))
971   {
972     viewport_x = DX;
973     viewport_y = DY;
974     viewport_width  = DXSIZE;
975     viewport_height = DYSIZE;
976   }
977   else if (part->control_info.class == get_hash_from_key("door_2"))
978   {
979     if (part->mode_nr == GAME_MODE_EDITOR)
980     {
981       viewport_x = EX;
982       viewport_y = EY;
983       viewport_width  = EXSIZE;
984       viewport_height = EYSIZE;
985     }
986     else
987     {
988       viewport_x = VX;
989       viewport_y = VY;
990       viewport_width  = VXSIZE;
991       viewport_height = VYSIZE;
992     }
993   }
994   else          // default: "playfield"
995   {
996     viewport_x = REAL_SX;
997     viewport_y = REAL_SY;
998     viewport_width  = FULL_SXSIZE;
999     viewport_height = FULL_SYSIZE;
1000   }
1001
1002   if (viewport_x != part->viewport_x ||
1003       viewport_y != part->viewport_y ||
1004       viewport_width  != part->viewport_width ||
1005       viewport_height != part->viewport_height)
1006   {
1007     part->viewport_x = viewport_x;
1008     part->viewport_y = viewport_y;
1009     part->viewport_width  = viewport_width;
1010     part->viewport_height = viewport_height;
1011
1012     if (part->control_info.class != get_hash_from_key("pointer"))
1013       changed = TRUE;
1014   }
1015
1016   return changed;
1017 }
1018
1019 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1020 {
1021   int sound = part->sound;
1022
1023   if (sound == SND_UNDEFINED)
1024     return;
1025
1026   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
1027       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
1028     return;
1029
1030   // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
1031   if (IS_LOOP_SOUND(sound))
1032     PlaySoundLoop(sound);
1033   else
1034     PlaySound(sound);
1035
1036 #if 0
1037   Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
1038          part->anim_nr, part->nr, part->mode_nr, sound);
1039 #endif
1040 }
1041
1042 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1043 {
1044   int sound = part->sound;
1045
1046   if (sound == SND_UNDEFINED)
1047     return;
1048
1049   StopSound(sound);
1050
1051 #if 0
1052   Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1053         part->anim_nr, part->nr, part->mode_nr, sound);
1054 #endif
1055 }
1056
1057 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1058 {
1059   int music = part->music;
1060
1061   if (music == MUS_UNDEFINED)
1062     return;
1063
1064   if (!setup.sound_music)
1065     return;
1066
1067   if (IS_LOOP_MUSIC(music))
1068     PlayMusicLoop(music);
1069   else
1070     PlayMusic(music);
1071
1072 #if 0
1073   Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1074         part->anim_nr, part->nr, part->mode_nr, music);
1075 #endif
1076 }
1077
1078 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1079 {
1080   int music = part->music;
1081
1082   if (music == MUS_UNDEFINED)
1083     return;
1084
1085   StopMusic();
1086
1087 #if 0
1088   Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1089         part->anim_nr, part->nr, part->mode_nr, music);
1090 #endif
1091 }
1092
1093 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1094 {
1095   // when drawing animations to fading buffer, do not play sounds or music
1096   if (drawing_to_fading_buffer)
1097     return;
1098
1099   PlayGlobalAnimSound(part);
1100   PlayGlobalAnimMusic(part);
1101 }
1102
1103 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1104 {
1105   StopGlobalAnimSound(part);
1106   StopGlobalAnimMusic(part);
1107 }
1108
1109 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1110 {
1111   // when drawing animations to fading buffer, do not play sounds
1112   if (drawing_to_fading_buffer)
1113     return;
1114
1115   // loop sounds only expire when playing
1116   if (game_status != GAME_MODE_PLAYING)
1117     return;
1118
1119   // check if any sound is defined for this animation part
1120   if (part->sound == SND_UNDEFINED)
1121     return;
1122
1123   // normal (non-loop) sounds do not expire when playing
1124   if (!IS_LOOP_SOUND(part->sound))
1125     return;
1126
1127   // prevent expiring loop sounds when playing
1128   PlayGlobalAnimSound(part);
1129 }
1130
1131 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1132 {
1133   int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1134
1135   if (mask & ANIM_EVENT_ANY)
1136     return (anim_event & ANIM_EVENT_ANY);
1137   else if (mask & ANIM_EVENT_SELF)
1138     return (anim_event & ANIM_EVENT_SELF);
1139   else if (mask & ANIM_EVENT_UNCLICK_ANY)
1140     return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1141   else
1142     return (anim_event == mask ||
1143             anim_event == mask_anim_only);
1144 }
1145
1146 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1147 {
1148   struct GraphicInfo *c = &part->control_info;
1149   int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1150   int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1151   int i;
1152
1153   for (i = 0; i < num_init_events; i++)
1154   {
1155     int init_event = GetGlobalAnimEventValue(c->init_event, i);
1156
1157     if (checkGlobalAnimEvent(init_event, mask))
1158       return TRUE;
1159   }
1160
1161   for (i = 0; i < num_anim_events; i++)
1162   {
1163     int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1164
1165     if (checkGlobalAnimEvent(anim_event, mask))
1166       return TRUE;
1167   }
1168
1169   return FALSE;
1170 }
1171
1172 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1173                              int mx, int my, boolean clicked)
1174 {
1175   struct GraphicInfo *g = &part->graphic_info;
1176   int part_x = part->viewport_x + part->x;
1177   int part_y = part->viewport_y + part->y;
1178   int part_width  = g->width;
1179   int part_height = g->height;
1180
1181   // check if mouse click was detected at all
1182   if (!clicked)
1183     return FALSE;
1184
1185   // check if mouse click is inside the animation part's viewport
1186   if (mx <  part->viewport_x ||
1187       mx >= part->viewport_x + part->viewport_width ||
1188       my <  part->viewport_y ||
1189       my >= part->viewport_y + part->viewport_height)
1190     return FALSE;
1191
1192   // check if mouse click is inside the animation part's graphic
1193   if (mx <  part_x ||
1194       mx >= part_x + part_width ||
1195       my <  part_y ||
1196       my >= part_y + part_height)
1197     return FALSE;
1198
1199   return TRUE;
1200 }
1201
1202 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1203 {
1204   return ((part->control_info.style & STYLE_BLOCK) ? TRUE : FALSE);
1205 }
1206
1207 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1208 {
1209   return ((part->control_info.style & STYLE_PASSTHROUGH) ? FALSE : TRUE);
1210 }
1211
1212 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1213                                      boolean *click_consumed,
1214                                      boolean *any_event_action,
1215                                      int event_value, char *info_text)
1216 {
1217   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1218
1219   int gic_anim_nr = part->old_anim_nr + 1;      // X as in "anim_X"
1220   int gic_part_nr = part->old_nr + 1;           // Y as in "part_Y"
1221   int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1222
1223   if (!part->is_base)
1224     mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1225
1226   int anim2_nr;
1227
1228   for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1229   {
1230     struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1231     int part2_nr;
1232
1233     for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1234     {
1235       struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1236
1237       if (!(part2->state & ANIM_STATE_RUNNING))
1238         continue;
1239
1240       if (isClickablePart(part2, mask))
1241       {
1242         part2->triggered = TRUE;
1243         *click_consumed |= clickConsumed(part);         // click was on "part"!
1244
1245 #if DEBUG_ANIM_EVENTS
1246         Debug("anim:InitGlobalAnim_Triggered",
1247               "%d.%d TRIGGERED BY %s OF %d.%d",
1248               part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1249               part->old_anim_nr + 1, part->old_nr + 1);
1250 #endif
1251 #if 0
1252         Debug("anim:InitGlobalAnim_Triggered",
1253               "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1254               part2->control_info.anim_event_action);
1255 #endif
1256
1257         // after executing event action, ignore any further actions
1258         if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1259           *any_event_action = TRUE;
1260       }
1261
1262 #if 0
1263       struct GraphicInfo *c = &part2->control_info;
1264
1265       if (isClickablePart(part2, mask))
1266         Debug("anim:InitGlobalAnim_Triggered",
1267               "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1268               anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1269               anim_nr, part_nr);
1270       else
1271         Debug("anim:InitGlobalAnim_Triggered",
1272               "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1273               anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1274 #endif
1275     }
1276   }
1277 }
1278
1279 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1280                                   int delay_type, char *info_text)
1281 {
1282 #if DEBUG_ANIM_DELAY
1283   Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1284         part->old_anim_nr + 1, part->old_nr + 1, info_text);
1285 #endif
1286
1287   DoGlobalAnim_DelayAction(part, delay_type);
1288 }
1289
1290 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1291                                   int event_value, char *info_text)
1292 {
1293 #if DEBUG_ANIM_EVENTS
1294   Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1295         part->old_anim_nr + 1, part->old_nr + 1, info_text);
1296 #endif
1297
1298   boolean click_consumed = FALSE;
1299   boolean any_event_action = FALSE;
1300
1301   // check if this event is defined to trigger other animations
1302   InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1303                            event_value, info_text);
1304 }
1305
1306 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1307                                  int state)
1308 {
1309   if (handle_click && !part->clicked)
1310     return state;
1311
1312   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1313   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1314   struct GraphicInfo *g = &part->graphic_info;
1315   struct GraphicInfo *c = &part->control_info;
1316   boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1317
1318   if (viewport_changed)
1319     state |= ANIM_STATE_RESTART;
1320
1321   if (state & ANIM_STATE_RESTART)
1322   {
1323     // when drawing animations to fading buffer, only start fixed animations
1324     if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1325                                      c->y == ARG_UNDEFINED_VALUE))
1326       return ANIM_STATE_INACTIVE;
1327
1328     ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1329
1330     part->init_delay_counter =
1331       (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1332
1333     part->anim_delay_counter =
1334       (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1335
1336     part->post_delay_counter = 0;
1337
1338     part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1339     part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1340
1341     part->initial_anim_sync_frame =
1342       (g->anim_global_sync || g->anim_global_anim_sync ? 0 :
1343        anim_sync_frame + part->init_delay_counter);
1344
1345     // do not re-initialize random animation frame after fade-in
1346     if (part->anim_random_frame == -1)
1347       part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1348
1349     if (c->direction & MV_HORIZONTAL)
1350     {
1351       int pos_bottom = part->viewport_height - g->height;
1352
1353       if (c->position == POS_TOP)
1354         part->y = 0;
1355       else if (c->position == POS_UPPER)
1356         part->y = GetSimpleRandom(pos_bottom / 2);
1357       else if (c->position == POS_MIDDLE)
1358         part->y = pos_bottom / 2;
1359       else if (c->position == POS_LOWER)
1360         part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1361       else if (c->position == POS_BOTTOM)
1362         part->y = pos_bottom;
1363       else
1364         part->y = GetSimpleRandom(pos_bottom);
1365
1366       if (c->direction == MV_RIGHT)
1367       {
1368         part->step_xoffset = c->step_offset;
1369         part->x = -g->width + part->step_xoffset;
1370       }
1371       else
1372       {
1373         part->step_xoffset = -c->step_offset;
1374         part->x = part->viewport_width + part->step_xoffset;
1375       }
1376
1377       part->step_yoffset = 0;
1378     }
1379     else if (c->direction & MV_VERTICAL)
1380     {
1381       int pos_right = part->viewport_width - g->width;
1382
1383       if (c->position == POS_LEFT)
1384         part->x = 0;
1385       else if (c->position == POS_RIGHT)
1386         part->x = pos_right;
1387       else
1388         part->x = GetSimpleRandom(pos_right);
1389
1390       if (c->direction == MV_DOWN)
1391       {
1392         part->step_yoffset = c->step_offset;
1393         part->y = -g->height + part->step_yoffset;
1394       }
1395       else
1396       {
1397         part->step_yoffset = -c->step_offset;
1398         part->y = part->viewport_height + part->step_yoffset;
1399       }
1400
1401       part->step_xoffset = 0;
1402     }
1403     else
1404     {
1405       part->x = 0;
1406       part->y = 0;
1407
1408       part->step_xoffset = 0;
1409       part->step_yoffset = 0;
1410     }
1411
1412     if (part->control_info.class != get_hash_from_key("pointer"))
1413     {
1414       if (c->x != ARG_UNDEFINED_VALUE)
1415         part->x = c->x;
1416       if (c->y != ARG_UNDEFINED_VALUE)
1417         part->y = c->y;
1418     }
1419
1420     if (c->position == POS_LAST &&
1421         anim->last_x > -g->width  && anim->last_x < part->viewport_width &&
1422         anim->last_y > -g->height && anim->last_y < part->viewport_height)
1423     {
1424       part->x = anim->last_x;
1425       part->y = anim->last_y;
1426     }
1427
1428     if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1429       part->step_xoffset = c->step_xoffset;
1430     if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1431       part->step_yoffset = c->step_yoffset;
1432
1433     if (part->init_delay_counter == 0 &&
1434         !part->init_event_state)
1435     {
1436       PlayGlobalAnimSoundAndMusic(part);
1437
1438       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
1439       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1440     }
1441     else
1442     {
1443       HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1444     }
1445   }
1446
1447   if (part->clicked &&
1448       part->init_event_state)
1449   {
1450     if (part->initial_anim_sync_frame > 0)
1451       part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1452
1453     part->init_delay_counter = 1;
1454     part->init_event_state = FALSE;
1455
1456     part->clicked = FALSE;
1457   }
1458
1459   if (part->clicked &&
1460       part->anim_event_state)
1461   {
1462     part->anim_delay_counter = 1;
1463     part->anim_event_state = FALSE;
1464
1465     part->clicked = FALSE;
1466   }
1467
1468   if (part->init_delay_counter > 0)
1469   {
1470     part->init_delay_counter--;
1471
1472     if (part->init_delay_counter == 0)
1473     {
1474       part->init_event_state = FALSE;
1475
1476       PlayGlobalAnimSoundAndMusic(part);
1477
1478       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
1479       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1480     }
1481
1482     return ANIM_STATE_WAITING;
1483   }
1484
1485   if (part->init_event_state)
1486     return ANIM_STATE_WAITING;
1487
1488   // animation part is now running/visible and therefore clickable
1489   part->clickable = TRUE;
1490
1491   // check if moving animation has left the visible screen area
1492   if ((part->x <= -g->width              && part->step_xoffset <= 0) ||
1493       (part->x >=  part->viewport_width  && part->step_xoffset >= 0) ||
1494       (part->y <= -g->height             && part->step_yoffset <= 0) ||
1495       (part->y >=  part->viewport_height && part->step_yoffset >= 0))
1496   {
1497     // do not wait for "anim" events for off-screen animations
1498     part->anim_event_state = FALSE;
1499
1500     // do not stop animation before "anim" or "post" counter are finished
1501     if (part->anim_delay_counter == 0 &&
1502         part->post_delay_counter == 0)
1503     {
1504       StopGlobalAnimSoundAndMusic(part);
1505
1506       HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1507
1508       part->post_delay_counter =
1509         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1510
1511       if (part->post_delay_counter > 0)
1512         return ANIM_STATE_RUNNING;
1513
1514       // drawing last frame not needed here -- animation not visible anymore
1515       return ANIM_STATE_RESTART;
1516     }
1517   }
1518
1519   if (part->anim_delay_counter > 0)
1520   {
1521     part->anim_delay_counter--;
1522
1523     if (part->anim_delay_counter == 0)
1524     {
1525       part->anim_event_state = FALSE;
1526
1527       StopGlobalAnimSoundAndMusic(part);
1528
1529       HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1530       HandleGlobalAnimEvent(part, ANIM_EVENT_END,  "END [ANIM_DELAY/EVENT]");
1531
1532       part->post_delay_counter =
1533         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1534
1535       if (part->post_delay_counter > 0)
1536         return ANIM_STATE_RUNNING;
1537
1538       // additional state "RUNNING" required to not skip drawing last frame
1539       return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1540     }
1541   }
1542
1543   if (part->post_delay_counter > 0)
1544   {
1545     part->post_delay_counter--;
1546
1547     if (part->post_delay_counter == 0)
1548     {
1549       HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1550       HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1551
1552       return ANIM_STATE_RESTART;
1553     }
1554
1555     return ANIM_STATE_WAITING;
1556   }
1557
1558   // special case to prevent expiring loop sounds when playing
1559   PlayGlobalAnimSoundIfLoop(part);
1560
1561   if (!DelayReachedExt(&part->step_delay, anim_sync_frame))
1562     return ANIM_STATE_RUNNING;
1563
1564 #if 0
1565   {
1566     static unsigned int last_counter = -1;
1567     unsigned int counter = Counter();
1568
1569     Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1570           anim_sync_frame, counter - last_counter);
1571
1572     last_counter = counter;
1573   }
1574 #endif
1575
1576   part->x += part->step_xoffset;
1577   part->y += part->step_yoffset;
1578
1579   anim->last_x = part->x;
1580   anim->last_y = part->y;
1581
1582   return ANIM_STATE_RUNNING;
1583 }
1584
1585 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1586                                   int action)
1587 {
1588   struct GlobalAnimPartControlInfo *part;
1589   struct GraphicInfo *c = &anim->control_info;
1590   int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1591   int state, active_part_nr;
1592   int i;
1593
1594 #if 0
1595   Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1596          anim->mode_nr, anim->nr, anim->num_parts);
1597   Debug("anim:HandleGlobalAnim_Main",
1598         "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1599 #endif
1600
1601 #if 0
1602   Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1603         (action == ANIM_START ? "ANIM_START" :
1604          action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1605          action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1606         anim->nr,
1607         anim->state & ANIM_STATE_RESTART,
1608         anim->state & ANIM_STATE_WAITING,
1609         anim->state & ANIM_STATE_RUNNING,
1610         anim->num_parts);
1611 #endif
1612
1613   switch (action)
1614   {
1615     case ANIM_START:
1616       anim->state = anim->last_state = ANIM_STATE_RESTART;
1617       anim->active_part_nr = anim->last_active_part_nr = 0;
1618       anim->part_counter = 0;
1619
1620       break;
1621
1622     case ANIM_CONTINUE:
1623       if (anim->state == ANIM_STATE_INACTIVE)
1624         return;
1625
1626       anim->state = anim->last_state;
1627       anim->active_part_nr = anim->last_active_part_nr;
1628
1629       break;
1630
1631     case ANIM_STOP:
1632       anim->state = ANIM_STATE_INACTIVE;
1633
1634       for (i = 0; i < num_parts; i++)
1635         StopGlobalAnimSoundAndMusic(&anim->part[i]);
1636
1637       return;
1638
1639     default:
1640       break;
1641   }
1642
1643   if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1644   {
1645 #if 0
1646     Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1647           anim->mode_nr, anim->nr, num_parts);
1648 #endif
1649
1650     for (i = 0; i < num_parts; i++)
1651     {
1652       part = &anim->part[i];
1653
1654       switch (action)
1655       {
1656         case ANIM_START:
1657           anim->state = ANIM_STATE_RUNNING;
1658           part->state = ANIM_STATE_RESTART;
1659
1660           break;
1661
1662         case ANIM_CONTINUE:
1663           if (part->state == ANIM_STATE_INACTIVE)
1664             continue;
1665
1666           break;
1667
1668         case ANIM_STOP:
1669           part->state = ANIM_STATE_INACTIVE;
1670
1671           continue;
1672
1673         default:
1674           break;
1675       }
1676
1677       part->state = HandleGlobalAnim_Part(part, part->state);
1678
1679       // when animation mode is "once", stop after animation was played once
1680       if (c->anim_mode & ANIM_ONCE &&
1681           part->state & ANIM_STATE_RESTART)
1682         part->state = ANIM_STATE_INACTIVE;
1683     }
1684
1685     anim->last_state = anim->state;
1686     anim->last_active_part_nr = anim->active_part_nr;
1687
1688     return;
1689   }
1690
1691   if (anim->state & ANIM_STATE_RESTART)         // directly after restart
1692     anim->active_part_nr = getGlobalAnimationPart(anim);
1693
1694   part = &anim->part[anim->active_part_nr];
1695
1696   // first set all animation parts to "inactive", ...
1697   for (i = 0; i < num_parts; i++)
1698     anim->part[i].state = ANIM_STATE_INACTIVE;
1699
1700   // ... then set current animation parts to "running"
1701   part->state = ANIM_STATE_RUNNING;
1702
1703   anim->state = HandleGlobalAnim_Part(part, anim->state);
1704
1705   if (anim->state & ANIM_STATE_RESTART)
1706     anim->part_counter++;
1707
1708   // when animation mode is "once", stop after all animations were played once
1709   if (c->anim_mode & ANIM_ONCE &&
1710       anim->part_counter == anim->num_parts)
1711     anim->state = ANIM_STATE_INACTIVE;
1712
1713   state = anim->state;
1714   active_part_nr = anim->active_part_nr;
1715
1716   // while the animation parts are pausing (waiting or inactive), play the base
1717   // (main) animation; this corresponds to the "boring player animation" logic
1718   // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1719   if (anim->has_base)
1720   {
1721     if (anim->state == ANIM_STATE_WAITING ||
1722         anim->state == ANIM_STATE_INACTIVE)
1723     {
1724       anim->active_part_nr = anim->num_parts;   // part nr of base animation
1725       part = &anim->part[anim->active_part_nr];
1726
1727       if (anim->state != anim->last_state)
1728         part->state = ANIM_STATE_RESTART;
1729
1730       anim->state = ANIM_STATE_RUNNING;
1731       part->state = HandleGlobalAnim_Part(part, part->state);
1732     }
1733   }
1734
1735   anim->last_state = state;
1736   anim->last_active_part_nr = active_part_nr;
1737 }
1738
1739 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1740 {
1741   int i;
1742
1743 #if 0
1744   Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1745 #endif
1746
1747   for (i = 0; i < ctrl->num_anims; i++)
1748     HandleGlobalAnim_Main(&ctrl->anim[i], action);
1749 }
1750
1751 static void HandleGlobalAnim(int action, int game_mode)
1752 {
1753 #if 0
1754   Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1755 #endif
1756
1757   HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1758 }
1759
1760 static void DoAnimationExt(void)
1761 {
1762   int i;
1763
1764 #if 0
1765   Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
1766 #endif
1767
1768   // global animations now synchronized with frame delay of screen update
1769   anim_sync_frame++;
1770
1771   for (i = 0; i < NUM_GAME_MODES; i++)
1772     HandleGlobalAnim(ANIM_CONTINUE, i);
1773
1774 #if 0
1775   // force screen redraw in next frame to continue drawing global animations
1776   redraw_mask = REDRAW_ALL;
1777 #endif
1778 }
1779
1780 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1781                                      int delay_type)
1782 {
1783   int delay_action =
1784     (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
1785      delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
1786      delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
1787      ANIM_DELAY_ACTION_NONE);
1788
1789   if (delay_action == ANIM_DELAY_ACTION_NONE)
1790     return;
1791
1792   PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
1793 }
1794
1795 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1796 {
1797   int event_action = (part->init_event_state ?
1798                       part->control_info.init_event_action :
1799                       part->control_info.anim_event_action);
1800
1801   if (event_action == ANIM_EVENT_ACTION_NONE)
1802     return FALSE;
1803
1804   if (event_action < MAX_IMAGE_FILES)
1805     PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
1806   else
1807     OpenURLFromHash(anim_url_hash, event_action);
1808
1809   // check if further actions are allowed to be executed
1810   if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1811     return FALSE;
1812
1813   return TRUE;
1814 }
1815
1816 static void InitGlobalAnim_Clickable(void)
1817 {
1818   int mode_nr;
1819
1820   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1821   {
1822     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1823     int anim_nr;
1824
1825     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1826     {
1827       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1828       int part_nr;
1829
1830       for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1831       {
1832         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1833
1834         if (part->triggered)
1835           part->clicked = TRUE;
1836
1837         part->triggered = FALSE;
1838         part->clickable = FALSE;
1839       }
1840     }
1841   }
1842 }
1843
1844 #define ANIM_CLICKED_RESET      0
1845 #define ANIM_CLICKED_PRESSED    1
1846 #define ANIM_CLICKED_RELEASED   2
1847
1848 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
1849 {
1850   boolean click_consumed = FALSE;
1851   boolean anything_clicked = FALSE;
1852   boolean any_part_clicked = FALSE;
1853   boolean any_event_action = FALSE;
1854   int mode_nr;
1855   int i;
1856
1857   // check game modes in reverse draw order (to stop when clicked)
1858   for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
1859   {
1860     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1861     int anim_nr;
1862
1863     // check animations in reverse draw order (to stop when clicked)
1864     for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
1865     {
1866       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1867       int part_nr;
1868
1869       // check animation parts in reverse draw order (to stop when clicked)
1870       for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
1871       {
1872         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1873
1874         if (clicked_event == ANIM_CLICKED_RESET)
1875         {
1876           part->clicked = FALSE;
1877
1878           continue;
1879         }
1880
1881         if (!part->clickable)
1882           continue;
1883
1884         if (!(part->state & ANIM_STATE_RUNNING))
1885           continue;
1886
1887         // always handle "any" click events (clicking anywhere on screen) ...
1888         if (clicked_event == ANIM_CLICKED_PRESSED &&
1889             isClickablePart(part, ANIM_EVENT_ANY))
1890         {
1891 #if DEBUG_ANIM_EVENTS
1892           Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
1893                 part->old_anim_nr + 1, part->old_nr + 1);
1894 #endif
1895
1896           anything_clicked = part->clicked = TRUE;
1897           click_consumed |= clickConsumed(part);
1898         }
1899
1900         // always handle "unclick:any" events (releasing anywhere on screen) ...
1901         if (clicked_event == ANIM_CLICKED_RELEASED &&
1902             isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
1903         {
1904 #if DEBUG_ANIM_EVENTS
1905           Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
1906                 part->old_anim_nr + 1, part->old_nr + 1);
1907 #endif
1908
1909           anything_clicked = part->clicked = TRUE;
1910           click_consumed |= clickConsumed(part);
1911         }
1912
1913         // ... but only handle the first (topmost) clickable animation
1914         if (any_part_clicked)
1915           continue;
1916
1917         if (clicked_event == ANIM_CLICKED_PRESSED &&
1918             isClickedPart(part, mx, my, TRUE))
1919         {
1920 #if 0
1921           Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
1922                 anim_nr, part_nr, part->control_info.anim_event_action);
1923 #endif
1924
1925           // after executing event action, ignore any further actions
1926           if (!any_event_action && DoGlobalAnim_EventAction(part))
1927             any_event_action = TRUE;
1928
1929           // determine if mouse clicks should be blocked from other animations
1930           any_part_clicked |= clickConsumed(part);
1931
1932           if (isClickablePart(part, ANIM_EVENT_SELF))
1933           {
1934 #if DEBUG_ANIM_EVENTS
1935             Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
1936                   part->old_anim_nr + 1, part->old_nr + 1);
1937 #endif
1938
1939             anything_clicked = part->clicked = TRUE;
1940             click_consumed |= clickConsumed(part);
1941           }
1942
1943           // determine if mouse clicks should be blocked by this animation
1944           click_consumed |= clickBlocked(part);
1945
1946           // check if this click is defined to trigger other animations
1947           InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1948                                    ANIM_EVENT_CLICK, "CLICK");
1949         }
1950       }
1951     }
1952   }
1953
1954   if (anything_clicked)
1955   {
1956     handle_click = TRUE;
1957
1958     for (i = 0; i < NUM_GAME_MODES; i++)
1959       HandleGlobalAnim(ANIM_CONTINUE, i);
1960
1961     handle_click = FALSE;
1962
1963     // prevent ignoring release event if processed within same game frame
1964     StopProcessingEvents();
1965   }
1966
1967   return (click_consumed || any_event_action);
1968 }
1969
1970 static void ResetGlobalAnim_Clickable(void)
1971 {
1972   InitGlobalAnim_Clickable();
1973 }
1974
1975 static void ResetGlobalAnim_Clicked(void)
1976 {
1977   InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
1978 }
1979
1980 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
1981 {
1982   static boolean click_consumed = FALSE;
1983   static int last_button = 0;
1984   boolean press_event;
1985   boolean release_event;
1986   boolean click_consumed_current = click_consumed;
1987
1988   if (button != 0 && force_click)
1989     last_button = 0;
1990
1991   // check if button state has changed since last invocation
1992   press_event   = (button != 0 && last_button == 0);
1993   release_event = (button == 0 && last_button != 0);
1994   last_button = button;
1995
1996   if (press_event)
1997   {
1998     click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
1999     click_consumed_current = click_consumed;
2000   }
2001
2002   if (release_event)
2003   {
2004     InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
2005     click_consumed = FALSE;
2006   }
2007
2008   return click_consumed_current;
2009 }
2010
2011 int getGlobalAnimSyncFrame(void)
2012 {
2013   return anim_sync_frame;
2014 }