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